|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00030 require_once(dirname(__FILE__).'/locallib.php'); 00031 00032 class repository_dropbox extends repository { 00033 private $dropbox; 00034 public $files; 00035 public $logged=false; 00036 00043 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) { 00044 global $SESSION, $CFG; 00045 $options['page'] = optional_param('p', 1, PARAM_INT); 00046 parent::__construct($repositoryid, $context, $options); 00047 00048 $this->setting = 'dropbox_'; 00049 00050 $this->dropbox_key = $this->get_option('dropbox_key'); 00051 $this->dropbox_secret = $this->get_option('dropbox_secret'); 00052 00053 $this->access_key = get_user_preferences($this->setting.'_access_key', ''); 00054 $this->access_secret = get_user_preferences($this->setting.'_access_secret', ''); 00055 00056 if (!empty($this->access_key) && !empty($this->access_secret)) { 00057 $this->logged = true; 00058 } 00059 00060 $this->callback = new moodle_url($CFG->wwwroot.'/repository/repository_callback.php', array( 00061 'callback'=>'yes', 00062 'repo_id'=>$repositoryid 00063 )); 00064 00065 $args = array( 00066 'oauth_consumer_key'=>$this->dropbox_key, 00067 'oauth_consumer_secret'=>$this->dropbox_secret, 00068 'oauth_callback' => $this->callback->out(false), 00069 'api_root' => 'https://www.dropbox.com/1/oauth', 00070 ); 00071 00072 $this->dropbox = new dropbox($args); 00073 } 00074 00079 public function check_login() { 00080 return !empty($this->logged); 00081 } 00082 00087 public function print_login() { 00088 $result = $this->dropbox->request_token(); 00089 set_user_preference($this->setting.'_request_secret', $result['oauth_token_secret']); 00090 $url = $result['authorize_url']; 00091 if ($this->options['ajax']) { 00092 $ret = array(); 00093 $popup_btn = new stdClass(); 00094 $popup_btn->type = 'popup'; 00095 $popup_btn->url = $url; 00096 $ret['login'] = array($popup_btn); 00097 return $ret; 00098 } else { 00099 echo '<a target="_blank" href="'.$url.'">'.get_string('login', 'repository').'</a>'; 00100 } 00101 } 00102 00107 public function callback() { 00108 $token = optional_param('oauth_token', '', PARAM_TEXT); 00109 $secret = get_user_preferences($this->setting.'_request_secret', ''); 00110 $access_token = $this->dropbox->get_access_token($token, $secret); 00111 set_user_preference($this->setting.'_access_key', $access_token['oauth_token']); 00112 set_user_preference($this->setting.'_access_secret', $access_token['oauth_token_secret']); 00113 } 00114 00121 public function get_listing($path = '', $page = '1') { 00122 global $OUTPUT; 00123 if (empty($path) || $path=='/') { 00124 $path = '/'; 00125 } else { 00126 $path = file_correct_filepath($path); 00127 } 00128 $encoded_path = str_replace("%2F", "/", rawurlencode($path)); 00129 00130 $list = array(); 00131 $list['list'] = array(); 00132 $list['manage'] = false; 00133 $list['dynload'] = true; 00134 $list['nosearch'] = true; 00135 // process breadcrumb trail 00136 $list['path'] = array( 00137 array('name'=>get_string('dropbox', 'repository_dropbox'), 'path'=>'/') 00138 ); 00139 00140 $result = $this->dropbox->get_listing($encoded_path, $this->access_key, $this->access_secret); 00141 00142 if (!is_object($result) || empty($result)) { 00143 return $list; 00144 } 00145 if (empty($result->path)) { 00146 $current_path = '/'; 00147 } else { 00148 $current_path = file_correct_filepath($result->path); 00149 } 00150 00151 $trail = ''; 00152 if (!empty($path)) { 00153 $parts = explode('/', $path); 00154 if (count($parts) > 1) { 00155 foreach ($parts as $part) { 00156 if (!empty($part)) { 00157 $trail .= ('/'.$part); 00158 $list['path'][] = array('name'=>$part, 'path'=>$trail); 00159 } 00160 } 00161 } else { 00162 $list['path'][] = array('name'=>$path, 'path'=>$path); 00163 } 00164 } 00165 00166 if (!empty($result->error)) { 00167 // reset access key 00168 set_user_preference($this->setting.'_access_key', ''); 00169 set_user_preference($this->setting.'_access_secret', ''); 00170 throw new repository_exception('repositoryerror', 'repository', '', $result->error); 00171 } 00172 if (empty($result->contents) or !is_array($result->contents)) { 00173 return $list; 00174 } 00175 $files = $result->contents; 00176 foreach ($files as $file) { 00177 if ($file->is_dir) { 00178 $list['list'][] = array( 00179 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), 00180 'path' => file_correct_filepath($file->path), 00181 'size' => $file->size, 00182 'date' => $file->modified, 00183 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false), 00184 'children' => array(), 00185 ); 00186 } else { 00187 $list['list'][] = array( 00188 'title' => substr($file->path, strpos($file->path, $current_path)+strlen($current_path)), 00189 'source' => $file->path, 00190 'size' => $file->size, 00191 'date' => $file->modified, 00192 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($file->path, 32))->out(false) 00193 ); 00194 } 00195 } 00196 return $list; 00197 } 00202 public function logout() { 00203 set_user_preference($this->setting.'_access_key', ''); 00204 set_user_preference($this->setting.'_access_secret', ''); 00205 $this->access_key = ''; 00206 $this->access_secret = ''; 00207 return $this->print_login(); 00208 } 00209 00215 public function set_option($options = array()) { 00216 if (!empty($options['dropbox_key'])) { 00217 set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox'); 00218 } 00219 if (!empty($options['dropbox_secret'])) { 00220 set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox'); 00221 } 00222 unset($options['dropbox_key']); 00223 unset($options['dropbox_secret']); 00224 $ret = parent::set_option($options); 00225 return $ret; 00226 } 00227 00233 public function get_option($config = '') { 00234 if ($config==='dropbox_key') { 00235 return trim(get_config('dropbox', 'dropbox_key')); 00236 } elseif ($config==='dropbox_secret') { 00237 return trim(get_config('dropbox', 'dropbox_secret')); 00238 } else { 00239 $options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key')); 00240 $options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret')); 00241 } 00242 $options = parent::get_option($config); 00243 return $options; 00244 } 00245 00252 public function get_file($filepath, $saveas = '') { 00253 $this->dropbox->set_access_token($this->access_key, $this->access_secret); 00254 $saveas = $this->prepare_file($saveas); 00255 return $this->dropbox->get_file($filepath, $saveas); 00256 } 00261 public function type_config_form($mform) { 00262 global $CFG; 00263 parent::type_config_form($mform); 00264 $key = get_config('dropbox', 'dropbox_key'); 00265 $secret = get_config('dropbox', 'dropbox_secret'); 00266 00267 if (empty($key)) { 00268 $key = ''; 00269 } 00270 if (empty($secret)) { 00271 $secret = ''; 00272 } 00273 00274 $strrequired = get_string('required'); 00275 00276 $mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40')); 00277 $mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40')); 00278 00279 $mform->addRule('dropbox_key', $strrequired, 'required', null, 'client'); 00280 $mform->addRule('dropbox_secret', $strrequired, 'required', null, 'client'); 00281 $str_getkey = get_string('instruction', 'repository_dropbox'); 00282 $mform->addElement('static', null, '', $str_getkey); 00283 } 00284 00289 public static function get_type_option_names() { 00290 return array('dropbox_key', 'dropbox_secret', 'pluginname'); 00291 } 00292 00297 public function supported_filetypes() { 00298 return '*'; 00299 } 00300 00305 public function supported_returntypes() { 00306 return FILE_INTERNAL; 00307 } 00308 }