inc/auth.inc.php
author peter
Wed, 12 Mar 2008 19:47:25 +0000
changeset 80 48b5f7021aaa
parent 79 0c0aa144356a
child 82 c255196bc447
permissions -rwxr-xr-x
[feladat @ 158] Bugfix for ticket:18: Minor modification in the CSS to make sure no data is displayed on top of other data. Also a little modification to make sure the sub-owners (if there are any) are displayed above the record form, instead of next to it.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
     1
<?php
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
79
0c0aa144356a [feladat @ 156]
peter
parents: 71
diff changeset
    22
//session_start();
1
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.
65
ce1c4d5e1576 [feladat @ 112]
peter
parents: 47
diff changeset
    48
	$result = $db->query("SELECT id, fullname, level FROM users WHERE username=". $db->quote($_SESSION["userlogin"])  ." AND password=". $db->quote(md5($_SESSION["userpwd"]))  ." AND active=1");
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"];
79
0c0aa144356a [feladat @ 156]
peter
parents: 71
diff changeset
    55
        	if(isset($_POST["authenticate"]))
1
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
	?>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
    87
	<h2><?php echo _('Login'); ?></h2>
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
    88
	<?php
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    89
	?>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
    90
	<form method="post" action="<?php echo $_SERVER["PHP_SELF"] ?>">
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    91
	 <table border="0">
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
    92
	  <tr>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
    93
	   <td class="n"><?php echo _('Login'); ?>:</td>
13
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>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
    97
	   <td class="n"><?php echo _('Password'); ?>:</td>
13
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">
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
   103
	    <input type="submit" name="authenticate" class="button" value=" <?php echo _('Login'); ?> ">
13
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>
71
e1b918eaf69a [feladat @ 118]
peter
parents: 65
diff changeset
   108
	<?php
1
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
{
79
0c0aa144356a [feladat @ 156]
peter
parents: 71
diff changeset
   120
	$type = '';
6
9fcac40c1b0e [feladat @ 7]
rejo
parents: 4
diff changeset
   121
	if ( $msg == "" ) {
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   122
		$msg = _('You have logged out.');
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   123
		$type = "success";
6
9fcac40c1b0e [feladat @ 7]
rejo
parents: 4
diff changeset
   124
	};
25
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   125
	unset($_SESSION["userid"]);
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   126
	unset($_SESSION["name"]);
576034a80ea8 [feladat @ 72]
rejo
parents: 13
diff changeset
   127
	unset($_SESSION["level"]);;
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   128
	session_destroy();
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   129
	session_write_close();
13
2ff220cfde13 [feladat @ 60]
rejo
parents: 6
diff changeset
   130
	auth($msg, $type);
1
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   131
	exit;
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   132
}
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   133
58094faf794d [feladat @ 2]
rejo
parents:
diff changeset
   134
?>