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