Bump Dropbox library to newest upstream version
This commit is contained in:
parent
2f8296875f
commit
418f4e1a90
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dropbox API class
|
||||
*
|
||||
* @package Dropbox
|
||||
* Dropbox API class
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
|
||||
* @author Evert Pot (http://www.rooftopsolutions.nl/)
|
||||
* @author Evert Pot (http://www.rooftopsolutions.nl/)
|
||||
* @license http://code.google.com/p/dropbox-php/wiki/License MIT
|
||||
*/
|
||||
class Dropbox_API {
|
||||
|
@ -19,43 +19,58 @@ class Dropbox_API {
|
|||
* Dropbox root-path
|
||||
*/
|
||||
const ROOT_DROPBOX = 'dropbox';
|
||||
|
||||
|
||||
/**
|
||||
* API URl
|
||||
*/
|
||||
protected $api_url = 'https://api.dropbox.com/1/';
|
||||
|
||||
|
||||
/**
|
||||
* Content API URl
|
||||
*/
|
||||
protected $api_content_url = 'https://api-content.dropbox.com/1/';
|
||||
|
||||
/**
|
||||
* OAuth object
|
||||
*
|
||||
* OAuth object
|
||||
*
|
||||
* @var Dropbox_OAuth
|
||||
*/
|
||||
protected $oauth;
|
||||
|
||||
|
||||
/**
|
||||
* Default root-path, this will most likely be 'sandbox' or 'dropbox'
|
||||
*
|
||||
* @var string
|
||||
* Default root-path, this will most likely be 'sandbox' or 'dropbox'
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $root;
|
||||
protected $useSSL;
|
||||
|
||||
/**
|
||||
* PHP is Safe Mode
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $isSafeMode;
|
||||
|
||||
/**
|
||||
* Chunked file size limit
|
||||
*
|
||||
* @var unknown
|
||||
*/
|
||||
public $chunkSize = 20971520; //20MB
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Constructor
|
||||
*
|
||||
* @param Dropbox_OAuth Dropbox_Auth object
|
||||
* @param string $root default root path (sandbox or dropbox)
|
||||
* @param string $root default root path (sandbox or dropbox)
|
||||
*/
|
||||
public function __construct(Dropbox_OAuth $oauth, $root = self::ROOT_DROPBOX, $useSSL = true) {
|
||||
|
||||
$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');
|
||||
|
@ -64,9 +79,9 @@ class Dropbox_API {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns information about the current dropbox account
|
||||
*
|
||||
* @return stdclass
|
||||
* Returns information about the current dropbox account
|
||||
*
|
||||
* @return stdclass
|
||||
*/
|
||||
public function getAccountInfo() {
|
||||
|
||||
|
@ -76,11 +91,11 @@ class Dropbox_API {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns a file's contents
|
||||
*
|
||||
* @param string $path path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return string
|
||||
* Returns a file's contents
|
||||
*
|
||||
* @param string $path path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return string
|
||||
*/
|
||||
public function getFile($path = '', $root = null) {
|
||||
|
||||
|
@ -94,19 +109,58 @@ class Dropbox_API {
|
|||
/**
|
||||
* Uploads a new file
|
||||
*
|
||||
* @param string $path Target path (including filename)
|
||||
* @param string $file Either a path to a file or a stream resource
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return bool
|
||||
* @param string $path Target path (including filename)
|
||||
* @param string $file Either a path to a file or a stream resource
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return bool
|
||||
*/
|
||||
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,27 +168,141 @@ 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');
|
||||
}
|
||||
$result=$this->multipartFetch($this->api_content_url . 'files/' .
|
||||
$root . '/' . trim($directory,'/'), $file, $filename);
|
||||
|
||||
if(!isset($result["httpStatus"]) || $result["httpStatus"] != 200)
|
||||
if (!$this->isSafeMode) {
|
||||
set_time_limit(600);
|
||||
}
|
||||
$result=$this->multipartFetch($this->api_content_url . 'files/' .
|
||||
$root . '/' . trim($directory,'/'), $file, $filename);
|
||||
|
||||
if(!isset($result["httpStatus"]) || $result["httpStatus"] != 200)
|
||||
throw new Dropbox_Exception("Uploading file to Dropbox failed");
|
||||
|
||||
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');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies a file or directory from one location to another
|
||||
* 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
|
||||
*
|
||||
* This method returns the file information of the newly created file.
|
||||
*
|
||||
* @param string $from source path
|
||||
* @param string $to destination path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
* @param string $from source path
|
||||
* @param string $to destination path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
*/
|
||||
public function copy($from, $to, $root = null) {
|
||||
|
||||
|
@ -146,20 +314,20 @@ class Dropbox_API {
|
|||
}
|
||||
|
||||
/**
|
||||
* Creates a new folder
|
||||
* Creates a new folder
|
||||
*
|
||||
* This method returns the information from the newly created directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
* @param string $path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
*/
|
||||
public function createFolder($path, $root = null) {
|
||||
|
||||
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);
|
||||
|
@ -170,10 +338,10 @@ class Dropbox_API {
|
|||
* Deletes a file or folder.
|
||||
*
|
||||
* This method will return the metadata information from the deleted file or folder, if successful.
|
||||
*
|
||||
* @param string $path Path to new folder
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return array
|
||||
*
|
||||
* @param string $path Path to new folder
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return array
|
||||
*/
|
||||
public function delete($path, $root = null) {
|
||||
|
||||
|
@ -184,14 +352,14 @@ class Dropbox_API {
|
|||
}
|
||||
|
||||
/**
|
||||
* Moves a file or directory to a new location
|
||||
* Moves a file or directory to a new location
|
||||
*
|
||||
* This method returns the information from the newly created directory
|
||||
*
|
||||
* @param mixed $from Source path
|
||||
* @param mixed $from Source path
|
||||
* @param mixed $to destination path
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return stdclass
|
||||
*/
|
||||
public function move($from, $to, $root = null) {
|
||||
|
||||
|
@ -204,13 +372,13 @@ class Dropbox_API {
|
|||
|
||||
/**
|
||||
* Returns file and directory information
|
||||
*
|
||||
* @param string $path Path to receive information from
|
||||
*
|
||||
* @param string $path Path to receive information from
|
||||
* @param bool $list When set to true, this method returns information from all files in a directory. When set to false it will only return infromation from the specified directory.
|
||||
* @param string $hash If a hash is supplied, this method simply returns true if nothing has changed since the last request. Good for caching.
|
||||
* @param int $fileLimit Maximum number of file-information to receive
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return array|true
|
||||
* @param int $fileLimit Maximum number of file-information to receive
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return array|true
|
||||
*/
|
||||
public function getMetaData($path, $list = true, $hash = null, $fileLimit = null, $root = null) {
|
||||
|
||||
|
@ -220,20 +388,20 @@ class Dropbox_API {
|
|||
'list' => $list,
|
||||
);
|
||||
|
||||
if (!is_null($hash)) $args['hash'] = $hash;
|
||||
if (!is_null($fileLimit)) $args['file_limit'] = $fileLimit;
|
||||
if (!is_null($hash)) $args['hash'] = $hash;
|
||||
if (!is_null($fileLimit)) $args['file_limit'] = $fileLimit;
|
||||
|
||||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
$response = $this->oauth->fetch($this->api_url . 'metadata/' . $root . '/' . ltrim($path,'/'), $args);
|
||||
|
||||
/* 304 is not modified */
|
||||
if ($response['httpStatus']==304) {
|
||||
return true;
|
||||
return true;
|
||||
} else {
|
||||
return json_decode($response['body'],true);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A way of letting you keep up with changes to files and folders in a user's Dropbox. You can periodically call /delta to get a list of "delta entries", which are instructions on how to update your local state to match the server's state.
|
||||
|
@ -244,21 +412,21 @@ class Dropbox_API {
|
|||
* @return stdclass
|
||||
*/
|
||||
public function delta($cursor) {
|
||||
|
||||
|
||||
$arg['cursor'] = $cursor;
|
||||
|
||||
|
||||
$response = $this->oauth->fetch($this->api_url . 'delta', $arg, 'POST');
|
||||
return json_decode($response['body'],true);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a thumbnail (as a string) for a file path.
|
||||
*
|
||||
* @param string $path Path to file
|
||||
* @param string $size small, medium or large
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return string
|
||||
* Returns a thumbnail (as a string) for a file path.
|
||||
*
|
||||
* @param string $path Path to file
|
||||
* @param string $size small, medium or large
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @return string
|
||||
*/
|
||||
public function getThumbnail($path, $size = 'small', $root = null) {
|
||||
|
||||
|
@ -271,11 +439,11 @@ class Dropbox_API {
|
|||
}
|
||||
|
||||
/**
|
||||
* This method is used to generate multipart POST requests for file upload
|
||||
*
|
||||
* @param string $uri
|
||||
* @param array $arguments
|
||||
* @return bool
|
||||
* This method is used to generate multipart POST requests for file upload
|
||||
*
|
||||
* @param string $uri
|
||||
* @param array $arguments
|
||||
* @return bool
|
||||
*/
|
||||
protected function multipartFetch($uri, $file, $filename) {
|
||||
|
||||
|
@ -295,22 +463,22 @@ class Dropbox_API {
|
|||
$body.="--" . $boundary . "--";
|
||||
|
||||
// Dropbox requires the filename to also be part of the regular arguments, so it becomes
|
||||
// part of the signature.
|
||||
// part of the signature.
|
||||
$uri.='?file=' . $filename;
|
||||
|
||||
return $this->oauth->fetch($uri, $body, 'POST', $headers);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Search
|
||||
*
|
||||
* Returns metadata for all files and folders that match the search query.
|
||||
*
|
||||
* @added by: diszo.sasil
|
||||
* @added by: diszo.sasil
|
||||
*
|
||||
* @param string $query
|
||||
* @param string $query
|
||||
* @param string $root Use this to override the default root path (sandbox/dropbox)
|
||||
* @param string $path
|
||||
* @return array
|
||||
|
@ -326,17 +494,19 @@ class Dropbox_API {
|
|||
|
||||
/**
|
||||
* Creates and returns a shareable link to files or folders.
|
||||
*
|
||||
*
|
||||
* 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);
|
||||
|
||||
}
|
||||
|
@ -357,7 +527,7 @@ class Dropbox_API {
|
|||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
$response = $this->oauth->fetch($this->api_url. 'media/'. $root . '/' . ltrim($path, '/'), array(), 'POST');
|
||||
return json_decode($response['body'],true);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -373,8 +543,8 @@ class Dropbox_API {
|
|||
$path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path));
|
||||
$response = $this->oauth->fetch($this->api_url. 'copy_ref/'. $root . '/' . ltrim($path, '/'));
|
||||
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,7 +61,34 @@ 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.
|
||||
*
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
|
||||
/**
|
||||
* Dropbox OAuth
|
||||
*
|
||||
* @package Dropbox
|
||||
*
|
||||
* @package Dropbox
|
||||
* @copyright Copyright (C) 2011 Daniel Huesken
|
||||
* @author Daniel Huesken (http://www.danielhuesken.de/)
|
||||
* @license MIT
|
||||
|
@ -31,61 +31,71 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
* @var string ProzessCallBack
|
||||
*/
|
||||
public $ProgressFunction = false;
|
||||
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*
|
||||
* @param string $consumerKey
|
||||
* @param string $consumerSecret
|
||||
*/
|
||||
public function __construct($consumerKey, $consumerSecret) {
|
||||
if (!function_exists('curl_exec'))
|
||||
if (!function_exists('curl_exec'))
|
||||
throw new Dropbox_Exception('The PHP curl functions not available!');
|
||||
|
||||
$this->consumerKey = $consumerKey;
|
||||
$this->consumerSecret = $consumerSecret;
|
||||
$this->putSupported = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a secured oauth url and returns the response body.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param mixed $arguments
|
||||
* @param string $method
|
||||
* @param array $httpHeaders
|
||||
* @return string
|
||||
* 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()) {
|
||||
|
||||
public function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()) {
|
||||
|
||||
$uri=str_replace('http://', 'https://', $uri); // all https, upload makes problems if not
|
||||
if (is_string($arguments) and strtoupper($method) == 'POST') {
|
||||
preg_match("/\?file=(.*)$/i", $uri, $matches);
|
||||
preg_match("/\?file=(.*)$/i", $uri, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$uri = str_replace($matches[0], "", $uri);
|
||||
$filename = $matches[1];
|
||||
$uri = str_replace($matches[0], "", $uri);
|
||||
$filename = rawurldecode(str_replace('%7E', '~', $matches[1]));
|
||||
$httpHeaders=array_merge($httpHeaders,$this->getOAuthHeader($uri, array("file" => $filename), $method));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$httpHeaders=array_merge($httpHeaders,$this->getOAuthHeader($uri, $arguments, $method));
|
||||
}
|
||||
$ch = curl_init();
|
||||
$ch = curl_init();
|
||||
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_SSL_VERIFYPEER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||||
// curl_setopt($ch, CURLOPT_CAINFO, "rootca");
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 600);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
|
||||
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) {
|
||||
|
@ -104,13 +114,22 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
$error=curl_error($ch);
|
||||
$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,25 +138,34 @@ 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(
|
||||
'body' => $response,
|
||||
'httpStatus' => $status
|
||||
);
|
||||
'httpStatus' => $status
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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));
|
||||
|
@ -207,7 +235,7 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
/**
|
||||
* Requests the OAuth request token.
|
||||
*
|
||||
* @return void
|
||||
* @return void
|
||||
*/
|
||||
public function getRequestToken() {
|
||||
$result = $this->fetch(self::URI_REQUEST_TOKEN, array(), 'POST');
|
||||
|
@ -226,8 +254,8 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
*
|
||||
* This method requires the 'unauthorized' request tokens
|
||||
* and, if successful will set the authorized request tokens.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function getAccessToken() {
|
||||
$result = $this->fetch(self::URI_ACCESS_TOKEN, array(), 'POST');
|
||||
|
@ -277,6 +305,6 @@ class Dropbox_OAuth_Curl extends Dropbox_OAuth {
|
|||
return $hash;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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