Completed
Pull Request — master (#271)
by
unknown
03:48
created

HTTP::fsockopen_header()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Rmccue\Requests\Proxy;
3
4
use Rmccue\Requests\Proxy as Proxy;
5
use Rmccue\Requests\Hooks as Hooks;
6
use Rmccue\Requests\Exception as Exception;
7
8
/**
9
 * HTTP Proxy connection interface
10
 *
11
 * @package Rmccue\Requests
12
 * @subpackage Proxy
13
 * @since 1.6
14
 */
15
16
/**
17
 * HTTP Proxy connection interface
18
 *
19
 * Provides a handler for connection via an HTTP proxy
20
 *
21
 * @package Rmccue\Requests
22
 * @subpackage Proxy
23
 * @since 1.6
24
 */
25
class HTTP implements Proxy {
26
	/**
27
	 * Proxy host and port
28
	 *
29
	 * Notation: "host:port" (eg 127.0.0.1:8080 or someproxy.com:3128)
30
	 *
31
	 * @var string
32
	 */
33
	public $proxy;
34
35
	/**
36
	 * Username
37
	 *
38
	 * @var string
39
	 */
40
	public $user;
41
42
	/**
43
	 * Password
44
	 *
45
	 * @var string
46
	 */
47
	public $pass;
48
49
	/**
50
	 * Do we need to authenticate? (ie username & password have been provided)
51
	 *
52
	 * @var boolean
53
	 */
54
	public $use_authentication;
55
56
	/**
57
	 * Constructor
58
	 *
59
	 * @since 1.6
60
	 * @throws Rmccue\Requests\Exception On incorrect number of arguments (`authbasicbadargs`)
61
	 * @param array|null $args Array of user and password. Must have exactly two elements
62
	 */
63
	public function __construct($args = null) {
64
		if (is_string($args)) {
65
			$this->proxy = $args;
66
		}
67
		elseif (is_array($args)) {
68
			if (count($args) == 1) {
69
				list($this->proxy) = $args;
70
			}
71
			elseif (count($args) == 3) {
72
				list($this->proxy, $this->user, $this->pass) = $args;
73
				$this->use_authentication = true;
74
			}
75
			else {
76
				throw new Exception('Invalid number of arguments', 'proxyhttpbadargs');
77
			}
78
		}
79
	}
80
81
	/**
82
	 * Register the necessary callbacks
83
	 *
84
	 * @since 1.6
85
	 * @see curl_before_send
86
	 * @see fsockopen_remote_socket
87
	 * @see fsockopen_remote_host_path
88
	 * @see fsockopen_header
89
	 * @param Rmccue\Requests\Hooks $hooks Hook system
0 ignored issues
show
Documentation introduced by
Should the type for parameter $hooks not be Hooks?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
90
	 */
91
	public function register(Hooks &$hooks) {
92
		$hooks->register('curl.before_send', array(&$this, 'curl_before_send'));
93
94
		$hooks->register('fsockopen.remote_socket', array(&$this, 'fsockopen_remote_socket'));
95
		$hooks->register('fsockopen.remote_host_path', array(&$this, 'fsockopen_remote_host_path'));
96
		if ($this->use_authentication) {
97
			$hooks->register('fsockopen.after_headers', array(&$this, 'fsockopen_header'));
98
		}
99
	}
100
101
	/**
102
	 * Set cURL parameters before the data is sent
103
	 *
104
	 * @since 1.6
105
	 * @param resource $handle cURL resource
106
	 */
107
	public function curl_before_send(&$handle) {
108
		curl_setopt($handle, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
109
		curl_setopt($handle, CURLOPT_PROXY, $this->proxy);
110
111
		if ($this->use_authentication) {
112
			curl_setopt($handle, CURLOPT_PROXYAUTH, CURLAUTH_ANY);
113
			curl_setopt($handle, CURLOPT_PROXYUSERPWD, $this->get_auth_string());
114
		}
115
	}
116
117
	/**
118
	 * Alter remote socket information before opening socket connection
119
	 *
120
	 * @since 1.6
121
	 * @param string $remote_socket Socket connection string
122
	 */
123
	public function fsockopen_remote_socket(&$remote_socket) {
124
		$remote_socket = $this->proxy;
125
	}
126
127
	/**
128
	 * Alter remote path before getting stream data
129
	 *
130
	 * @since 1.6
131
	 * @param string $path Path to send in HTTP request string ("GET ...")
132
	 * @param string $url Full URL we're requesting
133
	 */
134
	public function fsockopen_remote_host_path(&$path, $url) {
135
		$path = $url;
136
	}
137
138
	/**
139
	 * Add extra headers to the request before sending
140
	 *
141
	 * @since 1.6
142
	 * @param string $out HTTP header string
143
	 */
144
	public function fsockopen_header(&$out) {
145
		$out .= sprintf("Proxy-Authorization: Basic %s\r\n", base64_encode($this->get_auth_string()));
146
	}
147
148
	/**
149
	 * Get the authentication string (user:pass)
150
	 *
151
	 * @since 1.6
152
	 * @return string
153
	 */
154
	public function get_auth_string() {
155
		return $this->user . ':' . $this->pass;
156
	}
157
}