MDL-22982
[moodle.git] / repository / picasa / lib.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Picasa Repository Plugin
20  *
21  * @since 2.0
22  * @package moodlecore
23  * @subpackage repository
24  * @copyright 2009 Dan Poltawski
25  * @author Dan Poltawski <talktodan@gmail.com>
26  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27  */
29 require_once($CFG->libdir.'/googleapi.php');
31 class repository_picasa extends repository {
32     private $subauthtoken = '';
34     public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
35         global $USER;
36         parent::__construct($repositoryid, $context, $options);
38         // TODO: I wish there was somewhere we could explicitly put this outside of constructor..
39         $googletoken = optional_param('token', false, PARAM_RAW);
40         if($googletoken){
41             $gauth = new google_authsub(false, $googletoken); // will throw exception if fails
42             google_picasa::set_sesskey($gauth->get_sessiontoken(), $USER->id);
43         }
44         $this->check_login();
45     }
47     public function check_login() {
48         global $USER;
50         $sesskey = google_picasa::get_sesskey($USER->id);
52         if($sesskey){
53             try{
54                 $gauth = new google_authsub($sesskey);
55                 $this->subauthtoken = $sesskey;
56                 return true;
57             }catch(Exception $e){
58                 // sesskey is not valid, delete store and re-auth
59                 google_picasa::delete_sesskey($USER->id);
60             }
61         }
63         return false;
64     }
66     public function print_login(){
67         global $CFG;
68         $returnurl = $CFG->wwwroot.'/repository/repository_ajax.php?callback=yes&repo_id='.$this->id;
69         $authurl = google_authsub::login_url($returnurl, google_picasa::REALM);
70         if($this->options['ajax']){
71             $ret = array();
72             $popup_btn = new stdclass;
73             $popup_btn->type = 'popup';
74             $popup_btn->url = $authurl;
75             $ret['login'] = array($popup_btn);
76             return $ret;
77         } else {
78             echo '<a target="_blank" href="'.$authurl.'">Login</a>';
79         }
80     }
82     public function get_listing($path='', $page = '') {
83         $picasa = new google_picasa(new google_authsub($this->subauthtoken));
85         $ret = array();
86         $ret['dynload'] = true;
87         $ret['list'] = $picasa->get_file_list($path);
88         return $ret;
89     }
91     public function search($query){
92         $picasa = new google_picasa(new google_authsub($this->subauthtoken));
94         $ret = array();
95         $ret['list'] =  $picasa->do_photo_search($query);
96         return $ret;
97     }
99     public function logout(){
100         global $USER;
102         $token = google_picasa::get_sesskey($USER->id);
104         $gauth = new google_authsub($token);
105         // revoke token from google
106         $gauth->revoke_session_token();
108         google_picasa::delete_sesskey($USER->id);
109         $this->subauthtoken = '';
111         return parent::logout();
112     }
114     public function get_name(){
115         return get_string('pluginname', 'repository_picasa');
116     }
117     public function supported_filetypes() {
118         return array('web_image');
119     }
120     public function supported_returntypes() {
121         return (FILE_INTERNAL | FILE_EXTERNAL);
122     }
125 // Icon for this plugin retrieved from http://www.iconspedia.com/icon/picasa-2711.html
126 // Where the license is said documented to be Free