| 1 | ############################################################################# | ||
| 2 | # | ||
| 3 | # NOTE: This file under revision control using RCS | ||
| 4 | # Any changes made without RCS will be lost | ||
| 5 | # | ||
| 6 | # $Source: /usr/local/cvsroot/vbtk/VBTK/PHtml.pm,v $ | ||
| 7 | # $Revision: 1.7 $ | ||
| 8 | # $Date: 2002/03/04 20:53:07 $ | ||
| 9 | # $Author: bhenry $ | ||
| 10 | # $Locker: $ | ||
| 11 | # $State: Exp $ | ||
| 12 | # | ||
| 13 | # Purpose: | ||
| 14 | # | ||
| 15 | # Description: | ||
| 16 | # | ||
| 17 | # Directions: | ||
| 18 | # | ||
| 19 | # Depends on: | ||
| 20 | # | ||
| 21 | # Copyright (C) 1996-2002 Brent Henry | ||
| 22 | # | ||
| 23 | # This program is free software; you can redistribute it and/or | ||
| 24 | # modify it under the terms of version 2 of the GNU General Public | ||
| 25 | # License as published by the Free Software Foundation available at: | ||
| 26 | # http://www.gnu.org/copyleft/gpl.html | ||
| 27 | # | ||
| 28 | # This program is distributed in the hope that it will be useful, | ||
| 29 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 30 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 31 | # GNU General Public License for more details. | ||
| 32 | # | ||
| 33 | ############################################################################# | ||
| 34 | # | ||
| 35 | # | ||
| 36 | # REVISION HISTORY: | ||
| 37 | # | ||
| 38 | # $Log: PHtml.pm,v $ | ||
| 39 | # Revision 1.7 2002/03/04 20:53:07 bhenry | ||
| 40 | # *** empty log message *** | ||
| 41 | # | ||
| 42 | # Revision 1.6 2002/03/04 16:49:09 bhenry | ||
| 43 | # Changed requirement back to perl 5.6.0 | ||
| 44 | # | ||
| 45 | # Revision 1.5 2002/03/02 00:53:55 bhenry | ||
| 46 | # Documentation updates | ||
| 47 | # | ||
| 48 | # Revision 1.4 2002/01/23 19:16:48 bhenry | ||
| 49 | # Improved handling of passed parms | ||
| 50 | # | ||
| 51 | # Revision 1.3 2002/01/21 17:07:40 bhenry | ||
| 52 | # Disabled 'uninitialized' warnings | ||
| 53 | # | ||
| 54 | # Revision 1.2 2002/01/18 19:24:50 bhenry | ||
| 55 | # Warning Fixes | ||
| 56 | # | ||
| 57 | # Revision 1.1.1.1 2002/01/17 18:05:57 bhenry | ||
| 58 | # VBTK Project | ||
| 59 | # | ||
| 60 | |||
| 61 | package VBTK::PHtml; | ||
| 62 | |||
| 63 | use 5.6.0; | ||
| 64 | use strict; | ||
| 65 | use warnings; | ||
| 66 | # I like using undef as a value so I'm turning off the uninitialized warnings | ||
| 67 | no warnings qw(uninitialized); | ||
| 68 | |||
| 69 | use VBTK::Common; | ||
| 70 | use FileHandle; | ||
| 71 | |||
| 72 | our %PHTML_CACHE; | ||
| 73 | our $VERBOSE = $ENV{VERBOSE}; | ||
| 74 | |||
| 75 | #------------------------------------------------------------------------------- | ||
| 76 | # Function: new | ||
| 77 | # Description: Object constructor. Allocates memory for all class members | ||
| 78 | # Input Parms: | ||
| 79 | # Output Parms: Pointer to class | ||
| 80 | #------------------------------------------------------------------------------- | ||
| 81 | sub new | ||
| 82 | { | ||
| 83 | my $type = shift; | ||
| 84 | my $self = {}; | ||
| 85 | bless $self, $type; | ||
| 86 | |||
| 87 | my ($fileName,$docRoot) = @_; | ||
| 88 | |||
| 89 | &log("Setting up new PHtml object for '$fileName'") if ($VERBOSE); | ||
| 90 | |||
| 91 | $self->{fileName} = $fileName; | ||
| 92 | |||
| 93 | $self; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | #------------------------------------------------------------------------------- | ||
| 98 | # Function: findObj | ||
| 99 | # Description: Check the passed value to see if it's a reference to a PHtml object. | ||
| 100 | # If not, then it must be a filename, so lookup it's object in the | ||
| 101 | # global cache, or create one for it. | ||
| 102 | # Input Parms: | ||
| 103 | # Output Parms: | ||
| 104 | #------------------------------------------------------------------------------- | ||
| 105 | sub findObj | ||
| 106 | { | ||
| 107 | my ($obj) = @_; | ||
| 108 | my $sQuote = '"'; | ||
| 109 | my $dQuote = "'"; | ||
| 110 | |||
| 111 | # If $obj is not a reference, then it must be a filename | ||
| 112 | if (ref($obj) ne 'PHtml') | ||
| 113 | { | ||
| 114 | # If so, try to look it up in the global cache | ||
| 115 | my $fileName = $obj; | ||
| 116 | |||
| 117 | &log("Searching for PHtml object for '$fileName'") if ($VERBOSE > 1); | ||
| 118 | |||
| 119 | # Strip off any double or single quotes | ||
| 120 | $fileName =~ s/[$sQuote$dQuote]//g; | ||
| 121 | |||
| 122 | # Try to lookup the fileName to see if there's an existing object. | ||
| 123 | $obj = $PHTML_CACHE{$fileName}; | ||
| 124 | |||
| 125 | # Otherwise, just allocate a new object for it. | ||
| 126 | if (! defined $obj) | ||
| 127 | { | ||
| 128 | $obj = new VBTK::PHtml ($fileName); | ||
| 129 | $PHTML_CACHE{$fileName} = $obj; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | ($obj); | ||
| 134 | } | ||
| 135 | |||
| 136 | #------------------------------------------------------------------------------- | ||
| 137 | # Function: generateHtml | ||
| 138 | # Description: Read in file, parse out perl, and produce HTML. Note that all | ||
| 139 | # variables in this subroutine start with '_' so as not to conflict | ||
| 140 | # with variables in the parsedContent which are eval'd in the scope | ||
| 141 | # of this subroutine. The base directory must be passed in so that | ||
| 142 | # we know where to find files referenced in '#include' statements | ||
| 143 | # in the phmtl. | ||
| 144 | # Input Parms: Target or object, Base directory of web server | ||
| 145 | # Output Parms: HTML | ||
| 146 | #------------------------------------------------------------------------------- | ||
| 147 | sub generateHtml | ||
| 148 | { | ||
| 149 | my $_obj = shift; | ||
| 150 | my $_baseDir = shift; | ||
| 151 | |||
| 152 | # These variables will be accessible from within the PHtml | ||
| 153 | my ($_parms,$_conn,$_req) = @_; | ||
| 154 | |||
| 155 | my ($_html,$evalText); | ||
| 156 | |||
| 157 | # Just in case a filename was passed, run it through the finder | ||
| 158 | $_obj = &findObj($_obj); | ||
| 159 | |||
| 160 | &log("Generating PHtml code") if ($VERBOSE > 1); | ||
| 161 | $_obj->parseFile($_baseDir); | ||
| 162 | |||
| 163 | # Protect local variables from the eval, but pass along a copy of $_baseDir | ||
| 164 | $evalText = "my(\$_baseDir,\$_obj,\$_html,\$evalText);\n\n"; | ||
| 165 | |||
| 166 | # Make sure the last thing mentioned in the code is the html accumulator, | ||
| 167 | # so that it returns back the html to us. | ||
| 168 | $evalText .= $_obj->{parsedContent} . "\n(\$_html);\n"; | ||
| 169 | |||
| 170 | # Turn off uninitialized variable warnings temporarily because it's just too | ||
| 171 | # annoying to troubleshoot and it's a very minor thing. | ||
| 172 | # no warnings 'uninitialized'; | ||
| 173 | |||
| 174 | # Now execute the PHtml, unless we're in debug mode | ||
| 175 | if(! defined $_parms->{debugPHtml}) | ||
| 176 | { | ||
| 177 | &log("Executing PHtml code") if ($VERBOSE > 1); | ||
| 178 | $_html = eval($evalText); | ||
| 179 | } | ||
| 180 | |||
| 181 | # Turn warnings back on. | ||
| 182 | # use warnings 'uninitialized'; | ||
| 183 | |||
| 184 | # Dump out the code if we had an error, or if we're in debug mode. | ||
| 185 | if (($@)||(defined $_parms->{debugPHtml})) | ||
| 186 | { | ||
| 187 | my $msg = (! $_parms->{debugPHtml}) ? "Error Parsing PHtml - $@" : | ||
| 188 | "Debug mode specified, dumping code"; | ||
| 189 | &log($msg); | ||
| 190 | my $count = 1; | ||
| 191 | my $code; | ||
| 192 | |||
| 193 | # Number the lines | ||
| 194 | foreach my $row (split(/\n/,$evalText)) | ||
| 195 | { | ||
| 196 | $code .= sprintf("%04d: %s\n",$count++,$row); | ||
| 197 | } | ||
| 198 | |||
| 199 | # Convert < and > to html characters. | ||
| 200 | $code =~ s/</\<\;/g; | ||
| 201 | $code =~ s/>/\>\;/g; | ||
| 202 | return "<HTML><H3>$@</H3><PRE>$code</PRE></HTML>"; | ||
| 203 | } | ||
| 204 | |||
| 205 | &log("Generated PHtml:\n$_html\n") if ($VERBOSE > 3); | ||
| 206 | &log("Generated PHtml") if ($VERBOSE == 3); | ||
| 207 | |||
| 208 | ($_html); | ||
| 209 | } | ||
| 210 | |||
| 211 | #------------------------------------------------------------------------------- | ||
| 212 | # Function: parseFile | ||
| 213 | # Description: Read in file and parse out perl and html | ||
| 214 | # Input Parms: | ||
| 215 | # Output Parms: Structure containing perl and html | ||
| 216 | #------------------------------------------------------------------------------- | ||
| 217 | sub parseFile | ||
| 218 | { | ||
| 219 | my $obj = shift; | ||
| 220 | my $baseDir = shift; | ||
| 221 | |||
| 222 | $obj->loadFile() || return 0; | ||
| 223 | |||
| 224 | my $fileContent = $obj->{fileContent}; | ||
| 225 | my $directory = $obj->{directory}; | ||
| 226 | |||
| 227 | my @masterAccum; | ||
| 228 | my $mode='html'; | ||
| 229 | my $LF='"\n"'; | ||
| 230 | my $pound="#"; | ||
| 231 | my ($accum,$modeSwitchFlg,$fileName,$childObj,$rcstag,$includeFileName); | ||
| 232 | |||
| 233 | # Escape out any RCS tags so they won't be interpreted | ||
| 234 | foreach $rcstag ('source:','revision:','date:') | ||
| 235 | { | ||
| 236 | grep(s/[^\\]\$($rcstag.*)\$/\\\$$1\\\$/i,@{$fileContent}); | ||
| 237 | } | ||
| 238 | |||
| 239 | # Step through each line from the file | ||
| 240 | foreach (@{$fileContent}) | ||
| 241 | { | ||
| 242 | $modeSwitchFlg = 0; | ||
| 243 | |||
| 244 | # Watch for #include statements and if found, recurse into the named file | ||
| 245 | if(/<!--${pound}include\s+virtual\s*=\s*["'"]?([^"'"\s]+)["'"]?\s*-->/) | ||
| 246 | { | ||
| 247 | $includeFileName = $1; | ||
| 248 | |||
| 249 | # If include filename is relative, then look in the current objects | ||
| 250 | # directory for it. If it's absolute, then build the path from the | ||
| 251 | # base directory. | ||
| 252 | if($includeFileName =~ /^\//) | ||
| 253 | { | ||
| 254 | $includeFileName = $baseDir . $includeFileName; | ||
| 255 | } | ||
| 256 | else | ||
| 257 | { | ||
| 258 | $includeFileName = "$directory/$includeFileName"; | ||
| 259 | } | ||
| 260 | |||
| 261 | &log("Including file '$includeFileName'") if ($VERBOSE > 2); | ||
| 262 | $childObj=&findObj($includeFileName); | ||
| 263 | |||
| 264 | $accum .= $`; | ||
| 265 | $accum =~ s/\s+$//; | ||
| 266 | push(@masterAccum, "\$_html .= qq($accum);\n") if ($accum ne ''); | ||
| 267 | $accum = ''; | ||
| 268 | $modeSwitchFlg = 1; | ||
| 269 | |||
| 270 | if($childObj->parseFile($baseDir)) | ||
| 271 | { | ||
| 272 | push(@masterAccum, "\$_html .= \"\\n\";\n"); | ||
| 273 | push(@masterAccum, $childObj->{parsedContent}); | ||
| 274 | push(@masterAccum, "\$_html .= \"\\n\";\n"); | ||
| 275 | } | ||
| 276 | else | ||
| 277 | { | ||
| 278 | push(@masterAccum, | ||
| 279 | "\$_html .= qq(\n<!-- Error, can't find $fileName -->);\n"); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | |||
| 283 | # If we find a perl marker, then switch to perl mode | ||
| 284 | if(/<!--${pound}perl\s?/) | ||
| 285 | { | ||
| 286 | $accum .= $`; | ||
| 287 | $accum =~ s/\s+$//; | ||
| 288 | $accum =~ s/\@/\\\@/; | ||
| 289 | push(@masterAccum, "\$_html .= qq($accum);\n") if ($accum ne ''); | ||
| 290 | $mode = 'perl'; | ||
| 291 | $accum = ''; | ||
| 292 | $modeSwitchFlg = 1; | ||
| 293 | } | ||
| 294 | |||
| 295 | # If we're in perl mode and find an end-of-comment marker, then switch | ||
| 296 | # back. | ||
| 297 | if(($mode eq 'perl')&&(/-->/)) | ||
| 298 | { | ||
| 299 | $accum .= $`; | ||
| 300 | $accum =~ s/<!--${pound}perl //; | ||
| 301 | push(@masterAccum, $accum) unless ($accum =~ /^\s*$/); | ||
| 302 | $mode = 'html'; | ||
| 303 | $accum = ''; | ||
| 304 | $modeSwitchFlg = 1; | ||
| 305 | } | ||
| 306 | |||
| 307 | $accum .= ($modeSwitchFlg) ? $' : $_ ; | ||
| 308 | } | ||
| 309 | |||
| 310 | # Chop off whitespace on the end of the line and escape out special characters | ||
| 311 | $accum =~ s/\s+$//; | ||
| 312 | $accum =~ s/([@])/\\$1/; | ||
| 313 | |||
| 314 | # Add whatever's left onto the end of the line | ||
| 315 | push(@masterAccum, "\$_html .= qq($accum);\n") unless ($accum =~ /^\s*$/); | ||
| 316 | |||
| 317 | # Now join it all together and store it in the object | ||
| 318 | $obj->{parsedContent} = join("\n",@masterAccum); | ||
| 319 | |||
| 320 | (1); | ||
| 321 | } | ||
| 322 | |||
| 323 | #------------------------------------------------------------------------------- | ||
| 324 | # Function: loadFile | ||
| 325 | # Description: Read in specified file, using a cached copy if nothing has changed | ||
| 326 | # Input Parms: | ||
| 327 | # Output Parms: Return value | ||
| 328 | #------------------------------------------------------------------------------- | ||
| 329 | sub loadFile | ||
| 330 | { | ||
| 331 | my $obj = shift; | ||
| 332 | my $fsize = $obj->{fsize}; | ||
| 333 | my $mtime = $obj->{mtime}; | ||
| 334 | my $fileName = $obj->{fileName}; | ||
| 335 | |||
| 336 | # Check the size and mtime of the file to see if it has changed. | ||
| 337 | &log("Checking size and mtime of '$fileName'") if ($VERBOSE > 2); | ||
| 338 | my ($inode,$curFsize,$curMtime) = (stat($fileName))[1,7,9]; | ||
| 339 | |||
| 340 | # See if the file really exists. | ||
| 341 | if(! defined $inode) | ||
| 342 | { | ||
| 343 | &error("Invalid file request '$fileName'"); | ||
| 344 | return 0; | ||
| 345 | } | ||
| 346 | |||
| 347 | # If the file exists and the size and mtime haven't changed, then just return | ||
| 348 | return 1 if((defined $fsize)&&(defined $mtime)&& | ||
| 349 | ($fsize == $curFsize)&&($mtime == $curMtime)); | ||
| 350 | |||
| 351 | # Otherwise, open the file, read it in, and store it's contents. | ||
| 352 | my $fh = new FileHandle; | ||
| 353 | |||
| 354 | &log("Reading file '$fileName'") if ($VERBOSE); | ||
| 355 | unless($fh->open("$fileName")) | ||
| 356 | { | ||
| 357 | &error("Cannot open file '$fileName'"); | ||
| 358 | return 0; | ||
| 359 | } | ||
| 360 | |||
| 361 | my @fileContent = <$fh>; | ||
| 362 | |||
| 363 | $fh->close; | ||
| 364 | |||
| 365 | # Store the directory where this file resides. | ||
| 366 | if($fileName =~ /^(\S+)\/([^\/\s]+)$/) | ||
| 367 | { | ||
| 368 | &log("Directory is $1") if ($VERBOSE > 2); | ||
| 369 | $obj->{directory} = $1; | ||
| 370 | } | ||
| 371 | |||
| 372 | $obj->{fileContent} = \@fileContent; | ||
| 373 | $obj->{fsize} = $curFsize; | ||
| 374 | $obj->{mtime} = $curMtime; | ||
| 375 | |||
| 376 | (1); | ||
| 377 | } | ||
| 378 | |||
| 379 | #------------------------------------------------------------------------------- | ||
| 380 | # Function: dumpParsedContent | ||
| 381 | # Description: Dump out the contents of the parsed file | ||
| 382 | # Input Parms: | ||
| 383 | # Output Parms: Return value | ||
| 384 | #------------------------------------------------------------------------------- | ||
| 385 | sub dumpParsedContent | ||
| 386 | { | ||
| 387 | my $obj = shift; | ||
| 388 | |||
| 389 | my $parsedContent = $obj->{parsedContent}; | ||
| 390 | |||
| 391 | print STDOUT "$parsedContent"; | ||
| 392 | |||
| 393 | (0); | ||
| 394 | } | ||
| 395 | |||
| 396 | 1; | ||
| 397 | __END__ |