MDL-35991 - use PARAM_LOCALURL for local urls
[moodle.git] / lib / boxlib.php
blob974124f89ffc05f1184ab4b7b9eefaf8d02d70be
1 <?php
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.
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/>.
17 /**
18 * Box REST Client Library for PHP5 Developers
21 * @package moodlecore
22 * @author James Levy <james@box.net>
23 * @link http://enabled.box.net
24 * @access public
25 * @version 1.0
26 * @copyright copyright Box.net 2007
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 /**
31 * @package moodlecore
32 * @copyright copyright Box.net 2007
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class boxclient {
36 /** @var string */
37 public $auth_token = '';
38 /** @var string */
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';
43 private $_error_code = '';
44 private $_error_msg = '';
45 /** @var bool */
46 private $debug = false;
48 /**
49 * @param string $api_key
50 * @param string $auth_token
51 * @param bool $debug
53 public function __construct($api_key, $auth_token = '', $debug = false) {
54 $this->api_key = $api_key;
55 $this->auth_token = $auth_token;
56 if (!empty($debug)) {
57 $this->debug = true;
58 } else {
59 $this->debug = false;
62 /**
63 * Setup for Functions
65 * @param string $method
66 * @param array $params
67 * @return array
69 function makeRequest($method, $params = array()) {
70 $this->_clearErrors();
71 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
72 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
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);
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;
90 return $data;
92 /**
93 * @param array $params
94 * @return array
96 function getTicket($params = array()) {
97 $params['api_key'] = $this->api_key;
98 $params['action'] = 'get_ticket';
99 $ret_array = array();
100 $data = $this->makeRequest('action=get_ticket', $params);
101 if ($this->_checkForError($data)) {
102 return false;
104 foreach ($data as $a) {
105 switch ($a['tag']) {
106 case 'STATUS':
107 $ret_array['status'] = $a['value'];
108 break;
109 case 'TICKET':
110 $ret_array['ticket'] = $a['value'];
111 break;
114 return $ret_array;
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:
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>
131 * @param string $ticket
132 * @param string $username
133 * @param string $password
134 * @return mixed
136 function getAuthToken($ticket, $username, $password) {
137 $c = new curl(array('debug'=>$this->debug));
138 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
139 $param = array(
140 'login_form1'=>'',
141 'login'=>$username,
142 'password'=>$password,
143 'dologin'=>1,
144 '__login'=>1
146 try {
147 $ret = $c->post($this->_box_api_auth_url.'/'.$ticket, $param);
148 } catch (moodle_exception $e) {
149 $this->setError(0, 'connection time-out or invalid url');
150 return false;
152 $header = $c->getResponse();
153 if(empty($header['location'])) {
154 throw new repository_exception('invalidpassword', 'repository_boxnet');
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;
161 return $auth_token;
162 } else {
163 throw new repository_exception('invalidtoken', 'repository_boxnet');
167 * @param string $path Unused
168 * @param array $params
169 * @return array
171 function getfiletree($path, $params = array()) {
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';
179 $c = new curl(array('debug'=>$this->debug));
180 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
181 try {
182 $args = array();
183 $xml = $c->get($this->_box_api_url, $params);
184 } catch (Exception $e){
186 $ret = array();
187 $o = simplexml_load_string(trim($xml));
188 if($o->status == 'listing_ok') {
189 $tree = $o->tree->folder;
190 $this->buildtree($tree, $ret);
192 return $ret;
196 * Get box.net file info
198 * @param string $fileid
199 * @param int $timeout request timeout in seconds
200 * @return stdClass|null
202 function get_file_info($fileid, $timeout = 0) {
203 $this->_clearErrors();
204 $params = array();
205 $params['action'] = 'get_file_info';
206 $params['file_id'] = $fileid;
207 $params['auth_token'] = $this->auth_token;
208 $params['api_key'] = $this->api_key;
209 $http = new curl(array('debug'=>$this->debug));
210 $xml = $http->get($this->_box_api_url, $params, array('timeout' => $timeout));
211 if (!$http->get_errno()) {
212 $o = simplexml_load_string(trim($xml));
213 if ($o->status == 's_get_file_info') {
214 return $o->info;
217 return null;
221 * @param array $sax
222 * @param array $tree Passed by reference
224 function buildtree($sax, &$tree){
225 $sax = (array)$sax;
226 $count = 0;
227 foreach($sax as $k=>$v){
228 if($k == 'folders'){
229 $o = $sax[$k];
230 foreach($o->folder as $z){
231 $tmp = array('title'=>(string)$z->attributes()->name,
232 'size'=>0, 'date'=>userdate(time()),
233 'thumbnail'=>'https://www.box.com/img/small_folder_icon.gif',
234 'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
235 $tmp['children'] = array();
236 $this->buildtree($z, $tmp['children']);
237 $tree[] = $tmp;
239 } elseif ($k == 'files') {
240 $val = $sax[$k]->file;
241 foreach($val as $file){
242 $thumbnail = (string)$file->attributes()->thumbnail;
243 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
244 $thumbnail = 'http://www.box.com'.$thumbnail;
246 $tmp = array('title'=>(string)$file->attributes()->file_name,
247 'size'=>display_size((int)$file->attributes()->size),
248 'thumbnail'=>$thumbnail,
249 'date'=>userdate((int)$file->attributes()->updated),
250 'source'=> $this->_box_api_download_url .'/'
251 .$this->auth_token.'/'.(string)$file->attributes()->id,
252 'url'=>(string)$file->attributes()->shared_link);
253 $tree[] = $tmp;
256 $count++;
260 * @param array $params
261 * @return bool|array Array or false
263 function getAccountTree($params = array()) {
264 $params['auth_token'] = $this->auth_token;
265 $params['folder_id'] = 0;
266 $params['api_key'] = $this->api_key;
267 $params['action'] = 'get_account_tree';
268 $params['onelevel'] = 1;
269 $params['params[]'] = 'nozip';
270 $ret_array = array();
271 $data = $this->makeRequest('action=get_account_tree', $params);
272 if ($this->_checkForError($data)) {
273 return false;
275 $tree_count=count($data);
276 $entry_count = 0;
277 for ($i=0; $i<$tree_count; $i++) {
278 $a = $data[$i];
279 switch ($a['tag'])
281 case 'FOLDER':
282 if (@is_array($a['attributes'])) {
283 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
284 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
285 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
287 break;
289 case 'FILE':
290 if (@is_array($a['attributes'])) {
291 $ret_array['file_id'][$i] = $a['attributes']['ID'];
292 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
293 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
294 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
295 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
296 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
297 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
298 } else {
299 @$ret_array['thumbnail'][$i] = 'http://www.box.com'.$a['attributes']['THUMBNAIL'];
301 $entry_count++;
303 break;
306 return $ret_array;
310 * @param string $new_folder_name
311 * @param array $params
312 * @return bool|array Array or false
314 function CreateFolder($new_folder_name, $params = array()) {
315 $params['auth_token'] = $this->auth_token;
316 $params['api_key'] = $this->api_key;
317 $params['action'] = 'create_folder';
318 $params['name'] = $new_folder_name;
319 $defaults = array(
320 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
321 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
323 foreach ($defaults as $key => $value) {
324 if (!array_key_exists($key, $params)) {
325 $params[$key] = $value;
329 $ret_array = array();
330 $data = $this->makeRequest('action=create_folder', $params);
331 if ($this->_checkForError($data)) {
332 return false;
334 foreach ($data as $a) {
335 if (!empty($a['value'])) {
336 switch ($a['tag']) {
338 case 'FOLDER_ID':
339 $ret_array['folder_id'] = $a['value'];
340 break;
342 case 'FOLDER_NAME':
343 $ret_array['folder_name'] = $a['value'];
344 break;
346 case 'FOLDER_TYPE_ID':
347 $ret_array['folder_type_id'] = $a['value'];
348 break;
350 case 'SHARED':
351 $ret_array['shared'] = $a['value'];
352 break;
354 case 'PASSWORD':
355 $ret_array['password'] = $a['value'];
356 break;
358 } else {
359 $ret_array[strtolower($a['tag'])] = null;
362 return $ret_array;
366 * Upload a File
367 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
368 * @return array|bool Array or false
370 function UploadFile ($params = array()) {
371 $params['auth_token'] = $this->auth_token;
372 // this param should be the full path of the file
373 $params['new_file1'] = $params['file'];
374 unset($params['file']);
375 $defaults = array(
376 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
377 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
379 foreach ($defaults as $key => $value) {
380 if (!array_key_exists($key, $params)) {
381 $params[$key] = $value;
384 $ret_array = array();
385 $entry_count = 0;
386 $data = $this->makeRequest('upload', $params);
387 if ($this->_checkForError($data)) {
388 return false;
390 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
391 $a = $data[$i];
392 switch ($a['tag']) {
393 case 'STATUS':
394 $ret_array['status'] = $a['value'];
395 break;
397 case 'FILE':
398 if (is_array($a['attributes'])) {
399 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
400 @$ret_array['id'][$i] = $a['attributes']['ID'];
401 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
402 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
403 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
404 $entry_count++;
406 break;
410 return $ret_array;
413 * @param string $fileid
414 * @param string $newname
415 * @return bool
417 function RenameFile($fileid, $newname) {
418 $params = array(
419 'api_key' => $this->api_key,
420 'auth_token' => $this->auth_token,
421 'action' => 'rename',
422 'target' => 'file',
423 'target_id' => $fileid,
424 'new_name' => $newname,
426 $data = $this->makeRequest('action=rename', $params);
427 if ($this->_checkForError($data)) {
428 return false;
430 foreach ($data as $a) {
431 switch ($a['tag']) {
432 case 'STATUS':
433 if ($a['value'] == 's_rename_node') {
434 return true;
438 return false;
442 * Register New User
444 * @param array $params
445 * @return array|bool Outcome Array or false
447 function RegisterUser($params = array()) {
448 $params['api_key'] = $this->api_key;
449 $params['action'] = 'register_new_user';
450 $params['login'] = $_REQUEST['login'];
451 $params['password'] = $_REQUEST['password'];
452 $ret_array = array();
453 $data = $this->makeRequest('action=register_new_user', $params);
454 if ($this->_checkForError($data)) {
455 return false;
457 foreach ($data as $a) {
458 switch ($a['tag']) {
459 case 'STATUS':
460 $ret_array['status'] = $a['value'];
461 break;
463 case 'AUTH_TOKEN':
464 $ret_array['auth_token'] = $a['value'];
465 break;
467 case 'LOGIN':
468 $ret_array['login'] = $a['value'];
469 break;
470 case 'SPACE_AMOUNT':
471 $ret_array['space_amount'] = $a['value'];
472 break;
473 case 'SPACE_USED':
474 $ret_array['space_used'] = $a['value'];
475 break;
479 return $ret_array;
483 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
485 * @param string $tag
486 * @param string $id Set to ID of file or folder
487 * @param string $target_type File or folder
488 * @param array $params
489 * @return array|bool Outcome Array or false
491 function AddTag($tag, $id, $target_type, $params = array()) {
492 $params['auth_token'] = $this->auth_token;
493 $params['api_key'] = $this->api_key;
494 $params['action'] = 'add_to_tag';
495 $params['target'] = $target_type; // File or folder
496 $params['target_id'] = $id; // Set to ID of file or folder
497 $params['tags[]'] = $tag;
498 $ret_array = array();
499 $data = $this->makeRequest('action=add_to_tag', $params);
500 if ($this->_checkForError($data)) {
501 return false;
503 foreach ($data as $a) {
504 switch ($a['tag']) {
505 case 'STATUS':
506 $ret_array['status'] = $a['value'];
508 break;
511 return $ret_array;
515 * Public Share (http://enabled.box.net/docs/rest#public_share)
517 * @param string $message
518 * @param string $emails
519 * @param string $id Set to ID of file or folder
520 * @param string $target_type File or folder
521 * @param string $password
522 * @param array $params
523 * @return array|bool Outcome Array or false
525 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
526 $params['auth_token'] = $this->auth_token;
527 $params['api_key'] = $this->api_key;
528 $params['action'] = 'public_share';
529 $params['target'] = $target_type;
530 $params['target_id'] = $id;
531 $params['password'] = $password;
532 $params['message'] = $message;
533 $params['emails'] = $emails;
534 $ret_array = array();
535 $data = $this->makeRequest('action=public_share', $params);
536 if ($this->_checkForError($data)) {
537 return false;
539 foreach ($data as $a) {
540 switch ($a['tag']) {
541 case 'STATUS':
542 $ret_array['status'] = $a['value'];
543 break;
544 case 'PUBLIC_NAME':
545 $ret_array['public_name'] = $a['value'];
546 break;
550 return $ret_array;
553 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
555 * @param array $params
556 * @return array|bool Outcome Array or false
558 function GetFriends ($params = array()) {
559 $params['auth_token'] = $this->auth_token;
560 $params['action'] = 'get_friends';
561 $params['api_key'] = $this->api_key;
562 $params['params[]'] = 'nozip';
563 $ret_array = array();
564 $data = $this->makeRequest('action=get_friends', $params);
565 if ($this->_checkForError($data)) {
566 return false;
568 foreach ($data as $a) {
569 switch ($a['tag']) {
570 case 'NAME':
571 $ret_array['name'] = $a['value'];
572 break;
573 case 'EMAIL':
574 $ret_array['email'] = $a['value'];
575 break;
576 case 'ACCEPTED':
577 $ret_array['accepted'] = $a['value'];
578 break;
579 case 'AVATAR_URL':
580 $ret_array['avatar_url'] = $a['value'];
581 break;
582 case 'ID':
583 $ret_array['id'] = $a['value'];
584 break;
585 case 'URL':
586 $ret_array['url'] = $a['value'];
587 break;
588 case 'STATUS':
589 $ret_array['status'] = $a['value'];
590 break;
593 return $ret_array;
597 * Logout User (http://enabled.box.net/docs/rest#get_friends)
599 * @param array $params
600 * @return array|bool Outcome Array or false
602 function Logout($params = array()) {
603 $params['auth_token'] = $this->auth_token;
604 $params['api_key'] = $this->api_key;
605 $params['action'] = 'logout';
606 $ret_array = array();
607 $data = $this->makeRequest('action=logout', $params);
608 if ($this->_checkForError($data)) {
609 return false;
611 foreach ($data as $a) {
612 switch ($a['tag']) {
613 case 'ACTION':
614 $ret_array['logout'] = $a['value'];
616 break;
618 return $ret_array;
622 * @param array $data
623 * @return bool
625 function _checkForError($data) {
626 if ($this->_error_msg != '') {
627 return true;
629 if (@$data[0]['attributes']['STAT'] == 'fail') {
630 $this->_error_code = $data[1]['attributes']['CODE'];
631 $this->_error_msg = $data[1]['attributes']['MSG'];
632 return true;
634 return false;
638 * @return bool
640 public function isError() {
641 if ($this->_error_msg != '') {
642 return true;
644 return false;
649 public function setError($code = 0, $msg){
650 $this->_error_code = $code;
651 $this->_error_msg = $msg;
654 * @return string
656 function getErrorMsg() {
657 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
660 * @return string
662 function getErrorCode() {
663 return $this->_error_code;
668 function _clearErrors() {
669 $this->_error_code = '';
670 $this->_error_msg = '';