Bump Dropbox library to newest upstream version
This commit is contained in:
parent
2f8296875f
commit
418f4e1a90
|
@ -45,6 +45,20 @@ class Dropbox_API {
|
|||
protected $root;
|
||||
protected $useSSL;
|
||||
|
||||
/**
|
||||
* PHP is Safe Mode
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isSafeMode;
|
||||
|
||||
/**
|
||||
* Chunked file size limit
|
||||
*
|
||||
* @var unknown
|
||||
*/
|
||||
public $chunkSize = 20971520; //20MB
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
|
@ -56,6 +70,7 @@ class Dropbox_API {
|
|||
$this->oauth = $oauth;
|
||||
$this->root = $root;
|
||||
$this->useSSL = $useSSL;
|
||||
$this->isSafeMode = (version_compare(PHP_VERSION, '5.4', '<') && get_cfg_var('safe_mode'));
|
||||
if (!$this->useSSL)
|
||||
{
|
||||
throw new Dropbox_Exception('Dropbox REST API now requires that all requests use SSL');
|
||||
|
@ -101,12 +116,51 @@ class Dropbox_API {
|
|||
*/
|
||||
public function putFile($path, $file, $root = null) {
|
||||
|
||||
if (is_resource($file)) {
|
||||
$stat = fstat($file);
|
||||
$size = $stat['size'];
|
||||
} else if (is_string($file) && is_readable($file)) {
|
||||
$size = filesize($file);
|
||||
} else {
|
||||
throw new Dropbox_Exception('File must be a file-resource or a file path string');
|
||||
}
|
||||
|
||||
if ($this->oauth->isPutSupport()) {
|
||||
if ($size) {
|
||||
if ($size > $this->chunkSize) {
|
||||
$res = array('uploadID' => null, 'offset' => 0);
|
||||
$i[$res['offset']] = 0;
|
||||
while($i[$res['offset']] < 5) {
|
||||
$res = $this->chunkedUpload($path, $file, $root, true, $res['offset'], $res['uploadID']);
|
||||
if (isset($res['uploadID'])) {
|
||||
if (!isset($i[$res['offset']])) {
|
||||
$i[$res['offset']] = 0;
|
||||
} else {
|
||||
$i[$res['offset']]++;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return $this->putStream($path, $file, $root);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($size > 157286400) {
|
||||
// Dropbox API /files has a maximum file size limit of 150 MB.
|
||||
// https://www.dropbox.com/developers/core/docs#files-POST
|
||||
throw new Dropbox_Exception("Uploading file to Dropbox failed");
|
||||
}
|
||||
|
||||
$directory = dirname($path);
|
||||
$filename = basename($path);
|
||||
|
||||
if($directory==='.') $directory = '';
|
||||
$directory = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($directory));
|
||||
// $filename = str_replace('~', '%7E', rawurlencode($filename));
|
||||
$filename = str_replace('~', '%7E', rawurlencode($filename));
|
||||
if (is_null($root)) $root = $this->root;
|
||||
|
||||
if (is_string($file)) {
|
||||
|
@ -114,7 +168,11 @@ class Dropbox_API {
|
|||
$file = fopen($file,'rb');
|
||||
|
||||
} elseif (!is_resource($file)) {
|
||||
throw new Dropbox_Exception('File must be a file-resource or a string');
|
||||
throw new Dropbox_Exception('File must be a file-resource or a file path string');
|
||||
}
|
||||
|
||||
if (!$this->isSafeMode) {
|
||||
set_time_limit(600);
|
||||
}
|
||||
$result=$this->multipartFetch($this->api_content_url . 'files/' .
|
||||
$root . '/' . trim($directory,'/'), $file, $filename);
|
||||
|
@ -125,6 +183,116 @@ class Dropbox_API {
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads large files to Dropbox in mulitple chunks
|
||||
*
|
||||
* @param string $file Absolute path to the file to be uploaded
|
||||
* @param string|bool $filename The destination filename of the uploaded file
|
||||
* @param string $path Path to upload the file to, relative to root
|
||||
* @param boolean $overwrite Should the file be overwritten? (Default: true)
|
||||
* @return stdClass
|
||||
*/
|
||||
public function chunkedUpload($path, $handle, $root = null, $overwrite = true, $offset = 0, $uploadID = null)
|
||||
{
|
||||
if (is_string($handle) && is_readable($handle)) {
|
||||
$handle = fopen($handle, 'rb');
|
||||
}
|
||||
|
||||
if (is_resource($handle)) {
|
||||
// Seek to the correct position on the file pointer
|
||||
fseek($handle, $offset);
|
||||
|
||||
// Read from the file handle until EOF, uploading each chunk
|
||||
while ($data = fread($handle, $this->chunkSize)) {
|
||||
if (!$this->isSafeMode) {
|
||||
set_time_limit(600);
|
||||
}
|
||||
|
||||
// Open a temporary file handle and write a chunk of data to it
|
||||
$chunkHandle = fopen('php://temp', 'rwb');
|
||||
fwrite($chunkHandle, $data);
|
||||
|
||||
// Set the file, request parameters and send the request
|
||||
$this->oauth->setInFile($chunkHandle);
|
||||
$params = array('upload_id' => $uploadID, 'offset' => $offset);
|
||||
|
||||
try {
|
||||
// Attempt to upload the current chunk
|
||||
$res = $this->oauth->fetch($this->api_content_url . 'chunked_upload', $params, 'PUT');
|
||||
$response = json_decode($res['body'], true);
|
||||
} catch (Exception $e) {
|
||||
$res = $this->oauth->getLastResponse();
|
||||
if ($response['httpStatus'] == 400) {
|
||||
// Incorrect offset supplied, return expected offset and upload ID
|
||||
$response = json_decode($res['body'], true);
|
||||
$uploadID = $response['upload_id'];
|
||||
$offset = $response['offset'];
|
||||
return array('uploadID' => $uploadID, 'offset' => $offset);
|
||||
} else {
|
||||
// Re-throw the caught Exception
|
||||
throw $e;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
|
||||
// On subsequent chunks, use the upload ID returned by the previous request
|
||||
if (isset($response['upload_id'])) {
|
||||
$uploadID = $response['upload_id'];
|
||||
}
|
||||
|
||||
// Set the data offset
|
||||
if (isset($response['offset'])) {
|
||||
$offset = $response['offset'];
|
||||
}
|
||||
|
||||
// Close the file handle for this chunk
|
||||
fclose($chunkHandle);
|
||||
}
|
||||
|
||||
// Complete the chunked upload
|
||||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
if (is_null($root)) {
|
||||
$root = $this->root;
|
||||
}
|
||||
$params = array('overwrite' => (int) $overwrite, 'upload_id' => $uploadID);
|
||||
return $this->oauth->fetch($this->api_content_url . 'commit_chunked_upload/' .
|
||||
$root . '/' . ltrim($path,'/'), $params, 'POST');
|
||||
} else {
|
||||
throw new Dropbox_Exception('Could not open ' . $handle . ' for reading');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uploads file data from a stream
|
||||
*
|
||||
* Note: This function is experimental and requires further testing
|
||||
* @param resource $stream A readable stream created using fopen()
|
||||
* @param string $filename The destination filename, including path
|
||||
* @param boolean $overwrite Should the file be overwritten? (Default: true)
|
||||
* @return array
|
||||
*/
|
||||
public function putStream($path, $file, $root = null, $overwrite = true)
|
||||
{
|
||||
if ($this->isSafeMode) {
|
||||
set_time_limit(600);
|
||||
}
|
||||
|
||||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
if (is_null($root)) {
|
||||
$root = $this->root;
|
||||
}
|
||||
|
||||
$params = array('overwrite' => (int) $overwrite);
|
||||
$this->oauth->setInfile($file);
|
||||
$result=$this->oauth->fetch($this->api_content_url . 'files_put/' .
|
||||
$root . '/' . ltrim($path,'/'), $params, 'PUT');
|
||||
|
||||
if (!isset($result["httpStatus"]) || $result["httpStatus"] != 200) {
|
||||
throw new Dropbox_Exception("Uploading file to Dropbox failed");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a file or directory from one location to another
|
||||
|
@ -159,7 +327,7 @@ class Dropbox_API {
|
|||
if (is_null($root)) $root = $this->root;
|
||||
|
||||
// Making sure the path starts with a /
|
||||
// $path = '/' . ltrim($path,'/');
|
||||
$path = '/' . ltrim($path,'/');
|
||||
|
||||
$response = $this->oauth->fetch($this->api_url . 'fileops/create_folder', array('path' => $path, 'root' => $root),'POST');
|
||||
return json_decode($response['body'],true);
|
||||
|
@ -329,14 +497,16 @@ class Dropbox_API {
|
|||
*
|
||||
* Note: Links created by the /shares API call expire after thirty days.
|
||||
*
|
||||
* @param type $path
|
||||
* @param type $root
|
||||
* @return type
|
||||
* @param string $path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @param string $short_url When true (default), the URL returned will be shortened using the Dropbox URL shortener
|
||||
* @return array
|
||||
*/
|
||||
public function share($path, $root = null) {
|
||||
public function share($path, $root = null, $short_url = true) {
|
||||
if (is_null($root)) $root = $this->root;
|
||||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
$response = $this->oauth->fetch($this->api_url. 'shares/'. $root . '/' . ltrim($path, '/'), array(), 'POST');
|
||||
$short_url = ((is_string($short_url) && strtolower($short_url) === 'false') || !$short_url)? 'false' : 'true';
|
||||
$response = $this->oauth->fetch($this->api_url. 'shares/'. $root . '/' . ltrim($path, '/'), array('short_url' => $short_url), 'POST');
|
||||
return json_decode($response['body'],true);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,19 +0,0 @@
|
|||
Copyright (c) 2010 Rooftop Solutions
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
|
@ -61,6 +61,33 @@ abstract class Dropbox_OAuth {
|
|||
*/
|
||||
protected $oauth_token_secret = null;
|
||||
|
||||
/**
|
||||
* Get OAuth request last responce
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $lastResponse = array();
|
||||
|
||||
/**
|
||||
* Input file stream pointer or file path for PUT method
|
||||
*
|
||||
* @var resource|string
|
||||
*/
|
||||
protected $inFile = null;
|
||||
|
||||
/**
|
||||
* Input file size for PUT method
|
||||
*
|
||||
* @var resource | string
|
||||
*/
|
||||
protected $inFileSize = null;
|
||||
|
||||
/**
|
||||
* Is support PUT method on OAuth consumer
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $putSupported = false;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
@ -123,6 +150,44 @@ abstract class Dropbox_OAuth {
|
|||
return $uri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set input file for PUT method
|
||||
*
|
||||
* @param resource|string $file
|
||||
* @throws Dropbox_Exception
|
||||
*/
|
||||
public function setInfile($file) {
|
||||
if (is_resource($file)) {
|
||||
$stat = fstat($file);
|
||||
$this->inFileSize = $stat['size'];
|
||||
} else if (is_string($file) && is_readable($file)) {
|
||||
$this->inFileSize = filesize($file);
|
||||
$file = fopen($file, 'rb');
|
||||
}
|
||||
if (!is_resource($file)) {
|
||||
throw new Dropbox_Exception('File must be a file-resource or a string');
|
||||
}
|
||||
$this->inFile = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return is PUT method supported
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isPutSupport() {
|
||||
return $this->putSupported;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last request response
|
||||
*
|
||||
* @return array:
|
||||
*/
|
||||
public function getLastResponse() {
|
||||
return $this->lastResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
|
|
|
@ -44,6 +44,7 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
|
||||
$this->consumerKey = $consumerKey;
|
||||
$this->consumerSecret = $consumerSecret;
|
||||
$this->putSupported = true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +63,7 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
preg_match("/\?file=(.*)$/i", $uri, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$uri = str_replace($matches[0], "", $uri);
|
||||
$filename = $matches[1];
|
||||
$filename = rawurldecode(str_replace('%7E', '~', $matches[1]));
|
||||
$httpHeaders=array_merge($httpHeaders,$this->getOAuthHeader($uri, array("file" => $filename), $method));
|
||||
}
|
||||
} else {
|
||||
|
@ -72,20 +73,29 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
if (strtoupper($method) == 'POST') {
|
||||
curl_setopt($ch, CURLOPT_URL, $uri);
|
||||
curl_setopt($ch, CURLOPT_POST, true);
|
||||
// if (is_array($arguments))
|
||||
// $arguments=http_build_query($arguments);
|
||||
if (is_array($arguments)) {
|
||||
$arguments=http_build_query($arguments);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $arguments);
|
||||
// $httpHeaders['Content-Length']=strlen($arguments);
|
||||
$httpHeaders['Content-Length']=strlen($arguments);
|
||||
} else if (strtoupper($method) == 'PUT' && $this->inFile) {
|
||||
curl_setopt($ch, CURLOPT_URL, $uri.'?'.http_build_query($arguments));
|
||||
curl_setopt($ch, CURLOPT_PUT, true);
|
||||
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_INFILE, $this->inFile);
|
||||
curl_setopt($ch, CURLOPT_INFILESIZE, $this->inFileSize);
|
||||
fseek($this->inFile, 0);
|
||||
$this->inFileSize = $this->inFile = null;
|
||||
} else {
|
||||
curl_setopt($ch, CURLOPT_URL, $uri.'?'.http_build_query($arguments));
|
||||
curl_setopt($ch, CURLOPT_POST, false);
|
||||
}
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||||
// curl_setopt($ch, CURLOPT_CAINFO, "rootca");
|
||||
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
|
||||
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__) . DIRECTORY_SEPARATOR . 'ca-bundle.pem');
|
||||
//Build header
|
||||
$headers = array();
|
||||
foreach ($httpHeaders as $name => $value) {
|
||||
|
@ -105,12 +115,21 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
$status=curl_getinfo($ch,CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
$this->lastResponse = array(
|
||||
'httpStatus' => $status,
|
||||
'body' => $response
|
||||
);
|
||||
|
||||
if (!empty($errorno))
|
||||
throw new Dropbox_Exception_NotFound('Curl error: ('.$errorno.') '.$error."\n");
|
||||
|
||||
if ($status>=300) {
|
||||
$body = json_decode($response,true);
|
||||
$body = array();
|
||||
$body = json_decode($response, true);
|
||||
if (!is_array($body)) {
|
||||
$body = array();
|
||||
}
|
||||
$jsonErr = isset($body['error'])? $body['error'] : '';
|
||||
switch ($status) {
|
||||
// Not modified
|
||||
case 304 :
|
||||
|
@ -119,19 +138,28 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
'body' => null,
|
||||
);
|
||||
break;
|
||||
case 400 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
|
||||
case 401 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.');
|
||||
case 403 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden.
|
||||
This could mean a bad OAuth request, or a file or folder already existing at the target location.
|
||||
' . $body["error"] . "\n");
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
|
||||
case 404 :
|
||||
throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found. ' .
|
||||
$body["error"] . "\n");
|
||||
throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
|
||||
case 405 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
|
||||
case 500 :
|
||||
throw new Dropbox_Exception_Forbidden('Server error. ' . $jsonErr);
|
||||
case 503 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.');
|
||||
case 507 :
|
||||
throw new Dropbox_Exception_OverQuota('This dropbox is full. ' .
|
||||
$body["error"] . "\n");
|
||||
throw new Dropbox_Exception_OverQuota('This dropbox is full');
|
||||
default:
|
||||
throw new Dropbox_Exception_RequestToken('Error: ('.$status.') ' . $jsonErr);
|
||||
|
||||
}
|
||||
if (!empty($body["error"]))
|
||||
throw new Dropbox_Exception_RequestToken('Error: ('.$status.') '.$body["error"]."\n");
|
||||
throw new Dropbox_Exception_RequestToken('Error: ('.$status.') ' . $jsonErr);
|
||||
}
|
||||
|
||||
return array(
|
||||
|
@ -183,7 +211,7 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
|
||||
$encodedParams = array();
|
||||
foreach ($signatureParams as $key => $value) {
|
||||
$encodedParams[] = $this->oauth_urlencode($key) . '=' . $this->oauth_urlencode($value);
|
||||
if (!is_null($value)) $encodedParams[] = rawurlencode($key) . '=' . rawurlencode($value);
|
||||
}
|
||||
|
||||
$baseString .= $this->oauth_urlencode(implode('&', $encodedParams));
|
||||
|
|
|
@ -0,0 +1,187 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dropbox OAuth
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
|
||||
* @author Evert Pot (http://www.rooftopsolutions.nl/)
|
||||
* @license http://code.google.com/p/dropbox-php/wiki/License MIT
|
||||
*/
|
||||
|
||||
if (!class_exists('HTTP_OAuth_Consumer')) {
|
||||
|
||||
// We're going to try to load in manually
|
||||
include 'HTTP/OAuth/Consumer.php';
|
||||
|
||||
}
|
||||
if (!class_exists('HTTP_OAuth_Consumer'))
|
||||
throw new Dropbox_Exception('The HTTP_OAuth_Consumer class could not be found! Did you install the pear HTTP_OAUTH class?');
|
||||
|
||||
/**
|
||||
* This class is used to sign all requests to dropbox
|
||||
*
|
||||
* This classes use the PEAR HTTP_OAuth package. Make sure this is installed.
|
||||
*/
|
||||
class Dropbox_OAuth_PEAR extends Dropbox_OAuth {
|
||||
|
||||
/**
|
||||
* OAuth object
|
||||
*
|
||||
* @var OAuth
|
||||
*/
|
||||
protected $oAuth;
|
||||
|
||||
/**
|
||||
* OAuth consumer key
|
||||
*
|
||||
* We need to keep this around for later.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $consumerKey;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*/
|
||||
public function __construct($consumerKey, $consumerSecret)
|
||||
{
|
||||
$this->OAuth = new Dropbox_OAuth_Consumer_Dropbox($consumerKey, $consumerSecret);
|
||||
$this->consumerKey = $consumerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the request token and secret.
|
||||
*
|
||||
* The tokens can also be passed as an array into the first argument.
|
||||
* The array must have the elements token and token_secret.
|
||||
*
|
||||
* @param string|array $token
|
||||
* @param string $token_secret
|
||||
* @return void
|
||||
*/
|
||||
public function setToken($token, $token_secret = null) {
|
||||
|
||||
parent::setToken($token,$token_secret);
|
||||
$this->OAuth->setToken($this->oauth_token);
|
||||
$this->OAuth->setTokenSecret($this->oauth_token_secret);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param mixed $arguments
|
||||
* @param string $method
|
||||
* @param array $httpHeaders
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array())
|
||||
{
|
||||
$httpRequest = new HTTP_Request2(null,
|
||||
HTTP_Request2::METHOD_GET,
|
||||
array(
|
||||
'ssl_verify_peer' => false,
|
||||
'ssl_verify_host' => false
|
||||
)
|
||||
);
|
||||
|
||||
$consumerRequest = new HTTP_OAuth_Consumer_Request();
|
||||
$consumerRequest->accept($httpRequest);
|
||||
$consumerRequest->setUrl($uri);
|
||||
$consumerRequest->setMethod($method);
|
||||
$consumerRequest->setSecrets($this->OAuth->getSecrets());
|
||||
|
||||
$parameters = array(
|
||||
'oauth_consumer_key' => $this->consumerKey,
|
||||
'oauth_signature_method' => 'HMAC-SHA1',
|
||||
'oauth_token' => $this->oauth_token,
|
||||
);
|
||||
|
||||
|
||||
if (is_array($arguments)) {
|
||||
$parameters = array_merge($parameters,$arguments);
|
||||
} elseif (is_string($arguments)) {
|
||||
$consumerRequest->setBody($arguments);
|
||||
}
|
||||
$consumerRequest->setParameters($parameters);
|
||||
|
||||
|
||||
if (count($httpHeaders)) {
|
||||
foreach($httpHeaders as $k=>$v) {
|
||||
$consumerRequest->setHeader($k, $v);
|
||||
}
|
||||
}
|
||||
|
||||
$response = $consumerRequest->send();
|
||||
|
||||
switch($response->getStatus()) {
|
||||
|
||||
// Not modified
|
||||
case 304 :
|
||||
return array(
|
||||
'httpStatus' => 304,
|
||||
'body' => null,
|
||||
);
|
||||
break;
|
||||
case 400 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
|
||||
case 401 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.');
|
||||
case 403 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
|
||||
case 404 :
|
||||
throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
|
||||
case 405 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
|
||||
case 500 :
|
||||
throw new Dropbox_Exception_Forbidden('Server error. ' . $e->getMessage());
|
||||
case 503 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.');
|
||||
case 507 :
|
||||
throw new Dropbox_Exception_OverQuota('This dropbox is full');
|
||||
|
||||
}
|
||||
|
||||
return array(
|
||||
'httpStatus' => $response->getStatus(),
|
||||
'body' => $response->getBody()
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth request token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getRequestToken() {
|
||||
|
||||
$this->OAuth->getRequestToken(self::URI_REQUEST_TOKEN);
|
||||
$this->setToken($this->OAuth->getToken(), $this->OAuth->getTokenSecret());
|
||||
return $this->getToken();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth access tokens.
|
||||
*
|
||||
* This method requires the 'unauthorized' request tokens
|
||||
* and, if successful will set the authorized request tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAccessToken() {
|
||||
|
||||
$this->OAuth->getAccessToken(self::URI_ACCESS_TOKEN);
|
||||
$this->setToken($this->OAuth->getToken(), $this->OAuth->getTokenSecret());
|
||||
return $this->getToken();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,157 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dropbox OAuth
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
|
||||
* @author Evert Pot (http://www.rooftopsolutions.nl/)
|
||||
* @license http://code.google.com/p/dropbox-php/wiki/License MIT
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* This class is used to sign all requests to dropbox.
|
||||
*
|
||||
* This specific class uses the PHP OAuth extension
|
||||
*/
|
||||
class Dropbox_OAuth_PHP extends Dropbox_OAuth {
|
||||
|
||||
/**
|
||||
* OAuth object
|
||||
*
|
||||
* @var OAuth
|
||||
*/
|
||||
protected $oAuth;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*/
|
||||
public function __construct($consumerKey, $consumerSecret) {
|
||||
|
||||
if (!class_exists('OAuth'))
|
||||
throw new Dropbox_Exception('The OAuth class could not be found! Did you install and enable the oauth extension?');
|
||||
|
||||
$this->OAuth = new OAuth($consumerKey, $consumerSecret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_URI);
|
||||
$this->OAuth->enableDebug();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the request token and secret.
|
||||
*
|
||||
* The tokens can also be passed as an array into the first argument.
|
||||
* The array must have the elements token and token_secret.
|
||||
*
|
||||
* @param string|array $token
|
||||
* @param string $token_secret
|
||||
* @return void
|
||||
*/
|
||||
public function setToken($token, $token_secret = null) {
|
||||
|
||||
parent::setToken($token,$token_secret);
|
||||
$this->OAuth->setToken($this->oauth_token, $this->oauth_token_secret);
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param mixed $arguments
|
||||
* @param string $method
|
||||
* @param array $httpHeaders
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
|
||||
|
||||
try {
|
||||
$this->OAuth->fetch($uri, $arguments, $method, $httpHeaders);
|
||||
$result = $this->OAuth->getLastResponse();
|
||||
$lastResponseInfo = $this->OAuth->getLastResponseInfo();
|
||||
return array(
|
||||
'httpStatus' => $lastResponseInfo['http_code'],
|
||||
'body' => $result,
|
||||
);
|
||||
} catch (OAuthException $e) {
|
||||
|
||||
$lastResponseInfo = $this->OAuth->getLastResponseInfo();
|
||||
switch($lastResponseInfo['http_code']) {
|
||||
|
||||
// Not modified
|
||||
case 304 :
|
||||
return array(
|
||||
'httpStatus' => 304,
|
||||
'body' => null,
|
||||
);
|
||||
break;
|
||||
case 400 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad input parameter. Error message should indicate which one and why.');
|
||||
case 401 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.');
|
||||
case 403 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. This could mean a bad OAuth request, or a file or folder already existing at the target location.');
|
||||
case 404 :
|
||||
throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found');
|
||||
case 405 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Request method not expected (generally should be GET or POST).');
|
||||
case 500 :
|
||||
throw new Dropbox_Exception_Forbidden('Server error. ' . $e->getMessage());
|
||||
case 503 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden. Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.');
|
||||
case 507 :
|
||||
throw new Dropbox_Exception_OverQuota('This dropbox is full');
|
||||
default:
|
||||
// rethrowing
|
||||
throw $e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth request token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getRequestToken() {
|
||||
|
||||
try {
|
||||
|
||||
$tokens = $this->OAuth->getRequestToken(self::URI_REQUEST_TOKEN);
|
||||
$this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
|
||||
return $this->getToken();
|
||||
|
||||
} catch (OAuthException $e) {
|
||||
|
||||
throw new Dropbox_Exception_RequestToken('We were unable to fetch request tokens. This likely means that your consumer key and/or secret are incorrect.',0,$e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Requests the OAuth access tokens.
|
||||
*
|
||||
* This method requires the 'unauthorized' request tokens
|
||||
* and, if successful will set the authorized request tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAccessToken() {
|
||||
|
||||
$uri = self::URI_ACCESS_TOKEN;
|
||||
$tokens = $this->OAuth->getAccessToken($uri);
|
||||
$this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
|
||||
return $this->getToken();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,223 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dropbox OAuth
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2010 Stefan Motz
|
||||
* @author Stefan Motz (http://www.multimediamotz.de/)
|
||||
* @license MIT
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class is used to sign all requests to dropbox.
|
||||
*
|
||||
* This specific class uses WordPress WP_Http to authenticate.
|
||||
*/
|
||||
class Dropbox_OAuth_Wordpress extends Dropbox_OAuth {
|
||||
|
||||
/**
|
||||
*
|
||||
* @var string ConsumerKey
|
||||
*/
|
||||
protected $consumerKey = null;
|
||||
/**
|
||||
*
|
||||
* @var string ConsumerSecret
|
||||
*/
|
||||
protected $consumerSecret = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*/
|
||||
public function __construct($consumerKey, $consumerSecret) {
|
||||
if (!(defined('ABSPATH') && defined('WPINC')))
|
||||
throw new Dropbox_Exception('The Wordpress OAuth class is available within a wordpress context only!');
|
||||
if (!class_exists('WP_Http')) {
|
||||
include_once( ABSPATH . WPINC . '/class-http.php' );
|
||||
}
|
||||
|
||||
$this->consumerKey = $consumerKey;
|
||||
$this->consumerSecret = $consumerSecret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param mixed $arguments
|
||||
* @param string $method
|
||||
* @param array $httpHeaders
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
|
||||
|
||||
$requestParams = array();
|
||||
|
||||
$requestParams['method'] = $method;
|
||||
$oAuthHeader = $this->getOAuthHeader($uri, $arguments, $method);
|
||||
$requestParams['headers'] = array_merge($httpHeaders, $oAuthHeader);
|
||||
|
||||
// arguments will be passed to uri for GET, to body for POST etc.
|
||||
if ($method == 'GET') {
|
||||
$uri .= '?' . http_build_query($arguments);
|
||||
} else {
|
||||
if (count($arguments)) {
|
||||
$requestParams['body'] = $arguments;
|
||||
}
|
||||
}
|
||||
|
||||
$request = new WP_Http;
|
||||
|
||||
//$uri = str_replace('api.dropbox.com', 'localhost:12346', $uri);
|
||||
|
||||
$result = $request->request($uri, $requestParams);
|
||||
|
||||
return array(
|
||||
'httpStatus' => $result['response']['code'],
|
||||
'body' => $result['body'],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns named array with oauth parameters for further use
|
||||
* @return array Array with oauth_ parameters
|
||||
*/
|
||||
private function getOAuthBaseParams() {
|
||||
$params['oauth_version'] = '1.0';
|
||||
$params['oauth_signature_method'] = 'HMAC-SHA1';
|
||||
|
||||
$params['oauth_consumer_key'] = $this->consumerKey;
|
||||
$tokens = $this->getToken();
|
||||
if (isset($tokens['token']) && $tokens['token']) {
|
||||
$params['oauth_token'] = $tokens['token'];
|
||||
}
|
||||
$params['oauth_timestamp'] = time();
|
||||
$params['oauth_nonce'] = md5(microtime() . mt_rand());
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates valid Authorization header for OAuth, based on URI and Params
|
||||
*
|
||||
* @param string $uri
|
||||
* @param array $params
|
||||
* @param string $method GET or POST, standard is GET
|
||||
* @param array $oAuthParams optional, pass your own oauth_params here
|
||||
* @return array Array for request's headers section like
|
||||
* array('Authorization' => 'OAuth ...');
|
||||
*/
|
||||
private function getOAuthHeader($uri, $params, $method = 'GET', $oAuthParams = null) {
|
||||
$oAuthParams = $oAuthParams ? $oAuthParams : $this->getOAuthBaseParams();
|
||||
|
||||
// create baseString to encode for the sent parameters
|
||||
$baseString = $method . '&';
|
||||
$baseString .= $this->oauth_urlencode($uri) . "&";
|
||||
|
||||
// OAuth header does not include GET-Parameters
|
||||
$signatureParams = array_merge($params, $oAuthParams);
|
||||
|
||||
// sorting the parameters
|
||||
ksort($signatureParams);
|
||||
|
||||
$encodedParams = array();
|
||||
foreach ($signatureParams as $key => $value) {
|
||||
$encodedParams[] = $this->oauth_urlencode($key) . '=' . $this->oauth_urlencode($value);
|
||||
}
|
||||
|
||||
$baseString .= $this->oauth_urlencode(implode('&', $encodedParams));
|
||||
|
||||
// encode the signature
|
||||
$tokens = $this->getToken();
|
||||
$hash = $this->hash_hmac_sha1($this->consumerSecret.'&'.$tokens['token_secret'], $baseString);
|
||||
$signature = base64_encode($hash);
|
||||
|
||||
// add signature to oAuthParams
|
||||
$oAuthParams['oauth_signature'] = $signature;
|
||||
|
||||
$oAuthEncoded = array();
|
||||
foreach ($oAuthParams as $key => $value) {
|
||||
$oAuthEncoded[] = $key . '="' . $this->oauth_urlencode($value) . '"';
|
||||
}
|
||||
|
||||
return array('Authorization' => 'OAuth ' . implode(', ', $oAuthEncoded));
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth request token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getRequestToken() {
|
||||
$result = $this->fetch(self::URI_REQUEST_TOKEN, array(), 'POST');
|
||||
if ($result['httpStatus'] == "200") {
|
||||
$tokens = array();
|
||||
parse_str($result['body'], $tokens);
|
||||
$this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
|
||||
return $this->getToken();
|
||||
} else {
|
||||
throw new Dropbox_Exception_RequestToken('We were unable to fetch request tokens. This likely means that your consumer key and/or secret are incorrect.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth access tokens.
|
||||
*
|
||||
* This method requires the 'unauthorized' request tokens
|
||||
* and, if successful will set the authorized request tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAccessToken() {
|
||||
$result = $this->fetch(self::URI_ACCESS_TOKEN, array(), 'POST');
|
||||
if ($result['httpStatus'] == "200") {
|
||||
$tokens = array();
|
||||
parse_str($result['body'], $tokens);
|
||||
$this->setToken($tokens['oauth_token'], $tokens['oauth_token_secret']);
|
||||
return $this->getToken();
|
||||
} else {
|
||||
throw new Dropbox_Exception_RequestToken('We were unable to fetch request tokens. This likely means that your consumer key and/or secret are incorrect.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to properly urlencode parameters.
|
||||
* See http://php.net/manual/en/function.oauth-urlencode.php
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
private function oauth_urlencode($string) {
|
||||
return str_replace('%E7', '~', rawurlencode($string));
|
||||
}
|
||||
|
||||
/**
|
||||
* Hash function for hmac_sha1; uses native function if available.
|
||||
*
|
||||
* @param string $key
|
||||
* @param string $data
|
||||
* @return string
|
||||
*/
|
||||
private function hash_hmac_sha1($key, $data) {
|
||||
if (function_exists('hash_hmac') && in_array('sha1', hash_algos())) {
|
||||
return hash_hmac('sha1', $data, $key, true);
|
||||
} else {
|
||||
$blocksize = 64;
|
||||
$hashfunc = 'sha1';
|
||||
if (strlen($key) > $blocksize) {
|
||||
$key = pack('H*', $hashfunc($key));
|
||||
}
|
||||
|
||||
$key = str_pad($key, $blocksize, chr(0x00));
|
||||
$ipad = str_repeat(chr(0x36), $blocksize);
|
||||
$opad = str_repeat(chr(0x5c), $blocksize);
|
||||
$hash = pack('H*', $hashfunc(( $key ^ $opad ) . pack('H*', $hashfunc(($key ^ $ipad) . $data))));
|
||||
|
||||
return $hash;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,244 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dropbox OAuth
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
|
||||
* @author Michael Johansen <michael@taskcamp.com>
|
||||
* @license http://code.google.com/p/dropbox-php/wiki/License MIT
|
||||
*/
|
||||
|
||||
/**
|
||||
* This class is used to sign all requests to dropbox
|
||||
*
|
||||
* This classes use the Zend_Oauth package.
|
||||
*/
|
||||
class Dropbox_OAuth_Zend extends Dropbox_OAuth {
|
||||
|
||||
/**
|
||||
* OAuth object
|
||||
*
|
||||
* @var Zend_Oauth_Consumer
|
||||
*/
|
||||
protected $oAuth;
|
||||
/**
|
||||
* OAuth consumer key
|
||||
*
|
||||
* We need to keep this around for later.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $consumerKey;
|
||||
/**
|
||||
*
|
||||
* @var Zend_Oauth_Token
|
||||
*/
|
||||
protected $zend_oauth_token;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*/
|
||||
public function __construct($consumerKey, $consumerSecret) {
|
||||
if (!class_exists('Zend_Oauth_Consumer')) {
|
||||
// We're going to try to load in manually
|
||||
include 'Zend/Oauth/Consumer.php';
|
||||
}
|
||||
if (!class_exists('Zend_Oauth_Consumer'))
|
||||
throw new Dropbox_Exception('The Zend_Oauth_Consumer class could not be found!');
|
||||
$this->OAuth = new Zend_Oauth_Consumer(array(
|
||||
"consumerKey" => $consumerKey,
|
||||
"consumerSecret" => $consumerSecret,
|
||||
"requestTokenUrl" => self::URI_REQUEST_TOKEN,
|
||||
"accessTokenUrl" => self::URI_ACCESS_TOKEN,
|
||||
"authorizeUrl" => self::URI_AUTHORIZE,
|
||||
"signatureMethod" => "HMAC-SHA1",
|
||||
));
|
||||
$this->consumerKey = $consumerKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the request token and secret.
|
||||
*
|
||||
* The tokens can also be passed as an array into the first argument.
|
||||
* The array must have the elements token and token_secret.
|
||||
*
|
||||
* @param string|array $token
|
||||
* @param string $token_secret
|
||||
* @return void
|
||||
*/
|
||||
public function setToken($token, $token_secret = null) {
|
||||
if (is_a($token, "Zend_Oauth_Token")) {
|
||||
if (is_a($token, "Zend_Oauth_Token_Access")) {
|
||||
$this->OAuth->setToken($token);
|
||||
}
|
||||
$this->zend_oauth_token = $token;
|
||||
return parent::setToken($token->getToken(), $token->getTokenSecret());
|
||||
} elseif (is_string($token) && is_null($token_secret)) {
|
||||
return $this->setToken(unserialize($token));
|
||||
} elseif (isset($token['zend_oauth_token'])) {
|
||||
return $this->setToken(unserialize($token['zend_oauth_token']));
|
||||
} else {
|
||||
parent::setToken($token, $token_secret);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param mixed $arguments
|
||||
* @param string $method
|
||||
* @param array $httpHeaders
|
||||
* @return string
|
||||
*/
|
||||
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
|
||||
$token = $this->OAuth->getToken();
|
||||
if (!is_a($token, "Zend_Oauth_Token")) {
|
||||
if (is_a($this->zend_oauth_token, "Zend_Oauth_Token_Access")) {
|
||||
$token = $this->zend_oauth_token;
|
||||
} else {
|
||||
$token = new Zend_Oauth_Token_Access();
|
||||
$token->setToken($this->oauth_token);
|
||||
$token->setTokenSecret($this->oauth_token_secret);
|
||||
}
|
||||
}
|
||||
/* @var $token Zend_Oauth_Token_Access */
|
||||
$oauthOptions = array(
|
||||
'consumerKey' => $this->consumerKey,
|
||||
'signatureMethod' => "HMAC-SHA1",
|
||||
'consumerSecret' => $this->OAuth->getConsumerSecret(),
|
||||
);
|
||||
$config = array("timeout" => 15);
|
||||
|
||||
/* @var $consumerRequest Zend_Oauth_Client */
|
||||
$consumerRequest = $token->getHttpClient($oauthOptions);
|
||||
$consumerRequest->setMethod($method);
|
||||
if (is_array($arguments)) {
|
||||
$consumerRequest->setUri($uri);
|
||||
if ($method == "GET") {
|
||||
foreach ($arguments as $param => $value) {
|
||||
$consumerRequest->setParameterGet($param, $value);
|
||||
}
|
||||
} else {
|
||||
foreach ($arguments as $param => $value) {
|
||||
$consumerRequest->setParameterPost($param, $value);
|
||||
}
|
||||
}
|
||||
} elseif (is_string($arguments)) {
|
||||
preg_match("/\?file=(.*)$/i", $uri, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$uri = str_replace($matches[0], "", $uri);
|
||||
$filename = $matches[1];
|
||||
$uri = Zend_Uri::factory($uri);
|
||||
$uri->addReplaceQueryParameters(array("file" => $filename));
|
||||
$consumerRequest->setParameterGet("file", $filename);
|
||||
}
|
||||
$consumerRequest->setUri($uri);
|
||||
$consumerRequest->setRawData($arguments);
|
||||
} elseif (is_resource($arguments)) {
|
||||
$consumerRequest->setUri($uri);
|
||||
/** Placeholder for Oauth streaming support. */
|
||||
}
|
||||
if (count($httpHeaders)) {
|
||||
foreach ($httpHeaders as $k => $v) {
|
||||
$consumerRequest->setHeaders($k, $v);
|
||||
}
|
||||
}
|
||||
$response = $consumerRequest->request();
|
||||
$body = Zend_Json::decode($response->getBody());
|
||||
switch ($response->getStatus()) {
|
||||
// Not modified
|
||||
case 304 :
|
||||
return array(
|
||||
'httpStatus' => 304,
|
||||
'body' => null,
|
||||
);
|
||||
break;
|
||||
case 403 :
|
||||
throw new Dropbox_Exception_Forbidden('Forbidden.
|
||||
This could mean a bad OAuth request, or a file or folder already existing at the target location.
|
||||
' . $body["error"] . "\n");
|
||||
case 404 :
|
||||
throw new Dropbox_Exception_NotFound('Resource at uri: ' . $uri . ' could not be found. ' .
|
||||
$body["error"] . "\n");
|
||||
case 507 :
|
||||
throw new Dropbox_Exception_OverQuota('This dropbox is full. ' .
|
||||
$body["error"] . "\n");
|
||||
}
|
||||
|
||||
return array(
|
||||
'httpStatus' => $response->getStatus(),
|
||||
'body' => $response->getBody(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth request token.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getRequestToken() {
|
||||
$token = $this->OAuth->getRequestToken();
|
||||
$this->setToken($token);
|
||||
return $this->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests the OAuth access tokens.
|
||||
*
|
||||
* This method requires the 'unauthorized' request tokens
|
||||
* and, if successful will set the authorized request tokens.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAccessToken() {
|
||||
if (is_a($this->zend_oauth_token, "Zend_Oauth_Token_Request")) {
|
||||
$requestToken = $this->zend_oauth_token;
|
||||
} else {
|
||||
$requestToken = new Zend_Oauth_Token_Request();
|
||||
$requestToken->setToken($this->oauth_token);
|
||||
$requestToken->setTokenSecret($this->oauth_token_secret);
|
||||
}
|
||||
$token = $this->OAuth->getAccessToken($_GET, $requestToken);
|
||||
$this->setToken($token);
|
||||
return $this->getToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the oauth request tokens as an associative array.
|
||||
*
|
||||
* The array will contain the elements 'token' and 'token_secret' and the serialized
|
||||
* Zend_Oauth_Token object.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getToken() {
|
||||
//$token = $this->OAuth->getToken();
|
||||
//return serialize($token);
|
||||
return array(
|
||||
'token' => $this->oauth_token,
|
||||
'token_secret' => $this->oauth_token_secret,
|
||||
'zend_oauth_token' => serialize($this->zend_oauth_token),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authorization url
|
||||
*
|
||||
* Overloading Dropbox_OAuth to use the built in functions in Zend_Oauth
|
||||
*
|
||||
* @param string $callBack Specify a callback url to automatically redirect the user back
|
||||
* @return string
|
||||
*/
|
||||
public function getAuthorizeUrl($callBack = null) {
|
||||
if ($callBack)
|
||||
$this->OAuth->setCallbackUrl($callBack);
|
||||
return $this->OAuth->getRedirectUrl();
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,31 +0,0 @@
|
|||
Dropbox-php
|
||||
===========
|
||||
|
||||
This PHP library allows you to easily integrate dropbox with PHP.
|
||||
|
||||
The following PHP extension is required:
|
||||
|
||||
* json
|
||||
|
||||
The library makes use of OAuth. At the moment you can use either of these libraries:
|
||||
|
||||
[PHP OAuth extension](http://pecl.php.net/package/oauth)
|
||||
[PEAR's HTTP_OAUTH package](http://pear.php.net/package/http_oauth)
|
||||
|
||||
The extension is recommended, but if you can't install php extensions you should go for the pear package.
|
||||
Installing
|
||||
----------
|
||||
|
||||
pear channel-discover pear.dropbox-php.com
|
||||
pear install dropbox-php/Dropbox-alpha
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
Check out the [documentation](http://www.dropbox-php.com/docs).
|
||||
|
||||
Questions?
|
||||
----------
|
||||
|
||||
[Dropbox-php Mailing list](http://groups.google.com/group/dropbox-php)
|
||||
[Official Dropbox developer forum](http://forums.dropbox.com/forum.php?id=5)
|
||||
|
Loading…
Reference in New Issue