diff --git a/lib/google/Google_Client.php b/lib/google/Google_Client.php deleted file mode 100644 index 3e12137a8da..00000000000 --- a/lib/google/Google_Client.php +++ /dev/null @@ -1,454 +0,0 @@ - - * @author Chirag Shah - */ -class Google_Client { - /** - * @static - * @var Google_Auth $auth - */ - static $auth; - - /** - * @static - * @var Google_IO $io - */ - static $io; - - /** - * @static - * @var Google_Cache $cache - */ - static $cache; - - /** - * @static - * @var boolean $useBatch - */ - static $useBatch = false; - - /** @var array $scopes */ - protected $scopes = array(); - - /** @var bool $useObjects */ - protected $useObjects = false; - - // definitions of services that are discovered. - protected $services = array(); - - // Used to track authenticated state, can't discover services after doing authenticate() - private $authenticated = false; - - public function __construct($config = array()) { - global $apiConfig; - $apiConfig = array_merge($apiConfig, $config); - self::$cache = new $apiConfig['cacheClass'](); - self::$auth = new $apiConfig['authClass'](); - self::$io = new $apiConfig['ioClass'](); - } - - /** - * Add a service - */ - public function addService($service, $version = false) { - global $apiConfig; - if ($this->authenticated) { - throw new Google_Exception('Cant add services after having authenticated'); - } - $this->services[$service] = array(); - if (isset($apiConfig['services'][$service])) { - // Merge the service descriptor with the default values - $this->services[$service] = array_merge($this->services[$service], $apiConfig['services'][$service]); - } - } - - public function authenticate($code = null) { - $service = $this->prepareService(); - $this->authenticated = true; - return self::$auth->authenticate($service, $code); - } - - /** - * @return array - * @visible For Testing - */ - public function prepareService() { - $service = array(); - $scopes = array(); - if ($this->scopes) { - $scopes = $this->scopes; - } else { - foreach ($this->services as $key => $val) { - if (isset($val['scope'])) { - if (is_array($val['scope'])) { - $scopes = array_merge($val['scope'], $scopes); - } else { - $scopes[] = $val['scope']; - } - } else { - $scopes[] = 'https://www.googleapis.com/auth/' . $key; - } - unset($val['discoveryURI']); - unset($val['scope']); - $service = array_merge($service, $val); - } - } - $service['scope'] = implode(' ', $scopes); - return $service; - } - - /** - * Set the OAuth 2.0 access token using the string that resulted from calling authenticate() - * or Google_Client#getAccessToken(). - * @param string $accessToken JSON encoded string containing in the following format: - * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer", - * "expires_in":3600, "id_token":"TOKEN", "created":1320790426} - */ - public function setAccessToken($accessToken) { - if ($accessToken == null || 'null' == $accessToken) { - $accessToken = null; - } - self::$auth->setAccessToken($accessToken); - } - - /** - * Set the type of Auth class the client should use. - * @param string $authClassName - */ - public function setAuthClass($authClassName) { - self::$auth = new $authClassName(); - } - - /** - * Construct the OAuth 2.0 authorization request URI. - * @return string - */ - public function createAuthUrl() { - $service = $this->prepareService(); - return self::$auth->createAuthUrl($service['scope']); - } - - /** - * Get the OAuth 2.0 access token. - * @return string $accessToken JSON encoded string in the following format: - * {"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer", - * "expires_in":3600,"id_token":"TOKEN", "created":1320790426} - */ - public function getAccessToken() { - $token = self::$auth->getAccessToken(); - return (null == $token || 'null' == $token) ? null : $token; - } - - /** - * Returns if the access_token is expired. - * @return bool Returns True if the access_token is expired. - */ - public function isAccessTokenExpired() { - return self::$auth->isAccessTokenExpired(); - } - - /** - * Set the developer key to use, these are obtained through the API Console. - * @see http://code.google.com/apis/console-help/#generatingdevkeys - * @param string $developerKey - */ - public function setDeveloperKey($developerKey) { - self::$auth->setDeveloperKey($developerKey); - } - - /** - * Set OAuth 2.0 "state" parameter to achieve per-request customization. - * @see http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-3.1.2.2 - * @param string $state - */ - public function setState($state) { - self::$auth->setState($state); - } - - /** - * @param string $accessType Possible values for access_type include: - * {@code "offline"} to request offline access from the user. (This is the default value) - * {@code "online"} to request online access from the user. - */ - public function setAccessType($accessType) { - self::$auth->setAccessType($accessType); - } - - /** - * @param string $approvalPrompt Possible values for approval_prompt include: - * {@code "force"} to force the approval UI to appear. (This is the default value) - * {@code "auto"} to request auto-approval when possible. - */ - public function setApprovalPrompt($approvalPrompt) { - self::$auth->setApprovalPrompt($approvalPrompt); - } - - /** - * Set the application name, this is included in the User-Agent HTTP header. - * @param string $applicationName - */ - public function setApplicationName($applicationName) { - global $apiConfig; - $apiConfig['application_name'] = $applicationName; - } - - /** - * Set the OAuth 2.0 Client ID. - * @param string $clientId - */ - public function setClientId($clientId) { - global $apiConfig; - $apiConfig['oauth2_client_id'] = $clientId; - self::$auth->clientId = $clientId; - } - - /** - * Get the OAuth 2.0 Client ID. - */ - public function getClientId() { - return self::$auth->clientId; - } - - /** - * Set the OAuth 2.0 Client Secret. - * @param string $clientSecret - */ - public function setClientSecret($clientSecret) { - global $apiConfig; - $apiConfig['oauth2_client_secret'] = $clientSecret; - self::$auth->clientSecret = $clientSecret; - } - - /** - * Get the OAuth 2.0 Client Secret. - */ - public function getClientSecret() { - return self::$auth->clientSecret; - } - - /** - * Set the OAuth 2.0 Redirect URI. - * @param string $redirectUri - */ - public function setRedirectUri($redirectUri) { - global $apiConfig; - $apiConfig['oauth2_redirect_uri'] = $redirectUri; - self::$auth->redirectUri = $redirectUri; - } - - /** - * Get the OAuth 2.0 Redirect URI. - */ - public function getRedirectUri() { - return self::$auth->redirectUri; - } - - /** - * Fetches a fresh OAuth 2.0 access token with the given refresh token. - * @param string $refreshToken - * @return void - */ - public function refreshToken($refreshToken) { - self::$auth->refreshToken($refreshToken); - } - - /** - * Revoke an OAuth2 access token or refresh token. This method will revoke the current access - * token, if a token isn't provided. - * @throws Google_AuthException - * @param string|null $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 = null) { - self::$auth->revokeToken($token); - } - - /** - * Verify an id_token. This method will verify the current id_token, if one - * isn't provided. - * @throws Google_AuthException - * @param string|null $token The token (id_token) that should be verified. - * @return Google_LoginTicket Returns an apiLoginTicket if the verification was - * successful. - */ - public function verifyIdToken($token = null) { - return self::$auth->verifyIdToken($token); - } - - /** - * @param Google_AssertionCredentials $creds - * @return void - */ - public function setAssertionCredentials(Google_AssertionCredentials $creds) { - self::$auth->setAssertionCredentials($creds); - } - - /** - * This function allows you to overrule the automatically generated scopes, - * so that you can ask for more or less permission in the auth flow - * Set this before you call authenticate() though! - * @param array $scopes, ie: array('https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/moderator') - */ - public function setScopes($scopes) { - $this->scopes = is_string($scopes) ? explode(" ", $scopes) : $scopes; - } - - /** - * Declare if objects should be returned by the api service classes. - * - * @param boolean $useObjects True if objects should be returned by the service classes. - * False if associative arrays should be returned (default behavior). - * @experimental - */ - public function setUseObjects($useObjects) { - global $apiConfig; - $apiConfig['use_objects'] = $useObjects; - } - - /** - * Declare if objects should be returned by the api service classes. - * - * @param boolean $useBatch True if the experimental batch support should - * be enabled. Defaults to False. - * @experimental - */ - public function setUseBatch($useBatch) { - self::$useBatch = $useBatch; - } - - /** - * @static - * @return Google_Auth the implementation of apiAuth. - */ - public static function getAuth() { - return Google_Client::$auth; - } - - /** - * @static - * @return Google_IO the implementation of apiIo. - */ - public static function getIo() { - return Google_Client::$io; - } - - /** - * @return Google_Cache the implementation of apiCache. - */ - public function getCache() { - return Google_Client::$cache; - } -} - -// Exceptions that the Google PHP API Library can throw -class Google_Exception extends Exception {} -class Google_AuthException extends Google_Exception {} -class Google_CacheException extends Google_Exception {} -class Google_IOException extends Google_Exception {} -class Google_ServiceException extends Google_Exception { - /** - * Optional list of errors returned in a JSON body of an HTTP error response. - */ - protected $errors = array(); - - /** - * Override default constructor to add ability to set $errors. - * - * @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; - } -} diff --git a/lib/google/config.php b/lib/google/config.php deleted file mode 100644 index 00917f40458..00000000000 --- a/lib/google/config.php +++ /dev/null @@ -1,81 +0,0 @@ - false, - - // The application_name is included in the User-Agent HTTP header. - 'application_name' => '', - - // OAuth2 Settings, you can get these keys at https://code.google.com/apis/console - 'oauth2_client_id' => '', - 'oauth2_client_secret' => '', - 'oauth2_redirect_uri' => '', - - // The developer key, you get this at https://code.google.com/apis/console - 'developer_key' => '', - - // Site name to show in the Google's OAuth 1 authentication screen. - 'site_name' => 'www.example.org', - - // Which Authentication, Storage and HTTP IO classes to use. - 'authClass' => 'Google_OAuth2', - 'ioClass' => 'Google_CurlIO', - 'cacheClass' => 'Google_FileCache', - - // Don't change these unless you're working against a special development or testing environment. - 'basePath' => 'https://www.googleapis.com', - - // IO Class dependent configuration, you only have to configure the values - // for the class that was configured as the ioClass above - 'ioFileCache_directory' => - (function_exists('sys_get_temp_dir') ? - sys_get_temp_dir() . '/Google_Client' : - '/tmp/Google_Client'), - - // Definition of service specific values like scopes, oauth token URLs, etc - 'services' => array( - 'analytics' => array('scope' => 'https://www.googleapis.com/auth/analytics.readonly'), - 'calendar' => array( - 'scope' => array( - "https://www.googleapis.com/auth/calendar", - "https://www.googleapis.com/auth/calendar.readonly", - ) - ), - 'books' => array('scope' => 'https://www.googleapis.com/auth/books'), - 'latitude' => array( - 'scope' => array( - 'https://www.googleapis.com/auth/latitude.all.best', - 'https://www.googleapis.com/auth/latitude.all.city', - ) - ), - 'moderator' => array('scope' => 'https://www.googleapis.com/auth/moderator'), - 'oauth2' => array( - 'scope' => array( - 'https://www.googleapis.com/auth/userinfo.profile', - 'https://www.googleapis.com/auth/userinfo.email', - ) - ), - 'plus' => array('scope' => 'https://www.googleapis.com/auth/plus.me'), - 'siteVerification' => array('scope' => 'https://www.googleapis.com/auth/siteverification'), - 'tasks' => array('scope' => 'https://www.googleapis.com/auth/tasks'), - 'urlshortener' => array('scope' => 'https://www.googleapis.com/auth/urlshortener') - ) -); \ No newline at end of file diff --git a/lib/google/curlio.php b/lib/google/curlio.php index 2a287c2d88c..72e7d2fb859 100644 --- a/lib/google/curlio.php +++ b/lib/google/curlio.php @@ -23,9 +23,8 @@ */ defined('MOODLE_INTERNAL') || die(); + require_once($CFG->libdir . '/filelib.php'); -require_once($CFG->libdir . '/google/src/Google/IO/Curl.php'); -require_once($CFG->libdir . '/google/src/Google/IO/Exception.php'); /** * Class moodle_google_curlio. diff --git a/lib/google/lib.php b/lib/google/lib.php index cca4dee9b87..94fa2fe84a1 100644 --- a/lib/google/lib.php +++ b/lib/google/lib.php @@ -24,11 +24,9 @@ defined('MOODLE_INTERNAL') || die(); -require_once($CFG->libdir . '/weblib.php'); - -// Update the include_path so that the library can use require_once in its own files. -set_include_path(get_include_path() . PATH_SEPARATOR . $CFG->libdir . '/google'); -require_once($CFG->libdir . '/google/src/Google/Client.php'); +// All Google API classes support autoload with this. +require_once($CFG->libdir . '/google/autoload.php'); +// To be able to use our custom IO class. require_once($CFG->libdir . '/google/curlio.php'); /** diff --git a/lib/google/local_config.php b/lib/google/local_config.php deleted file mode 100644 index b136b9fba4c..00000000000 --- a/lib/google/local_config.php +++ /dev/null @@ -1,52 +0,0 @@ -. - -/** - * Basic configuration overwrite for Google API. - * - * @package core - * @copyright Frédéric Massart - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -defined('MOODLE_INTERNAL') || die(); -global $CFG, $SITE; -require_once($CFG->libdir . '/weblib.php'); -require_once($CFG->libdir . '/google/curlio.php'); - -make_temp_directory('googleapi'); -$GoogleConfigTempDir = $CFG->tempdir . '/googleapi'; - -global $apiConfig; -$apiConfig = array( - // Application name. - 'application_name' => 'Moodle ' . $CFG->release, - - // Site name to show in the Google's OAuth 1 authentication screen. - 'site_name' => $SITE->fullname, - - // Which HTTP IO classes to use. - 'ioClass' => 'moodle_google_curlio', - - // Cache class directory, it should never be used but created just in case. - 'ioFileCache_directory' => $GoogleConfigTempDir, - - // Default Access Type for OAuth 2.0. - 'oauth2_access_type' => 'online', - - // Default Approval Prompt for OAuth 2.0. - 'oauth2_approval_prompt' => 'auto' -); diff --git a/lib/google/readme_moodle.txt b/lib/google/readme_moodle.txt index 44e95d413a7..cdf5ae4e834 100644 --- a/lib/google/readme_moodle.txt +++ b/lib/google/readme_moodle.txt @@ -1,9 +1,9 @@ Google APIs Client Library for PHP ================================== -Only the source, README and autoloader files have been kept in this directory: +Only the source, LICENSE, README and autoloader files have been kept in this directory: -- Copy /src/Google to /src/Google +- Copy /src/Google to /src/Google - Copy /LICENSE to LICENSE - Copy /README.md to README.md - Copy /autoload.php to autoload.php @@ -16,6 +16,17 @@ Here are the files that we have added. that should be used throughout Moodle. It also takes care of including the required files and updating the include_path. + Every use of the Google PHP API should always start by requiring this file. + Apart from the wrapping of Google_Client above... it's also responsible for + enabling the autoload of all the API classes. + + So, basically, every use of the Google Client API should be something like: + + require_once($CFG->libdir . '/google/lib.php'); + $client = get_google_client(); + + And, from there, use the Client API normally. Everything will be autoloaded. + /curlio.php An override of the default Google_IO_Curl class to use our Curl class @@ -23,6 +34,8 @@ Here are the files that we have added. Curl class should be checked to ensure that its functionalities are covered in this file. + This should not ever be used directly. The wrapper above uses it automatically. + Information ----------- diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 0da0df3eeb1..93dbd8cdac8 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -36,6 +36,8 @@ information provided here is intended especially for developers. * Use standard PHP date time classes and methods - see new core_date class for timezone normalisation methods. * Moved lib/google/Google/ to lib/google/src/Google. This is to address autoloader issues with Google's provided autoloader for the library. See MDL-49519 for details. +* The outdated lib/google/Google_Client.php and related files have been completely removed. To use + the new client, read lib/google/readme_moodle.txt, please. === 2.8 === diff --git a/portfolio/googledocs/lib.php b/portfolio/googledocs/lib.php index a4716b57ea2..04733dda0f5 100644 --- a/portfolio/googledocs/lib.php +++ b/portfolio/googledocs/lib.php @@ -22,7 +22,6 @@ */ require_once($CFG->libdir.'/portfolio/plugin.php'); require_once($CFG->libdir . '/google/lib.php'); -require_once($CFG->libdir . '/google/src/Google/Service/Drive.php'); class portfolio_plugin_googledocs extends portfolio_plugin_push_base { /** diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index 9b975f4eade..ca3566996b9 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -27,7 +27,6 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/repository/lib.php'); require_once($CFG->libdir . '/google/lib.php'); -require_once($CFG->libdir . '/google/src/Google/Service/Drive.php'); /** * Google Docs Plugin