diff --git a/api/src/Exception/AuthenticationRequired/Admin.php b/api/src/Exception/AuthenticationRequired/Admin.php new file mode 100644 index 0000000000..db51082319 --- /dev/null +++ b/api/src/Exception/AuthenticationRequired/Admin.php @@ -0,0 +1,26 @@ + + * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License + * @package api + * @subpackage exception + * @access public + */ + +namespace EGroupware\Api\Exception\NoPermission; + +use EGroupware\Api\Exception\NoPermission; + +/** + * User is not authenticated + */ +class AuthenticationRequired extends NoPermission +{ + function __construct($msg=null, $code=401) + { + parent::__construct($msg,$code); + } +} \ No newline at end of file diff --git a/api/src/loader/exception.php b/api/src/loader/exception.php index ce97383361..c4985dd347 100755 --- a/api/src/loader/exception.php +++ b/api/src/loader/exception.php @@ -53,7 +53,11 @@ function try_lang($key,$vars=null) function _egw_log_exception($e,&$headline=null) { $trace = explode("\n", $e->getTraceAsString()); - if ($e instanceof Api\Exception\NoPermission) + if ($e instanceof Api\Exception\NoPermission\AuthenticationRequired) + { + $headline = try_lang('Unauthorized: Authentication required!'); + } + elseif ($e instanceof Api\Exception\NoPermission) { $headline = try_lang('Permission denied!'); } @@ -105,6 +109,14 @@ function egw_exception_handler($e) { Api\Egw::redirect($e->url, $e->app); } + elseif ($e instanceof Api\Exception\NoPermission\AuthenticationRequired) + { + header('WWW-Authenticate: Basic realm="'.$GLOBALS['egw_info']['flags']['auth_realm'] ?? 'EGroupware'.'"'); + http_response_code(401); + echo "\n\n401 Unauthorized\n\nAuthorization failed.\n\n\n"; + _egw_log_exception($e); + exit; + } // logging all exceptions to the error_log (if not cli) and get headline $headline = null; _egw_log_exception($e,$headline); diff --git a/calendar/freebusy.php b/calendar/freebusy.php index e20c3d63ca..ebaf512589 100644 --- a/calendar/freebusy.php +++ b/calendar/freebusy.php @@ -25,6 +25,7 @@ $GLOBALS['egw_info'] = array( 'currentapp' => 'calendar', 'noheader' => True, 'nofooter' => True, + 'no_exception_handler' => 'basic_auth', // we use a basic auth exception handler (sends exception message as basic auth realm) ), ); // check if we are already logged in @@ -45,16 +46,6 @@ if (!($logged_in = !empty(Api\Session::get_sessionid()))) } include ('../header.inc.php'); -function fail_exit($msg) -{ - echo "\n\n$msg\n\n\n

$msg

\n\n\n"; - - header('WWW-Authenticate: Basic realm="'.($GLOBALS['egw_info']['flags']['auth_realm'] ?: 'EGroupware').'"'); - http_response_code(401); - exit; -} - if (!$logged_in) { include ('../api/src/loader.php'); @@ -84,7 +75,7 @@ elseif (isset($_GET['email'])) } if ($user === false || !($username = $GLOBALS['egw']->accounts->id2name($user))) { - fail_exit(lang("freebusy: unknown user '%1', wrong password or not available to not logged in users !!!"." $username($user)",$_GET['user'])); + throw new Api\Exception\NoPermission\AuthenticationRequired(lang("freebusy: unknown user '%1', wrong password or not available to not logged in users !!!"." $username($user)", $_GET['user'])); } if (!$logged_in) { @@ -126,7 +117,7 @@ if (!$logged_in) } if (!$logged_in) { - fail_exit(lang("freebusy: unknown user '%1', or not available for unauthenticated users!", $_GET['user'])); + throw new Api\Exception\NoPermission\AuthenticationRequired(lang("freebusy: unknown user '%1', or not available for unauthenticated users!", $_GET['user'])); } } if ($_GET['debug']) @@ -138,4 +129,4 @@ else Api\Header\Content::type('freebusy.ifb','text/calendar'); } $ical = new calendar_ical(); -echo $ical->freebusy($user, $_GET['end']); +echo $ical->freebusy($user, $_GET['end']); \ No newline at end of file