Merge branch 'wip_MDL-49327_2.7_guide_getinstance' of https://github.com/nixorv/moodl...
[moodle.git] / lib / boxlib.php
blob79e62c13fc579282ce4d3ba8aeb1ebb401d68d1d
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.net client.
20 * @package core
21 * @author James Levy <james@box.net>
22 * @link http://enabled.box.net
23 * @access public
24 * @version 1.0
25 * @copyright copyright Box.net 2007
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29 defined('MOODLE_INTERNAL') || die();
30 require_once($CFG->libdir . '/oauthlib.php');
32 /**
33 * Box.net client class.
35 * @package core
36 * @copyright 2013 Frédéric Massart
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class boxnet_client extends oauth2_client {
41 /** @const API URL */
42 const API = 'https://api.box.com/2.0';
44 /** @const UPLOAD_API URL */
45 const UPLOAD_API = 'https://upload.box.com/api/2.0';
47 /**
48 * Return authorize URL.
50 * @return string
52 protected function auth_url() {
53 return 'https://www.box.com/api/oauth2/authorize';
56 /**
57 * Create a folder.
59 * @param string $foldername The folder name.
60 * @param int $parentid The ID of the parent folder.
61 * @return array Information about the new folder.
63 public function create_folder($foldername, $parentid = 0) {
64 $params = array('name' => $foldername, 'parent' => array('id' => (string) $parentid));
65 $this->reset_state();
66 $result = $this->post($this->make_url("/folders"), json_encode($params));
67 $result = json_decode($result);
68 return $result;
71 /**
72 * Download the file.
74 * @param int $fileid File ID.
75 * @param string $path Path to download the file to.
76 * @return bool Success or not.
78 public function download_file($fileid, $path) {
79 $this->reset_state();
80 $result = $this->download_one($this->make_url("/files/$fileid/content"), array(),
81 array('filepath' => $path, 'CURLOPT_FOLLOWLOCATION' => true));
82 return ($result === true && $this->info['http_code'] === 200);
85 /**
86 * Get info of a file.
88 * @param int $fileid File ID.
89 * @return object
91 public function get_file_info($fileid) {
92 $this->reset_state();
93 $result = $this->request($this->make_url("/files/$fileid"));
94 return json_decode($result);
97 /**
98 * Get a folder content.
100 * @param int $folderid Folder ID.
101 * @return object
103 public function get_folder_items($folderid = 0) {
104 $this->reset_state();
105 $result = $this->request($this->make_url("/folders/$folderid/items",
106 array('fields' => 'id,name,type,modified_at,size,owned_by')));
107 return json_decode($result);
111 * Log out.
113 * @return void
115 public function log_out() {
116 if ($accesstoken = $this->get_accesstoken()) {
117 $params = array(
118 'client_id' => $this->get_clientid(),
119 'client_secret' => $this->get_clientsecret(),
120 'token' => $accesstoken->token
122 $this->reset_state();
123 $this->post($this->revoke_url(), $params);
125 parent::log_out();
129 * Build a request URL.
131 * @param string $uri The URI to request.
132 * @param array $params Query string parameters.
133 * @param bool $uploadapi Whether this works with the upload API or not.
134 * @return string
136 protected function make_url($uri, $params = array(), $uploadapi = false) {
137 $api = $uploadapi ? self::UPLOAD_API : self::API;
138 $url = new moodle_url($api . '/' . ltrim($uri, '/'), $params);
139 return $url->out(false);
143 * Rename a file.
145 * @param int $fileid The file ID.
146 * @param string $newname The new file name.
147 * @return object Box.net file object.
149 public function rename_file($fileid, $newname) {
150 // This requires a PUT request with data within it. We cannot use
151 // the standard PUT request 'CURLOPT_PUT' because it expects a file.
152 $data = array('name' => $newname);
153 $options = array(
154 'CURLOPT_CUSTOMREQUEST' => 'PUT',
155 'CURLOPT_POSTFIELDS' => json_encode($data)
157 $url = $this->make_url("/files/$fileid");
158 $this->reset_state();
159 $result = $this->request($url, $options);
160 $result = json_decode($result);
161 return $result;
165 * Resets curl for multiple requests.
167 * @return void
169 public function reset_state() {
170 $this->cleanopt();
171 $this->resetHeader();
175 * Return the revoke URL.
177 * @return string
179 protected function revoke_url() {
180 return 'https://www.box.com/api/oauth2/revoke';
184 * Share a file and return the link to it.
186 * @param string $fileid The file ID.
187 * @param bool $businesscheck Whether or not to check if the user can share files, has a business account.
188 * @return object
190 public function share_file($fileid, $businesscheck = true) {
191 // Sharing the file, this requires a PUT request with data within it. We cannot use
192 // the standard PUT request 'CURLOPT_PUT' because it expects a file.
193 $data = array('shared_link' => array('access' => 'open', 'permissions' =>
194 array('can_download' => true, 'can_preview' => true)));
195 $options = array(
196 'CURLOPT_CUSTOMREQUEST' => 'PUT',
197 'CURLOPT_POSTFIELDS' => json_encode($data)
199 $this->reset_state();
200 $result = $this->request($this->make_url("/files/$fileid"), $options);
201 $result = json_decode($result);
203 if ($businesscheck) {
204 // Checks that the user has the right to share the file. If not, throw an exception.
205 $this->reset_state();
206 $this->head($result->shared_link->download_url);
207 $info = $this->get_info();
208 if ($info['http_code'] == 403) {
209 throw new moodle_exception('No permission to share the file');
213 return $result->shared_link;
217 * Search.
219 * @return object
221 public function search($query) {
222 $this->reset_state();
223 $result = $this->request($this->make_url('/search', array('query' => $query, 'limit' => 50, 'offset' => 0)));
224 return json_decode($result);
228 * Return token URL.
230 * @return string
232 protected function token_url() {
233 return 'https://www.box.com/api/oauth2/token';
237 * Upload a file.
239 * Please note that the file is named on Box.net using the path we are providing, and so
240 * the file has the name of the stored_file hash.
242 * @param stored_file $storedfile A stored_file.
243 * @param integer $parentid The ID of the parent folder.
244 * @return object Box.net file object.
246 public function upload_file(stored_file $storedfile, $parentid = 0) {
247 $url = $this->make_url('/files/content', array(), true);
248 $options = array(
249 'filename' => $storedfile,
250 'parent_id' => $parentid
252 $this->reset_state();
253 $result = $this->post($url, $options);
254 $result = json_decode($result);
255 return $result;
261 * Box REST Client Library for PHP5 Developers.
263 * Deprecation note: As of the 14th of December 2013 Box.net APIv1, used by this class,
264 * is reaching its end of life. Please use boxnet_client() instead.
266 * @package core
267 * @author James Levy <james@box.net>
268 * @link http://enabled.box.net
269 * @access public
270 * @version 1.0
271 * @copyright copyright Box.net 2007
272 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
273 * @deprecated since 2.6, 2.5.3, 2.4.7
275 class boxclient {
276 /** @var string */
277 public $auth_token = '';
278 /** @var string */
279 private $_box_api_url = 'https://www.box.com/api/1.0/rest';
280 private $_box_api_upload_url = 'http://upload.box.com/api/1.0/upload';
281 private $_box_api_download_url = 'http://www.box.com/api/1.0/download';
282 private $_box_api_auth_url = 'http://www.box.com/api/1.0/auth';
283 private $_error_code = '';
284 private $_error_msg = '';
285 /** @var bool */
286 private $debug = false;
289 * @param string $api_key
290 * @param string $auth_token
291 * @param bool $debug
293 public function __construct($api_key, $auth_token = '', $debug = false) {
294 $this->api_key = $api_key;
295 $this->auth_token = $auth_token;
296 if (!empty($debug)) {
297 $this->debug = true;
298 } else {
299 $this->debug = false;
303 * Setup for Functions
305 * @param string $method
306 * @param array $params
307 * @return array
309 function makeRequest($method, $params = array()) {
310 $this->_clearErrors();
311 $c = new curl(array('debug'=>$this->debug, 'cache'=>true, 'module_cache'=>'repository'));
312 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
313 try {
314 if ($method == 'upload'){
315 $request = $this->_box_api_upload_url.'/'.
316 $this->auth_token.'/'.$params['folder_id'];
317 $xml = $c->post($request, $params);
318 }else{
319 $args = array();
320 $xml = $c->get($this->_box_api_url, $params);
322 $xml_parser = xml_parser_create();
323 // set $data here
324 xml_parse_into_struct($xml_parser, $xml, $data);
325 xml_parser_free($xml_parser);
326 } catch (moodle_exception $e) {
327 $this->setError(0, 'connection time-out or invalid url');
328 return false;
330 return $data;
333 * @param array $params
334 * @return array
336 function getTicket($params = array()) {
337 $params['api_key'] = $this->api_key;
338 $params['action'] = 'get_ticket';
339 $ret_array = array();
340 $data = $this->makeRequest('action=get_ticket', $params);
341 if ($this->_checkForError($data)) {
342 return false;
344 foreach ($data as $a) {
345 switch ($a['tag']) {
346 case 'STATUS':
347 $ret_array['status'] = $a['value'];
348 break;
349 case 'TICKET':
350 $ret_array['ticket'] = $a['value'];
351 break;
354 return $ret_array;
358 * $options['username'] and $options['password'] must be
359 * given, we will use them to obtain a valid auth_token
360 * To get a token, you should use following code:
362 * <code>
363 * $box = new boxclient('dmls97d8j3i9tn7av8y71m9eb55vrtj4');
364 * Get a ticket
365 * $t = $box->getTicket();
366 * $box->getAuthToken($t['ticket'], array(
367 * 'username'=>'dongsheng@moodle.com',
368 * 'password'=>'xxx'));
369 * </code>
371 * @param string $ticket
372 * @param string $username
373 * @param string $password
374 * @return mixed
376 function getAuthToken($ticket, $username, $password) {
377 $c = new curl(array('debug'=>$this->debug));
378 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>0));
379 $param = array(
380 'login_form1'=>'',
381 'login'=>$username,
382 'password'=>$password,
383 'dologin'=>1,
384 '__login'=>1
386 try {
387 $ret = $c->post($this->_box_api_auth_url.'/'.$ticket, $param);
388 } catch (moodle_exception $e) {
389 $this->setError(0, 'connection time-out or invalid url');
390 return false;
392 $header = $c->getResponse();
393 if(empty($header['location'])) {
394 throw new repository_exception('invalidpassword', 'repository_boxnet');
396 $location = $header['location'];
397 preg_match('#auth_token=(.*)$#i', $location, $matches);
398 $auth_token = $matches[1];
399 if(!empty($auth_token)) {
400 $this->auth_token = $auth_token;
401 return $auth_token;
402 } else {
403 throw new repository_exception('invalidtoken', 'repository_boxnet');
407 * @param string $path Unused
408 * @param array $params
409 * @return array
411 function getfiletree($path, $params = array()) {
412 $this->_clearErrors();
413 $params['auth_token'] = $this->auth_token;
414 $params['folder_id'] = 0;
415 $params['api_key'] = $this->api_key;
416 $params['action'] = 'get_account_tree';
417 $params['onelevel'] = 1;
418 $params['params[]'] = 'nozip';
419 $c = new curl(array('debug'=>$this->debug));
420 $c->setopt(array('CURLOPT_FOLLOWLOCATION'=>1));
421 try {
422 $args = array();
423 $xml = $c->get($this->_box_api_url, $params);
424 } catch (Exception $e){
426 $ret = array();
427 $o = simplexml_load_string(trim($xml));
428 if($o->status == 'listing_ok') {
429 $tree = $o->tree->folder;
430 $this->buildtree($tree, $ret);
432 return $ret;
436 * Get box.net file info
438 * @param string $fileid
439 * @param int $timeout request timeout in seconds
440 * @return stdClass|null
442 function get_file_info($fileid, $timeout = 0) {
443 $this->_clearErrors();
444 $params = array();
445 $params['action'] = 'get_file_info';
446 $params['file_id'] = $fileid;
447 $params['auth_token'] = $this->auth_token;
448 $params['api_key'] = $this->api_key;
449 $http = new curl(array('debug'=>$this->debug));
450 $xml = $http->get($this->_box_api_url, $params, array('timeout' => $timeout));
451 if (!$http->get_errno()) {
452 $o = simplexml_load_string(trim($xml));
453 if ($o->status == 's_get_file_info') {
454 return $o->info;
457 return null;
461 * @param array $sax
462 * @param array $tree Passed by reference
464 function buildtree($sax, &$tree){
465 $sax = (array)$sax;
466 $count = 0;
467 foreach($sax as $k=>$v){
468 if($k == 'folders'){
469 $o = $sax[$k];
470 foreach($o->folder as $z){
471 $tmp = array('title'=>(string)$z->attributes()->name,
472 'size'=>0, 'date'=>userdate(time()),
473 'thumbnail'=>'https://www.box.com/img/small_folder_icon.gif',
474 'path'=>array('name'=>(string)$z->attributes()->name, 'path'=>(int)$z->attributes()->id));
475 $tmp['children'] = array();
476 $this->buildtree($z, $tmp['children']);
477 $tree[] = $tmp;
479 } elseif ($k == 'files') {
480 $val = $sax[$k]->file;
481 foreach($val as $file){
482 $thumbnail = (string)$file->attributes()->thumbnail;
483 if (!preg_match('#^(?:http://)?([^/]+)#i', $thumbnail)) {
484 $thumbnail = 'http://www.box.com'.$thumbnail;
486 $tmp = array('title'=>(string)$file->attributes()->file_name,
487 'size'=>display_size((int)$file->attributes()->size),
488 'thumbnail'=>$thumbnail,
489 'date'=>userdate((int)$file->attributes()->updated),
490 'source'=> $this->_box_api_download_url .'/'
491 .$this->auth_token.'/'.(string)$file->attributes()->id,
492 'url'=>(string)$file->attributes()->shared_link);
493 $tree[] = $tmp;
496 $count++;
500 * @param array $params
501 * @return bool|array Array or false
503 function getAccountTree($params = array()) {
504 $params['auth_token'] = $this->auth_token;
505 $params['folder_id'] = 0;
506 $params['api_key'] = $this->api_key;
507 $params['action'] = 'get_account_tree';
508 $params['onelevel'] = 1;
509 $params['params[]'] = 'nozip';
510 $ret_array = array();
511 $data = $this->makeRequest('action=get_account_tree', $params);
512 if ($this->_checkForError($data)) {
513 return false;
515 $tree_count=count($data);
516 $entry_count = 0;
517 for ($i=0; $i<$tree_count; $i++) {
518 $a = $data[$i];
519 switch ($a['tag'])
521 case 'FOLDER':
522 if (@is_array($a['attributes'])) {
523 $ret_array['folder_id'][$i] = $a['attributes']['ID'];
524 $ret_array['folder_name'][$i] = $a['attributes']['NAME'];
525 $ret_array['shared'][$i] = $a['attributes']['SHARED'];
527 break;
529 case 'FILE':
530 if (@is_array($a['attributes'])) {
531 $ret_array['file_id'][$i] = $a['attributes']['ID'];
532 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
533 @$ret_array['file_keyword'][$i] = $a['attributes']['KEYWORD'];
534 @$ret_array['file_size'][$i] = display_size($a['attributes']['SIZE']);
535 @$ret_array['file_date'][$i] = userdate($a['attributes']['UPDATED']);
536 if (preg_match('#^(?:http://)?([^/]+)#i', $a['attributes']['THUMBNAIL'])) {
537 @$ret_array['thumbnail'][$i] = $a['attributes']['THUMBNAIL'];
538 } else {
539 @$ret_array['thumbnail'][$i] = 'http://www.box.com'.$a['attributes']['THUMBNAIL'];
541 $entry_count++;
543 break;
546 return $ret_array;
550 * @param string $new_folder_name
551 * @param array $params
552 * @return bool|array Array or false
554 function CreateFolder($new_folder_name, $params = array()) {
555 $params['auth_token'] = $this->auth_token;
556 $params['api_key'] = $this->api_key;
557 $params['action'] = 'create_folder';
558 $params['name'] = $new_folder_name;
559 $defaults = array(
560 'parent_id' => 0, //Set to '0' by default. Change to create within sub-folder.
561 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
563 foreach ($defaults as $key => $value) {
564 if (!array_key_exists($key, $params)) {
565 $params[$key] = $value;
569 $ret_array = array();
570 $data = $this->makeRequest('action=create_folder', $params);
571 if ($this->_checkForError($data)) {
572 return false;
574 foreach ($data as $a) {
575 if (!empty($a['value'])) {
576 switch ($a['tag']) {
578 case 'FOLDER_ID':
579 $ret_array['folder_id'] = $a['value'];
580 break;
582 case 'FOLDER_NAME':
583 $ret_array['folder_name'] = $a['value'];
584 break;
586 case 'FOLDER_TYPE_ID':
587 $ret_array['folder_type_id'] = $a['value'];
588 break;
590 case 'SHARED':
591 $ret_array['shared'] = $a['value'];
592 break;
594 case 'PASSWORD':
595 $ret_array['password'] = $a['value'];
596 break;
598 } else {
599 $ret_array[strtolower($a['tag'])] = null;
602 return $ret_array;
606 * Upload a File
607 * @param array $params the file MUST be present in key 'file' and be a moodle stored_file object.
608 * @return array|bool Array or false
610 function UploadFile ($params = array()) {
611 $params['auth_token'] = $this->auth_token;
612 // this param should be the full path of the file
613 $params['new_file1'] = $params['file'];
614 unset($params['file']);
615 $defaults = array(
616 'folder_id' => 0, //Set to '0' by default. Change to create within sub-folder.
617 'share' => 1, //Set to '1' by default. Set to '0' to make folder private.
619 foreach ($defaults as $key => $value) {
620 if (!array_key_exists($key, $params)) {
621 $params[$key] = $value;
624 $ret_array = array();
625 $entry_count = 0;
626 $data = $this->makeRequest('upload', $params);
627 if ($this->_checkForError($data)) {
628 return false;
630 for ($i=0, $tree_count=count($data); $i<$tree_count; $i++) {
631 $a = $data[$i];
632 switch ($a['tag']) {
633 case 'STATUS':
634 $ret_array['status'] = $a['value'];
635 break;
637 case 'FILE':
638 if (is_array($a['attributes'])) {
639 @$ret_array['file_name'][$i] = $a['attributes']['FILE_NAME'];
640 @$ret_array['id'][$i] = $a['attributes']['ID'];
641 @$ret_array['folder_name'][$i] = $a['attributes']['FOLDER_NAME'];
642 @$ret_array['error'][$i] = $a['attributes']['ERROR'];
643 @$ret_array['public_name'][$i] = $a['attributes']['PUBLIC_NAME'];
644 $entry_count++;
646 break;
650 return $ret_array;
653 * @param string $fileid
654 * @param string $newname
655 * @return bool
657 function RenameFile($fileid, $newname) {
658 $params = array(
659 'api_key' => $this->api_key,
660 'auth_token' => $this->auth_token,
661 'action' => 'rename',
662 'target' => 'file',
663 'target_id' => $fileid,
664 'new_name' => $newname,
666 $data = $this->makeRequest('action=rename', $params);
667 if ($this->_checkForError($data)) {
668 return false;
670 foreach ($data as $a) {
671 switch ($a['tag']) {
672 case 'STATUS':
673 if ($a['value'] == 's_rename_node') {
674 return true;
678 return false;
682 * Register New User
684 * @param array $params
685 * @return array|bool Outcome Array or false
687 function RegisterUser($params = array()) {
688 $params['api_key'] = $this->api_key;
689 $params['action'] = 'register_new_user';
690 $params['login'] = $_REQUEST['login'];
691 $params['password'] = $_REQUEST['password'];
692 $ret_array = array();
693 $data = $this->makeRequest('action=register_new_user', $params);
694 if ($this->_checkForError($data)) {
695 return false;
697 foreach ($data as $a) {
698 switch ($a['tag']) {
699 case 'STATUS':
700 $ret_array['status'] = $a['value'];
701 break;
703 case 'AUTH_TOKEN':
704 $ret_array['auth_token'] = $a['value'];
705 break;
707 case 'LOGIN':
708 $ret_array['login'] = $a['value'];
709 break;
710 case 'SPACE_AMOUNT':
711 $ret_array['space_amount'] = $a['value'];
712 break;
713 case 'SPACE_USED':
714 $ret_array['space_used'] = $a['value'];
715 break;
719 return $ret_array;
723 * Add Tags (http://enabled.box.net/docs/rest#add_to_tag)
725 * @param string $tag
726 * @param string $id Set to ID of file or folder
727 * @param string $target_type File or folder
728 * @param array $params
729 * @return array|bool Outcome Array or false
731 function AddTag($tag, $id, $target_type, $params = array()) {
732 $params['auth_token'] = $this->auth_token;
733 $params['api_key'] = $this->api_key;
734 $params['action'] = 'add_to_tag';
735 $params['target'] = $target_type; // File or folder
736 $params['target_id'] = $id; // Set to ID of file or folder
737 $params['tags[]'] = $tag;
738 $ret_array = array();
739 $data = $this->makeRequest('action=add_to_tag', $params);
740 if ($this->_checkForError($data)) {
741 return false;
743 foreach ($data as $a) {
744 switch ($a['tag']) {
745 case 'STATUS':
746 $ret_array['status'] = $a['value'];
748 break;
751 return $ret_array;
755 * Public Share (http://enabled.box.net/docs/rest#public_share)
757 * @param string $message
758 * @param string $emails
759 * @param string $id Set to ID of file or folder
760 * @param string $target_type File or folder
761 * @param string $password
762 * @param array $params
763 * @return array|bool Outcome Array or false
765 function PublicShare($message, $emails, $id, $target_type, $password, $params = array()) {
766 $params['auth_token'] = $this->auth_token;
767 $params['api_key'] = $this->api_key;
768 $params['action'] = 'public_share';
769 $params['target'] = $target_type;
770 $params['target_id'] = $id;
771 $params['password'] = $password;
772 $params['message'] = $message;
773 $params['emails'] = $emails;
774 $ret_array = array();
775 $data = $this->makeRequest('action=public_share', $params);
776 if ($this->_checkForError($data)) {
777 return false;
779 foreach ($data as $a) {
780 switch ($a['tag']) {
781 case 'STATUS':
782 $ret_array['status'] = $a['value'];
783 break;
784 case 'PUBLIC_NAME':
785 $ret_array['public_name'] = $a['value'];
786 break;
790 return $ret_array;
793 * Get Friends (http://enabled.box.net/docs/rest#get_friends)
795 * @param array $params
796 * @return array|bool Outcome Array or false
798 function GetFriends ($params = array()) {
799 $params['auth_token'] = $this->auth_token;
800 $params['action'] = 'get_friends';
801 $params['api_key'] = $this->api_key;
802 $params['params[]'] = 'nozip';
803 $ret_array = array();
804 $data = $this->makeRequest('action=get_friends', $params);
805 if ($this->_checkForError($data)) {
806 return false;
808 foreach ($data as $a) {
809 switch ($a['tag']) {
810 case 'NAME':
811 $ret_array['name'] = $a['value'];
812 break;
813 case 'EMAIL':
814 $ret_array['email'] = $a['value'];
815 break;
816 case 'ACCEPTED':
817 $ret_array['accepted'] = $a['value'];
818 break;
819 case 'AVATAR_URL':
820 $ret_array['avatar_url'] = $a['value'];
821 break;
822 case 'ID':
823 $ret_array['id'] = $a['value'];
824 break;
825 case 'URL':
826 $ret_array['url'] = $a['value'];
827 break;
828 case 'STATUS':
829 $ret_array['status'] = $a['value'];
830 break;
833 return $ret_array;
837 * Logout User (http://enabled.box.net/docs/rest#get_friends)
839 * @param array $params
840 * @return array|bool Outcome Array or false
842 function Logout($params = array()) {
843 $params['auth_token'] = $this->auth_token;
844 $params['api_key'] = $this->api_key;
845 $params['action'] = 'logout';
846 $ret_array = array();
847 $data = $this->makeRequest('action=logout', $params);
848 if ($this->_checkForError($data)) {
849 return false;
851 foreach ($data as $a) {
852 switch ($a['tag']) {
853 case 'ACTION':
854 $ret_array['logout'] = $a['value'];
856 break;
858 return $ret_array;
862 * @param array $data
863 * @return bool
865 function _checkForError($data) {
866 if ($this->_error_msg != '') {
867 return true;
869 if (@$data[0]['attributes']['STAT'] == 'fail') {
870 $this->_error_code = $data[1]['attributes']['CODE'];
871 $this->_error_msg = $data[1]['attributes']['MSG'];
872 return true;
874 return false;
878 * @return bool
880 public function isError() {
881 if ($this->_error_msg != '') {
882 return true;
884 return false;
889 public function setError($code = 0, $msg){
890 $this->_error_code = $code;
891 $this->_error_msg = $msg;
894 * @return string
896 function getErrorMsg() {
897 return '<p>Error: (' . $this->_error_code . ') ' . $this->_error_msg . '</p>';
900 * @return string
902 function getErrorCode() {
903 return $this->_error_code;
908 function _clearErrors() {
909 $this->_error_code = '';
910 $this->_error_msg = '';