summaryrefslogtreecommitdiff
blob: 91adcfbd6f5e7623056f0c8a62b097bb91d6fcd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
# Adopt a Developer
#
# Copyright (C) 2004, 2006 Thomas Cort
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

class login_action extends actor {
  function execute() {
    trigger("html_headers");
    if (isset($_REQUEST['login'])) {
      trigger("login");
    } else {
      trigger("display_login");
    }
    trigger("html_footers");
    return new return_result(true);
  }
}

class display_login_event extends actor {
  function execute() {
    global $username;

    if ($username == "guest") {

      echo "<br><div align=\"center\"><form action=\"./\" method=\"post\"><table>";
      echo "<tr><td><small>Username: </small></td><td>";
      echo "<input type=\"text\" name=\"username\" size=\"10\"></td></tr><tr><td><small>Password: </small></td><td>";
      echo "<input type=\"password\" name=\"passwd\" size=\"10\"></td></tr><tr><td align=\"center\">";
      echo "<input type=\"hidden\" name=\"a\" value=\"login\">";
      echo "<input type=\"hidden\" name=\"login\" value=\"1\">";
      echo "<input type=\"submit\" id=\"button\" style=\"font-size:8pt;background-color:#cccccc;color:#000000\" value=\"login\">";
      echo "</td><td align=\"center\"><input type=\"reset\" id=\"button\" style=\"font-size:8pt;background-color:#cccccc;color:#000000\" value=\"clear\"></td></tr>";
      echo "</table>";
      echo "</form></div>";

    } else {

          echo "<h2>Already Logged In</h2>";
          echo "<h2>Thank You! Come again!</h2>";

    }

    return new return_result(true);    
  }
}

class login_event extends actor {
  function execute() {
    global $username, $accesslevel;

    $_username = isset($_REQUEST['username']) ? doslashes($_REQUEST['username']) : "";
    $password  = isset($_REQUEST['passwd'])   ? md5($_REQUEST['passwd']): "";
    $sessionid = session_id();

    if ($_username != "" and $password !="") {
      $result = db_query("SELECT users.userid,users.accesslevel FROM users WHERE username='$_username' and passwd='$password';");
      if ($result->has_next()) {
        $row    = $result->get_row();
        $userid = $row[0];
        $_accesslevel = $row[1];
        $result = db_query("SELECT * FROM sessions WHERE userid = '$userid';");

        if ($result->has_next() && !db_exec("DELETE FROM sessions WHERE userid = '$userid';")) {
          trigger("begin_story");
          echo "<h2>Database Error</h2>";
          echo "<h2>Thank You! Come again!</h2>";
          trigger("end_story");
        }

        if (db_exec("INSERT INTO sessions (sessionid,userid) VALUES ('$sessionid','$userid');")) {
          $username = $_username;
          $accesslevel = $_accesslevel;
          trigger("default");
        } else {
          trigger("begin_story");
          echo "<h2>Database Error</h2>";
          echo "<h2>Thank You! Come again!</h2>";
          trigger("end_story");
        }
      } else {
        trigger("begin_story");
        echo "<h2>Authentication Error</h2>";
        echo "<h2>Thank You! Come again!</h2>";
        trigger("end_story");
      }
    } else {
      trigger("begin_story");
      echo "<h2>Authentication Error</h2>";
      echo "<h2>Thank You! Come again!</h2>";
      trigger("end_story");
	  }
    return new return_result(true);
  }
}

class logout_action extends actor {
  function execute() {
    global $accesslevel, $username;
    trigger("html_headers");

    $sessionid = session_id();
    if (db_exec("DELETE FROM sessions WHERE sessionid = '$sessionid';")) {
      session_unset();
      session_destroy();
      $accesslevel = 1;
      $username = "guest";
      trigger("begin_story");
      echo "<h2>Logged Out!</h2>";
      echo "<h2>Thank You! Come again!</h2>";
      trigger("end_story");
      echo "<br \>";
      trigger("default");
    } else {
      trigger("begin_story");
      echo "<h2>Database Error!</h2>";
      echo "<h2>Thank You! Come again!</h2>";
      trigger("end_story");
    }
    trigger("html_footers");
    return new return_result(true);
  }
}

class passwd_action extends actor {
  function execute() {
    global $accesslevel, $username;
    trigger("html_headers");
    if (isset($_REQUEST['passwd'])) {
      trigger("passwd");
    } else {
      trigger("display_passwd");
    }
    trigger("html_footers");
    return new return_result(true);
  }
}

class display_passwd_event extends actor {
  function execute() {
    global $username;

    trigger("begin_story");

    if ($username != "guest") {
      echo "<h2>Change Password</h2>";
      echo "<form action=\"./\" method=\"post\"><table>";
      echo "<tr><th>Old Password: </td><td bgcolor=\"#eeeeee\" colspan=\"2\">";
      echo "<input type=\"password\" name=\"old_passwd\"></td></tr><tr><td>";
      echo "<tr><th>New Password: </td><td bgcolor=\"#eeeeee\" colspan=\"2\">";
      echo "<input type=\"password\" name=\"new_passwd\"></td></tr><tr><td>";
      echo "<tr><th>Again: </td><td bgcolor=\"#eeeeee\" colspan=\"2\">";
      echo "<input type=\"password\" name=\"again_passwd\"></td></tr><tr><td bgcolor=\"#eeeeee\">";
      echo "<input type=\"hidden\" name=\"a\" value=\"passwd\">";
      echo "<input type=\"hidden\" name=\"passwd\" value=\"1\">";
      echo "&nbsp;</td><td bgcolor=\"#eeeeee\"><input type=\"submit\" id=\"button\" value=\"change\">";
      echo "</td><td bgcolor=\"#eeeeee\"><input type=\"reset\" id=\"button\" value=\"clear\"></td></tr>";
      echo "</table></form>";
    } else {

      echo "<h2>You aren't Logged In!</h2>";
      echo "<h2>Thank You! Come again!</h2>";

    }

    trigger("end_story");
    return new return_result(true);
  }
}

class passwd_event extends actor {
  function execute() {
    global $username;

    $old_passwd   = isset($_REQUEST['old_passwd'])   ? md5($_REQUEST['old_passwd'])   : "";
    $again_passwd = isset($_REQUEST['again_passwd']) ? md5($_REQUEST['again_passwd']) : "";
    $new_passwd   = isset($_REQUEST['new_passwd'])   ? md5($_REQUEST['new_passwd'])   : "";

    if ($old_passwd != "" && $again_passwd != "" && $new_passwd != "") {
      if ($new_passwd == $again_passwd) {
        $result = db_query("SELECT * from users WHERE username = '$username' AND passwd = '$old_passwd';");
        if ($result->has_next()) {
          if (db_exec("UPDATE users SET passwd = '$new_passwd' WHERE username = '$username';")) {
            trigger("begin_story");
            echo "<h2>Password Updated!</h2>";
            trigger("end_story");
            echo "<br \>";
            trigger("default");
          } else {
            trigger("begin_story");
            echo "<h2>Database Error!</h2>";
            echo "<h2>Thank You! Come again!</h2>";
            trigger("end_story");
          }
        } else {
          trigger("begin_story");
          echo "<h2>Wrong Old Password!</h2>";
          echo "<h2>Thank You! Come again!</h2>";
          trigger("end_story");
        }
      } else {
        trigger("begin_story");
        echo "<h2>New and Again Passwords Don't Match!</h2>";
        echo "<h2>Thank You! Come again!</h2>";
        trigger("end_story");
      }
    } else {
      trigger("begin_story");
      echo "<h2>Incomplete Form Data!</h2>";
      echo "<h2>Thank You! Come again!</h2>";
      trigger("end_story");
    }

    return new return_result(true);
  }
}

register_handler(new login_event("login",50));
register_handler(new display_login_event("display_login",50));
register_action(new login_action("login",50));
register_action(new logout_action("logout",50));
register_handler(new passwd_event("passwd",50));
register_handler(new display_passwd_event("display_passwd",50));
register_action(new passwd_action("passwd",50));
?>