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