PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/cc/entity.resource.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 183 lines | 127 code | 27 blank | 29 comment | 26 complexity | 7828c92b25a2dc63bec91342969c726b MD5 | raw file
  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. * @copyright 2011 Darko Miletic (dmiletic@moodlerooms.com)
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
  24. class cc_resource extends entities {
  25. public function generate_node () {
  26. cc2moodle::log_action('Creating Resource mods');
  27. $response = '';
  28. $sheet_mod_resource = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE);
  29. if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) {
  30. foreach (cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE] as $instance) {
  31. $response .= $this->create_node_course_modules_mod_resource($sheet_mod_resource, $instance);
  32. }
  33. }
  34. return $response;
  35. }
  36. private function create_node_course_modules_mod_resource ($sheet_mod_resource, $instance) {
  37. global $CFG;
  38. require_once($CFG->libdir.'/validateurlsyntax.php');
  39. $link = '';
  40. $mod_alltext = '';
  41. $mod_summary = '';
  42. $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
  43. if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_ASSOCIATED_CONTENT) {
  44. $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
  45. $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  46. if (empty($resource)) {
  47. unset($resource);
  48. $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
  49. $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
  50. }
  51. if (!empty($resource)) {
  52. $link = $resource;
  53. }
  54. }
  55. if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBLINK) {
  56. $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
  57. if ($external_resource) {
  58. $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
  59. if (!empty($resource)) {
  60. $xpath = cc2moodle::newx_path($resource, cc2moodle::getresourcens());
  61. $resource = $xpath->query('//url/@href');
  62. if ($resource->length > 0) {
  63. $rawlink = $resource->item(0)->nodeValue;
  64. if (!validateUrlSyntax($rawlink, 's+')) {
  65. $changed = rawurldecode($rawlink);
  66. if (validateUrlSyntax($changed, 's+')) {
  67. $link = $changed;
  68. } else {
  69. $link = 'http://invalidurldetected/';
  70. }
  71. } else {
  72. $link = $rawlink;
  73. }
  74. }
  75. }
  76. }
  77. }
  78. $find_tags = array('[#mod_instance#]',
  79. '[#mod_name#]',
  80. '[#mod_type#]',
  81. '[#mod_reference#]',
  82. '[#mod_summary#]',
  83. '[#mod_alltext#]',
  84. '[#mod_options#]',
  85. '[#date_now#]');
  86. $mod_type = 'file';
  87. $mod_options = 'objectframe';
  88. $mod_reference = $link;
  89. //detected if we are dealing with html file
  90. if (!empty($link) && ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT)) {
  91. $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
  92. if (in_array($ext, array('html', 'htm', 'xhtml'))) {
  93. $mod_type = 'html';
  94. //extract the content of the file
  95. $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
  96. $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
  97. $dirpath = dirname($htmlpath);
  98. if (file_exists($htmlpath)) {
  99. $fcontent = file_get_contents($htmlpath);
  100. $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
  101. $mod_reference = '';
  102. $mod_options = '';
  103. //TODO: try to handle embedded resources
  104. /**
  105. * images, linked static resources, applets, videos
  106. */
  107. $doc = new DOMDocument();
  108. $cdir = getcwd();
  109. chdir($dirpath);
  110. try {
  111. if (!empty($mod_alltext) && $doc->loadHTML($mod_alltext)) {
  112. $xpath = new DOMXPath($doc);
  113. $attributes = array('href', 'src', 'background', 'archive', 'code');
  114. $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
  115. $query = '';
  116. foreach ($attributes as $attrname) {
  117. if (!empty($query)) {
  118. $query .= " | ";
  119. }
  120. $query .= str_replace('##', $attrname, $qtemplate);
  121. }
  122. $list = $xpath->query($query);
  123. $searches = array();
  124. $replaces = array();
  125. foreach ($list as $resrc) {
  126. $rpath = $resrc->nodeValue;
  127. $rtp = realpath($rpath);
  128. if (($rtp !== false) && is_file($rtp)) {
  129. //file is there - we are in business
  130. $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
  131. $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip);
  132. $searches[] = $resrc->nodeValue;
  133. $replaces[] = $encoded_file;
  134. }
  135. }
  136. $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
  137. }
  138. } catch (Exception $e) {
  139. //silence the complaints
  140. }
  141. chdir($cdir);
  142. $mod_alltext = self::safexml($mod_alltext);
  143. }
  144. }
  145. }
  146. $replace_values = array($instance['instance'],
  147. self::safexml($instance['title']),
  148. $mod_type,
  149. $mod_reference,
  150. '',
  151. $mod_alltext,
  152. $mod_options,
  153. time());
  154. return str_replace($find_tags, $replace_values, $sheet_mod_resource);
  155. }
  156. }