PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/ezwebdav/classes/ezwebdavfileserver.php

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