MDL-9251 backup/restore *all* users - is now forbidden for users
[moodle.git] / portfolio / picasa / lib.php
CommitLineData
4317f92f 1<?php
ee91cf95 2/**
3 * Picasa Portfolio Plugin
4 *
5 * @author Dan Poltawski <talktodan@gmail.com>
ee91cf95 6 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
7 */
8
9require_once($CFG->libdir.'/googleapi.php');
10
11class portfolio_plugin_picasa extends portfolio_plugin_push_base {
12 private $sessionkey;
13
14 public static function supported_formats() {
15 return array(PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_VIDEO);
16 }
17
18 public static function get_name() {
19 return get_string('pluginname', 'portfolio_picasa');
20 }
21
22 public function prepare_package() {
23 // we send the files as they are, no prep required
4317f92f 24 return true;
ee91cf95 25 }
26
5d0dbf13 27 public function get_interactive_continue_url(){
ee91cf95 28 return 'http://picasaweb.google.com/';
29 }
30
31 public function expected_time($callertime) {
32 return $callertime;
33 }
34
35 public function send_package() {
36 if(!$this->sessionkey){
37 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa');
38 }
39
40 $picasa = new google_picasa(new google_authsub($this->sessionkey));
41
42 foreach ($this->exporter->get_tempfiles() as $file) {
43
44 if(!$picasa->send_file($file)){
45 throw new portfolio_plugin_exception('sendfailed', 'portfolio_picasa', $file->get_filename());
46 }
47 }
48 }
49
50 public function steal_control($stage) {
51 global $CFG;
52 if ($stage != PORTFOLIO_STAGE_CONFIG) {
53 return false;
54 }
55
56 $sesskey = google_picasa::get_sesskey($this->get('user')->id);
57
58 if($sesskey){
59 try{
60 $gauth = new google_authsub($sesskey);
61 $this->sessionkey = $sesskey;
62 return false;
63 }catch(Exception $e){
64 // sesskey is not valid, delete store and re-auth
65 google_picasa::delete_sesskey($this->get('user')->id);
66 }
67 }
68
63d09fc5 69 return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_picasa::REALM);
ee91cf95 70 }
71
72 public function post_control($stage, $params) {
73 if ($stage != PORTFOLIO_STAGE_CONFIG) {
74 return;
75 }
76
77 if(!array_key_exists('token', $params)){
78 throw new portfolio_plugin_exception('noauthtoken', 'portfolio_picasa');
79 }
80
81 // we now have our auth token, get a session token..
82 $gauth = new google_authsub(false, $params['token']);
83
84 $this->sessionkey = $gauth->get_sessiontoken();
85
86 google_picasa::set_sesskey($this->sessionkey, $this->get('user')->id);
87 }
88
89}
90
91/**
4317f92f 92 * Registers to the user_deleted event to revoke any
ee91cf95 93 * subauth tokens we have from them
94 *
95 * @param $user user object
96 * @return boolean true in all cases as its only minor cleanup
97 */
98function portfolio_picasa_user_deleted($user){
99 // it is only by luck that the user prefstill exists now?
100 // We probably need a pre-delete event?
101 if($sesskey = google_picasa::get_sesskey($user->id)){
102 try{
103 $gauth = new google_authsub($sesskey);
104
105 $gauth->revoke_session_token();
106 }catch(Exception $e){
107 // we don't care that much about success- just being good
108 // google api citzens
109 return true;
110 }
111 }
112
4317f92f 113 return true;
ee91cf95 114}