#!/usr/bin/perl
print "content-type: text/html\n\n";
print "hello world\n";
## Error: could not find run mode \'$rm\' This is the ##
= 1 = #!/usr/bin/perl
= 2 = use strict;
= 3 = use lib '/var/www/cgi-bin/WebApp/libs';
= 4 = use MyLib::Simple;
= 5 =
= 6 = my $webapp = MyLib::Simple->new(
= 7 = PARAMS => {
= 8 = cfg_file => ['simple.ini'],
= 9 = format => 'equal',
= 10 = },
= 11 = );
= 12 = $webapp->run();
= 13 =
##
##
= 14 = DB_DSN = dbi:mysql:database=webapp
= 15 = DB_USER = webadmin
= 16 = DB_PASS =
= 17 =
##
##
= 18 = package MyLib::Simple;
= 19 = use strict;
= 20 =
= 21 = use lib '/var/www/cgi-bin/WebApp/libs';
= 22 = use base 'MyLib::Login';
= 23 =
= 24 = sub cgiapp_init {
= 25 = my $self = shift;
= 26 =
= 27 = $self->SUPER::cgiapp_init;
= 28 =
= 29 = # define runmodes (pages) that require successful login:
= 30 = $self->authen->protected_runmodes(
= 31 = 'private',
= 32 = 'private2',
= 33 = );
= 34 =
= 35 = }
= 36 =
= 37 = sub index : StartRunmode {
= 38 = my $self = shift;
= 39 = my $template = $self->load_tmpl("index.html");
= 40 = $template->param({
= 41 = NAME => 'INDEX',
= 42 = MYURL => $self->query->url(),
= 43 = USER => $self->authen->username,
= 44 = });
= 45 = return $template->output;
= 46 = }
= 47 =
= 48 = sub public : Runmode {
= 49 = my $self = shift;
= 50 = my $template = $self->load_tmpl("default.html");
= 51 = my $msg = "You can see this without logging in.
";
= 52 = $template->param(MESSAGE => $msg);
= 53 = $template->param(NAME => 'Public');
= 54 = $template->param(MYURL => $self->query->url);
= 55 = return $template->output;
= 56 = }
= 57 =
= 58 = sub public2 : Runmode {
= 59 = my $self = shift;
= 60 = my $template = $self->load_tmpl("default.html");
= 61 = my $msg = "You can see this without logging in.
";
= 62 = $template->param(MESSAGE => $msg);
= 63 = $template->param(NAME => 'Public2');
= 64 = $template->param(MYURL => $self->query->url);
= 65 =
= 66 = ### die "made it here\n";
= 67 =
= 68 = return $template->output;
= 69 = }
= 70 =
= 71 = sub private : Runmode {
= 72 = my $self = shift;
= 73 = my $template = $self->load_tmpl("default.html");
= 74 = my $msg = "You must log in to see this.";
= 75 = $template->param(MESSAGE => $msg);
= 76 = $template->param(NAME => 'Private');
= 77 = $template->param(MYURL => $self->query->url);
= 78 = return $template->output;
= 79 = }
= 80 =
= 81 = sub private2 : Runmode {
= 82 = my $self = shift;
= 83 = my $template = $self->load_tmpl("default.html");
= 84 = my $msg = "You must log in to see this.";
= 85 = $template->param(MESSAGE => $msg);
= 86 = $template->param(NAME => 'Private2');
= 87 = $template->param(MYURL => $self->query->url);
= 88 = return $template->output;
= 89 = }
= 90 =
= 91 =
= 92 = 1;
= 93 =
####
= 94 = package MyLib::Login;
= 95 =
= 96 = use strict;
= 97 = use base 'CGI::Application';
= 98 =
= 99 = use CGI::Application::Plugin::AutoRunmode;
= 100 = use CGI::Application::Plugin::DBH(qw/dbh_config dbh/);
= 101 = use CGI::Application::Plugin::Session;
= 102 = use CGI::Application::Plugin::Authentication;
= 103 = use CGI::Application::Plugin::Redirect;
= 104 = use CGI::Application::Plugin::ConfigAuto (qw/cfg/);
= 105 = use Digest::MD5 qw(md5_hex);
= 106 =
= 107 = sub setup {
= 108 = my $self = shift;
= 109 =
= 110 = $self->mode_param(
= 111 = path_info => 1,
= 112 = param => 'rm',
= 113 = );
= 114 = }
= 115 =
= 116 = sub cgiapp_init {
= 117 = my $self = shift;
= 118 =
= 119 = my %CFG = $self->cfg;
= 120 =
= 121 = $self->tmpl_path(['./templates']);
= 122 =
= 123 = # open database connection
= 124 = $self->dbh_config(
= 125 = $CFG{'DB_DSN'}, # "dbi:mysql:database=webapp",
= 126 = $CFG{'DB_USER'}, # "webadmin",
= 127 = $CFG{'DB_PASS'}, # ""
= 128 = );
= 129 =
= 130 = $self->session_config(
= 131 = CGI_SESSION_OPTIONS => [
= 132 = "driver:mysql;serializer:Storable;id:md5",
= 133 = $self->query, {Handle => $self->dbh},
= 134 = ],
= 135 =
= 136 = DEFAULT_EXPIRY => '+1h',
= 137 = # COOKIE_PARAMS => {
= 138 = # -name => 'MYCGIAPPSID',
= 139 = # -expires => '+24h',
= 140 = # -path => '/',
= 141 = # },
= 142 = );
= 143 =
= 144 = # configure authentication parameters
= 145 = $self->authen->config(
= 146 = DRIVER => [ 'DBI',
= 147 = DBH => $self->dbh,
= 148 = TABLE => 'user_info',
= 149 = CONSTRAINTS => {
= 150 = 'user_info.username' => '__CREDENTIAL_1__',
= 151 = 'MD5:user_info.password' => '__CREDENTIAL_2__'
= 152 = },
= 153 = ],
= 154 =
= 155 = STORE => 'Session',
= 156 = LOGOUT_RUNMODE => 'logout',
= 157 = LOGIN_RUNMODE => 'login',
= 158 = POST_LOGIN_RUNMODE => 'okay',
= 159 = RENDER_LOGIN => \&my_login_form,
= 160 = );
= 161 =
= 162 = # define runmodes (pages) that require successful login:
= 163 = $self->authen->protected_runmodes(
= 164 = 'mustlogin',
= 165 = );
= 166 =
= 167 = }
= 168 =
= 169 = sub teardown {
= 170 = my $self = shift;
= 171 = $self->dbh->disconnect(); # close database connection
= 172 = }
= 173 =
= 174 = sub mustlogin : Runmode {
= 175 = my $self = shift;
= 176 = my $url = $self->query->url;
= 177 = return $self->redirect($url);
= 178 = }
= 179 =
= 180 = sub okay : Runmode {
= 181 = my $self = shift;
= 182 =
= 183 = my $url = $self->query->url;
= 184 = # my $user = $self->authen->username;
= 185 = my $dest = $self->query->param('destination') || 'index';
= 186 =
= 187 = if ($url =~ /^https/) {
= 188 = $url =~ s/^https/http/;
= 189 = }
= 190 =
= 191 = return $self->redirect("$url/$dest");
= 192 = }
= 193 =
= 194 = sub login : Runmode {
= 195 = my $self = shift;
= 196 = my $url = $self->query->url;
= 197 =
= 198 = my $user = $self->authen->username;
= 199 = if ($user) {
= 200 = my $message = "User $user is already logged in!";
= 201 = my $template = $self->load_tmpl('default.html');
= 202 = $template->param(MESSAGE => $message);
= 203 = $template->param(MYURL => $url);
= 204 = return $template->output;
= 205 = } else {
= 206 = my $url = $self->query->self_url;
= 207 = unless ($url =~ /^https/) {
= 208 = $url =~ s/^http/https/;
= 209 = return $self->redirect($url);
= 210 = }
= 211 = return $self->my_login_form;
= 212 = }
= 213 = }
= 214 =
= 215 = sub my_login_form {
= 216 = my $self = shift;
= 217 = my $template = $self->load_tmpl('login_form.html');
= 218 =
= 219 = (undef, my $info) = split(/\//, $ENV{'PATH_INFO'});
= 220 = my $url = $self->query->url;
= 221 =
= 222 = my $destination = $self->query->param('destination');
= 223 =
= 224 = unless ($destination) {
= 225 = if ($info) {
= 226 = $destination = $info;
= 227 = } else {
= 228 = $destination = "index";
= 229 = }
= 230 = }
= 231 =
= 232 = my $error = $self->authen->login_attempts;
= 233 =
= 234 = $template->param(MYURL => $url);
= 235 = $template->param(ERROR => $error);
= 236 = $template->param(DESTINATION => $destination);
= 237 = return $template->output;
= 238 = }
= 239 =
= 240 = sub logout : Runmode {
= 241 = my $self = shift;
= 242 = if ($self->authen->username) {
= 243 = $self->authen->logout;
= 244 = $self->session->delete;
= 245 = }
= 246 = return $self->redirect($self->query->url);
= 247 = }
= 248 =
= 249 = sub myerror : ErrorRunmode {
= 250 = my $self = shift;
= 251 = my $error = shift;
= 252 = my $template = $self->load_tmpl("default.html");
= 253 = $template->param(NAME => 'ERROR');
= 254 = $template->param(MESSAGE => $error);
= 255 = $template->param(MYURL => $self->query->url);
= 256 = return $template->output;
= 257 = }
= 258 =
= 259 = sub AUTOLOAD : Runmode {
= 260 = my $self = shift;
= 261 = my $rm = shift;
= 262 = my $template = $self->load_tmpl("default.html");
= 263 = $template->param(NAME => 'AUTOLOAD');
= 264 = $template->param(MESSAGE =>
= 265 = "
\n");
= 266 = $template->param(MYURL => $self->query->url);
= 267 = return $template->output;
= 268 = }
= 269 =
= 270 =
= 271 = 1;
= 272 =
####
= 273 =
= 274 =
= 275 =
= 276 =
User:
Click here to logout.
= 295 =Click here to login.
= 297 =
##
= 308 =
= 309 =
= 310 =
= 311 =
= 312 =
= 313 =
= 314 =
= 315 =
= 316 =
= 317 = This is the page.
= 318 =
= 319 =
= 320 =
= 321 =
= 322 =
= 323 =
= 324 =
= 325 =
= 326 =
= 327 =
= 328 =
= 329 =
= 330 =
##
##
= 331 =
= 332 =
= 333 = Log In
= 334 =
= 335 =
= 336 = Please login:
= 337 =
= 338 = ERROR - Please try again.
= 339 =
= 340 =
= 341 =
= 358 =
= 359 =
= 360 =
= 361 =