My favorites | Sign in
Project Logo
                
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
122
123
124
125
126
127
128
129
130
131
132
133
<?php

// {$Id$} //

// !!!! Make sure the Sabre directory is in the include_path !!!
// example:
// set_include_path('lib/' . PATH_SEPARATOR . get_include_path());

/*

This example demonstrates a simple way to create your own virtual filesystems.
By extending the _File and Directory classes, you can easily create a tree
based on various datasources.

The most obvious example is the filesystem itself. A more complete and documented
example can be found in:

lib/Sabre/DAV/FS/Node.php
lib/Sabre/DAV/FS/Directory.php
lib/Sabre/DAV/FS/File.php

*/

// settings
date_default_timezone_set('Canada/Eastern');
$publicDir = 'public';
$baseUri = '/';

// Files we need
require_once 'Sabre.includes.php';

class MyDirectory extends Sabre_DAV_Directory {

private $myPath;

function __construct($myPath) {

$this->myPath = $myPath;

}

function getChildren() {

$children = array();
// Loop through the directory, and create objects for each node
foreach(scandir($this->myPath) as $node) {

// Ignoring files staring with .
if ($node[0]==='.') continue;

$children[] = $this->getChild($node);

}

return $children;

}

function getChild($name) {

$path = $this->myPath . '/' . $name;

// We have to throw a FileNotFound exception if the file didn't exist
if (!file_exists($this->myPath)) throw new Sabre_DAV_Exception_FileNotFound('The file with name: ' . $name . ' could not be found');
// Some added security

if ($name[0]=='.') throw new Sabre_DAV_Exception_FileNotFound('Access denied');

if (is_dir($path)) {

return new MyDirectory($name);

} else {

return new MyFile($path);

}

}

function getName() {

return basename($this->myPath);

}

}

class MyFile extends Sabre_DAV_File {

private $myPath;

function __construct($myPath) {

$this->myPath = $myPath;

}

function getName() {

return basename($this->myPath);

}

function get() {

return fopen($this->myPath,'r');

}

function getSize() {

return filesize($this->myPath);

}

}

// Make sure there is a directory in your current directory named 'public'. We will be exposing that directory to WebDAV
$rootDir = new MyDirectory($publicDir);

// Now we create an ObjectTree, which dispatches all requests to your newly created file system
$objectTree = new Sabre_DAV_ObjectTree($rootDir);

// The object tree needs in turn to be passed to the server class
$server = new Sabre_DAV_Server($objectTree);

$server->setBaseUri($baseUri);

// And off we go!
$server->exec();

?>
Show details Hide details

Change log

r666 by evertpot on Nov 03, 2009   Diff
Fixes  Issue 15 
Go to: 
Sign in to write a code review

Older revisions

r400 by evertpot on May 21, 2009   Diff
Renamed all exception to follow the
coding standards
This is a compatibility break for a
lot of system
r371 by evertpot on Mar 29, 2009   Diff
More documentation
r215 by evertpot on Jan 29, 2009   Diff
It's more efficient to use streams for
->get, so we want to promote this best
practice.
All revisions of this file

File info

Size: 2634 bytes, 133 lines

File properties

svn:eol-style
LF
svn:keywords
Id
Hosted by Google Code