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; }