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 = ''; |
aa754fe3 |
27 | |
a9493cbe |
28 | public function __construct($api_key, $auth_token = '') { |
d710e100 |
29 | $this->api_key = $api_key; |
a9493cbe |
30 | $this->auth_token = $auth_token; |
d710e100 |
31 | } |
32 | // Setup for Functions |
33 | function makeRequest($method, $params = array()) { |
34 | $this->_clearErrors(); |
a9493cbe |
35 | $c = new curl(array('cache'=>true)); |
d710e100 |
36 | if ($method == 'upload'){ |
a9493cbe |
37 | $request = $this->_box_api_upload_url.'/'. |
38 | $this->auth_token.'/'.$params['folder_id']; |
a9493cbe |
39 | $xml = $c->post($request, params); |
d710e100 |
40 | }else{ |
41 | $args = array(); |
34f210f6 |
42 | $xml = $c->get($this->_box_api_url, $params); |
d710e100 |
43 | } |
d710e100 |
44 | $xml_parser = xml_parser_create(); |
45 | xml_parse_into_struct($xml_parser, $xml, $data); |
46 | xml_parser_free($xml_parser); |
47 | return $data; |
48 | } |
49 | function getTicket($params = array()) { |
34f210f6 |
50 | $params['api_key'] = $this->api_key; |
51 | $params['action'] = 'get_ticket'; |
d710e100 |
52 | $ret_array = array(); |
53 | $data = $this->makeRequest('action=get_ticket', $params); |
54 | if ($this->_checkForError($data)) { |
55 | return false; |
56 | } |
57 | foreach ($data as $a) { |
58 | switch ($a['tag']) { |
a9493cbe |
59 | case 'STATUS': |
60 | $ret_array['status'] = $a['value']; |
61 | break; |
62 | case 'TICKET': |
63 | $ret_array['ticket'] = $a['value']; |
64 | break; |
d710e100 |
65 | } |
66 | } |
d710e100 |
67 | return $ret_array; |
68 | } |
aa754fe3 |
69 | |
34f210f6 |
70 | // $options['username'] and $options['password'] must be |
71 | // given, we will use them to obtain a valid auth_token |
72 | // To get a token, you should use following code: |
73 | // |
74 | // $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4'); |
75 | // Get a ticket |
76 | // $t = $box->getTicket(); |
77 | // $box->getAuthToken($t['ticket'], array( |
78 | // 'username'=>'dongsheng@moodle.com', |
79 | // 'password'=>'xxx')); |
80 | // |
bb2c046d |
81 | function getAuthToken($ticket, $username, $password) { |
34f210f6 |
82 | $c = new curl; |
83 | $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0)); |
84 | $param = array( |
85 | 'login_form1'=>'', |
bb2c046d |
86 | 'login'=>$username, |
87 | 'password'=>$password, |
34f210f6 |
88 | 'dologin'=>1, |
89 | '__login'=>1 |
90 | ); |
91 | $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param); |
92 | $header = $c->getResponse(); |
93 | $location = $header['location']; |
94 | preg_match('#auth_token=(.*)$#i', $location, $matches); |
95 | $auth_token = $matches[1]; |
96 | if(!empty($auth_token)) { |
97 | $this->auth_token = $auth_token; |
0eb58cf4 |
98 | return $auth_token; |
34f210f6 |
99 | } else { |
0eb58cf4 |
100 | return false; |
aa754fe3 |
101 | } |
d710e100 |
102 | } |
aa754fe3 |
103 | |
34f210f6 |
104 | // Get the file list |
d710e100 |
105 | function getAccountTree($params = array()) { |
34f210f6 |
106 | $params['auth_token'] = $this->auth_token; |
107 | $params['folder_id'] = 0; |
108 | $params['api_key'] = $this->api_key; |
109 | $params['action'] = 'get_account_tree'; |
110 | $params['onelevel'] = 1; |
111 | $params['params[]'] = 'nozip'; |
d710e100 |
112 | $ret_array = array(); |
113 | $data = $this->makeRequest('action=get_account_tree', $params); |
114 | if ($this->_checkForError($data)) { |
115 | return false; |
116 | } |
117 | $tree_count=count($data); |
118 | global $tree_count; |
119 | $entry_count = 0; |
120 | for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) { |
121 | $a = $data[$i]; |
aa754fe3 |
122 | switch ($a['tag']) |
d710e100 |
123 | { |
a9493cbe |
124 | case 'FOLDER': |
125 | if (@is_array($a['attributes'])) { |
126 | $ret_array['folder_id'][$i] = $a['attributes']['ID']; |
127 | $ret_array['folder_name'][$i] = $a['attributes']['NAME']; |
128 | $ret_array['shared'][$i] = $a['attributes']['SHARED']; |
129 | } |
130 | break; |
131 | |
132 | case 'FILE': |
133 | if (@is_array($a['attributes'])) { |
134 | $ret_array['file_id'][$i] = $a['attributes']['ID']; |
135 | @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME']; |
136 | @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD']; |
137 | $entry_count++; |
138 | } |
139 | break; |
d710e100 |
140 | } |
141 | } |
d710e100 |
142 | return $ret_array; |
143 | } |
34f210f6 |
144 | |
a9493cbe |
145 | // Create New Folder |
146 | function CreateFolder($new_folder_name, $params = array()) { |
34f210f6 |
147 | $params['auth_token'] = $this->auth_token; |
a9493cbe |
148 | $params['api_key'] = $this->api_key; |
34f210f6 |
149 | $params['action'] = 'create_folder'; |
150 | //Set to '0' by default. Change to create within sub-folder. |
151 | $params['parent_id'] = 0; |
a9493cbe |
152 | $params['name'] = $new_folder_name; |
34f210f6 |
153 | //Set to '1' by default. Set to '0' to make folder private. |
154 | $params['share'] = 1; |
aa754fe3 |
155 | |
a9493cbe |
156 | $ret_array = array(); |
157 | $data = $this->makeRequest('action=create_folder', $params); |
aa754fe3 |
158 | |
159 | |
a9493cbe |
160 | if ($this->_checkForError($data)) { |
161 | return false; |
162 | } |
163 | foreach ($data as $a) { |
164 | switch ($a['tag']) { |
165 | case 'FOLDER_ID': |
166 | $ret_array['folder_id'] = $a['value']; |
167 | break; |
168 | |
169 | case 'FOLDER_NAME': |
170 | $ret_array['folder_type'] = $a['value']; |
171 | break; |
172 | |
173 | case 'SHARED': |
174 | $ret_array['shared'] = $a['value']; |
175 | break; |
176 | case 'PASSWORD': |
177 | $ret_array['password'] = $a['value']; |
178 | break; |
d710e100 |
179 | } |
a9493cbe |
180 | } |
181 | return $ret_array; |
aa754fe3 |
182 | } |
a9493cbe |
183 | // Upload File |
184 | function UploadFile ($params = array()) { |
185 | $params['auth_token'] = $this->auth_token; |
186 | // this param should be the full path of the file |
187 | $params['new_file1'] = '@'.$params['file']; |
188 | $params['folder_id'] = 0; //Set to '0' by default. Change to create within sub-folder. |
189 | $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private. |
190 | $ret_array = array(); |
191 | $data = $this->makeRequest('upload', $params); |
192 | if ($this->_checkForError($data)) { |
193 | return false; |
194 | } |
195 | for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) { |
196 | $a = $data[$i]; |
197 | switch ($a['tag']) { |
198 | case 'STATUS': |
199 | $ret_array['status'] = $a['value']; |
200 | break; |
201 | |
202 | case 'FILE': |
203 | if (is_array($a['attributes'])) { |
204 | $ret_array['file_name'][$i] = $a['attributes']['FILE_NAME']; |
205 | $ret_array['id'][$i] = $a['attributes']['ID']; |
206 | $ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME']; |
207 | $ret_array['error'][$i] = $a['attributes']['ERROR']; |
208 | $ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME']; |
209 | $entry_count++; |
210 | } |
211 | break; |
d710e100 |
212 | } |
213 | } |
aa754fe3 |
214 | |
a9493cbe |
215 | return $ret_array; |
aa754fe3 |
216 | } |
aa754fe3 |
217 | |
a9493cbe |
218 | // Register New User |
219 | function RegisterUser($params = array()) { |
34f210f6 |
220 | $params['api_key'] = $this->api_key; |
221 | $params['action'] = 'register_new_user'; |
222 | $params['login'] = $_REQUEST['login']; |
223 | $params['password'] = $_REQUEST['password']; |
a9493cbe |
224 | $ret_array = array(); |
225 | $data = $this->makeRequest('action=register_new_user', $params); |
226 | if ($this->_checkForError($data)) { |
227 | return false; |
228 | } |
229 | foreach ($data as $a) { |
230 | switch ($a['tag']) { |
231 | case 'STATUS': |
232 | $ret_array['status'] = $a['value']; |
233 | break; |
234 | |
235 | case 'AUTH_TOKEN': |
236 | $ret_array['auth_token'] = $a['value']; |
237 | break; |
238 | |
239 | case 'LOGIN': |
240 | $ret_array['login'] = $a['value']; |
241 | break; |
242 | case 'SPACE_AMOUNT': |
243 | $ret_array['space_amount'] = $a['value']; |
244 | break; |
245 | case 'SPACE_USED': |
246 | $ret_array['space_used'] = $a['value']; |
247 | break; |
d710e100 |
248 | } |
249 | } |
aa754fe3 |
250 | |
a9493cbe |
251 | return $ret_array; |
aa754fe3 |
252 | } |
253 | |
a9493cbe |
254 | // Add Tags (http://enabled.box.net/docs/rest#add_to_tag) |
aa754fe3 |
255 | |
d710e100 |
256 | function AddTag($tag, $id, $target_type, $params = array()) { |
34f210f6 |
257 | $params['auth_token'] = $this->auth_token; |
258 | $params['api_key'] = $this->api_key; |
259 | $params['action'] = 'add_to_tag'; |
260 | $params['target'] = $target_type; // File or folder |
261 | $params['target_id'] = $id; // Set to ID of file or folder |
d710e100 |
262 | $params['tags[]'] = $tag; |
263 | $ret_array = array(); |
264 | $data = $this->makeRequest('action=add_to_tag', $params); |
265 | if ($this->_checkForError($data)) { |
266 | return false; |
267 | } |
268 | foreach ($data as $a) { |
269 | switch ($a['tag']) { |
a9493cbe |
270 | case 'STATUS': |
271 | $ret_array['status'] = $a['value']; |
aa754fe3 |
272 | |
a9493cbe |
273 | break; |
d710e100 |
274 | } |
275 | } |
d710e100 |
276 | return $ret_array; |
d710e100 |
277 | } |
aa754fe3 |
278 | |
d710e100 |
279 | // Public Share (http://enabled.box.net/docs/rest#public_share) |
d710e100 |
280 | function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) { |
34f210f6 |
281 | $params['auth_token'] = $this->auth_token; |
282 | $params['api_key'] = $this->api_key; |
283 | $params['action'] = 'public_share'; |
284 | $params['target'] = $target_type; |
285 | $params['target_id'] = $id; |
286 | $params['password'] = $password; |
d710e100 |
287 | $params['message'] = $message; |
34f210f6 |
288 | $params['emails'] = $emails; |
d710e100 |
289 | $ret_array = array(); |
290 | $data = $this->makeRequest('action=public_share', $params); |
291 | if ($this->_checkForError($data)) { |
292 | return false; |
293 | } |
294 | foreach ($data as $a) { |
295 | switch ($a['tag']) { |
a9493cbe |
296 | case 'STATUS': |
297 | $ret_array['status'] = $a['value']; |
298 | break; |
299 | case 'PUBLIC_NAME': |
300 | $ret_array['public_name'] = $a['value']; |
301 | break; |
d710e100 |
302 | } |
303 | } |
aa754fe3 |
304 | |
d710e100 |
305 | return $ret_array; |
306 | } |
d710e100 |
307 | // Get Friends (http://enabled.box.net/docs/rest#get_friends) |
a9493cbe |
308 | function GetFriends ($params = array()) { |
34f210f6 |
309 | $params['auth_token'] = $this->auth_token; |
310 | $params['action'] = 'get_friends'; |
311 | $params['api_key'] = $this->api_key; |
312 | $params['params[]'] = 'nozip'; |
d710e100 |
313 | $ret_array = array(); |
314 | $data = $this->makeRequest('action=get_friends', $params); |
315 | if ($this->_checkForError($data)) { |
316 | return false; |
317 | } |
318 | foreach ($data as $a) { |
319 | switch ($a['tag']) { |
a9493cbe |
320 | case 'NAME': |
321 | $ret_array['name'] = $a['value']; |
322 | break; |
323 | case 'EMAIL': |
324 | $ret_array['email'] = $a['value']; |
325 | break; |
326 | case 'ACCEPTED': |
327 | $ret_array['accepted'] = $a['value']; |
328 | break; |
329 | case 'AVATAR_URL': |
330 | $ret_array['avatar_url'] = $a['value']; |
331 | break; |
332 | case 'ID': |
333 | $ret_array['id'] = $a['value']; |
334 | break; |
335 | case 'URL': |
336 | $ret_array['url'] = $a['value']; |
337 | break; |
338 | case 'STATUS': |
339 | $ret_array['status'] = $a['value']; |
340 | break; |
d710e100 |
341 | } |
342 | } |
d710e100 |
343 | return $ret_array; |
344 | } |
aa754fe3 |
345 | |
d710e100 |
346 | // Logout User |
a9493cbe |
347 | function Logout($params = array()) { |
34f210f6 |
348 | $params['auth_token'] = $this->auth_token; |
349 | $params['api_key'] = $this->api_key; |
350 | $params['action'] = 'logout'; |
a9493cbe |
351 | $ret_array = array(); |
352 | $data = $this->makeRequest('action=logout', $params); |
353 | if ($this->_checkForError($data)) { |
354 | return false; |
355 | } |
356 | foreach ($data as $a) { |
357 | switch ($a['tag']) { |
358 | case 'ACTION': |
359 | $ret_array['logout'] = $a['value']; |
aa754fe3 |
360 | |
a9493cbe |
361 | break; |
aa754fe3 |
362 | } |
d710e100 |
363 | return $ret_array; |
364 | } |
365 | } |
a9493cbe |
366 | function _checkForError($data) { |
367 | if (@$data[0]['attributes']['STAT'] == 'fail') { |
368 | $this->_error_code = $data[1]['attributes']['CODE']; |
369 | $this->_error_msg = $data[1]['attributes']['MSG']; |
370 | return true; |
371 | } |
372 | return false; |
aa754fe3 |
373 | } |
aa754fe3 |
374 | |
a9493cbe |
375 | public function isError() { |
376 | if ($this->_error_msg != '') { |
377 | return true; |
378 | } |
379 | return false; |
aa754fe3 |
380 | } |
aa754fe3 |
381 | |
a9493cbe |
382 | function getErrorMsg() { |
383 | return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>'; |
384 | } |
aa754fe3 |
385 | |
a9493cbe |
386 | function getErrorCode() { |
387 | return $this->_error_code; |
388 | } |
aa754fe3 |
389 | |
a9493cbe |
390 | function _clearErrors() { |
391 | $this->_error_code = ''; |
392 | $this->_error_msg = ''; |
393 | } |
aa754fe3 |
394 | |
d710e100 |
395 | } |
396 | ?> |