| 1 | # ShellShark's CMS | ||||||||||||||||||||||||||||
| 2 | # Content Management System engine for ShellShark Networks, Inc. | ||||||||||||||||||||||||||||
| 3 | # Copyright (c)2005 ShellShark Networks, Inc. All rights reserved. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package WWW::CMS; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use POSIX qw( strftime ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | # Create a new WWW::CMS instance | ||||||||||||||||||||||||||||
| 13 | # Takes a hash of arguments containing Base and Module | ||||||||||||||||||||||||||||
| 14 | # Returns an object reference on success, nothing on failure | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $self = { }; | |||||||||||||||||||||||||||
| 17 | 0 | my ( $class, $args ) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Bless me father, for I have OOP'ed | ||||||||||||||||||||||||||||
| 20 | 0 | bless $self, $class; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Is our template basedir defined, and does it exist? | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | if ( !$args->{ TemplateBase } || !-d $args->{ TemplateBase } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"TemplateBase"} and -d $args->{"TemplateBase"}
MC/DC0%0/2
$args->{"TemplateBase"} and -d $args->{"TemplateBase"}
$args->{"TemplateBase"}
-d $args->{"TemplateBase"}
Truth table0%0/3
A and B
A= $args->{"TemplateBase"}B= -d $args->{"TemplateBase"}
| |||||||||||||||||||||||||||||
| 24 | 0 | print STDERR "WWW::CMS: Fatal error: Template base directory not defined or does not exist!\n"; | |||||||||||||||||||||||||||
| 25 | 0 | return; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # Strip trailing slash from template basedir | ||||||||||||||||||||||||||||
| 29 | 0 | $args->{ TemplateBase } =~ s/\/$//; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Is our template module defined, and does it exist? | ||||||||||||||||||||||||||||
| 32 | 0 | ▶ | if ( !$args->{ Module } || !-f "$args->{ TemplateBase }/$args->{ Module }" ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$args->{"Module"} and -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
MC/DC0%0/2
$args->{"Module"} and -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
$args->{"Module"}
-f qq[$args->{"TemplateBase"}/$args->{"Module"}]
Truth table0%0/3
A and B
A= $args->{"Module"}B= -f qq[$args->{"TemplateBase"}/$args->{"Module"}]
| |||||||||||||||||||||||||||||
| 33 | 0 | print STDERR "WWW::CMS: Fatal error: Template module not defined or not found\n"; | |||||||||||||||||||||||||||
| 34 | 0 | return; | |||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # Open the template and shove it in memory | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | open my $fh, "<", "$args->{ TemplateBase }/$args->{ Module }" || sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 39 | 0 | $self->{ ERRMSG } = "Failed to open template module '$args->{ TemplateBase }/$args->{ Module }': $!"; | |||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||
| 41 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition0%0/3
"$args->{'TemplateBase'}/$args->{'Module'}" || sub {
$self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!";
return;
}
MC/DC0%0/2
"$args->{'TemplateBase'}/$args->{'Module'}" || sub {
$self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!";
return;
}
"$args->{'TemplateBase'}/$args->{'Module'}"
sub {
$self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!";
return;
}
Truth table0%0/3
A || B
A= "$args->{'TemplateBase'}/$args->{'Module'}"B= sub {
$self->{'ERRMSG'} = "Failed to open template module '$args->{'TemplateBase'}/$args->{'Module'}': $!";
return;
}
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # Slurpy, slurpy.... | ||||||||||||||||||||||||||||
| 44 | 0 | $self->{ template } = do { local $/; <$fh> }; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | close $fh; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | # Stuff our arguments into our namespace | ||||||||||||||||||||||||||||
| 49 | 0 | $self->{ TemplateBase } = $args->{ TemplateBase }; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Give this instance the template module's name for later identification, if needed | ||||||||||||||||||||||||||||
| 52 | 0 | $self->{ instance } = $args->{ Module }; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # Setup a heap for arbitrary variable storage for IF/ELSE operators in templates | ||||||||||||||||||||||||||||
| 55 | 0 | $self->{ heap } = { }; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | return $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | sub publicize { | ||||||||||||||||||||||||||
|
Subroutine
publicize: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | # Find / replace all instances of built-in tags with output of respective code | ||||||||||||||||||||||||||||
| 62 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # Pass in an existing CGI object | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $query = shift || sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 66 | 0 | $self->{ ERRSTR } = 'CGI object not passed into publicize()'; | |||||||||||||||||||||||||||
| 67 | 0 | return; | |||||||||||||||||||||||||||
| 68 | 0 | ▶ | }; | ||||||||||||||||||||||||||
|
Condition0%0/3
shift() || sub {
$self->{'ERRSTR'} = 'CGI object not passed into publicize()';
return;
}
MC/DC0%0/2
shift() || sub {
$self->{'ERRSTR'} = 'CGI object not passed into publicize()';
return;
}
shift()
sub {
$self->{'ERRSTR'} = 'CGI object not passed into publicize()';
return;
}
Truth table0%0/3
A || B
A= shift()B= sub {
$self->{'ERRSTR'} = 'CGI object not passed into publicize()';
return;
}
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Many thanks to revdiablo for making this uberleet regex | ||||||||||||||||||||||||||||
| 71 | 0 | $self->{ template } =~ s{ | |||||||||||||||||||||||||||
| 72 | \Q^^[\E # start operator block | ||||||||||||||||||||||||||||
| 73 | (IF|WHILE) # operator name | ||||||||||||||||||||||||||||
| 74 | \Q]=\E # start condition | ||||||||||||||||||||||||||||
| 75 | ([^\n]*) # condition expression | ||||||||||||||||||||||||||||
| 76 | \Q^^\E # end condition | ||||||||||||||||||||||||||||
| 77 | ((?:.(?!\^\^))*.) # contents | ||||||||||||||||||||||||||||
| 78 | (?: \Q^^[ELSE]^^\E # start optional else block | ||||||||||||||||||||||||||||
| 79 | ((?:.(?!\^\^))*.) # contents | ||||||||||||||||||||||||||||
| 80 | )? # ensure it is optional | ||||||||||||||||||||||||||||
| 81 | \Q^^[END]^^\E # end operator block | ||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 0 | $self->tempeval($1, $2, $3, $4) | |||||||||||||||||||||||||||
| 85 | }gisex; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | $self->{ PageName } = '' unless $self->{ PageName }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $self->{ template } =~ s/%%PAGE%%/$self->{ PageName }/gi; | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 0 | my $ts = strftime ( "%A, %B %e, %G - %I:%M%p", localtime ); | |||||||||||||||||||||||||||
| 92 | 0 | $self->{ template } =~ s/%%DATETIME%%/$ts/gi; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 0 | ▶ | if ( $self->{ content } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $tmpcont = join ( "\n", @{ $self->{ content } } ); | |||||||||||||||||||||||||||
| 96 | 0 | $self->{ template } =~ s/%%CONTENT%%/$tmpcont/gi; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 0 | my $url = $query->url(); | |||||||||||||||||||||||||||
| 100 | 0 | $self->{ template } =~ s/%%MYURL%%/$url/gi; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 0 | return $self->{ template }; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | sub repl_tag { | ||||||||||||||||||||||||||
|
Subroutine
repl_tag: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 106 | # Find / replace all instances of a given tag with the output of a given coderef | ||||||||||||||||||||||||||||
| 107 | 0 | my ( $self, $var, $coderef ) = @_; | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # See how many find/replace operations will have to take place | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Evaluate the code once, and cache the output to save overhead | ||||||||||||||||||||||||||||
| 112 | 0 | my $output = $coderef->(); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # In the template, find $var (CaSE sEnsiTiVE!) and replace it with $output | ||||||||||||||||||||||||||||
| 115 | 0 | $self->{ template } =~ s/$var/$output/g; | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | return 1; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub tempeval { | ||||||||||||||||||||||||||
|
Subroutine
tempeval: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 121 | # Many thanks again to revdiablo for creating this initially as a stub | ||||||||||||||||||||||||||||
| 122 | # to show me how the hell his funky regex works ;-) | ||||||||||||||||||||||||||||
| 123 | 0 | my ( $self, $op, $exp, $true, $false ) = @_; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ( $op =~ /^IF$/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if ( $self->{heap}->{$exp} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 0 | return "<!-- $op ($exp => $self->{heap}->{$exp}) succeeded -->\n$true"; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | else { | ||||||||||||||||||||||||||||
| 130 | 0 | return "<!-- $op ($exp => $self->{heap}->{$exp}) failed -->\n$false"; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 1 | 1; | |||||||||||||||||||||||||||