MDL-32990 - Repositories - Updating Box.net API calls to use HTTPS
[moodle.git] / lib / boxlib.php
CommitLineData
d710e100 1<?php
aaeba371 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/>.
911e0eb0 16
d710e100 17/**
18 * Box REST Client Library for PHP5 Developers
aa754fe3 19 *
20 *
aaeba371 21 * @package moodlecore
d710e100 22 * @author James Levy <james@box.net>
23 * @link http://enabled.box.net
24 * @access public
25 * @version 1.0
6d19ced2 26 * @copyright copyright Box.net 2007
aaeba371 27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 */
29
30/**
31 * @package moodlecore
32 * @copyright copyright Box.net 2007
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
d710e100 34 */
d710e100 35class boxclient {
6d19ced2 36 /** @var string */
34f210f6 37 public $auth_token = '';
6d19ced2 38 /** @var string */
9e938253
JF
39 private $_box_api_url = 'https://www.box.com/api/1.0/rest';
40 private $_box_api_upload_url = 'http://upload.box.com/api/1.0/upload';
41 private $_box_api_download_url = 'http://www.box.com/api/1.0/download';
42 private $_box_api_auth_url = 'http://www.box.com/api/1.0/auth';
34f210f6 43 private $_error_code = '';
44 private $_error_msg = '';
6d19ced2 45 /** @var bool */
6135bd45 46 private $debug = false;
aa754fe3 47
6d19ced2 48 /**
49 * @param string $api_key
50 * @param string $auth_token
51 * @param bool $debug
52 */
6135bd45 53 public function __construct($api_key, $auth_token = '', $debug = false) {
d710e100 54 $this->api_key = $api_key;
a9493cbe 55 $this->auth_token = $auth_token;
5430f05b 56 if (!empty($debug)) {
57 $this->debug = true;
58 } else {
59 $this->debug = false;
60 }
6f5e0852 61 }
6d19ced2 62 /**
63 * Setup for Functions
64 *
65 * @param string $method
66 * @param array $params
6f5e0852 67 * @return array
6d19ced2 68 */
d710e100 69 function makeRequest($method, $params = array()) {
70 $this->_clearErrors();
5430f05b 71 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
9e938253 72 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
6135bd45 73 try {
74 if ($method == 'upload'){
75 $request = $this->_box_api_upload_url.'/'.
76 $this->auth_token.'/'.$params['folder_id'];
77 $xml = $c->post($request, $params);
78 }else{
79 $args = array();
80 $xml = $c->get($this->_box_api_url, $params);
81 }
82 $xml_parser = xml_parser_create();
83 // set $data here
84 xml_parse_into_struct($xml_parser, $xml, $data);
85 xml_parser_free($xml_parser);
86 } catch (moodle_exception $e) {
87 $this->setError(0, 'connection time-out or invalid url');
88 return false;
d710e100 89 }
d710e100 90 return $data;
91 }
6d19ced2 92 /**
93 * @param array $params
6f5e0852 94 * @return array
6d19ced2 95 */
d710e100 96 function getTicket($params = array()) {
34f210f6 97 $params['api_key'] = $this->api_key;
98 $params['action'] = 'get_ticket';
d710e100 99 $ret_array = array();
100 $data = $this->makeRequest('action=get_ticket', $params);
101 if ($this->_checkForError($data)) {
102 return false;
103 }
104 foreach ($data as $a) {
105 switch ($a['tag']) {
a9493cbe 106 case 'STATUS':
107 $ret_array['status'] = $a['value'];
108 break;
109 case 'TICKET':
110 $ret_array['ticket'] = $a['value'];
111 break;
d710e100 112 }
113 }
d710e100 114 return $ret_array;
115 }
aa754fe3 116
6d19ced2 117 /**
118 * $options['username'] and $options['password'] must be
119 * given, we will use them to obtain a valid auth_token
120 * To get a token, you should use following code:
121 *
122 * <code>
123 * $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
124 * Get a ticket
125 * $t = $box->getTicket();
126 * $box->getAuthToken($t['ticket'], array(
127 * 'username'=>'dongsheng@moodle.com',
128 * 'password'=>'xxx'));
129 * </code>
130 *
131 * @param string $ticket
132 * @param string $username
133 * @param string $password
134 * @return mixed
135 */
bb2c046d 136 function getAuthToken($ticket, $username, $password) {
5430f05b 137 $c = new curl(array('debug'=>$this->debug));
34f210f6 138 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
139 $param = array(
140 'login_form1'=>'',
bb2c046d 141 'login'=>$username,
142 'password'=>$password,
34f210f6 143 'dologin'=>1,
144 '__login'=>1
145 );
6135bd45 146 try {
9e938253 147 $ret = $c->post($this->_box_api_auth_url.$ticket, $param);
6135bd45 148 } catch (moodle_exception $e) {
149 $this->setError(0, 'connection time-out or invalid url');
150 return false;
151 }
34f210f6 152 $header = $c->getResponse();
1afc87ad 153 if(empty($header['location'])) {
f5b57320 154 throw new repository_exception('invalidpassword', 'repository_boxnet');
1afc87ad 155 }
34f210f6 156 $location = $header['location'];
157 preg_match('#auth_token=(.*)$#i', $location, $matches);
158 $auth_token = $matches[1];
159 if(!empty($auth_token)) {
160 $this->auth_token = $auth_token;
0eb58cf4 161 return $auth_token;
34f210f6 162 } else {
f5b57320 163 throw new repository_exception('invalidtoken', 'repository_boxnet');
aa754fe3 164 }
d710e100 165 }
6d19ced2 166 /**
167 * @param string $path Unused
168 * @param array $params
169 * @return array
170 */
31640be2 171 function getfiletree($path, $params = array()) {
68e374a6 172 $this->_clearErrors();
173 $params['auth_token'] = $this->auth_token;
174 $params['folder_id'] = 0;
175 $params['api_key'] = $this->api_key;
176 $params['action'] = 'get_account_tree';
177 $params['onelevel'] = 1;
178 $params['params[]'] = 'nozip';
5430f05b 179 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
9e938253 180 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
68e374a6 181 try {
182 $args = array();
183 $xml = $c->get($this->_box_api_url, $params);
184 } catch (Exception $e){
185 }
186 $ret = array();
55b4bb1d 187 $o = simplexml_load_string(trim($xml));
68e374a6 188 if($o->status == 'listing_ok') {
189 $tree = $o->tree->folder;
190 $this->buildtree($tree, $ret);
191 }
192 return $ret;
193 }
aa754fe3 194
d6453211
DC
195 /**
196 * Get box.net file info
197 *
198 * @param string $fileid
199 * @return string|null
200 */
201 function get_file_info($fileid) {
202 $this->_clearErrors();
203 $params = array();
204 $params['action'] = 'get_file_info';
205 $params['file_id'] = $fileid;
206 $params['auth_token'] = $this->auth_token;
207 $params['api_key'] = $this->api_key;
208 $http = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
209 $xml = $http->get($this->_box_api_url, $params);
210 $o = simplexml_load_string(trim($xml));
211 if ($o->status == 's_get_file_info') {
212 return $o->info;
213 } else {
214 return null;
215 }
216 }
217
6d19ced2 218 /**
219 * @param array $sax
220 * @param array $tree Passed by reference
221 */
68e374a6 222 function buildtree($sax, &$tree){
223 $sax = (array)$sax;
31640be2 224 $count = 0;
68e374a6 225 foreach($sax as $k=>$v){
226 if($k == 'folders'){
227 $o = $sax[$k];
228 foreach($o->folder as $z){
229 $tmp = array('title'=>(string)$z->attributes()->name,
230 'size'=>0, 'date'=>userdate(time()),
9e938253 231 'thumbnail'=>'https://www.box.com/img/small_folder_icon.gif',
31640be2 232 'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
68e374a6 233 $tmp['children'] = array();
234 $this->buildtree($z, $tmp['children']);
235 $tree[] = $tmp;
236 }
237 } elseif ($k == 'files') {
238 $val = $sax[$k]->file;
239 foreach($val as $file){
240 $thumbnail = (string)$file->attributes()->thumbnail;
241 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
9e938253 242 $thumbnail = 'http://www.box.com'.$thumbnail;
68e374a6 243 }
244 $tmp = array('title'=>(string)$file->attributes()->file_name,
245 'size'=>display_size((int)$file->attributes()->size),
246 'thumbnail'=>$thumbnail,
6f5e0852 247 'date'=>userdate((int)$file->attributes()->updated),
9e938253 248 'source'=> $this->_box_api_download_url
31640be2 249 .$this->auth_token.'/'.(string)$file->attributes()->id,
250 'url'=>(string)$file->attributes()->shared_link);
68e374a6 251 $tree[] = $tmp;
252 }
253 }
31640be2 254 $count++;
68e374a6 255 }
256 }
6d19ced2 257 /**
258 * @param array $params
259 * @return bool|array Array or false
260 */
d710e100 261 function getAccountTree($params = array()) {
34f210f6 262 $params['auth_token'] = $this->auth_token;
263 $params['folder_id'] = 0;
264 $params['api_key'] = $this->api_key;
265 $params['action'] = 'get_account_tree';
266 $params['onelevel'] = 1;
267 $params['params[]'] = 'nozip';
d710e100 268 $ret_array = array();
269 $data = $this->makeRequest('action=get_account_tree', $params);
270 if ($this->_checkForError($data)) {
271 return false;
272 }
273 $tree_count=count($data);
3570711a 274 $entry_count = 0;
6135bd45 275 for ($i=0; $i<$tree_count; $i++) {
d710e100 276 $a = $data[$i];
aa754fe3 277 switch ($a['tag'])
d710e100 278 {
a9493cbe 279 case 'FOLDER':
280 if (@is_array($a['attributes'])) {
281 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
282 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
283 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
284 }
285 break;
286
287 case 'FILE':
288 if (@is_array($a['attributes'])) {
289 $ret_array['file_id'][$i] = $a['attributes']['ID'];
290 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
291 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
284cf75f 292 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
293 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
c145b657 294 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
295 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
296 } else {
9e938253 297 @$ret_array['thumbnail'][$i] = 'http://www.box.com'.$a['attributes']['THUMBNAIL'];
c145b657 298 }
a9493cbe 299 $entry_count++;
300 }
301 break;
d710e100 302 }
303 }
d710e100 304 return $ret_array;
305 }
34f210f6 306
6d19ced2 307 /**
308 * @param string $new_folder_name
309 * @param array $params
310 * @return bool|array Array or false
311 */
a9493cbe 312 function CreateFolder($new_folder_name, $params = array()) {
34f210f6 313 $params['auth_token'] = $this->auth_token;
a9493cbe 314 $params['api_key'] = $this->api_key;
34f210f6 315 $params['action'] = 'create_folder';
a9493cbe 316 $params['name'] = $new_folder_name;
dff30094 317 $defaults = array(
318 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
319 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
320 );
321 foreach ($defaults as $key => $value) {
322 if (!array_key_exists($key, $params)) {
323 $params[$key] = $value;
324 }
325 }
aa754fe3 326
a9493cbe 327 $ret_array = array();
328 $data = $this->makeRequest('action=create_folder', $params);
a9493cbe 329 if ($this->_checkForError($data)) {
330 return false;
331 }
332 foreach ($data as $a) {
2de72cb5 333 if (!empty($a['value'])) {
9e5f5f9f 334 switch ($a['tag']) {
a9493cbe 335
9e5f5f9f 336 case 'FOLDER_ID':
337 $ret_array['folder_id'] = $a['value'];
338 break;
615a2859 339
9e5f5f9f 340 case 'FOLDER_NAME':
341 $ret_array['folder_name'] = $a['value'];
342 break;
a9493cbe 343
9e5f5f9f 344 case 'FOLDER_TYPE_ID':
345 $ret_array['folder_type_id'] = $a['value'];
346 break;
347
348 case 'SHARED':
615a2859 349 $ret_array['shared'] = $a['value'];
9e5f5f9f 350 break;
351
352 case 'PASSWORD':
353 $ret_array['password'] = $a['value'];
354 break;
615a2859 355 }
9e5f5f9f 356 } else {
357 $ret_array[strtolower($a['tag'])] = null;
d710e100 358 }
a9493cbe 359 }
360 return $ret_array;
aa754fe3 361 }
db79c1b9 362
6f5e0852 363 /**
6d19ced2 364 * Upload a File
365 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
366 * @return array|bool Array or false
367 */
a9493cbe 368 function UploadFile ($params = array()) {
369 $params['auth_token'] = $this->auth_token;
370 // this param should be the full path of the file
db79c1b9 371 $params['new_file1'] = $params['file'];
372 unset($params['file']);
b3fac92f 373 $defaults = array(
374 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
375 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
376 );
377 foreach ($defaults as $key => $value) {
378 if (!array_key_exists($key, $params)) {
379 $params[$key] = $value;
380 }
381 }
a9493cbe 382 $ret_array = array();
1afc87ad 383 $entry_count = 0;
a9493cbe 384 $data = $this->makeRequest('upload', $params);
385 if ($this->_checkForError($data)) {
386 return false;
387 }
388 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
389 $a = $data[$i];
390 switch ($a['tag']) {
391 case 'STATUS':
392 $ret_array['status'] = $a['value'];
393 break;
394
395 case 'FILE':
396 if (is_array($a['attributes'])) {
1afc87ad 397 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
398 @$ret_array['id'][$i] = $a['attributes']['ID'];
399 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
400 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
401 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
a9493cbe 402 $entry_count++;
403 }
404 break;
d710e100 405 }
406 }
aa754fe3 407
a9493cbe 408 return $ret_array;
aa754fe3 409 }
6d19ced2 410 /**
411 * @param string $fileid
412 * @param string $newname
413 * @return bool
414 */
db79c1b9 415 function RenameFile($fileid, $newname) {
416 $params = array(
417 'api_key' => $this->api_key,
418 'auth_token' => $this->auth_token,
419 'action' => 'rename',
420 'target' => 'file',
421 'target_id' => $fileid,
422 'new_name' => $newname,
423 );
424 $data = $this->makeRequest('action=rename', $params);
425 if ($this->_checkForError($data)) {
426 return false;
427 }
428 foreach ($data as $a) {
429 switch ($a['tag']) {
430 case 'STATUS':
5ec4b697 431 if ($a['value'] == 's_rename_node') {
db79c1b9 432 return true;
433 }
434 }
435 }
436 return false;
437 }
438
6d19ced2 439 /**
440 * Register New User
441 *
442 * @param array $params
443 * @return array|bool Outcome Array or false
444 */
a9493cbe 445 function RegisterUser($params = array()) {
34f210f6 446 $params['api_key'] = $this->api_key;
447 $params['action'] = 'register_new_user';
448 $params['login'] = $_REQUEST['login'];
449 $params['password'] = $_REQUEST['password'];
a9493cbe 450 $ret_array = array();
451 $data = $this->makeRequest('action=register_new_user', $params);
452 if ($this->_checkForError($data)) {
453 return false;
454 }
455 foreach ($data as $a) {
456 switch ($a['tag']) {
457 case 'STATUS':
458 $ret_array['status'] = $a['value'];
459 break;
460
461 case 'AUTH_TOKEN':
462 $ret_array['auth_token'] = $a['value'];
463 break;
464
465 case 'LOGIN':
466 $ret_array['login'] = $a['value'];
467 break;
468 case 'SPACE_AMOUNT':
469 $ret_array['space_amount'] = $a['value'];
470 break;
471 case 'SPACE_USED':
472 $ret_array['space_used'] = $a['value'];
473 break;
d710e100 474 }
475 }
aa754fe3 476
a9493cbe 477 return $ret_array;
aa754fe3 478 }
479
6d19ced2 480 /**
481 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
482 *
483 * @param string $tag
484 * @param string $id Set to ID of file or folder
485 * @param string $target_type File or folder
486 * @param array $params
487 * @return array|bool Outcome Array or false
488 */
d710e100 489 function AddTag($tag, $id, $target_type, $params = array()) {
34f210f6 490 $params['auth_token'] = $this->auth_token;
491 $params['api_key'] = $this->api_key;
492 $params['action'] = 'add_to_tag';
493 $params['target'] = $target_type; // File or folder
494 $params['target_id'] = $id; // Set to ID of file or folder
d710e100 495 $params['tags[]'] = $tag;
496 $ret_array = array();
497 $data = $this->makeRequest('action=add_to_tag', $params);
498 if ($this->_checkForError($data)) {
499 return false;
500 }
501 foreach ($data as $a) {
502 switch ($a['tag']) {
a9493cbe 503 case 'STATUS':
504 $ret_array['status'] = $a['value'];
aa754fe3 505
a9493cbe 506 break;
d710e100 507 }
508 }
d710e100 509 return $ret_array;
d710e100 510 }
aa754fe3 511
6d19ced2 512 /**
513 * Public Share (http://enabled.box.net/docs/rest#public_share)
514 *
515 * @param string $message
516 * @param string $emails
517 * @param string $id Set to ID of file or folder
518 * @param string $target_type File or folder
519 * @param string $password
520 * @param array $params
521 * @return array|bool Outcome Array or false
522 */
d710e100 523 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
34f210f6 524 $params['auth_token'] = $this->auth_token;
525 $params['api_key'] = $this->api_key;
526 $params['action'] = 'public_share';
527 $params['target'] = $target_type;
528 $params['target_id'] = $id;
529 $params['password'] = $password;
d710e100 530 $params['message'] = $message;
34f210f6 531 $params['emails'] = $emails;
d710e100 532 $ret_array = array();
533 $data = $this->makeRequest('action=public_share', $params);
534 if ($this->_checkForError($data)) {
535 return false;
536 }
537 foreach ($data as $a) {
538 switch ($a['tag']) {
a9493cbe 539 case 'STATUS':
540 $ret_array['status'] = $a['value'];
541 break;
542 case 'PUBLIC_NAME':
543 $ret_array['public_name'] = $a['value'];
544 break;
d710e100 545 }
546 }
aa754fe3 547
d710e100 548 return $ret_array;
549 }
6d19ced2 550 /**
551 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
552 *
553 * @param array $params
554 * @return array|bool Outcome Array or false
555 */
a9493cbe 556 function GetFriends ($params = array()) {
34f210f6 557 $params['auth_token'] = $this->auth_token;
558 $params['action'] = 'get_friends';
559 $params['api_key'] = $this->api_key;
560 $params['params[]'] = 'nozip';
d710e100 561 $ret_array = array();
562 $data = $this->makeRequest('action=get_friends', $params);
563 if ($this->_checkForError($data)) {
564 return false;
565 }
566 foreach ($data as $a) {
567 switch ($a['tag']) {
a9493cbe 568 case 'NAME':
569 $ret_array['name'] = $a['value'];
570 break;
571 case 'EMAIL':
572 $ret_array['email'] = $a['value'];
573 break;
574 case 'ACCEPTED':
575 $ret_array['accepted'] = $a['value'];
576 break;
577 case 'AVATAR_URL':
578 $ret_array['avatar_url'] = $a['value'];
579 break;
580 case 'ID':
581 $ret_array['id'] = $a['value'];
582 break;
583 case 'URL':
584 $ret_array['url'] = $a['value'];
585 break;
586 case 'STATUS':
587 $ret_array['status'] = $a['value'];
588 break;
d710e100 589 }
590 }
d710e100 591 return $ret_array;
592 }
aa754fe3 593
6d19ced2 594 /**
595 * Logout User (http://enabled.box.net/docs/rest#get_friends)
596 *
597 * @param array $params
598 * @return array|bool Outcome Array or false
599 */
a9493cbe 600 function Logout($params = array()) {
34f210f6 601 $params['auth_token'] = $this->auth_token;
602 $params['api_key'] = $this->api_key;
603 $params['action'] = 'logout';
a9493cbe 604 $ret_array = array();
605 $data = $this->makeRequest('action=logout', $params);
606 if ($this->_checkForError($data)) {
607 return false;
608 }
609 foreach ($data as $a) {
610 switch ($a['tag']) {
611 case 'ACTION':
612 $ret_array['logout'] = $a['value'];
aa754fe3 613
a9493cbe 614 break;
aa754fe3 615 }
d710e100 616 return $ret_array;
617 }
618 }
6d19ced2 619 /**
620 * @param array $data
621 * @return bool
622 */
a9493cbe 623 function _checkForError($data) {
6135bd45 624 if ($this->_error_msg != '') {
625 return true;
626 }
a9493cbe 627 if (@$data[0]['attributes']['STAT'] == 'fail') {
628 $this->_error_code = $data[1]['attributes']['CODE'];
629 $this->_error_msg = $data[1]['attributes']['MSG'];
630 return true;
631 }
632 return false;
aa754fe3 633 }
aa754fe3 634
6d19ced2 635 /**
636 * @return bool
637 */
a9493cbe 638 public function isError() {
639 if ($this->_error_msg != '') {
640 return true;
641 }
642 return false;
aa754fe3 643 }
6d19ced2 644 /**
645 *
646 */
6135bd45 647 public function setError($code = 0, $msg){
648 $this->_error_code = $code;
649 $this->_error_msg = $msg;
650 }
6d19ced2 651 /**
652 * @return string
653 */
a9493cbe 654 function getErrorMsg() {
655 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
656 }
6d19ced2 657 /**
658 * @return string
659 */
a9493cbe 660 function getErrorCode() {
661 return $this->_error_code;
662 }
6d19ced2 663 /**
664 *
665 */
a9493cbe 666 function _clearErrors() {
667 $this->_error_code = '';
668 $this->_error_msg = '';
669 }
aa754fe3 670
d710e100 671}