inc/auth.inc.php
author rejo
Wed, 02 Jan 2008 21:49:07 +0000
changeset 58 78558a77131e
parent 47 ae140472d97c
child 65 ce1c4d5e1576
permissions -rwxr-xr-x
[feladat @ 105] Correcting included file in users.inc.php.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     1
<?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
     2
47
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     3
/*  PowerAdmin, a friendly web-based admin tool for PowerDNS.
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     4
 *  See <https://rejo.zenger.nl/poweradmin> for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     5
 *
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     6
 *  Copyright 2007, 2008  Rejo Zenger <rejo@zenger.nl>
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     7
 *
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     8
 *  This program is free software: you can redistribute it and/or modify
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
     9
 *  it under the terms of the GNU General Public License as published by
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    10
 *  the Free Software Foundation, either version 3 of the License, or
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    11
 *  (at your option) any later version.
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    12
 *
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    13
 *  This program is distributed in the hope that it will be useful,
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    14
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    15
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    16
 *  GNU General Public License for more details.
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    17
 *
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    18
 *  You should have received a copy of the GNU General Public License
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    19
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    20
 */
ae140472d97c [feladat @ 94]
rejo
parents: 25
diff changeset
    21
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    22
session_start();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    23
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    24
if (isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] == "logout")
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    25
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    26
	logout();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    27
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    28
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    29
// If a user had just entered his/her login && password, store them in our session.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    30
if(isset($_POST["authenticate"]))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    31
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    32
    	$_SESSION["userpwd"] = $_POST["password"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    33
    	$_SESSION["userlogin"] = $_POST["username"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    34
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    35
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    36
// Check if the session hasnt expired yet.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    37
if ((isset($_SESSION["userid"])) && ($_SESSION["lastmod"] != "") && ((time() - $_SESSION["lastmod"]) > $EXPIRE))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    38
{
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    39
	logout( _('Session expired, please login again.'),"error");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    40
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    41
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    42
// If the session hasn't expired yet, give our session a fresh new timestamp.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    43
$_SESSION["lastmod"] = time();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    44
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    45
if(isset($_SESSION["userlogin"]) && isset($_SESSION["userpwd"]))
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    46
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    47
    //Username and password are set, lets try to authenticate.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    48
	$result = $db->query("SELECT id, fullname, level FROM users WHERE username='". $_SESSION["userlogin"]  ."' AND password='". md5($_SESSION["userpwd"])  ."' AND active=1");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    49
	if($result->numRows() == 1)
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    50
	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    51
        	$rowObj = $result->fetchRow();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    52
		$_SESSION["userid"] = $rowObj["id"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    53
		$_SESSION["name"] = $rowObj["fullname"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    54
		$_SESSION["level"] = $rowObj["level"];
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    55
        	if($_POST["authenticate"])
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    56
        	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    57
            		//If a user has just authenticated, redirect him to index with timestamp, so post-data gets lost.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    58
            		session_write_close();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    59
            		clean_page("index.php");
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    60
            		exit;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    61
        	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    62
    	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    63
    	else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    64
    	{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    65
        	//Authentication failed, retry.
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    66
	        auth( _('Authentication failed!'),"error");
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    67
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    68
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    69
else
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    70
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    71
	//No username and password set, show auth form (again).
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    72
	auth();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    73
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    74
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    75
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    76
 * Print the login form.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    77
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    78
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    79
function auth($msg="",$type="success")
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    80
{
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    81
	include_once('inc/header.inc.php');
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    82
	if ( $msg )
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    83
	{
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    84
		print "<div class=\"$type\">$msg</div>\n";
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    85
	}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
    86
	?>
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    87
	<h2><? echo _('Login'); ?></h2>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    88
	<?
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    89
	?>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    90
	<form method="post" action="<? echo $_SERVER["PHP_SELF"] ?>">
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    91
	 <table border="0">
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    92
	  <tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    93
	   <td class="n"><? echo _('Login'); ?>:</td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    94
	   <td class="n"><input type="text" class="input" name="username"></td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    95
	  </tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    96
	  <tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    97
	   <td class="n"><? echo _('Password'); ?>:</td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    98
	   <td class="n"><input type="password" class="input" name="password"></td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    99
	  </tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   100
	  <tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   101
	   <td class="n">&nbsp;</td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   102
	   <td class="n">
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   103
	    <input type="submit" name="authenticate" class="button" value=" <? echo _('Login'); ?> ">
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   104
	   </td>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   105
	  </tr>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   106
	 </table>
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   107
	</form>
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   108
	<?
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   109
	include_once('inc/footer.inc.php');
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   110
	exit;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   111
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   112
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   113
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   114
/*
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   115
 * Logout the user and kickback to login form.
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   116
 */
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   117
6
9fcac40c1b0e [feladat @ 7]
rejo
parents: 4
diff changeset
   118
function logout($msg="")
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   119
{
6
9fcac40c1b0e [feladat @ 7]
rejo
parents: 4
diff changeset
   120
	if ( $msg == "" ) {
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   121
		$msg = _('You have logged out.');
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   122
		$type = "success";
6
9fcac40c1b0e [feladat @ 7]
rejo
parents: 4
diff changeset
   123
	};
25
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   124
	unset($_SESSION["userid"]);
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   125
	unset($_SESSION["name"]);
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   126
	unset($_SESSION["level"]);;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   127
	session_destroy();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
	session_write_close();
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   129
	auth($msg, $type);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   130
	exit;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
?>