|
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 final class error_messages { 00029 private static $instance = null; 00030 private function __construct(){} 00031 private function __clone(){} 00035 public static function instance() { 00036 if (empty(self::$instance)) { 00037 $c = __CLASS__; 00038 self::$instance = new $c(); 00039 } 00040 return self::$instance; 00041 } 00042 00046 private $items = array(); 00047 00051 public function add($msg) { 00052 if (!empty($msg)) { 00053 $this->items[] = $msg; 00054 } 00055 } 00056 00060 public function errors() { 00061 $this->items; 00062 } 00063 00067 public function reset() { 00068 $this->items = array(); 00069 } 00070 00075 public function to_string($web = false) { 00076 $result = ''; 00077 if ($web) { 00078 $result .= '<ol>'.PHP_EOL; 00079 } 00080 foreach ($this->items as $error) { 00081 if ($web) { 00082 $result .= '<li>'; 00083 } 00084 00085 $result .= $error.PHP_EOL; 00086 00087 if ($web) { 00088 $result .= '</li>'.PHP_EOL; 00089 } 00090 } 00091 if ($web) { 00092 $result .= '</ol>'.PHP_EOL; 00093 } 00094 return $result; 00095 } 00096 00101 public function __toString() { 00102 return $this->to_string(false); 00103 } 00104 00105 } 00106 00107 final class libxml_errors_mgr { 00111 private $previous = false; 00112 00116 public function __construct($reset=false){ 00117 if ($reset) { 00118 error_messages::instance()->reset(); 00119 } 00120 $this->previous = libxml_use_internal_errors(true); 00121 libxml_clear_errors(); 00122 } 00123 00124 private function collect_errors ($filename=''){ 00125 $errors = libxml_get_errors(); 00126 static $error_types = array( 00127 LIBXML_ERR_ERROR => 'Error' 00128 ,LIBXML_ERR_FATAL => 'Fatal Error' 00129 ,LIBXML_ERR_WARNING => 'Warning' 00130 ); 00131 $result = array(); 00132 foreach($errors as $error){ 00133 $add = ''; 00134 if (!empty($filename)) { 00135 $add = " in {$filename}"; 00136 } elseif (!empty($error->file)) { 00137 $add = " in {$error->file}"; 00138 } 00139 $line = ''; 00140 if (!empty($error->line)) { 00141 $line = " at line {$error->line}"; 00142 } 00143 $err = "{$error_types[$error->level]}{$add}: {$error->message}{$line}"; 00144 error_messages::instance()->add($err); 00145 } 00146 libxml_clear_errors(); 00147 return $result; 00148 } 00149 00150 public function __destruct(){ 00151 $this->collect_errors(); 00152 if (!$this->previous) { 00153 libxml_use_internal_errors($this->previous); 00154 } 00155 } 00156 00157 public function collect() { 00158 $this->collect_errors(); 00159 } 00160 } 00161 00162 00163 function validate_xml($xml, $schema){ 00164 $result = false; 00165 $manifest_file = realpath($xml); 00166 $schema_file = realpath($schema); 00167 if (empty($manifest_file) || empty($schema_file)) { 00168 return false; 00169 } 00170 00171 $xml_error = new libxml_errors_mgr(); 00172 $manifest = new DOMDocument(); 00173 $doc->validateOnParse = false; 00174 $result = $manifest->load($manifest_file, LIBXML_NONET) && 00175 $manifest->schemaValidate($schema_file); 00176 00177 return $result; 00178 } 00179 00180 class cc_validate_type { 00181 const manifest_validator1 = 'cclibxml2validator.xsd' ; 00182 const assesment_validator1 = '/domainProfile_4/ims_qtiasiv1p2_localised.xsd'; 00183 const discussion_validator1 = '/domainProfile_6/imsdt_v1p0_localised.xsd' ; 00184 const weblink_validator1 = '/domainProfile_5/imswl_v1p0_localised.xsd' ; 00185 00186 const manifest_validator11 = 'cc11libxml2validator.xsd' ; 00187 const blti_validator11 = 'imslticc_v1p0p1.xsd' ; 00188 const assesment_validator11 = 'ccv1p1_qtiasiv1p2p1_v1p0.xsd'; 00189 const discussion_validator11 = 'ccv1p1_imsdt_v1p1.xsd' ; 00190 const weblink_validator11 = 'ccv1p1_imswl_v1p1.xsd' ; 00191 00195 protected $type = null; 00196 00200 protected $location = null; 00201 00202 public function __construct($type, $location){ 00203 $this->type = $type; 00204 $this->location = $location; 00205 } 00206 00212 public function validate($element) { 00213 $this->last_error = null; 00214 $celement = realpath($element); 00215 $cvalidator = realpath($this->location.DIRECTORY_SEPARATOR.$this->type); 00216 $result = (empty($celement) || empty($cvalidator)); 00217 if (!$result) { 00218 $xml_error = new libxml_errors_mgr(); 00219 $doc = new DOMDocument(); 00220 $doc->validateOnParse = false; 00221 $result = $doc->load($celement, LIBXML_NONET) && 00222 $doc->schemaValidate($cvalidator); 00223 } 00224 return $result; 00225 } 00226 00227 } 00228 00229 class manifest_validator extends cc_validate_type { 00230 public function __construct($location){ 00231 parent::__construct(self::manifest_validator11, $location); 00232 } 00233 } 00234 00235 class manifest10_validator extends cc_validate_type { 00236 public function __construct($location){ 00237 parent::__construct(self::manifest_validator1, $location); 00238 } 00239 } 00240 00241 class blti_validator extends cc_validate_type { 00242 public function __construct($location){ 00243 parent::__construct(self::blti_validator11, $location); 00244 } 00245 } 00246 00247 class assesment_validator extends cc_validate_type { 00248 public function __construct($location){ 00249 parent::__construct(self::assesment_validator11, $location); 00250 } 00251 } 00252 00253 class discussion_validator extends cc_validate_type { 00254 public function __construct($location){ 00255 parent::__construct(self::discussion_validator11, $location); 00256 } 00257 } 00258 00259 class weblink_validator extends cc_validate_type { 00260 public function __construct($location){ 00261 parent::__construct(self::weblink_validator11, $location); 00262 } 00263 } 00264