/** * Load XML function * Using the $xmlurl variable set above, it creates the basic array used for parsing. */ function load_xml() { global $xmlurl, $xml, $array_xml, $arr_attributes, $arr_attributes_master, $updated, $players_online, $xml_age; $xml = new XMLToArray($xmlurl, array(), array('server' => '_array_'), TRUE, FALSE); $array_xml = $xml->getArray(); $arr_attributes = $xml->getAttributes(); // Récupère les attrib de "masterquery" (update timestamp et Players) $arr_attributes_master = $arr_attributes[0]; $arr_attributes_master = $arr_attributes_master['serverlist']; $arr_attributes_master = $arr_attributes_master[0]; // Récupère les attrib de chaque server pour le merge... + simplifie (root du array) $arr_attributes = $arr_attributes[1]; $arr_attributes = $arr_attributes['server']; $updated = $arr_attributes_master['mktime']; $players_online = $arr_attributes_master['players']; $xml_age = mktime() - $updated; }
function junipertelnet($config) { $this->tmpdir = BASEDIR . 'tmp'; if (!$config['telnet_port']) { $config['telnet_port'] = 23; } if (is_array($config['columns'])) { $this->columns = $config['columns']; } if (is_numeric($config['cachetime'])) { $this->cachetime = $config['cachetime']; } $rawpeerlist = ''; while (!preg_match("/^\\<rpc-reply/", $rawpeerlist) && !preg_match("/\\<\\/rpc-reply\\>\$/", $rawpeerlist)) { $this->cachefile = $this->tmpdir . '/' . preg_replace('/[^a-zA-Z0-9]/', '', $config['hostname']) . ':' . $config['telnet_port'] . '.cache'; if (@filemtime($this->cachefile) < date('U') - $this->cachetime) { $this->_getpeersfromrouter($config); } $rawpeerlist = file_get_contents($this->cachefile); if (!preg_match("/^\\<rpc-reply/", $rawpeerlist) && !preg_match("/\\<\\/rpc-reply\\>\$/", $rawpeerlist)) { unlink($this->cachefile); } } $xml2a = new XMLToArray(); $root_node = $xml2a->parse($rawpeerlist); $out = array_shift($root_node["_ELEMENTS"]); foreach ($out['_ELEMENTS'][0]['_ELEMENTS'] as $c) { if ($c['_NAME'] == 'bgp-peer') { $mydata = array(); foreach ($c['_ELEMENTS'] as $e) { $mydata[$e['_NAME']] = $e['_DATA']; } if ($mydata['peer-state'] == "Established") { $status = 'up'; } else { $status = 'down'; } $this->peers[$mydata['peer-address']] = array('ip' => $mydata['peer-address'], 'asn' => $mydata['peer-as'], 'msgrcvd' => $mydata['input-messages'], 'msgsent' => $mydata['output-messages'], 'outq' => $mydata['route-queue-count'], 'uptime' => $mydata['elapsed-time'], 'status' => $mydata['peer-state'], 'flaps' => $mydata['flap-count'], '_shortstatus' => $status); } } }
static function fromXml($data) { $sig = new XMLToArray($data, array(), array(), true, false); return $sig->getArray(); }
/** * Uploading a video from a url. * * @param string $url * @param string $title the title for the video * @param string $description the description for the video * @param int|\Profile $profile the size for the video to be encoded, if not specified it will use the vzaar default * @param int $bitrate the bitrate for the video to be encoded * @param int $width the width for the video to be encoded * @param int $replace_id an existing video id to be replaced with the new video * @param boolean $transcoding if true forces vzaar to transcode the video, false will use the original source file (only for mp4 and flv files) * * @return int $video_id returns the video id */ public static function uploadLink($url, $title = NULL, $description = NULL, $profile = Profile::Medium, $bitrate = 256, $width = 200, $replace_id = NULL, $transcoding = false) { $_url = self::$url . "api/upload/link.xml"; $signature = Vzaar::getUploadSignature(); $req = Vzaar::setAuth($_url, 'POST'); $data = '<?xml version="1.0" encoding="UTF-8"?> <vzaar-api> <link_upload> <key>' . $signature['vzaar-api']['key'] . '</key> <guid>' . $signature['vzaar-api']['guid'] . '</guid> <url>' . urlencode($url) . '</url> <encoding_params> <title>' . self::_sanitize_str($title) . '</title> <description>' . self::_sanitize_str($description) . '</description> <size_id>' . $profile . '</size_id> <bitrate>' . $bitrate . '</bitrate> <width>' . $width . '</width> <replace_id>' . $replace_id . '</replace_id> <transcoding>' . $transcoding . '</transcoding> </encoding_params> </link_upload> </vzaar-api>'; $c = new HttpRequest($_url); $c->verbose = Vzaar::$enableHttpVerbose; $c->method = 'POST'; array_push($c->headers, $req->to_header()); array_push($c->headers, 'User-Agent: Vzaar OAuth Client'); array_push($c->headers, 'Connection: close'); array_push($c->headers, 'Content-Type: application/xml'); $reply = $c->send($data); $xmlObj = new XMLToArray($reply); $arr = $xmlObj->getArray(); $video_id = $arr['vzaar-api'] ? $arr['vzaar-api']['id'] : false; return $video_id; }
public static function run(SymQLQuery $query, $output = SymQL::RETURN_XML) { self::init(); self::getQueryCount(); // stores all config locally so that the same SymQLManager can be used for mutliple queries $section = null; $section_fields = array(); $where = null; $joins = null; $entry_ids = array(); // resolve section $resolved_section = self::getResolvedSection($query->section); if (is_null($resolved_section)) { $section = $query->section; if (!is_numeric($query->section)) { $section = self::$_sectionManager->fetchIDFromHandle($query->section); } $section = self::$_sectionManager->fetch($section); if (!$section instanceof Section) { throw new Exception(sprintf("%s: section '%s' does not not exist", __CLASS__, $query->section)); } $fields = $section->fetchFields(); self::$_resolved_sections[] = $section; self::$_resolved_fields[$section->get('id')] = $fields; } else { $section = $resolved_section; $fields = self::getResolvedFields($section->get('id')); } self::$_debug['queries']['Resolve section and fields'] = self::getQueryCount(); // cache list of field objects in this section (id => object) foreach ($fields as $field) { $section_fields[] = $field->get('id'); } $section_fields = self::indexFieldsByID($section_fields, $fields, true); // resolve list of fields from SELECT statement if ($query->fields == '*') { foreach ($fields as $field) { $select_fields[] = $field->get('element_name'); } } else { $select_fields = $query->fields; } $select_fields = self::indexFieldsByID($select_fields, $fields); // resolve list of fields from WHERE statements (filters) $filters = array(); if (is_array($query->filters)) { foreach ($query->filters as $i => $filter) { $field = self::indexFieldsByID($filter['field'], $fields); if ($field) { $filters[$i][reset(array_keys($field))]['value'] = $filter['value']; $filters[$i][reset(array_keys($field))]['type'] = $filter['type']; } } } // resolve sort field if (in_array($query->sort_field, self::$_reserved_fields)) { $handle_exploded = explode(':', $query->sort_field); if (count($handle_exploded) == 2) { self::$_entryManager->setFetchSorting(end($handle_exploded), $query->sort_direction); } } else { $sort_field = self::indexFieldsByID($query->sort_field, $fields); $sort_field = $section_fields[reset(array_keys($sort_field))]; if ($sort_field && $sort_field->isSortable()) { self::$_entryManager->setFetchSorting($sort_field->get('id'), $query->sort_direction); } } $where = null; $joins = null; foreach ($filters as $filter) { $field_id = reset(array_keys($filter)); $filter = reset($filter); if ($field_id == 'system:id') { $entry_ids[] = (int) $filter['value']; continue; } // get the cached field object $field = $section_fields[$field_id]; if (!$field) { throw new Exception(sprintf("%s: field '%s' does not not exist", __CLASS__, $field_id)); } if (!$field->canFilter() || !method_exists($field, 'buildDSRetrivalSQL')) { throw new Exception(sprintf("%s: field '%s' can not be used as a filter", __CLASS__, $field_id)); } // local $_where = null; $_joins = null; $filter_type = false === strpos($filter['value'], '+') ? self::DS_FILTER_OR : self::DS_FILTER_AND; $value = preg_split('/' . ($filter_type == self::DS_FILTER_AND ? '\\+' : ',') . '\\s*/', $filter['value'], -1, PREG_SPLIT_NO_EMPTY); if (!is_array($value)) { $value = array($value); } // Get the WHERE and JOIN from the field $where_before = $_where; $field->buildDSRetrivalSQL($value, $_joins, $_where, $filter_type == self::DS_FILTER_AND ? true : false); // HACK: if this is an OR statement, strip the first AND from the returned SQL // and replace with OR if ($filter['type'] == SymQL::DS_FILTER_OR) { $_where_after = substr($_where, strlen($_where_before), strlen($where)); $_where_after = preg_replace('/^AND/', 'OR', trim($_where_after)); $_where = $_where_before . $_where_after; } $joins .= $_joins; $where .= $_where; } // resolve the SELECT type and fetch entries if (reset(array_keys($select_fields)) == 'system:count') { $select_type = SymQL::SELECT_COUNT; $fetch_result = (int) self::$_entryManager->fetchCount($section->get('id'), $where, $joins); } else { if (count($entry_ids) > 0) { $select_type = SymQL::SELECT_ENTRY_ID; $fetch_result = self::$_entryManager->fetch($entry_ids, $section->get('id'), $query->per_page, null, null, false, false, true, array_values($select_fields)); } else { $select_type = SymQL::SELECT_ENTRIES; $fetch_result = self::$_entryManager->fetchByPage($query->page, $section->get('id'), $query->per_page, $where, $joins, false, false, true, array_values($select_fields)); } } self::$_debug['sql']['joins'] = $joins; self::$_debug['sql']['where'] = $where; self::$_debug['queries']['Fetch entries'] = self::getQueryCount(); // section metadata $section_metadata = array('name' => $section->get('name'), 'id' => $section->get('id'), 'handle' => $section->get('handle')); // build pagination metadata if ($select_type == SymQL::SELECT_ENTRIES) { $pagination = array('total-entries' => (int) $fetch_result['total-entries'], 'total-pages' => (int) $fetch_result['total-pages'], 'per-page' => (int) $fetch_result['limit'], 'current-page' => (int) $query->page); } // find the array of entries returned from EntryManager fetch $entries = array(); switch ($select_type) { case SymQL::SELECT_ENTRY_ID: $entries = $fetch_result; break; case SymQL::SELECT_ENTRIES: $entries = $fetch_result['records']; break; case SymQL::SELECT_COUNT: $count = $fetch_result; break; } // set up result container depending on return type switch ($output) { case SymQL::RETURN_ARRAY: case SymQL::RETURN_RAW_COLUMNS: case SymQL::RETURN_ENTRY_OBJECTS: $result = array(); $result['section'] = $section_metadata; if ($pagination) { $result['pagination'] = $pagination; } break; case SymQL::RETURN_XML: $result = new XMLElement($query->root_element ? $query->root_element : 'symql'); $result->appendChild(new XMLElement('section', $section_metadata['name'], array('id' => $section_metadata['id'], 'handle' => $section_metadata['handle']))); if ($pagination) { $result->appendChild(General::buildPaginationElement($pagination['total-entries'], $pagination['total-pages'], $pagination['per-page'], $pagination['current-page'])); } break; } // append returned entries to results container if ($select_type == SymQL::SELECT_ENTRY_ID || $select_type == SymQL::SELECT_ENTRIES) { foreach ($entries as $entry) { switch ($output) { case SymQL::RETURN_RAW_COLUMNS: $fields = array(); foreach ($entry->getData() as $field_id => $values) { $field = $section_fields[$field_id]; $fields[$field->get('element_name')] = $values; } $result['entries'][$entry->get('id')] = $fields; break; case SymQL::RETURN_ENTRY_OBJECTS: $result['entries'][$entry->get('id')] = $entry; break; case SymQL::RETURN_XML: case SymQL::RETURN_ARRAY: $xml_entry = new XMLElement('entry'); $xml_entry->setAttribute('id', $entry->get('id')); foreach ($entry->getData() as $field_id => $values) { $field = $section_fields[$field_id]; $handle = $field->get('element_name'); $handle_exploded = explode(':', $select_fields[$field_id]); if (count($handle_exploded) == 2) { $mode = end($handle_exploded); } $field->appendFormattedElement($xml_entry, $values, $encode, $mode); } if ($output == SymQL::RETURN_ARRAY) { $result['entries'][] = XMLToArray::convert($xml_entry->generate()); } else { $result->appendChild($xml_entry); } break; } } } elseif ($select_type == SymQL::SELECT_COUNT) { switch ($output) { case SymQL::RETURN_ARRAY: case SymQL::RETURN_RAW_COLUMNS: case SymQL::RETURN_ENTRY_OBJECTS: $result['count'] = $count; break; case SymQL::RETURN_XML: $xml_entry = new XMLElement('count', $count); $result->appendChild($xml_entry); break; } } self::$_debug['queries']['Total'] = self::$_context->Database->queryCount() - self::$_base_querycount; // reset for the next query self::$_entryManager->setFetchSorting(null, null); self::$_base_querycount = null; self::$_cumulative_querycount = null; return $result; }
public static function sendOutput($response_body = NULL, $code = 200) { switch ($code) { case 200: header('HTTP/1.0 200 OK'); break; case 401: header('HTTP/1.0 401 Bad Request'); break; case 403: header('HTTP/1.0 403 Forbidden'); break; case 404: header('HTTP/1.0 404 Not Found'); break; } $xml = $response_body; if (is_array($xml)) { $xml = reset($xml); } if ($xml instanceof XMLElement) { $xml = $xml->generate(TRUE); } switch (self::$_output_type) { case 'json': header('Content-Type: text/plain; charset=utf-8'); $output = json_encode(XMLToArray::convert($xml)); break; case 'serialise': case 'serialize': header('Content-Type: text/plain; charset=utf-8'); $output = serialize(XMLToArray::convert($xml)); break; case 'yaml': header('Content-Type: text/plain; charset=utf-8'); require_once 'spyc-0.4.5/spyc.php'; $output = Spyc::YAMLDump(XMLToArray::convert($xml)); break; case 'xml': header('Content-Type: text/xml; charset=utf-8'); $output = $xml; break; case 'csv': header('Content-Type: text/plain; charset=utf-8'); $entries = XMLToArray::convert($xml); $entries = $entries['response']['entry']; $file_name = sprintf('%s/%s-%d.csv', TMP, self::$_plugin_name, time()); $csv = fopen($file_name, 'w'); $columns = array(); $rows = array(); // iterate over all entries to build columns. do not assume that the // first entry has all fields (if a value is missing the field will not be present!) foreach ($entries as $entry) { foreach ($entry as $handle => $value) { if (!in_array($handle, $columns)) { $columns[] = $handle; } } } fputcsv($csv, $columns, ',', '"'); foreach ($entries as $entry) { $row = array(); // build the data for each field in this entry foreach ($columns as $column) { // use a "value" column if it exists if (isset($entry[$column]['value'])) { $value = $entry[$column]['value']; } elseif (isset($entry[$column]['filename']['value'])) { $value = $entry[$column]['filename']['value']; } else { $value = ''; } $row[$column] = $value; } fputcsv($csv, $row, ',', '"'); } fclose($csv); $output = file_get_contents($file_name); unlink($file_name); break; } echo $output; exit; }