learnpath_functions.inc.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is a function library for the learning path.
  5. *
  6. * Due to the face that the learning path has been built upon the resoucelinker,
  7. * naming conventions have changed at least 2 times. You can see here in order the :
  8. * 1. name used in the first version of the resourcelinker
  9. * 2. name used in the first version of the LP
  10. * 3. name used in the second (current) version of the LP
  11. *
  12. * 1. 2. 3.
  13. * Category = Chapter = Module
  14. * Item (?) = Item = Step
  15. *
  16. * @author Denes Nagy <darkden@evk.bke.hu>, main author
  17. * @author Roan Embrechts, some code cleaning
  18. * @author Yannick Warnier <yannick.warnier@beeznest.com>, multi-level learnpath behaviour + new SCORM tool
  19. * @access public
  20. * @package chamilo.learnpath
  21. * @todo rename functions to coding conventions: not deleteitem but delete_item, etc
  22. * @todo rewrite functions to comply with phpDocumentor
  23. * @todo remove code duplication
  24. */
  25. /**
  26. * This function deletes an entire directory
  27. * @param string The directory path
  28. * @return boolean True on success, false on failure
  29. */
  30. function deldir($dir) {
  31. $dh = opendir($dir);
  32. while ($file = readdir($dh)) {
  33. if ($file != '.' && $file != '..') {
  34. $fullpath = $dir.'/'.$file;
  35. if (!is_dir($fullpath)) {
  36. unlink($fullpath);
  37. } else {
  38. deldir($fullpath);
  39. }
  40. }
  41. }
  42. closedir($dh);
  43. if (rmdir($dir)) {
  44. return true;
  45. }
  46. return false;
  47. }
  48. function rcopy($source, $dest) {
  49. //error_log($source." -> ".$dest, 0);
  50. if (!file_exists($source)) {
  51. //error_log($source." does not exist", 0);
  52. return false;
  53. }
  54. if (is_dir($source)) {
  55. //error_log($source." is a dir", 0);
  56. // This is a directory.
  57. // Remove trailing '/'
  58. if (strrpos($source, '/') == sizeof($source) - 1) {
  59. $source = substr($source, 0, size_of($source) - 1);
  60. }
  61. if (strrpos($dest, '/') == sizeof($dest) - 1) {
  62. $dest = substr($dest, 0, size_of($dest) - 1);
  63. }
  64. if (!is_dir($dest)) {
  65. $res = @mkdir($dest, api_get_permissions_for_new_directories());
  66. if ($res !== false) {
  67. return true;
  68. } else {
  69. // Remove latest part of path and try creating that.
  70. if (rcopy(substr($source, 0, strrpos($source, '/')), substr($dest, 0, strrpos($dest, '/')))) {
  71. return @mkdir($dest, api_get_permissions_for_new_directories());
  72. } else {
  73. return false;
  74. }
  75. }
  76. }
  77. return true;
  78. } else {
  79. // This is presumably a file.
  80. //error_log($source." is a file", 0);
  81. if (!@ copy($source, $dest)) {
  82. //error_log("Could not simple-copy $source", 0);
  83. $res = rcopy(dirname($source), dirname($dest));
  84. if ($res === true) {
  85. //error_log("Welcome dir created", 0);
  86. return @ copy($source, $dest);
  87. } else {
  88. return false;
  89. //error_log("Error creating path", 0);
  90. }
  91. } else {
  92. //error_log("Could well simple-copy $source", 0);
  93. return true;
  94. }
  95. }
  96. }