tagging as ATutor 1.5.4-release
[atutor.git] / include / classes / Backup / Backup.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2004 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
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 /************************************************************************/
13 // $Id$
14
15 require_once(AT_INCLUDE_PATH.'classes/zipfile.class.php');
16
17 define('NUMBER',        1);
18 define('TEXT',          2);
19
20 /**
21 * Backup
22 * Class for creating and managing course backups
23 * @access       public
24 * @author       Joel Kronenberg
25 * @package      Backup
26 */
27 class Backup {
28
29         // private
30         // number of backups in the backup dir
31         var $num_backups;
32
33         // private
34         // the current course id
35         var $course_id;
36
37         // private
38         // where to store the backup
39         var $backup_dir;
40
41         // private
42         // db handler
43         var $db;
44
45         // the backup zipfile Object
46         var $zipfile;
47
48         // the timestamp for the zip files
49         var $timestamp;
50
51         // private
52         // array of installed modules that support backups
53         var $modules;
54
55         var $backup_tables;
56
57         // constructor
58         function Backup(&$db, $course_id = 0) {
59
60                 $this->db = $db;
61
62                 $this->setCourseID($course_id);
63         }
64
65         // public
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;
70         }
71
72
73         // public
74         // call staticly
75         function generateFileName( ) {
76                 global $system_courses;
77                 $title = $system_courses[$this->course_id]['title'];
78
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);
84
85                 $title .= '_' . date('d_M_y') . '.zip';
86
87                 return $title;
88         }
89
90         // public
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;
95
96                 if ($this->getNumAvailable() >= AT_COURSE_BACKUPS) {
97                         return FALSE;
98                 }
99
100                 $timestamp = time();
101
102                 $zipfile =& new zipfile();
103
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);
106
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);
112                 }
113                 $zipfile->close();
114
115                 $system_file_name = md5($timestamp);
116                 
117                 if (!is_dir(AT_BACKUP_DIR)) {
118                         @mkdir(AT_BACKUP_DIR);
119                 }
120
121                 if (!is_dir(AT_BACKUP_DIR . $this->course_id)) {
122                         @mkdir(AT_BACKUP_DIR . $this->course_id);
123                 }
124
125                 $zipfile->write_file(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $system_file_name . '.zip');
126
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();
132
133                 $this->add($row);
134
135                 return TRUE;
136         }
137
138         // public
139         function upload($_FILES, $description) {
140                 global $msg;
141         
142                 $ext = pathinfo($_FILES['file']['name']);
143                 $ext = $ext['extension'];
144
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); 
149                         } else {
150                                 $msg->addError('FILE_NOT_SELECTED');
151                         }
152                 }
153
154                 if ($_FILES['file']['size'] == 0) {
155                         $msg->addError('IMPORTFILE_EMPTY');
156                 }
157
158                 if($msg->containsErrors()) {
159                         return;
160                 }
161
162                 $row = array();
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'];
168
169                 if (!is_dir(AT_BACKUP_DIR)) {
170                         @mkdir(AT_BACKUP_DIR);
171                 }
172
173                 if (!is_dir(AT_BACKUP_DIR . $this->course_id)) {
174                         @mkdir(AT_BACKUP_DIR . $this->course_id);
175                 }
176
177                 $backup_path = AT_BACKUP_DIR . DIRECTORY_SEPARATOR . $this->course_id . DIRECTORY_SEPARATOR;
178
179                 move_uploaded_file($_FILES['file']['tmp_name'], $backup_path . $row['system_file_name'].'.zip');
180
181                 $this->add($row);
182
183                 return;
184         }
185
186         // private
187         // adds a backup to the database
188         function add($row) {
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);
191         }
192
193         // public
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;
199                 }
200
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);
204
205                 $this->num_backups = $row['cnt'];
206                 return $row['cnt'];
207         }
208
209         // public
210         // get list of backups
211         function getAvailableList() {
212                 $backup_list = array();
213
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']);
219                 }
220
221                 $this->num_backups = count($backup_list);
222
223                 return $backup_list;
224         }
225
226         // public
227         function download($backup_id) { // or fetch()
228                 $list = $this->getAvailableList($this->course_id);
229                 if (!isset($list[$backup_id])) {
230                         // catch the error
231                         //debug('does not belong to us');
232                         exit;
233                 }
234
235                 $my_backup = $list[$backup_id];
236                 $file_name = $my_backup['file_name'];
237
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']);
245
246                 // see the note in get.php about the use of x-Sendfile
247                 ob_end_clean();
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
251
252                 readfile(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $my_backup['system_file_name']. '.zip');
253                 exit;
254         }
255
256         // public
257         function delete($backup_id) {
258                 $list = $this->getAvailableList($this->course_id);
259                 if (!isset($list[$backup_id])) {
260                         // catch the error
261                         //debug('does not belong to us');
262                         exit;
263                 }
264                 $my_backup = $list[$backup_id];
265
266                 // delete the backup file:
267                 @unlink(AT_BACKUP_DIR . $this->course_id . DIRECTORY_SEPARATOR . $my_backup['system_file_name']. '.zip');
268
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);
272         }
273
274         // public
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);
279
280         }
281
282         // public
283         function getRow($backup_id, $course_id = 0) {
284                 if ($course_id) {
285                         $sql    = "SELECT *, UNIX_TIMESTAMP(date) AS date_timestamp FROM ".TABLE_PREFIX."backups WHERE backup_id=$backup_id AND course_id=$course_id";
286                 } else {
287                         $sql    = "SELECT *, UNIX_TIMESTAMP(date) AS date_timestamp FROM ".TABLE_PREFIX."backups WHERE backup_id=$backup_id AND course_id=$this->course_id";
288                 }
289
290                 $result = mysql_query($sql, $this->db);
291                 $row = mysql_fetch_assoc($result);
292
293                 if ($row) {
294                         $row['contents'] = unserialize($row['contents']);
295                 }
296                 return $row;
297         }
298
299         // public
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);
304
305                 return $input;
306         }
307
308         // public
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]);
312                 } else {
313                         return false;
314                 }
315         }
316
317         // public
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');
322
323                 if (!$from_course_id) {
324                         $from_course_id = $this->course_id;
325                 }
326
327                 // 1. get backup row/information
328                 $my_backup = $this->getRow($backup_id, $from_course_id);
329
330                 @mkdir(AT_CONTENT_DIR . 'import/' . $this->course_id);
331                 $this->import_dir = AT_CONTENT_DIR . 'import/' . $this->course_id . '/';
332
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));
338                 }
339
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();
343
344                 // 4. figure out version number
345                 $this->version = $this->getVersion();
346                 if (!$this->version) {
347                         clr_dir($this->import_dir);
348                         global $msg;
349                         $msg->addError('BACKUP_RESTORE');
350                         header('Location: '.$_SERVER['PHP_SELF']);
351                         exit;
352                         //exit('version not found. backups < 1.3 are not supported.');
353                 }
354
355                 if (version_compare($this->version, VERSION, '>') == 1) {
356                         clr_dir($this->import_dir);
357                         global $msg;
358
359                         $msg->addError('BACKUP_UNSUPPORTED_GREATER_VERSION');
360                         header('Location: '.$_SERVER['PHP_SELF']);
361                         exit;
362                 }
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');
366                         }
367                         if (file_exists($this->import_dir . 'resource_links.csv')) {
368                                 @rename($this->import_dir . 'resource_links.csv', $this->import_dir. 'links.csv');
369                         }
370                 }
371
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
378
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;
383                 }
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);
387                 }
388                 clr_dir($this->import_dir);
389         }
390
391         // private
392         // no longer used
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);
397
398                 if ($row['max_quota'] != AT_COURSESIZE_UNLIMITED) {
399                         global $MaxCourseSize, $MaxCourseFloat;
400
401                         if ($row['max_quota'] == AT_COURSESIZE_DEFAULT) {
402                                 $row['max_quota'] = $MaxCourseSize;
403                         }
404                         
405                         $totalBytes   = dirsize($this->import_dir . 'content/');
406                         
407                         $course_total = dirsize(AT_CONTENT_DIR . $this->course_id . '/');
408                 
409                         $total_after  = $row['max_quota'] - $course_total - $totalBytes + $MaxCourseFloat;
410
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);
415                                 return FALSE;
416                         }
417                 }
418
419                 copys($this->import_dir.'content/', AT_CONTENT_DIR . $this->course_id);
420         }
421 }
422
423 ?>