Commit | Line | Data |
---|---|---|
6667f9e4 | 1 | <?php |
10d53fd3 DC |
2 | |
3 | // This file is part of Moodle - http://moodle.org/ | |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
6667f9e4 | 18 | /** |
19 | * Google Docs Plugin | |
20 | * | |
10d53fd3 | 21 | * @since 2.0 |
d078f6d3 PS |
22 | * @package repository |
23 | * @subpackage googledocs | |
24 | * @copyright 2009 Dan Poltawski <talktodan@gmail.com> | |
25 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6667f9e4 | 26 | */ |
27 | ||
28 | require_once($CFG->libdir.'/googleapi.php'); | |
29 | ||
30 | class repository_googledocs extends repository { | |
31 | private $subauthtoken = ''; | |
32 | ||
447c7a19 | 33 | public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { |
6667f9e4 | 34 | global $USER; |
35 | parent::__construct($repositoryid, $context, $options); | |
36 | ||
37 | // TODO: I wish there was somewhere we could explicitly put this outside of constructor.. | |
38 | $googletoken = optional_param('token', false, PARAM_RAW); | |
39 | if($googletoken){ | |
40 | $gauth = new google_authsub(false, $googletoken); // will throw exception if fails | |
41 | google_docs::set_sesskey($gauth->get_sessiontoken(), $USER->id); | |
42 | } | |
94742bca | 43 | $this->check_login(); |
6667f9e4 | 44 | } |
45 | ||
46 | public function check_login() { | |
47 | global $USER; | |
48 | ||
49 | $sesskey = google_docs::get_sesskey($USER->id); | |
50 | ||
51 | if($sesskey){ | |
52 | try{ | |
53 | $gauth = new google_authsub($sesskey); | |
54 | $this->subauthtoken = $sesskey; | |
55 | return true; | |
56 | }catch(Exception $e){ | |
57 | // sesskey is not valid, delete store and re-auth | |
58 | google_docs::delete_sesskey($USER->id); | |
59 | } | |
60 | } | |
61 | ||
62 | return false; | |
63 | } | |
64 | ||
4317f92f | 65 | public function print_login($ajax = true){ |
6667f9e4 | 66 | global $CFG; |
67 | if($ajax){ | |
4317f92f | 68 | $ret = array(); |
6bdfef5d | 69 | $popup_btn = new stdClass(); |
4317f92f | 70 | $popup_btn->type = 'popup'; |
0b75ea78 | 71 | $returnurl = $CFG->wwwroot.'/repository/repository_callback.php?callback=yes&repo_id='.$this->id; |
6667f9e4 | 72 | $popup_btn->url = google_authsub::login_url($returnurl, google_docs::REALM); |
4317f92f PS |
73 | $ret['login'] = array($popup_btn); |
74 | return $ret; | |
6667f9e4 | 75 | } |
76 | } | |
77 | ||
78 | public function get_listing($path='', $page = '') { | |
79 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); | |
80 | ||
81 | $ret = array(); | |
82 | $ret['dynload'] = true; | |
83 | $ret['list'] = $gdocs->get_file_list(); | |
84 | return $ret; | |
85 | } | |
86 | ||
87 | public function search($query){ | |
88 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); | |
89 | ||
90 | $ret = array(); | |
91 | $ret['dynload'] = true; | |
92 | $ret['list'] = $gdocs->get_file_list($query); | |
93 | return $ret; | |
94 | } | |
95 | ||
96 | public function logout(){ | |
97 | global $USER; | |
98 | ||
99 | $token = google_docs::get_sesskey($USER->id); | |
100 | ||
101 | $gauth = new google_authsub($token); | |
102 | // revoke token from google | |
103 | $gauth->revoke_session_token(); | |
104 | ||
105 | google_docs::delete_sesskey($USER->id); | |
106 | $this->subauthtoken = ''; | |
107 | ||
108 | return parent::logout(); | |
109 | } | |
110 | ||
6667f9e4 | 111 | public function get_file($url, $file) { |
112 | global $CFG; | |
a53d4f45 | 113 | $path = $this->prepare_file($file); |
6667f9e4 | 114 | |
a53d4f45 | 115 | $fp = fopen($path, 'w'); |
6667f9e4 | 116 | $gdocs = new google_docs(new google_authsub($this->subauthtoken)); |
117 | $gdocs->download_file($url, $fp); | |
118 | ||
1dce6261 | 119 | return array('path'=>$path, 'url'=>$url); |
41076c58 | 120 | } |
6667f9e4 | 121 | |
41076c58 DC |
122 | public function supported_filetypes() { |
123 | return array('document'); | |
124 | } | |
125 | public function supported_returntypes() { | |
126 | return FILE_INTERNAL; | |
127 | } | |
6667f9e4 | 128 | } |
129 | //Icon from: http://www.iconspedia.com/icon/google-2706.html |