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