| 1 |
<?php |
|---|
| 2 |
header('Expires: Sat, 01 Jan 2000 00:00:00 GMT'); |
|---|
| 3 |
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); |
|---|
| 4 |
header('Cache-Control: post-check=0, pre-check=0',false); |
|---|
| 5 |
header('Pragma: no-cache'); |
|---|
| 6 |
session_cache_limiter('public, no-store'); |
|---|
| 7 |
|
|---|
| 8 |
// connect to database |
|---|
| 9 |
require_once('common/db_connect.php'); //PEAR must be installed |
|---|
| 10 |
|
|---|
| 11 |
function check_login() { |
|---|
| 12 |
global $amp_conf; |
|---|
| 13 |
|
|---|
| 14 |
if ($amp_conf['AUTHTYPE'] == 'database') { |
|---|
| 15 |
$baselink = ($_SERVER['HTTPS']!=''?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; |
|---|
| 16 |
|
|---|
| 17 |
// start a session and don't let it stop automatically |
|---|
| 18 |
session_set_cookie_params(0); |
|---|
| 19 |
session_start(); |
|---|
| 20 |
// setcookie('PHPSESSID', session_id()); ?? |
|---|
| 21 |
|
|---|
| 22 |
// check if the current loading of the page is the first loading after a logout |
|---|
| 23 |
if ($_SESSION['logout'] != '') { |
|---|
| 24 |
unset($_SESSION['logout']); |
|---|
| 25 |
// |
|---|
| 26 |
// initialize a relogin on Firefox |
|---|
| 27 |
// (request login with username 'relogin'): |
|---|
| 28 |
// |
|---|
| 29 |
// CAUTION: After that, relative hyperlinks like |
|---|
| 30 |
// <a href="{$_SERVER['PHP_SELF']}">Link</a> |
|---|
| 31 |
// may be translated into an absolute hyperlink like |
|---|
| 32 |
// http://relogin:relogin@... |
|---|
| 33 |
// which will lead to an error-message in Firefox. |
|---|
| 34 |
// |
|---|
| 35 |
// So you always have to use absolute hyperlinks like $baselink. |
|---|
| 36 |
// |
|---|
| 37 |
if (! preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { |
|---|
| 38 |
$link = preg_replace('/^(https{0,1}\/\/)(.*)$/', '$1relogin:relogin@$2', $baselink); |
|---|
| 39 |
header("Location: $link"); |
|---|
| 40 |
exit; |
|---|
| 41 |
} |
|---|
| 42 |
} |
|---|
| 43 |
|
|---|
| 44 |
// check if a new realm needs to be generated because |
|---|
| 45 |
// it's the first loading of the page (or the first loading |
|---|
| 46 |
// after a logout): |
|---|
| 47 |
// |
|---|
| 48 |
// Remark: The realm is generated with a random ID number |
|---|
| 49 |
// because Internet Explorer will forget the username if the |
|---|
| 50 |
// realm changes. Unfortunately Firefox doesn't do so. |
|---|
| 51 |
if (! isset($_SESSION['realm'])) { |
|---|
| 52 |
srand(); |
|---|
| 53 |
$_SESSION['realm'] = 'freePBX User Portal (SEQ'.mt_rand( 1, 1000000000 ).')'; |
|---|
| 54 |
$_SESSION['login'] = true; |
|---|
| 55 |
header("WWW-Authenticate: Basic realm=\"{$_SESSION['realm']}\""); |
|---|
| 56 |
header('HTTP/1.0 401 Unauthorized'); |
|---|
| 57 |
return false; |
|---|
| 58 |
} |
|---|
| 59 |
|
|---|
| 60 |
// check if a user has already logged in before |
|---|
| 61 |
if (isset($_SESSION['AMP_user'])) { |
|---|
| 62 |
unset($_SESSION['login']); |
|---|
| 63 |
return true; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
// check if a user just entered a username and password |
|---|
| 67 |
// |
|---|
| 68 |
// is_authorized() has to return 'true' if and only if |
|---|
| 69 |
// the username and the passwort given are correct. |
|---|
| 70 |
if (isset($_SESSION['login'])) { |
|---|
| 71 |
if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) { |
|---|
| 72 |
$_SESSION['AMP_user'] = new enduser($_SERVER['PHP_AUTH_USER']); |
|---|
| 73 |
|
|---|
| 74 |
if (!$_SESSION['AMP_user']->checkPassword($_SERVER['PHP_AUTH_PW'])) { |
|---|
| 75 |
// one last chance -- check admin user |
|---|
| 76 |
if ( !(count(getAmpAdminUsers()) > 0) && ($_SERVER['PHP_AUTH_USER'] == $amp_conf['AMPDBUSER']) |
|---|
| 77 |
&& ($_SERVER['PHP_AUTH_PW'] == $amp_conf['AMPDBPASS'])) { |
|---|
| 78 |
|
|---|
| 79 |
// set admin access |
|---|
| 80 |
$_SESSION['AMP_user']->setAdmin(); |
|---|
| 81 |
unset($_SESSION['login']); |
|---|
| 82 |
return true; |
|---|
| 83 |
} |
|---|
| 84 |
} else { |
|---|
| 85 |
unset($_SESSION['login']); |
|---|
| 86 |
return true; |
|---|
| 87 |
} |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
|
|---|
| 91 |
// let the browser ask for a username and a password |
|---|
| 92 |
$_SESSION['login'] = true; |
|---|
| 93 |
header("WWW-Authenticate: Basic realm=\"{$_SESSION['realm']}\""); |
|---|
| 94 |
header('HTTP/1.0 401 Unauthorized'); |
|---|
| 95 |
|
|---|
| 96 |
return false; |
|---|
| 97 |
} else { |
|---|
| 98 |
if (!isset($_SESSION['AMP_user'])) { |
|---|
| 99 |
$_SESSION['AMP_user'] = new ampuser($amp_conf['AMPDBUSER']); |
|---|
| 100 |
} |
|---|
| 101 |
$_SESSION['AMP_user']->setAdmin(); |
|---|
| 102 |
|
|---|
| 103 |
return true; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
|
|---|
| 107 |
$result = check_login(); |
|---|
| 108 |
if ( !(isset($result) ? $result : false) ) { |
|---|
| 109 |
unset($_SESSION['AMP_user']); |
|---|
| 110 |
} |
|---|
| 111 |
|
|---|
| 112 |
include 'header.php'; |
|---|
| 113 |
|
|---|
| 114 |
if ( !(isset($result) ? $result : false) ) { |
|---|
| 115 |
echo '<br><br><br><br><h2><center>You must log in first before you can access this page.</center></h2><br><br><br><br>'; |
|---|
| 116 |
|
|---|
| 117 |
include 'footer.php'; |
|---|
| 118 |
exit; |
|---|
| 119 |
} |
|---|
| 120 |
?> |
|---|