private function __geocodeAddress($address, $can_return_default = true) { $coordinates = null; $cache_id = md5('maplocationfield_' . $address); $cache = new Cacheable(Symphony::Database()); $cachedData = $cache->check($cache_id); // no data has been cached if (!$cachedData) { include_once TOOLKIT . '/class.gateway.php'; $ch = new Gateway(); $ch->init(); $ch->setopt('URL', '//maps.googleapis.com/maps/api/geocode/json?address=' . urlencode($address) . '&sensor=false&v=3.13'); $response = json_decode($ch->exec()); $coordinates = $response->results[0]->geometry->location; if ($coordinates && is_object($coordinates)) { $cache->write($cache_id, $coordinates->lat . ', ' . $coordinates->lng, $this->_geocode_cache_expire); // cache lifetime in minutes } } else { $coordinates = $cachedData['data']; } // coordinates is an array, split and return if ($coordinates && is_object($coordinates)) { return $coordinates->lat . ', ' . $coordinates->lng; } elseif ($coordinates) { return $coordinates; } elseif ($return_default) { return $this->_default_coordinates; } }
function getValuesFromXML() { $xml_location = $this->get('xml_location'); $cache_life = (int) $this->get('cache'); require TOOLKIT . '/util.validators.php'; // allow use of choice params in URL $xml_location = preg_replace('/{\\$root}/', URL, $xml_location); $xml_location = preg_replace('/{\\$workspace}/', WORKSPACE, $xml_location); $doc = new DOMDocument(); if (preg_match($validators['URI'], $xml_location)) { // is a URL, check cache $cache_id = md5('xml_selectbox_' . $xml_location); $cache = new Cacheable($this->_Parent->_Parent->Database); $cachedData = $cache->check($cache_id); if (!$cachedData) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $xml_location); $ch->setopt('TIMEOUT', 6); $xml = $ch->exec(); $writeToCache = true; $cache->write($cache_id, $xml, $cache_life); // Cache life is in minutes not seconds e.g. 2 = 2 minutes $xml = trim($xml); if (empty($xml) && $cachedData) { $xml = $cachedData['data']; } } else { $xml = $cachedData['data']; } $doc->loadXML($xml); } elseif (substr($xml_location, 0, 1) == '/') { // relative to DOCROOT $doc->load(DOCROOT . $this->get('xml_location')); } else { // in extension's /xml folder $doc->load(EXTENSIONS . '/xml_selectbox/xml/' . $this->get('xml_location')); } $xpath = new DOMXPath($doc); $options = array(); foreach ($xpath->query($this->get('item_xpath')) as $item) { $option = array(); $option['text'] = $this->run($xpath, $item, $this->get('text_xpath')); $option['value'] = $this->run($xpath, $item, $this->get('value_xpath')); if (is_null($option['value'])) { $option['value'] = $option['text']; } $options[] = $option; if ($item->hasChildNodes()) { foreach ($xpath->query('*', $item) as $child) { $text = $this->run($xpath, $child, $this->get('text_xpath')); $value = $this->run($xpath, $child, $this->get('value_xpath')); $options[] = array('text' => $option['text'] . " / " . $text, 'value' => $option['value'] . "-" . (!is_null($value) ? $value : $text)); } } } return $options; }
function getValuesFromXML() { $xml_location = $this->get('xml_location'); $cache_life = (int) $this->get('cache'); require TOOLKIT . '/util.validators.php'; // allow use of choice params in URL $xml_location = preg_replace('/{\\$root}/', URL, $xml_location); $xml_location = preg_replace('/{\\$workspace}/', WORKSPACE, $xml_location); if (preg_match($validators['URI'], $xml_location)) { // is a URL, check cache $cache_id = md5('xml_selectbox_' . $xml_location); $cache = new Cacheable($this->_Parent->_Parent->Database); $cachedData = $cache->check($cache_id); if (!$cachedData) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $xml_location); $ch->setopt('TIMEOUT', 6); $xml = $ch->exec(); $writeToCache = true; $cache->write($cache_id, $xml, $cache_life); // Cache life is in minutes not seconds e.g. 2 = 2 minutes $xml = trim($xml); if (empty($xml) && $cachedData) { $xml = $cachedData['data']; } } else { $xml = $cachedData['data']; } $xml = simplexml_load_string($xml); } elseif (substr($xml_location, 0, 1) == '/') { // relative to DOCROOT $xml = simplexml_load_file(DOCROOT . $this->get('xml_location')); } else { // in extension's /xml folder $xml = simplexml_load_file(EXTENSIONS . '/xml_selectbox/xml/' . $this->get('xml_location')); } $options = array(); if (!$xml) { return $options; } $items = $xml->xpath($this->get('item_xpath')); foreach ($items as $item) { $option = array(); $text_xpath = $item->xpath($this->get('text_xpath')); $option['text'] = General::sanitize((string) $text_xpath[0]); if ($this->get('value_xpath') != '') { $value_xpath = $item->xpath($this->get('value_xpath')); $option['value'] = General::sanitize((string) $value_xpath[0]); } if ((string) $option['value'] == '') { $option['value'] = $option['text']; } $options[] = $option; } return $options; }
function getValuesFromXML() { $xml_location = $this->get('xml_location'); if (General::validateURL($xml_location) != '') { // is a URL, check cache $cache_id = md5($xml_location); $cache = new Cacheable($this->_Parent->_Parent->Database); $cachedData = $cache->check($cache_id); $creation = DateTimeObj::get('c'); if (!$cachedData || time() - $cachedData['creation'] > 5 * 60) { if (Mutex::acquire($cache_id, 6, TMP)) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $xml_location); $ch->setopt('TIMEOUT', 6); $xml = $ch->exec(); $writeToCache = true; Mutex::release($cache_id, TMP); $xml = trim($xml); if (empty($xml) && $cachedData) { $xml = $cachedData['data']; } } elseif ($cachedData) { $xml = $cachedData['data']; } } else { $xml = $cachedData['data']; } $xml = simplexml_load_string($xml); } elseif (substr($xml_location, 0, 1) == '/') { // relative to DOCROOT $xml = simplexml_load_file(DOCROOT . $this->get('xml_location')); } else { // in extension's /xml folder $xml = simplexml_load_file(EXTENSIONS . '/xml_selectbox/xml/' . $this->get('xml_location')); } if (!$xml) { return; } $items = $xml->xpath($this->get('item_xpath')); $options = array(); foreach ($items as $item) { $option = array(); $text_xpath = $item->xpath($this->get('text_xpath')); $option['text'] = General::sanitize((string) $text_xpath[0]); if ($this->get('value_xpath') != '') { $value_xpath = $item->xpath($this->get('value_xpath')); $option['value'] = General::sanitize((string) $value_xpath[0]); } if ((string) $option['value'] == '') { $option['value'] = $option['text']; } $options[] = $option; } return $options; }
/** * query - Queries the SEOMoz API * * @param string $apiName * @param string $target_url * @returns mixed URL contents on success, false on failure */ function query($api_call, $argument) { $timestamp = mktime() + 300; // 5 minutes into the future $argument = urlencode($argument); $request_url = "http://lsapi.seomoz.com/linkscape/{$api_call}/{$argument}?AccessID={$this->access_id}&Expires={$timestamp}&Signature=" . $this->generate_signature($timestamp); $cache = new Cacheable(Symphony::Database()); $cache_id = md5('seomoz_cache'); $data = $cache->check($cache_id); if (false == $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $data = curl_exec($ch); curl_close($ch); $data = $cache->write($cache_id, $data, 60); } return $data['data']; }
/** * Helper function to build Cache information block * * @param XMLElement $wrapper * @param Cacheable $cache * @param string $cache_id */ public static function buildCacheInformation(XMLElement $wrapper, Cacheable $cache, $cache_id) { $cachedData = $cache->check($cache_id); if (is_array($cachedData) && !empty($cachedData) && time() < $cachedData['expiry']) { $a = Widget::Anchor(__('Clear now'), SYMPHONY_URL . getCurrentPage() . 'clear_cache/'); $wrapper->appendChild(new XMLElement('p', __('Cache expires in %d minutes. %s', array(($cachedData['expiry'] - time()) / 60, $a->generate(false))), array('class' => 'help'))); } else { $wrapper->appendChild(new XMLElement('p', __('Cache has expired or does not exist.'), array('class' => 'help'))); } }
$template->setAttribute('match', '/'); $instruction = new XMLElement('xsl:copy-of'); ## Namespaces foreach ($this->dsParamFILTERS as $name => $uri) { $instruction->setAttribute('xmlns' . ($name ? ":{$name}" : NULL), $uri); } ## XPath $instruction->setAttribute('select', $this->dsParamXPATH); $template->appendChild($instruction); $stylesheet->appendChild($template); $stylesheet->setIncludeHeader(true); $xsl = $stylesheet->generate(true); $proc =& new XsltProcess(); $cache_id = md5($this->dsParamURL . serialize($this->dsParamFILTERS) . $this->dsParamXPATH); $cache = new Cacheable($this->_Parent->Database); $cachedData = $cache->check($cache_id); $writeToCache = false; $valid = true; $result = NULL; $creation = DateTimeObj::get('c'); if (!$cachedData || time() - $cachedData['creation'] > $this->dsParamCACHE * 60) { if (Mutex::acquire($cache_id, 6, TMP)) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $this->dsParamURL); $ch->setopt('TIMEOUT', 6); $xml = $ch->exec(); $writeToCache = true; Mutex::release($cache_id, TMP); $xml = trim($xml); if (!empty($xml)) {
public function execute(array &$param_pool = null) { $result = new XMLElement($this->dsParamROOTELEMENT); $this->dsParamURL = $this->parseParamURL($this->dsParamURL); if (isset($this->dsParamXPATH)) { $this->dsParamXPATH = $this->__processParametersInString($this->dsParamXPATH, $this->_env); } $stylesheet = new XMLElement('xsl:stylesheet'); $stylesheet->setAttributeArray(array('version' => '1.0', 'xmlns:xsl' => 'http://www.w3.org/1999/XSL/Transform')); $output = new XMLElement('xsl:output'); $output->setAttributeArray(array('method' => 'xml', 'version' => '1.0', 'encoding' => 'utf-8', 'indent' => 'yes', 'omit-xml-declaration' => 'yes')); $stylesheet->appendChild($output); $template = new XMLElement('xsl:template'); $template->setAttribute('match', '/'); $instruction = new XMLElement('xsl:copy-of'); // Namespaces if (isset($this->dsParamFILTERS) && is_array($this->dsParamFILTERS)) { foreach ($this->dsParamFILTERS as $name => $uri) { $instruction->setAttribute('xmlns' . ($name ? ":{$name}" : null), $uri); } } // XPath $instruction->setAttribute('select', $this->dsParamXPATH); $template->appendChild($instruction); $stylesheet->appendChild($template); $stylesheet->setIncludeHeader(true); $xsl = $stylesheet->generate(true); $cache_id = md5($this->dsParamURL . serialize($this->dsParamFILTERS) . $this->dsParamXPATH); $cache = new Cacheable(Symphony::Database()); $cachedData = $cache->check($cache_id); $writeToCache = false; $valid = true; $creation = DateTimeObj::get('c'); $timeout = isset($this->dsParamTIMEOUT) ? (int) max(1, $this->dsParamTIMEOUT) : 6; // Execute if the cache doesn't exist, or if it is old. if (!is_array($cachedData) || empty($cachedData) || time() - $cachedData['creation'] > $this->dsParamCACHE * 60) { if (Mutex::acquire($cache_id, $timeout, TMP)) { $ch = new Gateway(); $ch->init($this->dsParamURL); $ch->setopt('TIMEOUT', $timeout); $ch->setopt('HTTPHEADER', array('Accept: text/xml, */*')); $data = $ch->exec(); $info = $ch->getInfoLast(); Mutex::release($cache_id, TMP); $data = trim($data); $writeToCache = true; // Handle any response that is not a 200, or the content type does not include XML, plain or text if ((int) $info['http_code'] != 200 || !preg_match('/(xml|plain|text)/i', $info['content_type'])) { $writeToCache = false; $result->setAttribute('valid', 'false'); // 28 is CURLE_OPERATION_TIMEOUTED if ($info['curl_error'] == 28) { $result->appendChild(new XMLElement('error', sprintf('Request timed out. %d second limit reached.', $timeout))); } else { $result->appendChild(new XMLElement('error', sprintf('Status code %d was returned. Content-type: %s', $info['http_code'], $info['content_type']))); } return $result; // Handle where there is `$data` } elseif (strlen($data) > 0) { // If the XML doesn't validate.. if (!General::validateXML($data, $errors, false, new XsltProcess())) { $writeToCache = false; } // If the `$data` is invalid, return a result explaining why if ($writeToCache === false) { $element = new XMLElement('errors'); $result->setAttribute('valid', 'false'); $result->appendChild(new XMLElement('error', __('Data returned is invalid.'))); foreach ($errors as $e) { if (strlen(trim($e['message'])) == 0) { continue; } $element->appendChild(new XMLElement('item', General::sanitize($e['message']))); } $result->appendChild($element); return $result; } // If `$data` is empty, set the `force_empty_result` to true. } elseif (strlen($data) == 0) { $this->_force_empty_result = true; } // Failed to acquire a lock } else { $result->appendChild(new XMLElement('error', __('The %s class failed to acquire a lock, check that %s exists and is writable.', array('<code>Mutex</code>', '<code>' . TMP . '</code>')))); } // The cache is good, use it! } else { $data = trim($cachedData['data']); $creation = DateTimeObj::get('c', $cachedData['creation']); } // If `$writeToCache` is set to false, invalidate the old cache if it existed. if (is_array($cachedData) && !empty($cachedData) && $writeToCache === false) { $data = trim($cachedData['data']); $valid = false; $creation = DateTimeObj::get('c', $cachedData['creation']); if (empty($data)) { $this->_force_empty_result = true; } } // If `force_empty_result` is false and `$result` is an instance of // XMLElement, build the `$result`. if (!$this->_force_empty_result && is_object($result)) { $proc = new XsltProcess(); $ret = $proc->process($data, $xsl); if ($proc->isErrors()) { $result->setAttribute('valid', 'false'); $error = new XMLElement('error', __('Transformed XML is invalid.')); $result->appendChild($error); $element = new XMLElement('errors'); foreach ($proc->getError() as $e) { if (strlen(trim($e['message'])) == 0) { continue; } $element->appendChild(new XMLElement('item', General::sanitize($e['message']))); } $result->appendChild($element); } elseif (strlen(trim($ret)) == 0) { $this->_force_empty_result = true; } else { if ($writeToCache) { $cache->write($cache_id, $data, $this->dsParamCACHE); } $result->setValue(PHP_EOL . str_repeat("\t", 2) . preg_replace('/([\\r\\n]+)/', "\$1\t", $ret)); $result->setAttribute('status', $valid === true ? 'fresh' : 'stale'); $result->setAttribute('creation', $creation); } } return $result; }
public function render_panel($context) { $config = $context['config']; switch ($context['type']) { case 'datasource_to_table': $ds = DatasourceManager::create($config['datasource'], NULL, false); if (!$ds) { $context['panel']->appendChild(new XMLElement('div', __('The Data Source with the name <code>%s</code> could not be found.', array($config['datasource'])))); return; } $param_pool = array(); $xml = $ds->grab($param_pool); if (!$xml) { return; } $xml = $xml->generate(); require_once TOOLKIT . '/class.xsltprocess.php'; $proc = new XsltProcess(); $data = $proc->process($xml, file_get_contents(EXTENSIONS . '/dashboard/lib/datasource-to-table.xsl')); $context['panel']->appendChild(new XMLElement('div', $data)); break; case 'rss_reader': require_once TOOLKIT . '/class.gateway.php'; require_once CORE . '/class.cacheable.php'; $cache_id = md5('rss_reader_cache' . $config['url']); $cache = new Cacheable(Administration::instance()->Database()); $data = $cache->check($cache_id); if (!$data) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $config['url']); $ch->setopt('TIMEOUT', 6); $new_data = $ch->exec(); $writeToCache = true; if ((int) $config['cache'] > 0) { $cache->write($cache_id, $new_data, $config['cache']); } $xml = $new_data; if (empty($xml) && $data) { $xml = $data['data']; } } else { $xml = $data['data']; } if (!$xml) { $xml = '<error>' . __('Error: could not retrieve panel XML feed.') . '</error>'; } require_once TOOLKIT . '/class.xsltprocess.php'; $proc = new XsltProcess(); $data = $proc->process($xml, file_get_contents(EXTENSIONS . '/dashboard/lib/rss-reader.xsl'), array('show' => $config['show'])); $context['panel']->appendChild(new XMLElement('div', $data)); break; case 'html_block': require_once TOOLKIT . '/class.gateway.php'; require_once CORE . '/class.cacheable.php'; $cache_id = md5('html_block_' . $config['url']); $cache = new Cacheable(Administration::instance()->Database()); $data = $cache->check($cache_id); if (!$data) { $ch = new Gateway(); $ch->init(); $ch->setopt('URL', $config['url']); $ch->setopt('TIMEOUT', 6); $new_data = $ch->exec(); $writeToCache = true; if ((int) $config['cache'] > 0) { $cache->write($cache_id, $new_data, $config['cache']); } $html = $new_data; if (empty($html) && $data) { $html = $data['data']; } } else { $html = $data['data']; } if (!$html) { $html = '<p class="invalid">' . __('Error: could not retrieve panel HTML.') . '</p>'; } $context['panel']->appendChild(new XMLElement('div', $html)); break; case 'symphony_overview': $container = new XMLElement('div'); $dl = new XMLElement('dl'); $dl->appendChild(new XMLElement('dt', __('Website Name'))); $dl->appendChild(new XMLElement('dd', Symphony::Configuration()->get('sitename', 'general'))); $current_version = Symphony::Configuration()->get('version', 'symphony'); require_once TOOLKIT . '/class.gateway.php'; $ch = new Gateway(); $ch->init(); $ch->setopt('URL', 'https://api.github.com/repos/symphonycms/symphony-2/tags'); $ch->setopt('TIMEOUT', $timeout); $repo_tags = $ch->exec(); // tags request found if (is_array($repo_tags)) { $repo_tags = json_decode($repo_tags); $tags = array(); foreach ($repo_tags as $tag) { // remove tags that contain strings if (preg_match('/[a-zA]/i', $tag->name)) { continue; } $tags[] = $tag->name; } natsort($tags); rsort($tags); $latest_version = reset($tags); } else { $latest_version = $current_version; } $needs_update = version_compare($latest_version, $current_version, '>'); $dl->appendChild(new XMLElement('dt', __('Version'))); $dl->appendChild(new XMLElement('dd', $current_version . ($needs_update ? ' (<a href="http://getsymphony.com/download/releases/version/' . $latest_version . '/">' . __('Latest is %s', array($latest_version)) . "</a>)" : ''))); $container->appendChild(new XMLElement('h4', __('Configuration'))); $container->appendChild($dl); $entries = 0; foreach (SectionManager::fetch() as $section) { $entries += EntryManager::fetchCount($section->get('id')); } $dl = new XMLElement('dl'); $dl->appendChild(new XMLElement('dt', __('Sections'))); $dl->appendChild(new XMLElement('dd', (string) count(SectionManager::fetch()))); $dl->appendChild(new XMLElement('dt', __('Entries'))); $dl->appendChild(new XMLElement('dd', (string) $entries)); $dl->appendChild(new XMLElement('dt', __('Data Sources'))); $dl->appendChild(new XMLElement('dd', (string) count(DatasourceManager::listAll()))); $dl->appendChild(new XMLElement('dt', __('Events'))); $dl->appendChild(new XMLElement('dd', (string) count(EventManager::listAll()))); $dl->appendChild(new XMLElement('dt', __('Pages'))); $dl->appendChild(new XMLElement('dd', (string) count(PageManager::fetch()))); $container->appendChild(new XMLElement('h4', __('Statistics'))); $container->appendChild($dl); $context['panel']->appendChild($container); break; case 'markdown_text': $formatter = TextformatterManager::create($config['formatter']); $html = $formatter->run($config['text']); $context['panel']->appendChild(new XMLElement('div', $html)); break; } }
/** * Get the CSV object as it is stored in the database. * @return bool|mixed the CSV object on success, false on failure */ private function __getCSV() { $cache = new Cacheable(Symphony::Database()); $data = $cache->check('importcsv'); if ($data != false) { return unserialize($data['data']); } else { return false; } }
public function login() { //Hash for the API Key $cache_u_apikey = md5($this->get('eventarc-password')); //HAsh for the User ID $cache_u_id = md5($this->get('eventarc-username')); $cache = new Cacheable(Symphony::Database()); //Check for a cached API Key $cached_u_apikey = $cache->check($cache_u_apikey); $this->u_apikey = $cached_u_apikey['data']; //Check for a cached User ID $cached_u_id = $cache->check($cache_u_id); $this->u_id = $cached_u_id['data']; // Login first using the credentials from preferences. // This returns the API key which is stored and used for all subsequent requests. if (empty($this->u_apikey) || empty($this->u_id)) { $eventarc = new Eventarc; try { $login_data = $eventarc->user_login($this->get('eventarc-username'),$this->get('eventarc-password')); //Save the API key in Cache for 1 hour. $cache->write($cache_u_apikey, $login_data['u_apikey'], 60); $cached_u_apikey = $cache->check($cache_u_apikey); $this->u_apikey = $cached_u_apikey['data']; //Save the User ID in Cache for 1 hour. $cache->write($cache_u_id, $login_data['u_id'], 60); $cached_u_id = $cache->check($cache_u_id); $this->u_apikey = $cached_u_id['data']; // Re-make eventarc with the new apikey $this->eventarc = new Eventarc($this->u_apikey, $this->get('eventarc-username')); return $this->eventarc; } catch (Eventarc_Exception $e) { echo 'Error: '.$e->getMessage(); return false; } } else { try { $this->eventarc = new Eventarc($this->u_apikey, $this->get('eventarc-username')); return $this->eventarc; } catch (Eventarc_Exception $e) { echo 'Error: '.$e->getMessage(); return false; } } }