MDL-15350, Initial Revision of boxnet plugin
[moodle.git] / repository / boxnet / boxlibphp5.php
CommitLineData
d710e100 1<?php
2/**
3 * Box REST Client Library for PHP5 Developers
4 *
5 *
6 * @author James Levy <james@box.net>
7 * @link http://enabled.box.net
8 * @access public
9 * @version 1.0
10 * copyright Box.net 2007
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
20require_once 'class.curl.php';
21
22class boxclient {
23
24 var $_debug = false;
25 var $_box_api_url = 'http://www.box.net/api/1.0/rest';
26 var $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
27 var $_error_code = '';
28 var $_error_msg = '';
29 var $auth_token = '';
30
31 public function setAuth($str){
32 $this->auth_token = $str;
33
34 }
35
36 public function __construct($api_key, $auth_token) {
37 $this->api_key = $api_key;
38 $this->auth_token = $auth_token;
39 }
40 // Setup for Functions
41 function makeRequest($method, $params = array()) {
42 $this->_clearErrors();
43 $useCURL = in_array('curl', get_loaded_extensions());
44 if ($method == 'upload'){
45 $args = array();
46 foreach($params as $k => $v){
47 array_push($args, urlencode($v));
48 $query_str = implode('/', $args);
49 }
50 $request = $this->_box_api_upload_url .'/'. $query_str;
51 if ($this->_debug){
52 echo "Upload Request: ".$request;
53 }
54 }else{
55 $args = array();
56 foreach($params as $k => $v){
57 array_push($args, urlencode($k).'='.urlencode($v));
58 $query_str = implode('&', $args);
59 }
60 $request = $this->_box_api_url .'?'. $method . '&' . $query_str;
61 if ($this->_debug){ echo "Request: ".$request; }
62 }
63 if ($useCURL) {
64 $c = &new curl($request );
65 $c->setopt(CURLOPT_FOLLOWLOCATION, true);
66 $xml = $c->exec();
67 $error = $c->hasError();
68 if ($error) {
69 $this->_error_msg = $error;
70 return false;
71 }
72 $c->close() ;
73 } else {
74 $url_parsed = parse_url($request);
75 $host = $url_parsed["host"];
76 $port = ($url_parsed['port'] == 0) ? 80 : $url_parsed['port'];
77 $path = $url_parsed["path"] . (($url_parsed['query'] != '') ? $path .= "?{$url_parsed[query]}" : '');
78 $headers = "GET $path HTTP/1.0\r\n";
79 $headers .= "Host: $host\r\n\r\n";
80 $fp = fsockopen($host, $port, $errno, $errstr, 30);
81 if (!$fp) {
82 $this->_error_msg = $errstr;
83 $this->_error_code = $errno;
84 return false;
85 } else {
86 fwrite($fp, $headers);
87 while (!feof($fp)) {
88 $xml .= fgets($fp, 1024);
89 }
90 fclose($fp);
91
92
93 $xml_start = strpos($xml, '<?xml');
94 $xml = substr($xml, $xml_start, strlen($xml));
95 }
96 }
97
98 if ($this->_debug) {
99 echo '<h2>XML Response</h2>';
100 echo '<pre class="xml">';
101 echo htmlspecialchars($xml);
102 echo '</pre>';
103 }
104
105 $xml_parser = xml_parser_create();
106 xml_parse_into_struct($xml_parser, $xml, $data);
107 xml_parser_free($xml_parser);
108 return $data;
109 }
110 function getTicket($params = array()) {
111 $params['api_key'] = $this->api_key;
112 $ret_array = array();
113 $data = $this->makeRequest('action=get_ticket', $params);
114 if ($this->_checkForError($data)) {
115 return false;
116 }
117 foreach ($data as $a) {
118 switch ($a['tag']) {
119 case 'STATUS':
120 $ret_array['status'] = $a['value'];
121 break;
122 case 'TICKET':
123 $ret_array['ticket'] = $a['value'];
124 break;
125 }
126 }
127 if ($this->_debug) {
128 echo '<h2>Ticket Return</h2>';
129 $this->_a($ret_array);
130 var_dump ($a);
131 }
132 return $ret_array;
133 }
134 // Get Auth Token
135 function getAuthToken($ticket) {
136 $params['api_key'] = $this->api_key;
137 $params['ticket'] = $ticket;
138 $ret_array = array();
139 $data = $this->makeRequest('action=get_auth_token', $params);
140 if ($this->_checkForError($data)) {
141 return false;
142 }
143 foreach ($data as $a) {
144 switch ($a['tag'])
145 {
146 case 'STATUS':
147 $ret_array['status'] = $a['value'];
148 break;
149 case 'AUTH_TOKEN':
150 $ret_array['auth_token'] = $a['value'];
151 break;
152 }
153 }
154
155 if ($ret_array['status'] == 'get_auth_token_ok'){
156 $this->auth_token = $ret_array['auth_token'];
157 $auth_token = $ret_array['auth_token'];
158 global $auth_token;
159 }else{
160 echo '<a href="http://www.box.net/api/1.0/auth/'.$ticket.'">Login</a>';
161 //header ('location: http://www.box.net/api/1.0/auth/'.$ticket) ;
162 }
163 }
164
165 // Retrieve Account Tree (http://enabled.box.net/docs/rest#get_account_tree)
166 function getAccountTree($params = array()) {
167 $params['api_key'] = $this->api_key;
168 $params['auth_token'] = $this->auth_token;
169 $params['folder_id'] = 0;
170 $params['onelevel'] = 1;
171 $params['params[]'] = 'nozip';
172 $ret_array = array();
173 $data = $this->makeRequest('action=get_account_tree', $params);
174 if ($this->_checkForError($data)) {
175 return false;
176 }
177 $tree_count=count($data);
178 global $tree_count;
179 $entry_count = 0;
180 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
181 $a = $data[$i];
182 switch ($a['tag'])
183 {
184 case 'FOLDER':
185 if (@is_array($a['attributes'])) {
186 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
187 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
188 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
189 }
190 break;
191
192 case 'FILE':
193 if (@is_array($a['attributes'])) {
194 $ret_array['file_id'][$i] = $a['attributes']['ID'];
195 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
196 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
197 $entry_count++;
198 }
199 break;
200 }
201 }
202 if ($this->_debug) {
203 echo '<h2>Account Tree Return</h2>';
204 $this->_a($ret_array);
205 var_dump ($a);
206 }
207 return $ret_array;
208 }
209 // Create New Folder
210 function CreateFolder($new_folder_name, $params = array()) {
211 $params['api_key'] = $this->api_key;
212 $params['auth_token'] = $this->auth_token;
213 $params['parent_id'] = 0; //Set to '0' by default. Change to create within sub-folder.
214 $params['name'] = $new_folder_name;
215 $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
216
217 $ret_array = array();
218 $data = $this->makeRequest('action=create_folder', $params);
219
220
221 if ($this->_checkForError($data)) {
222 return false;
223 }
224 foreach ($data as $a) {
225 switch ($a['tag']) {
226 case 'FOLDER_ID':
227 $ret_array['folder_id'] = $a['value'];
228 break;
229
230 case 'FOLDER_NAME':
231 $ret_array['folder_type'] = $a['value'];
232 break;
233
234 case 'SHARED':
235 $ret_array['shared'] = $a['value'];
236 break;
237 case 'PASSWORD':
238 $ret_array['password'] = $a['value'];
239 break;
240 }
241 }
242 if ($this->_debug) {
243 echo '<h2>Account Tree Return</h2>';
244 $this->_a($ret_array);
245 var_dump ($a);
246 }
247 return $ret_array;
248 }
249 // Upload File
250 function UploadFile ($params = array()) {
251 $params['auth_token'] = $this->auth_token;
252 $params['new_file1'] = $_FILES['new_file1'];
253 $params['folder id'] = 0; //Set to '0' by default. Change to create within sub-folder.
254 $params['share'] = 1; //Set to '1' by default. Set to '0' to make folder private.
255 $ret_array = array();
256 $data = $this->makeUploadRequst($params);
257 if ($this->_checkForError($data)) {
258 return false;
259 }
260 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
261 $a = $data[$i];
262 switch ($a['tag']) {
263 case 'STATUS':
264 $ret_array['status'] = $a['value'];
265
266 break;
267
268 case 'FILE':
269 if (is_array($a['attributes'])) {
270 $ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
271 $ret_array['id'][$i] = $a['attributes']['ID'];
272 $ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
273 $ret_array['error'][$i] = $a['attributes']['ERROR'];
274 $ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
275 $entry_count++;
276 }
277 break;
278 }
279 }
280
281 if ($this->_debug) {
282 echo '<h2>Account Tree Return</h2>';
283 $this->_a($ret_array);
284 var_dump ($a);
285 }
286 return $ret_array;
287 }
288
289
290 // Set Description of File or Folder
291
292
293
294
295 // Register New User
296
297 function RegisterUser($params = array()) {
298
299 $params['api_key'] = $this->api_key;
300 $params['login'] = $_REQUEST['login'];
301 $params['password'] = $_REQUEST['password'];
302 $ret_array = array();
303 $data = $this->makeRequest('action=register_new_user', $params);
304 if ($this->_checkForError($data)) {
305 return false;
306 }
307 foreach ($data as $a) {
308 switch ($a['tag']) {
309 case 'STATUS':
310 $ret_array['status'] = $a['value'];
311 break;
312
313 case 'AUTH_TOKEN':
314 $ret_array['auth_token'] = $a['value'];
315 break;
316
317 case 'LOGIN':
318 $ret_array['login'] = $a['value'];
319 break;
320 case 'SPACE_AMOUNT':
321 $ret_array['space_amount'] = $a['value'];
322 break;
323 case 'SPACE_USED':
324 $ret_array['space_used'] = $a['value'];
325 break;
326 }
327 }
328
329 if ($this->_debug) {
330 echo '<h2>Registration Return</h2>';
331 $this->_a($ret_array);
332 var_dump ($a);
333 }
334
335 return $ret_array;
336 }
337
338 // Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
339
340 function AddTag($tag, $id, $target_type, $params = array()) {
341
342 $params['api_key'] = $this->api_key;
343 $params['auth_token'] = $this->auth_token;
344 $params['target'] = $target_type; // File or folder
345 $params['target_id'] = $id; // Set to ID of file or folder
346 $params['tags[]'] = $tag;
347 $ret_array = array();
348 $data = $this->makeRequest('action=add_to_tag', $params);
349 if ($this->_checkForError($data)) {
350 return false;
351 }
352 foreach ($data as $a) {
353 switch ($a['tag']) {
354 case 'STATUS':
355 $ret_array['status'] = $a['value'];
356
357 break;
358 }
359 }
360
361 if ($this->_debug) {
362 echo '<h2>Tag Return</h2>';
363 $this->_a($ret_array);
364 var_dump ($a);
365 }
366
367 return $ret_array;
368
369
370 }
371
372 // Public Share (http://enabled.box.net/docs/rest#public_share)
373
374 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
375 $params['api_key'] = $this->api_key;
376 $params['auth_token'] = $this->auth_token;
377 $params['target'] = $target_type; // File or folder
378 $params['target_id'] = $id; // Set to ID of file or folder
379 $params['password'] = $password; //optional
380 $params['message'] = $message;
381 $params['emails'] = $emails;
382 $ret_array = array();
383 $data = $this->makeRequest('action=public_share', $params);
384 if ($this->_checkForError($data)) {
385 return false;
386 }
387 foreach ($data as $a) {
388 switch ($a['tag']) {
389 case 'STATUS':
390 $ret_array['status'] = $a['value'];
391 break;
392 case 'PUBLIC_NAME':
393 $ret_array['public_name'] = $a['value'];
394 break;
395 }
396 }
397
398 if ($this->_debug) {
399 echo '<h2>Public Share Return</h2>';
400 $this->_a($ret_array);
401 var_dump ($a);
402 }
403 return $ret_array;
404 }
405
406
407 // Get Friends (http://enabled.box.net/docs/rest#get_friends)
408
409 function GetFriends ($params = array()) {
410
411 $params['api_key'] = $this->api_key;
412 $params['auth_token'] = $this->auth_token;
413 $params['params[]'] = 'nozip';
414 $ret_array = array();
415 $data = $this->makeRequest('action=get_friends', $params);
416 if ($this->_checkForError($data)) {
417 return false;
418 }
419 foreach ($data as $a) {
420 switch ($a['tag']) {
421 case 'NAME':
422 $ret_array['name'] = $a['value'];
423 break;
424 case 'EMAIL':
425 $ret_array['email'] = $a['value'];
426 break;
427 case 'ACCEPTED':
428 $ret_array['accepted'] = $a['value'];
429 break;
430 case 'AVATAR_URL':
431 $ret_array['avatar_url'] = $a['value'];
432 break;
433 case 'ID':
434 $ret_array['id'] = $a['value'];
435 break;
436 case 'URL':
437 $ret_array['url'] = $a['value'];
438 break;
439 case 'STATUS':
440 $ret_array['status'] = $a['value'];
441 break;
442 }
443 }
444 if ($this->_debug) {
445 echo '<h2>Get Friend Return</h2>';
446 $this->_a($ret_array);
447 var_dump ($a);
448 }
449 return $ret_array;
450 }
451
452
453 // Logout User
454
455 function Logout($params = array()) {
456
457 $params['api_key'] = $this->api_key;
458 $params['auth_token'] = $this->auth_token;
459 $ret_array = array();
460 $data = $this->makeRequest('action=logout', $params);
461 if ($this->_checkForError($data)) {
462 return false;
463 }
464 foreach ($data as $a) {
465 switch ($a['tag']) {
466 case 'ACTION':
467 $ret_array['logout'] = $a['value'];
468
469 break;
470 }
471 if ($this->_debug) {
472 echo '<h2>Logout Return</h2>';
473 $this->_a($ret_array);
474 var_dump ($a);
475 }
476 return $ret_array;
477 }
478 }
479 function _checkForError($data) {
480 if (@$data[0]['attributes']['STAT'] == 'fail') {
481 $this->_error_code = $data[1]['attributes']['CODE'];
482 $this->_error_msg = $data[1]['attributes']['MSG'];
483 return true;
484 }
485 return false;
486 }
487
488
489 public function isError() {
490 if ($this->_error_msg != '') {
491 return true;
492 }
493 return false;
494 }
495
496
497 function getErrorMsg() {
498 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
499 }
500
501 function getErrorCode() {
502 return $this->_error_code;
503 }
504
505
506 function _clearErrors() {
507 $this->_error_code = '';
508 $this->_error_msg = '';
509 }
510
511 public function setDebug($debug) {
512 $this->_debug = $debug;
513 }
514
515 private function _a($array) {
516 var_dump($array);
517 }
518}
519?>