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