protected function transform($xml, $name, $value) { if (isset($this->output[$name]) && !empty($this->output[$name])) { foreach ($this->output[$name] as $transform) { switch ($transform) { case 'none': return $xml->element($name, htmlspecialchars($value)); case 'xml': return $this->xml($xml, $name, $value); case 'vars': $value = $this->vars->apply($value); break; case 'json2xml': return xml::assoc_node($xml, $name, json_decode($value, true)); case 'nl2p': return $this->nl2p($xml, $name, $value); default: runtime_error('Unknown transform: ' . $transform); } } return $xml->element($name, htmlspecialchars($value)); } else { return $this->nl2br($xml, $name, $value); } }
function insert($name, $value) { $top =& $this->vars[count($this->vars) - 1]; preg_match('/\\A[\\w:]+\\w\\Z/', $name) or runtime_error('Invalid characters in variable name: ' . $name); !isset($top[$name]) or runtime_error('Duplicate variable name: ' . $name); $top[$name] = $this->apply($value); }
function query($args, $document) { $this->validate($args); $xml = new xml(); switch ($this->method) { case 'record': isset($args['host']) or runtime_error('GeoIP host parameter not found'); $root = $xml->element($this->root[0]); $xml->append($root); if ($record = geoip_record_by_name($args['host'])) { $country = $xml->element('country'); $root->append($country); $country->append($xml->element('alpha2', $record['country_code'])); $country->append($xml->element('alpha3', $record['country_code3'])); $country->append($xml->element('name', $record['country_name'])); $root->append($xml->element('region', $record['region'])); $root->append($xml->element('city', $record['city'])); $root->append($xml->element('latitude', $record['latitude'])); $root->append($xml->element('longitude', $record['longitude'])); } break; default: runtime_error('Unknown GeoIP method: ' . $this->method); } return $xml; }
function setup($language, $country) { in_array($language, self::$languages) or runtime_error('Unknown language alpha2 code: ' . $language); in_array($country, self::$countries) or runtime_error('Unknown country alpha2 code: ' . $country); $this->language = $language; $this->country = $country; }
private function flush() { if (!$this->ready()) { $message = implode(', ', $this->stack); $this->stack = array(); runtime_error($message); } }
private function to_xml($result) { switch ($this->content_type) { case 'xml': return xml::parse($result); case 'json': return xml::json($this->root[0], $result); default: runtime_error('Unknown HTTP procedure content type: ' . $this->content_type); } }
static function decode($expression) { $args = array(); $separator = ','; if (preg_match('/\\A\\[(.)\\] +(.*)\\Z/', $expression, $m)) { $separator = $m[1]; $expression = $m[2]; } if (trim($expression) !== '') { foreach (explode($separator, $expression) as $nvp) { preg_match('/\\A(\\w+) +\\-> +(.+)\\Z/', trim($nvp), $match) or runtime_error('Bad arguments syntax: ' . $expression); $args[trim($match[1])] = self::unquote($match[2]); } } return $args; }
function offsetUnset($offset) { runtime_error('Method not implemented'); }
private function replace_xsl($document) { $extensions = false; foreach ($document->query('//' . implode(':*|//', array_keys($this->xsl)) . ':*') as $node) { isset($this->xsl[$node->ns()][$node->name()]) or runtime_error('Unknown user extension element: ' . $node->name()); $extensions = true; $handler = $this->xsl[$node->ns()][$node->name()]['handler']; self::replace_node($document, $node, $handler($node)); } return $extensions; }
static function assoc_node($xml, $name, $assoc) { is_array($assoc) or runtime_error('Associative array expected'); $node = $xml->element($name); foreach ($assoc as $key => $value) { $key = is_numeric($key) ? 'element' : $key; if (is_array($value)) { $node->append(self::assoc_node($xml, $key, $value)); } else { $node->append($xml->element($key, $value)); } } return $node; }
static function select($name) { isset($_POST[$name]) or runtime_error('POST select not found: ' . $name); return $_POST[$name]; }
static function crc32($filename) { fs::exists($filename) or runtime_error('File not found: ' . $filename); return hash_file('crc32', fs::normalize($filename)); }
private function group($name, $args) { $mangled = xpression::mangle($name, $args); isset($this->groups[$mangled]) or runtime_error('Group not found: ' . $mangled); return $this->groups[$mangled]; }
static function register($type, $pattern) { !isset(self::$patterns[$type]) or runtime_error('Duplicate type: ' . $type); self::$patterns[$type] = $pattern; }
function import($filename, $params = array()) { fs::exists($filename) or runtime_error('XSL stylesheet not found: ' . $filename); $xsl = new DOMDocument(); $xsl->load(fs::normalize($filename)); $import = $xsl->createElementNS('http://www.w3.org/1999/XSL/Transform', 'xsl:import'); $href = $xsl->createAttribute('href'); $href->value = dirname(__FILE__) . '/xpath.xsl'; $import->appendChild($href); $xsl->firstChild->insertBefore($import, $xsl->firstChild->firstChild); $xsl->xinclude(); $this->xslt->importStylesheet($xsl); foreach ($this->params as $name => $value) { $this->xslt->removeParameter('', $name); } $this->params = $params; $this->xslt->setParameter('', $params); }
function query($args, $document) { $this->validate($args); $xml = new xml(); $solr = $this->datasource->get($this->core); switch ($this->method) { case 'add': !empty($args) or runtime_error('Solr add method should accept parameters'); if (is_array(reset($args))) { $docs = array(); foreach (reset($args) as $document) { $doc = new SolrInputDocument(); foreach ($document as $name => $value) { if (is_array($value)) { foreach ($value as $element) { $doc->addField($name, $element); } } else { $doc->addField($name, $value); } } $docs[] = $doc; } $solr->addDocuments($docs); } else { $doc = new SolrInputDocument(); foreach ($args as $name => $value) { $doc->addField($name, $value); } $solr->addDocument($doc); } $solr->request("<commit/>"); break; case 'delete': $solr->deleteByQuery(vars::apply_assoc($this->body, $args)); $solr->request("<commit/>"); break; case 'query': $root = $xml->element($this->root[0]); $xml->append($root); $query = new SolrQuery(vars::apply_assoc($this->body, $args)); foreach ($this->order_by as $name => $order) { $query->addSortField($name, $order == 'desc' ? SolrQuery::ORDER_DESC : SolrQuery::ORDER_ASC); } if (!is_null($this->offset)) { $query->setStart(vars::apply_assoc($this->offset, $args)); } is_null($this->count) or $query->setRows(vars::apply_assoc($this->count, $args)); $response = $solr->query($query); $object = $response->getResponse(); if (is_array($object['response']['docs'])) { $root['@matched'] = $object['response']['numFound']; foreach ($object['response']['docs'] as $doc) { $item = $xml->element($this->item[0]); $root->append($item); foreach ($doc as $name => $value) { if (is_array($value)) { $array = $xml->element($name); $item->append($array); foreach ($value as $element) { $element = $xml->element('element', $element); $array->append($element); } } else { $node = $this->transform($xml, $name, $value); $item->append($node); } } } } else { $this->empty or runtime_error('Procedure returned an empty result: ' . $this->mangled()); } break; default: runtime_error('Unknown Solr method: ' . $this->method); } return $xml; }
function get($name) { isset($this->children[$name]) or runtime_error('Template not found: ' . $name); return $this->children[$name]; }
static function substitute($value, $params) { return preg_replace_callback('/\\{\\$(\\w+)\\}/', function ($matches) use($params) { isset($params[$matches[1]]) or runtime_error('Unknown parameter: ' . $matches[1]); return $params[$matches[1]]; }, $value); }
private static function assert($name = null) { isset($_SESSION) or runtime_error('Session is not started'); if (!is_null($name)) { isset($_SESSION[$name]) or runtime_error('Session variable not found: ' . $name); } }
function query($args, $document) { $this->validate($args); $xml = new xml(); switch ($this->method) { case 'photos': isset($args['venue_id']) or runtime_error('Foursquare venue_id parameter not found'); $root = $xml->element($this->root[0]); $xml->append($root); if ($result = json_decode(file_get_contents('https://api.foursquare.com/v2/venues/' . $args['venue_id'] . '/photos?' . $this->datasource->get()))) { foreach ($result->response->photos->groups as $group) { if ($group->type == 'venue') { foreach ($group->items as $item) { $image = $xml->element('image'); $root->append($image); $image['@url'] = $item->url; foreach ($item->sizes->items as $size) { $resampled = $xml->element('resampled'); $image->append($resampled); $resampled['@url'] = $size->url; $resampled['@width'] = $size->width; $resampled['@height'] = $size->height; $resampled['@created'] = @date("Y-m-d H:i:s", $item->createdAt); $resampled['@user-id'] = $item->user->id; $resampled['@user-first-name'] = $item->user->firstName; if (isset($item->user->lastName)) { $resampled['@user-last-name'] = $item->user->lastName; } $resampled['@user-gender'] = $item->user->gender; $resampled['@user-photo'] = $item->user->photo; } } } } } break; case 'venues': isset($args['latitude']) or runtime_error('Foursquare latitude parameter not found'); isset($args['longitude']) or runtime_error('Foursquare longitude parameter not found'); $root = $xml->element($this->root[0]); $xml->append($root); if ($result = json_decode(file_get_contents('https://api.foursquare.com/v2/venues/search?ll=' . $args['latitude'] . ',' . $args['longitude'] . '&' . $this->datasource->get()))) { foreach ($result->response->groups as $group) { if ($group->type == 'nearby') { foreach ($group->items as $item) { $venue = $xml->element('venue'); $root->append($venue); $venue['@id'] = $item->id; $venue['@name'] = $item->name; $venue['@url'] = $item->canonicalUrl; foreach ($item->categories as $cat) { $category = $xml->element('category'); $venue->append($category); $category['@id'] = $cat->id; $category['@name'] = $cat->name; $category['@plural-name'] = $cat->pluralName; $category['@short-name'] = $cat->shortName; $category['@icon'] = $cat->icon; } } } } } break; default: runtime_error('Unknown Foursquare method: ' . $this->method); } return $xml; }
private function replace_escape($name, $args) { isset($args[$name]) or runtime_error('Unknown procedure parameter: ' . $name); return $this->datasource->get()->quote($args[$name]); }
function get($name) { isset($this->pages[$name]) or runtime_error('Page not found: ' . $name); return $this->pages[$name]; }
private function get($name, $args) { $mangled = procedure::mangle($name, $args); isset($this->procedures[$mangled]) or runtime_error('Unknown procedure: ' . $mangled); return $this->procedures[$mangled]; }