a204320d26803c8180534f223b4f8308a20ab49e
[gnucomo.git] / src / web / login.php
1 <?php
2 session_start();
3 require_once('classes/gnucomo_config.php');
4 include "functions.php";
5 ?>
6 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
7 <html>
8 <head>
9 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
10 <link rel='stylesheet' href='gnucomo.css' type='text/css'>
11 <title>GNUCoMo login</title>
12 </head>
13 <body>
14 <?php
15
16 if (empty($_SESSION["username"]))
17 {
18
19 if( isset($_POST["username"]) and isset($_POST["password"]) and isset($_SESSION['login']) )
20 {
21    $name   = $_POST["username"];   // PostgreSQL username
22    $passw  = $_POST["password"];   // PostgreSQL user password
23
24    $config = new gnucomo_config;
25
26    $config->read("gnucomo");
27
28    // connect to the database
29    //echo "Database connection = " . $config->Database($name, $passw) . "<br>";
30    $conn = pg_connect($config->Database($name, $passw));
31
32    if ( !$conn )
33    {
34       echo "Error connecting, try again.";
35       echo login_form();
36    }
37    else
38    {
39       $sql = "SELECT * FROM usr WHERE username='$name'";
40       $res = pg_exec( $conn, $sql );
41       if ($res && pg_numrows($res) == 1)
42       {
43          echo "<br> Database login OK.<br>";
44          session_register('username');
45          $_SESSION["username"] = $name;
46          session_register('password');
47          $_SESSION["password"] = $passw;
48       }
49       else
50       {
51          //  User not found in database. Check if we have any users at all.
52
53          $res = pg_exec($conn, "SELECT count(username) FROM usr");
54          $nusers = pg_fetch_object($res, 0);
55          $nusers = $nusers->count;
56          if ($nusers == 0)
57          {
58             echo "<br>No users in database. Authentication granted.<br>";
59             session_register('username');
60             $_SESSION["username"] = $name;
61             session_register('password');
62             $_SESSION["password"] = $passw;
63          }
64          else
65          {
66             echo "<br>User $name not found in database.<br>";
67          }
68       }
69    }
70 }
71 else
72 {
73    echo login_form();
74    $_SESSION["login"] = true;
75 }
76 }
77 else
78 {
79   echo "Logged in as " . $_SESSION['username'] . "<br>";
80 }
81 ?>
82 </body>
83 </html>