Commit | Line | Data |
---|---|---|
4317f92f | 1 | <?php |
ee91cf95 | 2 | /** |
3 | * Google Documents 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 | require_once($CFG->libdir.'/googleapi.php'); | |
9 | ||
10 | class portfolio_plugin_googledocs extends portfolio_plugin_push_base { | |
11 | private $sessiontoken; | |
12 | ||
13 | public static function supported_formats() { | |
a9ec9031 DP |
14 | return array( |
15 | PORTFOLIO_FORMAT_PLAINHTML, | |
16 | PORTFOLIO_FORMAT_IMAGE, | |
17 | PORTFOLIO_FORMAT_TEXT, | |
18 | PORTFOLIO_FORMAT_PDF, | |
19 | PORTFOLIO_FORMAT_DOCUMENT, | |
20 | PORTFOLIO_FORMAT_PRESENTATION, | |
21 | PORTFOLIO_FORMAT_SPREADSHEET | |
22 | ); | |
ee91cf95 | 23 | } |
24 | ||
25 | public static function get_name() { | |
26 | return get_string('pluginname', 'portfolio_googledocs'); | |
27 | } | |
28 | ||
29 | public function prepare_package() { | |
30 | // we send the files as they are, no prep required | |
4317f92f | 31 | return true; |
ee91cf95 | 32 | } |
33 | ||
5d0dbf13 | 34 | public function get_interactive_continue_url(){ |
ee91cf95 | 35 | return 'http://docs.google.com/'; |
36 | } | |
37 | ||
38 | public function expected_time($callertime) { | |
39 | // we trust what the portfolio says | |
40 | return $callertime; | |
41 | } | |
42 | ||
43 | public function send_package() { | |
44 | ||
45 | if(!$this->sessiontoken){ | |
46 | throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs'); | |
47 | } | |
48 | ||
49 | $gdocs = new google_docs(new google_authsub($this->sessiontoken)); | |
50 | ||
51 | foreach ($this->exporter->get_tempfiles() as $file) { | |
52 | if(!$gdocs->send_file($file)){ | |
53 | throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | public function steal_control($stage) { | |
59 | global $CFG; | |
60 | if ($stage != PORTFOLIO_STAGE_CONFIG) { | |
61 | return false; | |
62 | } | |
63 | ||
64 | $sesskey = google_docs::get_sesskey($this->get('user')->id); | |
65 | ||
66 | if($sesskey){ | |
67 | try{ | |
68 | $gauth = new google_authsub($sesskey); | |
69 | $this->sessiontoken = $sesskey; | |
70 | return false; | |
71 | }catch(Exception $e){ | |
72 | // sesskey is not valid, delete store and re-auth | |
73 | google_docs::delete_sesskey($this->get('user')->id); | |
74 | } | |
75 | } | |
76 | ||
63d09fc5 | 77 | return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1&id=' . $this->exporter->get('id') . '&sesskey=' . sesskey(), google_docs::REALM); |
ee91cf95 | 78 | } |
79 | ||
80 | public function post_control($stage, $params) { | |
81 | if ($stage != PORTFOLIO_STAGE_CONFIG) { | |
82 | return; | |
83 | } | |
84 | ||
85 | if(!array_key_exists('token', $params)){ | |
86 | throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); | |
87 | } | |
88 | ||
89 | // we now have our auth token, get a session token.. | |
90 | $gauth = new google_authsub(false, $params['token']); | |
91 | $this->sessiontoken = $gauth->get_sessiontoken(); | |
92 | ||
93 | google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id); | |
94 | } | |
95 | ||
16f4918a DP |
96 | public static function allows_multiple_instances() { |
97 | return false; | |
98 | } | |
ee91cf95 | 99 | } |
100 | ||
101 | /** | |
4317f92f | 102 | * Registers to the user_deleted event to revoke any |
ee91cf95 | 103 | * subauth tokens we have from them |
104 | * | |
105 | * @param $user user object | |
106 | * @return boolean true in all cases as its only minor cleanup | |
107 | */ | |
108 | function portfolio_googledocs_user_deleted($user){ | |
109 | // it is only by luck that the user prefstill exists now? | |
110 | // We probably need a pre-delete event? | |
111 | if($sesskey = google_docs::get_sesskey($user->id)){ | |
112 | try{ | |
113 | $gauth = new google_authsub($sesskey); | |
114 | ||
115 | $gauth->revoke_session_token(); | |
116 | }catch(Exception $e){ | |
117 | // we don't care that much about success- just being good | |
118 | // google api citzens | |
119 | return true; | |
120 | } | |
121 | } | |
122 | ||
4317f92f | 123 | return true; |
ee91cf95 | 124 | } |