From: Michael Gapczynski Date: Sat, 9 Jun 2012 19:02:26 +0000 (-0400) Subject: Add Dropbox storage backend X-Git-Tag: v4.5.0beta1~74^2~422^2~7 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ab41b57f6266200e1b1c94d45487975163deb560;p=nextcloud-server.git Add Dropbox storage backend --- diff --git a/3rdparty/Dropbox/API.php b/3rdparty/Dropbox/API.php new file mode 100644 index 00000000000..cff6c35c7fb --- /dev/null +++ b/3rdparty/Dropbox/API.php @@ -0,0 +1,380 @@ +oauth = $oauth; + $this->root = $root; + $this->useSSL = $useSSL; + if (!$this->useSSL) + { + throw new Dropbox_Exception('Dropbox REST API now requires that all requests use SSL'); + } + + } + + /** + * Returns information about the current dropbox account + * + * @return stdclass + */ + public function getAccountInfo() { + + $data = $this->oauth->fetch($this->api_url . 'account/info'); + return json_decode($data['body'],true); + + } + + /** + * 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) { + + if (is_null($root)) $root = $this->root; + $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path)); + $result = $this->oauth->fetch($this->api_content_url . 'files/' . $root . '/' . ltrim($path,'/')); + return $result['body']; + + } + + /** + * 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 + */ + public function putFile($path, $file, $root = null) { + + $directory = dirname($path); + $filename = basename($path); + + if($directory==='.') $directory = ''; + $directory = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($directory)); +// $filename = str_replace('~', '%7E', rawurlencode($filename)); + if (is_null($root)) $root = $this->root; + + if (is_string($file)) { + + $file = fopen($file,'rb'); + + } elseif (!is_resource($file)) { + throw new Dropbox_Exception('File must be a file-resource or a string'); + } + $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; + } + + + /** + * 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 + */ + public function copy($from, $to, $root = null) { + + if (is_null($root)) $root = $this->root; + $response = $this->oauth->fetch($this->api_url . 'fileops/copy', array('from_path' => $from, 'to_path' => $to, 'root' => $root)); + + return json_decode($response['body'],true); + + } + + /** + * 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 + */ + public function createFolder($path, $root = null) { + + if (is_null($root)) $root = $this->root; + + // Making sure the path starts with a / +// $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); + + } + + /** + * 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 + */ + public function delete($path, $root = null) { + + if (is_null($root)) $root = $this->root; + $response = $this->oauth->fetch($this->api_url . 'fileops/delete', array('path' => $path, 'root' => $root)); + return json_decode($response['body']); + + } + + /** + * 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 $to destination path + * @param string $root Use this to override the default root path (sandbox/dropbox) + * @return stdclass + */ + public function move($from, $to, $root = null) { + + if (is_null($root)) $root = $this->root; + $response = $this->oauth->fetch($this->api_url . 'fileops/move', array('from_path' => rawurldecode($from), 'to_path' => rawurldecode($to), 'root' => $root)); + + return json_decode($response['body'],true); + + } + + /** + * Returns file and directory information + * + * @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 + */ + public function getMetaData($path, $list = true, $hash = null, $fileLimit = null, $root = null) { + + if (is_null($root)) $root = $this->root; + + $args = array( + 'list' => $list, + ); + + 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; + } 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. + * + * This method returns the information from the newly created directory + * + * @param string $cursor A string that is used to keep track of your current state. On the next call pass in this value to return delta entries that have been recorded since the cursor was returned. + * @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 + */ + public function getThumbnail($path, $size = 'small', $root = null) { + + if (is_null($root)) $root = $this->root; + $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path)); + $response = $this->oauth->fetch($this->api_content_url . 'thumbnails/' . $root . '/' . ltrim($path,'/'),array('size' => $size)); + + return $response['body']; + + } + + /** + * 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) { + + /* random string */ + $boundary = 'R50hrfBj5JYyfR3vF3wR96GPCC9Fd2q2pVMERvEaOE3D8LZTgLLbRpNwXek3'; + + $headers = array( + 'Content-Type' => 'multipart/form-data; boundary=' . $boundary, + ); + + $body="--" . $boundary . "\r\n"; + $body.="Content-Disposition: form-data; name=file; filename=".rawurldecode($filename)."\r\n"; + $body.="Content-type: application/octet-stream\r\n"; + $body.="\r\n"; + $body.=stream_get_contents($file); + $body.="\r\n"; + $body.="--" . $boundary . "--"; + + // Dropbox requires the filename to also be part of the regular arguments, so it becomes + // 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 + * + * @param string $query + * @param string $root Use this to override the default root path (sandbox/dropbox) + * @param string $path + * @return array + */ + public function search($query = '', $root = null, $path = ''){ + if (is_null($root)) $root = $this->root; + if(!empty($path)){ + $path = str_replace(array('%2F','~'), array('/','%7E'), rawurlencode($path)); + } + $response = $this->oauth->fetch($this->api_url . 'search/' . $root . '/' . ltrim($path,'/'),array('query' => $query)); + return json_decode($response['body'],true); + } + + /** + * 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 + */ + public function share($path, $root = null) { + 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'); + return json_decode($response['body'],true); + + } + + /** + * Returns a link directly to a file. + * Similar to /shares. The difference is that this bypasses the Dropbox webserver, used to provide a preview of the file, so that you can effectively stream the contents of your media. + * + * Note: The /media link expires after four hours, allotting enough time to stream files, but not enough to leave a connection open indefinitely. + * + * @param type $path + * @param type $root + * @return type + */ + public function media($path, $root = null) { + + if (is_null($root)) $root = $this->root; + $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); + + } + + /** + * Creates and returns a copy_ref to a file. This reference string can be used to copy that file to another user's Dropbox by passing it in as the from_copy_ref parameter on /fileops/copy. + * + * @param type $path + * @param type $root + * @return type + */ + public function copy_ref($path, $root = null) { + + if (is_null($root)) $root = $this->root; + $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); + + } + + +} diff --git a/3rdparty/Dropbox/Exception.php b/3rdparty/Dropbox/Exception.php new file mode 100644 index 00000000000..50cbc4c7915 --- /dev/null +++ b/3rdparty/Dropbox/Exception.php @@ -0,0 +1,15 @@ +oauth_token = $token['token']; + $this->oauth_token_secret = $token['token_secret']; + } else { + $this->oauth_token = $token; + $this->oauth_token_secret = $token_secret; + } + + } + + /** + * Returns the oauth request tokens as an associative array. + * + * The array will contain the elements 'token' and 'token_secret'. + * + * @return array + */ + public function getToken() { + + return array( + 'token' => $this->oauth_token, + 'token_secret' => $this->oauth_token_secret, + ); + + } + + /** + * Returns the authorization url + * + * @param string $callBack Specify a callback url to automatically redirect the user back + * @return string + */ + public function getAuthorizeUrl($callBack = null) { + + // Building the redirect uri + $token = $this->getToken(); + $uri = self::URI_AUTHORIZE . '?oauth_token=' . $token['token']; + if ($callBack) $uri.='&oauth_callback=' . $callBack; + return $uri; + } + + /** + * 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 abstract function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array()); + + /** + * Requests the OAuth request token. + * + * @return array + */ + abstract public function getRequestToken(); + + /** + * Requests the OAuth access tokens. + * + * @return array + */ + abstract public function getAccessToken(); + +} diff --git a/3rdparty/Dropbox/OAuth/Consumer/Dropbox.php b/3rdparty/Dropbox/OAuth/Consumer/Dropbox.php new file mode 100644 index 00000000000..204a659de00 --- /dev/null +++ b/3rdparty/Dropbox/OAuth/Consumer/Dropbox.php @@ -0,0 +1,37 @@ +consumerRequest instanceof HTTP_OAuth_Consumer_Request) { + $this->consumerRequest = new HTTP_OAuth_Consumer_Request; + } + + // TODO: Change this and add in code to validate the SSL cert. + // see https://github.com/bagder/curl/blob/master/lib/mk-ca-bundle.pl + $this->consumerRequest->setConfig(array( + 'ssl_verify_peer' => false, + 'ssl_verify_host' => false + )); + + return $this->consumerRequest; + } +} diff --git a/3rdparty/Dropbox/OAuth/Curl.php b/3rdparty/Dropbox/OAuth/Curl.php new file mode 100644 index 00000000000..b75b27bb363 --- /dev/null +++ b/3rdparty/Dropbox/OAuth/Curl.php @@ -0,0 +1,282 @@ +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()) { + + $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); + if (isset($matches[1])) { + $uri = str_replace($matches[0], "", $uri); + $filename = $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(); + 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); + curl_setopt($ch, CURLOPT_POSTFIELDS, $arguments); +// $httpHeaders['Content-Length']=strlen($arguments); + } 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_FRESH_CONNECT, true); + //Build header + $headers = array(); + foreach ($httpHeaders as $name => $value) { + $headers[] = "{$name}: $value"; + } + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + if (!ini_get('safe_mode') && !ini_get('open_basedir')) + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true ); + if (function_exists($this->ProgressFunction) and defined('CURLOPT_PROGRESSFUNCTION')) { + curl_setopt($ch, CURLOPT_NOPROGRESS, false); + curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $this->ProgressFunction); + curl_setopt($ch, CURLOPT_BUFFERSIZE, 512); + } + $response=curl_exec($ch); + $errorno=curl_errno($ch); + $error=curl_error($ch); + $status=curl_getinfo($ch,CURLINFO_HTTP_CODE); + curl_close($ch); + + + if (!empty($errorno)) + throw new Dropbox_Exception_NotFound('Curl error: ('.$errorno.') '.$error."\n"); + + if ($status>=300) { + $body = json_decode($response,true); + switch ($status) { + // 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"); + } + if (!empty($body["error"])) + throw new Dropbox_Exception_RequestToken('Error: ('.$status.') '.$body["error"]."\n"); + } + + return array( + 'body' => $response, + 'httpStatus' => $status + ); + } + + /** + * 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; + } + } + + +} \ No newline at end of file diff --git a/3rdparty/Dropbox/README.md b/3rdparty/Dropbox/README.md new file mode 100644 index 00000000000..54e05db762b --- /dev/null +++ b/3rdparty/Dropbox/README.md @@ -0,0 +1,31 @@ +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) + diff --git a/3rdparty/Dropbox/autoload.php b/3rdparty/Dropbox/autoload.php new file mode 100644 index 00000000000..5388ea6334a --- /dev/null +++ b/3rdparty/Dropbox/autoload.php @@ -0,0 +1,29 @@ +. +*/ + +require_once 'Dropbox/autoload.php'; + +class OC_Filestorage_Dropbox extends OC_Filestorage_Common { + + private $dropbox; + private $metaData = array(); + + private static $tempFiles = array(); + + public function __construct($params) { + $oauth = new Dropbox_OAuth_Curl($params['app_key'], $params['app_secret']); + $oauth->setToken($params['token'], $params['token_secret']); + $this->dropbox = new Dropbox_API($oauth, 'dropbox'); + + } + + private function getMetaData($path, $list = false) { + if (!$list && isset($this->metaData[$path])) { + return $this->metaData[$path]; + } else { + if ($list) { + $response = $this->dropbox->getMetaData($path); + if ($response && isset($response['contents'])) { + $contents = $response['contents']; + // Cache folder's contents + foreach ($contents as $file) { + $this->metaData[$path.'/'.basename($file['path'])] = $file; + } + unset($response['contents']); + $this->metaData[$path] = $response; + } + $this->metaData[$path] = $response; + // Return contents of folder only + return $contents; + } else { + try { + $response = $this->dropbox->getMetaData($path, 'false'); + $this->metaData[$path] = $response; + return $response; + } catch (Exception $exception) { + return false; + } + } + } + } + + public function mkdir($path) { + return $this->dropbox->createFolder($path); + } + + public function rmdir($path) { + return $this->dropbox->delete($path); + } + + public function opendir($path) { + if ($contents = $this->getMetaData($path, true)) { + $files = array(); + foreach ($contents as $file) { + $files[] = basename($file['path']); + } + OC_FakeDirStream::$dirs['dropbox'] = $files; + return opendir('fakedir://dropbox'); + } + return false; + } + + public function stat($path) { + if ($metaData = $this->getMetaData($path)) { + $stat['size'] = $metaData['bytes']; + $stat['atime'] = time(); + $stat['mtime'] = strtotime($metaData['modified']); + $stat['ctime'] = $stat['mtime']; + return $stat; + } + return false; + } + + public function filetype($path) { + if ($path == '' || $path == '/') { + return 'dir'; + } else if ($metaData = $this->getMetaData($path)) { + if ($metaData['is_dir'] == 'true') { + return 'dir'; + } else { + return 'file'; + } + } + return false; + } + + public function is_readable($path) { + return true; + } + + public function is_writable($path) { + return true; + } + + public function file_exists($path) { + if ($path == '' || $path == '/') { + return true; + } + if ($this->getMetaData($path)) { + return true; + } + return false; + } + + public function unlink($path) { + return $this->dropbox->delete($path); + } + + public function fopen($path, $mode) { + switch ($mode) { + case 'r': + case 'rb': + $tmpFile = OC_Helper::tmpFile(); + file_put_contents($tmpFile, $this->dropbox->getFile($path)); + return fopen($tmpFile, 'r'); + case 'w': + case 'wb': + case 'a': + case 'ab': + case 'r+': + case 'w+': + case 'wb+': + case 'a+': + case 'x': + case 'x+': + case 'c': + case 'c+': + if (strrpos($path, '.') !== false) { + $ext = substr($path, strrpos($path, '.')); + } else { + $ext = ''; + } + $tmpFile = OC_Helper::tmpFile($ext); + OC_CloseStreamWrapper::$callBacks[$tmpFile] = array($this, 'writeBack'); + if ($this->file_exists($path)) { + $source = $this->fopen($path, 'r'); + file_put_contents($tmpFile, $source); + } + self::$tempFiles[$tmpFile] = $path; + return fopen('close://'.$tmpFile, $mode); + } + return false; + } + + public function writeBack($tmpFile) { + if (isset(self::$tempFiles[$tmpFile])) { + $handle = fopen($tmpFile, 'r'); + $response = $this->dropbox->putFile(self::$tempFiles[$tmpFile], $handle); + if ($response) { + unlink($tmpFile); + } + } + } + + public function getMimeType($path) { + if ($this->filetype($path) == 'dir') { + return 'httpd/unix-directory'; + } else if ($metaData = $this->getMetaData($path)) { + return $metaData['mime_type']; + } + return false; + } + + public function free_space($path) { + if ($info = $this->dropbox->getAccountInfo()) { + return $info['quota_info']['quota'] - $info['quota_info']['normal']; + } + return false; + } + + public function touch($path, $mtime = null) { + return false; + } + +} + +?> \ No newline at end of file