fileManage.lib.php 8.9 KB

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