Chnaged version number to 0.0.6 in login form.
[gnucomo.git] / src / web / page.class.php
1 <?php
2 /**************************************************************************
3 **  (c) Copyright 2003, Andromeda Technology & Automation
4 ** This is free software; you can redistribute it and/or modify it under the
5 ** terms of the GNU General Public License, see the file COPYING.
6 ***************************************************************************
7 ** MODULE INFORMATION *
8 ***********************
9 **      FILE NAME      : page.class.php
10 **      SYSTEM NAME    : Gnucomo - Gnu Computer Monitoring
11 **      VERSION NUMBER : $Revision: 1.4 $
12 **
13 **  DESCRIPTION      : Base class for Gnucomo web interface pages.
14 **
15 **  EXPORTED OBJECTS : 
16 **  LOCAL    OBJECTS : 
17 **  MODULES  USED    :
18 ***************************************************************************
19 **  ADMINISTRATIVE INFORMATION *
20 ********************************
21 **      ORIGINAL AUTHOR : Arjen Baart - arjen@andromeda.nl
22 **      CREATION DATE   : Jan 22, 2003
23 **      LAST UPDATE     : Feb 19, 2003
24 **      MODIFICATIONS   : 
25 **************************************************************************/
26
27 /*****************************
28    $Log: page.class.php,v $
29    Revision 1.4  2003-07-15 11:05:32  arjen
30    Chnaged version number to 0.0.6 in login form.
31
32    Revision 1.3  2003/02/19 09:12:27  arjen
33    Added the 'form' class on table and td elements. This class is intended
34    for borderless tables that are used to layout HTML forms.
35
36    Revision 1.2  2003/02/13 08:59:52  arjen
37    Use our own error handler for PHP errors and warnings
38
39    Revision 1.1  2003/02/05 09:38:42  arjen
40    A base class for all web interface pages
41
42 ******************************/
43
44 // RCSID = "$Id: page.class.php,v 1.4 2003-07-15 11:05:32 arjen Exp $";
45
46
47 require_once('gnucomo_config.php');
48
49 $last_error = "&nbsp;";
50
51 function error($errno, $errstr, $errfile, $errline)
52 {
53    global $last_error;
54
55    $last_error = $errstr;
56 }
57
58 function login_form()
59 {
60    global $last_error;
61
62 ?>
63    <div class='login'>
64    <h1 align="center">GNU Computer Monitoring</h1>
65    <h4 align="center"><i>Version 0.0.6, July 15, 2003</i></h4>
66    <p>
67       <h2 class='error'><?php echo $last_error?></h2>
68    </p>
69    <center><table class='form'>
70    <tr>
71    <td width='50%' class='form'>
72       <a href='http://gnucomo.org/' target='_top'><img src='logo.png' alt='GnoCoMo logo'></a></td>
73    <td class='form'><form name="login" method="POST">
74        <table class='form'>
75        <tr>
76        <td class='form'>Username</td>
77        <td class='form'><input type="text" name="username"></td>
78        </tr>
79        <tr>
80        <td class='form'>Password</td>
81        <td class='form'><input type="password" name="password"></td>
82        </tr>
83        <tr>
84        <td class='form'>&nbsp;</td>
85        <td align="right" class='form'><input type="submit" value="signin"></td>
86        </tr>
87        </table>
88        </form>
89    </td>
90    </tr>
91    </table></center>
92    </div>
93 <?php
94 }
95
96 class page
97 {
98    var  $the_title;       //  The title used on the page.
99    var  $database;        //  Connection to the database server.
100    var  $path;            //  Directory path to stylesheets and images.
101    var  $config;          //  The XML configuration object.
102
103    //   Page constructor.
104
105    function page($title, $path = "")
106    {
107       $this->the_title = $title;
108       $this->database = false;
109       $this->path = $path;
110       session_start();
111       set_error_handler("error");
112    }
113
114    function Head()
115    {
116 ?>
117       <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
118                     "http://www.w3.org/TR/html4/loose.dtd">
119       <html>
120       <head>
121       <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
122       <meta name="author" content="Arjen Baart">
123       <meta name="expires" content="0">
124
125       <link rel='stylesheet' href='<?php echo $this->path ?>gnucomo.css' type='text/css'>
126       <title><?php echo $this->the_title ?></title>
127       </head>
128       <body>
129 <?php
130    }
131
132    function Body()
133    {
134       echo "The default body.";
135    }
136
137    function Tail()
138    {
139       if ($this->database)
140       {
141          pg_close($this->database);
142       }
143       echo "</body>";
144       echo "</html>";
145    }
146
147    function Showpage()
148    {
149       $this->config = new gnucomo_config;
150
151       $this->config->read("gnucomo");
152
153       $this->Head();
154       if (empty($_SESSION['username']))
155       {
156          //  Nobody logged in yet.
157
158          if (isset($_POST['username']) && isset($_POST['password']))
159          {
160             //  Login form submitted. Try to start a new session.
161
162             $name   = $_POST['username'];   // PostgreSQL username
163             $passw  = $_POST['password'];   // PostgreSQL user password
164
165             //  Connect to the database
166             $this->database = pg_connect($this->config->Database($name, $passw));
167             if ($this->database == false)
168             {
169                login_form();
170             }
171             else
172             {
173                session_register('username');
174                $_SESSION['username'] = $name;
175                session_register('password');
176                $_SESSION['password'] = $passw;
177                $this->Body();
178             }
179
180          }
181          else
182          {
183             //  Nobody tried to login yet. Present the login form.
184             login_form();
185          }
186       }
187       else
188       {
189          $name   = $_SESSION['username'];   // PostgreSQL username
190          $passw  = $_SESSION['password'];   // PostgreSQL user password
191
192          //  Connect to the database
193          $this->database = pg_connect($this->config->Database($name, $passw));
194          $this->Body();
195       }
196       $this->Tail();
197    }
198 }