public function parseResponse(IWP_google_Http_Request $response) { $contentType = $response->getResponseHeader('content-type'); $contentType = explode(';', $contentType); $boundary = false; foreach ($contentType as $part) { $part = explode('=', $part, 2); if (isset($part[0]) && 'boundary' == trim($part[0])) { $boundary = $part[1]; } } $body = $response->getResponseBody(); if ($body) { $body = str_replace("--{$boundary}--", "--{$boundary}", $body); $parts = explode("--{$boundary}", $body); $responses = array(); foreach ($parts as $part) { $part = trim($part); if (!empty($part)) { list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2); $metaHeaders = $this->client->getIo()->getHttpResponseHeaders($metaHeaders); $status = substr($part, 0, strpos($part, "\n")); $status = explode(" ", $status); $status = $status[1]; list($partHeaders, $partBody) = $this->client->getIo()->ParseHttpResponse($part, false); $response = new IWP_google_Http_Request(""); $response->setResponseHttpCode($status); $response->setResponseHeaders($partHeaders); $response->setResponseBody($partBody); // Need content id. $key = $metaHeaders['content-id']; if (isset($this->expected_classes[$key]) && strlen($this->expected_classes[$key]) > 0) { $class = $this->expected_classes[$key]; $response->setExpectedClass($class); } try { $response = IWP_google_Http_REST::decodeHttpResponse($response); $responses[$key] = $response; } catch (IWP_google_Service_Exception $e) { // Store the exception as the response, so succesful responses // can be processed. $responses[$key] = $e; } } } return $responses; } return null; }
private function getResumeUri() { $result = null; $body = $this->request->getPostBody(); if ($body) { $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => IWP_google_Utils::getStrLen($body), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => ''); $this->request->setRequestHeaders($headers); } $response = $this->client->getIo()->makeRequest($this->request); $location = $response->getResponseHeader('location'); $code = $response->getResponseHttpCode(); if (200 == $code && true == $location) { return $location; } throw new IWP_google_Exception("Failed to start the resumable upload"); }
function get_google_drive_backup($args) { require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Client.php'; require_once $GLOBALS['iwp_mmb_plugin_dir'] . '/lib/Google/Service/Drive.php'; //refresh token $client = new IWP_google_Client(); $client->setClientId($args['clientID']); $client->setClientSecret($args['clientSecretKey']); $client->setRedirectUri($args['redirectURL']); $client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile')); //$client->setUseObjects(true); $accessToken = $args['token']; $refreshToken = $accessToken['refresh_token']; try { $client->refreshToken($refreshToken); } catch (Exception $e) { echo 'google Error ', $e->getMessage(), "\n"; return array("error" => $e->getMessage(), "error_code" => "google_error_refresh_token"); } //downloading the file $service = new IWP_google_Service_Drive($client); $file = $service->files->get($args['backup_file']); $downloadUrl = $file->getDownloadUrl(); $temp = wp_tempnam('iwp_temp_backup.zip'); try { if ($downloadUrl) { $request = new IWP_google_Http_Request($downloadUrl, 'GET', null, null); $signHttpRequest = $client->getAuth()->sign($request); $httpRequest = $client->getIo()->makeRequest($signHttpRequest); if ($httpRequest->getResponseHttpCode() == 200) { file_put_contents($temp, $httpRequest->getResponseBody()); return $temp; } else { // An error occurred. return array("error" => "There is some error.", "error_code" => "google_error_bad_response_code"); } } else { // The file doesn't have any content stored on Drive. return array("error" => "Google Drive file doesnt have nay content.", "error_code" => "google_error_download_url"); } } catch (Exception $e) { echo 'google Error ', $e->getMessage(), "\n"; return array("error" => $e->getMessage(), "error_code" => "google_error_download_url_catch_excep"); } }
/** * Executes a IWP_google_Http_Request * * @param IWP_google_Client $client * @param IWP_google_Http_Request $req * @return array decoded result * @throws IWP_google_Service_Exception on server side error (ie: not authenticated, * invalid or malformed post body, invalid url) */ public static function execute(IWP_google_Client $client, IWP_google_Http_Request $req) { $httpRequest = $client->getIo()->makeRequest($req); $httpRequest->setExpectedClass($req->getExpectedClass()); return self::decodeHttpResponse($httpRequest); }