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