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