CourseArchiver.class.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // $Id: CourseArchiver.class.php 20139 2009-04-27 22:32:39Z aportugal $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2004-2008 Dokeos SPRL
  7. Copyright (c) 2003 Ghent University (UGent)
  8. Copyright (c) 2001 Universite catholique de Louvain (UCL)
  9. Copyright (c) Bart Mollet (bart.mollet@hogent.be)
  10. For a full list of contributors, see "credits.txt".
  11. The full license can be read in "license.txt".
  12. This program is free software; you can redistribute it and/or
  13. modify it under the terms of the GNU General Public License
  14. as published by the Free Software Foundation; either version 2
  15. of the License, or (at your option) any later version.
  16. See the GNU General Public License for more details.
  17. Contact address: Dokeos, rue du Corbeau, 108, B-1030 Brussels, Belgium
  18. Mail: info@dokeos.com
  19. ==============================================================================
  20. */
  21. require_once ('Course.class.php');
  22. require_once ('mkdirr.php');
  23. require_once ('rmdirr.php');
  24. require_once (api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  25. /**
  26. * Some functions to write a course-object to a zip-file and to read a course-
  27. * object from such a zip-file.
  28. * @author Bart Mollet <bart.mollet@hogent.be>
  29. * @package dokeos.backup
  30. *
  31. * @todo Use archive-folder of Dokeos?
  32. */
  33. class CourseArchiver
  34. {
  35. /**
  36. * Delete old temp-dirs
  37. */
  38. function clean_backup_dir()
  39. {
  40. $dir = api_get_path(SYS_ARCHIVE_PATH);
  41. if ($handle = @ opendir($dir))
  42. {
  43. while (($file = readdir($handle)) !== false)
  44. {
  45. if ($file != "." && $file != ".." && strpos($file,'CourseArchiver_') === 0 && is_dir($dir.'/'.$file))
  46. {
  47. rmdirr($dir.'/'.$file);
  48. }
  49. }
  50. closedir($handle);
  51. }
  52. }
  53. /**
  54. * Write a course and all its resources to a zip-file.
  55. * @return string A pointer to the zip-file
  56. */
  57. function write_course($course)
  58. {
  59. CourseArchiver::clean_backup_dir();
  60. // Create a temp directory
  61. $tmp_dir_name = 'CourseArchiver_'.uniqid('');
  62. $backup_dir = api_get_path(SYS_ARCHIVE_PATH).''.$tmp_dir_name.'/';
  63. // All course-information will be stored in course_info.dat
  64. $course_info_file = $backup_dir.'course_info.dat';
  65. $zip_dir = api_get_path(SYS_ARCHIVE_PATH).'';
  66. $user = api_get_user_info();
  67. $zip_file = $user['user_id'].'_'.$course->code.'_'.date("YmdHis").'.zip';
  68. $php_errormsg = '';
  69. $res = @mkdir($backup_dir, 0755);
  70. if($res == false)
  71. {
  72. //TODO set and handle an error message telling the user to review the permissions on the archive directory
  73. 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);
  74. }
  75. // Write the course-object to the file
  76. $fp = @fopen($course_info_file, 'w');
  77. if($fp == false)
  78. {
  79. 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);
  80. }
  81. $res = @fwrite($fp, base64_encode(serialize($course)));
  82. if($res == false)
  83. {
  84. 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);
  85. }
  86. $res = @fclose($fp);
  87. if($res == false)
  88. {
  89. 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);
  90. }
  91. // Copy all documents to the temp-dir
  92. if( is_array($course->resources[RESOURCE_DOCUMENT])) {
  93. foreach ($course->resources[RESOURCE_DOCUMENT] as $id => $document) {
  94. if ($document->file_type == DOCUMENT) {
  95. $doc_dir = $backup_dir.$document->path;
  96. mkdirr(dirname($doc_dir), 0755);
  97. if (file_exists($course->path.$document->path)) {
  98. copy($course->path.$document->path, $doc_dir);
  99. }
  100. } else {
  101. mkdirr($backup_dir.$document->path, 0755);
  102. }
  103. }
  104. }
  105. // Copy all scorm documents to the temp-dir
  106. if( is_array($course->resources[RESOURCE_SCORM]))
  107. {
  108. foreach ($course->resources[RESOURCE_SCORM] as $id => $document)
  109. {
  110. $doc_dir=dirname($backup_dir.$document->path);
  111. mkdirr($doc_dir,0755);
  112. copyDirTo($course->path.$document->path, $doc_dir, false);
  113. }
  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. *
  125. */
  126. function get_available_backups($user_id = null)
  127. {
  128. global $dateTimeFormatLong;
  129. $backup_files = array();
  130. $dirname = api_get_path(SYS_ARCHIVE_PATH).'';
  131. if ($dir = opendir($dirname)) {
  132. while (($file = readdir($dir)) !== false) {
  133. $file_parts = explode('_',$file);
  134. if(count($file_parts) == 3)
  135. {
  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 = $file_parts[1];
  141. if($ext == 'zip' && ($user_id != null && $owner_id == $user_id || $user_id == null) )
  142. {
  143. $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);
  144. $backup_files[] = array('file' => $file, 'date' => $date, 'course_code' => $course_code);
  145. }
  146. }
  147. }
  148. closedir($dir);
  149. }
  150. return $backup_files;
  151. }
  152. /**
  153. *
  154. */
  155. function import_uploaded_file($file)
  156. {
  157. $new_filename = uniqid('').'.zip';
  158. $new_dir = api_get_path(SYS_ARCHIVE_PATH);
  159. if(is_dir($new_dir) && is_writable($new_dir))
  160. {
  161. move_uploaded_file($file,api_get_path(SYS_ARCHIVE_PATH).''.$new_filename);
  162. return $new_filename;
  163. }
  164. return false;
  165. }
  166. /**
  167. * Read a course-object from a zip-file
  168. * @return course The course
  169. * @param boolean $delete Delete the file after reading the course?
  170. * @todo Check if the archive is a correct Dokeos-export
  171. */
  172. function read_course($filename,$delete = false)
  173. {
  174. CourseArchiver::clean_backup_dir();
  175. // Create a temp directory
  176. $tmp_dir_name = 'CourseArchiver_'.uniqid('');
  177. $unzip_dir = api_get_path(SYS_ARCHIVE_PATH).''.$tmp_dir_name;
  178. mkdirr($unzip_dir,0755);
  179. @copy(api_get_path(SYS_ARCHIVE_PATH).''.$filename,$unzip_dir.'/backup.zip');
  180. // unzip the archive
  181. $zip = new PclZip($unzip_dir.'/backup.zip');
  182. @chdir($unzip_dir);
  183. $zip->extract();
  184. // remove the archive-file
  185. if($delete)
  186. {
  187. @unlink(api_get_path(SYS_ARCHIVE_PATH).''.$filename);
  188. }
  189. // read the course
  190. if(!is_file('course_info.dat'))
  191. {
  192. return new Course();
  193. }
  194. $fp = @fopen('course_info.dat', "r");
  195. $contents = @fread($fp, filesize('course_info.dat'));
  196. @fclose($fp);
  197. $course = unserialize(base64_decode($contents));
  198. if( get_class($course) != 'Course')
  199. {
  200. return new Course();
  201. }
  202. $course->backup_path = $unzip_dir;
  203. return $course;
  204. }
  205. }
  206. ?>