Commit | Line | Data |
---|---|---|
69dd0c8c EL |
1 | <?php |
2 | ||
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | /** | |
19 | * @package moodlecore | |
20 | * @subpackage backup-helper | |
21 | * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | /** | |
26 | * Non instantiable helper class providing general helper methods for backup/restore | |
27 | * | |
28 | * This class contains various general helper static methods available for backup/restore | |
29 | * | |
30 | * TODO: Finish phpdocs | |
31 | */ | |
32 | abstract class backup_general_helper extends backup_helper { | |
33 | ||
34 | /** | |
35 | * Calculate one checksum for any array/object. Works recursively | |
36 | */ | |
37 | public static function array_checksum_recursive($arr) { | |
38 | ||
39 | $checksum = ''; // Init checksum | |
40 | ||
41 | // Check we are going to process one array always, objects must be cast before | |
42 | if (!is_array($arr)) { | |
43 | throw new backup_helper_exception('array_expected'); | |
44 | } | |
45 | foreach ($arr as $key => $value) { | |
46 | if ($value instanceof checksumable) { | |
47 | $checksum = md5($checksum . '-' . $key . '-' . $value->calculate_checksum()); | |
48 | } else if (is_object($value)) { | |
49 | $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive((array)$value)); | |
50 | } else if (is_array($value)) { | |
51 | $checksum = md5($checksum . '-' . $key . '-' . self::array_checksum_recursive($value)); | |
52 | } else { | |
53 | $checksum = md5($checksum . '-' . $key . '-' . $value); | |
54 | } | |
55 | } | |
56 | return $checksum; | |
57 | } | |
f60f4666 EL |
58 | |
59 | /** | |
60 | * Given one temp/backup/xxx dir, detect its format | |
61 | * | |
62 | * TODO: Move harcoded detection here to delegated classes under backup/format (moodle1, imscc..) | |
63 | * conversion code will be there too. | |
64 | */ | |
65 | public static function detect_backup_format($tempdir) { | |
66 | global $CFG; | |
67 | ||
68 | // First look for MOODLE (moodle2) format | |
69 | $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle_backup.xml'; | |
70 | if (file_exists($filepath)) { // Looks promising, lets load some information | |
71 | $handle = fopen ($filepath, "r"); | |
72 | $first_chars = fread($handle,200); | |
73 | $status = fclose ($handle); | |
74 | // Check if it has the required strings | |
75 | if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false && | |
76 | strpos($first_chars,'<moodle_backup>') !== false && | |
77 | strpos($first_chars,'<information>') !== false) { | |
78 | return backup::FORMAT_MOODLE; | |
79 | } | |
80 | } | |
81 | ||
82 | // Then look for MOODLE1 (moodle1) format | |
83 | $filepath = $CFG->dataroot . '/temp/backup/' . $tempdir . '/moodle.xml'; | |
84 | if (file_exists($filepath)) { // Looks promising, lets load some information | |
85 | $handle = fopen ($filepath, "r"); | |
86 | $first_chars = fread($handle,200); | |
87 | $status = fclose ($handle); | |
88 | // Check if it has the required strings | |
89 | if (strpos($first_chars,'<?xml version="1.0" encoding="UTF-8"?>') !== false && | |
90 | strpos($first_chars,'<MOODLE_BACKUP>') !== false && | |
91 | strpos($first_chars,'<INFO>') !== false) { | |
92 | return backup::FORMAT_MOODLE1; | |
93 | } | |
94 | } | |
95 | ||
96 | // Other formats | |
97 | ||
98 | // Arrived here, unknown format | |
99 | return backup::FORMAT_UNKNOWN; | |
100 | } | |
69dd0c8c | 101 | } |