Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch '3.1/develop' into 3.1/master

  • Loading branch information...
commit fbbdfcc477ddb8f1adfbe274e9c7194983f43a33 2 parents a3dfdef + 78867c4
Woody Gilk authored March 10, 2011
59  classes/kohana/oauth.php
@@ -17,6 +17,65 @@
17 17
 	public static $version = '1.0';
18 18
 
19 19
 	/**
  20
+	 * Returns the output of a remote URL. Any [curl option](http://php.net/curl_setopt)
  21
+	 * may be used.
  22
+	 *
  23
+	 *     // Do a simple GET request
  24
+	 *     $data = Remote::get($url);
  25
+	 *
  26
+	 *     // Do a POST request
  27
+	 *     $data = Remote::get($url, array(
  28
+	 *         CURLOPT_POST       => TRUE,
  29
+	 *         CURLOPT_POSTFIELDS => http_build_query($array),
  30
+	 *     ));
  31
+	 *
  32
+	 * @param   string   remote URL
  33
+	 * @param   array    curl options
  34
+	 * @return  string
  35
+	 * @throws  Kohana_Exception
  36
+	 */
  37
+	public static function remote($url, array $options = NULL)
  38
+	{
  39
+		// The transfer must always be returned
  40
+		$options[CURLOPT_RETURNTRANSFER] = TRUE;
  41
+
  42
+		// Open a new remote connection
  43
+		$remote = curl_init($url);
  44
+
  45
+		// Set connection options
  46
+		if ( ! curl_setopt_array($remote, $options))
  47
+		{
  48
+			throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url',
  49
+				array(':url' => 'http://php.net/curl_setopt_array'));
  50
+		}
  51
+
  52
+		// Get the response
  53
+		$response = curl_exec($remote);
  54
+
  55
+		// Get the response information
  56
+		$code = curl_getinfo($remote, CURLINFO_HTTP_CODE);
  57
+
  58
+		if ($code AND $code < 200 OR $code > 299)
  59
+		{
  60
+			$error = $response;
  61
+		}
  62
+		elseif ($response === FALSE)
  63
+		{
  64
+			$error = curl_error($remote);
  65
+		}
  66
+
  67
+		// Close the connection
  68
+		curl_close($remote);
  69
+
  70
+		if (isset($error))
  71
+		{
  72
+			throw new Kohana_OAuth_Exception('Error fetching remote :url [ status :code ] :error',
  73
+				array(':url' => $url, ':code' => $code, ':error' => $error));
  74
+		}
  75
+
  76
+		return $response;
  77
+	}
  78
+	/**
20 79
 	 * RFC3986 compatible version of urlencode. Passing an array will encode
21 80
 	 * all of the values in the array. Array keys will not be encoded.
22 81
 	 *
101  classes/kohana/oauth/request.php
@@ -60,6 +60,11 @@ public static function factory($type, $method, $url = NULL, array $params = NULL
60 60
 	protected $params = array();
61 61
 
62 62
 	/**
  63
+	 * @var  array  upload parameters
  64
+	 */
  65
+	protected $upload = array();
  66
+
  67
+	/**
63 68
 	 * @var  array  required parameters
64 69
 	 */
65 70
 	protected $required = array();
@@ -181,7 +186,7 @@ public function base_string()
181 186
 		$url = $this->url;
182 187
 
183 188
 		// Get the request parameters
184  
-		$params = $this->params;
  189
+		$params = array_diff_key($this->params, $this->upload);
185 190
 
186 191
 		// "oauth_signature" is never included in the base string!
187 192
 		unset($params['oauth_signature']);
@@ -203,9 +208,6 @@ public function base_string()
203 208
 	 *     // Get the "oauth_consumer_key" value
204 209
 	 *     $key = $request->param('oauth_consumer_key');
205 210
 	 *
206  
-	 *     // Remove "oauth_consumer_key"
207  
-	 *     $request->param('oauth_consumer_key', NULL);
208  
-	 *
209 211
 	 * @param   string   parameter name
210 212
 	 * @param   mixed    parameter value
211 213
 	 * @param   boolean  allow duplicates?
@@ -215,35 +217,27 @@ public function base_string()
215 217
 	 */
216 218
 	public function param($name, $value = NULL, $duplicate = FALSE)
217 219
 	{
218  
-		if (func_num_args() < 2)
  220
+		if ($value === NULL)
219 221
 		{
220 222
 			// Get the parameter
221 223
 			return Arr::get($this->params, $name);
222 224
 		}
223 225
 
224  
-		if ($value === NULL)
  226
+		if (isset($this->params[$name]) AND $duplicate)
225 227
 		{
226  
-			// Remove the parameter
227  
-			unset($this->params[$name]);
  228
+			if ( ! is_array($this->params[$name]))
  229
+			{
  230
+				// Convert the parameter into an array
  231
+				$this->params[$name] = array($this->params[$name]);
  232
+			}
  233
+
  234
+			// Add the duplicate value
  235
+			$this->params[$name][] = $value;
228 236
 		}
229 237
 		else
230 238
 		{
231  
-			if (isset($this->params[$name]) AND $duplicate)
232  
-			{
233  
-				if ( ! is_array($this->params[$name]))
234  
-				{
235  
-					// Convert the parameter into an array
236  
-					$this->params[$name] = array($this->params[$name]);
237  
-				}
238  
-
239  
-				// Add the duplicate value
240  
-				$this->params[$name][] = $value;
241  
-			}
242  
-			else
243  
-			{
244  
-				// Set the parameter value
245  
-				$this->params[$name] = $value;
246  
-			}
  239
+			// Set the parameter value
  240
+			$this->params[$name] = $value;
247 241
 		}
248 242
 
249 243
 		return $this;
@@ -270,6 +264,38 @@ public function params(array $params, $duplicate = FALSE)
270 264
 	}
271 265
 
272 266
 	/**
  267
+	 * Upload getter and setter. Setting the value to `NULL` will remove it.
  268
+	 *
  269
+	 *     // Set the "image" file path for uploading
  270
+	 *     $request->upload('image', $file_path);
  271
+	 *
  272
+	 *     // Get the "image" file path
  273
+	 *     $key = $request->param('oauth_consumer_key');
  274
+	 *
  275
+	 * @param   string   upload name
  276
+	 * @param   mixed    upload file path
  277
+	 * @return  mixed    when getting
  278
+	 * @return  $this    when setting
  279
+	 * @uses    OAuth_Request::param
  280
+	 */
  281
+	public function upload($name, $value = NULL)
  282
+	{
  283
+		if ($value !== NULL)
  284
+		{
  285
+			// This is an upload parameter
  286
+			$this->upload[$name] = TRUE;
  287
+
  288
+			// Get the mime type of the image
  289
+			$mime = File::mime($value);
  290
+
  291
+			// Format the image path for CURL
  292
+			$value = "@{$value};type={$mime}";
  293
+		}
  294
+
  295
+		return $this->param($name, $value, FALSE);
  296
+	}
  297
+
  298
+	/**
273 299
 	 * Get and set required parameters.
274 300
 	 *
275 301
 	 *     $request->required($field, $value);
@@ -283,6 +309,7 @@ public function required($param, $value = NULL)
283 309
 	{
284 310
 		if ($value === NULL)
285 311
 		{
  312
+			// Get the current status
286 313
 			return ! empty($this->required[$param]);
287 314
 		}
288 315
 
@@ -326,16 +353,25 @@ public function as_header()
326 353
 	 *
327 354
 	 * [!!] This method implements [OAuth 1.0 Spec 5.2 (2,3)](http://oauth.net/core/1.0/#rfc.section.5.2).
328 355
 	 *
329  
-	 * @param   boolean   include oauth parameters
  356
+	 * @param   boolean   include oauth parameters?
  357
+	 * @param   boolean   return a normalized string?
330 358
 	 * @return  string
331 359
 	 */
332  
-	public function as_query($include_oauth = NULL)
  360
+	public function as_query($include_oauth = NULL, $as_string = TRUE)
333 361
 	{
334  
-		if ($include_oauth !== TRUE AND $this->send_header)
  362
+		if ($include_oauth === NULL)
335 363
 		{
336 364
 			// If we are sending a header, OAuth parameters should not be
337 365
 			// included in the query string.
  366
+			$include_oauth = ! $this->send_header;
  367
+		}
338 368
 
  369
+		if ($include_oauth)
  370
+		{
  371
+			$params = $this->params;
  372
+		}
  373
+		else
  374
+		{
339 375
 			$params = array();
340 376
 			foreach ($this->params as $name => $value)
341 377
 			{
@@ -346,12 +382,8 @@ public function as_query($include_oauth = NULL)
346 382
 				}
347 383
 			}
348 384
 		}
349  
-		else
350  
-		{
351  
-			$params = $this->params;
352  
-		}
353 385
 
354  
-		return OAuth::normalize_params($params);
  386
+		return $as_string ? OAuth::normalize_params($params) : $params;
355 387
 	}
356 388
 
357 389
 	/**
@@ -422,7 +454,6 @@ public function check()
422 454
 	/**
423 455
 	 * Execute the request and return a response.
424 456
 	 *
425  
-	 * @param   string   request type: GET, POST, etc (NULL for header)
426 457
 	 * @param   array    additional cURL options
427 458
 	 * @return  string   request response body
428 459
 	 * @uses    OAuth_Request::check
@@ -460,7 +491,7 @@ public function execute(array $options = NULL)
460 491
 			// Send the request as a POST
461 492
 			$options[CURLOPT_POST] = TRUE;
462 493
 
463  
-			if ($post = $this->as_query())
  494
+			if ($post = $this->as_query(NULL, empty($this->upload)))
464 495
 			{
465 496
 				// Attach the post fields to the request
466 497
 				$options[CURLOPT_POSTFIELDS] = $post;
@@ -472,7 +503,7 @@ public function execute(array $options = NULL)
472 503
 			$url = "{$url}?{$query}";
473 504
 		}
474 505
 
475  
-		return Remote::get($url, $options);
  506
+		return OAuth::remote($url, $options);
476 507
 	}
477 508
 
478 509
 } // End OAuth_Request
2  config/userguide.php
@@ -14,7 +14,7 @@
14 14
 			'name' => 'OAuth',
15 15
 
16 16
 			// A short description of this module, shown on the index page
17  
-			'description' => 'Official OAuth module, used for open protocol authorization.',
  17
+			'description' => 'Open protocol authorization.',
18 18
 			
19 19
 			// Copyright message, shown in the footer for this module
20 20
 			'copyright' => '&copy; 2008–2010 Kohana Team',

0 notes on commit fbbdfcc

Please sign in to comment.
Something went wrong with that request. Please try again.