3 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
19 * repository_dropbox class
20 * This plugin is used to access user's dropbox files
25 * @copyright 2010 Dongsheng Cai
26 * @author Dongsheng Cai <dongsheng@moodle.com>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 require_once(dirname(__FILE__
).'/locallib.php');
32 class repository_dropbox
extends repository
{
38 * Constructor of dropbox plugin
39 * @param int $repositoryid
40 * @param object $context
41 * @param array $options
43 public function __construct($repositoryid, $context = SYSCONTEXTID
, $options = array()) {
44 global $SESSION, $CFG;
45 $options['page'] = optional_param('p', 1, PARAM_INT
);
46 parent
::__construct($repositoryid, $context, $options);
48 $this->setting
= 'dropbox_';
50 $this->dropbox_key
= $this->get_option('dropbox_key');
51 $this->dropbox_secret
= $this->get_option('dropbox_secret');
53 $this->access_key
= get_user_preferences($this->setting
.'_access_key', '');
54 $this->access_secret
= get_user_preferences($this->setting
.'_access_secret', '');
56 if (!empty($this->access_key
) && !empty($this->access_secret
)) {
60 $this->callback
= new moodle_url($CFG->wwwroot
.'/repository/repository_callback.php', array(
62 'repo_id'=>$repositoryid
66 'oauth_consumer_key'=>$this->dropbox_key
,
67 'oauth_consumer_secret'=>$this->dropbox_secret
,
68 'oauth_callback' => $this->callback
->out(false),
69 'api_root' => 'http://www.dropbox.com/0/oauth',
72 $this->dropbox
= new dropbox($args);
76 * Check if moodle has got access token and secret
79 public function check_login() {
80 return !empty($this->logged
);
84 * Generate dropbox login url
87 public function print_login() {
88 $result = $this->dropbox
->request_token();
89 set_user_preference($this->setting
.'_request_secret', $result['oauth_token_secret']);
90 $url = $result['authorize_url'];
91 if ($this->options
['ajax']) {
93 $popup_btn = new stdClass();
94 $popup_btn->type
= 'popup';
95 $popup_btn->url
= $url;
96 $ret['login'] = array($popup_btn);
99 echo '<a target="_blank" href="'.$this->flickr
->auth().'">'.get_string('login', 'repository').'</a>';
104 * Request access token
107 public function callback() {
108 $token = optional_param('oauth_token', '', PARAM_TEXT
);
109 $secret = get_user_preferences($this->setting
.'_request_secret', '');
110 $access_token = $this->dropbox
->get_access_token($token, $secret);
111 set_user_preference($this->setting
.'_access_key', $access_token['oauth_token']);
112 set_user_preference($this->setting
.'_access_secret', $access_token['oauth_token_secret']);
117 * @param string $path
121 public function get_listing($path = '', $page = '1') {
123 if (empty($path) ||
$path=='/') {
126 $path = file_correct_filepath($path);
128 $encoded_path = str_replace("%2F", "/", rawurlencode($path));
131 $list['list'] = array();
132 $list['manage'] = false;
133 $list['dynload'] = true;
134 $list['nosearch'] = true;
135 // process breadcrumb trail
136 $list['path'] = array(
137 array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/')
140 $result = $this->dropbox
->get_listing($encoded_path, $this->access_key
, $this->access_secret
);
142 if (!is_object($result) ||
empty($result)) {
145 if (empty($result->path
)) {
148 $current_path = file_correct_filepath($result->path
);
153 $parts = explode('/', $path);
154 if (count($parts) > 1) {
155 foreach ($parts as $part) {
157 $trail .= ('/'.$part);
158 $list['path'][] = array('name'=>$part, 'path'=>$trail);
162 $list['path'][] = array('name'=>$path, 'path'=>$path);
166 if (!empty($result->error
)) {
168 set_user_preference($this->setting
.'_access_key', '');
169 set_user_preference($this->setting
.'_access_secret', '');
170 throw new repository_exception('repositoryerror', 'repository', '', $result->error
);
172 if (empty($result->contents
) or !is_array($result->contents
)) {
175 $files = $result->contents
;
176 foreach ($files as $file) {
178 $list['list'][] = array(
179 'title' => substr($file->path
, strpos($file->path
, $current_path)+
strlen($current_path)),
180 'path' => file_correct_filepath($file->path
),
181 'size' => $file->size
,
182 'date' => $file->modified
,
183 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false),
184 'children' => array(),
187 $list['list'][] = array(
188 'title' => substr($file->path
, strpos($file->path
, $current_path)+
strlen($current_path)),
189 'source' => $file->path
,
190 'size' => $file->size
,
191 'date' => $file->modified
,
192 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path
, 32))->out(false)
199 * Logout from dropbox
202 public function logout() {
203 set_user_preference($this->setting
.'_access_key', '');
204 set_user_preference($this->setting
.'_access_secret', '');
205 $this->access_key
= '';
206 $this->access_secret
= '';
207 return $this->print_login();
212 * @param array $options
215 public function set_option($options = array()) {
216 if (!empty($options['dropbox_key'])) {
217 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
219 if (!empty($options['dropbox_secret'])) {
220 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
222 unset($options['dropbox_key']);
223 unset($options['dropbox_secret']);
224 $ret = parent
::set_option($options);
229 * Get dropbox options
230 * @param string $config
233 public function get_option($config = '') {
234 if ($config==='dropbox_key') {
235 return trim(get_config('dropbox', 'dropbox_key'));
236 } elseif ($config==='dropbox_secret') {
237 return trim(get_config('dropbox', 'dropbox_secret'));
239 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
240 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
242 $options = parent
::get_option($config);
248 * @param string $photo_id
249 * @param string $file
252 public function get_file($filepath, $saveas = '') {
253 $this->dropbox
->set_access_token($this->access_key
, $this->access_secret
);
254 $saveas = $this->prepare_file($saveas);
255 return $this->dropbox
->get_file($filepath, $saveas);
258 * Add Plugin settings input to Moodle form
259 * @param object $mform
261 public function type_config_form($mform) {
263 parent
::type_config_form($mform);
264 $key = get_config('dropbox', 'dropbox_key');
265 $secret = get_config('dropbox', 'dropbox_secret');
270 if (empty($secret)) {
274 $strrequired = get_string('required');
276 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
277 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
279 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client');
280 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client');
281 $str_getkey = get_string('instruction', 'repository_dropbox');
282 $mform->addElement('static', null, '', $str_getkey);
286 * Option names of dropbox plugin
289 public static function get_type_option_names() {
290 return array('dropbox_key', 'dropbox_secret', 'pluginname');
294 * Dropbox plugin supports all kinds of files
297 public function supported_filetypes() {
302 * User cannot use the external link to dropbox
305 public function supported_returntypes() {
306 return FILE_INTERNAL
;