From 5469dd00db002002c00744b39e6d4d9b48d3c936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 23 Sep 2013 19:22:13 -0400 Subject: [PATCH] Add proxy support --- README.markdown | 4 ++++ common.php | 9 +++++++-- model.php | 1 - vendor/PicoFeed/Client.php | 14 ++++++++++++++ vendor/PicoFeed/Clients/Curl.php | 12 ++++++++++++ vendor/PicoFeed/Clients/Stream.php | 10 ++++++++++ vendor/PicoTools/Helper.php | 2 +- 7 files changed, 48 insertions(+), 4 deletions(-) diff --git a/README.markdown b/README.markdown index 4399e6f..4053106 100644 --- a/README.markdown +++ b/README.markdown @@ -184,6 +184,10 @@ Actually, the following constants can be overrided: - `DEBUG_FILENAME` => default is `data/debug.log` - `THEME_DIRECTORY` => default is themes - `SESSION_SAVE_PATH` => default is empty (used to store session files in a custom directory) +- `PROXY_HOSTNAME` => default is empty (make HTTP requests through a HTTP proxy if set) +- `PROXY_PORT` => default is 3128 (default port of Squid) +- `PROXY_USERNAME` => default is empty (set the proxy username is needed) +- `PROXY_PASSWORD` => default is empty ### How to change the session save path? diff --git a/common.php b/common.php index 78289f5..28b3299 100644 --- a/common.php +++ b/common.php @@ -6,6 +6,7 @@ require 'vendor/PicoTools/Dependency_Injection.php'; require 'vendor/PicoTools/Translator.php'; require 'vendor/PicoDb/Database.php'; require 'vendor/PicoDb/Table.php'; +require 'vendor/PicoFeed/Client.php'; require 'schema.php'; require 'model.php'; @@ -18,6 +19,12 @@ defined('DEBUG') or define('DEBUG', true); defined('DEBUG_FILENAME') or define('DEBUG_FILENAME', 'data/debug.log'); defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes'); defined('SESSION_SAVE_PATH') or define('SESSION_SAVE_PATH', ''); +defined('PROXY_HOSTNAME') or define('PROXY_HOSTNAME', ''); +defined('PROXY_PORT') or define('PROXY_PORT', 3128); +defined('PROXY_USERNAME') or define('PROXY_USERNAME', ''); +defined('PROXY_PASSWORD') or define('PROXY_PASSWORD', ''); + +PicoFeed\Client::proxy(PROXY_HOSTNAME, PROXY_PORT, PROXY_USERNAME, PROXY_PASSWORD); PicoTools\container('db', function() { @@ -27,11 +34,9 @@ PicoTools\container('db', function() { )); if ($db->schema()->check(Model\DB_VERSION)) { - return $db; } else { - die('Unable to migrate database schema.'); } }); diff --git a/model.php b/model.php index 37a92ec..5cf9594 100644 --- a/model.php +++ b/model.php @@ -3,7 +3,6 @@ namespace Model; require_once 'vendor/PicoFeed/Filter.php'; -require_once 'vendor/PicoFeed/Client.php'; require_once 'vendor/PicoFeed/Export.php'; require_once 'vendor/PicoFeed/Import.php'; require_once 'vendor/PicoFeed/Reader.php'; diff --git a/vendor/PicoFeed/Client.php b/vendor/PicoFeed/Client.php index e832422..816d22a 100644 --- a/vendor/PicoFeed/Client.php +++ b/vendor/PicoFeed/Client.php @@ -6,6 +6,11 @@ require_once __DIR__.'/Logging.php'; abstract class Client { + protected static $proxy_hostname = null; + protected static $proxy_port = null; + protected static $proxy_username = null; + protected static $proxy_password = null; + public $etag = ''; public $last_modified = ''; public $is_modified = true; @@ -97,6 +102,15 @@ abstract class Client } + public static function proxy($hostname, $port = 3128, $username = '', $password = '') + { + self::$proxy_hostname = $hostname; + self::$proxy_port = $port; + self::$proxy_username = $username; + self::$proxy_password = $password; + } + + public function setLastModified($last_modified) { $this->last_modified = $last_modified; diff --git a/vendor/PicoFeed/Clients/Curl.php b/vendor/PicoFeed/Clients/Curl.php index 103fcde..0ccf164 100644 --- a/vendor/PicoFeed/Clients/Curl.php +++ b/vendor/PicoFeed/Clients/Curl.php @@ -66,6 +66,18 @@ class Curl extends \PicoFeed\Client curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders')); curl_setopt($ch, CURLOPT_COOKIEJAR, 'php://memory'); curl_setopt($ch, CURLOPT_COOKIEFILE, 'php://memory'); + + if (parent::$proxy_hostname) { + + curl_setopt($ch, CURLOPT_PROXYPORT, parent::$proxy_port); + curl_setopt($ch, CURLOPT_PROXYTYPE, 'HTTP'); + curl_setopt($ch, CURLOPT_PROXY, parent::$proxy_hostname); + + if (parent::$proxy_username) { + curl_setopt($ch, CURLOPT_PROXYUSERPWD, parent::$proxy_username.':'.parent::$proxy_password); + } + } + curl_exec($ch); Logging::log(\get_called_class().' cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME)); diff --git a/vendor/PicoFeed/Clients/Stream.php b/vendor/PicoFeed/Clients/Stream.php index de54d4f..1a6b241 100644 --- a/vendor/PicoFeed/Clients/Stream.php +++ b/vendor/PicoFeed/Clients/Stream.php @@ -28,6 +28,16 @@ class Stream extends \PicoFeed\Client ) ); + if (parent::$proxy_hostname) { + $context_options['http']['proxy'] = 'tcp://'.parent::$proxy_hostname.':'.parent::$proxy_port; + $context_options['http']['request_fulluri'] = true; + + if (parent::$proxy_username) { + $headers[] = 'Proxy-Authorization: Basic '.base64_encode(parent::$proxy_username.':'.parent::$proxy_password); + $context_options['http']['header'] = implode("\r\n", $headers); + } + } + $context = stream_context_create($context_options); // Make HTTP request diff --git a/vendor/PicoTools/Helper.php b/vendor/PicoTools/Helper.php index daa6f66..1815f58 100644 --- a/vendor/PicoTools/Helper.php +++ b/vendor/PicoTools/Helper.php @@ -7,7 +7,7 @@ function get_current_base_url() $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; $url .= $_SERVER['SERVER_NAME']; $url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; - $url .= dirname($_SERVER['PHP_SELF']).'/'; + $url .= dirname($_SERVER['PHP_SELF']) !== '/' ? dirname($_SERVER['PHP_SELF']).'/' : '/'; return $url; }