ee91cf95 |
1 | <?php |
2 | /** |
3 | * Google Documents Portfolio Plugin |
4 | * |
5 | * @author Dan Poltawski <talktodan@gmail.com> |
6 | * @version $Id$ |
7 | * @license http://www.gnu.org/copyleft/gpl.html GNU Public License |
8 | */ |
9 | require_once($CFG->libdir.'/googleapi.php'); |
10 | |
11 | class portfolio_plugin_googledocs extends portfolio_plugin_push_base { |
12 | private $sessiontoken; |
13 | |
14 | public static function supported_formats() { |
15 | return array(PORTFOLIO_FORMAT_PLAINHTML, PORTFOLIO_FORMAT_IMAGE, PORTFOLIO_FORMAT_TEXT); |
16 | } |
17 | |
18 | public static function get_name() { |
19 | return get_string('pluginname', 'portfolio_googledocs'); |
20 | } |
21 | |
22 | public function prepare_package() { |
23 | // we send the files as they are, no prep required |
24 | return true; |
25 | } |
26 | |
27 | public function get_continue_url(){ |
28 | return 'http://docs.google.com/'; |
29 | } |
30 | |
31 | public function expected_time($callertime) { |
32 | // we trust what the portfolio says |
33 | return $callertime; |
34 | } |
35 | |
36 | public function send_package() { |
37 | |
38 | if(!$this->sessiontoken){ |
39 | throw new portfolio_plugin_exception('nosessiontoken', 'portfolio_googledocs'); |
40 | } |
41 | |
42 | $gdocs = new google_docs(new google_authsub($this->sessiontoken)); |
43 | |
44 | foreach ($this->exporter->get_tempfiles() as $file) { |
45 | if(!$gdocs->send_file($file)){ |
46 | throw new portfolio_plugin_exception('sendfailed', 'portfolio_gdocs', $file->get_filename()); |
47 | } |
48 | } |
49 | } |
50 | |
51 | public function steal_control($stage) { |
52 | global $CFG; |
53 | if ($stage != PORTFOLIO_STAGE_CONFIG) { |
54 | return false; |
55 | } |
56 | |
57 | $sesskey = google_docs::get_sesskey($this->get('user')->id); |
58 | |
59 | if($sesskey){ |
60 | try{ |
61 | $gauth = new google_authsub($sesskey); |
62 | $this->sessiontoken = $sesskey; |
63 | return false; |
64 | }catch(Exception $e){ |
65 | // sesskey is not valid, delete store and re-auth |
66 | google_docs::delete_sesskey($this->get('user')->id); |
67 | } |
68 | } |
69 | |
70 | return google_authsub::login_url($CFG->wwwroot.'/portfolio/add.php?postcontrol=1', google_docs::REALM); |
71 | } |
72 | |
73 | public function post_control($stage, $params) { |
74 | if ($stage != PORTFOLIO_STAGE_CONFIG) { |
75 | return; |
76 | } |
77 | |
78 | if(!array_key_exists('token', $params)){ |
79 | throw new portfolio_plugin_exception('noauthtoken', 'portfolio_googledocs'); |
80 | } |
81 | |
82 | // we now have our auth token, get a session token.. |
83 | $gauth = new google_authsub(false, $params['token']); |
84 | $this->sessiontoken = $gauth->get_sessiontoken(); |
85 | |
86 | google_docs::set_sesskey($this->sessiontoken, $this->get('user')->id); |
87 | } |
88 | |
89 | } |
90 | |
91 | /** |
92 | * Registers to the user_deleted event to revoke any |
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 | */ |
98 | function portfolio_googledocs_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_docs::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 | |
113 | return true; |
114 | } |