Пример #1
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);
 }