df73d19d |
1 | <?php |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // |
4 | // Moodle is free software: you can redistribute it and/or modify |
5 | // it under the terms of the GNU General Public License as published by |
6 | // the Free Software Foundation, either version 3 of the License, or |
7 | // (at your option) any later version. |
8 | // |
9 | // Moodle is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | // GNU General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU General Public License |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
16 | /** |
17 | * @package moodlecore |
18 | * @subpackage backup-imscc |
19 | * @copyright 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com) |
20 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
21 | */ |
22 | |
23 | defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.'); |
24 | |
25 | class entities { |
26 | |
27 | public function load_xml_resource ($path_to_file) { |
28 | |
29 | $resource = new DOMDocument(); |
30 | |
31 | cc2moodle::log_action('Load the XML resource file: ' . $path_to_file); |
32 | |
33 | if (!$resource->load($path_to_file)) { |
34 | cc2moodle::log_action('Cannot load the XML resource file: ' . $path_to_file, true); |
35 | } |
36 | |
37 | return $resource; |
38 | } |
39 | |
40 | public function update_sources ($html, $root_path = '') { |
41 | |
42 | $document = new DOMDocument(); |
43 | |
44 | @$document->loadHTML($html); |
45 | |
46 | $tags = array('img' => 'src' , 'a' => 'href'); |
47 | |
48 | foreach ($tags as $tag => $attribute) { |
49 | |
50 | $elements = $document->getElementsByTagName($tag); |
51 | |
52 | foreach ($elements as $element) { |
53 | |
54 | $attribute_value = $element->getAttribute($attribute); |
55 | $protocol = parse_url($attribute_value, PHP_URL_SCHEME); |
56 | |
57 | if (empty($protocol)) { |
58 | $attribute_value = str_replace("\$IMS-CC-FILEBASE\$", "", $attribute_value); |
59 | $attribute_value = $this->full_path($root_path . "/" . $attribute_value, "/"); |
60 | $attribute_value = "\$@FILEPHP@\$" . "/" . $attribute_value; |
61 | } |
62 | |
63 | $element->setAttribute($attribute, $attribute_value); |
64 | } |
65 | } |
66 | |
67 | $html = $this->clear_doctype($document->saveHTML()); |
68 | |
69 | return $html; |
70 | } |
71 | |
72 | public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) { |
73 | |
74 | $token = '$IMS-CC-FILEBASE$'; |
75 | $path = str_replace($token, '', $path); |
76 | |
77 | if (is_string($path) && ($path != '')) { |
78 | $dir_sep; |
79 | $dot_dir = '.'; |
80 | $up_dir = '..'; |
81 | $length = strlen($path); |
82 | $rtemp = trim($path); |
83 | $start = strrpos($path, $dir_sep); |
84 | $can_continue = ($start !== false); |
85 | $result = $can_continue ? '' : $path; |
86 | $rcount = 0; |
87 | |
88 | while ($can_continue) { |
89 | |
90 | $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp; |
91 | $can_continue = ($dir_part !== false); |
92 | |
93 | if ($can_continue) { |
94 | if ($dir_part != $dot_dir) { |
95 | if ($dir_part == $up_dir) { |
96 | $rcount++; |
97 | } else { |
98 | if ($rcount > 0) { |
99 | $rcount --; |
100 | } else { |
101 | $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result; |
102 | } |
103 | } |
104 | } |
105 | $rtemp = substr($path, 0, $start); |
106 | $start = strrpos($rtemp, $dir_sep); |
107 | $can_continue = (($start !== false) || (strlen($rtemp) > 0)); |
108 | } |
109 | } |
110 | } |
111 | |
112 | return $result; |
113 | |
114 | } |
115 | |
116 | public function include_titles ($html) { |
117 | |
118 | $document = new DOMDocument(); |
119 | @$document->loadHTML($html); |
120 | |
121 | $images = $document->getElementsByTagName('img'); |
122 | |
123 | foreach ($images as $image) { |
124 | |
125 | $src = $image->getAttribute('src'); |
126 | $alt = $image->getAttribute('alt'); |
127 | $title = $image->getAttribute('title'); |
128 | |
129 | $filename = pathinfo($src); |
130 | $filename = $filename['filename']; |
131 | |
132 | $alt = empty($alt) ? $filename : $alt; |
133 | $title = empty($title) ? $filename : $title; |
134 | |
135 | $image->setAttribute('alt', $alt); |
136 | $image->setAttribute('title', $title); |
137 | } |
138 | |
139 | $html = $this->clear_doctype($document->saveHTML()); |
140 | |
141 | return $html; |
142 | } |
143 | |
144 | public function get_external_xml ($identifier) { |
145 | |
146 | $response = ''; |
147 | |
148 | $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces); |
149 | |
150 | $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/imscc:file/@href'); |
151 | |
152 | if (empty($files)) { |
153 | $response = ''; |
154 | } else { |
155 | $response = $files->item(0)->nodeValue; |
156 | } |
157 | |
158 | return $response; |
159 | } |
160 | |
161 | public function move_files ($files, $destination_folder) { |
4756e9c9 |
162 | global $CFG; |
df73d19d |
163 | |
164 | if (!empty($files)) { |
165 | |
166 | foreach ($files as $file) { |
167 | |
168 | $source = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $file->nodeValue; |
169 | $destination = $destination_folder . DIRECTORY_SEPARATOR . $file->nodeValue; |
170 | |
171 | $destination_directory = dirname($destination); |
172 | |
173 | cc2moodle::log_action('Copy the file: ' . $source . ' to ' . $destination); |
174 | |
175 | //echo 'Copy the file: ' . $source . ' to ' . $destination . '<br>'; |
176 | |
177 | if (!file_exists($destination_directory)) { |
4756e9c9 |
178 | mkdir($destination_directory, $CFG->directorypermissions, true); |
df73d19d |
179 | } |
180 | |
181 | $copy_success = @copy($source, $destination); |
182 | |
183 | if (!$copy_success) { |
184 | notify('WARNING: Cannot copy the file ' . $source . ' to ' . $destination); |
185 | cc2moodle::log_action('Cannot copy the file ' . $source . ' to ' . $destination, false); |
186 | } |
187 | } |
188 | } |
189 | } |
190 | |
191 | private function get_all_files () { |
192 | |
193 | $all_files = array(); |
194 | |
195 | $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces); |
196 | |
197 | $types = array('associatedcontent/imscc_xmlv1p0/learning-application-resource', |
198 | 'webcontent', |
199 | ); |
200 | |
201 | foreach ($types as $type) { |
202 | |
203 | $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href'); |
204 | |
205 | if (!empty($files)) { |
206 | foreach ($files as $file) { |
207 | $all_files[] = $file; |
208 | } |
209 | } |
210 | |
211 | unset($files); |
212 | } |
213 | |
214 | $all_files = empty($all_files) ? '' : $all_files; |
215 | |
216 | return $all_files; |
217 | } |
218 | |
219 | public function move_all_files() { |
220 | |
221 | $files = $this->get_all_files(); |
222 | |
223 | if (!empty($files)) { |
224 | $this->move_files($files, cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files', true); |
225 | } |
226 | |
227 | } |
228 | |
229 | private function clear_doctype ($html) { |
230 | |
231 | return preg_replace('/^<!DOCTYPE.+?>/', |
232 | '', |
233 | str_replace(array('<html>' , '</html>' , '<body>' , '</body>'), |
234 | array('' , '' , '' , ''), |
235 | $html)); |
236 | } |
237 | |
238 | public function generate_random_string ($length = 6) { |
239 | |
240 | $response = ''; |
241 | $source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
242 | |
243 | if ($length > 0) { |
244 | |
245 | $response = ''; |
246 | $source = str_split($source, 1); |
247 | |
248 | for ($i = 1; $i <= $length; $i++) { |
249 | mt_srand((double) microtime() * 1000000); |
250 | $num = mt_rand(1, count($source)); |
251 | $response .= $source[$num - 1]; |
252 | } |
253 | } |
254 | |
255 | return $response; |
256 | } |
257 | |
258 | public function truncate_text ($text, $max, $remove_html) { |
259 | |
260 | if ($max > 10) { |
261 | $text = substr($text, 0, ($max - 6)) . ' [...]'; |
262 | } else { |
263 | $text = substr($text, 0, $max); |
264 | } |
265 | |
266 | $text = $remove_html ? strip_tags($text) : $text; |
267 | |
268 | return $text; |
269 | } |
270 | } |