コード例 #1
0
ファイル: Backup.php プロジェクト: jimrucinski/Vine
 /**
  * Uploads backup file from server to Dropbox.
  *
  * @param array $args arguments passed to the function
  *                    [consumer_key] -> consumer key of ManageWP Dropbox application
  *                    [consumer_secret] -> consumer secret of ManageWP Dropbox application
  *                    [oauth_token] -> oauth token of user on ManageWP Dropbox application
  *                    [oauth_token_secret] -> oauth token secret of user on ManageWP Dropbox application
  *                    [dropbox_destination] -> folder on user's Dropbox account which backup file should be upload to
  *                    [dropbox_site_folder] -> subfolder with site name in dropbox_destination which backup file should be upload to
  *                    [backup_file] -> absolute path of backup file on local server
  *
  * @return bool|array true is successful, array with error message if not
  */
 public function dropbox_backup($args)
 {
     mwp_logger()->info('Acquiring Dropbox token to start uploading the backup file');
     try {
         $dropbox = mwp_dropbox_oauth_factory($args['consumer_key'], $args['consumer_secret'], $args['oauth_token'], $args['oauth_token_secret']);
     } catch (Exception $e) {
         mwp_logger()->error('Error while acquiring Dropbox token', array('exception' => $e));
         return array('error' => $e->getMessage(), 'partial' => 1);
     }
     $args['dropbox_destination'] = '/' . ltrim($args['dropbox_destination'], '/');
     if ($args['dropbox_site_folder'] == true) {
         $args['dropbox_destination'] .= '/' . $this->site_name . '/' . basename($args['backup_file']);
     } else {
         $args['dropbox_destination'] .= '/' . basename($args['backup_file']);
     }
     $fileSize = filesize($args['backup_file']);
     $start = microtime(true);
     try {
         mwp_logger()->info('Uploading backup file to Dropbox; file size is {backup_size} (progress support: {progress_support})', array('backup_file' => $args['backup_file'], 'backup_size' => mwp_format_bytes($fileSize), 'directory' => $args['dropbox_destination'], 'progress_support' => version_compare(PHP_VERSION, '5.3', '>=') ? 'enabled' : 'disabled'));
         $callback = null;
         if (version_compare(PHP_VERSION, '5.3', '>=')) {
             $progress = new MWP_Progress_Upload($fileSize, 3, mwp_logger());
             $callback = $progress->getCallback();
         }
         $dropbox->uploadFile($args['dropbox_destination'], Dropbox_WriteMode::force(), fopen($args['backup_file'], 'r'), $fileSize, $callback);
     } catch (Exception $e) {
         mwp_logger()->error('Error while uploading the file to Dropbox', array('exception' => $e));
         return array('error' => $e->getMessage(), 'partial' => 1);
     }
     mwp_logger()->info('Backup to Dropbox completed; average speed is {speed}/s', array('speed' => mwp_format_bytes($fileSize / (microtime(true) - $start))));
     return true;
 }