PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezwebdav/classes/ezwebdavfileserver.php

https://github.com/lserwatka/ezpublish
PHP | 468 lines | 278 code | 61 blank | 129 comment | 37 complexity | d5b9204ec92ab21f4e5c016a9c94f722 MD5 | raw file
  1. <?php
  2. //
  3. // This is the index_webdav.php file. Manages WebDAV sessions.
  4. //
  5. // Created on: <18-Aug-2003 15:15:15 bh>
  6. //
  7. // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  8. // SOFTWARE NAME: eZ Publish
  9. // SOFTWARE RELEASE: 4.1.x
  10. // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
  11. // SOFTWARE LICENSE: GNU General Public License v2.0
  12. // NOTICE: >
  13. // This program is free software; you can redistribute it and/or
  14. // modify it under the terms of version 2.0 of the GNU General
  15. // Public License as published by the Free Software Foundation.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of version 2.0 of the GNU General
  23. // Public License along with this program; if not, write to the Free
  24. // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. // MA 02110-1301, USA.
  26. //
  27. //
  28. // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
  29. //
  30. /*!
  31. \class eZWebDAVFileServer ezwebdavfileserver.php
  32. \ingroup eZWebDAV
  33. \brief A simple file based WebDAV server
  34. Enables local file administration/management through the WebDAV interface.
  35. Usage:
  36. \code
  37. $myserver = new eZWebDAVFileServer();
  38. $myserver->processClientRequest();
  39. \endcode
  40. */
  41. // Get and return the files/dir-names that reside at a given path.
  42. function getDirEntries( $targetPath )
  43. {
  44. $files = array();
  45. // Attempt to open the target dir for listing.
  46. if ( $handle = opendir( $targetPath ) )
  47. {
  48. // For all entries in target dir: get filename.
  49. while ( false !== ( $file = readdir( $handle ) ) )
  50. {
  51. if ( $file != "." && $file != ".." )
  52. {
  53. $files[] = $file;
  54. }
  55. }
  56. closedir( $handle );
  57. }
  58. // Else: unable to open the dir for listing, bail out...
  59. else
  60. {
  61. return false;
  62. }
  63. // Return array of filenames.
  64. return $files;
  65. }
  66. // Recursively copies the contents of a directory.
  67. function copyDir( $source, $destination )
  68. {
  69. // Attempt to create destination dir.
  70. $status = eZDir::mkdir( $destination );
  71. // If no success: bail out.
  72. if ( !$status )
  73. {
  74. return false;
  75. }
  76. // Get the contents of the directory.
  77. $entries = getDirEntries( $source );
  78. // Bail if contents is unavailable.
  79. if ( $entries == false )
  80. {
  81. return false;
  82. }
  83. // Else: contents is OK:
  84. else
  85. {
  86. // Copy each and every entry:
  87. foreach ( $entries as $entry )
  88. {
  89. if ( $entry )
  90. {
  91. $from = "$source/$entry";
  92. $to = "$destination/$entry";
  93. // Is this a directory? -> special case.
  94. if ( is_dir( $from ) )
  95. {
  96. $status = copyDir( $from, $to );
  97. if (!$status)
  98. {
  99. return false;
  100. }
  101. }
  102. // Else: simple file case.
  103. else
  104. {
  105. $status = copy( $from, $to );
  106. if (!$status)
  107. {
  108. return false;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. // Phew: if we got this far then everything is OK.
  115. return true;
  116. }
  117. // Recursively deletes the contents of a directory.
  118. function delDir( $dir )
  119. {
  120. // Attempt to open the target dir.
  121. $currentDir = opendir( $dir );
  122. // Bail if unable to open dir.
  123. if ( $currentDir == false )
  124. {
  125. return false;
  126. }
  127. // Else, dir is available, do the thing:
  128. else
  129. {
  130. // For all entires in the dir:
  131. while ( false !== ( $entry = readdir( $currentDir ) ) )
  132. {
  133. // If entry is a directory and not . && .. :
  134. if ( is_dir( "$dir/$entry" ) and
  135. ( $entry != "." and $entry!="..") )
  136. {
  137. // Delete the dir.
  138. $status = deldir( "${dir}/${entry}" );
  139. // Bail if unable to delete the dir.
  140. if ( !$status )
  141. {
  142. return false;
  143. }
  144. }
  145. // Else: not dir but plain file.
  146. elseif ( $entry != "." and $entry != ".." )
  147. {
  148. // Simply unlink the file.
  149. $status = unlink( "${dir}/${entry}" );
  150. // Bail if unable to delete the file.
  151. if ( !$status )
  152. {
  153. return false;
  154. }
  155. }
  156. }
  157. }
  158. // We're finished going through the contents of the target dir.
  159. closedir( $currentDir );
  160. // Attempt to remove the target dir itself & return status (should be
  161. // OK as soon as we get this far...
  162. $status = rmdir( ${dir} );
  163. return $status;
  164. }
  165. /* getFileInfo
  166. Gathers information about a specific file,
  167. stores it in an associative array and returns it.
  168. */
  169. function getFileInfo( $dir, $file )
  170. {
  171. append_to_log( "inside getFileInfo, dir: $dir, file: $file");
  172. $realPath = $dir.'/'.$file;
  173. $fileInfo = array();
  174. $fileInfo["name"] = $file;
  175. // If the file is a directory:
  176. if ( is_dir( $realPath ) )
  177. {
  178. $fileInfo["size"] = 0;
  179. $fileInfo["mimetype"] = "httpd/unix-directory";
  180. // Get the dir's creation & modification times.
  181. $fileInfo["ctime"] = filectime( $realPath.'/.' );
  182. $fileInfo["mtime"] = filemtime( $realPath.'/.' );
  183. }
  184. // Else: The file is an actual file (not a dir):
  185. else
  186. {
  187. // Get the file's creation & modification times.
  188. $fileInfo["ctime"] = filectime( $realPath );
  189. $fileInfo["mtime"] = filemtime( $realPath );
  190. // Get the file size (bytes).
  191. $fileInfo["size"] = filesize( $realPath );
  192. // Check if the filename exists and is readable:
  193. if ( is_readable( $realPath ) )
  194. {
  195. // Attempt to get & set the MIME type.
  196. $mimeInfo = eZMimeType::findByURL( $dir . '/' . $file );
  197. $fileInfo['mimetype'] = $mimeInfo['name'];
  198. }
  199. // Non-readable? -> MIME type fallback to 'application/x-non-readable'
  200. else
  201. {
  202. $fileInfo["mimetype"] = "application/x-non-readable";
  203. }
  204. }
  205. // Return the array (hopefully containing correct info).
  206. return $fileInfo;
  207. }
  208. class eZWebDAVFileServer extends eZWebDAVServer
  209. {
  210. function eZWebDAVFileServer()
  211. {
  212. $this->eZWebDAVServer();
  213. }
  214. /*!
  215. Returns if the file \a $target exists or not
  216. */
  217. function head( $target )
  218. {
  219. // Make real path.
  220. $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
  221. append_to_log( "HEAD: realPath is $realPath");
  222. // Check if the target file/dir really exists:
  223. if ( file_exists( $realPath ) )
  224. {
  225. return eZWebDAVServer::OK_CREATED;
  226. }
  227. else
  228. {
  229. return eZWebDAVServer::FAILED_NOT_FOUND;
  230. }
  231. }
  232. /*!
  233. Renames the temp file \a $tempFile to \a $target.
  234. */
  235. function put( $target, $tempFile )
  236. {
  237. // Make real path.
  238. $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
  239. append_to_log( "PUT: realPath is $realPath" );
  240. append_to_log( "PUT: tempfile is $tempFile" );
  241. // Attempt to move the file from temp to desired location.
  242. eZFile::rename( $tempFile, $realPath );
  243. // Check status & return corresponding code:
  244. if ( $status )
  245. {
  246. append_to_log( "move of tempfile was OK" );
  247. return eZWebDAVServer::OK_CREATED;
  248. }
  249. else
  250. {
  251. append_to_log( "move of tempfile FAILED" );
  252. return eZWebDAVServer::FAILED_FORBIDDEN;
  253. }
  254. }
  255. /*!
  256. \return An information structure with the filename.
  257. */
  258. function get( $target )
  259. {
  260. $result = array();
  261. $result["data"] = false;
  262. $result["file"] = false;
  263. // Set the file.
  264. $result["file"] = $_SERVER["DOCUMENT_ROOT"] . $target;
  265. append_to_log( "GET: file is ".$result["file"]);
  266. return $result;
  267. }
  268. /*!
  269. Creates the directory \a $target
  270. */
  271. function mkcol( $target )
  272. {
  273. // Make real path.
  274. $realPath = $_SERVER["DOCUMENT_ROOT"].$target;
  275. append_to_log( "attempting to create dir: $realPath" );
  276. // Proceed only if the dir/file-name doesn't exist:
  277. if ( !file_exists( $realPath ) )
  278. {
  279. // Attempt to create the directory.
  280. $status = mkdir( $realPath );
  281. // Check status:
  282. if ( $status )
  283. {
  284. // OK:
  285. return eZWebDAVServer::OK_CREATED;
  286. }
  287. else
  288. {
  289. // No deal.
  290. return eZWebDAVServer::FAILED_FORBIDDEN;
  291. }
  292. }
  293. // Else: a dir/file with that name already exists:
  294. else
  295. {
  296. return eZWebDAVServer::FAILED_EXISTS;
  297. }
  298. }
  299. /*!
  300. Removes the directory or file \a $target
  301. */
  302. function delete( $target )
  303. {
  304. // Make real path.
  305. $realPath = $_SERVER["DOCUMENT_ROOT"] . $target;
  306. append_to_log( "attempting to DELETE: $realPath" );
  307. // Check if the file actually exists (NULL compliance).
  308. if ( file_exists( $realPath ) )
  309. {
  310. append_to_log( "File/dir exists..." );
  311. if ( is_dir( $realPath ) )
  312. {
  313. // Attempt to remove the target directory.
  314. $status = delDir( $realPath );
  315. }
  316. else
  317. {
  318. append_to_log( "File is a file..." );
  319. // Attempt to remove the file.
  320. $status = unlink( $realPath );
  321. }
  322. // Check the return code:
  323. if ( $status )
  324. {
  325. append_to_log( "delete was OK" );
  326. return eZWebDAVServer::OK;
  327. }
  328. else
  329. {
  330. append_to_log( "delete FAILED" );
  331. return eZWebDAVServer::FAILED_FORBIDDEN;
  332. }
  333. }
  334. else
  335. {
  336. return eZWebDAVServer::FAILED_NOT_FOUND;
  337. }
  338. }
  339. /*!
  340. Moves the file or directory \a $source to \a $destination by trying to rename it.
  341. */
  342. function move( $source, $destination )
  343. {
  344. append_to_log( "Source: $source Destination: $destination" );
  345. // Make real path to source and destination.
  346. $realSource = $_SERVER["DOCUMENT_ROOT"] . $source;
  347. $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
  348. append_to_log( "RealSource: $realSource RealDestination: $realDestination" );
  349. $status = eZFile::rename( $realSource, $realDestination );
  350. if ( $status )
  351. {
  352. append_to_log( "move was OK" );
  353. return eZWebDAVServer::OK_CREATED;
  354. }
  355. else
  356. {
  357. append_to_log( "move FAILED" );
  358. return eZWebDAVServer::FAILED_CONFLICT;
  359. }
  360. }
  361. /*!
  362. Copies the file or directory \a $source to \a $destination.
  363. */
  364. function copy( $source, $destination )
  365. {
  366. append_to_log( "Source: $source Destination: $destination" );
  367. ob_start(); var_dump( $_SERVER ); $m = ob_get_contents(); ob_end_clean(); append_to_log( $m );
  368. // Make real path to source and destination.
  369. $realSource = $_SERVER["DOCUMENT_ROOT"] . $source;
  370. $realDestination = $_SERVER["DOCUMENT_ROOT"] . $destination;
  371. append_to_log( "RealSource: $realSource RealDestination: $realDestination" );
  372. $status = copyDir( $realSource, $realDestination );
  373. if ( $status )
  374. {
  375. append_to_log( "copy was OK" );
  376. return eZWebDAVServer::OK_CREATED;
  377. }
  378. else
  379. {
  380. append_to_log( "copy FAILED" );
  381. return eZWebDAVServer::FAILED_CONFLICT;
  382. }
  383. }
  384. /*!
  385. Finds all files and directories in the directory \a $dir and return an element list of it.
  386. */
  387. function getCollectionContent( $dir, $depth = false, $properties = false )
  388. {
  389. $directory = dirname( $_SERVER['SCRIPT_FILENAME'] ) . $dir;
  390. $files = array();
  391. append_to_log( "inside getDirectoryContent, dir: $directory" );
  392. $handle = opendir( $directory );
  393. // For all the entries in the directory:
  394. while ( false !== ( $filename = readdir( $handle ) ) )
  395. {
  396. // Skip current and parent dirs ('.' and '..').
  397. if ( $filename == '.' or $filename == '..' )
  398. continue;
  399. $files[] = getFileInfo( $directory, $filename );
  400. append_to_log( "inside getDirectoryContent, dir: $directory, fil: $filename" );
  401. }
  402. return $files;
  403. }
  404. }
  405. ?>