<?php


class WebdavFile {

	public $davpath = '';
	public $realpath = '';
	public $is_dir = null;
	public $virtual_dirs = array();
	public $virtual_files = array();
	public $perms = 'R';
	public $prevent_scandir = false;

	function __construct($davpath) {
		$this->davpath = $davpath;
	}

	public function getRealPath() {
		return $this->realpath;
	}

	public function getIsDir() {
		return $this->is_dir;
	}

	public function getVirtualDirs() {
		return $this->virtual_dirs;
	}

	public function getVirtualFiles() {
		return $this->virtual_files;
	}

	public function getPerms() {
		return $this->perms;
	}

	public function setRealPath($realpath) {
		$this->realpath = $realpath;
	}

	public function addRealPathPart($part) {
		$this->realpath .= '/' . $part;
	}

	public function setIsDir($isDir) {
		$this->is_dir = $isDir;
	}

	public function setVirtualDirs($virtual_dirs) {
		$this->virtual_dirs = $virtual_dirs;
	}

	public function addVirtualDir($virtual_dir) {
		$this->virtual_dirs[] = $virtual_dir;
	}

	public function setVirtualFiles($virtual_files) {
		$this->virtual_files = $virtual_files;
	}

	public function addVirtualFile($virtual_file) {
		$this->virtual_files[] = $virtual_file;
	}

	public function setPerms($perms) {
		$this->perms = $perms;
	}

	public function setPreventScandir($prevent_scandir) {
		$this->prevent_scandir = $prevent_scandir;
	}

	/**
	 * @param string $davpath eg.: SE/DRUKI/1379.UMOWA.2011-06-03.umowa_abonencka.2011-06-03
	 * 
	 * @returns array $list [realname => type(dir,file,virtualdir?)]
	 */
	function getChildren() {
		$list = array();

		if ($this->is_dir === true) {
			if (!empty($this->virtual_dirs)) {
				foreach ($this->virtual_dirs as $name) {
					$list[$name] = 'dir';
				}
			}
			if (!empty($this->virtual_files)) {
				foreach ($this->virtual_files as $name) {
					$list[$name] = 'file';
				}
			}
			if (empty($this->prevent_scandir)) {
				foreach (scandir($this->realpath) as $name) {
					if (substr($name, 0, 1) == '.') continue;

					//$id = reset(explode('.', $name));
					//if (is_numeric($id)) {// TODO: check name format
					if (is_dir($this->realpath . '/' . $name)) {
						$list[$name] = 'dir';
					} else {
						$list[$name] = 'file';
					}
					//}
				}
			}
		}
		return $list;
	}

}
