]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/examples/simplefsserver.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / SabreDAV / examples / simplefsserver.php
1 <?php
2
3 // !!!! Make sure the Sabre directory is in the include_path !!!
4 // example:
5 // set_include_path('lib/' . PATH_SEPARATOR . get_include_path());
6
7 /*
8
9 This example demonstrates a simple way to create your own virtual filesystems.
10 By extending the _File and Directory classes, you can easily create a tree
11 based on various datasources.
12
13 The most obvious example is the filesystem itself. A more complete and documented
14 example can be found in:
15
16 lib/Sabre/DAV/FS/Node.php
17 lib/Sabre/DAV/FS/Directory.php
18 lib/Sabre/DAV/FS/File.php
19
20 */
21
22 // settings
23 date_default_timezone_set('Canada/Eastern');
24 $publicDir = 'public';
25
26 // Files we need
27 require_once 'Sabre/autoload.php';
28
29 class MyDirectory extends Sabre_DAV_Directory {
30
31   private $myPath;
32
33   function __construct($myPath) {
34
35     $this->myPath = $myPath;
36
37   }
38
39   function getChildren() {
40
41     $children = array();
42     // Loop through the directory, and create objects for each node
43     foreach(scandir($this->myPath) as $node) {
44
45       // Ignoring files staring with .
46       if ($node[0]==='.') continue;
47
48       $children[] = $this->getChild($node);
49
50     }
51
52     return $children;
53
54   }
55
56     function getChild($name) {
57
58         $path = $this->myPath . '/' . $name;
59
60         // We have to throw a NotFound exception if the file didn't exist
61         if (!file_exists($this->myPath)) throw new Sabre_DAV_Exception_NotFound('The file with name: ' . $name . ' could not be found');
62         // Some added security
63
64         if ($name[0]=='.')  throw new Sabre_DAV_Exception_NotFound('Access denied');
65
66         if (is_dir($path)) {
67
68             return new MyDirectory($name);
69
70         } else {
71
72             return new MyFile($path);
73
74         }
75
76     }
77
78     function getName() {
79
80         return basename($this->myPath);
81
82     }
83
84 }
85
86 class MyFile extends Sabre_DAV_File {
87
88   private $myPath;
89
90   function __construct($myPath) {
91
92     $this->myPath = $myPath;
93
94   }
95
96   function getName() {
97
98       return basename($this->myPath);
99
100   }
101
102   function get() {
103
104     return fopen($this->myPath,'r');
105
106   }
107
108   function getSize() {
109
110       return filesize($this->myPath);
111
112   }
113
114 }
115
116 // Make sure there is a directory in your current directory named 'public'. We will be exposing that directory to WebDAV
117 $rootNode = new MyDirectory($publicDir);
118
119 // The rootNode needs to be passed to the server object.
120 $server = new Sabre_DAV_Server($rootNode);
121
122 // And off we go!
123 $server->exec();