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