Commit | Line | Data |
---|---|---|
83b51087 | 1 | <?php |
10d53fd3 DC |
2 | |
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/>. | |
17 | ||
83b51087 | 18 | /** |
19 | * Picasa Repository Plugin | |
20 | * | |
10d53fd3 DC |
21 | * @since 2.0 |
22 | * @package moodlecore | |
23 | * @subpackage repository | |
24 | * @copyright 2009 Dan Poltawski | |
83b51087 | 25 | * @author Dan Poltawski <talktodan@gmail.com> |
10d53fd3 | 26 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
83b51087 | 27 | */ |
28 | ||
29 | require_once($CFG->libdir.'/googleapi.php'); | |
30 | ||
31 | class repository_picasa extends repository { | |
32 | private $subauthtoken = ''; | |
33 | ||
447c7a19 | 34 | public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { |
83b51087 | 35 | global $USER; |
36 | parent::__construct($repositoryid, $context, $options); | |
37 | ||
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 | } | |
d1bfc05e | 44 | $this->check_login(); |
83b51087 | 45 | } |
46 | ||
47 | public function check_login() { | |
48 | global $USER; | |
49 | ||
50 | $sesskey = google_picasa::get_sesskey($USER->id); | |
51 | ||
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 | } | |
62 | ||
63 | return false; | |
64 | } | |
65 | ||
4317f92f | 66 | public function print_login(){ |
83b51087 | 67 | global $CFG; |
0b75ea78 | 68 | $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id; |
6876783a | 69 | $authurl = google_authsub::login_url($returnurl, google_picasa::REALM); |
70 | if($this->options['ajax']){ | |
4317f92f PS |
71 | $ret = array(); |
72 | $popup_btn = new stdclass; | |
73 | $popup_btn->type = 'popup'; | |
6876783a | 74 | $popup_btn->url = $authurl; |
4317f92f PS |
75 | $ret['login'] = array($popup_btn); |
76 | return $ret; | |
6876783a | 77 | } else { |
78 | echo '<a target="_blank" href="'.$authurl.'">Login</a>'; | |
83b51087 | 79 | } |
80 | } | |
81 | ||
5fa358d1 | 82 | public function get_listing($path='', $page = '') { |
83b51087 | 83 | $picasa = new google_picasa(new google_authsub($this->subauthtoken)); |
84 | ||
85 | $ret = array(); | |
86 | $ret['dynload'] = true; | |
87 | $ret['list'] = $picasa->get_file_list($path); | |
88 | return $ret; | |
89 | } | |
90 | ||
91 | public function search($query){ | |
92 | $picasa = new google_picasa(new google_authsub($this->subauthtoken)); | |
93 | ||
94 | $ret = array(); | |
95 | $ret['list'] = $picasa->do_photo_search($query); | |
96 | return $ret; | |
97 | } | |
98 | ||
99 | public function logout(){ | |
100 | global $USER; | |
101 | ||
102 | $token = google_picasa::get_sesskey($USER->id); | |
103 | ||
104 | $gauth = new google_authsub($token); | |
105 | // revoke token from google | |
106 | $gauth->revoke_session_token(); | |
107 | ||
108 | google_picasa::delete_sesskey($USER->id); | |
109 | $this->subauthtoken = ''; | |
110 | ||
111 | return parent::logout(); | |
112 | } | |
113 | ||
114 | public function get_name(){ | |
614d18d2 | 115 | return get_string('pluginname', 'repository_picasa'); |
83b51087 | 116 | } |
7137e7b9 | 117 | public function supported_filetypes() { |
118 | return array('web_image'); | |
119 | } | |
41076c58 DC |
120 | public function supported_returntypes() { |
121 | return (FILE_INTERNAL | FILE_EXTERNAL); | |
122 | } | |
83b51087 | 123 | } |
124 | ||
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 |