Skip to content

Commit

Permalink
Merge branch '3.0/develop' into 3.1/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Woody Gilk committed Jan 10, 2011
2 parents a3dfdef + 4f1696c commit bc79b8a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 35 deletions.
99 changes: 65 additions & 34 deletions classes/kohana/oauth/request.php
Expand Up @@ -59,6 +59,11 @@ public static function factory($type, $method, $url = NULL, array $params = NULL
*/ */
protected $params = array(); protected $params = array();


/**
* @var array upload parameters
*/
protected $upload = array();

/** /**
* @var array required parameters * @var array required parameters
*/ */
Expand Down Expand Up @@ -181,7 +186,7 @@ public function base_string()
$url = $this->url; $url = $this->url;


// Get the request parameters // Get the request parameters
$params = $this->params; $params = array_diff_key($this->params, $this->upload);


// "oauth_signature" is never included in the base string! // "oauth_signature" is never included in the base string!
unset($params['oauth_signature']); unset($params['oauth_signature']);
Expand All @@ -203,9 +208,6 @@ public function base_string()
* // Get the "oauth_consumer_key" value * // Get the "oauth_consumer_key" value
* $key = $request->param('oauth_consumer_key'); * $key = $request->param('oauth_consumer_key');
* *
* // Remove "oauth_consumer_key"
* $request->param('oauth_consumer_key', NULL);
*
* @param string parameter name * @param string parameter name
* @param mixed parameter value * @param mixed parameter value
* @param boolean allow duplicates? * @param boolean allow duplicates?
Expand All @@ -215,35 +217,27 @@ public function base_string()
*/ */
public function param($name, $value = NULL, $duplicate = FALSE) public function param($name, $value = NULL, $duplicate = FALSE)
{ {
if (func_num_args() < 2) if ($value === NULL)
{ {
// Get the parameter // Get the parameter
return Arr::get($this->params, $name); return Arr::get($this->params, $name);
} }


if ($value === NULL) if (isset($this->params[$name]) AND $duplicate)
{ {
// Remove the parameter if ( ! is_array($this->params[$name]))
unset($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 else
{ {
if (isset($this->params[$name]) AND $duplicate) // Set the parameter value
{ $this->params[$name] = $value;
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;
}
} }


return $this; return $this;
Expand All @@ -269,6 +263,38 @@ public function params(array $params, $duplicate = FALSE)
return $this; return $this;
} }


/**
* 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. * Get and set required parameters.
* *
Expand All @@ -283,6 +309,7 @@ public function required($param, $value = NULL)
{ {
if ($value === NULL) if ($value === NULL)
{ {
// Get the current status
return ! empty($this->required[$param]); return ! empty($this->required[$param]);
} }


Expand Down Expand Up @@ -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). * [!!] 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 * @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 // If we are sending a header, OAuth parameters should not be
// included in the query string. // included in the query string.
$include_oauth = ! $this->send_header;
}


if ($include_oauth)
{
$params = $this->params;
}
else
{
$params = array(); $params = array();
foreach ($this->params as $name => $value) foreach ($this->params as $name => $value)
{ {
Expand All @@ -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;
} }


/** /**
Expand Down Expand Up @@ -422,7 +454,6 @@ public function check()
/** /**
* Execute the request and return a response. * Execute the request and return a response.
* *
* @param string request type: GET, POST, etc (NULL for header)
* @param array additional cURL options * @param array additional cURL options
* @return string request response body * @return string request response body
* @uses OAuth_Request::check * @uses OAuth_Request::check
Expand Down Expand Up @@ -460,7 +491,7 @@ public function execute(array $options = NULL)
// Send the request as a POST // Send the request as a POST
$options[CURLOPT_POST] = TRUE; $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 // Attach the post fields to the request
$options[CURLOPT_POSTFIELDS] = $post; $options[CURLOPT_POSTFIELDS] = $post;
Expand Down
2 changes: 1 addition & 1 deletion config/userguide.php
Expand Up @@ -14,7 +14,7 @@
'name' => 'OAuth', 'name' => 'OAuth',


// A short description of this module, shown on the index page // 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 message, shown in the footer for this module
'copyright' => '&copy; 2008–2010 Kohana Team', 'copyright' => '&copy; 2008–2010 Kohana Team',
Expand Down

0 comments on commit bc79b8a

Please sign in to comment.