fileManage.lib.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. */
  8. /**
  9. * Cheks a file or a directory actually exist at this location.
  10. *
  11. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  12. *
  13. * @param string $file_path Path of the presume existing file or dir
  14. *
  15. * @return bool TRUE if the file or the directory exists or FALSE otherwise
  16. */
  17. function check_name_exist($file_path)
  18. {
  19. clearstatcache();
  20. $save_dir = getcwd();
  21. if (!is_dir(dirname($file_path))) {
  22. return false;
  23. }
  24. chdir(dirname($file_path));
  25. $file_name = basename($file_path);
  26. if (file_exists($file_name)) {
  27. chdir($save_dir);
  28. return true;
  29. } else {
  30. chdir($save_dir);
  31. return false;
  32. }
  33. }
  34. /**
  35. * Deletes a file or a directory.
  36. *
  37. * @author - Hugues Peeters
  38. *
  39. * @param $file (String) - the path of file or directory to delete
  40. *
  41. * @return bool - true if the delete succeed, false otherwise
  42. *
  43. * @see - delete() uses check_name_exist() and removeDir() functions
  44. */
  45. function my_delete($file)
  46. {
  47. if (check_name_exist($file)) {
  48. if (is_file($file)) { // FILE CASE
  49. unlink($file);
  50. return true;
  51. } elseif (is_dir($file)) { // DIRECTORY CASE
  52. removeDir($file);
  53. return true;
  54. }
  55. }
  56. return false; // no file or directory to delete
  57. }
  58. /**
  59. * Removes a directory recursively.
  60. *
  61. * @returns true if OK, otherwise false
  62. *
  63. * @author Amary <MasterNES@aol.com> (from Nexen.net)
  64. * @author Olivier Brouckaert <oli.brouckaert@skynet.be>
  65. *
  66. * @param string $dir directory to remove
  67. */
  68. function removeDir($dir)
  69. {
  70. if (!@$opendir = opendir($dir)) {
  71. return false;
  72. }
  73. while ($readdir = readdir($opendir)) {
  74. if ($readdir != '..' && $readdir != '.') {
  75. if (is_file($dir.'/'.$readdir)) {
  76. if (!@unlink($dir.'/'.$readdir)) {
  77. return false;
  78. }
  79. } elseif (is_dir($dir.'/'.$readdir)) {
  80. if (!removeDir($dir.'/'.$readdir)) {
  81. return false;
  82. }
  83. }
  84. }
  85. }
  86. closedir($opendir);
  87. if (!@rmdir($dir)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92. /**
  93. * Return true if folder is empty.
  94. *
  95. * @author hubert.borderiou@grenet.fr
  96. *
  97. * @param string $in_folder folder path on disk
  98. *
  99. * @return int 1 if folder is empty, 0 otherwise
  100. */
  101. function folder_is_empty($in_folder)
  102. {
  103. $folder_is_empty = 0;
  104. if (is_dir($in_folder)) {
  105. $tab_folder_content = scandir($in_folder);
  106. if ((
  107. count($tab_folder_content) == 2 &&
  108. in_array(".", $tab_folder_content) &&
  109. in_array("..", $tab_folder_content)
  110. ) ||
  111. (count($tab_folder_content) < 2)
  112. ) {
  113. $folder_is_empty = 1;
  114. }
  115. }
  116. return $folder_is_empty;
  117. }
  118. /**
  119. * Renames a file or a directory.
  120. *
  121. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  122. *
  123. * @param string $file_path complete path of the file or the directory
  124. * @param string $new_file_name new name for the file or the directory
  125. *
  126. * @return bool true if succeed, false otherwise
  127. *
  128. * @see rename() uses the check_name_exist() and php2phps() functions
  129. */
  130. function my_rename($file_path, $new_file_name)
  131. {
  132. $save_dir = getcwd();
  133. $path = dirname($file_path);
  134. $old_file_name = basename($file_path);
  135. $new_file_name = api_replace_dangerous_char($new_file_name);
  136. // If no extension, take the old one
  137. if ((strpos($new_file_name, '.') === false) && ($dotpos = strrpos($old_file_name, '.'))) {
  138. $new_file_name .= substr($old_file_name, $dotpos);
  139. }
  140. // Note: still possible: 'xx.yy' -rename-> '.yy' -rename-> 'zz'
  141. // This is useful for folder names, where otherwise '.' would be sticky
  142. // Extension PHP is not allowed, change to PHPS
  143. $new_file_name = php2phps($new_file_name);
  144. if ($new_file_name == $old_file_name) {
  145. return $old_file_name;
  146. }
  147. if (strtolower($new_file_name) != strtolower($old_file_name) && check_name_exist($path.'/'.$new_file_name)) {
  148. return false;
  149. }
  150. // On a Windows server, it would be better not to do the above check
  151. // because it succeeds for some new names resembling the old name.
  152. // But on Unix/Linux the check must be done because rename overwrites.
  153. chdir($path);
  154. $res = rename($old_file_name, $new_file_name) ? $new_file_name : false;
  155. chdir($save_dir);
  156. return $res;
  157. }
  158. /**
  159. * Moves a file or a directory to an other area.
  160. *
  161. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  162. *
  163. * @param string $source the path of file or directory to move
  164. * @param string $target the path of the new area
  165. * @param bool $forceMove Whether to force a move or to make a copy (safer but slower) and then delete the original
  166. * @param bool $moveContent In some cases (including migrations), we need to move the *content* and not the folder itself
  167. *
  168. * @return bool true if the move succeed, false otherwise
  169. *
  170. * @see move() uses check_name_exist() and copyDirTo() functions
  171. */
  172. function move($source, $target, $forceMove = true, $moveContent = false)
  173. {
  174. $target = realpath($target); // remove trailing slash
  175. $source = realpath($source);
  176. if (check_name_exist($source)) {
  177. $file_name = basename($source);
  178. // move onto self illegal: mv a/b/c a/b/c or mv a/b/c a/b
  179. if (strcasecmp($target, dirname($source)) === 0) {
  180. return false;
  181. }
  182. $isWindowsOS = api_is_windows_os();
  183. $canExec = function_exists('exec');
  184. /* File case */
  185. if (is_file($source)) {
  186. if ($forceMove) {
  187. if (!$isWindowsOS && $canExec) {
  188. exec('mv '.$source.' '.$target.'/'.$file_name);
  189. } else {
  190. // Try copying
  191. copy($source, $target.'/'.$file_name);
  192. unlink($source);
  193. }
  194. } else {
  195. copy($source, $target.'/'.$file_name);
  196. unlink($source);
  197. }
  198. return true;
  199. } elseif (is_dir($source)) {
  200. // move dir down will cause loop: mv a/b/ a/b/c/ not legal
  201. if (strncasecmp($target, $source, strlen($source)) == 0) {
  202. return false;
  203. }
  204. /* Directory */
  205. if ($forceMove && !$isWindowsOS && $canExec) {
  206. if ($moveContent) {
  207. $base = basename($source);
  208. $out = [];
  209. $retVal = -1;
  210. exec('mv '.$source.'/* '.$target.'/'.$base, $out, $retVal);
  211. if ($retVal !== 0) {
  212. return false; // mv should return 0 on success
  213. }
  214. exec('rm -rf '.$source);
  215. } else {
  216. $out = [];
  217. $retVal = -1;
  218. exec("mv $source $target", $out, $retVal);
  219. if ($retVal !== 0) {
  220. error_log("Chamilo error fileManage.lib.php: mv $source $target\n");
  221. return false; // mv should return 0 on success
  222. }
  223. }
  224. } else {
  225. return copyDirTo($source, $target);
  226. }
  227. return true;
  228. }
  229. }
  230. return false;
  231. }
  232. /**
  233. * Moves a directory and its content to an other area.
  234. *
  235. * @author Hugues Peeters <peeters@ipm.ucl.ac.be>
  236. *
  237. * @param string $source the path of the directory to move
  238. * @param string $destination the path of the new area
  239. * @param bool $move Whether we want to remove the source at the end
  240. *
  241. * @return bool false on error
  242. */
  243. function copyDirTo($source, $destination, $move = true)
  244. {
  245. $fs = new Filesystem();
  246. if (is_dir($source)) {
  247. $fs->mkdir($destination);
  248. if (!is_dir($destination)) {
  249. error_log("Chamilo copyDirTo cannot mkdir $destination\n");
  250. return false; // could not create destination dir
  251. }
  252. $fs->mirror($source, $destination);
  253. if ($move) {
  254. $fs->remove($source);
  255. }
  256. }
  257. return true;
  258. }
  259. /**
  260. * Copy a directory and its directories (not files) to an other area.
  261. *
  262. * @param string $source the path of the directory to move
  263. * @param string $destination the path of the new area
  264. *
  265. * @return bool false on error
  266. */
  267. function copyDirWithoutFilesTo($source, $destination)
  268. {
  269. $fs = new Filesystem();
  270. if (!is_dir($source)) {
  271. return false;
  272. }
  273. if (!$fs->exists($destination)) {
  274. $fs->mkdir($destination);
  275. }
  276. $dirIterator = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
  277. $iterator = new RecursiveIteratorIterator($dirIterator, RecursiveIteratorIterator::SELF_FIRST);
  278. /** @var \SplFileInfo $item */
  279. foreach ($iterator as $item) {
  280. if ($item->isFile()) {
  281. continue;
  282. }
  283. $newDir = $destination.'/'.$item->getFilename();
  284. if (!$fs->exists($newDir)) {
  285. $fs->mkdir($destination.'/'.$item->getFilename());
  286. }
  287. }
  288. return true;
  289. }
  290. /**
  291. * Extracting extension of a filename.
  292. *
  293. * @returns array
  294. *
  295. * @param string $filename filename
  296. */
  297. function getextension($filename)
  298. {
  299. $bouts = explode('.', $filename);
  300. return [array_pop($bouts), implode('.', $bouts)];
  301. }
  302. /**
  303. * Get a list of all PHP (.php) files in a given directory. Includes .tpl files.
  304. *
  305. * @param string $base_path The base path in which to find the corresponding files
  306. * @param bool $includeStatic Include static .html, .htm and .css files
  307. *
  308. * @return array
  309. */
  310. function getAllPhpFiles($base_path, $includeStatic = false)
  311. {
  312. $list = scandir($base_path);
  313. $files = [];
  314. $extensionsArray = ['.php', '.tpl'];
  315. if ($includeStatic) {
  316. $extensionsArray[] = 'html';
  317. $extensionsArray[] = '.htm';
  318. $extensionsArray[] = '.css';
  319. }
  320. foreach ($list as $item) {
  321. if (substr($item, 0, 1) == '.') {
  322. continue;
  323. }
  324. $special_dirs = [api_get_path(SYS_TEST_PATH), api_get_path(SYS_COURSE_PATH), api_get_path(SYS_LANG_PATH), api_get_path(SYS_ARCHIVE_PATH)];
  325. if (in_array($base_path.$item.'/', $special_dirs)) {
  326. continue;
  327. }
  328. if (is_dir($base_path.$item)) {
  329. $files = array_merge($files, getAllPhpFiles($base_path.$item.'/', $includeStatic));
  330. } else {
  331. //only analyse php files
  332. $sub = substr($item, -4);
  333. if (in_array($sub, $extensionsArray)) {
  334. $files[] = $base_path.$item;
  335. }
  336. }
  337. }
  338. $list = null;
  339. return $files;
  340. }