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