Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 126 |
|
0.00% |
0 / 12 |
CRAP | |
0.00% |
0 / 1 |
| tao_models_classes_Parser | |
0.00% |
0 / 126 |
|
0.00% |
0 / 12 |
2652 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
90 | |||
| getSource | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validate | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| validateMultiple | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| isValid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getErrors | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| displayErrors | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| addError | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
20 | |||
| getContent | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
240 | |||
| addErrors | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| clearErrors | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getReport | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program 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 this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * |
| 25 | */ |
| 26 | use oat\oatbox\service\ServiceManager; |
| 27 | use oat\tao\model\upload\UploadService; |
| 28 | |
| 29 | /** |
| 30 | * The Parser enables you to load, parse and validate xml content from an xml |
| 31 | * Usually used for to load and validate the itemContent property. |
| 32 | * |
| 33 | * @access public |
| 34 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 35 | * @package tao |
| 36 | |
| 37 | */ |
| 38 | class tao_models_classes_Parser |
| 39 | { |
| 40 | /** |
| 41 | * XML content string |
| 42 | * |
| 43 | * @access protected |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $content = null; |
| 47 | |
| 48 | /** |
| 49 | * Short description of attribute source |
| 50 | * |
| 51 | * @access protected |
| 52 | * @var string |
| 53 | */ |
| 54 | protected $source = ''; |
| 55 | |
| 56 | /** |
| 57 | * Short description of attribute sourceType |
| 58 | * |
| 59 | * @access protected |
| 60 | * @var int |
| 61 | */ |
| 62 | protected $sourceType = 0; |
| 63 | |
| 64 | /** |
| 65 | * Short description of attribute errors |
| 66 | * |
| 67 | * @access protected |
| 68 | * @var array |
| 69 | */ |
| 70 | protected $errors = []; |
| 71 | |
| 72 | /** |
| 73 | * Short description of attribute valid |
| 74 | * |
| 75 | * @access protected |
| 76 | * @var boolean |
| 77 | */ |
| 78 | protected $valid = false; |
| 79 | |
| 80 | /** |
| 81 | * Short description of attribute fileExtension |
| 82 | * |
| 83 | * @access protected |
| 84 | * @var string |
| 85 | */ |
| 86 | protected $fileExtension = 'xml'; |
| 87 | |
| 88 | /** |
| 89 | * Short description of attribute SOURCE_FILE |
| 90 | * |
| 91 | * @access public |
| 92 | * @var int |
| 93 | */ |
| 94 | |
| 95 | public const SOURCE_FILE = 1; |
| 96 | |
| 97 | /** |
| 98 | * Short description of attribute SOURCE_URL |
| 99 | * |
| 100 | * @access public |
| 101 | * @var int |
| 102 | */ |
| 103 | public const SOURCE_URL = 2; |
| 104 | |
| 105 | /** |
| 106 | * Short description of attribute SOURCE_STRING |
| 107 | * |
| 108 | * @access public |
| 109 | * @var int |
| 110 | */ |
| 111 | public const SOURCE_STRING = 3; |
| 112 | |
| 113 | /** |
| 114 | * Current file is \oat\oatbox\filesystem\File object |
| 115 | */ |
| 116 | public const SOURCE_FLYFILE = 4; |
| 117 | |
| 118 | /** |
| 119 | * Short description of method __construct |
| 120 | * |
| 121 | * @access public |
| 122 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 123 | * @param string $source |
| 124 | * @param array $options |
| 125 | * @throws common_exception_Error |
| 126 | * @throws \common_Exception |
| 127 | * @throws \oat\oatbox\service\ServiceNotFoundException |
| 128 | */ |
| 129 | public function __construct($source, $options = []) |
| 130 | { |
| 131 | $sourceType = false; |
| 132 | |
| 133 | if ($source instanceof \oat\oatbox\filesystem\File) { |
| 134 | $sourceType = self::SOURCE_FLYFILE; |
| 135 | } elseif (is_string($source)) { |
| 136 | if (preg_match("/^<\?xml(.*)?/m", trim($source))) { |
| 137 | $sourceType = self::SOURCE_STRING; |
| 138 | } elseif (preg_match("/^http/", $source)) { |
| 139 | $sourceType = self::SOURCE_URL; |
| 140 | } elseif (is_file($source)) { |
| 141 | $sourceType = self::SOURCE_FILE; |
| 142 | } else { |
| 143 | $uploadFile = ServiceManager::getServiceManager() |
| 144 | ->get(UploadService::SERVICE_ID) |
| 145 | ->universalizeUpload($source); |
| 146 | if ($uploadFile instanceof \oat\oatbox\filesystem\File) { |
| 147 | $sourceType = self::SOURCE_FLYFILE; |
| 148 | $source = $uploadFile; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | if ($sourceType === false) { |
| 154 | throw new common_exception_Error( |
| 155 | "Denied content in the source parameter! " . get_class($this) |
| 156 | . " accepts either XML content, a URL to an XML Content or the path to a file but got " |
| 157 | . substr($source, 0, 500) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | $this->sourceType = $sourceType; |
| 162 | $this->source = $source; |
| 163 | |
| 164 | if (isset($options['extension'])) { |
| 165 | $this->fileExtension = $options['extension']; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | public function getSource() |
| 170 | { |
| 171 | return $this->source; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Short description of method validate |
| 176 | * |
| 177 | * @access public |
| 178 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 179 | * @param string schema |
| 180 | * @return boolean |
| 181 | */ |
| 182 | public function validate($schema = '') |
| 183 | { |
| 184 | //You know sometimes you think you have enough time, but it is not always true ... |
| 185 | //(timeout in hudson with the generis-hard test suite) |
| 186 | helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::MEDIUM); |
| 187 | |
| 188 | $content = $this->getContent(); |
| 189 | if (!empty($content)) { |
| 190 | try { |
| 191 | libxml_use_internal_errors(true); |
| 192 | |
| 193 | $dom = new DomDocument(); |
| 194 | $dom->formatOutput = true; |
| 195 | $dom->preserveWhiteSpace = false; |
| 196 | |
| 197 | $this->valid = $dom->loadXML($content); |
| 198 | |
| 199 | if ($this->valid && !empty($schema)) { |
| 200 | $this->valid = $dom->schemaValidate($schema); |
| 201 | } |
| 202 | |
| 203 | if (!$this->valid) { |
| 204 | $this->addErrors(libxml_get_errors()); |
| 205 | } |
| 206 | libxml_clear_errors(); |
| 207 | } catch (DOMException $de) { |
| 208 | $this->addError($de); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | helpers_TimeOutHelper::reset(); |
| 214 | return (bool) $this->valid; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Excecute parser validation and stops at the first valid one, and returns the identified schema |
| 219 | * |
| 220 | * @param array $xsds |
| 221 | * @return string |
| 222 | */ |
| 223 | public function validateMultiple($xsds = []) |
| 224 | { |
| 225 | $returnValue = ''; |
| 226 | |
| 227 | foreach ($xsds as $xsd) { |
| 228 | $this->errors = []; |
| 229 | if ($this->validate($xsd)) { |
| 230 | $returnValue = $xsd; |
| 231 | break; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return $returnValue; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Short description of method isValid |
| 240 | * |
| 241 | * @access public |
| 242 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 243 | * @return boolean |
| 244 | */ |
| 245 | public function isValid() |
| 246 | { |
| 247 | return (bool) $this->valid; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Short description of method getErrors |
| 252 | * |
| 253 | * @access public |
| 254 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 255 | * @return array |
| 256 | */ |
| 257 | public function getErrors() |
| 258 | { |
| 259 | $returnValue = $this->errors; |
| 260 | return (array) $returnValue; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Short description of method displayErrors |
| 265 | * |
| 266 | * @access public |
| 267 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 268 | * @param boolean htmlOutput |
| 269 | * @return string |
| 270 | */ |
| 271 | public function displayErrors($htmlOutput = true) |
| 272 | { |
| 273 | |
| 274 | $returnValue = (string) ''; |
| 275 | |
| 276 | foreach ($this->errors as $error) { |
| 277 | $returnValue .= $error['message']; |
| 278 | if (isset($error['file']) && isset($error['line'])) { |
| 279 | $returnValue .= ' in file ' . $error['file'] . ', line ' . $error['line']; |
| 280 | } |
| 281 | $returnValue .= PHP_EOL; |
| 282 | } |
| 283 | |
| 284 | if ($htmlOutput) { |
| 285 | $returnValue = nl2br($returnValue); |
| 286 | } |
| 287 | |
| 288 | return (string) $returnValue; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Short description of method addError |
| 293 | * |
| 294 | * @access protected |
| 295 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 296 | * @param mixed error |
| 297 | * @return mixed |
| 298 | */ |
| 299 | protected function addError($error) |
| 300 | { |
| 301 | |
| 302 | $this->valid = false; |
| 303 | |
| 304 | if ($error instanceof Exception) { |
| 305 | $this->errors[] = [ |
| 306 | 'file' => $error->getFile(), |
| 307 | 'line' => $error->getLine(), |
| 308 | 'message' => "[" . get_class($error) . "] " . $error->getMessage() |
| 309 | ]; |
| 310 | } elseif ($error instanceof LibXMLError) { |
| 311 | $this->errors[] = [ |
| 312 | 'file' => $error->file, |
| 313 | 'line' => $error->line, |
| 314 | 'message' => "[" . get_class($error) . "] " . $error->message |
| 315 | ]; |
| 316 | } elseif (is_string($error)) { |
| 317 | $this->errors[] = [ |
| 318 | 'message' => $error |
| 319 | ]; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get XML content. |
| 325 | * |
| 326 | * @access protected |
| 327 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 328 | * @param boolean $refresh load content again. |
| 329 | * @return string |
| 330 | */ |
| 331 | public function getContent($refresh = false) |
| 332 | { |
| 333 | if ($this->content === null || $refresh) { |
| 334 | try { |
| 335 | switch ($this->sourceType) { |
| 336 | case self::SOURCE_FILE: |
| 337 | //check file |
| 338 | if (!file_exists($this->source)) { |
| 339 | throw new Exception("File {$this->source} not found."); |
| 340 | } |
| 341 | if (!is_readable($this->source)) { |
| 342 | throw new Exception("Unable to read file {$this->source}."); |
| 343 | } |
| 344 | if (!preg_match("/\.{$this->fileExtension}$/", basename($this->source))) { |
| 345 | throw new Exception( |
| 346 | "Wrong file extension in " . basename($this->source) |
| 347 | . ", {$this->fileExtension} extension is expected" |
| 348 | ); |
| 349 | } |
| 350 | if (!tao_helpers_File::securityCheck($this->source)) { |
| 351 | throw new Exception("{$this->source} seems to contain some security issues"); |
| 352 | } |
| 353 | $this->content = file_get_contents($this->source); |
| 354 | break; |
| 355 | case self::SOURCE_URL: |
| 356 | //only same domain |
| 357 | if (!preg_match("/^" . preg_quote(BASE_URL, '/') . "/", $this->source)) { |
| 358 | throw new Exception("The given uri must be in the domain {$_SERVER['HTTP_HOST']}"); |
| 359 | } |
| 360 | $this->content = tao_helpers_Request::load($this->source, true); |
| 361 | break; |
| 362 | case self::SOURCE_STRING: |
| 363 | $this->content = $this->source; |
| 364 | break; |
| 365 | case self::SOURCE_FLYFILE: |
| 366 | if (! $this->source->exists()) { |
| 367 | throw new Exception( |
| 368 | 'Source file does not exists ("' . $this->source->getBasename() . '").' |
| 369 | ); |
| 370 | } |
| 371 | if (! $this->content = $this->source->read()) { |
| 372 | throw new Exception('Unable to read file ("' . $this->source->getBasename() . '").'); |
| 373 | } |
| 374 | break; |
| 375 | } |
| 376 | } catch (Exception $e) { |
| 377 | $this->addError($e); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return $this->content; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Short description of method addErrors |
| 386 | * |
| 387 | * @access protected |
| 388 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 389 | * @param array errors |
| 390 | * @return mixed |
| 391 | */ |
| 392 | protected function addErrors($errors) |
| 393 | { |
| 394 | |
| 395 | foreach ($errors as $error) { |
| 396 | $this->addError($error); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Short description of method clearErrors |
| 402 | * |
| 403 | * @access protected |
| 404 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 405 | * @return mixed |
| 406 | */ |
| 407 | protected function clearErrors() |
| 408 | { |
| 409 | $this->errors = []; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Creates a report without title of the parsing result |
| 414 | * @return common_report_Report |
| 415 | */ |
| 416 | public function getReport() |
| 417 | { |
| 418 | if ($this->isValid()) { |
| 419 | return common_report_Report::createSuccess(''); |
| 420 | } else { |
| 421 | $report = new common_report_Report(''); |
| 422 | foreach ($this->getErrors() as $error) { |
| 423 | $report->add(common_report_Report::createFailure($error['message'])); |
| 424 | } |
| 425 | return $report; |
| 426 | } |
| 427 | } |
| 428 | } |