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