MDL-35238 Support deployment from yet another plugins check page too
[moodle.git] / backup / cc / entities.class.php
blobe0737fdca1183f51803010a577962ade0598d29a
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.
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 * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
26 class entities {
27 /**
29 * Prepares convert for inclusion into XML
30 * @param string $value
32 public static function safexml($value) {
33 $result = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'),
34 ENT_NOQUOTES,
35 'UTF-8',
36 false);
37 return $result;
40 protected function prepare_content($content) {
41 $result = $content;
42 if (empty($result)) {
43 return '';
45 $encoding = null;
46 $xml_error = new libxml_errors_mgr();
47 $dom = new DOMDocument();
48 $dom->validateOnParse = false;
49 $dom->strictErrorChecking = false;
50 if ($dom->loadHTML($content)) {
51 $encoding = $dom->xmlEncoding;
53 if (empty($encoding)) {
54 $encoding = mb_detect_encoding($content, 'auto', true);
56 if (!empty($encoding) && !mb_check_encoding($content, 'UTF-8')) {
57 $result = mb_convert_encoding($content, 'UTF-8', $encoding);
60 // See if we can strip off body tag and anything outside of it
61 foreach (array('body', 'html') as $tagname) {
62 $regex = str_replace('##', $tagname, "/<##[^>]*>(.+)<\/##>/is");
63 if (preg_match($regex, $result, $matches)) {
64 $result = $matches[1];
65 break;
68 return $result;
71 public function load_xml_resource ($path_to_file) {
73 $resource = new DOMDocument();
75 cc2moodle::log_action('Load the XML resource file: ' . $path_to_file);
77 if (!$resource->load($path_to_file)) {
78 cc2moodle::log_action('Cannot load the XML resource file: ' . $path_to_file, true);
81 return $resource;
84 public function update_sources ($html, $root_path = '') {
86 $document = new DOMDocument();
88 @$document->loadHTML($html);
90 $tags = array('img' => 'src' , 'a' => 'href');
92 foreach ($tags as $tag => $attribute) {
94 $elements = $document->getElementsByTagName($tag);
96 foreach ($elements as $element) {
98 $attribute_value = $element->getAttribute($attribute);
99 $protocol = parse_url($attribute_value, PHP_URL_SCHEME);
101 if (empty($protocol)) {
102 $attribute_value = str_replace("\$IMS-CC-FILEBASE\$", "", $attribute_value);
103 $attribute_value = $this->full_path($root_path . "/" . $attribute_value, "/");
104 $attribute_value = "\$@FILEPHP@\$" . "/" . $attribute_value;
107 $element->setAttribute($attribute, $attribute_value);
111 $html = $this->clear_doctype($document->saveHTML());
113 return $html;
116 public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) {
118 $token = '$IMS-CC-FILEBASE$';
119 $path = str_replace($token, '', $path);
121 if (is_string($path) && ($path != '')) {
122 $dir_sep;
123 $dot_dir = '.';
124 $up_dir = '..';
125 $length = strlen($path);
126 $rtemp = trim($path);
127 $start = strrpos($path, $dir_sep);
128 $can_continue = ($start !== false);
129 $result = $can_continue ? '' : $path;
130 $rcount = 0;
132 while ($can_continue) {
134 $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
135 $can_continue = ($dir_part !== false);
137 if ($can_continue) {
138 if ($dir_part != $dot_dir) {
139 if ($dir_part == $up_dir) {
140 $rcount++;
141 } else {
142 if ($rcount > 0) {
143 $rcount --;
144 } else {
145 $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result;
149 $rtemp = substr($path, 0, $start);
150 $start = strrpos($rtemp, $dir_sep);
151 $can_continue = (($start !== false) || (strlen($rtemp) > 0));
156 return $result;
160 public function include_titles ($html) {
162 $document = new DOMDocument();
163 @$document->loadHTML($html);
165 $images = $document->getElementsByTagName('img');
167 foreach ($images as $image) {
169 $src = $image->getAttribute('src');
170 $alt = $image->getAttribute('alt');
171 $title = $image->getAttribute('title');
173 $filename = pathinfo($src);
174 $filename = $filename['filename'];
176 $alt = empty($alt) ? $filename : $alt;
177 $title = empty($title) ? $filename : $title;
179 $image->setAttribute('alt', $alt);
180 $image->setAttribute('title', $title);
183 $html = $this->clear_doctype($document->saveHTML());
185 return $html;
188 public function get_external_xml ($identifier) {
190 $response = '';
192 $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
194 $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/imscc:file/@href');
196 if (empty($files)) {
197 $response = '';
198 } else {
199 $response = $files->item(0)->nodeValue;
202 return $response;
205 public function move_files ($files, $destination_folder) {
206 global $CFG;
208 if (!empty($files)) {
210 foreach ($files as $file) {
211 $source = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $file;
212 $destination = $destination_folder . DIRECTORY_SEPARATOR . $file;
214 $destination_directory = dirname($destination);
216 cc2moodle::log_action('Copy the file: ' . $source . ' to ' . $destination);
218 if (!file_exists($destination_directory)) {
219 mkdir($destination_directory, $CFG->directorypermissions, true);
222 $copy_success = true;
223 if (is_file($source)) {
224 $copy_success = @copy($source, $destination);
227 if (!$copy_success) {
228 notify('WARNING: Cannot copy the file ' . $source . ' to ' . $destination);
229 cc2moodle::log_action('Cannot copy the file ' . $source . ' to ' . $destination, false);
235 protected function get_all_files () {
236 global $CFG;
238 $all_files = array();
240 $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
242 foreach (cc2moodle::$restypes as $type) {
244 $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href');
246 if (!empty($files) && ($files->length > 0)) {
247 foreach ($files as $file) {
248 //omit html files
249 $ext = strtolower(pathinfo($file->nodeValue, PATHINFO_EXTENSION));
250 if (in_array($ext, array('html', 'htm', 'xhtml'))) {
251 continue;
253 $all_files[] = $file->nodeValue;
256 unset($files);
259 //are there any labels?
260 $xquery = "//imscc:item/imscc:item/imscc:item[imscc:title][not(@identifierref)]";
261 $labels = $xpath->query($xquery);
262 if (!empty($labels) && ($labels->length > 0)) {
263 $tname = 'course_files';
264 $dpath = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $tname;
265 $rfpath = 'files.gif';
266 $fpath = $dpath . DIRECTORY_SEPARATOR . $rfpath;
268 if (!file_exists($dpath)) {
269 mkdir($dpath);
271 //copy the folder.gif file
272 $folder_gif = "{$CFG->dirroot}/pix/i/files.gif";
273 copy($folder_gif, $fpath);
274 $all_files[] = $rfpath;
277 $all_files = empty($all_files) ? '' : $all_files;
279 return $all_files;
282 public function move_all_files() {
284 $files = $this->get_all_files();
286 if (!empty($files)) {
287 $this->move_files($files, cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files', true);
292 private function clear_doctype ($html) {
294 return preg_replace('/^<!DOCTYPE.+?>/',
296 str_replace(array('<html>' , '</html>' , '<body>' , '</body>'),
297 array('' , '' , '' , ''),
298 $html));
301 public function generate_random_string ($length = 6) {
303 $response = '';
304 $source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
306 if ($length > 0) {
308 $response = '';
309 $source = str_split($source, 1);
311 for ($i = 1; $i <= $length; $i++) {
312 mt_srand((double) microtime() * 1000000);
313 $num = mt_rand(1, count($source));
314 $response .= $source[$num - 1];
318 return $response;
321 public function truncate_text ($text, $max, $remove_html) {
323 if ($max > 10) {
324 $text = substr($text, 0, ($max - 6)) . ' [...]';
325 } else {
326 $text = substr($text, 0, $max);
329 $text = $remove_html ? strip_tags($text) : $text;
331 return $text;