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