ctucx.git: tinyDAV

[php] Cal-/ CardDAV server with a simple web-GUI based on SabreDAV

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 <?php
namespace FilesPlugin;

class Plugin extends \Sabre\DAV\ServerPlugin {
	protected $server      = null;
	protected $storagePath = null;


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


	public function getPluginName() {
		return 'files';
	}

	public function getPluginInfo() {
		return [
			'name'        => $this->getPluginName(),
			'description' => 'Files support',
			'link'        => ''
		];
	}

	public function initialize(\Sabre\DAV\Server $server) {
		$this->server = $server;

		$this->server->on('propFind',    [$this, 'propFind']);
	}

	public function propFind(\Sabre\DAV\PropFind $propFind, \Sabre\DAV\INode $node) {
		$propFind->handle(
			'{DAV:}getcontenttype',
			function() use ($propFind) {
				$mimes = new \Mimey\MimeTypes;

				return $mimes->getExtension(pathinfo($propFind->getPath(), PATHINFO_EXTENSION));
			}
		);
	}
}

class FilesCollection extends \Sabre\DAVACL\FS\HomeCollection {
	public $collectionName = 'files';


	function getChildForPrincipal(array $principalInfo) {
		$owner = $principalInfo['uri'];

		$acl   = [
			[
				'privilege' => '{DAV:}read',
				'principal' => $owner,
				'protected' => true,
			],
			[
				'privilege' => '{DAV:}write',
				'principal' => $owner,
				'protected' => true,
			],
		];

		list(, $principalBaseName) = \Sabre\Uri\split($owner);

		$path = $this->storagePath.'/'.$principalBaseName;

		if (!is_dir($path)) {
			mkdir($path);
		}

		$out = new Directory($path, $acl, $owner);
		$out->setRelativePath($this->storagePath);

		return $out;
	}
}

class Directory extends \Sabre\DAVACL\FS\Collection {
	protected static $relativePath = null;


	public function getChild($name) {
		$path = $this->path.'/'.$name;

		if (!file_exists($path)) {
			throw new \Sabre\DAV\Exception\NotFound('File does not exist!');
		}

		if ('.' === $name || '..' === $name) {
			throw new \Sabre\DAV\Exception\Forbidden('Permission denied to . and ..');
		}

		if (is_dir($path)) {
			return new self($path, $this->acl, $this->owner);
		} else {
			return new \Sabre\DAVACL\FS\File($path, $this->acl, $this->owner);
		}
	}

	public function setRelativePath($relativePath) {
		self::$relativePath = $relativePath;
	}

	public static function getRelativePath() {
		return self::$relativePath;
	}

	private function getDirSize($path){
		$totalbytes = 0;
		$path       = realpath($path);

		if($path!==false && $path!='' && file_exists($path)){
			foreach(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS)) as $object){
				$totalbytes += $object->getSize();
			}
		}

		return $totalbytes;
	}
}