d710e100 |
1 | <?php |
2 | /** |
3 | * Box REST Client Library for PHP5 Developers |
aa754fe3 |
4 | * |
5 | * |
d710e100 |
6 | * @author James Levy <james@box.net> |
7 | * @link http://enabled.box.net |
8 | * @access public |
9 | * @version 1.0 |
aa754fe3 |
10 | * copyright Box.net 2007 |
d710e100 |
11 | * Available for use and distribution under GPL-license |
12 | * Go to http://www.gnu.org/licenses/gpl-3.0.txt for full text |
13 | */ |
14 | |
15 | /** |
16 | * Modified by Dongsheng Cai <dongsheng@cvs.moodle.org> |
17 | * |
18 | */ |
19 | |
d710e100 |
20 | class boxclient { |
34f210f6 |
21 | public $auth_token = ''; |
d710e100 |
22 | |
34f210f6 |
23 | private $_box_api_url = 'http://www.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 = ''; |
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 | } |
aa754fe3 |
127 | |
34f210f6 |
128 | // Get the file list |
d710e100 |
129 | function getAccountTree($params = array()) { |
34f210f6 |
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'; |
d710e100 |
136 | $ret_array = array(); |
137 | $data = $this->makeRequest('action=get_account_tree', $params); |
138 | if ($this->_checkForError($data)) { |
139 | return false; |
140 | } |
141 | $tree_count=count($data); |
3570711a |
142 | $entry_count = 0; |
6135bd45 |
143 | for ($i=0; $i<$tree_count; $i++) { |
d710e100 |
144 | $a = $data[$i]; |
aa754fe3 |
145 | switch ($a['tag']) |
d710e100 |
146 | { |
a9493cbe |
147 | case 'FOLDER': |
148 | if (@is_array($a['attributes'])) { |
149 | $ret_array['folder_id'][$i] = $a['attributes']['ID']; |
150 | $ret_array['folder_name'][$i] = $a['attributes']['NAME']; |
151 | $ret_array['shared'][$i] = $a['attributes']['SHARED']; |
152 | } |
153 | break; |
154 | |
155 | case 'FILE': |
156 | if (@is_array($a['attributes'])) { |
157 | $ret_array['file_id'][$i] = $a['attributes']['ID']; |
158 | @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME']; |
159 | @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD']; |
284cf75f |
160 | @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']); |
161 | @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']); |
c145b657 |
162 | if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) { |
163 | @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL']; |
164 | } else { |
165 | @$ret_array['thumbnail'][$i] = 'http://www.box.net'.$a['attributes']['THUMBNAIL']; |
166 | } |
a9493cbe |
167 | $entry_count++; |
168 | } |
169 | break; |
d710e100 |
170 | } |
171 | } |
d710e100 |
172 | return $ret_array; |
173 | } |
34f210f6 |
174 | |
a9493cbe |
175 | // Create New Folder |
176 | function CreateFolder($new_folder_name, $params = array()) { |
34f210f6 |
177 | $params['auth_token'] = $this->auth_token; |
a9493cbe |
178 | $params['api_key'] = $this->api_key; |
34f210f6 |
179 | $params['action'] = 'create_folder'; |
180 | //Set to '0' by default. Change to create within sub-folder. |
181 | $params['parent_id'] = 0; |
a9493cbe |
182 | $params['name'] = $new_folder_name; |
34f210f6 |
183 | //Set to '1' by default. Set to '0' to make folder private. |
184 | $params['share'] = 1; |
aa754fe3 |
185 | |
a9493cbe |
186 | $ret_array = array(); |
187 | $data = $this->makeRequest('action=create_folder', $params); |
a9493cbe |
188 | if ($this->_checkForError($data)) { |
189 | return false; |
190 | } |
191 | foreach ($data as $a) { |
192 | switch ($a['tag']) { |
193 | case 'FOLDER_ID': |
194 | $ret_array['folder_id'] = $a['value']; |
195 | break; |
196 | |
197 | case 'FOLDER_NAME': |
198 | $ret_array['folder_type'] = $a['value']; |
199 | break; |
200 | |
201 | case 'SHARED': |
202 | $ret_array['shared'] = $a['value']; |
203 | break; |
204 | case 'PASSWORD': |
205 | $ret_array['password'] = $a['value']; |
206 | break; |
d710e100 |
207 | } |
a9493cbe |
208 | } |
209 | return $ret_array; |
aa754fe3 |
210 | } |
db79c1b9 |
211 | |
212 | /** Upload a File |
213 | * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object. |
214 | */ |
a9493cbe |
215 | function UploadFile ($params = array()) { |
216 | $params['auth_token'] = $this->auth_token; |
217 | // this param should be the full path of the file |
db79c1b9 |
218 | $params['new_file1'] = $params['file']; |
219 | unset($params['file']); |
b3fac92f |
220 | $defaults = array( |
221 | 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder. |
222 | 'share' => 1, //Set to '1' by default. Set to '0' to make folder private. |
223 | ); |
224 | foreach ($defaults as $key => $value) { |
225 | if (!array_key_exists($key, $params)) { |
226 | $params[$key] = $value; |
227 | } |
228 | } |
a9493cbe |
229 | $ret_array = array(); |
1afc87ad |
230 | $entry_count = 0; |
a9493cbe |
231 | $data = $this->makeRequest('upload', $params); |
232 | if ($this->_checkForError($data)) { |
233 | return false; |
234 | } |
235 | for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) { |
236 | $a = $data[$i]; |
237 | switch ($a['tag']) { |
238 | case 'STATUS': |
239 | $ret_array['status'] = $a['value']; |
240 | break; |
241 | |
242 | case 'FILE': |
243 | if (is_array($a['attributes'])) { |
1afc87ad |
244 | @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME']; |
245 | @$ret_array['id'][$i] = $a['attributes']['ID']; |
246 | @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME']; |
247 | @$ret_array['error'][$i] = $a['attributes']['ERROR']; |
248 | @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME']; |
a9493cbe |
249 | $entry_count++; |
250 | } |
251 | break; |
d710e100 |
252 | } |
253 | } |
aa754fe3 |
254 | |
a9493cbe |
255 | return $ret_array; |
aa754fe3 |
256 | } |
aa754fe3 |
257 | |
db79c1b9 |
258 | function RenameFile($fileid, $newname) { |
259 | $params = array( |
260 | 'api_key' => $this->api_key, |
261 | 'auth_token' => $this->auth_token, |
262 | 'action' => 'rename', |
263 | 'target' => 'file', |
264 | 'target_id' => $fileid, |
265 | 'new_name' => $newname, |
266 | ); |
267 | $data = $this->makeRequest('action=rename', $params); |
268 | if ($this->_checkForError($data)) { |
269 | return false; |
270 | } |
271 | foreach ($data as $a) { |
272 | switch ($a['tag']) { |
273 | case 'STATUS': |
274 | if ($a['value'] == 'e_rename_node') { |
275 | return true; |
276 | } |
277 | } |
278 | } |
279 | return false; |
280 | } |
281 | |
a9493cbe |
282 | // Register New User |
283 | function RegisterUser($params = array()) { |
34f210f6 |
284 | $params['api_key'] = $this->api_key; |
285 | $params['action'] = 'register_new_user'; |
286 | $params['login'] = $_REQUEST['login']; |
287 | $params['password'] = $_REQUEST['password']; |
a9493cbe |
288 | $ret_array = array(); |
289 | $data = $this->makeRequest('action=register_new_user', $params); |
290 | if ($this->_checkForError($data)) { |
291 | return false; |
292 | } |
293 | foreach ($data as $a) { |
294 | switch ($a['tag']) { |
295 | case 'STATUS': |
296 | $ret_array['status'] = $a['value']; |
297 | break; |
298 | |
299 | case 'AUTH_TOKEN': |
300 | $ret_array['auth_token'] = $a['value']; |
301 | break; |
302 | |
303 | case 'LOGIN': |
304 | $ret_array['login'] = $a['value']; |
305 | break; |
306 | case 'SPACE_AMOUNT': |
307 | $ret_array['space_amount'] = $a['value']; |
308 | break; |
309 | case 'SPACE_USED': |
310 | $ret_array['space_used'] = $a['value']; |
311 | break; |
d710e100 |
312 | } |
313 | } |
aa754fe3 |
314 | |
a9493cbe |
315 | return $ret_array; |
aa754fe3 |
316 | } |
317 | |
a9493cbe |
318 | // Add Tags (http://enabled.box.net/docs/rest#add_to_tag) |
aa754fe3 |
319 | |
d710e100 |
320 | function AddTag($tag, $id, $target_type, $params = array()) { |
34f210f6 |
321 | $params['auth_token'] = $this->auth_token; |
322 | $params['api_key'] = $this->api_key; |
323 | $params['action'] = 'add_to_tag'; |
324 | $params['target'] = $target_type; // File or folder |
325 | $params['target_id'] = $id; // Set to ID of file or folder |
d710e100 |
326 | $params['tags[]'] = $tag; |
327 | $ret_array = array(); |
328 | $data = $this->makeRequest('action=add_to_tag', $params); |
329 | if ($this->_checkForError($data)) { |
330 | return false; |
331 | } |
332 | foreach ($data as $a) { |
333 | switch ($a['tag']) { |
a9493cbe |
334 | case 'STATUS': |
335 | $ret_array['status'] = $a['value']; |
aa754fe3 |
336 | |
a9493cbe |
337 | break; |
d710e100 |
338 | } |
339 | } |
d710e100 |
340 | return $ret_array; |
d710e100 |
341 | } |
aa754fe3 |
342 | |
d710e100 |
343 | // Public Share (http://enabled.box.net/docs/rest#public_share) |
d710e100 |
344 | function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) { |
34f210f6 |
345 | $params['auth_token'] = $this->auth_token; |
346 | $params['api_key'] = $this->api_key; |
347 | $params['action'] = 'public_share'; |
348 | $params['target'] = $target_type; |
349 | $params['target_id'] = $id; |
350 | $params['password'] = $password; |
d710e100 |
351 | $params['message'] = $message; |
34f210f6 |
352 | $params['emails'] = $emails; |
d710e100 |
353 | $ret_array = array(); |
354 | $data = $this->makeRequest('action=public_share', $params); |
355 | if ($this->_checkForError($data)) { |
356 | return false; |
357 | } |
358 | foreach ($data as $a) { |
359 | switch ($a['tag']) { |
a9493cbe |
360 | case 'STATUS': |
361 | $ret_array['status'] = $a['value']; |
362 | break; |
363 | case 'PUBLIC_NAME': |
364 | $ret_array['public_name'] = $a['value']; |
365 | break; |
d710e100 |
366 | } |
367 | } |
aa754fe3 |
368 | |
d710e100 |
369 | return $ret_array; |
370 | } |
d710e100 |
371 | // Get Friends (http://enabled.box.net/docs/rest#get_friends) |
a9493cbe |
372 | function GetFriends ($params = array()) { |
34f210f6 |
373 | $params['auth_token'] = $this->auth_token; |
374 | $params['action'] = 'get_friends'; |
375 | $params['api_key'] = $this->api_key; |
376 | $params['params[]'] = 'nozip'; |
d710e100 |
377 | $ret_array = array(); |
378 | $data = $this->makeRequest('action=get_friends', $params); |
379 | if ($this->_checkForError($data)) { |
380 | return false; |
381 | } |
382 | foreach ($data as $a) { |
383 | switch ($a['tag']) { |
a9493cbe |
384 | case 'NAME': |
385 | $ret_array['name'] = $a['value']; |
386 | break; |
387 | case 'EMAIL': |
388 | $ret_array['email'] = $a['value']; |
389 | break; |
390 | case 'ACCEPTED': |
391 | $ret_array['accepted'] = $a['value']; |
392 | break; |
393 | case 'AVATAR_URL': |
394 | $ret_array['avatar_url'] = $a['value']; |
395 | break; |
396 | case 'ID': |
397 | $ret_array['id'] = $a['value']; |
398 | break; |
399 | case 'URL': |
400 | $ret_array['url'] = $a['value']; |
401 | break; |
402 | case 'STATUS': |
403 | $ret_array['status'] = $a['value']; |
404 | break; |
d710e100 |
405 | } |
406 | } |
d710e100 |
407 | return $ret_array; |
408 | } |
aa754fe3 |
409 | |
d710e100 |
410 | // Logout User |
a9493cbe |
411 | function Logout($params = array()) { |
34f210f6 |
412 | $params['auth_token'] = $this->auth_token; |
413 | $params['api_key'] = $this->api_key; |
414 | $params['action'] = 'logout'; |
a9493cbe |
415 | $ret_array = array(); |
416 | $data = $this->makeRequest('action=logout', $params); |
417 | if ($this->_checkForError($data)) { |
418 | return false; |
419 | } |
420 | foreach ($data as $a) { |
421 | switch ($a['tag']) { |
422 | case 'ACTION': |
423 | $ret_array['logout'] = $a['value']; |
aa754fe3 |
424 | |
a9493cbe |
425 | break; |
aa754fe3 |
426 | } |
d710e100 |
427 | return $ret_array; |
428 | } |
429 | } |
a9493cbe |
430 | function _checkForError($data) { |
6135bd45 |
431 | if ($this->_error_msg != '') { |
432 | return true; |
433 | } |
a9493cbe |
434 | if (@$data[0]['attributes']['STAT'] == 'fail') { |
435 | $this->_error_code = $data[1]['attributes']['CODE']; |
436 | $this->_error_msg = $data[1]['attributes']['MSG']; |
437 | return true; |
438 | } |
439 | return false; |
aa754fe3 |
440 | } |
aa754fe3 |
441 | |
a9493cbe |
442 | public function isError() { |
443 | if ($this->_error_msg != '') { |
444 | return true; |
445 | } |
446 | return false; |
aa754fe3 |
447 | } |
6135bd45 |
448 | public function setError($code = 0, $msg){ |
449 | $this->_error_code = $code; |
450 | $this->_error_msg = $msg; |
451 | } |
aa754fe3 |
452 | |
a9493cbe |
453 | function getErrorMsg() { |
454 | return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>'; |
455 | } |
aa754fe3 |
456 | |
a9493cbe |
457 | function getErrorCode() { |
458 | return $this->_error_code; |
459 | } |
aa754fe3 |
460 | |
a9493cbe |
461 | function _clearErrors() { |
462 | $this->_error_code = ''; |
463 | $this->_error_msg = ''; |
464 | } |
aa754fe3 |
465 | |
d710e100 |
466 | } |
467 | ?> |