Completed
Branch master (c3ca5f)
by Seth
04:51
created
include/class/CanvasApiProcess.class.php 1 patch
Doc Comments   +6 added lines patch added patch discarded remove patch
@@ -240,6 +240,9 @@  discard block
 block discarded – undo
240 240
 		return $this->call(CANVAS_API_PUT, $path, $data, $throwsExceptions);
241 241
 	}
242 242
 	
243
+	/**
244
+	 * @param string $page
245
+	 */
243 246
 	private function pageLink($page) {
244 247
 		if (array_key_exists($page, $this->lastCall['pagination'])) {
245 248
 			$this->pagePest = new Pest($this->lastCall['pagination'][$page]);
@@ -264,6 +267,9 @@  discard block
 block discarded – undo
264 267
 		return $this->pageLink('last');
265 268
 	}
266 269
 	
270
+	/**
271
+	 * @param string $page
272
+	 */
267 273
 	private function getPageNumber($page) {
268 274
 		if (array_key_exists($page, $this->lastCall['pagination'])) {
269 275
 			parse_str(parse_url($this->lastCall['pagination'][$page], PHP_URL_QUERY), $query);
Please login to merge, or discard this patch.
include/Pest.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -217,7 +217,7 @@
 block discarded – undo
217 217
     /**
218 218
      * Do CURL request
219 219
      * @param resource $curl
220
-     * @return mixed
220
+     * @return string
221 221
      * @throws Pest_Curl_Exec
222 222
      * @throws Pest_Curl_Meta
223 223
      */
Please login to merge, or discard this patch.
Indentation   +500 added lines, -500 removed lines patch added patch discarded remove patch
@@ -9,514 +9,514 @@
 block discarded – undo
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
Please login to merge, or discard this patch.
phpicalendar/caldav.php 1 patch
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -34,6 +34,9 @@
 block discarded – undo
34 34
 		return $basePath;
35 35
 	}
36 36
 
37
+	/**
38
+	 * @param string $absolutePath
39
+	 */
37 40
 	function deletePath($absolutePath) {
38 41
 		$dirs = array();
39 42
 		$paths = array();
Please login to merge, or discard this patch.
phpicalendar/functions/admin_functions.php 2 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -208,6 +208,9 @@
 block discarded – undo
208 208
 //
209 209
 // arg0: string version of php to check against
210 210
 // return boolean true if $version is >= current php version
211
+/**
212
+ * @param string $version
213
+ */
211 214
 function check_php_version($version) {
212 215
 	// intval used for version like "4.0.4pl1"
213 216
 	$testVer=intval(str_replace(".", "",$version));
Please login to merge, or discard this patch.
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -222,15 +222,15 @@
 block discarded – undo
222 222
 // arg0: string filename
223 223
 // returns boolean is the uploaded a file
224 224
 function is_uploaded_file_v4 ($filename) {
225
-    if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
226
-        $tmp_file = dirname(tempnam('', ''));
227
-    }
228
-    $tmp_file .= '/' . basename($filename);
229
-    // For Windows compat
230
-    $filename = str_replace ("\\", "/", $filename);
231
-    $tmp_file = str_replace ("\\", "/", $tmp_file);
232
-    // User might have trailing slash in php.ini... 
233
-    return (ereg_replace('/+', '/', $tmp_file) == $filename);
225
+	if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
226
+		$tmp_file = dirname(tempnam('', ''));
227
+	}
228
+	$tmp_file .= '/' . basename($filename);
229
+	// For Windows compat
230
+	$filename = str_replace ("\\", "/", $filename);
231
+	$tmp_file = str_replace ("\\", "/", $tmp_file);
232
+	// User might have trailing slash in php.ini... 
233
+	return (ereg_replace('/+', '/', $tmp_file) == $filename);
234 234
 }
235 235
 
236 236
 // return the appropriate error message if the file upload had an error
Please login to merge, or discard this patch.
phpicalendar/functions/date_functions.php 1 patch
Doc Comments   +9 added lines patch added patch discarded remove patch
@@ -75,6 +75,9 @@  discard block
 block discarded – undo
75 75
  * Resolve relative paths
76 76
  * Utility function for remote_filemtime()
77 77
  */
78
+/**
79
+ * @param string $rel_path
80
+ */
78 81
 function resolve_path($url, $rel_path) {
79 82
 	if (parse_url($rel_path) !== FALSE) {
80 83
 		// Path is a URL
@@ -224,6 +227,9 @@  discard block
 block discarded – undo
224 227
 // $have is the current offset (ie, '-0500')
225 228
 // $want is the wanted offset (ie, '-0700')
226 229
 // $time is the unixtime relative to $have
230
+/**
231
+ * @param integer $time
232
+ */
227 233
 function calcTime($have, $want, $time) {
228 234
 	if ($have == 'none' || $want == 'none') return $time;
229 235
 	$have_secs = calcOffset($have);
@@ -233,6 +239,9 @@  discard block
 block discarded – undo
233 239
 	return $time;
234 240
 }
235 241
 
242
+/**
243
+ * @param integer $time
244
+ */
236 245
 function chooseOffset($time, $timezone = '') {
237 246
 	global $tz_array, $summary;
238 247
 	switch ($timezone) {
Please login to merge, or discard this patch.
phpicalendar/functions/draw_functions.php 1 patch
Doc Comments   +7 added lines patch added patch discarded remove patch
@@ -38,6 +38,9 @@  discard block
 block discarded – undo
38 38
 
39 39
 // word wrap function that returns specified number of lines
40 40
 // when lines is 0, it returns the entire string as wordwrap() does it
41
+/**
42
+ * @param integer $length
43
+ */
41 44
 function word_wrap($str, $length, $lines=0) {
42 45
 	if ($lines > 0) {
43 46
 		$len = $length * $lines;
@@ -51,6 +54,10 @@  discard block
 block discarded – undo
51 54
 
52 55
 // String intercept By Bleakwind
53 56
 // utf-8:$byte=3 | gb2312:$byte=2 | big5:$byte=2
57
+/**
58
+ * @param integer $start
59
+ * @param double $len
60
+ */
54 61
 function bite_str($string, $start, $len, $byte=3){
55 62
 	$str = "";
56 63
 	$count = 0;
Please login to merge, or discard this patch.
phpicalendar/functions/init/sanitize.php 2 patches
Doc Comments   +2 added lines patch added patch discarded remove patch
@@ -35,6 +35,7 @@  discard block
 block discarded – undo
35 35
 
36 36
 /**
37 37
  * Truncate a string to a specific number of words
38
+ * @param integer $count
38 39
  */
39 40
 function chopToWordCount($string, $count) {
40 41
     $wc = str_word_count($string);
@@ -49,6 +50,7 @@  discard block
 block discarded – undo
49 50
 
50 51
 /**
51 52
  * Strip "dangerous" HTML to make it safe to print to web browsers
53
+ * @param string $string
52 54
  */
53 55
 function sanitizeForWeb($string) {
54 56
     $string = preg_replace('/<br\s*\/?>/', "\n", $string);
Please login to merge, or discard this patch.
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -14,22 +14,22 @@  discard block
 block discarded – undo
14 14
  * @return mixed
15 15
  */
16 16
 function recursiveSanitize($value) {
17
-    if (is_array($value)) {
18
-        $valmod = array();
19
-        foreach ($value as $key => $subval) {
20
-            if (is_array($subval)) {
21
-                $subval = recursiveSanitize($subval);
22
-            } else {
23
-                $subval = strip_tags($subval);
24
-            }
25
-            $valmod[$key] = $subval;
26
-        }
27
-        $value = $valmod;
28
-    } else {
29
-        $value = strip_tags($value);
30
-    }
17
+	if (is_array($value)) {
18
+		$valmod = array();
19
+		foreach ($value as $key => $subval) {
20
+			if (is_array($subval)) {
21
+				$subval = recursiveSanitize($subval);
22
+			} else {
23
+				$subval = strip_tags($subval);
24
+			}
25
+			$valmod[$key] = $subval;
26
+		}
27
+		$value = $valmod;
28
+	} else {
29
+		$value = strip_tags($value);
30
+	}
31 31
     
32
-    return $value;
32
+	return $value;
33 33
 }
34 34
 
35 35
 
@@ -37,34 +37,34 @@  discard block
 block discarded – undo
37 37
  * Truncate a string to a specific number of words
38 38
  */
39 39
 function chopToWordCount($string, $count) {
40
-    $wc = str_word_count($string);
41
-    if ($wc > $count) {
40
+	$wc = str_word_count($string);
41
+	if ($wc > $count) {
42 42
 	$words = str_word_count($string, 2);
43 43
 	$last_word = array_slice($words, $count, 1, true);
44 44
 	$pos = key($last_word);
45 45
 	$string = substr($string, 0, $pos) . '...';
46
-    }
47
-    return $string;
46
+	}
47
+	return $string;
48 48
 }
49 49
 
50 50
 /**
51 51
  * Strip "dangerous" HTML to make it safe to print to web browsers
52 52
  */
53 53
 function sanitizeForWeb($string) {
54
-    $string = preg_replace('/<br\s*\/?>/', "\n", $string);
54
+	$string = preg_replace('/<br\s*\/?>/', "\n", $string);
55 55
 
56
-    $string = str_replace('&#36;', '$', $string);
57
-    $string = str_replace('&', '&amp;', $string);
58
-    $string = str_replace('<', '&lt;', $string);
59
-    $string = str_replace('>', '&gt;', $string);
60
-    $string = str_replace('\'', '&#39;', $string);
61
-    $string = str_replace('"', '&#34;', $string);
62
-    $string = str_replace('$', '&#36;', $string);
56
+	$string = str_replace('&#36;', '$', $string);
57
+	$string = str_replace('&', '&amp;', $string);
58
+	$string = str_replace('<', '&lt;', $string);
59
+	$string = str_replace('>', '&gt;', $string);
60
+	$string = str_replace('\'', '&#39;', $string);
61
+	$string = str_replace('"', '&#34;', $string);
62
+	$string = str_replace('$', '&#36;', $string);
63 63
 
64
-    $string = str_replace("\n", '<br />', $string);
65
-    $string = str_replace("\t", ' &nbsp; &nbsp; ', $string);
64
+	$string = str_replace("\n", '<br />', $string);
65
+	$string = str_replace("\t", ' &nbsp; &nbsp; ', $string);
66 66
 
67
-    return $string;
67
+	return $string;
68 68
 }
69 69
 
70 70
 
Please login to merge, or discard this patch.
phpicalendar/functions/is_daylight.php 1 patch
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -2,6 +2,9 @@
 block discarded – undo
2 2
 /* 	values are offset in hhmm (not seconds!) relative to GMT
3 3
 	case The first value is for standard, and the second value is for daylight
4 4
 */
5
+/**
6
+ * @param string $timezone
7
+ */
5 8
 function is_daylight($time, $timezone){
6 9
 	global $tz_array, $summary;
7 10
 	# default to std time, overwrite if daylight.
Please login to merge, or discard this patch.
phpicalendar/functions/parse/overlapping_events.php 1 patch
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -1,6 +1,9 @@
 block discarded – undo
1 1
 <?php
2 2
 // function to determine maximum necessary columns per day
3 3
 // actually an algorithm to get the smallest multiple for two numbers
4
+/**
5
+ * @param integer $b
6
+ */
4 7
 function kgv($a, $b) {
5 8
 	$x = $a;
6 9
 	$y = $b;
Please login to merge, or discard this patch.