/** * Get the root SimpleXMLElement object of the currently parsed manifest. * * @throws common_exception_Error * @return SimpleXMLElement */ private function getSimpleXMLElement() { switch ($this->sourceType) { case self::SOURCE_FILE: $xml = simplexml_load_file($this->source); break; case self::SOURCE_URL: $xmlContent = tao_helpers_Request::load($this->source, true); $xml = simplexml_load_string($xmlContent); break; case self::SOURCE_STRING: $xml = simplexml_load_string($this->source); break; default: throw new taoItems_models_classes_Import_ImportException('Invalid sourceType'); } if ($xml === false) { $this->addErrors(libxml_get_errors()); libxml_clear_errors(); throw new common_exception_Error('Invalid XML.'); } return $xml; }
/** * Get XML content. * * @access protected * @author Aleh Hutnikau, <*****@*****.**> * @param boolean $refresh load content again. * @return string */ protected function getContent($refresh = false) { if ($this->content === null || $refresh) { try { switch ($this->sourceType) { case self::SOURCE_FILE: //check file if (!file_exists($this->source)) { throw new Exception("File {$this->source} not found."); } if (!is_readable($this->source)) { throw new Exception("Unable to read file {$this->source}."); } if (!preg_match("/\\.{$this->fileExtension}\$/", basename($this->source))) { throw new Exception("Wrong file extension in " . basename($this->source) . ", {$this->fileExtension} extension is expected"); } if (!tao_helpers_File::securityCheck($this->source)) { throw new Exception("{$this->source} seems to contain some security issues"); } $this->content = file_get_contents($this->source); break; case self::SOURCE_URL: //only same domain if (!preg_match("/^" . preg_quote(BASE_URL, '/') . "/", $this->source)) { throw new Exception("The given uri must be in the domain {$_SERVER['HTTP_HOST']}"); } $this->content = tao_helpers_Request::load($this->source, true); break; case self::SOURCE_STRING: $this->content = $this->source; break; case self::SOURCE_FLYFILE: if (!$this->source->exists()) { throw new common_Exception('Source file does not exists ("' . $this->source->getPath() . '").'); } if (!($this->content = $this->source->read())) { throw new common_Exception('Unable to read file ("' . $this->source->getPath() . '").'); } break; } } catch (Exception $e) { $this->addError($e); } } return $this->content; }
/** * Short description of method validate * * @access public * @author Bertrand Chevrier, <*****@*****.**> * @param string schema * @return boolean */ public function validate($schema = '') { //You know sometimes you think you have enough time, but it is not always true ... //(timeout in hudson with the generis-hard test suite) helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::MEDIUM); $forced = $this->valid; $this->valid = true; try { switch ($this->sourceType) { case self::SOURCE_FILE: //check file if (!file_exists($this->source)) { throw new Exception("File {$this->source} not found."); } if (!is_readable($this->source)) { throw new Exception("Unable to read file {$this->source}."); } if (!preg_match("/\\.{$this->fileExtension}\$/", basename($this->source))) { throw new Exception("Wrong file extension in " . basename($this->source) . ", {$this->fileExtension} extension is expected"); } if (!tao_helpers_File::securityCheck($this->source)) { throw new Exception("{$this->source} seems to contain some security issues"); } break; case self::SOURCE_URL: //only same domain if (!preg_match("/^" . preg_quote(BASE_URL, '/') . "/", $this->source)) { throw new Exception("The given uri must be in the domain {$_SERVER['HTTP_HOST']}"); } break; } } catch (Exception $e) { if ($forced) { throw $e; } else { $this->addError($e); } } if ($this->valid && !$forced) { //valida can be true if forceValidation has been called $this->valid = false; try { libxml_use_internal_errors(true); $dom = new DomDocument(); $loadResult = false; switch ($this->sourceType) { case self::SOURCE_FILE: $loadResult = $dom->load($this->source); break; case self::SOURCE_URL: $xmlContent = tao_helpers_Request::load($this->source, true); $loadResult = $dom->loadXML($xmlContent); break; case self::SOURCE_STRING: $loadResult = $dom->loadXML($this->source); break; } if ($loadResult) { if (!empty($schema)) { $this->valid = $dom->schemaValidate($schema); } else { $this->valid = true; //only well-formed } } if (!$this->valid) { $this->addErrors(libxml_get_errors()); } libxml_clear_errors(); } catch (DOMException $de) { $this->addError($de); } } $returnValue = $this->valid; helpers_TimeOutHelper::reset(); return (bool) $returnValue; }