|
1 <?php |
|
2 /** |
|
3 * HTTP Proxy connection interface |
|
4 * |
|
5 * @package Requests |
|
6 * @subpackage Proxy |
|
7 * @since 1.6 |
|
8 */ |
|
9 |
|
10 /** |
|
11 * HTTP Proxy connection interface |
|
12 * |
|
13 * Provides a handler for connection via an HTTP proxy |
|
14 * |
|
15 * @package Requests |
|
16 * @subpackage Proxy |
|
17 * @since 1.6 |
|
18 */ |
|
19 class Requests_Proxy_HTTP implements Requests_Proxy { |
|
20 /** |
|
21 * Proxy host and port |
|
22 * |
|
23 * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128) |
|
24 * |
|
25 * @var string |
|
26 */ |
|
27 public $proxy; |
|
28 |
|
29 /** |
|
30 * Username |
|
31 * |
|
32 * @var string |
|
33 */ |
|
34 public $user; |
|
35 |
|
36 /** |
|
37 * Password |
|
38 * |
|
39 * @var string |
|
40 */ |
|
41 public $pass; |
|
42 |
|
43 /** |
|
44 * Do we need to authenticate? (ie username & password have been provided) |
|
45 * |
|
46 * @var boolean |
|
47 */ |
|
48 public $use_authentication; |
|
49 |
|
50 /** |
|
51 * Constructor |
|
52 * |
|
53 * @since 1.6 |
|
54 * @throws Requests_Exception On incorrect number of arguments (`authbasicbadargs`) |
|
55 * @param array|null $args Array of user and password. Must have exactly two elements |
|
56 */ |
|
57 public function __construct($args = null) { |
|
58 if (is_string($args)) { |
|
59 $this->proxy = $args; |
|
60 } |
|
61 elseif (is_array($args)) { |
|
62 if (count($args) == 1) { |
|
63 list($this->proxy) = $args; |
|
64 } |
|
65 elseif (count($args) == 3) { |
|
66 list($this->proxy, $this->user, $this->pass) = $args; |
|
67 $this->use_authentication = true; |
|
68 } |
|
69 else { |
|
70 throw new Requests_Exception('Invalid number of arguments', 'proxyhttpbadargs'); |
|
71 } |
|
72 } |
|
73 } |
|
74 |
|
75 /** |
|
76 * Register the necessary callbacks |
|
77 * |
|
78 * @since 1.6 |
|
79 * @see curl_before_send |
|
80 * @see fsockopen_remote_socket |
|
81 * @see fsockopen_remote_host_path |
|
82 * @see fsockopen_header |
|
83 * @param Requests_Hooks $hooks Hook system |
|
84 */ |
|
85 public function register(Requests_Hooks &$hooks) { |
|
86 $hooks->register('curl.before_send', array(&$this, 'curl_before_send')); |
|
87 |
|
88 $hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket')); |
|
89 $hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path')); |
|
90 if ($this->use_authentication) { |
|
91 $hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header')); |
|
92 } |
|
93 } |
|
94 |
|
95 /** |
|
96 * Set cURL parameters before the data is sent |
|
97 * |
|
98 * @since 1.6 |
|
99 * @param resource $handle cURL resource |
|
100 */ |
|
101 public function curl_before_send(&$handle) { |
|
102 curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); |
|
103 curl_setopt($handle, CURLOPT_PROXY, $this->proxy); |
|
104 |
|
105 if ($this->use_authentication) { |
|
106 curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY); |
|
107 curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string()); |
|
108 } |
|
109 } |
|
110 |
|
111 /** |
|
112 * Alter remote socket information before opening socket connection |
|
113 * |
|
114 * @since 1.6 |
|
115 * @param string $remote_socket Socket connection string |
|
116 */ |
|
117 public function fsockopen_remote_socket(&$remote_socket) { |
|
118 $remote_socket = $this->proxy; |
|
119 } |
|
120 |
|
121 /** |
|
122 * Alter remote path before getting stream data |
|
123 * |
|
124 * @since 1.6 |
|
125 * @param string $path Path to send in HTTP request string ("GET ...") |
|
126 * @param string $url Full URL we're requesting |
|
127 */ |
|
128 public function fsockopen_remote_host_path(&$path, $url) { |
|
129 $path = $url; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Add extra headers to the request before sending |
|
134 * |
|
135 * @since 1.6 |
|
136 * @param string $out HTTP header string |
|
137 */ |
|
138 public function fsockopen_header(&$out) { |
|
139 $out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string())); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Get the authentication string (user:pass) |
|
144 * |
|
145 * @since 1.6 |
|
146 * @return string |
|
147 */ |
|
148 public function get_auth_string() { |
|
149 return $this->user . ':' . $this->pass; |
|
150 } |
|
151 } |