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 |
20 | class 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) { |
9e5f5f9f |
259 | if (!empty($a['value']) { |
260 | switch ($a['tag']) { |
a9493cbe |
261 | |
9e5f5f9f |
262 | case 'FOLDER_ID': |
263 | $ret_array['folder_id'] = $a['value']; |
264 | break; |
615a2859 |
265 | |
9e5f5f9f |
266 | case 'FOLDER_NAME': |
267 | $ret_array['folder_name'] = $a['value']; |
268 | break; |
a9493cbe |
269 | |
9e5f5f9f |
270 | case 'FOLDER_TYPE_ID': |
271 | $ret_array['folder_type_id'] = $a['value']; |
272 | break; |
273 | |
274 | case 'SHARED': |
615a2859 |
275 | $ret_array['shared'] = $a['value']; |
9e5f5f9f |
276 | break; |
277 | |
278 | case 'PASSWORD': |
279 | $ret_array['password'] = $a['value']; |
280 | break; |
615a2859 |
281 | } |
9e5f5f9f |
282 | } else { |
283 | $ret_array[strtolower($a['tag'])] = null; |
d710e100 |
284 | } |
a9493cbe |
285 | } |
286 | return $ret_array; |
aa754fe3 |
287 | } |
db79c1b9 |
288 | |
289 | /** Upload a File |
290 | * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object. |
291 | */ |
a9493cbe |
292 | function UploadFile ($params = array()) { |
293 | $params['auth_token'] = $this->auth_token; |
294 | // this param should be the full path of the file |
db79c1b9 |
295 | $params['new_file1'] = $params['file']; |
296 | unset($params['file']); |
b3fac92f |
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 | } |
a9493cbe |
306 | $ret_array = array(); |
1afc87ad |
307 | $entry_count = 0; |
a9493cbe |
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; |
318 | |
319 | case 'FILE': |
320 | if (is_array($a['attributes'])) { |
1afc87ad |
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']; |
a9493cbe |
326 | $entry_count++; |
327 | } |
328 | break; |
d710e100 |
329 | } |
330 | } |
aa754fe3 |
331 | |
a9493cbe |
332 | return $ret_array; |
aa754fe3 |
333 | } |
aa754fe3 |
334 | |
db79c1b9 |
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': |
5ec4b697 |
351 | if ($a['value'] == 's_rename_node') { |
db79c1b9 |
352 | return true; |
353 | } |
354 | } |
355 | } |
356 | return false; |
357 | } |
358 | |
a9493cbe |
359 | // Register New User |
360 | function RegisterUser($params = array()) { |
34f210f6 |
361 | $params['api_key'] = $this->api_key; |
362 | $params['action'] = 'register_new_user'; |
363 | $params['login'] = $_REQUEST['login']; |
364 | $params['password'] = $_REQUEST['password']; |
a9493cbe |
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; |
375 | |
376 | case 'AUTH_TOKEN': |
377 | $ret_array['auth_token'] = $a['value']; |
378 | break; |
379 | |
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; |
d710e100 |
389 | } |
390 | } |
aa754fe3 |
391 | |
a9493cbe |
392 | return $ret_array; |
aa754fe3 |
393 | } |
394 | |
a9493cbe |
395 | // Add Tags (http://enabled.box.net/docs/rest#add_to_tag) |
aa754fe3 |
396 | |
d710e100 |
397 | function AddTag($tag, $id, $target_type, $params = array()) { |
34f210f6 |
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 |
d710e100 |
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']) { |
a9493cbe |
411 | case 'STATUS': |
412 | $ret_array['status'] = $a['value']; |
aa754fe3 |
413 | |
a9493cbe |
414 | break; |
d710e100 |
415 | } |
416 | } |
d710e100 |
417 | return $ret_array; |
d710e100 |
418 | } |
aa754fe3 |
419 | |
d710e100 |
420 | // Public Share (http://enabled.box.net/docs/rest#public_share) |
d710e100 |
421 | function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) { |
34f210f6 |
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; |
d710e100 |
428 | $params['message'] = $message; |
34f210f6 |
429 | $params['emails'] = $emails; |
d710e100 |
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']) { |
a9493cbe |
437 | case 'STATUS': |
438 | $ret_array['status'] = $a['value']; |
439 | break; |
440 | case 'PUBLIC_NAME': |
441 | $ret_array['public_name'] = $a['value']; |
442 | break; |
d710e100 |
443 | } |
444 | } |
aa754fe3 |
445 | |
d710e100 |
446 | return $ret_array; |
447 | } |
d710e100 |
448 | // Get Friends (http://enabled.box.net/docs/rest#get_friends) |
a9493cbe |
449 | function GetFriends ($params = array()) { |
34f210f6 |
450 | $params['auth_token'] = $this->auth_token; |
451 | $params['action'] = 'get_friends'; |
452 | $params['api_key'] = $this->api_key; |
453 | $params['params[]'] = 'nozip'; |
d710e100 |
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']) { |
a9493cbe |
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; |
d710e100 |
482 | } |
483 | } |
d710e100 |
484 | return $ret_array; |
485 | } |
aa754fe3 |
486 | |
d710e100 |
487 | // Logout User |
a9493cbe |
488 | function Logout($params = array()) { |
34f210f6 |
489 | $params['auth_token'] = $this->auth_token; |
490 | $params['api_key'] = $this->api_key; |
491 | $params['action'] = 'logout'; |
a9493cbe |
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']; |
aa754fe3 |
501 | |
a9493cbe |
502 | break; |
aa754fe3 |
503 | } |
d710e100 |
504 | return $ret_array; |
505 | } |
506 | } |
a9493cbe |
507 | function _checkForError($data) { |
6135bd45 |
508 | if ($this->_error_msg != '') { |
509 | return true; |
510 | } |
a9493cbe |
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; |
aa754fe3 |
517 | } |
aa754fe3 |
518 | |
a9493cbe |
519 | public function isError() { |
520 | if ($this->_error_msg != '') { |
521 | return true; |
522 | } |
523 | return false; |
aa754fe3 |
524 | } |
6135bd45 |
525 | public function setError($code = 0, $msg){ |
526 | $this->_error_code = $code; |
527 | $this->_error_msg = $msg; |
528 | } |
aa754fe3 |
529 | |
a9493cbe |
530 | function getErrorMsg() { |
531 | return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>'; |
532 | } |
aa754fe3 |
533 | |
a9493cbe |
534 | function getErrorCode() { |
535 | return $this->_error_code; |
536 | } |
aa754fe3 |
537 | |
a9493cbe |
538 | function _clearErrors() { |
539 | $this->_error_code = ''; |
540 | $this->_error_msg = ''; |
541 | } |
aa754fe3 |
542 | |
d710e100 |
543 | } |
544 | ?> |