[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Google Documents Portfolio Plugin 19 * 20 * @author Dan Poltawski <talktodan@gmail.com> 21 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 22 */ 23 require_once($CFG->libdir.'/portfolio/plugin.php'); 24 require_once($CFG->libdir . '/google/lib.php'); 25 26 class portfolio_plugin_googledocs extends portfolio_plugin_push_base { 27 /** 28 * Google Client. 29 * @var Google_Client 30 */ 31 private $client = null; 32 33 /** 34 * Google Drive Service. 35 * @var Google_Service_Drive 36 */ 37 private $service = null; 38 39 /** 40 * URL to redirect Google to. 41 * @var string 42 */ 43 const REDIRECTURL = '/admin/oauth2callback.php'; 44 /** 45 * Key in session which stores token (_drive_file is access level). 46 * @var string 47 */ 48 const SESSIONKEY = 'googledrive_accesstoken_drive_file'; 49 50 public function supported_formats() { 51 return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_RICHHTML); 52 } 53 54 public static function get_name() { 55 return get_string('pluginname', 'portfolio_googledocs'); 56 } 57 58 public function prepare_package() { 59 // We send the files as they are, no prep required. 60 return true; 61 } 62 63 public function get_interactive_continue_url() { 64 return 'http://drive.google.com/'; 65 } 66 67 public function expected_time($callertime) { 68 // We're forcing this to be run 'interactively' because the plugin 69 // does not support running in cron. 70 return PORTFOLIO_TIME_LOW; 71 } 72 73 public function send_package() { 74 if (!$this->client) { 75 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); 76 } 77 78 foreach ($this->exporter->get_tempfiles() as $file) { 79 try { 80 // Create drivefile object and fill it with data. 81 $drivefile = new Google_Service_Drive_DriveFile(); 82 $drivefile->setTitle($file->get_filename()); 83 $drivefile->setMimeType($file->get_mimetype()); 84 85 $filecontent = $file->get_content(); 86 $createdfile = $this->service->files->insert($drivefile, 87 array('data' => $filecontent, 88 'mimeType' => $file->get_mimetype(), 89 'uploadType' => 'multipart')); 90 } catch ( Exception $e ) { 91 throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); 92 } 93 } 94 return true; 95 } 96 /** 97 * Gets the access token from session and sets it to client. 98 * 99 * @return null|string null or token. 100 */ 101 private function get_access_token() { 102 global $SESSION; 103 if (isset($SESSION->{self::SESSIONKEY}) && $SESSION->{self::SESSIONKEY}) { 104 $this->client->setAccessToken($SESSION->{self::SESSIONKEY}); 105 return $SESSION->{self::SESSIONKEY}; 106 } 107 return null; 108 } 109 /** 110 * Sets the access token to session 111 * 112 * @param string $token access token in json format 113 * @return 114 */ 115 private function set_access_token($token) { 116 global $SESSION; 117 $SESSION->{self::SESSIONKEY} = $token; 118 } 119 120 public function steal_control($stage) { 121 global $CFG; 122 if ($stage != PORTFOLIO_STAGE_CONFIG) { 123 return false; 124 } 125 126 $this->initialize_oauth(); 127 if ($this->get_access_token()) { 128 // Ensure that token is not expired. 129 if (!$this->client->isAccessTokenExpired()) { 130 return false; 131 } 132 } 133 return $this->client->createAuthUrl(); 134 135 } 136 137 public function post_control($stage, $params) { 138 if ($stage != PORTFOLIO_STAGE_CONFIG) { 139 return; 140 } 141 // Get the authentication code send by Google. 142 $code = isset($params['oauth2code']) ? $params['oauth2code'] : null; 143 // Try to authenticate (throws exception which is catched higher). 144 $this->client->authenticate($code); 145 // Make sure we accually have access token at this time 146 // ...and store it for further use. 147 if ($accesstoken = $this->client->getAccessToken()) { 148 $this->set_access_token($accesstoken); 149 } else { 150 throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_gdocs'); 151 } 152 } 153 154 public static function allows_multiple_instances() { 155 return false; 156 } 157 158 public static function has_admin_config() { 159 return true; 160 } 161 162 public static function get_allowed_config() { 163 return array('clientid', 'secret'); 164 } 165 166 public static function admin_config_form(&$mform) { 167 $a = new stdClass; 168 $a->docsurl = get_docs_url('Google_OAuth_2.0_setup'); 169 $a->callbackurl = (new moodle_url(self::REDIRECTURL))->out(false); 170 171 $mform->addElement('static', null, '', get_string('oauthinfo', 'portfolio_googledocs', $a)); 172 173 $mform->addElement('text', 'clientid', get_string('clientid', 'portfolio_googledocs')); 174 $mform->setType('clientid', PARAM_RAW_TRIMMED); 175 $mform->addElement('text', 'secret', get_string('secret', 'portfolio_googledocs')); 176 $mform->setType('secret', PARAM_RAW_TRIMMED); 177 178 $strrequired = get_string('required'); 179 $mform->addRule('clientid', $strrequired, 'required', null, 'client'); 180 $mform->addRule('secret', $strrequired, 'required', null, 'client'); 181 } 182 183 private function initialize_oauth() { 184 $redirecturi = new moodle_url(self::REDIRECTURL); 185 $returnurl = new moodle_url('/portfolio/add.php'); 186 $returnurl->param('postcontrol', 1); 187 $returnurl->param('id', $this->exporter->get('id')); 188 $returnurl->param('sesskey', sesskey()); 189 190 $clientid = $this->get_config('clientid'); 191 $secret = $this->get_config('secret'); 192 193 // Setup Google client. 194 $this->client = get_google_client(); 195 $this->client->setClientId($clientid); 196 $this->client->setClientSecret($secret); 197 $this->client->setScopes(array(Google_Service_Drive::DRIVE_FILE)); 198 $this->client->setRedirectUri($redirecturi->out(false)); 199 // URL to be called when redirecting from authentication. 200 $this->client->setState($returnurl->out_as_local_url(false)); 201 // Setup drive upload service. 202 $this->service = new Google_Service_Drive($this->client); 203 204 } 205 206 public function instance_sanity_check() { 207 $clientid = $this->get_config('clientid'); 208 $secret = $this->get_config('secret'); 209 210 // If there is no oauth config (e.g. plugins upgraded from < 2.3 then 211 // there will be no config and this plugin should be disabled. 212 if (empty($clientid) or empty($secret)) { 213 return 'nooauthcredentials'; 214 } 215 return 0; 216 } 217 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |