File Coverage

blib/lib/WWW/CMS.pm
Criterion Covered Total %
statement 9 56 16.0
branch 0 12 0.0
condition 0 10 0.0
subroutine 3 9 33.3
pod 0 4 0.0
total 12 91 13.1


line stmt bran cond sub pod time code
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     1   26310 use warnings;
  1         2  
  1         37  
8 1     1   5 use strict;
  1         2  
  1         126  
9 1     1   1041 use POSIX qw( strftime );
  1         9060  
  1         10  
10              
11             sub new {
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     0 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 0 0       if ( !$args->{ TemplateBase } || !-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 0 0       if ( !$args->{ Module } || !-f "$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             open my $fh, "<", "$args->{ TemplateBase }/$args->{ Module }" || sub {
39 0     0     $self->{ ERRMSG } = "Failed to open template module '$args->{ TemplateBase }/$args->{ Module }': $!";
40 0           return;
41 0   0       };
42              
43             # Slurpy, slurpy....
44 0           $self->{ template } = do { local $/; <$fh> };
  0            
  0            
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             sub publicize {
61             # Find / replace all instances of built-in tags with output of respective code
62 0     0 0   my $self = shift;
63            
64             # Pass in an existing CGI object
65             my $query = shift || sub {
66 0     0     $self->{ ERRSTR } = 'CGI object not passed into publicize()';
67 0           return;
68 0   0       };
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 0         $self->{ PageName } = '' unless $self->{ PageName };
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 0         if ( $self->{ content } ) {
95 0           my $tmpcont = join ( "\n", @{ $self->{ content } } );
  0            
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             sub repl_tag {
106             # Find / replace all instances of a given tag with the output of a given coderef
107 0     0 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             sub tempeval {
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     0 0   my ( $self, $op, $exp, $true, $false ) = @_;
124              
125 0 0         if ( $op =~ /^IF$/i ) {
126 0 0         if ( $self->{heap}->{$exp} ) {
127 0           return "\n$true";
128             }
129             else {
130 0           return "\n$false";
131             }
132             }
133             }
134              
135             1;