| 1 | package SlideShow; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use vars qw($VERSION @ISA); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | $VERSION = '2.0'; | ||
| 7 | |||
| 8 | @SlideShow::ISA = qw(HTML::Parser); | ||
| 9 | |||
| 10 | 0 | ▶ | use CGI qw/:standard/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use LWP::UserAgent; | ||
| 12 | use HTML::Parser; | ||
| 13 | use URI::Escape; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | |||
| 18 | my $self = HTML::Parser->new(); | ||
| 19 | bless $self, $class; | ||
| 20 | |||
| 21 | while (my $X = shift) { | ||
| 22 | my $Y = shift; | ||
| 23 | $self->{$X} = $Y; | ||
| 24 | } | ||
| 25 | |||
| 26 | die "SlideShow::Master missing required parameter 'master_cgi'" | ||
| 27 | unless exists $self->{master_cgi}; | ||
| 28 | |||
| 29 | $self->{start_title} = "SlideShow Startup" | ||
| 30 | unless exists $self->{start_title}; | ||
| 31 | $self->{tmp_dir} = "/tmp" | ||
| 32 | unless exists $self->{tmp_dir}; | ||
| 33 | $self->{view_file} = "$self->{tmp_dir}/viewfile.html" | ||
| 34 | unless exists $self->{view_file}; | ||
| 35 | $self->{log_file} = "$self->{tmp_dir}/surflog.last" | ||
| 36 | unless exists $self->{tmp_log}; | ||
| 37 | $self->{tmp_log} = "$self->{tmp_dir}/surf.tmp" | ||
| 38 | unless exists $self->{tmp_log}; | ||
| 39 | |||
| 40 | $self->{url_list} = [ "http://www.perl.com/CPAN/" ] | ||
| 41 | unless exists $self->{url_list}; | ||
| 42 | |||
| 43 | $self->{commentary} = "Presented using SlideShow, a Perl module" | ||
| 44 | . " for remote browser control available at" | ||
| 45 | ." <a href=\"http://www.perl.com/CPAN/\">www.perl.com/CPAN/</a>" | ||
| 46 | unless exists $self->{commentary}; | ||
| 47 | |||
| 48 | $self->{redirect} = "$self->{master_cgi}?URL="; | ||
| 49 | $self->{inside} = 0; | ||
| 50 | |||
| 51 | $self->{ua} = new LWP::UserAgent; | ||
| 52 | $self->{ua}->agent("SlideShow/2.0"); | ||
| 53 | $SlideShow::last_marker = '<!-- last -->'; | ||
| 54 | |||
| 55 | $self; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub run { | ||
| 59 | my $self = shift; | ||
| 60 | |||
| 61 | if (not param()) { | ||
| 62 | # no URL given | ||
| 63 | |||
| 64 | header, | ||
| 65 | start_html(-title => $self->{start_title}, | ||
| 66 | -BGCOLOR => "#FFFFFF"), | ||
| 67 | h1($self->{start_title}), | ||
| 68 | start_form, | ||
| 69 | submit('Show URL'), | ||
| 70 | textfield('URL'), | ||
| 71 | end_form; | ||
| 72 | |||
| 73 | if ($self->{url_list}) { | ||
| 74 | print hr; | ||
| 75 | print h2('presets'); | ||
| 76 | print "<UL>\n"; | ||
| 77 | for my $url (@{$self->{url_list}}) { | ||
| 78 | print "<LI> <a href=\"$self->{master_cgi}?URL=$url\">$url</a>\n"; | ||
| 79 | } | ||
| 80 | print "</UL>\n"; | ||
| 81 | } | ||
| 82 | |||
| 83 | if ($self->{commentary}) { | ||
| 84 | print hr; | ||
| 85 | print $self->{commentary}; | ||
| 86 | } | ||
| 87 | |||
| 88 | print end_html; | ||
| 89 | |||
| 90 | } else { | ||
| 91 | my $current_http_dir = param('URL'); | ||
| 92 | $current_http_dir =~ s/\s+$//; | ||
| 93 | |||
| 94 | if (substr($current_http_dir, -1, 1) ne '/') { | ||
| 95 | $current_http_dir =~ s!^(http://.*/).*!$1!; | ||
| 96 | } | ||
| 97 | |||
| 98 | $self->{current_dir} = $current_http_dir; | ||
| 99 | |||
| 100 | print header; | ||
| 101 | |||
| 102 | my $item = param('URL'); | ||
| 103 | |||
| 104 | unlink($self->{view_file}); | ||
| 105 | |||
| 106 | if (not open VIEWFILE, ">$self->{view_file}") { | ||
| 107 | print "<p> can't create $self->{view_file}: $! </p>\n"; | ||
| 108 | } else { | ||
| 109 | print VIEWFILE $$."\n"; | ||
| 110 | |||
| 111 | if ($item =~ /^\s*last\s*$/) { | ||
| 112 | print VIEWFILE $SlideShow::last_marker."\n"; # send the termination | ||
| 113 | close VIEWFILE; | ||
| 114 | |||
| 115 | if (-e $self->{tmp_log}) { | ||
| 116 | unlink ($self->{log_file}); | ||
| 117 | rename ($self->{tmp_log}, $self->{log_file}); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | start_html(-title => "Session finished", | ||
| 122 | -BGCOLOR => "#FFFFFF"), | ||
| 123 | h1("Session finished"); | ||
| 124 | |||
| 125 | print "<a href=\"$self->{master_cgi}\">New Session</a>",p; | ||
| 126 | |||
| 127 | if ($self->{log_file}) { | ||
| 128 | print "<hr>\n"; | ||
| 129 | print "<i>The pages were visited in this order:</i><p>\n"; | ||
| 130 | |||
| 131 | open(LOG, $self->{log_file}); | ||
| 132 | |||
| 133 | while(<LOG>) { | ||
| 134 | print " <LI>".$_; | ||
| 135 | } | ||
| 136 | close(LOG); | ||
| 137 | print "<hr>\n"; | ||
| 138 | print "Save this document locally if you'd like to" | ||
| 139 | . " keep a record.\n"; | ||
| 140 | } | ||
| 141 | |||
| 142 | sleep(4); # sleep longer than the update time for clients | ||
| 143 | unlink ($self->{view_file}); | ||
| 144 | exit 0; | ||
| 145 | } | ||
| 146 | |||
| 147 | my $req = new HTTP::Request 'GET' => $item; | ||
| 148 | my $res = $self->{ua}->request($req); | ||
| 149 | |||
| 150 | if (not $res->is_success) { | ||
| 151 | print "Error: " . $res->status_line . "\n"; | ||
| 152 | } else { | ||
| 153 | my $item = $res->as_string; | ||
| 154 | print $self->rewrite($item, "master"); | ||
| 155 | print VIEWFILE $self->rewrite($item, "viewer"); | ||
| 156 | if ($self->{log_file}) { | ||
| 157 | open LOG, ">>$self->{tmp_log}" | ||
| 158 | or die "can't open $self->{tmp_log}: $!"; | ||
| 159 | print LOG param('URL')."\n"; | ||
| 160 | close LOG; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | close VIEWFILE; | ||
| 164 | |||
| 165 | } | ||
| 166 | print end_html; | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | sub start { | ||
| 171 | my $self = shift ; | ||
| 172 | my ($tag, $attr, $attrseq, $origtext) = @_; | ||
| 173 | |||
| 174 | $self->{result} .= '<' . $tag; | ||
| 175 | |||
| 176 | if (lc($tag) eq 'html') { | ||
| 177 | $self->{inside} = 1; | ||
| 178 | } | ||
| 179 | |||
| 180 | return unless $self->{inside}; | ||
| 181 | my $dirpref = $self->{current_dir}; | ||
| 182 | |||
| 183 | my $hostpref = $dirpref; | ||
| 184 | $hostpref =~ s!^((?:ht|f)tp://[^\/]+).*!$1/!; | ||
| 185 | |||
| 186 | if (defined $dirpref) { | ||
| 187 | $dirpref .= '/' unless | ||
| 188 | substr($dirpref, -1, 1) eq '/'; | ||
| 189 | } | ||
| 190 | |||
| 191 | if (defined $attr->{'src'}) { | ||
| 192 | # fully qualify any relative IMG paths | ||
| 193 | # perhaps this should get the images locally and | ||
| 194 | # serve them up, too, but for now it still points | ||
| 195 | # to the original sites. | ||
| 196 | if ($attr->{'src'} !~ m!^(?:ht|f)tp://!i) { | ||
| 197 | if (substr($attr->{'src'}, 0, 1) eq '/') { | ||
| 198 | $attr->{'src'} = $hostpref . $attr->{'src'}; | ||
| 199 | } else { | ||
| 200 | print STDERR "$attr->{'src'} -> "; | ||
| 201 | $attr->{'src'} = $dirpref . $attr->{'src'}; | ||
| 202 | print STDERR "$attr->{'src'}\n"; | ||
| 203 | } | ||
| 204 | } | ||
| 205 | $attr->{'src'} =~ s|/+$|/|; | ||
| 206 | } | ||
| 207 | |||
| 208 | if (defined $attr->{'href'}) { | ||
| 209 | if ($attr->{'href'} !~ m!^(ht|f)tp://!i) { | ||
| 210 | if (substr($attr->{'href'}, 0, 1) eq '/') { | ||
| 211 | $attr->{'href'} = $hostpref . $attr->{'href'}; | ||
| 212 | } else { | ||
| 213 | $attr->{'href'} = $dirpref . $attr->{'href'}; | ||
| 214 | } | ||
| 215 | } | ||
| 216 | $attr->{'href'} =~ s!/+$!/!; | ||
| 217 | } | ||
| 218 | |||
| 219 | if ($tag eq 'a') { | ||
| 220 | if ($attr->{'href'}) { | ||
| 221 | |||
| 222 | if ($self->{'which'} eq 'master') { | ||
| 223 | # redirect HREFs on the master back into the CGI | ||
| 224 | my $h = $self->{redirect} . $attr->{'href'}; | ||
| 225 | $attr->{'href'} = $h; | ||
| 226 | } else { | ||
| 227 | # convert the viewer's HREFs to some style | ||
| 228 | # change so that aren't as easily tempted | ||
| 229 | # to click off the path | ||
| 230 | delete $attr->{'href'}; | ||
| 231 | |||
| 232 | $attr->{'color'} = 'red'; | ||
| 233 | |||
| 234 | push @$attrseq, 'color' | ||
| 235 | unless grep $_ eq 'color', @$attrseq; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | for my $m (@$attrseq) { | ||
| 241 | $self->{result} .= " $m=\"$attr->{$m}\""; | ||
| 242 | } | ||
| 243 | |||
| 244 | $self->{result} .= '>'; | ||
| 245 | if ($tag eq 'body' and $self->{which} eq 'master') { | ||
| 246 | $self->{result} .= "<font size=-2><p align=right>\n"; | ||
| 247 | $self->{result} .= "\nSlideShow: "; | ||
| 248 | $self->{result} .= "<a href=\"$self->{master_cgi}\">Top</a> | "; | ||
| 249 | $self->{result} .= "<a href=\"$self->{master_cgi}?URL=last\">Quit</a> "; | ||
| 250 | $self->{result} .= "</font></p>\n"; | ||
| 251 | } | ||
| 252 | |||
| 253 | $self->{result}; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub text { | ||
| 257 | my $self = shift ; | ||
| 258 | my $text = shift; | ||
| 259 | |||
| 260 | return unless $self->{inside}; | ||
| 261 | |||
| 262 | $self->{result} .= $text; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub comment { | ||
| 266 | my $self = shift ; | ||
| 267 | my $comment = shift; | ||
| 268 | return unless $self->{inside}; | ||
| 269 | $self->{result} .= "<!-- $comment -->"; | ||
| 270 | } | ||
| 271 | |||
| 272 | sub end { | ||
| 273 | my $self = shift ; | ||
| 274 | return unless $self->{inside}; | ||
| 275 | |||
| 276 | my ($tag, $origtext) = @_; | ||
| 277 | if (lc($tag) eq 'html') { | ||
| 278 | $self->{inside} = 0; | ||
| 279 | } | ||
| 280 | if (lc($tag) eq 'body' and $self->{which} eq 'master') { | ||
| 281 | $self->{result} .= "\n"; | ||
| 282 | |||
| 283 | $self->{result} .= start_form | ||
| 284 | . submit('Next Slide:') | ||
| 285 | . textfield(-name => 'URL', -size=>40, -default=>param('URL')) | ||
| 286 | . end_form; | ||
| 287 | } | ||
| 288 | $self->{result} .= $origtext; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub rewrite { | ||
| 292 | my $self = shift; | ||
| 293 | my $html = shift; | ||
| 294 | my $which = shift; | ||
| 295 | |||
| 296 | $html =~ s/^.*?(<html)/$1/is; | ||
| 297 | |||
| 298 | if (lc($which) eq 'viewer') { | ||
| 299 | $self->{which} = 'viewer'; | ||
| 300 | } else { | ||
| 301 | $self->{which} = 'master'; | ||
| 302 | } | ||
| 303 | |||
| 304 | $self->{result} = ''; | ||
| 305 | |||
| 306 | $self->parse($html); | ||
| 307 | |||
| 308 | return $self->{result}; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub client { | ||
| 312 | my %p; | ||
| 313 | while (shift) { | ||
| 314 | $p{$_} = shift; | ||
| 315 | } | ||
| 316 | |||
| 317 | $p{view_file} = "/tmp/viewfile.html" | ||
| 318 | unless $p{view_file}; | ||
| 319 | |||
| 320 | $p{log_file} = "/tmp/surflog.last" | ||
| 321 | unless $p{log_file}; | ||
| 322 | # $DEBUG = 1; | ||
| 323 | |||
| 324 | # Unbuffer STDOUT | ||
| 325 | $|=1; | ||
| 326 | |||
| 327 | print "HTTP/1.0 200 OK\n"; | ||
| 328 | print "Content-type: multipart/x-mixed-replace;boundary=ThisRandomString\n"; | ||
| 329 | print "\n"; | ||
| 330 | print "--ThisRandomString\n"; | ||
| 331 | |||
| 332 | my $prev_surf_id = "-1"; | ||
| 333 | my $done = 0; | ||
| 334 | my $count = 0; | ||
| 335 | my $TIMEOUT = 60 * 4; # (browser timeout is typically 5 minutes) | ||
| 336 | |||
| 337 | # | ||
| 338 | # wait for the file to show up | ||
| 339 | # present the startup message until it does | ||
| 340 | # | ||
| 341 | while (!-r $p{view_file}) { | ||
| 342 | print "Content-type: text/html\n\n"; | ||
| 343 | |||
| 344 | print "<HTML>\n"; | ||
| 345 | print "<head><title>Waiting for session</title></head>\n"; | ||
| 346 | print "<body bgcolor=\"#ffffff\">\n"; | ||
| 347 | print "<h2>Presentation view</h2>\n"; | ||
| 348 | |||
| 349 | print "time: ".`date`."<p>\n"; | ||
| 350 | print "Waiting for session to begin...<br>\n\n"; | ||
| 351 | |||
| 352 | print "</body>\n"; | ||
| 353 | print "</HTML>\n"; | ||
| 354 | |||
| 355 | print "--ThisRandomString\n"; | ||
| 356 | sleep(5); | ||
| 357 | } | ||
| 358 | |||
| 359 | # | ||
| 360 | # Once we've seen the file, we're in a presentation. | ||
| 361 | # | ||
| 362 | my ($line, $surf_id); | ||
| 363 | while(($count < $TIMEOUT) && !$done) { | ||
| 364 | $count++; | ||
| 365 | $line = 0; | ||
| 366 | if (open(ITEM, $p{view_file})) { | ||
| 367 | while (my $item = <ITEM>) { | ||
| 368 | $line++; | ||
| 369 | if ($line == 1) { | ||
| 370 | chop $item; | ||
| 371 | $surf_id = $item; | ||
| 372 | |||
| 373 | if ($prev_surf_id eq $surf_id) { | ||
| 374 | last; | ||
| 375 | } else { | ||
| 376 | $prev_surf_id = $surf_id; | ||
| 377 | print "Content-type: text/html\n\n"; | ||
| 378 | |||
| 379 | ## Reset the presentation counter | ||
| 380 | $count = 0; | ||
| 381 | |||
| 382 | next; | ||
| 383 | } | ||
| 384 | } | ||
| 385 | |||
| 386 | if ($item =~ /^$SlideShow::last_marker$/) { | ||
| 387 | print "<HTML>\n"; | ||
| 388 | print "<HEAD><TITLE>Session finished</TITLE></HEAD>\n"; | ||
| 389 | print "<BODY bgcolor=\"#ffffff\">\n"; | ||
| 390 | |||
| 391 | print ' | ||
| 392 | <H2>Thank you</H2> | ||
| 393 | |||
| 394 | The session has finished.<br> | ||
| 395 | '; | ||
| 396 | if ($p{log_file}) { | ||
| 397 | print "<hr>\n"; | ||
| 398 | print "<i>The pages were visited in this order:</i><p>\n"; | ||
| 399 | |||
| 400 | open(LOG, $p{log_file}); | ||
| 401 | |||
| 402 | while(<LOG>) { | ||
| 403 | print " <LI>".$_; | ||
| 404 | } | ||
| 405 | close(LOG); | ||
| 406 | print "<hr>\n"; | ||
| 407 | print "Save this document locally if you'd like to" | ||
| 408 | . " keep a record.\n"; | ||
| 409 | } | ||
| 410 | $done = 1; | ||
| 411 | next; | ||
| 412 | } | ||
| 413 | |||
| 414 | print $item; | ||
| 415 | } | ||
| 416 | close(ITEM); | ||
| 417 | print "\n--ThisRandomString\n" if ($line > 1); | ||
| 418 | } | ||
| 419 | sleep(1); | ||
| 420 | } | ||
| 421 | |||
| 422 | if ($count >= $TIMEOUT) { | ||
| 423 | print "--ThisRandomString\n"; | ||
| 424 | |||
| 425 | print "Content-type: text/html\n\n"; | ||
| 426 | print "<HTML>\n"; | ||
| 427 | print '<BODY bgcolor="#ffffff">',"\n"; | ||
| 428 | print "<h2>Timed out.</h2>\n"; | ||
| 429 | print "<hr>\n"; | ||
| 430 | print "<I>This session has been active without update for too long.<br>\n"; | ||
| 431 | print "Hit <b>Reload</b> if you think the session is still in progress.\n"; | ||
| 432 | print "</HTML>\n"; | ||
| 433 | } | ||
| 434 | |||
| 435 | print "--ThisRandomString--\n"; | ||
| 436 | } | ||
| 437 | |||
| 438 | |||
| 439 | 1; | ||
| 440 | __END__ | ||