Skip to content

Instantly share code, notes, and snippets.

@rmccue
Created September 30, 2009 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccue/197903 to your computer and use it in GitHub Desktop.
Save rmccue/197903 to your computer and use it in GitHub Desktop.
From 253278458860aadf0d25b11351357bcf90c6db7a Mon Sep 17 00:00:00 2001
From: Ryan McCue <me+gist197903@ryanmccue.info>
Date: Tue, 29 Sep 2009 19:41:53 +1000
Subject: [PATCH] Add proxy support. Closes #19
---
simplepie.inc | 78 +++++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 60 insertions(+), 18 deletions(-)
diff --git a/simplepie.inc b/simplepie.inc
index abb938a..5699961 100644
--- a/simplepie.inc
+++ b/simplepie.inc
@@ -712,6 +712,13 @@ class SimplePie
var $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style');
/**
+ * @var string Stores the proxy used to fetch feeds
+ * @see SimplePie::set_proxy()
+ * @access private
+ */
+ var $proxy = '';
+
+ /**
* The SimplePie class contains feed level data and options
*
* There are two ways that you can create a new SimplePie object. The first
@@ -836,6 +843,18 @@ class SimplePie
}
/**
+ * Set proxy for SimplePie_File to use
+ *
+ * @access public
+ * @param string $proxy Proxy URL in the form: host:port
+ */
+ function set_proxy($proxy)
+ {
+ $this->proxy = $proxy;
+ $this->sanitize->proxy = $proxy;
+ }
+
+ /**
* Provides an instance of SimplePie_File to use as a feed
*
* @access public
@@ -1599,7 +1618,7 @@ class SimplePie
{
$headers['if-none-match'] = '"' . $this->data['headers']['etag'] . '"';
}
- $file =& new $this->file_class($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen);
+ $file =& new $this->file_class($this->feed_url, $this->timeout/10, 5, $headers, $this->useragent, $this->force_fsockopen, $this->proxy);
if ($file->success)
{
if ($file->status_code === 304)
@@ -1640,7 +1659,7 @@ class SimplePie
}
else
{
- $file =& new $this->file_class($this->feed_url, $this->timeout, 5, null, $this->useragent, $this->force_fsockopen);
+ $file =& new $this->file_class($this->feed_url, $this->timeout, 5, null, $this->useragent, $this->force_fsockopen, $this->proxy);
}
}
// If the file connection has an error, set SimplePie::error to that and quit
@@ -1971,7 +1990,7 @@ class SimplePie
}
else
{
- $file =& new $this->file_class($favicon, $this->timeout / 10, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen);
+ $file =& new $this->file_class($favicon, $this->timeout / 10, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen, $this->proxy);
if ($file->success && ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)) && strlen($file->body) > 0)
{
@@ -7644,7 +7663,7 @@ class SimplePie_File
var $error;
var $method = SIMPLEPIE_FILE_SOURCE_NONE;
- function SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false)
+ function SimplePie_File($url, $timeout = 10, $redirects = 5, $headers = null, $useragent = null, $force_fsockopen = false, $proxy = '')
{
if (class_exists('idna_convert'))
{
@@ -7691,6 +7710,10 @@ class SimplePie_File
curl_setopt($fp, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($fp, CURLOPT_MAXREDIRS, $redirects);
}
+ if ($proxy)
+ {
+ curl_setopt($fp, CURLOPT_PROXY, $proxy);
+ }
$this->headers = curl_exec($fp);
if (curl_errno($fp) === 23 || curl_errno($fp) === 61)
@@ -7738,6 +7761,18 @@ class SimplePie_File
$url_parts['port'] = 80;
}
$fp = @fsockopen($url_parts['host'], $url_parts['port'], $errno, $errstr, $timeout);
+ if ($proxy) {
+ $proxy_parts =& new SimplePie_IRI($proxy);
+ $host = $proxy_parts->get_host();
+ $port = $proxy_parts->get_port();
+ if (empty($port)) {
+ $port = 80;
+ }
+ } else {
+ $host = $url_parts['host'];
+ $port = $url_parts['port'];
+ }
+ $fp = @fsockopen($host, $port, $errno, $errstr, $timeout);
if (!$fp)
{
$this->error = 'fsockopen error: ' . $errstr;
@@ -7746,21 +7781,25 @@ class SimplePie_File
else
{
stream_set_timeout($fp, $timeout);
- if (isset($url_parts['path']))
- {
- if (isset($url_parts['query']))
+ if ($proxy) {
+ $get = $url;
+ } else {
+ if (isset($url_parts['path']))
{
- $get = "$url_parts[path]?$url_parts[query]";
+ if (isset($url_parts['query']))
+ {
+ $get = "$url_parts[path]?$url_parts[query]";
+ }
+ else
+ {
+ $get = $url_parts['path'];
+ }
}
else
{
- $get = $url_parts['path'];
+ $get = '/';
}
}
- else
- {
- $get = '/';
- }
$out = "GET $get HTTP/1.0\r\n";
$out .= "Host: $url_parts[host]\r\n";
$out .= "User-Agent: $useragent\r\n";
@@ -14075,8 +14114,9 @@ class SimplePie_Locator
var $checked_feeds = 0;
var $max_checked_feeds = 10;
var $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer';
+ var $proxy;
- function SimplePie_Locator(&$file, $timeout = 10, $useragent = null, $file_class = 'SimplePie_File', $max_checked_feeds = 10, $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer')
+ function SimplePie_Locator(&$file, $timeout = 10, $useragent = null, $file_class = 'SimplePie_File', $max_checked_feeds = 10, $content_type_sniffer_class = 'SimplePie_Content_Type_Sniffer', $proxy = '')
{
$this->file =& $file;
$this->file_class = $file_class;
@@ -14084,6 +14124,7 @@ class SimplePie_Locator
$this->timeout = $timeout;
$this->max_checked_feeds = $max_checked_feeds;
$this->content_type_sniffer_class = $content_type_sniffer_class;
+ $this->proxy = $proxy;
}
function find($type = SIMPLEPIE_LOCATOR_ALL, &$working)
@@ -14205,7 +14246,7 @@ class SimplePie_Locator
if (!in_array($href, $done) && in_array('feed', $rel) || (in_array('alternate', $rel) && !empty($link['attribs']['type']['data']) && in_array(strtolower(SimplePie_Misc::parse_mime($link['attribs']['type']['data'])), array('application/rss+xml', 'application/atom+xml'))) && !isset($feeds[$href]))
{
$this->checked_feeds++;
- $feed =& new $this->file_class($href, $this->timeout, 5, null, $this->useragent);
+ $feed =& new $this->file_class($href, $this->timeout, 5, null, $this->useragent, false, $this->proxy);
if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
{
$feeds[$href] = $feed;
@@ -14277,7 +14318,7 @@ class SimplePie_Locator
if (in_array(strtolower(strrchr($value, '.')), array('.rss', '.rdf', '.atom', '.xml')))
{
$this->checked_feeds++;
- $feed =& new $this->file_class($value, $this->timeout, 5, null, $this->useragent);
+ $feed =& new $this->file_class($value, $this->timeout, 5, null, $this->useragent, false, $this->proxy);
if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
{
return $feed;
@@ -14302,7 +14343,7 @@ class SimplePie_Locator
if (preg_match('/(rss|rdf|atom|xml)/i', $value))
{
$this->checked_feeds++;
- $feed =& new $this->file_class($value, $this->timeout, 5, null, $this->useragent);
+ $feed =& new $this->file_class($value, $this->timeout, 5, null, $this->useragent, false, $this->proxy);
if ($feed->success && ($feed->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($feed->status_code === 200 || $feed->status_code > 206 && $feed->status_code < 300)) && $this->is_feed($feed))
{
return $feed;
@@ -14679,6 +14720,7 @@ class SimplePie_Sanitize
var $timeout = 10;
var $useragent = '';
var $force_fsockopen = false;
+ var $proxy = '';
var $replace_url_attributes = array(
'a' => 'href',
@@ -14912,7 +14954,7 @@ class SimplePie_Sanitize
}
else
{
- $file =& new $this->file_class($img['attribs']['src']['data'], $this->timeout, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen);
+ $file =& new $this->file_class($img['attribs']['src']['data'], $this->timeout, 5, array('X-FORWARDED-FOR' => $_SERVER['REMOTE_ADDR']), $this->useragent, $this->force_fsockopen, $this->proxy);
$headers = $file->headers;
if ($file->success && ($file->method & SIMPLEPIE_FILE_SOURCE_REMOTE === 0 || ($file->status_code === 200 || $file->status_code > 206 && $file->status_code < 300)))
--
1.6.1.9.g97c34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment