MDL-20636 fix most of the remaining codechecker issues in mod/quiz and lib/questionli...
[moodle.git] / lib / boxlib.php
bloba647223209906a191ae364a23b43bc9a28224017
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Box REST Client Library for PHP5 Developers
21 * @package moodlecore
22 * @author James Levy <james@box.net>
23 * @link http://enabled.box.net
24 * @access public
25 * @version 1.0
26 * @copyright copyright Box.net 2007
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30 /**
31 * @package moodlecore
32 * @copyright copyright Box.net 2007
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class boxclient {
36 /** @var string */
37 public $auth_token = '';
38 /** @var string */
39 private $_box_api_url = 'http://box.net/api/1.0/rest';
40 private $_box_api_upload_url = 'http://upload.box.net/api/1.0/upload';
41 private $_error_code = '';
42 private $_error_msg = '';
43 /** @var bool */
44 private $debug = false;
46 /**
47 * @param string $api_key
48 * @param string $auth_token
49 * @param bool $debug
51 public function __construct($api_key, $auth_token = '', $debug = false) {
52 $this->api_key = $api_key;
53 $this->auth_token = $auth_token;
54 if (!empty($debug)) {
55 $this->debug = true;
56 } else {
57 $this->debug = false;
60 /**
61 * Setup for Functions
63 * @param string $method
64 * @param array $params
65 * @return array
67 function makeRequest($method, $params = array()) {
68 $this->_clearErrors();
69 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
70 try {
71 if ($method == 'upload'){
72 $request = $this->_box_api_upload_url.'/'.
73 $this->auth_token.'/'.$params['folder_id'];
74 $xml = $c->post($request, $params);
75 }else{
76 $args = array();
77 $xml = $c->get($this->_box_api_url, $params);
79 $xml_parser = xml_parser_create();
80 // set $data here
81 xml_parse_into_struct($xml_parser, $xml, $data);
82 xml_parser_free($xml_parser);
83 } catch (moodle_exception $e) {
84 $this->setError(0, 'connection time-out or invalid url');
85 return false;
87 return $data;
89 /**
90 * @param array $params
91 * @return array
93 function getTicket($params = array()) {
94 $params['api_key'] = $this->api_key;
95 $params['action'] = 'get_ticket';
96 $ret_array = array();
97 $data = $this->makeRequest('action=get_ticket', $params);
98 if ($this->_checkForError($data)) {
99 return false;
101 foreach ($data as $a) {
102 switch ($a['tag']) {
103 case 'STATUS':
104 $ret_array['status'] = $a['value'];
105 break;
106 case 'TICKET':
107 $ret_array['ticket'] = $a['value'];
108 break;
111 return $ret_array;
115 * $options['username'] and $options['password'] must be
116 * given, we will use them to obtain a valid auth_token
117 * To get a token, you should use following code:
119 * <code>
120 * $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
121 * Get a ticket
122 * $t = $box->getTicket();
123 * $box->getAuthToken($t['ticket'], array(
124 * 'username'=>'dongsheng@moodle.com',
125 * 'password'=>'xxx'));
126 * </code>
128 * @param string $ticket
129 * @param string $username
130 * @param string $password
131 * @return mixed
133 function getAuthToken($ticket, $username, $password) {
134 $c = new curl(array('debug'=>$this->debug));
135 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
136 $param = array(
137 'login_form1'=>'',
138 'login'=>$username,
139 'password'=>$password,
140 'dologin'=>1,
141 '__login'=>1
143 try {
144 $ret = $c->post('http://www.box.net/api/1.0/auth/'.$ticket, $param);
145 } catch (moodle_exception $e) {
146 $this->setError(0, 'connection time-out or invalid url');
147 return false;
149 $header = $c->getResponse();
150 if(empty($header['location'])) {
151 throw new repository_exception('invalidpassword', 'repository_boxnet');
153 $location = $header['location'];
154 preg_match('#auth_token=(.*)$#i', $location, $matches);
155 $auth_token = $matches[1];
156 if(!empty($auth_token)) {
157 $this->auth_token = $auth_token;
158 return $auth_token;
159 } else {
160 throw new repository_exception('invalidtoken', 'repository_boxnet');
164 * @param string $path Unused
165 * @param array $params
166 * @return array
168 function getfiletree($path, $params = array()) {
169 $this->_clearErrors();
170 $params['auth_token'] = $this->auth_token;
171 $params['folder_id'] = 0;
172 $params['api_key'] = $this->api_key;
173 $params['action'] = 'get_account_tree';
174 $params['onelevel'] = 1;
175 $params['params[]'] = 'nozip';
176 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
177 try {
178 $args = array();
179 $xml = $c->get($this->_box_api_url, $params);
180 } catch (Exception $e){
182 $ret = array();
183 $o = simplexml_load_string(trim($xml));
184 if($o->status == 'listing_ok') {
185 $tree = $o->tree->folder;
186 $this->buildtree($tree, $ret);
188 return $ret;
192 * @param array $sax
193 * @param array $tree Passed by reference
195 function buildtree($sax, &$tree){
196 $sax = (array)$sax;
197 $count = 0;
198 foreach($sax as $k=>$v){
199 if($k == 'folders'){
200 $o = $sax[$k];
201 foreach($o->folder as $z){
202 $tmp = array('title'=>(string)$z->attributes()->name,
203 'size'=>0, 'date'=>userdate(time()),
204 'thumbnail'=>'http://www.box.net/img/small_folder_icon.gif',
205 'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
206 $tmp['children'] = array();
207 $this->buildtree($z, $tmp['children']);
208 $tree[] = $tmp;
210 } elseif ($k == 'files') {
211 $val = $sax[$k]->file;
212 foreach($val as $file){
213 $thumbnail = (string)$file->attributes()->thumbnail;
214 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
215 $thumbnail = 'http://www.box.net'.$thumbnail;
217 $tmp = array('title'=>(string)$file->attributes()->file_name,
218 'size'=>display_size((int)$file->attributes()->size),
219 'thumbnail'=>$thumbnail,
220 'date'=>userdate((int)$file->attributes()->updated),
221 'source'=>'http://box.net/api/1.0/download/'
222 .$this->auth_token.'/'.(string)$file->attributes()->id,
223 'url'=>(string)$file->attributes()->shared_link);
224 $tree[] = $tmp;
227 $count++;
231 * @param array $params
232 * @return bool|array Array or false
234 function getAccountTree($params = array()) {
235 $params['auth_token'] = $this->auth_token;
236 $params['folder_id'] = 0;
237 $params['api_key'] = $this->api_key;
238 $params['action'] = 'get_account_tree';
239 $params['onelevel'] = 1;
240 $params['params[]'] = 'nozip';
241 $ret_array = array();
242 $data = $this->makeRequest('action=get_account_tree', $params);
243 if ($this->_checkForError($data)) {
244 return false;
246 $tree_count=count($data);
247 $entry_count = 0;
248 for ($i=0; $i<$tree_count; $i++) {
249 $a = $data[$i];
250 switch ($a['tag'])
252 case 'FOLDER':
253 if (@is_array($a['attributes'])) {
254 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
255 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
256 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
258 break;
260 case 'FILE':
261 if (@is_array($a['attributes'])) {
262 $ret_array['file_id'][$i] = $a['attributes']['ID'];
263 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
264 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
265 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
266 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
267 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
268 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
269 } else {
270 @$ret_array['thumbnail'][$i] = 'http://www.box.net'.$a['attributes']['THUMBNAIL'];
272 $entry_count++;
274 break;
277 return $ret_array;
281 * @param string $new_folder_name
282 * @param array $params
283 * @return bool|array Array or false
285 function CreateFolder($new_folder_name, $params = array()) {
286 $params['auth_token'] = $this->auth_token;
287 $params['api_key'] = $this->api_key;
288 $params['action'] = 'create_folder';
289 $params['name'] = $new_folder_name;
290 $defaults = array(
291 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
292 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
294 foreach ($defaults as $key => $value) {
295 if (!array_key_exists($key, $params)) {
296 $params[$key] = $value;
300 $ret_array = array();
301 $data = $this->makeRequest('action=create_folder', $params);
302 if ($this->_checkForError($data)) {
303 return false;
305 foreach ($data as $a) {
306 if (!empty($a['value'])) {
307 switch ($a['tag']) {
309 case 'FOLDER_ID':
310 $ret_array['folder_id'] = $a['value'];
311 break;
313 case 'FOLDER_NAME':
314 $ret_array['folder_name'] = $a['value'];
315 break;
317 case 'FOLDER_TYPE_ID':
318 $ret_array['folder_type_id'] = $a['value'];
319 break;
321 case 'SHARED':
322 $ret_array['shared'] = $a['value'];
323 break;
325 case 'PASSWORD':
326 $ret_array['password'] = $a['value'];
327 break;
329 } else {
330 $ret_array[strtolower($a['tag'])] = null;
333 return $ret_array;
337 * Upload a File
338 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
339 * @return array|bool Array or false
341 function UploadFile ($params = array()) {
342 $params['auth_token'] = $this->auth_token;
343 // this param should be the full path of the file
344 $params['new_file1'] = $params['file'];
345 unset($params['file']);
346 $defaults = array(
347 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
348 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
350 foreach ($defaults as $key => $value) {
351 if (!array_key_exists($key, $params)) {
352 $params[$key] = $value;
355 $ret_array = array();
356 $entry_count = 0;
357 $data = $this->makeRequest('upload', $params);
358 if ($this->_checkForError($data)) {
359 return false;
361 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
362 $a = $data[$i];
363 switch ($a['tag']) {
364 case 'STATUS':
365 $ret_array['status'] = $a['value'];
366 break;
368 case 'FILE':
369 if (is_array($a['attributes'])) {
370 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
371 @$ret_array['id'][$i] = $a['attributes']['ID'];
372 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
373 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
374 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
375 $entry_count++;
377 break;
381 return $ret_array;
384 * @param string $fileid
385 * @param string $newname
386 * @return bool
388 function RenameFile($fileid, $newname) {
389 $params = array(
390 'api_key' => $this->api_key,
391 'auth_token' => $this->auth_token,
392 'action' => 'rename',
393 'target' => 'file',
394 'target_id' => $fileid,
395 'new_name' => $newname,
397 $data = $this->makeRequest('action=rename', $params);
398 if ($this->_checkForError($data)) {
399 return false;
401 foreach ($data as $a) {
402 switch ($a['tag']) {
403 case 'STATUS':
404 if ($a['value'] == 's_rename_node') {
405 return true;
409 return false;
413 * Register New User
415 * @param array $params
416 * @return array|bool Outcome Array or false
418 function RegisterUser($params = array()) {
419 $params['api_key'] = $this->api_key;
420 $params['action'] = 'register_new_user';
421 $params['login'] = $_REQUEST['login'];
422 $params['password'] = $_REQUEST['password'];
423 $ret_array = array();
424 $data = $this->makeRequest('action=register_new_user', $params);
425 if ($this->_checkForError($data)) {
426 return false;
428 foreach ($data as $a) {
429 switch ($a['tag']) {
430 case 'STATUS':
431 $ret_array['status'] = $a['value'];
432 break;
434 case 'AUTH_TOKEN':
435 $ret_array['auth_token'] = $a['value'];
436 break;
438 case 'LOGIN':
439 $ret_array['login'] = $a['value'];
440 break;
441 case 'SPACE_AMOUNT':
442 $ret_array['space_amount'] = $a['value'];
443 break;
444 case 'SPACE_USED':
445 $ret_array['space_used'] = $a['value'];
446 break;
450 return $ret_array;
454 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
456 * @param string $tag
457 * @param string $id Set to ID of file or folder
458 * @param string $target_type File or folder
459 * @param array $params
460 * @return array|bool Outcome Array or false
462 function AddTag($tag, $id, $target_type, $params = array()) {
463 $params['auth_token'] = $this->auth_token;
464 $params['api_key'] = $this->api_key;
465 $params['action'] = 'add_to_tag';
466 $params['target'] = $target_type; // File or folder
467 $params['target_id'] = $id; // Set to ID of file or folder
468 $params['tags[]'] = $tag;
469 $ret_array = array();
470 $data = $this->makeRequest('action=add_to_tag', $params);
471 if ($this->_checkForError($data)) {
472 return false;
474 foreach ($data as $a) {
475 switch ($a['tag']) {
476 case 'STATUS':
477 $ret_array['status'] = $a['value'];
479 break;
482 return $ret_array;
486 * Public Share (http://enabled.box.net/docs/rest#public_share)
488 * @param string $message
489 * @param string $emails
490 * @param string $id Set to ID of file or folder
491 * @param string $target_type File or folder
492 * @param string $password
493 * @param array $params
494 * @return array|bool Outcome Array or false
496 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
497 $params['auth_token'] = $this->auth_token;
498 $params['api_key'] = $this->api_key;
499 $params['action'] = 'public_share';
500 $params['target'] = $target_type;
501 $params['target_id'] = $id;
502 $params['password'] = $password;
503 $params['message'] = $message;
504 $params['emails'] = $emails;
505 $ret_array = array();
506 $data = $this->makeRequest('action=public_share', $params);
507 if ($this->_checkForError($data)) {
508 return false;
510 foreach ($data as $a) {
511 switch ($a['tag']) {
512 case 'STATUS':
513 $ret_array['status'] = $a['value'];
514 break;
515 case 'PUBLIC_NAME':
516 $ret_array['public_name'] = $a['value'];
517 break;
521 return $ret_array;
524 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
526 * @param array $params
527 * @return array|bool Outcome Array or false
529 function GetFriends ($params = array()) {
530 $params['auth_token'] = $this->auth_token;
531 $params['action'] = 'get_friends';
532 $params['api_key'] = $this->api_key;
533 $params['params[]'] = 'nozip';
534 $ret_array = array();
535 $data = $this->makeRequest('action=get_friends', $params);
536 if ($this->_checkForError($data)) {
537 return false;
539 foreach ($data as $a) {
540 switch ($a['tag']) {
541 case 'NAME':
542 $ret_array['name'] = $a['value'];
543 break;
544 case 'EMAIL':
545 $ret_array['email'] = $a['value'];
546 break;
547 case 'ACCEPTED':
548 $ret_array['accepted'] = $a['value'];
549 break;
550 case 'AVATAR_URL':
551 $ret_array['avatar_url'] = $a['value'];
552 break;
553 case 'ID':
554 $ret_array['id'] = $a['value'];
555 break;
556 case 'URL':
557 $ret_array['url'] = $a['value'];
558 break;
559 case 'STATUS':
560 $ret_array['status'] = $a['value'];
561 break;
564 return $ret_array;
568 * Logout User (http://enabled.box.net/docs/rest#get_friends)
570 * @param array $params
571 * @return array|bool Outcome Array or false
573 function Logout($params = array()) {
574 $params['auth_token'] = $this->auth_token;
575 $params['api_key'] = $this->api_key;
576 $params['action'] = 'logout';
577 $ret_array = array();
578 $data = $this->makeRequest('action=logout', $params);
579 if ($this->_checkForError($data)) {
580 return false;
582 foreach ($data as $a) {
583 switch ($a['tag']) {
584 case 'ACTION':
585 $ret_array['logout'] = $a['value'];
587 break;
589 return $ret_array;
593 * @param array $data
594 * @return bool
596 function _checkForError($data) {
597 if ($this->_error_msg != '') {
598 return true;
600 if (@$data[0]['attributes']['STAT'] == 'fail') {
601 $this->_error_code = $data[1]['attributes']['CODE'];
602 $this->_error_msg = $data[1]['attributes']['MSG'];
603 return true;
605 return false;
609 * @return bool
611 public function isError() {
612 if ($this->_error_msg != '') {
613 return true;
615 return false;
620 public function setError($code = 0, $msg){
621 $this->_error_code = $code;
622 $this->_error_msg = $msg;
625 * @return string
627 function getErrorMsg() {
628 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
631 * @return string
633 function getErrorCode() {
634 return $this->_error_code;
639 function _clearErrors() {
640 $this->_error_code = '';
641 $this->_error_msg = '';