/** * Transform a URL depending on a given $baseUrl and how the * rendering engine is considering them. * * @param string $url A given URL (Uniform Resource Locator). * @param string $baseUrl a baseUrl (xml:base). * @return string A transformed URL. */ protected function transformUri($url, $baseUrl) { // Only relative URIs must be transformed while // taking xml:base into account. if (Url::isRelative($url) === false) { return $url; } $xmlBasePolicy = $this->getRenderingEngine()->getXmlBasePolicy(); switch ($xmlBasePolicy) { case AbstractMarkupRenderingEngine::XMLBASE_PROCESS: if (empty($baseUrl) === false) { return Url::rtrim($baseUrl) . '/' . Url::ltrim($url); } else { return $url; } break; default: return $url; break; } }
/** * Resolve what is the base URL to be used for the currently explored component. * * @return string A URL or the empty string ('') if no base URL could be resolved. */ protected function resolveXmlBase() { $stack = $this->getXmlBaseStack(); $stack->rewind(); $resolvedBase = ''; while ($stack->valid() === true) { if (($currentBase = $stack->current()) !== '') { if ($resolvedBase === '') { $resolvedBase = $currentBase; } else { $resolvedBase = Url::rtrim($currentBase) . '/' . Url::ltrim($resolvedBase); } } $stack->next(); } if ($stack->count() > 0) { $stack->pop(); } return $resolvedBase; }
/** * Copy all remote resource (absolute URLs to another host) contained in a rubricBlock into a dedicated directory. Remote resources * can be refereced by the following QTI classes/attributes: * * * a:href * * object:data * * img:src * * @param AssessmentTest $assessmentTest An AssessmentTest object. * @throws taoQtiTest_models_classes_QtiTestCompilationFailedException If a remote resource cannot be retrieved. */ protected function copyRemoteResources(RubricBlock $rubricBlock) { $ds = DIRECTORY_SEPARATOR; $tmpDir = tao_helpers_File::createTempDir(); $destPath = trim($this->getExtraPath(), $ds) . $ds . TAOQTITEST_REMOTE_FOLDER . $ds; // Search for all class-attributes in QTI-XML that might reference a remote file. $search = $rubricBlock->getComponentsByClassName(array('a', 'object', 'img')); foreach ($search as $component) { switch ($component->getQtiClassName()) { case 'object': $url = $component->getData(); break; case 'img': $url = $component->getSrc(); break; } if (isset($url) && !preg_match('@^' . ROOT_URL . '@', $url) && !Url::isRelative($url)) { $tmpFile = taoItems_helpers_Deployment::retrieveFile($url, $tmpDir); if ($tmpFile !== false) { $pathinfo = pathinfo($tmpFile); $handle = fopen($tmpFile, 'r'); $this->getPublicDirectory()->writeStream($destPath . $pathinfo['basename'], $handle); fclose($handle); unlink($tmpFile); $newUrl = TAOQTITEST_REMOTE_FOLDER . '/' . $pathinfo['basename']; switch ($component->getQtiClassName()) { case 'object': $component->setData($newUrl); break; case 'img': $component->setSrc($newUrl); break; } } else { $msg = "The remote resource referenced by '{$url}' could not be retrieved."; throw new taoQtiTest_models_classes_QtiTestCompilationFailedException($msg, $this->getResource(), taoQtiTest_models_classes_QtiTestCompilationFailedException::REMOTE_RESOURCE); } } } }
/** * @dataProvider invalidRelativeUrlProvider * @param unknown_type $url */ public function testInvalidRelativeUrl($url) { $this->assertFalse(Url::isRelative($url)); }
/** * Resolve include components. * * After the item has been loaded using the load or loadFromString method, * the include components can be resolved by calling this method. Files will * be included following the rules described by the XInclude specification. * * @param boolean $validate Whether or not validate files being included. * @throws \LogicException If the method is called prior the load or loadFromString method was called. * @throws \qtism\data\storage\xml\XmlStorageException If an error occured while parsing or validating files to be included. */ public function xInclude($validate = false) { if (($root = $this->getDocumentComponent()) !== false) { $baseUri = str_replace('\\', '/', $this->getDomDocument()->documentElement->baseURI); $pathinfo = pathinfo($baseUri); $basePath = $pathinfo['dirname']; $iterator = new QtiComponentIterator($root, array('include')); foreach ($iterator as $include) { $parent = $iterator->parent(); // Is the parent something we can deal with for replacement? $reflection = new ReflectionClass($parent); if ($reflection->hasMethod('getContent') === true && $parent->getContent() instanceof QtiComponentCollection) { $href = $include->getHref(); if (Url::isRelative($href) === true) { $href = Url::rtrim($basePath) . '/' . Url::ltrim($href); $doc = new XmlDocument(); $doc->load($href, $validate); $includeRoot = $doc->getDocumentComponent(); if ($includeRoot instanceof Flow) { // Derive xml:base... $xmlBase = Url::ltrim(str_replace($basePath, '', $href)); $xmlBasePathInfo = pathinfo($xmlBase); if ($xmlBasePathInfo['dirname'] !== '.') { $includeRoot->setXmlBase($xmlBasePathInfo['dirname'] . '/'); } } $parent->getContent()->replace($include, $includeRoot); } } } } else { $msg = "Cannot include fragments prior to loading any file."; throw new LogicException($msg); } }
public function testRtrim() { $this->assertEquals("/hello", Url::rtrim("/hello/\n")); }
/** * Copy all remote resource (absolute URLs to another host) contained in a rubricBlock into a dedicated directory. Remote resources * can be refereced by the following QTI classes/attributes: * * * a:href * * object:data * * img:src * * @param AssessmentTest $assessmentTest An AssessmentTest object. * @throws taoQtiTest_models_classes_QtiTestCompilationFailedException If a remote resource cannot be retrieved. */ protected function copyRemoteResources(RubricBlock $rubricBlock) { $publicCompiledDocDir = $this->getPublicDirectory()->getPath(); $ds = DIRECTORY_SEPARATOR; $destination = $publicCompiledDocDir . trim($this->getExtraPath(), $ds) . $ds . TAOQTITEST_REMOTE_FOLDER . $ds; // If remote directory does not exist yet, create it. if (file_exists($destination) === false) { mkdir($destination, 0770, true); } // Search for all class-attributes in QTI-XML that might reference a remote file. $search = $rubricBlock->getComponentsByClassName(array('a', 'object', 'img')); foreach ($search as $component) { switch ($component->getQtiClassName()) { case 'object': $url = $component->getData(); break; case 'img': $url = $component->getSrc(); break; } if (isset($url) && !preg_match('@^' . ROOT_URL . '@', $url) && !Url::isRelative($url)) { $finalDestination = taoItems_helpers_Deployment::retrieveFile($url, $destination); $pathinfo = pathinfo($finalDestination); if ($finalDestination !== false) { $newUrl = TAOQTITEST_REMOTE_FOLDER . '/' . $pathinfo['basename']; switch ($component->getQtiClassName()) { case 'object': $component->setData($newUrl); break; case 'img': $component->setSrc($newUrl); break; } } else { $msg = "The remote resource referenced by '{$url}' could not be retrieved."; throw new taoQtiTest_models_classes_QtiTestCompilationFailedException($msg, $this->getResource(), taoQtiTest_models_classes_QtiTestCompilationFailedException::REMOTE_RESOURCE); } } } }