123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- class sso {
- public $protocol;
- public $domain;
- public $auth_uri;
- public $deauth_uri;
- public $referer;
-
- public function __construct() {
- $this->protocol = api_get_setting('sso_authentication_protocol');
- $this->domain = api_get_setting('sso_authentication_domain');
- $this->auth_uri = api_get_setting('sso_authentication_auth_uri');
- $this->deauth_uri = api_get_setting('sso_authentication_unauth_uri');
-
- $this->referer = $this->protocol.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],'sso'));
- $this->deauth_url = $this->protocol.$this->domain.$this->deauth_uri;
- $this->master_url = $this->protocol.$this->domain.$this->auth_uri;
- $this->target = api_get_path(WEB_PATH);
- }
-
- public function logout() {
- header('Location: '.$this->deauth_url);
- exit;
- }
-
- public function ask_master() {
- header('Location: '.$this->master_url.'&sso_referer='.urlencode($this->referer).'&sso_target='.urlencode($this->target));
- exit;
- }
-
- public function check_user() {
- global $_user, $userPasswordCrypted, $_configuration;
- $loginFailed = false;
-
- $sso = $this->decode_cookie($_GET['sso_cookie']);
-
- $user_table = Database::get_main_table(TABLE_MAIN_USER);
- $sql = "SELECT user_id, username, password,
- auth_source, active, expiration_date
- FROM $user_table
- WHERE username = '".trim(addslashes($sso['username']))."'";
- $result = Database::query($sql);
- if (Database::num_rows($result) > 0) {
- $uData = Database::fetch_array($result);
-
- if ($uData['auth_source'] == PLATFORM_AUTH_SOURCE) {
-
- if (!$userPasswordCrypted) {
- $uData['password'] = md5($uData['password']);
- }
-
-
-
- if ($sso['secret'] === sha1($uData['password'])
- && ($sso['username'] == $uData['username'])) {
-
- if ($uData['active']=='1') {
-
- if ($uData['expiration_date'] > date('Y-m-d H:i:s')
- OR $uData['expiration_date']=='0000-00-00 00:00:00') {
-
- if ($_configuration['multiple_access_urls']) {
- $admin_table = Database::get_main_table(TABLE_MAIN_ADMIN);
-
- $sql = "SELECT user_id FROM $admin_table
- WHERE user_id = '".trim(addslashes($uData['user_id']))."' LIMIT 1";
- $result = Database::query($sql);
- $my_user_is_admin = false;
- if (Database::num_rows($result) > 0) {
- $my_user_is_admin = true;
- }
-
-
-
-
-
- $current_access_url_id = api_get_current_access_url_id();
-
-
- $my_url_list = api_get_access_url_from_user($uData['user_id']);
- if ($my_user_is_admin === false) {
- if (is_array($my_url_list) && count($my_url_list)>0 ) {
- if (in_array($current_access_url_id, $my_url_list)) {
-
- $_user['user_id'] = $uData['user_id'];
- api_session_register('_user');
- event_login();
-
- $sso_target = isset($sso['target']) ? $sso['target'] : api_get_path(WEB_PATH) .'.index.php';
- header('Location: '. $sso_target);
- exit;
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
- exit;
- }
- } else {
-
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
- exit;
- }
- } else {
-
-
- if (in_array(1, $my_url_list)) {
-
-
- $_user['user_id'] = $uData['user_id'];
- api_session_register('_user');
- event_login();
- } else {
-
-
- if (in_array($current_access_url_id, $my_url_list)) {
- $_user['user_id'] = $uData['user_id'];
- api_session_register('_user');
- event_login();
- } else {
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=access_url_inactive');
- exit;
- }
- }
- }
- } else {
-
- $_user['user_id'] = $uData['user_id'];
- api_session_register('_user');
- event_login();
-
-
- }
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_expired');
- exit;
- }
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=account_inactive');
- exit;
- }
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=wrong_password');
- exit;
- }
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=wrong_authentication_source');
- exit;
- }
- } else {
-
- $loginFailed = true;
- api_session_unregister('_uid');
- header('Location: '.api_get_path(WEB_PATH).'index.php?loginFailed=1&error=user_not_found');
- exit;
- }
- return $loginFailed;
- }
-
- private function decode_cookie($cookie) {
- return unserialize(base64_decode($cookie));
- }
- }
|