news/utility/feedfetcher.php

159 строки
4.5 KiB
PHP
Исходник Обычный вид История

2012-05-28 22:27:18 +04:00
<?php
2012-05-28 22:27:18 +04:00
/**
* ownCloud - News
2012-05-28 22:27:18 +04:00
*
* @author Alessandro Cosentino
* @author Bernhard Posselt
* @copyright 2012 Alessandro Cosentino cosenal@gmail.com
* @copyright 2012 Bernhard Posselt nukeawhale@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
2012-05-28 22:27:18 +04:00
*/
namespace OCA\News\Utility;
use \OCA\News\Db\Item;
use \OCA\News\Db\Feed;
class FeedFetcher {
2012-05-28 22:27:18 +04:00
/**
* Fetch a feed from remote
* @param string url remote url of the feed
* @throws FetcherException if simple pie fails
2013-03-22 15:35:30 +04:00
* @return array an array containing the new feed and its items
2012-05-28 22:27:18 +04:00
*/
public function fetch($url) {
$simplePie = new \SimplePie_Core();
$simplePie->set_feed_url( $url );
$simplePie->enable_cache( false );
2013-03-22 15:35:30 +04:00
if (!$simplePie->init()) {
2013-03-22 15:35:30 +04:00
throw new FetcherException('Could not initialize simple pie');
}
// temporary try-catch to bypass SimplePie bugs
2012-10-23 22:54:55 +04:00
try {
$simplePie->handle_content_type();
2012-10-23 22:54:55 +04:00
$items = array();
if ($feedItems = $simplePie->get_items()) {
foreach($feedItems as $feedItem) {
$item = new Item();
$item->setStatus(0);
$item->setUnread();
$item->setUrl( $feedItem->get_permalink() );
$item->setTitle( $feedItem->get_title() );
$item->setGuid( $feedItem->get_id() );
2013-03-25 14:48:15 +04:00
$item->setGuidHash( md5($feedItem->get_id()) );
$item->setBody( $feedItem->get_content() );
$item->setPubDate( $feedItem->get_date('U') );
$item->setLastModified(time());
$author = $feedItem->get_author();
if ($author !== null) {
$item->setAuthor( $author->get_name() );
2012-10-23 22:54:55 +04:00
}
2013-03-22 15:35:30 +04:00
// TODO: make it work for video files also
$enclosure = $feedItem->get_enclosure();
if($enclosure !== null) {
$enclosureType = $enclosure->get_type();
if(stripos($enclosureType, "audio/") !== false) {
$enclosure->setEnclosureMime($enclosureType);
$enclosure->setEnclosureLink($enclosure->get_link());
}
}
array_push($items, $item);
2012-08-15 20:27:32 +04:00
}
2012-07-30 23:38:58 +04:00
}
$feed = new Feed();
$feed->setTitle( $simplePie->get_title());
$feed->setUrl($url);
$feed->setUrlHash(md5($url));
$feed->setAdded(time());
$favicon = $simplePie->get_image_url();
if ($favicon !== null && $this->checkFavicon($favicon)) {
$feed->setFaviconLink($favicon);
} else {
$webFavicon = $this->discoverFavicon($url);
2012-10-23 22:54:55 +04:00
if ($webFavicon !== null) {
$feed->setFaviconLink($webFavicon);
2012-10-23 22:54:55 +04:00
}
}
return array($feed, $items);
2013-03-22 15:35:30 +04:00
} catch(\Exception $ex){
throw new FetcherException($ex->getMessage());
2012-07-23 23:02:49 +04:00
}
}
public function checkFavicon($favicon) {
2012-10-14 23:15:16 +04:00
if ($favicon === null || $favicon == false)
return false;
$file = new \SimplePie_File($favicon);
2012-07-23 23:56:58 +04:00
// size in bytes
$filesize = strlen($file->body);
2012-08-19 21:49:58 +04:00
if($file->success && $filesize > 0 && $filesize < 50000) { //bigger files are not considered favicons
$sniffer = new \SimplePie_Content_Type_Sniffer($file);
if(substr($sniffer->get_type(), 0, 6) === 'image/') {
$imgsize = @getimagesize($favicon);
if ($imgsize && $imgsize['0'] <= 32 && $imgsize['1'] <= 32) { //bigger images are not considered favicons
2012-08-19 21:49:58 +04:00
return true;
}
}
2012-07-18 01:37:54 +04:00
}
return false;
}
2012-07-22 01:25:39 +04:00
public function discoverFavicon($url) {
//try webroot favicon
$favicon = \SimplePie_Misc::absolutize_url('/favicon.ico', $url);
if($this->checkFavicon($favicon))
return $favicon;
//try to extract favicon from web page
$absoluteUrl = \SimplePie_Misc::absolutize_url('/', $url);
$page = \OC_Util::getUrlContent($absoluteUrl);
2012-10-14 23:15:16 +04:00
if ( FALSE !== $page ) {
preg_match ( '/<[^>]*link[^>]*(rel=["\']icon["\']|rel=["\']shortcut icon["\']) .*href=["\']([^>]*)["\'].*>/iU', $page, $match );
if (1<sizeof($match)) {
// the specified uri might be an url, an absolute or a relative path
// we have to turn it into an url to be able to display it out of context
$favicon = htmlspecialchars_decode ( $match[2] );
// test for an url
if (parse_url($favicon,PHP_URL_SCHEME)) {
if($this->checkFavicon($favicon))
return $favicon;
}
}
}
return null;
2012-05-28 22:27:18 +04:00
}
}