PluginProbe
WP Database Backup – Unlimited Database & Files Backup by Backup for WP / 7.13
WP Database Backup – Unlimited Database & Files Backup by Backup for WP v7.13
7.13 7.12 trunk 1.1 2.1.1 5.9 6.0 6.1 6.10 6.11 6.12 6.12.1 6.2 6.3 6.4 6.5 6.5.1 6.6 6.7 6.8 6.9 7.0 7.0.1 7.1 7.10 All 34 releases
wp-database-backup / includes / admin / Destination / Google / google-api-php-client / src / service / Google_BatchRequest.php

Google_BatchRequest.php in WP Database Backup – Unlimited Database & Files Backup by Backup for WP 7.13, at includes/admin/Destination/Google/google-api-php-client/src/service/Google_BatchRequest.php

111 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:ignoreFile -- third party library
3 /*
4 * Copyright 2012 Google Inc.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 /**
20 * @author Chirag Shah <chirags@google.com>
21 */
22 class Google_BatchRequest {
23 /** @var string Multipart Boundary. */
24 private $boundary;
25
26 /** @var array service requests to be executed. */
27 private $requests = array();
28
29 public function __construct($boundary = false) {
30 $boundary = (false == $boundary) ? mt_rand() : $boundary;
31 $this->boundary = str_replace('"', '', $boundary);
32 }
33
34 public function add(Google_HttpRequest $request, $key = false) {
35 if (false == $key) {
36 $key = mt_rand();
37 }
38
39 $this->requests[$key] = $request;
40 }
41
42 public function execute() {
43 $body = '';
44
45 /** @var Google_HttpRequest $req */
46 foreach($this->requests as $key => $req) {
47 $body .= "--{$this->boundary}\n";
48 $body .= $req->toBatchString($key) . "\n";
49 }
50
51 $body = rtrim($body);
52 $body .= "\n--{$this->boundary}--";
53
54 global $apiConfig;
55 $url = $apiConfig['basePath'] . '/batch';
56 $httpRequest = new Google_HttpRequest($url, 'POST');
57 $httpRequest->setRequestHeaders(array(
58 'Content-Type' => 'multipart/mixed; boundary=' . $this->boundary));
59
60 $httpRequest->setPostBody($body);
61 $response = Google_Client::$io->makeRequest($httpRequest);
62
63 $response = $this->parseResponse($response);
64 return $response;
65 }
66
67 public function parseResponse(Google_HttpRequest $response) {
68 $contentType = $response->getResponseHeader('content-type');
69 $contentType = explode(';', $contentType);
70 $boundary = false;
71 foreach($contentType as $part) {
72 $part = (explode('=', $part, 2));
73 if (isset($part[0]) && 'boundary' == trim($part[0])) {
74 $boundary = $part[1];
75 }
76 }
77
78 $body = $response->getResponseBody();
79 if ($body) {
80 $body = str_replace("--$boundary--", "--$boundary", $body);
81 $parts = explode("--$boundary", $body);
82 $responses = array();
83
84 foreach($parts as $part) {
85 $part = trim($part);
86 if (!empty($part)) {
87 list($metaHeaders, $part) = explode("\r\n\r\n", $part, 2);
88 $metaHeaders = Google_CurlIO::parseResponseHeaders($metaHeaders);
89
90 $status = substr($part, 0, strpos($part, "\n"));
91 $status = explode(" ", $status);
92 $status = $status[1];
93
94 list($partHeaders, $partBody) = Google_CurlIO::parseHttpResponse($part, false);
95 $response = new Google_HttpRequest("");
96 $response->setResponseHttpCode($status);
97 $response->setResponseHeaders($partHeaders);
98 $response->setResponseBody($partBody);
99 $response = Google_REST::decodeHttpResponse($response);
100
101 // Need content id.
102 $responses[$metaHeaders['content-id']] = $response;
103 }
104 }
105
106 return $responses;
107 }
108
109 return null;
110 }
111 }