fileManage.lib.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 ((
  101. count($tab_folder_content) == 2 &&
  102. in_array(".", $tab_folder_content) &&
  103. in_array("..", $tab_folder_content)
  104. ) ||
  105. (count($tab_folder_content) < 2)
  106. ) {
  107. $folder_is_empty = 1;
  108. }
  109. }
  110. return $folder_is_empty;
  111. }
  112. /**
  113. * Renames a file or a directory
  114. *
  115. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  116. * @param string $file_path complete path of the file or the directory
  117. * @param string $new_file_name new name for the file or the directory
  118. * @return boolean true if succeed, false otherwise
  119. * @see rename() uses the check_name_exist() and php2phps() functions
  120. */
  121. function my_rename($file_path, $new_file_name)
  122. {
  123. $save_dir = getcwd();
  124. $path = dirname($file_path);
  125. $old_file_name = basename($file_path);
  126. $new_file_name = api_replace_dangerous_char($new_file_name);
  127. // If no extension, take the old one
  128. if ((strpos($new_file_name, '.') === false) && ($dotpos = strrpos($old_file_name, '.'))) {
  129. $new_file_name .= substr($old_file_name, $dotpos);
  130. }
  131. // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz'
  132. // This is useful for folder names, where otherwise '.' would be sticky
  133. // Extension PHP is not allowed, change to PHPS
  134. $new_file_name = php2phps($new_file_name);
  135. if ($new_file_name == $old_file_name) {
  136. return $old_file_name;
  137. }
  138. if (strtolower($new_file_name) != strtolower($old_file_name) && check_name_exist($path.'/'.$new_file_name)) {
  139. return false;
  140. }
  141. // On a Windows server, it would be better not to do the above check
  142. // because it succeeds for some new names resembling the old name.
  143. // But on Unix/Linux the check must be done because rename overwrites.
  144. chdir($path);
  145. $res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
  146. chdir($save_dir);
  147. return $res;
  148. }
  149. /**
  150. * Moves a file or a directory to an other area
  151. *
  152. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  153. * @param string $source the path of file or directory to move
  154. * @param string $target the path of the new area
  155. * @param bool $forceMove Whether to force a move or to make a copy (safer but slower) and then delete the original
  156. * @param bool $moveContent In some cases (including migrations), we need to move the *content* and not the folder itself
  157. * @return bool true if the move succeed, false otherwise.
  158. * @see move() uses check_name_exist() and copyDirTo() functions
  159. */
  160. function move($source, $target, $forceMove = true, $moveContent = false)
  161. {
  162. $target = realpath($target); // remove trailing slash
  163. $source = realpath($source);
  164. if (check_name_exist($source)) {
  165. $file_name = basename($source);
  166. // move onto self illegal: mv a/b/c a/b/c or mv a/b/c a/b
  167. if (strcasecmp($target, dirname($source)) === 0) {
  168. return false;
  169. }
  170. $isWindowsOS = api_is_windows_os();
  171. $canExec = function_exists('exec');
  172. /* File case */
  173. if (is_file($source)) {
  174. if ($forceMove) {
  175. if (!$isWindowsOS && $canExec) {
  176. exec('mv '.$source.' '.$target.'/'.$file_name);
  177. } else {
  178. // Try copying
  179. copy($source, $target.'/'.$file_name);
  180. unlink($source);
  181. }
  182. } else {
  183. copy($source, $target.'/'.$file_name);
  184. unlink($source);
  185. }
  186. return true;
  187. } elseif (is_dir($source)) {
  188. // move dir down will cause loop: mv a/b/ a/b/c/ not legal
  189. if (strncasecmp($target, $source, strlen($source)) == 0) {
  190. return false;
  191. }
  192. /* Directory */
  193. if ($forceMove && !$isWindowsOS && $canExec) {
  194. if ($moveContent) {
  195. $base = basename($source);
  196. $out = [];
  197. $retVal = -1;
  198. exec('mv '.$source.'/* '.$target.'/'.$base, $out, $retVal);
  199. if ($retVal !== 0) {
  200. return false; // mv should return 0 on success
  201. }
  202. exec('rm -rf '.$source);
  203. } else {
  204. $out = [];
  205. $retVal = -1;
  206. exec("mv $source $target", $out, $retVal);
  207. if ($retVal !== 0) {
  208. error_log("Chamilo error fileManage.lib.php: mv $source $target\n");
  209. return false; // mv should return 0 on success
  210. }
  211. }
  212. } else {
  213. return copyDirTo($source, $target);
  214. }
  215. return true;
  216. }
  217. } else {
  218. return false;
  219. }
  220. }
  221. /**
  222. * Moves a directory and its content to an other area
  223. *
  224. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  225. * @param string $source the path of the directory to move
  226. * @param string $destination the path of the new area
  227. * @return bool false on error
  228. */
  229. function copyDirTo($source, $destination, $move = true)
  230. {
  231. $fs = new Filesystem();
  232. if (is_dir($source)) {
  233. $fs->mkdir($destination);
  234. if (!is_dir($destination)) {
  235. error_log("Chamilo copyDirTo cannot mkdir $destination\n");
  236. return false; // could not create destination dir
  237. }
  238. $fs->mirror($source, $destination);
  239. if ($move) {
  240. $fs->remove($source);
  241. }
  242. }
  243. return true;
  244. }
  245. /**
  246. * Copy a directory and its directories (not files) to an other area
  247. *
  248. * @param string $source the path of the directory to move
  249. * @param string $destination the path of the new area
  250. * @return bool false on error
  251. */
  252. function copyDirWithoutFilesTo($source, $destination)
  253. {
  254. $fs = new Filesystem();
  255. if (!is_dir($source)) {
  256. return false;
  257. }
  258. if (!$fs->exists($destination)) {
  259. $fs->mkdir($destination);
  260. }
  261. $dirIterator = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
  262. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);
  263. /** @var \SplFileInfo $item */
  264. foreach ($iterator as $item) {
  265. if ($item->isFile()) {
  266. continue;
  267. }
  268. $newDir = $destination.'/'.$item->getFilename();
  269. if (!$fs->exists($newDir)) {
  270. $fs->mkdir($destination.'/'.$item->getFilename());
  271. }
  272. }
  273. return true;
  274. }
  275. /**
  276. * Extracting extension of a filename
  277. *
  278. * @returns array
  279. * @param string $filename filename
  280. */
  281. function getextension($filename)
  282. {
  283. $bouts = explode('.', $filename);
  284. return [array_pop($bouts), implode('.', $bouts)];
  285. }