Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch '3.1/develop' into 3.1/master

  • Loading branch information...
commit fbbdfcc477ddb8f1adfbe274e9c7194983f43a33 2 parents a3dfdef + 78867c4
@shadowhand shadowhand authored
View
59 classes/kohana/oauth.php
@@ -17,6 +17,65 @@
public static $version = '1.0';
/**
+ * Returns the output of a remote URL. Any [curl option](http://php.net/curl_setopt)
+ * may be used.
+ *
+ * // Do a simple GET request
+ * $data = Remote::get($url);
+ *
+ * // Do a POST request
+ * $data = Remote::get($url, array(
+ * CURLOPT_POST => TRUE,
+ * CURLOPT_POSTFIELDS => http_build_query($array),
+ * ));
+ *
+ * @param string remote URL
+ * @param array curl options
+ * @return string
+ * @throws Kohana_Exception
+ */
+ public static function remote($url, array $options = NULL)
+ {
+ // The transfer must always be returned
+ $options[CURLOPT_RETURNTRANSFER] = TRUE;
+
+ // Open a new remote connection
+ $remote = curl_init($url);
+
+ // Set connection options
+ if ( ! curl_setopt_array($remote, $options))
+ {
+ throw new Kohana_Exception('Failed to set CURL options, check CURL documentation: :url',
+ array(':url' => 'http://php.net/curl_setopt_array'));
+ }
+
+ // Get the response
+ $response = curl_exec($remote);
+
+ // Get the response information
+ $code = curl_getinfo($remote, CURLINFO_HTTP_CODE);
+
+ if ($code AND $code < 200 OR $code > 299)
+ {
+ $error = $response;
+ }
+ elseif ($response === FALSE)
+ {
+ $error = curl_error($remote);
+ }
+
+ // Close the connection
+ curl_close($remote);
+
+ if (isset($error))
+ {
+ throw new Kohana_OAuth_Exception('Error fetching remote :url [ status :code ] :error',
+ array(':url' => $url, ':code' => $code, ':error' => $error));
+ }
+
+ return $response;
+ }
+ /**
* RFC3986 compatible version of urlencode. Passing an array will encode
* all of the values in the array. Array keys will not be encoded.
*
View
101 classes/kohana/oauth/request.php
@@ -60,6 +60,11 @@ public static function factory($type, $method, $url = NULL, array $params = NULL
protected $params = array();
/**
+ * @var array upload parameters
+ */
+ protected $upload = array();
+
+ /**
* @var array required parameters
*/
protected $required = array();
@@ -181,7 +186,7 @@ public function base_string()
$url = $this->url;
// Get the request parameters
- $params = $this->params;
+ $params = array_diff_key($this->params, $this->upload);
// "oauth_signature" is never included in the base string!
unset($params['oauth_signature']);
@@ -203,9 +208,6 @@ public function base_string()
* // Get the "oauth_consumer_key" value
* $key = $request->param('oauth_consumer_key');
*
- * // Remove "oauth_consumer_key"
- * $request->param('oauth_consumer_key', NULL);
- *
* @param string parameter name
* @param mixed parameter value
* @param boolean allow duplicates?
@@ -215,35 +217,27 @@ public function base_string()
*/
public function param($name, $value = NULL, $duplicate = FALSE)
{
- if (func_num_args() < 2)
+ if ($value === NULL)
{
// Get the parameter
return Arr::get($this->params, $name);
}
- if ($value === NULL)
+ if (isset($this->params[$name]) AND $duplicate)
{
- // Remove the parameter
- unset($this->params[$name]);
+ if ( ! is_array($this->params[$name]))
+ {
+ // Convert the parameter into an array
+ $this->params[$name] = array($this->params[$name]);
+ }
+
+ // Add the duplicate value
+ $this->params[$name][] = $value;
}
else
{
- if (isset($this->params[$name]) AND $duplicate)
- {
- if ( ! is_array($this->params[$name]))
- {
- // Convert the parameter into an array
- $this->params[$name] = array($this->params[$name]);
- }
-
- // Add the duplicate value
- $this->params[$name][] = $value;
- }
- else
- {
- // Set the parameter value
- $this->params[$name] = $value;
- }
+ // Set the parameter value
+ $this->params[$name] = $value;
}
return $this;
@@ -270,6 +264,38 @@ public function params(array $params, $duplicate = FALSE)
}
/**
+ * Upload getter and setter. Setting the value to `NULL` will remove it.
+ *
+ * // Set the "image" file path for uploading
+ * $request->upload('image', $file_path);
+ *
+ * // Get the "image" file path
+ * $key = $request->param('oauth_consumer_key');
+ *
+ * @param string upload name
+ * @param mixed upload file path
+ * @return mixed when getting
+ * @return $this when setting
+ * @uses OAuth_Request::param
+ */
+ public function upload($name, $value = NULL)
+ {
+ if ($value !== NULL)
+ {
+ // This is an upload parameter
+ $this->upload[$name] = TRUE;
+
+ // Get the mime type of the image
+ $mime = File::mime($value);
+
+ // Format the image path for CURL
+ $value = "@{$value};type={$mime}";
+ }
+
+ return $this->param($name, $value, FALSE);
+ }
+
+ /**
* Get and set required parameters.
*
* $request->required($field, $value);
@@ -283,6 +309,7 @@ public function required($param, $value = NULL)
{
if ($value === NULL)
{
+ // Get the current status
return ! empty($this->required[$param]);
}
@@ -326,16 +353,25 @@ public function as_header()
*
* [!!] This method implements [OAuth 1.0 Spec 5.2 (2,3)](http://oauth.net/core/1.0/#rfc.section.5.2).
*
- * @param boolean include oauth parameters
+ * @param boolean include oauth parameters?
+ * @param boolean return a normalized string?
* @return string
*/
- public function as_query($include_oauth = NULL)
+ public function as_query($include_oauth = NULL, $as_string = TRUE)
{
- if ($include_oauth !== TRUE AND $this->send_header)
+ if ($include_oauth === NULL)
{
// If we are sending a header, OAuth parameters should not be
// included in the query string.
+ $include_oauth = ! $this->send_header;
+ }
+ if ($include_oauth)
+ {
+ $params = $this->params;
+ }
+ else
+ {
$params = array();
foreach ($this->params as $name => $value)
{
@@ -346,12 +382,8 @@ public function as_query($include_oauth = NULL)
}
}
}
- else
- {
- $params = $this->params;
- }
- return OAuth::normalize_params($params);
+ return $as_string ? OAuth::normalize_params($params) : $params;
}
/**
@@ -422,7 +454,6 @@ public function check()
/**
* Execute the request and return a response.
*
- * @param string request type: GET, POST, etc (NULL for header)
* @param array additional cURL options
* @return string request response body
* @uses OAuth_Request::check
@@ -460,7 +491,7 @@ public function execute(array $options = NULL)
// Send the request as a POST
$options[CURLOPT_POST] = TRUE;
- if ($post = $this->as_query())
+ if ($post = $this->as_query(NULL, empty($this->upload)))
{
// Attach the post fields to the request
$options[CURLOPT_POSTFIELDS] = $post;
@@ -472,7 +503,7 @@ public function execute(array $options = NULL)
$url = "{$url}?{$query}";
}
- return Remote::get($url, $options);
+ return OAuth::remote($url, $options);
}
} // End OAuth_Request
View
2  config/userguide.php
@@ -14,7 +14,7 @@
'name' => 'OAuth',
// A short description of this module, shown on the index page
- 'description' => 'Official OAuth module, used for open protocol authorization.',
+ 'description' => 'Open protocol authorization.',
// Copyright message, shown in the footer for this module
'copyright' => '&copy; 2008–2010 Kohana Team',
Please sign in to comment.
Something went wrong with that request. Please try again.