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