Add UserAgent and HTTP connection timeout

This commit is contained in:
Frederic Guillot 2013-04-06 09:19:29 -04:00
parent 6f9c55df8a
commit 402b2fe7a9
4 changed files with 49 additions and 5 deletions

View File

@ -11,6 +11,8 @@ require 'model.php';
const DB_VERSION = 1; const DB_VERSION = 1;
const APP_VERSION = 'master'; const APP_VERSION = 'master';
const APP_USERAGENT = 'Miniflux - http://miniflux.net';
const HTTP_TIMEOUT = 5;
// For future use... // For future use...

View File

@ -64,7 +64,7 @@ function import_feeds($content)
function import_feed($url) function import_feed($url)
{ {
$reader = new Reader; $reader = new Reader;
$reader->download($url); $reader->download($url, HTTP_TIMEOUT, APP_USERAGENT);
$parser = $reader->getParser(); $parser = $reader->getParser();
@ -268,7 +268,7 @@ function update_feeds()
foreach (get_feeds() as $feed) { foreach (get_feeds() as $feed) {
$reader = new Reader; $reader = new Reader;
$reader->download($feed['feed_url']); $reader->download($feed['feed_url'], HTTP_TIMEOUT, APP_USERAGENT);
$parser = $reader->getParser(); $parser = $reader->getParser();
if ($parser !== false) { if ($parser !== false) {
@ -284,7 +284,7 @@ function update_feed($feed_id)
$feed = get_feed($feed_id); $feed = get_feed($feed_id);
$reader = new Reader; $reader = new Reader;
$reader->download($feed['feed_url']); $reader->download($feed['feed_url'], HTTP_TIMEOUT, APP_USERAGENT);
$parser = $reader->getParser(); $parser = $reader->getParser();
if ($parser !== false) { if ($parser !== false) {

View File

@ -6,6 +6,12 @@ class Export
{ {
private $content = array(); private $content = array();
public $required_fields = array(
'title',
'site_url',
'feed_url'
);
public function __construct(array $content) public function __construct(array $content)
{ {
@ -24,6 +30,19 @@ class Export
foreach ($this->content as $feed) { foreach ($this->content as $feed) {
$valid = true;
foreach ($this->required_fields as $field) {
if (! isset($feed[$field])) {
$valid = false;
break;
}
}
if (! $valid) continue;
$outline = $body->addChild('outline'); $outline = $body->addChild('outline');
$outline->addAttribute('xmlUrl', $feed['feed_url']); $outline->addAttribute('xmlUrl', $feed['feed_url']);
$outline->addAttribute('htmlUrl', $feed['site_url']); $outline->addAttribute('htmlUrl', $feed['site_url']);

View File

@ -16,7 +16,7 @@ class Reader
} }
public function download($url) public function download($url, $timeout = 5, $user_agent = 'PicoFeed (https://github.com/fguillot/picoFeed)')
{ {
if (strpos($url, 'http') !== 0) { if (strpos($url, 'http') !== 0) {
@ -24,12 +24,35 @@ class Reader
} }
$this->url = $url; $this->url = $url;
$this->content = @file_get_contents($this->url); $this->content = $this->fetchRemoteFile($url, $timeout, $user_agent);
return $this; return $this;
} }
public function fetchRemoteFile($url, $timeout, $user_agent)
{
if (! \function_exists('curl_init')) {
return @file_get_contents($this->url);
}
$ch = \curl_init();
\curl_setopt($ch, CURLOPT_URL, $url);
\curl_setopt($ch, CURLOPT_HEADER, false);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
\curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
$content = \curl_exec($ch);
\curl_close($ch);
return $content;
}
public function getContent() public function getContent()
{ {
return $this->content; return $this->content;