Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../includes
File: google-extensions.php
<?php
[0] Fix | Delete
[1] Fix | Delete
if (!defined('UPDRAFTPLUS_DIR')) die('No direct access allowed.');
[2] Fix | Delete
[3] Fix | Delete
/*
[4] Fix | Delete
[5] Fix | Delete
// Unfortunately, since the variables we want to access are private, not protected, we can't just extend the class. Instead, we have to clone it, and add these methods into our cloned copy.
[6] Fix | Delete
[7] Fix | Delete
class UpdraftPlus_Google_Http_MediaFileUpload extends Google_Http_MediaFileUpload {
[8] Fix | Delete
public function updraftplus_setResumeUri($resumeUri) { $this->resumeUri = $resumeUri; }
[9] Fix | Delete
public function updraftplus_setProgress($progress) { $this->progress = $progress; }
[10] Fix | Delete
// N.B. a public method getResumeUri already exists - and does something completely different
[11] Fix | Delete
public function updraftplus_getResumeUri() { return empty($this->resumeUri) ? '' : $this->resumeUri; }
[12] Fix | Delete
}
[13] Fix | Delete
*/
[14] Fix | Delete
[15] Fix | Delete
/**
[16] Fix | Delete
* Copyright 2012 Google Inc.
[17] Fix | Delete
*
[18] Fix | Delete
* Licensed under the Apache License, Version 2.0 (the "License");
[19] Fix | Delete
* you may not use this file except in compliance with the License.
[20] Fix | Delete
* You may obtain a copy of the License at
[21] Fix | Delete
*
[22] Fix | Delete
* http://www.apache.org/licenses/LICENSE-2.0
[23] Fix | Delete
*
[24] Fix | Delete
* Unless required by applicable law or agreed to in writing, software
[25] Fix | Delete
* distributed under the License is distributed on an "AS IS" BASIS,
[26] Fix | Delete
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[27] Fix | Delete
* See the License for the specific language governing permissions and
[28] Fix | Delete
* limitations under the License.
[29] Fix | Delete
*/
[30] Fix | Delete
[31] Fix | Delete
if (!class_exists('UDP_Google_Client')) {
[32] Fix | Delete
require_once dirname(__FILE__) . '/Google/autoload.php';
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Manage large file uploads, which may be media but can be any type
[37] Fix | Delete
* of sizable data.
[38] Fix | Delete
*/
[39] Fix | Delete
class UpdraftPlus_Google_Http_MediaFileUpload
[40] Fix | Delete
{
[41] Fix | Delete
[42] Fix | Delete
public function updraftplus_setResumeUri($resumeUri) { $this->resumeUri = $resumeUri; }
[43] Fix | Delete
public function updraftplus_setProgress($progress) { $this->progress = $progress; }
[44] Fix | Delete
//N.B. a public method getResumeUri already exists - and does something completely different
[45] Fix | Delete
public function updraftplus_getResumeUri() { return $this->resumeUri; }
[46] Fix | Delete
[47] Fix | Delete
const UPLOAD_MEDIA_TYPE = 'media';
[48] Fix | Delete
const UPLOAD_MULTIPART_TYPE = 'multipart';
[49] Fix | Delete
const UPLOAD_RESUMABLE_TYPE = 'resumable';
[50] Fix | Delete
[51] Fix | Delete
/** @var string $mimeType */
[52] Fix | Delete
private $mimeType;
[53] Fix | Delete
[54] Fix | Delete
/** @var string $data */
[55] Fix | Delete
private $data;
[56] Fix | Delete
[57] Fix | Delete
/** @var bool $resumable */
[58] Fix | Delete
private $resumable;
[59] Fix | Delete
[60] Fix | Delete
/** @var int $chunkSize */
[61] Fix | Delete
private $chunkSize;
[62] Fix | Delete
[63] Fix | Delete
/** @var int $size */
[64] Fix | Delete
private $size;
[65] Fix | Delete
[66] Fix | Delete
/** @var string $resumeUri */
[67] Fix | Delete
private $resumeUri;
[68] Fix | Delete
[69] Fix | Delete
/** @var int $progress */
[70] Fix | Delete
private $progress;
[71] Fix | Delete
[72] Fix | Delete
/** @var UDP_Google_Client */
[73] Fix | Delete
private $client;
[74] Fix | Delete
[75] Fix | Delete
/** @var Google_Http_Request */
[76] Fix | Delete
private $request;
[77] Fix | Delete
[78] Fix | Delete
/** @var string */
[79] Fix | Delete
private $boundary;
[80] Fix | Delete
[81] Fix | Delete
/**
[82] Fix | Delete
* Result code from last HTTP call
[83] Fix | Delete
* @var int
[84] Fix | Delete
*/
[85] Fix | Delete
private $httpResultCode;
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* @param $mimeType string
[89] Fix | Delete
* @param $data string The bytes you want to upload.
[90] Fix | Delete
* @param $resumable bool
[91] Fix | Delete
* @param bool $chunkSize File will be uploaded in chunks of this many bytes.
[92] Fix | Delete
* only used if resumable=True
[93] Fix | Delete
*/
[94] Fix | Delete
public function __construct(
[95] Fix | Delete
UDP_Google_Client $client,
[96] Fix | Delete
UDP_Google_Http_Request $request,
[97] Fix | Delete
$mimeType,
[98] Fix | Delete
$data,
[99] Fix | Delete
$resumable = false,
[100] Fix | Delete
$chunkSize = false,
[101] Fix | Delete
$boundary = false
[102] Fix | Delete
) {
[103] Fix | Delete
$this->client = $client;
[104] Fix | Delete
$this->request = $request;
[105] Fix | Delete
$this->mimeType = $mimeType;
[106] Fix | Delete
$this->data = $data;
[107] Fix | Delete
$this->size = strlen($this->data);
[108] Fix | Delete
$this->resumable = $resumable;
[109] Fix | Delete
if (!$chunkSize) {
[110] Fix | Delete
$chunkSize = 256 * 1024;
[111] Fix | Delete
}
[112] Fix | Delete
$this->chunkSize = $chunkSize;
[113] Fix | Delete
$this->progress = 0;
[114] Fix | Delete
$this->boundary = $boundary;
[115] Fix | Delete
[116] Fix | Delete
// Process Media Request
[117] Fix | Delete
$this->process();
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
/**
[121] Fix | Delete
* Set the size of the file that is being uploaded.
[122] Fix | Delete
* @param $size - int file size in bytes
[123] Fix | Delete
*/
[124] Fix | Delete
public function setFileSize($size)
[125] Fix | Delete
{
[126] Fix | Delete
$this->size = $size;
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
/**
[130] Fix | Delete
* Return the progress on the upload
[131] Fix | Delete
* @return int progress in bytes uploaded.
[132] Fix | Delete
*/
[133] Fix | Delete
public function getProgress()
[134] Fix | Delete
{
[135] Fix | Delete
return $this->progress;
[136] Fix | Delete
}
[137] Fix | Delete
[138] Fix | Delete
/**
[139] Fix | Delete
* Return the HTTP result code from the last call made.
[140] Fix | Delete
* @return int code
[141] Fix | Delete
*/
[142] Fix | Delete
public function getHttpResultCode()
[143] Fix | Delete
{
[144] Fix | Delete
return $this->httpResultCode;
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
/**
[148] Fix | Delete
* Send the next part of the file to upload.
[149] Fix | Delete
*
[150] Fix | Delete
* @param [$chunk] the next set of bytes to send. If false will used $data passed
[151] Fix | Delete
* at construct time.
[152] Fix | Delete
*/
[153] Fix | Delete
public function nextChunk($chunk = false)
[154] Fix | Delete
{
[155] Fix | Delete
if (false == $this->resumeUri) {
[156] Fix | Delete
$this->resumeUri = $this->getResumeUri();
[157] Fix | Delete
}
[158] Fix | Delete
[159] Fix | Delete
if (false == $chunk) {
[160] Fix | Delete
$chunk = substr($this->data, $this->progress, $this->chunkSize);
[161] Fix | Delete
}
[162] Fix | Delete
[163] Fix | Delete
$lastBytePos = $this->progress + strlen($chunk) - 1;
[164] Fix | Delete
$headers = array(
[165] Fix | Delete
'content-range' => "bytes $this->progress-$lastBytePos/$this->size",
[166] Fix | Delete
'content-type' => $this->request->getRequestHeader('content-type'),
[167] Fix | Delete
'content-length' => strlen($chunk),
[168] Fix | Delete
'expect' => '',
[169] Fix | Delete
);
[170] Fix | Delete
[171] Fix | Delete
$httpRequest = new UDP_Google_Http_Request(
[172] Fix | Delete
$this->resumeUri,
[173] Fix | Delete
'PUT',
[174] Fix | Delete
$headers,
[175] Fix | Delete
$chunk
[176] Fix | Delete
);
[177] Fix | Delete
[178] Fix | Delete
if ($this->client->getClassConfig("UDP_Google_Http_Request", "enable_gzip_for_uploads")) {
[179] Fix | Delete
$httpRequest->enableGzip();
[180] Fix | Delete
} else {
[181] Fix | Delete
$httpRequest->disableGzip();
[182] Fix | Delete
}
[183] Fix | Delete
[184] Fix | Delete
$response = $this->client->getIo()->makeRequest($httpRequest);
[185] Fix | Delete
$response->setExpectedClass($this->request->getExpectedClass());
[186] Fix | Delete
$code = $response->getResponseHttpCode();
[187] Fix | Delete
$this->httpResultCode = $code;
[188] Fix | Delete
[189] Fix | Delete
if (308 == $code) {
[190] Fix | Delete
// Track the amount uploaded.
[191] Fix | Delete
$range = explode('-', $response->getResponseHeader('range'));
[192] Fix | Delete
$this->progress = $range[1] + 1;
[193] Fix | Delete
[194] Fix | Delete
// Allow for changing upload URLs.
[195] Fix | Delete
$location = $response->getResponseHeader('location');
[196] Fix | Delete
if ($location) {
[197] Fix | Delete
$this->resumeUri = $location;
[198] Fix | Delete
}
[199] Fix | Delete
[200] Fix | Delete
// No problems, but upload not complete.
[201] Fix | Delete
return false;
[202] Fix | Delete
} else {
[203] Fix | Delete
return UDP_Google_Http_REST::decodeHttpResponse($response, $this->client);
[204] Fix | Delete
}
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
/**
[208] Fix | Delete
* @param $meta
[209] Fix | Delete
* @param $params
[210] Fix | Delete
* @return array|bool
[211] Fix | Delete
* @visible for testing
[212] Fix | Delete
*/
[213] Fix | Delete
private function process()
[214] Fix | Delete
{
[215] Fix | Delete
$postBody = false;
[216] Fix | Delete
$contentType = false;
[217] Fix | Delete
[218] Fix | Delete
$meta = $this->request->getPostBody();
[219] Fix | Delete
$meta = is_string($meta) ? json_decode($meta, true) : $meta;
[220] Fix | Delete
[221] Fix | Delete
$uploadType = $this->getUploadType($meta);
[222] Fix | Delete
$this->request->setQueryParam('uploadType', $uploadType);
[223] Fix | Delete
$this->transformToUploadUrl();
[224] Fix | Delete
$mimeType = $this->mimeType ?
[225] Fix | Delete
$this->mimeType :
[226] Fix | Delete
$this->request->getRequestHeader('content-type');
[227] Fix | Delete
[228] Fix | Delete
if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) {
[229] Fix | Delete
$contentType = $mimeType;
[230] Fix | Delete
$postBody = is_string($meta) ? $meta : json_encode($meta);
[231] Fix | Delete
} else if (self::UPLOAD_MEDIA_TYPE == $uploadType) {
[232] Fix | Delete
$contentType = $mimeType;
[233] Fix | Delete
$postBody = $this->data;
[234] Fix | Delete
} else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) {
[235] Fix | Delete
// This is a multipart/related upload.
[236] Fix | Delete
$boundary = $this->boundary ? $this->boundary : mt_rand();
[237] Fix | Delete
$boundary = str_replace('"', '', $boundary);
[238] Fix | Delete
$contentType = 'multipart/related; boundary=' . $boundary;
[239] Fix | Delete
$related = "--$boundary\r\n";
[240] Fix | Delete
$related .= "Content-Type: application/json; charset=UTF-8\r\n";
[241] Fix | Delete
$related .= "\r\n" . json_encode($meta) . "\r\n";
[242] Fix | Delete
$related .= "--$boundary\r\n";
[243] Fix | Delete
$related .= "Content-Type: $mimeType\r\n";
[244] Fix | Delete
$related .= "Content-Transfer-Encoding: base64\r\n";
[245] Fix | Delete
$related .= "\r\n" . base64_encode($this->data) . "\r\n";
[246] Fix | Delete
$related .= "--$boundary--";
[247] Fix | Delete
$postBody = $related;
[248] Fix | Delete
}
[249] Fix | Delete
[250] Fix | Delete
$this->request->setPostBody($postBody);
[251] Fix | Delete
[252] Fix | Delete
if (isset($contentType) && $contentType) {
[253] Fix | Delete
$contentTypeHeader['content-type'] = $contentType;
[254] Fix | Delete
$this->request->setRequestHeaders($contentTypeHeader);
[255] Fix | Delete
}
[256] Fix | Delete
}
[257] Fix | Delete
[258] Fix | Delete
private function transformToUploadUrl()
[259] Fix | Delete
{
[260] Fix | Delete
$base = $this->request->getBaseComponent();
[261] Fix | Delete
$this->request->setBaseComponent($base . '/upload');
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
/**
[265] Fix | Delete
* Valid upload types:
[266] Fix | Delete
* - resumable (UPLOAD_RESUMABLE_TYPE)
[267] Fix | Delete
* - media (UPLOAD_MEDIA_TYPE)
[268] Fix | Delete
* - multipart (UPLOAD_MULTIPART_TYPE)
[269] Fix | Delete
* @param $meta
[270] Fix | Delete
* @return string
[271] Fix | Delete
* @visible for testing
[272] Fix | Delete
*/
[273] Fix | Delete
public function getUploadType($meta)
[274] Fix | Delete
{
[275] Fix | Delete
if ($this->resumable) {
[276] Fix | Delete
return self::UPLOAD_RESUMABLE_TYPE;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
if (false == $meta && $this->data) {
[280] Fix | Delete
return self::UPLOAD_MEDIA_TYPE;
[281] Fix | Delete
}
[282] Fix | Delete
[283] Fix | Delete
return self::UPLOAD_MULTIPART_TYPE;
[284] Fix | Delete
}
[285] Fix | Delete
[286] Fix | Delete
private function getResumeUri()
[287] Fix | Delete
{
[288] Fix | Delete
$result = null;
[289] Fix | Delete
$body = $this->request->getPostBody();
[290] Fix | Delete
if ($body) {
[291] Fix | Delete
$headers = array(
[292] Fix | Delete
'content-type' => 'application/json; charset=UTF-8',
[293] Fix | Delete
'content-length' => Google_Utils::getStrLen($body),
[294] Fix | Delete
'x-upload-content-type' => $this->mimeType,
[295] Fix | Delete
'x-upload-content-length' => $this->size,
[296] Fix | Delete
'expect' => '',
[297] Fix | Delete
);
[298] Fix | Delete
$this->request->setRequestHeaders($headers);
[299] Fix | Delete
}
[300] Fix | Delete
[301] Fix | Delete
$response = $this->client->getIo()->makeRequest($this->request);
[302] Fix | Delete
$location = $response->getResponseHeader('location');
[303] Fix | Delete
$code = $response->getResponseHttpCode();
[304] Fix | Delete
[305] Fix | Delete
if (200 == $code && true == $location) {
[306] Fix | Delete
return $location;
[307] Fix | Delete
}
[308] Fix | Delete
$message = $code;
[309] Fix | Delete
$body = @json_decode($response->getResponseBody());
[310] Fix | Delete
if (!empty( $body->error->errors ) ) {
[311] Fix | Delete
$message .= ': ';
[312] Fix | Delete
foreach ($body->error->errors as $error) {
[313] Fix | Delete
$message .= "{$error->domain}, {$error->message};";
[314] Fix | Delete
}
[315] Fix | Delete
$message = rtrim($message, ';');
[316] Fix | Delete
}
[317] Fix | Delete
[318] Fix | Delete
$error = "Failed to start the resumable upload (HTTP {$message})";
[319] Fix | Delete
$this->client->getLogger()->error($error);
[320] Fix | Delete
throw new Google_Exception($error);
[321] Fix | Delete
}
[322] Fix | Delete
}
[323] Fix | Delete
[324] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function