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
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
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
35 require_once(dirname(__FILE__).'/locallib.php');
37 class repository_dropbox extends repository {
43 * Constructor of dropbox plugin
44 * @param int $repositoryid
45 * @param object $context
46 * @param array $options
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);
53 $this->setting = 'dropbox_';
55 $this->dropbox_key = $this->get_option('dropbox_key');
56 $this->dropbox_secret = $this->get_option('dropbox_secret');
58 $this->access_key = get_user_preferences($this->setting.'_access_key', '');
59 $this->access_secret = get_user_preferences($this->setting.'_access_secret', '');
61 if (!empty($this->access_key) && !empty($this->access_secret)) {
65 $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_ajax.php', array(
67 'repo_id'=>$repositoryid
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',
77 $this->dropbox = new dropbox($args);
81 * Check if moodle has got access token and secret
84 public function check_login() {
85 return !empty($this->logged);
89 * Generate dropbox login url
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']) {
98 $popup_btn = new stdclass;
99 $popup_btn->type = 'popup';
100 $popup_btn->url = $url;
101 $ret['login'] = array($popup_btn);
104 echo '<a target="_blank" href="'.$this->flickr->auth().'">'.get_string('login', 'repository').'</a>';
109 * Request access token
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']);
122 * @param string $path
126 public function get_listing($path = '', $page = '1') {
128 if (empty($path) || $path=='/') {
131 $path = file_correct_filepath($path);
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)) {
140 $list['list'] = array();
141 // process breacrumb trail
142 $list['path'] = array(
143 array('name'=>'Dropbox Sandbox', 'path'=>'/')
147 $parts = explode('/', $path);
148 if (count($parts) > 1) {
149 foreach ($parts as $part) {
151 $trail .= ('/'.$part);
152 $list['path'][] = array('name'=>$part, 'path'=>$trail);
156 $list['path'][] = array('name'=>$path, 'path'=>$path);
159 $list['manage'] = false;
160 $list['dynload'] = true;
161 $list['nosearch'] = true;
163 $files = $result->contents;
164 foreach ($files as $file) {
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(),
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)).''
187 * Logout from dropbox
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();
200 * @param array $options
203 public function set_option($options = array()) {
204 if (!empty($options['dropbox_key'])) {
205 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
207 if (!empty($options['dropbox_secret'])) {
208 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
210 unset($options['dropbox_key']);
211 unset($options['dropbox_secret']);
212 $ret = parent::set_option($options);
217 * Get dropbox options
218 * @param string $config
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'));
227 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
228 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
230 $options = parent::get_option($config);
236 * @param string $photo_id
237 * @param string $file
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);
245 * Add Plugin settings input to Moodle form
246 * @param object $mform
248 public function type_config_form($mform) {
250 $key = get_config('dropbox', 'dropbox_key');
251 $secret = get_config('dropbox', 'dropbox_secret');
256 if (empty($secret)) {
260 $strrequired = get_string('required');
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'));
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);
272 * Option names of dropbox plugin
275 public static function get_type_option_names() {
276 return array('dropbox_key', 'dropbox_secret');
280 * Dropbox plugin supports all kinds of files
283 public function supported_filetypes() {
288 * User cannot use the external link to dropbox
291 public function supported_returntypes() {
292 return FILE_INTERNAL;