/** * Reads the routes from the provided file * @param zibo\library\filesystem\File $file * @return array Array with Route objects as value and their path as key */ private function readRoutesFromFile(File $file) { $dom = new Document(); $dom->setRelaxNGFileFromConfig(self::CONFIG_ROUTES_RNG); $dom->load($file); return $this->getRoutesFromElement($file, $dom->documentElement); }
/** * Adds all the images from the page to the web * @param zibo\library\spider\Web $web The spider web * @param zibo\library\spider\WebNode $prey The current prey in the web * @param string $baseUrl Base URL of the crawl * @param string $preyBaseUrl Base URL of the prey * @param zibo\library\xml\dom\Document $dom The DOM document of the current prey * @return null */ protected function biteDocument(Web $web, WebNode $prey, $baseUrl, $preyBaseUrl, Document $dom) { $images = $dom->getElementsByTagName('img'); foreach ($images as $image) { $url = $image->getAttribute('src'); if (!$url) { continue; } $url = $this->getAbsoluteUrl($url, $baseUrl, $preyBaseUrl); $link = $web->getNode($url); $link->addType(WebNode::TYPE_IMAGE); $link->addReference($prey); $prey->addLink($link); } }
/** * Adds all the used javascripts to the web * @param zibo\library\spider\Web $web The spider web * @param zibo\library\spider\WebNode $prey The current prey in the web * @param string $baseUrl Base URL of the crawl * @param string $preyBaseUrl Base URL of the prey * @param zibo\library\xml\dom\Document $dom The DOM document of the current prey * @return null */ protected function biteDocument(Web $web, WebNode $prey, $baseUrl, $preyBaseUrl, Document $dom) { $scripts = $dom->getElementsByTagName('script'); foreach ($scripts as $script) { $type = $script->getAttribute('type'); $url = $script->getAttribute('src'); if ($type != 'text/javascript' || !$url) { continue; } $url = $this->getAbsoluteUrl($url, $baseUrl, $preyBaseUrl); $link = $web->getNode($url); $link->addType(WebNode::TYPE_JS); $link->addReference($prey); $prey->addLink($link); } }
/** * Adds the URL's from the anchors in the page to the web * @param zibo\library\spider\Web $web The spider web * @param zibo\library\spider\WebNode $prey The current prey in the web * @param string $baseUrl Base URL of the crawl * @param string $preyBaseUrl Base URL of the prey * @param zibo\library\xml\dom\Document $dom The DOM document of the current prey * @return null */ protected function biteDocument(Web $web, WebNode $prey, $baseUrl, $preyBaseUrl, Document $dom) { $anchors = $dom->getElementsByTagName('a'); foreach ($anchors as $anchor) { $url = $anchor->getAttribute('href'); if (!$url || String::startsWith($url, '#')) { continue; } if (!String::startsWith($url, 'mailto:')) { $url = $this->getAbsoluteUrl($url, $baseUrl, $preyBaseUrl); } $node = $web->getNode($url); $node->addReference($prey); $prey->addLink($node); } }
/** * Adds the used style sheets to the web * @param zibo\library\spider\Web $web The spider web * @param zibo\library\spider\WebNode $prey The current prey in the web * @param string $baseUrl Base URL of the crawl * @param string $preyBaseUrl Base URL of the prey * @param zibo\library\xml\dom\Document $dom The DOM document of the current prey * @return null */ protected function biteDocument(Web $web, WebNode $prey, $baseUrl, $preyBaseUrl, Document $dom) { $links = $dom->getElementsByTagName('link'); foreach ($links as $link) { $type = $link->getAttribute('type'); $rel = $link->getAttribute('rel'); $url = $link->getAttribute('href'); if ($type != 'text/css' || $rel != 'stylesheet' || !$url) { continue; } $url = $this->getAbsoluteUrl($url, $baseUrl, $preyBaseUrl); $link = $web->getNode($url); $link->addType(WebNode::TYPE_CSS); $link->addReference($prey); $prey->addLink($link); } }
/** * Gets a module DOM element from a module * @param zibo\library\xml\dom\Document $dom the document of the new element * @param zibo\admin\model\Module $module The module for the DOM element * @param string $elementName Name of the new module DOM element * @return DOMElement DOM element representing the provided module */ private function getModuleElementFromModule(Document $dom, Module $module, $elementName) { $element = $dom->createElementNS(self::XML_NAMESPACE, $elementName); $element->setAttribute(self::ATTRIBUTE_NAMESPACE, $module->getNamespace()); $element->setAttribute(self::ATTRIBUTE_NAME, $module->getName()); $element->setAttribute(self::ATTRIBUTE_VERSION, $module->getVersion()); $ziboVersion = $module->getZiboVersion(); if (!empty($ziboVersion)) { $element->setAttribute(self::ATTRIBUTE_VERSION_ZIBO, $ziboVersion); } $path = $module->getPath(); if (!empty($path)) { $element->setAttribute(self::ATTRIBUTE_PATH, $path->getPath()); } $dependencies = $module->getDependencies(); foreach ($dependencies as $dependency) { $dependencyElement = $this->getModuleElementFromModule($dom, $dependency, self::TAG_DEPENDENCY); $element->appendChild($dependencyElement); } return $element; }
/** * Gets a request object from the provided request XML * @param string $xml The XML of a XML-RPC request * @return Request */ public static function fromXMLString($xml) { $xml = trim($xml); if (empty($xml)) { throw new XmlRpcException('Empty request recieved', 1); } $dom = new Document('1.0', 'utf-8'); $dom->preserveWhiteSpace = false; $dom->setRelaxNGFileFromConfig(self::CONFIG_REQUEST_RNG); try { $dom->loadXML($xml); } catch (Exception $exception) { throw new XmlRpcException($xml, 0, $exception); } $element = $dom->documentElement; return new self($element); }
public function testLoadXMLWithValidXML() { $doc = new Document('1.0', 'utf-8'); $doc->loadXML('<?xml version="1.0" encoding="UTF-8"?><root><valid/></root>'); }
/** * Bites a prey to gather the needed information * @param WebNode $prey The current node to check * @param string $preyBaseUrl The base URL of the prey * @param string $preyBasePath The base path of the prey * @return null */ private function bite(WebNode $prey, $preyBaseUrl, $preyBasePath) { $dom = null; $response = $prey->getResponse(); $contentType = $response->getHeader('Content-Type'); $content = $response->getContent(); if (String::startsWith($contentType, 'text/html') && $content) { $dom = new Document('1.0', 'utf8'); try { $result = @$dom->loadHTML($content); if (!$result) { $error = error_get_last(); throw new Exception($error['message']); } } catch (Exception $exception) { $prey->setError($exception->getMessage()); } } foreach ($this->bites as $bite) { $bite->bite($this->web, $prey, $preyBaseUrl, $preyBasePath, $dom); } }
/** * Gets the index element for the provided index * @param zibo\library\xml\dom\Document $dom * @param zibo\library\database\definition\Index $index Index to get the element from * @return DOMElement */ protected function getElementFromIndex(Document $dom, Index $index) { $indexName = $index->getName(); $indexElement = $dom->createElement(self::TAG_INDEX); $indexElement->setAttribute(self::ATTRIBUTE_NAME, $indexName); $fields = $index->getFields(); foreach ($fields as $field) { $fieldElement = $dom->createElement(self::TAG_INDEX_FIELD); $fieldElement->setAttribute(self::ATTRIBUTE_NAME, $field->getName()); $indexElement->appendChild($fieldElement); } return $indexElement; }
/** * Reads the routes from the provided file * @param zibo\library\filesystem\File $file * @return array Array with Route objects as value and their path as key */ private function readRoutesFromFile(File $file) { $dom = new Document(); $relaxNg = $this->zibo->getConfigValue(self::CONFIG_RNG); if ($relaxNg) { $dom->setRelaxNGFile($relaxNg); } $dom->load($file); return $this->getRoutesFromElement($file, $dom->documentElement); }
/** * Adds a module to the repository * @param zibo\library\filesystem\File $pharFile Path to the phar file of the module * @return array Array with Module instances of the installed modules */ public function addModule(File $pharFile) { $moduleIO = new XmlModuleIO(); $modules = $moduleIO->readModules($pharFile); $zibo = Zibo::getInstance(); foreach ($modules as $module) { $namespace = $module->getNamespace(); $name = $module->getName(); $version = $module->getVersion(); $ziboVersion = $module->getZiboVersion(); $zibo->runEvent(Zibo::EVENT_LOG, $namespace . '.' . $name . ' ' . $version, '', 0, RepositoryModule::LOG_NAME); $query = $this->createQuery(); $xpath = new DOMXPath($this->indexDom); $modulesElement = $xpath->query($query)->item(0); $query = $this->createModuleQuery($namespace, $name); $moduleElement = $xpath->query($query, $modulesElement)->item(0); $zibo->runEvent(Zibo::EVENT_LOG, 'module element found?', is_object($moduleElement) ? $moduleElement->ownerDocument->saveXML($moduleElement) : $moduleElement); if (!$moduleElement) { $moduleElement = $this->indexDom->createElement(RepositoryModule::TAG_MODULE); $moduleElement->setAttribute(RepositoryModule::ATTRIBUTE_NAMESPACE, $namespace); $moduleElement->setAttribute(RepositoryModule::ATTRIBUTE_NAME, $name); $modulesElement->appendChild($moduleElement); } else { $query = $this->createVersionQuery($version); $versionElement = $xpath->query($query, $moduleElement)->item(0); if ($versionElement) { $zibo->runEvent(Zibo::EVENT_LOG, 'version element found?', is_object($versionElement) ? $versionElement->ownerDocument->saveXML($versionElement) : $versionElement); throw new ModuleVersionAlreadyExistsException("Version {$version} of module {$namespace}/{$name} already exists in the repository", $namespace, $name, $version); } } $versionElement = $this->indexDom->createElement(RepositoryModule::TAG_VERSION); $versionElement->setAttribute(RepositoryModule::ATTRIBUTE_VERSION, $version); $versionElement->setAttribute(RepositoryModule::ATTRIBUTE_ZIBO_VERSION, $ziboVersion); $dependencies = $module->getDependencies(); foreach ($dependencies as $dependency) { $dependencyElement = $this->indexDom->createElement(RepositoryModule::TAG_DEPENDENCY); $dependencyElement->setAttribute(RepositoryModule::ATTRIBUTE_NAMESPACE, $dependency->getNamespace()); $dependencyElement->setAttribute(RepositoryModule::ATTRIBUTE_NAME, $dependency->getName()); $dependencyElement->setAttribute(RepositoryModule::ATTRIBUTE_VERSION, $dependency->getVersion()); $versionElement->appendChild($dependencyElement); // should we also check if the dependencies are met in the repository, otherwise fail and show why ??? // dependency version acts as a minimal version AFAIK } // search the first existing version that has a version number lower than the one we want to insert // we will insert the new version before that existing version, to keep the version with the highest version number at the top $firstLowerVersionElement = null; foreach ($moduleElement->childNodes as $existingVersionElement) { if (version_compare($existingVersionElement->getAttribute(RepositoryModule::ATTRIBUTE_VERSION), $version) === -1) { $firstLowerVersionElement = $existingVersionElement; break; } } if ($firstLowerVersionElement) { $moduleElement->insertBefore($versionElement, $firstLowerVersionElement); } else { $moduleElement->appendChild($versionElement); } $moduleFile = $this->getModuleFile($namespace, $name, $version); $pharFile->copy($moduleFile); } $this->isIndexDirty = true; return $modules; }
/** * Reads the index of this repository * @return null */ private function readIndexFile() { $indexFile = new File($this->directory, self::INDEX_FILE); $dom = new Document('1.0', 'utf-8'); $dom->formatOutput = true; $dom->preserveWhiteSpace = false; if (!$indexFile->exists()) { Zibo::getInstance()->runEvent(Zibo::EVENT_LOG, 'Initializing new index file', $indexFile, 0, RepositoryModule::LOG_NAME); $modulesElement = $dom->createElement(RepositoryModule::TAG_MODULES); $repositoryElement = $dom->createElement(RepositoryModule::TAG_REPOSITORY); $repositoryElement->appendChild($modulesElement); $dom->appendChild($repositoryElement); } else { $dom->setRelaxNGFileFromConfig(self::CONFIG_REPOSITORY_RNG); $success = @$dom->load($indexFile); if (!$success) { $error = error_get_last(); throw new ZiboException("Failed loading {$indexFile} into a DOM tree: " . $error['message']); } } $this->indexDom = $dom; }
/** * Reads the dependencies from the provided file and adds them to the * provided container * @param zibo\core\di\DependencyContainer $container * @param zibo\library\filesystem\File $file * @return null */ private function readDependencies(DependencyContainer $container, File $file) { $dom = new Document(); $dom->load($file); $dependencyElements = $dom->getElementsByTagName(self::TAG_DEPENDENCY); foreach ($dependencyElements as $dependencyElement) { $interface = $dependencyElement->getAttribute(self::ATTRIBUTE_INTERFACE); $className = $dependencyElement->getAttribute(self::ATTRIBUTE_CLASS); $id = $dependencyElement->getAttribute(self::ATTRIBUTE_ID); if (!$id) { $id = null; } $dependency = new Dependency($className, $id); $this->readCalls($dependency, $dependencyElement); $container->addDependency($interface, $dependency); } }