]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
fix of #486 - import issue
authorMaryana Rozhankivska <mariroz@mr.lviv.ua>
Mon, 24 Feb 2014 17:13:54 +0000 (19:13 +0200)
committerMaryana Rozhankivska <mariroz@mr.lviv.ua>
Mon, 24 Feb 2014 17:13:54 +0000 (19:13 +0200)
inc/3rdparty/makefulltextfeed.php
inc/3rdparty/makefulltextfeedHelpers.php [new file with mode: 0755]

index 313397d2d9e35f0e9c380e1db5379dbf05541785..b024547f7b871dfa1a17bb4288c5346850881a36 100755 (executable)
@@ -55,42 +55,8 @@ if (get_magic_quotes_gpc()) {
 \r
 // set include path\r
 set_include_path(realpath(dirname(__FILE__).'/libraries').PATH_SEPARATOR.get_include_path());\r
-// Autoloading of classes allows us to include files only when they're\r
-// needed. If we've got a cached copy, for example, only Zend_Cache is loaded.\r
-function autoload($class_name) {\r
-       static $dir = null;\r
-       if ($dir === null) $dir = dirname(__FILE__).'/libraries/';\r
-       static $mapping = array(\r
-               // Include FeedCreator for RSS/Atom creation\r
-               'FeedWriter' => 'feedwriter/FeedWriter.php',\r
-               'FeedItem' => 'feedwriter/FeedItem.php',\r
-               // Include ContentExtractor and Readability for identifying and extracting content from URLs\r
-               'ContentExtractor' => 'content-extractor/ContentExtractor.php',\r
-               'SiteConfig' => 'content-extractor/SiteConfig.php',\r
-               'Readability' => 'readability/Readability.php',\r
-               // Include Humble HTTP Agent to allow parallel requests and response caching\r
-               'HumbleHttpAgent' => 'humble-http-agent/HumbleHttpAgent.php',\r
-               'SimplePie_HumbleHttpAgent' => 'humble-http-agent/SimplePie_HumbleHttpAgent.php',\r
-               'CookieJar' => 'humble-http-agent/CookieJar.php',\r
-               // Include Zend Cache to improve performance (cache results)\r
-               'Zend_Cache' => 'Zend/Cache.php',\r
-               // Language detect\r
-               'Text_LanguageDetect' => 'language-detect/LanguageDetect.php',\r
-               // HTML5 Lib\r
-               'HTML5_Parser' => 'html5/Parser.php',\r
-               // htmLawed - used if XSS filter is enabled (xss_filter)\r
-               'htmLawed' => 'htmLawed/htmLawed.php'\r
-       );\r
-       if (isset($mapping[$class_name])) {\r
-               debug("** Loading class $class_name ({$mapping[$class_name]})");\r
-               require $dir.$mapping[$class_name];\r
-               return true;\r
-       } else {\r
-               return false;\r
-       }\r
-}\r
-spl_autoload_register('autoload');\r
-require dirname(__FILE__).'/libraries/simplepie/autoloader.php';\r
+\r
+require_once dirname(__FILE__).'/makefulltextfeedHelpers.php';\r
 \r
 ////////////////////////////////\r
 // Load config file\r
@@ -415,7 +381,6 @@ if (!$debug_mode) {
 //////////////////////////////////\r
 // Set up HTTP agent\r
 //////////////////////////////////\r
-global $http;\r
 $http = new HumbleHttpAgent();\r
 $http->debug = $debug_mode;\r
 $http->userAgentMap = $options->user_agents;\r
@@ -479,29 +444,6 @@ if ($html_only || !$result) {
        $isDummyFeed = true;\r
        unset($feed, $result);\r
        // create single item dummy feed object\r
-       class DummySingleItemFeed {\r
-               public $item;\r
-               function __construct($url) { $this->item = new DummySingleItem($url); }\r
-               public function get_title() { return ''; }\r
-               public function get_description() { return 'Content extracted from '.$this->item->url; }\r
-               public function get_link() { return $this->item->url; }\r
-               public function get_language() { return false; }\r
-               public function get_image_url() { return false; }\r
-               public function get_items($start=0, $max=1) { return array(0=>$this->item); }\r
-       }\r
-       class DummySingleItem {\r
-               public $url;\r
-               function __construct($url) { $this->url = $url; }\r
-               public function get_permalink() { return $this->url; }\r
-               public function get_title() { return null; }\r
-               public function get_date($format='') { return false; }\r
-               public function get_author($key=0) { return null; }\r
-               public function get_authors() { return null; }\r
-               public function get_description() { return ''; }\r
-               public function get_enclosure($key=0, $prefer=null) { return null; }\r
-               public function get_enclosures() { return null; }\r
-               public function get_categories() { return null; }\r
-       }\r
        $feed = new DummySingleItemFeed($url);\r
 }\r
 \r
@@ -904,294 +846,3 @@ if (!$debug_mode) {
        if ($callback) echo ');';\r
 }\r
 \r
-///////////////////////////////\r
-// HELPER FUNCTIONS\r
-///////////////////////////////\r
-\r
-function url_allowed($url) {\r
-       global $options;\r
-       if (!empty($options->allowed_urls)) {\r
-               $allowed = false;\r
-               foreach ($options->allowed_urls as $allowurl) {\r
-                       if (stristr($url, $allowurl) !== false) {\r
-                               $allowed = true;\r
-                               break;\r
-                       }\r
-               }\r
-               if (!$allowed) return false;\r
-       } else {\r
-               foreach ($options->blocked_urls as $blockurl) {\r
-                       if (stristr($url, $blockurl) !== false) {\r
-                               return false;\r
-                       }\r
-               }\r
-       }\r
-       return true;\r
-}\r
-\r
-//////////////////////////////////////////////\r
-// Convert $html to UTF8\r
-// (uses HTTP headers and HTML to find encoding)\r
-// adapted from http://stackoverflow.com/questions/910793/php-detect-encoding-and-make-everything-utf-8\r
-//////////////////////////////////////////////\r
-function convert_to_utf8($html, $header=null)\r
-{\r
-       $encoding = null;\r
-       if ($html || $header) {\r
-               if (is_array($header)) $header = implode("\n", $header);\r
-               if (!$header || !preg_match_all('/^Content-Type:\s+([^;]+)(?:;\s*charset=["\']?([^;"\'\n]*))?/im', $header, $match, PREG_SET_ORDER)) {\r
-                       // error parsing the response\r
-                       debug('Could not find Content-Type header in HTTP response');\r
-               } else {\r
-                       $match = end($match); // get last matched element (in case of redirects)\r
-                       if (isset($match[2])) $encoding = trim($match[2], "\"' \r\n\0\x0B\t");\r
-               }\r
-               // TODO: check to see if encoding is supported (can we convert it?)\r
-               // If it's not, result will be empty string.\r
-               // For now we'll check for invalid encoding types returned by some sites, e.g. 'none'\r
-               // Problem URL: http://facta.co.jp/blog/archives/20111026001026.html\r
-               if (!$encoding || $encoding == 'none') {\r
-                       // search for encoding in HTML - only look at the first 50000 characters\r
-                       // Why 50000? See, for example, http://www.lemonde.fr/festival-de-cannes/article/2012/05/23/deux-cretes-en-goguette-sur-la-croisette_1705732_766360.html\r
-                       // TODO: improve this so it looks at smaller chunks first\r
-                       $html_head = substr($html, 0, 50000);\r
-                       if (preg_match('/^<\?xml\s+version=(?:"[^"]*"|\'[^\']*\')\s+encoding=("[^"]*"|\'[^\']*\')/s', $html_head, $match)) {\r
-                               $encoding = trim($match[1], '"\'');\r
-                       } elseif (preg_match('/<meta\s+http-equiv=["\']?Content-Type["\']? content=["\'][^;]+;\s*charset=["\']?([^;"\'>]+)/i', $html_head, $match)) {\r
-                               $encoding = trim($match[1]);\r
-                       } elseif (preg_match_all('/<meta\s+([^>]+)>/i', $html_head, $match)) {\r
-                               foreach ($match[1] as $_test) {\r
-                                       if (preg_match('/charset=["\']?([^"\']+)/i', $_test, $_m)) {\r
-                                               $encoding = trim($_m[1]);\r
-                                               break;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               if (isset($encoding)) $encoding = trim($encoding);\r
-               // trim is important here!\r
-               if (!$encoding || (strtolower($encoding) == 'iso-8859-1')) {\r
-                       // replace MS Word smart qutoes\r
-                       $trans = array();\r
-                       $trans[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark\r
-                       $trans[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook\r
-                       $trans[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark\r
-                       $trans[chr(133)] = '&hellip;';    // Horizontal Ellipsis\r
-                       $trans[chr(134)] = '&dagger;';    // Dagger\r
-                       $trans[chr(135)] = '&Dagger;';    // Double Dagger\r
-                       $trans[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent\r
-                       $trans[chr(137)] = '&permil;';    // Per Mille Sign\r
-                       $trans[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron\r
-                       $trans[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark\r
-                       $trans[chr(140)] = '&OElig;';    // Latin Capital Ligature OE\r
-                       $trans[chr(145)] = '&lsquo;';    // Left Single Quotation Mark\r
-                       $trans[chr(146)] = '&rsquo;';    // Right Single Quotation Mark\r
-                       $trans[chr(147)] = '&ldquo;';    // Left Double Quotation Mark\r
-                       $trans[chr(148)] = '&rdquo;';    // Right Double Quotation Mark\r
-                       $trans[chr(149)] = '&bull;';    // Bullet\r
-                       $trans[chr(150)] = '&ndash;';    // En Dash\r
-                       $trans[chr(151)] = '&mdash;';    // Em Dash\r
-                       $trans[chr(152)] = '&tilde;';    // Small Tilde\r
-                       $trans[chr(153)] = '&trade;';    // Trade Mark Sign\r
-                       $trans[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron\r
-                       $trans[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark\r
-                       $trans[chr(156)] = '&oelig;';    // Latin Small Ligature OE\r
-                       $trans[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis\r
-                       $html = strtr($html, $trans);\r
-               }\r
-               if (!$encoding) {\r
-                       debug('No character encoding found, so treating as UTF-8');\r
-                       $encoding = 'utf-8';\r
-               } else {\r
-                       debug('Character encoding: '.$encoding);\r
-                       if (strtolower($encoding) != 'utf-8') {\r
-                               debug('Converting to UTF-8');\r
-                               $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8');\r
-                               /*\r
-                               if (function_exists('iconv')) {\r
-                                       // iconv appears to handle certain character encodings better than mb_convert_encoding\r
-                                       $html = iconv($encoding, 'utf-8', $html);\r
-                               } else {\r
-                                       $html = mb_convert_encoding($html, 'utf-8', $encoding);\r
-                               }\r
-                               */\r
-                       }\r
-               }\r
-       }\r
-       return $html;\r
-}\r
-\r
-function makeAbsolute($base, $elem) {\r
-       $base = new SimplePie_IRI($base);\r
-       // remove '//' in URL path (used to prevent URLs from resolving properly)\r
-       // TODO: check if this is still the case\r
-       if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path);\r
-       foreach(array('a'=>'href', 'img'=>'src') as $tag => $attr) {\r
-               $elems = $elem->getElementsByTagName($tag);\r
-               for ($i = $elems->length-1; $i >= 0; $i--) {\r
-                       $e = $elems->item($i);\r
-                       //$e->parentNode->replaceChild($articleContent->ownerDocument->createTextNode($e->textContent), $e);\r
-                       makeAbsoluteAttr($base, $e, $attr);\r
-               }\r
-               if (strtolower($elem->tagName) == $tag) makeAbsoluteAttr($base, $elem, $attr);\r
-       }\r
-}\r
-function makeAbsoluteAttr($base, $e, $attr) {\r
-       if ($e->hasAttribute($attr)) {\r
-               // Trim leading and trailing white space. I don't really like this but \r
-               // unfortunately it does appear on some sites. e.g.  <img src=" /path/to/image.jpg" />\r
-               $url = trim(str_replace('%20', ' ', $e->getAttribute($attr)));\r
-               $url = str_replace(' ', '%20', $url);\r
-               if (!preg_match('!https?://!i', $url)) {\r
-                       if ($absolute = SimplePie_IRI::absolutize($base, $url)) {\r
-                               $e->setAttribute($attr, $absolute);\r
-                       }\r
-               }\r
-       }\r
-}\r
-function makeAbsoluteStr($base, $url) {\r
-       $base = new SimplePie_IRI($base);\r
-       // remove '//' in URL path (causes URLs not to resolve properly)\r
-       if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path);\r
-       if (preg_match('!^https?://!i', $url)) {\r
-               // already absolute\r
-               return $url;\r
-       } else {\r
-               if ($absolute = SimplePie_IRI::absolutize($base, $url)) {\r
-                       return $absolute;\r
-               }\r
-               return false;\r
-       }\r
-}\r
-// returns single page response, or false if not found\r
-function getSinglePage($item, $html, $url) {\r
-       global $http, $extractor;\r
-       debug('Looking for site config files to see if single page link exists');\r
-       $site_config = $extractor->buildSiteConfig($url, $html);\r
-       $splink = null;\r
-       if (!empty($site_config->single_page_link)) {\r
-               $splink = $site_config->single_page_link;\r
-       } elseif (!empty($site_config->single_page_link_in_feed)) {\r
-               // single page link xpath is targeted at feed\r
-               $splink = $site_config->single_page_link_in_feed;\r
-               // so let's replace HTML with feed item description\r
-               $html = $item->get_description();\r
-       }\r
-       if (isset($splink)) {\r
-               // Build DOM tree from HTML\r
-               $readability = new Readability($html, $url);\r
-               $xpath = new DOMXPath($readability->dom);\r
-               // Loop through single_page_link xpath expressions\r
-               $single_page_url = null;\r
-               foreach ($splink as $pattern) {\r
-                       $elems = @$xpath->evaluate($pattern, $readability->dom);\r
-                       if (is_string($elems)) {\r
-                               $single_page_url = trim($elems);\r
-                               break;\r
-                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {\r
-                               foreach ($elems as $item) {\r
-                                       if ($item instanceof DOMElement && $item->hasAttribute('href')) {\r
-                                               $single_page_url = $item->getAttribute('href');\r
-                                               break 2;\r
-                                       } elseif ($item instanceof DOMAttr && $item->value) {\r
-                                               $single_page_url = $item->value;\r
-                                               break 2;\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-               // If we've got URL, resolve against $url\r
-               if (isset($single_page_url) && ($single_page_url = makeAbsoluteStr($url, $single_page_url))) {\r
-                       // check it's not what we have already!\r
-                       if ($single_page_url != $url) {\r
-                               // it's not, so let's try to fetch it...\r
-                               $_prev_ref = $http->referer;\r
-                               $http->referer = $single_page_url;\r
-                               if (($response = $http->get($single_page_url, true)) && $response['status_code'] < 300) {\r
-                                       $http->referer = $_prev_ref;\r
-                                       return $response;\r
-                               }\r
-                               $http->referer = $_prev_ref;\r
-                       }\r
-               }\r
-       }\r
-       return false;\r
-}\r
-\r
-// based on content-type http header, decide what to do\r
-// param: HTTP headers string\r
-// return: array with keys: 'mime', 'type', 'subtype', 'action', 'name'\r
-// e.g. array('mime'=>'image/jpeg', 'type'=>'image', 'subtype'=>'jpeg', 'action'=>'link', 'name'=>'Image')\r
-function get_mime_action_info($headers) {\r
-       global $options;\r
-       // check if action defined for returned Content-Type\r
-       $info = array();\r
-       if (preg_match('!^Content-Type:\s*(([-\w]+)/([-\w\+]+))!im', $headers, $match)) {\r
-               // look for full mime type (e.g. image/jpeg) or just type (e.g. image)\r
-               // match[1] = full mime type, e.g. image/jpeg\r
-               // match[2] = first part, e.g. image\r
-               // match[3] = last part, e.g. jpeg\r
-               $info['mime'] = strtolower(trim($match[1]));\r
-               $info['type'] = strtolower(trim($match[2]));\r
-               $info['subtype'] = strtolower(trim($match[3]));\r
-               foreach (array($info['mime'], $info['type']) as $_mime) {\r
-                       if (isset($options->content_type_exc[$_mime])) {\r
-                               $info['action'] = $options->content_type_exc[$_mime]['action'];\r
-                               $info['name'] = $options->content_type_exc[$_mime]['name'];\r
-                               break;\r
-                       }\r
-               }\r
-       }\r
-       return $info;\r
-}\r
-\r
-function remove_url_cruft($url) {\r
-       // remove google analytics for the time being\r
-       // regex adapted from http://navitronic.co.uk/2010/12/removing-google-analytics-cruft-from-urls/\r
-       // https://gist.github.com/758177\r
-       return preg_replace('/(\?|\&)utm_[a-z]+=[^\&]+/', '', $url);\r
-}\r
-\r
-function make_substitutions($string) {\r
-       if ($string == '') return $string;\r
-       global $item, $effective_url;\r
-       $string = str_replace('{url}', htmlspecialchars($item->get_permalink()), $string);\r
-       $string = str_replace('{effective-url}', htmlspecialchars($effective_url), $string);\r
-       return $string;\r
-}\r
-\r
-function get_cache() {\r
-       global $options, $valid_key;\r
-       static $cache = null;\r
-       if ($cache === null) {\r
-               $frontendOptions = array(\r
-                       'lifetime' => 10*60, // cache lifetime of 10 minutes\r
-                       'automatic_serialization' => false,\r
-                       'write_control' => false,\r
-                       'automatic_cleaning_factor' => $options->cache_cleanup,\r
-                       'ignore_user_abort' => false\r
-               );\r
-               $backendOptions = array(\r
-                       'cache_dir' => ($valid_key) ? $options->cache_dir.'/rss-with-key/' : $options->cache_dir.'/rss/', // directory where to put the cache files\r
-                       'file_locking' => false,\r
-                       'read_control' => true,\r
-                       'read_control_type' => 'strlen',\r
-                       'hashed_directory_level' => $options->cache_directory_level,\r
-                       'hashed_directory_perm' => 0777,\r
-                       'cache_file_perm' => 0664,\r
-                       'file_name_prefix' => 'ff'\r
-               );\r
-               // getting a Zend_Cache_Core object\r
-               $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);\r
-       }\r
-       return $cache;\r
-}\r
-\r
-function debug($msg) {\r
-       global $debug_mode;\r
-       if ($debug_mode) {\r
-               echo '* ',$msg,"\n";\r
-               ob_flush();\r
-               flush();\r
-       }\r
-}
\ No newline at end of file
diff --git a/inc/3rdparty/makefulltextfeedHelpers.php b/inc/3rdparty/makefulltextfeedHelpers.php
new file mode 100755 (executable)
index 0000000..1c11b8f
--- /dev/null
@@ -0,0 +1,355 @@
+<?php
+
+// Autoloading of classes allows us to include files only when they're
+// needed. If we've got a cached copy, for example, only Zend_Cache is loaded.
+function autoload($class_name) {
+       static $dir = null;
+       if ($dir === null) $dir = dirname(__FILE__).'/libraries/';
+       static $mapping = array(
+               // Include FeedCreator for RSS/Atom creation
+               'FeedWriter' => 'feedwriter/FeedWriter.php',
+               'FeedItem' => 'feedwriter/FeedItem.php',
+               // Include ContentExtractor and Readability for identifying and extracting content from URLs
+               'ContentExtractor' => 'content-extractor/ContentExtractor.php',
+               'SiteConfig' => 'content-extractor/SiteConfig.php',
+               'Readability' => 'readability/Readability.php',
+               // Include Humble HTTP Agent to allow parallel requests and response caching
+               'HumbleHttpAgent' => 'humble-http-agent/HumbleHttpAgent.php',
+               'SimplePie_HumbleHttpAgent' => 'humble-http-agent/SimplePie_HumbleHttpAgent.php',
+               'CookieJar' => 'humble-http-agent/CookieJar.php',
+               // Include Zend Cache to improve performance (cache results)
+               'Zend_Cache' => 'Zend/Cache.php',
+               // Language detect
+               'Text_LanguageDetect' => 'language-detect/LanguageDetect.php',
+               // HTML5 Lib
+               'HTML5_Parser' => 'html5/Parser.php',
+               // htmLawed - used if XSS filter is enabled (xss_filter)
+               'htmLawed' => 'htmLawed/htmLawed.php'
+       );
+       if (isset($mapping[$class_name])) {
+               debug("** Loading class $class_name ({$mapping[$class_name]})");
+               require $dir.$mapping[$class_name];
+               return true;
+       } else {
+               return false;
+       }
+}
+spl_autoload_register('autoload');
+require dirname(__FILE__).'/libraries/simplepie/autoloader.php';
+
+
+class DummySingleItemFeed {
+       public $item;
+       function __construct($url) { $this->item = new DummySingleItem($url); }
+       public function get_title() { return ''; }
+       public function get_description() { return 'Content extracted from '.$this->item->url; }
+       public function get_link() { return $this->item->url; }
+       public function get_language() { return false; }
+       public function get_image_url() { return false; }
+       public function get_items($start=0, $max=1) { return array(0=>$this->item); }
+}
+class DummySingleItem {
+       public $url;
+       function __construct($url) { $this->url = $url; }
+       public function get_permalink() { return $this->url; }
+       public function get_title() { return null; }
+       public function get_date($format='') { return false; }
+       public function get_author($key=0) { return null; }
+       public function get_authors() { return null; }
+       public function get_description() { return ''; }
+       public function get_enclosure($key=0, $prefer=null) { return null; }
+       public function get_enclosures() { return null; }
+       public function get_categories() { return null; }
+}
+
+///////////////////////////////
+// HELPER FUNCTIONS
+///////////////////////////////
+
+function url_allowed($url) {
+       global $options;
+       if (!empty($options->allowed_urls)) {
+               $allowed = false;
+               foreach ($options->allowed_urls as $allowurl) {
+                       if (stristr($url, $allowurl) !== false) {
+                               $allowed = true;
+                               break;
+                       }
+               }
+               if (!$allowed) return false;
+       } else {
+               foreach ($options->blocked_urls as $blockurl) {
+                       if (stristr($url, $blockurl) !== false) {
+                               return false;
+                       }
+               }
+       }
+       return true;
+}
+
+//////////////////////////////////////////////
+// Convert $html to UTF8
+// (uses HTTP headers and HTML to find encoding)
+// adapted from http://stackoverflow.com/questions/910793/php-detect-encoding-and-make-everything-utf-8
+//////////////////////////////////////////////
+function convert_to_utf8($html, $header=null)
+{
+       $encoding = null;
+       if ($html || $header) {
+               if (is_array($header)) $header = implode("\n", $header);
+               if (!$header || !preg_match_all('/^Content-Type:\s+([^;]+)(?:;\s*charset=["\']?([^;"\'\n]*))?/im', $header, $match, PREG_SET_ORDER)) {
+                       // error parsing the response
+                       debug('Could not find Content-Type header in HTTP response');
+               } else {
+                       $match = end($match); // get last matched element (in case of redirects)
+                       if (isset($match[2])) $encoding = trim($match[2], "\"' \r\n\0\x0B\t");
+               }
+               // TODO: check to see if encoding is supported (can we convert it?)
+               // If it's not, result will be empty string.
+               // For now we'll check for invalid encoding types returned by some sites, e.g. 'none'
+               // Problem URL: http://facta.co.jp/blog/archives/20111026001026.html
+               if (!$encoding || $encoding == 'none') {
+                       // search for encoding in HTML - only look at the first 50000 characters
+                       // Why 50000? See, for example, http://www.lemonde.fr/festival-de-cannes/article/2012/05/23/deux-cretes-en-goguette-sur-la-croisette_1705732_766360.html
+                       // TODO: improve this so it looks at smaller chunks first
+                       $html_head = substr($html, 0, 50000);
+                       if (preg_match('/^<\?xml\s+version=(?:"[^"]*"|\'[^\']*\')\s+encoding=("[^"]*"|\'[^\']*\')/s', $html_head, $match)) {
+                               $encoding = trim($match[1], '"\'');
+                       } elseif (preg_match('/<meta\s+http-equiv=["\']?Content-Type["\']? content=["\'][^;]+;\s*charset=["\']?([^;"\'>]+)/i', $html_head, $match)) {
+                               $encoding = trim($match[1]);
+                       } elseif (preg_match_all('/<meta\s+([^>]+)>/i', $html_head, $match)) {
+                               foreach ($match[1] as $_test) {
+                                       if (preg_match('/charset=["\']?([^"\']+)/i', $_test, $_m)) {
+                                               $encoding = trim($_m[1]);
+                                               break;
+                                       }
+                               }
+                       }
+               }
+               if (isset($encoding)) $encoding = trim($encoding);
+               // trim is important here!
+               if (!$encoding || (strtolower($encoding) == 'iso-8859-1')) {
+                       // replace MS Word smart qutoes
+                       $trans = array();
+                       $trans[chr(130)] = '&sbquo;';    // Single Low-9 Quotation Mark
+                       $trans[chr(131)] = '&fnof;';    // Latin Small Letter F With Hook
+                       $trans[chr(132)] = '&bdquo;';    // Double Low-9 Quotation Mark
+                       $trans[chr(133)] = '&hellip;';    // Horizontal Ellipsis
+                       $trans[chr(134)] = '&dagger;';    // Dagger
+                       $trans[chr(135)] = '&Dagger;';    // Double Dagger
+                       $trans[chr(136)] = '&circ;';    // Modifier Letter Circumflex Accent
+                       $trans[chr(137)] = '&permil;';    // Per Mille Sign
+                       $trans[chr(138)] = '&Scaron;';    // Latin Capital Letter S With Caron
+                       $trans[chr(139)] = '&lsaquo;';    // Single Left-Pointing Angle Quotation Mark
+                       $trans[chr(140)] = '&OElig;';    // Latin Capital Ligature OE
+                       $trans[chr(145)] = '&lsquo;';    // Left Single Quotation Mark
+                       $trans[chr(146)] = '&rsquo;';    // Right Single Quotation Mark
+                       $trans[chr(147)] = '&ldquo;';    // Left Double Quotation Mark
+                       $trans[chr(148)] = '&rdquo;';    // Right Double Quotation Mark
+                       $trans[chr(149)] = '&bull;';    // Bullet
+                       $trans[chr(150)] = '&ndash;';    // En Dash
+                       $trans[chr(151)] = '&mdash;';    // Em Dash
+                       $trans[chr(152)] = '&tilde;';    // Small Tilde
+                       $trans[chr(153)] = '&trade;';    // Trade Mark Sign
+                       $trans[chr(154)] = '&scaron;';    // Latin Small Letter S With Caron
+                       $trans[chr(155)] = '&rsaquo;';    // Single Right-Pointing Angle Quotation Mark
+                       $trans[chr(156)] = '&oelig;';    // Latin Small Ligature OE
+                       $trans[chr(159)] = '&Yuml;';    // Latin Capital Letter Y With Diaeresis
+                       $html = strtr($html, $trans);
+               }
+               if (!$encoding) {
+                       debug('No character encoding found, so treating as UTF-8');
+                       $encoding = 'utf-8';
+               } else {
+                       debug('Character encoding: '.$encoding);
+                       if (strtolower($encoding) != 'utf-8') {
+                               debug('Converting to UTF-8');
+                               $html = SimplePie_Misc::change_encoding($html, $encoding, 'utf-8');
+                               /*
+                               if (function_exists('iconv')) {
+                                       // iconv appears to handle certain character encodings better than mb_convert_encoding
+                                       $html = iconv($encoding, 'utf-8', $html);
+                               } else {
+                                       $html = mb_convert_encoding($html, 'utf-8', $encoding);
+                               }
+                               */
+                       }
+               }
+       }
+       return $html;
+}
+
+function makeAbsolute($base, $elem) {
+       $base = new SimplePie_IRI($base);
+       // remove '//' in URL path (used to prevent URLs from resolving properly)
+       // TODO: check if this is still the case
+       if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path);
+       foreach(array('a'=>'href', 'img'=>'src') as $tag => $attr) {
+               $elems = $elem->getElementsByTagName($tag);
+               for ($i = $elems->length-1; $i >= 0; $i--) {
+                       $e = $elems->item($i);
+                       //$e->parentNode->replaceChild($articleContent->ownerDocument->createTextNode($e->textContent), $e);
+                       makeAbsoluteAttr($base, $e, $attr);
+               }
+               if (strtolower($elem->tagName) == $tag) makeAbsoluteAttr($base, $elem, $attr);
+       }
+}
+function makeAbsoluteAttr($base, $e, $attr) {
+       if ($e->hasAttribute($attr)) {
+               // Trim leading and trailing white space. I don't really like this but 
+               // unfortunately it does appear on some sites. e.g.  <img src=" /path/to/image.jpg" />
+               $url = trim(str_replace('%20', ' ', $e->getAttribute($attr)));
+               $url = str_replace(' ', '%20', $url);
+               if (!preg_match('!https?://!i', $url)) {
+                       if ($absolute = SimplePie_IRI::absolutize($base, $url)) {
+                               $e->setAttribute($attr, $absolute);
+                       }
+               }
+       }
+}
+function makeAbsoluteStr($base, $url) {
+       $base = new SimplePie_IRI($base);
+       // remove '//' in URL path (causes URLs not to resolve properly)
+       if (isset($base->path)) $base->path = preg_replace('!//+!', '/', $base->path);
+       if (preg_match('!^https?://!i', $url)) {
+               // already absolute
+               return $url;
+       } else {
+               if ($absolute = SimplePie_IRI::absolutize($base, $url)) {
+                       return $absolute;
+               }
+               return false;
+       }
+}
+// returns single page response, or false if not found
+function getSinglePage($item, $html, $url) {
+       global $http, $extractor;
+       debug('Looking for site config files to see if single page link exists');
+       $site_config = $extractor->buildSiteConfig($url, $html);
+       $splink = null;
+       if (!empty($site_config->single_page_link)) {
+               $splink = $site_config->single_page_link;
+       } elseif (!empty($site_config->single_page_link_in_feed)) {
+               // single page link xpath is targeted at feed
+               $splink = $site_config->single_page_link_in_feed;
+               // so let's replace HTML with feed item description
+               $html = $item->get_description();
+       }
+       if (isset($splink)) {
+               // Build DOM tree from HTML
+               $readability = new Readability($html, $url);
+               $xpath = new DOMXPath($readability->dom);
+               // Loop through single_page_link xpath expressions
+               $single_page_url = null;
+               foreach ($splink as $pattern) {
+                       $elems = @$xpath->evaluate($pattern, $readability->dom);
+                       if (is_string($elems)) {
+                               $single_page_url = trim($elems);
+                               break;
+                       } elseif ($elems instanceof DOMNodeList && $elems->length > 0) {
+                               foreach ($elems as $item) {
+                                       if ($item instanceof DOMElement && $item->hasAttribute('href')) {
+                                               $single_page_url = $item->getAttribute('href');
+                                               break 2;
+                                       } elseif ($item instanceof DOMAttr && $item->value) {
+                                               $single_page_url = $item->value;
+                                               break 2;
+                                       }
+                               }
+                       }
+               }
+               // If we've got URL, resolve against $url
+               if (isset($single_page_url) && ($single_page_url = makeAbsoluteStr($url, $single_page_url))) {
+                       // check it's not what we have already!
+                       if ($single_page_url != $url) {
+                               // it's not, so let's try to fetch it...
+                               $_prev_ref = $http->referer;
+                               $http->referer = $single_page_url;
+                               if (($response = $http->get($single_page_url, true)) && $response['status_code'] < 300) {
+                                       $http->referer = $_prev_ref;
+                                       return $response;
+                               }
+                               $http->referer = $_prev_ref;
+                       }
+               }
+       }
+       return false;
+}
+
+// based on content-type http header, decide what to do
+// param: HTTP headers string
+// return: array with keys: 'mime', 'type', 'subtype', 'action', 'name'
+// e.g. array('mime'=>'image/jpeg', 'type'=>'image', 'subtype'=>'jpeg', 'action'=>'link', 'name'=>'Image')
+function get_mime_action_info($headers) {
+       global $options;
+       // check if action defined for returned Content-Type
+       $info = array();
+       if (preg_match('!^Content-Type:\s*(([-\w]+)/([-\w\+]+))!im', $headers, $match)) {
+               // look for full mime type (e.g. image/jpeg) or just type (e.g. image)
+               // match[1] = full mime type, e.g. image/jpeg
+               // match[2] = first part, e.g. image
+               // match[3] = last part, e.g. jpeg
+               $info['mime'] = strtolower(trim($match[1]));
+               $info['type'] = strtolower(trim($match[2]));
+               $info['subtype'] = strtolower(trim($match[3]));
+               foreach (array($info['mime'], $info['type']) as $_mime) {
+                       if (isset($options->content_type_exc[$_mime])) {
+                               $info['action'] = $options->content_type_exc[$_mime]['action'];
+                               $info['name'] = $options->content_type_exc[$_mime]['name'];
+                               break;
+                       }
+               }
+       }
+       return $info;
+}
+
+function remove_url_cruft($url) {
+       // remove google analytics for the time being
+       // regex adapted from http://navitronic.co.uk/2010/12/removing-google-analytics-cruft-from-urls/
+       // https://gist.github.com/758177
+       return preg_replace('/(\?|\&)utm_[a-z]+=[^\&]+/', '', $url);
+}
+
+function make_substitutions($string) {
+       if ($string == '') return $string;
+       global $item, $effective_url;
+       $string = str_replace('{url}', htmlspecialchars($item->get_permalink()), $string);
+       $string = str_replace('{effective-url}', htmlspecialchars($effective_url), $string);
+       return $string;
+}
+
+function get_cache() {
+       global $options, $valid_key;
+       static $cache = null;
+       if ($cache === null) {
+               $frontendOptions = array(
+                       'lifetime' => 10*60, // cache lifetime of 10 minutes
+                       'automatic_serialization' => false,
+                       'write_control' => false,
+                       'automatic_cleaning_factor' => $options->cache_cleanup,
+                       'ignore_user_abort' => false
+               );
+               $backendOptions = array(
+                       'cache_dir' => ($valid_key) ? $options->cache_dir.'/rss-with-key/' : $options->cache_dir.'/rss/', // directory where to put the cache files
+                       'file_locking' => false,
+                       'read_control' => true,
+                       'read_control_type' => 'strlen',
+                       'hashed_directory_level' => $options->cache_directory_level,
+                       'hashed_directory_perm' => 0777,
+                       'cache_file_perm' => 0664,
+                       'file_name_prefix' => 'ff'
+               );
+               // getting a Zend_Cache_Core object
+               $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);
+       }
+       return $cache;
+}
+
+function debug($msg) {
+       global $debug_mode;
+       if ($debug_mode) {
+               echo '* ',$msg,"\n";
+               ob_flush();
+               flush();
+       }
+}