Initialise password fields when loading the page (to prevent the
[misc/kostenrechnung] / lib / login.php
1 <?php
2
3 function check_passwd()
4 {
5   if (empty($_POST['login']) || empty($_POST['passwd']))
6     return false;
7
8   $sql = sprintf("SELECT * FROM sys_user WHERE login = '%s' AND passwd = '%s'",
9                  pg_escape_string($_POST['login']), passwd($_POST['login'], $_POST['passwd']));
10
11   $sth = pg_query($sql);
12
13   if ($sth === false) return false;
14
15   if (substr($_SERVER['HTTP_REFERER'],-12) != '/?login=true'
16       || substr($_SERVER['SCRIPT_FILENAME'],-10) != '/index.php') {
17     error_log('Wrong referrer or wrong request uri');
18     return false;
19   }
20
21   if ($row = pg_fetch_assoc($sth)) {
22     $_SESSION['sys'] = array('uid' => $row['id'],
23                              'login' => $row['login'],
24                              'name' => $row['name'],
25                              'email' => $row['email'],
26                              'basedir' => substr($_SERVER['SCRIPT_FILENAME'],0,-9));
27     return true;
28   }
29
30   error_log('Failed login attempt for user ' . $_POST['login']);
31   return false;
32 }
33
34 function mask_login()
35 {
36   $ret = '<div class="login">';
37
38   $ret .= '<div align="center">';
39   $ret .= '<form action="index.php" method="POST">';
40   $ret .= '<table class="login" cellpadding="5">';
41   $ret .= '<tr><th align="left" colspan="2" style="background: #BBD9EE;">Anmeldung</th></tr>';
42
43   $ret .= '<tr><th align="right">Login</th><td><input type="text" name="login" size="15"></td></tr>';
44   $ret .= '<tr><th align="right">Passwort</th><td><input type="password" name="passwd" size="15"></td></tr>';
45
46   $ret .= '<tr><td colspan="2" align="center"><input type="submit" value="Anmelden"></td></tr>';
47
48   $ret .= '</table>';
49   $ret .= '</form>';
50   $ret .= '</div>';
51   $ret .= '</div>';
52
53   return $ret;
54 }
55
56 ?>