diff --git a/portfolio/googledocs/db/events.php b/admin/oauth2callback.php
similarity index 52%
rename from portfolio/googledocs/db/events.php
rename to admin/oauth2callback.php
index b0d3d0c702f..364c0023de0 100644
--- a/portfolio/googledocs/db/events.php
+++ b/admin/oauth2callback.php
@@ -1,4 +1,5 @@
.
/**
- * Add event handlers for the googledocs portfolio.
+ * An oauth2 redirection endpoint which can be used for an application:
+ * http://tools.ietf.org/html/draft-ietf-oauth-v2-26#section-3.1.2
*
- * @package portfolio_googledocs
- * @category event
- * @copyright 2009 Penny Leach
+ * This is used because some oauth servers will not allow a redirect urls
+ * with get params (like repository callback) and that needs to be called
+ * using the state param.
+ *
+ * @package core
+ * @copyright 2012 Dan Poltawski
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
-$handlers = array (
- 'user_deleted' => array (
- 'handlerfile' => '/portfolio/googledocs/lib.php',
- 'handlerfunction' => 'portfolio_googledocs_user_deleted',
- 'schedule' => 'cron',
- 'internal' => 0,
- ),
-);
+require_once(dirname(dirname(__FILE__)).'/config.php');
+// The authorization code generated by the authorization server.
+$code = required_param('code', PARAM_RAW);
+// The state parameter we've given (used in moodle as a redirect url).
+$state = required_param('state', PARAM_URL);
+redirect(new moodle_url($state, array('code' => $code)));
diff --git a/lib/googleapi.php b/lib/googleapi.php
index a079ffb5d2f..5994dd382d8 100644
--- a/lib/googleapi.php
+++ b/lib/googleapi.php
@@ -1,276 +1,62 @@
.
+
/**
- * Moodle - Modular Object-Oriented Dynamic Learning Environment
- * http://moodle.org
- * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
+ * Simple implementation of some Google API functions for Moodle.
*
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
+ * @package core
+ * @copyright Dan Poltawski
{$a->callbackurl}
'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Google Docs portfolio plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Google Docs portfolio plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must configure oauth credentials in this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Google Docs portfolio plugin has been disabled until configured with OAuth2'; $string['pluginname'] = 'Google Docs'; $string['sendfailed'] = 'The file {$a} failed to transfer to google'; +$string['secret'] = 'Secret'; diff --git a/portfolio/googledocs/lib.php b/portfolio/googledocs/lib.php index 2223f9d4831..9820246f9db 100644 --- a/portfolio/googledocs/lib.php +++ b/portfolio/googledocs/lib.php @@ -1,4 +1,19 @@ . + /** * Google Documents Portfolio Plugin * @@ -9,18 +24,10 @@ require_once($CFG->libdir.'/portfolio/plugin.php'); require_once($CFG->libdir.'/googleapi.php'); class portfolio_plugin_googledocs extends portfolio_plugin_push_base { - private $sessiontoken; + private $googleoauth = null; public function supported_formats() { - return array( - PORTFOLIO_FORMAT_PLAINHTML, - PORTFOLIO_FORMAT_IMAGE, - PORTFOLIO_FORMAT_TEXT, - PORTFOLIO_FORMAT_PDF, - PORTFOLIO_FORMAT_DOCUMENT, - PORTFOLIO_FORMAT_PRESENTATION, - PORTFOLIO_FORMAT_SPREADSHEET - ); + return array(PORTFOLIO_FORMAT_FILE); } public static function get_name() { @@ -28,29 +35,27 @@ class portfolio_plugin_googledocs extends portfolio_plugin_push_base { } public function prepare_package() { - // we send the files as they are, no prep required + // We send the files as they are, no prep required. return true; } - public function get_interactive_continue_url(){ + public function get_interactive_continue_url() { return 'http://docs.google.com/'; } public function expected_time($callertime) { - // we trust what the portfolio says + // We trust what the portfolio says. return $callertime; } public function send_package() { - - if(!$this->sessiontoken){ - throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs'); + if (!$this->googleoauth) { + throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); } - $gdocs = new google_docs(new google_authsub($this->sessiontoken)); - + $gdocs = new google_docs($this->googleoauth); foreach ($this->exporter->get_tempfiles() as $file) { - if(!$gdocs->send_file($file)){ + if (!$gdocs->send_file($file)) { throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); } } @@ -62,20 +67,12 @@ class portfolio_plugin_googledocs extends portfolio_plugin_push_base { return false; } - $sesskey = google_docs::get_sesskey($this->get('user')->id); - - if($sesskey){ - try{ - $gauth = new google_authsub($sesskey); - $this->sessiontoken = $sesskey; - return false; - }catch(Exception $e){ - // sesskey is not valid, delete store and re-auth - google_docs::delete_sesskey($this->get('user')->id); - } + $this->initialize_oauth(); + if ($this->googleoauth->is_logged_in()) { + return false; + } else { + return $this->googleoauth->get_login_url(); } - - return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM); } public function post_control($stage, $params) { @@ -83,43 +80,62 @@ class portfolio_plugin_googledocs extends portfolio_plugin_push_base { return; } - if(!array_key_exists('token', $params)){ - throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); + $this->initialize_oauth(); + if ($this->googleoauth->is_logged_in()) { + return false; + } else { + return $this->googleoauth->get_login_url(); } - - // we now have our auth token, get a session token.. - $gauth = new google_authsub(false, $params['token']); - $this->sessiontoken = $gauth->get_sessiontoken(); - - google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id); } public static function allows_multiple_instances() { return false; } -} -/** - * Registers to the user_deleted event to revoke any - * subauth tokens we have from them - * - * @param $user user object - * @return boolean true in all cases as its only minor cleanup - */ -function portfolio_googledocs_user_deleted($user){ - // it is only by luck that the user prefstill exists now? - // We probably need a pre-delete event? - if($sesskey = google_docs::get_sesskey($user->id)){ - try{ - $gauth = new google_authsub($sesskey); - - $gauth->revoke_session_token(); - }catch(Exception $e){ - // we don't care that much about success- just being good - // google api citzens - return true; - } + public static function has_admin_config() { + return true; } - return true; + public static function get_allowed_config() { + return array('clientid', 'secret'); + } + + public function admin_config_form(&$mform) { + $a = new stdClass; + $a->docsurl = get_docs_url('Google_OAuth2_Setup'); + $a->callbackurl = google_oauth::callback_url()->out(false); + + $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a)); + + $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs')); + $mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs')); + + $strrequired = get_string('required'); + $mform->addRule('clientid', $strrequired, 'required', null, 'client'); + $mform->addRule('secret', $strrequired, 'required', null, 'client'); + } + + private function initialize_oauth() { + $returnurl = new moodle_url('/portfolio/add.php'); + $returnurl->param('postcontrol', 1); + $returnurl->param('id', $this->exporter->get('id')); + $returnurl->param('sesskey', sesskey()); + + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_docs::REALM); + } + + public function instance_sanity_check() { + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + // If there is no oauth config (e.g. plugins upgraded from < 2.3 then + // there will be no config and this plugin should be disabled. + if (empty($clientid) or empty($secret)) { + return 'nooauthcredentials'; + } + return 0; + } } diff --git a/portfolio/googledocs/version.php b/portfolio/googledocs/version.php index 20446664e67..5cee5db320b 100644 --- a/portfolio/googledocs/version.php +++ b/portfolio/googledocs/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2011112900; // Requires this Moodle version -$plugin->component = 'portfolio_googledocs'; // Full name of the plugin (used for diagnostics) +$plugin->version = 2012053001; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2012051100; // Requires this Moodle version. +$plugin->component = 'portfolio_googledocs'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0; diff --git a/portfolio/picasa/db/events.php b/portfolio/picasa/db/events.php deleted file mode 100644 index 09ef7cca20c..00000000000 --- a/portfolio/picasa/db/events.php +++ /dev/null @@ -1,33 +0,0 @@ -. - -/** - * Add event handlers for the picasa portfolio. - * - * @package portfolio_picasa - * @category event - * @copyright 2009 Penny Leach - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -$handlers = array ( - 'user_deleted' => array ( - 'handlerfile' => '/portfolio/picasa/lib.php', - 'handlerfunction' => 'portfolio_picasa_user_deleted', - 'schedule' => 'cron', - 'internal' => 0, - ), -); \ No newline at end of file diff --git a/portfolio/picasa/db/upgrade.php b/portfolio/picasa/db/upgrade.php new file mode 100644 index 00000000000..e5d1cca6d1d --- /dev/null +++ b/portfolio/picasa/db/upgrade.php @@ -0,0 +1,67 @@ +. + +/** + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_portfolio_picasa_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012053000) { + // Delete old user preferences containing authsub tokens. + $DB->delete_records('user_preferences', array('name' => 'google_authsub_sesskey_picasa')); + upgrade_plugin_savepoint(true, 2012053000, 'portfolio', 'picasa'); + } + + if ($oldversion < 2012053001) { + $existing = $DB->get_record('portfolio_instance', array('plugin' => 'picasa'), '*', IGNORE_MISSING); + + if ($existing) { + portfolio_picasa_admin_upgrade_notification(); + } + + upgrade_plugin_savepoint(true, 2012053001, 'portfolio', 'picasa'); + } + + return true; +} + +function portfolio_picasa_admin_upgrade_notification() { + $admins = get_admins(); + + if (empty($admins)) { + return; + } + $mainadmin = reset($admins); + + foreach ($admins as $admin) { + $message = new stdClass(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = $mainadmin; + $message->userto = $admin; + $message->smallmessage = get_string('oauth2upgrade_message_small', 'portfolio_picasa'); + $message->subject = get_string('oauth2upgrade_message_subject', 'portfolio_picasa'); + $message->fullmessage = get_string('oauth2upgrade_message_content', 'portfolio_picasa'); + $message->fullmessagehtml = get_string('oauth2upgrade_message_content', 'portfolio_picasa'); + $message->fullmessageformat = FORMAT_PLAIN; + $message->notification = 1; + message_send($message); + } +} diff --git a/portfolio/picasa/lang/en/portfolio_picasa.php b/portfolio/picasa/lang/en/portfolio_picasa.php index fec154b9391..93781b6df96 100644 --- a/portfolio/picasa/lang/en/portfolio_picasa.php +++ b/portfolio/picasa/lang/en/portfolio_picasa.php @@ -1,5 +1,4 @@ To use the Picasa portfolio you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:{$a->callbackurl}
'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Picasa portfolio plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Picasa portfolio plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must configure oauth credentials in this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Picasa portfolio plugin has been disabled until configured with OAuth2'; $string['pluginname'] = 'Picasa'; $string['sendfailed'] = 'The file {$a} failed to transfer to picasa'; +$string['secret'] = 'Secret'; diff --git a/portfolio/picasa/lib.php b/portfolio/picasa/lib.php index a9ba23fd886..d637b54edca 100644 --- a/portfolio/picasa/lib.php +++ b/portfolio/picasa/lib.php @@ -1,4 +1,19 @@ . + /** * Picasa Portfolio Plugin * @@ -9,7 +24,7 @@ require_once($CFG->libdir.'/portfolio/plugin.php'); require_once($CFG->libdir.'/googleapi.php'); class portfolio_plugin_picasa extends portfolio_plugin_push_base { - private $sessionkey; + private $googleoauth = null; public function supported_formats() { return array(PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_VIDEO); @@ -20,11 +35,11 @@ class portfolio_plugin_picasa extends portfolio_plugin_push_base { } public function prepare_package() { - // we send the files as they are, no prep required + // We send the files as they are, no prep required. return true; } - public function get_interactive_continue_url(){ + public function get_interactive_continue_url() { return 'http://picasaweb.google.com/'; } @@ -33,40 +48,31 @@ class portfolio_plugin_picasa extends portfolio_plugin_push_base { } public function send_package() { - if(!$this->sessionkey){ + if (!$this->googleoauth) { throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa'); } - $picasa = new google_picasa(new google_authsub($this->sessionkey)); - + $picasa = new google_picasa($this->googleoauth); foreach ($this->exporter->get_tempfiles() as $file) { - if(!$picasa->send_file($file)){ + if (!$picasa->send_file($file)) { throw new portfolio_plugin_exception('sendfailed', 'portfolio_picasa', $file->get_filename()); } } } public function steal_control($stage) { - global $CFG; if ($stage != PORTFOLIO_STAGE_CONFIG) { return false; } - $sesskey = google_picasa::get_sesskey($this->get('user')->id); + $this->initialize_oauth(); - if($sesskey){ - try{ - $gauth = new google_authsub($sesskey); - $this->sessionkey = $sesskey; - return false; - }catch(Exception $e){ - // sesskey is not valid, delete store and re-auth - google_picasa::delete_sesskey($this->get('user')->id); - } + if ($this->googleoauth->is_logged_in()) { + return false; + } else { + return $this->googleoauth->get_login_url(); } - - return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_picasa::REALM); } public function post_control($stage, $params) { @@ -74,44 +80,62 @@ class portfolio_plugin_picasa extends portfolio_plugin_push_base { return; } - if(!array_key_exists('token', $params)){ - throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa'); + $this->initialize_oauth(); + if ($this->googleoauth->is_logged_in()) { + return false; + } else { + return $this->googleoauth->get_login_url(); } + } - // we now have our auth token, get a session token.. - $gauth = new google_authsub(false, $params['token']); - - $this->sessionkey = $gauth->get_sessiontoken(); - - google_picasa::set_sesskey($this->sessionkey, $this->get('user')->id); + public static function has_admin_config() { + return true; } public static function allows_multiple_instances() { return false; } -} -/** - * Registers to the user_deleted event to revoke any - * subauth tokens we have from them - * - * @param $user user object - * @return boolean true in all cases as its only minor cleanup - */ -function portfolio_picasa_user_deleted($user){ - // it is only by luck that the user prefstill exists now? - // We probably need a pre-delete event? - if($sesskey = google_picasa::get_sesskey($user->id)){ - try{ - $gauth = new google_authsub($sesskey); - - $gauth->revoke_session_token(); - }catch(Exception $e){ - // we don't care that much about success- just being good - // google api citzens - return true; - } + public static function get_allowed_config() { + return array('clientid', 'secret'); } - return true; + public function admin_config_form(&$mform) { + $a = new stdClass; + $a->docsurl = get_docs_url('Google_OAuth2_Setup'); + $a->callbackurl = google_oauth::callback_url()->out(false); + + $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_picasa', $a)); + + $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_picasa')); + $mform->addElement('text', 'secret', get_string('secret', 'portfolio_picasa')); + + $strrequired = get_string('required'); + $mform->addRule('clientid', $strrequired, 'required', null, 'client'); + $mform->addRule('secret', $strrequired, 'required', null, 'client'); + } + + private function initialize_oauth() { + $returnurl = new moodle_url('/portfolio/add.php'); + $returnurl->param('postcontrol', 1); + $returnurl->param('id', $this->exporter->get('id')); + $returnurl->param('sesskey', sesskey()); + + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_picasa::REALM); + } + + public function instance_sanity_check() { + $clientid = $this->get_config('clientid'); + $secret = $this->get_config('secret'); + + // If there is no oauth config (e.g. plugins upgraded from < 2.3 then + // there will be no config and this plugin should be disabled. + if (empty($clientid) or empty($secret)) { + return 'nooauthcredentials'; + } + return 0; + } } diff --git a/portfolio/picasa/version.php b/portfolio/picasa/version.php index 7906a251af3..b56c5e65b00 100644 --- a/portfolio/picasa/version.php +++ b/portfolio/picasa/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2011112900; // Requires this Moodle version -$plugin->component = 'portfolio_picasa'; // Full name of the plugin (used for diagnostics) +$plugin->version = 2012053001; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2012051100; // Requires this Moodle version. +$plugin->component = 'portfolio_picasa'; // Full name of the plugin (used for diagnostics). $plugin->cron = 0; diff --git a/repository/googledocs/db/upgrade.php b/repository/googledocs/db/upgrade.php new file mode 100644 index 00000000000..893ed39dced --- /dev/null +++ b/repository/googledocs/db/upgrade.php @@ -0,0 +1,70 @@ +. + +/** + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_repository_googledocs_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012051400) { + // Delete old user preferences containing authsub tokens. + $DB->delete_records('user_preferences', array('name' => 'google_authsub_sesskey')); + upgrade_plugin_savepoint(true, 2012051400, 'repository', 'googledocs'); + } + + if ($oldversion < 2012053000) { + require_once($CFG->dirroot.'/repository/lib.php'); + $existing = $DB->get_record('repository', array('type' => 'googledocs'), '*', IGNORE_MULTIPLE); + + if ($existing) { + $googledocsplugin = new repository_type('googledocs', array(), true); + $googledocsplugin->delete(); + repository_googledocs_admin_upgrade_notification(); + } + + upgrade_plugin_savepoint(true, 2012053000, 'repository', 'googledocs'); + } + + return true; +} + +function repository_googledocs_admin_upgrade_notification() { + $admins = get_admins(); + + if (empty($admins)) { + return; + } + $mainadmin = reset($admins); + + foreach ($admins as $admin) { + $message = new stdClass(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = $mainadmin; + $message->userto = $admin; + $message->smallmessage = get_string('oauth2upgrade_message_small', 'repository_googledocs'); + $message->subject = get_string('oauth2upgrade_message_subject', 'repository_googledocs'); + $message->fullmessage = get_string('oauth2upgrade_message_content', 'repository_googledocs'); + $message->fullmessagehtml = get_string('oauth2upgrade_message_content', 'repository_googledocs'); + $message->fullmessageformat = FORMAT_PLAIN; + $message->notification = 1; + message_send($message); + } +} diff --git a/repository/googledocs/lang/en/repository_googledocs.php b/repository/googledocs/lang/en/repository_googledocs.php index 38f1436b27a..4f7faa7b324 100644 --- a/repository/googledocs/lang/en/repository_googledocs.php +++ b/repository/googledocs/lang/en/repository_googledocs.php @@ -1,5 +1,4 @@ To use the Google Docs repository you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:{$a->callbackurl}
'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Google Docs repository plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Google Docs repository plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must add and reconfigure configure oauth credentials of this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Google Docs repository plugin has been disabled until configured with OAuth2'; $string['pluginname'] = 'Google Docs'; -$string['configplugin'] = 'Configurate Google Docs plugin'; +$string['secret'] = 'Secret'; + diff --git a/repository/googledocs/lib.php b/repository/googledocs/lib.php index 36648bdb81e..6ccb33bc436 100644 --- a/repository/googledocs/lib.php +++ b/repository/googledocs/lib.php @@ -34,55 +34,40 @@ require_once($CFG->libdir.'/googleapi.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_googledocs extends repository { - private $subauthtoken = ''; + private $googleoauth = null; public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { - global $USER; parent::__construct($repositoryid, $context, $options); - // TODO: I wish there was somewhere we could explicitly put this outside of constructor.. - $googletoken = optional_param('token', false, PARAM_RAW); - if($googletoken){ - $gauth = new google_authsub(false, $googletoken); // will throw exception if fails - google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id); - } + $returnurl = new moodle_url('/repository/repository_callback.php', + array('callback' => 'yes', 'repo_id' =>$this->id)); + + $clientid = get_config('googledocs', 'clientid'); + $secret = get_config('googledocs', 'secret'); + $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_docs::REALM); + $this->check_login(); } public function check_login() { - global $USER; - - $sesskey = google_docs::get_sesskey($USER->id); - - if($sesskey){ - try{ - $gauth = new google_authsub($sesskey); - $this->subauthtoken = $sesskey; - return true; - }catch(Exception $e){ - // sesskey is not valid, delete store and re-auth - google_docs::delete_sesskey($USER->id); - } - } - - return false; + return $this->googleoauth->is_logged_in(); } - public function print_login($ajax = true){ - global $CFG; - if($ajax){ - $ret = array(); - $popup_btn = new stdClass(); - $popup_btn->type = 'popup'; - $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id; - $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM); - $ret['login'] = array($popup_btn); - return $ret; + public function print_login() { + $url = $this->googleoauth->get_login_url(); + + if ($this->options['ajax']) { + $popup = new stdClass(); + $popup->type = 'popup'; + $popup->url = $url->out(false); + return array('login' => array($popup)); + } else { + echo ''.get_string('login', 'repository').''; } } public function get_listing($path='', $page = '') { - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); + $gdocs = new google_docs($this->googleoauth); $ret = array(); $ret['dynload'] = true; @@ -91,7 +76,7 @@ class repository_googledocs extends repository { } public function search($search_text, $page = 0) { - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); + $gdocs = new google_docs($this->googleoauth); $ret = array(); $ret['dynload'] = true; @@ -99,37 +84,44 @@ class repository_googledocs extends repository { return $ret; } - public function logout(){ - global $USER; - - $token = google_docs::get_sesskey($USER->id); - - $gauth = new google_authsub($token); - // revoke token from google - $gauth->revoke_session_token(); - - google_docs::delete_sesskey($USER->id); - $this->subauthtoken = ''; - + public function logout() { + $this->googleoauth->log_out(); return parent::logout(); } public function get_file($url, $file = '') { - global $CFG; + $gdocs = new google_docs($this->googleoauth); + $path = $this->prepare_file($file); - - $fp = fopen($path, 'w'); - $gdocs = new google_docs(new google_authsub($this->subauthtoken)); - $gdocs->download_file($url, $fp); - - return array('path'=>$path, 'url'=>$url); + return $gdocs->download_file($url, $path); } public function supported_filetypes() { - return array('document'); + return '*'; } public function supported_returntypes() { return FILE_INTERNAL; } + + public static function get_type_option_names() { + return array('clientid', 'secret', 'pluginname'); + } + + public static function type_config_form($mform, $classname = 'repository') { + + $a = new stdClass; + $a->docsurl = get_docs_url('Google_OAuth2_Setup'); + $a->callbackurl = google_oauth::callback_url()->out(false); + + $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_googledocs', $a)); + + parent::type_config_form($mform); + $mform->addElement('text', 'clientid', get_string('clientid', 'repository_googledocs')); + $mform->addElement('text', 'secret', get_string('secret', 'repository_googledocs')); + + $strrequired = get_string('required'); + $mform->addRule('clientid', $strrequired, 'required', null, 'client'); + $mform->addRule('secret', $strrequired, 'required', null, 'client'); + } } -//Icon from: http://www.iconspedia.com/icon/google-2706.html +// Icon from: http://www.iconspedia.com/icon/google-2706.html. diff --git a/repository/googledocs/version.php b/repository/googledocs/version.php index 79c6db7ff39..2e03f6c9c43 100644 --- a/repository/googledocs/version.php +++ b/repository/googledocs/version.php @@ -25,6 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2011112900; // Requires this Moodle version -$plugin->component = 'repository_googledocs'; // Full name of the plugin (used for diagnostics) +$plugin->version = 2012053000; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2012051100; // Requires this Moodle version. +$plugin->component = 'repository_googledocs'; // Full name of the plugin (used for diagnostics). diff --git a/repository/picasa/db/upgrade.php b/repository/picasa/db/upgrade.php new file mode 100644 index 00000000000..082e5e3433f --- /dev/null +++ b/repository/picasa/db/upgrade.php @@ -0,0 +1,70 @@ +. + +/** + * @param int $oldversion the version we are upgrading from + * @return bool result + */ +function xmldb_repository_picasa_upgrade($oldversion) { + global $CFG, $DB; + + $dbman = $DB->get_manager(); + + if ($oldversion < 2012051400) { + // Delete old user preferences storing authsub tokens. + $DB->delete_records('user_preferences', array('name' => 'google_authsub_sesskey_picasa')); + upgrade_plugin_savepoint(true, 2012051400, 'repository', 'picasa'); + } + + if ($oldversion < 2012053000) { + require_once($CFG->dirroot.'/repository/lib.php'); + $existing = $DB->get_record('repository', array('type' => 'picasa'), '*', IGNORE_MULTIPLE); + + if ($existing) { + $picasaplugin = new repository_type('picasa', array(), true); + $picasaplugin->delete(); + repository_picasa_admin_upgrade_notification(); + } + + upgrade_plugin_savepoint(true, 2012053000, 'repository', 'picasa'); + } + + return true; +} + +function repository_picasa_admin_upgrade_notification() { + $admins = get_admins(); + + if (empty($admins)) { + return; + } + $mainadmin = reset($admins); + + foreach ($admins as $admin) { + $message = new stdClass(); + $message->component = 'moodle'; + $message->name = 'notices'; + $message->userfrom = $mainadmin; + $message->userto = $admin; + $message->smallmessage = get_string('oauth2upgrade_message_small', 'repository_picasa'); + $message->subject = get_string('oauth2upgrade_message_subject', 'repository_picasa'); + $message->fullmessage = get_string('oauth2upgrade_message_content', 'repository_picasa'); + $message->fullmessagehtml = get_string('oauth2upgrade_message_content', 'repository_picasa'); + $message->fullmessageformat = FORMAT_PLAIN; + $message->notification = 1; + message_send($message); + } +} diff --git a/repository/picasa/lang/en/repository_picasa.php b/repository/picasa/lang/en/repository_picasa.php index 23d9dbe66bb..89d75a68079 100644 --- a/repository/picasa/lang/en/repository_picasa.php +++ b/repository/picasa/lang/en/repository_picasa.php @@ -1,5 +1,4 @@ To use the Picasa repository you must be registered with Google. Instructions for registing your installation with Google are described in Moodle Docs. The redirect url should be set to:{$a->callbackurl}
'; +$string['oauth2upgrade_message_subject'] = 'Important information regarding Picasa repository plugin'; +$string['oauth2upgrade_message_content'] = 'As part of the upgrade to Moodle 2.3, the Picasa repository plugin has been disabled due to changes in Googles API. To re-enable your plugin, you must add and reconfigure configure oauth credentials of this plugin.'; +$string['oauth2upgrade_message_small'] = 'The Picasa repository plugin has been disabled until configured with OAuth2'; $string['picasa:view'] = 'View picasa repository'; $string['pluginname'] = 'Picasa web album'; -$string['configplugin'] = 'Picasa repository configuration'; +$string['secret'] = 'Secret'; diff --git a/repository/picasa/lib.php b/repository/picasa/lib.php index 6a83ca08238..902238025fa 100644 --- a/repository/picasa/lib.php +++ b/repository/picasa/lib.php @@ -36,58 +36,40 @@ require_once($CFG->libdir.'/googleapi.php'); * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class repository_picasa extends repository { - private $subauthtoken = ''; + private $googleoauth = null; public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { - global $USER; parent::__construct($repositoryid, $context, $options); - // TODO: I wish there was somewhere we could explicitly put this outside of constructor.. - $googletoken = optional_param('token', false, PARAM_RAW); - if($googletoken){ - $gauth = new google_authsub(false, $googletoken); // will throw exception if fails - google_picasa::set_sesskey($gauth->get_sessiontoken(), $USER->id); - } + $returnurl = new moodle_url('/repository/repository_callback.php', + array('callback' => 'yes', 'repo_id' =>$this->id)); + + $clientid = get_config('picasa', 'clientid'); + $secret = get_config('picasa', 'secret'); + $this->googleoauth = new google_oauth($clientid, $secret, $returnurl->out(false), google_picasa::REALM); + $this->check_login(); } public function check_login() { - global $USER; - - $sesskey = google_picasa::get_sesskey($USER->id); - - if($sesskey){ - try{ - $gauth = new google_authsub($sesskey); - $this->subauthtoken = $sesskey; - return true; - }catch(Exception $e){ - // sesskey is not valid, delete store and re-auth - google_picasa::delete_sesskey($USER->id); - } - } - - return false; + return $this->googleoauth->is_logged_in(); } - public function print_login(){ - global $CFG; - $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id; - $authurl = google_authsub::login_url($returnurl, google_picasa::REALM); - if($this->options['ajax']){ - $ret = array(); - $popup_btn = new stdClass(); - $popup_btn->type = 'popup'; - $popup_btn->url = $authurl; - $ret['login'] = array($popup_btn); - return $ret; + public function print_login() { + $url = $this->googleoauth->get_login_url(); + + if ($this->options['ajax']) { + $popup = new stdClass(); + $popup->type = 'popup'; + $popup->url = $url->out(false); + return array('login' => array($popup)); } else { - echo 'Login'; + echo ''.get_string('login', 'repository').''; } } public function get_listing($path='', $page = '') { - $picasa = new google_picasa(new google_authsub($this->subauthtoken)); + $picasa = new google_picasa($this->googleoauth); $ret = array(); $ret['dynload'] = true; @@ -101,7 +83,7 @@ class repository_picasa extends repository { } public function search($search_text, $page = 0) { - $picasa = new google_picasa(new google_authsub($this->subauthtoken)); + $picasa = new google_picasa($this->googleoauth); $ret = array(); $ret['manage'] = google_picasa::MANAGE_URL; @@ -109,22 +91,12 @@ class repository_picasa extends repository { return $ret; } - public function logout(){ - global $USER; - - $token = google_picasa::get_sesskey($USER->id); - - $gauth = new google_authsub($token); - // revoke token from google - $gauth->revoke_session_token(); - - google_picasa::delete_sesskey($USER->id); - $this->subauthtoken = ''; - + public function logout() { + $this->googleoauth->log_out(); return parent::logout(); } - public function get_name(){ + public function get_name() { return get_string('pluginname', 'repository_picasa'); } public function supported_filetypes() { @@ -133,7 +105,27 @@ class repository_picasa extends repository { public function supported_returntypes() { return (FILE_INTERNAL | FILE_EXTERNAL); } + + public static function get_type_option_names() { + return array('clientid', 'secret', 'pluginname'); + } + + public static function type_config_form($mform, $classname = 'repository') { + $a = new stdClass; + $a->docsurl = get_docs_url('Google_OAuth2_Setup'); + $a->callbackurl = google_oauth::callback_url()->out(false); + + $mform->addElement('static', null, '', get_string('oauthinfo', 'repository_picasa', $a)); + + parent::type_config_form($mform); + $mform->addElement('text', 'clientid', get_string('clientid', 'repository_picasa')); + $mform->addElement('text', 'secret', get_string('secret', 'repository_picasa')); + + $strrequired = get_string('required'); + $mform->addRule('clientid', $strrequired, 'required', null, 'client'); + $mform->addRule('secret', $strrequired, 'required', null, 'client'); + } } // Icon for this plugin retrieved from http://www.iconspedia.com/icon/picasa-2711.html -// Where the license is said documented to be Free +// Where the license is said documented to be Free. diff --git a/repository/picasa/version.php b/repository/picasa/version.php index 5ee9d18b00a..c6dc0fc84ff 100644 --- a/repository/picasa/version.php +++ b/repository/picasa/version.php @@ -26,6 +26,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2011112900; // The current plugin version (Date: YYYYMMDDXX) -$plugin->requires = 2011112900; // Requires this Moodle version -$plugin->component = 'repository_picasa'; // Full name of the plugin (used for diagnostics) +$plugin->version = 2012053000; // The current plugin version (Date: YYYYMMDDXX). +$plugin->requires = 2012051100; // Requires this Moodle version. +$plugin->component = 'repository_picasa'; // Full name of the plugin (used for diagnostics).