| 15 | | // start a session and don't let it stop automatically |
|---|
| 16 | | session_set_cookie_params(0); |
|---|
| 17 | | if (!session_id()) session_start(); |
|---|
| 18 | | setcookie('PHPSESSID', session_id()); |
|---|
| | 26 | if (!$_SESSION['AMP_user']->checkPassword($_SERVER['PHP_AUTH_PW'])) { |
|---|
| | 27 | // failed, one last chance -- fallback to amportal.conf db admin user |
|---|
| | 28 | if ( (count(getAmpAdminUsers()) == 0) && ($_SERVER['PHP_AUTH_USER'] == $amp_conf['AMPDBUSER']) |
|---|
| | 29 | && ($_SERVER['PHP_AUTH_PW'] == $amp_conf['AMPDBPASS'])) { |
|---|
| 20 | | // check if the current loading of the page is the first loading after a logout |
|---|
| 21 | | if (isset($_SESSION['logout'])) { |
|---|
| 22 | | unset($_SESSION['logout']); |
|---|
| 23 | | // |
|---|
| 24 | | // initialize a relogin on Firefox |
|---|
| 25 | | // (request login with username 'relogin'): |
|---|
| 26 | | // |
|---|
| 27 | | // CAUTION: After that, relative hyperlinks like |
|---|
| 28 | | // <a href="{$_SERVER['PHP_SELF']}">Link</a> |
|---|
| 29 | | // may be translated into an absolute hyperlink like |
|---|
| 30 | | // http://relogin:relogin@... |
|---|
| 31 | | // which will lead to an error-message in Firefox. |
|---|
| 32 | | // |
|---|
| 33 | | // So you always have to use absolute hyperlinks like $baselink. |
|---|
| 34 | | // |
|---|
| 35 | | if (! preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { |
|---|
| 36 | | $link = preg_replace('/^(https{0,1}\/\/)(.*)$/', '$1relogin:relogin@$2', $baselink); |
|---|
| 37 | | header("Location: $link"); |
|---|
| 38 | | exit; |
|---|
| 39 | | } |
|---|
| | 31 | // password succesfully matched amportal.conf db admin user |
|---|
| | 32 | |
|---|
| | 33 | // set admin access |
|---|
| | 34 | $_SESSION['AMP_user']->setAdmin(); |
|---|
| | 35 | } else { |
|---|
| | 36 | // password failed and admin user fall-back failed |
|---|
| | 37 | unset($_SESSION['AMP_user']); |
|---|
| | 38 | } |
|---|
| | 39 | } // else, succesfully logged in |
|---|
| | 40 | } |
|---|
| | 41 | |
|---|
| | 42 | if (!isset($_SESSION['AMP_user'])) { |
|---|
| | 43 | // not logged in, send headers |
|---|
| | 44 | header('WWW-Authenticate: Basic realm="FreePBX '._('Administration').'"'); |
|---|
| | 45 | header('HTTP/1.0 401 Unauthorized'); |
|---|
| | 46 | showview("noaccess"); |
|---|
| | 47 | exit; |
|---|
| 41 | | |
|---|
| 42 | | // check if a new realm needs to be generated because |
|---|
| 43 | | // it's the first loading of the page (or the first loading |
|---|
| 44 | | // after a logout): |
|---|
| 45 | | // |
|---|
| 46 | | // Remark: The realm is generated with a random ID number |
|---|
| 47 | | // because Internet Explorer will forget the username if the |
|---|
| 48 | | // realm changes. Unfortunately Firefox doesn't do so. |
|---|
| 49 | | if (! isset($_SESSION['realm'])) { |
|---|
| 50 | | srand(); |
|---|
| 51 | | $_SESSION['realm'] = 'FreePBX (SEQ'.mt_rand( 1, 1000000000 ).')'; |
|---|
| 52 | | $_SESSION['login'] = true; |
|---|
| 53 | | header("WWW-Authenticate: Basic realm=\"{$_SESSION['realm']}\""); |
|---|
| 54 | | header('HTTP/1.0 401 Unauthorized'); |
|---|
| 55 | | return false; |
|---|
| 56 | | } |
|---|
| 57 | | |
|---|
| 58 | | // check if a user has already logged in before |
|---|
| 59 | | if (isset($_SESSION['AMP_user'])) { |
|---|
| 60 | | unset($_SESSION['login']); |
|---|
| 61 | | return true; |
|---|
| 62 | | } |
|---|
| 63 | | |
|---|
| 64 | | // check if a user just entered a username and password |
|---|
| 65 | | // |
|---|
| 66 | | // is_authorized() has to return 'true' if and only if |
|---|
| 67 | | // the username and the passwort given are correct. |
|---|
| 68 | | if (isset($_SESSION['login'])) { |
|---|
| 69 | | if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { |
|---|
| 70 | | $_SESSION['AMP_user'] = new ampuser($_SERVER['PHP_AUTH_USER']); |
|---|
| 71 | | |
|---|
| 72 | | if (!$_SESSION['AMP_user']->checkPassword($_SERVER['PHP_AUTH_PW'])) { |
|---|
| 73 | | // one last chance -- check admin user |
|---|
| 74 | | if ( (count(getAmpAdminUsers()) == 0) && ($_SERVER['PHP_AUTH_USER'] == $amp_conf['AMPDBUSER']) |
|---|
| 75 | | && ($_SERVER['PHP_AUTH_PW'] == $amp_conf['AMPDBPASS'])) { |
|---|
| 76 | | |
|---|
| 77 | | // set admin access |
|---|
| 78 | | $_SESSION['AMP_user']->setAdmin(); |
|---|
| 79 | | unset($_SESSION['login']); |
|---|
| 80 | | return true; |
|---|
| 81 | | } |
|---|
| 82 | | } else { |
|---|
| 83 | | unset($_SESSION['login']); |
|---|
| 84 | | return true; |
|---|
| 85 | | } |
|---|
| 86 | | } |
|---|
| 87 | | } |
|---|
| 88 | | |
|---|
| 89 | | // let the browser ask for a username and a password |
|---|
| 90 | | $_SESSION['login'] = true; |
|---|
| 91 | | header("WWW-Authenticate: Basic realm=\"{$_SESSION['realm']}\""); |
|---|
| 92 | | header('HTTP/1.0 401 Unauthorized'); |
|---|
| 93 | | |
|---|
| 94 | | return false; |
|---|
| 95 | | } else { |
|---|
| 96 | | if (!isset($_SESSION['AMP_user'])) { |
|---|
| 97 | | $_SESSION['AMP_user'] = new ampuser($amp_conf['AMPDBUSER']); |
|---|
| 98 | | } |
|---|
| 99 | | $_SESSION['AMP_user']->setAdmin(); |
|---|
| 100 | | |
|---|
| 101 | | return true; |
|---|
| 102 | | } |
|---|
| | 49 | break; |
|---|