CourseArchiver.class.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once 'Course.class.php';
  4. /**
  5. * Some functions to write a course-object to a zip-file and to read a course-
  6. * object from such a zip-file.
  7. * @author Bart Mollet <bart.mollet@hogent.be>
  8. * @package chamilo.backup
  9. *
  10. * @todo Use archive-folder of Chamilo?
  11. */
  12. class CourseArchiver
  13. {
  14. /**
  15. * Delete old temp-dirs
  16. */
  17. public static function clean_backup_dir()
  18. {
  19. $dir = api_get_path(SYS_ARCHIVE_PATH);
  20. if ($handle = @ opendir($dir)) {
  21. while (($file = readdir($handle)) !== false) {
  22. if ($file != "." && $file != ".." &&
  23. strpos($file, 'CourseArchiver_') === 0 &&
  24. is_dir($dir . '/' . $file)
  25. ) {
  26. rmdirr($dir . '/' . $file);
  27. }
  28. }
  29. closedir($handle);
  30. }
  31. }
  32. /**
  33. * Write a course and all its resources to a zip-file.
  34. * @return string A pointer to the zip-file
  35. */
  36. public static function write_course($course)
  37. {
  38. $perm_dirs = api_get_permissions_for_new_directories();
  39. CourseArchiver::clean_backup_dir();
  40. // Create a temp directory
  41. $tmp_dir_name = 'CourseArchiver_' . api_get_unique_id();
  42. $backup_dir = api_get_path(SYS_ARCHIVE_PATH) . $tmp_dir_name . '/';
  43. // All course-information will be stored in course_info.dat
  44. $course_info_file = $backup_dir . 'course_info.dat';
  45. $zip_dir = api_get_path(SYS_ARCHIVE_PATH);
  46. $user = api_get_user_info();
  47. $date = new DateTime(api_get_local_time());
  48. $zip_file = $user['user_id'] . '_' . $course->code . '_' . $date->format('Ymd-His') . '.zip';
  49. $php_errormsg = '';
  50. $res = @mkdir($backup_dir, $perm_dirs);
  51. if ($res === false) {
  52. //TODO set and handle an error message telling the user to review the permissions on the archive directory
  53. error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini') . ' - This error, occuring because your archive directory will not let this script write data into it, will prevent courses backups to be created', 0);
  54. }
  55. // Write the course-object to the file
  56. $fp = @fopen($course_info_file, 'w');
  57. if ($fp === false) {
  58. error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
  59. }
  60. $res = @fwrite($fp, base64_encode(serialize($course)));
  61. if ($res === false) {
  62. error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
  63. }
  64. $res = @fclose($fp);
  65. if ($res === false) {
  66. error_log(__FILE__ . ' line ' . __LINE__ . ': ' . (ini_get('track_errors') != false ? $php_errormsg : 'error not recorded because track_errors is off in your php.ini'), 0);
  67. }
  68. // Copy all documents to the temp-dir
  69. if (isset($course->resources[RESOURCE_DOCUMENT]) && is_array($course->resources[RESOURCE_DOCUMENT])) {
  70. foreach ($course->resources[RESOURCE_DOCUMENT] as $document) {
  71. if ($document->file_type == DOCUMENT) {
  72. $doc_dir = $backup_dir . $document->path;
  73. @mkdir(dirname($doc_dir), $perm_dirs, true);
  74. if (file_exists($course->path . $document->path)) {
  75. copy($course->path . $document->path, $doc_dir);
  76. }
  77. } else {
  78. @mkdir($backup_dir . $document->path, $perm_dirs, true);
  79. }
  80. }
  81. }
  82. // Copy all scorm documents to the temp-dir
  83. if (isset($course->resources[RESOURCE_SCORM]) && is_array($course->resources[RESOURCE_SCORM])) {
  84. foreach ($course->resources[RESOURCE_SCORM] as $document) {
  85. $doc_dir = dirname($backup_dir . $document->path);
  86. @mkdir($doc_dir, $perm_dirs, true);
  87. copyDirTo($course->path . $document->path, $doc_dir, false);
  88. }
  89. }
  90. // Copy calendar attachments.
  91. if (isset($course->resources[RESOURCE_EVENT]) && is_array($course->resources[RESOURCE_EVENT])) {
  92. $doc_dir = dirname($backup_dir . '/upload/calendar/');
  93. @mkdir($doc_dir, $perm_dirs, true);
  94. copyDirTo($course->path . 'upload/calendar/', $doc_dir, false);
  95. }
  96. // Copy Learning path author image.
  97. if (isset($course->resources[RESOURCE_LEARNPATH]) && is_array($course->resources[RESOURCE_LEARNPATH])) {
  98. $doc_dir = dirname($backup_dir . '/upload/learning_path/');
  99. @mkdir($doc_dir, $perm_dirs, true);
  100. copyDirTo($course->path . 'upload/learning_path/', $doc_dir, false);
  101. }
  102. // Copy announcements attachments.
  103. if (isset($course->resources[RESOURCE_ANNOUNCEMENT]) && is_array($course->resources[RESOURCE_ANNOUNCEMENT])) {
  104. $doc_dir = dirname($backup_dir . '/upload/announcements/');
  105. @mkdir($doc_dir, $perm_dirs, true);
  106. copyDirTo($course->path . 'upload/announcements/', $doc_dir, false);
  107. }
  108. // Copy work folders (only folders)
  109. if (isset($course->resources[RESOURCE_WORK]) && is_array($course->resources[RESOURCE_WORK])) {
  110. $doc_dir = dirname($backup_dir . '/upload/work/');
  111. @mkdir($doc_dir, $perm_dirs, true);
  112. // @todo: adjust to only create subdirs, but not copy files
  113. copyDirTo($course->path . 'upload/work/', $doc_dir, false);
  114. }
  115. // Zip the course-contents
  116. $zip = new PclZip($zip_dir . $zip_file);
  117. $zip->create($zip_dir . $tmp_dir_name, PCLZIP_OPT_REMOVE_PATH, $zip_dir . $tmp_dir_name . '/');
  118. //$zip->deleteByIndex(0);
  119. // Remove the temp-dir.
  120. rmdirr($backup_dir);
  121. return '' . $zip_file;
  122. }
  123. /**
  124. * @param int $user_id
  125. * @return array
  126. */
  127. public static function get_available_backups($user_id = null)
  128. {
  129. global $dateTimeFormatLong;
  130. $backup_files = array();
  131. $dirname = api_get_path(SYS_ARCHIVE_PATH) . '';
  132. if ($dir = opendir($dirname)) {
  133. while (($file = readdir($dir)) !== false) {
  134. $file_parts = explode('_', $file);
  135. if (count($file_parts) == 3) {
  136. $owner_id = $file_parts[0];
  137. $course_code = $file_parts[1];
  138. $file_parts = explode('.', $file_parts[2]);
  139. $date = $file_parts[0];
  140. $ext = isset($file_parts[1]) ? $file_parts[1] : null;
  141. if ($ext == 'zip' && ($user_id != null && $owner_id == $user_id || $user_id == null)) {
  142. $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2) . ' ' . substr($date, 9, 2) . ':' . substr($date, 11, 2) . ':' . substr($date, 13, 2);
  143. $backup_files[] = array(
  144. 'file' => $file,
  145. 'date' => $date,
  146. 'course_code' => $course_code
  147. );
  148. }
  149. }
  150. }
  151. closedir($dir);
  152. }
  153. return $backup_files;
  154. }
  155. /**
  156. * @param array $file
  157. * @return bool|string
  158. */
  159. public static function import_uploaded_file($file)
  160. {
  161. $new_filename = uniqid('') . '.zip';
  162. $new_dir = api_get_path(SYS_ARCHIVE_PATH);
  163. if (is_dir($new_dir) && is_writable($new_dir)) {
  164. move_uploaded_file($file, api_get_path(SYS_ARCHIVE_PATH).$new_filename);
  165. return $new_filename;
  166. }
  167. return false;
  168. }
  169. /**
  170. * Read a course-object from a zip-file
  171. * @param string $filename
  172. * @param boolean $delete Delete the file after reading the course?
  173. *
  174. * @return course The course
  175. * @todo Check if the archive is a correct Chamilo-export
  176. */
  177. public static function read_course($filename, $delete = false)
  178. {
  179. CourseArchiver::clean_backup_dir();
  180. // Create a temp directory
  181. $tmp_dir_name = 'CourseArchiver_' . uniqid('');
  182. $unzip_dir = api_get_path(SYS_ARCHIVE_PATH) . '' . $tmp_dir_name;
  183. @mkdir($unzip_dir, api_get_permissions_for_new_directories(), true);
  184. @copy(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename, $unzip_dir . '/backup.zip');
  185. // unzip the archive
  186. $zip = new PclZip($unzip_dir . '/backup.zip');
  187. @chdir($unzip_dir);
  188. $zip->extract(PCLZIP_OPT_TEMP_FILE_ON);
  189. // remove the archive-file
  190. if ($delete) {
  191. @unlink(api_get_path(SYS_ARCHIVE_PATH) . '' . $filename);
  192. }
  193. // read the course
  194. if (!is_file('course_info.dat')) {
  195. return new Course();
  196. }
  197. $fp = @fopen('course_info.dat', "r");
  198. $contents = @fread($fp, filesize('course_info.dat'));
  199. @fclose($fp);
  200. // CourseCopyLearnpath class appeared in Chamilo 1.8.7, it is the former Learnpath class in the "Copy course" tool.
  201. // For backward comaptibility with archives created on Chamilo 1.8.6.2 or older systems, we have to do the following:
  202. // Before unserialization, if class name "Learnpath" was found, it should be renamed as "CourseCopyLearnpath".
  203. $course = unserialize(str_replace('O:9:"Learnpath":', 'O:19:"CourseCopyLearnpath":', base64_decode($contents)));
  204. if (get_class($course) != 'Course') {
  205. return new Course();
  206. }
  207. $course->backup_path = $unzip_dir;
  208. return $course;
  209. }
  210. }