PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/backup/cc/entities.class.php

https://bitbucket.org/kudutest1/moodlegit
PHP | 333 lines | 224 code | 79 blank | 30 comment | 39 complexity | a431e91717d721a0909197e7b36e20ad 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 entities {
  25. /**
  26. *
  27. * Prepares convert for inclusion into XML
  28. * @param string $value
  29. */
  30. public static function safexml($value) {
  31. $result = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'),
  32. ENT_NOQUOTES,
  33. 'UTF-8',
  34. false);
  35. return $result;
  36. }
  37. protected function prepare_content($content) {
  38. $result = $content;
  39. if (empty($result)) {
  40. return '';
  41. }
  42. $encoding = null;
  43. $xml_error = new libxml_errors_mgr();
  44. $dom = new DOMDocument();
  45. $dom->validateOnParse = false;
  46. $dom->strictErrorChecking = false;
  47. if ($dom->loadHTML($content)) {
  48. $encoding = $dom->xmlEncoding;
  49. }
  50. if (empty($encoding)) {
  51. $encoding = mb_detect_encoding($content, 'auto', true);
  52. }
  53. if (!empty($encoding) && !mb_check_encoding($content, 'UTF-8')) {
  54. $result = mb_convert_encoding($content, 'UTF-8', $encoding);
  55. }
  56. // See if we can strip off body tag and anything outside of it
  57. foreach (array('body', 'html') as $tagname) {
  58. $regex = str_replace('##', $tagname, "/<##[^>]*>(.+)<\/##>/is");
  59. if (preg_match($regex, $result, $matches)) {
  60. $result = $matches[1];
  61. break;
  62. }
  63. }
  64. return $result;
  65. }
  66. public function load_xml_resource ($path_to_file) {
  67. $resource = new DOMDocument();
  68. cc2moodle::log_action('Load the XML resource file: ' . $path_to_file);
  69. if (!$resource->load($path_to_file)) {
  70. cc2moodle::log_action('Cannot load the XML resource file: ' . $path_to_file, true);
  71. }
  72. return $resource;
  73. }
  74. public function update_sources ($html, $root_path = '') {
  75. $document = new DOMDocument();
  76. @$document->loadHTML($html);
  77. $tags = array('img' => 'src' , 'a' => 'href');
  78. foreach ($tags as $tag => $attribute) {
  79. $elements = $document->getElementsByTagName($tag);
  80. foreach ($elements as $element) {
  81. $attribute_value = $element->getAttribute($attribute);
  82. $protocol = parse_url($attribute_value, PHP_URL_SCHEME);
  83. if (empty($protocol)) {
  84. $attribute_value = str_replace("\$IMS-CC-FILEBASE\$", "", $attribute_value);
  85. $attribute_value = $this->full_path($root_path . "/" . $attribute_value, "/");
  86. $attribute_value = "\$@FILEPHP@\$" . "/" . $attribute_value;
  87. }
  88. $element->setAttribute($attribute, $attribute_value);
  89. }
  90. }
  91. $html = $this->clear_doctype($document->saveHTML());
  92. return $html;
  93. }
  94. public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) {
  95. $token = '$IMS-CC-FILEBASE$';
  96. $path = str_replace($token, '', $path);
  97. if (is_string($path) && ($path != '')) {
  98. $dir_sep;
  99. $dot_dir = '.';
  100. $up_dir = '..';
  101. $length = strlen($path);
  102. $rtemp = trim($path);
  103. $start = strrpos($path, $dir_sep);
  104. $can_continue = ($start !== false);
  105. $result = $can_continue ? '' : $path;
  106. $rcount = 0;
  107. while ($can_continue) {
  108. $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp;
  109. $can_continue = ($dir_part !== false);
  110. if ($can_continue) {
  111. if ($dir_part != $dot_dir) {
  112. if ($dir_part == $up_dir) {
  113. $rcount++;
  114. } else {
  115. if ($rcount > 0) {
  116. $rcount --;
  117. } else {
  118. $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result;
  119. }
  120. }
  121. }
  122. $rtemp = substr($path, 0, $start);
  123. $start = strrpos($rtemp, $dir_sep);
  124. $can_continue = (($start !== false) || (strlen($rtemp) > 0));
  125. }
  126. }
  127. }
  128. return $result;
  129. }
  130. public function include_titles ($html) {
  131. $document = new DOMDocument();
  132. @$document->loadHTML($html);
  133. $images = $document->getElementsByTagName('img');
  134. foreach ($images as $image) {
  135. $src = $image->getAttribute('src');
  136. $alt = $image->getAttribute('alt');
  137. $title = $image->getAttribute('title');
  138. $filename = pathinfo($src);
  139. $filename = $filename['filename'];
  140. $alt = empty($alt) ? $filename : $alt;
  141. $title = empty($title) ? $filename : $title;
  142. $image->setAttribute('alt', $alt);
  143. $image->setAttribute('title', $title);
  144. }
  145. $html = $this->clear_doctype($document->saveHTML());
  146. return $html;
  147. }
  148. public function get_external_xml ($identifier) {
  149. $response = '';
  150. $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
  151. $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/imscc:file/@href');
  152. if (empty($files)) {
  153. $response = '';
  154. } else {
  155. $response = $files->item(0)->nodeValue;
  156. }
  157. return $response;
  158. }
  159. public function move_files ($files, $destination_folder) {
  160. global $CFG;
  161. if (!empty($files)) {
  162. foreach ($files as $file) {
  163. $source = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $file;
  164. $destination = $destination_folder . DIRECTORY_SEPARATOR . $file;
  165. $destination_directory = dirname($destination);
  166. cc2moodle::log_action('Copy the file: ' . $source . ' to ' . $destination);
  167. if (!file_exists($destination_directory)) {
  168. mkdir($destination_directory, $CFG->directorypermissions, true);
  169. }
  170. $copy_success = true;
  171. if (is_file($source)) {
  172. $copy_success = @copy($source, $destination);
  173. }
  174. if (!$copy_success) {
  175. notify('WARNING: Cannot copy the file ' . $source . ' to ' . $destination);
  176. cc2moodle::log_action('Cannot copy the file ' . $source . ' to ' . $destination, false);
  177. }
  178. }
  179. }
  180. }
  181. protected function get_all_files () {
  182. global $CFG;
  183. $all_files = array();
  184. $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
  185. foreach (cc2moodle::$restypes as $type) {
  186. $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href');
  187. if (!empty($files) && ($files->length > 0)) {
  188. foreach ($files as $file) {
  189. //omit html files
  190. $ext = strtolower(pathinfo($file->nodeValue, PATHINFO_EXTENSION));
  191. if (in_array($ext, array('html', 'htm', 'xhtml'))) {
  192. continue;
  193. }
  194. $all_files[] = $file->nodeValue;
  195. }
  196. }
  197. unset($files);
  198. }
  199. //are there any labels?
  200. $xquery = "//imscc:item/imscc:item/imscc:item[imscc:title][not(@identifierref)]";
  201. $labels = $xpath->query($xquery);
  202. if (!empty($labels) && ($labels->length > 0)) {
  203. $tname = 'course_files';
  204. $dpath = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $tname;
  205. $rfpath = 'files.gif';
  206. $fpath = $dpath . DIRECTORY_SEPARATOR . $rfpath;
  207. if (!file_exists($dpath)) {
  208. mkdir($dpath);
  209. }
  210. //copy the folder.gif file
  211. $folder_gif = "{$CFG->dirroot}/pix/i/files.gif";
  212. copy($folder_gif, $fpath);
  213. $all_files[] = $rfpath;
  214. }
  215. $all_files = empty($all_files) ? '' : $all_files;
  216. return $all_files;
  217. }
  218. public function move_all_files() {
  219. $files = $this->get_all_files();
  220. if (!empty($files)) {
  221. $this->move_files($files, cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files', true);
  222. }
  223. }
  224. private function clear_doctype ($html) {
  225. return preg_replace('/^<!DOCTYPE.+?>/',
  226. '',
  227. str_replace(array('<html>' , '</html>' , '<body>' , '</body>'),
  228. array('' , '' , '' , ''),
  229. $html));
  230. }
  231. public function generate_random_string ($length = 6) {
  232. $response = '';
  233. $source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  234. if ($length > 0) {
  235. $response = '';
  236. $source = str_split($source, 1);
  237. for ($i = 1; $i <= $length; $i++) {
  238. mt_srand((double) microtime() * 1000000);
  239. $num = mt_rand(1, count($source));
  240. $response .= $source[$num - 1];
  241. }
  242. }
  243. return $response;
  244. }
  245. public function truncate_text ($text, $max, $remove_html) {
  246. if ($max > 10) {
  247. $text = substr($text, 0, ($max - 6)) . ' [...]';
  248. } else {
  249. $text = substr($text, 0, $max);
  250. }
  251. $text = $remove_html ? strip_tags($text) : $text;
  252. return $text;
  253. }
  254. }