1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-03 17:59:55 +02:00

Added vendor directory to source control

This commit is contained in:
Daniel 2021-10-05 13:43:49 -03:00
parent 1a661614b7
commit 20cafdb5fb
397 changed files with 44799 additions and 3 deletions

@ -1 +0,0 @@
Subproject commit 7db9eb40c8ba887e81c0fe84f2888a967396cdfb

View file

@ -0,0 +1,43 @@
# Contributor Code of Conduct
As contributors and maintainers of this project,
and in the interest of fostering an open and welcoming community,
we pledge to respect all people who contribute through reporting issues,
posting feature requests, updating documentation,
submitting pull requests or patches, and other activities.
We are committed to making participation in this project
a harassment-free experience for everyone,
regardless of level of experience, gender, gender identity and expression,
sexual orientation, disability, personal appearance,
body size, race, ethnicity, age, religion, or nationality.
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information,
such as physical or electronic
addresses, without explicit permission
* Other unethical or unprofessional conduct.
Project maintainers have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions
that are not aligned to this Code of Conduct.
By adopting this Code of Conduct,
project maintainers commit themselves to fairly and consistently
applying these principles to every aspect of managing this project.
Project maintainers who do not follow or enforce the Code of Conduct
may be permanently removed from the project team.
This code of conduct applies both within project spaces and in public spaces
when an individual is representing the project or its community.
Instances of abusive, harassing, or otherwise unacceptable behavior
may be reported by opening an issue
or contacting one or more of the project maintainers.
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)

203
vendor/google/apiclient/LICENSE vendored Normal file
View file

@ -0,0 +1,203 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

477
vendor/google/apiclient/README.md vendored Normal file
View file

@ -0,0 +1,477 @@
![](https://github.com/googleapis/google-api-php-client/workflows/.github/workflows/tests.yml/badge.svg)
# Google APIs Client Library for PHP #
<dl>
<dt>Reference Docs</dt><dd><a href="https://googleapis.github.io/google-api-php-client/master/">https://googleapis.github.io/google-api-php-client/master/</a></dd>
<dt>License</dt><dd>Apache 2.0</dd>
</dl>
The Google API Client Library enables you to work with Google APIs such as Gmail, Drive or YouTube on your server.
These client libraries are officially supported by Google. However, the libraries are considered complete and are in maintenance mode. This means that we will address critical bugs and security issues but will not add any new features.
## Google Cloud Platform
For Google Cloud Platform APIs such as [Datastore][cloud-datastore], [Cloud Storage][cloud-storage], [Pub/Sub][cloud-pubsub], and [Compute Engine][cloud-compute], we recommend using the Google Cloud client libraries. For a complete list of supported Google Cloud client libraries, see [googleapis/google-cloud-php](https://github.com/googleapis/google-cloud-php).
[cloud-datastore]: https://github.com/googleapis/google-cloud-php-datastore
[cloud-pubsub]: https://github.com/googleapis/google-cloud-php-pubsub
[cloud-storage]: https://github.com/googleapis/google-cloud-php-storage
[cloud-compute]: https://github.com/googleapis/google-cloud-php-compute
## Requirements ##
* [PHP 5.6.0 or higher](https://www.php.net/)
## Developer Documentation ##
The [docs folder](docs/) provides detailed guides for using this library.
## Installation ##
You can use **Composer** or simply **Download the Release**
### Composer
The preferred method is via [composer](https://getcomposer.org/). Follow the
[installation instructions](https://getcomposer.org/doc/00-intro.md) if you do not already have
composer installed.
Once composer is installed, execute the following command in your project root to install this library:
```sh
composer require google/apiclient:^2.11
```
Finally, be sure to include the autoloader:
```php
require_once '/path/to/your-project/vendor/autoload.php';
```
This library relies on `google/apiclient-services`. That library provides up-to-date API wrappers for a large number of Google APIs. In order that users may make use of the latest API clients, this library does not pin to a specific version of `google/apiclient-services`. **In order to prevent the accidental installation of API wrappers with breaking changes**, it is highly recommended that you pin to the [latest version](https://github.com/googleapis/google-api-php-client-services/releases) yourself prior to using this library in production.
#### Cleaning up unused services
There are over 200 Google API services. The chances are good that you will not
want them all. In order to avoid shipping these dependencies with your code,
you can run the `Google\Task\Composer::cleanup` task and specify the services
you want to keep in `composer.json`:
```json
{
"require": {
"google/apiclient": "^2.11"
},
"scripts": {
"pre-autoload-dump": "Google\\Task\\Composer::cleanup"
},
"extra": {
"google/apiclient-services": [
"Drive",
"YouTube"
]
}
}
```
This example will remove all services other than "Drive" and "YouTube" when
`composer update` or a fresh `composer install` is run.
**IMPORTANT**: If you add any services back in `composer.json`, you will need to
remove the `vendor/google/apiclient-services` directory explicity for the
change you made to have effect:
```sh
rm -r vendor/google/apiclient-services
composer update
```
**NOTE**: This command performs an exact match on the service name, so to keep
`YouTubeReporting` and `YouTubeAnalytics` as well, you'd need to add each of
them explicitly:
```json
{
"extra": {
"google/apiclient-services": [
"Drive",
"YouTube",
"YouTubeAnalytics",
"YouTubeReporting"
]
}
}
```
### Download the Release
If you prefer not to use composer, you can download the package in its entirety. The [Releases](https://github.com/googleapis/google-api-php-client/releases) page lists all stable versions. Download any file
with the name `google-api-php-client-[RELEASE_NAME].zip` for a package including this library and its dependencies.
Uncompress the zip file you download, and include the autoloader in your project:
```php
require_once '/path/to/google-api-php-client/vendor/autoload.php';
```
For additional installation and setup instructions, see [the documentation](docs/).
## Examples ##
See the [`examples/`](examples) directory for examples of the key client features. You can
view them in your browser by running the php built-in web server.
```
$ php -S localhost:8000 -t examples/
```
And then browsing to the host and port you specified
(in the above example, `http://localhost:8000`).
### Basic Example ###
```php
// include your composer dependencies
require_once 'vendor/autoload.php';
$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");
$service = new Google\Service\Books($client);
$query = 'Henry David Thoreau';
$optParams = [
'filter' => 'free-ebooks',
];
$results = $service->volumes->listVolumes($query, $optParams);
foreach ($results->getItems() as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
```
### Authentication with OAuth ###
> An example of this can be seen in [`examples/simple-file-upload.php`](examples/simple-file-upload.php).
1. Follow the instructions to [Create Web Application Credentials](docs/oauth-web.md#create-authorization-credentials)
1. Download the JSON credentials
1. Set the path to these credentials using `Google\Client::setAuthConfig`:
```php
$client = new Google\Client();
$client->setAuthConfig('/path/to/client_credentials.json');
```
1. Set the scopes required for the API you are going to call
```php
$client->addScope(Google\Service\Drive::DRIVE);
```
1. Set your application's redirect URI
```php
// Your redirect URI can be any registered URI, but in this example
// we redirect back to this same page
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client->setRedirectUri($redirect_uri);
```
1. In the script handling the redirect URI, exchange the authorization code for an access token:
```php
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
}
```
### Authentication with Service Accounts ###
> An example of this can be seen in [`examples/service-account.php`](examples/service-account.php).
Some APIs
(such as the [YouTube Data API](https://developers.google.com/youtube/v3/)) do
not support service accounts. Check with the specific API documentation if API
calls return unexpected 401 or 403 errors.
1. Follow the instructions to [Create a Service Account](docs/oauth-server.md#creating-a-service-account)
1. Download the JSON credentials
1. Set the path to these credentials using the `GOOGLE_APPLICATION_CREDENTIALS` environment variable:
```php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
```
1. Tell the Google client to use your service account credentials to authenticate:
```php
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
```
1. Set the scopes required for the API you are going to call
```php
$client->addScope(Google\Service\Drive::DRIVE);
```
1. If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method setSubject:
```php
$client->setSubject($user_to_impersonate);
```
#### How to use a specific JSON key
If you want to a specific JSON key instead of using `GOOGLE_APPLICATION_CREDENTIALS` environment variable, you can do this:
```php
$jsonKey = [
'type' => 'service_account',
// ...
];
$client = new Google\Client();
$client->setAuthConfig($jsonKey);
```
### Making Requests ###
The classes used to call the API in [google-api-php-client-services](https://github.com/googleapis/google-api-php-client-services) are autogenerated. They map directly to the JSON requests and responses found in the [APIs Explorer](https://developers.google.com/apis-explorer/#p/).
A JSON request to the [Datastore API](https://developers.google.com/apis-explorer/#p/datastore/v1beta3/datastore.projects.runQuery) would look like this:
```json
POST https://datastore.googleapis.com/v1beta3/projects/YOUR_PROJECT_ID:runQuery?key=YOUR_API_KEY
{
"query": {
"kind": [{
"name": "Book"
}],
"order": [{
"property": {
"name": "title"
},
"direction": "descending"
}],
"limit": 10
}
}
```
Using this library, the same call would look something like this:
```php
// create the datastore service class
$datastore = new Google\Service\Datastore($client);
// build the query - this maps directly to the JSON
$query = new Google\Service\Datastore\Query([
'kind' => [
[
'name' => 'Book',
],
],
'order' => [
'property' => [
'name' => 'title',
],
'direction' => 'descending',
],
'limit' => 10,
]);
// build the request and response
$request = new Google\Service\Datastore\RunQueryRequest(['query' => $query]);
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
```
However, as each property of the JSON API has a corresponding generated class, the above code could also be written like this:
```php
// create the datastore service class
$datastore = new Google\Service\Datastore($client);
// build the query
$request = new Google\Service\Datastore_RunQueryRequest();
$query = new Google\Service\Datastore\Query();
// - set the order
$order = new Google\Service\Datastore_PropertyOrder();
$order->setDirection('descending');
$property = new Google\Service\Datastore\PropertyReference();
$property->setName('title');
$order->setProperty($property);
$query->setOrder([$order]);
// - set the kinds
$kind = new Google\Service\Datastore\KindExpression();
$kind->setName('Book');
$query->setKinds([$kind]);
// - set the limit
$query->setLimit(10);
// add the query to the request and make the request
$request->setQuery($query);
$response = $datastore->projects->runQuery('YOUR_DATASET_ID', $request);
```
The method used is a matter of preference, but *it will be very difficult to use this library without first understanding the JSON syntax for the API*, so it is recommended to look at the [APIs Explorer](https://developers.google.com/apis-explorer/#p/) before using any of the services here.
### Making HTTP Requests Directly ###
If Google Authentication is desired for external applications, or a Google API is not available yet in this library, HTTP requests can be made directly.
If you are installing this client only to authenticate your own HTTP client requests, you should use [`google/auth`](https://github.com/googleapis/google-auth-library-php#call-the-apis) instead.
The `authorize` method returns an authorized [Guzzle Client](http://docs.guzzlephp.org/), so any request made using the client will contain the corresponding authorization.
```php
// create the Google client
$client = new Google\Client();
/**
* Set your method for authentication. Depending on the API, This could be
* directly with an access token, API key, or (recommended) using
* Application Default Credentials.
*/
$client->useApplicationDefaultCredentials();
$client->addScope(Google\Service\Plus::PLUS_ME);
// returns a Guzzle HTTP Client
$httpClient = $client->authorize();
// make an HTTP request
$response = $httpClient->get('https://www.googleapis.com/plus/v1/people/me');
```
### Caching ###
It is recommended to use another caching library to improve performance. This can be done by passing a [PSR-6](https://www.php-fig.org/psr/psr-6/) compatible library to the client:
```php
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
$filesystemAdapter = new Local(__DIR__.'/');
$filesystem = new Filesystem($filesystemAdapter);
$cache = new FilesystemCachePool($filesystem);
$client->setCache($cache);
```
In this example we use [PHP Cache](http://www.php-cache.com/). Add this to your project with composer:
```
composer require cache/filesystem-adapter
```
### Updating Tokens ###
When using [Refresh Tokens](https://developers.google.com/identity/protocols/OAuth2InstalledApp#offline) or [Service Account Credentials](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#overview), it may be useful to perform some action when a new access token is granted. To do this, pass a callable to the `setTokenCallback` method on the client:
```php
$logger = new Monolog\Logger();
$tokenCallback = function ($cacheKey, $accessToken) use ($logger) {
$logger->debug(sprintf('new access token received at cache key %s', $cacheKey));
};
$client->setTokenCallback($tokenCallback);
```
### Debugging Your HTTP Request using Charles ###
It is often very useful to debug your API calls by viewing the raw HTTP request. This library supports the use of [Charles Web Proxy](https://www.charlesproxy.com/documentation/getting-started/). Download and run Charles, and then capture all HTTP traffic through Charles with the following code:
```php
// FOR DEBUGGING ONLY
$httpClient = new GuzzleHttp\Client([
'proxy' => 'localhost:8888', // by default, Charles runs on localhost port 8888
'verify' => false, // otherwise HTTPS requests will fail.
]);
$client = new Google\Client();
$client->setHttpClient($httpClient);
```
Now all calls made by this library will appear in the Charles UI.
One additional step is required in Charles to view SSL requests. Go to **Charles > Proxy > SSL Proxying Settings** and add the domain you'd like captured. In the case of the Google APIs, this is usually `*.googleapis.com`.
### Controlling HTTP Client Configuration Directly
Google API Client uses [Guzzle](http://docs.guzzlephp.org/) as its default HTTP client. That means that you can control your HTTP requests in the same manner you would for any application using Guzzle.
Let's say, for instance, we wished to apply a referrer to each request.
```php
use GuzzleHttp\Client;
$httpClient = new Client([
'headers' => [
'referer' => 'mysite.com'
]
]);
$client = new Google\Client();
$client->setHttpClient($httpClient);
```
Other Guzzle features such as [Handlers and Middleware](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) offer even more control.
### Service Specific Examples ###
YouTube: https://github.com/youtube/api-samples/tree/master/php
## How Do I Contribute? ##
Please see the [contributing](.github/CONTRIBUTING.md) page for more information. In particular, we love pull requests - but please make sure to sign the contributor license agreement.
## Frequently Asked Questions ##
### What do I do if something isn't working? ###
For support with the library the best place to ask is via the google-api-php-client tag on StackOverflow: https://stackoverflow.com/questions/tagged/google-api-php-client
If there is a specific bug with the library, please [file an issue](https://github.com/googleapis/google-api-php-client/issues) in the GitHub issues tracker, including an example of the failing code and any specific errors retrieved. Feature requests can also be filed, as long as they are core library requests, and not-API specific: for those, refer to the documentation for the individual APIs for the best place to file requests. Please try to provide a clear statement of the problem that the feature would address.
### I want an example of X! ###
If X is a feature of the library, file away! If X is an example of using a specific service, the best place to go is to the teams for those specific APIs - our preference is to link to their examples rather than add them to the library, as they can then pin to specific versions of the library. If you have any examples for other APIs, let us know and we will happily add a link to the README above!
### Why do some Google\Service classes have weird names? ###
The _Google\Service_ classes are generally automatically generated from the API discovery documents: https://developers.google.com/discovery/. Sometimes new features are added to APIs with unusual names, which can cause some unexpected or non-standard style naming in the PHP classes.
### How do I deal with non-JSON response types? ###
Some services return XML or similar by default, rather than JSON, which is what the library supports. You can request a JSON response by adding an 'alt' argument to optional params that is normally the last argument to a method call:
```
$opt_params = array(
'alt' => "json"
);
```
### How do I set a field to null? ###
The library strips out nulls from the objects sent to the Google APIs as its the default value of all of the uninitialized properties. To work around this, set the field you want to null to `Google\Model::NULL_VALUE`. This is a placeholder that will be replaced with a true null when sent over the wire.
## Code Quality ##
Run the PHPUnit tests with PHPUnit. You can configure an API key and token in BaseTest.php to run all calls, but this will require some setup on the Google Developer Console.
phpunit tests/
### Coding Style
To check for coding style violations, run
```
vendor/bin/phpcs src --standard=style/ruleset.xml -np
```
To automatically fix (fixable) coding style violations, run
```
vendor/bin/phpcbf src --standard=style/ruleset.xml
```

7
vendor/google/apiclient/SECURITY.md vendored Normal file
View file

@ -0,0 +1,7 @@
# Security Policy
To report a security issue, please use [g.co/vulnz](https://g.co/vulnz).
The Google Security Team will respond within 5 working days of your report on g.co/vulnz.
We use g.co/vulnz for our intake, and do coordination and disclosure here using GitHub Security Advisory to privately discuss and fix the issue.

377
vendor/google/apiclient/UPGRADING.md vendored Normal file
View file

@ -0,0 +1,377 @@
Google API Client Upgrade Guide
===============================
2.x to 2.10.0
-------------
### Namespaces
The Google API Client for PHP now uses namespaces for all classes. Code using
the legacy classnames will continue to work, but it is advised to upgrade to the
underspaced names, as the legacy classnames will be deprecated some time in the
future.
**Before**
```php
$client = new Google_Client();
$service = new Google_Service_Books($client);
```
**After**
```php
$client = new Google\Client();
$service = new Google\Service\Books($client);
```
### Service class constructors
Service class constructors now accept an optional `Google\Client|array` parameter
as their first argument, rather than requiring an instance of `Google\Client`.
**Before**
```php
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("YOUR_APP_KEY");
$service = new Google_Service_Books($client);
```
**After**
```php
$service = new Google\Service\Books([
'application_name' => "Client_Library_Examples",
'developer_key' => "YOUR_APP_KEY",
]);
```
1.0 to 2.0
----------
The Google API Client for PHP has undergone major internal changes in `2.0`. Please read through
the list below in order to upgrade to the latest version:
## Installation now uses `Composer`
**Before**
The project was cloned in your project and you would run the autoloader from wherever:
```php
// the autoload file was included
require_once 'google-api-php-client/src/Google/autoload.php'; // or wherever autoload.php is located
// OR classes were added one-by-one
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
```
**After**
This library now uses [composer](https://getcomposer.org) (We suggest installing
composer [globally](http://symfony.com/doc/current/cookbook/composer.html)). Add this library by
running the following in the root of your project:
```
$ composer require google/apiclient:~2.0
```
This will install this library and generate an autoload file in `vendor/autoload.php` in the root
of your project. You can now include this library with the following code:
```php
require_once 'vendor/autoload.php';
```
## Access Tokens are passed around as arrays instead of JSON strings
**Before**
```php
$accessToken = $client->getAccessToken();
print_r($accessToken);
// would output:
// string(153) "{"access_token":"ya29.FAKsaByOPoddfzvKRo_LBpWWCpVTiAm4BjsvBwxtN7IgSNoUfcErBk_VPl4iAiE1ntb_","token_type":"Bearer","expires_in":3593,"created":1445548590}"
file_put_contents($credentialsPath, $accessToken);
```
**After**
```php
$accessToken = $client->getAccessToken();
print_r($accessToken);
// will output:
// array(4) {
// ["access_token"]=>
// string(73) "ya29.FAKsaByOPoddfzvKRo_LBpWWCpVTiAm4BjsvBwxtN7IgSNoUfcErBk_VPl4iAiE1ntb_"
// ["token_type"]=>
// string(6) "Bearer"
// ["expires_in"]=>
// int(3593)
// ["created"]=>
// int(1445548590)
// }
file_put_contents($credentialsPath, json_encode($accessToken));
```
## ID Token data is returned as an array
**Before**
```php
$ticket = $client->verifyIdToken($idToken);
$data = $ticket->getAttributes();
$userId = $data['payload']['sub'];
```
**After**
```php
$userData = $client->verifyIdToken($idToken);
$userId = $userData['sub'];
```
## `Google_Auth_AssertionCredentials` has been removed
For service accounts, we now use `setAuthConfig` or `useApplicationDefaultCredentials`
**Before**
```php
$client_email = '1234567890-a1b2c3d4e5f6g7h8i@developer.gserviceaccount.com';
$private_key = file_get_contents('MyProject.p12');
$scopes = array('https://www.googleapis.com/auth/sqlservice.admin');
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
```
**After**
```php
$client->setAuthConfig('/path/to/service-account.json');
// OR use environment variables (recommended)
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client->useApplicationDefaultCredentials();
```
> Note: P12s are deprecated in favor of service account JSON, which can be generated in the
> Credentials section of Google Developer Console.
In order to impersonate a user, call `setSubject` when your service account
credentials are being used.
**Before**
```php
$user_to_impersonate = 'user@example.org';
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key,
'notasecret', // Default P12 password
'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type
$user_to_impersonate,
);
```
**After**
```php
$user_to_impersonate = 'user@example.org';
$client->setSubject($user_to_impersonate);
```
Additionally, `Google_Client::loadServiceAccountJson` has been removed in favor
of `Google_Client::setAuthConfig`:
**Before**
```php
$scopes = [ Google_Service_Books::BOOKS ];
$client->loadServiceAccountJson('/path/to/service-account.json', $scopes);
```
**After**
```php
$scopes = [ Google_Service_Books::BOOKS ];
$client->setAuthConfig('/path/to/service-account.json');
$client->setScopes($scopes);
```
## `Google_Auth_AppIdentity` has been removed
For App Engine authentication, we now use the underlying [`google/auth`][Google Auth] and
call `useApplicationDefaultCredentials`:
**Before**
```php
$client->setAuth(new Google_Auth_AppIdentity($client));
$client->getAuth()
->authenticateForScope('https://www.googleapis.com/auth/sqlservice.admin')
```
**After**
```php
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/sqlservice.admin');
```
This will detect when the App Engine environment is present, and use the appropriate credentials.
## `Google_Auth_Abstract` classes have been removed
[`google/auth`][Google Auth] is now used for authentication. As a result, all
`Google_Auth`-related functionality has been removed. The methods that were a part of
`Google_Auth_Abstract` have been moved into the `Google_Client` object.
**Before**
```php
$request = new Google_Http_Request();
$client->getAuth()->sign($request);
```
**After**
```php
// create an authorized HTTP client
$httpClient = $client->authorize();
// OR add authorization to an existing client
$httpClient = new GuzzleHttp\Client();
$httpClient = $client->authorize($httpClient);
```
**Before**
```php
$request = new Google_Http_Request();
$response = $client->getAuth()->authenticatedRequest($request);
```
**After**
```php
$httpClient = $client->authorize();
$request = new GuzzleHttp\Psr7\Request('POST', $url);
$response = $httpClient->send($request);
```
> NOTE: `$request` can be any class implementing
> `Psr\Http\Message\RequestInterface`
In addition, other methods that were callable on `Google_Auth_OAuth2` are now called
on the `Google_Client` object:
**Before**
```php
$client->getAuth()->refreshToken($token);
$client->getAuth()->refreshTokenWithAssertion();
$client->getAuth()->revokeToken($token);
$client->getAuth()->isAccessTokenExpired();
```
**After**
```php
$client->refreshToken($token);
$client->refreshTokenWithAssertion();
$client->revokeToken($token);
$client->isAccessTokenExpired();
```
## PHP 5.6 is now the minimum supported PHP version
This was previously `PHP 5.2`. If you still need to use PHP 5.2, please continue to use
the [v1-master](https://github.com/google/google-api-php-client/tree/v1-master) branch.
## Guzzle and PSR-7 are used for HTTP Requests
The HTTP library Guzzle is used for all HTTP Requests. By default, [`Guzzle 6`][Guzzle 6]
is used, but this library is also compatible with [`Guzzle 5`][Guzzle 5]. As a result,
all `Google_IO`-related functionality and `Google_Http`-related functionality has been
changed or removed.
1. Removed `Google_Http_Request`
1. Removed `Google_IO_Abstract`, `Google_IO_Exception`, `Google_IO_Curl`, and `Google_IO_Stream`
1. Removed methods `Google_Client::getIo` and `Google_Client::setIo`
1. Refactored `Google_Http_Batch` and `Google_Http_MediaFileUpload` for Guzzle
1. Added `Google_Client::getHttpClient` and `Google_Client::setHttpClient` for getting and
setting the Guzzle `GuzzleHttp\ClientInterface` object.
> NOTE: `PSR-7`-compatible libraries can now be used with this library.
## Other Changes
- [`PSR 3`][PSR 3] `LoggerInterface` is now supported, and [Monolog][Monolog] is used for all
logging. As a result, all `Google_Logger`-related functionality has been removed:
1. Removed `Google_Logger_Abstract`, `Google_Logger_Exception`, `Google_Logger_File`,
`Google_Logger_Null`, and `Google_Logger_Psr`
1. `Google_Client::setLogger` now requires `Psr\Log\LoggerInterface`
- [`firebase/jwt`][Firebase JWT] is now used for all JWT signing and verifying. As a result, the
following classes have been changed or removed:
1. Removed `Google_Signer_P12`
1. Removed `Google_Verifier_Pem`
1. Removed `Google_Auth_LoginTicket` (see below)
- The following classes and methods have been removed in favor of [`google/auth`][Google Auth]:
1. Removed methods `Google_Client::getAuth` and `Google_Client::setAuth`
1. Removed `Google_Auth_Abstract`
- `Google_Auth_Abstract::sign` and `Google_Auth_Abstract::authenticatedRequest` have been
replaced by `Google_Client::authorize`. See the above examples for more details.
1. Removed `Google_Auth_AppIdentity`. This is now supported in [`google/auth`][Google Auth AppIdentity]
and is used automatically when `Google_Client::useApplicationDefaultCredentials` is called.
1. Removed `Google_Auth_AssertionCredentials`. Use `Google_Client::setAuthConfig` instead.
1. Removed `Google_Auth_ComputeEngine`. This is now supported in
[`google/auth`][Google Auth GCE], and is used automatically when
`Google_Client::useApplicationDefaultCredentials` is called.
1. Removed `Google_Auth_Exception`
1. Removed `Google_Auth_LoginTicket`. Calls to `Google_Client::verifyIdToken` now returns
the payload of the ID Token as an array if the verification is successful.
1. Removed `Google_Auth_OAuth2`. This functionality is now supported in [`google/auth`][Google Auth OAuth2] and wrapped in `Google_Client`. These changes will only affect applications calling `Google_Client::getAuth`,
as the methods on `Google_Client` have not changed.
1. Removed `Google_Auth_Simple`. This is now supported in [`google/auth`][Google Auth Simple]
and is used automatically when `Google_Client::setDeveloperKey` is called.
- `Google_Client::sign` has been replaced by `Google_Client::authorize`. This function
now takes a `GuzzleHttp\ClientInterface` object and uses the following decision tree for
authentication:
1. Uses Application Default Credentials when
`Google_Client::useApplicationDefaultCredentials` is called
- Looks for `GOOGLE_APPLICATION_CREDENTIALS` environment variable if set
- Looks in `~/.config/gcloud/application_default_credentials.json`
- Otherwise, uses `GCECredentials`
1. Uses API Key if set (see `Client::setDeveloperKey`)
1. Uses Access Token if set (call `Client::setAccessToken`)
1. Automatically refreshes access tokens if one is set and the access token is expired
- Removed `Google_Config`
- Removed `Google_Utils`
- [`PSR-6`][PSR 6] cache is used for all caching. As a result:
1. Removed `Google_Cache_Abstract`
1. Classes `Google_Cache_Apc`, `Google_Cache_File`, `Google_Cache_Memcache`, and
`Google_Cache_Null` now implement `Google\Auth\CacheInterface`.
1. Google Auth provides simple [caching utilities][Google Auth Cache] which
are used by default unless you provide alternatives.
- Removed `$boundary` constructor argument for `Google_Http_MediaFileUpload`
[PSR 3]: https://www.php-fig.org/psr/psr-3/
[PSR 6]: https://www.php-fig.org/psr/psr-6/
[Guzzle 5]: https://github.com/guzzle/guzzle
[Guzzle 6]: http://docs.guzzlephp.org/en/latest/psr7.html
[Monolog]: https://github.com/Seldaek/monolog
[Google Auth]: https://github.com/google/google-auth-library-php
[Google Auth Cache]: https://github.com/googleapis/google-auth-library-php/tree/master/src/Cache
[Google Auth GCE]: https://github.com/google/google-auth-library-php/blob/master/src/GCECredentials.php
[Google Auth OAuth2]: https://github.com/google/google-auth-library-php/blob/master/src/OAuth2.php
[Google Auth Simple]: https://github.com/google/google-auth-library-php/blob/master/src/Simple.php
[Google Auth AppIdentity]: https://github.com/google/google-auth-library-php/blob/master/src/AppIdentityCredentials.php
[Firebase JWT]: https://github.com/firebase/php-jwt

49
vendor/google/apiclient/composer.json vendored Normal file
View file

@ -0,0 +1,49 @@
{
"name": "google/apiclient",
"type": "library",
"description": "Client library for Google APIs",
"keywords": ["google"],
"homepage": "http://developers.google.com/api-client-library/php",
"license": "Apache-2.0",
"require": {
"php": "^5.6|^7.0|^8.0",
"google/auth": "^1.10",
"google/apiclient-services": "~0.200",
"firebase/php-jwt": "~2.0||~3.0||~4.0||~5.0",
"monolog/monolog": "^1.17||^2.0",
"phpseclib/phpseclib": "~2.0||^3.0.2",
"guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0",
"guzzlehttp/psr7": "^1.7||^2.0.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "~2.3",
"symfony/dom-crawler": "~2.1",
"symfony/css-selector": "~2.1",
"cache/filesystem-adapter": "^0.3.2|^1.1",
"phpcompatibility/php-compatibility": "^9.2",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7",
"composer/composer": "^1.10.22",
"yoast/phpunit-polyfills": "^1.0",
"phpspec/prophecy-phpunit": "^1.1||^2.0",
"phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0"
},
"suggest": {
"cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)"
},
"autoload": {
"psr-4": {
"Google\\": "src/"
},
"files": [
"src/aliases.php"
],
"classmap": [
"src/aliases.php"
]
},
"extra": {
"branch-alias": {
"dev-master": "2.x-dev"
}
}
}

15
vendor/google/apiclient/docs/README.md vendored Normal file
View file

@ -0,0 +1,15 @@
# Google API Client Library for PHP Docs
The Google API Client Library for PHP offers simple, flexible access to many Google APIs.
## Documentation
- [Getting Started](start.md)
- [API Keys](api-keys.md)
- [Auth](auth.md)
- [Installation](install.md)
- [Media](media.md)
- [OAuth Server](oauth-server.md)
- [OAuth Web](oauth-web.md)
- [Pagination](pagination.md)
- [Parameters](parameters.md)

View file

@ -0,0 +1,13 @@
# API Keys
When calling APIs that do not access private user data, you can use simple API keys. These keys are used to authenticate your application for accounting purposes. The Google Developers Console documentation also describes [API keys](https://developers.google.com/console/help/using-keys).
> Note: If you do need to access private user data, you must use OAuth 2.0. See [Using OAuth 2.0 for Web Server Applications](/docs/oauth-web.md) and [Using OAuth 2.0 for Server to Server Applications](/docs/oauth-server.md) for more information.
## Using API Keys
To use API keys, call the `setDeveloperKey()` method of the `Google\Client` object before making any API calls. For example:
```php
$client->setDeveloperKey($api_key);
```

11
vendor/google/apiclient/docs/auth.md vendored Normal file
View file

@ -0,0 +1,11 @@
# Authorization
The Google PHP Client Library supports several methods for making authenticated calls to the Google APIs.
- [API Keys](api-keys.md)
- [OAuth 2.0 For Webservers](oauth-web.md)
- [OAuth 2.0 Service Accounts](oauth-server.md)
In addition, it supports a method of identifying users without granting access to make Google API calls.
- [ID Token Verification](id-token.md)

View file

@ -0,0 +1,22 @@
# ID Token Authentication
ID tokens allow authenticating a user securely without requiring a network call (in many cases), and without granting the server access to request user information from the Google APIs.
> For a complete example, see the [idtoken.php](https://github.com/googleapis/google-api-php-client/blob/master/examples/idtoken.php) sample in the examples/ directory of the client library.
This is accomplished because each ID token is a cryptographically signed, base64 encoded JSON structure. The token payload includes the Google user ID, the client ID of the application the user signed in to, and the issuer (in this case, Google). It also contains a cryptographic signature which can be verified with the public Google certificates to ensure that the token was created by Google. If the user has granted permission to view their email address to the application, the ID token will additionally include their email address.
The token can be easily and securely verified with the PHP client library
```php
function getUserFromToken($token) {
$ticket = $client->verifyIdToken($token);
if ($ticket) {
$data = $ticket->getAttributes();
return $data['payload']['sub']; // user ID
}
return false
}
```
The library will automatically download and cache the certificate required for verification, and refresh it if it has expired.

39
vendor/google/apiclient/docs/install.md vendored Normal file
View file

@ -0,0 +1,39 @@
# Installation
This page contains information about installing the Google APIs Client Library for PHP.
## Requirements
* PHP version 5.6 or greater.
## Obtaining the client library
There are two options for obtaining the files for the client library.
### Using Composer
You can install the library by adding it as a dependency to your composer.json.
```json
"require": {
"google/apiclient": "^2.0"
}
```
### Downloading from GitHub
Follow [the instructions in the README](https://github.com/google/google-api-php-client#download-the-release) to download the package locally.
### What to do with the files
After obtaining the files, include the autoloader. If you used Composer, your require statement will look like this:
```php
require_once '/path/to/your-project/vendor/autoload.php';
```
If you downloaded the package separately, your require statement will look like this:
```php
require_once '/path/to/google-api-php-client/vendor/autoload.php';
```

75
vendor/google/apiclient/docs/media.md vendored Normal file
View file

@ -0,0 +1,75 @@
# Media Upload
The PHP client library allows for uploading large files for use with APIs such as Drive or YouTube. There are three different methods available.
## Simple Upload
In the simple upload case, the data is passed as the body of the request made to the server. This limits the ability to specify metadata, but is very easy to use.
```php
$file = new Google\Service\Drive\DriveFile();
$result = $service->files->insert($file, array(
'data' => file_get_contents("path/to/file"),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media'
));
```
## Multipart File Upload
With multipart file uploads, the uploaded file is sent as one part of a multipart form post. This allows metadata about the file object to be sent as part of the post as well. This is triggered by specifying the _multipart_ uploadType.
```php
$file = new Google\Service\Drive\DriveFile();
$file->setTitle("Hello World!");
$result = $service->files->insert($file, array(
'data' => file_get_contents("path/to/file"),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
));
```
## Resumable File Upload
It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata.
```php
$file = new Google\Service\Drive\DriveFile();
$file->title = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->insert($file);
// Create a media file upload to represent our upload process.
$media = new Google\Http\MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize("path/to/file"));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen("path/to/file", "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
$result = $status;
}
fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
```

View file

@ -0,0 +1,140 @@
# Using OAuth 2.0 for Server to Server Applications
The Google APIs Client Library for PHP supports using OAuth 2.0 for server-to-server interactions such as those between a web application and a Google service. For this scenario you need a service account, which is an account that belongs to your application instead of to an individual end user. Your application calls Google APIs on behalf of the service account, so users aren't directly involved. This scenario is sometimes called "two-legged OAuth," or "2LO." (The related term "three-legged OAuth" refers to scenarios in which your application calls Google APIs on behalf of end users, and in which user consent is sometimes required.)
Typically, an application uses a service account when the application uses Google APIs to work with its own data rather than a user's data. For example, an application that uses [Google Cloud Datastore](https://cloud.google.com/datastore/) for data persistence would use a service account to authenticate its calls to the Google Cloud Datastore API.
If you have a G Suite domain—if you use [G Suite Business](https://gsuite.google.com), for example—an administrator of the G Suite domain can authorize an application to access user data on behalf of users in the G Suite domain. For example, an application that uses the [Google Calendar API](https://developers.google.com/google-apps/calendar/) to add events to the calendars of all users in a G Suite domain would use a service account to access the Google Calendar API on behalf of users. Authorizing a service account to access data on behalf of users in a domain is sometimes referred to as "delegating domain-wide authority" to a service account.
> **Note:** When you use [G Suite Marketplace](https://www.google.com/enterprise/marketplace/) to install an application for your domain, the required permissions are automatically granted to the application. You do not need to manually authorize the service accounts that the application uses.
> **Note:** Although you can use service accounts in applications that run from a G Suite domain, service accounts are not members of your G Suite account and aren't subject to domain policies set by G Suite administrators. For example, a policy set in the G Suite admin console to restrict the ability of Apps end users to share documents outside of the domain would not apply to service accounts.
This document describes how an application can complete the server-to-server OAuth 2.0 flow by using the Google APIs Client Library for PHP.
## Overview
To support server-to-server interactions, first create a service account for your project in the Developers Console. If you want to access user data for users in your G Suite domain, then delegate domain-wide access to the service account.
Then, your application prepares to make authorized API calls by using the service account's credentials to request an access token from the OAuth 2.0 auth server.
Finally, your application can use the access token to call Google APIs.
## Creating a service account
A service account's credentials include a generated email address that is unique, a client ID, and at least one public/private key pair.
If your application runs on Google App Engine, a service account is set up automatically when you create your project.
If your application doesn't run on Google App Engine or Google Compute Engine, you must obtain these credentials in the Google Developers Console. To generate service-account credentials, or to view the public credentials that you've already generated, do the following:
1. Open the [**Service accounts** section](https://console.developers.google.com/permissions/serviceaccounts?project=_) of the Developers Console's **Permissions** page.
2. Click **Create service account**.
3. In the **Create service account** window, type a name for the service account and select **Furnish a new private key**. If you want to [grant G Suite domain-wide authority](https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) to the service account, also select **Enable G Suite Domain-wide Delegation**. Then, click **Create**.
Your new public/private key pair is generated and downloaded to your machine; it serves as the only copy of this key. You are responsible for storing it securely.
You can return to the [Developers Console](https://console.developers.google.com/) at any time to view the client ID, email address, and public key fingerprints, or to generate additional public/private key pairs. For more details about service account credentials in the Developers Console, see [Service accounts](https://developers.google.com/console/help/service-accounts) in the Developers Console help file.
Take note of the service account's email address and store the service account's private key file in a location accessible to your application. Your application needs them to make authorized API calls.
**Note:** You must store and manage private keys securely in both development and production environments. Google does not keep a copy of your private keys, only your public keys.
### Delegating domain-wide authority to the service account
If your application runs in a G Suite domain and accesses user data, the service account that you created needs to be granted access to the user data that you want to access.
The following steps must be performed by an administrator of the G Suite domain:
1. Go to your G Suite domains [Admin console](http://admin.google.com).
2. Select **Security** from the list of controls. If you don't see **Security** listed, select **More controls** from the gray bar at the bottom of the page, then select **Security** from the list of controls. If you can't see the controls, make sure you're signed in as an administrator for the domain.
3. Select **Advanced settings** from the list of options.
4. Select **Manage third party OAuth Client access** in the **Authentication** section.
5. In the **Client name** field enter the service account's **Client ID**.
6. In the **One or More API Scopes** field enter the list of scopes that your application should be granted access to. For example, if your application needs domain-wide access to the Google Drive API and the Google Calendar API, enter: https://www.googleapis.com/auth/drive, https://www.googleapis.com/auth/calendar.
7. Click **Authorize**.
Your application now has the authority to make API calls as users in your domain (to "impersonate" users). When you prepare to make authorized API calls, you specify the user to impersonate.
[](#top_of_page)Preparing to make an authorized API call
--------------------------------------------------------
After you have obtained the client email address and private key from the Developers Console, set the path to these credentials in the `GOOGLE_APPLICATION_CREDENTIALS` environment variable ( **Note:** This is not required in the App Engine environment):
```php
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
```
Call the `useApplicationDefaultCredentials` to use your service account credentials to authenticate:
```php
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
```
If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account using the method `setSubject`:
```php
$client->setSubject($user_to_impersonate);
```
Use the authorized `Google\Client` object to call Google APIs in your application.
## Calling Google APIs
Use the authorized `Google\Client` object to call Google APIs by completing the following steps:
1. Build a service object for the API that you want to call, providing the authorized `Google\Client` object. For example, to call the Cloud SQL Administration API:
```php
$sqladmin = new Google\Service\SQLAdmin($client);
```
2. Make requests to the API service using the [interface provided by the service object](https://github.com/googleapis/google-api-php-client/blob/master/docs/start.md#build-the-service-object). For example, to list the instances of Cloud SQL databases in the examinable-example-123 project:
```php
$response = $sqladmin->instances->listInstances('examinable-example-123')->getItems();
```
## Complete example
The following example prints a JSON-formatted list of Cloud SQL instances in a project.
To run this example:
1. Create a new directory and change to it. For example:
```sh
mkdir ~/php-oauth2-example
cd ~/php-oauth2-example
```
2. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org):
```sh
composer require google/apiclient:^2.0
```
3. Create the file sqlinstances.php with the content below.
4. Run the example from the command line:
```
php ~/php-oauth2-example/sqlinstances.php
```
### sqlinstances.php
```php
<?php
require_once __DIR__.'/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json');
$client = new Google\Client();
$client->useApplicationDefaultCredentials();
$sqladmin = new Google\Service\SQLAdmin($client);
$response = $sqladmin->instances
->listInstances('examinable-example-123')->getItems();
echo json_encode($response) . "\n";
```

View file

@ -0,0 +1,424 @@
# Using OAuth 2.0 for Web Server Applications
This document explains how web server applications use the Google API Client Library for PHP to implement OAuth 2.0 authorization to access Google APIs. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private. For example, an application can use OAuth 2.0 to obtain permission from users to store files in their Google Drives.
This OAuth 2.0 flow is specifically for user authorization. It is designed for applications that can store confidential information and maintain state. A properly authorized web server application can access an API while the user interacts with the application or after the user has left the application.
Web server applications frequently also use [service accounts](oauth-server.md) to authorize API requests, particularly when calling Cloud APIs to access project-based data rather than user-specific data. Web server applications can use service accounts in conjunction with user authorization.
## Prerequisites
### Enable APIs for your project
Any application that calls Google APIs needs to enable those APIs in the API Console. To enable the appropriate APIs for your project:
1. Open the [Library](https://console.developers.google.com/apis/library) page in the API Console.
2. Select the project associated with your application. Create a project if you do not have one already.
3. Use the **Library** page to find each API that your application will use. Click on each API and enable it for your project.
### Create authorization credentials
Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
1. Open the [Credentials page](https://console.developers.google.com/apis/credentials) in the API Console.
2. Click **Create credentials > OAuth client ID**.
3. Complete the form. Set the application type to `Web application`. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized **redirect URIs**. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses.
For testing, you can specify URIs that refer to the local machine, such as `http://localhost:8080`. With that in mind, please note that all of the examples in this document use `http://localhost:8080` as the redirect URI.
We recommend that you design your app's auth endpoints so that your application does not expose authorization codes to other resources on the page.
After creating your credentials, download the **client_secret.json** file from the API Console. Securely store the file in a location that only your application can access.
> **Important:** Do not store the **client_secret.json** file in a publicly-accessible location. In addition, if you share the source code to your application—for example, on GitHub—store the **client_secret.json** file outside of your source tree to avoid inadvertently sharing your client credentials.
### Identify access scopes
Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there may be an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent.
Before you start implementing OAuth 2.0 authorization, we recommend that you identify the scopes that your app will need permission to access.
We also recommend that your application request access to authorization scopes via an [incremental authorization](#incremental-authorization) process, in which your application requests access to user data in context. This best practice helps users to more easily understand why your application needs the access it is requesting.
The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document contains a full list of scopes that you might use to access Google APIs.
> If your public application uses scopes that permit access to certain user data, it must pass review. If you see **unverified app** on the screen when testing your application, you must submit a verification request to remove it. Find out more about [unverified apps](https://support.google.com/cloud/answer/7454865) and get answers to [frequently asked questions about app verification](https://support.google.com/cloud/answer/9110914) in the Help Center.
### Language-specific requirements
To run any of the code samples in this document, you'll need a Google account, access to the Internet, and a web browser. If you are using one of the API client libraries, also see the language-specific requirements below.
To run the PHP code samples in this document, you'll need:
* PHP 5.6 or greater with the command-line interface (CLI) and JSON extension installed.
* The [Composer](https://getcomposer.org/) dependency management tool.
* The Google APIs Client Library for PHP:
```sh
php composer.phar require google/apiclient:^2.0
```
## Obtaining OAuth 2.0 access tokens
The following steps show how your application interacts with Google's OAuth 2.0 server to obtain a user's consent to perform an API request on the user's behalf. Your application must have that consent before it can execute a Google API request that requires user authorization.
The list below quickly summarizes these steps:
1. Your application identifies the permissions it needs.
2. Your application redirects the user to Google along with the list of requested permissions.
3. The user decides whether to grant the permissions to your application.
4. Your application finds out what the user decided.
5. If the user granted the requested permissions, your application retrieves tokens needed to make API requests on the user's behalf.
### Step 1: Set authorization parameters
Your first step is to create the authorization request. That request sets parameters that identify your application and define the permissions that the user will be asked to grant to your application.
The code snippet below creates a `Google\Client()` object, which defines the parameters in the authorization request.
That object uses information from your **client_secret.json** file to identify your application. The object also identifies the scopes that your application is requesting permission to access and the URL to your application's auth endpoint, which will handle the response from Google's OAuth 2.0 server. Finally, the code sets the optional access_type and include_granted_scopes parameters.
For example, this code requests read-only, offline access to a user's Google Drive:
```php
$client = new Google\Client();
$client->setAuthConfig('client_secret.json');
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->setAccessType('offline'); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
```
The request specifies the following information:
#### Parameters
##### `client_id`
**Required**. The client ID for your application. You can find this value in the [API Console](https://console.developers.google.com/). In PHP, call the `setAuthConfig` function to load authorization credentials from a **client_secret.json** file.
```php
$client = new Google\Client();
$client->setAuthConfig('client_secret.json');
```
##### `redirect_uri`
**Required**. Determines where the API server redirects the user after the user completes the authorization flow. The value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in the [API Console](https://console.developers.google.com/). If this value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch' error. Note that the `http` or `https` scheme, case, and trailing slash ('`/`') must all match.
To set this value in PHP, call the `setRedirectUri` function. Note that you must specify a valid redirect URI for your API Console project.
```php
$client->setRedirectUri('http://localhost:8080/oauth2callback.php');
```
##### `scope`
**Required**. A space-delimited list of scopes that identify the resources that your application could access on the user's behalf. These values inform the consent screen that Google displays to the user.
Scopes enable your application to only request access to the resources that it needs while also enabling users to control the amount of access that they grant to your application. Thus, there is an inverse relationship between the number of scopes requested and the likelihood of obtaining user consent. To set this value in PHP, call the `addScope` function:
```php
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
```
The [OAuth 2.0 API Scopes](https://developers.google.com/identity/protocols/googlescopes) document provides a full list of scopes that you might use to access Google APIs.
We recommend that your application request access to authorization scopes in context whenever possible. By requesting access to user data in context, via [incremental authorization](#Incremental-authorization), you help users to more easily understand why your application needs the access it is requesting.
##### `access_type`
**Recommended**. Indicates whether your application can refresh access tokens when the user is not present at the browser. Valid parameter values are `online`, which is the default value, and `offline`.
Set the value to `offline` if your application needs to refresh access tokens when the user is not present at the browser. This is the method of refreshing access tokens described later in this document. This value instructs the Google authorization server to return a refresh token _and_ an access token the first time that your application exchanges an authorization code for tokens.
To set this value in PHP, call the `setAccessType` function:
```php
$client->setAccessType('offline');
```
##### `state`
**Recommended**. Specifies any string value that your application uses to maintain state between your authorization request and the authorization server's response. The server returns the exact value that you send as a `name=value` pair in the hash (`#`) fragment of the `redirect_uri` after the user consents to or denies your application's access request.
You can use this parameter for several purposes, such as directing the user to the correct resource in your application, sending nonces, and mitigating cross-site request forgery. Since your `redirect_uri` can be guessed, using a `state` value can increase your assurance that an incoming connection is the result of an authentication request. If you generate a random string or encode the hash of a cookie or another value that captures the client's state, you can validate the response to additionally ensure that the request and response originated in the same browser, providing protection against attacks such as cross-site request forgery. See the [OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect#createxsrftoken) documentation for an example of how to create and confirm a `state` token.
To set this value in PHP, call the `setState` function:
```php
$client->setState($sample_passthrough_value);
```
##### `include_granted_scopes`
**Optional**. Enables applications to use incremental authorization to request access to additional scopes in context. If you set this parameter's value to `true` and the authorization request is granted, then the new access token will also cover any scopes to which the user previously granted the application access. See the [incremental authorization](#Incremental-authorization) section for examples.
To set this value in PHP, call the `setIncludeGrantedScopes` function:
```php
$client->setIncludeGrantedScopes(true);
```
##### `login_hint`
**Optional**. If your application knows which user is trying to authenticate, it can use this parameter to provide a hint to the Google Authentication Server. The server uses the hint to simplify the login flow either by prefilling the email field in the sign-in form or by selecting the appropriate multi-login session.
Set the parameter value to an email address or `sub` identifier, which is equivalent to the user's Google ID.
To set this value in PHP, call the `setLoginHint` function:
```php
$client->setLoginHint('timmerman@google.com');
```
##### `prompt`
**Optional**. A space-delimited, case-sensitive list of prompts to present the user. If you don't specify this parameter, the user will be prompted only the first time your app requests access.
To set this value in PHP, call the `setPrompt` function:
```php
$client->setPrompt('consent');
```
Possible values are:
`none`
Do not display any authentication or consent screens. Must not be specified with other values.
`consent`
Prompt the user for consent.
`select_account`
Prompt the user to select an account.
### Step 2: Redirect to Google's OAuth 2.0 server
Redirect the user to Google's OAuth 2.0 server to initiate the authentication and authorization process. Typically, this occurs when your application first needs to access the user's data. In the case of [incremental authorization](#incremental-authorization), this step also occurs when your application first needs to access additional resources that it does not yet have permission to access.
1. Generate a URL to request access from Google's OAuth 2.0 server:
```php
$auth_url = $client->createAuthUrl();
```
2. Redirect the user to `$auth_url`:
```php
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
```
Google's OAuth 2.0 server authenticates the user and obtains consent from the user for your application to access the requested scopes. The response is sent back to your application using the redirect URL you specified.
### Step 3: Google prompts user for consent
In this step, the user decides whether to grant your application the requested access. At this stage, Google displays a consent window that shows the name of your application and the Google API services that it is requesting permission to access with the user's authorization credentials. The user can then consent or refuse to grant access to your application.
Your application doesn't need to do anything at this stage as it waits for the response from Google's OAuth 2.0 server indicating whether the access was granted. That response is explained in the following step.
### Step 4: Handle the OAuth 2.0 server response
The OAuth 2.0 server responds to your application's access request by using the URL specified in the request.
If the user approves the access request, then the response contains an authorization code. If the user does not approve the request, the response contains an error message. The authorization code or error message that is returned to the web server appears on the query string, as shown below:
An error response:
https://oauth2.example.com/auth?error=access_denied
An authorization code response:
https://oauth2.example.com/auth?code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7
> **Important**: If your response endpoint renders an HTML page, any resources on that page will be able to see the authorization code in the URL. Scripts can read the URL directly, and the URL in the `Referer` HTTP header may be sent to any or all resources on the page.
>
> Carefully consider whether you want to send authorization credentials to all resources on that page (especially third-party scripts such as social plugins and analytics). To avoid this issue, we recommend that the server first handle the request, then redirect to another URL that doesn't include the response parameters.
#### Sample OAuth 2.0 server response
You can test this flow by clicking on the following sample URL, which requests read-only access to view metadata for files in your Google Drive:
```
https://accounts.google.com/o/oauth2/v2/auth?
 scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.metadata.readonly&
 access_type=offline&
 include_granted_scopes=true&
 state=state_parameter_passthrough_value&
 redirect_uri=http%3A%2F%2Foauth2.example.com%2Fcallback&
 response_type=code&
 client_id=client_id
```
After completing the OAuth 2.0 flow, you should be redirected to `http://localhost/oauth2callback`, which will likely yield a `404 NOT FOUND` error unless your local machine serves a file at that address. The next step provides more detail about the information returned in the URI when the user is redirected back to your application.
### Step 5: Exchange authorization code for refresh and access tokens
After the web server receives the authorization code, it can exchange the authorization code for an access token.
To exchange an authorization code for an access token, use the `authenticate` method:
```php
$client->authenticate($_GET['code']);
```
You can retrieve the access token with the `getAccessToken` method:
```php
$access_token = $client->getAccessToken();
```
[](#top_of_page)Calling Google APIs
-----------------------------------
Use the access token to call Google APIs by completing the following steps:
1. If you need to apply an access token to a new `Google\Client` object—for example, if you stored the access token in a user session—use the `setAccessToken` method:
```php
$client->setAccessToken($access_token);
```
2. Build a service object for the API that you want to call. You build a a service object by providing an authorized `Google\Client` object to the constructor for the API you want to call. For example, to call the Drive API:
```php
$drive = new Google\Service\Drive($client);
```
3. Make requests to the API service using the [interface provided by the service object](start.md). For example, to list the files in the authenticated user's Google Drive:
```php
$files = $drive->files->listFiles(array())->getItems();
```
[](#top_of_page)Complete example
--------------------------------
The following example prints a JSON-formatted list of files in a user's Google Drive after the user authenticates and gives consent for the application to access the user's Drive files.
To run this example:
1. In the API Console, add the URL of the local machine to the list of redirect URLs. For example, add `http://localhost:8080`.
2. Create a new directory and change to it. For example:
```sh
mkdir ~/php-oauth2-example
cd ~/php-oauth2-example
```
3. Install the [Google API Client Library](https://github.com/google/google-api-php-client) for PHP using [Composer](https://getcomposer.org):
```sh
composer require google/apiclient:^2.0
```
4. Create the files `index.php` and `oauth2callback.php` with the content below.
5. Run the example with a web server configured to serve PHP. If you use PHP 5.6 or newer, you can use PHP's built-in test web server:
```sh
php -S localhost:8080 ~/php-oauth2-example
```
#### index.php
```php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google\Service\Drive($client);
$files = $drive->files->listFiles(array())->getItems();
echo json_encode($files);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
```
#### oauth2callback.php
```php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
```
## Incremental authorization
In the OAuth 2.0 protocol, your app requests authorization to access resources, which are identified by scopes. It is considered a best user-experience practice to request authorization for resources at the time you need them. To enable that practice, Google's authorization server supports incremental authorization. This feature lets you request scopes as they are needed and, if the user grants permission, add those scopes to your existing access token for that user.
For example, an app that lets people sample music tracks and create mixes might need very few resources at sign-in time, perhaps nothing more than the name of the person signing in. However, saving a completed mix would require access to their Google Drive. Most people would find it natural if they only were asked for access to their Google Drive at the time the app actually needed it.
In this case, at sign-in time the app might request the `profile` scope to perform basic sign-in, and then later request the `https://www.googleapis.com/auth/drive.file` scope at the time of the first request to save a mix.
To implement incremental authorization, you complete the normal flow for requesting an access token but make sure that the authorization request includes previously granted scopes. This approach allows your app to avoid having to manage multiple access tokens.
The following rules apply to an access token obtained from an incremental authorization:
* The token can be used to access resources corresponding to any of the scopes rolled into the new, combined authorization.
* When you use the refresh token for the combined authorization to obtain an access token, the access token represents the combined authorization and can be used for any of its scopes.
* The combined authorization includes all scopes that the user granted to the API project even if the grants were requested from different clients. For example, if a user granted access to one scope using an application's desktop client and then granted another scope to the same application via a mobile client, the combined authorization would include both scopes.
* If you revoke a token that represents a combined authorization, access to all of that authorization's scopes on behalf of the associated user are revoked simultaneously.
The example for [setting authorization parameters](#Step-1-Set-authorization-parameters) demonstrates how to ensure authorization requests follow this best practice. The code snippet below also shows the code that you need to add to use incremental authorization.
```php
$client->setIncludeGrantedScopes(true);
```
## Refreshing an access token (offline access)
Access tokens periodically expire. You can refresh an access token without prompting the user for permission (including when the user is not present) if you requested offline access to the scopes associated with the token.
If you use a Google API Client Library, the [client object](#Step-1-Set-authorization-parameters) refreshes the access token as needed as long as you configure that object for offline access.
Requesting offline access is a requirement for any application that needs to access a Google API when the user is not present. For example, an app that performs backup services or executes actions at predetermined times needs to be able to refresh its access token when the user is not present. The default style of access is called `online`.
Server-side web applications, installed applications, and devices all obtain refresh tokens during the authorization process. Refresh tokens are not typically used in client-side (JavaScript) web applications.
If your application needs offline access to a Google API, set the API client's access type to `offline`:
```php
$client->setAccessType("offline");
```
After a user grants offline access to the requested scopes, you can continue to use the API client to access Google APIs on the user's behalf when the user is offline. The client object will refresh the access token as needed.
## Revoking a token
In some cases a user may wish to revoke access given to an application. A user can revoke access by visiting [Account Settings](https://security.google.com/settings/security/permissions). It is also possible for an application to programmatically revoke the access given to it. Programmatic revocation is important in instances where a user unsubscribes or removes an application. In other words, part of the removal process can include an API request to ensure the permissions granted to the application are removed.
To programmatically revoke a token, call `revokeToken()`:
```php
$client->revokeToken();
```
**Note:** Following a successful revocation response, it might take some time before the revocation has full effect.
Except as otherwise noted, the content of this page is licensed under the [Creative Commons Attribution 4.0 License](https://creativecommons.org/licenses/by/4.0/), and code samples are licensed under the [Apache 2.0 License](https://www.apache.org/licenses/LICENSE-2.0). For details, see our [Site Policies](https://developers.google.com/terms/site-policies). Java is a registered trademark of Oracle and/or its affiliates.

View file

@ -0,0 +1,10 @@
# Pagination
Most list API calls have a maximum limit of results they will return in a single response. To allow retrieving more than this number of results, responses may return a pagination token which can be passed with a request in order to access subsequent pages.
The token for the page will normally be found on list response objects, normally `nextPageToken`. This can be passed in the optional params.
```php
$token = $results->getNextPageToken();
$server->listActivities('me', 'public', array('pageToken' => $token));
```

View file

@ -0,0 +1,14 @@
# Standard Parameters
Many API methods include support for certain optional parameters. In addition to these there are several standard parameters that can be applied to any API call. These are defined in the `Google\Service\Resource` class.
## Parameters
- **alt**: Specify an alternative response type, for example csv.
- **fields**: A comma separated list of fields that should be included in the response. Nested parameters can be specified with parens, e.g. key,parent(child/subsection).
- **userIp**: The IP of the end-user making the request. This is used in per-user request quotas, as defined in the Google Developers Console
- **quotaUser**: A user ID for the end user, an alternative to userIp for applying per-user request quotas
- **data**: Used as part of [media](media.md)
- **mimeType**: Used as part of [media](media.md)
- **uploadType**: Used as part of [media](media.md)
- **mediaUpload**: Used as part of [media](media.md)

91
vendor/google/apiclient/docs/start.md vendored Normal file
View file

@ -0,0 +1,91 @@
# Getting Started
This document provides all the basic information you need to start using the library. It covers important library concepts, shows examples for various use cases, and gives links to more information.
## Setup
There are a few setup steps you need to complete before you can use this library:
1. If you don't already have a Google account, [sign up](https://www.google.com/accounts).
2. If you have never created a Google API project, read the [Managing Projects page](https://developers.google.com/console/help/#managingprojects) and create a project in the [Google Developers Console](https://console.developers.google.com/)
3. [Install](install.md) the library.
## Authentication and authorization
It is important to understand the basics of how API authentication and authorization are handled. All API calls must use either simple or authorized access (defined below). Many API methods require authorized access, but some can use either. Some API methods that can use either behave differently, depending on whether you use simple or authorized access. See the API's method documentation to determine the appropriate access type.
### 1. Simple API access (API keys)
These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google Cloud project. This is needed to measure project usage for accounting purposes.
#### Important concepts
* **API key**: To authenticate your application, use an [API key](https://cloud.google.com/docs/authentication/api-keys) for your Google Cloud Console project. Every simple access call your application makes must include this key.
> **Warning**: Keep your API key private. If someone obtains your key, they could use it to consume your quota or incur charges against your Google Cloud project.
### 2. Authorized API access (OAuth 2.0)
These API calls access private user data. Before you can call them, the user that has access to the private data must grant your application access. Therefore, your application must be authenticated, the user must grant access for your application, and the user must be authenticated in order to grant that access. All of this is accomplished with [OAuth 2.0](https://developers.google.com/identity/protocols/OAuth2) and libraries written for it.
#### Important concepts
* **Scope**: Each API defines one or more scopes that declare a set of operations permitted. For example, an API might have read-only and read-write scopes. When your application requests access to user data, the request must include one or more scopes. The user needs to approve the scope of access your application is requesting.
* **Refresh and access tokens**: When a user grants your application access, the OAuth 2.0 authorization server provides your application with refresh and access tokens. These tokens are only valid for the scope requested. Your application uses access tokens to authorize API calls. Access tokens expire, but refresh tokens do not. Your application can use a refresh token to acquire a new access token.
> **Warning**: Keep refresh and access tokens private. If someone obtains your tokens, they could use them to access private user data.
* **Client ID and client secret**: These strings uniquely identify your application and are used to acquire tokens. They are created for your Google Cloud project on the [API Access pane](https://code.google.com/apis/console#:access) of the Google Cloud. There are three types of client IDs, so be sure to get the correct type for your application:
* Web application client IDs
* Installed application client IDs
* [Service Account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) client IDs
> **Warning**: Keep your client secret private. If someone obtains your client secret, they could use it to consume your quota, incur charges against your Google Cloud project, and request access to user data.
## Building and calling a service
This section described how to build an API-specific service object, make calls to the service, and process the response.
### Build the client object
The client object is the primary container for classes and configuration in the library.
```php
$client = new Google\Client();
$client->setApplicationName("My Application");
$client->setDeveloperKey("MY_SIMPLE_API_KEY");
```
### Build the service object
Services are called through queries to service specific objects. These are created by constructing the service object, and passing an instance of `Google\Client` to it. `Google\Client` contains the IO, authentication and other classes required by the service to function, and the service informs the client which scopes it uses to provide a default when authenticating a user.
```php
$service = new Google\Service\Books($client);
```
### Calling an API
Each API provides resources and methods, usually in a chain. These can be accessed from the service object in the form `$service->resource->method(args)`. Most method require some arguments, then accept a final parameter of an array containing optional parameters. For example, with the Google Books API, we can make a call to list volumes matching a certain string, and add an optional _filter_ parameter.
```php
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
```
### Handling the result
There are two main types of response - items and collections of items. Each can be accessed either as an object or as an array. Collections implement the `Iterator` interface so can be used in foreach and other constructs.
```php
foreach ($results as $item) {
echo $item['volumeInfo']['title'], "<br /> \n";
}
```
## Google App Engine support
This library works well with Google App Engine applications. The Memcache class is automatically used for caching, and the file IO is implemented with the use of the Streams API.

View file

@ -0,0 +1,19 @@
# Examples for Google APIs Client Library for PHP #
## How to run the examples ##
1. Following the [Installation Instructions](../README.md#installation)
1. Run the PHP built-in web server. Supply the `-t` option to point to this directory:
```
$ php -S localhost:8000 -t examples/
```
1. Point your browser to the host and port you specified, i.e `http://localhost:8000`.
## G Suite OAuth Samples
G Suite APIs such as Slides, Sheets, and Drive use 3-legged OAuth authentification.
Samples using OAuth can be found here:
https://github.com/gsuitedevs/php-samples

View file

@ -0,0 +1,93 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("Batching Queries");
/************************************************
We're going to use the simple access to the
books API again as an example, but this time we
will batch up two queries into a single call.
************************************************/
/************************************************
We create the client and set the simple API
access key. If you comment out the call to
setDeveloperKey, the request may still succeed
using the anonymous quota.
************************************************/
$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
if (!$apiKey = getApiKey()) {
echo missingApiKeyWarning();
return;
}
$client->setDeveloperKey($apiKey);
$service = new Google\Service\Books($client);
/************************************************
To actually make the batch call we need to
enable batching on the client - this will apply
globally until we set it to false. This causes
call to the service methods to return the query
rather than immediately executing.
************************************************/
$client->setUseBatch(true);
/************************************************
We then create a batch, and add each query we
want to execute with keys of our choice - these
keys will be reflected in the returned array.
************************************************/
// NOTE: Some services use `new Google\Http\Batch($client);` instead
$batch = $service->createBatch();
$query = 'Henry David Thoreau';
$optParams = array('filter' => 'free-ebooks');
$req1 = $service->volumes->listVolumes($query, $optParams);
$batch->add($req1, "thoreau");
$query = 'George Bernard Shaw';
$req2 = $service->volumes->listVolumes($query, $optParams);
$batch->add($req2, "shaw");
/************************************************
Executing the batch will send all requests off
at once.
************************************************/
$results = $batch->execute();
?>
<h3>Results Of Call 1:</h3>
<?php foreach ($results['response-thoreau'] as $item): ?>
<?= $item['volumeInfo']['title'] ?>
<br />
<?php endforeach ?>
<h3>Results Of Call 2:</h3>
<?php foreach ($results['response-shaw'] as $item): ?>
<?= $item['volumeInfo']['title'] ?>
<br />
<?php endforeach ?>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,109 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("Retrieving An Id Token");
/*************************************************
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
echo missingOAuth2CredentialsWarning();
return;
}
/************************************************
* NOTICE:
* The redirect URI is to the current page, e.g:
* http://localhost:8080/idtoken.php
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->setScopes('email');
/************************************************
* If we're logging out we just need to clear our
* local access token in this case
************************************************/
if (isset($_REQUEST['logout'])) {
unset($_SESSION['id_token_token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
* Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
// store in the session also
$_SESSION['id_token_token'] = $token;
// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
return;
}
/************************************************
If we have an access token, we can make
requests, else we generate an authentication URL.
************************************************/
if (
!empty($_SESSION['id_token_token'])
&& isset($_SESSION['id_token_token']['id_token'])
) {
$client->setAccessToken($_SESSION['id_token_token']);
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
If we're signed in we can go ahead and retrieve
the ID token, which is part of the bundle of
data that is exchange in the authenticate step
- we only need to do a network call if we have
to retrieve the Google certificate to verify it,
and that can be cached.
************************************************/
if ($client->getAccessToken()) {
$token_data = $client->verifyIdToken();
}
?>
<div class="box">
<?php if (isset($authUrl)): ?>
<div class="request">
<a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
</div>
<?php else: ?>
<div class="data">
<p>Here is the data from your Id Token:</p>
<pre><?php var_export($token_data) ?></pre>
</div>
<?php endif ?>
</div>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,43 @@
<?php include_once "templates/base.php" ?>
<?php if (!isWebRequest()): ?>
To view this example, run the following command from the root directory of this repository:
php -S localhost:8080 -t examples/
And then browse to "localhost:8080" in your web browser
<?php return ?>
<?php endif ?>
<?= pageHeader("PHP Library Examples"); ?>
<?php if (isset($_POST['api_key'])): ?>
<?php setApiKey($_POST['api_key']) ?>
<span class="warn">
API Key set!
</span>
<?php endif ?>
<?php if (!getApiKey()): ?>
<div class="api-key">
<strong>You have not entered your API key</strong>
<form action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="POST">
API Key:<input type="text" name="api_key" placeholder="API-Key" required/>
<input type="submit" value="Set API-Key"/>
</form>
<em>This can be found in the <a href="http://developers.google.com/console" target="_blank">Google API Console</em>
</div>
<?php endif ?>
<ul>
<li><a href="simple-query.php">A query using simple API access</a></li>
<li><a href="batch.php">An example of combining multiple calls into a batch request</a></li>
<li><a href="service-account.php">A query using the service account functionality.</a></li>
<li><a href="simple-file-upload.php">An example of a small file upload.</a></li>
<li><a href="large-file-upload.php">An example of a large file upload.</a></li>
<li><a href="large-file-download.php">An example of a large file download.</a></li>
<li><a href="idtoken.php">An example of verifying and retrieving the id token.</a></li>
<li><a href="multi-api.php">An example of using multiple APIs.</a></li>
</ul>
<?= pageFooter(); ?>

View file

@ -0,0 +1,149 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("File Download - Downloading a large file");
/*************************************************
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
echo missingOAuth2CredentialsWarning();
return;
}
/************************************************
* The redirect URI is to the current page, e.g:
* http://localhost:8080/large-file-download.php
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google\Service\Drive($client);
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
* Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);
// store in the session also
$_SESSION['upload_token'] = $token;
// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
* If we're signed in then lets try to download our
* file.
************************************************/
if ($client->getAccessToken()) {
// Check for "Big File" and include the file ID and size
$files = $service->files->listFiles([
'q' => "name='Big File'",
'fields' => 'files(id,size)'
]);
if (count($files) == 0) {
echo "
<h3 class='warn'>
Before you can use this sample, you need to
<a href='/large-file-upload.php'>upload a large file to Drive</a>.
</h3>";
return;
}
// If this is a POST, download the file
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Determine the file's size and ID
$fileId = $files[0]->id;
$fileSize = intval($files[0]->size);
// Get the authorized Guzzle HTTP client
$http = $client->authorize();
// Open a file for writing
$fp = fopen('Big File (downloaded)', 'w');
// Download in 1 MB chunks
$chunkSizeBytes = 1 * 1024 * 1024;
$chunkStart = 0;
// Iterate over each chunk and write it to our file
while ($chunkStart < $fileSize) {
$chunkEnd = $chunkStart + $chunkSizeBytes;
$response = $http->request(
'GET',
sprintf('/drive/v3/files/%s', $fileId),
[
'query' => ['alt' => 'media'],
'headers' => [
'Range' => sprintf('bytes=%s-%s', $chunkStart, $chunkEnd)
]
]
);
$chunkStart = $chunkEnd + 1;
fwrite($fp, $response->getBody()->getContents());
}
// close the file pointer
fclose($fp);
// redirect back to this example
header('Location: ' . filter_var($redirect_uri . '?downloaded', FILTER_SANITIZE_URL));
}
}
?>
<div class="box">
<?php if (isset($authUrl)): ?>
<div class="request">
<a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
</div>
<?php elseif(isset($_GET['downloaded'])): ?>
<div class="shortened">
<p>Your call was successful! Check your filesystem for the file:</p>
<p><code><?= __DIR__ . DIRECTORY_SEPARATOR ?>Big File (downloaded)</code></p>
</div>
<?php else: ?>
<form method="POST">
<input type="submit" value="Click here to download a large (20MB) test file" />
</form>
<?php endif ?>
</div>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,169 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("File Upload - Uploading a large file");
/*************************************************
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
echo missingOAuth2CredentialsWarning();
return;
}
/************************************************
* The redirect URI is to the current page, e.g:
* http://localhost:8080/large-file-upload.php
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google\Service\Drive($client);
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
* Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);
// store in the session also
$_SESSION['upload_token'] = $token;
// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
* If we're signed in then lets try to upload our
* file.
************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
/************************************************
* We'll setup an empty 20MB file to upload.
************************************************/
DEFINE("TESTFILE", 'testfile.txt');
if (!file_exists(TESTFILE)) {
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024*1024*20);
fwrite($fh, "!", 1);
fclose($fh);
}
$file = new Google\Service\Drive\DriveFile();
$file->name = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new Google\Http\MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
// read until you get $chunkSizeBytes from TESTFILE
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
// An example of a read buffered file is when reading from a URL
$chunk = readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
}
function readVideoChunk ($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
// fread will never return more than 8192 bytes if the stream is read buffered and it does not represent a plain file
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize)
{
return $giantChunk;
}
}
return $giantChunk;
}
?>
<div class="box">
<?php if (isset($authUrl)): ?>
<div class="request">
<a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
</div>
<?php elseif($_SERVER['REQUEST_METHOD'] == 'POST'): ?>
<div class="shortened">
<p>Your call was successful! Check your drive for this file:</p>
<p><a href="https://drive.google.com/open?id=<?= $result->id ?>" target="_blank"><?= $result->name ?></a></p>
<p>Now try <a href="/large-file-download.php">downloading a large file from Drive</a>.
</div>
<?php else: ?>
<form method="POST">
<input type="submit" value="Click here to upload a large (20MB) test file" />
</form>
<?php endif ?>
</div>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,120 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("User Query - Multiple APIs");
/*************************************************
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
echo missingOAuth2CredentialsWarning();
return;
}
/************************************************
* The redirect URI is to the current page, e.g:
* http://localhost:8080/multi-api.php
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->addScope("https://www.googleapis.com/auth/youtube");
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
unset($_SESSION['multi-api-token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
* Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);
// store in the session also
$_SESSION['multi-api-token'] = $token;
// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['multi-api-token'])) {
$client->setAccessToken($_SESSION['multi-api-token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['multi-api-token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
We are going to create both YouTube and Drive
services, and query both.
************************************************/
$yt_service = new Google\Service\YouTube($client);
$dr_service = new Google\Service\Drive($client);
/************************************************
If we're signed in, retrieve channels from YouTube
and a list of files from Drive.
************************************************/
if ($client->getAccessToken()) {
$_SESSION['multi-api-token'] = $client->getAccessToken();
$dr_results = $dr_service->files->listFiles(array('pageSize' => 10));
$yt_channels = $yt_service->channels->listChannels('contentDetails', array("mine" => true));
$likePlaylist = $yt_channels[0]->contentDetails->relatedPlaylists->likes;
$yt_results = $yt_service->playlistItems->listPlaylistItems(
"snippet",
array("playlistId" => $likePlaylist)
);
}
?>
<div class="box">
<div class="request">
<?php if (isset($authUrl)): ?>
<a class="login" href="<?= $authUrl ?>">Connect Me!</a>
<?php else: ?>
<h3>Results Of Drive List:</h3>
<?php foreach ($dr_results as $item): ?>
<?= $item->name ?><br />
<?php endforeach ?>
<h3>Results Of YouTube Likes:</h3>
<?php foreach ($yt_results as $item): ?>
<?= $item['snippet']['title'] ?><br />
<?php endforeach ?>
<?php endif ?>
</div>
</div>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,75 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("Service Account Access");
/************************************************
Make an API request authenticated with a service
account.
************************************************/
$client = new Google\Client();
/************************************************
ATTENTION: Fill in these values, or make sure you
have set the GOOGLE_APPLICATION_CREDENTIALS
environment variable. You can get these credentials
by creating a new Service Account in the
API console. Be sure to store the key file
somewhere you can get to it - though in real
operations you'd want to make sure it wasn't
accessible from the webserver!
Make sure the Books API is enabled on this
account as well, or the call will fail.
************************************************/
if ($credentials_file = checkServiceAccountCredentialsFile()) {
// set the location manually
$client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
// use the application default credentials
$client->useApplicationDefaultCredentials();
} else {
echo missingServiceAccountDetailsWarning();
return;
}
$client->setApplicationName("Client_Library_Examples");
$client->setScopes(['https://www.googleapis.com/auth/books']);
$service = new Google\Service\Books($client);
/************************************************
We're just going to make the same call as in the
simple query as an example.
************************************************/
$query = 'Henry David Thoreau';
$optParams = array(
'filter' => 'free-ebooks',
);
$results = $service->volumes->listVolumes($query, $optParams);
?>
<h3>Results Of Call:</h3>
<?php foreach ($results as $item): ?>
<?= $item['volumeInfo']['title'] ?>
<br />
<?php endforeach ?>
<?php pageFooter(__FILE__); ?>

View file

@ -0,0 +1,135 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("File Upload - Uploading a simple file");
/*************************************************
* Ensure you've downloaded your oauth credentials
************************************************/
if (!$oauth_credentials = getOAuthCredentialsFile()) {
echo missingOAuth2CredentialsWarning();
return;
}
/************************************************
* The redirect URI is to the current page, e.g:
* http://localhost:8080/simple-file-upload.php
************************************************/
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$client = new Google\Client();
$client->setAuthConfig($oauth_credentials);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google\Service\Drive($client);
// add "?logout" to the URL to remove a token from the session
if (isset($_REQUEST['logout'])) {
unset($_SESSION['upload_token']);
}
/************************************************
* If we have a code back from the OAuth 2.0 flow,
* we need to exchange that with the
* Google\Client::fetchAccessTokenWithAuthCode()
* function. We store the resultant access token
* bundle in the session, and redirect to ourself.
************************************************/
if (isset($_GET['code'])) {
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
$client->setAccessToken($token);
// store in the session also
$_SESSION['upload_token'] = $token;
// redirect back to the example
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// set the access token as part of the client
if (!empty($_SESSION['upload_token'])) {
$client->setAccessToken($_SESSION['upload_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['upload_token']);
}
} else {
$authUrl = $client->createAuthUrl();
}
/************************************************
* If we're signed in then lets try to upload our
* file. For larger files, see fileupload.php.
************************************************/
if ($_SERVER['REQUEST_METHOD'] == 'POST' && $client->getAccessToken()) {
// We'll setup an empty 1MB file to upload.
DEFINE("TESTFILE", 'testfile-small.txt');
if (!file_exists(TESTFILE)) {
$fh = fopen(TESTFILE, 'w');
fseek($fh, 1024 * 1024);
fwrite($fh, "!", 1);
fclose($fh);
}
// This is uploading a file directly, with no metadata associated.
$file = new Google\Service\Drive\DriveFile();
$result = $service->files->create(
$file,
array(
'data' => file_get_contents(TESTFILE),
'mimeType' => 'application/octet-stream',
'uploadType' => 'media'
)
);
// Now lets try and send the metadata as well using multipart!
$file = new Google\Service\Drive\DriveFile();
$file->setName("Hello World!");
$result2 = $service->files->create(
$file,
array(
'data' => file_get_contents(TESTFILE),
'mimeType' => 'application/octet-stream',
'uploadType' => 'multipart'
)
);
}
?>
<div class="box">
<?php if (isset($authUrl)): ?>
<div class="request">
<a class='login' href='<?= $authUrl ?>'>Connect Me!</a>
</div>
<?php elseif($_SERVER['REQUEST_METHOD'] == 'POST'): ?>
<div class="shortened">
<p>Your call was successful! Check your drive for the following files:</p>
<ul>
<li><a href="https://drive.google.com/open?id=<?= $result->id ?>" target="_blank"><?= $result->name ?></a></li>
<li><a href="https://drive.google.com/open?id=<?= $result2->id ?>" target="_blank"><?= $result2->name ?></a></li>
</ul>
</div>
<?php else: ?>
<form method="POST">
<input type="submit" value="Click here to upload two small (1MB) test files" />
</form>
<?php endif ?>
</div>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,90 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";
echo pageHeader("Simple API Access");
/************************************************
We create the client and set the simple API
access key. If you comment out the call to
setDeveloperKey, the request may still succeed
using the anonymous quota.
************************************************/
$client = new Google\Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
if (!$apiKey = getApiKey()) {
echo missingApiKeyWarning();
return;
}
$client->setDeveloperKey($apiKey);
$service = new Google\Service\Books($client);
/************************************************
We make a call to our service, which will
normally map to the structure of the API.
In this case $service is Books API, the
resource is volumes, and the method is
listVolumes. We pass it a required parameters
(the query), and an array of named optional
parameters.
************************************************/
$query = 'Henry David Thoreau';
$optParams = array(
'filter' => 'free-ebooks',
);
$results = $service->volumes->listVolumes($query, $optParams);
/************************************************
This is an example of deferring a call.
***********************************************/
$client->setDefer(true);
$query = 'Henry David Thoreau';
$optParams = array(
'filter' => 'free-ebooks',
);
$request = $service->volumes->listVolumes($query, $optParams);
$resultsDeferred = $client->execute($request);
/************************************************
These calls returns a list of volumes, so we
can iterate over them as normal with any
array.
Some calls will return a single item which we
can immediately use. The individual responses
are typed as Google\Service\Books_Volume, but
can be treated as an array.
************************************************/
?>
<h3>Results Of Call:</h3>
<?php foreach ($results as $item): ?>
<?= $item['volumeInfo']['title'] ?>
<br />
<?php endforeach ?>
<h3>Results Of Deferred Call:</h3>
<?php foreach ($resultsDeferred as $item): ?>
<?= $item['volumeInfo']['title'] ?>
<br />
<?php endforeach ?>
<?= pageFooter(__FILE__) ?>

View file

@ -0,0 +1,117 @@
/*
* Copyright 2013 Google Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
body {
font-family: Arial,sans-serif;
margin: auto;
padding: 10px;
}
.box {
border: .5em solid #E3E9FF;
-webkit-box-orient: vertical;
-webkit-box-align: center;
-moz-box-orient: vertical;
-moz-box-align: center;
display: block;
box-orient: vertical;
box-align: center;
width: 400px;
height: auto;
margin: auto;
padding: 10px;
overflow: scroll;
}
.request {
-webkit-box-flex: 1;
-moz-box-flex: 1;
box-flex: 1;
}
.result {
-webkit-box-flex: 2;
-moz-box-flex: 2;
box-flex: 2;
}
header {
color:#000;
padding:2px 5px;
font-size:100%;
margin: auto;
text-align: center
}
header h1.logo {
margin:6px 0;
padding:0;
font-size:24px;
line-height:20px;
text-align: center
}
.login {
font-size: 200%;
display: block;
margin: auto;
cursor: pointer;
text-align: center;
font-weight: bold;
color: #2779AA;
line-height: normal;
}
.logout {
font-weight: normal;
margin-top: 0;
}
.shortened {
overflow: scroll;
}
.url {
color: #019;
font-size: 100%;
vertical-align: middle;
padding: 1px;
background-color: white;
border: 1px inset;
cursor: auto;
margin: 0;
text-indent: 0;
display: inline-block;
}
pre.code {
padding: 10px;
border: .5em solid #E3E9FF;
margin: 10px;
height: 400px;
overflow: scroll;
}
.warn {
color: red;
}
.api-key {
background-color:#DDD;
}

View file

@ -0,0 +1,167 @@
<?php
/* Ad hoc functions to make the examples marginally prettier.*/
function isWebRequest()
{
return isset($_SERVER['HTTP_USER_AGENT']);
}
function pageHeader($title)
{
$ret = "<!doctype html>
<html>
<head>
<title>" . $title . "</title>
<link href='styles/style.css' rel='stylesheet' type='text/css' />
</head>
<body>\n";
if ($_SERVER['PHP_SELF'] != "/index.php") {
$ret .= "<p><a href='index.php'>Back</a></p>";
}
$ret .= "<header><h1>" . $title . "</h1></header>";
// Start the session (for storing access tokens and things)
if (!headers_sent()) {
session_start();
}
return $ret;
}
function pageFooter($file = null)
{
$ret = "";
if ($file) {
$ret .= "<h3>Code:</h3>";
$ret .= "<pre class='code'>";
$ret .= htmlspecialchars(file_get_contents($file));
$ret .= "</pre>";
}
$ret .= "</html>";
return $ret;
}
function missingApiKeyWarning()
{
$ret = "
<h3 class='warn'>
Warning: You need to set a Simple API Access key from the
<a href='http://developers.google.com/console'>Google API console</a>
</h3>";
return $ret;
}
function missingClientSecretsWarning()
{
$ret = "
<h3 class='warn'>
Warning: You need to set Client ID, Client Secret and Redirect URI from the
<a href='http://developers.google.com/console'>Google API console</a>
</h3>";
return $ret;
}
function missingServiceAccountDetailsWarning()
{
$ret = "
<h3 class='warn'>
Warning: You need download your Service Account Credentials JSON from the
<a href='http://developers.google.com/console'>Google API console</a>.
</h3>
<p>
Once downloaded, move them into the root directory of this repository and
rename them 'service-account-credentials.json'.
</p>
<p>
In your application, you should set the GOOGLE_APPLICATION_CREDENTIALS environment variable
as the path to this file, but in the context of this example we will do this for you.
</p>";
return $ret;
}
function missingOAuth2CredentialsWarning()
{
$ret = "
<h3 class='warn'>
Warning: You need to set the location of your OAuth2 Client Credentials from the
<a href='http://developers.google.com/console'>Google API console</a>.
</h3>
<p>
Once downloaded, move them into the root directory of this repository and
rename them 'oauth-credentials.json'.
</p>";
return $ret;
}
function invalidCsrfTokenWarning()
{
$ret = "
<h3 class='warn'>
The CSRF token is invalid, your session probably expired. Please refresh the page.
</h3>";
return $ret;
}
function checkServiceAccountCredentialsFile()
{
// service account creds
$application_creds = __DIR__ . '/../../service-account-credentials.json';
return file_exists($application_creds) ? $application_creds : false;
}
function getCsrfToken()
{
if (!isset($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32));
}
return $_SESSION['csrf_token'];
}
function validateCsrfToken()
{
return isset($_REQUEST['csrf_token'])
&& isset($_SESSION['csrf_token'])
&& $_REQUEST['csrf_token'] === $_SESSION['csrf_token'];
}
function getOAuthCredentialsFile()
{
// oauth2 creds
$oauth_creds = __DIR__ . '/../../oauth-credentials.json';
if (file_exists($oauth_creds)) {
return $oauth_creds;
}
return false;
}
function setClientCredentialsFile($apiKey)
{
$file = __DIR__ . '/../../tests/.apiKey';
file_put_contents($file, $apiKey);
}
function getApiKey()
{
$file = __DIR__ . '/../../tests/.apiKey';
if (file_exists($file)) {
return file_get_contents($file);
}
}
function setApiKey($apiKey)
{
$file = __DIR__ . '/../../tests/.apiKey';
file_put_contents($file, $apiKey);
}

163
vendor/google/apiclient/phpcs.xml.dist vendored Normal file
View file

@ -0,0 +1,163 @@
<?xml version="1.0"?>
<ruleset name="GAPI">
<description>The Google API client library coding standard.</description>
<!-- PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations. -->
<rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound">
<severity>0</severity>
</rule>
<!-- PHP code MUST use only UTF-8 without BOM. -->
<rule ref="Generic.Files.ByteOrderMark"/>
<!-- Check for duplicated class names -->
<rule ref="Generic.Classes.DuplicateClassName" />
<!-- Class constants MUST be declared in all upper case with underscore separators. -->
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>
<!-- Method names MUST be declared in camelCase(). -->
<rule ref="Generic.NamingConventions.CamelCapsFunctionName">
<properties>
<property name="strict" value="false"/>
</properties>
<!-- Generated libs have some properties that break this! -->
<exclude-pattern>Service/*.php</exclude-pattern>
</rule>
<!-- All PHP files MUST use the Unix LF (linefeed) line ending. -->
<rule ref="Generic.Files.LineEndings">
<properties>
<property name="eolChar" value="\n"/>
</properties>
</rule>
<!-- All PHP files MUST end with a single blank line. -->
<rule ref="PSR2.Files.EndFileNewline" />
<!-- The closing ?> tag MUST be omitted from files containing only PHP. -->
<rule ref="Zend.Files.ClosingTag"/>
<!-- The soft limit on line length MUST be 100 characters; automated style checkers MUST warn but MUST NOT error at the soft limit. -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="100"/>
<property name="absoluteLineLimit" value="120"/>
</properties>
<!-- Generated libs have some rather long class names that break this! -->
<exclude-pattern>Service/*.php</exclude-pattern>
</rule>
<!-- There MUST NOT be trailing whitespace at the end of non-blank lines. -->
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
<properties>
<property name="ignoreBlankLines" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
<severity>0</severity>
</rule>
<!-- There MUST NOT be more than one statement per line. -->
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>
<!-- Code MUST use an indent of 2 spaces, and MUST NOT use tabs for indenting. -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="2" />
</properties>
</rule>
<rule ref="Generic.WhiteSpace.DisallowTabIndent"/>
<!-- PHP keywords MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseKeyword"/>
<!-- The PHP constants true, false, and null MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseConstant"/>
<!-- The extends and implements keywords MUST be declared on the same line as the class name.
The opening brace for the class go MUST go on its own line; the closing brace for the class MUST go on the next line after the body.
Lists of implements MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one interface per line. -->
<rule ref="PSR2.Classes.ClassDeclaration">
<exclude-pattern>src/aliases\.php</exclude-pattern>
</rule>
<!-- Visibility MUST be declared on all properties.
The var keyword MUST NOT be used to declare a property.
There MUST NOT be more than one property declared per statement.
Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. -->
<rule ref="PSR2.Classes.PropertyDeclaration" />
<rule ref="PSR2.Classes.PropertyDeclaration.Underscore" />
<!-- Visibility MUST be declared on all methods. -->
<rule ref="Squiz.Scope.MethodScope"/>
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>
<!-- Method names MUST NOT be declared with a space after the method name. The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. -->
<rule ref="Squiz.Functions.FunctionDeclaration"/>
<rule ref="Squiz.Functions.LowercaseFunctionKeywords"/>
<!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
<property name="equalsSpacing" value="1"/>
</properties>
</rule>
<!-- Method arguments with default values MUST go at the end of the argument list. -->
<rule ref="PEAR.Functions.ValidDefaultValue"/>
<!-- Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. -->
<rule ref="Squiz.Functions.MultiLineFunctionDeclaration"/>
<!-- When present, the abstract and final declarations MUST precede the visibility declaration.
When present, the static declaration MUST come after the visibility declaration. -->
<!-- Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. -->
<rule ref="PSR2.Methods.MethodDeclaration" />
<!-- When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis, there MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. -->
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
<rule ref="PEAR.Functions.FunctionCallSignature">
<properties>
<property name="allowMultipleArguments" value="false"/>
</properties>
</rule>
<!-- The general style rules for control structures are as follows:
There MUST be one space after the control structure keyword
There MUST NOT be a space after the opening parenthesis
There MUST NOT be a space before the closing parenthesis
There MUST be one space between the closing parenthesis and the opening brace
The structure body MUST be indented once
The closing brace MUST be on the next line after the body -->
<rule ref="Squiz.ControlStructures.ControlSignature">
<properties>
<property name="ignoreComments" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace">
<exclude-pattern>src/aliases\.php</exclude-pattern>
</rule>
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration"/>
<rule ref="Squiz.ControlStructures.ForLoopDeclaration"/>
<rule ref="Squiz.ControlStructures.LowercaseDeclaration"/>
<!-- The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body. -->
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<!-- The case statement MUST be indented once from switch, and the break keyword (or other terminating keyword) MUST be indented at the same level as the case body. There MUST be a comment such as // no break when fall-through is intentional in a non-empty case body. -->
<rule ref="PSR2.ControlStructures.SwitchDeclaration" >
<properties>
<property name="indent" value="2" />
</properties>
</rule>
</ruleset>

View file

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.2/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Google PHP Client Unit Test Suite">
<directory>tests/Google</directory>
</testsuite>
<testsuite name="Google PHP Client Examples Test Suite">
<directory>tests/examples</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>

View file

@ -0,0 +1,81 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AccessToken;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
/**
* Wrapper around Google Access Tokens which provides convenience functions
*
*/
class Revoke
{
/**
* @var ClientInterface The http client
*/
private $http;
/**
* Instantiates the class, but does not initiate the login flow, leaving it
* to the discretion of the caller.
*/
public function __construct(ClientInterface $http = null)
{
$this->http = $http;
}
/**
* Revoke an OAuth2 access token or refresh token. This method will revoke the current access
* token, if a token isn't provided.
*
* @param string|array $token The token (access token or a refresh token) that should be revoked.
* @return boolean Returns True if the revocation was successful, otherwise False.
*/
public function revokeToken($token)
{
if (is_array($token)) {
if (isset($token['refresh_token'])) {
$token = $token['refresh_token'];
} else {
$token = $token['access_token'];
}
}
$body = Psr7\Utils::streamFor(http_build_query(array('token' => $token)));
$request = new Request(
'POST',
Client::OAUTH2_REVOKE_URI,
[
'Cache-Control' => 'no-store',
'Content-Type' => 'application/x-www-form-urlencoded',
],
$body
);
$httpHandler = HttpHandlerFactory::build($this->http);
$response = $httpHandler($request);
return $response->getStatusCode() == 200;
}
}

View file

@ -0,0 +1,309 @@
<?php
/*
* Copyright 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AccessToken;
use Firebase\JWT\ExpiredException as ExpiredExceptionV3;
use Firebase\JWT\SignatureInvalidException;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA\PublicKey;
use Psr\Cache\CacheItemPoolInterface;
use Google\Auth\Cache\MemoryCacheItemPool;
use Google\Exception as GoogleException;
use Stash\Driver\FileSystem;
use Stash\Pool;
use DateTime;
use DomainException;
use Exception;
use ExpiredException; // Firebase v2
use LogicException;
/**
* Wrapper around Google Access Tokens which provides convenience functions
*
*/
class Verify
{
const FEDERATED_SIGNON_CERT_URL = 'https://www.googleapis.com/oauth2/v3/certs';
const OAUTH2_ISSUER = 'accounts.google.com';
const OAUTH2_ISSUER_HTTPS = 'https://accounts.google.com';
/**
* @var ClientInterface The http client
*/
private $http;
/**
* @var CacheItemPoolInterface cache class
*/
private $cache;
/**
* Instantiates the class, but does not initiate the login flow, leaving it
* to the discretion of the caller.
*/
public function __construct(
ClientInterface $http = null,
CacheItemPoolInterface $cache = null,
$jwt = null
) {
if (null === $http) {
$http = new Client();
}
if (null === $cache) {
$cache = new MemoryCacheItemPool;
}
$this->http = $http;
$this->cache = $cache;
$this->jwt = $jwt ?: $this->getJwtService();
}
/**
* Verifies an id token and returns the authenticated apiLoginTicket.
* Throws an exception if the id token is not valid.
* The audience parameter can be used to control which id tokens are
* accepted. By default, the id token must have been issued to this OAuth2 client.
*
* @param string $idToken the ID token in JWT format
* @param string $audience Optional. The audience to verify against JWt "aud"
* @return array the token payload, if successful
*/
public function verifyIdToken($idToken, $audience = null)
{
if (empty($idToken)) {
throw new LogicException('id_token cannot be null');
}
// set phpseclib constants if applicable
$this->setPhpsecConstants();
// Check signature
$certs = $this->getFederatedSignOnCerts();
foreach ($certs as $cert) {
try {
$payload = $this->jwt->decode(
$idToken,
$this->getPublicKey($cert),
array('RS256')
);
if (property_exists($payload, 'aud')) {
if ($audience && $payload->aud != $audience) {
return false;
}
}
// support HTTP and HTTPS issuers
// @see https://developers.google.com/identity/sign-in/web/backend-auth
$issuers = array(self::OAUTH2_ISSUER, self::OAUTH2_ISSUER_HTTPS);
if (!isset($payload->iss) || !in_array($payload->iss, $issuers)) {
return false;
}
return (array) $payload;
} catch (ExpiredException $e) {
return false;
} catch (ExpiredExceptionV3 $e) {
return false;
} catch (SignatureInvalidException $e) {
// continue
} catch (DomainException $e) {
// continue
}
}
return false;
}
private function getCache()
{
return $this->cache;
}
/**
* Retrieve and cache a certificates file.
*
* @param $url string location
* @throws \Google\Exception
* @return array certificates
*/
private function retrieveCertsFromLocation($url)
{
// If we're retrieving a local file, just grab it.
if (0 !== strpos($url, 'http')) {
if (!$file = file_get_contents($url)) {
throw new GoogleException(
"Failed to retrieve verification certificates: '" .
$url . "'."
);
}
return json_decode($file, true);
}
$response = $this->http->get($url);
if ($response->getStatusCode() == 200) {
return json_decode((string) $response->getBody(), true);
}
throw new GoogleException(
sprintf(
'Failed to retrieve verification certificates: "%s".',
$response->getBody()->getContents()
),
$response->getStatusCode()
);
}
// Gets federated sign-on certificates to use for verifying identity tokens.
// Returns certs as array structure, where keys are key ids, and values
// are PEM encoded certificates.
private function getFederatedSignOnCerts()
{
$certs = null;
if ($cache = $this->getCache()) {
$cacheItem = $cache->getItem('federated_signon_certs_v3');
$certs = $cacheItem->get();
}
if (!$certs) {
$certs = $this->retrieveCertsFromLocation(
self::FEDERATED_SIGNON_CERT_URL
);
if ($cache) {
$cacheItem->expiresAt(new DateTime('+1 hour'));
$cacheItem->set($certs);
$cache->save($cacheItem);
}
}
if (!isset($certs['keys'])) {
throw new InvalidArgumentException(
'federated sign-on certs expects "keys" to be set'
);
}
return $certs['keys'];
}
private function getJwtService()
{
$jwtClass = 'JWT';
if (class_exists('\Firebase\JWT\JWT')) {
$jwtClass = 'Firebase\JWT\JWT';
}
if (property_exists($jwtClass, 'leeway') && $jwtClass::$leeway < 1) {
// Ensures JWT leeway is at least 1
// @see https://github.com/google/google-api-php-client/issues/827
$jwtClass::$leeway = 1;
}
return new $jwtClass;
}
private function getPublicKey($cert)
{
$bigIntClass = $this->getBigIntClass();
$modulus = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['n']), 256);
$exponent = new $bigIntClass($this->jwt->urlsafeB64Decode($cert['e']), 256);
$component = array('n' => $modulus, 'e' => $exponent);
if (class_exists('phpseclib3\Crypt\RSA\PublicKey')) {
/** @var PublicKey $loader */
$loader = PublicKeyLoader::load($component);
return $loader->toString('PKCS8');
}
$rsaClass = $this->getRsaClass();
$rsa = new $rsaClass();
$rsa->loadKey($component);
return $rsa->getPublicKey();
}
private function getRsaClass()
{
if (class_exists('phpseclib3\Crypt\RSA')) {
return 'phpseclib3\Crypt\RSA';
}
if (class_exists('phpseclib\Crypt\RSA')) {
return 'phpseclib\Crypt\RSA';
}
return 'Crypt_RSA';
}
private function getBigIntClass()
{
if (class_exists('phpseclib3\Math\BigInteger')) {
return 'phpseclib3\Math\BigInteger';
}
if (class_exists('phpseclib\Math\BigInteger')) {
return 'phpseclib\Math\BigInteger';
}
return 'Math_BigInteger';
}
private function getOpenSslConstant()
{
if (class_exists('phpseclib3\Crypt\AES')) {
return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL';
}
if (class_exists('phpseclib\Crypt\RSA')) {
return 'phpseclib\Crypt\RSA::MODE_OPENSSL';
}
if (class_exists('Crypt_RSA')) {
return 'CRYPT_RSA_MODE_OPENSSL';
}
throw new Exception('Cannot find RSA class');
}
/**
* phpseclib calls "phpinfo" by default, which requires special
* whitelisting in the AppEngine VM environment. This function
* sets constants to bypass the need for phpseclib to check phpinfo
*
* @see phpseclib/Math/BigInteger
* @see https://github.com/GoogleCloudPlatform/getting-started-php/issues/85
*/
private function setPhpsecConstants()
{
if (filter_var(getenv('GAE_VM'), FILTER_VALIDATE_BOOLEAN)) {
if (!defined('MATH_BIGINTEGER_OPENSSL_ENABLED')) {
define('MATH_BIGINTEGER_OPENSSL_ENABLED', true);
}
if (!defined('CRYPT_RSA_MODE')) {
define('CRYPT_RSA_MODE', constant($this->getOpenSslConstant()));
}
}
}
}

View file

@ -0,0 +1,52 @@
<?php
/**
* Copyright 2015 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AuthHandler;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Exception;
class AuthHandlerFactory
{
/**
* Builds out a default http handler for the installed version of guzzle.
*
* @return Guzzle5AuthHandler|Guzzle6AuthHandler|Guzzle7AuthHandler
* @throws Exception
*/
public static function build($cache = null, array $cacheConfig = [])
{
$guzzleVersion = null;
if (defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
$guzzleVersion = ClientInterface::MAJOR_VERSION;
} elseif (defined('\GuzzleHttp\ClientInterface::VERSION')) {
$guzzleVersion = (int) substr(ClientInterface::VERSION, 0, 1);
}
switch ($guzzleVersion) {
case 5:
return new Guzzle5AuthHandler($cache, $cacheConfig);
case 6:
return new Guzzle6AuthHandler($cache, $cacheConfig);
case 7:
return new Guzzle7AuthHandler($cache, $cacheConfig);
default:
throw new Exception('Version not supported');
}
}
}

View file

@ -0,0 +1,110 @@
<?php
namespace Google\AuthHandler;
use Google\Auth\CredentialsLoader;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\Subscriber\AuthTokenSubscriber;
use Google\Auth\Subscriber\ScopedAccessTokenSubscriber;
use Google\Auth\Subscriber\SimpleSubscriber;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
*
*/
class Guzzle5AuthHandler
{
protected $cache;
protected $cacheConfig;
public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = [])
{
$this->cache = $cache;
$this->cacheConfig = $cacheConfig;
}
public function attachCredentials(
ClientInterface $http,
CredentialsLoader $credentials,
callable $tokenCallback = null
) {
// use the provided cache
if ($this->cache) {
$credentials = new FetchAuthTokenCache(
$credentials,
$this->cacheConfig,
$this->cache
);
}
return $this->attachCredentialsCache($http, $credentials, $tokenCallback);
}
public function attachCredentialsCache(
ClientInterface $http,
FetchAuthTokenCache $credentials,
callable $tokenCallback = null
) {
// if we end up needing to make an HTTP request to retrieve credentials, we
// can use our existing one, but we need to throw exceptions so the error
// bubbles up.
$authHttp = $this->createAuthHttp($http);
$authHttpHandler = HttpHandlerFactory::build($authHttp);
$subscriber = new AuthTokenSubscriber(
$credentials,
$authHttpHandler,
$tokenCallback
);
$http->setDefaultOption('auth', 'google_auth');
$http->getEmitter()->attach($subscriber);
return $http;
}
public function attachToken(ClientInterface $http, array $token, array $scopes)
{
$tokenFunc = function ($scopes) use ($token) {
return $token['access_token'];
};
$subscriber = new ScopedAccessTokenSubscriber(
$tokenFunc,
$scopes,
$this->cacheConfig,
$this->cache
);
$http->setDefaultOption('auth', 'scoped');
$http->getEmitter()->attach($subscriber);
return $http;
}
public function attachKey(ClientInterface $http, $key)
{
$subscriber = new SimpleSubscriber(['key' => $key]);
$http->setDefaultOption('auth', 'simple');
$http->getEmitter()->attach($subscriber);
return $http;
}
private function createAuthHttp(ClientInterface $http)
{
return new Client(
[
'base_url' => $http->getBaseUrl(),
'defaults' => [
'exceptions' => true,
'verify' => $http->getDefaultOption('verify'),
'proxy' => $http->getDefaultOption('proxy'),
]
]
);
}
}

View file

@ -0,0 +1,117 @@
<?php
namespace Google\AuthHandler;
use Google\Auth\CredentialsLoader;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\Middleware\AuthTokenMiddleware;
use Google\Auth\Middleware\ScopedAccessTokenMiddleware;
use Google\Auth\Middleware\SimpleMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use Psr\Cache\CacheItemPoolInterface;
/**
* This supports Guzzle 6
*/
class Guzzle6AuthHandler
{
protected $cache;
protected $cacheConfig;
public function __construct(CacheItemPoolInterface $cache = null, array $cacheConfig = [])
{
$this->cache = $cache;
$this->cacheConfig = $cacheConfig;
}
public function attachCredentials(
ClientInterface $http,
CredentialsLoader $credentials,
callable $tokenCallback = null
) {
// use the provided cache
if ($this->cache) {
$credentials = new FetchAuthTokenCache(
$credentials,
$this->cacheConfig,
$this->cache
);
}
return $this->attachCredentialsCache($http, $credentials, $tokenCallback);
}
public function attachCredentialsCache(
ClientInterface $http,
FetchAuthTokenCache $credentials,
callable $tokenCallback = null
) {
// if we end up needing to make an HTTP request to retrieve credentials, we
// can use our existing one, but we need to throw exceptions so the error
// bubbles up.
$authHttp = $this->createAuthHttp($http);
$authHttpHandler = HttpHandlerFactory::build($authHttp);
$middleware = new AuthTokenMiddleware(
$credentials,
$authHttpHandler,
$tokenCallback
);
$config = $http->getConfig();
$config['handler']->remove('google_auth');
$config['handler']->push($middleware, 'google_auth');
$config['auth'] = 'google_auth';
$http = new Client($config);
return $http;
}
public function attachToken(ClientInterface $http, array $token, array $scopes)
{
$tokenFunc = function ($scopes) use ($token) {
return $token['access_token'];
};
$middleware = new ScopedAccessTokenMiddleware(
$tokenFunc,
$scopes,
$this->cacheConfig,
$this->cache
);
$config = $http->getConfig();
$config['handler']->remove('google_auth');
$config['handler']->push($middleware, 'google_auth');
$config['auth'] = 'scoped';
$http = new Client($config);
return $http;
}
public function attachKey(ClientInterface $http, $key)
{
$middleware = new SimpleMiddleware(['key' => $key]);
$config = $http->getConfig();
$config['handler']->remove('google_auth');
$config['handler']->push($middleware, 'google_auth');
$config['auth'] = 'simple';
$http = new Client($config);
return $http;
}
private function createAuthHttp(ClientInterface $http)
{
return new Client(
[
'base_uri' => $http->getConfig('base_uri'),
'http_errors' => true,
'verify' => $http->getConfig('verify'),
'proxy' => $http->getConfig('proxy'),
]
);
}
}

View file

@ -0,0 +1,25 @@
<?php
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\AuthHandler;
/**
* This supports Guzzle 7
*/
class Guzzle7AuthHandler extends Guzzle6AuthHandler
{
}

1274
vendor/google/apiclient/src/Client.php vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,104 @@
<?php
namespace Google;
/**
* Extension to the regular Google\Model that automatically
* exposes the items array for iteration, so you can just
* iterate over the object rather than a reference inside.
*/
class Collection extends Model implements \Iterator, \Countable
{
protected $collection_key = 'items';
#[\ReturnTypeWillChange]
public function rewind()
{
if (isset($this->{$this->collection_key})
&& is_array($this->{$this->collection_key})) {
reset($this->{$this->collection_key});
}
}
#[\ReturnTypeWillChange]
public function current()
{
$this->coerceType($this->key());
if (is_array($this->{$this->collection_key})) {
return current($this->{$this->collection_key});
}
}
#[\ReturnTypeWillChange]
public function key()
{
if (isset($this->{$this->collection_key})
&& is_array($this->{$this->collection_key})) {
return key($this->{$this->collection_key});
}
}
#[\ReturnTypeWillChange]
public function next()
{
return next($this->{$this->collection_key});
}
#[\ReturnTypeWillChange]
public function valid()
{
$key = $this->key();
return $key !== null && $key !== false;
}
#[\ReturnTypeWillChange]
public function count()
{
if (!isset($this->{$this->collection_key})) {
return 0;
}
return count($this->{$this->collection_key});
}
public function offsetExists($offset)
{
if (!is_numeric($offset)) {
return parent::offsetExists($offset);
}
return isset($this->{$this->collection_key}[$offset]);
}
public function offsetGet($offset)
{
if (!is_numeric($offset)) {
return parent::offsetGet($offset);
}
$this->coerceType($offset);
return $this->{$this->collection_key}[$offset];
}
public function offsetSet($offset, $value)
{
if (!is_numeric($offset)) {
parent::offsetSet($offset, $value);
}
$this->{$this->collection_key}[$offset] = $value;
}
public function offsetUnset($offset)
{
if (!is_numeric($offset)) {
parent::offsetUnset($offset);
}
unset($this->{$this->collection_key}[$offset]);
}
private function coerceType($offset)
{
$keyType = $this->keyType($this->collection_key);
if ($keyType && !is_object($this->{$this->collection_key}[$offset])) {
$this->{$this->collection_key}[$offset] =
new $keyType($this->{$this->collection_key}[$offset]);
}
}
}

View file

@ -0,0 +1,24 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google;
use Exception as BaseException;
class Exception extends BaseException
{
}

View file

@ -0,0 +1,258 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Http;
use Google\Client;
use Google\Http\REST;
use Google\Service\Exception as GoogleServiceException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* Class to handle batched requests to the Google API service.
*
* Note that calls to `Google\Http\Batch::execute()` do not clear the queued
* requests. To start a new batch, be sure to create a new instance of this
* class.
*/
class Batch
{
const BATCH_PATH = 'batch';
private static $CONNECTION_ESTABLISHED_HEADERS = array(
"HTTP/1.0 200 Connection established\r\n\r\n",
"HTTP/1.1 200 Connection established\r\n\r\n",
);
/** @var string Multipart Boundary. */
private $boundary;
/** @var array service requests to be executed. */
private $requests = array();
/** @var Client */
private $client;
private $rootUrl;
private $batchPath;
public function __construct(
Client $client,
$boundary = false,
$rootUrl = null,
$batchPath = null
) {
$this->client = $client;
$this->boundary = $boundary ?: mt_rand();
$this->rootUrl = rtrim($rootUrl ?: $this->client->getConfig('base_path'), '/');
$this->batchPath = $batchPath ?: self::BATCH_PATH;
}
public function add(RequestInterface $request, $key = false)
{
if (false == $key) {
$key = mt_rand();
}
$this->requests[$key] = $request;
}
public function execute()
{
$body = '';
$classes = array();
$batchHttpTemplate = <<<EOF
--%s
Content-Type: application/http
Content-Transfer-Encoding: binary
MIME-Version: 1.0
Content-ID: %s
%s
%s%s
EOF;
/** @var RequestInterface $req */
foreach ($this->requests as $key => $request) {
$firstLine = sprintf(
'%s %s HTTP/%s',
$request->getMethod(),
$request->getRequestTarget(),
$request->getProtocolVersion()
);
$content = (string) $request->getBody();
$headers = '';
foreach ($request->getHeaders() as $name => $values) {
$headers .= sprintf("%s:%s\r\n", $name, implode(', ', $values));
}
$body .= sprintf(
$batchHttpTemplate,
$this->boundary,
$key,
$firstLine,
$headers,
$content ? "\n".$content : ''
);
$classes['response-' . $key] = $request->getHeaderLine('X-Php-Expected-Class');
}
$body .= "--{$this->boundary}--";
$body = trim($body);
$url = $this->rootUrl . '/' . $this->batchPath;
$headers = array(
'Content-Type' => sprintf('multipart/mixed; boundary=%s', $this->boundary),
'Content-Length' => strlen($body),
);
$request = new Request(
'POST',
$url,
$headers,
$body
);
$response = $this->client->execute($request);
return $this->parseResponse($response, $classes);
}
public function parseResponse(ResponseInterface $response, $classes = array())
{
$contentType = $response->getHeaderLine('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 = (string) $response->getBody();
if (!empty($body)) {
$body = str_replace("--$boundary--", "--$boundary", $body);
$parts = explode("--$boundary", $body);
$responses = array();
$requests = array_values($this->requests);
foreach ($parts as $i => $part) {
$part = trim($part);
if (!empty($part)) {
list($rawHeaders, $part) = explode("\r\n\r\n", $part, 2);
$headers = $this->parseRawHeaders($rawHeaders);
$status = substr($part, 0, strpos($part, "\n"));
$status = explode(" ", $status);
$status = $status[1];
list($partHeaders, $partBody) = $this->parseHttpResponse($part, false);
$response = new Response(
$status,
$partHeaders,
Psr7\Utils::streamFor($partBody)
);
// Need content id.
$key = $headers['content-id'];
try {
$response = REST::decodeHttpResponse($response, $requests[$i-1]);
} catch (GoogleServiceException $e) {
// Store the exception as the response, so successful responses
// can be processed.
$response = $e;
}
$responses[$key] = $response;
}
}
return $responses;
}
return null;
}
private function parseRawHeaders($rawHeaders)
{
$headers = array();
$responseHeaderLines = explode("\r\n", $rawHeaders);
foreach ($responseHeaderLines as $headerLine) {
if ($headerLine && strpos($headerLine, ':') !== false) {
list($header, $value) = explode(': ', $headerLine, 2);
$header = strtolower($header);
if (isset($headers[$header])) {
$headers[$header] .= "\n" . $value;
} else {
$headers[$header] = $value;
}
}
}
return $headers;
}
/**
* Used by the IO lib and also the batch processing.
*
* @param $respData
* @param $headerSize
* @return array
*/
private function parseHttpResponse($respData, $headerSize)
{
// check proxy header
foreach (self::$CONNECTION_ESTABLISHED_HEADERS as $established_header) {
if (stripos($respData, $established_header) !== false) {
// existed, remove it
$respData = str_ireplace($established_header, '', $respData);
// Subtract the proxy header size unless the cURL bug prior to 7.30.0
// is present which prevented the proxy header size from being taken into
// account.
// @TODO look into this
// if (!$this->needsQuirk()) {
// $headerSize -= strlen($established_header);
// }
break;
}
}
if ($headerSize) {
$responseBody = substr($respData, $headerSize);
$responseHeaders = substr($respData, 0, $headerSize);
} else {
$responseSegments = explode("\r\n\r\n", $respData, 2);
$responseHeaders = $responseSegments[0];
$responseBody = isset($responseSegments[1]) ? $responseSegments[1] :
null;
}
$responseHeaders = $this->parseRawHeaders($responseHeaders);
return array($responseHeaders, $responseBody);
}
}

View file

@ -0,0 +1,358 @@
<?php
/**
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Http;
use Google\Client;
use Google\Http\REST;
use Google\Exception as GoogleException;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use Psr\Http\Message\RequestInterface;
/**
* Manage large file uploads, which may be media but can be any type
* of sizable data.
*/
class MediaFileUpload
{
const UPLOAD_MEDIA_TYPE = 'media';
const UPLOAD_MULTIPART_TYPE = 'multipart';
const UPLOAD_RESUMABLE_TYPE = 'resumable';
/** @var string $mimeType */
private $mimeType;
/** @var string $data */
private $data;
/** @var bool $resumable */
private $resumable;
/** @var int $chunkSize */
private $chunkSize;
/** @var int $size */
private $size;
/** @var string $resumeUri */
private $resumeUri;
/** @var int $progress */
private $progress;
/** @var Client */
private $client;
/** @var RequestInterface */
private $request;
/** @var string */
private $boundary;
/**
* Result code from last HTTP call
* @var int
*/
private $httpResultCode;
/**
* @param Client $client
* @param RequestInterface $request
* @param string $mimeType
* @param string $data The bytes you want to upload.
* @param bool $resumable
* @param bool $chunkSize File will be uploaded in chunks of this many bytes.
* only used if resumable=True
*/
public function __construct(
Client $client,
RequestInterface $request,
$mimeType,
$data,
$resumable = false,
$chunkSize = false
) {
$this->client = $client;
$this->request = $request;
$this->mimeType = $mimeType;
$this->data = $data;
$this->resumable = $resumable;
$this->chunkSize = $chunkSize;
$this->progress = 0;
$this->process();
}
/**
* Set the size of the file that is being uploaded.
* @param $size - int file size in bytes
*/
public function setFileSize($size)
{
$this->size = $size;
}
/**
* Return the progress on the upload
* @return int progress in bytes uploaded.
*/
public function getProgress()
{
return $this->progress;
}
/**
* Send the next part of the file to upload.
* @param string|bool $chunk Optional. The next set of bytes to send. If false will
* use $data passed at construct time.
*/
public function nextChunk($chunk = false)
{
$resumeUri = $this->getResumeUri();
if (false == $chunk) {
$chunk = substr($this->data, $this->progress, $this->chunkSize);
}
$lastBytePos = $this->progress + strlen($chunk) - 1;
$headers = array(
'content-range' => "bytes $this->progress-$lastBytePos/$this->size",
'content-length' => strlen($chunk),
'expect' => '',
);
$request = new Request(
'PUT',
$resumeUri,
$headers,
Psr7\Utils::streamFor($chunk)
);
return $this->makePutRequest($request);
}
/**
* Return the HTTP result code from the last call made.
* @return int code
*/
public function getHttpResultCode()
{
return $this->httpResultCode;
}
/**
* Sends a PUT-Request to google drive and parses the response,
* setting the appropiate variables from the response()
*
* @param RequestInterface $request the Request which will be send
*
* @return false|mixed false when the upload is unfinished or the decoded http response
*
*/
private function makePutRequest(RequestInterface $request)
{
$response = $this->client->execute($request);
$this->httpResultCode = $response->getStatusCode();
if (308 == $this->httpResultCode) {
// Track the amount uploaded.
$range = $response->getHeaderLine('range');
if ($range) {
$range_array = explode('-', $range);
$this->progress = $range_array[1] + 1;
}
// Allow for changing upload URLs.
$location = $response->getHeaderLine('location');
if ($location) {
$this->resumeUri = $location;
}
// No problems, but upload not complete.
return false;
}
return REST::decodeHttpResponse($response, $this->request);
}
/**
* Resume a previously unfinished upload
* @param $resumeUri the resume-URI of the unfinished, resumable upload.
*/
public function resume($resumeUri)
{
$this->resumeUri = $resumeUri;
$headers = array(
'content-range' => "bytes */$this->size",
'content-length' => 0,
);
$httpRequest = new Request(
'PUT',
$this->resumeUri,
$headers
);
return $this->makePutRequest($httpRequest);
}
/**
* @return RequestInterface
* @visible for testing
*/
private function process()
{
$this->transformToUploadUrl();
$request = $this->request;
$postBody = '';
$contentType = false;
$meta = (string) $request->getBody();
$meta = is_string($meta) ? json_decode($meta, true) : $meta;
$uploadType = $this->getUploadType($meta);
$request = $request->withUri(
Uri::withQueryValue($request->getUri(), 'uploadType', $uploadType)
);
$mimeType = $this->mimeType ?: $request->getHeaderLine('content-type');
if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) {
$contentType = $mimeType;
$postBody = is_string($meta) ? $meta : json_encode($meta);
} else if (self::UPLOAD_MEDIA_TYPE == $uploadType) {
$contentType = $mimeType;
$postBody = $this->data;
} else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) {
// This is a multipart/related upload.
$boundary = $this->boundary ?: mt_rand();
$boundary = str_replace('"', '', $boundary);
$contentType = 'multipart/related; boundary=' . $boundary;
$related = "--$boundary\r\n";
$related .= "Content-Type: application/json; charset=UTF-8\r\n";
$related .= "\r\n" . json_encode($meta) . "\r\n";
$related .= "--$boundary\r\n";
$related .= "Content-Type: $mimeType\r\n";
$related .= "Content-Transfer-Encoding: base64\r\n";
$related .= "\r\n" . base64_encode($this->data) . "\r\n";
$related .= "--$boundary--";
$postBody = $related;
}
$request = $request->withBody(Psr7\Utils::streamFor($postBody));
if (isset($contentType) && $contentType) {
$request = $request->withHeader('content-type', $contentType);
}
return $this->request = $request;
}
/**
* Valid upload types:
* - resumable (UPLOAD_RESUMABLE_TYPE)
* - media (UPLOAD_MEDIA_TYPE)
* - multipart (UPLOAD_MULTIPART_TYPE)
* @param $meta
* @return string
* @visible for testing
*/
public function getUploadType($meta)
{
if ($this->resumable) {
return self::UPLOAD_RESUMABLE_TYPE;
}
if (false == $meta && $this->data) {
return self::UPLOAD_MEDIA_TYPE;
}
return self::UPLOAD_MULTIPART_TYPE;
}
public function getResumeUri()
{
if (null === $this->resumeUri) {
$this->resumeUri = $this->fetchResumeUri();
}
return $this->resumeUri;
}
private function fetchResumeUri()
{
$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 = $this->request->withHeader($key, $value);
}
}
$response = $this->client->execute($this->request, false);
$location = $response->getHeaderLine('location');
$code = $response->getStatusCode();
if (200 == $code && true == $location) {
return $location;
}
$message = $code;
$body = json_decode((string) $this->request->getBody(), true);
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 GoogleException($error);
}
private function transformToUploadUrl()
{
$parts = parse_url((string) $this->request->getUri());
if (!isset($parts['path'])) {
$parts['path'] = '';
}
$parts['path'] = '/upload' . $parts['path'];
$uri = Uri::fromParts($parts);
$this->request = $this->request->withUri($uri);
}
public function setChunkSize($chunkSize)
{
$this->chunkSize = $chunkSize;
}
public function getRequest()
{
return $this->request;
}
}

View file

@ -0,0 +1,192 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Http;
use Google\Auth\HttpHandler\HttpHandlerFactory;
use Google\Client;
use Google\Task\Runner;
use Google\Service\Exception as GoogleServiceException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
/**
* This class implements the RESTful transport of apiServiceRequest()'s
*/
class REST
{
/**
* Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries
* when errors occur.
*
* @param Client $client
* @param RequestInterface $req
* @param string $expectedClass
* @param array $config
* @param array $retryMap
* @return mixed decoded result
* @throws \Google\Service\Exception on server side error (ie: not authenticated,
* invalid or malformed post body, invalid url)
*/
public static function execute(
ClientInterface $client,
RequestInterface $request,
$expectedClass = null,
$config = array(),
$retryMap = null
) {
$runner = new Runner(
$config,
sprintf('%s %s', $request->getMethod(), (string) $request->getUri()),
array(get_class(), 'doExecute'),
array($client, $request, $expectedClass)
);
if (null !== $retryMap) {
$runner->setRetryMap($retryMap);
}
return $runner->run();
}
/**
* Executes a Psr\Http\Message\RequestInterface
*
* @param Client $client
* @param RequestInterface $request
* @param string $expectedClass
* @return array decoded result
* @throws \Google\Service\Exception on server side error (ie: not authenticated,
* invalid or malformed post body, invalid url)
*/
public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null)
{
try {
$httpHandler = HttpHandlerFactory::build($client);
$response = $httpHandler($request);
} catch (RequestException $e) {
// if Guzzle throws an exception, catch it and handle the response
if (!$e->hasResponse()) {
throw $e;
}
$response = $e->getResponse();
// specific checking for Guzzle 5: convert to PSR7 response
if ($response instanceof \GuzzleHttp\Message\ResponseInterface) {
$response = new Response(
$response->getStatusCode(),
$response->getHeaders() ?: [],
$response->getBody(),
$response->getProtocolVersion(),
$response->getReasonPhrase()
);
}
}
return self::decodeHttpResponse($response, $request, $expectedClass);
}
/**
* Decode an HTTP Response.
* @static
* @throws \Google\Service\Exception
* @param RequestInterface $response The http response to be decoded.
* @param ResponseInterface $response
* @param string $expectedClass
* @return mixed|null
*/
public static function decodeHttpResponse(
ResponseInterface $response,
RequestInterface $request = null,
$expectedClass = null
) {
$code = $response->getStatusCode();
// retry strategy
if (intVal($code) >= 400) {
// if we errored out, it should be safe to grab the response body
$body = (string) $response->getBody();
// Check if we received errors, and add those to the Exception for convenience
throw new GoogleServiceException($body, $code, null, self::getResponseErrors($body));
}
// Ensure we only pull the entire body into memory if the request is not
// of media type
$body = self::decodeBody($response, $request);
if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) {
$json = json_decode($body, true);
return new $expectedClass($json);
}
return $response;
}
private static function decodeBody(ResponseInterface $response, RequestInterface $request = null)
{
if (self::isAltMedia($request)) {
// don't decode the body, it's probably a really long string
return '';
}
return (string) $response->getBody();
}
private static function determineExpectedClass($expectedClass, RequestInterface $request = null)
{
// "false" is used to explicitly prevent an expected class from being returned
if (false === $expectedClass) {
return null;
}
// if we don't have a request, we just use what's passed in
if (null === $request) {
return $expectedClass;
}
// return what we have in the request header if one was not supplied
return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class');
}
private static function getResponseErrors($body)
{
$json = json_decode($body, true);
if (isset($json['error']['errors'])) {
return $json['error']['errors'];
}
return null;
}
private static function isAltMedia(RequestInterface $request = null)
{
if ($request && $qs = $request->getUri()->getQuery()) {
parse_str($qs, $query);
if (isset($query['alt']) && $query['alt'] == 'media') {
return true;
}
}
return false;
}
}

328
vendor/google/apiclient/src/Model.php vendored Normal file
View file

@ -0,0 +1,328 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google;
use Google\Exception as GoogleException;
use ReflectionObject;
use ReflectionProperty;
use stdClass;
/**
* This class defines attributes, valid values, and usage which is generated
* from a given json schema.
* http://tools.ietf.org/html/draft-zyp-json-schema-03#section-5
*
*/
class Model implements \ArrayAccess
{
/**
* If you need to specify a NULL JSON value, use Google\Model::NULL_VALUE
* instead - it will be replaced when converting to JSON with a real null.
*/
const NULL_VALUE = "{}gapi-php-null";
protected $internal_gapi_mappings = array();
protected $modelData = array();
protected $processed = array();
/**
* Polymorphic - accepts a variable number of arguments dependent
* on the type of the model subclass.
*/
final public function __construct()
{
if (func_num_args() == 1 && is_array(func_get_arg(0))) {
// Initialize the model with the array's contents.
$array = func_get_arg(0);
$this->mapTypes($array);
}
$this->gapiInit();
}
/**
* Getter that handles passthrough access to the data array, and lazy object creation.
* @param string $key Property name.
* @return mixed The value if any, or null.
*/
public function __get($key)
{
$keyType = $this->keyType($key);
$keyDataType = $this->dataType($key);
if ($keyType && !isset($this->processed[$key])) {
if (isset($this->modelData[$key])) {
$val = $this->modelData[$key];
} elseif ($keyDataType == 'array' || $keyDataType == 'map') {
$val = array();
} else {
$val = null;
}
if ($this->isAssociativeArray($val)) {
if ($keyDataType && 'map' == $keyDataType) {
foreach ($val as $arrayKey => $arrayItem) {
$this->modelData[$key][$arrayKey] =
new $keyType($arrayItem);
}
} else {
$this->modelData[$key] = new $keyType($val);
}
} else if (is_array($val)) {
$arrayObject = array();
foreach ($val as $arrayIndex => $arrayItem) {
$arrayObject[$arrayIndex] = new $keyType($arrayItem);
}
$this->modelData[$key] = $arrayObject;
}
$this->processed[$key] = true;
}
return isset($this->modelData[$key]) ? $this->modelData[$key] : null;
}
/**
* Initialize this object's properties from an array.
*
* @param array $array Used to seed this object's properties.
* @return void
*/
protected function mapTypes($array)
{
// Hard initialise simple types, lazy load more complex ones.
foreach ($array as $key => $val) {
if ($keyType = $this->keyType($key)) {
$dataType = $this->dataType($key);
if ($dataType == 'array' || $dataType == 'map') {
$this->$key = array();
foreach ($val as $itemKey => $itemVal) {
if ($itemVal instanceof $keyType) {
$this->{$key}[$itemKey] = $itemVal;
} else {
$this->{$key}[$itemKey] = new $keyType($itemVal);
}
}
} elseif ($val instanceof $keyType) {
$this->$key = $val;
} else {
$this->$key = new $keyType($val);
}
unset($array[$key]);
} elseif (property_exists($this, $key)) {
$this->$key = $val;
unset($array[$key]);
} elseif (property_exists($this, $camelKey = $this->camelCase($key))) {
// This checks if property exists as camelCase, leaving it in array as snake_case
// in case of backwards compatibility issues.
$this->$camelKey = $val;
}
}
$this->modelData = $array;
}
/**
* Blank initialiser to be used in subclasses to do post-construction initialisation - this
* avoids the need for subclasses to have to implement the variadics handling in their
* constructors.
*/
protected function gapiInit()
{
return;
}
/**
* Create a simplified object suitable for straightforward
* conversion to JSON. This is relatively expensive
* due to the usage of reflection, but shouldn't be called
* a whole lot, and is the most straightforward way to filter.
*/
public function toSimpleObject()
{
$object = new stdClass();
// Process all other data.
foreach ($this->modelData as $key => $val) {
$result = $this->getSimpleValue($val);
if ($result !== null) {
$object->$key = $this->nullPlaceholderCheck($result);
}
}
// Process all public properties.
$reflect = new ReflectionObject($this);
$props = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
foreach ($props as $member) {
$name = $member->getName();
$result = $this->getSimpleValue($this->$name);
if ($result !== null) {
$name = $this->getMappedName($name);
$object->$name = $this->nullPlaceholderCheck($result);
}
}
return $object;
}
/**
* Handle different types of values, primarily
* other objects and map and array data types.
*/
private function getSimpleValue($value)
{
if ($value instanceof Model) {
return $value->toSimpleObject();
} else if (is_array($value)) {
$return = array();
foreach ($value as $key => $a_value) {
$a_value = $this->getSimpleValue($a_value);
if ($a_value !== null) {
$key = $this->getMappedName($key);
$return[$key] = $this->nullPlaceholderCheck($a_value);
}
}
return $return;
}
return $value;
}
/**
* Check whether the value is the null placeholder and return true null.
*/
private function nullPlaceholderCheck($value)
{
if ($value === self::NULL_VALUE) {
return null;
}
return $value;
}
/**
* If there is an internal name mapping, use that.
*/
private function getMappedName($key)
{
if (isset($this->internal_gapi_mappings, $this->internal_gapi_mappings[$key])) {
$key = $this->internal_gapi_mappings[$key];
}
return $key;
}
/**
* Returns true only if the array is associative.
* @param array $array
* @return bool True if the array is associative.
*/
protected function isAssociativeArray($array)
{
if (!is_array($array)) {
return false;
}
$keys = array_keys($array);
foreach ($keys as $key) {
if (is_string($key)) {
return true;
}
}
return false;
}
/**
* Verify if $obj is an array.
* @throws \Google\Exception Thrown if $obj isn't an array.
* @param array $obj Items that should be validated.
* @param string $method Method expecting an array as an argument.
*/
public function assertIsArray($obj, $method)
{
if ($obj && !is_array($obj)) {
throw new GoogleException(
"Incorrect parameter type passed to $method(). Expected an array."
);
}
}
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return isset($this->$offset) || isset($this->modelData[$offset]);
}
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return isset($this->$offset) ?
$this->$offset :
$this->__get($offset);
}
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
if (property_exists($this, $offset)) {
$this->$offset = $value;
} else {
$this->modelData[$offset] = $value;
$this->processed[$offset] = true;
}
}
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
unset($this->modelData[$offset]);
}
protected function keyType($key)
{
$keyType = $key . "Type";
// ensure keyType is a valid class
if (property_exists($this, $keyType) && class_exists($this->$keyType)) {
return $this->$keyType;
}
}
protected function dataType($key)
{
$dataType = $key . "DataType";
if (property_exists($this, $dataType)) {
return $this->$dataType;
}
}
public function __isset($key)
{
return isset($this->modelData[$key]);
}
public function __unset($key)
{
unset($this->modelData[$key]);
}
/**
* Convert a string to camelCase
* @param string $value
* @return string
*/
private function camelCase($value)
{
$value = ucwords(str_replace(array('-', '_'), ' ', $value));
$value = str_replace(' ', '', $value);
$value[0] = strtolower($value[0]);
return $value;
}
}

71
vendor/google/apiclient/src/Service.php vendored Normal file
View file

@ -0,0 +1,71 @@
<?php
/*
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google;
use Google\Http\Batch;
use TypeError;
class Service
{
public $batchPath;
public $rootUrl;
public $version;
public $servicePath;
public $availableScopes;
public $resource;
private $client;
public function __construct($clientOrConfig = [])
{
if ($clientOrConfig instanceof Client) {
$this->client = $clientOrConfig;
} elseif (is_array($clientOrConfig)) {
$this->client = new Client($clientOrConfig ?: []);
} else {
$errorMessage = 'constructor must be array or instance of Google\Client';
if (class_exists('TypeError')) {
throw new TypeError($errorMessage);
}
trigger_error($errorMessage, E_USER_ERROR);
}
}
/**
* Return the associated Google\Client class.
* @return \Google\Client
*/
public function getClient()
{
return $this->client;
}
/**
* Create a new HTTP Batch handler for this service
*
* @return Batch
*/
public function createBatch()
{
return new Batch(
$this->client,
false,
$this->rootUrl,
$this->batchPath
);
}
}

View file

@ -0,0 +1,71 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Service;
use Google\Exception as GoogleException;
class Exception extends GoogleException
{
/**
* Optional list of errors returned in a JSON body of an HTTP error response.
*/
protected $errors = array();
/**
* Override default constructor to add the ability to set $errors and a retry
* map.
*
* @param string $message
* @param int $code
* @param \Exception|null $previous
* @param [{string, string}] errors List of errors returned in an HTTP
* response. Defaults to [].
*/
public function __construct(
$message,
$code = 0,
Exception $previous = null,
$errors = array()
) {
if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
parent::__construct($message, $code, $previous);
} else {
parent::__construct($message, $code);
}
$this->errors = $errors;
}
/**
* An example of the possible errors returned.
*
* {
* "domain": "global",
* "reason": "authError",
* "message": "Invalid Credentials",
* "locationType": "header",
* "location": "Authorization",
* }
*
* @return [{string, string}] List of errors return in an HTTP response or [].
*/
public function getErrors()
{
return $this->errors;
}
}

View file

@ -0,0 +1,5 @@
# Google API Client Services
Google API Client Service classes have been moved to the
[google-api-php-client-services](https://github.com/google/google-api-php-client-services)
repository.

View file

@ -0,0 +1,308 @@
<?php
/**
* Copyright 2010 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Service;
use Google\Model;
use Google\Http\MediaFileUpload;
use Google\Exception as GoogleException;
use Google\Utils\UriTemplate;
use GuzzleHttp\Psr7\Request;
/**
* Implements the actual methods/resources of the discovered Google API using magic function
* calling overloading (__call()), which on call will see if the method name (plus.activities.list)
* is available in this service, and if so construct an apiHttpRequest representing it.
*
*/
class Resource
{
// Valid query parameters that work, but don't appear in discovery.
private $stackParameters = array(
'alt' => array('type' => 'string', 'location' => 'query'),
'fields' => array('type' => 'string', 'location' => 'query'),
'trace' => array('type' => 'string', 'location' => 'query'),
'userIp' => array('type' => 'string', 'location' => 'query'),
'quotaUser' => array('type' => 'string', 'location' => 'query'),
'data' => array('type' => 'string', 'location' => 'body'),
'mimeType' => array('type' => 'string', 'location' => 'header'),
'uploadType' => array('type' => 'string', 'location' => 'query'),
'mediaUpload' => array('type' => 'complex', 'location' => 'query'),
'prettyPrint' => array('type' => 'string', 'location' => 'query'),
);
/** @var string $rootUrl */
private $rootUrl;
/** @var \Google\Client $client */
private $client;
/** @var string $serviceName */
private $serviceName;
/** @var string $servicePath */
private $servicePath;
/** @var string $resourceName */
private $resourceName;
/** @var array $methods */
private $methods;
public function __construct($service, $serviceName, $resourceName, $resource)
{
$this->rootUrl = $service->rootUrl;
$this->client = $service->getClient();
$this->servicePath = $service->servicePath;
$this->serviceName = $serviceName;
$this->resourceName = $resourceName;
$this->methods = is_array($resource) && isset($resource['methods']) ?
$resource['methods'] :
array($resourceName => $resource);
}
/**
* TODO: This function needs simplifying.
* @param $name
* @param $arguments
* @param $expectedClass - optional, the expected class name
* @return mixed|$expectedClass|ResponseInterface|RequestInterface
* @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 GoogleException(
"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 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 GoogleException("($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 GoogleException("($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 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);
}
protected function convertToArrayAndStripNulls($o)
{
$o = (array) $o;
foreach ($o as $k => $v) {
if ($v === null) {
unset($o[$k]);
} elseif (is_object($v) || is_array($v)) {
$o[$k] = $this->convertToArrayAndStripNulls($o[$k]);
}
}
return $o;
}
/**
* Parse/expand request parameters and create a fully qualified
* request uri.
* @static
* @param string $restPath
* @param array $params
* @return string $requestUrl
*/
public function createRequestUri($restPath, $params)
{
// Override the default servicePath address if the $restPath use a /
if ('/' == substr($restPath, 0, 1)) {
$requestUrl = substr($restPath, 1);
} else {
$requestUrl = $this->servicePath . $restPath;
}
// code for leading slash
if ($this->rootUrl) {
if ('/' !== substr($this->rootUrl, -1) && '/' !== substr($requestUrl, 0, 1)) {
$requestUrl = '/' . $requestUrl;
}
$requestUrl = $this->rootUrl . $requestUrl;
}
$uriTemplateVars = array();
$queryVars = array();
foreach ($params as $paramName => $paramSpec) {
if ($paramSpec['type'] == 'boolean') {
$paramSpec['value'] = $paramSpec['value'] ? 'true' : 'false';
}
if ($paramSpec['location'] == 'path') {
$uriTemplateVars[$paramName] = $paramSpec['value'];
} else if ($paramSpec['location'] == 'query') {
if (is_array($paramSpec['value'])) {
foreach ($paramSpec['value'] as $value) {
$queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($value));
}
} else {
$queryVars[] = $paramName . '=' . rawurlencode(rawurldecode($paramSpec['value']));
}
}
}
if (count($uriTemplateVars)) {
$uriTemplateParser = new UriTemplate();
$requestUrl = $uriTemplateParser->parse($requestUrl, $uriTemplateVars);
}
if (count($queryVars)) {
$requestUrl .= '?' . implode('&', $queryVars);
}
return $requestUrl;
}
}

View file

@ -0,0 +1,115 @@
<?php
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
namespace Google\Task;
use Composer\Script\Event;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use InvalidArgumentException;
class Composer
{
/**
* @param Event $event Composer event passed in for any script method
* @param Filesystem $filesystem Optional. Used for testing.
*/
public static function cleanup(
Event $event,
Filesystem $filesystem = null
) {
$composer = $event->getComposer();
$extra = $composer->getPackage()->getExtra();
$servicesToKeep = isset($extra['google/apiclient-services']) ?
$extra['google/apiclient-services'] : [];
if ($servicesToKeep) {
$vendorDir = $composer->getConfig()->get('vendor-dir');
$serviceDir = sprintf(
'%s/google/apiclient-services/src/Google/Service',
$vendorDir
);
if (!is_dir($serviceDir)) {
// path for google/apiclient-services >= 0.200.0
$serviceDir = sprintf(
'%s/google/apiclient-services/src',
$vendorDir
);
}
self::verifyServicesToKeep($serviceDir, $servicesToKeep);
$finder = self::getServicesToRemove($serviceDir, $servicesToKeep);
$filesystem = $filesystem ?: new Filesystem();
if (0 !== $count = count($finder)) {
$event->getIO()->write(
sprintf(
'Removing %s google services',
$count
)
);
foreach ($finder as $file) {
$realpath = $file->getRealPath();
$filesystem->remove($realpath);
$filesystem->remove($realpath . '.php');
}
}
}
}
/**
* @throws InvalidArgumentException when the service doesn't exist
*/
private static function verifyServicesToKeep(
$serviceDir,
array $servicesToKeep
) {
$finder = (new Finder())
->directories()
->depth('== 0');
foreach ($servicesToKeep as $service) {
if (!preg_match('/^[a-zA-Z0-9]*$/', $service)) {
throw new InvalidArgumentException(
sprintf(
'Invalid Google service name "%s"',
$service
)
);
}
try {
$finder->in($serviceDir . '/' . $service);
} catch (InvalidArgumentException $e) {
throw new InvalidArgumentException(
sprintf(
'Google service "%s" does not exist or was removed previously',
$service
)
);
}
}
}
private static function getServicesToRemove(
$serviceDir,
array $servicesToKeep
) {
// find all files in the current directory
return (new Finder())
->directories()
->depth('== 0')
->in($serviceDir)
->exclude($servicesToKeep);
}
}

View file

@ -0,0 +1,24 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Task;
use Google\Exception as GoogleException;
class Exception extends GoogleException
{
}

View file

@ -0,0 +1,26 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Task;
/**
* Interface for checking how many times a given task can be retried following
* a failure.
*/
interface Retryable
{
}

View file

@ -0,0 +1,287 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Task;
use Google\Service\Exception as GoogleServiceException;
use Google\Task\Exception as GoogleTaskException;
/**
* A task runner with exponential backoff support.
*
* @see https://developers.google.com/drive/web/handle-errors#implementing_exponential_backoff
*/
class Runner
{
const TASK_RETRY_NEVER = 0;
const TASK_RETRY_ONCE = 1;
const TASK_RETRY_ALWAYS = -1;
/**
* @var integer $maxDelay The max time (in seconds) to wait before a retry.
*/
private $maxDelay = 60;
/**
* @var integer $delay The previous delay from which the next is calculated.
*/
private $delay = 1;
/**
* @var integer $factor The base number for the exponential back off.
*/
private $factor = 2;
/**
* @var float $jitter A random number between -$jitter and $jitter will be
* added to $factor on each iteration to allow for a better distribution of
* retries.
*/
private $jitter = 0.5;
/**
* @var integer $attempts The number of attempts that have been tried so far.
*/
private $attempts = 0;
/**
* @var integer $maxAttempts The max number of attempts allowed.
*/
private $maxAttempts = 1;
/**
* @var callable $action The task to run and possibly retry.
*/
private $action;
/**
* @var array $arguments The task arguments.
*/
private $arguments;
/**
* @var array $retryMap Map of errors with retry counts.
*/
protected $retryMap = [
'500' => self::TASK_RETRY_ALWAYS,
'503' => self::TASK_RETRY_ALWAYS,
'rateLimitExceeded' => self::TASK_RETRY_ALWAYS,
'userRateLimitExceeded' => self::TASK_RETRY_ALWAYS,
6 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_RESOLVE_HOST
7 => self::TASK_RETRY_ALWAYS, // CURLE_COULDNT_CONNECT
28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED
35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR
52 => self::TASK_RETRY_ALWAYS, // CURLE_GOT_NOTHING
'lighthouseError' => self::TASK_RETRY_NEVER
];
/**
* Creates a new task runner with exponential backoff support.
*
* @param array $config The task runner config
* @param string $name The name of the current task (used for logging)
* @param callable $action The task to run and possibly retry
* @param array $arguments The task arguments
* @throws \Google\Task\Exception when misconfigured
*/
public function __construct(
$config,
$name,
$action,
array $arguments = array()
) {
if (isset($config['initial_delay'])) {
if ($config['initial_delay'] < 0) {
throw new GoogleTaskException(
'Task configuration `initial_delay` must not be negative.'
);
}
$this->delay = $config['initial_delay'];
}
if (isset($config['max_delay'])) {
if ($config['max_delay'] <= 0) {
throw new GoogleTaskException(
'Task configuration `max_delay` must be greater than 0.'
);
}
$this->maxDelay = $config['max_delay'];
}
if (isset($config['factor'])) {
if ($config['factor'] <= 0) {
throw new GoogleTaskException(
'Task configuration `factor` must be greater than 0.'
);
}
$this->factor = $config['factor'];
}
if (isset($config['jitter'])) {
if ($config['jitter'] <= 0) {
throw new GoogleTaskException(
'Task configuration `jitter` must be greater than 0.'
);
}
$this->jitter = $config['jitter'];
}
if (isset($config['retries'])) {
if ($config['retries'] < 0) {
throw new GoogleTaskException(
'Task configuration `retries` must not be negative.'
);
}
$this->maxAttempts += $config['retries'];
}
if (!is_callable($action)) {
throw new GoogleTaskException(
'Task argument `$action` must be a valid callable.'
);
}
$this->action = $action;
$this->arguments = $arguments;
}
/**
* Checks if a retry can be attempted.
*
* @return boolean
*/
public function canAttempt()
{
return $this->attempts < $this->maxAttempts;
}
/**
* Runs the task and (if applicable) automatically retries when errors occur.
*
* @return mixed
* @throws \Google\Service\Exception on failure when no retries are available.
*/
public function run()
{
while ($this->attempt()) {
try {
return call_user_func_array($this->action, $this->arguments);
} catch (GoogleServiceException $exception) {
$allowedRetries = $this->allowedRetries(
$exception->getCode(),
$exception->getErrors()
);
if (!$this->canAttempt() || !$allowedRetries) {
throw $exception;
}
if ($allowedRetries > 0) {
$this->maxAttempts = min(
$this->maxAttempts,
$this->attempts + $allowedRetries
);
}
}
}
}
/**
* Runs a task once, if possible. This is useful for bypassing the `run()`
* loop.
*
* NOTE: If this is not the first attempt, this function will sleep in
* accordance to the backoff configurations before running the task.
*
* @return boolean
*/
public function attempt()
{
if (!$this->canAttempt()) {
return false;
}
if ($this->attempts > 0) {
$this->backOff();
}
$this->attempts++;
return true;
}
/**
* Sleeps in accordance to the backoff configurations.
*/
private function backOff()
{
$delay = $this->getDelay();
usleep($delay * 1000000);
}
/**
* Gets the delay (in seconds) for the current backoff period.
*
* @return float
*/
private function getDelay()
{
$jitter = $this->getJitter();
$factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter);
return $this->delay = min($this->maxDelay, $this->delay * $factor);
}
/**
* Gets the current jitter (random number between -$this->jitter and
* $this->jitter).
*
* @return float
*/
private function getJitter()
{
return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter;
}
/**
* Gets the number of times the associated task can be retried.
*
* NOTE: -1 is returned if the task can be retried indefinitely
*
* @return integer
*/
public function allowedRetries($code, $errors = array())
{
if (isset($this->retryMap[$code])) {
return $this->retryMap[$code];
}
if (
!empty($errors) &&
isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']])
) {
return $this->retryMap[$errors[0]['reason']];
}
return 0;
}
public function setRetryMap($retryMap)
{
$this->retryMap = $retryMap;
}
}

View file

@ -0,0 +1,335 @@
<?php
/*
* Copyright 2013 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Utils;
/**
* Implementation of levels 1-3 of the URI Template spec.
* @see http://tools.ietf.org/html/rfc6570
*/
class UriTemplate
{
const TYPE_MAP = "1";
const TYPE_LIST = "2";
const TYPE_SCALAR = "4";
/**
* @var $operators array
* These are valid at the start of a template block to
* modify the way in which the variables inside are
* processed.
*/
private $operators = array(
"+" => "reserved",
"/" => "segments",
"." => "dotprefix",
"#" => "fragment",
";" => "semicolon",
"?" => "form",
"&" => "continuation"
);
/**
* @var reserved array
* These are the characters which should not be URL encoded in reserved
* strings.
*/
private $reserved = array(
"=", ",", "!", "@", "|", ":", "/", "?", "#",
"[", "]",'$', "&", "'", "(", ")", "*", "+", ";"
);
private $reservedEncoded = array(
"%3D", "%2C", "%21", "%40", "%7C", "%3A", "%2F", "%3F",
"%23", "%5B", "%5D", "%24", "%26", "%27", "%28", "%29",
"%2A", "%2B", "%3B"
);
public function parse($string, array $parameters)
{
return $this->resolveNextSection($string, $parameters);
}
/**
* This function finds the first matching {...} block and
* executes the replacement. It then calls itself to find
* subsequent blocks, if any.
*/
private function resolveNextSection($string, $parameters)
{
$start = strpos($string, "{");
if ($start === false) {
return $string;
}
$end = strpos($string, "}");
if ($end === false) {
return $string;
}
$string = $this->replace($string, $start, $end, $parameters);
return $this->resolveNextSection($string, $parameters);
}
private function replace($string, $start, $end, $parameters)
{
// We know a data block will have {} round it, so we can strip that.
$data = substr($string, $start + 1, $end - $start - 1);
// If the first character is one of the reserved operators, it effects
// the processing of the stream.
if (isset($this->operators[$data[0]])) {
$op = $this->operators[$data[0]];
$data = substr($data, 1);
$prefix = "";
$prefix_on_missing = false;
switch ($op) {
case "reserved":
// Reserved means certain characters should not be URL encoded
$data = $this->replaceVars($data, $parameters, ",", null, true);
break;
case "fragment":
// Comma separated with fragment prefix. Bare values only.
$prefix = "#";
$prefix_on_missing = true;
$data = $this->replaceVars($data, $parameters, ",", null, true);
break;
case "segments":
// Slash separated data. Bare values only.
$prefix = "/";
$data =$this->replaceVars($data, $parameters, "/");
break;
case "dotprefix":
// Dot separated data. Bare values only.
$prefix = ".";
$prefix_on_missing = true;
$data = $this->replaceVars($data, $parameters, ".");
break;
case "semicolon":
// Semicolon prefixed and separated. Uses the key name
$prefix = ";";
$data = $this->replaceVars($data, $parameters, ";", "=", false, true, false);
break;
case "form":
// Standard URL format. Uses the key name
$prefix = "?";
$data = $this->replaceVars($data, $parameters, "&", "=");
break;
case "continuation":
// Standard URL, but with leading ampersand. Uses key name.
$prefix = "&";
$data = $this->replaceVars($data, $parameters, "&", "=");
break;
}
// Add the initial prefix character if data is valid.
if ($data || ($data !== false && $prefix_on_missing)) {
$data = $prefix . $data;
}
} else {
// If no operator we replace with the defaults.
$data = $this->replaceVars($data, $parameters);
}
// This is chops out the {...} and replaces with the new section.
return substr($string, 0, $start) . $data . substr($string, $end + 1);
}
private function replaceVars(
$section,
$parameters,
$sep = ",",
$combine = null,
$reserved = false,
$tag_empty = false,
$combine_on_empty = true
) {
if (strpos($section, ",") === false) {
// If we only have a single value, we can immediately process.
return $this->combine(
$section,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
);
} else {
// If we have multiple values, we need to split and loop over them.
// Each is treated individually, then glued together with the
// separator character.
$vars = explode(",", $section);
return $this->combineList(
$vars,
$sep,
$parameters,
$combine,
$reserved,
false, // Never emit empty strings in multi-param replacements
$combine_on_empty
);
}
}
public function combine(
$key,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
) {
$length = false;
$explode = false;
$skip_final_combine = false;
$value = false;
// Check for length restriction.
if (strpos($key, ":") !== false) {
list($key, $length) = explode(":", $key);
}
// Check for explode parameter.
if ($key[strlen($key) - 1] == "*") {
$explode = true;
$key = substr($key, 0, -1);
$skip_final_combine = true;
}
// Define the list separator.
$list_sep = $explode ? $sep : ",";
if (isset($parameters[$key])) {
$data_type = $this->getDataType($parameters[$key]);
switch ($data_type) {
case self::TYPE_SCALAR:
$value = $this->getValue($parameters[$key], $length);
break;
case self::TYPE_LIST:
$values = array();
foreach ($parameters[$key] as $pkey => $pvalue) {
$pvalue = $this->getValue($pvalue, $length);
if ($combine && $explode) {
$values[$pkey] = $key . $combine . $pvalue;
} else {
$values[$pkey] = $pvalue;
}
}
$value = implode($list_sep, $values);
if ($value == '') {
return '';
}
break;
case self::TYPE_MAP:
$values = array();
foreach ($parameters[$key] as $pkey => $pvalue) {
$pvalue = $this->getValue($pvalue, $length);
if ($explode) {
$pkey = $this->getValue($pkey, $length);
$values[] = $pkey . "=" . $pvalue; // Explode triggers = combine.
} else {
$values[] = $pkey;
$values[] = $pvalue;
}
}
$value = implode($list_sep, $values);
if ($value == '') {
return false;
}
break;
}
} else if ($tag_empty) {
// If we are just indicating empty values with their key name, return that.
return $key;
} else {
// Otherwise we can skip this variable due to not being defined.
return false;
}
if ($reserved) {
$value = str_replace($this->reservedEncoded, $this->reserved, $value);
}
// If we do not need to include the key name, we just return the raw
// value.
if (!$combine || $skip_final_combine) {
return $value;
}
// Else we combine the key name: foo=bar, if value is not the empty string.
return $key . ($value != '' || $combine_on_empty ? $combine . $value : '');
}
/**
* Return the type of a passed in value
*/
private function getDataType($data)
{
if (is_array($data)) {
reset($data);
if (key($data) !== 0) {
return self::TYPE_MAP;
}
return self::TYPE_LIST;
}
return self::TYPE_SCALAR;
}
/**
* Utility function that merges multiple combine calls
* for multi-key templates.
*/
private function combineList(
$vars,
$sep,
$parameters,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
) {
$ret = array();
foreach ($vars as $var) {
$response = $this->combine(
$var,
$parameters,
$sep,
$combine,
$reserved,
$tag_empty,
$combine_on_empty
);
if ($response === false) {
continue;
}
$ret[] = $response;
}
return implode($sep, $ret);
}
/**
* Utility function to encode and trim values
*/
private function getValue($value, $length)
{
if ($length) {
$value = substr($value, 0, $length);
}
$value = rawurlencode($value);
return $value;
}
}

65
vendor/google/apiclient/src/aliases.php vendored Normal file
View file

@ -0,0 +1,65 @@
<?php
if (class_exists('Google_Client', false)) {
// Prevent error with preloading in PHP 7.4
// @see https://github.com/googleapis/google-api-php-client/issues/1976
return;
}
$classMap = [
'Google\\Client' => 'Google_Client',
'Google\\Service' => 'Google_Service',
'Google\\AccessToken\\Revoke' => 'Google_AccessToken_Revoke',
'Google\\AccessToken\\Verify' => 'Google_AccessToken_Verify',
'Google\\Model' => 'Google_Model',
'Google\\Utils\\UriTemplate' => 'Google_Utils_UriTemplate',
'Google\\AuthHandler\\Guzzle6AuthHandler' => 'Google_AuthHandler_Guzzle6AuthHandler',
'Google\\AuthHandler\\Guzzle7AuthHandler' => 'Google_AuthHandler_Guzzle7AuthHandler',
'Google\\AuthHandler\\Guzzle5AuthHandler' => 'Google_AuthHandler_Guzzle5AuthHandler',
'Google\\AuthHandler\\AuthHandlerFactory' => 'Google_AuthHandler_AuthHandlerFactory',
'Google\\Http\\Batch' => 'Google_Http_Batch',
'Google\\Http\\MediaFileUpload' => 'Google_Http_MediaFileUpload',
'Google\\Http\\REST' => 'Google_Http_REST',
'Google\\Task\\Retryable' => 'Google_Task_Retryable',
'Google\\Task\\Exception' => 'Google_Task_Exception',
'Google\\Task\\Runner' => 'Google_Task_Runner',
'Google\\Collection' => 'Google_Collection',
'Google\\Service\\Exception' => 'Google_Service_Exception',
'Google\\Service\\Resource' => 'Google_Service_Resource',
'Google\\Exception' => 'Google_Exception',
];
foreach ($classMap as $class => $alias) {
class_alias($class, $alias);
}
/**
* This class needs to be defined explicitly as scripts must be recognized by
* the autoloader.
*/
class Google_Task_Composer extends \Google\Task\Composer
{
}
if (\false) {
class Google_AccessToken_Revoke extends \Google\AccessToken\Revoke {}
class Google_AccessToken_Verify extends \Google\AccessToken\Verify {}
class Google_AuthHandler_AuthHandlerFactory extends \Google\AuthHandler\AuthHandlerFactory {}
class Google_AuthHandler_Guzzle5AuthHandler extends \Google\AuthHandler\Guzzle5AuthHandler {}
class Google_AuthHandler_Guzzle6AuthHandler extends \Google\AuthHandler\Guzzle6AuthHandler {}
class Google_AuthHandler_Guzzle7AuthHandler extends \Google\AuthHandler\Guzzle7AuthHandler {}
class Google_Client extends \Google\Client {}
class Google_Collection extends \Google\Collection {}
class Google_Exception extends \Google\Exception {}
class Google_Http_Batch extends \Google\Http\Batch {}
class Google_Http_MediaFileUpload extends \Google\Http\MediaFileUpload {}
class Google_Http_REST extends \Google\Http\REST {}
class Google_Model extends \Google\Model {}
class Google_Service extends \Google\Service {}
class Google_Service_Exception extends \Google\Service\Exception {}
class Google_Service_Resource extends \Google\Service\Resource {}
class Google_Task_Exception extends \Google\Task\Exception {}
interface Google_Task_Retryable extends \Google\Task\Retryable {}
class Google_Task_Runner extends \Google\Task\Runner {}
class Google_Utils_UriTemplate extends \Google\Utils\UriTemplate {}
}

View file

@ -0,0 +1,294 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests;
use Google\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DomCrawler\Crawler;
use League\Flysystem\Adapter\Local;
use League\Flysystem\Filesystem;
use Cache\Adapter\Filesystem\FilesystemCachePool;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) {
trait BaseTestTrait
{
use \Prophecy\PhpUnit\ProphecyTrait;
}
} else {
trait BaseTestTrait
{
}
}
class BaseTest extends TestCase
{
private $key;
private $client;
use BaseTestTrait;
public function getClient()
{
if (!$this->client) {
$this->client = $this->createClient();
}
return $this->client;
}
public function getCache($path = null)
{
$path = $path ?: sys_get_temp_dir().'/google-api-php-client-tests/';
$filesystemAdapter = new Local($path);
$filesystem = new Filesystem($filesystemAdapter);
return new FilesystemCachePool($filesystem);
}
private function createClient()
{
$options = [
'auth' => 'google_auth',
'exceptions' => false,
];
if ($proxy = getenv('HTTP_PROXY')) {
$options['proxy'] = $proxy;
$options['verify'] = false;
}
// adjust constructor depending on guzzle version
if ($this->isGuzzle5()) {
$options = ['defaults' => $options];
}
$httpClient = new GuzzleClient($options);
$client = new Client();
$client->setApplicationName('google-api-php-client-tests');
$client->setHttpClient($httpClient);
$client->setScopes(
[
"https://www.googleapis.com/auth/tasks",
"https://www.googleapis.com/auth/adsense",
"https://www.googleapis.com/auth/youtube",
"https://www.googleapis.com/auth/drive",
]
);
if ($this->key) {
$client->setDeveloperKey($this->key);
}
list($clientId, $clientSecret) = $this->getClientIdAndSecret();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
if (version_compare(PHP_VERSION, '5.5', '>=')) {
$client->setCache($this->getCache());
}
return $client;
}
public function checkToken()
{
$client = $this->getClient();
$cache = $client->getCache();
$cacheItem = $cache->getItem('access_token');
if (!$token = $cacheItem->get()) {
if (!$token = $this->tryToGetAnAccessToken($client)) {
return $this->markTestSkipped("Test requires access token");
}
$cacheItem->set($token);
$cache->save($cacheItem);
}
$client->setAccessToken($token);
if ($client->isAccessTokenExpired()) {
// as long as we have client credentials, even if its expired
// our access token will automatically be refreshed
$this->checkClientCredentials();
}
return true;
}
public function tryToGetAnAccessToken(Client $client)
{
$this->checkClientCredentials();
$client->setRedirectUri("urn:ietf:wg:oauth:2.0:oob");
$client->setConfig('access_type', 'offline');
$authUrl = $client->createAuthUrl();
echo "\nGo to: $authUrl\n";
echo "\nPlease enter the auth code:\n";
ob_flush();
`open '$authUrl'`;
$authCode = trim(fgets(STDIN));
if ($accessToken = $client->fetchAccessTokenWithAuthCode($authCode)) {
if (isset($accessToken['access_token'])) {
return $accessToken;
}
}
return false;
}
private function getClientIdAndSecret()
{
$clientId = getenv('GOOGLE_CLIENT_ID') ?: null;
$clientSecret = getenv('GOOGLE_CLIENT_SECRET') ?: null;
return array($clientId, $clientSecret);
}
protected function checkClientCredentials()
{
list($clientId, $clientSecret) = $this->getClientIdAndSecret();
if (!($clientId && $clientSecret)) {
$this->markTestSkipped("Test requires GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET to be set");
}
}
protected function checkServiceAccountCredentials()
{
if (!$f = getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
$skip = "This test requires the GOOGLE_APPLICATION_CREDENTIALS environment variable to be set\n"
. "see https://developers.google.com/accounts/docs/application-default-credentials";
$this->markTestSkipped($skip);
return false;
}
if (!file_exists($f)) {
$this->markTestSkipped('invalid path for GOOGLE_APPLICATION_CREDENTIALS');
}
return true;
}
protected function checkKey()
{
if (file_exists($apiKeyFile = __DIR__ . DIRECTORY_SEPARATOR . '.apiKey')) {
$apiKey = file_get_contents($apiKeyFile);
} elseif (!$apiKey = getenv('GOOGLE_API_KEY')) {
$this->markTestSkipped(
"Test requires api key\nYou can create one in your developer console"
);
file_put_contents($apiKeyFile, $apiKey);
}
$this->key = $apiKey;
}
protected function loadExample($example)
{
// trick app into thinking we are a web server
$_SERVER['HTTP_USER_AGENT'] = 'google-api-php-client-tests';
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['REQUEST_METHOD'] = 'GET';
// include the file and return an HTML crawler
$file = __DIR__ . '/../examples/' . $example;
if (is_file($file)) {
ob_start();
include $file;
$html = ob_get_clean();
return new Crawler($html);
}
return false;
}
protected function isGuzzle7()
{
if (!defined('\GuzzleHttp\ClientInterface::MAJOR_VERSION')) {
return false;
}
return (7 === ClientInterface::MAJOR_VERSION);
}
protected function isGuzzle6()
{
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
return false;
}
$version = ClientInterface::VERSION;
return ('6' === $version[0]);
}
protected function isGuzzle5()
{
if (!defined('\GuzzleHttp\ClientInterface::VERSION')) {
return false;
}
$version = ClientInterface::VERSION;
return ('5' === $version[0]);
}
public function onlyGuzzle6()
{
if (!$this->isGuzzle6()) {
$this->markTestSkipped('Guzzle 6 only');
}
}
public function onlyPhp55AndAbove()
{
if (version_compare(PHP_VERSION, '5.5', '<')) {
$this->markTestSkipped('PHP 5.5 and above only');
}
}
public function onlyGuzzle5()
{
if (!$this->isGuzzle5()) {
$this->markTestSkipped('Guzzle 5 only');
}
}
public function onlyGuzzle6Or7()
{
if (!$this->isGuzzle6() && !$this->isGuzzle7()) {
$this->markTestSkipped('Guzzle 6 or 7 only');
}
}
protected function getGuzzle5ResponseMock()
{
$response = $this->prophesize('GuzzleHttp\Message\ResponseInterface');
$response->getStatusCode()
->willReturn(200);
$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn('');
$response->getProtocolVersion()->willReturn('');
$response->getReasonPhrase()->willReturn('');
return $response;
}
}

View file

@ -0,0 +1,159 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\AccessToken;
use Google\AccessToken\Revoke;
use Google\Tests\BaseTest;
use Prophecy\Argument;
class RevokeTest extends BaseTest
{
public function testRevokeAccessGuzzle5()
{
$this->onlyGuzzle5();
$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';
$response = $this->prophesize('GuzzleHttp\Message\ResponseInterface');
$response->getStatusCode()
->shouldBeCalledTimes(3)
->willReturn(200);
$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn('');
$response->getProtocolVersion()->willReturn('');
$response->getReasonPhrase()->willReturn('');
$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('GuzzleHttp\Message\RequestInterface'))
->shouldBeCalledTimes(3)
->will(function ($args) use (&$token, $response) {
$request = $args[0];
parse_str((string) $request->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;
return $response->reveal();
});
$requestToken = null;
$request = $this->prophesize('GuzzleHttp\Message\RequestInterface');
$request->getBody()
->shouldBeCalledTimes(3)
->will(function () use (&$requestToken) {
return 'token='.$requestToken;
});
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->shouldBeCalledTimes(3)
->will(function ($args) use (&$requestToken, $request) {
$params = $args[2];
parse_str((string) $params['body'], $fields);
$requestToken = isset($fields['token']) ? $fields['token'] : null;
return $request;
});
$t = [
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
];
// Test with access token.
$revoke = new Revoke($http->reveal());
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
// Test with refresh token.
$revoke = new Revoke($http->reveal());
$t = [
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
];
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);
// Test with token string.
$revoke = new Revoke($http->reveal());
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}
public function testRevokeAccessGuzzle6Or7()
{
$this->onlyGuzzle6Or7();
$accessToken = 'ACCESS_TOKEN';
$refreshToken = 'REFRESH_TOKEN';
$token = '';
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$response->getStatusCode()
->shouldBeCalledTimes(3)
->willReturn(200);
$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(3)
->will(function ($args) use (&$token, $response) {
parse_str((string) $args[0]->getBody(), $fields);
$token = isset($fields['token']) ? $fields['token'] : null;
return $response->reveal();
});
$t = [
'access_token' => $accessToken,
'created' => time(),
'expires_in' => '3600'
];
// Test with access token.
$revoke = new Revoke($http->reveal());
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
// Test with refresh token.
$revoke = new Revoke($http->reveal());
$t = [
'access_token' => $accessToken,
'refresh_token' => $refreshToken,
'created' => time(),
'expires_in' => '3600'
];
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($refreshToken, $token);
// Test with token string.
$revoke = new Revoke($http->reveal());
$t = $accessToken;
$this->assertTrue($revoke->revokeToken($t));
$this->assertEquals($accessToken, $token);
}
}

View file

@ -0,0 +1,160 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\AccessToken;
use Google\AccessToken\Verify;
use Google\Tests\BaseTest;
use ReflectionMethod;
class VerifyTest extends BaseTest
{
/**
* This test needs to run before the other verify tests,
* to ensure the constants are not defined.
*/
public function testPhpsecConstants()
{
$client = $this->getClient();
$verify = new Verify($client->getHttpClient());
// set these to values that will be changed
if (defined('MATH_BIGINTEGER_OPENSSL_ENABLED') || defined('CRYPT_RSA_MODE')) {
$this->markTestSkipped('Cannot run test - constants already defined');
}
// Pretend we are on App Engine VMs
putenv('GAE_VM=1');
$verify->verifyIdToken('a.b.c');
putenv('GAE_VM=0');
$openSslEnable = constant('MATH_BIGINTEGER_OPENSSL_ENABLED');
$rsaMode = constant('CRYPT_RSA_MODE');
$this->assertTrue($openSslEnable);
$this->assertEquals(constant($this->getOpenSslConstant()), $rsaMode);
}
/**
* Most of the logic for ID token validation is in AuthTest -
* this is just a general check to ensure we verify a valid
* id token if one exists.
*/
public function testValidateIdToken()
{
$this->checkToken();
$jwt = $this->getJwtService();
$client = $this->getClient();
$http = $client->getHttpClient();
$token = $client->getAccessToken();
if ($client->isAccessTokenExpired()) {
$token = $client->fetchAccessTokenWithRefreshToken();
}
$segments = explode('.', $token['id_token']);
$this->assertCount(3, $segments);
// Extract the client ID in this case as it wont be set on the test client.
$data = json_decode($jwt->urlSafeB64Decode($segments[1]));
$verify = new Verify($http);
$payload = $verify->verifyIdToken($token['id_token'], $data->aud);
$this->assertArrayHasKey('sub', $payload);
$this->assertGreaterThan(0, strlen($payload['sub']));
// TODO: Need to be smart about testing/disabling the
// caching for this test to make sense. Not sure how to do that
// at the moment.
$client = $this->getClient();
$http = $client->getHttpClient();
$data = json_decode($jwt->urlSafeB64Decode($segments[1]));
$verify = new Verify($http);
$payload = $verify->verifyIdToken($token['id_token'], $data->aud);
$this->assertArrayHasKey('sub', $payload);
$this->assertGreaterThan(0, strlen($payload['sub']));
}
/**
* Most of the logic for ID token validation is in AuthTest -
* this is just a general check to ensure we verify a valid
* id token if one exists.
*/
public function testLeewayIsUnchangedWhenPassingInJwt()
{
$this->checkToken();
$jwt = $this->getJwtService();
// set arbitrary leeway so we can check this later
$jwt::$leeway = $leeway = 1.5;
$client = $this->getClient();
$token = $client->getAccessToken();
if ($client->isAccessTokenExpired()) {
$token = $client->fetchAccessTokenWithRefreshToken();
}
$segments = explode('.', $token['id_token']);
$this->assertCount(3, $segments);
// Extract the client ID in this case as it wont be set on the test client.
$data = json_decode($jwt->urlSafeB64Decode($segments[1]));
$verify = new Verify($client->getHttpClient(), null, $jwt);
$payload = $verify->verifyIdToken($token['id_token'], $data->aud);
// verify the leeway is set as it was
$this->assertEquals($leeway, $jwt::$leeway);
}
public function testRetrieveCertsFromLocation()
{
$client = $this->getClient();
$verify = new Verify($client->getHttpClient());
// make this method public for testing purposes
$method = new ReflectionMethod($verify, 'retrieveCertsFromLocation');
$method->setAccessible(true);
$certs = $method->invoke($verify, Verify::FEDERATED_SIGNON_CERT_URL);
$this->assertArrayHasKey('keys', $certs);
$this->assertGreaterThan(1, count($certs['keys']));
$this->assertArrayHasKey('alg', $certs['keys'][0]);
$this->assertEquals('RS256', $certs['keys'][0]['alg']);
}
private function getJwtService()
{
if (class_exists('\Firebase\JWT\JWT')) {
return new \Firebase\JWT\JWT;
}
return new \JWT;
}
private function getOpenSslConstant()
{
if (class_exists('phpseclib3\Crypt\AES')) {
return 'phpseclib3\Crypt\AES::ENGINE_OPENSSL';
}
if (class_exists('phpseclib\Crypt\RSA')) {
return 'phpseclib\Crypt\RSA::MODE_OPENSSL';
}
if (class_exists('Crypt_RSA')) {
return 'CRYPT_RSA_MODE_OPENSSL';
}
}
}

View file

@ -0,0 +1,103 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests;
use Google\Client;
use Google\Auth\Cache\MemoryCacheItemPool;
use Google\Service\Drive;
use DateTime;
class CacheTest extends BaseTest
{
public function testInMemoryCache()
{
$this->checkServiceAccountCredentials();
$client = $this->getClient();
$client->useApplicationDefaultCredentials();
$client->setAccessType('offline');
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
$client->setCache(new MemoryCacheItemPool);
/* Refresh token when expired */
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
/* Make a service call */
$service = new Drive($client);
$files = $service->files->listFiles();
$this->assertInstanceOf('Google_Service_Drive_FileList', $files);
}
public function testFileCache()
{
$this->onlyPhp55AndAbove();
$this->checkServiceAccountCredentials();
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
// filecache with new cache dir
$cache = $this->getCache(sys_get_temp_dir() . '/cloud-samples-tests-php-cache-test/');
$client->setCache($cache);
$token1 = null;
$client->setTokenCallback(function($cacheKey, $accessToken) use ($cache, &$token1) {
$token1 = $accessToken;
$cacheItem = $cache->getItem($cacheKey);
// expire the item
$cacheItem->expiresAt(new DateTime('now -1 second'));
$cache->save($cacheItem);
$cacheItem2 = $cache->getItem($cacheKey);
});
/* Refresh token when expired */
if ($client->isAccessTokenExpired()) {
$client->refreshTokenWithAssertion();
}
/* Make a service call */
$service = new Drive($client);
$files = $service->files->listFiles();
$this->assertInstanceOf(Drive\FileList::class, $files);
sleep(2);
// make sure the token expires
$client = new Client();
$client->useApplicationDefaultCredentials();
$client->setScopes(['https://www.googleapis.com/auth/drive.readonly']);
$client->setCache($cache);
$token2 = null;
$client->setTokenCallback(function($cacheKey, $accessToken) use (&$token2) {
$token2 = $accessToken;
});
/* Make another service call */
$service = new Drive($client);
$files = $service->files->listFiles();
$this->assertInstanceOf(Drive\FileList::class, $files);
$this->assertNotEquals($token1, $token2);
}
}

View file

@ -0,0 +1,955 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests;
use Google\Client;
use Google\Service\Drive;
use Google\AuthHandler\AuthHandlerFactory;
use Google\Auth\FetchAuthTokenCache;
use Google\Auth\GCECache;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\ClientException;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use ReflectionClass;
use ReflectionMethod;
use InvalidArgumentException;
use Exception;
class ClientTest extends BaseTest
{
public function testClientConstructor()
{
$this->assertInstanceOf(Client::class, $this->getClient());
}
public function testSignAppKey()
{
$client = $this->getClient();
$client->setDeveloperKey('devKey');
$http = new GuzzleClient();
$client->authorize($http);
$this->checkAuthHandler($http, 'Simple');
}
private function checkAuthHandler($http, $className)
{
if ($this->isGuzzle6() || $this->isGuzzle7()) {
$stack = $http->getConfig('handler');
$class = new ReflectionClass(get_class($stack));
$property = $class->getProperty('stack');
$property->setAccessible(true);
$middlewares = $property->getValue($stack);
$middleware = array_pop($middlewares);
if (null === $className) {
// only the default middlewares have been added
$this->assertCount(3, $middlewares);
} else {
$authClass = sprintf('Google\Auth\Middleware\%sMiddleware', $className);
$this->assertInstanceOf($authClass, $middleware[0]);
}
} else {
$listeners = $http->getEmitter()->listeners('before');
if (null === $className) {
$this->assertCount(0, $listeners);
} else {
$authClass = sprintf('Google\Auth\Subscriber\%sSubscriber', $className);
$this->assertCount(1, $listeners);
$this->assertCount(2, $listeners[0]);
$this->assertInstanceOf($authClass, $listeners[0][0]);
}
}
}
private function checkCredentials($http, $fetcherClass, $sub = null)
{
if ($this->isGuzzle6() || $this->isGuzzle7()) {
$stack = $http->getConfig('handler');
$class = new ReflectionClass(get_class($stack));
$property = $class->getProperty('stack');
$property->setAccessible(true);
$middlewares = $property->getValue($stack); // Works
$middleware = array_pop($middlewares);
$auth = $middleware[0];
} else {
// access the protected $fetcher property
$listeners = $http->getEmitter()->listeners('before');
$auth = $listeners[0][0];
}
$class = new ReflectionClass(get_class($auth));
$property = $class->getProperty('fetcher');
$property->setAccessible(true);
$cacheFetcher = $property->getValue($auth);
$this->assertInstanceOf(FetchAuthTokenCache::class, $cacheFetcher);
$class = new ReflectionClass(get_class($cacheFetcher));
$property = $class->getProperty('fetcher');
$property->setAccessible(true);
$fetcher = $property->getValue($cacheFetcher);
$this->assertInstanceOf($fetcherClass, $fetcher);
if ($sub) {
// access the protected $auth property
$class = new ReflectionClass(get_class($fetcher));
$property = $class->getProperty('auth');
$property->setAccessible(true);
$auth = $property->getValue($fetcher);
$this->assertEquals($sub, $auth->getSub());
}
}
public function testSignAccessToken()
{
$client = $this->getClient();
$http = new GuzzleClient();
$client->setAccessToken([
'access_token' => 'test_token',
'expires_in' => 3600,
'created' => time(),
]);
$client->setScopes('test_scope');
$client->authorize($http);
$this->checkAuthHandler($http, 'ScopedAccessToken');
}
public function testCreateAuthUrl()
{
$client = $this->getClient();
$client->setClientId('clientId1');
$client->setClientSecret('clientSecret1');
$client->setRedirectUri('http://localhost');
$client->setDeveloperKey('devKey');
$client->setState('xyz');
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setRequestVisibleActions('http://foo');
$client->setLoginHint('bob@example.org');
$authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo");
$expected = "https://accounts.google.com/o/oauth2/auth"
. "?response_type=code"
. "&access_type=offline"
. "&client_id=clientId1"
. "&redirect_uri=http%3A%2F%2Flocalhost"
. "&state=xyz"
. "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo"
. "&approval_prompt=force"
. "&login_hint=bob%40example.org";
$this->assertEquals($expected, $authUrl);
// Again with a blank login hint (should remove all traces from authUrl)
$client->setLoginHint('');
$client->setHostedDomain('example.com');
$client->setOpenIdRealm('example.com');
$client->setPrompt('select_account');
$client->setIncludeGrantedScopes(true);
$authUrl = $client->createAuthUrl("http://googleapis.com/scope/foo");
$expected = "https://accounts.google.com/o/oauth2/auth"
. "?response_type=code"
. "&access_type=offline"
. "&client_id=clientId1"
. "&redirect_uri=http%3A%2F%2Flocalhost"
. "&state=xyz"
. "&scope=http%3A%2F%2Fgoogleapis.com%2Fscope%2Ffoo"
. "&hd=example.com"
. "&include_granted_scopes=true"
. "&openid.realm=example.com"
. "&prompt=select_account";
$this->assertEquals($expected, $authUrl);
}
public function testPrepareNoScopes()
{
$client = new Client();
$scopes = $client->prepareScopes();
$this->assertNull($scopes);
}
public function testNoAuthIsNull()
{
$client = new Client();
$this->assertNull($client->getAccessToken());
}
public function testPrepareService()
{
$this->onlyGuzzle6Or7();
$client = new Client();
$client->setScopes(array("scope1", "scope2"));
$scopes = $client->prepareScopes();
$this->assertEquals("scope1 scope2", $scopes);
$client->setScopes(array("", "scope2"));
$scopes = $client->prepareScopes();
$this->assertEquals(" scope2", $scopes);
$client->setScopes("scope2");
$client->addScope("scope3");
$client->addScope(array("scope4", "scope5"));
$scopes = $client->prepareScopes();
$this->assertEquals("scope2 scope3 scope4 scope5", $scopes);
$client->setClientId('test1');
$client->setRedirectUri('http://localhost/');
$client->setState('xyz');
$client->setScopes(array("http://test.com", "scope2"));
$scopes = $client->prepareScopes();
$this->assertEquals("http://test.com scope2", $scopes);
$this->assertEquals(
''
. 'https://accounts.google.com/o/oauth2/auth'
. '?response_type=code'
. '&access_type=online'
. '&client_id=test1'
. '&redirect_uri=http%3A%2F%2Flocalhost%2F'
. '&state=xyz'
. '&scope=http%3A%2F%2Ftest.com%20scope2'
. '&approval_prompt=auto',
$client->createAuthUrl()
);
$stream = $this->prophesize('GuzzleHttp\Psr7\Stream');
$stream->__toString()->willReturn('');
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
$response->getBody()
->shouldBeCalledTimes(1)
->willReturn($stream->reveal());
$response->getStatusCode()->willReturn(200);
$http = $this->prophesize('GuzzleHttp\ClientInterface');
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
$client->setHttpClient($http->reveal());
$dr_service = new Drive($client);
$this->assertInstanceOf('Google\Model', $dr_service->files->listFiles());
}
public function testDefaultLogger()
{
$client = new Client();
$logger = $client->getLogger();
$this->assertInstanceOf('Monolog\Logger', $logger);
$handler = $logger->popHandler();
$this->assertInstanceOf('Monolog\Handler\StreamHandler', $handler);
}
public function testDefaultLoggerAppEngine()
{
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
$client = new Client();
$logger = $client->getLogger();
$handler = $logger->popHandler();
unset($_SERVER['SERVER_SOFTWARE']);
$this->assertInstanceOf('Monolog\Logger', $logger);
$this->assertInstanceOf('Monolog\Handler\SyslogHandler', $handler);
}
public function testSettersGetters()
{
$client = new Client();
$client->setClientId("client1");
$client->setClientSecret('client1secret');
$client->setState('1');
$client->setApprovalPrompt('force');
$client->setAccessType('offline');
$client->setRedirectUri('localhost');
$client->setConfig('application_name', 'me');
$cache = $this->prophesize(CacheItemPoolInterface::class);
$client->setCache($cache->reveal());
$this->assertInstanceOf(CacheItemPoolInterface::class, $client->getCache());
try {
$client->setAccessToken(null);
$this->fail('Should have thrown an Exception.');
} catch (InvalidArgumentException $e) {
$this->assertEquals('invalid json token', $e->getMessage());
}
$token = array('access_token' => 'token');
$client->setAccessToken($token);
$this->assertEquals($token, $client->getAccessToken());
}
public function testDefaultConfigOptions()
{
$client = new Client();
if ($this->isGuzzle6() || $this->isGuzzle7()) {
$this->assertArrayHasKey('http_errors', $client->getHttpClient()->getConfig());
$this->assertArrayNotHasKey('exceptions', $client->getHttpClient()->getConfig());
$this->assertFalse($client->getHttpClient()->getConfig()['http_errors']);
}
if ($this->isGuzzle5()) {
$this->assertArrayHasKey('exceptions', $client->getHttpClient()->getDefaultOption());
$this->assertArrayNotHasKey('http_errors', $client->getHttpClient()->getDefaultOption());
$this->assertFalse($client->getHttpClient()->getDefaultOption()['exceptions']);
}
}
public function testAppEngineStreamHandlerConfig()
{
$this->onlyGuzzle5();
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
$client = new Client();
// check Stream Handler is used
$http = $client->getHttpClient();
$class = new ReflectionClass(get_class($http));
$property = $class->getProperty('fsm');
$property->setAccessible(true);
$fsm = $property->getValue($http);
$class = new ReflectionClass(get_class($fsm));
$property = $class->getProperty('handler');
$property->setAccessible(true);
$handler = $property->getValue($fsm);
$this->assertInstanceOf('GuzzleHttp\Ring\Client\StreamHandler', $handler);
unset($_SERVER['SERVER_SOFTWARE']);
}
public function testAppEngineVerifyConfig()
{
$this->onlyGuzzle5();
$_SERVER['SERVER_SOFTWARE'] = 'Google App Engine';
$client = new Client();
$this->assertEquals(
'/etc/ca-certificates.crt',
$client->getHttpClient()->getDefaultOption('verify')
);
unset($_SERVER['SERVER_SOFTWARE']);
}
public function testJsonConfig()
{
// Device config
$client = new Client();
$device =
'{"installed":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"'.
':"N0aHCBT1qX1VAcF5J1pJAn6S","token_uri":"https://oauth2.googleapis.com/token",'.
'"client_email":"","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],"client_x509_cert_url"'.
':"","client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'.
'"https://www.googleapis.com/oauth2/v1/certs"}}';
$dObj = json_decode($device, true);
$client->setAuthConfig($dObj);
$this->assertEquals($client->getClientId(), $dObj['installed']['client_id']);
$this->assertEquals($client->getClientSecret(), $dObj['installed']['client_secret']);
$this->assertEquals($client->getRedirectUri(), $dObj['installed']['redirect_uris'][0]);
// Web config
$client = new Client();
$web = '{"web":{"auth_uri":"https://accounts.google.com/o/oauth2/auth","client_secret"' .
':"lpoubuib8bj-Fmke_YhhyHGgXc","token_uri":"https://oauth2.googleapis.com/token"' .
',"client_email":"123456789@developer.gserviceaccount.com","client_x509_cert_url":'.
'"https://www.googleapis.com/robot/v1/metadata/x509/123456789@developer.gserviceaccount.com"'.
',"client_id":"123456789.apps.googleusercontent.com","auth_provider_x509_cert_url":'.
'"https://www.googleapis.com/oauth2/v1/certs"}}';
$wObj = json_decode($web, true);
$client->setAuthConfig($wObj);
$this->assertEquals($client->getClientId(), $wObj['web']['client_id']);
$this->assertEquals($client->getClientSecret(), $wObj['web']['client_secret']);
$this->assertEquals($client->getRedirectUri(), '');
}
public function testIniConfig()
{
$config = parse_ini_file(__DIR__ . '/../config/test.ini');
$client = new Client($config);
$this->assertEquals('My Test application', $client->getConfig('application_name'));
$this->assertEquals(
'gjfiwnGinpena3',
$client->getClientSecret()
);
}
public function testNoAuth()
{
/** @var $noAuth Google_Auth_Simple */
$client = new Client();
$client->setDeveloperKey(null);
// unset application credentials
$GOOGLE_APPLICATION_CREDENTIALS = getenv('GOOGLE_APPLICATION_CREDENTIALS');
$HOME = getenv('HOME');
putenv('GOOGLE_APPLICATION_CREDENTIALS=');
putenv('HOME='.sys_get_temp_dir());
$http = new GuzzleClient();
$client->authorize($http);
putenv("GOOGLE_APPLICATION_CREDENTIALS=$GOOGLE_APPLICATION_CREDENTIALS");
putenv("HOME=$HOME");
$this->checkAuthHandler($http, null);
}
public function testApplicationDefaultCredentials()
{
$this->checkServiceAccountCredentials();
$credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS');
$client = new Client();
$client->setAuthConfig($credentialsFile);
$http = new GuzzleClient();
$client->authorize($http);
$this->checkAuthHandler($http, 'AuthToken');
$this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials');
}
public function testApplicationDefaultCredentialsWithSubject()
{
$this->checkServiceAccountCredentials();
$credentialsFile = getenv('GOOGLE_APPLICATION_CREDENTIALS');
$sub = 'sub123';
$client = new Client();
$client->setAuthConfig($credentialsFile);
$client->setSubject($sub);
$http = new GuzzleClient();
$client->authorize($http);
$this->checkAuthHandler($http, 'AuthToken');
$this->checkCredentials($http, 'Google\Auth\Credentials\ServiceAccountCredentials', $sub);
}
/**
* Test that the ID token is properly refreshed.
*/
public function testRefreshTokenSetsValues()
{
$token = json_encode([
'access_token' => 'xyz',
'id_token' => 'ID_TOKEN',
]);
$postBody = $this->prophesize('GuzzleHttp\Psr7\Stream');
$postBody->__toString()
->shouldBeCalledTimes(1)
->willReturn($token);
if ($this->isGuzzle5()) {
$response = $this->getGuzzle5ResponseMock();
$response->getStatusCode()
->shouldBeCalledTimes(1)
->willReturn(200);
} else {
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
}
$response->getBody()
->shouldBeCalledTimes(1)
->willReturn($postBody->reveal());
$response->hasHeader('Content-Type')->willReturn(false);
$http = $this->prophesize('GuzzleHttp\ClientInterface');
if ($this->isGuzzle5()) {
$guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->shouldBeCalledTimes(1)
->willReturn($guzzle5Request);
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
} else {
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
}
$client = $this->getClient();
$client->setHttpClient($http->reveal());
$client->fetchAccessTokenWithRefreshToken("REFRESH_TOKEN");
$token = $client->getAccessToken();
$this->assertEquals("ID_TOKEN", $token['id_token']);
}
/**
* Test that the Refresh Token is set when refreshed.
*/
public function testRefreshTokenIsSetOnRefresh()
{
$refreshToken = 'REFRESH_TOKEN';
$token = json_encode(array(
'access_token' => 'xyz',
'id_token' => 'ID_TOKEN',
));
$postBody = $this->prophesize('Psr\Http\Message\StreamInterface');
$postBody->__toString()
->shouldBeCalledTimes(1)
->willReturn($token);
if ($this->isGuzzle5()) {
$response = $this->getGuzzle5ResponseMock();
$response->getStatusCode()
->shouldBeCalledTimes(1)
->willReturn(200);
} else {
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
}
$response->getBody()
->shouldBeCalledTimes(1)
->willReturn($postBody->reveal());
$response->hasHeader('Content-Type')->willReturn(false);
$http = $this->prophesize('GuzzleHttp\ClientInterface');
if ($this->isGuzzle5()) {
$guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn($guzzle5Request);
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
} else {
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
}
$client = $this->getClient();
$client->setHttpClient($http->reveal());
$client->fetchAccessTokenWithRefreshToken($refreshToken);
$token = $client->getAccessToken();
$this->assertEquals($refreshToken, $token['refresh_token']);
}
/**
* Test that the Refresh Token is not set when a new refresh token is returned.
*/
public function testRefreshTokenIsNotSetWhenNewRefreshTokenIsReturned()
{
$refreshToken = 'REFRESH_TOKEN';
$token = json_encode(array(
'access_token' => 'xyz',
'id_token' => 'ID_TOKEN',
'refresh_token' => 'NEW_REFRESH_TOKEN'
));
$postBody = $this->prophesize('GuzzleHttp\Psr7\Stream');
$postBody->__toString()
->wilLReturn($token);
if ($this->isGuzzle5()) {
$response = $this->getGuzzle5ResponseMock();
$response->getStatusCode()
->willReturn(200);
} else {
$response = $this->prophesize('Psr\Http\Message\ResponseInterface');
}
$response->getBody()
->willReturn($postBody->reveal());
$response->hasHeader('Content-Type')->willReturn(false);
$http = $this->prophesize('GuzzleHttp\ClientInterface');
if ($this->isGuzzle5()) {
$guzzle5Request = new \GuzzleHttp\Message\Request('POST', '/', ['body' => $token]);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn($guzzle5Request);
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->willReturn($response->reveal());
} else {
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response->reveal());
}
$client = $this->getClient();
$client->setHttpClient($http->reveal());
$client->fetchAccessTokenWithRefreshToken($refreshToken);
$token = $client->getAccessToken();
$this->assertEquals('NEW_REFRESH_TOKEN', $token['refresh_token']);
}
/**
* Test fetching an access token with assertion credentials
* using "useApplicationDefaultCredentials"
*/
public function testFetchAccessTokenWithAssertionFromEnv()
{
$this->checkServiceAccountCredentials();
$client = $this->getClient();
$client->useApplicationDefaultCredentials();
$token = $client->fetchAccessTokenWithAssertion();
$this->assertNotNull($token);
$this->assertArrayHasKey('access_token', $token);
}
/**
* Test fetching an access token with assertion credentials
* using "setAuthConfig"
*/
public function testFetchAccessTokenWithAssertionFromFile()
{
$this->checkServiceAccountCredentials();
$client = $this->getClient();
$client->setAuthConfig(getenv('GOOGLE_APPLICATION_CREDENTIALS'));
$token = $client->fetchAccessTokenWithAssertion();
$this->assertNotNull($token);
$this->assertArrayHasKey('access_token', $token);
}
/**
* Test fetching an access token with assertion credentials
* populates the "created" field
*/
public function testFetchAccessTokenWithAssertionAddsCreated()
{
$this->checkServiceAccountCredentials();
$client = $this->getClient();
$client->useApplicationDefaultCredentials();
$token = $client->fetchAccessTokenWithAssertion();
$this->assertNotNull($token);
$this->assertArrayHasKey('created', $token);
}
/**
* Test fetching an access token with assertion credentials
* using "setAuthConfig" and "setSubject" but with user credentials
*/
public function testBadSubjectThrowsException()
{
$this->checkServiceAccountCredentials();
$client = $this->getClient();
$client->useApplicationDefaultCredentials();
$client->setSubject('bad-subject');
$authHandler = AuthHandlerFactory::build();
// make this method public for testing purposes
$method = new ReflectionMethod($authHandler, 'createAuthHttp');
$method->setAccessible(true);
$authHttp = $method->invoke($authHandler, $client->getHttpClient());
try {
$token = $client->fetchAccessTokenWithAssertion($authHttp);
$this->fail('no exception thrown');
} catch (ClientException $e) {
$response = $e->getResponse();
$this->assertContains('Invalid impersonation', (string) $response->getBody());
}
}
public function testTokenCallback()
{
$this->onlyPhp55AndAbove();
$this->checkToken();
$client = $this->getClient();
$accessToken = $client->getAccessToken();
if (!isset($accessToken['refresh_token'])) {
$this->markTestSkipped('Refresh Token required');
}
// make the auth library think the token is expired
$accessToken['expires_in'] = 0;
$cache = $client->getCache();
$path = sys_get_temp_dir().'/google-api-php-client-tests-'.time();
$client->setCache($this->getCache($path));
$client->setAccessToken($accessToken);
// create the callback function
$phpunit = $this;
$called = false;
$callback = function ($key, $value) use ($client, $cache, $phpunit, &$called) {
// assert the expected keys and values
$phpunit->assertNotNull($key);
$phpunit->assertNotNull($value);
$called = true;
// go back to the previous cache
$client->setCache($cache);
};
// set the token callback to the client
$client->setTokenCallback($callback);
// make a silly request to obtain a new token (it's ok if it fails)
$http = $client->authorize();
try {
$http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire');
} catch (Exception $e) {}
$newToken = $client->getAccessToken();
// go back to the previous cache
// (in case callback wasn't called)
$client->setCache($cache);
$this->assertTrue($called);
}
public function testDefaultTokenCallback()
{
$this->onlyPhp55AndAbove();
$this->checkToken();
$client = $this->getClient();
$accessToken = $client->getAccessToken();
if (!isset($accessToken['refresh_token'])) {
$this->markTestSkipped('Refresh Token required');
}
// make the auth library think the token is expired
$accessToken['expires_in'] = 0;
$client->setAccessToken($accessToken);
// make a silly request to obtain a new token (it's ok if it fails)
$http = $client->authorize();
try {
$http->get('https://www.googleapis.com/books/v1/volumes?q=Voltaire');
} catch (Exception $e) {}
// Assert the in-memory token has been updated
$newToken = $client->getAccessToken();
$this->assertNotEquals(
$accessToken['access_token'],
$newToken['access_token']
);
$this->assertFalse($client->isAccessTokenExpired());
}
/** @runInSeparateProcess */
public function testOnGceCacheAndCacheOptions()
{
if (!class_exists(GCECache::class)) {
$this->markTestSkipped('Requires google/auth >= 1.12');
}
putenv('HOME=');
putenv('GOOGLE_APPLICATION_CREDENTIALS=');
$prefix = 'test_prefix_';
$cacheConfig = ['gce_prefix' => $prefix];
$mockCacheItem = $this->prophesize(CacheItemInterface::class);
$mockCacheItem->isHit()
->willReturn(true);
$mockCacheItem->get()
->shouldBeCalledTimes(1)
->willReturn(true);
$mockCache = $this->prophesize(CacheItemPoolInterface::class);
$mockCache->getItem($prefix . GCECache::GCE_CACHE_KEY)
->shouldBeCalledTimes(1)
->willReturn($mockCacheItem->reveal());
$client = new Client(['cache_config' => $cacheConfig]);
$client->setCache($mockCache->reveal());
$client->useApplicationDefaultCredentials();
$client->authorize();
}
/** @runInSeparateProcess */
public function testFetchAccessTokenWithAssertionCache()
{
$this->checkServiceAccountCredentials();
$cachedValue = ['access_token' => '2/abcdef1234567890'];
$mockCacheItem = $this->prophesize(CacheItemInterface::class);
$mockCacheItem->isHit()
->shouldBeCalledTimes(1)
->willReturn(true);
$mockCacheItem->get()
->shouldBeCalledTimes(1)
->willReturn($cachedValue);
$mockCache = $this->prophesize(CacheItemPoolInterface::class);
$mockCache->getItem(Argument::any())
->shouldBeCalledTimes(1)
->willReturn($mockCacheItem->reveal());
$client = new Client();
$client->setCache($mockCache->reveal());
$client->useApplicationDefaultCredentials();
$token = $client->fetchAccessTokenWithAssertion();
$this->assertArrayHasKey('access_token', $token);
$this->assertEquals($cachedValue['access_token'], $token['access_token']);
}
public function testCacheClientOption()
{
$mockCache = $this->prophesize(CacheItemPoolInterface::class);
$client = new Client([
'cache' => $mockCache->reveal()
]);
$this->assertEquals($mockCache->reveal(), $client->getCache());
}
public function testExecuteWithFormat()
{
$this->onlyGuzzle6Or7();
$client = new Client([
'api_format_v2' => true
]);
$guzzle = $this->prophesize('GuzzleHttp\Client');
$guzzle
->send(Argument::allOf(
Argument::type('Psr\Http\Message\RequestInterface'),
Argument::that(function (RequestInterface $request) {
return $request->getHeaderLine('X-GOOG-API-FORMAT-VERSION') === '2';
})
), [])
->shouldBeCalled()
->willReturn(new Response(200, [], null));
$client->setHttpClient($guzzle->reveal());
$request = new Request('POST', 'http://foo.bar/');
$client->execute($request);
}
public function testExecuteSetsCorrectHeaders()
{
$this->onlyGuzzle6Or7();
$client = new Client();
$guzzle = $this->prophesize('GuzzleHttp\Client');
$guzzle->send(Argument::that(function (RequestInterface $request) {
$userAgent = sprintf(
'%s%s',
Client::USER_AGENT_SUFFIX,
Client::LIBVER
);
$xGoogApiClient = sprintf(
'gl-php/%s gdcl/%s',
phpversion(),
Client::LIBVER
);
if ($request->getHeaderLine('User-Agent') !== $userAgent) {
return false;
}
if ($request->getHeaderLine('x-goog-api-client') !== $xGoogApiClient) {
return false;
}
return true;
}), [])->shouldBeCalledTimes(1)->willReturn(new Response(200, [], null));
$client->setHttpClient($guzzle->reveal());
$request = new Request('POST', 'http://foo.bar/');
$client->execute($request);
}
/**
* @runInSeparateProcess
*/
public function testClientOptions()
{
// Test credential file
$tmpCreds = [
'type' => 'service_account',
'client_id' => 'foo',
'client_email' => '',
'private_key' => ''
];
$tmpCredFile = tempnam(sys_get_temp_dir(), 'creds') . '.json';
file_put_contents($tmpCredFile, json_encode($tmpCreds));
$client = new Client([
'credentials' => $tmpCredFile
]);
$this->assertEquals('foo', $client->getClientId());
// Test credentials array
$client = new Client([
'credentials' => $tmpCredFile
]);
$this->assertEquals('foo', $client->getClientId());
// Test singular scope
$client = new Client([
'scopes' => 'a-scope'
]);
$this->assertEquals(['a-scope'], $client->getScopes());
// Test multiple scopes
$client = new Client([
'scopes' => ['one-scope', 'two-scope']
]);
$this->assertEquals(['one-scope', 'two-scope'], $client->getScopes());
// Test quota project
$client = new Client([
'quota_project' => 'some-quota-project'
]);
$this->assertEquals('some-quota-project', $client->getConfig('quota_project'));
// Test quota project in google/auth dependency
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$tmpCredFile);
$method = new ReflectionMethod($client, 'createApplicationDefaultCredentials');
$method->setAccessible(true);
$credentials = $method->invoke($client);
$this->assertEquals('some-quota-project', $credentials->getQuotaProject());
}
}

View file

@ -0,0 +1,93 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Http;
use Google\Tests\BaseTest;
use Google\Http\Batch;
use Google\Service\Books;
use Google\Service\Storage;
use Google\Service\Exception as ServiceException;
use GuzzleHttp\Psr7;
class BatchTest extends BaseTest
{
public function testBatchRequest()
{
$this->checkKey();
$client = $this->getClient();
$client->setUseBatch(true);
$books = new Books($client);
$batch = $books->createBatch();
$batch->add($books->volumes->listVolumes('Henry David Thoreau'), 'key1');
$batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2');
$result = $batch->execute();
$this->assertArrayHasKey('response-key1', $result);
$this->assertArrayHasKey('response-key2', $result);
}
public function testInvalidBatchRequest()
{
$this->checkKey();
$client = $this->getClient();
$client->setUseBatch(true);
$books = new Books($client);
$batch = $books->createBatch();
$batch->add($books->volumes->listVolumes(false), 'key1');
$batch->add($books->volumes->listVolumes('Edgar Allen Poe'), 'key2');
$result = $batch->execute();
$this->assertArrayHasKey('response-key1', $result);
$this->assertArrayHasKey('response-key2', $result);
$this->assertInstanceOf(
ServiceException::class,
$result['response-key1']
);
}
public function testMediaFileBatch()
{
$client = $this->getClient();
$storage = new Storage($client);
$bucket = 'testbucket';
$stream = Psr7\Utils::streamFor("testbucket-text");
$params = [
'data' => $stream,
'mimeType' => 'text/plain',
];
// Metadata object for new Google Cloud Storage object
$obj = new Storage\StorageObject();
$obj->contentType = "text/plain";
// Batch Upload
$client->setUseBatch(true);
$obj->name = "batch";
/** @var \GuzzleHttp\Psr7\Request $request */
$request = $storage->objects->insert($bucket, $obj, $params);
$this->assertStringContainsString('multipart/related', $request->getHeaderLine('content-type'));
$this->assertStringContainsString('/upload/', $request->getUri()->getPath());
$this->assertStringContainsString('uploadType=multipart', $request->getUri()->getQuery());
}
}

View file

@ -0,0 +1,205 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Http;
use Google\Tests\BaseTest;
use Google\Http\MediaFileUpload;
use Google\Service\Drive;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
class MediaFileUploadTest extends BaseTest
{
public function testMediaFile()
{
$client = $this->getClient();
$request = new Request('POST', 'http://www.example.com');
$media = new MediaFileUpload(
$client,
$request,
'image/png',
base64_decode('data:image/png;base64,a')
);
$request = $media->getRequest();
$this->assertEquals(0, $media->getProgress());
$this->assertGreaterThan(0, strlen($request->getBody()));
}
public function testGetUploadType()
{
$client = $this->getClient();
$request = new Request('POST', 'http://www.example.com');
// Test resumable upload
$media = new MediaFileUpload($client, $request, 'image/png', 'a', true);
$this->assertEquals('resumable', $media->getUploadType(null));
// Test data *only* uploads
$media = new MediaFileUpload($client, $request, 'image/png', 'a', false);
$this->assertEquals('media', $media->getUploadType(null));
// Test multipart uploads
$media = new MediaFileUpload($client, $request, 'image/png', 'a', false);
$this->assertEquals('multipart', $media->getUploadType(array('a' => 'b')));
}
public function testProcess()
{
$client = $this->getClient();
$data = 'foo';
// Test data *only* uploads.
$request = new Request('POST', 'http://www.example.com');
$media = new MediaFileUpload($client, $request, 'image/png', $data, false);
$request = $media->getRequest();
$this->assertEquals($data, (string) $request->getBody());
// Test resumable (meta data) - we want to send the metadata, not the app data.
$request = new Request('POST', 'http://www.example.com');
$reqData = json_encode("hello");
$request = $request->withBody(Psr7\Utils::streamFor($reqData));
$media = new MediaFileUpload($client, $request, 'image/png', $data, true);
$request = $media->getRequest();
$this->assertEquals(json_decode($reqData), (string) $request->getBody());
// Test multipart - we are sending encoded meta data and post data
$request = new Request('POST', 'http://www.example.com');
$reqData = json_encode("hello");
$request = $request->withBody(Psr7\Utils::streamFor($reqData));
$media = new MediaFileUpload($client, $request, 'image/png', $data, false);
$request = $media->getRequest();
$this->assertStringContainsString($reqData, (string) $request->getBody());
$this->assertStringContainsString(base64_encode($data), (string) $request->getBody());
}
public function testGetResumeUri()
{
$this->checkToken();
$client = $this->getClient();
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Drive($client);
$file = new Drive\DriveFile();
$file->name = 'TESTFILE-testGetResumeUri';
$chunkSizeBytes = 1 * 1024 * 1024;
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new MediaFileUpload(
$client,
$request,
'text/plain',
null,
true,
$chunkSizeBytes
);
// request the resumable url
$uri = $media->getResumeUri();
$this->assertIsString($uri);
// parse the URL
$parts = parse_url($uri);
$this->assertArrayHasKey('query', $parts);
// parse the querystring
parse_str($parts['query'], $query);
$this->assertArrayHasKey('uploadType', $query);
$this->assertArrayHasKey('upload_id', $query);
$this->assertEquals('resumable', $query['uploadType']);
}
public function testNextChunk()
{
$this->checkToken();
$client = $this->getClient();
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Drive($client);
$data = 'foo';
$file = new Drive\DriveFile();
$file->name = $name = 'TESTFILE-testNextChunk';
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new MediaFileUpload(
$client,
$request,
'text/plain',
null,
true
);
$media->setFileSize(strlen($data));
// upload the file
$file = $media->nextChunk($data);
$this->assertInstanceOf(Drive\DriveFile::class, $file);
$this->assertEquals($name, $file->name);
// remove the file
$client->setDefer(false);
$response = $service->files->delete($file->id);
$this->assertEquals(204, $response->getStatusCode());
}
public function testNextChunkWithMoreRemaining()
{
$this->checkToken();
$client = $this->getClient();
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Drive($client);
$chunkSizeBytes = 262144; // smallest chunk size allowed by APIs
$data = str_repeat('.', $chunkSizeBytes+1);
$file = new Drive\DriveFile();
$file->name = $name = 'TESTFILE-testNextChunkWithMoreRemaining';
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new MediaFileUpload(
$client,
$request,
'text/plain',
$data,
true,
$chunkSizeBytes
);
$media->setFileSize(strlen($data));
// upload the file
$file = $media->nextChunk();
// false means we aren't done uploading, which is exactly what we expect!
$this->assertFalse($file);
}
}

View file

@ -0,0 +1,141 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Http;
use Google\Http\REST;
use Google\Service\Exception as ServiceException;
use Google\Tests\BaseTest;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
class RESTTest extends BaseTest
{
/**
* @var REST $rest
*/
private $rest;
public function set_up()
{
$this->rest = new REST();
$this->request = new Request('GET', '/');
}
public function testDecodeResponse()
{
$client = $this->getClient();
$response = new Response(204);
$decoded = $this->rest->decodeHttpResponse($response, $this->request);
$this->assertEquals($response, $decoded);
foreach (array(200, 201) as $code) {
$headers = array('foo', 'bar');
$stream = Psr7\Utils::streamFor('{"a": 1}');
$response = new Response($code, $headers, $stream);
$decoded = $this->rest->decodeHttpResponse($response, $this->request);
$this->assertEquals('{"a": 1}', (string) $decoded->getBody());
}
}
public function testDecodeMediaResponse()
{
$client = $this->getClient();
$request = new Request('GET', 'http://www.example.com?alt=media');
$headers = array();
$stream = Psr7\Utils::streamFor('thisisnotvalidjson');
$response = new Response(200, $headers, $stream);
$decoded = $this->rest->decodeHttpResponse($response, $request);
$this->assertEquals('thisisnotvalidjson', (string) $decoded->getBody());
}
public function testDecode500ResponseThrowsException()
{
$this->expectException(ServiceException::class);
$response = new Response(500);
$this->rest->decodeHttpResponse($response, $this->request);
}
public function testExceptionResponse()
{
$this->expectException(ServiceException::class);
$http = new GuzzleClient();
$request = new Request('GET', 'http://httpbin.org/status/500');
$response = $this->rest->doExecute($http, $request);
}
public function testDecodeEmptyResponse()
{
$stream = Psr7\Utils::streamFor('{}');
$response = new Response(200, array(), $stream);
$decoded = $this->rest->decodeHttpResponse($response, $this->request);
$this->assertEquals('{}', (string) $decoded->getBody());
}
public function testBadErrorFormatting()
{
$this->expectException(ServiceException::class);
$stream = Psr7\Utils::streamFor(
'{
"error": {
"code": 500,
"message": null
}
}'
);
$response = new Response(500, array(), $stream);
$this->rest->decodeHttpResponse($response, $this->request);
}
public function tesProperErrorFormatting()
{
$this->expectException(ServiceException::class);
$stream = Psr7\Utils::streamFor(
'{
error: {
errors: [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}'
);
$response = new Response(401, array(), $stream);
$this->rest->decodeHttpResponse($response, $this->request);
}
public function testNotJson404Error()
{
$this->expectException(ServiceException::class);
$stream = Psr7\Utils::streamFor('Not Found');
$response = new Response(404, array(), $stream);
$this->rest->decodeHttpResponse($response, $this->request);
}
}

View file

@ -0,0 +1,289 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests;
use Google\Model;
use Google\Service\AdExchangeBuyer;
use Google\Service\Calendar;
use Google\Service\Dfareporting;
use Google\Service\Drive;
class ModelTest extends BaseTest
{
private $calendarData = '{
"kind": "calendar#event",
"etag": "\"-kteSF26GsdKQ5bfmcd4H3_-u3g/MTE0NTUyNTAxOTk0MjAwMA\"",
"id": "1234566",
"status": "confirmed",
"htmlLink": "https://www.google.com/calendar/event?eid=N",
"created": "2006-04-13T14:22:08.000Z",
"updated": "2006-04-20T09:23:39.942Z",
"summary": "Evening Jolt Q3 CTFL",
"description": "6.30 - Adminning\n9.30 - Game",
"creator": {
"email": "ian@example.com",
"displayName": "Ian Test",
"self": true
},
"organizer": {
"email": "ian@example.com",
"displayName": "Ian Test",
"self": true
},
"start": {
"date": "2006-04-23"
},
"end": {
"date": "2006-04-24"
},
"iCalUID": "5gi2ac493nnrfdfd7jhesafget8@google.com",
"sequence": 0,
"reminders": {
"useDefault": false
}
}';
public function testIntentionalNulls()
{
$data = json_decode($this->calendarData, true);
$event = new Calendar\Event($data);
$obj = json_decode(json_encode($event->toSimpleObject()), true);
$this->assertArrayHasKey('date', $obj['start']);
$this->assertArrayNotHasKey('dateTime', $obj['start']);
$date = new Calendar\EventDateTime();
$date->setDate(Model::NULL_VALUE);
$event->setStart($date);
$obj = json_decode(json_encode($event->toSimpleObject()), true);
$this->assertNull($obj['start']['date']);
$this->assertArrayHasKey('date', $obj['start']);
$this->assertArrayNotHasKey('dateTime', $obj['start']);
}
public function testModelMutation()
{
$data = json_decode($this->calendarData, true);
$event = new Calendar\Event($data);
$date = new Calendar\EventDateTime();
date_default_timezone_set('UTC');
$dateString = Date("c");
$summary = "hello";
$date->setDate($dateString);
$event->setStart($date);
$event->setEnd($date);
$event->setSummary($summary);
$simpleEvent = $event->toSimpleObject();
$this->assertEquals($dateString, $simpleEvent->start->date);
$this->assertEquals($dateString, $simpleEvent->end->date);
$this->assertEquals($summary, $simpleEvent->summary);
$event2 = new Calendar\Event();
$this->assertNull($event2->getStart());
}
public function testVariantTypes()
{
$file = new Drive\DriveFile();
$metadata = new Drive\DriveFileImageMediaMetadata();
$metadata->setCameraMake('Pokémon Snap');
$file->setImageMediaMetadata($metadata);
$data = json_decode(json_encode($file->toSimpleObject()), true);
$this->assertEquals('Pokémon Snap', $data['imageMediaMetadata']['cameraMake']);
}
public function testOddMappingNames()
{
$creative = new AdExchangeBuyer\Creative();
$creative->setAccountId('12345');
$creative->setBuyerCreativeId('12345');
$creative->setAdvertiserName('Hi');
$creative->setHTMLSnippet("<p>Foo!</p>");
$creative->setClickThroughUrl(array('http://somedomain.com'));
$creative->setWidth(100);
$creative->setHeight(100);
$data = json_decode(json_encode($creative->toSimpleObject()), true);
$this->assertEquals($data['HTMLSnippet'], "<p>Foo!</p>");
$this->assertEquals($data['width'], 100);
$this->assertEquals($data['height'], 100);
$this->assertEquals($data['accountId'], "12345");
}
public function testJsonStructure()
{
$model = new Model();
$model->publicA = "This is a string";
$model2 = new Model();
$model2->publicC = 12345;
$model2->publicD = null;
$model->publicB = $model2;
$model3 = new Model();
$model3->publicE = 54321;
$model3->publicF = null;
$model->publicG = array($model3, "hello", false);
$model->publicH = false;
$model->publicI = 0;
$string = json_encode($model->toSimpleObject());
$data = json_decode($string, true);
$this->assertEquals(12345, $data['publicB']['publicC']);
$this->assertEquals("This is a string", $data['publicA']);
$this->assertArrayNotHasKey("publicD", $data['publicB']);
$this->assertArrayHasKey("publicE", $data['publicG'][0]);
$this->assertArrayNotHasKey("publicF", $data['publicG'][0]);
$this->assertEquals("hello", $data['publicG'][1]);
$this->assertFalse($data['publicG'][2]);
$this->assertArrayNotHasKey("data", $data);
$this->assertFalse($data['publicH']);
$this->assertEquals(0, $data['publicI']);
}
public function testIssetPropertyOnModel()
{
$model = new Model();
$model['foo'] = 'bar';
$this->assertTrue(isset($model->foo));
}
public function testUnsetPropertyOnModel()
{
$model = new Model();
$model['foo'] = 'bar';
unset($model->foo);
$this->assertFalse(isset($model->foo));
}
public function testCollectionWithItemsFromConstructor()
{
$data = json_decode(
'{
"kind": "calendar#events",
"id": "1234566",
"etag": "abcdef",
"totalItems": 4,
"items": [
{"id": 1},
{"id": 2},
{"id": 3},
{"id": 4}
]
}',
true
);
$collection = new Calendar\Events($data);
$this->assertCount(4, $collection);
$count = 0;
foreach ($collection as $col) {
$count++;
}
$this->assertEquals(4, $count);
$this->assertEquals(1, $collection[0]->id);
}
public function testCollectionWithItemsFromSetter()
{
$data = json_decode(
'{
"kind": "calendar#events",
"id": "1234566",
"etag": "abcdef",
"totalItems": 4
}',
true
);
$collection = new Calendar\Events($data);
$collection->setItems([
new Calendar\Event(['id' => 1]),
new Calendar\Event(['id' => 2]),
new Calendar\Event(['id' => 3]),
new Calendar\Event(['id' => 4]),
]);
$this->assertCount(4, $collection);
$count = 0;
foreach ($collection as $col) {
$count++;
}
$this->assertEquals(4, $count);
$this->assertEquals(1, $collection[0]->id);
}
public function testMapDataType()
{
$data = json_decode(
'{
"calendar": {
"regular": { "background": "#FFF", "foreground": "#000" },
"inverted": { "background": "#000", "foreground": "#FFF" }
}
}',
true
);
$collection = new Calendar\Colors($data);
$this->assertCount(2, $collection->calendar);
$this->assertTrue(isset($collection->calendar['regular']));
$this->assertTrue(isset($collection->calendar['inverted']));
$this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']);
$this->assertEquals('#FFF', $collection->calendar['regular']->getBackground());
$this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground());
}
public function testPassingInstanceInConstructor()
{
$creator = new Calendar\EventCreator();
$creator->setDisplayName('Brent Shaffer');
$data = [
"creator" => $creator
];
$event = new Calendar\Event($data);
$this->assertInstanceOf(Calendar\EventCreator::class, $event->getCreator());
$this->assertEquals('Brent Shaffer', $event->creator->getDisplayName());
}
public function testPassingInstanceInConstructorForMap()
{
$regular = new Calendar\ColorDefinition();
$regular->setBackground('#FFF');
$regular->setForeground('#000');
$data = [
"calendar" => [
"regular" => $regular,
"inverted" => [ "background" => "#000", "foreground" => "#FFF" ],
]
];
$collection = new Calendar\Colors($data);
$this->assertCount(2, $collection->calendar);
$this->assertTrue(isset($collection->calendar['regular']));
$this->assertTrue(isset($collection->calendar['inverted']));
$this->assertInstanceOf(Calendar\ColorDefinition::class, $collection->calendar['regular']);
$this->assertEquals('#FFF', $collection->calendar['regular']->getBackground());
$this->assertEquals('#FFF', $collection->calendar['inverted']->getForeground());
}
/**
* @see https://github.com/google/google-api-php-client/issues/1308
*/
public function testKeyTypePropertyConflict()
{
$data = [
"duration" => 0,
"durationType" => "unknown",
];
$creativeAsset = new Dfareporting\CreativeAsset($data);
$this->assertEquals(0, $creativeAsset->getDuration());
$this->assertEquals('unknown', $creativeAsset->getDurationType());
}
}

View file

@ -0,0 +1,493 @@
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Service;
use Google\Service\AdSense;
use Google\Tests\BaseTest;
class AdSenseTest extends BaseTest
{
public $adsense;
public function set_up()
{
$this->markTestSkipped('Thesse tests need to be fixed');
$this->checkToken();
$this->adsense = new AdSense($this->getClient());
}
public function testAccountsList()
{
$accounts = $this->adsense->accounts->listAccounts();
$this->assertArrayHasKey('kind', $accounts);
$this->assertEquals($accounts['kind'], 'adsense#accounts');
$account = $this->getRandomElementFromArray($accounts['items']);
$this->checkAccountElement($account);
}
/**
* @depends testAccountsList
*/
public function testAccountsGet()
{
$accounts = $this->adsense->accounts->listAccounts();
$account = $this->getRandomElementFromArray($accounts['items']);
$retrievedAccount = $this->adsense->accounts->get($account['id']);
$this->checkAccountElement($retrievedAccount);
}
/**
* @depends testAccountsList
*/
public function testAccountsReportGenerate()
{
$startDate = '2011-01-01';
$endDate = '2011-01-31';
$optParams = $this->getReportOptParams();
$accounts = $this->adsense->accounts->listAccounts();
$accountId = $accounts['items'][0]['id'];
$report = $this->adsense->accounts_reports->generate(
$accountId,
$startDate,
$endDate,
$optParams
);
$this->checkReport($report);
}
/**
* @depends testAccountsList
*/
public function testAccountsAdClientsList()
{
$accounts = $this->adsense->accounts->listAccounts();
$account = $this->getRandomElementFromArray($accounts['items']);
$adClients =
$this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
$this->checkAdClientsCollection($adClients);
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
*/
public function testAccountsAdUnitsList()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$adUnits = $this->adsense->accounts_adunits->listAccountsAdunits(
$account['id'],
$adClient['id']
);
$this->checkAdUnitsCollection($adUnits);
break 2;
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
*/
public function testAccountsAdUnitsGet()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$adUnits = $this->adsense->accounts_adunits->listAccountsAdunits(
$account['id'],
$adClient['id']
);
if (array_key_exists('items', $adUnits)) {
$adUnit = $this->getRandomElementFromArray($adUnits['items']);
$this->checkAdUnitElement($adUnit);
break 2;
}
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
*/
public function testAccountsCustomChannelsList()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$customChannels = $this->adsense->accounts_customchannels
->listAccountsCustomchannels($account['id'], $adClient['id']);
$this->checkCustomChannelsCollection($customChannels);
break 2;
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
*/
public function testAccountsCustomChannelsGet()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$customChannels =
$this->adsense->accounts_customchannels->listAccountsCustomchannels(
$account['id'],
$adClient['id']
);
if (array_key_exists('items', $customChannels)) {
$customChannel =
$this->getRandomElementFromArray($customChannels['items']);
$this->checkCustomChannelElement($customChannel);
break 2;
}
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
*/
public function testAccountsUrlChannelsList()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$urlChannels =
$this->adsense->accounts_urlchannels->listAccountsUrlchannels(
$account['id'],
$adClient['id']
);
$this->checkUrlChannelsCollection($urlChannels);
break 2;
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
* @depends testAccountsAdUnitsList
*/
public function testAccountsAdUnitsCustomChannelsList()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients = $this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$adUnits =
$this->adsense->accounts_adunits->listAccountsAdunits($account['id'], $adClient['id']);
if (array_key_exists('items', $adUnits)) {
foreach ($adUnits['items'] as $adUnit) {
$customChannels =
$this->adsense->accounts_adunits_customchannels->listAccountsAdunitsCustomchannels(
$account['id'],
$adClient['id'],
$adUnit['id']
);
$this->checkCustomChannelsCollection($customChannels);
// it's too expensive to go through each, if one is correct good
break 3;
}
}
}
}
}
/**
* @depends testAccountsList
* @depends testAccountsAdClientsList
* @depends testAccountsCustomChannelsList
*/
public function testAccountsCustomChannelsAdUnitsList()
{
$accounts = $this->adsense->accounts->listAccounts();
foreach ($accounts['items'] as $account) {
$adClients =
$this->adsense->accounts_adclients->listAccountsAdclients($account['id']);
foreach ($adClients['items'] as $adClient) {
$customChannels =
$this->adsense->accounts_customchannels->listAccountsCustomchannels(
$account['id'],
$adClient['id']
);
if (array_key_exists('items', $customChannels)) {
foreach ($customChannels['items'] as $customChannel) {
$adUnits =
$this->adsense->accounts_customchannels_adunits->listAccountsCustomchannelsAdunits(
$account['id'],
$adClient['id'],
$customChannel['id']
);
$this->checkAdUnitsCollection($adUnits);
// it's too expensive to go through each, if one is correct good
break 3;
}
}
}
}
}
public function testAdClientsList()
{
$adClients = $this->adsense->adclients->listAdclients();
$this->checkAdClientsCollection($adClients);
}
/**
* @depends testAdClientsList
*/
public function testAdUnitsList()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$adUnits = $this->adsense->adunits->listAdunits($adClient['id']);
$this->checkAdUnitsCollection($adUnits);
}
}
/**
* @depends testAdClientsList
*/
public function testAdUnitsGet()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$adUnits = $this->adsense->adunits->listAdunits($adClient['id']);
if (array_key_exists('items', $adUnits)) {
$adUnit = $this->getRandomElementFromArray($adUnits['items']);
$this->checkAdUnitElement($adUnit);
break 1;
}
}
}
/**
* @depends testAdClientsList
* @depends testAdUnitsList
*/
public function testAdUnitsCustomChannelsList()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$adUnits = $this->adsense->adunits->listAdunits($adClient['id']);
if (array_key_exists('items', $adUnits)) {
foreach ($adUnits['items'] as $adUnit) {
$customChannels =
$this->adsense->adunits_customchannels->listAdunitsCustomchannels(
$adClient['id'],
$adUnit['id']
);
$this->checkCustomChannelsCollection($customChannels);
break 2;
}
}
}
}
/**
* @depends testAdClientsList
*/
public function testCustomChannelsList()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$customChannels =
$this->adsense->customchannels->listCustomchannels($adClient['id']);
$this->checkCustomChannelsCollection($customChannels);
}
}
/**
* @depends testAdClientsList
*/
public function testCustomChannelsGet()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']);
if (array_key_exists('items', $customChannels)) {
$customChannel = $this->getRandomElementFromArray($customChannels['items']);
$this->checkCustomChannelElement($customChannel);
break 1;
}
}
}
/**
* @depends testAdClientsList
* @depends testCustomChannelsList
*/
public function testCustomChannelsAdUnitsList()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$customChannels = $this->adsense->customchannels->listCustomchannels($adClient['id']);
if (array_key_exists('items', $customChannels)) {
foreach ($customChannels['items'] as $customChannel) {
$adUnits =
$this->adsense->customchannels_adunits->listCustomchannelsAdunits(
$adClient['id'],
$customChannel['id']
);
$this->checkAdUnitsCollection($adUnits);
break 2;
}
}
}
}
/**
* @depends testAdClientsList
*/
public function testUrlChannelsList()
{
$adClients = $this->adsense->adclients->listAdclients();
foreach ($adClients['items'] as $adClient) {
$urlChannels = $this->adsense->urlchannels->listUrlchannels($adClient['id']);
$this->checkUrlChannelsCollection($urlChannels);
}
}
public function testReportsGenerate()
{
if (!$this->checkToken()) {
return;
}
$startDate = '2011-01-01';
$endDate = '2011-01-31';
$optParams = $this->getReportOptParams();
$report = $this->adsense->reports->generate($startDate, $endDate, $optParams);
$this->checkReport($report);
}
private function checkAccountElement($account)
{
$this->assertArrayHasKey('kind', $account);
$this->assertArrayHasKey('id', $account);
$this->assertArrayHasKey('name', $account);
}
private function checkAdClientsCollection($adClients)
{
$this->assertArrayHasKey('kind', $adClients);
$this->assertEquals($adClients['kind'], 'adsense#adClients');
foreach ($adClients['items'] as $adClient) {
$this->assertArrayHasKey('id', $adClient);
$this->assertArrayHasKey('kind', $adClient);
$this->assertArrayHasKey('productCode', $adClient);
$this->assertArrayHasKey('supportsReporting', $adClient);
}
}
private function checkAdUnitsCollection($adUnits)
{
$this->assertArrayHasKey('kind', $adUnits);
$this->assertEquals($adUnits['kind'], 'adsense#adUnits');
if (array_key_exists('items', $adUnits)) {
foreach ($adUnits['items'] as $adUnit) {
$this->checkAdUnitElement($adUnit);
}
}
}
private function checkAdUnitElement($adUnit)
{
$this->assertArrayHasKey('code', $adUnit);
$this->assertArrayHasKey('id', $adUnit);
$this->assertArrayHasKey('kind', $adUnit);
$this->assertArrayHasKey('name', $adUnit);
$this->assertArrayHasKey('status', $adUnit);
}
private function checkCustomChannelsCollection($customChannels)
{
$this->assertArrayHasKey('kind', $customChannels);
$this->assertEquals($customChannels['kind'], 'adsense#customChannels');
if (array_key_exists('items', $customChannels)) {
foreach ($customChannels['items'] as $customChannel) {
$this->checkCustomChannelElement($customChannel);
}
}
}
private function checkCustomChannelElement($customChannel)
{
$this->assertArrayHasKey('kind', $customChannel);
$this->assertArrayHasKey('id', $customChannel);
$this->assertArrayHasKey('code', $customChannel);
$this->assertArrayHasKey('name', $customChannel);
}
private function checkUrlChannelsCollection($urlChannels)
{
$this->assertArrayHasKey('kind', $urlChannels);
$this->assertEquals($urlChannels['kind'], 'adsense#urlChannels');
if (array_key_exists('items', $urlChannels)) {
foreach ($urlChannels['items'] as $urlChannel) {
$this->assertArrayHasKey('kind', $urlChannel);
$this->assertArrayHasKey('id', $urlChannel);
$this->assertArrayHasKey('urlPattern', $urlChannel);
}
}
}
private function getReportOptParams()
{
return array(
'metric' => array('PAGE_VIEWS', 'AD_REQUESTS'),
'dimension' => array ('DATE', 'AD_CLIENT_ID'),
'sort' => array('DATE'),
'filter' => array('COUNTRY_NAME==United States'),
);
}
private function checkReport($report)
{
$this->assertArrayHasKey('kind', $report);
$this->assertEquals($report['kind'], 'adsense#report');
$this->assertArrayHasKey('totalMatchedRows', $report);
$this->assertGreaterThan(0, count($report->headers));
foreach ($report['headers'] as $header) {
$this->assertArrayHasKey('name', $header);
$this->assertArrayHasKey('type', $header);
}
if (array_key_exists('items', $report)) {
foreach ($report['items'] as $row) {
$this->assertCount(4, $row);
}
}
$this->assertArrayHasKey('totals', $report);
$this->assertArrayHasKey('averages', $report);
}
private function getRandomElementFromArray($array)
{
$elementKey = array_rand($array);
return $array[$elementKey];
}
}

View file

@ -0,0 +1,473 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Service;
use Google\Client;
use Google\Tests\BaseTest;
use Google\Service as GoogleService;
use Google\Service\Exception as ServiceException;
use Google\Service\Resource as GoogleResource;
use Google\Exception as GoogleException;
use GuzzleHttp\Message\Response as Guzzle5Response;
use GuzzleHttp\Psr7;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Psr7\Stream;
use GuzzleHttp\Stream\Stream as Guzzle5Stream;
use Prophecy\Argument;
class TestService extends \Google\Service
{
public function __construct(Client $client)
{
parent::__construct($client);
$this->rootUrl = "https://test.example.com";
$this->servicePath = "";
$this->version = "v1beta1";
$this->serviceName = "test";
}
}
class ResourceTest extends BaseTest
{
private $client;
private $service;
public function set_up()
{
$this->client = $this->prophesize(Client::class);
$logger = $this->prophesize("Monolog\Logger");
$this->client->getLogger()->willReturn($logger->reveal());
$this->client->shouldDefer()->willReturn(true);
$this->client->getHttpClient()->willReturn(new GuzzleClient());
$this->service = new TestService($this->client->reveal());
}
public function testCallFailure()
{
$this->expectException(GoogleException::class);
$this->expectExceptionMessage('Unknown function: test->testResource->someothermethod()');
$resource = new GoogleResource(
$this->service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
$resource->call("someothermethod", array());
}
public function testCall()
{
$resource = new GoogleResource(
$this->service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
$request = $resource->call("testMethod", array(array()));
$this->assertEquals("https://test.example.com/method/path", (string) $request->getUri());
$this->assertEquals("POST", $request->getMethod());
}
public function testCallServiceDefinedRoot()
{
$this->service->rootUrl = "https://sample.example.com";
$resource = new GoogleResource(
$this->service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
$request = $resource->call("testMethod", array(array()));
$this->assertEquals("https://sample.example.com/method/path", (string) $request->getUri());
$this->assertEquals("POST", $request->getMethod());
}
/**
* Some Google Service (Google\Service\Directory\Resource\Channels and
* Google\Service\Reports\Resource\Channels) use a different servicePath value
* that should override the default servicePath value, it's represented by a /
* before the resource path. All other Services have no / before the path
*/
public function testCreateRequestUriForASelfDefinedServicePath()
{
$this->service->servicePath = '/admin/directory/v1/';
$resource = new GoogleResource(
$this->service,
'test',
'testResource',
array("methods" =>
array(
'testMethod' => array(
'parameters' => array(),
'path' => '/admin/directory_v1/watch/stop',
'httpMethod' => 'POST',
)
)
)
);
$request = $resource->call('testMethod', array(array()));
$this->assertEquals('https://test.example.com/admin/directory_v1/watch/stop', (string) $request->getUri());
}
public function testCreateRequestUri()
{
$restPath = "plus/{u}";
$service = new GoogleService($this->client->reveal());
$service->servicePath = "http://localhost/";
$resource = new GoogleResource($service, 'test', 'testResource', array());
// Test Path
$params = array();
$params['u']['type'] = 'string';
$params['u']['location'] = 'path';
$params['u']['value'] = 'me';
$value = $resource->createRequestUri($restPath, $params);
$this->assertEquals("http://localhost/plus/me", $value);
// Test Query
$params = array();
$params['u']['type'] = 'string';
$params['u']['location'] = 'query';
$params['u']['value'] = 'me';
$value = $resource->createRequestUri('plus', $params);
$this->assertEquals("http://localhost/plus?u=me", $value);
// Test Booleans
$params = array();
$params['u']['type'] = 'boolean';
$params['u']['location'] = 'path';
$params['u']['value'] = '1';
$value = $resource->createRequestUri($restPath, $params);
$this->assertEquals("http://localhost/plus/true", $value);
$params['u']['location'] = 'query';
$value = $resource->createRequestUri('plus', $params);
$this->assertEquals("http://localhost/plus?u=true", $value);
// Test encoding
$params = array();
$params['u']['type'] = 'string';
$params['u']['location'] = 'query';
$params['u']['value'] = '@me/';
$value = $resource->createRequestUri('plus', $params);
$this->assertEquals("http://localhost/plus?u=%40me%2F", $value);
}
public function testNoExpectedClassForAltMediaWithHttpSuccess()
{
// set the "alt" parameter to "media"
$arguments = [['alt' => 'media']];
$request = new Request('GET', '/?alt=media');
$http = $this->prophesize("GuzzleHttp\Client");
if ($this->isGuzzle5()) {
$body = Guzzle5Stream::factory('thisisnotvalidjson');
$response = new Guzzle5Response(200, [], $body);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media'));
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response);
} else {
$body = Psr7\Utils::streamFor('thisisnotvalidjson');
$response = new Response(200, [], $body);
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response);
}
$client = new Client();
$client->setHttpClient($http->reveal());
$service = new TestService($client);
// set up mock objects
$resource = new GoogleResource(
$service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
$expectedClass = 'ThisShouldBeIgnored';
$response = $resource->call('testMethod', $arguments, $expectedClass);
$this->assertInstanceOf('Psr\Http\Message\ResponseInterface', $response);
$this->assertEquals('thisisnotvalidjson', (string) $response->getBody());
}
public function testNoExpectedClassForAltMediaWithHttpFail()
{
// set the "alt" parameter to "media"
$arguments = [['alt' => 'media']];
$request = new Request('GET', '/?alt=media');
$http = $this->prophesize("GuzzleHttp\Client");
if ($this->isGuzzle5()) {
$body = Guzzle5Stream::factory('thisisnotvalidjson');
$response = new Guzzle5Response(400, [], $body);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media'));
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response);
} else {
$body = Psr7\Utils::streamFor('thisisnotvalidjson');
$response = new Response(400, [], $body);
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response);
}
$client = new Client();
$client->setHttpClient($http->reveal());
$service = new TestService($client);
// set up mock objects
$resource = new GoogleResource(
$service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
try {
$expectedClass = 'ThisShouldBeIgnored';
$decoded = $resource->call('testMethod', $arguments, $expectedClass);
$this->fail('should have thrown exception');
} catch (ServiceException $e) {
// Alt Media on error should return a safe error
$this->assertEquals('thisisnotvalidjson', $e->getMessage());
}
}
public function testErrorResponseWithVeryLongBody()
{
// set the "alt" parameter to "media"
$arguments = [['alt' => 'media']];
$request = new Request('GET', '/?alt=media');
$http = $this->prophesize("GuzzleHttp\Client");
if ($this->isGuzzle5()) {
$body = Guzzle5Stream::factory('this will be pulled into memory');
$response = new Guzzle5Response(400, [], $body);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media'));
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response);
} else {
$body = Psr7\Utils::streamFor('this will be pulled into memory');
$response = new Response(400, [], $body);
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response);
}
$client = new Client();
$client->setHttpClient($http->reveal());
$service = new TestService($client);
// set up mock objects
$resource = new GoogleResource(
$service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
try {
$expectedClass = 'ThisShouldBeIgnored';
$decoded = $resource->call('testMethod', $arguments, $expectedClass);
$this->fail('should have thrown exception');
} catch (ServiceException $e) {
// empty message - alt=media means no message
$this->assertEquals('this will be pulled into memory', $e->getMessage());
}
}
public function testSuccessResponseWithVeryLongBody()
{
$this->onlyGuzzle6Or7();
// set the "alt" parameter to "media"
$arguments = [['alt' => 'media']];
$stream = $this->prophesize(Stream::class);
$stream->__toString()
->shouldNotBeCalled();
$response = new Response(200, [], $stream->reveal());
$http = $this->prophesize("GuzzleHttp\Client");
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response);
$client = new Client();
$client->setHttpClient($http->reveal());
$service = new TestService($client);
// set up mock objects
$resource = new GoogleResource(
$service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
$expectedClass = 'ThisShouldBeIgnored';
$response = $resource->call('testMethod', $arguments, $expectedClass);
$this->assertEquals(200, $response->getStatusCode());
// $this->assertFalse($stream->toStringCalled);
}
public function testExceptionMessage()
{
// set the "alt" parameter to "media"
$request = new Request('GET', '/');
$errors = [ ["domain" => "foo"] ];
$content = json_encode([
'error' => [
'errors' => $errors
]
]);
$http = $this->prophesize("GuzzleHttp\Client");
if ($this->isGuzzle5()) {
$body = Guzzle5Stream::factory($content);
$response = new Guzzle5Response(400, [], $body);
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->willReturn(new \GuzzleHttp\Message\Request('GET', '/?alt=media'));
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes(1)
->willReturn($response);
} else {
$body = Psr7\Utils::streamFor($content);
$response = new Response(400, [], $body);
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes(1)
->willReturn($response);
}
$client = new Client();
$client->setHttpClient($http->reveal());
$service = new TestService($client);
// set up mock objects
$resource = new GoogleResource(
$service,
"test",
"testResource",
array("methods" =>
array(
"testMethod" => array(
"parameters" => array(),
"path" => "method/path",
"httpMethod" => "POST",
)
)
)
);
try {
$decoded = $resource->call('testMethod', array(array()));
$this->fail('should have thrown exception');
} catch (ServiceException $e) {
$this->assertEquals($errors, $e->getErrors());
}
}
}

View file

@ -0,0 +1,95 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Service;
use Google\Service\Tasks;
use Google\Tests\BaseTest;
class TasksTest extends BaseTest
{
/** @var Tasks */
public $taskService;
public function set_up()
{
$this->checkToken();
$this->taskService = new Tasks($this->getClient());
}
public function testInsertTask()
{
$list = $this->createTaskList('List: ' . __METHOD__);
$task = $this->createTask('Task: '.__METHOD__, $list->id);
$this->assertIsTask($task);
}
/**
* @depends testInsertTask
*/
public function testGetTask()
{
$tasks = $this->taskService->tasks;
$list = $this->createTaskList('List: ' . __METHOD__);
$task = $this->createTask('Task: '. __METHOD__, $list['id']);
$task = $tasks->get($list['id'], $task['id']);
$this->assertIsTask($task);
}
/**
* @depends testInsertTask
*/
public function testListTask()
{
$tasks = $this->taskService->tasks;
$list = $this->createTaskList('List: ' . __METHOD__);
for ($i=0; $i<4; $i++) {
$this->createTask("Task: $i ".__METHOD__, $list['id']);
}
$tasksArray = $tasks->listTasks($list['id']);
$this->assertGreaterThan(1, count($tasksArray));
foreach ($tasksArray['items'] as $task) {
$this->assertIsTask($task);
}
}
private function createTaskList($name)
{
$list = new Tasks\TaskList();
$list->title = $name;
return $this->taskService->tasklists->insert($list);
}
private function createTask($title, $listId)
{
$tasks = $this->taskService->tasks;
$task = new Tasks\Task();
$task->title = $title;
return $tasks->insert($listId, $task);
}
private function assertIsTask($task)
{
$this->assertArrayHasKey('title', $task);
$this->assertArrayHasKey('kind', $task);
$this->assertArrayHasKey('id', $task);
$this->assertArrayHasKey('position', $task);
}
}

View file

@ -0,0 +1,83 @@
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Service;
use Google\Service\YouTube;
use Google\Tests\BaseTest;
class YouTubeTest extends BaseTest
{
/** @var YouTube */
public $youtube;
public function set_up()
{
$this->checkToken();
$this->youtube = new YouTube($this->getClient());
}
public function testMissingFieldsAreNull()
{
$parts = "id,brandingSettings";
$opts = array("mine" => true);
$channels = $this->youtube->channels->listChannels($parts, $opts);
$newChannel = new YouTube\Channel();
$newChannel->setId( $channels[0]->getId());
$newChannel->setBrandingSettings($channels[0]->getBrandingSettings());
$simpleOriginal = $channels[0]->toSimpleObject();
$simpleNew = $newChannel->toSimpleObject();
$this->assertObjectHasAttribute('etag', $simpleOriginal);
$this->assertObjectNotHasAttribute('etag', $simpleNew);
$owner_details = new YouTube\ChannelContentOwnerDetails();
$owner_details->setTimeLinked("123456789");
$o_channel = new YouTube\Channel();
$o_channel->setContentOwnerDetails($owner_details);
$simpleManual = $o_channel->toSimpleObject();
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
$owner_details = new YouTube\ChannelContentOwnerDetails();
$owner_details->timeLinked = "123456789";
$o_channel = new YouTube\Channel();
$o_channel->setContentOwnerDetails($owner_details);
$simpleManual = $o_channel->toSimpleObject();
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
$owner_details = new YouTube\ChannelContentOwnerDetails();
$owner_details['timeLinked'] = "123456789";
$o_channel = new YouTube\Channel();
$o_channel->setContentOwnerDetails($owner_details);
$simpleManual = $o_channel->toSimpleObject();
$this->assertObjectHasAttribute('timeLinked', $simpleManual->contentOwnerDetails);
$this->assertObjectNotHasAttribute('contentOwner', $simpleManual->contentOwnerDetails);
$ping = new YouTube\ChannelConversionPing();
$ping->setContext("hello");
$pings = new YouTube\ChannelConversionPings();
$pings->setPings(array($ping));
$simplePings = $pings->toSimpleObject();
$this->assertObjectHasAttribute('context', $simplePings->pings[0]);
$this->assertObjectNotHasAttribute('conversionUrl', $simplePings->pings[0]);
}
}

View file

@ -0,0 +1,186 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests;
use Google\Client;
use Google\Model;
use Google\Service;
use Google\Http\Batch;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
class TestModel extends Model
{
public function mapTypes($array)
{
return parent::mapTypes($array);
}
public function isAssociativeArray($array)
{
return parent::isAssociativeArray($array);
}
}
class TestService extends Service
{
public $batchPath = 'batch/test';
}
if (trait_exists('\Prophecy\PhpUnit\ProphecyTrait')) {
trait ServiceTestTrait
{
use \Prophecy\PhpUnit\ProphecyTrait;
}
} else {
trait ServiceTestTrait
{
}
}
class ServiceTest extends TestCase
{
private static $errorMessage;
use ServiceTestTrait;
public function testCreateBatch()
{
$response = $this->prophesize(ResponseInterface::class);
$client = $this->prophesize(Client::class);
$client->execute(
Argument::allOf(
Argument::type(RequestInterface::class),
Argument::that(
function ($request) {
$this->assertEquals('/batch/test', $request->getRequestTarget());
return $request;
}
)
),
Argument::any()
)->willReturn($response->reveal());
$client->getConfig('base_path')->willReturn('');
$model = new TestService($client->reveal());
$batch = $model->createBatch();
$this->assertInstanceOf(Batch::class, $batch);
$batch->execute();
}
public function testModel()
{
$model = new TestModel();
$model->mapTypes(
array(
'name' => 'asdf',
'gender' => 'z',
)
);
$this->assertEquals('asdf', $model->name);
$this->assertEquals('z', $model->gender);
$model->mapTypes(
array(
'__infoType' => 'Google_Model',
'__infoDataType' => 'map',
'info' => array (
'location' => 'mars',
'timezone' => 'mst',
),
'name' => 'asdf',
'gender' => 'z',
)
);
$this->assertEquals('asdf', $model->name);
$this->assertEquals('z', $model->gender);
$this->assertFalse($model->isAssociativeArray(""));
$this->assertFalse($model->isAssociativeArray(false));
$this->assertFalse($model->isAssociativeArray(null));
$this->assertFalse($model->isAssociativeArray(array()));
$this->assertFalse($model->isAssociativeArray(array(1, 2)));
$this->assertFalse($model->isAssociativeArray(array(1 => 2)));
$this->assertTrue($model->isAssociativeArray(array('test' => 'a')));
$this->assertTrue($model->isAssociativeArray(array("a", "b" => 2)));
}
public function testConfigConstructor()
{
$clientId = 'test-client-id';
$service = new TestService(['client_id' => $clientId]);
$this->assertEquals($clientId, $service->getClient()->getClientId());
}
public function testNoConstructor()
{
$service = new TestService();
$this->assertInstanceOf(Client::class, $service->getClient());
}
public function testInvalidConstructorPhp7Plus()
{
if (!class_exists('TypeError')) {
$this->markTestSkipped('PHP 7+ only');
}
try {
$service = new TestService('foo');
} catch (\TypeError $e) {
}
$this->assertInstanceOf('TypeError', $e);
$this->assertEquals(
'constructor must be array or instance of Google\Client',
$e->getMessage()
);
}
/** @runInSeparateProcess */
public function testInvalidConstructorPhp5()
{
if (class_exists('TypeError')) {
$this->markTestSkipped('PHP 5 only');
}
set_error_handler('Google\Tests\ServiceTest::handlePhp5Error');
$service = new TestService('foo');
$this->assertEquals(
'constructor must be array or instance of Google\Client',
self::$errorMessage
);
}
public static function handlePhp5Error($errno, $errstr, $errfile, $errline)
{
self::assertEquals(E_USER_ERROR, $errno);
self::$errorMessage = $errstr;
return true;
}
}

View file

@ -0,0 +1,268 @@
<?php
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Task;
use Google\Tests\BaseTest;
use Google\Task\Composer;
use Symfony\Component\Filesystem\Filesystem;
use InvalidArgumentException;
class ComposerTest extends BaseTest
{
private static $composerBaseConfig = [
'repositories' => [
[
'type' => 'path',
'url' => __DIR__ . '/../../..',
'options' => [
'symlink' => false
]
]
],
'require' => [
'google/apiclient' => '*'
],
'scripts' => [
'pre-autoload-dump' => 'Google\Task\Composer::cleanup'
],
'minimum-stability' => 'dev',
];
public function testInvalidServiceName()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Google service "Foo" does not exist');
Composer::cleanup($this->createMockEvent(['Foo']));
}
public function testRelatePathServiceName()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid Google service name "../YouTube"');
Composer::cleanup($this->createMockEvent(['../YouTube']));
}
public function testEmptyServiceName()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Google service "" does not exist');
Composer::cleanup($this->createMockEvent(['']));
}
public function testWildcardServiceName()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid Google service name "YouTube*"');
Composer::cleanup($this->createMockEvent(['YouTube*']));
}
public function testRemoveServices()
{
$vendorDir = sys_get_temp_dir() . '/rand-' . rand();
$serviceDir = sprintf(
'%s/google/apiclient-services/src/',
$vendorDir
);
$dirs = [
'ServiceToKeep',
'ServiceToDelete1',
'ServiceToDelete2',
];
$files = [
'ServiceToKeep/ServiceFoo.php',
'ServiceToKeep.php',
'SomeRandomFile.txt',
'ServiceToDelete1/ServiceFoo.php',
'ServiceToDelete1.php',
'ServiceToDelete2/ServiceFoo.php',
'ServiceToDelete2.php',
];
foreach ($dirs as $dir) {
@mkdir($serviceDir . $dir, 0777, true);
}
foreach ($files as $file) {
touch($serviceDir . $file);
}
$print = 'Removing 2 google services';
Composer::cleanup(
$this->createMockEvent(['ServiceToKeep'], $vendorDir, $print),
$this->createMockFilesystem([
'ServiceToDelete2',
'ServiceToDelete2.php',
'ServiceToDelete1',
'ServiceToDelete1.php',
], $serviceDir)
);
}
private function createMockFilesystem(array $files, $serviceDir)
{
$mockFilesystem = $this->prophesize(Filesystem::class);
foreach ($files as $filename) {
$file = new \SplFileInfo($serviceDir . $filename);
$mockFilesystem->remove($file->getRealPath())
->shouldBeCalledTimes(1);
}
return $mockFilesystem->reveal();
}
private function createMockEvent(
array $servicesToKeep,
$vendorDir = '',
$print = null
) {
$mockPackage = $this->prophesize('Composer\Package\RootPackage');
$mockPackage->getExtra()
->shouldBeCalledTimes(1)
->willReturn(['google/apiclient-services' => $servicesToKeep]);
$mockConfig = $this->prophesize('Composer\Config');
$mockConfig->get('vendor-dir')
->shouldBeCalledTimes(1)
->willReturn($vendorDir);
$mockComposer = $this->prophesize('Composer\Composer');
$mockComposer->getPackage()
->shouldBeCalledTimes(1)
->willReturn($mockPackage->reveal());
$mockComposer->getConfig()
->shouldBeCalledTimes(1)
->willReturn($mockConfig->reveal());
$mockEvent = $this->prophesize('Composer\Script\Event');
$mockEvent->getComposer()
->shouldBeCalledTimes(1)
->willReturn($mockComposer);
if ($print) {
$mockIO = $this->prophesize('Composer\IO\ConsoleIO');
$mockIO->write($print)
->shouldBeCalledTimes(1);
$mockEvent->getIO()
->shouldBeCalledTimes(1)
->willReturn($mockIO->reveal());
}
return $mockEvent->reveal();
}
public function testE2E()
{
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
'YouTube'
]
]
]);
$serviceDir = $dir . '/vendor/google/apiclient-services/src';
$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
$this->assertFileExists($serviceDir . '/YouTube.php');
$this->assertFileExists($serviceDir . '/YouTube');
$this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting.php');
$this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting');
// Remove the "apiclient-services" directory, which is required to
// update the cleanup command.
passthru('rm -r ' . $dir . '/vendor/google/apiclient-services');
$this->runComposerInstall(self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
'YouTube',
'YouTubeReporting',
]
]
], $dir);
$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
$this->assertFileExists($serviceDir . '/YouTube.php');
$this->assertFileExists($serviceDir . '/YouTube');
$this->assertFileExists($serviceDir . '/YouTubeReporting.php');
$this->assertFileExists($serviceDir . '/YouTubeReporting');
}
public function testE2EBCTaskName()
{
$composerConfig = self::$composerBaseConfig + [
'extra' => [
'google/apiclient-services' => [
'Drive',
]
]
];
// Test BC Task name
$composerConfig['scripts']['pre-autoload-dump'] = 'Google_Task_Composer::cleanup';
$dir = $this->runComposerInstall($composerConfig);
$serviceDir = $dir . '/vendor/google/apiclient-services/src';
$this->assertFileExists($serviceDir . '/Drive.php');
$this->assertFileExists($serviceDir . '/Drive');
$this->assertFileDoesNotExist($serviceDir . '/YouTube.php');
$this->assertFileDoesNotExist($serviceDir . '/YouTube');
$this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting.php');
$this->assertFileDoesNotExist($serviceDir . '/YouTubeReporting');
}
public function testE2EOptimized()
{
$dir = $this->runComposerInstall(self::$composerBaseConfig + [
'config' => [
'optimize-autoloader' => true,
],
'extra' => [
'google/apiclient-services' => [
'Drive'
]
]
]);
$classmap = require_once $dir . '/vendor/composer/autoload_classmap.php';
// Verify removed services do not show up in the classmap
$this->assertArrayHasKey('Google\Service\Drive', $classmap);
$this->assertArrayNotHasKey('Google\Service\YouTube', $classmap);
}
private function runComposerInstall(array $composerConfig, $dir = null)
{
$composerJson = json_encode($composerConfig);
if (is_null($dir)) {
$dir = sys_get_temp_dir() . '/test-' . rand();
mkdir($dir);
}
file_put_contents($dir . '/composer.json', $composerJson);
passthru('composer install -d ' . $dir);
return $dir;
}
}

View file

@ -0,0 +1,786 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Google\Tests\Task;
use Google\Client;
use Google\Task\Runner;
use Google\Tests\BaseTest;
use Google\Http\Request as GoogleRequest;
use Google\Http\REST;
use Google\Service\Exception as ServiceException;
use Google\Task\Exception as TaskException;
use GuzzleHttp\Message\Response as Guzzle5Response;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Stream\Stream as Guzzle5Stream;
use Prophecy\Argument;
use Exception;
class RunnerTest extends BaseTest
{
private $client;
private $mockedCallsCount = 0;
private $currentMockedCall = 0;
private $mockedCalls = array();
private $retryMap;
private $retryConfig;
protected function set_up()
{
$this->client = new Client();
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testRestRetryOffByDefault($errorCode, $errorBody = '{}')
{
$this->expectException(ServiceException::class);
$this->setNextResponse($errorCode, $errorBody)->makeRequest();
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testOneRestRetryWithError($errorCode, $errorBody = '{}')
{
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 1));
$this->setNextResponses(2, $errorCode, $errorBody)->makeRequest();
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testMultipleRestRetriesWithErrors(
$errorCode,
$errorBody = '{}'
) {
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponses(6, $errorCode, $errorBody)->makeRequest();
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testOneRestRetryWithSuccess($errorCode, $errorBody = '{}')
{
$this->setRetryConfig(array('retries' => 1));
$result = $this->setNextResponse($errorCode, $errorBody)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testMultipleRestRetriesWithSuccess(
$errorCode,
$errorBody = '{}'
) {
$this->setRetryConfig(array('retries' => 5));
$result = $this->setNextResponses(2, $errorCode, $errorBody)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @dataProvider defaultRestErrorProvider
*/
public function testCustomRestRetryMapReplacesDefaults(
$errorCode,
$errorBody = '{}'
) {
$this->expectException(ServiceException::class);
$this->setRetryMap(array());
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponse($errorCode, $errorBody)->makeRequest();
}
public function testCustomRestRetryMapAddsNewHandlers()
{
$this->setRetryMap(
array('403' => Runner::TASK_RETRY_ALWAYS)
);
$this->setRetryConfig(array('retries' => 5));
$result = $this->setNextResponses(2, 403)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @dataProvider customLimitsProvider
*/
public function testCustomRestRetryMapWithCustomLimits($limit)
{
$this->expectException(ServiceException::class);
$this->setRetryMap(
array('403' => $limit)
);
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponses($limit + 1, 403)->makeRequest();
}
/**
* @dataProvider timeoutProvider
*/
public function testRestTimeouts($config, $minTime)
{
$this->setRetryConfig($config);
$this->setNextResponses($config['retries'], 500)
->setNextResponse(200, '{"success": true}');
$this->assertTaskTimeGreaterThanOrEqual(
$minTime,
array($this, 'makeRequest'),
$config['initial_delay'] / 10
);
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testCurlRetryOffByDefault($errorCode, $errorMessage = '')
{
$this->expectException(ServiceException::class);
$this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest();
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testOneCurlRetryWithError($errorCode, $errorMessage = '')
{
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 1));
$this->setNextResponsesThrow(2, $errorMessage, $errorCode)->makeRequest();
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testMultipleCurlRetriesWithErrors(
$errorCode,
$errorMessage = ''
) {
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponsesThrow(6, $errorMessage, $errorCode)->makeRequest();
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testOneCurlRetryWithSuccess($errorCode, $errorMessage = '')
{
$this->setRetryConfig(array('retries' => 1));
$result = $this->setNextResponseThrows($errorMessage, $errorCode)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testMultipleCurlRetriesWithSuccess(
$errorCode,
$errorMessage = ''
) {
$this->setRetryConfig(array('retries' => 5));
$result = $this->setNextResponsesThrow(2, $errorMessage, $errorCode)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @requires extension curl
* @dataProvider defaultCurlErrorProvider
*/
public function testCustomCurlRetryMapReplacesDefaults(
$errorCode,
$errorMessage = ''
) {
$this->expectException(ServiceException::class);
$this->setRetryMap(array());
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponseThrows($errorMessage, $errorCode)->makeRequest();
}
/**
* @requires extension curl
*/
public function testCustomCurlRetryMapAddsNewHandlers()
{
$this->setRetryMap(
array(CURLE_COULDNT_RESOLVE_PROXY => Runner::TASK_RETRY_ALWAYS)
);
$this->setRetryConfig(array('retries' => 5));
$result = $this->setNextResponsesThrow(2, '', CURLE_COULDNT_RESOLVE_PROXY)
->setNextResponse(200, '{"success": true}')
->makeRequest();
$this->assertEquals('{"success": true}', (string) $result->getBody());
}
/**
* @requires extension curl
* @dataProvider customLimitsProvider
*/
public function testCustomCurlRetryMapWithCustomLimits($limit)
{
$this->expectException(ServiceException::class);
$this->setRetryMap(
array(CURLE_COULDNT_RESOLVE_PROXY => $limit)
);
$this->setRetryConfig(array('retries' => 5));
$this->setNextResponsesThrow($limit + 1, '', CURLE_COULDNT_RESOLVE_PROXY)
->makeRequest();
}
/**
* @requires extension curl
* @dataProvider timeoutProvider
*/
public function testCurlTimeouts($config, $minTime)
{
$this->setRetryConfig($config);
$this->setNextResponsesThrow($config['retries'], '', CURLE_GOT_NOTHING)
->setNextResponse(200, '{"success": true}');
$this->assertTaskTimeGreaterThanOrEqual(
$minTime,
array($this, 'makeRequest'),
$config['initial_delay'] / 10
);
}
/**
* @dataProvider badTaskConfigProvider
*/
public function testBadTaskConfig($config, $message)
{
$this->expectException(TaskException::class);
$this->expectExceptionMessage($message);
$this->setRetryConfig($config);
new Runner(
$this->retryConfig,
'',
array($this, 'testBadTaskConfig')
);
}
/**
* @expectedExceptionMessage must be a valid callable
*/
public function testBadTaskCallback()
{
$this->expectException(TaskException::class);
$config = [];
new Runner($config, '', 5);
}
public function testTaskRetryOffByDefault()
{
$this->expectException(ServiceException::class);
$this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS)
->runTask();
}
public function testOneTaskRetryWithError()
{
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 1));
$this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS)
->runTask();
}
public function testMultipleTaskRetriesWithErrors()
{
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 5));
$this->setNextTasksAllowedRetries(6, Runner::TASK_RETRY_ALWAYS)
->runTask();
}
public function testOneTaskRetryWithSuccess()
{
$this->setRetryConfig(array('retries' => 1));
$result = $this->setNextTaskAllowedRetries(Runner::TASK_RETRY_ALWAYS)
->setNextTaskReturnValue('success')
->runTask();
$this->assertEquals('success', $result);
}
public function testMultipleTaskRetriesWithSuccess()
{
$this->setRetryConfig(array('retries' => 5));
$result = $this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS)
->setNextTaskReturnValue('success')
->runTask();
$this->assertEquals('success', $result);
}
/**
* @dataProvider customLimitsProvider
*/
public function testTaskRetryWithCustomLimits($limit)
{
$this->expectException(ServiceException::class);
$this->setRetryConfig(array('retries' => 5));
$this->setNextTasksAllowedRetries($limit + 1, $limit)
->runTask();
}
/**
* @dataProvider timeoutProvider
*/
public function testTaskTimeouts($config, $minTime)
{
$this->setRetryConfig($config);
$this->setNextTasksAllowedRetries($config['retries'], $config['retries'] + 1)
->setNextTaskReturnValue('success');
$this->assertTaskTimeGreaterThanOrEqual(
$minTime,
array($this, 'runTask'),
$config['initial_delay'] / 10
);
}
public function testTaskWithManualRetries()
{
$this->setRetryConfig(array('retries' => 2));
$this->setNextTasksAllowedRetries(2, Runner::TASK_RETRY_ALWAYS);
$task = new Runner(
$this->retryConfig,
'',
array($this, 'runNextTask')
);
$this->assertTrue($task->canAttempt());
$this->assertTrue($task->attempt());
$this->assertTrue($task->canAttempt());
$this->assertTrue($task->attempt());
$this->assertTrue($task->canAttempt());
$this->assertTrue($task->attempt());
$this->assertFalse($task->canAttempt());
$this->assertFalse($task->attempt());
}
/**
* Provider for backoff configurations and expected minimum runtimes.
*
* @return array
*/
public function timeoutProvider()
{
$config = array('initial_delay' => .001, 'max_delay' => .01);
return array(
array(array_merge($config, array('retries' => 1)), .001),
array(array_merge($config, array('retries' => 2)), .0015),
array(array_merge($config, array('retries' => 3)), .00225),
array(array_merge($config, array('retries' => 4)), .00375),
array(array_merge($config, array('retries' => 5)), .005625)
);
}
/**
* Provider for custom retry limits.
*
* @return array
*/
public function customLimitsProvider()
{
return array(
array(Runner::TASK_RETRY_NEVER),
array(Runner::TASK_RETRY_ONCE),
);
}
/**
* Provider for invalid task configurations.
*
* @return array
*/
public function badTaskConfigProvider()
{
return array(
array(array('initial_delay' => -1), 'must not be negative'),
array(array('max_delay' => 0), 'must be greater than 0'),
array(array('factor' => 0), 'must be greater than 0'),
array(array('jitter' => 0), 'must be greater than 0'),
array(array('retries' => -1), 'must not be negative')
);
}
/**
* Provider for the default REST errors.
*
* @return array
*/
public function defaultRestErrorProvider()
{
return array(
array(500),
array(503),
array(403, '{"error":{"errors":[{"reason":"rateLimitExceeded"}]}}'),
array(403, '{"error":{"errors":[{"reason":"userRateLimitExceeded"}]}}'),
);
}
/**
* Provider for the default cURL errors.
*
* @return array
*/
public function defaultCurlErrorProvider()
{
return array(
array(6), // CURLE_COULDNT_RESOLVE_HOST
array(7), // CURLE_COULDNT_CONNECT
array(28), // CURLE_OPERATION_TIMEOUTED
array(35), // CURLE_SSL_CONNECT_ERROR
array(52), // CURLE_GOT_NOTHING
);
}
/**
* Assert the minimum amount of time required to run a task.
*
* NOTE: Intentionally crude for brevity.
*
* @param float $expected The expected minimum execution time
* @param callable $callback The task to time
* @param float $delta Allowable relative error
*
* @throws PHPUnit_Framework_ExpectationFailedException
*/
public static function assertTaskTimeGreaterThanOrEqual(
$expected,
$callback,
$delta = 0.0
) {
$time = microtime(true);
call_user_func($callback);
self::assertThat(
microtime(true) - $time,
self::logicalOr(
self::greaterThan($expected),
self::equalTo($expected, $delta)
)
);
}
/**
* Sets the task runner configurations.
*
* @param array $config The task runner configurations
*/
private function setRetryConfig(array $config)
{
$config += array(
'initial_delay' => .0001,
'max_delay' => .001,
'factor' => 2,
'jitter' => .5,
'retries' => 1
);
$this->retryConfig = $config;
}
private function setRetryMap(array $retryMap)
{
$this->retryMap = $retryMap;
}
/**
* Sets the next responses.
*
* @param integer $count The number of responses
* @param string $code The response code
* @param string $body The response body
* @param array $headers The response headers
*
* @return TaskTest
*/
private function setNextResponses(
$count,
$code = '200',
$body = '{}',
array $headers = array()
) {
while ($count-- > 0) {
$this->setNextResponse($code, $body, $headers);
}
return $this;
}
/**
* Sets the next response.
*
* @param string $code The response code
* @param string $body The response body
* @param array $headers The response headers
*
* @return TaskTest
*/
private function setNextResponse(
$code = '200',
$body = '{}',
array $headers = array()
) {
$this->mockedCalls[$this->mockedCallsCount++] = array(
'code' => (string) $code,
'headers' => $headers,
'body' => is_string($body) ? $body : json_encode($body)
);
return $this;
}
/**
* Forces the next responses to throw an IO exception.
*
* @param integer $count The number of responses
* @param string $message The exception messages
* @param string $code The exception code
*
* @return TaskTest
*/
private function setNextResponsesThrow($count, $message, $code)
{
while ($count-- > 0) {
$this->setNextResponseThrows($message, $code);
}
return $this;
}
/**
* Forces the next response to throw an IO exception.
*
* @param string $message The exception messages
* @param string $code The exception code
*
* @return TaskTest
*/
private function setNextResponseThrows($message, $code)
{
$this->mockedCalls[$this->mockedCallsCount++] = new ServiceException(
$message,
$code,
null,
array()
);
return $this;
}
/**
* Runs the defined request.
*
* @return array
*/
private function makeRequest()
{
$request = new Request('GET', '/test');
$http = $this->prophesize('GuzzleHttp\ClientInterface');
if ($this->isGuzzle5()) {
$http->createRequest(Argument::any(), Argument::any(), Argument::any())
->shouldBeCalledTimes($this->mockedCallsCount)
->willReturn(new \GuzzleHttp\Message\Request('GET', '/test'));
$http->send(Argument::type('GuzzleHttp\Message\Request'))
->shouldBeCalledTimes($this->mockedCallsCount)
->will([$this, 'getNextMockedCall']);
} else {
$http->send(Argument::type('Psr\Http\Message\RequestInterface'), [])
->shouldBeCalledTimes($this->mockedCallsCount)
->will([$this, 'getNextMockedCall']);
}
return REST::execute($http->reveal(), $request, '', $this->retryConfig, $this->retryMap);
}
/**
* Gets the next mocked response.
*
* @param GoogleRequest $request The mocked request
*
* @return GoogleRequest
*/
public function getNextMockedCall($request)
{
$current = $this->mockedCalls[$this->currentMockedCall++];
if ($current instanceof Exception) {
throw $current;
}
if ($this->isGuzzle5()) {
$stream = Guzzle5Stream::factory($current['body']);
$response = new Guzzle5Response($current['code'], $current['headers'], $stream);
} else {
$stream = Psr7\Utils::streamFor($current['body']);
$response = new Response($current['code'], $current['headers'], $stream);
}
return $response;
}
/**
* Sets the next task return value.
*
* @param mixed $value The next return value
*
* @return TaskTest
*/
private function setNextTaskReturnValue($value)
{
$this->mockedCalls[$this->mockedCallsCount++] = $value;
return $this;
}
/**
* Sets the next exception `allowedRetries()` return value.
*
* @param boolean $allowedRetries The next `allowedRetries()` return value.
*
* @return TaskTest
*/
private function setNextTaskAllowedRetries($allowedRetries)
{
$this->mockedCalls[$this->mockedCallsCount++] = $allowedRetries;
return $this;
}
/**
* Sets multiple exception `allowedRetries()` return value.
*
* @param integer $count The number of `allowedRetries()` return values.
* @param boolean $allowedRetries The `allowedRetries()` return value.
*
* @return TaskTest
*/
private function setNextTasksAllowedRetries($count, $allowedRetries)
{
while ($count-- > 0) {
$this->setNextTaskAllowedRetries($allowedRetries);
}
return $this;
}
/**
* Runs the defined task.
*
* @return mixed
*/
private function runTask()
{
$task = new Runner(
$this->retryConfig,
'',
array($this, 'runNextTask')
);
if (null !== $this->retryMap) {
$task->setRetryMap($this->retryMap);
}
$exception = $this->prophesize(ServiceException::class);
$exceptionCount = 0;
$exceptionCalls = array();
for ($i = 0; $i < $this->mockedCallsCount; $i++) {
if (is_int($this->mockedCalls[$i])) {
$exceptionCalls[$exceptionCount++] = $this->mockedCalls[$i];
$this->mockedCalls[$i] = $exception->reveal();
}
}
$task->setRetryMap($exceptionCalls);
return $task->run();
}
/**
* Gets the next task return value.
*
* @return mixed
*/
public function runNextTask()
{
$current = $this->mockedCalls[$this->currentMockedCall++];
if ($current instanceof Exception) {
throw $current;
}
return $current;
}
}

View file

@ -0,0 +1,306 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Utils;
use Google\Tests\BaseTest;
use Google\Utils\UriTemplate;
class UriTemplateTest extends BaseTest
{
public function testLevelOne()
{
$var = "value";
$hello = "Hello World!";
$urit = new UriTemplate();
$this->assertEquals(
"value",
$urit->parse("{var}", array("var" => $var))
);
$this->assertEquals(
"Hello%20World%21",
$urit->parse("{hello}", array("hello" => $hello))
);
}
public function testLevelTwo()
{
$var = "value";
$hello = "Hello World!";
$path = "/foo/bar";
$urit = new UriTemplate();
$this->assertEquals(
"value",
$urit->parse("{+var}", array("var" => $var))
);
$this->assertEquals(
"Hello%20World!",
$urit->parse("{+hello}", array("hello" => $hello))
);
$this->assertEquals(
"/foo/bar/here",
$urit->parse("{+path}/here", array("path" => $path))
);
$this->assertEquals(
"here?ref=/foo/bar",
$urit->parse("here?ref={+path}", array("path" => $path))
);
$this->assertEquals(
"X#value",
$urit->parse("X{#var}", array("var" => $var))
);
$this->assertEquals(
"X#Hello%20World!",
$urit->parse("X{#hello}", array("hello" => $hello))
);
}
public function testLevelThree()
{
$var = "value";
$hello = "Hello World!";
$empty = '';
$path = "/foo/bar";
$x = "1024";
$y = "768";
$urit = new UriTemplate();
$this->assertEquals(
"map?1024,768",
$urit->parse("map?{x,y}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
"1024,Hello%20World%21,768",
$urit->parse("{x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
);
$this->assertEquals(
"1024,Hello%20World!,768",
$urit->parse("{+x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
);
$this->assertEquals(
"/foo/bar,1024/here",
$urit->parse("{+path,x}/here", array("x" => $x, "path" => $path))
);
$this->assertEquals(
"#1024,Hello%20World!,768",
$urit->parse("{#x,hello,y}", array("x" => $x, "y" => $y, "hello" => $hello))
);
$this->assertEquals(
"#/foo/bar,1024/here",
$urit->parse("{#path,x}/here", array("x" => $x, "path" => $path))
);
$this->assertEquals(
"X.value",
$urit->parse("X{.var}", array("var" => $var))
);
$this->assertEquals(
"X.1024.768",
$urit->parse("X{.x,y}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
"X.value",
$urit->parse("X{.var}", array("var" => $var))
);
$this->assertEquals(
"X.1024.768",
$urit->parse("X{.x,y}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
"/value",
$urit->parse("{/var}", array("var" => $var))
);
$this->assertEquals(
"/value/1024/here",
$urit->parse("{/var,x}/here", array("x" => $x, "var" => $var))
);
$this->assertEquals(
";x=1024;y=768",
$urit->parse("{;x,y}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
";x=1024;y=768;empty",
$urit->parse("{;x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
);
$this->assertEquals(
"?x=1024&y=768",
$urit->parse("{?x,y}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
"?x=1024&y=768&empty=",
$urit->parse("{?x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
);
$this->assertEquals(
"?fixed=yes&x=1024",
$urit->parse("?fixed=yes{&x}", array("x" => $x, "y" => $y))
);
$this->assertEquals(
"&x=1024&y=768&empty=",
$urit->parse("{&x,y,empty}", array("x" => $x, "y" => $y, "empty" => $empty))
);
}
public function testLevelFour()
{
$values = array(
'var' => "value",
'hello' => "Hello World!",
'path' => "/foo/bar",
'list' => array("red", "green", "blue"),
'keys' => array("semi" => ";", "dot" => ".", "comma" => ","),
);
$tests = array(
"{var:3}" => "val",
"{var:30}" => "value",
"{list}" => "red,green,blue",
"{list*}" => "red,green,blue",
"{keys}" => "semi,%3B,dot,.,comma,%2C",
"{keys*}" => "semi=%3B,dot=.,comma=%2C",
"{+path:6}/here" => "/foo/b/here",
"{+list}" => "red,green,blue",
"{+list*}" => "red,green,blue",
"{+keys}" => "semi,;,dot,.,comma,,",
"{+keys*}" => "semi=;,dot=.,comma=,",
"{#path:6}/here" => "#/foo/b/here",
"{#list}" => "#red,green,blue",
"{#list*}" => "#red,green,blue",
"{#keys}" => "#semi,;,dot,.,comma,,",
"{#keys*}" => "#semi=;,dot=.,comma=,",
"X{.var:3}" => "X.val",
"X{.list}" => "X.red,green,blue",
"X{.list*}" => "X.red.green.blue",
"X{.keys}" => "X.semi,%3B,dot,.,comma,%2C",
"X{.keys*}" => "X.semi=%3B.dot=..comma=%2C",
"{/var:1,var}" => "/v/value",
"{/list}" => "/red,green,blue",
"{/list*}" => "/red/green/blue",
"{/list*,path:4}" => "/red/green/blue/%2Ffoo",
"{/keys}" => "/semi,%3B,dot,.,comma,%2C",
"{/keys*}" => "/semi=%3B/dot=./comma=%2C",
"{;hello:5}" => ";hello=Hello",
"{;list}" => ";list=red,green,blue",
"{;list*}" => ";list=red;list=green;list=blue",
"{;keys}" => ";keys=semi,%3B,dot,.,comma,%2C",
"{;keys*}" => ";semi=%3B;dot=.;comma=%2C",
"{?var:3}" => "?var=val",
"{?list}" => "?list=red,green,blue",
"{?list*}" => "?list=red&list=green&list=blue",
"{?keys}" => "?keys=semi,%3B,dot,.,comma,%2C",
"{?keys*}" => "?semi=%3B&dot=.&comma=%2C",
"{&var:3}" => "&var=val",
"{&list}" => "&list=red,green,blue",
"{&list*}" => "&list=red&list=green&list=blue",
"{&keys}" => "&keys=semi,%3B,dot,.,comma,%2C",
"{&keys*}" => "&semi=%3B&dot=.&comma=%2C",
"find{?list*}" => "find?list=red&list=green&list=blue",
"www{.list*}" => "www.red.green.blue"
);
$urit = new UriTemplate();
foreach ($tests as $input => $output) {
$this->assertEquals($output, $urit->parse($input, $values), $input . " failed");
}
}
public function testMultipleAnnotations()
{
$var = "value";
$hello = "Hello World!";
$urit = new UriTemplate();
$this->assertEquals(
"http://www.google.com/Hello%20World!?var=value",
$urit->parse(
"http://www.google.com/{+hello}{?var}",
array("var" => $var, "hello" => $hello)
)
);
$params = array(
"playerId" => "me",
"leaderboardId" => "CgkIhcG1jYEbEAIQAw",
"timeSpan" => "ALL_TIME",
"other" => "irrelevant"
);
$this->assertEquals(
"players/me/leaderboards/CgkIhcG1jYEbEAIQAw/scores/ALL_TIME",
$urit->parse(
"players/{playerId}/leaderboards/{leaderboardId}/scores/{timeSpan}",
$params
)
);
}
/**
* This test test against the JSON files defined in
* https://github.com/uri-templates/uritemplate-test
*
* We don't ship these tests with it, so they'll just silently
* skip unless provided - this is mainly for use when
* making specific URI template changes and wanting
* to do a full regression check.
*/
public function testAgainstStandardTests()
{
$location = __DIR__ . "/../../uritemplate-test/*.json";
$files = glob($location);
if (!$files) {
$this->markTestSkipped('No JSON files provided');
}
$urit = new UriTemplate();
foreach ($files as $file) {
$test = json_decode(file_get_contents($file), true);
foreach ($test as $title => $testsets) {
foreach ($testsets['testcases'] as $cases) {
$input = $cases[0];
$output = $cases[1];
if ($output == false) {
continue; // skipping negative tests for now
} else if (is_array($output)) {
$response = $urit->parse($input, $testsets['variables']);
$this->assertContains(
$response,
$output,
$input . " failed from " . $title
);
} else {
$this->assertEquals(
$output,
$urit->parse($input, $testsets['variables']),
$input . " failed."
);
}
}
}
}
}
}

2
vendor/google/apiclient/tests/README vendored Normal file
View file

@ -0,0 +1,2 @@
These tests depend on PHPUnit, see
http://www.phpunit.de/manual/current/en/installation.html for more instructions

View file

@ -0,0 +1,21 @@
<?php
/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/BaseTest.php';
date_default_timezone_set('UTC');

View file

@ -0,0 +1,9 @@
<?php
include_once __DIR__ . '/bootstrap.php';
$test = new BaseTest();
$cacheItem = $test->getCache()->getItem('access_token');
print_r($cacheItem->get());
$test->getCache()->deleteItem('access_token');
echo "SUCCESS\n";

View file

@ -0,0 +1,6 @@
; Test.ini file
application_name = My Test application
auth_class = Google_Auth_OAuth2
client_id = 12345.apps.googleusercontent.com
client_secret = gjfiwnGinpena3
redirect_uri = http://example.com

View file

@ -0,0 +1,39 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class batchTest extends BaseTest
{
public function testBatch()
{
$this->checkKey();
$crawler = $this->loadExample('batch.php');
$nodes = $crawler->filter('br');
$this->assertCount(20, $nodes);
$this->assertContains('Walden', $crawler->text());
$this->assertContains('George Bernard Shaw', $crawler->text());
}
}

View file

@ -0,0 +1,42 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class idTokenTest extends BaseTest
{
public function testIdToken()
{
$this->checkServiceAccountCredentials();
$crawler = $this->loadExample('idtoken.php');
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('Retrieving An Id Token', $nodes->first()->text());
$nodes = $crawler->filter('a.login');
$this->assertCount(1, $nodes);
$this->assertEquals('Connect Me!', $nodes->first()->text());
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class indexTest extends BaseTest
{
public function testIndex()
{
$crawler = $this->loadExample('index.php');
$nodes = $crawler->filter('li');
$this->assertCount(8, $nodes);
$this->assertEquals('A query using simple API access', $nodes->first()->text());
}
}

View file

@ -0,0 +1,59 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class largeFileDownloadTest extends BaseTest
{
/**
* @runInSeparateProcess
*/
public function testSimpleFileDownloadNoToken()
{
$this->checkServiceAccountCredentials();
$crawler = $this->loadExample('large-file-download.php');
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('File Download - Downloading a large file', $nodes->first()->text());
$nodes = $crawler->filter('a.login');
$this->assertCount(1, $nodes);
$this->assertEquals('Connect Me!', $nodes->first()->text());
}
public function testSimpleFileDownloadWithToken()
{
$this->checkToken();
global $_SESSION;
$_SESSION['upload_token'] = $this->getClient()->getAccessToken();
$crawler = $this->loadExample('large-file-download.php');
$buttonText = 'Click here to download a large (20MB) test file';
$nodes = $crawler->filter('input');
$this->assertCount(1, $nodes);
$this->assertEquals($buttonText, $nodes->first()->attr('value'));
}
}

View file

@ -0,0 +1,45 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class largeFileUploadTest extends BaseTest
{
/**
* @runInSeparateProcess
*/
public function testLargeFileUpload()
{
$this->checkServiceAccountCredentials();
$crawler = $this->loadExample('large-file-upload.php');
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('File Upload - Uploading a large file', $nodes->first()->text());
$nodes = $crawler->filter('a.login');
$this->assertCount(1, $nodes);
$this->assertEquals('Connect Me!', $nodes->first()->text());
}
}

View file

@ -0,0 +1,38 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class multiApiTest extends BaseTest
{
public function testMultiApi()
{
$this->checkKey();
$crawler = $this->loadExample('multi-api.php');
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('User Query - Multiple APIs', $nodes->first()->text());
}
}

View file

@ -0,0 +1,38 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class serviceAccountTest extends BaseTest
{
public function testServiceAccount()
{
$this->checkServiceAccountCredentials();
$crawler = $this->loadExample('service-account.php');
$nodes = $crawler->filter('br');
$this->assertCount(10, $nodes);
$this->assertContains('Walden', $crawler->text());
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class simpleFileUploadTest extends BaseTest
{
/**
* @runInSeparateProcess
*/
public function testSimpleFileUploadNoToken()
{
$this->checkServiceAccountCredentials();
$crawler = $this->loadExample('simple-file-upload.php');
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('File Upload - Uploading a simple file', $nodes->first()->text());
$nodes = $crawler->filter('a.login');
$this->assertCount(1, $nodes);
$this->assertEquals('Connect Me!', $nodes->first()->text());
}
public function testSimpleFileUploadWithToken()
{
$this->checkToken();
global $_SESSION;
$_SESSION['upload_token'] = $this->getClient()->getAccessToken();
$crawler = $this->loadExample('simple-file-upload.php');
$buttonText = 'Click here to upload two small (1MB) test files';
$nodes = $crawler->filter('input');
$this->assertCount(1, $nodes);
$this->assertEquals($buttonText, $nodes->first()->attr('value'));
}
}

View file

@ -0,0 +1,41 @@
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
namespace Google\Tests\Examples;
use Google\Tests\BaseTest;
class simpleQueryTest extends BaseTest
{
public function testSimpleQuery()
{
$this->checkKey();
$crawler = $this->loadExample('simple-query.php');
$nodes = $crawler->filter('br');
$this->assertCount(20, $nodes);
$nodes = $crawler->filter('h1');
$this->assertCount(1, $nodes);
$this->assertEquals('Simple API Access', $nodes->first()->text());
}
}

@ -1 +0,0 @@
Subproject commit c6370fe2c0a445aedc08f592a6a3149da1fea4c7

View file

@ -0,0 +1,175 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Amp\CancelledException;
use Amp\Http\Client\DelegateHttpClient;
use Amp\Http\Client\InterceptedHttpClient;
use Amp\Http\Client\PooledHttpClient;
use Amp\Http\Client\Request;
use Amp\Http\Tunnel\Http1TunnelConnector;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\AmpClientState;
use Symfony\Component\HttpClient\Response\AmpResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;
if (!interface_exists(DelegateHttpClient::class)) {
throw new \LogicException('You cannot use "Symfony\Component\HttpClient\AmpHttpClient" as the "amphp/http-client" package is not installed. Try running "composer require amphp/http-client".');
}
/**
* A portable implementation of the HttpClientInterface contracts based on Amp's HTTP client.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class AmpHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use HttpClientTrait;
use LoggerAwareTrait;
private $defaultOptions = self::OPTIONS_DEFAULTS;
/** @var AmpClientState */
private $multi;
/**
* @param array $defaultOptions Default requests' options
* @param callable $clientConfigurator A callable that builds a {@see DelegateHttpClient} from a {@see PooledHttpClient};
* passing null builds an {@see InterceptedHttpClient} with 2 retries on failures
* @param int $maxHostConnections The maximum number of connections to a single host
* @param int $maxPendingPushes The maximum number of pushed responses to accept in the queue
*
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
*/
public function __construct(array $defaultOptions = [], callable $clientConfigurator = null, int $maxHostConnections = 6, int $maxPendingPushes = 50)
{
$this->defaultOptions['buffer'] = $this->defaultOptions['buffer'] ?? \Closure::fromCallable([__CLASS__, 'shouldBuffer']);
if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
$this->multi = new AmpClientState($clientConfigurator, $maxHostConnections, $maxPendingPushes, $this->logger);
}
/**
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
*
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
$options['proxy'] = self::getProxy($options['proxy'], $url, $options['no_proxy']);
if (null !== $options['proxy'] && !class_exists(Http1TunnelConnector::class)) {
throw new \LogicException('You cannot use the "proxy" option as the "amphp/http-tunnel" package is not installed. Try running "composer require amphp/http-tunnel".');
}
if ($options['bindto']) {
if (0 === strpos($options['bindto'], 'if!')) {
throw new TransportException(__CLASS__.' cannot bind to network interfaces, use e.g. CurlHttpClient instead.');
}
if (0 === strpos($options['bindto'], 'host!')) {
$options['bindto'] = substr($options['bindto'], 5);
}
}
if ('' !== $options['body'] && 'POST' === $method && !isset($options['normalized_headers']['content-type'])) {
$options['headers'][] = 'Content-Type: application/x-www-form-urlencoded';
}
if (!isset($options['normalized_headers']['user-agent'])) {
$options['headers'][] = 'User-Agent: Symfony HttpClient/Amp';
}
if (0 < $options['max_duration']) {
$options['timeout'] = min($options['max_duration'], $options['timeout']);
}
if ($options['resolve']) {
$this->multi->dnsCache = $options['resolve'] + $this->multi->dnsCache;
}
if ($options['peer_fingerprint'] && !isset($options['peer_fingerprint']['pin-sha256'])) {
throw new TransportException(__CLASS__.' supports only "pin-sha256" fingerprints.');
}
$request = new Request(implode('', $url), $method);
if ($options['http_version']) {
switch ((float) $options['http_version']) {
case 1.0: $request->setProtocolVersions(['1.0']); break;
case 1.1: $request->setProtocolVersions(['1.1', '1.0']); break;
default: $request->setProtocolVersions(['2', '1.1', '1.0']); break;
}
}
foreach ($options['headers'] as $v) {
$h = explode(': ', $v, 2);
$request->addHeader($h[0], $h[1]);
}
$request->setTcpConnectTimeout(1000 * $options['timeout']);
$request->setTlsHandshakeTimeout(1000 * $options['timeout']);
$request->setTransferTimeout(1000 * $options['max_duration']);
if (method_exists($request, 'setInactivityTimeout')) {
$request->setInactivityTimeout(0);
}
if ('' !== $request->getUri()->getUserInfo() && !$request->hasHeader('authorization')) {
$auth = explode(':', $request->getUri()->getUserInfo(), 2);
$auth = array_map('rawurldecode', $auth) + [1 => ''];
$request->setHeader('Authorization', 'Basic '.base64_encode(implode(':', $auth)));
}
return new AmpResponse($this->multi, $request, $options, $this->logger);
}
/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof AmpResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of AmpResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}
return new ResponseStream(AmpResponse::stream($responses, $timeout));
}
public function reset()
{
$this->multi->dnsCache = [];
foreach ($this->multi->pushedResponses as $authority => $pushedResponses) {
foreach ($pushedResponses as [$pushedUrl, $pushDeferred]) {
$pushDeferred->fail(new CancelledException());
if ($this->logger) {
$this->logger->debug(sprintf('Unused pushed response: "%s"', $pushedUrl));
}
}
}
$this->multi->pushedResponses = [];
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Symfony\Component\HttpClient\Response\AsyncResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
/**
* Eases with processing responses while streaming them.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait AsyncDecoratorTrait
{
use DecoratorTrait;
/**
* {@inheritdoc}
*
* @return AsyncResponse
*/
abstract public function request(string $method, string $url, array $options = []): ResponseInterface;
/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof AsyncResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of AsyncResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}
return new ResponseStream(AsyncResponse::stream($responses, $timeout, static::class));
}
}

49
vendor/symfony/http-client/CHANGELOG.md vendored Normal file
View file

@ -0,0 +1,49 @@
CHANGELOG
=========
5.3
---
* Implement `HttpClientInterface::withOptions()` from `symfony/contracts` v2.4
* Add `DecoratorTrait` to ease writing simple decorators
5.2.0
-----
* added `AsyncDecoratorTrait` to ease processing responses without breaking async
* added support for pausing responses with a new `pause_handler` callable exposed as an info item
* added `StreamableInterface` to ease turning responses into PHP streams
* added `MockResponse::getRequestMethod()` and `getRequestUrl()` to allow inspecting which request has been sent
* added `EventSourceHttpClient` a Server-Sent events stream implementing the [EventSource specification](https://www.w3.org/TR/eventsource/#eventsource)
* added option "extra.curl" to allow setting additional curl options in `CurlHttpClient`
* added `RetryableHttpClient` to automatically retry failed HTTP requests.
* added `extra.trace_content` option to `TraceableHttpClient` to prevent it from keeping the content in memory
5.1.0
-----
* added `NoPrivateNetworkHttpClient` decorator
* added `AmpHttpClient`, a portable HTTP/2 implementation based on Amp
* added `LoggerAwareInterface` to `ScopingHttpClient` and `TraceableHttpClient`
* made `HttpClient::create()` return an `AmpHttpClient` when `amphp/http-client` is found but curl is not or too old
4.4.0
-----
* added `canceled` to `ResponseInterface::getInfo()`
* added `HttpClient::createForBaseUri()`
* added `HttplugClient` with support for sync and async requests
* added `max_duration` option
* added support for NTLM authentication
* added `StreamWrapper` to cast any `ResponseInterface` instances to PHP streams.
* added `$response->toStream()` to cast responses to regular PHP streams
* made `Psr18Client` implement relevant PSR-17 factories and have streaming responses
* added `TraceableHttpClient`, `HttpClientDataCollector` and `HttpClientPass` to integrate with the web profiler
* allow enabling buffering conditionally with a Closure
* allow option "buffer" to be a stream resource
* allow arbitrary values for the "json" option
4.3.0
-----
* added the component

View file

@ -0,0 +1,144 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpKernel\HttpCache\StoreInterface;
use Symfony\Component\HttpKernel\HttpClientKernel;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
/**
* Adds caching on top of an HTTP client.
*
* The implementation buffers responses in memory and doesn't stream directly from the network.
* You can disable/enable this layer by setting option "no_cache" under "extra" to true/false.
* By default, caching is enabled unless the "buffer" option is set to false.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CachingHttpClient implements HttpClientInterface
{
use HttpClientTrait;
private $client;
private $cache;
private $defaultOptions = self::OPTIONS_DEFAULTS;
public function __construct(HttpClientInterface $client, StoreInterface $store, array $defaultOptions = [])
{
if (!class_exists(HttpClientKernel::class)) {
throw new \LogicException(sprintf('Using "%s" requires that the HttpKernel component version 4.3 or higher is installed, try running "composer require symfony/http-kernel:^4.3".', __CLASS__));
}
$this->client = $client;
$kernel = new HttpClientKernel($client);
$this->cache = new HttpCache($kernel, $store, null, $defaultOptions);
unset($defaultOptions['debug']);
unset($defaultOptions['default_ttl']);
unset($defaultOptions['private_headers']);
unset($defaultOptions['allow_reload']);
unset($defaultOptions['allow_revalidate']);
unset($defaultOptions['stale_while_revalidate']);
unset($defaultOptions['stale_if_error']);
unset($defaultOptions['trace_level']);
unset($defaultOptions['trace_header']);
if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
}
/**
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = $this->prepareRequest($method, $url, $options, $this->defaultOptions, true);
$url = implode('', $url);
if (!empty($options['body']) || !empty($options['extra']['no_cache']) || !\in_array($method, ['GET', 'HEAD', 'OPTIONS'])) {
return $this->client->request($method, $url, $options);
}
$request = Request::create($url, $method);
$request->attributes->set('http_client_options', $options);
foreach ($options['normalized_headers'] as $name => $values) {
if ('cookie' !== $name) {
foreach ($values as $value) {
$request->headers->set($name, substr($value, 2 + \strlen($name)), false);
}
continue;
}
foreach ($values as $cookies) {
foreach (explode('; ', substr($cookies, \strlen('Cookie: '))) as $cookie) {
if ('' !== $cookie) {
$cookie = explode('=', $cookie, 2);
$request->cookies->set($cookie[0], $cookie[1] ?? '');
}
}
}
}
$response = $this->cache->handle($request);
$response = new MockResponse($response->getContent(), [
'http_code' => $response->getStatusCode(),
'response_headers' => $response->headers->allPreserveCase(),
]);
return MockResponse::fromRequest($method, $url, $options, $response);
}
/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof ResponseInterface) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of ResponseInterface objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}
$mockResponses = [];
$clientResponses = [];
foreach ($responses as $response) {
if ($response instanceof MockResponse) {
$mockResponses[] = $response;
} else {
$clientResponses[] = $response;
}
}
if (!$mockResponses) {
return $this->client->stream($clientResponses, $timeout);
}
if (!$clientResponses) {
return new ResponseStream(MockResponse::stream($mockResponses, $timeout));
}
return new ResponseStream((function () use ($mockResponses, $clientResponses, $timeout) {
yield from MockResponse::stream($mockResponses, $timeout);
yield $this->client->stream($clientResponses, $timeout);
})());
}
}

View file

@ -0,0 +1,87 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
use Symfony\Contracts\HttpClient\ChunkInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class DataChunk implements ChunkInterface
{
private $offset = 0;
private $content = '';
public function __construct(int $offset = 0, string $content = '')
{
$this->offset = $offset;
$this->content = $content;
}
/**
* {@inheritdoc}
*/
public function isTimeout(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function isFirst(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function isLast(): bool
{
return false;
}
/**
* {@inheritdoc}
*/
public function getInformationalStatus(): ?array
{
return null;
}
/**
* {@inheritdoc}
*/
public function getContent(): string
{
return $this->content;
}
/**
* {@inheritdoc}
*/
public function getOffset(): int
{
return $this->offset;
}
/**
* {@inheritdoc}
*/
public function getError(): ?string
{
return null;
}
}

View file

@ -0,0 +1,143 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
use Symfony\Component\HttpClient\Exception\TimeoutException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Contracts\HttpClient\ChunkInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class ErrorChunk implements ChunkInterface
{
private $didThrow = false;
private $offset;
private $errorMessage;
private $error;
/**
* @param \Throwable|string $error
*/
public function __construct(int $offset, $error)
{
$this->offset = $offset;
if (\is_string($error)) {
$this->errorMessage = $error;
} else {
$this->error = $error;
$this->errorMessage = $error->getMessage();
}
}
/**
* {@inheritdoc}
*/
public function isTimeout(): bool
{
$this->didThrow = true;
if (null !== $this->error) {
throw new TransportException($this->errorMessage, 0, $this->error);
}
return true;
}
/**
* {@inheritdoc}
*/
public function isFirst(): bool
{
$this->didThrow = true;
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
}
/**
* {@inheritdoc}
*/
public function isLast(): bool
{
$this->didThrow = true;
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
}
/**
* {@inheritdoc}
*/
public function getInformationalStatus(): ?array
{
$this->didThrow = true;
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
}
/**
* {@inheritdoc}
*/
public function getContent(): string
{
$this->didThrow = true;
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
}
/**
* {@inheritdoc}
*/
public function getOffset(): int
{
return $this->offset;
}
/**
* {@inheritdoc}
*/
public function getError(): ?string
{
return $this->errorMessage;
}
/**
* @return bool Whether the wrapped error has been thrown or not
*/
public function didThrow(bool $didThrow = null): bool
{
if (null !== $didThrow && $this->didThrow !== $didThrow) {
return !$this->didThrow = $didThrow;
}
return $this->didThrow;
}
/**
* @return array
*/
public function __sleep()
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
}
public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
public function __destruct()
{
if (!$this->didThrow) {
$this->didThrow = true;
throw null !== $this->error ? new TransportException($this->errorMessage, 0, $this->error) : new TimeoutException($this->errorMessage);
}
}
}

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class FirstChunk extends DataChunk
{
/**
* {@inheritdoc}
*/
public function isFirst(): bool
{
return true;
}
}

View file

@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class InformationalChunk extends DataChunk
{
private $status;
public function __construct(int $statusCode, array $headers)
{
$this->status = [$statusCode, $headers];
}
/**
* {@inheritdoc}
*/
public function getInformationalStatus(): ?array
{
return $this->status;
}
}

View file

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
/**
* @author Nicolas Grekas <p@tchwork.com>
*
* @internal
*/
class LastChunk extends DataChunk
{
/**
* {@inheritdoc}
*/
public function isLast(): bool
{
return true;
}
}

View file

@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\Chunk;
use Symfony\Contracts\HttpClient\ChunkInterface;
/**
* @author Antoine Bluchet <soyuka@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
final class ServerSentEvent extends DataChunk implements ChunkInterface
{
private $data = '';
private $id = '';
private $type = 'message';
private $retry = 0;
public function __construct(string $content)
{
parent::__construct(-1, $content);
// remove BOM
if (0 === strpos($content, "\xEF\xBB\xBF")) {
$content = substr($content, 3);
}
foreach (preg_split("/(?:\r\n|[\r\n])/", $content) as $line) {
if (0 === $i = strpos($line, ':')) {
continue;
}
$i = false === $i ? \strlen($line) : $i;
$field = substr($line, 0, $i);
$i += 1 + (' ' === ($line[1 + $i] ?? ''));
switch ($field) {
case 'id': $this->id = substr($line, $i); break;
case 'event': $this->type = substr($line, $i); break;
case 'data': $this->data .= ('' === $this->data ? '' : "\n").substr($line, $i); break;
case 'retry':
$retry = substr($line, $i);
if ('' !== $retry && \strlen($retry) === strspn($retry, '0123456789')) {
$this->retry = $retry / 1000.0;
}
break;
}
}
}
public function getId(): string
{
return $this->id;
}
public function getType(): string
{
return $this->type;
}
public function getData(): string
{
return $this->data;
}
public function getRetry(): float
{
return $this->retry;
}
}

View file

@ -0,0 +1,599 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\HttpClient\Exception\InvalidArgumentException;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\Internal\CurlClientState;
use Symfony\Component\HttpClient\Internal\PushedResponse;
use Symfony\Component\HttpClient\Response\CurlResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
use Symfony\Contracts\Service\ResetInterface;
/**
* A performant implementation of the HttpClientInterface contracts based on the curl extension.
*
* This provides fully concurrent HTTP requests, with transparent
* HTTP/2 push when a curl version that supports it is installed.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class CurlHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use HttpClientTrait;
use LoggerAwareTrait;
private $defaultOptions = self::OPTIONS_DEFAULTS + [
'auth_ntlm' => null, // array|string - an array containing the username as first value, and optionally the
// password as the second one; or string like username:password - enabling NTLM auth
'extra' => [
'curl' => [], // A list of extra curl options indexed by their corresponding CURLOPT_*
],
];
/**
* An internal object to share state between the client and its responses.
*
* @var CurlClientState
*/
private $multi;
private static $curlVersion;
/**
* @param array $defaultOptions Default request's options
* @param int $maxHostConnections The maximum number of connections to a single host
* @param int $maxPendingPushes The maximum number of pushed responses to accept in the queue
*
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
*/
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6, int $maxPendingPushes = 50)
{
if (!\extension_loaded('curl')) {
throw new \LogicException('You cannot use the "Symfony\Component\HttpClient\CurlHttpClient" as the "curl" extension is not installed.');
}
$this->defaultOptions['buffer'] = $this->defaultOptions['buffer'] ?? \Closure::fromCallable([__CLASS__, 'shouldBuffer']);
if ($defaultOptions) {
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, $this->defaultOptions);
}
$this->multi = new CurlClientState();
self::$curlVersion = self::$curlVersion ?? curl_version();
// Don't enable HTTP/1.1 pipelining: it forces responses to be sent in order
if (\defined('CURLPIPE_MULTIPLEX')) {
curl_multi_setopt($this->multi->handle, \CURLMOPT_PIPELINING, \CURLPIPE_MULTIPLEX);
}
if (\defined('CURLMOPT_MAX_HOST_CONNECTIONS')) {
$maxHostConnections = curl_multi_setopt($this->multi->handle, \CURLMOPT_MAX_HOST_CONNECTIONS, 0 < $maxHostConnections ? $maxHostConnections : \PHP_INT_MAX) ? 0 : $maxHostConnections;
}
if (\defined('CURLMOPT_MAXCONNECTS') && 0 < $maxHostConnections) {
curl_multi_setopt($this->multi->handle, \CURLMOPT_MAXCONNECTS, $maxHostConnections);
}
// Skip configuring HTTP/2 push when it's unsupported or buggy, see https://bugs.php.net/77535
if (0 >= $maxPendingPushes || \PHP_VERSION_ID < 70217 || (\PHP_VERSION_ID >= 70300 && \PHP_VERSION_ID < 70304)) {
return;
}
// HTTP/2 push crashes before curl 7.61
if (!\defined('CURLMOPT_PUSHFUNCTION') || 0x073d00 > self::$curlVersion['version_number'] || !(\CURL_VERSION_HTTP2 & self::$curlVersion['features'])) {
return;
}
curl_multi_setopt($this->multi->handle, \CURLMOPT_PUSHFUNCTION, function ($parent, $pushed, array $requestHeaders) use ($maxPendingPushes) {
return $this->handlePush($parent, $pushed, $requestHeaders, $maxPendingPushes);
});
}
/**
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
*
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
$scheme = $url['scheme'];
$authority = $url['authority'];
$host = parse_url($authority, \PHP_URL_HOST);
$url = implode('', $url);
if (!isset($options['normalized_headers']['user-agent'])) {
$options['headers'][] = 'User-Agent: Symfony HttpClient/Curl';
}
$curlopts = [
\CURLOPT_URL => $url,
\CURLOPT_TCP_NODELAY => true,
\CURLOPT_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
\CURLOPT_REDIR_PROTOCOLS => \CURLPROTO_HTTP | \CURLPROTO_HTTPS,
\CURLOPT_FOLLOWLOCATION => true,
\CURLOPT_MAXREDIRS => 0 < $options['max_redirects'] ? $options['max_redirects'] : 0,
\CURLOPT_COOKIEFILE => '', // Keep track of cookies during redirects
\CURLOPT_TIMEOUT => 0,
\CURLOPT_PROXY => $options['proxy'],
\CURLOPT_NOPROXY => $options['no_proxy'] ?? $_SERVER['no_proxy'] ?? $_SERVER['NO_PROXY'] ?? '',
\CURLOPT_SSL_VERIFYPEER => $options['verify_peer'],
\CURLOPT_SSL_VERIFYHOST => $options['verify_host'] ? 2 : 0,
\CURLOPT_CAINFO => $options['cafile'],
\CURLOPT_CAPATH => $options['capath'],
\CURLOPT_SSL_CIPHER_LIST => $options['ciphers'],
\CURLOPT_SSLCERT => $options['local_cert'],
\CURLOPT_SSLKEY => $options['local_pk'],
\CURLOPT_KEYPASSWD => $options['passphrase'],
\CURLOPT_CERTINFO => $options['capture_peer_cert_chain'],
];
if (1.0 === (float) $options['http_version']) {
$curlopts[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_0;
} elseif (1.1 === (float) $options['http_version']) {
$curlopts[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1;
} elseif (\defined('CURL_VERSION_HTTP2') && (\CURL_VERSION_HTTP2 & self::$curlVersion['features']) && ('https:' === $scheme || 2.0 === (float) $options['http_version'])) {
$curlopts[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_2_0;
}
if (isset($options['auth_ntlm'])) {
$curlopts[\CURLOPT_HTTPAUTH] = \CURLAUTH_NTLM;
$curlopts[\CURLOPT_HTTP_VERSION] = \CURL_HTTP_VERSION_1_1;
if (\is_array($options['auth_ntlm'])) {
$count = \count($options['auth_ntlm']);
if ($count <= 0 || $count > 2) {
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must contain 1 or 2 elements, %d given.', $count));
}
$options['auth_ntlm'] = implode(':', $options['auth_ntlm']);
}
if (!\is_string($options['auth_ntlm'])) {
throw new InvalidArgumentException(sprintf('Option "auth_ntlm" must be a string or an array, "%s" given.', get_debug_type($options['auth_ntlm'])));
}
$curlopts[\CURLOPT_USERPWD] = $options['auth_ntlm'];
}
if (!\ZEND_THREAD_SAFE) {
$curlopts[\CURLOPT_DNS_USE_GLOBAL_CACHE] = false;
}
if (\defined('CURLOPT_HEADEROPT') && \defined('CURLHEADER_SEPARATE')) {
$curlopts[\CURLOPT_HEADEROPT] = \CURLHEADER_SEPARATE;
}
// curl's resolve feature varies by host:port but ours varies by host only, let's handle this with our own DNS map
if (isset($this->multi->dnsCache->hostnames[$host])) {
$options['resolve'] += [$host => $this->multi->dnsCache->hostnames[$host]];
}
if ($options['resolve'] || $this->multi->dnsCache->evictions) {
// First reset any old DNS cache entries then add the new ones
$resolve = $this->multi->dnsCache->evictions;
$this->multi->dnsCache->evictions = [];
$port = parse_url($authority, \PHP_URL_PORT) ?: ('http:' === $scheme ? 80 : 443);
if ($resolve && 0x072a00 > self::$curlVersion['version_number']) {
// DNS cache removals require curl 7.42 or higher
// On lower versions, we have to create a new multi handle
curl_multi_close($this->multi->handle);
$this->multi->handle = (new self())->multi->handle;
}
foreach ($options['resolve'] as $host => $ip) {
$resolve[] = null === $ip ? "-$host:$port" : "$host:$port:$ip";
$this->multi->dnsCache->hostnames[$host] = $ip;
$this->multi->dnsCache->removals["-$host:$port"] = "-$host:$port";
}
$curlopts[\CURLOPT_RESOLVE] = $resolve;
}
if ('POST' === $method) {
// Use CURLOPT_POST to have browser-like POST-to-GET redirects for 301, 302 and 303
$curlopts[\CURLOPT_POST] = true;
} elseif ('HEAD' === $method) {
$curlopts[\CURLOPT_NOBODY] = true;
} else {
$curlopts[\CURLOPT_CUSTOMREQUEST] = $method;
}
if ('\\' !== \DIRECTORY_SEPARATOR && $options['timeout'] < 1) {
$curlopts[\CURLOPT_NOSIGNAL] = true;
}
if (\extension_loaded('zlib') && !isset($options['normalized_headers']['accept-encoding'])) {
$options['headers'][] = 'Accept-Encoding: gzip'; // Expose only one encoding, some servers mess up when more are provided
}
foreach ($options['headers'] as $header) {
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
// curl requires a special syntax to send empty headers
$curlopts[\CURLOPT_HTTPHEADER][] = substr_replace($header, ';', -2);
} else {
$curlopts[\CURLOPT_HTTPHEADER][] = $header;
}
}
// Prevent curl from sending its default Accept and Expect headers
foreach (['accept', 'expect'] as $header) {
if (!isset($options['normalized_headers'][$header][0])) {
$curlopts[\CURLOPT_HTTPHEADER][] = $header.':';
}
}
if (!\is_string($body = $options['body'])) {
if (\is_resource($body)) {
$curlopts[\CURLOPT_INFILE] = $body;
} else {
$eof = false;
$buffer = '';
$curlopts[\CURLOPT_READFUNCTION] = static function ($ch, $fd, $length) use ($body, &$buffer, &$eof) {
return self::readRequestBody($length, $body, $buffer, $eof);
};
}
if (isset($options['normalized_headers']['content-length'][0])) {
$curlopts[\CURLOPT_INFILESIZE] = substr($options['normalized_headers']['content-length'][0], \strlen('Content-Length: '));
} elseif (!isset($options['normalized_headers']['transfer-encoding'])) {
$curlopts[\CURLOPT_HTTPHEADER][] = 'Transfer-Encoding: chunked'; // Enable chunked request bodies
}
if ('POST' !== $method) {
$curlopts[\CURLOPT_UPLOAD] = true;
}
} elseif ('' !== $body || 'POST' === $method) {
$curlopts[\CURLOPT_POSTFIELDS] = $body;
}
if ($options['peer_fingerprint']) {
if (!isset($options['peer_fingerprint']['pin-sha256'])) {
throw new TransportException(__CLASS__.' supports only "pin-sha256" fingerprints.');
}
$curlopts[\CURLOPT_PINNEDPUBLICKEY] = 'sha256//'.implode(';sha256//', $options['peer_fingerprint']['pin-sha256']);
}
if ($options['bindto']) {
if (file_exists($options['bindto'])) {
$curlopts[\CURLOPT_UNIX_SOCKET_PATH] = $options['bindto'];
} elseif (!str_starts_with($options['bindto'], 'if!') && preg_match('/^(.*):(\d+)$/', $options['bindto'], $matches)) {
$curlopts[\CURLOPT_INTERFACE] = $matches[1];
$curlopts[\CURLOPT_LOCALPORT] = $matches[2];
} else {
$curlopts[\CURLOPT_INTERFACE] = $options['bindto'];
}
}
if (0 < $options['max_duration']) {
$curlopts[\CURLOPT_TIMEOUT_MS] = 1000 * $options['max_duration'];
}
if (!empty($options['extra']['curl']) && \is_array($options['extra']['curl'])) {
$this->validateExtraCurlOptions($options['extra']['curl']);
$curlopts += $options['extra']['curl'];
}
if ($pushedResponse = $this->multi->pushedResponses[$url] ?? null) {
unset($this->multi->pushedResponses[$url]);
if (self::acceptPushForRequest($method, $options, $pushedResponse)) {
$this->logger && $this->logger->debug(sprintf('Accepting pushed response: "%s %s"', $method, $url));
// Reinitialize the pushed response with request's options
$ch = $pushedResponse->handle;
$pushedResponse = $pushedResponse->response;
$pushedResponse->__construct($this->multi, $url, $options, $this->logger);
} else {
$this->logger && $this->logger->debug(sprintf('Rejecting pushed response: "%s"', $url));
$pushedResponse = null;
}
}
if (!$pushedResponse) {
$ch = curl_init();
$this->logger && $this->logger->info(sprintf('Request: "%s %s"', $method, $url));
}
foreach ($curlopts as $opt => $value) {
if (null !== $value && !curl_setopt($ch, $opt, $value) && \CURLOPT_CERTINFO !== $opt && (!\defined('CURLOPT_HEADEROPT') || \CURLOPT_HEADEROPT !== $opt)) {
$constantName = $this->findConstantName($opt);
throw new TransportException(sprintf('Curl option "%s" is not supported.', $constantName ?? $opt));
}
}
return $pushedResponse ?? new CurlResponse($this->multi, $ch, $options, $this->logger, $method, self::createRedirectResolver($options, $host), self::$curlVersion['version_number']);
}
/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
if ($responses instanceof CurlResponse) {
$responses = [$responses];
} elseif (!is_iterable($responses)) {
throw new \TypeError(sprintf('"%s()" expects parameter 1 to be an iterable of CurlResponse objects, "%s" given.', __METHOD__, get_debug_type($responses)));
}
if (\is_resource($this->multi->handle) || $this->multi->handle instanceof \CurlMultiHandle) {
$active = 0;
while (\CURLM_CALL_MULTI_PERFORM === curl_multi_exec($this->multi->handle, $active));
}
return new ResponseStream(CurlResponse::stream($responses, $timeout));
}
public function reset()
{
$this->multi->logger = $this->logger;
$this->multi->reset();
}
/**
* @return array
*/
public function __sleep()
{
throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
}
public function __wakeup()
{
throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
public function __destruct()
{
$this->multi->logger = $this->logger;
}
private function handlePush($parent, $pushed, array $requestHeaders, int $maxPendingPushes): int
{
$headers = [];
$origin = curl_getinfo($parent, \CURLINFO_EFFECTIVE_URL);
foreach ($requestHeaders as $h) {
if (false !== $i = strpos($h, ':', 1)) {
$headers[substr($h, 0, $i)][] = substr($h, 1 + $i);
}
}
if (!isset($headers[':method']) || !isset($headers[':scheme']) || !isset($headers[':authority']) || !isset($headers[':path'])) {
$this->logger && $this->logger->debug(sprintf('Rejecting pushed response from "%s": pushed headers are invalid', $origin));
return \CURL_PUSH_DENY;
}
$url = $headers[':scheme'][0].'://'.$headers[':authority'][0];
// curl before 7.65 doesn't validate the pushed ":authority" header,
// but this is a MUST in the HTTP/2 RFC; let's restrict pushes to the original host,
// ignoring domains mentioned as alt-name in the certificate for now (same as curl).
if (!str_starts_with($origin, $url.'/')) {
$this->logger && $this->logger->debug(sprintf('Rejecting pushed response from "%s": server is not authoritative for "%s"', $origin, $url));
return \CURL_PUSH_DENY;
}
if ($maxPendingPushes <= \count($this->multi->pushedResponses)) {
$fifoUrl = key($this->multi->pushedResponses);
unset($this->multi->pushedResponses[$fifoUrl]);
$this->logger && $this->logger->debug(sprintf('Evicting oldest pushed response: "%s"', $fifoUrl));
}
$url .= $headers[':path'][0];
$this->logger && $this->logger->debug(sprintf('Queueing pushed response: "%s"', $url));
$this->multi->pushedResponses[$url] = new PushedResponse(new CurlResponse($this->multi, $pushed), $headers, $this->multi->openHandles[(int) $parent][1] ?? [], $pushed);
return \CURL_PUSH_OK;
}
/**
* Accepts pushed responses only if their headers related to authentication match the request.
*/
private static function acceptPushForRequest(string $method, array $options, PushedResponse $pushedResponse): bool
{
if ('' !== $options['body'] || $method !== $pushedResponse->requestHeaders[':method'][0]) {
return false;
}
foreach (['proxy', 'no_proxy', 'bindto', 'local_cert', 'local_pk'] as $k) {
if ($options[$k] !== $pushedResponse->parentOptions[$k]) {
return false;
}
}
foreach (['authorization', 'cookie', 'range', 'proxy-authorization'] as $k) {
$normalizedHeaders = $options['normalized_headers'][$k] ?? [];
foreach ($normalizedHeaders as $i => $v) {
$normalizedHeaders[$i] = substr($v, \strlen($k) + 2);
}
if (($pushedResponse->requestHeaders[$k] ?? []) !== $normalizedHeaders) {
return false;
}
}
return true;
}
/**
* Wraps the request's body callback to allow it to return strings longer than curl requested.
*/
private static function readRequestBody(int $length, \Closure $body, string &$buffer, bool &$eof): string
{
if (!$eof && \strlen($buffer) < $length) {
if (!\is_string($data = $body($length))) {
throw new TransportException(sprintf('The return value of the "body" option callback must be a string, "%s" returned.', get_debug_type($data)));
}
$buffer .= $data;
$eof = '' === $data;
}
$data = substr($buffer, 0, $length);
$buffer = substr($buffer, $length);
return $data;
}
/**
* Resolves relative URLs on redirects and deals with authentication headers.
*
* Work around CVE-2018-1000007: Authorization and Cookie headers should not follow redirects - fixed in Curl 7.64
*/
private static function createRedirectResolver(array $options, string $host): \Closure
{
$redirectHeaders = [];
if (0 < $options['max_redirects']) {
$redirectHeaders['host'] = $host;
$redirectHeaders['with_auth'] = $redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
return 0 !== stripos($h, 'Host:');
});
if (isset($options['normalized_headers']['authorization'][0]) || isset($options['normalized_headers']['cookie'][0])) {
$redirectHeaders['no_auth'] = array_filter($options['headers'], static function ($h) {
return 0 !== stripos($h, 'Authorization:') && 0 !== stripos($h, 'Cookie:');
});
}
}
return static function ($ch, string $location) use ($redirectHeaders) {
try {
$location = self::parseUrl($location);
} catch (InvalidArgumentException $e) {
return null;
}
if ($redirectHeaders && $host = parse_url('http:'.$location['authority'], \PHP_URL_HOST)) {
$requestHeaders = $redirectHeaders['host'] === $host ? $redirectHeaders['with_auth'] : $redirectHeaders['no_auth'];
curl_setopt($ch, \CURLOPT_HTTPHEADER, $requestHeaders);
}
$url = self::parseUrl(curl_getinfo($ch, \CURLINFO_EFFECTIVE_URL));
return implode('', self::resolveUrl($location, $url));
};
}
private function findConstantName(int $opt): ?string
{
$constants = array_filter(get_defined_constants(), static function ($v, $k) use ($opt) {
return $v === $opt && 'C' === $k[0] && (str_starts_with($k, 'CURLOPT_') || str_starts_with($k, 'CURLINFO_'));
}, \ARRAY_FILTER_USE_BOTH);
return key($constants);
}
/**
* Prevents overriding options that are set internally throughout the request.
*/
private function validateExtraCurlOptions(array $options): void
{
$curloptsToConfig = [
//options used in CurlHttpClient
\CURLOPT_HTTPAUTH => 'auth_ntlm',
\CURLOPT_USERPWD => 'auth_ntlm',
\CURLOPT_RESOLVE => 'resolve',
\CURLOPT_NOSIGNAL => 'timeout',
\CURLOPT_HTTPHEADER => 'headers',
\CURLOPT_INFILE => 'body',
\CURLOPT_READFUNCTION => 'body',
\CURLOPT_INFILESIZE => 'body',
\CURLOPT_POSTFIELDS => 'body',
\CURLOPT_UPLOAD => 'body',
\CURLOPT_PINNEDPUBLICKEY => 'peer_fingerprint',
\CURLOPT_UNIX_SOCKET_PATH => 'bindto',
\CURLOPT_INTERFACE => 'bindto',
\CURLOPT_TIMEOUT_MS => 'max_duration',
\CURLOPT_TIMEOUT => 'max_duration',
\CURLOPT_MAXREDIRS => 'max_redirects',
\CURLOPT_PROXY => 'proxy',
\CURLOPT_NOPROXY => 'no_proxy',
\CURLOPT_SSL_VERIFYPEER => 'verify_peer',
\CURLOPT_SSL_VERIFYHOST => 'verify_host',
\CURLOPT_CAINFO => 'cafile',
\CURLOPT_CAPATH => 'capath',
\CURLOPT_SSL_CIPHER_LIST => 'ciphers',
\CURLOPT_SSLCERT => 'local_cert',
\CURLOPT_SSLKEY => 'local_pk',
\CURLOPT_KEYPASSWD => 'passphrase',
\CURLOPT_CERTINFO => 'capture_peer_cert_chain',
\CURLOPT_USERAGENT => 'normalized_headers',
\CURLOPT_REFERER => 'headers',
//options used in CurlResponse
\CURLOPT_NOPROGRESS => 'on_progress',
\CURLOPT_PROGRESSFUNCTION => 'on_progress',
];
$curloptsToCheck = [
\CURLOPT_PRIVATE,
\CURLOPT_HEADERFUNCTION,
\CURLOPT_WRITEFUNCTION,
\CURLOPT_VERBOSE,
\CURLOPT_STDERR,
\CURLOPT_RETURNTRANSFER,
\CURLOPT_URL,
\CURLOPT_FOLLOWLOCATION,
\CURLOPT_HEADER,
\CURLOPT_CONNECTTIMEOUT,
\CURLOPT_CONNECTTIMEOUT_MS,
\CURLOPT_HTTP_VERSION,
\CURLOPT_PORT,
\CURLOPT_DNS_USE_GLOBAL_CACHE,
\CURLOPT_PROTOCOLS,
\CURLOPT_REDIR_PROTOCOLS,
\CURLOPT_COOKIEFILE,
\CURLINFO_REDIRECT_COUNT,
];
if (\defined('CURLOPT_HTTP09_ALLOWED')) {
$curloptsToCheck[] = \CURLOPT_HTTP09_ALLOWED;
}
if (\defined('CURLOPT_HEADEROPT')) {
$curloptsToCheck[] = \CURLOPT_HEADEROPT;
}
$methodOpts = [
\CURLOPT_POST,
\CURLOPT_PUT,
\CURLOPT_CUSTOMREQUEST,
\CURLOPT_HTTPGET,
\CURLOPT_NOBODY,
];
foreach ($options as $opt => $optValue) {
if (isset($curloptsToConfig[$opt])) {
$constName = $this->findConstantName($opt) ?? $opt;
throw new InvalidArgumentException(sprintf('Cannot set "%s" with "extra.curl", use option "%s" instead.', $constName, $curloptsToConfig[$opt]));
}
if (\in_array($opt, $methodOpts)) {
throw new InvalidArgumentException('The HTTP method cannot be overridden using "extra.curl".');
}
if (\in_array($opt, $curloptsToCheck)) {
$constName = $this->findConstantName($opt) ?? $opt;
throw new InvalidArgumentException(sprintf('Cannot set "%s" with "extra.curl".', $constName));
}
}
}
}

View file

@ -0,0 +1,170 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\DataCollector;
use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
use Symfony\Component\VarDumper\Caster\ImgStub;
/**
* @author Jérémy Romey <jeremy@free-agent.fr>
*/
final class HttpClientDataCollector extends DataCollector implements LateDataCollectorInterface
{
/**
* @var TraceableHttpClient[]
*/
private $clients = [];
public function registerClient(string $name, TraceableHttpClient $client)
{
$this->clients[$name] = $client;
}
/**
* {@inheritdoc}
*/
public function collect(Request $request, Response $response, \Throwable $exception = null)
{
$this->reset();
foreach ($this->clients as $name => $client) {
[$errorCount, $traces] = $this->collectOnClient($client);
$this->data['clients'][$name] = [
'traces' => $traces,
'error_count' => $errorCount,
];
$this->data['request_count'] += \count($traces);
$this->data['error_count'] += $errorCount;
}
}
public function lateCollect()
{
foreach ($this->clients as $client) {
$client->reset();
}
}
public function getClients(): array
{
return $this->data['clients'] ?? [];
}
public function getRequestCount(): int
{
return $this->data['request_count'] ?? 0;
}
public function getErrorCount(): int
{
return $this->data['error_count'] ?? 0;
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'http_client';
}
public function reset()
{
$this->data = [
'clients' => [],
'request_count' => 0,
'error_count' => 0,
];
}
private function collectOnClient(TraceableHttpClient $client): array
{
$traces = $client->getTracedRequests();
$errorCount = 0;
$baseInfo = [
'response_headers' => 1,
'retry_count' => 1,
'redirect_count' => 1,
'redirect_url' => 1,
'user_data' => 1,
'error' => 1,
'url' => 1,
];
foreach ($traces as $i => $trace) {
if (400 <= ($trace['info']['http_code'] ?? 0)) {
++$errorCount;
}
$info = $trace['info'];
$traces[$i]['http_code'] = $info['http_code'] ?? 0;
unset($info['filetime'], $info['http_code'], $info['ssl_verify_result'], $info['content_type']);
if (($info['http_method'] ?? null) === $trace['method']) {
unset($info['http_method']);
}
if (($info['url'] ?? null) === $trace['url']) {
unset($info['url']);
}
foreach ($info as $k => $v) {
if (!$v || (is_numeric($v) && 0 > $v)) {
unset($info[$k]);
}
}
if (\is_string($content = $trace['content'])) {
$contentType = 'application/octet-stream';
foreach ($info['response_headers'] ?? [] as $h) {
if (0 === stripos($h, 'content-type: ')) {
$contentType = substr($h, \strlen('content-type: '));
break;
}
}
if (0 === strpos($contentType, 'image/') && class_exists(ImgStub::class)) {
$content = new ImgStub($content, $contentType, '');
} else {
$content = [$content];
}
$content = ['response_content' => $content];
} elseif (\is_array($content)) {
$content = ['response_json' => $content];
} else {
$content = [];
}
if (isset($info['retry_count'])) {
$content['retries'] = $info['previous_info'];
unset($info['previous_info']);
}
$debugInfo = array_diff_key($info, $baseInfo);
$info = ['info' => $debugInfo] + array_diff_key($info, $debugInfo) + $content;
unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient
$traces[$i]['info'] = $this->cloneVar($info);
$traces[$i]['options'] = $this->cloneVar($trace['options']);
}
return [$errorCount, $traces];
}
}

View file

@ -0,0 +1,58 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\HttpClient\ResponseStreamInterface;
/**
* Eases with writing decorators.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait DecoratorTrait
{
private $client;
public function __construct(HttpClientInterface $client = null)
{
$this->client = $client ?? HttpClient::create();
}
/**
* {@inheritdoc}
*/
public function request(string $method, string $url, array $options = []): ResponseInterface
{
return $this->client->request($method, $url, $options);
}
/**
* {@inheritdoc}
*/
public function stream($responses, float $timeout = null): ResponseStreamInterface
{
return $this->client->stream($responses, $timeout);
}
/**
* {@inheritdoc}
*/
public function withOptions(array $options): self
{
$clone = clone $this;
$clone->client = $this->client->withOptions($options);
return $clone;
}
}

View file

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpClient\DependencyInjection;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpClient\TraceableHttpClient;
final class HttpClientPass implements CompilerPassInterface
{
private $clientTag;
public function __construct(string $clientTag = 'http_client.client')
{
if (0 < \func_num_args()) {
trigger_deprecation('symfony/http-client', '5.3', 'Configuring "%s" is deprecated.', __CLASS__);
}
$this->clientTag = $clientTag;
}
/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('data_collector.http_client')) {
return;
}
foreach ($container->findTaggedServiceIds($this->clientTag) as $id => $tags) {
$container->register('.debug.'.$id, TraceableHttpClient::class)
->setArguments([new Reference('.debug.'.$id.'.inner'), new Reference('debug.stopwatch', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)])
->setDecoratedService($id);
$container->getDefinition('data_collector.http_client')
->addMethodCall('registerClient', [$id, new Reference('.debug.'.$id)]);
}
}
}

Some files were not shown because too many files have changed in this diff Show more