public function render(Register $ParameterOutput) { $doc = new XMLDocument(); $root = $doc->createElement($this->parameters()->{'root-element'}); try { $static = new XMLDocument(); $node = $static->loadXML($this->parameters()->xml); $root->appendChild($doc->importNode($static->documentElement, true)); } catch (FrontendPageNotFoundException $error) { FrontendPageNotFoundExceptionHandler::render($error); } catch (Exception $error) { $root->appendChild($doc->createElement('error', General::sanitize($error->getMessage()))); } $doc->appendChild($root); return $doc; }
public function __buildPageXML($view, View $parent = null) { $result = new XMLDocument(); $xView = $result->createElement('view'); $xView->setAttribute('handle', $view->handle); $xView->appendChild($result->createElement('title', $view->title)); ## Types if (is_array($view->types) && count($view->types) > 0) { $types = $result->createElement('types'); foreach ($view->types as $t) { $types->appendChild($result->createElement('item', General::sanitize($t))); } $xView->appendChild($types); } ## Children foreach ($view->children() as $child) { $node = $this->__buildPageXML($child, $view); if (!is_null($node)) { $xView->appendChild($result->importNode($node, true)); } } return $xView; }
public function render(Register $Parameters, XMLDocument &$Document = NULL, DocumentHeaders &$Headers = NULL) { $ParameterOutput = new Register(); if (!is_null($Headers)) { $Headers->append('Content-Type', $this->{'content-type'}); } else { header('Content-Type: ' . $this->{'content-type'}); } if (is_null($Document)) { $Document = new XMLDocument(); $Document->appendChild($Document->createElement('data')); } $root = $Document->documentElement; $datasources = $events = array(); $events_wrapper = $Document->createElement('events'); $root->appendChild($events_wrapper); if (is_array($this->about()->{'events'}) && !empty($this->about()->{'events'})) { $events = $this->about()->{'events'}; } if (is_array($this->about()->{'data-sources'}) && !empty($this->about()->{'data-sources'})) { $datasources = $this->about()->{'data-sources'}; } #### # Delegate: FrontendEventsAppend # Description: Append additional Events. # Global: Yes Extension::notify('FrontendEventsAppend', '/frontend/', array('events' => &$events)); if (!empty($events)) { $postdata = General::getPostData(); $events_ordered = array(); foreach ($events as $handle) { $events_ordered[] = Event::loadFromHandle($handle); } uasort($events_ordered, array($this, '__cbSortEventsByPriority')); foreach ($events_ordered as $e) { if (!$e->canTrigger($postdata)) { continue; } $fragment = $e->trigger($ParameterOutput, $postdata); if ($fragment instanceof DOMDocument && !is_null($fragment->documentElement)) { $node = $Document->importNode($fragment->documentElement, true); $events_wrapper->appendChild($node); } } } #### # Delegate: FrontendDataSourceAppend # Description: Append additional DataSources. # Global: Yes Extension::notify('FrontendDataSourcesAppend', '/frontend/', array('datasources' => &$datasources)); // Find dependancies and order accordingly $datasource_pool = array(); $dependency_list = array(); $datasources_ordered = array(); $all_dependencies = array(); foreach ($datasources as $handle) { $datasource_pool[$handle] = Datasource::loadFromHandle($handle); $dependency_list[$handle] = $datasource_pool[$handle]->parameters()->dependencies; } $datasources_ordered = $this->__sortByDependencies($dependency_list); if (!empty($datasources_ordered)) { foreach ($datasources_ordered as $handle) { $ds = $datasource_pool[$handle]; try { $fragment = $ds->render($ParameterOutput); } catch (FrontendPageNotFoundException $e) { FrontendPageNotFoundExceptionHandler::render($e); } if ($fragment instanceof DOMDocument && !is_null($fragment->documentElement)) { $node = $Document->importNode($fragment->documentElement, true); $root->appendChild($node); } } } if ($ParameterOutput->length() > 0) { foreach ($ParameterOutput as $p) { $Parameters->{$p->key} = $p->value; } } #### # Delegate: FrontendParamsPostResolve # Description: Access to the resolved param pool, including additional parameters provided by Data Source outputs # Global: Yes Extension::notify('FrontendParamsPostResolve', '/frontend/', array('params' => $Parameters)); $element = $Document->createElement('parameters'); $root->appendChild($element); foreach ($Parameters as $key => $parameter) { if (is_array($parameter->value) && count($parameter->value) > 1) { $p = $Document->createElement($key); $p->setAttribute('value', (string) $parameter); foreach ($parameter->value as $v) { $p->appendChild($Document->createElement('item', (string) $v)); } $element->appendChild($p); } else { $element->appendChild($Document->createElement($key, (string) $parameter)); } } $template = $this->template; #### # Delegate: FrontendTemplatePreRender # Description: Access to the template source, before it is rendered. # Global: Yes Extension::notify('FrontendTemplatePreRender', '/frontend/', array('document' => $Document, 'template' => &$template)); $this->template = $template; // When the XSLT executes, it uses the CWD as set here $cwd = getcwd(); chdir(WORKSPACE); $output = XSLProc::transform($Document, $this->template, XSLProc::XML, $Parameters->toArray(), array()); chdir($cwd); if (XSLProc::hasErrors()) { throw new XSLProcException('Transformation Failed'); } /* header('Content-Type: text/plain; charset=utf-8'); $Document->formatOutput = true; print $Document->saveXML(); die(); */ return $output; }
public function render(Register $ParameterOutput) { $result = null; $doc = new XMLDocument(); if (isset($this->parameters()->url)) { $this->parameters()->url = self::replaceParametersInString($this->parameters()->url, $ParameterOutput); } if (isset($this->parameters()->xpath)) { $this->parameters()->xpath = self::replaceParametersInString($this->parameters()->xpath, $ParameterOutput); } $cache_id = md5($this->parameters()->url . serialize($this->parameters()->namespaces) . $this->parameters()->xpath); $cache = Cache::instance(); $cachedData = $cache->read($cache_id); $writeToCache = false; $force_empty_result = false; $valid = true; $result = NULL; $creation = DateTimeObj::get('c'); if (isset($this->parameters()->timeout)) { $timeout = (int) max(1, $this->parameters()->timeout); } if (!is_array($cachedData) || empty($cachedData) || time() - $cachedData['creation'] > $this->parameters()->{'cache-timeout'} * 60) { if (Mutex::acquire($cache_id, $timeout, TMP)) { $start = precision_timer(); $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $this->parameters()->url); $ch->setopt('TIMEOUT', $this->parameters()->timeout); $xml = $ch->exec(); $writeToCache = true; $end = precision_timer('STOP', $start); $info = $ch->getInfoLast(); Mutex::release($cache_id, TMP); $xml = trim($xml); if ((int) $info['http_code'] != 200 || !preg_match('/(xml|plain|text)/i', $info['content_type'])) { $writeToCache = false; if (is_array($cachedData) && !empty($cachedData)) { $xml = trim($cachedData['data']); $valid = false; $creation = DateTimeObj::get('c', $cachedData['creation']); } else { $result = $doc->createElement($this->parameters()->{'root-element'}); $result->setAttribute('valid', 'false'); if ($end > $timeout) { $result->appendChild($doc->createElement('error', sprintf('Request timed out. %d second limit reached.', $timeout))); } else { $result->appendChild($doc->createElement('error', sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type']))); } return $result; } } elseif (strlen($xml) > 0 && !General::validateXML($xml, $errors)) { $writeToCache = false; if (is_array($cachedData) && !empty($cachedData)) { $xml = trim($cachedData['data']); $valid = false; $creation = DateTimeObj::get('c', $cachedData['creation']); } else { $result = $doc->createElement($this->parameters()->{'root-element'}, $doc->createElement('error', __('XML returned is invalid.')), array('valid' => 'false')); return $result; } } elseif (strlen($xml) == 0) { $force_empty_result = true; } } elseif (is_array($cachedData) && !empty($cachedData)) { $xml = trim($cachedData['data']); $valid = false; $creation = DateTimeObj::get('c', $cachedData['creation']); if (empty($xml)) { $force_empty_result = true; } } else { $force_empty_result = true; } } else { $xml = trim($cachedData['data']); $creation = DateTimeObj::get('c', $cachedData['creation']); } if (!$force_empty_result) { $result = new XMLDocument(); $root = $result->createElement($this->parameters()->{'root-element'}); //XPath Approach, saves Transforming the Document. $xDom = new XMLDocument(); $xDom->loadXML($xml); if ($xDom->hasErrors()) { $root->setAttribute('valid', 'false'); $root->appendChild($result->createElement('error', __('XML returned is invalid.'))); $messages = $result->createElement('messages'); foreach ($xDom->getErrors() as $e) { if (strlen(trim($e->message)) == 0) { continue; } $messages->appendChild($result->createElement('item', General::sanitize($e->message))); } $root->appendChild($messages); } else { if ($writeToCache) { $cache->write($cache_id, $xml); } $xpath = new DOMXPath($xDom); ## Namespaces if (is_array($this->parameters()->namespaces) && !empty($this->parameters()->namespaces)) { foreach ($this->parameters()->namespaces as $index => $namespace) { $xpath->registerNamespace($namespace['name'], $namespace['uri']); } } $xpath_list = $xpath->query($this->parameters()->xpath); foreach ($xpath_list as $node) { if ($node instanceof XMLDocument) { $root->appendChild($result->importNode($node->documentElement, true)); } else { $root->appendChild($result->importNode($node, true)); } } $root->setAttribute('status', $valid === true ? 'fresh' : 'stale'); $root->setAttribute('creation', $creation); } } if (!$root->hasChildNodes() || $force_empty_result) { $this->emptyXMLSet($root); } $result->appendChild($root); return $result; }