Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / backup / cc / entity11.resource.class.php
blobbc5bb5aab07cda94f5620ac549ceef28ae5e698f
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 cc11_resource extends entities11 {
28 public function generate_node () {
30 cc112moodle::log_action('Creating Resource mods');
32 $response = '';
33 $sheet_mod_resource = cc112moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE);
35 if (!empty(cc112moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) {
36 foreach (cc112moodle::$instances['instances'][MOODLE_TYPE_RESOURCE] as $instance) {
37 $response .= $this->create_node_course_modules_mod_resource($sheet_mod_resource, $instance);
41 return $response;
45 private function create_node_course_modules_mod_resource ($sheet_mod_resource, $instance) {
46 global $CFG;
48 require_once($CFG->libdir.'/validateurlsyntax.php');
50 $link = '';
51 $mod_alltext = '';
52 $mod_summary = '';
53 $xpath = cc112moodle::newx_path(cc112moodle::$manifest, cc112moodle::$namespaces);
55 if ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_ASSOCIATED_CONTENT) {
56 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
57 if ($resource->length > 0) {
58 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
59 } else {
60 $resource = '';
63 if (empty($resource)) {
65 unset($resource);
67 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
68 if ($resource->length > 0) {
69 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
70 } else {
71 $resource = '';
76 if (!empty($resource)) {
77 $link = $resource;
81 if ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBLINK) {
83 $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
85 if ($external_resource) {
87 $resource = $this->load_xml_resource(cc112moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
89 if (!empty($resource)) {
90 $xpath = cc112moodle::newx_path($resource, cc112moodle::$resourcens);
91 $resource = $xpath->query('/wl:webLink/wl:url/@href');
92 if ($resource->length > 0) {
93 $rawlink = $resource->item(0)->nodeValue;
94 if (!validateUrlSyntax($rawlink, 's+')) {
95 $changed = rawurldecode($rawlink);
96 if (validateUrlSyntax($changed, 's+')) {
97 $link = $changed;
98 } else {
99 $link = 'http://invalidurldetected/';
101 } else {
102 $link = $rawlink;
109 $find_tags = array('[#mod_instance#]',
110 '[#mod_name#]',
111 '[#mod_type#]',
112 '[#mod_reference#]',
113 '[#mod_summary#]',
114 '[#mod_alltext#]',
115 '[#mod_options#]',
116 '[#date_now#]');
118 $mod_type = 'file';
119 $mod_options = 'objectframe';
120 $mod_reference = $link;
121 //detected if we are dealing with html file
122 if (!empty($link) && ($instance['common_cartriedge_type'] == cc112moodle::CC_TYPE_WEBCONTENT)) {
123 $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
124 if (in_array($ext, array('html', 'htm', 'xhtml'))) {
125 $mod_type = 'html';
126 //extract the content of the file
127 $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
128 $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
129 $dirpath = dirname($htmlpath);
130 if (file_exists($htmlpath)) {
131 $fcontent = file_get_contents($htmlpath);
132 $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
133 $mod_reference = '';
134 $mod_options = '';
136 * try to handle embedded resources
137 * images, linked static resources, applets, videos
139 $doc = new DOMDocument();
140 $cdir = getcwd();
141 chdir($dirpath);
142 try {
143 $doc->loadHTML($mod_alltext);
144 $xpath = new DOMXPath($doc);
145 $attributes = array('href', 'src', 'background', 'archive', 'code');
146 $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
147 $query = '';
148 foreach ($attributes as $attrname) {
149 if (!empty($query)) {
150 $query .= " | ";
152 $query .= str_replace('##', $attrname, $qtemplate);
154 $list = $xpath->query($query);
155 $searches = array();
156 $replaces = array();
157 foreach ($list as $resrc) {
158 $rpath = $resrc->nodeValue;
159 $rtp = realpath($rpath);
160 if (($rtp !== false) && is_file($rtp)) {
161 //file is there - we are in business
162 $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
163 $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip);
164 $searches[] = $resrc->nodeValue;
165 $replaces[] = $encoded_file;
168 $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
169 } catch (Exception $e) {
170 //silence the complaints
172 chdir($cdir);
173 $mod_alltext = self::safexml($mod_alltext);
178 $replace_values = array($instance['instance'],
179 self::safexml($instance['title']),
180 $mod_type,
181 $mod_reference,
183 $mod_alltext,
184 $mod_options,
185 time());
187 return str_replace($find_tags, $replace_values, $sheet_mod_resource);