"MDL-16539, fix warning when create new foler"
[moodle.git] / lib / boxlib.php
1 <?php
2 /**
3  * Modified by Dongsheng Cai <dongsheng@moodle.com>
4  * @version: $Id$
5  */
7 /**
8  * Box REST Client Library for PHP5 Developers
9  *
10  *
11  * @author James Levy <james@box.net>
12  * @link http://enabled.box.net
13  * @access public
14  * @version 1.0
15  * copyright Box.net 2007
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  */
20 class boxclient {
21     public $auth_token = '';
23     private $_box_api_url = 'http://box.net/api/1.0/rest';
24     private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
25     private $_error_code = '';
26     private $_error_msg = '';
27     private $debug = false;
29     public function __construct($api_key, $auth_token = '', $debug = false) {
30         $this->api_key    = $api_key;
31         $this->auth_token = $auth_token;
32         $this->debug = $debug;
33     }
34     // Setup for Functions
35     function makeRequest($method, $params = array()) {
36         $this->_clearErrors();
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;
58         }
59         return $data;
60     }
61     function getTicket($params = array()) {
62         $params['api_key'] = $this->api_key;
63         $params['action']  = 'get_ticket';
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']) {
71             case 'STATUS':
72                 $ret_array['status'] = $a['value'];
73                 break;
74             case 'TICKET':
75                 $ret_array['ticket'] = $a['value'];
76                 break;
77             }
78         }
79         return $ret_array;
80     }
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     //
93     function getAuthToken($ticket, $username, $password) {
94         if($this->debug){
95             $c = new curl(array('debug'=>true));
96         } else {
97             $c = new curl(array('debug'=>false));
98         }
99         $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
100         $param =  array(
101             'login_form1'=>'',
102             'login'=>$username,
103             'password'=>$password,
104             'dologin'=>1,
105             '__login'=>1
106             );
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         }
113         $header = $c->getResponse();
114         if(empty($header['location'])) {
115             throw new repository_exception('invalidpassword', 'repository_boxnet');
116         }
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;
122             return $auth_token;
123         } else {
124             throw new repository_exception('invalidtoken', 'repository_boxnet');
125         }
126     }
127     //
128     function getfiletree($path, $params = array()) {
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();
147         $o = simplexml_load_string(trim($xml));
148         if($o->status == 'listing_ok') {
149             $tree = $o->tree->folder;
150             $this->buildtree($tree, $ret);
151         }
152         return $ret;
153     }
155     function buildtree($sax, &$tree){
156         $sax = (array)$sax;
157         $count = 0;
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()),
164                         'thumbnail'=>'http://www.box.net/img/small_folder_icon.gif',
165                         'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
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/'
182                             .$this->auth_token.'/'.(string)$file->attributes()->id,
183                         'url'=>(string)$file->attributes()->shared_link);
184                     $tree[] = $tmp;
185                 }
186             }
187             $count++;
188         }
189     }
190     // Get the file list
191     function getAccountTree($params = array()) {
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';
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);
204         $entry_count = 0;
205         for ($i=0; $i<$tree_count; $i++) {
206             $a = $data[$i];
207             switch ($a['tag'])
208             {
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;
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'];
222                     @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
223                     @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
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                     }
229                     $entry_count++;
230                 }
231                 break;
232             }
233         }
234         return $ret_array;
235     }
237     // Create New Folder
238     function CreateFolder($new_folder_name, $params = array()) {
239         $params['auth_token'] =  $this->auth_token;
240         $params['api_key']    = $this->api_key;
241         $params['action']     = 'create_folder';
242         $params['name']       = $new_folder_name;
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         }
253         $ret_array = array();
254         $data = $this->makeRequest('action=create_folder', $params);
255         if ($this->_checkForError($data)) {
256             return false;
257         }
258         foreach ($data as $a) {
259             if (!empty($a['value']) {
260                 switch ($a['tag']) {
262                 case 'FOLDER_ID':
263                     $ret_array['folder_id'] = $a['value'];
264                     break;
266                 case 'FOLDER_NAME':
267                     $ret_array['folder_name'] = $a['value'];
268                     break;
270                 case 'FOLDER_TYPE_ID':
271                     $ret_array['folder_type_id'] = $a['value'];
272                     break;
274                 case 'SHARED':
275                     $ret_array['shared'] = $a['value'];
276                     break;
278                 case 'PASSWORD':
279                     $ret_array['password'] = $a['value'];
280                     break;
281                 }
282             } else {
283                 $ret_array[strtolower($a['tag'])] = null;
284             }
285         }
286         return $ret_array;
287     }
289     /** Upload a File
290     * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
291     */
292     function UploadFile ($params = array()) {
293         $params['auth_token'] = $this->auth_token;
294         // this param should be the full path of the file
295         $params['new_file1']  = $params['file'];
296         unset($params['file']);
297         $defaults = array(
298             'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
299             'share'     => 1, //Set to '1' by default. Set to '0' to make folder private.
300         );
301         foreach ($defaults as $key => $value) {
302             if (!array_key_exists($key, $params)) {
303                 $params[$key] = $value;
304             }
305         }
306         $ret_array = array();
307         $entry_count = 0;
308         $data = $this->makeRequest('upload', $params);
309         if ($this->_checkForError($data)) {
310             return false;
311         }
312         for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
313             $a = $data[$i];
314             switch ($a['tag']) {
315             case 'STATUS':
316                 $ret_array['status'] = $a['value'];
317                 break;
319             case 'FILE':
320                 if (is_array($a['attributes'])) {
321                     @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
322                     @$ret_array['id'][$i] = $a['attributes']['ID'];
323                     @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
324                     @$ret_array['error'][$i] = $a['attributes']['ERROR'];
325                     @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
326                     $entry_count++;
327                 }
328                 break;
329             }
330         }
332         return $ret_array;
333     }
335     function RenameFile($fileid, $newname) {
336         $params = array(
337             'api_key'    => $this->api_key,
338             'auth_token' => $this->auth_token,
339             'action'     => 'rename',
340             'target'     => 'file',
341             'target_id'  => $fileid,
342             'new_name'   => $newname,
343         );
344         $data = $this->makeRequest('action=rename', $params);
345         if ($this->_checkForError($data)) {
346             return false;
347         }
348         foreach ($data as $a) {
349             switch ($a['tag']) {
350                 case 'STATUS':
351                     if ($a['value'] == 's_rename_node') {
352                         return true;
353                     }
354             }
355         }
356         return false;
357     }
359     // Register New User
360     function RegisterUser($params = array()) {
361         $params['api_key'] = $this->api_key;
362         $params['action']  = 'register_new_user';
363         $params['login']   = $_REQUEST['login'];
364         $params['password'] = $_REQUEST['password'];
365         $ret_array = array();
366         $data = $this->makeRequest('action=register_new_user', $params);
367         if ($this->_checkForError($data)) {
368             return false;
369         }
370         foreach ($data as $a) {
371             switch ($a['tag']) {
372             case 'STATUS':
373                 $ret_array['status'] = $a['value'];
374                 break;
376             case 'AUTH_TOKEN':
377                 $ret_array['auth_token'] = $a['value'];
378                 break;
380             case 'LOGIN':
381                 $ret_array['login'] = $a['value'];
382                 break;
383             case 'SPACE_AMOUNT':
384                 $ret_array['space_amount'] = $a['value'];
385                 break;
386             case 'SPACE_USED':
387                 $ret_array['space_used'] = $a['value'];
388                 break;
389             }
390         }
392         return $ret_array;
393     }
395     // Add Tags  (http://enabled.box.net/docs/rest#add_to_tag)
397     function AddTag($tag, $id, $target_type, $params = array()) {
398         $params['auth_token'] = $this->auth_token;
399         $params['api_key']    = $this->api_key;
400         $params['action']     = 'add_to_tag';
401         $params['target']     = $target_type; // File or folder
402         $params['target_id']  = $id; // Set to ID of file or folder
403         $params['tags[]']     = $tag;
404         $ret_array = array();
405         $data = $this->makeRequest('action=add_to_tag', $params);
406         if ($this->_checkForError($data)) {
407             return false;
408         }
409         foreach ($data as $a) {
410             switch ($a['tag']) {
411             case 'STATUS':
412                 $ret_array['status'] = $a['value'];
414                 break;
415             }
416         }
417         return $ret_array;
418     }
420     // Public Share  (http://enabled.box.net/docs/rest#public_share)
421     function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
422         $params['auth_token'] = $this->auth_token;
423         $params['api_key']    = $this->api_key;
424         $params['action']     = 'public_share';
425         $params['target']     = $target_type;
426         $params['target_id']  = $id;
427         $params['password']   =  $password;
428         $params['message']    = $message;
429         $params['emails']     = $emails;
430         $ret_array = array();
431         $data = $this->makeRequest('action=public_share', $params);
432         if ($this->_checkForError($data)) {
433             return false;
434         }
435         foreach ($data as $a) {
436             switch ($a['tag']) {
437             case 'STATUS':
438                 $ret_array['status'] = $a['value'];
439                 break;
440             case 'PUBLIC_NAME':
441                 $ret_array['public_name'] = $a['value'];
442                 break;
443             }
444         }
446         return $ret_array;
447     }
448     // Get Friends  (http://enabled.box.net/docs/rest#get_friends)
449     function GetFriends ($params = array()) {
450         $params['auth_token'] = $this->auth_token;
451         $params['action']     = 'get_friends';
452         $params['api_key']    = $this->api_key;
453         $params['params[]']   = 'nozip';
454         $ret_array = array();
455         $data = $this->makeRequest('action=get_friends', $params);
456         if ($this->_checkForError($data)) {
457             return false;
458         }
459         foreach ($data as $a) {
460             switch ($a['tag']) {
461             case 'NAME':
462                 $ret_array['name'] = $a['value'];
463                 break;
464             case 'EMAIL':
465                 $ret_array['email'] = $a['value'];
466                 break;
467             case 'ACCEPTED':
468                 $ret_array['accepted'] = $a['value'];
469                 break;
470             case 'AVATAR_URL':
471                 $ret_array['avatar_url'] = $a['value'];
472                 break;
473             case 'ID':
474                 $ret_array['id'] = $a['value'];
475                 break;
476             case 'URL':
477                 $ret_array['url'] = $a['value'];
478                 break;
479             case 'STATUS':
480                 $ret_array['status'] = $a['value'];
481                 break;
482             }
483         }
484         return $ret_array;
485     }
487     // Logout User
488     function Logout($params = array()) {
489         $params['auth_token'] = $this->auth_token;
490         $params['api_key']    = $this->api_key;
491         $params['action']     = 'logout';
492         $ret_array = array();
493         $data = $this->makeRequest('action=logout', $params);
494         if ($this->_checkForError($data)) {
495             return false;
496         }
497         foreach ($data as $a) {
498             switch ($a['tag']) {
499             case 'ACTION':
500                 $ret_array['logout'] = $a['value'];
502                 break;
503             }
504             return $ret_array;
505         }
506     }
507     function _checkForError($data) {
508         if ($this->_error_msg != '') {
509             return true;
510         }
511         if (@$data[0]['attributes']['STAT'] == 'fail') {
512             $this->_error_code = $data[1]['attributes']['CODE'];
513             $this->_error_msg = $data[1]['attributes']['MSG'];
514             return true;
515         }
516         return false;
517     }
519     public function isError() {
520         if  ($this->_error_msg != '') {
521             return true;
522         }
523         return false;
524     }
525     public function setError($code = 0, $msg){
526         $this->_error_code = $code;
527         $this->_error_msg  = $msg;
528     }
530     function getErrorMsg() {
531         return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
532     }
534     function getErrorCode() {
535         return $this->_error_code;
536     }
538     function _clearErrors() {
539         $this->_error_code = '';
540         $this->_error_msg = '';
541     }
544 ?>