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