fileManage.lib.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Symfony\Component\Filesystem\Filesystem;
  4. /**
  5. * This is the file manage library for Chamilo.
  6. * Include/require it in your code to use its functionality.
  7. * @package chamilo.library
  8. */
  9. /**
  10. * Cheks a file or a directory actually exist at this location
  11. *
  12. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  13. * @param string $file_path Path of the presume existing file or dir
  14. * @return boolean TRUE if the file or the directory exists or FALSE otherwise.
  15. */
  16. function check_name_exist($file_path)
  17. {
  18. clearstatcache();
  19. $save_dir = getcwd();
  20. if (!is_dir(dirname($file_path))) {
  21. return false;
  22. }
  23. chdir(dirname($file_path));
  24. $file_name = basename($file_path);
  25. if (file_exists($file_name)) {
  26. chdir($save_dir);
  27. return true;
  28. } else {
  29. chdir($save_dir);
  30. return false;
  31. }
  32. }
  33. /**
  34. * Deletes a file or a directory
  35. *
  36. * @author - Hugues Peeters
  37. * @param $file (String) - the path of file or directory to delete
  38. * @return boolean - true if the delete succeed, false otherwise.
  39. * @see - delete() uses check_name_exist() and removeDir() functions
  40. */
  41. function my_delete($file)
  42. {
  43. if (check_name_exist($file)) {
  44. if (is_file($file)) { // FILE CASE
  45. unlink($file);
  46. return true;
  47. } elseif (is_dir($file)) { // DIRECTORY CASE
  48. removeDir($file);
  49. return true;
  50. }
  51. } else {
  52. return false; // no file or directory to delete
  53. }
  54. }
  55. /**
  56. * Removes a directory recursively
  57. *
  58. * @returns true if OK, otherwise false
  59. *
  60. * @author Amary <MasterNES@aol.com> (from Nexen.net)
  61. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  62. *
  63. * @param string $dir directory to remove
  64. */
  65. function removeDir($dir)
  66. {
  67. if (!@$opendir = opendir($dir)) {
  68. return false;
  69. }
  70. while ($readdir = readdir($opendir)) {
  71. if ($readdir != '..' && $readdir != '.') {
  72. if (is_file($dir.'/'.$readdir)) {
  73. if (!@unlink($dir.'/'.$readdir)) {
  74. return false;
  75. }
  76. } elseif (is_dir($dir.'/'.$readdir)) {
  77. if (!removeDir($dir.'/'.$readdir)) {
  78. return false;
  79. }
  80. }
  81. }
  82. }
  83. closedir($opendir);
  84. if (!@rmdir($dir)) {
  85. return false;
  86. }
  87. return true;
  88. }
  89. /**
  90. * Return true if folder is empty
  91. * @author hubert.borderiou@grenet.fr
  92. * @param string $in_folder folder path on disk
  93. * @return int 1 if folder is empty, 0 otherwise
  94. */
  95. function folder_is_empty($in_folder)
  96. {
  97. $folder_is_empty = 0;
  98. if (is_dir($in_folder)) {
  99. $tab_folder_content = scandir($in_folder);
  100. if ((count($tab_folder_content) == 2 &&
  101. in_array(".", $tab_folder_content) &&
  102. in_array("..", $tab_folder_content)
  103. ) ||
  104. (count($tab_folder_content) < 2)
  105. ) {
  106. $folder_is_empty = 1;
  107. }
  108. }
  109. return $folder_is_empty;
  110. }
  111. /**
  112. * Renames a file or a directory
  113. *
  114. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  115. * @param string $file_path complete path of the file or the directory
  116. * @param string $new_file_name new name for the file or the directory
  117. * @return boolean true if succeed, false otherwise
  118. * @see rename() uses the check_name_exist() and php2phps() functions
  119. */
  120. function my_rename($file_path, $new_file_name) {
  121. $save_dir = getcwd();
  122. $path = dirname($file_path);
  123. $old_file_name = basename($file_path);
  124. $new_file_name = api_replace_dangerous_char($new_file_name);
  125. // If no extension, take the old one
  126. if ((strpos($new_file_name, '.') === false) && ($dotpos = strrpos($old_file_name, '.'))) {
  127. $new_file_name .= substr($old_file_name, $dotpos);
  128. }
  129. // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz'
  130. // This is useful for folder names, where otherwise '.' would be sticky
  131. // Extension PHP is not allowed, change to PHPS
  132. $new_file_name = php2phps($new_file_name);
  133. if ($new_file_name == $old_file_name) {
  134. return $old_file_name;
  135. }
  136. if (strtolower($new_file_name) != strtolower($old_file_name) && check_name_exist($path.'/'.$new_file_name)) {
  137. return false;
  138. }
  139. // On a Windows server, it would be better not to do the above check
  140. // because it succeeds for some new names resembling the old name.
  141. // But on Unix/Linux the check must be done because rename overwrites.
  142. chdir($path);
  143. $res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
  144. chdir($save_dir);
  145. return $res;
  146. }
  147. /**
  148. * Moves a file or a directory to an other area
  149. *
  150. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  151. * @param string $source the path of file or directory to move
  152. * @param string $target the path of the new area
  153. * @param bool $forceMove Whether to force a move or to make a copy (safer but slower) and then delete the original
  154. * @param bool $moveContent In some cases (including migrations), we need to move the *content* and not the folder itself
  155. * @return bool true if the move succeed, false otherwise.
  156. * @see move() uses check_name_exist() and copyDirTo() functions
  157. */
  158. function move($source, $target, $forceMove = true, $moveContent = false)
  159. {
  160. $target = realpath($target); // remove trailing slash
  161. $source = realpath($source);
  162. if (check_name_exist($source)) {
  163. $file_name = basename($source);
  164. // move onto self illegal: mv a/b/c a/b/c or mv a/b/c a/b
  165. if (strcasecmp($target, dirname($source)) === 0) {
  166. return false;
  167. }
  168. $isWindowsOS = api_is_windows_os();
  169. $canExec = function_exists('exec');
  170. /* File case */
  171. if (is_file($source)) {
  172. if ($forceMove) {
  173. if (!$isWindowsOS && $canExec) {
  174. exec('mv '.$source.' '.$target.'/'.$file_name);
  175. } else {
  176. // Try copying
  177. copy($source, $target.'/'.$file_name);
  178. unlink($source);
  179. }
  180. } else {
  181. copy($source, $target.'/'.$file_name);
  182. unlink($source);
  183. }
  184. return true;
  185. } elseif (is_dir($source)) {
  186. // move dir down will cause loop: mv a/b/ a/b/c/ not legal
  187. if (strncasecmp($target, $source, strlen($source)) == 0) {
  188. return false;
  189. }
  190. /* Directory */
  191. if ($forceMove && !$isWindowsOS && $canExec) {
  192. if ($moveContent) {
  193. $base = basename($source);
  194. $out = []; $retVal = -1;
  195. exec('mv '.$source.'/* '.$target.'/'.$base, $out, $retVal);
  196. if ($retVal !== 0) {
  197. return false; // mv should return 0 on success
  198. }
  199. exec('rm -rf '.$source);
  200. } else {
  201. $out = []; $retVal = -1;
  202. exec("mv $source $target", $out, $retVal);
  203. if ($retVal !== 0) {
  204. error_log("Chamilo error fileManage.lib.php: mv $source $target\n");
  205. return false; // mv should return 0 on success
  206. }
  207. }
  208. } else {
  209. return copyDirTo($source, $target);
  210. }
  211. return true;
  212. }
  213. } else {
  214. return false;
  215. }
  216. }
  217. /**
  218. * Moves a directory and its content to an other area
  219. *
  220. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  221. * @param string $source the path of the directory to move
  222. * @param string $destination the path of the new area
  223. * @return bool false on error
  224. */
  225. function copyDirTo($source, $destination, $move = true)
  226. {
  227. $fs = new Filesystem();
  228. if (is_dir($source)) {
  229. $fs->mkdir($destination);
  230. if (!is_dir($destination)) {
  231. error_log("Chamilo copyDirTo cannot mkdir $destination\n");
  232. return false; // could not create destination dir
  233. }
  234. $fs->mirror($source, $destination);
  235. if ($move) {
  236. $fs->remove($source);
  237. }
  238. }
  239. return true;
  240. }
  241. /**
  242. * Extracting extension of a filename
  243. *
  244. * @returns array
  245. * @param string $filename filename
  246. */
  247. function getextension($filename)
  248. {
  249. $bouts = explode('.', $filename);
  250. return array(array_pop($bouts), implode('.', $bouts));
  251. }
  252. /**
  253. * Calculation size of a directory
  254. *
  255. * @returns integer size
  256. * @param string $root path of dir to measure
  257. * @param boolean $recursive if true , include subdirectory in total
  258. */
  259. function dirsize($root, $recursive = true) {
  260. $dir = @opendir($root);
  261. $size = 0;
  262. while ($file = @readdir($dir)) {
  263. if (!in_array($file, array('.', '..'))) {
  264. if (is_dir($root.'/'.$file)) {
  265. $size += $recursive ? dirsize($root.'/'.$file) : 0;
  266. } else {
  267. $size += @filesize($root.'/'.$file);
  268. }
  269. }
  270. }
  271. @closedir($dir);
  272. return $size;
  273. }