Parent Directory
|
Revision Log
Revision 1.10 - (view) (download)
| 1 : | samhemelryk | 1.7 | <?php |
| 2 : | poltawski | 1.1 | /** |
| 3 : | * Moodle - Modular Object-Oriented Dynamic Learning Environment | ||
| 4 : | * http://moodle.org | ||
| 5 : | * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com | ||
| 6 : | * | ||
| 7 : | * This program is free software: you can redistribute it and/or modify | ||
| 8 : | * it under the terms of the GNU General Public License as published by | ||
| 9 : | * the Free Software Foundation, either version 2 of the License, or | ||
| 10 : | * (at your option) any later version. | ||
| 11 : | * | ||
| 12 : | * This program is distributed in the hope that it will be useful, | ||
| 13 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 14 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 15 : | * GNU General Public License for more details. | ||
| 16 : | * | ||
| 17 : | * You should have received a copy of the GNU General Public License | ||
| 18 : | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 19 : | * | ||
| 20 : | samhemelryk | 1.7 | * @package moodlecore |
| 21 : | poltawski | 1.1 | * @subpackage lib |
| 22 : | samhemelryk | 1.7 | * @copyright Dan Poltawski <talktodan@gmail.com> |
| 23 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 24 : | poltawski | 1.1 | * |
| 25 : | * Simple implementation of some Google API functions for Moodle. | ||
| 26 : | */ | ||
| 27 : | |||
| 28 : | samhemelryk | 1.7 | /** Include essential file */ |
| 29 : | poltawski | 1.1 | require_once($CFG->libdir.'/filelib.php'); |
| 30 : | |||
| 31 : | /** | ||
| 32 : | skodak | 1.9 | * Base class for google authenticated http requests |
| 33 : | * | ||
| 34 : | * Most Google API Calls required that requests are sent with an | ||
| 35 : | poltawski | 1.1 | * Authorization header + token. This class extends the curl class |
| 36 : | * to aid this | ||
| 37 : | samhemelryk | 1.7 | * |
| 38 : | * @package moodlecore | ||
| 39 : | * @subpackage lib | ||
| 40 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 41 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 42 : | poltawski | 1.1 | */ |
| 43 : | abstract class google_auth_request extends curl{ | ||
| 44 : | protected $token = ''; | ||
| 45 : | poltawski | 1.10 | private $persistantheaders = array(); |
| 46 : | poltawski | 1.1 | |
| 47 : | // Must be overriden with the authorization header name | ||
| 48 : | public abstract static function get_auth_header_name(); | ||
| 49 : | |||
| 50 : | protected function request($url, $options = array()){ | ||
| 51 : | if($this->token){ | ||
| 52 : | // Adds authorisation head to a request so that it can be authentcated | ||
| 53 : | $this->setHeader('Authorization: '. $this->get_auth_header_name().'"'.$this->token.'"'); | ||
| 54 : | } | ||
| 55 : | |||
| 56 : | poltawski | 1.10 | foreach($this->persistantheaders as $h){ |
| 57 : | $this->setHeader($h); | ||
| 58 : | } | ||
| 59 : | |||
| 60 : | poltawski | 1.1 | $ret = parent::request($url, $options); |
| 61 : | // reset headers for next request | ||
| 62 : | $this->header = array(); | ||
| 63 : | return $ret; | ||
| 64 : | } | ||
| 65 : | |||
| 66 : | poltawski | 1.6 | protected function multi($requests, $options = array()) { |
| 67 : | if($this->token){ | ||
| 68 : | // Adds authorisation head to a request so that it can be authentcated | ||
| 69 : | $this->setHeader('Authorization: '. $this->get_auth_header_name().'"'.$this->token.'"'); | ||
| 70 : | } | ||
| 71 : | |||
| 72 : | poltawski | 1.10 | foreach($this->persistantheaders as $h){ |
| 73 : | $this->setHeader($h); | ||
| 74 : | } | ||
| 75 : | |||
| 76 : | poltawski | 1.6 | $ret = parent::multi($requests, $options); |
| 77 : | // reset headers for next request | ||
| 78 : | $this->header = array(); | ||
| 79 : | return $ret; | ||
| 80 : | } | ||
| 81 : | |||
| 82 : | poltawski | 1.1 | public function get_sessiontoken(){ |
| 83 : | return $this->token; | ||
| 84 : | } | ||
| 85 : | poltawski | 1.10 | |
| 86 : | public function add_persistant_header($header){ | ||
| 87 : | $this->persistantheaders[] = $header; | ||
| 88 : | } | ||
| 89 : | poltawski | 1.1 | } |
| 90 : | |||
| 91 : | /******* | ||
| 92 : | skodak | 1.9 | * The following two classes are usd to implement AuthSub google |
| 93 : | poltawski | 1.1 | * authtentication, as documented here: |
| 94 : | * http://code.google.com/apis/accounts/docs/AuthSub.html | ||
| 95 : | *******/ | ||
| 96 : | |||
| 97 : | /** | ||
| 98 : | * Used to uprade a google AuthSubRequest one-time token into | ||
| 99 : | * a session token which can be used long term. | ||
| 100 : | samhemelryk | 1.7 | * |
| 101 : | * @package moodlecore | ||
| 102 : | * @subpackage lib | ||
| 103 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 104 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 105 : | poltawski | 1.1 | */ |
| 106 : | class google_authsub_request extends google_auth_request { | ||
| 107 : | const AUTHSESSION_URL = 'https://www.google.com/accounts/AuthSubSessionToken'; | ||
| 108 : | |||
| 109 : | /** | ||
| 110 : | skodak | 1.9 | * Constructor. Calls constructor of its parents |
| 111 : | poltawski | 1.1 | * |
| 112 : | * @param string $authtoken The token to upgrade to a session token | ||
| 113 : | */ | ||
| 114 : | public function __construct($authtoken){ | ||
| 115 : | parent::__construct(); | ||
| 116 : | $this->token = $authtoken; | ||
| 117 : | } | ||
| 118 : | |||
| 119 : | /** | ||
| 120 : | skodak | 1.9 | * Requests a long-term session token from google based on the |
| 121 : | poltawski | 1.1 | * |
| 122 : | skodak | 1.9 | * @return string Sub-Auth token |
| 123 : | poltawski | 1.1 | */ |
| 124 : | public function get_session_token(){ | ||
| 125 : | $content = $this->get(google_authsub_request::AUTHSESSION_URL); | ||
| 126 : | |||
| 127 : | if( preg_match('/token=(.*)/i', $content, $matches) ){ | ||
| 128 : | return $matches[1]; | ||
| 129 : | }else{ | ||
| 130 : | throw new moodle_exception('could not upgrade google authtoken to session token'); | ||
| 131 : | } | ||
| 132 : | } | ||
| 133 : | |||
| 134 : | public static function get_auth_header_name(){ | ||
| 135 : | return 'AuthSub token='; | ||
| 136 : | } | ||
| 137 : | } | ||
| 138 : | |||
| 139 : | /** | ||
| 140 : | * Allows http calls using google subauth authorisation | ||
| 141 : | samhemelryk | 1.7 | * |
| 142 : | * @package moodlecore | ||
| 143 : | * @subpackage lib | ||
| 144 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 145 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 146 : | poltawski | 1.1 | */ |
| 147 : | class google_authsub extends google_auth_request { | ||
| 148 : | const LOGINAUTH_URL = 'https://www.google.com/accounts/AuthSubRequest'; | ||
| 149 : | const VERIFY_TOKEN_URL = 'https://www.google.com/accounts/AuthSubTokenInfo'; | ||
| 150 : | const REVOKE_TOKEN_URL = 'https://www.google.com/accounts/AuthSubRevokeToken'; | ||
| 151 : | |||
| 152 : | /** | ||
| 153 : | skodak | 1.9 | * Constructor, allows subauth requests using the response from an initial |
| 154 : | poltawski | 1.1 | * AuthSubRequest or with the subauth long-term token. Note that constructing |
| 155 : | * this object without a valid token will cause an exception to be thrown. | ||
| 156 : | * | ||
| 157 : | * @param string $sessiontoken A long-term subauth session token | ||
| 158 : | * @param string $authtoken A one-time auth token wich is used to upgrade to session token | ||
| 159 : | * @param mixed @options Options to pass to the base curl object | ||
| 160 : | */ | ||
| 161 : | public function __construct($sessiontoken = '', $authtoken = '', $options = array()){ | ||
| 162 : | parent::__construct($options); | ||
| 163 : | |||
| 164 : | if( $authtoken ){ | ||
| 165 : | $gauth = new google_authsub_request($authtoken); | ||
| 166 : | $sessiontoken = $gauth->get_session_token(); | ||
| 167 : | } | ||
| 168 : | |||
| 169 : | $this->token = $sessiontoken; | ||
| 170 : | if(! $this->valid_token() ){ | ||
| 171 : | throw new moodle_exception('Invalid subauth token'); | ||
| 172 : | } | ||
| 173 : | } | ||
| 174 : | |||
| 175 : | /** | ||
| 176 : | * Tests if a subauth token used is valid | ||
| 177 : | * | ||
| 178 : | * @return boolean true if token valid | ||
| 179 : | */ | ||
| 180 : | public function valid_token(){ | ||
| 181 : | $this->get(google_authsub::VERIFY_TOKEN_URL); | ||
| 182 : | |||
| 183 : | if($this->info['http_code'] === 200){ | ||
| 184 : | return true; | ||
| 185 : | }else{ | ||
| 186 : | return false; | ||
| 187 : | } | ||
| 188 : | } | ||
| 189 : | |||
| 190 : | /** | ||
| 191 : | * Calls googles api to revoke the subauth token | ||
| 192 : | * | ||
| 193 : | * @return boolean Returns true if token succesfully revoked | ||
| 194 : | */ | ||
| 195 : | public function revoke_session_token(){ | ||
| 196 : | $this->get(google_authsub::REVOKE_TOKEN_URL); | ||
| 197 : | |||
| 198 : | if($this->info['http_code'] === 200){ | ||
| 199 : | $this->token = ''; | ||
| 200 : | return true; | ||
| 201 : | }else{ | ||
| 202 : | return false; | ||
| 203 : | } | ||
| 204 : | } | ||
| 205 : | |||
| 206 : | /** | ||
| 207 : | * Creates a login url for subauth request | ||
| 208 : | * | ||
| 209 : | * @param string $returnaddr The address which the user should be redirected to recieve the token | ||
| 210 : | skodak | 1.9 | * @param string $realm The google realm which is access is being requested |
| 211 : | poltawski | 1.1 | * @return string URL to bounce the user to |
| 212 : | */ | ||
| 213 : | public static function login_url($returnaddr, $realm){ | ||
| 214 : | $uri = google_authsub::LOGINAUTH_URL.'?next=' | ||
| 215 : | .urlencode($returnaddr) | ||
| 216 : | .'&scope=' | ||
| 217 : | .urlencode($realm) | ||
| 218 : | .'&session=1&secure=0'; | ||
| 219 : | |||
| 220 : | return $uri; | ||
| 221 : | } | ||
| 222 : | |||
| 223 : | public static function get_auth_header_name(){ | ||
| 224 : | return 'AuthSub token='; | ||
| 225 : | } | ||
| 226 : | } | ||
| 227 : | |||
| 228 : | /** | ||
| 229 : | * Class for manipulating google documents through the google data api | ||
| 230 : | * Docs for this can be found here: | ||
| 231 : | samhemelryk | 1.7 | * {@link http://code.google.com/apis/documents/docs/2.0/developers_guide_protocol.html} |
| 232 : | * | ||
| 233 : | * @package moodlecore | ||
| 234 : | * @subpackage lib | ||
| 235 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 236 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 237 : | poltawski | 1.1 | */ |
| 238 : | class google_docs { | ||
| 239 : | poltawski | 1.6 | // need both docs and the spreadsheets realm |
| 240 : | poltawski | 1.10 | const REALM = 'http://docs.google.com/feeds/ http://spreadsheets.google.com/feeds/ http://docs.googleusercontent.com/'; |
| 241 : | const DOCUMENTFEED_URL = 'http://docs.google.com/feeds/default/private/full'; | ||
| 242 : | poltawski | 1.1 | const USER_PREF_NAME = 'google_authsub_sesskey'; |
| 243 : | |||
| 244 : | private $google_curl = null; | ||
| 245 : | |||
| 246 : | /** | ||
| 247 : | * Constructor. | ||
| 248 : | * | ||
| 249 : | * @param object A google_auth_request object which can be used to do http requests | ||
| 250 : | */ | ||
| 251 : | public function __construct($google_curl){ | ||
| 252 : | if(is_a($google_curl, 'google_auth_request')){ | ||
| 253 : | $this->google_curl = $google_curl; | ||
| 254 : | poltawski | 1.10 | $this->google_curl->add_persistant_header('GData-Version: 3.0'); |
| 255 : | poltawski | 1.1 | }else{ |
| 256 : | throw new moodle_exception('Google Curl Request object not given'); | ||
| 257 : | } | ||
| 258 : | } | ||
| 259 : | |||
| 260 : | public static function get_sesskey($userid){ | ||
| 261 : | return get_user_preferences(google_docs::USER_PREF_NAME, false, $userid); | ||
| 262 : | } | ||
| 263 : | |||
| 264 : | public static function set_sesskey($value, $userid){ | ||
| 265 : | return set_user_preference(google_docs::USER_PREF_NAME, $value, $userid); | ||
| 266 : | } | ||
| 267 : | |||
| 268 : | public static function delete_sesskey($userid){ | ||
| 269 : | return unset_user_preference(google_docs::USER_PREF_NAME, $userid); | ||
| 270 : | } | ||
| 271 : | |||
| 272 : | /** | ||
| 273 : | * Returns a list of files the user has formated for files api | ||
| 274 : | * | ||
| 275 : | * @param string $search A search string to do full text search on the documents | ||
| 276 : | * @return mixed Array of files formated for fileapoi | ||
| 277 : | */ | ||
| 278 : | #FIXME | ||
| 279 : | public function get_file_list($search = ''){ | ||
| 280 : | poltawski | 1.10 | global $CFG, $OUTPUT; |
| 281 : | poltawski | 1.1 | $url = google_docs::DOCUMENTFEED_URL; |
| 282 : | |||
| 283 : | if($search){ | ||
| 284 : | $url.='?q='.urlencode($search); | ||
| 285 : | } | ||
| 286 : | $content = $this->google_curl->get($url); | ||
| 287 : | |||
| 288 : | $xml = new SimpleXMLElement($content); | ||
| 289 : | |||
| 290 : | poltawski | 1.6 | |
| 291 : | |||
| 292 : | poltawski | 1.10 | |
| 293 : | poltawski | 1.1 | $files = array(); |
| 294 : | foreach($xml->entry as $gdoc){ | ||
| 295 : | |||
| 296 : | poltawski | 1.10 | $docid = (string) $gdoc->children('http://schemas.google.com/g/2005')->resourceId; |
| 297 : | list($type) = explode(':', $docid); | ||
| 298 : | |||
| 299 : | $title = ''; | ||
| 300 : | $source = ''; | ||
| 301 : | // FIXME: We're making hard-coded choices about format here. | ||
| 302 : | // If the repo api can support it, we could let the user | ||
| 303 : | // chose. | ||
| 304 : | switch($type){ | ||
| 305 : | skodak | 1.9 | case 'document': |
| 306 : | poltawski | 1.6 | $title = $gdoc->title.'.rtf'; |
| 307 : | $source = 'http://docs.google.com/feeds/download/documents/Export?docID='.$docid.'&exportFormat=rtf'; | ||
| 308 : | break; | ||
| 309 : | case 'presentation': | ||
| 310 : | $title = $gdoc->title.'.ppt'; | ||
| 311 : | $source = 'http://docs.google.com/feeds/download/presentations/Export?docID='.$docid.'&exportFormat=ppt'; | ||
| 312 : | break; | ||
| 313 : | case 'spreadsheet': | ||
| 314 : | $title = $gdoc->title.'.xls'; | ||
| 315 : | poltawski | 1.10 | $source = 'http://spreadsheets.google.com/feeds/download/spreadsheets/Export?key='.$docid.'&exportFormat=xls'; |
| 316 : | poltawski | 1.6 | break; |
| 317 : | poltawski | 1.10 | case 'pdf': |
| 318 : | $title = (string)$gdoc->title; | ||
| 319 : | $source = (string)$gdoc->content[0]->attributes()->src; | ||
| 320 : | break; | ||
| 321 : | } | ||
| 322 : | poltawski | 1.6 | |
| 323 : | poltawski | 1.10 | if(!empty($source)){ |
| 324 : | skodak | 1.9 | $files[] = array( 'title' => $title, |
| 325 : | poltawski | 1.6 | 'url' => "{$gdoc->link[0]->attributes()->href}", |
| 326 : | skodak | 1.9 | 'source' => $source, |
| 327 : | poltawski | 1.6 | 'date' => usertime(strtotime($gdoc->updated)), |
| 328 : | poltawski | 1.10 | 'thumbnail' => $OUTPUT->old_icon_url(file_extension_icon($title, 32)) |
| 329 : | poltawski | 1.6 | ); |
| 330 : | } | ||
| 331 : | poltawski | 1.1 | } |
| 332 : | |||
| 333 : | return $files; | ||
| 334 : | } | ||
| 335 : | |||
| 336 : | /** | ||
| 337 : | * Sends a file object to google documents | ||
| 338 : | * | ||
| 339 : | * @param object $file File object | ||
| 340 : | * @return boolean True on success | ||
| 341 : | */ | ||
| 342 : | public function send_file($file){ | ||
| 343 : | $this->google_curl->setHeader("Content-Length: ". $file->get_filesize()); | ||
| 344 : | $this->google_curl->setHeader("Content-Type: ". $file->get_mimetype()); | ||
| 345 : | $this->google_curl->setHeader("Slug: ". $file->get_filename()); | ||
| 346 : | |||
| 347 : | $this->google_curl->post(google_docs::DOCUMENTFEED_URL, $file->get_content()); | ||
| 348 : | |||
| 349 : | if($this->google_curl->info['http_code'] === 201){ | ||
| 350 : | return true; | ||
| 351 : | }else{ | ||
| 352 : | return false; | ||
| 353 : | } | ||
| 354 : | } | ||
| 355 : | |||
| 356 : | public function download_file($url, $fp){ | ||
| 357 : | return $this->google_curl->download(array( array('url'=>$url, 'file' => $fp) )); | ||
| 358 : | } | ||
| 359 : | } | ||
| 360 : | |||
| 361 : | /** | ||
| 362 : | * Class for manipulating picasa through the google data api | ||
| 363 : | * Docs for this can be found here: | ||
| 364 : | samhemelryk | 1.7 | * {@link http://code.google.com/apis/picasaweb/developers_guide_protocol.html} |
| 365 : | * | ||
| 366 : | * @package moodlecore | ||
| 367 : | * @subpackage lib | ||
| 368 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 369 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 370 : | poltawski | 1.1 | */ |
| 371 : | class google_picasa { | ||
| 372 : | const REALM = 'http://picasaweb.google.com/data/'; | ||
| 373 : | const USER_PREF_NAME = 'google_authsub_sesskey_picasa'; | ||
| 374 : | const UPLOAD_LOCATION = 'http://picasaweb.google.com/data/feed/api/user/default/albumid/default'; | ||
| 375 : | poltawski | 1.2 | const ALBUM_PHOTO_LIST = 'http://picasaweb.google.com/data/feed/api/user/default/albumid/'; |
| 376 : | const PHOTO_SEARCH_URL = 'http://picasaweb.google.com/data/feed/api/user/default?kind=photo&q='; | ||
| 377 : | const LIST_ALBUMS_URL = 'http://picasaweb.google.com/data/feed/api/user/default'; | ||
| 378 : | poltawski | 1.1 | |
| 379 : | private $google_curl = null; | ||
| 380 : | |||
| 381 : | /** | ||
| 382 : | * Constructor. | ||
| 383 : | * | ||
| 384 : | * @param object A google_auth_request object which can be used to do http requests | ||
| 385 : | */ | ||
| 386 : | public function __construct($google_curl){ | ||
| 387 : | if(is_a($google_curl, 'google_auth_request')){ | ||
| 388 : | $this->google_curl = $google_curl; | ||
| 389 : | poltawski | 1.10 | $this->google_curl->add_persistant_header('GData-Version: 2'); |
| 390 : | poltawski | 1.1 | }else{ |
| 391 : | throw new moodle_exception('Google Curl Request object not given'); | ||
| 392 : | } | ||
| 393 : | } | ||
| 394 : | |||
| 395 : | public static function get_sesskey($userid){ | ||
| 396 : | return get_user_preferences(google_picasa::USER_PREF_NAME, false, $userid); | ||
| 397 : | } | ||
| 398 : | |||
| 399 : | public static function set_sesskey($value, $userid){ | ||
| 400 : | return set_user_preference(google_picasa::USER_PREF_NAME, $value, $userid); | ||
| 401 : | } | ||
| 402 : | |||
| 403 : | public static function delete_sesskey($userid){ | ||
| 404 : | return unset_user_preference(google_picasa::USER_PREF_NAME, $userid); | ||
| 405 : | } | ||
| 406 : | |||
| 407 : | /** | ||
| 408 : | * Sends a file object to picasaweb | ||
| 409 : | * | ||
| 410 : | * @param object $file File object | ||
| 411 : | * @return boolean True on success | ||
| 412 : | */ | ||
| 413 : | public function send_file($file){ | ||
| 414 : | $this->google_curl->setHeader("Content-Length: ". $file->get_filesize()); | ||
| 415 : | $this->google_curl->setHeader("Content-Type: ". $file->get_mimetype()); | ||
| 416 : | $this->google_curl->setHeader("Slug: ". $file->get_filename()); | ||
| 417 : | |||
| 418 : | $this->google_curl->post(google_picasa::UPLOAD_LOCATION, $file->get_content()); | ||
| 419 : | |||
| 420 : | if($this->google_curl->info['http_code'] === 201){ | ||
| 421 : | return true; | ||
| 422 : | }else{ | ||
| 423 : | return false; | ||
| 424 : | } | ||
| 425 : | } | ||
| 426 : | poltawski | 1.2 | |
| 427 : | /** | ||
| 428 : | * Returns list of photos for file picker. | ||
| 429 : | * If top level then returns list of albums, otherwise | ||
| 430 : | * photos within an album. | ||
| 431 : | * | ||
| 432 : | * @param string $path The path to files (assumed to be albumid) | ||
| 433 : | * @return mixed $files A list of files for the file picker | ||
| 434 : | */ | ||
| 435 : | public function get_file_list($path = ''){ | ||
| 436 : | if(!$path){ | ||
| 437 : | return $this->get_albums(); | ||
| 438 : | }else{ | ||
| 439 : | return $this->get_album_photos($path); | ||
| 440 : | } | ||
| 441 : | } | ||
| 442 : | |||
| 443 : | /** | ||
| 444 : | * Returns list of photos in album specified | ||
| 445 : | * | ||
| 446 : | * @param int $albumid Photo album to list photos from | ||
| 447 : | * @return mixed $files A list of files for the file picker | ||
| 448 : | */ | ||
| 449 : | public function get_album_photos($albumid){ | ||
| 450 : | $albumcontent = $this->google_curl->get(google_picasa::ALBUM_PHOTO_LIST.$albumid); | ||
| 451 : | |||
| 452 : | return $this->get_photo_details($albumcontent); | ||
| 453 : | } | ||
| 454 : | |||
| 455 : | /** | ||
| 456 : | skodak | 1.9 | * Does text search on the users photos and returns |
| 457 : | poltawski | 1.2 | * matches in format for picasa api |
| 458 : | * | ||
| 459 : | * @param string $query Search terms | ||
| 460 : | * @return mixed $files A list of files for the file picker | ||
| 461 : | */ | ||
| 462 : | public function do_photo_search($query){ | ||
| 463 : | $content = $this->google_curl->get(google_picasa::PHOTO_SEARCH_URL.htmlentities($query)); | ||
| 464 : | |||
| 465 : | return $this->get_photo_details($content); | ||
| 466 : | } | ||
| 467 : | |||
| 468 : | /** | ||
| 469 : | * Gets all the users albums and returns them as a list of folders | ||
| 470 : | * for the file picker | ||
| 471 : | * | ||
| 472 : | * @return mixes $files Array in the format get_listing uses for folders | ||
| 473 : | */ | ||
| 474 : | public function get_albums(){ | ||
| 475 : | $content = $this->google_curl->get(google_picasa::LIST_ALBUMS_URL); | ||
| 476 : | $xml = new SimpleXMLElement($content); | ||
| 477 : | |||
| 478 : | $files = array(); | ||
| 479 : | |||
| 480 : | foreach($xml->entry as $album){ | ||
| 481 : | $gphoto = $album->children('http://schemas.google.com/photos/2007'); | ||
| 482 : | |||
| 483 : | $mediainfo = $album->children('http://search.yahoo.com/mrss/'); | ||
| 484 : | //hacky... | ||
| 485 : | $thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes(); | ||
| 486 : | |||
| 487 : | $files[] = array( 'title' => (string) $gphoto->name, | ||
| 488 : | 'date' => userdate($gphoto->timestamp), | ||
| 489 : | 'size' => (int) $gphoto->bytesUsed, | ||
| 490 : | 'path' => (string) $gphoto->id, | ||
| 491 : | 'thumbnail' => (string) $thumbnailinfo['url'], | ||
| 492 : | moodler | 1.3 | 'thumbnail_width' => 160, // 160 is the native maximum dimension |
| 493 : | 'thumbnail_height' => 160, | ||
| 494 : | poltawski | 1.2 | 'children' => array(), |
| 495 : | ); | ||
| 496 : | |||
| 497 : | } | ||
| 498 : | |||
| 499 : | return $files; | ||
| 500 : | } | ||
| 501 : | |||
| 502 : | /** | ||
| 503 : | * Recieves XML from a picasa list of photos and returns | ||
| 504 : | * array in format for file picker. | ||
| 505 : | * | ||
| 506 : | * @param string $rawxml XML from picasa api | ||
| 507 : | * @return mixed $files A list of files for the file picker | ||
| 508 : | */ | ||
| 509 : | public function get_photo_details($rawxml){ | ||
| 510 : | |||
| 511 : | $xml = new SimpleXMLElement($rawxml); | ||
| 512 : | |||
| 513 : | $files = array(); | ||
| 514 : | |||
| 515 : | foreach($xml->entry as $photo){ | ||
| 516 : | $gphoto = $photo->children('http://schemas.google.com/photos/2007'); | ||
| 517 : | |||
| 518 : | $mediainfo = $photo->children('http://search.yahoo.com/mrss/'); | ||
| 519 : | $fullinfo = $mediainfo->group->content->attributes(); | ||
| 520 : | //hacky... | ||
| 521 : | $thumbnailinfo = $mediainfo->group->thumbnail[0]->attributes(); | ||
| 522 : | |||
| 523 : | moodler | 1.4 | // Derive the nicest file name we can |
| 524 : | if (!empty($mediainfo->group->description)) { | ||
| 525 : | $title = shorten_text((string)$mediainfo->group->description, 20, false, ''); | ||
| 526 : | $title = clean_filename($title).'.jpg'; | ||
| 527 : | } else { | ||
| 528 : | $title = (string)$mediainfo->group->title; | ||
| 529 : | } | ||
| 530 : | |||
| 531 : | $files[] = array( | ||
| 532 : | 'title' => $title, | ||
| 533 : | poltawski | 1.2 | 'date' => userdate($gphoto->timestamp), |
| 534 : | 'size' => (int) $gphoto->size, | ||
| 535 : | 'path' => $gphoto->albumid.'/'.$gphoto->id, | ||
| 536 : | 'thumbnail' => (string) $thumbnailinfo['url'], | ||
| 537 : | moodler | 1.3 | 'thumbnail_width' => 72, // 72 is the native maximum dimension |
| 538 : | 'thumbnail_height' => 72, | ||
| 539 : | moodler | 1.5 | 'source' => (string) $fullinfo['url'], |
| 540 : | 'url' => (string) $fullinfo['url'] | ||
| 541 : | poltawski | 1.2 | ); |
| 542 : | } | ||
| 543 : | |||
| 544 : | return $files; | ||
| 545 : | } | ||
| 546 : | |||
| 547 : | poltawski | 1.1 | } |
| 548 : | |||
| 549 : | /** | ||
| 550 : | skodak | 1.9 | * Beginings of an implementation of Clientogin authenticaton for google |
| 551 : | poltawski | 1.1 | * accounts as documented here: |
| 552 : | samhemelryk | 1.7 | * {@link http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#ClientLogin} |
| 553 : | poltawski | 1.1 | * |
| 554 : | skodak | 1.9 | * With this authentication we have to accept a username and password and to post |
| 555 : | poltawski | 1.1 | * it to google. Retrieving a token for use afterwards. |
| 556 : | samhemelryk | 1.7 | * |
| 557 : | * @package moodlecore | ||
| 558 : | * @subpackage lib | ||
| 559 : | * @copyright Dan Poltawski <talktodan@gmail.com> | ||
| 560 : | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
| 561 : | poltawski | 1.1 | */ |
| 562 : | class google_authclient extends google_auth_request { | ||
| 563 : | const LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'; | ||
| 564 : | |||
| 565 : | public function __construct($sessiontoken = '', $username = '', $password = '', $options = array() ){ | ||
| 566 : | parent::__construct($options); | ||
| 567 : | |||
| 568 : | if($username and $password){ | ||
| 569 : | $param = array( | ||
| 570 : | 'accountType'=>'GOOGLE', | ||
| 571 : | 'Email'=>$username, | ||
| 572 : | 'Passwd'=>$password, | ||
| 573 : | 'service'=>'writely' | ||
| 574 : | ); | ||
| 575 : | |||
| 576 : | $content = $this->post(google_authclient::LOGIN_URL, $param); | ||
| 577 : | |||
| 578 : | if( preg_match('/auth=(.*)/i', $content, $matches) ){ | ||
| 579 : | $sessiontoken = $matches[1]; | ||
| 580 : | }else{ | ||
| 581 : | throw new moodle_exception('could not upgrade authtoken'); | ||
| 582 : | } | ||
| 583 : | |||
| 584 : | } | ||
| 585 : | |||
| 586 : | if($sessiontoken){ | ||
| 587 : | $this->token = $sessiontoken; | ||
| 588 : | }else{ | ||
| 589 : | throw new moodle_exception('no session token specified'); | ||
| 590 : | } | ||
| 591 : | } | ||
| 592 : | |||
| 593 : | public static function get_auth_header_name(){ | ||
| 594 : | return 'GoogleLogin auth='; | ||
| 595 : | } | ||
| 596 : | } |
| Moodle CVS Admin | ViewVC Help |
| Powered by ViewVC 1.0.7 |