2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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');
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);
45 private function create_node_course_modules_mod_resource ($sheet_mod_resource, $instance) {
48 require_once($CFG->libdir
.'/validateurlsyntax.php');
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
: '';
63 if (empty($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
: '';
76 if (!empty($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+')) {
99 $link = 'http://invalidurldetected/';
102 $link = htmlspecialchars(trim($rawlink), ENT_COMPAT
, 'UTF-8', false);
109 $find_tags = array('[#mod_instance#]',
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'))) {
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
);
136 * try to handle embedded resources
137 * images, linked static resources, applets, videos
139 $doc = new DOMDocument();
143 $doc->loadHTML($mod_alltext);
144 $xpath = new DOMXPath($doc);
145 $attributes = array('href', 'src', 'background', 'archive', 'code');
146 $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
148 foreach ($attributes as $attrname) {
149 if (!empty($query)) {
152 $query .= str_replace('##', $attrname, $qtemplate);
154 $list = $xpath->query($query);
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
173 $mod_alltext = self
::safexml($mod_alltext);
178 $replace_values = array($instance['instance'],
179 self
::safexml($instance['title']),
187 return str_replace($find_tags, $replace_values, $sheet_mod_resource);