Requests_Proxy_HTTP::fsockopen_header()   A
last analyzed

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
/**
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 {
0 ignored issues
show
Coding Style introduced by
The property $use_authentication is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
Coding Style introduced by
This class is not in CamelCase format.

Classes in PHP are usually named in CamelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.

Thus the name database provider becomes DatabaseProvider.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 20 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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) {
0 ignored issues
show
Coding Style Naming introduced by
The parameter $remote_socket is not named in camelCase.

This check marks parameter names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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() {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
149
		return $this->user . ':' . $this->pass;
150
	}
151
}