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']); |
a9493cbe |
162 | $entry_count++; |
163 | } |
164 | break; |
d710e100 |
165 | } |
166 | } |
d710e100 |
167 | return $ret_array; |
168 | } |
34f210f6 |
169 | |
a9493cbe |
170 | // Create New Folder |
171 | function CreateFolder($new_folder_name, $params = array()) { |
34f210f6 |
172 | $params['auth_token'] = $this->auth_token; |
a9493cbe |
173 | $params['api_key'] = $this->api_key; |
34f210f6 |
174 | $params['action'] = 'create_folder'; |
175 | //Set to '0' by default. Change to create within sub-folder. |
176 | $params['parent_id'] = 0; |
a9493cbe |
177 | $params['name'] = $new_folder_name; |
34f210f6 |
178 | //Set to '1' by default. Set to '0' to make folder private. |
179 | $params['share'] = 1; |
aa754fe3 |
180 | |
a9493cbe |
181 | $ret_array = array(); |
182 | $data = $this->makeRequest('action=create_folder', $params); |
a9493cbe |
183 | if ($this->_checkForError($data)) { |
184 | return false; |
185 | } |
186 | foreach ($data as $a) { |
187 | switch ($a['tag']) { |
188 | case 'FOLDER_ID': |
189 | $ret_array['folder_id'] = $a['value']; |
190 | break; |
191 | |
192 | case 'FOLDER_NAME': |
193 | $ret_array['folder_type'] = $a['value']; |
194 | break; |
195 | |
196 | case 'SHARED': |
197 | $ret_array['shared'] = $a['value']; |
198 | break; |
199 | case 'PASSWORD': |
200 | $ret_array['password'] = $a['value']; |
201 | break; |
d710e100 |
202 | } |
a9493cbe |
203 | } |
204 | return $ret_array; |
aa754fe3 |
205 | } |
a9493cbe |
206 | // Upload File |
207 | function UploadFile ($params = array()) { |
208 | $params['auth_token'] = $this->auth_token; |
209 | // this param should be the full path of the file |
210 | $params['new_file1'] = '@'.$params['file']; |
b3fac92f |
211 | $defaults = array( |
212 | 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder. |
213 | 'share' => 1, //Set to '1' by default. Set to '0' to make folder private. |
214 | ); |
215 | foreach ($defaults as $key => $value) { |
216 | if (!array_key_exists($key, $params)) { |
217 | $params[$key] = $value; |
218 | } |
219 | } |
a9493cbe |
220 | $ret_array = array(); |
1afc87ad |
221 | $entry_count = 0; |
a9493cbe |
222 | $data = $this->makeRequest('upload', $params); |
223 | if ($this->_checkForError($data)) { |
224 | return false; |
225 | } |
226 | for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) { |
227 | $a = $data[$i]; |
228 | switch ($a['tag']) { |
229 | case 'STATUS': |
230 | $ret_array['status'] = $a['value']; |
231 | break; |
232 | |
233 | case 'FILE': |
234 | if (is_array($a['attributes'])) { |
1afc87ad |
235 | @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME']; |
236 | @$ret_array['id'][$i] = $a['attributes']['ID']; |
237 | @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME']; |
238 | @$ret_array['error'][$i] = $a['attributes']['ERROR']; |
239 | @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME']; |
a9493cbe |
240 | $entry_count++; |
241 | } |
242 | break; |
d710e100 |
243 | } |
244 | } |
aa754fe3 |
245 | |
a9493cbe |
246 | return $ret_array; |
aa754fe3 |
247 | } |
aa754fe3 |
248 | |
a9493cbe |
249 | // Register New User |
250 | function RegisterUser($params = array()) { |
34f210f6 |
251 | $params['api_key'] = $this->api_key; |
252 | $params['action'] = 'register_new_user'; |
253 | $params['login'] = $_REQUEST['login']; |
254 | $params['password'] = $_REQUEST['password']; |
a9493cbe |
255 | $ret_array = array(); |
256 | $data = $this->makeRequest('action=register_new_user', $params); |
257 | if ($this->_checkForError($data)) { |
258 | return false; |
259 | } |
260 | foreach ($data as $a) { |
261 | switch ($a['tag']) { |
262 | case 'STATUS': |
263 | $ret_array['status'] = $a['value']; |
264 | break; |
265 | |
266 | case 'AUTH_TOKEN': |
267 | $ret_array['auth_token'] = $a['value']; |
268 | break; |
269 | |
270 | case 'LOGIN': |
271 | $ret_array['login'] = $a['value']; |
272 | break; |
273 | case 'SPACE_AMOUNT': |
274 | $ret_array['space_amount'] = $a['value']; |
275 | break; |
276 | case 'SPACE_USED': |
277 | $ret_array['space_used'] = $a['value']; |
278 | break; |
d710e100 |
279 | } |
280 | } |
aa754fe3 |
281 | |
a9493cbe |
282 | return $ret_array; |
aa754fe3 |
283 | } |
284 | |
a9493cbe |
285 | // Add Tags (http://enabled.box.net/docs/rest#add_to_tag) |
aa754fe3 |
286 | |
d710e100 |
287 | function AddTag($tag, $id, $target_type, $params = array()) { |
34f210f6 |
288 | $params['auth_token'] = $this->auth_token; |
289 | $params['api_key'] = $this->api_key; |
290 | $params['action'] = 'add_to_tag'; |
291 | $params['target'] = $target_type; // File or folder |
292 | $params['target_id'] = $id; // Set to ID of file or folder |
d710e100 |
293 | $params['tags[]'] = $tag; |
294 | $ret_array = array(); |
295 | $data = $this->makeRequest('action=add_to_tag', $params); |
296 | if ($this->_checkForError($data)) { |
297 | return false; |
298 | } |
299 | foreach ($data as $a) { |
300 | switch ($a['tag']) { |
a9493cbe |
301 | case 'STATUS': |
302 | $ret_array['status'] = $a['value']; |
aa754fe3 |
303 | |
a9493cbe |
304 | break; |
d710e100 |
305 | } |
306 | } |
d710e100 |
307 | return $ret_array; |
d710e100 |
308 | } |
aa754fe3 |
309 | |
d710e100 |
310 | // Public Share (http://enabled.box.net/docs/rest#public_share) |
d710e100 |
311 | function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) { |
34f210f6 |
312 | $params['auth_token'] = $this->auth_token; |
313 | $params['api_key'] = $this->api_key; |
314 | $params['action'] = 'public_share'; |
315 | $params['target'] = $target_type; |
316 | $params['target_id'] = $id; |
317 | $params['password'] = $password; |
d710e100 |
318 | $params['message'] = $message; |
34f210f6 |
319 | $params['emails'] = $emails; |
d710e100 |
320 | $ret_array = array(); |
321 | $data = $this->makeRequest('action=public_share', $params); |
322 | if ($this->_checkForError($data)) { |
323 | return false; |
324 | } |
325 | foreach ($data as $a) { |
326 | switch ($a['tag']) { |
a9493cbe |
327 | case 'STATUS': |
328 | $ret_array['status'] = $a['value']; |
329 | break; |
330 | case 'PUBLIC_NAME': |
331 | $ret_array['public_name'] = $a['value']; |
332 | break; |
d710e100 |
333 | } |
334 | } |
aa754fe3 |
335 | |
d710e100 |
336 | return $ret_array; |
337 | } |
d710e100 |
338 | // Get Friends (http://enabled.box.net/docs/rest#get_friends) |
a9493cbe |
339 | function GetFriends ($params = array()) { |
34f210f6 |
340 | $params['auth_token'] = $this->auth_token; |
341 | $params['action'] = 'get_friends'; |
342 | $params['api_key'] = $this->api_key; |
343 | $params['params[]'] = 'nozip'; |
d710e100 |
344 | $ret_array = array(); |
345 | $data = $this->makeRequest('action=get_friends', $params); |
346 | if ($this->_checkForError($data)) { |
347 | return false; |
348 | } |
349 | foreach ($data as $a) { |
350 | switch ($a['tag']) { |
a9493cbe |
351 | case 'NAME': |
352 | $ret_array['name'] = $a['value']; |
353 | break; |
354 | case 'EMAIL': |
355 | $ret_array['email'] = $a['value']; |
356 | break; |
357 | case 'ACCEPTED': |
358 | $ret_array['accepted'] = $a['value']; |
359 | break; |
360 | case 'AVATAR_URL': |
361 | $ret_array['avatar_url'] = $a['value']; |
362 | break; |
363 | case 'ID': |
364 | $ret_array['id'] = $a['value']; |
365 | break; |
366 | case 'URL': |
367 | $ret_array['url'] = $a['value']; |
368 | break; |
369 | case 'STATUS': |
370 | $ret_array['status'] = $a['value']; |
371 | break; |
d710e100 |
372 | } |
373 | } |
d710e100 |
374 | return $ret_array; |
375 | } |
aa754fe3 |
376 | |
d710e100 |
377 | // Logout User |
a9493cbe |
378 | function Logout($params = array()) { |
34f210f6 |
379 | $params['auth_token'] = $this->auth_token; |
380 | $params['api_key'] = $this->api_key; |
381 | $params['action'] = 'logout'; |
a9493cbe |
382 | $ret_array = array(); |
383 | $data = $this->makeRequest('action=logout', $params); |
384 | if ($this->_checkForError($data)) { |
385 | return false; |
386 | } |
387 | foreach ($data as $a) { |
388 | switch ($a['tag']) { |
389 | case 'ACTION': |
390 | $ret_array['logout'] = $a['value']; |
aa754fe3 |
391 | |
a9493cbe |
392 | break; |
aa754fe3 |
393 | } |
d710e100 |
394 | return $ret_array; |
395 | } |
396 | } |
a9493cbe |
397 | function _checkForError($data) { |
6135bd45 |
398 | if ($this->_error_msg != '') { |
399 | return true; |
400 | } |
a9493cbe |
401 | if (@$data[0]['attributes']['STAT'] == 'fail') { |
402 | $this->_error_code = $data[1]['attributes']['CODE']; |
403 | $this->_error_msg = $data[1]['attributes']['MSG']; |
404 | return true; |
405 | } |
406 | return false; |
aa754fe3 |
407 | } |
aa754fe3 |
408 | |
a9493cbe |
409 | public function isError() { |
410 | if ($this->_error_msg != '') { |
411 | return true; |
412 | } |
413 | return false; |
aa754fe3 |
414 | } |
6135bd45 |
415 | public function setError($code = 0, $msg){ |
416 | $this->_error_code = $code; |
417 | $this->_error_msg = $msg; |
418 | } |
aa754fe3 |
419 | |
a9493cbe |
420 | function getErrorMsg() { |
421 | return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>'; |
422 | } |
aa754fe3 |
423 | |
a9493cbe |
424 | function getErrorCode() { |
425 | return $this->_error_code; |
426 | } |
aa754fe3 |
427 | |
a9493cbe |
428 | function _clearErrors() { |
429 | $this->_error_code = ''; |
430 | $this->_error_msg = ''; |
431 | } |
aa754fe3 |
432 | |
d710e100 |
433 | } |
434 | ?> |