@@ -48,8 +48,8 @@ discard block |
||
48 | 48 | 'header' => BASE.'templates/'.$template.'/header.tpl', |
49 | 49 | 'event_js' => BASE.'functions/event.js', |
50 | 50 | 'footer' => BASE.'templates/'.$template.'/footer.tpl', |
51 | - 'sidebar' => BASE.'templates/'.$template.'/sidebar_year.tpl', |
|
52 | - 'search_box' => BASE.'templates/'.$template.'/search_box.tpl' |
|
51 | + 'sidebar' => BASE.'templates/'.$template.'/sidebar_year.tpl', |
|
52 | + 'search_box' => BASE.'templates/'.$template.'/search_box.tpl' |
|
53 | 53 | )); |
54 | 54 | |
55 | 55 | $page->replace_tags(array( |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | 'default_path' => $phpiCal_config->default_path, |
62 | 62 | 'cal' => $cal, |
63 | 63 | 'getcpath' => "&cpath=$cpath", |
64 | - 'cpath' => $cpath, |
|
64 | + 'cpath' => $cpath, |
|
65 | 65 | 'getdate' => $getdate, |
66 | 66 | 'calendar_name' => $cal_displayname, |
67 | 67 | 'display_date' => $this_year, |
@@ -66,8 +66,8 @@ |
||
66 | 66 | 'header' => BASE.'templates/'.$phpiCal_config->template.'/header.tpl', |
67 | 67 | 'event_js' => BASE.'functions/event.js', |
68 | 68 | 'footer' => BASE.'templates/'.$phpiCal_config->template.'/footer.tpl', |
69 | - 'calendar_nav' => BASE.'templates/'.$phpiCal_config->template.'/calendar_nav.tpl', |
|
70 | - 'search_box' => BASE.'templates/'.$phpiCal_config->template.'/search_box.tpl' |
|
69 | + 'calendar_nav' => BASE.'templates/'.$phpiCal_config->template.'/calendar_nav.tpl', |
|
70 | + 'search_box' => BASE.'templates/'.$phpiCal_config->template.'/search_box.tpl' |
|
71 | 71 | )); |
72 | 72 | |
73 | 73 | $page->replace_tags(array( |
@@ -58,8 +58,8 @@ discard block |
||
58 | 58 | 'header' => BASE.'templates/'.$phpiCal_config->template.'/header.tpl', |
59 | 59 | 'event_js' => BASE.'functions/event.js', |
60 | 60 | 'footer' => BASE.'templates/'.$phpiCal_config->template.'/footer.tpl', |
61 | - 'sidebar' => BASE.'templates/'.$phpiCal_config->template.'/sidebar.tpl', |
|
62 | - 'search_box' => BASE.'templates/'.$phpiCal_config->template.'/search_box.tpl' |
|
61 | + 'sidebar' => BASE.'templates/'.$phpiCal_config->template.'/sidebar.tpl', |
|
62 | + 'search_box' => BASE.'templates/'.$phpiCal_config->template.'/search_box.tpl' |
|
63 | 63 | )); |
64 | 64 | |
65 | 65 | $page->replace_tags(array( |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | 'cal' => $cal, |
71 | 71 | 'getdate' => $getdate, |
72 | 72 | 'getcpath' => "&cpath=$cpath", |
73 | - 'cpath' => $cpath, |
|
73 | + 'cpath' => $cpath, |
|
74 | 74 | 'calendar_name' => $cal_displayname, |
75 | 75 | 'display_date' => $display_date, |
76 | 76 | 'current_view' => $current_view, |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | 'rss_powered' => $rss_powered, |
79 | 79 | 'rss_available' => '', |
80 | 80 | 'rss_valid' => '', |
81 | - 'show_search' => $phpiCal_config->show_search, |
|
81 | + 'show_search' => $phpiCal_config->show_search, |
|
82 | 82 | 'next_day' => $next_day, |
83 | 83 | 'next_week' => $next_week, |
84 | 84 | 'prev_day' => $prev_day, |
@@ -9,514 +9,514 @@ |
||
9 | 9 | */ |
10 | 10 | class Pest |
11 | 11 | { |
12 | - /** |
|
13 | - * @var array Default CURL options |
|
14 | - */ |
|
15 | - public $curl_opts = array( |
|
16 | - CURLOPT_RETURNTRANSFER => true, // return result instead of echoing |
|
17 | - CURLOPT_SSL_VERIFYPEER => false, // stop cURL from verifying the peer's certificate |
|
18 | - CURLOPT_FOLLOWLOCATION => false, // follow redirects, Location: headers |
|
19 | - CURLOPT_MAXREDIRS => 10, // but dont redirect more than 10 times |
|
20 | - CURLOPT_HTTPHEADER => array() |
|
21 | - ); |
|
22 | - |
|
23 | - /** |
|
24 | - * @var string Base URL |
|
25 | - */ |
|
26 | - public $base_url; |
|
27 | - |
|
28 | - /** |
|
29 | - * @var array Last response |
|
30 | - */ |
|
31 | - public $last_response; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var array Last request |
|
35 | - */ |
|
36 | - public $last_request; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var array Last headers |
|
40 | - */ |
|
41 | - public $last_headers; |
|
42 | - |
|
43 | - /** |
|
44 | - * @var bool Throw exceptions on HTTP error codes |
|
45 | - */ |
|
46 | - public $throw_exceptions = true; |
|
47 | - |
|
48 | - |
|
49 | - /** |
|
50 | - * Class constructor |
|
51 | - * @param string $base_url |
|
52 | - * @throws Exception |
|
53 | - */ |
|
54 | - public function __construct($base_url) |
|
55 | - { |
|
56 | - if (!function_exists('curl_init')) { |
|
57 | - throw new Exception('CURL module not available! Pest requires CURL. See http://php.net/manual/en/book.curl.php'); |
|
58 | - } |
|
59 | - |
|
60 | - /* |
|
12 | + /** |
|
13 | + * @var array Default CURL options |
|
14 | + */ |
|
15 | + public $curl_opts = array( |
|
16 | + CURLOPT_RETURNTRANSFER => true, // return result instead of echoing |
|
17 | + CURLOPT_SSL_VERIFYPEER => false, // stop cURL from verifying the peer's certificate |
|
18 | + CURLOPT_FOLLOWLOCATION => false, // follow redirects, Location: headers |
|
19 | + CURLOPT_MAXREDIRS => 10, // but dont redirect more than 10 times |
|
20 | + CURLOPT_HTTPHEADER => array() |
|
21 | + ); |
|
22 | + |
|
23 | + /** |
|
24 | + * @var string Base URL |
|
25 | + */ |
|
26 | + public $base_url; |
|
27 | + |
|
28 | + /** |
|
29 | + * @var array Last response |
|
30 | + */ |
|
31 | + public $last_response; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var array Last request |
|
35 | + */ |
|
36 | + public $last_request; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var array Last headers |
|
40 | + */ |
|
41 | + public $last_headers; |
|
42 | + |
|
43 | + /** |
|
44 | + * @var bool Throw exceptions on HTTP error codes |
|
45 | + */ |
|
46 | + public $throw_exceptions = true; |
|
47 | + |
|
48 | + |
|
49 | + /** |
|
50 | + * Class constructor |
|
51 | + * @param string $base_url |
|
52 | + * @throws Exception |
|
53 | + */ |
|
54 | + public function __construct($base_url) |
|
55 | + { |
|
56 | + if (!function_exists('curl_init')) { |
|
57 | + throw new Exception('CURL module not available! Pest requires CURL. See http://php.net/manual/en/book.curl.php'); |
|
58 | + } |
|
59 | + |
|
60 | + /* |
|
61 | 61 | * Only enable CURLOPT_FOLLOWLOCATION if safe_mode and open_base_dir are |
62 | 62 | * not in use |
63 | 63 | */ |
64 | - if (ini_get('open_basedir') == '' && strtolower(ini_get('safe_mode')) == 'off') { |
|
65 | - $this->curl_opts['CURLOPT_FOLLOWLOCATION'] = true; |
|
66 | - } |
|
67 | - |
|
68 | - $this->base_url = $base_url; |
|
69 | - |
|
70 | - // The callback to handle return headers |
|
71 | - // Using PHP 5.2, it cannot be initialised in the static context |
|
72 | - $this->curl_opts[CURLOPT_HEADERFUNCTION] = array($this, 'handle_header'); |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * Setup authentication |
|
77 | - * |
|
78 | - * @param string $user |
|
79 | - * @param string $pass |
|
80 | - * @param string $auth Can be 'basic' or 'digest' |
|
81 | - */ |
|
82 | - public function setupAuth($user, $pass, $auth = 'basic') |
|
83 | - { |
|
84 | - $this->curl_opts[CURLOPT_HTTPAUTH] = constant('CURLAUTH_' . strtoupper($auth)); |
|
85 | - $this->curl_opts[CURLOPT_USERPWD] = $user . ":" . $pass; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Setup proxy |
|
90 | - * @param string $host |
|
91 | - * @param int $port |
|
92 | - * @param string $user Optional. |
|
93 | - * @param string $pass Optional. |
|
94 | - */ |
|
95 | - public function setupProxy($host, $port, $user = NULL, $pass = NULL) |
|
96 | - { |
|
97 | - $this->curl_opts[CURLOPT_PROXYTYPE] = 'HTTP'; |
|
98 | - $this->curl_opts[CURLOPT_PROXY] = $host; |
|
99 | - $this->curl_opts[CURLOPT_PROXYPORT] = $port; |
|
100 | - if ($user && $pass) { |
|
101 | - $this->curl_opts[CURLOPT_PROXYUSERPWD] = $user . ":" . $pass; |
|
102 | - } |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Perform HTTP GET request |
|
107 | - * |
|
108 | - * @param string $url |
|
109 | - * @param array $data |
|
110 | - * @param array $headers |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function get($url, $data = array(), $headers=array()) |
|
114 | - { |
|
115 | - if (!empty($data)) { |
|
116 | - $pos = strpos($url, '?'); |
|
117 | - if ($pos !== false) { |
|
118 | - $url = substr($url, 0, $pos); |
|
119 | - } |
|
120 | - $url .= '?' . http_build_query($data); |
|
121 | - } |
|
122 | - |
|
123 | - $curl_opts = $this->curl_opts; |
|
64 | + if (ini_get('open_basedir') == '' && strtolower(ini_get('safe_mode')) == 'off') { |
|
65 | + $this->curl_opts['CURLOPT_FOLLOWLOCATION'] = true; |
|
66 | + } |
|
67 | + |
|
68 | + $this->base_url = $base_url; |
|
69 | + |
|
70 | + // The callback to handle return headers |
|
71 | + // Using PHP 5.2, it cannot be initialised in the static context |
|
72 | + $this->curl_opts[CURLOPT_HEADERFUNCTION] = array($this, 'handle_header'); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Setup authentication |
|
77 | + * |
|
78 | + * @param string $user |
|
79 | + * @param string $pass |
|
80 | + * @param string $auth Can be 'basic' or 'digest' |
|
81 | + */ |
|
82 | + public function setupAuth($user, $pass, $auth = 'basic') |
|
83 | + { |
|
84 | + $this->curl_opts[CURLOPT_HTTPAUTH] = constant('CURLAUTH_' . strtoupper($auth)); |
|
85 | + $this->curl_opts[CURLOPT_USERPWD] = $user . ":" . $pass; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Setup proxy |
|
90 | + * @param string $host |
|
91 | + * @param int $port |
|
92 | + * @param string $user Optional. |
|
93 | + * @param string $pass Optional. |
|
94 | + */ |
|
95 | + public function setupProxy($host, $port, $user = NULL, $pass = NULL) |
|
96 | + { |
|
97 | + $this->curl_opts[CURLOPT_PROXYTYPE] = 'HTTP'; |
|
98 | + $this->curl_opts[CURLOPT_PROXY] = $host; |
|
99 | + $this->curl_opts[CURLOPT_PROXYPORT] = $port; |
|
100 | + if ($user && $pass) { |
|
101 | + $this->curl_opts[CURLOPT_PROXYUSERPWD] = $user . ":" . $pass; |
|
102 | + } |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Perform HTTP GET request |
|
107 | + * |
|
108 | + * @param string $url |
|
109 | + * @param array $data |
|
110 | + * @param array $headers |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function get($url, $data = array(), $headers=array()) |
|
114 | + { |
|
115 | + if (!empty($data)) { |
|
116 | + $pos = strpos($url, '?'); |
|
117 | + if ($pos !== false) { |
|
118 | + $url = substr($url, 0, $pos); |
|
119 | + } |
|
120 | + $url .= '?' . http_build_query($data); |
|
121 | + } |
|
122 | + |
|
123 | + $curl_opts = $this->curl_opts; |
|
124 | 124 | |
125 | - $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
126 | - |
|
127 | - $curl = $this->prepRequest($curl_opts, $url); |
|
128 | - $body = $this->doRequest($curl); |
|
129 | - $body = $this->processBody($body); |
|
130 | - |
|
131 | - return $body; |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * Prepare request |
|
136 | - * |
|
137 | - * @param array $opts |
|
138 | - * @param string $url |
|
139 | - * @return resource |
|
140 | - * @throws Pest_Curl_Init |
|
141 | - */ |
|
142 | - protected function prepRequest($opts, $url) |
|
143 | - { |
|
144 | - if (strncmp($url, $this->base_url, strlen($this->base_url)) != 0) { |
|
145 | - $url = rtrim($this->base_url, '/') . '/' . ltrim($url, '/'); |
|
146 | - } |
|
147 | - |
|
148 | - $curl = curl_init($url); |
|
149 | - if ($curl === false) { |
|
150 | - throw new Pest_Curl_Init($this->processError(curl_error($curl), 'curl')); |
|
151 | - } |
|
152 | - |
|
153 | - foreach ($opts as $opt => $val) |
|
154 | - curl_setopt($curl, $opt, $val); |
|
155 | - |
|
156 | - $this->last_request = array( |
|
157 | - 'url' => $url |
|
158 | - ); |
|
159 | - |
|
160 | - if (isset($opts[CURLOPT_CUSTOMREQUEST])) |
|
161 | - $this->last_request['method'] = $opts[CURLOPT_CUSTOMREQUEST]; |
|
162 | - else |
|
163 | - $this->last_request['method'] = 'GET'; |
|
164 | - |
|
165 | - if (isset($opts[CURLOPT_POSTFIELDS])) |
|
166 | - $this->last_request['data'] = $opts[CURLOPT_POSTFIELDS]; |
|
167 | - |
|
168 | - return $curl; |
|
169 | - } |
|
125 | + $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
126 | + |
|
127 | + $curl = $this->prepRequest($curl_opts, $url); |
|
128 | + $body = $this->doRequest($curl); |
|
129 | + $body = $this->processBody($body); |
|
130 | + |
|
131 | + return $body; |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * Prepare request |
|
136 | + * |
|
137 | + * @param array $opts |
|
138 | + * @param string $url |
|
139 | + * @return resource |
|
140 | + * @throws Pest_Curl_Init |
|
141 | + */ |
|
142 | + protected function prepRequest($opts, $url) |
|
143 | + { |
|
144 | + if (strncmp($url, $this->base_url, strlen($this->base_url)) != 0) { |
|
145 | + $url = rtrim($this->base_url, '/') . '/' . ltrim($url, '/'); |
|
146 | + } |
|
147 | + |
|
148 | + $curl = curl_init($url); |
|
149 | + if ($curl === false) { |
|
150 | + throw new Pest_Curl_Init($this->processError(curl_error($curl), 'curl')); |
|
151 | + } |
|
152 | + |
|
153 | + foreach ($opts as $opt => $val) |
|
154 | + curl_setopt($curl, $opt, $val); |
|
155 | + |
|
156 | + $this->last_request = array( |
|
157 | + 'url' => $url |
|
158 | + ); |
|
159 | + |
|
160 | + if (isset($opts[CURLOPT_CUSTOMREQUEST])) |
|
161 | + $this->last_request['method'] = $opts[CURLOPT_CUSTOMREQUEST]; |
|
162 | + else |
|
163 | + $this->last_request['method'] = 'GET'; |
|
164 | + |
|
165 | + if (isset($opts[CURLOPT_POSTFIELDS])) |
|
166 | + $this->last_request['data'] = $opts[CURLOPT_POSTFIELDS]; |
|
167 | + |
|
168 | + return $curl; |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * Determines if a given array is numerically indexed or not |
|
173 | - * |
|
174 | - * @param array $array |
|
175 | - * @return boolean |
|
176 | - */ |
|
177 | - protected function _isNumericallyIndexedArray($array) |
|
178 | - { |
|
179 | - return !(bool)count(array_filter(array_keys($array), 'is_string')); |
|
180 | - } |
|
171 | + /** |
|
172 | + * Determines if a given array is numerically indexed or not |
|
173 | + * |
|
174 | + * @param array $array |
|
175 | + * @return boolean |
|
176 | + */ |
|
177 | + protected function _isNumericallyIndexedArray($array) |
|
178 | + { |
|
179 | + return !(bool)count(array_filter(array_keys($array), 'is_string')); |
|
180 | + } |
|
181 | 181 | |
182 | - /** |
|
183 | - * Flatten headers from an associative array to a numerically indexed array of "Name: Value" |
|
184 | - * style entries like CURLOPT_HTTPHEADER expects. Numerically indexed arrays are not modified. |
|
185 | - * |
|
186 | - * @param array $headers |
|
187 | - * @return array |
|
188 | - */ |
|
189 | - protected function prepHeaders($headers) |
|
190 | - { |
|
191 | - if ($this->_isNumericallyIndexedArray($headers)) { |
|
192 | - return $headers; |
|
193 | - } |
|
182 | + /** |
|
183 | + * Flatten headers from an associative array to a numerically indexed array of "Name: Value" |
|
184 | + * style entries like CURLOPT_HTTPHEADER expects. Numerically indexed arrays are not modified. |
|
185 | + * |
|
186 | + * @param array $headers |
|
187 | + * @return array |
|
188 | + */ |
|
189 | + protected function prepHeaders($headers) |
|
190 | + { |
|
191 | + if ($this->_isNumericallyIndexedArray($headers)) { |
|
192 | + return $headers; |
|
193 | + } |
|
194 | 194 | |
195 | - $flattened = array(); |
|
196 | - foreach ($headers as $name => $value) { |
|
197 | - $flattened[] = $name . ': ' . $value; |
|
198 | - } |
|
195 | + $flattened = array(); |
|
196 | + foreach ($headers as $name => $value) { |
|
197 | + $flattened[] = $name . ': ' . $value; |
|
198 | + } |
|
199 | 199 | |
200 | - return $flattened; |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Process error |
|
205 | - * @param string $body |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - protected function processError($body) |
|
209 | - { |
|
210 | - // Override this in classes that extend Pest. |
|
211 | - // The body of every erroneous (non-2xx/3xx) GET/POST/PUT/DELETE |
|
212 | - // response goes through here prior to being used as the 'message' |
|
213 | - // of the resulting Pest_Exception |
|
214 | - return $body; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Do CURL request |
|
219 | - * @param resource $curl |
|
220 | - * @return mixed |
|
221 | - * @throws Pest_Curl_Exec |
|
222 | - * @throws Pest_Curl_Meta |
|
223 | - */ |
|
224 | - private function doRequest($curl) |
|
225 | - { |
|
226 | - $this->last_headers = array(); |
|
227 | - $this->last_response = array(); |
|
228 | - |
|
229 | - // curl_error() needs to be tested right after function failure |
|
230 | - $this->last_response["body"] = curl_exec($curl); |
|
231 | - if ($this->last_response["body"] === false && $this->throw_exceptions) { |
|
232 | - throw new Pest_Curl_Exec(curl_error($curl)); |
|
233 | - } |
|
234 | - |
|
235 | - $this->last_response["meta"] = curl_getinfo($curl); |
|
236 | - if ($this->last_response["meta"] === false && $this->throw_exceptions) { |
|
237 | - throw new Pest_Curl_Meta(curl_error($curl)); |
|
238 | - } |
|
239 | - |
|
240 | - curl_close($curl); |
|
241 | - |
|
242 | - $this->checkLastResponseForError(); |
|
243 | - |
|
244 | - return $this->last_response["body"]; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Check last response for error |
|
249 | - * |
|
250 | - * @throws Pest_Conflict |
|
251 | - * @throws Pest_Gone |
|
252 | - * @throws Pest_Unauthorized |
|
253 | - * @throws Pest_ClientError |
|
254 | - * @throws Pest_MethodNotAllowed |
|
255 | - * @throws Pest_NotFound |
|
256 | - * @throws Pest_BadRequest |
|
257 | - * @throws Pest_UnknownResponse |
|
258 | - * @throws Pest_InvalidRecord |
|
259 | - * @throws Pest_ServerError |
|
260 | - * @throws Pest_Forbidden |
|
261 | - */ |
|
262 | - protected function checkLastResponseForError() |
|
263 | - { |
|
264 | - if (!$this->throw_exceptions) |
|
265 | - return; |
|
266 | - |
|
267 | - $meta = $this->last_response['meta']; |
|
268 | - $body = $this->last_response['body']; |
|
269 | - |
|
270 | - if ($meta === false) |
|
271 | - return; |
|
272 | - |
|
273 | - $err = null; |
|
274 | - switch ($meta['http_code']) { |
|
275 | - case 400: |
|
276 | - throw new Pest_BadRequest($this->processError($body)); |
|
277 | - break; |
|
278 | - case 401: |
|
279 | - throw new Pest_Unauthorized($this->processError($body)); |
|
280 | - break; |
|
281 | - case 403: |
|
282 | - throw new Pest_Forbidden($this->processError($body)); |
|
283 | - break; |
|
284 | - case 404: |
|
285 | - throw new Pest_NotFound($this->processError($body)); |
|
286 | - break; |
|
287 | - case 405: |
|
288 | - throw new Pest_MethodNotAllowed($this->processError($body)); |
|
289 | - break; |
|
290 | - case 409: |
|
291 | - throw new Pest_Conflict($this->processError($body)); |
|
292 | - break; |
|
293 | - case 410: |
|
294 | - throw new Pest_Gone($this->processError($body)); |
|
295 | - break; |
|
296 | - case 422: |
|
297 | - // Unprocessable Entity -- see http://www.iana.org/assignments/http-status-codes |
|
298 | - // This is now commonly used (in Rails, at least) to indicate |
|
299 | - // a response to a request that is syntactically correct, |
|
300 | - // but semantically invalid (for example, when trying to |
|
301 | - // create a resource with some required fields missing) |
|
302 | - throw new Pest_InvalidRecord($this->processError($body)); |
|
303 | - break; |
|
304 | - default: |
|
305 | - if ($meta['http_code'] >= 400 && $meta['http_code'] <= 499) |
|
306 | - throw new Pest_ClientError($this->processError($body)); |
|
307 | - elseif ($meta['http_code'] >= 500 && $meta['http_code'] <= 599) |
|
308 | - throw new Pest_ServerError($this->processError($body)); elseif (!isset($meta['http_code']) || $meta['http_code'] >= 600) { |
|
309 | - throw new Pest_UnknownResponse($this->processError($body)); |
|
310 | - } |
|
311 | - } |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Process body |
|
316 | - * @param string $body |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - protected function processBody($body) |
|
320 | - { |
|
321 | - // Override this in classes that extend Pest. |
|
322 | - // The body of every GET/POST/PUT/DELETE response goes through |
|
323 | - // here prior to being returned. |
|
324 | - return $body; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Perform HTTP HEAD request |
|
329 | - * @param string $url |
|
330 | - * @return string |
|
331 | - */ |
|
332 | - public function head($url) |
|
333 | - { |
|
334 | - $curl_opts = $this->curl_opts; |
|
335 | - $curl_opts[CURLOPT_NOBODY] = true; |
|
336 | - |
|
337 | - $curl = $this->prepRequest($this->curl_opts, $url); |
|
338 | - $body = $this->doRequest($curl); |
|
339 | - |
|
340 | - $body = $this->processBody($body); |
|
341 | - |
|
342 | - return $body; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Perform HTTP POST request |
|
347 | - * |
|
348 | - * @param string $url |
|
349 | - * @param array $data |
|
350 | - * @param array $headers |
|
351 | - * @return string |
|
352 | - */ |
|
353 | - public function post($url, $data, $headers = array()) |
|
354 | - { |
|
355 | - $data = $this->prepData($data); |
|
356 | - |
|
357 | - $curl_opts = $this->curl_opts; |
|
358 | - $curl_opts[CURLOPT_CUSTOMREQUEST] = 'POST'; |
|
359 | - if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
360 | - $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
361 | - $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
362 | - |
|
363 | - $curl = $this->prepRequest($curl_opts, $url); |
|
364 | - $body = $this->doRequest($curl); |
|
365 | - |
|
366 | - $body = $this->processBody($body); |
|
367 | - |
|
368 | - return $body; |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Prepare data |
|
373 | - * @param array $data |
|
374 | - * @return array|string |
|
375 | - */ |
|
376 | - public function prepData($data) |
|
377 | - { |
|
378 | - if (is_array($data)) { |
|
379 | - $multipart = false; |
|
380 | - |
|
381 | - foreach ($data as $item) { |
|
382 | - if (is_string($item) && strncmp($item, "@", 1) == 0 && is_file(substr($item, 1))) { |
|
383 | - $multipart = true; |
|
384 | - break; |
|
385 | - } |
|
386 | - } |
|
387 | - |
|
388 | - return ($multipart) ? $data : http_build_query($data); |
|
389 | - } else { |
|
390 | - return $data; |
|
391 | - } |
|
392 | - } |
|
393 | - |
|
394 | - /** |
|
395 | - * Perform HTTP PUT request |
|
396 | - * |
|
397 | - * @param string $url |
|
398 | - * @param array $data |
|
399 | - * @param array $headers |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - public function put($url, $data, $headers = array()) |
|
403 | - { |
|
404 | - $data = $this->prepData($data); |
|
405 | - |
|
406 | - $curl_opts = $this->curl_opts; |
|
407 | - $curl_opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; |
|
408 | - if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
409 | - $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
410 | - $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
411 | - |
|
412 | - $curl = $this->prepRequest($curl_opts, $url); |
|
413 | - $body = $this->doRequest($curl); |
|
414 | - |
|
415 | - $body = $this->processBody($body); |
|
416 | - |
|
417 | - return $body; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Perform HTTP PATCH request |
|
422 | - * |
|
423 | - * @param string $url |
|
424 | - * @param array $data |
|
425 | - * @param array $headers |
|
426 | - * @return string |
|
427 | - */ |
|
428 | - public function patch($url, $data, $headers = array()) |
|
429 | - { |
|
430 | - $data = (is_array($data)) ? http_build_query($data) : $data; |
|
431 | - |
|
432 | - $curl_opts = $this->curl_opts; |
|
433 | - $curl_opts[CURLOPT_CUSTOMREQUEST] = 'PATCH'; |
|
434 | - $headers[] = 'Content-Length: ' . strlen($data); |
|
435 | - $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
436 | - $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
437 | - |
|
438 | - $curl = $this->prepRequest($curl_opts, $url); |
|
439 | - $body = $this->doRequest($curl); |
|
440 | - |
|
441 | - $body = $this->processBody($body); |
|
442 | - |
|
443 | - return $body; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Perform HTTP DELETE request |
|
448 | - * |
|
449 | - * @param string $url |
|
450 | - * @param array $headers |
|
451 | - * @return string |
|
452 | - */ |
|
453 | - public function delete($url, $data, $headers=array()) |
|
454 | - { |
|
455 | - $data = $this->prepData($data); |
|
456 | - |
|
457 | - $curl_opts = $this->curl_opts; |
|
458 | - $curl_opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
|
459 | - if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
460 | - $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
461 | - $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
462 | - |
|
463 | - $curl = $this->prepRequest($curl_opts, $url); |
|
464 | - $body = $this->doRequest($curl); |
|
465 | - |
|
466 | - $body = $this->processBody($body); |
|
467 | - |
|
468 | - return $body; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Get last response body |
|
473 | - * |
|
474 | - * @return string |
|
475 | - */ |
|
476 | - public function lastBody() |
|
477 | - { |
|
478 | - return $this->last_response['body']; |
|
479 | - } |
|
480 | - |
|
481 | - /** |
|
482 | - * Get last response status |
|
483 | - * |
|
484 | - * @return int |
|
485 | - */ |
|
486 | - public function lastStatus() |
|
487 | - { |
|
488 | - return $this->last_response['meta']['http_code']; |
|
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * Return the last response header (case insensitive) or NULL if not present. |
|
493 | - * HTTP allows empty headers (e.g. RFC 2616, Section 14.23), thus is_null() |
|
494 | - * and not negation or empty() should be used. |
|
495 | - * |
|
496 | - * @param string $header |
|
497 | - * @return string |
|
498 | - */ |
|
499 | - public function lastHeader($header) |
|
500 | - { |
|
501 | - if (empty($this->last_headers[strtolower($header)])) { |
|
502 | - return NULL; |
|
503 | - } |
|
504 | - return $this->last_headers[strtolower($header)]; |
|
505 | - } |
|
506 | - |
|
507 | - /** |
|
508 | - * Handle header |
|
509 | - * @param $ch |
|
510 | - * @param $str |
|
511 | - * @return int |
|
512 | - */ |
|
513 | - private function handle_header($ch, $str) |
|
514 | - { |
|
515 | - if (preg_match('/([^:]+):\s(.+)/m', $str, $match)) { |
|
516 | - $this->last_headers[strtolower($match[1])] = trim($match[2]); |
|
517 | - } |
|
518 | - return strlen($str); |
|
519 | - } |
|
200 | + return $flattened; |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Process error |
|
205 | + * @param string $body |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + protected function processError($body) |
|
209 | + { |
|
210 | + // Override this in classes that extend Pest. |
|
211 | + // The body of every erroneous (non-2xx/3xx) GET/POST/PUT/DELETE |
|
212 | + // response goes through here prior to being used as the 'message' |
|
213 | + // of the resulting Pest_Exception |
|
214 | + return $body; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Do CURL request |
|
219 | + * @param resource $curl |
|
220 | + * @return mixed |
|
221 | + * @throws Pest_Curl_Exec |
|
222 | + * @throws Pest_Curl_Meta |
|
223 | + */ |
|
224 | + private function doRequest($curl) |
|
225 | + { |
|
226 | + $this->last_headers = array(); |
|
227 | + $this->last_response = array(); |
|
228 | + |
|
229 | + // curl_error() needs to be tested right after function failure |
|
230 | + $this->last_response["body"] = curl_exec($curl); |
|
231 | + if ($this->last_response["body"] === false && $this->throw_exceptions) { |
|
232 | + throw new Pest_Curl_Exec(curl_error($curl)); |
|
233 | + } |
|
234 | + |
|
235 | + $this->last_response["meta"] = curl_getinfo($curl); |
|
236 | + if ($this->last_response["meta"] === false && $this->throw_exceptions) { |
|
237 | + throw new Pest_Curl_Meta(curl_error($curl)); |
|
238 | + } |
|
239 | + |
|
240 | + curl_close($curl); |
|
241 | + |
|
242 | + $this->checkLastResponseForError(); |
|
243 | + |
|
244 | + return $this->last_response["body"]; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Check last response for error |
|
249 | + * |
|
250 | + * @throws Pest_Conflict |
|
251 | + * @throws Pest_Gone |
|
252 | + * @throws Pest_Unauthorized |
|
253 | + * @throws Pest_ClientError |
|
254 | + * @throws Pest_MethodNotAllowed |
|
255 | + * @throws Pest_NotFound |
|
256 | + * @throws Pest_BadRequest |
|
257 | + * @throws Pest_UnknownResponse |
|
258 | + * @throws Pest_InvalidRecord |
|
259 | + * @throws Pest_ServerError |
|
260 | + * @throws Pest_Forbidden |
|
261 | + */ |
|
262 | + protected function checkLastResponseForError() |
|
263 | + { |
|
264 | + if (!$this->throw_exceptions) |
|
265 | + return; |
|
266 | + |
|
267 | + $meta = $this->last_response['meta']; |
|
268 | + $body = $this->last_response['body']; |
|
269 | + |
|
270 | + if ($meta === false) |
|
271 | + return; |
|
272 | + |
|
273 | + $err = null; |
|
274 | + switch ($meta['http_code']) { |
|
275 | + case 400: |
|
276 | + throw new Pest_BadRequest($this->processError($body)); |
|
277 | + break; |
|
278 | + case 401: |
|
279 | + throw new Pest_Unauthorized($this->processError($body)); |
|
280 | + break; |
|
281 | + case 403: |
|
282 | + throw new Pest_Forbidden($this->processError($body)); |
|
283 | + break; |
|
284 | + case 404: |
|
285 | + throw new Pest_NotFound($this->processError($body)); |
|
286 | + break; |
|
287 | + case 405: |
|
288 | + throw new Pest_MethodNotAllowed($this->processError($body)); |
|
289 | + break; |
|
290 | + case 409: |
|
291 | + throw new Pest_Conflict($this->processError($body)); |
|
292 | + break; |
|
293 | + case 410: |
|
294 | + throw new Pest_Gone($this->processError($body)); |
|
295 | + break; |
|
296 | + case 422: |
|
297 | + // Unprocessable Entity -- see http://www.iana.org/assignments/http-status-codes |
|
298 | + // This is now commonly used (in Rails, at least) to indicate |
|
299 | + // a response to a request that is syntactically correct, |
|
300 | + // but semantically invalid (for example, when trying to |
|
301 | + // create a resource with some required fields missing) |
|
302 | + throw new Pest_InvalidRecord($this->processError($body)); |
|
303 | + break; |
|
304 | + default: |
|
305 | + if ($meta['http_code'] >= 400 && $meta['http_code'] <= 499) |
|
306 | + throw new Pest_ClientError($this->processError($body)); |
|
307 | + elseif ($meta['http_code'] >= 500 && $meta['http_code'] <= 599) |
|
308 | + throw new Pest_ServerError($this->processError($body)); elseif (!isset($meta['http_code']) || $meta['http_code'] >= 600) { |
|
309 | + throw new Pest_UnknownResponse($this->processError($body)); |
|
310 | + } |
|
311 | + } |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Process body |
|
316 | + * @param string $body |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + protected function processBody($body) |
|
320 | + { |
|
321 | + // Override this in classes that extend Pest. |
|
322 | + // The body of every GET/POST/PUT/DELETE response goes through |
|
323 | + // here prior to being returned. |
|
324 | + return $body; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Perform HTTP HEAD request |
|
329 | + * @param string $url |
|
330 | + * @return string |
|
331 | + */ |
|
332 | + public function head($url) |
|
333 | + { |
|
334 | + $curl_opts = $this->curl_opts; |
|
335 | + $curl_opts[CURLOPT_NOBODY] = true; |
|
336 | + |
|
337 | + $curl = $this->prepRequest($this->curl_opts, $url); |
|
338 | + $body = $this->doRequest($curl); |
|
339 | + |
|
340 | + $body = $this->processBody($body); |
|
341 | + |
|
342 | + return $body; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Perform HTTP POST request |
|
347 | + * |
|
348 | + * @param string $url |
|
349 | + * @param array $data |
|
350 | + * @param array $headers |
|
351 | + * @return string |
|
352 | + */ |
|
353 | + public function post($url, $data, $headers = array()) |
|
354 | + { |
|
355 | + $data = $this->prepData($data); |
|
356 | + |
|
357 | + $curl_opts = $this->curl_opts; |
|
358 | + $curl_opts[CURLOPT_CUSTOMREQUEST] = 'POST'; |
|
359 | + if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
360 | + $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
361 | + $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
362 | + |
|
363 | + $curl = $this->prepRequest($curl_opts, $url); |
|
364 | + $body = $this->doRequest($curl); |
|
365 | + |
|
366 | + $body = $this->processBody($body); |
|
367 | + |
|
368 | + return $body; |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Prepare data |
|
373 | + * @param array $data |
|
374 | + * @return array|string |
|
375 | + */ |
|
376 | + public function prepData($data) |
|
377 | + { |
|
378 | + if (is_array($data)) { |
|
379 | + $multipart = false; |
|
380 | + |
|
381 | + foreach ($data as $item) { |
|
382 | + if (is_string($item) && strncmp($item, "@", 1) == 0 && is_file(substr($item, 1))) { |
|
383 | + $multipart = true; |
|
384 | + break; |
|
385 | + } |
|
386 | + } |
|
387 | + |
|
388 | + return ($multipart) ? $data : http_build_query($data); |
|
389 | + } else { |
|
390 | + return $data; |
|
391 | + } |
|
392 | + } |
|
393 | + |
|
394 | + /** |
|
395 | + * Perform HTTP PUT request |
|
396 | + * |
|
397 | + * @param string $url |
|
398 | + * @param array $data |
|
399 | + * @param array $headers |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + public function put($url, $data, $headers = array()) |
|
403 | + { |
|
404 | + $data = $this->prepData($data); |
|
405 | + |
|
406 | + $curl_opts = $this->curl_opts; |
|
407 | + $curl_opts[CURLOPT_CUSTOMREQUEST] = 'PUT'; |
|
408 | + if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
409 | + $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
410 | + $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
411 | + |
|
412 | + $curl = $this->prepRequest($curl_opts, $url); |
|
413 | + $body = $this->doRequest($curl); |
|
414 | + |
|
415 | + $body = $this->processBody($body); |
|
416 | + |
|
417 | + return $body; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Perform HTTP PATCH request |
|
422 | + * |
|
423 | + * @param string $url |
|
424 | + * @param array $data |
|
425 | + * @param array $headers |
|
426 | + * @return string |
|
427 | + */ |
|
428 | + public function patch($url, $data, $headers = array()) |
|
429 | + { |
|
430 | + $data = (is_array($data)) ? http_build_query($data) : $data; |
|
431 | + |
|
432 | + $curl_opts = $this->curl_opts; |
|
433 | + $curl_opts[CURLOPT_CUSTOMREQUEST] = 'PATCH'; |
|
434 | + $headers[] = 'Content-Length: ' . strlen($data); |
|
435 | + $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
436 | + $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
437 | + |
|
438 | + $curl = $this->prepRequest($curl_opts, $url); |
|
439 | + $body = $this->doRequest($curl); |
|
440 | + |
|
441 | + $body = $this->processBody($body); |
|
442 | + |
|
443 | + return $body; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Perform HTTP DELETE request |
|
448 | + * |
|
449 | + * @param string $url |
|
450 | + * @param array $headers |
|
451 | + * @return string |
|
452 | + */ |
|
453 | + public function delete($url, $data, $headers=array()) |
|
454 | + { |
|
455 | + $data = $this->prepData($data); |
|
456 | + |
|
457 | + $curl_opts = $this->curl_opts; |
|
458 | + $curl_opts[CURLOPT_CUSTOMREQUEST] = 'DELETE'; |
|
459 | + if (!is_array($data)) $headers[] = 'Content-Length: ' . strlen($data); |
|
460 | + $curl_opts[CURLOPT_HTTPHEADER] = $this->prepHeaders($headers); |
|
461 | + $curl_opts[CURLOPT_POSTFIELDS] = $data; |
|
462 | + |
|
463 | + $curl = $this->prepRequest($curl_opts, $url); |
|
464 | + $body = $this->doRequest($curl); |
|
465 | + |
|
466 | + $body = $this->processBody($body); |
|
467 | + |
|
468 | + return $body; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Get last response body |
|
473 | + * |
|
474 | + * @return string |
|
475 | + */ |
|
476 | + public function lastBody() |
|
477 | + { |
|
478 | + return $this->last_response['body']; |
|
479 | + } |
|
480 | + |
|
481 | + /** |
|
482 | + * Get last response status |
|
483 | + * |
|
484 | + * @return int |
|
485 | + */ |
|
486 | + public function lastStatus() |
|
487 | + { |
|
488 | + return $this->last_response['meta']['http_code']; |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * Return the last response header (case insensitive) or NULL if not present. |
|
493 | + * HTTP allows empty headers (e.g. RFC 2616, Section 14.23), thus is_null() |
|
494 | + * and not negation or empty() should be used. |
|
495 | + * |
|
496 | + * @param string $header |
|
497 | + * @return string |
|
498 | + */ |
|
499 | + public function lastHeader($header) |
|
500 | + { |
|
501 | + if (empty($this->last_headers[strtolower($header)])) { |
|
502 | + return NULL; |
|
503 | + } |
|
504 | + return $this->last_headers[strtolower($header)]; |
|
505 | + } |
|
506 | + |
|
507 | + /** |
|
508 | + * Handle header |
|
509 | + * @param $ch |
|
510 | + * @param $str |
|
511 | + * @return int |
|
512 | + */ |
|
513 | + private function handle_header($ch, $str) |
|
514 | + { |
|
515 | + if (preg_match('/([^:]+):\s(.+)/m', $str, $match)) { |
|
516 | + $this->last_headers[strtolower($match[1])] = trim($match[2]); |
|
517 | + } |
|
518 | + return strlen($str); |
|
519 | + } |
|
520 | 520 | } |
521 | 521 | |
522 | 522 | class Pest_Exception extends Exception |