-
Notifications
You must be signed in to change notification settings - Fork 0
/
webdav.php
executable file
·240 lines (178 loc) · 6.61 KB
/
webdav.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/*
* ByteHoard 2.1
* Copyright (c) Andrew Godwin & contributors 2004
*
* webdav.php
* $Id$
*
*/
# WebDAV Server file
define("IN_BH", 1);
# Get includes
require "includes/configfunc.inc.php"; # Config functions
bh_checkconfig(); # Check the config exists, or don't bother with the rest.
require "config.inc.php"; # Load config file
require "includes/db/".$dbconfig['dbmod']; # Database functions (TODO: make user-selectable)
bh_loadconfig(); # Load configuration
require "includes/auth/".$bhconfig['authmodule']; # Authentication functions
require "langs/".$bhconfig['lang'].".lang.php"; # Language File
require "includes/filesystem/".$bhconfig['filesystemmodule']."/filesystem.inc.php"; # Filesystem functions
require "includes/filesystem/".$bhconfig['filesystemmodule']."/mimetype.inc.php"; # Mimetype functions
require "includes/filesystem/".$bhconfig['filesystemmodule']."/thumbnails.inc.php"; # Thumbnail functions
require "layouts/".$bhconfig['layout']."/main.inc.php"; # Layout file
require "includes/log.inc.php"; # Logging functions
require "includes/users.inc.php"; # User functions
require "includes/modules.inc.php"; # Module functions
require "includes/detect.inc.php"; # Detection functions
require "includes/bandwidth.inc.php"; # Bandwidth functions
require "includes/webdav/server.php";
class webdav extends HTTP_WebDAV_Server {
# Constructor - Initialises settings, etc.
function webdav() {
global $bhconfig;
parent::HTTP_WebDAV_Server();
$this->http_auth_realm = $bhconfig['sitename'];
}
function check_auth($type, $username, $password) {
global $bhconfig, $bhcurrent, $bhsession;
$bhcurrent['in_webdav'] = 1;
if (bh_authenticate($username, $password) == false) {
$bhcurrent['username'] = "guest";
$bhsession['username'] = "guest";
$bhcurrent['usertype'] = "guest";
return false;
} else {
$bhcurrent['username'] = $username;
$bhsession['username'] = $username;
$bhcurrent['userobj'] = new bhuser($username);
$bhcurrent['usertype'] = $bhcurrent['userobj']->type;
return true;
}
}
function GET(&$options) {
global $bhsession;
$fileexist = bh_user_file_exists($options['path']);
if (!$fileexist) {
return "404 Not found";
}
$fileobj = new bhfile($options['path']);
$options['path'] = $options['path'];
if ($fileobj->is_dir() == 1) {
$options['mimetype'] = "directory";
} else {
$options['mimetype'] = $fileobj->mimetype();
}
$options['mtime'] = $fileobj->fileinfo['createdate'];
$options['size'] = $fileobj->fileinfo['filesize'];
$options['stream'] = $fileobj->filestream();
return true;
}
function PUT(&$options) {
global $bhsession;
#$fn = fopen("wdlog", "w");
#fwrite($fn, print_r($options, true));
#fclose($fn);
$newfilepath = bh_fpclean($options['path']);
if (bh_checkrights(bh_get_parent($newfilepath), $bhsession['username']) >= 2) {
$newfileobj = new bhfile($newfilepath);
$newfileobj->filefromstream($options['stream']);
return '201 Created';
} else {
$newfileobj = new bhfile($newfilepath);
$newfileobj->filefromstream($options['stream']);
return '201 Created';
}
}
function DELETE(&$options) {
global $bhsession;
$fileexist = bh_user_file_exists($options['path']);
a($options['path']);
if (!$fileexist) {
return "404 Not found";
}
$fileobj = new bhfile($options['path']);
$fileobj->smartdeletefile();
return "204 No Content";
}
function MKCOL(&$options) {
global $bhsession;
$filepath = bh_fpclean($options['path']);
$infolder = bh_get_parent($filepath);
if (bh_checkrights(bh_fpclean($infolder), $bhsession['username']) >= 2) {
bh_mkdir($filepath);
$fileobj = new bhfile($filepath);
unset($fileobj);
return "201 Created";
} else {
return "403 Forbidden";
}
}
function MOVE(&$options) {
global $bhsession;
$destfilepath = bh_fpclean($options['dest']);
$filepath = bh_fpclean($options['path']);
$infolder = bh_get_parent($destfilepath);
$fileexist = bh_user_file_exists($filepath);
if (!$fileexist) { return "404 Not Found"; }
if (bh_checkrights(bh_fpclean($infolder), $bhsession['username']) <= 1) { a($infolder); return "403 Forbidden"; }
$fileobj = new bhfile($filepath);
$fileobj->copyto($destfilepath);
$fileobj->smartdeletefile();
return "201 Created";
}
function COPY(&$options) {
global $bhsession;
$destfilepath = bh_fpclean($options['dest']);
$filepath = bh_fpclean($options['path']);
$infolder = bh_get_parent($destfilepath);
$fileexist = bh_user_file_exists($filepath);
if (!$fileexist) { return "404 Not Found"; }
if (bh_checkrights(bh_fpclean($infolder), $bhsession['username']) <= 1) { return "403 Forbidden"; }
$fileobj = new bhfile($filepath);
$fileobj->copyto($destfilepath);
return "204 No Content";
}
function PROPFIND(&$options, &$files) {
global $bhsession;
$filepath = bh_fpclean($options['path']);
$fileobj = new bhfile($filepath);
$files['files'][] = $this->fileinfo($options['path']);
if ($fileobj->is_dir() == 1) {
$filelist = $fileobj->loadfile();
foreach ($filelist as $afile) {
$files['files'][] = $this->fileinfo($afile['filepath']);
}
}
return "200 OK";
}
function fileinfo($filepath) {
$return = array();
$filepath = bh_fpclean($filepath);
$fileobj = new bhfile($filepath);
$filename = bh_get_filename($filepath);
$return['path'] = utf8_encode($filepath);
$return['props'][] = $this->mkprop("getdisplayname", $filepath);
$return['props'][] = $this->mkprop("displayname", $filepath);
$return['props'][] = $this->mkprop("creationdate", $fileobj->fileinfo['createdate']);
if (!empty($fileobj->fileinfo['moddate'])) {
$return['props'][] = $this->mkprop("getlastmodified", $fileobj->fileinfo['moddate']);
} else {
$return['props'][] = $this->mkprop("getlastmodified", $fileobj->fileinfo['createdate']);
}
$return['props'][] = $this->mkprop("getcontentlength", $fileobj->fileinfo['filesize']);
if ($fileobj->is_dir() == 1) {
$return['props'][] = $this->mkprop('getcontenttype', "directory");
#$return['props'][] = $this->mkprop('contenttype', "directory");
$return['props'][] = $this->mkprop('resourcetype', 'collection');
} else {
$return['props'][] = $this->mkprop('getcontenttype', $fileobj->mimetype());
#$return['props'][] = $this->mkprop('contenttype', $fileobj->mimetype());
$return['props'][] = $this->mkprop('resourcetype', '');
}
return $return;
}
}
function a($a) { $fn = fopen('debuglog', 'a'); fputs($fn, "\n".time().":".$a); fclose($fn); };
$webdav = new webdav();
$webdav->ServeRequest();