2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2004 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
15 require_once(AT_INCLUDE_PATH.'classes/zipfile.class.php');
22 * Class for creating and managing course backups
24 * @author Joel Kronenberg
30 // number of backups in the backup dir
34 // the current course id
38 // where to store the backup
45 // the backup zipfile Object
48 // the timestamp for the zip files
52 // array of installed modules that support backups
58 function Backup(&$db, $course_id = 0) {
62 $this->setCourseID($course_id);
66 // should be used by the admin section
67 function setCourseID($course_id) {
68 $this->course_id = $course_id;
69 $this->backup_dir = AT_BACKUP_DIR . $course_id . DIRECTORY_SEPARATOR;
75 function generateFileName( ) {
76 global $system_courses;
77 $title = $system_courses[$this->course_id]['title'];
79 $title = str_replace(' ', '_', $title);
80 $title = str_replace('%', '', $title);
81 $title = str_replace('\'', '', $title);
82 $title = str_replace('"', '', $title);
83 $title = str_replace('`', '', $title);
85 $title .= '_' . date('d_M_y') . '.zip';
91 // NOTE: should the create() deal with saving it to disk as well? or should it be general to just create it, and not actually
92 // responsible for where to save it? (write a diff method to save it after)
93 function create($description) {
94 global $addslashes, $moduleFactory;
96 if ($this->getNumAvailable() >= AT_COURSE_BACKUPS) {
102 $zipfile =& new zipfile();
104 $package_identifier = VERSION."\n\n\n".'Do not change the first line of this file it contains the ATutor version this backup was created with.';
105 $zipfile->add_file($package_identifier, 'atutor_backup_version', $timestamp);
107 $modules = $moduleFactory->getModules(AT_MODULE_STATUS_ENABLED | AT_MODULE_STATUS_DISABLED);
108 $keys = array_keys($modules);
109 foreach($keys as $module_name) {
110 $module =& $modules[$module_name];
111 $module->backup($this->course_id, $zipfile);
115 $system_file_name = md5($timestamp);
117 if (!is_dir(AT_BACKUP_DIR)) {
118 @mkdir(AT_BACKUP_DIR);
121 if (!is_dir(AT_BACKUP_DIR . $this->course_id)) {
122 @mkdir(AT_BACKUP_DIR . $this->course_id);
125 $zipfile->write_file(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $system_file_name . '.zip');
127 $row['description'] = $addslashes($description);
128 $row['contents'] = addslashes(serialize($table_counters));
129 $row['system_file_name'] = $system_file_name;
130 $row['file_size'] = $zipfile->get_size();
131 $row['file_name'] = $this->generateFileName();
139 function upload($_FILES, $description) {
142 $ext = pathinfo($_FILES['file']['name']);
143 $ext = $ext['extension'];
145 if (!$_FILES['file']['name'] || !is_uploaded_file($_FILES['file']['tmp_name']) || ($ext != 'zip')) {
146 if ($_FILES['file']['error'] == 1) { // LEQ to UPLOAD_ERR_INI_SIZE
147 $errors = array('FILE_TOO_BIG', ini_get('upload_max_filesize'));
148 $msg->addError($errors);
150 $msg->addError('FILE_NOT_SELECTED');
154 if ($_FILES['file']['size'] == 0) {
155 $msg->addError('IMPORTFILE_EMPTY');
158 if($msg->containsErrors()) {
163 $row['description'] = $description;
164 $row['system_file_name'] = md5(time());
165 $row['contents'] = '';
166 $row['file_size'] = $_FILES['file']['size'];
167 $row['file_name'] = $_FILES['file']['name'];
169 if (!is_dir(AT_BACKUP_DIR)) {
170 @mkdir(AT_BACKUP_DIR);
173 if (!is_dir(AT_BACKUP_DIR . $this->course_id)) {
174 @mkdir(AT_BACKUP_DIR . $this->course_id);
177 $backup_path = AT_BACKUP_DIR . DIRECTORY_SEPARATOR . $this->course_id . DIRECTORY_SEPARATOR;
179 move_uploaded_file($_FILES['file']['tmp_name'], $backup_path . $row['system_file_name'].'.zip');
187 // adds a backup to the database
189 $sql = "INSERT INTO ".TABLE_PREFIX."backups VALUES (NULL, $this->course_id, NOW(), '$row[description]', '$row[file_size]', '$row[system_file_name]', '$row[file_name]', '$row[contents]')";
190 mysql_query($sql, $this->db);
194 // get number of backups
195 function getNumAvailable() {
196 // use $num_backups, if not set then do a COUNT(*) on the table
197 if (isset($this->num_backups)) {
198 return $this->num_backups;
201 $sql = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."backups WHERE course_id=$this->course_id";
202 $result = mysql_query($sql, $this->db);
203 $row = mysql_fetch_assoc($result);
205 $this->num_backups = $row['cnt'];
210 // get list of backups
211 function getAvailableList() {
212 $backup_list = array();
214 $sql = "SELECT *, UNIX_TIMESTAMP(date) AS date_timestamp FROM ".TABLE_PREFIX."backups WHERE course_id=$this->course_id ORDER BY date DESC";
215 $result = mysql_query($sql, $this->db);
216 while ($row = mysql_fetch_assoc($result)) {
217 $backup_list[$row['backup_id']] = $row;
218 $backup_list[$row['backup_id']]['contents'] = unserialize($row['contents']);
221 $this->num_backups = count($backup_list);
227 function download($backup_id) { // or fetch()
228 $list = $this->getAvailableList($this->course_id);
229 if (!isset($list[$backup_id])) {
231 //debug('does not belong to us');
235 $my_backup = $list[$backup_id];
236 $file_name = $my_backup['file_name'];
238 header('Content-Type: application/zip');
239 header('Content-transfer-encoding: binary');
240 header('Content-Disposition: attachment; filename="'.htmlspecialchars($file_name).'"');
241 header('Expires: 0');
242 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
243 header('Pragma: public');
244 header('Content-Length: '.$my_backup['file_size']);
246 // see the note in get.php about the use of x-Sendfile
248 header("Content-Encoding: none");
249 header('x-Sendfile: ' . AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $my_backup['system_file_name']. '.zip');
250 header('x-Sendfile: ', TRUE); // if we get here then it didn't work
252 readfile(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $my_backup['system_file_name']. '.zip');
257 function delete($backup_id) {
258 $list = $this->getAvailableList($this->course_id);
259 if (!isset($list[$backup_id])) {
261 //debug('does not belong to us');
264 $my_backup = $list[$backup_id];
266 // delete the backup file:
267 @unlink(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $my_backup['system_file_name']. '.zip');
269 // delete the row in the table:
270 $sql = "DELETE FROM ".TABLE_PREFIX."backups WHERE backup_id=$backup_id AND course_id=$this->course_id";
271 $result = mysql_query($sql, $this->db);
275 function edit($backup_id, $description) {
276 // update description in the table:
277 $sql = "UPDATE ".TABLE_PREFIX."backups SET description='$description', date=date WHERE backup_id=$backup_id AND course_id=$this->course_id";
278 $result = mysql_query($sql, $this->db);
283 function getRow($backup_id, $course_id = 0) {
285 $sql = "SELECT *, UNIX_TIMESTAMP(date) AS date_timestamp FROM ".TABLE_PREFIX."backups WHERE backup_id=$backup_id AND course_id=$course_id";
287 $sql = "SELECT *, UNIX_TIMESTAMP(date) AS date_timestamp FROM ".TABLE_PREFIX."backups WHERE backup_id=$backup_id AND course_id=$this->course_id";
290 $result = mysql_query($sql, $this->db);
291 $row = mysql_fetch_assoc($result);
294 $row['contents'] = unserialize($row['contents']);
300 function translate_whitespace($input) {
301 $input = str_replace('\n', "\n", $input);
302 $input = str_replace('\r', "\r", $input);
303 $input = str_replace('\x00', "\0", $input);
309 function getVersion() {
310 if ((file_exists($this->import_dir.'atutor_backup_version')) && ($version = file($this->import_dir.'atutor_backup_version'))) {
311 return trim($version[0]);
318 function restore($material, $action, $backup_id, $from_course_id = 0) {
319 global $moduleFactory;
320 require_once(AT_INCLUDE_PATH.'classes/pclzip.lib.php');
321 require_once(AT_INCLUDE_PATH.'lib/filemanager.inc.php');
323 if (!$from_course_id) {
324 $from_course_id = $this->course_id;
327 // 1. get backup row/information
328 $my_backup = $this->getRow($backup_id, $from_course_id);
330 @mkdir(AT_CONTENT_DIR . 'import/' . $this->course_id);
331 $this->import_dir = AT_CONTENT_DIR . 'import/' . $this->course_id . '/';
333 // 2. extract the backup
334 $archive = new PclZip(AT_BACKUP_DIR . $from_course_id. '/' . $my_backup['system_file_name']. '.zip');
335 if ($archive->extract( PCLZIP_OPT_PATH, $this->import_dir,
336 PCLZIP_CB_PRE_EXTRACT, 'preImportCallBack') == 0) {
337 die("Error : ".$archive->errorInfo(true));
340 // 3. get the course's max_quota. if backup is too big AND we want to import files then abort/return FALSE
341 /* get the course's max_quota */
342 // $this->getFilesSize();
344 // 4. figure out version number
345 $this->version = $this->getVersion();
346 if (!$this->version) {
347 clr_dir($this->import_dir);
349 $msg->addError('BACKUP_RESTORE');
350 header('Location: '.$_SERVER['PHP_SELF']);
352 //exit('version not found. backups < 1.3 are not supported.');
355 if (version_compare($this->version, VERSION, '>') == 1) {
356 clr_dir($this->import_dir);
359 $msg->addError('BACKUP_UNSUPPORTED_GREATER_VERSION');
360 header('Location: '.$_SERVER['PHP_SELF']);
363 if (version_compare($this_version, '1.5.3', '<')) {
364 if (file_exists($this->import_dir . 'resource_categories.csv')) {
365 @rename($this->import_dir . 'resource_categories.csv', $this->import_dir. 'links_categories.csv');
367 if (file_exists($this->import_dir . 'resource_links.csv')) {
368 @rename($this->import_dir . 'resource_links.csv', $this->import_dir. 'links.csv');
372 // 5. if override is set then delete the content
373 if ($action == 'overwrite') {
374 require_once(AT_INCLUDE_PATH.'lib/delete_course.inc.php');
375 delete_course($this->course_id, $material);
376 $_SESSION['s_cid'] = 0;
377 } // else: appending content
379 if ($material === TRUE) {
380 // restore the entire backup (used when creating a new course)
381 $module_list = $moduleFactory->getModules(AT_MODULE_ENABLED | AT_MODULE_CORE);
382 $_POST['material'] = $module_list;
384 foreach ($_POST['material'] as $module_name => $garbage) {
385 $module =& $moduleFactory->getModule($module_name);
386 $module->restore($this->course_id, $this->version, $this->import_dir);
388 clr_dir($this->import_dir);
393 function restore_files() {
394 $sql = "SELECT max_quota FROM ".TABLE_PREFIX."courses WHERE course_id=$this->course_id";
395 $result = mysql_query($sql, $this->db);
396 $row = mysql_fetch_assoc($result);
398 if ($row['max_quota'] != AT_COURSESIZE_UNLIMITED) {
399 global $MaxCourseSize, $MaxCourseFloat;
401 if ($row['max_quota'] == AT_COURSESIZE_DEFAULT) {
402 $row['max_quota'] = $MaxCourseSize;
405 $totalBytes = dirsize($this->import_dir . 'content/');
407 $course_total = dirsize(AT_CONTENT_DIR . $this->course_id . '/');
409 $total_after = $row['max_quota'] - $course_total - $totalBytes + $MaxCourseFloat;
411 if ($total_after < 0) {
412 //debug('not enough space. delete everything');
413 // remove the content dir, since there's no space for it
414 clr_dir($this->import_dir);
419 copys($this->import_dir.'content/', AT_CONTENT_DIR . $this->course_id);