CourseArchiver.class.php 9.5 KB

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