PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/php/main/inc/lib/fckeditor/editor/plugins/ajaxfilemanager/inc/class.file.php

https://bitbucket.org/frchico/chamilo_openshift
PHP | 436 lines | 287 code | 26 blank | 123 comment | 48 complexity | 0e1af8357b8e8f9421574b5e3a9a925f MD5 | raw file
  1. <?php
  2. /**
  3. * file modification
  4. * @author Logan Cai (cailongqun [at] yahoo [dot] com [dot] cn)
  5. * @link www.phpletter.com
  6. * @since 22/April/2007
  7. *
  8. */
  9. class file
  10. {
  11. var $fileInfo = "";
  12. var $filePath = "";
  13. var $fileStat = "";
  14. var $mask = '0775';
  15. var $debug = false;
  16. var $errors = array();
  17. /**
  18. * constructor
  19. *
  20. * @param string $path the path to a file or folder
  21. */
  22. function __construct($path = null)
  23. {
  24. if(!is_null($path))
  25. {
  26. if(file_exists($path))
  27. {
  28. $this->filePath = $path;
  29. if(is_file($this->filePath))
  30. {
  31. $this->fileStat = @stat($path);
  32. $this->fileInfo['size'] = $this->fileStat[7];
  33. $this->fileInfo['atime'] = $this->fileStat[8];
  34. $this->fileInfo['ctime'] = $this->fileStat[10];
  35. $this->fileInfo['mtime'] = $this->fileStat[9];
  36. $this->fileInfo['path'] = $path;
  37. $this->fileInfo['name'] = str_replace('_',' ',basename($path)); //for Chamilo. Prevent long file name
  38. $this->fileInfo['is_writable'] = $this->isWritable();
  39. $this->fileInfo['is_readable'] = $this->isReadable();
  40. }elseif(is_dir($this->filePath))
  41. {
  42. $this->fileStat = @stat($path);
  43. $this->fileInfo['name'] = str_replace('_',' ',basename($path)); //for Chamilo. Prevent long file name
  44. $this->fileInfo['path'] = $path;
  45. $this->fileInfo['atime'] = $this->fileStat[8];
  46. $this->fileInfo['ctime'] = $this->fileStat[10];
  47. $this->fileInfo['mtime'] = $this->fileStat[9];
  48. $this->fileInfo['is_writable'] = $this->isWritable();
  49. $this->fileInfo['is_readable'] = $this->isReadable();
  50. }
  51. }else
  52. {
  53. trigger_error('File does not exist ', E_USER_NOTICE);
  54. }
  55. }
  56. }
  57. /**
  58. * contructor
  59. *
  60. * @param string $path
  61. */
  62. function file($path=null)
  63. {
  64. $this->__construct($path);
  65. }
  66. /**
  67. * check if a file or folder writable
  68. *
  69. * @param file path $path
  70. * @return boolean
  71. */
  72. function isWritable($path=null)
  73. {
  74. $path = (is_null($path)?$this->filePath:$path);
  75. if (DIRECTORY_SEPARATOR == "\\")
  76. {
  77. $path = slashToBackslash($path);
  78. if(is_file($path))
  79. {
  80. $fp = @fopen($path,'ab');
  81. if($fp)
  82. {
  83. @fclose($fp);
  84. return true;
  85. }
  86. }elseif(is_dir($path))
  87. {
  88. $path = addTrailingSlash($path);
  89. $tmp = uniqid(time());
  90. if (@touch($path . $tmp))
  91. {
  92. @unlink($path . $tmp);
  93. return true;
  94. }
  95. }
  96. return false;
  97. }else
  98. {
  99. return @is_writable(slashToBackslash($path));
  100. }
  101. }
  102. /**
  103. * Returns true if the files is readable.
  104. *
  105. * @return boolean true if the files is readable.
  106. */
  107. function isReadable($path =null)
  108. {
  109. $path = is_null($path)?$this->filePath:$path;
  110. return @is_readable(slashToBackslash($path));
  111. }
  112. /**
  113. * change the modified time
  114. *
  115. * @param string $path
  116. * @param string $time
  117. * @return boolean
  118. */
  119. function setLastModified($path=null, $time)
  120. {
  121. $path = is_null($path)?$this->filePath:$path;
  122. $time = is_null($time)?time():$time;
  123. return @touch(slashToBackslash($path), $time);
  124. }
  125. /**
  126. * create a new folder
  127. *
  128. * @path the path for the new folder
  129. * @mask
  130. * @dirOwner
  131. * @return boolean
  132. */
  133. function mkdir($path = null, $mask=null, $dirOwner='')
  134. {
  135. $path = is_null($path)?$this->filePath:$path;
  136. if(!file_exists($path))
  137. {
  138. $mask = is_null($mask)?$this->mask:$mask;
  139. $status = @mkdir(slashToBackslash($path));
  140. if ($mask)
  141. {
  142. @chmod(slashToBackslash($path), intval($mask, 8));
  143. }
  144. if($dirOwner)
  145. {
  146. $this->chown(slashToBackslash($path), $dirOwner);
  147. }
  148. return $status;
  149. }
  150. return true;
  151. }
  152. /**
  153. * change the own of a file or folder
  154. *
  155. * @param the file path $path
  156. * @param $owner
  157. */
  158. function chown($path, $owner)
  159. {
  160. if(!empty($owner))
  161. {
  162. $owners = explode(":", $owner);
  163. if(!empty($owners[0]))
  164. @chown($path, $owners[0]);
  165. if(!empty($owners[1]))
  166. @chgrp($path, $owner[1]);
  167. }
  168. }
  169. /**
  170. * Copy a file, or recursively copy a folder and its contents
  171. * @author Aidan Lister <aidan@php.net>
  172. * @author Paul Scott
  173. * @version 1.0.1
  174. * @param string $source Source path
  175. * @param string $dest Destination path
  176. * @return bool Returns TRUE on success, FALSE on failure
  177. */
  178. function copyTo($source, $dest)
  179. {
  180. $source = removeTrailingSlash(backslashToSlash($source));
  181. $dest = removeTrailingSlash(backslashToSlash($dest));
  182. if(!file_exists($dest) || !is_dir($dest))
  183. {
  184. if(!$this->mkdir($dest))
  185. {
  186. $this->_debug('Unable to create folder (' . $dest . ")");
  187. return false;
  188. }
  189. }
  190. // Copy in to your self?
  191. if (getAbsPath($source) == getAbsPath($dest))
  192. {
  193. $this->_debug('Unable to copy itself. source: ' . getAbsPath($source) . "; dest: " . getAbsPath($dest));
  194. return false;
  195. }
  196. // Simple copy for a file
  197. if (is_file($source))
  198. {
  199. $dest = addTrailingSlash($dest) . (basename($source));
  200. if(file_exists($dest))
  201. {
  202. return false;
  203. }else {
  204. return copy($source, $dest);
  205. }
  206. }elseif(is_dir($source))
  207. {
  208. // Loop through the folder
  209. if(file_exists(addTrailingSlash($dest) . basename($source)))
  210. {
  211. return false;
  212. }else
  213. {
  214. if(!file_exists(addTrailingSlash($dest) . basename($source)) || !is_dir(addTrailingSlash($dest) . basename($source)))
  215. {
  216. if(!$this->mkdir(addTrailingSlash($dest) . basename($source)))
  217. {
  218. $this->_debug('Unable to create folder (' . addTrailingSlash($dest) . basename($source) . ")");
  219. return false;
  220. }
  221. }
  222. $handle = opendir($source);
  223. while(false !== ($readdir = readdir($handle)))
  224. {
  225. if($readdir != '.' && $readdir != '..')
  226. {
  227. $path = addTrailingSlash($source).'/'.$readdir;
  228. $this->copyTo($path, addTrailingSlash($dest) . basename($source));
  229. }
  230. }
  231. closedir($handle);
  232. return true;
  233. }
  234. }
  235. return false;
  236. }
  237. /**
  238. * get next available file name
  239. *
  240. * @param string $fileToMove the path of the file will be moved to
  241. * @param string $destFolder the path of destination folder
  242. * @return string
  243. */
  244. function getNextAvailableFileName($fileToMove, $destFolder)
  245. {
  246. $folderPath = addslashes(backslashToSlash(getParentPath($fileToMove)));
  247. $destFolder = addslashes(backslashToSlash(getParentPath($destFolder)));
  248. $finalPath = $destFolder . basename($fileToMove);
  249. if(file_exists($fileToMove))
  250. {
  251. if(is_file())
  252. {
  253. $fileExt = getFileExt($fileToMove);
  254. $fileBaseName = basename($fileToMove, '.' . $fileExt);
  255. $count = 1;
  256. while(file_exists($destFolder . $fileBaseName . $count . "." . $fileExt))
  257. {
  258. $count++;
  259. }
  260. $filePath = $destFolder . $fileBaseName . $count . "." . $fileExt;
  261. }elseif(is_dir())
  262. {
  263. $folderName = basename($fileToMove);
  264. $count = 1;
  265. while(file_exists($destFolder . $folderName . $count))
  266. {
  267. $count++;
  268. }
  269. $filePath = $destFolder . $fileBaseName . $count;
  270. }
  271. }
  272. return $finalPath;
  273. }
  274. /**
  275. * get file information
  276. *
  277. * @return array
  278. */
  279. function getFileInfo()
  280. {
  281. return $this->fileInfo;
  282. }
  283. /**
  284. * close
  285. *
  286. */
  287. function close()
  288. {
  289. $this->fileInfo = null;
  290. $this->fileStat = null;
  291. }
  292. /**
  293. * delete a file or a folder and all contents within that folder
  294. *
  295. * @param string $path
  296. * @return boolean
  297. */
  298. function delete($path = null)
  299. {
  300. $path = is_null($path)?$this->filePath:$path;
  301. if(file_exists($path))
  302. {
  303. if(is_file($path))
  304. {
  305. return @unlink($path);
  306. }elseif(is_dir($path))
  307. {
  308. return $this->__recursive_remove_directory($path);
  309. }
  310. }
  311. return false;
  312. }
  313. /**
  314. * empty a folder
  315. *
  316. * @param string $path
  317. * @return boolean
  318. */
  319. function emptyFolder($path)
  320. {
  321. $path = is_null($path)?$this->filePath:"";
  322. if(file_exists($path) && is_dir($path))
  323. {
  324. return $this->__recursive_remove_directory($path, true);
  325. }
  326. return false;
  327. }
  328. function _debug($info)
  329. {
  330. if($this->debug)
  331. {
  332. echo $info . "<br>\n";
  333. }else
  334. {
  335. $this->errors[] = $info;
  336. }
  337. }
  338. /**
  339. * recursive_remove_directory( directory to delete, empty )
  340. * expects path to directory and optional TRUE / FALSE to empty
  341. * of course PHP has to have the rights to delete the directory
  342. * you specify and all files and folders inside the directory
  343. *
  344. * to use this function to totally remove a directory, write:
  345. * recursive_remove_directory('path/to/directory/to/delete');
  346. * to use this function to empty a directory, write:
  347. * recursive_remove_directory('path/to/full_directory',TRUE);
  348. * @param string $directory
  349. * @param boolean $empty
  350. * @return boolean
  351. */
  352. function __recursive_remove_directory($directory, $empty = FALSE)
  353. {
  354. // if the path has a slash at the end we remove it here
  355. if(substr($directory,-1) == '/')
  356. {
  357. $directory = substr($directory,0,-1);
  358. }
  359. // if the path is not valid or is not a directory ...
  360. if(!file_exists($directory) || !is_dir($directory))
  361. {
  362. // ... we return false and exit the function
  363. return FALSE;
  364. // ... if the path is not readable
  365. }elseif(!is_readable($directory))
  366. {
  367. // ... we return false and exit the function
  368. return FALSE;
  369. // ... else if the path is readable
  370. }else{
  371. // we open the directory
  372. $handle = @opendir($directory);
  373. // and scan through the items inside
  374. while (FALSE !== ($item = @readdir($handle)))
  375. {
  376. // if the filepointer is not the current directory
  377. // or the parent directory
  378. if($item != '.' && $item != '..')
  379. {
  380. // we build the new path to delete
  381. $path = $directory.'/'.$item;
  382. // if the new path is a directory
  383. if(is_dir($path)) {
  384. // we call this function with the new path
  385. $this->__recursive_remove_directory($path);
  386. // if the new path is a file
  387. }else{
  388. // we remove the file
  389. @unlink($path);
  390. }
  391. }
  392. }
  393. // close the directory
  394. @closedir($handle);
  395. // if the option to empty is not set to true
  396. if (!$empty)
  397. {
  398. // try to delete the now empty directory
  399. if(!@rmdir($directory))
  400. {
  401. // return false if not possible
  402. return FALSE;
  403. }
  404. }
  405. // return success
  406. return TRUE;
  407. }
  408. }
  409. }
  410. ?>