Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/updraftp.../includes/Google/Service
File: Resource.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* Copyright 2010 Google Inc.
[2] Fix | Delete
*
[3] Fix | Delete
* Licensed under the Apache License, Version 2.0 (the "License");
[4] Fix | Delete
* you may not use this file except in compliance with the License.
[5] Fix | Delete
* You may obtain a copy of the License at
[6] Fix | Delete
*
[7] Fix | Delete
* http://www.apache.org/licenses/LICENSE-2.0
[8] Fix | Delete
*
[9] Fix | Delete
* Unless required by applicable law or agreed to in writing, software
[10] Fix | Delete
* distributed under the License is distributed on an "AS IS" BASIS,
[11] Fix | Delete
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
[12] Fix | Delete
* See the License for the specific language governing permissions and
[13] Fix | Delete
* limitations under the License.
[14] Fix | Delete
*/
[15] Fix | Delete
[16] Fix | Delete
if (!class_exists('UDP_Google_Client')) {
[17] Fix | Delete
require_once dirname(__FILE__) . '/../autoload.php';
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Implements the actual methods/resources of the discovered Google API using magic function
[22] Fix | Delete
* calling overloading (__call()), which on call will see if the method name (plus.activities.list)
[23] Fix | Delete
* is available in this service, and if so construct an apiHttpRequest representing it.
[24] Fix | Delete
*
[25] Fix | Delete
*/
[26] Fix | Delete
class UDP_Google_Service_Resource
[27] Fix | Delete
{
[28] Fix | Delete
// Valid query parameters that work, but don't appear in discovery.
[29] Fix | Delete
private $stackParameters = array(
[30] Fix | Delete
'alt' => array('type' => 'string', 'location' => 'query'),
[31] Fix | Delete
'fields' => array('type' => 'string', 'location' => 'query'),
[32] Fix | Delete
'trace' => array('type' => 'string', 'location' => 'query'),
[33] Fix | Delete
'userIp' => array('type' => 'string', 'location' => 'query'),
[34] Fix | Delete
'quotaUser' => array('type' => 'string', 'location' => 'query'),
[35] Fix | Delete
'data' => array('type' => 'string', 'location' => 'body'),
[36] Fix | Delete
'mimeType' => array('type' => 'string', 'location' => 'header'),
[37] Fix | Delete
'uploadType' => array('type' => 'string', 'location' => 'query'),
[38] Fix | Delete
'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
[39] Fix | Delete
'prettyPrint' => array('type' => 'string', 'location' => 'query'),
[40] Fix | Delete
);
[41] Fix | Delete
[42] Fix | Delete
/** @var string $rootUrl */
[43] Fix | Delete
private $rootUrl;
[44] Fix | Delete
[45] Fix | Delete
/** @var Google_Client $client */
[46] Fix | Delete
private $client;
[47] Fix | Delete
[48] Fix | Delete
/** @var string $serviceName */
[49] Fix | Delete
private $serviceName;
[50] Fix | Delete
[51] Fix | Delete
/** @var string $servicePath */
[52] Fix | Delete
private $servicePath;
[53] Fix | Delete
[54] Fix | Delete
/** @var string $resourceName */
[55] Fix | Delete
private $resourceName;
[56] Fix | Delete
[57] Fix | Delete
/** @var array $methods */
[58] Fix | Delete
private $methods;
[59] Fix | Delete
[60] Fix | Delete
public function __construct($service, $serviceName, $resourceName, $resource)
[61] Fix | Delete
{
[62] Fix | Delete
$this->rootUrl = $service->rootUrl;
[63] Fix | Delete
$this->client = $service->getClient();
[64] Fix | Delete
$this->servicePath = $service->servicePath;
[65] Fix | Delete
$this->serviceName = $serviceName;
[66] Fix | Delete
$this->resourceName = $resourceName;
[67] Fix | Delete
$this->methods = is_array($resource) && isset($resource['methods']) ?
[68] Fix | Delete
$resource['methods'] :
[69] Fix | Delete
array($resourceName => $resource);
[70] Fix | Delete
}
[71] Fix | Delete
[72] Fix | Delete
/**
[73] Fix | Delete
* TODO(ianbarber): This function needs simplifying.
[74] Fix | Delete
* @param $name
[75] Fix | Delete
* @param $arguments
[76] Fix | Delete
* @param $expected_class - optional, the expected class name
[77] Fix | Delete
* @return Google_Http_Request|expected_class
[78] Fix | Delete
* @throws Google_Exception
[79] Fix | Delete
*/
[80] Fix | Delete
public function call($name, $arguments, $expected_class = null)
[81] Fix | Delete
{
[82] Fix | Delete
if (! isset($this->methods[$name])) {
[83] Fix | Delete
$this->client->getLogger()->error(
[84] Fix | Delete
'Service method unknown',
[85] Fix | Delete
array(
[86] Fix | Delete
'service' => $this->serviceName,
[87] Fix | Delete
'resource' => $this->resourceName,
[88] Fix | Delete
'method' => $name
[89] Fix | Delete
)
[90] Fix | Delete
);
[91] Fix | Delete
[92] Fix | Delete
throw new Google_Exception(
[93] Fix | Delete
"Unknown function: " .
[94] Fix | Delete
"{$this->serviceName}->{$this->resourceName}->{$name}()"
[95] Fix | Delete
);
[96] Fix | Delete
}
[97] Fix | Delete
$method = $this->methods[$name];
[98] Fix | Delete
$parameters = $arguments[0];
[99] Fix | Delete
[100] Fix | Delete
// postBody is a special case since it's not defined in the discovery
[101] Fix | Delete
// document as parameter, but we abuse the param entry for storing it.
[102] Fix | Delete
$postBody = null;
[103] Fix | Delete
if (isset($parameters['postBody'])) {
[104] Fix | Delete
if ($parameters['postBody'] instanceof Google_Model) {
[105] Fix | Delete
// In the cases the post body is an existing object, we want
[106] Fix | Delete
// to use the smart method to create a simple object for
[107] Fix | Delete
// for JSONification.
[108] Fix | Delete
$parameters['postBody'] = $parameters['postBody']->toSimpleObject();
[109] Fix | Delete
} else if (is_object($parameters['postBody'])) {
[110] Fix | Delete
// If the post body is another kind of object, we will try and
[111] Fix | Delete
// wrangle it into a sensible format.
[112] Fix | Delete
$parameters['postBody'] =
[113] Fix | Delete
$this->convertToArrayAndStripNulls($parameters['postBody']);
[114] Fix | Delete
}
[115] Fix | Delete
$postBody = json_encode($parameters['postBody']);
[116] Fix | Delete
unset($parameters['postBody']);
[117] Fix | Delete
}
[118] Fix | Delete
[119] Fix | Delete
// TODO(ianbarber): optParams here probably should have been
[120] Fix | Delete
// handled already - this may well be redundant code.
[121] Fix | Delete
if (isset($parameters['optParams'])) {
[122] Fix | Delete
$optParams = $parameters['optParams'];
[123] Fix | Delete
unset($parameters['optParams']);
[124] Fix | Delete
$parameters = array_merge($parameters, $optParams);
[125] Fix | Delete
}
[126] Fix | Delete
[127] Fix | Delete
if (!isset($method['parameters'])) {
[128] Fix | Delete
$method['parameters'] = array();
[129] Fix | Delete
}
[130] Fix | Delete
[131] Fix | Delete
$method['parameters'] = array_merge(
[132] Fix | Delete
$method['parameters'],
[133] Fix | Delete
$this->stackParameters
[134] Fix | Delete
);
[135] Fix | Delete
foreach ($parameters as $key => $val) {
[136] Fix | Delete
if ($key != 'postBody' && ! isset($method['parameters'][$key])) {
[137] Fix | Delete
$this->client->getLogger()->error(
[138] Fix | Delete
'Service parameter unknown',
[139] Fix | Delete
array(
[140] Fix | Delete
'service' => $this->serviceName,
[141] Fix | Delete
'resource' => $this->resourceName,
[142] Fix | Delete
'method' => $name,
[143] Fix | Delete
'parameter' => $key
[144] Fix | Delete
)
[145] Fix | Delete
);
[146] Fix | Delete
throw new Google_Exception("($name) unknown parameter: '$key'");
[147] Fix | Delete
}
[148] Fix | Delete
}
[149] Fix | Delete
[150] Fix | Delete
foreach ($method['parameters'] as $paramName => $paramSpec) {
[151] Fix | Delete
if (isset($paramSpec['required']) &&
[152] Fix | Delete
$paramSpec['required'] &&
[153] Fix | Delete
! isset($parameters[$paramName])
[154] Fix | Delete
) {
[155] Fix | Delete
$this->client->getLogger()->error(
[156] Fix | Delete
'Service parameter missing',
[157] Fix | Delete
array(
[158] Fix | Delete
'service' => $this->serviceName,
[159] Fix | Delete
'resource' => $this->resourceName,
[160] Fix | Delete
'method' => $name,
[161] Fix | Delete
'parameter' => $paramName
[162] Fix | Delete
)
[163] Fix | Delete
);
[164] Fix | Delete
throw new Google_Exception("($name) missing required param: '$paramName'");
[165] Fix | Delete
}
[166] Fix | Delete
if (isset($parameters[$paramName])) {
[167] Fix | Delete
$value = $parameters[$paramName];
[168] Fix | Delete
$parameters[$paramName] = $paramSpec;
[169] Fix | Delete
$parameters[$paramName]['value'] = $value;
[170] Fix | Delete
unset($parameters[$paramName]['required']);
[171] Fix | Delete
} else {
[172] Fix | Delete
// Ensure we don't pass nulls.
[173] Fix | Delete
unset($parameters[$paramName]);
[174] Fix | Delete
}
[175] Fix | Delete
}
[176] Fix | Delete
[177] Fix | Delete
$this->client->getLogger()->info(
[178] Fix | Delete
'Service Call',
[179] Fix | Delete
array(
[180] Fix | Delete
'service' => $this->serviceName,
[181] Fix | Delete
'resource' => $this->resourceName,
[182] Fix | Delete
'method' => $name,
[183] Fix | Delete
'arguments' => $parameters,
[184] Fix | Delete
)
[185] Fix | Delete
);
[186] Fix | Delete
[187] Fix | Delete
$url = UDP_Google_Http_REST::createRequestUri(
[188] Fix | Delete
$this->servicePath,
[189] Fix | Delete
$method['path'],
[190] Fix | Delete
$parameters
[191] Fix | Delete
);
[192] Fix | Delete
$httpRequest = new UDP_Google_Http_Request(
[193] Fix | Delete
$url,
[194] Fix | Delete
$method['httpMethod'],
[195] Fix | Delete
null,
[196] Fix | Delete
$postBody
[197] Fix | Delete
);
[198] Fix | Delete
[199] Fix | Delete
if ($this->rootUrl) {
[200] Fix | Delete
$httpRequest->setBaseComponent($this->rootUrl);
[201] Fix | Delete
} else {
[202] Fix | Delete
$httpRequest->setBaseComponent($this->client->getBasePath());
[203] Fix | Delete
}
[204] Fix | Delete
[205] Fix | Delete
if ($postBody) {
[206] Fix | Delete
$contentTypeHeader = array();
[207] Fix | Delete
$contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
[208] Fix | Delete
$httpRequest->setRequestHeaders($contentTypeHeader);
[209] Fix | Delete
$httpRequest->setPostBody($postBody);
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
$httpRequest = $this->client->getAuth()->sign($httpRequest);
[213] Fix | Delete
$httpRequest->setExpectedClass($expected_class);
[214] Fix | Delete
[215] Fix | Delete
if (isset($parameters['data']) &&
[216] Fix | Delete
($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) {
[217] Fix | Delete
// If we are doing a simple media upload, trigger that as a convenience.
[218] Fix | Delete
$mfu = new Google_Http_MediaFileUpload(
[219] Fix | Delete
$this->client,
[220] Fix | Delete
$httpRequest,
[221] Fix | Delete
isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream',
[222] Fix | Delete
$parameters['data']['value']
[223] Fix | Delete
);
[224] Fix | Delete
}
[225] Fix | Delete
[226] Fix | Delete
if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
[227] Fix | Delete
$httpRequest->enableExpectedRaw();
[228] Fix | Delete
}
[229] Fix | Delete
[230] Fix | Delete
if ($this->client->shouldDefer()) {
[231] Fix | Delete
// If we are in batch or upload mode, return the raw request.
[232] Fix | Delete
return $httpRequest;
[233] Fix | Delete
}
[234] Fix | Delete
[235] Fix | Delete
return $this->client->execute($httpRequest);
[236] Fix | Delete
}
[237] Fix | Delete
[238] Fix | Delete
protected function convertToArrayAndStripNulls($o)
[239] Fix | Delete
{
[240] Fix | Delete
$o = (array) $o;
[241] Fix | Delete
foreach ($o as $k => $v) {
[242] Fix | Delete
if ($v === null) {
[243] Fix | Delete
unset($o[$k]);
[244] Fix | Delete
} elseif (is_object($v) || is_array($v)) {
[245] Fix | Delete
$o[$k] = $this->convertToArrayAndStripNulls($o[$k]);
[246] Fix | Delete
}
[247] Fix | Delete
}
[248] Fix | Delete
return $o;
[249] Fix | Delete
}
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function