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