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