MDL-22985, repository plugins use new callback url.
[moodle.git] / repository / dropbox / lib.php
CommitLineData
3e123368
DC
1<?php
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
18/**
e35194be
DC
19 * repository_dropbox class
20 * This plugin is used to access user's dropbox files
21 *
22 * TODO:
23 * Dropbox has problems to process filepath with spaces, tried to use
24 * urlencode filepath, still doesn't work
25 * http://code.google.com/p/dropbox-php/ has the same problem
3e123368
DC
26 *
27 * @since 2.0
28 * @package moodlecore
29 * @subpackage repository
30 * @copyright 2010 Dongsheng Cai
31 * @author Dongsheng Cai <dongsheng@moodle.com>
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 */
34
e35194be
DC
35require_once(dirname(__FILE__).'/locallib.php');
36
37class repository_dropbox extends repository {
38 private $dropbox;
39 public $files;
40 public $logged=false;
41
42 /**
43 * Constructor of dropbox plugin
44 * @param int $repositoryid
45 * @param object $context
46 * @param array $options
47 */
48 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
49 global $SESSION, $CFG;
50 $options['page'] = optional_param('p', 1, PARAM_INT);
51 parent::__construct($repositoryid, $context, $options);
52
53 $this->setting = 'dropbox_';
54
55 $this->dropbox_key = $this->get_option('dropbox_key');
56 $this->dropbox_secret = $this->get_option('dropbox_secret');
57
58 $this->access_key = get_user_preferences($this->setting.'_access_key', '');
59 $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
60
61 if (!empty($this->access_key) && !empty($this->access_secret)) {
62 $this->logged = true;
63 }
64
0b75ea78 65 $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array(
e35194be
DC
66 'callback'=>'yes',
67 'repo_id'=>$repositoryid
68 ));
69
70 $args = array(
71 'oauth_consumer_key'=>$this->dropbox_key,
72 'oauth_consumer_secret'=>$this->dropbox_secret,
73 'oauth_callback' => $this->callback->out(false),
74 'api_root' => 'http://api.dropbox.com/0/oauth',
75 );
76
77 $this->dropbox = new dropbox($args);
78 }
79
80 /**
81 * Check if moodle has got access token and secret
82 * @return bool
83 */
84 public function check_login() {
85 return !empty($this->logged);
86 }
87
88 /**
89 * Generate dropbox login url
90 * @return array
91 */
92 public function print_login() {
93 $result = $this->dropbox->request_token();
94 set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']);
95 $url = $result['authorize_url'];
96 if ($this->options['ajax']) {
97 $ret = array();
98 $popup_btn = new stdclass;
99 $popup_btn->type = 'popup';
100 $popup_btn->url = $url;
101 $ret['login'] = array($popup_btn);
102 return $ret;
103 } else {
104 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
105 }
106 }
107
108 /**
109 * Request access token
110 * @return array
111 */
112 public function callback() {
113 $token = optional_param('oauth_token', '', PARAM_TEXT);
114 $secret = get_user_preferences($this->setting.'_request_secret', '');
115 $access_token = $this->dropbox->get_access_token($token, $secret);
116 set_user_preference($this->setting.'_access_key', $access_token['oauth_token']);
117 set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']);
118 }
119
120 /**
121 * Get dropbox files
122 * @param string $path
123 * @param int $page
124 * @return array
125 */
126 public function get_listing($path = '', $page = '1') {
127 global $OUTPUT;
128 if (empty($path) || $path=='/') {
129 $path = '/';
130 } else {
131 $path = file_correct_filepath($path);
132 }
133 $result = $this->dropbox->get_listing($path, $this->access_key, $this->access_secret);
134 $current_path = file_correct_filepath($result->path);
135 if (empty($result->path)) {
136 $current_path = '/';
137 }
138
139 $list = array();
140 $list['list'] = array();
141 // process breacrumb trail
142 $list['path'] = array(
143 array('name'=>'Dropbox Sandbox', 'path'=>'/')
144 );
145 $trail = '';
146 if (!empty($path)) {
147 $parts = explode('/', $path);
148 if (count($parts) > 1) {
149 foreach ($parts as $part) {
150 if (!empty($part)) {
151 $trail .= ('/'.$part);
152 $list['path'][] = array('name'=>$part, 'path'=>$trail);
153 }
154 }
155 } else {
156 $list['path'][] = array('name'=>$path, 'path'=>$path);
157 }
158 }
159 $list['manage'] = false;
160 $list['dynload'] = true;
161 $list['nosearch'] = true;
162
163 $files = $result->contents;
164 foreach ($files as $file) {
165 if ($file->is_dir) {
166 $list['list'][] = array(
167 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
168 'path' => file_correct_filepath($file->path),
169 'size' => $file->size,
170 'date' => $file->modified,
171 'thumbnail' => $OUTPUT->pix_url('f/folder-32').'',
172 'children' => array(),
173 );
174 } else {
175 $list['list'][] = array(
176 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)),
177 'source' => $file->path,
178 'size' => $file->size,
179 'date' => $file->modified,
180 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32)).''
181 );
182 }
183 }
184 return $list;
185 }
186 /**
187 * Logout from dropbox
188 * @return array
189 */
190 public function logout() {
191 set_user_preference($this->setting.'_access_key', '');
192 set_user_preference($this->setting.'_access_secret', '');
193 $this->access_key = '';
194 $this->access_secret = '';
195 return $this->print_login();
196 }
197
198 /**
199 * Set dropbox option
200 * @param array $options
201 * @return mixed
202 */
203 public function set_option($options = array()) {
204 if (!empty($options['dropbox_key'])) {
205 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
206 }
207 if (!empty($options['dropbox_secret'])) {
208 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
209 }
210 unset($options['dropbox_key']);
211 unset($options['dropbox_secret']);
212 $ret = parent::set_option($options);
213 return $ret;
214 }
215
216 /**
217 * Get dropbox options
218 * @param string $config
219 * @return mixed
220 */
221 public function get_option($config = '') {
222 if ($config==='dropbox_key') {
223 return trim(get_config('dropbox', 'dropbox_key'));
224 } elseif ($config==='dropbox_secret') {
225 return trim(get_config('dropbox', 'dropbox_secret'));
226 } else {
227 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
228 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
229 }
230 $options = parent::get_option($config);
231 return $options;
232 }
233
234 /**
235 *
236 * @param string $photo_id
237 * @param string $file
238 * @return string
239 */
240 public function get_file($filepath, $saveas = '') {
241 $this->dropbox->set_access_token($this->access_key, $this->access_secret);
242 return $this->dropbox->get_file($filepath, $saveas);
243 }
244 /**
245 * Add Plugin settings input to Moodle form
246 * @param object $mform
247 */
248 public function type_config_form($mform) {
249 global $CFG;
250 $key = get_config('dropbox', 'dropbox_key');
251 $secret = get_config('dropbox', 'dropbox_secret');
252
253 if (empty($key)) {
254 $key = '';
255 }
256 if (empty($secret)) {
257 $secret = '';
258 }
259
260 $strrequired = get_string('required');
261
262 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
263 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
264
265 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
266 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
267 $str_getkey = get_string('instruction', 'repository_dropbox');
268 $mform->addElement('static', null, '', $str_getkey);
269 }
270
271 /**
272 * Option names of dropbox plugin
273 * @return array
274 */
275 public static function get_type_option_names() {
276 return array('dropbox_key', 'dropbox_secret');
277 }
278
279 /**
280 * Dropbox plugin supports all kinds of files
281 * @return array
282 */
283 public function supported_filetypes() {
284 return '*';
285 }
3e123368 286
e35194be
DC
287 /**
288 * User cannot use the external link to dropbox
289 * @return int
290 */
291 public function supported_returntypes() {
292 return FILE_INTERNAL;
3e123368
DC
293 }
294}