Example #1
1
 public function __construct(Google_Client $client)
 {
     if (!function_exists('memcache_connect') && !class_exists("Memcached")) {
         $error = "Memcache functions not available";
         $client->getLogger()->error($error);
         //throw new Google_Cache_Exception($error);
         echo json_encode(array('status' => FALSE, 'response-code' => 400, 'response' => $error, 'message' => $error, 'map' => ''));
         die;
     }
     $this->client = $client;
     if ($client->isAppEngine()) {
         // No credentials needed for GAE.
         $this->mc = new Memcached();
         $this->connection = true;
     } else {
         $this->host = $client->getClassConfig($this, 'host');
         $this->port = $client->getClassConfig($this, 'port');
         if (empty($this->host) || empty($this->port) && (string) $this->port != "0") {
             $error = "You need to supply a valid memcache host and port";
             $client->getLogger()->error($error);
             //throw new Google_Cache_Exception($error);
             echo json_encode(array('status' => FALSE, 'response-code' => 400, 'response' => $error, 'message' => $error, 'map' => ''));
             die;
         }
     }
 }
 private function fetchResumeUri()
 {
     $result = null;
     $body = $this->request->getBody();
     if ($body) {
         $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '');
         foreach ($headers as $key => $value) {
             $this->request->setHeader($key, $value);
         }
     }
     $response = $this->client->getHttpClient()->send($this->request);
     $location = $response->getHeader('location');
     $code = $response->getStatusCode();
     if (200 == $code && true == $location) {
         return $location;
     }
     $message = $code;
     $body = $response->json();
     if (isset($body['error']['errors'])) {
         $message .= ': ';
         foreach ($body['error']['errors'] as $error) {
             $message .= "{$error[domain]}, {$error[message]};";
         }
         $message = rtrim($message, ';');
     }
     $error = "Failed to start the resumable upload (HTTP {$message})";
     $this->client->getLogger()->error($error);
     throw new Google_Exception($error);
 }
Example #3
0
 private function getResumeUri()
 {
     $result = null;
     $body = $this->request->getPostBody();
     if ($body) {
         $headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => 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;
     }
     $message = $code;
     $body = @json_decode($response->getResponseBody());
     if (!empty($body->error->errors)) {
         $message .= ': ';
         foreach ($body->error->errors as $error) {
             $message .= "{$error->domain}, {$error->message};";
         }
         $message = rtrim($message, ';');
     }
     $error = "Failed to start the resumable upload (HTTP {$message})";
     $this->client->getLogger()->error($error);
     throw new Google_Exception($error);
 }
Example #4
0
 public function __construct(Google_Client $client)
 {
     if (!function_exists('apc_add')) {
         $error = "Apc functions not available";
         $client->getLogger()->error($error);
         throw new Google_Cache_Exception($error);
     }
     $this->client = $client;
 }
Example #5
0
 public function __construct(Google_Client $client)
 {
     if (!class_exists('Guzzle\\Http\\Client')) {
         $error = 'Cannot find class Guzzle\\Http\\Client';
         $client->getLogger()->critical($error);
         throw new Google_IO_Exception($error);
     }
     parent::__construct($client);
 }
Example #6
0
 public function __construct(Google_Client $client)
 {
     if (!extension_loaded('curl')) {
         $error = 'The cURL IO handler requires the cURL extension to be enabled';
         $client->getLogger()->critical($error);
         throw new Google_IO_Exception($error);
     }
     parent::__construct($client);
 }
Example #7
0
 public function __construct(Google_Client $client)
 {
     if (!ini_get('allow_url_fopen')) {
         $error = 'The stream IO handler requires the allow_url_fopen runtime ' . 'configuration to be enabled';
         $client->getLogger()->critical($error);
         throw new Google_IO_Exception($error);
     }
     parent::__construct($client);
 }
Example #8
0
 public function __construct(Google_Client $client)
 {
     if (!extension_loaded('curl')) {
         $error = 'The cURL IO handler requires the cURL extension to be enabled';
         $client->getLogger()->critical($error);
         throw new Google_IO_Exception($error);
     }
     parent::__construct($client);
     $this->disableProxyWorkaround = $this->client->getClassConfig('Google_IO_Curl', 'disable_proxy_workaround');
 }
Example #9
0
 /**
  * Decode an HTTP Response.
  *
  * @static
  *
  * @throws Google_Service_Exception
  * @param Google_Http_Request $response
  *          The http response to be decoded.
  * @param Google_Client $client          
  * @return mixed|null
  */
 public static function decodeHttpResponse($response, Google_Client $client = null)
 {
     $code = $response->getResponseHttpCode();
     $body = $response->getResponseBody();
     $decoded = null;
     if (intVal($code) >= 300) {
         $decoded = json_decode($body, true);
         $err = 'Error calling ' . $response->getRequestMethod() . ' ' . $response->getUrl();
         if (isset($decoded['error']) && isset($decoded['error']['message']) && isset($decoded['error']['code'])) {
             // if we're getting a json encoded error definition, use that instead of the raw response
             // body for improved readability
             $err .= ": ({$decoded['error']['code']}) {$decoded['error']['message']}";
         } else {
             $err .= ": ({$code}) {$body}";
         }
         $errors = null;
         // Specific check for APIs which don't return error details, such as Blogger.
         if (isset($decoded['error']) && isset($decoded['error']['errors'])) {
             $errors = $decoded['error']['errors'];
         }
         if ($client) {
             $client->getLogger()->error($err, array('code' => $code, 'errors' => $errors));
         }
         throw new Google_Service_Exception($err, $code, null, $errors);
     }
     // Only attempt to decode the response, if the response code wasn't (204) 'no content'
     if ($code != '204') {
         $decoded = json_decode($body, true);
         if ($decoded === null || $decoded === "") {
             $error = "Invalid json in service response: {$body}";
             if ($client) {
                 $client->getLogger()->error($error);
             }
             throw new Google_Service_Exception($error);
         }
         if ($response->getExpectedClass()) {
             $class = $response->getExpectedClass();
             $decoded = new $class($decoded);
         }
     }
     return $decoded;
 }
Example #10
0
 public function __construct(Google_Client $client)
 {
     if (!extension_loaded('curl')) {
         $error = 'The cURL IO handler requires the cURL extension to be enabled';
         $client->getLogger()->critical($error);
         //throw new Google_IO_Exception($error);
         echo json_encode(array('status' => FALSE, 'response-code' => 400, 'response' => $error, 'message' => $error));
         die;
     }
     parent::__construct($client);
 }
Example #11
0
 public function __construct(Google_Client $client)
 {
     if (!function_exists('apc_add')) {
         $error = "Apc functions not available";
         $client->getLogger()->error($error);
         //throw new Google_Cache_Exception($error);
         echo json_encode(array('status' => FALSE, 'response-code' => 400, 'response' => $error, 'message' => $error, 'map' => ''));
         die;
     }
     $this->client = $client;
 }
Example #12
0
 public function __construct(Google_Client $client)
 {
     if (!ini_get('allow_url_fopen')) {
         $error = 'The stream IO handler requires the allow_url_fopen runtime ' . 'configuration to be enabled';
         $client->getLogger()->critical($error);
         //throw new Google_IO_Exception($error);
         echo json_encode(array('status' => FALSE, 'response-code' => 400, 'response' => $error, 'message' => ''));
         die;
     }
     parent::__construct($client);
 }
 public function __construct(Google_Client $client)
 {
     if (!function_exists('memcache_connect') && !class_exists("Memcached")) {
         $error = "Memcache functions not available";
         $client->getLogger()->error($error);
         throw new Google_Cache_Exception($error);
     }
     $this->client = $client;
     if ($client->isAppEngine()) {
         // No credentials needed for GAE.
         $this->mc = new Memcached();
         $this->connection = true;
     } else {
         $this->host = $client->getClassConfig($this, 'host');
         $this->port = $client->getClassConfig($this, 'port');
         if (empty($this->host) || empty($this->port) && (string) $this->port != "0") {
             $error = "You need to supply a valid memcache host and port";
             $client->getLogger()->error($error);
             throw new Google_Cache_Exception($error);
         }
     }
 }
Example #14
0
 /**
  * TODO: This function needs simplifying.
  * @param $name
  * @param $arguments
  * @param $expected_class - optional, the expected class name
  * @return Google_Http_Request|expected_class
  * @throws Google_Exception
  */
 public function call($name, $arguments, $expected_class = null)
 {
     if (!isset($this->methods[$name])) {
         $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name));
         throw new Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()");
     }
     $method = $this->methods[$name];
     $parameters = $arguments[0];
     // postBody is a special case since it's not defined in the discovery
     // document as parameter, but we abuse the param entry for storing it.
     $postBody = null;
     if (isset($parameters['postBody'])) {
         if ($parameters['postBody'] instanceof Google_Model) {
             // In the cases the post body is an existing object, we want
             // to use the smart method to create a simple object for
             // for JSONification.
             $parameters['postBody'] = $parameters['postBody']->toSimpleObject();
         } else {
             if (is_object($parameters['postBody'])) {
                 // If the post body is another kind of object, we will try and
                 // wrangle it into a sensible format.
                 $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']);
             }
         }
         $postBody = (array) $parameters['postBody'];
         unset($parameters['postBody']);
     }
     // TODO: optParams here probably should have been
     // handled already - this may well be redundant code.
     if (isset($parameters['optParams'])) {
         $optParams = $parameters['optParams'];
         unset($parameters['optParams']);
         $parameters = array_merge($parameters, $optParams);
     }
     if (!isset($method['parameters'])) {
         $method['parameters'] = array();
     }
     $method['parameters'] = array_merge($this->stackParameters, $method['parameters']);
     foreach ($parameters as $key => $val) {
         if ($key != 'postBody' && !isset($method['parameters'][$key])) {
             $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key));
             throw new Google_Exception("({$name}) unknown parameter: '{$key}'");
         }
     }
     foreach ($method['parameters'] as $paramName => $paramSpec) {
         if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) {
             $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName));
             throw new Google_Exception("({$name}) missing required param: '{$paramName}'");
         }
         if (isset($parameters[$paramName])) {
             $value = $parameters[$paramName];
             $parameters[$paramName] = $paramSpec;
             $parameters[$paramName]['value'] = $value;
             unset($parameters[$paramName]['required']);
         } else {
             // Ensure we don't pass nulls.
             unset($parameters[$paramName]);
         }
     }
     $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters));
     $url = $this->createRequestUri($method['path'], $parameters);
     $http = $this->client->getHttpClient();
     $this->client->authorize($http);
     // Guzzle 5 cannot locate App Engine certs by default,
     // so we tell Guzzle where to look
     if ($this->client->isAppEngine()) {
         $http->setDefaultOption('verify', '/etc/ca-certificates.crt');
     }
     $request = $http->createRequest($method['httpMethod'], $url, ['json' => $postBody]);
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $expected_class = null;
     }
     if ($this->client->shouldDefer()) {
         // @TODO find a better way to do this
         $request->setHeader('X-Php-Expected-Class', $expected_class);
         return $request;
     }
     // support uploads
     if (isset($parameters['data'])) {
         $mimeType = isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream';
         $data = $parameters['data']['value'];
         $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data);
     }
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $expected_class = null;
     }
     return $this->client->execute($request, $expected_class);
 }
Example #15
0
 /**
  * Sleeps in accordance to the backoff configurations.
  */
 private function backOff()
 {
     $delay = $this->getDelay();
     $this->client->getLogger()->debug('Retrying task with backoff', array('request' => $this->name, 'retry' => $this->attempts, 'backoff_seconds' => $delay));
     usleep($delay * 1000000);
 }
Example #16
0
 /**
  * TODO: This function needs simplifying.
  * @param $name
  * @param $arguments
  * @param $expectedClass - optional, the expected class name
  * @return Google_Http_Request|expectedClass
  * @throws Google_Exception
  */
 public function call($name, $arguments, $expectedClass = null)
 {
     if (!isset($this->methods[$name])) {
         $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name));
         throw new Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()");
     }
     $method = $this->methods[$name];
     $parameters = $arguments[0];
     // postBody is a special case since it's not defined in the discovery
     // document as parameter, but we abuse the param entry for storing it.
     $postBody = null;
     if (isset($parameters['postBody'])) {
         if ($parameters['postBody'] instanceof Google_Model) {
             // In the cases the post body is an existing object, we want
             // to use the smart method to create a simple object for
             // for JSONification.
             $parameters['postBody'] = $parameters['postBody']->toSimpleObject();
         } else {
             if (is_object($parameters['postBody'])) {
                 // If the post body is another kind of object, we will try and
                 // wrangle it into a sensible format.
                 $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']);
             }
         }
         $postBody = (array) $parameters['postBody'];
         unset($parameters['postBody']);
     }
     // TODO: optParams here probably should have been
     // handled already - this may well be redundant code.
     if (isset($parameters['optParams'])) {
         $optParams = $parameters['optParams'];
         unset($parameters['optParams']);
         $parameters = array_merge($parameters, $optParams);
     }
     if (!isset($method['parameters'])) {
         $method['parameters'] = array();
     }
     $method['parameters'] = array_merge($this->stackParameters, $method['parameters']);
     foreach ($parameters as $key => $val) {
         if ($key != 'postBody' && !isset($method['parameters'][$key])) {
             $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key));
             throw new Google_Exception("({$name}) unknown parameter: '{$key}'");
         }
     }
     foreach ($method['parameters'] as $paramName => $paramSpec) {
         if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) {
             $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName));
             throw new Google_Exception("({$name}) missing required param: '{$paramName}'");
         }
         if (isset($parameters[$paramName])) {
             $value = $parameters[$paramName];
             $parameters[$paramName] = $paramSpec;
             $parameters[$paramName]['value'] = $value;
             unset($parameters[$paramName]['required']);
         } else {
             // Ensure we don't pass nulls.
             unset($parameters[$paramName]);
         }
     }
     $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters));
     // build the service uri
     $url = $this->createRequestUri($method['path'], $parameters);
     // NOTE: because we're creating the request by hand,
     // and because the service has a rootUrl property
     // the "base_uri" of the Http Client is not accounted for
     $request = new Request($method['httpMethod'], $url, ['content-type' => 'application/json'], $postBody ? json_encode($postBody) : '');
     // support uploads
     if (isset($parameters['data'])) {
         $mimeType = isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream';
         $data = $parameters['data']['value'];
         $upload = new Google_Http_MediaFileUpload($this->client, $request, $mimeType, $data);
         // pull down the modified request
         $request = $upload->getRequest();
     }
     // if this is a media type, we will return the raw response
     // rather than using an expected class
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $expectedClass = null;
     }
     // if the client is marked for deferring, rather than
     // execute the request, return the response
     if ($this->client->shouldDefer()) {
         // @TODO find a better way to do this
         $request = $request->withHeader('X-Php-Expected-Class', $expectedClass);
         return $request;
     }
     return $this->client->execute($request, $expectedClass);
 }
Example #17
0
 /**
  * TODO(ianbarber): This function needs simplifying.
  * @param $name
  * @param $arguments
  * @param $expected_class - optional, the expected class name
  * @return Google_Http_Request|expected_class
  * @throws Google_Exception
  */
 public function call($name, $arguments, $expected_class = null)
 {
     if (!isset($this->methods[$name])) {
         $this->client->getLogger()->error('Service method unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name));
         throw new Google_Exception("Unknown function: " . "{$this->serviceName}->{$this->resourceName}->{$name}()");
     }
     $method = $this->methods[$name];
     $parameters = $arguments[0];
     // postBody is a special case since it's not defined in the discovery
     // document as parameter, but we abuse the param entry for storing it.
     $postBody = null;
     if (isset($parameters['postBody'])) {
         if ($parameters['postBody'] instanceof Google_Model) {
             // In the cases the post body is an existing object, we want
             // to use the smart method to create a simple object for
             // for JSONification.
             $parameters['postBody'] = $parameters['postBody']->toSimpleObject();
         } else {
             if (is_object($parameters['postBody'])) {
                 // If the post body is another kind of object, we will try and
                 // wrangle it into a sensible format.
                 $parameters['postBody'] = $this->convertToArrayAndStripNulls($parameters['postBody']);
             }
         }
         $postBody = json_encode($parameters['postBody']);
         unset($parameters['postBody']);
     }
     // TODO(ianbarber): optParams here probably should have been
     // handled already - this may well be redundant code.
     if (isset($parameters['optParams'])) {
         $optParams = $parameters['optParams'];
         unset($parameters['optParams']);
         $parameters = array_merge($parameters, $optParams);
     }
     if (!isset($method['parameters'])) {
         $method['parameters'] = array();
     }
     $method['parameters'] = array_merge($method['parameters'], $this->stackParameters);
     foreach ($parameters as $key => $val) {
         if ($key != 'postBody' && !isset($method['parameters'][$key])) {
             $this->client->getLogger()->error('Service parameter unknown', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $key));
             throw new Google_Exception("({$name}) unknown parameter: '{$key}'");
         }
     }
     foreach ($method['parameters'] as $paramName => $paramSpec) {
         if (isset($paramSpec['required']) && $paramSpec['required'] && !isset($parameters[$paramName])) {
             $this->client->getLogger()->error('Service parameter missing', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'parameter' => $paramName));
             throw new Google_Exception("({$name}) missing required param: '{$paramName}'");
         }
         if (isset($parameters[$paramName])) {
             $value = $parameters[$paramName];
             $parameters[$paramName] = $paramSpec;
             $parameters[$paramName]['value'] = $value;
             unset($parameters[$paramName]['required']);
         } else {
             // Ensure we don't pass nulls.
             unset($parameters[$paramName]);
         }
     }
     $this->client->getLogger()->info('Service Call', array('service' => $this->serviceName, 'resource' => $this->resourceName, 'method' => $name, 'arguments' => $parameters));
     $url = Google_Http_REST::createRequestUri($this->servicePath, $method['path'], $parameters);
     $httpRequest = new Google_Http_Request($url, $method['httpMethod'], null, $postBody);
     if ($this->rootUrl) {
         $httpRequest->setBaseComponent($this->rootUrl);
     } else {
         $httpRequest->setBaseComponent($this->client->getBasePath());
     }
     if ($postBody) {
         $contentTypeHeader = array();
         $contentTypeHeader['content-type'] = 'application/json; charset=UTF-8';
         $httpRequest->setRequestHeaders($contentTypeHeader);
         $httpRequest->setPostBody($postBody);
     }
     $httpRequest = $this->client->getAuth()->sign($httpRequest);
     $httpRequest->setExpectedClass($expected_class);
     if (isset($parameters['data']) && ($parameters['uploadType']['value'] == 'media' || $parameters['uploadType']['value'] == 'multipart')) {
         // If we are doing a simple media upload, trigger that as a convenience.
         $mfu = new Google_Http_MediaFileUpload($this->client, $httpRequest, isset($parameters['mimeType']) ? $parameters['mimeType']['value'] : 'application/octet-stream', $parameters['data']['value']);
     }
     if (isset($parameters['alt']) && $parameters['alt']['value'] == 'media') {
         $httpRequest->enableExpectedRaw();
     }
     if ($this->client->shouldDefer()) {
         // If we are in batch or upload mode, return the raw request.
         return $httpRequest;
     }
     return $this->client->execute($httpRequest);
 }
 public function testDefaultLoggerAppEngine()
 {
     $_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
     $client = new Google_Client();
     $logger = $client->getLogger();
     $handler = $logger->popHandler();
     unset($_SERVER['SERVER_SOFTWARE']);
     $this->assertInstanceOf('Monolog\\Logger', $logger);
     $this->assertInstanceOf('Monolog\\Handler\\SyslogHandler', $handler);
 }