|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00024 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.'); 00025 00026 class entities { 00032 public static function safexml($value) { 00033 $result = htmlspecialchars(html_entity_decode($value, ENT_QUOTES, 'UTF-8'), 00034 ENT_NOQUOTES, 00035 'UTF-8', 00036 false); 00037 return $result; 00038 } 00039 00040 protected function prepare_content($content) { 00041 $result = $content; 00042 if (empty($result)) { 00043 return ''; 00044 } 00045 $encoding = null; 00046 $xml_error = new libxml_errors_mgr(); 00047 $dom = new DOMDocument(); 00048 $dom->validateOnParse = false; 00049 $dom->strictErrorChecking = false; 00050 if ($dom->loadHTML($content)) { 00051 $encoding = $dom->xmlEncoding; 00052 } 00053 if (empty($encoding)) { 00054 $encoding = mb_detect_encoding($content, 'auto', true); 00055 } 00056 if (!empty($encoding) && !mb_check_encoding($content, 'UTF-8')) { 00057 $result = mb_convert_encoding($content, 'UTF-8', $encoding); 00058 } 00059 00060 // See if we can strip off body tag and anything outside of it 00061 foreach (array('body', 'html') as $tagname) { 00062 $regex = str_replace('##', $tagname, "/<##[^>]*>(.+)<\/##>/is"); 00063 if (preg_match($regex, $result, $matches)) { 00064 $result = $matches[1]; 00065 break; 00066 } 00067 } 00068 return $result; 00069 } 00070 00071 public function load_xml_resource ($path_to_file) { 00072 00073 $resource = new DOMDocument(); 00074 00075 cc2moodle::log_action('Load the XML resource file: ' . $path_to_file); 00076 00077 if (!$resource->load($path_to_file)) { 00078 cc2moodle::log_action('Cannot load the XML resource file: ' . $path_to_file, true); 00079 } 00080 00081 return $resource; 00082 } 00083 00084 public function update_sources ($html, $root_path = '') { 00085 00086 $document = new DOMDocument(); 00087 00088 @$document->loadHTML($html); 00089 00090 $tags = array('img' => 'src' , 'a' => 'href'); 00091 00092 foreach ($tags as $tag => $attribute) { 00093 00094 $elements = $document->getElementsByTagName($tag); 00095 00096 foreach ($elements as $element) { 00097 00098 $attribute_value = $element->getAttribute($attribute); 00099 $protocol = parse_url($attribute_value, PHP_URL_SCHEME); 00100 00101 if (empty($protocol)) { 00102 $attribute_value = str_replace("\$IMS-CC-FILEBASE\$", "", $attribute_value); 00103 $attribute_value = $this->full_path($root_path . "/" . $attribute_value, "/"); 00104 $attribute_value = "\$@FILEPHP@\$" . "/" . $attribute_value; 00105 } 00106 00107 $element->setAttribute($attribute, $attribute_value); 00108 } 00109 } 00110 00111 $html = $this->clear_doctype($document->saveHTML()); 00112 00113 return $html; 00114 } 00115 00116 public function full_path ($path, $dir_sep = DIRECTORY_SEPARATOR) { 00117 00118 $token = '$IMS-CC-FILEBASE$'; 00119 $path = str_replace($token, '', $path); 00120 00121 if (is_string($path) && ($path != '')) { 00122 $dir_sep; 00123 $dot_dir = '.'; 00124 $up_dir = '..'; 00125 $length = strlen($path); 00126 $rtemp = trim($path); 00127 $start = strrpos($path, $dir_sep); 00128 $can_continue = ($start !== false); 00129 $result = $can_continue ? '' : $path; 00130 $rcount = 0; 00131 00132 while ($can_continue) { 00133 00134 $dir_part = ($start !== false) ? substr($rtemp, $start + 1, $length - $start) : $rtemp; 00135 $can_continue = ($dir_part !== false); 00136 00137 if ($can_continue) { 00138 if ($dir_part != $dot_dir) { 00139 if ($dir_part == $up_dir) { 00140 $rcount++; 00141 } else { 00142 if ($rcount > 0) { 00143 $rcount --; 00144 } else { 00145 $result = ($result == '') ? $dir_part : $dir_part . $dir_sep . $result; 00146 } 00147 } 00148 } 00149 $rtemp = substr($path, 0, $start); 00150 $start = strrpos($rtemp, $dir_sep); 00151 $can_continue = (($start !== false) || (strlen($rtemp) > 0)); 00152 } 00153 } 00154 } 00155 00156 return $result; 00157 00158 } 00159 00160 public function include_titles ($html) { 00161 00162 $document = new DOMDocument(); 00163 @$document->loadHTML($html); 00164 00165 $images = $document->getElementsByTagName('img'); 00166 00167 foreach ($images as $image) { 00168 00169 $src = $image->getAttribute('src'); 00170 $alt = $image->getAttribute('alt'); 00171 $title = $image->getAttribute('title'); 00172 00173 $filename = pathinfo($src); 00174 $filename = $filename['filename']; 00175 00176 $alt = empty($alt) ? $filename : $alt; 00177 $title = empty($title) ? $filename : $title; 00178 00179 $image->setAttribute('alt', $alt); 00180 $image->setAttribute('title', $title); 00181 } 00182 00183 $html = $this->clear_doctype($document->saveHTML()); 00184 00185 return $html; 00186 } 00187 00188 public function get_external_xml ($identifier) { 00189 00190 $response = ''; 00191 00192 $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces); 00193 00194 $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/imscc:file/@href'); 00195 00196 if (empty($files)) { 00197 $response = ''; 00198 } else { 00199 $response = $files->item(0)->nodeValue; 00200 } 00201 00202 return $response; 00203 } 00204 00205 public function move_files ($files, $destination_folder) { 00206 global $CFG; 00207 00208 if (!empty($files)) { 00209 00210 foreach ($files as $file) { 00211 $source = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $file; 00212 $destination = $destination_folder . DIRECTORY_SEPARATOR . $file; 00213 00214 $destination_directory = dirname($destination); 00215 00216 cc2moodle::log_action('Copy the file: ' . $source . ' to ' . $destination); 00217 00218 if (!file_exists($destination_directory)) { 00219 mkdir($destination_directory, $CFG->directorypermissions, true); 00220 } 00221 00222 $copy_success = true; 00223 if (is_file($source)) { 00224 $copy_success = @copy($source, $destination); 00225 } 00226 00227 if (!$copy_success) { 00228 notify('WARNING: Cannot copy the file ' . $source . ' to ' . $destination); 00229 cc2moodle::log_action('Cannot copy the file ' . $source . ' to ' . $destination, false); 00230 } 00231 } 00232 } 00233 } 00234 00235 protected function get_all_files () { 00236 global $CFG; 00237 00238 $all_files = array(); 00239 00240 $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces); 00241 00242 foreach (cc2moodle::$restypes as $type) { 00243 00244 $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href'); 00245 00246 if (!empty($files) && ($files->length > 0)) { 00247 foreach ($files as $file) { 00248 //omit html files 00249 $ext = strtolower(pathinfo($file->nodeValue, PATHINFO_EXTENSION)); 00250 if (in_array($ext, array('html', 'htm', 'xhtml'))) { 00251 continue; 00252 } 00253 $all_files[] = $file->nodeValue; 00254 } 00255 } 00256 unset($files); 00257 } 00258 00259 //are there any labels? 00260 $xquery = "//imscc:item/imscc:item/imscc:item[imscc:title][not(@identifierref)]"; 00261 $labels = $xpath->query($xquery); 00262 if (!empty($labels) && ($labels->length > 0)) { 00263 $tname = 'course_files'; 00264 $dpath = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $tname; 00265 $rfpath = 'folder.gif'; 00266 $fpath = $dpath . DIRECTORY_SEPARATOR . $rfpath; 00267 00268 if (!file_exists($dpath)) { 00269 mkdir($dpath); 00270 } 00271 //copy the folder.gif file 00272 $folder_gif = "{$CFG->dirroot}/pix/f/folder.gif"; 00273 copy($folder_gif, $fpath); 00274 $all_files[] = $rfpath; 00275 } 00276 00277 $all_files = empty($all_files) ? '' : $all_files; 00278 00279 return $all_files; 00280 } 00281 00282 public function move_all_files() { 00283 00284 $files = $this->get_all_files(); 00285 00286 if (!empty($files)) { 00287 $this->move_files($files, cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files', true); 00288 } 00289 00290 } 00291 00292 private function clear_doctype ($html) { 00293 00294 return preg_replace('/^<!DOCTYPE.+?>/', 00295 '', 00296 str_replace(array('<html>' , '</html>' , '<body>' , '</body>'), 00297 array('' , '' , '' , ''), 00298 $html)); 00299 } 00300 00301 public function generate_random_string ($length = 6) { 00302 00303 $response = ''; 00304 $source = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 00305 00306 if ($length > 0) { 00307 00308 $response = ''; 00309 $source = str_split($source, 1); 00310 00311 for ($i = 1; $i <= $length; $i++) { 00312 mt_srand((double) microtime() * 1000000); 00313 $num = mt_rand(1, count($source)); 00314 $response .= $source[$num - 1]; 00315 } 00316 } 00317 00318 return $response; 00319 } 00320 00321 public function truncate_text ($text, $max, $remove_html) { 00322 00323 if ($max > 10) { 00324 $text = substr($text, 0, ($max - 6)) . ' [...]'; 00325 } else { 00326 $text = substr($text, 0, $max); 00327 } 00328 00329 $text = $remove_html ? strip_tags($text) : $text; 00330 00331 return $text; 00332 } 00333 }