6667f9e4 |
1 | <?php |
2 | /** |
3 | * Google Docs 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 | |
10 | require_once($CFG->libdir.'/googleapi.php'); |
11 | |
12 | class repository_googledocs extends repository { |
13 | private $subauthtoken = ''; |
14 | |
15 | public function __construct($repositoryid, $context = SITEID, $options = array()) { |
16 | global $USER; |
17 | parent::__construct($repositoryid, $context, $options); |
18 | |
19 | // TODO: I wish there was somewhere we could explicitly put this outside of constructor.. |
20 | $googletoken = optional_param('token', false, PARAM_RAW); |
21 | if($googletoken){ |
22 | $gauth = new google_authsub(false, $googletoken); // will throw exception if fails |
23 | google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id); |
24 | } |
94742bca |
25 | $this->check_login(); |
6667f9e4 |
26 | } |
27 | |
28 | public function check_login() { |
29 | global $USER; |
30 | |
31 | $sesskey = google_docs::get_sesskey($USER->id); |
32 | |
33 | if($sesskey){ |
34 | try{ |
35 | $gauth = new google_authsub($sesskey); |
36 | $this->subauthtoken = $sesskey; |
37 | return true; |
38 | }catch(Exception $e){ |
39 | // sesskey is not valid, delete store and re-auth |
40 | google_docs::delete_sesskey($USER->id); |
41 | } |
42 | } |
43 | |
44 | return false; |
45 | } |
46 | |
47 | public function print_login($ajax = true){ |
48 | global $CFG; |
49 | if($ajax){ |
50 | $ret = array(); |
51 | $popup_btn = new stdclass; |
52 | $popup_btn->type = 'popup'; |
53 | $returnurl = $CFG->wwwroot.'/repository/ws.php?callback=yes&repo_id='.$this->id; |
54 | $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM); |
55 | $ret['login'] = array($popup_btn); |
56 | return $ret; |
57 | } |
58 | } |
59 | |
60 | public function get_listing($path='', $page = '') { |
61 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); |
62 | |
63 | $ret = array(); |
64 | $ret['dynload'] = true; |
65 | $ret['list'] = $gdocs->get_file_list(); |
66 | return $ret; |
67 | } |
68 | |
69 | public function search($query){ |
70 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); |
71 | |
72 | $ret = array(); |
73 | $ret['dynload'] = true; |
74 | $ret['list'] = $gdocs->get_file_list($query); |
75 | return $ret; |
76 | } |
77 | |
78 | public function logout(){ |
79 | global $USER; |
80 | |
81 | $token = google_docs::get_sesskey($USER->id); |
82 | |
83 | $gauth = new google_authsub($token); |
84 | // revoke token from google |
85 | $gauth->revoke_session_token(); |
86 | |
87 | google_docs::delete_sesskey($USER->id); |
88 | $this->subauthtoken = ''; |
89 | |
90 | return parent::logout(); |
91 | } |
92 | |
93 | public function get_name(){ |
94 | return get_string('repositoryname', 'repository_googledocs'); |
95 | } |
96 | |
97 | public function get_file($url, $file) { |
98 | global $CFG; |
a53d4f45 |
99 | $path = $this->prepare_file($file); |
6667f9e4 |
100 | |
a53d4f45 |
101 | $fp = fopen($path, 'w'); |
6667f9e4 |
102 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); |
103 | $gdocs->download_file($url, $fp); |
104 | |
a53d4f45 |
105 | return $path; |
6667f9e4 |
106 | } |
107 | |
a86b44b1 |
108 | public function supported_filetypes() { |
109 | return array('document'); |
110 | } |
6667f9e4 |
111 | } |
112 | //Icon from: http://www.iconspedia.com/icon/google-2706.html |