make it possible to subscribe to password-protected feeds (closes #314)

This commit is contained in:
Andrew Dolgov 2011-03-17 19:05:28 +03:00
parent dce0dc739f
commit 8d505d78e3
1 changed files with 683 additions and 661 deletions

View File

@ -370,7 +370,10 @@
} }
} }
function fetch_file_contents($url, $type = false) { function fetch_file_contents($url, $type = false, $login = false, $pass = false) {
$login = urlencode($login);
$pass = urlencode($pass);
if (USE_CURL) { if (USE_CURL) {
$ch = curl_init($url); $ch = curl_init($url);
@ -380,6 +383,11 @@
curl_setopt($ch, CURLOPT_MAXREDIRS, 20); curl_setopt($ch, CURLOPT_MAXREDIRS, 20);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($fp, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
if ($login && $pass)
curl_setopt($ch, CURLOPT_USERPWD, "$login:$pass");
$contents = @curl_exec($ch); $contents = @curl_exec($ch);
if ($contents === false) { if ($contents === false) {
@ -387,15 +395,26 @@
return false; return false;
} }
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $content_type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch); curl_close($ch);
if ($type && strpos($content_type, "$type") === false) { if ($http_code != 200 || $type && strpos($content_type, "$type") === false) {
return false; return false;
} }
return $contents; return $contents;
} else { } else {
if ($login && $pass && $updated != 3) {
$url_parts = array();
preg_match("/(^[^:]*):\/\/(.*)/", $url, $url_parts);
if ($url_parts[1] && $url_parts[2]) {
$url = $url_parts[1] . "://$login:$pass@" . $url_parts[2];
}
}
return @file_get_contents($url); return @file_get_contents($url);
} }
@ -2935,10 +2954,10 @@
$has_oauth = db_fetch_result($result, 0, 'twitter_oauth'); $has_oauth = db_fetch_result($result, 0, 'twitter_oauth');
if (!$has_oauth || strpos($url, '://api.twitter.com') === false) { if (!$has_oauth || strpos($url, '://api.twitter.com') === false) {
if (!fetch_file_contents($url)) return 5; if (!fetch_file_contents($url, false, $auth_login, $auth_pass)) return 5;
if (url_is_html($url)) { if (url_is_html($url, $auth_login, $auth_pass)) {
$feedUrls = get_feeds_from_html($url); $feedUrls = get_feeds_from_html($url, $auth_login, $auth_pass);
if (count($feedUrls) == 0) { if (count($feedUrls) == 0) {
return 3; return 3;
} else if (count($feedUrls) > 1) { } else if (count($feedUrls) > 1) {
@ -6759,15 +6778,17 @@
* *
* @return array Array of feeds. Key is the full URL, value the title * @return array Array of feeds. Key is the full URL, value the title
*/ */
function get_feeds_from_html($url) function get_feeds_from_html($url, $login = false, $pass = false)
{ {
$url = fix_url($url); $url = fix_url($url);
$baseUrl = substr($url, 0, strrpos($url, '/') + 1); $baseUrl = substr($url, 0, strrpos($url, '/') + 1);
libxml_use_internal_errors(true); libxml_use_internal_errors(true);
$content = @fetch_file_contents($url, false, $login, $pass);
$doc = new DOMDocument(); $doc = new DOMDocument();
$doc->loadHTMLFile($url); $doc->loadHTML($content);
$xpath = new DOMXPath($doc); $xpath = new DOMXPath($doc);
$entries = $xpath->query('/html/head/link[@rel="alternate"]'); $entries = $xpath->query('/html/head/link[@rel="alternate"]');
$feedUrls = array(); $feedUrls = array();
@ -6793,8 +6814,9 @@
* *
* @return boolean True if the URL contains HTML content * @return boolean True if the URL contains HTML content
*/ */
function url_is_html($url) { function url_is_html($url, $login = false, $pass = false) {
$content = substr(fetch_file_contents($url, false), 0, 1000); $content = substr(fetch_file_contents($url, false, $login, $pass), 0, 1000);
if (stripos($content, '<html>') === false if (stripos($content, '<html>') === false
&& stripos($content, '<html ') === false && stripos($content, '<html ') === false
) { ) {