From 28cdc0439fe30eccd58008282eb1540ccec9a56f Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Tue, 30 Sep 2014 19:59:02 +0800 Subject: [PATCH] MDL-47297 repository_googledocs: Use the newer Google API --- lib/google/lib.php | 55 +++++++++++++++++++++++++++++++++++ repository/googledocs/lib.php | 22 +++++++------- 2 files changed, 66 insertions(+), 11 deletions(-) create mode 100644 lib/google/lib.php diff --git a/lib/google/lib.php b/lib/google/lib.php new file mode 100644 index 00000000000..7c4a7f9e12e --- /dev/null +++ b/lib/google/lib.php @@ -0,0 +1,55 @@ +. + +/** + * Moodle's lib to use for the Google API. + * + * @package core + * @copyright 2014 Frédéric Massart - FMCorz.net + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +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/Google/Client.php'); +require_once($CFG->libdir . '/google/curlio.php'); + +/** + * Wrapper to get a Google Client object. + * + * This automatically sets the config to Moodle's defaults. + * + * @return Google_Client + */ +function get_google_client() { + global $CFG, $SITE; + + make_temp_directory('googleapi'); + $tempdir = $CFG->tempdir . '/googleapi'; + + $config = new Google_Config(); + $config->setApplicationName('Moodle ' . $CFG->release); + $config->setIoClass('moodle_google_curlio'); + $config->setClassConfig('Google_Cache_File', 'directory', $tempdir); + $config->setClassConfig('Google_Auth_OAuth2', 'access_type', 'online'); + $config->setClassConfig('Google_Auth_OAuth2', 'approval_prompt', 'auto'); + + return new Google_Client($config); +} diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index d6c77d88e95..7d1393b6754 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -26,8 +26,8 @@ defined('MOODLE_INTERNAL') || die(); require_once($CFG->dirroot . '/repository/lib.php'); -require_once($CFG->libdir . '/google/Google_Client.php'); -require_once($CFG->libdir . '/google/contrib/Google_DriveService.php'); +require_once($CFG->libdir . '/google/lib.php'); +require_once($CFG->libdir . '/google/Google/Service/Drive.php'); /** * Google Docs Plugin @@ -47,7 +47,7 @@ class repository_googledocs extends repository { /** * Google Drive Service. - * @var Google_DriveService + * @var Google_Drive_Service */ private $service = null; @@ -77,12 +77,12 @@ class repository_googledocs extends repository { $callbackurl = new moodle_url(self::CALLBACKURL); - $this->client = new Google_Client(); + $this->client = get_google_client(); $this->client->setClientId(get_config('googledocs', 'clientid')); $this->client->setClientSecret(get_config('googledocs', 'secret')); - $this->client->setScopes(array('https://www.googleapis.com/auth/drive.readonly')); + $this->client->setScopes(array(Google_Service_Drive::DRIVE_READONLY)); $this->client->setRedirectUri($callbackurl->out(false)); - $this->service = new Google_DriveService($this->client); + $this->service = new Google_Service_Drive($this->client); $this->check_login(); } @@ -311,7 +311,7 @@ class repository_googledocs extends repository { try { // Retrieving files and folders. $response = $this->service->files->listFiles($params); - } catch (Google_ServiceException $e) { + } catch (Google_Service_Exception $e) { if ($e->getCode() == 403 && strpos($e->getMessage(), 'Access Not Configured') !== false) { // This is raised when the service Drive API has not been enabled on Google APIs control panel. throw new repository_exception('servicenotenabled', 'repository_googledocs'); @@ -416,11 +416,11 @@ class repository_googledocs extends repository { public function get_file($reference, $filename = '') { global $CFG; - $request = new Google_HttpRequest($reference); - $httpRequest = Google_Client::$io->authenticatedRequest($request); - if ($httpRequest->getResponseHttpCode() == 200) { + $auth = $this->client->getAuth(); + $request = $auth->authenticatedRequest(new Google_Http_Request($reference)); + if ($request->getResponseHttpCode() == 200) { $path = $this->prepare_file($filename); - $content = $httpRequest->getResponseBody(); + $content = $request->getResponseBody(); if (file_put_contents($path, $content) !== false) { @chmod($path, $CFG->filepermissions); return array(