Пример #1
0
 /**
  * @param string $localPath
  * @param string $filename
  *
  * @return string URL
  */
 public function upload($localPath, $filename)
 {
     $size = @getimagesize($localPath);
     if ($size) {
         $extension = image_type_to_extension($size[2]);
     } else {
         $extension = strrchr($filename, '.');
     }
     $metadata = $this->client->uploadFile($this->prefix . hash_file('crc32b', $localPath) . $extension, Dropbox\WriteMode::add(), fopen($localPath, 'rb'), filesize($localPath));
     if (is_array($metadata) && isset($metadata['path'])) {
         //return $this->client->createShareableLink($metadata['path']);
         $data = $this->client->createTemporaryDirectLink($metadata['path']);
         if (is_array($data)) {
             return $data[0];
         }
     }
     return null;
 }
 public function wpua_avatar_upload($file)
 {
     try {
         $fp = fopen($file->path, 'rb');
         $metadata = $this->dropbox->uploadFile($this->getUploadPath($file->name), Dropbox\WriteMode::add(), $fp);
         fclose($fp);
         $sharable = $this->dropbox->createShareableLink($this->getUploadPath($file->name));
         if ($sharable) {
             remove_query_arg('dl', $sharable);
             $this->avatar_url = add_query_arg('dl', '1', $sharable);
             $this->avatar_filename = $file->name;
             $this->resource = trailingslashit($this->folder_path);
             $this->save();
         }
     } catch (Exception $e) {
         return new WP_Error('upload_problem', $e->getMessage());
     }
 }
Пример #3
0
 /**
  * uploadFile upload file to dropbox using the Dropbox API
  * @param  string $file path to file
  */
 public function uploadFile($file)
 {
     $f = fopen($file, "rb");
     $this->dbxClient->uploadFile("/" . $this->projectFolder . "/{$file}", Dropbox\WriteMode::add(), $f);
     fclose($f);
 }
Пример #4
0
function Dropbox_WriteMode_add()
{
    return Dropbox\WriteMode::add();
}
Пример #5
0
 /**
  * Upload files to dropbox
  *
  * @since  1.0
  * @uses   \Dropbox\Client
  * @see    https://www.dropbox.com/developers/core/start/php#uploading
  * @param  array $files
  * @param bool $offline
  * @return integer
  */
 public function upload($files = array(), $offline = false)
 {
     if ($this->isAuthenticated() === false) {
         return 401;
     }
     if (empty($files)) {
         return 404;
     }
     $filepath = $this->getBackupsPath();
     /*if (null === $token = $this->readToken()) {
           $token = $_SESSION[self::TOKEN_SESS_NAME];
       }*/
     $token = $this->getToken();
     $client = new Dropbox\Client($token, $this->applicationName);
     foreach ($files as $file) {
         $file = basename($file);
         if (file_exists($filepath . $file)) {
             try {
                 $stream = @fopen($filepath . $file, 'rb');
                 $client->uploadFile($this->getDropboxPath() . $file, Dropbox\WriteMode::add(), $stream);
                 fclose($stream);
             } catch (Exception $ex) {
                 $this->pushError($ex->getMessage());
                 return 500;
             }
         }
     }
     return 200;
 }
Пример #6
0
 function dosAction($args)
 {
     global $User, $SqlDatabase, $Config, $Logger;
     // ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### ##### #####
     // Do a directory listing
     // TODO: Make it uniform! Not to methods! use command == dir
     if (isset($args->command) && $args->command == 'directory' || isset($args->command) && $args->command == 'dosaction' && isset($args->args->action) && $args->args->action == 'dir') {
         $fo = false;
         // Can we get sub folder?
         $thePath = isset($args->path) ? $args->path : (isset($args->args->path) ? $args->args->path : '');
         $subPath = false;
         if (isset($thePath) && strlen($thePath) > 0 && ($subPath = trim(end(explode(':', $thePath))))) {
             // Failed to find a path
             if (!$subPath) {
                 die('fail<!--separate-->Path error.');
             }
         }
         $out = [];
         if (substr($thePath, -1, 1) == ':') {
             if ($this->state == self::UNAUTHORIZED) {
                 die('ok<!--separate-->' . json_encode($this->loginFile($this->Name . ':/')));
             } else {
                 if ($this->connectClient()) {
                     $fo = $this->listFolderContents('/');
                 }
             }
         } else {
             if ($this->connectClient()) {
                 $fo = $this->listFolderContents($subPath);
             } else {
                 die('fail<!--separate-->Dropbox connect failed');
             }
         }
         return 'ok<!--separate-->' . json_encode($fo);
     } else {
         if ($args->command == 'write') {
             if ($this->state != self::UNAUTHORIZED && isset($args->tmpfile) && $this->connectClient()) {
                 $dropboxpath = end(explode(':', $args->path));
                 if (substr($dropboxpath, 0, 1) != '/') {
                     $dropboxpath = '/' . $dropboxpath;
                 }
                 //sometimes the leading trail is missing; like in sometimes when a folder is created at root level
                 $fm = $this->dbx->getMetadata($dropboxpath);
                 //check if this file exists; does not work like this and seems not really necessary... we just update exsting ones as Dropbox keeps revisions :)
                 //$md = $this->dbx->getMetaData( '/' . $dropboxpath );
                 $fp = fopen($args->tmpfile, 'rb');
                 if (is_array($fm) && $fm['rev']) {
                     $newmeta = $this->dbx->uploadFile($dropboxpath, Dropbox\WriteMode::update($fm['rev']), $fp);
                 } else {
                     $newmeta = $this->dbx->uploadFile($dropboxpath, Dropbox\WriteMode::add(), $fp);
                 }
                 fclose($fp);
                 if (is_array($newmeta) && isset($newmeta['bytes'])) {
                     $this->updateAccountInfo();
                     return 'ok<!--separate-->' . $newmeta['bytes'] . '<!--separate-->' . $newmeta['path'];
                 } else {
                     $Logger->log('Write to dropboxdrive ' . $this->Name . ' failed ' . $newmeta);
                     return 'fail<!--separate-->Write to dropbobx failed';
                 }
             } else {
                 return 'fail<!--separate-->Not authorized to write to Dropbox.';
             }
         } else {
             if ($args->command == 'read') {
                 if ($this->state == self::UNAUTHORIZED) {
                     // return login Dialogue
                     if ($args->path == $this->Name . ':/Login.jsx') {
                         //
                         //$Logger->log( 'What can we send to dropbox... args ... ' . print_r( $args,1 ) . ' User ######## ' . print_r( $User,1 ));
                         // we want to use token... but as we do not have SSL yet we need to work with the code
                         // our state info must contain user id and our mountname so that we can update the database...
                         $statevar = $User->ID . '::' . $this->Name . '::' . $args->sessionid;
                         $targetURL = 'https://www.dropbox.com/1/oauth2/authorize' . '?response_type=token&client_id=' . $this->sysinfo['api-key']['key'] . '&state=' . rawurlencode(bin2hex($statevar));
                         if (file_exists(self::LOGINAPP)) {
                             $loginapp = file_get_contents(self::LOGINAPP);
                             $loginapp = str_replace('{dropboxurl}', $targetURL, $loginapp);
                             $loginapp = str_replace('{authid}', $args->sessionid, $loginapp);
                             $loginapp = str_replace('{path}', $this->Name . ':Authorized.html', $loginapp);
                             ob_clean();
                             return 'ok<!--separate-->' . $loginapp;
                             die;
                         } else {
                             return 'fail<!--separate-->Login app not found' . self::LOGINAPP;
                         }
                     }
                 } else {
                     if ($this->connectClient()) {
                         //we want a file stream here
                         return $this->getFile($args->path);
                     }
                 }
                 die('fail<!--separate-->Could not connect to Dropbox for file read ::' . $this->state);
             } else {
                 if ($args->command == 'import') {
                     /* TODO: implement import; postponed as Hogne said this is not needed */
                     die('fail');
                 } else {
                     if ($args->command == 'loaddocumentformat') {
                         /* TODO: check if we can just reuse the existing code from mysql drive here... */
                         die('fail');
                     } else {
                         if ($args->command == 'gendocumentpdf') {
                             /* TODO: Postponed as Hogne said not needed now */
                             die('fail');
                         } else {
                             if ($args->command == 'writedocumentformat') {
                                 /* TODO: Postponed as Hogne said not needed now */
                                 die('fail');
                             } else {
                                 if ($args->command == 'volumeinfo') {
                                     if ($this->state == self::UNAUTHORIZED) {
                                         $o = new stdClass();
                                         $o->Volume = $this->Name . ':';
                                         $o->Used = 0;
                                         $o->Filesize = 0;
                                         die('ok<!--separate-->' . json_encode($o));
                                     } else {
                                         if ($this->connectClient()) {
                                             $o = new stdClass();
                                             $o->Volume = $this->Name . ':';
                                             $o->Used = $this->accountinfo['quota_info']['normal'];
                                             $o->Filesize = $this->accountinfo['quota_info']['quota'];
                                             die('ok<!--separate-->' . json_encode($o));
                                         }
                                     }
                                     die('fail');
                                 } else {
                                     if ($args->command == 'dosaction') {
                                         $action = isset($args->action) ? $args->action : (isset($args->args->action) ? $args->args->action : false);
                                         $path = isset($args->path) ? $args->path : (isset($args->args->path) ? $args->args->path : false);
                                         switch ($action) {
                                             case 'mount':
                                                 return $this->connectToDropbox();
                                                 break;
                                             case 'unmount':
                                                 return 'ok';
                                                 break;
                                             case 'rename':
                                                 if (!isset($args->path) || !isset($args->newname)) {
                                                     return 'fail<!--separate-->Cannot rename without proper arguments';
                                                 }
                                                 if ($this->connectClient()) {
                                                     $oldpath = end(explode(':', $args->path));
                                                     $tmp = explode('/', $oldpath);
                                                     array_pop($tmp);
                                                     $newpath = implode('/', $tmp) . '/' . $args->newname;
                                                     if (substr($oldpath, 0, 1) != '/') {
                                                         $oldpath = '/' . $oldpath;
                                                     }
                                                     //sometimes the leading trail is missing; like in sometimes when a folder is created at root level
                                                     $result = $this->dbx->move($oldpath, $newpath);
                                                     //$Logger->log( 'API move result is \n\n' . print_r( $result, 1 ) );
                                                     return 'ok<!--separate-->File moved.';
                                                 } else {
                                                     return 'fail<!--separate-->Could not connect to Dropbox';
                                                 }
                                                 break;
                                             case 'makedir':
                                                 if (isset($args->path) && $this->connectClient()) {
                                                     $dropboxpath = end(explode(':', $args->path));
                                                     if (substr($dropboxpath, 0, 1) != '/') {
                                                         $dropboxpath = '/' . $dropboxpath;
                                                     }
                                                     //sometimes the leading trail is missing; like in sometimes when a folder is created at root level
                                                     $result = $this->dbx->createFolder($dropboxpath);
                                                     if (is_array($result) && isset($result['path']) && isset($result['is_dir']) && $result['is_dir'] == 1) {
                                                         return 'ok<!--separate-->Folder created';
                                                     }
                                                 }
                                                 return 'fail<!--separate-->Could not create folder at Dropbox target';
                                                 break;
                                             case 'delete':
                                                 $dropboxpath = end(explode(':', $args->path));
                                                 if (substr($dropboxpath, 0, 1) != '/') {
                                                     $dropboxpath = '/' . $dropboxpath;
                                                 }
                                                 //sometimes the leading trail is missing; like in sometimes when a folder is created at root level
                                                 if (substr($dropboxpath, -1) == '/') {
                                                     $dropboxpath = rtrim($dropboxpath, '/');
                                                 }
                                                 if (strlen($dropboxpath) && $this->connectClient()) {
                                                     $result = $this->dbx->delete($dropboxpath);
                                                     //$Logger->log( 'Result of delete operation on '. $dropboxpath .' was this: \n\n' . print_r($result,1) );
                                                     $this->updateAccountInfo();
                                                     if (is_array($result) && $result['is_deleted'] == 1) {
                                                         return 'ok';
                                                     }
                                                 }
                                                 return 'fail<!--separate-->Delete in dropbox failed';
                                                 break;
                                             case 'copy':
                                                 return 'fail<!--separate-->Copy not implemented yet.';
                                                 break;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return 'fail<!--separate-->' . print_r($args, 1);
 }
Пример #7
0
 /**
  * Send the request to the Dropbox service
  *
  * @param $url string       	
  * @param $post boolean       	
  * @param $postData array       	
  * @throws Exception
  * @return mixed
  */
 protected static function request($url, $post = false, $postData = array())
 {
     try {
         $token = Settings::findbyParam('dropbox_token');
         $dropbox = new Dropbox\Client($token, 'shineisp', null, self::$appInfo->getHost());
         $fh = fopen($postData['file'], "rb");
         $result = $dropbox->uploadFile($postData['dest'] . basename($postData['file']), Dropbox\WriteMode::force(), $fh);
         fclose($fh);
         return $result;
     } catch (Exception $e) {
         Shineisp_Commons_Utilities::log(__METHOD__ . " " . $e->getMessage(), "plugin_dropbox.log");
     }
 }
Пример #8
0
 public function uploadFile($fileName)
 {
     $file = fopen('uploads/' . $fileName, 'r');
     $size = filesize('uploads/' . $fileName);
     $this->client->uploadFile('/AdServey/Completed/' . $fileName, Dropbox\WriteMode::add(), $file, $size);
 }