public function embed_gists($content)
 {
     $gists_regex = '/<script[^>]+src="(http:\\/\\/gist.github.com\\/[^"]+)"[^>]*><\\/script>/i';
     // remove gists from multiple-post templates
     if (Options::get('gistextras__removefrommultiple')) {
         if (!in_array(URL::get_matched_rule()->name, array('display_entry', 'display_page'))) {
             return preg_replace($gists_regex, '', $content);
         }
     }
     preg_match_all($gists_regex, $content, $gists);
     for ($i = 0, $n = count($gists[0]); $i < $n; $i++) {
         if (Options::get('gistextras__cachegists')) {
             if (Cache::has($gists[1][$i])) {
                 $gist = Cache::get($gists[1][$i]);
             } else {
                 if ($gist = RemoteRequest::get_contents($gists[1][$i])) {
                     $gist = $this->process_gist($gist);
                     Cache::set($gists[1][$i], $gist, 86400);
                     // cache for 1 day
                 }
             }
         } else {
             $gist = RemoteRequest::get_contents($gists[1][$i]);
             $gist = $this->process_gist($gist);
         }
         // replace the script tag
         $content = str_replace($gists[0][$i], $gist, $content);
     }
     return $content;
 }
Пример #2
0
 public function action_plugin_act_serve_ga()
 {
     if (Cache::has('ga.js')) {
         $js = Cache::get('ga.js');
     } else {
         $js = RemoteRequest::get_contents('http://www.google-analytics.com/ga.js');
         Cache::set('ga.js', $js, 86400);
         // cache for 1 day
     }
     // Clean the output buffer, so we can output from the header/scratch
     ob_clean();
     header('Content-Type: application/javascript');
     echo $js;
 }
 function get_links($url)
 {
     $html = RemoteRequest::get_contents($url);
     $dom = str_get_html($html);
     $as = $dom->find('a');
     $hrefs = array();
     foreach ($as as $a) {
         $href = rtrim($a->getAttribute('href'), '/');
         if (strpos($href, '..') !== false || strpos($href, '/') !== false) {
         } else {
             $hrefs[] = $href;
         }
     }
     $dom->clear();
     return $hrefs;
 }
 public function get_technorati_stats()
 {
     $technorati_stats = array();
     $technorati_url = 'http://api.technorati.com/bloginfo?key=' . Options::get('technorati__apikey') . '&url=' . Site::get_url('habari');
     $response = RemoteRequest::get_contents($technorati_url);
     if ($response !== FALSE) {
         $xml = new SimpleXMLElement($response);
         if (isset($xml->document->result->weblog)) {
             $technorati_inbound_blogs = (int) $xml->document->result->weblog[0]->inboundblogs;
             $technorati_inbound_links = (int) $xml->document->result->weblog[0]->inboundlinks;
             $technorati_rank = (int) $xml->document->result->weblog[0]->rank;
             $technorati_stats['Rank'] = $technorati_rank;
             $technorati_stats['Inbound Links'] = $technorati_inbound_links;
             $technorati_stats['Inbound Blogs'] = $technorati_inbound_blogs;
         }
     }
     return $technorati_stats;
 }
Пример #5
0
 function fetch($method, $params = array(), $tokenize = false, $debug = false)
 {
     $url = $this->endpoint;
     $url .= '?method=' . strtolower($method);
     foreach ($params as $key => $val) {
         $url .= '&' . $key . '=' . $val;
     }
     $url .= '&api_key=' . $this->key;
     if ($debug) {
         print_r($url);
     }
     $contents = RemoteRequest::get_contents($url);
     $data = new SimpleXMLElement($contents);
     if ($data['status'] == 'ok') {
         return $data;
     } else {
         return FALSE;
     }
 }
 public function action_block_content_textlinkads($block)
 {
     if (!Cache::has('textlinkads')) {
         $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
         $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
         $inventory_key = 'NYWSW7HTCIN307VV7BBD';
         $tla_uri = 'http://www.text-link-ads.com/xml.php?inventory_key=' . $inventory_key . '&referer=' . urlencode($request_uri) . '&user_agent=' . urlencode($user_agent);
         Cache::set('textlinkads', RemoteRequest::get_contents($tla_uri));
         Utils::debug('Cache set');
     }
     $xml = new SimpleXMLElement(Cache::get('textlinkads'));
     $links = array();
     foreach ($xml->Link as $link) {
         $ad = new StdClass();
         $ad->before = (string) $link->BeforeText;
         $ad->after = (string) $link->AfterText;
         $ad->text = (string) $link->Text;
         $ad->url = (string) $link->URL;
         $links[(string) $link->LinkID] = $ad;
     }
     $block->links = $links;
 }
Пример #7
0
 private function call($method, $cache = true)
 {
     if ($cache && Cache::has(array('mpango_githubapi', md5($method)))) {
         return Cache::get(array('mpango_githubapi', md5($method)));
     }
     $url = $this->api . '/' . $method;
     if (Options::get('mpango__github_id') != null) {
         $url .= '?client_id=' . Options::get('mpango__github_id') . '&client_secret=' . Options::get('mpango__github_secret');
     }
     $contents = RemoteRequest::get_contents($url);
     if (!$contents) {
         return false;
     }
     $parsed = json_decode($contents);
     Cache::set(array('mpango_githubapi', md5($method)), $parsed);
     // Cache::set( array( 'mpango_githubapi', md5( $method ) ), $parsed );
     return $parsed;
 }
Пример #8
0
 private function add_footnote($matches)
 {
     $url = $matches[1];
     $footnote = $matches[2];
     if ($url != '') {
         $url = preg_replace('/\\s+url=[\'"](.*)[\'"]/', '$1', $url);
         $url_parts = array();
         $info = array();
         // It's a link to a flickr photo
         if (preg_match('/http:\\/\\/(www\\.)?flickr.com\\/photos\\/(.*)\\/(.*)/', $url, $url_parts)) {
             $user = $url_parts[1];
             $photo = $url_parts[2];
             if (count($url_parts) > 3) {
                 // Matched longer version
                 $user = $url_parts[2];
                 $photo = $url_parts[3];
             }
             if (Cache::has('footcite__' . $photo)) {
                 $fetch = Cache::get('footcite__' . $photo);
             } else {
                 $fetch = RemoteRequest::get_contents('http://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=' . self::FLICKR_KEY . '&photo_id=' . $photo);
                 Cache::set('footcite__' . $photo, $fetch);
             }
             $xml = new SimpleXMLElement($fetch);
             $info['type'] = 'flickr';
             $info['photo'] = array('title' => (string) $xml->photo->title, 'id' => $photo, 'url' => (string) $xml->photo->urls->url[0]);
             $info['owner'] = array('username' => (string) $xml->photo->owner['username'], 'name' => (string) $xml->photo->owner['realname'], 'id' => (string) $xml->photo->owner['nsid'], 'url' => 'http://www.flickr.com/photos/' . $user . '/');
         } else {
             $info['type'] = 'vanilla';
             $info['url'] = $url;
             $info['text'] = $footnote;
         }
         $footnote = $info;
     }
     $i = count($this->footnotes) + 1;
     $this->footnotes[$i] = $footnote;
     $id = $this->current_id . '-' . $i;
     return '<sup class="footnote-link" id="footnote-link-' . $id . '"><a href="' . $this->post->permalink . '#footnote-' . $id . '" rel="footnote">' . $i . '</a></sup>';
 }
Пример #9
0
 public static function fetch()
 {
     if (Cache::get('chickler_subscribercount') == NULL) {
         $count = 0;
         foreach (Options::get('chicklet__feedname') as $feed) {
             $url = "https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=" . $feed;
             $remote = RemoteRequest::get_contents($url);
             @($xml = simplexml_load_string($remote));
             if ($xml == false) {
                 return 0;
             } else {
                 $count = $count + intval($xml->feed->entry['circulation']);
             }
         }
         Cache::set('chickler_subscribercount', $count);
     } else {
         $count = Cache::get('chickler_subscribercount');
     }
     return $count;
 }
Пример #10
0
 /**
  * Plugin load_feeds filter, executes for the cron job defined in action_plugin_activation()
  * @param boolean $result The incoming result passed by other sinks for this plugin hook
  * @return boolean True if the cron executed successfully, false if not.
  */
 public function filter_load_feeds($result)
 {
     $feedurls = Options::get('feedlist__feedurl');
     foreach ($feedurls as $feed_id => $feed_url) {
         if ($feed_url == '') {
             EventLog::log(sprintf(_t('Feed ID %1$d has an invalid URL.'), $feed_id), 'warning', 'feedlist', 'feedlist');
             continue;
         }
         // load the XML data
         $xml = RemoteRequest::get_contents($feed_url);
         if (!$xml) {
             EventLog::log(sprintf(_t('Unable to fetch feed %1$s data.'), $feed_url), 'err', 'feedlist', 'feedlist');
         }
         $dom = new DOMDocument();
         // @ to hide parse errors
         @$dom->loadXML($xml);
         if ($dom->getElementsByTagName('rss')->length > 0) {
             $items = $this->parse_rss($dom);
             $this->replace($feed_id, $items);
         } else {
             if ($dom->getElementsByTagName('feed')->length > 0) {
                 $items = $this->parse_atom($dom);
                 $this->replace($feed_id, $items);
             } else {
                 // it's an unsupported format
                 EventLog::log(sprintf(_t('Feed %1$s is an unsupported format.'), $feed_url), 'err', 'feedlist', 'feedlist');
                 continue;
             }
         }
         // log that the feed was updated
         EventLog::log(sprintf(_t('Updated feed %1$s'), $feed_url), 'info', 'feedlist', 'feedlist');
     }
     // log that we finished
     EventLog::log(sprintf(_t('Finished updating %1$d feed(s).'), count($feedurls)), 'info', 'feedlist', 'feedlist');
     // clean up old feed items
     $old_date = DB::get_value('select updated from {feedlist} order by updated desc limit 10, 1');
     DB::query('delete from {feedlist} where updated < ?', array($old_date));
     EventLog::log(sprintf(_t('Old feed items purged.')), 'info', 'feedlist', 'feedlist');
     return $result;
     // only change a cron result to false when it fails
 }
Пример #11
0
 function filter_stack_out($stack, $stack_name, $filter)
 {
     static $incmin = false;
     if (in_array($stack_name, array('template_header_javascript', 'template_footer_javascript')) && is_callable($filter) && strcasecmp(implode('::', $filter), 'stack::scripts') == 0) {
         // Load the minifier class once
         if (!$incmin) {
             include __DIR__ . '/jsmin/jsmin.php';
             $incmin = true;
         }
         // Get the script names to minify
         $domin = Options::get('jsmincdn__storage');
         // Find greatest common sequences
         $seqs = array();
         $script_build = 'jsmincdn';
         $seq = array();
         foreach ($stack as $stackitem) {
             $doomit = false;
             $name = $stackitem->name;
             if ($domin && in_array($name, $domin)) {
                 $script_build .= '.' . $name;
                 $seq[$name] = $stackitem;
             } else {
                 if (count($seq) > 0) {
                     $seqs[$script_build] = $seq;
                     $script_build = 'jsmincdn';
                     $seq = array();
                 }
                 $seqs[$name] = $stackitem;
             }
         }
         if (count($seq) > 0) {
             $seqs[$script_build] = $seq;
             $script_build = 'jsmincdn';
             $seq = array();
         }
         $script = '';
         $restack = array();
         $script_build = '';
         $output = '';
         foreach ($seqs as $seqname => $seqelement) {
             if ($seqelement instanceof StackItem) {
                 $doomit = true;
                 $restack[$seqname] = $seqelement;
                 //$restack[$seqname] = new StackItem($seqname, '0', $seqelement);
             } elseif (Cache::has(array('jsmincdn_post', $seqname))) {
                 $doomit = false;
                 $output = Cache::get(array('jsmincdn_post', $seqname));
                 //$restack[$seqname] = $output;
                 //$restack[$seqname] = URL::get('jsmincdn', array('name' => $seqname));
                 $restack[$seqname] = new StackItem($seqname, '0', URL::get('jsmincdn', array('name' => $seqname)));
             } else {
                 foreach ($seqelement as $name => $element) {
                     $script .= "\n\n/* {$name} */\n\n";
                     if (is_string($element) && strpos($element, "\n") !== FALSE) {
                         $script .= '/** FROM (a): ' . $element->resource . " **/\n";
                         $output = $element;
                     } elseif (Cache::has(array('jsmincdn', $element->resource))) {
                         $script .= '/** FROM (b): ' . $element->resource . " **/\n";
                         $output = Cache::get(array('jsmincdn', $element->resource));
                     } elseif (strpos($element->resource, Site::get_url('scripts')) === 0) {
                         $script .= '/** FROM (c): ' . $element->resource . " **/\n";
                         $base = substr($element->resource, strlen(Site::get_url('scripts')));
                         $filename = HABARI_PATH . '/system/vendor' . $base;
                         $output = file_get_contents($filename);
                         Cache::set(array('jsmincdn', $element->resource), $output, 3600 * 24);
                     } elseif (strpos($element->resource, Site::get_url('habari')) === 0) {
                         $script .= '/** FROM (d): ' . $element->resource . " **/\n";
                         $base = substr($element->resource, strlen(Site::get_url('habari')));
                         $filename = HABARI_PATH . $base;
                         $output = file_get_contents($filename);
                         Cache::set(array('jsmincdn', $element->resource), $output, 3600 * 24);
                     } elseif (strpos($element->resource, Site::get_url('admin_theme')) === 0) {
                         $script .= '/** FROM (e): ' . $element->resource . " **/\n";
                         $base = substr($element->resource, strlen(Site::get_url('admin_theme')));
                         $filename = HABARI_PATH . '/system/admin' . $base;
                         $output = file_get_contents($filename);
                         Cache::set(array('jsmincdn', $element->resource), $output, 3600 * 24);
                     } elseif (strpos($element->resource, 'http://') === 0 || strpos($element->resource, 'https://') === 0) {
                         $script .= '/** FROM (f): ' . $element->resource . " **/\n";
                         $output = RemoteRequest::get_contents($element->resource);
                         Cache::set(array('jsmincdn', $element->resource), $output, 3600 * 24);
                     } else {
                         $output .= '/** FROM (g): ' . $element->resource . " **/\n";
                         $output = $element->resource;
                     }
                     $script .= JSMin::minify($output);
                 }
                 $restack[$seqname] = new StackItem($seqname, '0', URL::get('jsmincdn', array('name' => $seqname)));
                 Cache::set(array('jsmincdn_post', $seqname), $script, 3600 * 24);
             }
         }
         $stack = $restack;
     }
     return $stack;
 }
 public function get_version_md5($url)
 {
     $file = RemoteRequest::get_contents($url);
     return md5($file);
 }
 public function filter_ping_sites($result)
 {
     $ping = RemoteRequest::get_contents('http://www.pingoat.com/index.php?pingoat=go&blog_name=' . urlencode(Options::get('title')) . '&blog_url=' . urlencode(Site::get_url('habari')) . '&rss_url=' . urlencode(URL::get('collection', array('index' => 1))) . '&cat_0=1&id%5B%5D=0&id%5B%5D=1&id%5B%5D=2&id%5B%5D=3&id%5B%5D=4&id%5B%5D=5&id%5B%5D=6&id%5B%5D=7&id%5B%5D=8&id%5B%5D=9&id%5B%5D=10&id%5B%5D=11&id%5B%5D=12&id%5B%5D=13&id%5B%5D=14&id%5B%5D=15&id%5B%5D=16&id%5B%5D=17&id%5B%5D=18&id%5B%5D=19&id%5B%5D=20&id%5B%5D=21&id%5B%5D=22&id%5B%5D=23&id%5B%5D=24&id%5B%5D=25&cat_1=0&cat_2=0');
     EventLog::log('Ping sent to Pingoat.', 'info', 'default', null, $ping);
     return $result;
 }
Пример #14
0
 public function action_auth_ajax_blogroll_import_opml(ActionHandler $handler)
 {
     $valid_fields = array('opml_url', 'opml_file');
     $inputs = array_intersect_key($_POST->getArrayCopy(), array_flip($valid_fields));
     extract($inputs);
     if (!empty($opml_url)) {
         $file = RemoteRequest::get_contents($opml_url);
     } elseif (!empty($opml_file)) {
         $file = Options::get("blogroll_{$opml_file}");
         Options::delete("blogroll_{$opml_file}");
     }
     try {
         if (empty($file)) {
             throw new \Exception();
         }
         $xml = @new \SimpleXMLElement($file);
         // errors as exceptions++
         $count = $this->import_opml($xml->body);
         echo '<p>';
         printf(_n('Imported %d link from %s', 'Imported %d links from %s', $count, self::DOMAIN), $count, (string) $xml->head->title);
         echo '</p>';
     } catch (\Exception $e) {
         _e('Sorry, could not parse that OPML file. It may be malformed.', self::DOMAIN);
     }
 }
Пример #15
0
 /**
  * Fetch notices from service.
  * @param string $svc The statusnet server
  * @param string $username The user on the statusnet server
  * @param bool $hide_replies Suppress @-notices
  * @param int $limit Number of notices to fetch
  * @param bool $linkify_urls Output anchor HTML for URLS
  * @param string $cache The name of the cache group to use
  * @param int $cachettl Cache duration in seconds
  * @return array notices The status messages
  **/
 public function notices($svc, $username, $hide_replies = false, $limit = 1, $linkify_urls = false, $cache = 'statusnet', $cachettl = 60)
 {
     $notices = array();
     if ($svc && $username != '') {
         $statusnet_url = 'http://' . $svc . '/api/statuses/user_timeline/' . urlencode($username) . '.xml';
         /* 
          * Only need to get a single notice if @replies are hidden.
          * (Otherwise, rely on the maximum returned and hope one is a non-reply.)
          */
         if (!$hide_replies && $limit) {
             $statusnet_url .= '?count=' . $limit;
         }
         // Get cache group.
         if (Cache::has_group($cache)) {
             $notices = Cache::get_group($cache);
         } else {
             try {
                 $response = RemoteRequest::get_contents($statusnet_url);
                 $xml = @new SimpleXMLElement($response);
                 // Check we've got a load of statuses returned
                 if ($xml->getName() === 'statuses') {
                     foreach ($xml->status as $status) {
                         if (!$hide_replies || strpos($status->text, '@') === false) {
                             $notice = (object) array('text' => (string) $status->text, 'time' => (string) $status->created_at, 'image_url' => (string) $status->user->profile_image_url, 'id' => (int) $status->id, 'permalink' => 'http://' . $svc . '/notice/' . (string) $status->id);
                             $notices[] = $notice;
                             if ($hide_replies && count($notices) >= $limit) {
                                 break;
                             }
                         } else {
                             // it's a @. Keep going.
                         }
                     }
                     if (!$notices) {
                         $notice->text = 'Only replies available from service.';
                         $notice->permalink = '';
                         $notice->time = '';
                         $notice->image_url = '';
                     }
                 } else {
                     if ($xml->getName() === 'error') {
                         $notice->text = (string) $xml;
                         $notice->permalink = '';
                         $notice->time = '';
                         $notice->image_url = '';
                     } else {
                         $notice->text = 'Received unexpected XML from service.';
                         $notice->permalink = '';
                         $notice->time = '';
                         $notice->image_url = '';
                     }
                 }
             } catch (Exception $e) {
                 $notice->text = 'Unable to contact service.';
                 $notice->permalink = '';
                 $notice->time = '';
                 $notice->image_url = '';
             }
             if (!$notices) {
                 $notices[] = $notice;
             }
             // Cache (even errors) to avoid hitting rate limit.
             // Use cache group to cache multiple statuses (objects).
             // Name caches dynamically, so one cache per cacher
             // (e.g., several Blocks with different users/services).
             foreach ($notices as $i => $notice) {
                 Cache::set(array($cache, $i), $notice, $cachettl !== false ? $cachettl : StatusNet::DEFAULT_CACHE_SECONDS);
             }
         }
         if ($linkify_urls != FALSE) {
             // http: links
             foreach ($notices as $notice) {
                 $notice->text = preg_replace('%https?://\\S+?(?=(?:[.:?"!$&\'()*+,=]|)(?:\\s|$))%i', "<a href=\"\$0\">\$0</a>", $notice->text);
             }
         }
     } else {
         $notice = (object) array('text' => _t('Check service and/or username', 'statusnet'), 'time' => '', 'image_url' => '');
         $notices[] = $notice;
     }
     return $notices;
 }
Пример #16
0
 /**
  * Outputs cached blocvk image.
  *
  * @param AjaxHandler $handler The AjaxHandler hadling the request.
  */
 public function action_auth_ajax_piwik_graph(AjaxHandler $handler)
 {
     $api_url = urldecode($handler->handler_vars->raw('id'));
     $expires = HabariDateTime::date_create('midnight + 1 day')->int - time();
     if (!Cache::has('piwik_graphs_' . $api_url)) {
         // Cache until midnight.
         Cache::set('piwik_graphs_' . $api_url, base64_encode(RemoteRequest::get_contents($api_url)), $expires);
     }
     // Serve the cached image.
     header('content-type: image/png');
     header('Expires: ' . gmdate("D, d M Y H:i:s", time() + $expires) . ' GMT');
     echo base64_decode(Cache::get('piwik_graphs_' . $api_url));
 }
Пример #17
0
 /**
  * Fetch a CC-licensed thumbnail from Flickr tagged with any of the given tags.
  * 
  * @param array $tags Tags to search for.
  */
 private function generate_thumb($tags = array())
 {
     // cache thumbnails so we only make one http request
     static $thumbs;
     // if we haven't fetched thumbnails yet, do it
     if (!isset($thumbs)) {
         $licenses = '4,2,7';
         // 4 = CC-Attribution, 2 = CC-Attribution-NonCommercial, 7 = no known copyright restrictions
         $url = 'https://www.flickr.com/services/rest?method=flickr.photos.search&api_key=' . self::FLICKR_API_KEY . '&license=' . $licenses . '&per_page=25&tags=' . urlencode(implode(',', $tags));
         // make the api request
         $results = RemoteRequest::get_contents($url);
         // parse the xml
         $xml = new \SimpleXMLElement($results);
         $photos = array();
         foreach ($xml->photos->photo as $photo) {
             $photos[] = array('id' => (string) $photo['id'], 'owner' => (string) $photo['owner'], 'secret' => (string) $photo['secret'], 'server' => (string) $photo['server'], 'title' => (string) $photo['title'], 'img_url' => 'http://static.flickr.com/' . (string) $photo['server'] . '/' . (string) $photo['id'] . '_' . (string) $photo['secret'] . '_m.jpg', 'url' => 'http://flickr.com/photos/' . (string) $photo['owner'] . '/' . (string) $photo['id']);
         }
         // save the photos in our static var
         $thumbs = $photos;
     }
     // now pick out a thumbnail and return the html
     $thumb = $thumbs[array_rand($thumbs)];
     // the various styles we might apply
     $styles = array('left' => 'style="float: left;"', 'right' => 'style="float: right;"', 'block' => 'style="display: block;"');
     // pick out the style we'll use for this one
     $style = $styles[array_rand($styles)];
     $thumb_html = '<a href="' . $thumb['url'] . '" title="' . Utils::htmlspecialchars($thumb['title']) . '" ' . $style . '><img src="' . $thumb['img_url'] . '"></a>';
     return $thumb_html;
 }