Merge branch 'install_27_STABLE' of https://git.in.moodle.com/amosbot/moodle-install...
[moodle.git] / backup / cc / entity.resource.class.php
blob6de81164296b966009a241a896bf7b4a455df2e3
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 cc_resource extends entities {
28 public function generate_node () {
30 cc2moodle::log_action('Creating Resource mods');
32 $response = '';
33 $sheet_mod_resource = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE);
35 if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) {
36 foreach (cc2moodle::$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 = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
55 if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_ASSOCIATED_CONTENT) {
56 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
57 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
59 if (empty($resource)) {
61 unset($resource);
63 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
64 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
68 if (!empty($resource)) {
69 $link = $resource;
73 if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBLINK) {
75 $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
77 if ($external_resource) {
79 $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
81 if (!empty($resource)) {
82 $xpath = cc2moodle::newx_path($resource, cc2moodle::getresourcens());
83 $resource = $xpath->query('//url/@href');
84 if ($resource->length > 0) {
85 $rawlink = $resource->item(0)->nodeValue;
86 if (!validateUrlSyntax($rawlink, 's+')) {
87 $changed = rawurldecode($rawlink);
88 if (validateUrlSyntax($changed, 's+')) {
89 $link = $changed;
90 } else {
91 $link = 'http://invalidurldetected/';
93 } else {
94 $link = $rawlink;
101 $find_tags = array('[#mod_instance#]',
102 '[#mod_name#]',
103 '[#mod_type#]',
104 '[#mod_reference#]',
105 '[#mod_summary#]',
106 '[#mod_alltext#]',
107 '[#mod_options#]',
108 '[#date_now#]');
110 $mod_type = 'file';
111 $mod_options = 'objectframe';
112 $mod_reference = $link;
113 //detected if we are dealing with html file
114 if (!empty($link) && ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT)) {
115 $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
116 if (in_array($ext, array('html', 'htm', 'xhtml'))) {
117 $mod_type = 'html';
118 //extract the content of the file
119 $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
120 $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
121 $dirpath = dirname($htmlpath);
122 if (file_exists($htmlpath)) {
123 $fcontent = file_get_contents($htmlpath);
124 $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
125 $mod_reference = '';
126 $mod_options = '';
127 //TODO: try to handle embedded resources
129 * images, linked static resources, applets, videos
131 $doc = new DOMDocument();
132 $cdir = getcwd();
133 chdir($dirpath);
134 try {
135 if (!empty($mod_alltext) && $doc->loadHTML($mod_alltext)) {
136 $xpath = new DOMXPath($doc);
137 $attributes = array('href', 'src', 'background', 'archive', 'code');
138 $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
139 $query = '';
140 foreach ($attributes as $attrname) {
141 if (!empty($query)) {
142 $query .= " | ";
144 $query .= str_replace('##', $attrname, $qtemplate);
146 $list = $xpath->query($query);
147 $searches = array();
148 $replaces = array();
149 foreach ($list as $resrc) {
150 $rpath = $resrc->nodeValue;
151 $rtp = realpath($rpath);
152 if (($rtp !== false) && is_file($rtp)) {
153 //file is there - we are in business
154 $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
155 $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip);
156 $searches[] = $resrc->nodeValue;
157 $replaces[] = $encoded_file;
160 $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
162 } catch (Exception $e) {
163 //silence the complaints
165 chdir($cdir);
166 $mod_alltext = self::safexml($mod_alltext);
171 $replace_values = array($instance['instance'],
172 self::safexml($instance['title']),
173 $mod_type,
174 $mod_reference,
176 $mod_alltext,
177 $mod_options,
178 time());
181 return str_replace($find_tags, $replace_values, $sheet_mod_resource);