WebDav.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. <?php
  2. class WebDav{
  3. public $root,$folder,$logs,$version = 1,$user,$lockfile,$ignoreFiles = array();
  4. public $on = array(
  5. 'login' => null,
  6. 'log' => null,
  7. 'edit' => null,
  8. 'move' => null,
  9. 'delete' => null,
  10. 'list' => null,
  11. 'properties' => null,
  12. 'copy' => null
  13. );
  14. public $do = array(
  15. 'edit' => null,
  16. 'move' => null,
  17. 'delete' => null,
  18. 'list' => null,
  19. 'copy' => null
  20. );
  21. public static function logPath() {
  22. return __DIR__.SLASH.'logs';
  23. }
  24. function start(){
  25. $server = $_SERVER;
  26. $method = strtoupper($server['REQUEST_METHOD']);
  27. $headers = getallheaders();
  28. $body = file_get_contents("php://input");
  29. $requestUri = explode('/',$server['REQUEST_URI']);
  30. $requestUri = array_filter($requestUri);
  31. $requestUri = '/'.implode('/',$requestUri);
  32. $server['REQUEST_URI'] = $requestUri;
  33. $log = PHP_EOL.PHP_EOL.'---------- '.$method.' --------------';
  34. $log .= PHP_EOL.'Fichier : '.$requestUri;
  35. foreach($headers as $key => $header){
  36. $log .= PHP_EOL."\t - ".$key.' : '.$header;
  37. }
  38. $log .= PHP_EOL."\t === ".'Requête : '.PHP_EOL. $body;
  39. $this->logit($log);
  40. if(!file_exists(self::logPath()))
  41. mkdir(self::logPath(),0755,true);
  42. if($method!='OPTIONS' && isset($this->do['login']) ){
  43. try{
  44. $function = $this->do['login'];
  45. $user = isset($server['PHP_AUTH_USER']) ? $server['PHP_AUTH_USER']: '';
  46. $password = isset($server['PHP_AUTH_PW']) ? $server['PHP_AUTH_PW']: '';
  47. $header = apache_request_headers();
  48. if(isset($header['Authorization']) && $header['Authorization']!=''){
  49. $authorization = explode(' ',$header['Authorization']);
  50. if(count($authorization)==2){
  51. $credentials = explode(':',base64_decode($authorization[1]));
  52. if(isset($credentials[0])) $user = $credentials[0];
  53. if(isset($credentials[1])) $password = $credentials[1];
  54. }
  55. }
  56. $user = $user =='' ? 'anonymous': $user;
  57. $password = $password =='' ? 'anonymous': $password;
  58. $this->user = $function($user,$password);
  59. }catch(Exception $e){
  60. $this->logit('REQUEST URI : '.$requestUri.PHP_EOL.'REQUEST HEADERS : '.json_encode($headers));
  61. $this->logit('Error '.self::headerFromCode(401).' for -'. $user.' :'.$e->getCode().' '.$e->getMessage() );
  62. $this->logit(json_encode($_SERVER,JSON_PRETTY_PRINT));
  63. $header = apache_request_headers();
  64. $header = $this->logit(json_encode($header,JSON_PRETTY_PRINT));
  65. $header = $this->logit(json_encode($_REQUEST,JSON_PRETTY_PRINT));
  66. self::emit_header($e->getCode());
  67. if($e->getCode()==401)
  68. header('WWW-Authenticate: Basic realm="Software Webdav gate"');
  69. echo $e->getMessage();
  70. exit();
  71. }
  72. }
  73. try{
  74. switch($method) {
  75. //Returns a list of properties of a file, a directory of a directory and its files.
  76. case 'PROPFIND':
  77. $log= "\t === ".'Réponse : '.PHP_EOL;
  78. try{
  79. $depth = isset($headers['Depth']) ? $headers['Depth'] : 0;
  80. if(in_array(mt_basename($server['REQUEST_URI']), $this->ignoreFiles)){
  81. $this->logit('File in blacklist ignored'.PHP_EOL);
  82. return self::emit_header(404);
  83. }
  84. $scanned = $this->folder.$this->get_path($server);
  85. $files = array();
  86. if(isset($this->do['list']) ){
  87. $function = $this->do['list'];
  88. $files = $function($scanned,$depth);
  89. }
  90. $locks = $this->lock();
  91. $xml = '';
  92. foreach ($files as $i=>$file) {
  93. $url = $requestUri.'/';
  94. //Si on est pas sur le premier dossier/fichier d'origine, c'est qu'on est sur un enfant du dossier, on concatene son url au dossier
  95. if($i!=0) $url .= rawurlencode($file['label']);
  96. $xml .= self::xml_file($url,$file,$locks);
  97. }
  98. $stream = '<D:multistatus xmlns:D="DAV:">'.$xml.'</D:multistatus>';
  99. self::emit_header(207);
  100. header('DAV: '.$this->version);
  101. header('Content-Type: text/xml');
  102. echo $stream;
  103. $log.= "\t".'DAV: '.$this->version.PHP_EOL;
  104. $log.= "\t\t".'Content-Type: text/xml'.PHP_EOL;
  105. $log.= "\t\t".$stream.PHP_EOL;
  106. $this->logit($log);
  107. }catch(Exception $e){
  108. $log.= "\t".$e->getCode().' : '.$e->getMessage().PHP_EOL;
  109. $this->logit($log);
  110. self::emit_header($e->getCode());
  111. }
  112. break;
  113. //Récuperation d'un fichier (ou dossier ?)
  114. case 'GET':
  115. $log= "\t === ".'Réponse : '.PHP_EOL;
  116. try{
  117. $file = $this->folder.$this->get_path($server);
  118. $this->logit('Download '.$file);
  119. if(!isset($this->do['download']) ) throw new Exception("Not implemented", 501);
  120. $function = $this->do['download'];
  121. $stream = $function($file);
  122. self::emit_header(200);
  123. header('Content-Length: '.strlen($stream));
  124. echo $stream;
  125. $log.= "\t".'- 200 OK'.PHP_EOL;
  126. $log.= "\t".'- Content-Length: '.strlen($stream);
  127. $log.= "\t\t".$stream.PHP_EOL;
  128. $this->logit($log);
  129. }catch(Exception $e){
  130. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  131. $this->logit($log);
  132. self::emit_header($e->getCode());
  133. header('Content-Type: text/xml');
  134. }
  135. break;
  136. //Envoi d'un fichier
  137. case 'PUT':
  138. $log= "\t === ".'Réponse : '.PHP_EOL;
  139. try{
  140. $target = $this->folder.$this->get_path($server);
  141. $this->logit('PUT '.$target);
  142. if(!isset($this->do['edit']) ) throw new Exception("Not implemented", 501);
  143. $function = $this->do['edit'];
  144. $function($target, $body,'file');
  145. self::emit_header(201);
  146. $log.= "\t".'- 201 Created'.PHP_EOL;
  147. $this->logit($log);
  148. }catch(Exception $e){
  149. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  150. $this->logit($log);
  151. self::emit_header($e->getCode());
  152. }
  153. break;
  154. //Suppression dossier/fichier
  155. case 'DELETE':
  156. $log= "\t === ".'Réponse : '.PHP_EOL;
  157. try{
  158. $path = $this->folder.$this->get_path($server);
  159. if(!isset($this->do['delete']) ) throw new Exception("Error Processing Request", 501);
  160. $function = $this->do['delete'];
  161. $function($path);
  162. self::emit_header(200);
  163. }catch(Exception $e){
  164. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  165. $this->logit($log);
  166. self::emit_header($e->getCode());
  167. }
  168. break;
  169. //Déplacement/renommage dossier/fichier
  170. case 'MOVE':
  171. $log= "\t === ".'Réponse : '.PHP_EOL;
  172. try{
  173. $target = $this->folder.$this->get_path($server);
  174. $toUrl = rawurldecode($server['HTTP_DESTINATION']);
  175. $toUrl = parse_url($toUrl, PHP_URL_PATH);
  176. $this->logit('Move to '.$toUrl);
  177. $to = $this->folder.str_replace('/',SLASH,utf8_decode(str_replace($this->root,'',$toUrl)));
  178. $to = substr($to, -1,1) == SLASH ? substr($to, 0,strlen($to)-1): $to;
  179. $this->logit('Move '.$target.' to '.$to.' (url :'.$toUrl.')');
  180. if(!isset($this->do['move']) ) throw new Exception("Not implemented", 501);
  181. $function = $this->do['move'];
  182. $function($target,$to);
  183. self::emit_header(200);
  184. }catch(Exception $e){
  185. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  186. $this->logit($log);
  187. self::emit_header($e->getCode());
  188. }
  189. break;
  190. //Creation de dossier
  191. case 'MKCOL':
  192. $log= "\t === ".'Réponse : '.PHP_EOL;
  193. try{
  194. $path = $this->folder.$this->get_path($server);
  195. $this->logit('Create folder '.$path);
  196. if(isset($this->do['edit']) ){
  197. $function = $this->do['edit'];
  198. $function($path, '','create','directory');
  199. self::emit_header(200);
  200. break;
  201. }
  202. self::emit_header(200);
  203. mkdir($path,0755,true);
  204. }catch(Exception $e){
  205. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  206. $this->logit($log);
  207. self::emit_header($e->getCode());
  208. }
  209. break;
  210. //The OPTIONS method allows an http client to find out what HTTP methods are supported on a specific url.
  211. case 'OPTIONS':
  212. $log= "\t === ".'Réponse : '.PHP_EOL;
  213. if (ob_get_length()) ob_end_clean();
  214. header('Allows: options get head delete propfind copy mkcol put lock unlock proppatch');
  215. $log.= "\t".'- Allows: options get head delete propfind copy mkcol put lock unlock proppatch'.PHP_EOL;
  216. $this->logit($log);
  217. break;
  218. case 'HEAD':
  219. $log= "\t === ".'Réponse : '.PHP_EOL;
  220. self::emit_header(200);
  221. $log.= "\t".'- 200 Ok'.PHP_EOL;
  222. $this->logit($log);
  223. break;
  224. case 'POST':
  225. $log= "\t === ".'Réponse : '.PHP_EOL;
  226. self::emit_header(501);
  227. $log.= "\t".'- 501 Not implemented'.PHP_EOL;
  228. $this->logit($log);
  229. break;
  230. case 'TRACE':
  231. $log= "\t === ".'Réponse : '.PHP_EOL;
  232. self::emit_header(501);
  233. $log.= "\t".'- 501 Not implemented'.PHP_EOL;
  234. $this->logit($log);
  235. break;
  236. //Updates properties of a resource or collection.
  237. case 'PROPPATCH':
  238. $log= "\t === ".'Réponse : '.PHP_EOL;
  239. $properties = array();
  240. $namespaces = array();
  241. $ns = '';
  242. preg_match_all('|xmlns:([^\=]*)="([^"]*)"|iUs', $body, $matches,PREG_SET_ORDER);
  243. foreach ($matches as $match) {
  244. if(count($match)!=3) continue;
  245. $namespaces[$match[1]] = $match[2];
  246. $ns.=' xmlns:'.$match[1].' = "'.$match[2].'" ';
  247. }
  248. preg_match('|<D:prop>(.*)<\/D:prop>|iUs', $body, $matches);
  249. if(isset($matches[1])){
  250. preg_match_all('|<([^:]*):([^\>]*)>([^\<]*)<\/\1:\2>|iUs', $matches[1], $matches,PREG_SET_ORDER);
  251. foreach ($matches as $match) {
  252. if(count($match)==4)
  253. $properties[$match[2]] = array('ns'=>$match[1],'value'=>$match[3],'key'=>$match[2]);
  254. }
  255. }
  256. $this->logit('Set properties '.json_encode($properties,JSON_PRETTY_PRINT));
  257. if(isset($this->on['properties']) ){
  258. self::emit_header(200);
  259. $function = $this->do['properties'];
  260. $function($properties,$namespaces);
  261. break;
  262. }
  263. $response = '<?xml version="1.0" encoding="utf-8"?>
  264. <d:multistatus xmlns:d="DAV:" '.$ns.'>
  265. <d:response>
  266. <d:href>/</d:href>
  267. <d:propstat>
  268. <d:status>HTTP/1.1 200 OK</d:status>
  269. <d:prop>
  270. ';
  271. foreach ($properties as $key => $value) {
  272. $response .= '<'.$value['ns'].':'.$value['key'].' />';
  273. }
  274. $response .= '
  275. </d:prop>
  276. </d:propstat>
  277. </d:response>
  278. </d:multistatus>';
  279. $this->logit('[RESPONSE]'.PHP_EOL.$response);
  280. self::emit_header(207);
  281. header('Content-Type: text/xml');
  282. echo $response;
  283. $log.= "\t".'- 207 Multi status'.PHP_EOL;
  284. $log.= "\t".'- Content-Type: text/xml'.PHP_EOL;
  285. $log.= "\t\t".$response.PHP_EOL;
  286. $this->logit($log);
  287. break;
  288. //Copie d'un élement vers un nouvel emplacement
  289. case 'COPY':
  290. $log= "\t === ".'Réponse : '.PHP_EOL;
  291. try{
  292. $target = $this->folder.$this->get_path($server);
  293. $toUrl = rawurldecode($server['HTTP_DESTINATION']);
  294. $toUrl = parse_url($toUrl, PHP_URL_PATH);
  295. $to = $this->folder.str_replace('/',SLASH,utf8_decode(str_replace($this->root,'',$toUrl)));
  296. $this->logit('Copy '.$target.' to '.$to.' (url :'.$toUrl.')');
  297. if(!isset($this->do['copy'])) throw new Exception("Not implemented", 501);
  298. $function = $this->do['copy'];
  299. $function($target,$to);
  300. self::emit_header(200);
  301. $log.= "\t".'- 200 OK'.PHP_EOL;
  302. $this->logit($log);
  303. }catch(Exception $e){
  304. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  305. $this->logit($log);
  306. self::emit_header($e->getCode());
  307. }
  308. break;
  309. //Verouillage d'un élement
  310. case 'LOCK':
  311. $log= "\t === ".'Réponse : '.PHP_EOL;
  312. try{
  313. $target = $this->folder.$this->get_path($server);
  314. $response = 200;
  315. $locks = $this->lock();
  316. //Renouvellement de lock ou demande par un autre user
  317. if(isset($locks[base64_encode($requestUri)])){
  318. $lock = $locks[base64_encode($requestUri)];
  319. if($lock['owner']!=$this->user->login) {
  320. $response = 423;
  321. }else{
  322. $properties = array(
  323. 'owner'=>$this->user->login,
  324. 'created'=>time(),
  325. 'depth'=>isset($headers['Depth']) ? $headers['Depth'] : 'Infinity',
  326. 'timeout'=>isset($headers['Timeout']) ? $headers['Timeout'] : 'Second-3600',
  327. );
  328. $lock = $this->lock($requestUri,$properties);
  329. }
  330. //Création d'un nouveau lock
  331. }else{
  332. $properties = array(
  333. 'owner'=>$this->user->login,
  334. 'created'=>time(),
  335. 'depth'=>isset($headers['Depth']) ? $headers['Depth'] : 'Infinity',
  336. 'timeout'=>isset($headers['Timeout']) ? $headers['Timeout'] : 'Second-3600',
  337. );
  338. $lock = $this->lock($requestUri,$properties);
  339. }
  340. self::emit_header($response);
  341. header('Lock-Token: <'.$lock['token'].'>');
  342. $requestUri = str_replace('&','%26',$requestUri);
  343. $xml = '<?xml version="1.0" encoding="utf-8" ?>
  344. <D:prop xmlns:D="DAV:">
  345. <D:lockdiscovery>
  346. <D:activelock>
  347. <D:locktype><D:write/></D:locktype>
  348. <D:lockscope><D:exclusive/></D:lockscope>
  349. <D:depth>'.$lock['depth'].'</D:depth>
  350. <D:owner>
  351. <D:href>'.$lock['owner'].'</D:href>
  352. </D:owner>
  353. <D:timeout>'.$lock['timeout'].'</D:timeout>
  354. <D:locktoken>
  355. <D:href>'.$lock['token'].'</D:href>
  356. </D:locktoken>
  357. <D:lockroot>
  358. <D:href>'.$requestUri.'</D:href>
  359. </D:lockroot>
  360. </D:activelock>
  361. </D:lockdiscovery>
  362. </D:prop>';
  363. echo $xml;
  364. $log.= "\t".'- '.$response.' '.self::headerFromCode($response).PHP_EOL;
  365. $log.= "\t".'- Lock-Token: <'.$lock['token'].'>'.PHP_EOL;
  366. $log.= "\t\t".$xml.PHP_EOL;
  367. $this->logit($log);
  368. }catch(Exception $e){
  369. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  370. $this->logit($log);
  371. self::emit_header($e->getCode());
  372. }
  373. break;
  374. //Déverouillage d'un élement
  375. case 'UNLOCK':
  376. $log= "\t === ".'Réponse : '.PHP_EOL;
  377. try{
  378. $target = $this->folder.$this->get_path($server);
  379. $token = isset($headers['Lock-Token']) ? $headers['Lock-Token'] : 'no-token';
  380. $token = preg_replace('|[\<\>]|is','',$token);
  381. $locks = $this->lock();
  382. if(isset($locks[base64_encode($requestUri)])){
  383. if($locks[base64_encode($requestUri)]['token'] == $token){
  384. $this->lock($requestUri,false);
  385. }
  386. }
  387. self::emit_header(204);
  388. $log.= "\t".'- 204 No Content'.PHP_EOL;
  389. $this->logit($log);
  390. }catch(Exception $e){
  391. $log.= "\t".'- '.$e->getCode().' '.$e->getMessage().PHP_EOL;
  392. $this->logit($log);
  393. self::emit_header($e->getCode());
  394. }
  395. break;
  396. }
  397. }catch(Exception $e){
  398. self::emit_header(500);
  399. }
  400. exit();
  401. }
  402. //Méthode de gestion du manifest des locks fichiers (ajout, supression, listing)
  403. // Si properties est a false : supression d'un verrou pour 'file'
  404. // Si file + properties est renseigné : Création d'un verrou pour 'file' avec ces propriétés
  405. // Si file et properties sont a vide : on retourne juste la liste des verrou non expirés
  406. public function lock($file=null,$properties = array()){
  407. if(!file_exists($this->lockfile)) file_put_contents($this->lockfile,json_encode(array()));
  408. $locks = json_decode(file_get_contents($this->lockfile),true);
  409. foreach($locks as $key=>$lock){
  410. $timeoutInfos = explode('-',$lock['timeout']);
  411. //Calcul du délais d'expiration d'un verrou
  412. $secondNumber = 60;
  413. if(count($timeoutInfos)==2){
  414. list($unity,$number) = $timeoutInfos;
  415. $secondNumber = $number;
  416. if($unity == 'Minut') $secondNumber *= 60;
  417. if($unity == 'Hour') $secondNumber *= 3600;
  418. if($unity == 'Day') $secondNumber *= 86400;
  419. }
  420. //Supression du verrou fichier si expiré
  421. if(time() <= ($secondNumber + $lock['created'])) continue;
  422. unset($locks[$key]);
  423. }
  424. //Si aucun parametre n'est spécifié, on retourne la liste des verrous
  425. if($file==null) return $locks;
  426. //Si les properties sont a false, on supprime le verrou
  427. if($properties===false){
  428. unset($locks[base64_encode($file)]);
  429. }else{
  430. //Si les properties du verrou sont renseigné, on le save
  431. $properties['token'] = isset($properties['token']) && $properties['token'] != '' ? $properties['token'] : 'fr.core.'.sha1($file);
  432. $locks[base64_encode($file)] = $properties;
  433. }
  434. file_put_contents($this->lockfile, json_encode($locks));
  435. return !$properties ? '': $locks[base64_encode($file)];
  436. }
  437. /*
  438. create_time : timestamp de création
  439. update_time : timestamp dernière modification
  440. length : taille du dossier ou du fichier
  441. type : directory/file
  442. */
  443. public static function xml_file($url,$fileInfos,$locks){
  444. if(!isset($fileInfos['create_time'])) $fileInfos['create_time'] = time();
  445. if(!isset($fileInfos['update_time'])) $fileInfos['update_time'] = time();
  446. if(!isset($fileInfos['length'])) $fileInfos['length'] = 0;
  447. $fileInfos['create_time'] = $fileInfos['create_time'] - (3600*2);
  448. $fileInfos['update_time'] = $fileInfos['update_time'] - (3600*2);
  449. $fileInfos['update_time'] = date('D, j M Y H:i:s', $fileInfos['update_time']).' GMT';
  450. $fileInfos['create_time'] = date('D, j M Y H:i:s', $fileInfos['create_time']).' GMT';
  451. $url = str_replace('&','%26',$url);
  452. $xml = '<D:response>
  453. <D:href>'.$url.'</D:href>';
  454. $xml .= '<D:propstat>
  455. <D:prop>';
  456. if($fileInfos['type']=='directory'){
  457. $xml .='<D:resourcetype>
  458. <D:collection/>
  459. </D:resourcetype>';
  460. }else{
  461. $xml .='<D:resourcetype/>';
  462. $xml .='<D:supportedlock>
  463. <D:lockentry>
  464. <D:lockscope><D:exclusive/></D:lockscope>
  465. <D:locktype><D:write/></D:locktype>
  466. </D:lockentry>
  467. </D:supportedlock>';
  468. }
  469. if(isset($locks[base64_encode($url)])){
  470. $lock = $locks[base64_encode($url)];
  471. //Envoi du verrou au client si un verrou est en place
  472. $xml .='<D:lockdiscovery>
  473. <D:activelock>
  474. <D:locktype><D:write/></D:locktype>
  475. <D:lockscope><D:exclusive/></D:lockscope>
  476. <D:depth>'.$lock['depth'].'</D:depth>
  477. <D:owner>'.$lock['owner'].'</D:owner>
  478. <D:timeout>'.$lock['timeout'].'</D:timeout>
  479. <D:locktoken>
  480. <D:href>'.$lock['token'].'</D:href>
  481. </D:locktoken>
  482. </D:activelock>
  483. </D:lockdiscovery>';
  484. }
  485. if(isset($fileInfos['length']))
  486. $xml.= '<D:getcontentlength>'.$fileInfos['length'].'</D:getcontentlength>';
  487. $xml .='
  488. <D:getetag>"'.sha1($fileInfos['update_time']).'"</D:getetag>
  489. <D:getlastmodified>'.$fileInfos['update_time'].'</D:getlastmodified>
  490. <D:creationdate>'.$fileInfos['create_time'].'</D:creationdate>
  491. </D:prop>
  492. <D:status>HTTP/1.1 200 OK</D:status>
  493. </D:propstat>';
  494. if(!isset($fileInfos['length'])){
  495. $xml .= '<D:propstat>
  496. <D:prop>
  497. <D:getcontentlength/>
  498. </D:prop>
  499. <D:status>HTTP/1.1 404 Not Found</D:status>
  500. </D:propstat>';
  501. }
  502. $xml .= '</D:response>';
  503. return $xml;
  504. }
  505. function get_path($server){
  506. $path = $this->get_parameter($server['REQUEST_URI']);
  507. $path = rawurldecode(utf8_decode($path));
  508. return str_replace('/',SLASH,$path);
  509. }
  510. public function get_parameter($parameter){
  511. $path = rawurldecode($parameter);
  512. $path = str_replace('\\', '/', $path);
  513. $path = str_replace('//', '/', $path);
  514. $path = substr($path,strlen($this->root));
  515. if(substr($path, -1)=='/') $path = substr($path, 0,-1);
  516. return $path;
  517. }
  518. function logit($message){
  519. if(isset($this->on['log']) ){
  520. $function = $this->on['log'];
  521. $function($message);
  522. }
  523. if($this->logs=='') return;
  524. if(!file_exists(dirname($this->logs))) mkdir(dirname($this->logs));
  525. file_put_contents($this->logs, $message.PHP_EOL,FILE_APPEND);
  526. }
  527. public static function emit_header($code){
  528. if (ob_get_length()) ob_end_clean();
  529. header(self::headerFromCode($code));
  530. }
  531. public static function headerFromCode($code){
  532. $codes = array(
  533. 200 => 'HTTP/1.1 200 Ok',
  534. 201 => 'HTTP/1.1 201 Created',
  535. 204 => 'HTTP/1.1 204 No Content',
  536. 207 => 'HTTP/1.1 207 Multi-Status',
  537. 401 => 'HTTP/1.0 401 Unauthorized',
  538. 403 => 'HTTP/1.1 403 Forbidden',
  539. 404 => 'HTTP/1.1 404 Not Found',
  540. 406 => 'HTTP/1.1 406 Not acceptable',
  541. 409 => 'HTTP/1.1 409 Conflict',
  542. 415 => 'HTTP/1.1 415 Unsupported Media Type',
  543. 423 => 'HTTP/1.1 423 Locked',
  544. 500 => 'HTTP/1.1 500 Internal Server Error',
  545. 501 => 'HTTP/1.1 501 Method not implemented',
  546. );
  547. return isset($codes[$code]) ? $codes[$code] : $codes[500];
  548. }
  549. }
  550. ?>