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