Add proxy support

This commit is contained in:
Frédéric Guillot 2013-09-23 19:22:13 -04:00
parent 1fc71c3a6d
commit 5469dd00db
7 changed files with 48 additions and 4 deletions

View File

@ -184,6 +184,10 @@ Actually, the following constants can be overrided:
- `DEBUG_FILENAME` => default is `data/debug.log` - `DEBUG_FILENAME` => default is `data/debug.log`
- `THEME_DIRECTORY` => default is themes - `THEME_DIRECTORY` => default is themes
- `SESSION_SAVE_PATH` => default is empty (used to store session files in a custom directory) - `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? ### How to change the session save path?

View File

@ -6,6 +6,7 @@ require 'vendor/PicoTools/Dependency_Injection.php';
require 'vendor/PicoTools/Translator.php'; require 'vendor/PicoTools/Translator.php';
require 'vendor/PicoDb/Database.php'; require 'vendor/PicoDb/Database.php';
require 'vendor/PicoDb/Table.php'; require 'vendor/PicoDb/Table.php';
require 'vendor/PicoFeed/Client.php';
require 'schema.php'; require 'schema.php';
require 'model.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('DEBUG_FILENAME') or define('DEBUG_FILENAME', 'data/debug.log');
defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes'); defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes');
defined('SESSION_SAVE_PATH') or define('SESSION_SAVE_PATH', ''); 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() { PicoTools\container('db', function() {
@ -27,11 +34,9 @@ PicoTools\container('db', function() {
)); ));
if ($db->schema()->check(Model\DB_VERSION)) { if ($db->schema()->check(Model\DB_VERSION)) {
return $db; return $db;
} }
else { else {
die('Unable to migrate database schema.'); die('Unable to migrate database schema.');
} }
}); });

View File

@ -3,7 +3,6 @@
namespace Model; namespace Model;
require_once 'vendor/PicoFeed/Filter.php'; require_once 'vendor/PicoFeed/Filter.php';
require_once 'vendor/PicoFeed/Client.php';
require_once 'vendor/PicoFeed/Export.php'; require_once 'vendor/PicoFeed/Export.php';
require_once 'vendor/PicoFeed/Import.php'; require_once 'vendor/PicoFeed/Import.php';
require_once 'vendor/PicoFeed/Reader.php'; require_once 'vendor/PicoFeed/Reader.php';

View File

@ -6,6 +6,11 @@ require_once __DIR__.'/Logging.php';
abstract class Client 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 $etag = '';
public $last_modified = ''; public $last_modified = '';
public $is_modified = true; 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) public function setLastModified($last_modified)
{ {
$this->last_modified = $last_modified; $this->last_modified = $last_modified;

View File

@ -66,6 +66,18 @@ class Curl extends \PicoFeed\Client
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders')); curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));
curl_setopt($ch, CURLOPT_COOKIEJAR, 'php://memory'); curl_setopt($ch, CURLOPT_COOKIEJAR, 'php://memory');
curl_setopt($ch, CURLOPT_COOKIEFILE, '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); curl_exec($ch);
Logging::log(\get_called_class().' cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME)); Logging::log(\get_called_class().' cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME));

View File

@ -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); $context = stream_context_create($context_options);
// Make HTTP request // Make HTTP request

View File

@ -7,7 +7,7 @@ function get_current_base_url()
$url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://'; $url = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url .= $_SERVER['SERVER_NAME']; $url .= $_SERVER['SERVER_NAME'];
$url .= $_SERVER['SERVER_PORT'] == 80 || $_SERVER['SERVER_PORT'] == 443 ? '' : ':'.$_SERVER['SERVER_PORT']; $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; return $url;
} }