mytest.pl
226 lines of code
1
#!/usr/local/bin/perl
2
3
# must have's!
4
use strict;
5
use warnings;
6
use CGI::Carp qw(fatalsToBrowser);
7
use DBI;
8
use URI::Escape;
9
use Digest::SHA qw(sha512_base64);
10
11
use lib "/var/www/html/Pm";
12
13
use Bc_sql qw(sql_connect $DB);
14
use Bc_misc qw(get_param referrer shorten_str);
15
use Html qw(header pre_html_header);
16
17
my %COOKIEMONSTER = CGI::Cookie->fetch;
18
19
sub cookie_set($$;$) {
20
  my ($name, $value, $exp) = @_;
21
  $value = uri_unescape($value);
22
  $value = uri_escape($value);
23
  my $rv = "";
24
25
  if ($name) {
26
    my $c = "$name=$value";
27
    if ($exp) { $c .= "; expires=$exp"; }
28
29
    $c =~ s/\n//g;
30
    $rv .= "set-cookie: $c; Secure; HttpOnly;\n";
31
  }
32
33
  return $rv;
34
}
35
36
sub cookie_get($) {
37
  my ($name) = @_;
38
  my $rv = 0;
39
40
  $rv = $COOKIEMONSTER{$name};
41
  $rv =~ s/;(.)+//;
42
  $rv =~ s/$name=//;
43
  $rv = uri_unescape($rv);
44
45
  return $rv;
46
}
47
#my $digest = sha512_base64(get_param("pw"));
48
49
my $DEBUG = 0;
50
51
my $NAME = get_param("cn");
52
my $VALUE = get_param("cv");
53
if (not $NAME) { $NAME = "cname"; }
54
if (not $VALUE) { $VALUE = "cvalue"; }
55
56
my $msg = get_param("msg"); # a msg
57
my $se = get_param("se"); # show all env key=value
58
my $sc = get_param("sc"); # show cookie data
59
my $dc = get_param("dc"); # delete cookie
60
my $r = get_param("r"); # delete all cookies from night-stand.ca
61
my $c = get_param("c"); # create cookie
62
my $v = ""; # value of the cookie named $NAME
63
my $COOKIES = $ENV{HTTP_COOKIE};
64
my $n = cookie_get("n");
65
if ($n) { $msg = $n; }
66
67
# so, this is gonna be a little project in of itself
68
my $onload = "";
69
my $desc = "";
70
if ($n) {
71
  $onload = "return setTimeout(removeMsg, 6000);";
72
  $desc = "to test cookie manipulation (with msg)";
73
}
74
75
my $output = pre_html_header() . header("mytest.pl", "", "", $onload, "", "");
76
77
if ($c) {
78
  $output = cookie_set($NAME, $VALUE);
79
  $output .= cookie_set("n", "cookie $NAME was created");
80
  if (referrer() =~ /\?/)
81
    { $output .= "location: " . referrer() . "&cn=$NAME&cv=$VALUE\n\n"; } else
82
    { $output .= "location: /mytest.pl?cn=$NAME&cv=$VALUE\n\n"; }
83
} elsif ($dc) {
84
  $output = cookie_set($NAME, 'deleted', 'Thu, 01 Jan 1970 00:00:00 GMT');
85
  $output .= cookie_set("n", "cookie $NAME was deleted");
86
  if (referrer() =~ /\?/)
87
    { $output .= "location: " . referrer() . "&cn=$NAME&cv=$VALUE\n\n"; } else
88
    { $output .= "location: /mytest.pl?cn=$NAME&cv=$VALUE\n\n"; }
89
} elsif ($r) {
90
  my $cookies = "";
91
  my @cookies = split(";", $COOKIES);
92
  if (@cookies) {
93
    foreach my $c (sort @cookies) {
94
      my ($cn) = split("=", $c);
95
      $cookies .= cookie_set($cn, 'deleted', 'Thu, 01 Jan 1970 00:00:00 GMT');
96
    }
97
  }
98
99
  $output = cookie_set("n", "reset complete");
100
  $output .= $cookies;
101
  if ($NAME eq "cname") {
102
    $output .= "location: " . referrer() . "\n\n";
103
  } else {
104
    if (referrer() =~ /\?/)
105
      { $output .= "location: " . referrer() . "&cn=$NAME&cv=$VALUE\n\n"; } else
106
      { $output .= "location: /mytest.pl?cn=$NAME&cv=$VALUE\n\n"; }
107
  }
108
109
##############################################################
110
111
} else {
112
113
##############################################################
114
115
  $v = $COOKIEMONSTER{$NAME};
116
  $v =~ s/;(.)*//;
117
  $output .= "<table align=center border=0 cellpadding=0 cellspacing=0 width=260><tr><td align=center class=subtitle>\n";
118
  $output .= "  Cookie Tester\n";
119
  $output .= "</td></tr><tr><td height=10>\n";
120
  $output .= "</td></tr><tr><td align=center style='white-space: nowrap;'>\n";
121
122
  my $options = "";
123
  if ($sc) { $options = "?sc=1"; }
124
  $output .= "  <button onclick=\"document.location='mytest.pl$options';\" title='mytest.pl home'><img src='/images/site/home.png' height=12> home</button>\n";
125
  if (not $se) {
126
    $output .= "   <button onclick=\"document.location='/mytest.pl?r=1';\" title='deletes all cookies from night-stand.ca'><img src='/images/site/eraser.png' height=12> reset</button>\n";
127
    $output .= "</td></tr><tr><td height=10>\n";
128
    $output .= "</td></tr><tr><td align=center style='white-space: nowrap;'>\n";
129
130
    if ($COOKIES) {
131
      if ($sc)
132
        { $output .= "  <button onclick=\"document.location='/mytest.pl';\" title=\"hides the raw cookie data\"> hide <img src='/images/site/cookies.png' height=12> data</button> \n"; } else
133
        { $output .= "  <button onclick=\"document.location='/mytest.pl?sc=1';\" title=\"shows the raw cookie data\">show <img src='/images/site/cookies.png' height=12> data</button> \n"; }
134
    } else {
135
      $output .= "  no <img src='/images/site/cookies.png' height=12><br>\n";
136
    }
137
138
    $output .= "  <button onclick=\"document.location='/mytest.pl?se=1';\" title=\"shows the environment variables\"><img src='/images/site/read.png' height=12> env</button> \n";
139
140
    $output .= "</td></tr><tr><td height=10>\n";
141
    $output .= "</td></tr><tr><td align=center>\n";
142
    $output .= "  <button onclick=\"document.location.reload();\" title=\"reloads the page\"><img src='/images/site/admin/reload.png' height=12> reload</button> \n";
143
    $output .= "</td></tr><tr><td height=10>\n";
144
    $output .= "</td></tr><tr><td align=center style='white-space: nowrap;'>\n";
145
    $output .= "  <form method=post style='display: inline;'>\n";
146
    $output .= "    <input type=hidden name=dc id=dc value=0>\n";
147
    $output .= "    <input type=hidden name=c id=c value=0>\n";
148
    $output .= "    Create or Delete a Cookie<br>\n";
149
    $output .= "    <table border=0 cellpadding=0 cellspacing=0><tr><td>\n";
150
    $output .= "      <input size=7 name=cn value=\"$NAME\" placeholder='a name' title=\"type a name for the cookie\">\n";
151
    $output .= "    </td><td>\n";
152
    $output .= "      <input size=7 name=cv value=\"$VALUE\" placeholder='a value' title=\"type a value for the cookie\">\n";
153
    $output .= "    </td></tr><tr><td align=right valign=top>\n";
154
    $output .= "      <small>Cookie Name</small>\n";
155
    $output .= "    </td><td align=right valign=top>\n";
156
    $output .= "      <small>Cookie Value</small>\n";
157
    $output .= "    </td></tr><tr><td align=right colspan=2>\n";
158
    $output .= "       <button onclick=\"document.getElementById('c').value=1;\" title=\"creates a cookie\">set <img src='/images/site/cookie.png' height=12> </button> \n";
159
    $output .= "       <button onclick=\"document.getElementById('dc').value=1;\" title=\"deletes a cookie\">del <img src='/images/site/cookie.png' height=12> </button> \n";
160
    $output .= "    </td></tr></table>\n";
161
    $output .= "  </form>\n";
162
  }
163
}
164
165
166
$output .= "  <hr>\n";
167
$output .= "  <table align=center border=0 cellpading=0 cellspacing=0 width=100%><tr><td align=center>\n";
168
$output .= "    <small>Notifications</small>\n";
169
$output .= "  </td></tr><tr><td align=center height=25 class=subnavbar>\n";
170
if ($n) {
171
  $output .= "    <table align=center border=0 cellpadding=0 cellspacing=0><tr><td align=center><div id=MSGS class=notice><small>$n</small></div></td></tr></table>\n";
172
}
173
$output .= "  </td></tr></table>\n";
174
175
########################################
176
177
178
$output .= "</td></tr><tr><td height=10>\n";
179
$output .= "</td></tr><tr><td style='white-space: nowrap;'><form method=post style='display: inline;'>\n";
180
if ($se) { $output .= "  <table border=0 cellpadding=0 cellspacing=0 height=2><tr><td></td></tr></table>\n"; }
181
$output .= "  <hr>\n";
182
183
184
########################################
185
186
187
if ($se) {
188
  $output .= "  <small>\n";
189
  foreach my $k (sort keys %ENV) {
190
    $output .= "    <b>$k</b> = <i>" . shorten_str($ENV{$k}, 25) . "</i><br>\n";
191
  }
192
  $output .= "  </small>\n";
193
} else {
194
  $v = $COOKIEMONSTER{$NAME};
195
  $v =~ s/;(.)*//;
196
197
  if ($COOKIES) {
198
    if ($sc) {
199
      $output .= "  cookie data:<br>\n";
200
      $output .= "  <ul style='margin: 0px;'>\n";
201
      my @cookies = split(";", $COOKIES);
202
203
      foreach my $cookie (@cookies) {
204
        my ($name, $value) = split("=", $cookie);
205
        $output .= "    <li><b>$name</b> = <i>" . shorten_str($value, 15) . "</i><br>\n";
206
      }
207
      $output .= "    <br>\n";
208
      $output .= "  </ul>\n\n";
209
    }
210
211
    if ($v) {
212
      $output .= "  value of cookie named <i>$NAME</i> is<br>\n  <b>" . shorten_str($v, 15) . "</b><br>\n  (display of value shortened)<br>\n";
213
    } else {
214
      $output .= "  no cookie named <b>$NAME</b></li><br>\n";
215
    }
216
  } else {
217
    $output .= "  no cookie data to display<br>\n";
218
  }
219
}
220
221
$output .= "</form></td></tr></table>\n";
222
223
print cookie_set("n", "", "Thu, 01 Jan 1970 00:00:00 GMT");
224
print $output;
225
226
exit 1;