Exemple #1
0
 public function download_googledrive($options)
 {
     $this->log(__('Downloading backup via google drive', 'my-wp-backup'));
     $client = Admin\Job::get_drive_client();
     $client->setAccessToken(html_entity_decode($options['token_json']));
     $service = new \Google_Service_Drive($client);
     $info = $this->backup['destinations']['googledrive'];
     foreach ($this->backup['archives'] as $archive) {
         $file = $info['files'][$archive];
         $local_filename = $this->basedir . $archive;
         $this->log(sprintf(__('Downloading %s -> %s...', 'my-wp-backup'), $file['id'], $local_filename), 'debug');
         $local = fopen($local_filename, 'wb');
         $url = $service->files->get($file['id'])->getDownloadUrl();
         $token = json_decode($client->getAccessToken(), true);
         if ($client->isAccessTokenExpired()) {
             $client->refreshToken($token['refresh_token']);
         }
         $token = json_decode($client->getAccessToken(), true);
         $httpclient = new \Guzzle\Http\Client();
         $httpclient->setDefaultOption('headers/Authorization', 'Bearer ' . $token['access_token']);
         $httpclient->get($url)->setResponseBody($local)->send();
         if (is_resource($local)) {
             fclose($local);
         }
         $this->log(__('Ok.', 'my-wp-backup'), 'debug');
     }
     $this->log(__('Done google drive download', 'my-wp-backup'));
 }