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; }
static function parse($node, $allow = null, $deny = null) { $find = array(); $replace = array(); foreach (self::$find as $n => $rule) { list($code, $expression) = $rule; $allowed = !$allow || $allow && in_array($code, $allow); $denied = $deny && in_array($code, $deny); if ($allowed && !$denied) { $find[] = $expression; $replace[] = self::$replace[$n]; } } $xml = new xml(); foreach ($node->children() as $child) { $xml->append($xml->import($child)); } foreach ($xml->query('//text()') as $text) { $parent = $text->parent(); $bbcode = preg_replace($find, $replace, $text->value()); $bbcode = xml::parse("<?xml version=\"1.0\" encoding=\"utf-8\" ?><bbcode>{$bbcode}</bbcode>"); foreach ($bbcode->query('/bbcode/* | /bbcode/text()') as $child) { $parent->insert($xml->import($child), $text); } $parent->remove($text); } return $xml; }
function query($args, $document) { $this->validate($args); $sql = $this->datasource->get(); $xml = new xml(); $this->single() or $sql->begin(); $n = 0; foreach ($this->body as $query) { if ($rows = $sql->query($this->apply($query, $args))) { $root = $xml->element($this->root[$n]); $xml->append($root); foreach ($rows as $row) { $item = $xml->element($this->item[$n]); $root->append($item); foreach ($row as $name => $value) { $node = $this->transform($xml, $name, $value); $item->append($node); } } ++$n; } } if (!$this->empty && $xml->blank()) { $this->single() or $sql->rollback(); runtime_error('SQL procedure returned empty result: ' . $this->mangled()); } else { $this->single() or $sql->commit(); } return $document ? $xml : $xml->evaluate('/*[position() = 1]/*[position() = 1]/*[position() = 1]/text()'); }
function xml() { $xml = new xml(); foreach ($this->children as $child) { $xml->append(self::node($xml, $child)); } return $xml; }
function get($alias) { $alias .= ':' . $this->language; if (isset($this->local[$alias])) { return $this->local[$alias]->xml(); } else { $xml = new xml(); $xml->append($xml->text("[Alias not found: {$alias}]")); return $xml; } }
function load($filename) { $xml = xml::load($filename); $query = []; foreach (self::$languages as $language) { $query[] = "/local//{$language}"; } foreach ($xml->query(implode('|', $query)) as $node) { $doc = new xml(); foreach ($xml->query('* | text()', $node) as $piece) { $doc->append($doc->import($piece)); } $path = explode('.', str_replace('/', '.', trim($node->path(), '/'))); array_shift($path); $this->local[implode('.', $path)] = $doc->render(false); } }
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; }
static function json($root, $json) { $xml = new xml(); $xml->append(self::assoc_node($xml, $root, json::decode($json, true))); return $xml; }
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 wwwtokenize($subject, $pattern) { $xml = new xml(); if (preg_match_all("/({$pattern})/", $subject, $matches)) { foreach ($matches[1] as $string) { $xml->append($xml->text($string)); } } return $xml->get(); }
function documentation() { $xml = new xml(); $methods = $xml->element('methods'); $xml->append($methods); foreach ($this->methods as $url => $group) { foreach ($group->schema() as $schema) { list($type, $get, $post) = $schema; $method = $xml->element('method'); $methods->append($method); $method['@id'] = str_pad(dechex(crc32("{$type}:{$url}:" . implode(':', array_keys($get)) . implode(':', array_keys($post)))), 8, '0', STR_PAD_LEFT); $method['@url'] = $url; $method['@type'] = strtoupper($type); foreach ($get as $name => $param) { $g = $xml->element('get'); $g['@name'] = $name; $g['@type'] = $param->type; $g['@min'] = isset($param->min) ? $param->min : 'default (' . datatype::min($param->type) . ')'; $g['@max'] = isset($param->max) ? $param->max : 'default (' . datatype::max($param->type) . ')'; $g['@required'] = $param->required ? 'true' : 'false'; !isset($param->default) or $g['@default'] = $param->default; !isset($param->domains) or $g['@domain'] = implode(', ', $param->domains); $method->append($g); } foreach ($post as $name => $param) { $p = $xml->element('post'); $p['@name'] = $name; $p['@type'] = $param->type; $p['@min'] = isset($param->min) ? $param->min : 'default (' . datatype::min($param->type) . ')'; $p['@max'] = isset($param->max) ? $param->max : 'default (' . datatype::max($param->type) . ')'; $p['@required'] = $param->required ? 'true' : 'false'; !isset($param->default) or $p['@default'] = $param->default; !isset($param->domains) or $p['@domain'] = implode(', ', $param->domains); $method->append($p); } } } $xslt = new XSLTProcessor(); $xsl = new DOMDocument(); $xsl->load(www_root . 'backend/documentation.xsl'); $xslt->importStylesheet($xsl); $documentation = $xslt->transformToDoc($xml->get()); return (object) ['code' => 200, 'message' => 'OK', 'headers' => ['Content-Type' => 'text/html'], 'body' => $documentation->saveXML()]; }