MDL-32990 - Repositories - Updating Box.net API calls to use HTTPS
[moodle.git] / repository / boxnet / lib.php
CommitLineData
d710e100 1<?php
10d53fd3
DC
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
67233725
DC
17/**
18 * This plugin is used to access box.net repository
19 *
20 * @since 2.0
21 * @package repository_boxnet
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25require_once($CFG->dirroot . '/repository/lib.php');
26require_once($CFG->libdir . '/boxlib.php');
26c9ecb0 27
d710e100 28/**
67233725 29 * repository_boxnet class implements box.net client
d710e100 30 *
10d53fd3 31 * @since 2.0
67233725
DC
32 * @package repository_boxnet
33 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
d078f6d3 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d710e100 35 */
bddcd933 36class repository_boxnet extends repository {
3deff6c9 37 private $boxclient;
d710e100 38
96297ca2 39 /**
792ad3aa 40 * Constructor
67233725 41 *
26c9ecb0 42 * @param int $repositoryid
67233725 43 * @param stdClass $context
26c9ecb0 44 * @param array $options
96297ca2 45 */
447c7a19 46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
a36670da 47 parent::__construct($repositoryid, $context, $options);
48 $this->api_key = $this->get_option('api_key');
3deff6c9
DC
49 $this->setting_prefix = 'boxnet_';
50
51 $this->auth_token = get_user_preferences($this->setting_prefix.'_auth_token', '');
52 $this->logged = false;
53 if (!empty($this->auth_token)) {
54 $this->logged = true;
d710e100 55 }
0eb58cf4 56 // already logged
3deff6c9
DC
57 if(!empty($this->logged)) {
58 if(empty($this->boxclient)) {
59 $this->boxclient = new boxclient($this->api_key, $this->auth_token);
bb2c046d 60 }
d710e100 61 } else {
3deff6c9 62 $this->boxclient = new boxclient($this->api_key);
d68c527f 63 }
64 }
65
96297ca2 66 /**
3deff6c9 67 * check if user logged
67233725 68 *
26c9ecb0 69 * @return boolean
96297ca2 70 */
bddcd933 71 public function check_login() {
3deff6c9 72 return $this->logged;
d68c527f 73 }
74
96297ca2 75 /**
3deff6c9 76 * reset auth token
96297ca2 77 *
26c9ecb0 78 * @return string
96297ca2 79 */
bddcd933 80 public function logout() {
3deff6c9
DC
81 // reset auth token
82 set_user_preference($this->setting_prefix . '_auth_token', '');
d68c527f 83 return $this->print_login();
84 }
85
96297ca2 86 /**
3deff6c9 87 * Save settings
96297ca2 88 *
26c9ecb0 89 * @param array $options
90 * @return mixed
96297ca2 91 */
bddcd933 92 public function set_option($options = array()) {
d68c527f 93 if (!empty($options['api_key'])) {
94 set_config('api_key', trim($options['api_key']), 'boxnet');
95 }
96 unset($options['api_key']);
97 $ret = parent::set_option($options);
98 return $ret;
99 }
100
96297ca2 101 /**
3deff6c9 102 * Get settings
96297ca2 103 *
26c9ecb0 104 * @param string $config
105 * @return mixed
96297ca2 106 */
bddcd933 107 public function get_option($config = '') {
353d5cf3 108 if($config==='api_key') {
d68c527f 109 return trim(get_config('boxnet', 'api_key'));
110 } else {
111 $options['api_key'] = trim(get_config('boxnet', 'api_key'));
112 }
113 $options = parent::get_option($config);
114 return $options;
115 }
116
96297ca2 117 /**
3deff6c9 118 * Search files from box.net
96297ca2 119 *
26c9ecb0 120 * @param string $search_text
121 * @return mixed
96297ca2 122 */
68a7c9a6 123 public function search($search_text, $page = 0) {
3deff6c9 124 global $OUTPUT;
0eb58cf4 125 $list = array();
126 $ret = array();
3deff6c9 127 $tree = $this->boxclient->getAccountTree();
353d5cf3 128 if (!empty($tree)) {
129 $filenames = $tree['file_name'];
130 $fileids = $tree['file_id'];
131 $filesizes = $tree['file_size'];
132 $filedates = $tree['file_date'];
133 $fileicon = $tree['thumbnail'];
134 foreach ($filenames as $n=>$v){
fc08d3c8 135 if(strstr(strtolower($v), strtolower($search_text)) !== false) {
4317f92f 136 $list[] = array('title'=>$v,
353d5cf3 137 'size'=>$filesizes[$n],
138 'date'=>$filedates[$n],
9e938253 139 'source'=>'https://www.box.com/api/1.0/download/'
527c4acd 140 .$this->auth_token.'/'.$fileids[$n],
559276b1 141 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($v, 90))->out(false));
d05ef4a3 142 }
143 }
d05ef4a3 144 }
c438f190 145 $ret['list'] = array_filter($list, array($this, 'filter'));
353d5cf3 146 return $ret;
147 }
96297ca2 148
149 /**
3deff6c9 150 * Get file listing
96297ca2 151 *
26c9ecb0 152 * @param string $path
67233725 153 * @param string $page
26c9ecb0 154 * @return mixed
96297ca2 155 */
5fa358d1 156 public function get_listing($path = '/', $page = ''){
353d5cf3 157 $list = array();
158 $ret = array();
159 $ret['list'] = array();
3deff6c9 160 $tree = $this->boxclient->getfiletree($path);
9e938253 161 $ret['manage'] = 'http://www.box.com/files';
353d5cf3 162 $ret['path'] = array(array('name'=>'Root', 'path'=>0));
163 if(!empty($tree)) {
c438f190 164 $ret['list'] = array_filter($tree, array($this, 'filter'));
d710e100 165 }
353d5cf3 166 return $ret;
d710e100 167 }
168
96297ca2 169 /**
3deff6c9 170 * Return login form
96297ca2 171 *
26c9ecb0 172 * @return array
96297ca2 173 */
353d5cf3 174 public function print_login(){
3deff6c9 175 $t = $this->boxclient->getTicket();
d68c527f 176 if ($this->options['ajax']) {
3deff6c9
DC
177 $popup_btn = new stdClass();
178 $popup_btn->type = 'popup';
9e938253 179 $popup_btn->url = ' https://www.box.com/api/1.0/auth/' . $t['ticket'];
d68c527f 180
181 $ret = array();
3deff6c9 182 $ret['login'] = array($popup_btn);
d68c527f 183 return $ret;
1c05907d 184 } else {
185 echo '<table>';
186 echo '<tr><td><label>'.get_string('username', 'repository_boxnet').'</label></td>';
187 echo '<td><input type="text" name="boxusername" /></td></tr>';
188 echo '<tr><td><label>'.get_string('password', 'repository_boxnet').'</label></td>';
189 echo '<td><input type="password" name="boxpassword" /></td></tr>';
190 echo '<input type="hidden" name="ticket" value="'.$t['ticket'].'" />';
191 echo '</table>';
362a728f 192 echo '<input type="submit" value="'.get_string('enter', 'repository').'" />';
d710e100 193 }
194 }
195
96297ca2 196 /**
792ad3aa 197 * Names of the plugin settings
67233725 198 *
26c9ecb0 199 * @return array
96297ca2 200 */
1b79955a 201 public static function get_type_option_names() {
a5adfa26 202 return array('api_key', 'pluginname');
a6600395 203 }
204
3deff6c9
DC
205 /**
206 * Store the auth token returned by box.net
207 */
208 public function callback() {
209 $this->auth_token = optional_param('auth_token', '', PARAM_TEXT);
210 set_user_preference($this->setting_prefix . '_auth_token', $this->auth_token);
211 }
212
96297ca2 213 /**
792ad3aa 214 * Add Plugin settings input to Moodle form
67233725
DC
215 *
216 * @param moodleform $mform
217 * @param string $classname
96297ca2 218 */
68a7c9a6 219 public static function type_config_form($mform, $classname = 'repository') {
3deff6c9 220 global $CFG;
a5adfa26 221 parent::type_config_form($mform);
a6600395 222 $public_account = get_config('boxnet', 'public_account');
ed2408e1 223 $api_key = get_config('boxnet', 'api_key');
224 if (empty($api_key)) {
225 $api_key = '';
226 }
4a65c39a 227 $strrequired = get_string('required');
a6600395 228 $mform->addElement('text', 'api_key', get_string('apikey', 'repository_boxnet'), array('value'=>$api_key,'size' => '40'));
4a65c39a 229 $mform->addRule('api_key', $strrequired, 'required', null, 'client');
e49eaacb 230 $mform->addElement('static', null, '', get_string('information','repository_boxnet'));
3deff6c9
DC
231
232 //retrieve the flickr instances
233 $params = array();
234 $params['context'] = array();
235 //$params['currentcontext'] = $this->context;
236 $params['onlyvisible'] = false;
237 $params['type'] = 'boxnet';
238 $instances = repository::get_instances($params);
239 if (empty($instances)) {
240 $callbackurl = get_string('callbackwarning', 'repository_boxnet');
241 $mform->addElement('static', null, '', $callbackurl);
242 } else {
243 $instance = array_shift($instances);
244 $callbackurl = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
245 $mform->addElement('static', 'callbackurl', '', get_string('callbackurltext', 'repository_boxnet', $callbackurl));
246 }
4a65c39a 247 }
67233725 248
3deff6c9
DC
249 /**
250 * Box.net supports file linking and copying
67233725
DC
251 *
252 * @return int
3deff6c9 253 */
41076c58 254 public function supported_returntypes() {
67233725
DC
255 return FILE_INTERNAL | FILE_EXTERNAL | FILE_REFERENCE;
256 }
257
258 /**
259 * Prepare file reference information
260 *
261 * @param string $source
262 * @return string file referece
263 */
264 public function get_file_reference($source) {
265 // Box.net returns a url.
266 return $source;
41076c58 267 }
4317f92f 268
67233725 269 /**
0b2bfbd1 270 * Returns information about file in this repository by reference
67233725
DC
271 * {@link repository::get_file_reference()}
272 * {@link repository::get_file()}
273 *
0b2bfbd1
MG
274 * Returns null if file not found or is not readable
275 *
67233725 276 * @param stdClass $reference file reference db record
0b2bfbd1 277 * @return null|stdClass with attribute 'filepath'
67233725
DC
278 */
279 public function get_file_by_reference($reference) {
67233725 280 $boxnetfile = $this->get_file($reference->reference);
0b2bfbd1
MG
281 // Please note that here we will ALWAYS receive a file
282 // If source file has been removed from external server, box.com still returns
283 // a plain/text file with content 'no such file' (filesize will be 12 bytes)
284 if (!empty($boxnetfile['path'])) {
285 return (object)array('filepath' => $boxnetfile['path']);
286 }
287 return null;
67233725
DC
288 }
289
290 /**
291 * Return human readable reference information
292 * {@link stored_file::get_reference()}
293 *
294 * @param string $reference
0b2bfbd1
MG
295 * @param int $filestatus status of the file, 0 - ok, 666 - source missing
296 * @return string
67233725 297 */
0b2bfbd1 298 public function get_reference_details($reference, $filestatus = 0) {
67233725 299 // Indicate it's from box.net repository + secure URL
0b2bfbd1
MG
300 $details = $this->get_name() . ': ' . $reference;
301 if (!$filestatus) {
302 return $details;
303 } else {
304 // at the moment for box.net files we never can be sure that source is missing
305 // because box.com never returns 404 error.
306 // So we never change the status and actually this part is unreachable
307 return get_string('lostsource', 'repository', $details);
308 }
67233725
DC
309 }
310
d6453211
DC
311 /**
312 * Return the source information
313 *
314 * @param stdClass $url
315 * @return string|null
316 */
317 public function get_file_source_info($url) {
318 $array = explode('/', $url);
319 $fileid = array_pop($array);
320 $fileinfo = $this->boxclient->get_file_info($fileid);
321 if (!empty($fileinfo)) {
322 return 'Box:' . (string)$fileinfo->file_name;
323 } else {
324 return $url;
325 }
326 }
327
67233725 328 /**
0b2bfbd1 329 * Repository method to serve the referenced file
67233725 330 *
0b2bfbd1 331 * @param stored_file $storedfile the file that contains the reference
67233725
DC
332 * @param int $lifetime Number of seconds before the file should expire from caches (default 24 hours)
333 * @param int $filter 0 (default)=no filtering, 1=all files, 2=html files only
334 * @param bool $forcedownload If true (default false), forces download of file rather than view in browser/plugin
335 * @param array $options additional options affecting the file serving
336 */
337 public function send_file($storedfile, $lifetime=86400 , $filter=0, $forcedownload=false, array $options = null) {
338 $ref = $storedfile->get_reference();
0b2bfbd1
MG
339 // Let box.net serve the file. It will return 'no such file' content if file not found
340 // also if file has the different name than alias, it will be returned with the box.net filename
67233725
DC
341 header('Location: ' . $ref);
342 }
343}