* * This file is licensed under the Affero General Public License version 3 or later. * See the COPYING-README file * */ namespace OCA\News; // load SimplePie library //TODO: is this a suitable place for the following require? require_once('news/3rdparty/SimplePie/autoloader.php'); class Utils { /** * @brief Fetch a feed from remote * @param url remote url of the feed * @returns an instance of OC_News_Feed */ public static function fetch($url){ $spfeed = new \SimplePie_Core(); $spfeed->set_feed_url( $url ); $spfeed->enable_cache( false ); if (!$spfeed->init()) { return null; } //I understand this try-catch sucks, but SimplePie gives weird errors sometimes try { $spfeed->handle_content_type(); $title = $spfeed->get_title(); $items = array(); if ($spitems = $spfeed->get_items()) { foreach($spitems as $spitem) { $itemUrl = $spitem->get_permalink(); $itemTitle = $spitem->get_title(); $itemGUID = $spitem->get_id(); $itemBody = $spitem->get_content(); $itemAuthor = $spitem->get_author(); $item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody); $item->setAuthor($itemAuthor->get_name()); $items[] = $item; } } $feed = new Feed($url, $title, $items); $favicon = $spfeed->get_image_url(); if ($favicon !== null) { // use favicon from feed if(self::checkFavicon($favicon)) $feed->setFavicon($favicon); } else { // try really hard to find a favicon if( null !== ($webFavicon = self::discoverFavicon($url)) ) $feed->setFavicon($webFavicon); } return $feed; } catch (Exception $e) { return null; } } public static function checkFavicon($favicon) { $file = new \SimplePie_File($favicon); // size in bytes $filesize = strlen($file->body); if($file->success && $filesize > 0 && $filesize < 50000) { $sniffer = new \SimplePie_Content_Type_Sniffer($file); if(substr($sniffer->get_type(), 0, 6) === 'image/') { return true; } } return false; } public static function discoverFavicon($url) { //try webroot favicon $favicon = \SimplePie_Misc::absolutize_url('/favicon.ico', $url); if(self::checkFavicon($favicon)) return $favicon; //try to extract favicon from web page $absoluteUrl = \SimplePie_Misc::absolutize_url('/', $url); $handle = curl_init ( ); curl_setopt ( $handle, CURLOPT_URL, $absoluteUrl ); curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, TRUE ); curl_setopt ( $handle, CURLOPT_MAXREDIRS, 10 ); if ( FALSE!==($page=curl_exec($handle)) ) { preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match ); if (1