Skip to content

Commit

Permalink
Merge remote branch 'origin/3.0.x' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
bluehawk committed Dec 2, 2010
2 parents 7abdd96 + 3b16790 commit 210b1aa
Showing 1 changed file with 65 additions and 34 deletions.
99 changes: 65 additions & 34 deletions classes/kohana/oauth/request.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,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
*/
Expand Down Expand Up @@ -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']);
Expand All @@ -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?
Expand All @@ -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;
Expand All @@ -269,6 +263,38 @@ public function params(array $params, $duplicate = FALSE)
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.
*
Expand All @@ -283,6 +309,7 @@ public function required($param, $value = NULL)
{
if ($value === NULL)
{
// Get the current status
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).
*
* @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)
{
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.
*
* @param string request type: GET, POST, etc (NULL for header)
* @param array additional cURL options
* @return string request response body
* @uses OAuth_Request::check
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 210b1aa

Please sign in to comment.