* Created the new Templates module to handle reading and preparing templates.

* Added an index.html and favicon as well as a very basic 'home' cgi script which will allow the web development to start.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent b571fa043e
commit 0ee84cd31e
  1. 23
      AN/Tools.pm
  2. 236
      AN/Tools/Template.pm
  3. 6
      AN/tools.conf
  4. 18
      cgi-bin/home
  5. BIN
      html/favicon.ico
  6. 8
      html/index.html
  7. 38
      html/skins/alteeve/main.html

@ -35,6 +35,7 @@ use AN::Tools::Alert;
use AN::Tools::Get;
use AN::Tools::Log;
use AN::Tools::Storage;
use AN::Tools::Template;
use AN::Tools::Words;
use AN::Tools::Validate;
@ -105,6 +106,7 @@ sub new
GET => AN::Tools::Get->new(),
LOG => AN::Tools::Log->new(),
STORAGE => AN::Tools::Storage->new(),
TEMPLATE => AN::Tools::Template->new(),
WORDS => AN::Tools::Words->new(),
VALIDATE => AN::Tools::Validate->new(),
},
@ -129,6 +131,7 @@ sub new
$an->Get->parent($an);
$an->Log->parent($an);
$an->Storage->parent($an);
$an->Template->parent($an);
$an->Words->parent($an);
$an->Validate->parent($an);
@ -314,6 +317,18 @@ sub Storage
return ($self->{HANDLE}{STORAGE});
}
=head2 Template
Access the C<Template.pm> methods via 'C<< $an->Template->method >>'.
=cut
sub Template
{
my $self = shift;
return ($self->{HANDLE}{TEMPLATE});
}
=head2 Words
Access the C<Words.pm> methods via 'C<< $an->Words->method >>'.
@ -481,6 +496,9 @@ sub _set_defaults
server => "",
tag => "an-tools",
},
template => {
html => "alteeve",
},
};
return(0);
@ -503,6 +521,9 @@ sub _set_paths
hostname => "/bin/hostname",
logger => "/usr/bin/logger",
},
source => {
skins => "/var/www/html/skins",
},
words => {
'an-tools.xml' => "/usr/share/perl5/AN/an-tools.xml",
},
@ -533,7 +554,7 @@ sub _set_paths
}
}
}
}
};
return(0);
}

@ -0,0 +1,236 @@
package AN::Tools::Template;
#
# This module contains methods used to handle templates.
#
use strict;
use warnings;
use Data::Dumper;
our $VERSION = "3.0.0";
my $THIS_FILE = "Template.pm";
### Methods;
# get
=pod
=encoding utf8
=head1 NAME
AN::Tools::Template
Provides all methods related to template handling.
=head1 SYNOPSIS
use AN::Tools;
# Template a common object handle on all AN::Tools modules.
my $an = AN::Tools->new();
# Access to methods using '$an->Template->X'.
#
# Example using '()';
=head1 METHODS
Methods in this module;
=cut
sub new
{
my $class = shift;
my $self = {
SKIN => {
HTML => "",
},
};
bless $self, $class;
return ($self);
}
# Get a handle on the AN::Tools object. I know that technically that is a sibling module, but it makes more
# sense in this case to think of it as a parent.
sub parent
{
my $self = shift;
my $parent = shift;
$self->{HANDLE}{TOOLS} = $parent if $parent;
return ($self->{HANDLE}{TOOLS});
}
#############################################################################################################
# Public methods #
#############################################################################################################
=head2 get
This method takes a template file name and a template section name and returns that body template.
my $body = $an->Template->get({file => "foo.html", name => "bar"}))
=head2 Parameters;
=head3 file (required)
This is the name of the template file containing the template section you want to read.
=head3 name (required)
This is the name of the template section, bounded by 'C<< <!-- start foo --> >>' and 'C<< <!-- end food --> >>' to read in from the file.
=head3 skin (optional)
By default, the
=cut
sub get
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $name = defined $parameter->{name} ? $parameter->{name} : "";
my $skin = defined $parameter->{skin} ? $parameter->{skin} : "";
my $template = "";
my $source = "";
my $error = 0;
if (not $file)
{
print $THIS_FILE." ".__LINE__."; [ Error ] - No template file passed to Template->get().\n";
$error = 1;
}
else
{
# Make sure the file exists.
if ($skin)
{
$source = $an->data->{path}{source}{skins}."/".$skin."/".$file;
print $THIS_FILE." ".__LINE__."; [ Debug ] - source: [$source]\n";
}
else
{
$source = $an->Template->skin."/".$file;
print $THIS_FILE." ".__LINE__."; [ Debug ] - source: [$source]\n";
}
if (not -e $source)
{
print $THIS_FILE." ".__LINE__."; [ Error ] - No requested template file: [".$source."] does not exist. Is it missing in the active skin?\n";
$error = 1;
}
elsif (not -r $source)
{
my $user_name = getpwuid($<);
$user_name = $< if not $user_name;
print $THIS_FILE." ".__LINE__."; [ Error ] - The requested template file: [".$source."] is not readable. Please check that it is readable by the webserver user: [$user_name]\n";
$error = 1;
}
}
if (not $name)
{
print $THIS_FILE." ".__LINE__."; [ Error ] - No template file passed to Template->get().\n";
$error = 1;
}
if (not $error)
{
my $in_template = 0;
my $shell_call = $source;
open(my $file_handle, "<$shell_call") or warn $THIS_FILE." ".__LINE__."; Failed to read: [$shell_call], error was: $!\n";
while(<$file_handle>)
{
chomp;
my $line = $_;
if ($line =~ /^<!-- start $name -->/)
{
$in_template = 1;
}
if ($in_template)
{
if ($line =~ /^<!-- end $name -->/)
{
$in_template = 0;
last;
}
else
{
$template .= $line."\n";
}
}
}
close $file_handle;
}
return($template);
}
=head2 skin
This sets or returns the active skin used when rendering web output.
The default skin is set via 'C<< defaults::template::html >>' and it must be the same as the directory name under 'C<< /var/www/html/skins/ >>'.
Get the active skin;
my $skin = $an->Template->skin;
Set the active skin to 'C<< foo >>'.
$an->Template->skin({set => "foo"});
Disable sensitive log entry recording.
$an->Log->secure(0);
=cut
sub skin
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $set = defined $parameter->{skin} ? $parameter->{skin} : "";
if ($set)
{
my $skin_directory = $an->data->{path}{source}{skins}."/".$set;
if (-d $skin_directory)
{
$self->{SKIN}{HTML} = $skin_directory
}
else
{
print $THIS_FILE." ".__LINE__."; [ Warning ] - Asked to set the skin: [$set], but the source directory: [$skin_directory] doesn't exist. Ignoring.\n";
}
}
if (not $self->{SKIN}{HTML})
{
$self->{SKIN}{HTML} = $an->data->{path}{source}{skins}."/".$an->data->{defaults}{template}{html};
}
return($self->{SKIN}{HTML});
}
# =head3
#
# Private Functions;
#
# =cut
#############################################################################################################
# Private functions #
#############################################################################################################
1;

@ -33,3 +33,9 @@
# This sets the default log tag used when logging an entry. Most programs will likely override this.
#defaults::log::tag = an-tools
### Templates
# This sets the default template used when rendering HTML pages. It must be the same as the directory name
# under /var/www/html/skins/
#defaults::template::html = alteeve

@ -0,0 +1,18 @@
#!/usr/bin/perl
#
use strict;
use warnings;
use AN::Tools;
my $an = AN::Tools->new();
my $THIS_FILE = "home";
# Turn off buffering so that the pinwheel will display while waiting for the SSH call(s) to complete.
$| = 1;
print "Content-type: text/html; charset=utf-8\n\n";
print "hello.\n";
exit(0);

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@ -0,0 +1,8 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Striker</title>
<META HTTP-EQUIV="Refresh" CONTENT="0; URL=cgi-bin/home">
</head>
<body>
Hi, I'll be right with you...
</body>

@ -0,0 +1,38 @@
<!-- start master -->
#!replace!header!#
<table>
<tr>
<td>
#!replace!left_top_bar!#
</td>
<td>
#!replace!center_top_bar!#
</td>
<td>
#!replace!right_top_bar!#
</td>
</tr>
<tr>
<td colspan="3">
#!replace!center_body!#
</td>
</tr>
<tr>
<td>
#!replace!left_bottom_bar!#
</td>
<td>
#!replace!center_bottom_bar!#
</td>
<td>
#!replace!right_bottom_bar!#
</td>
</tr>
</table>
#!replace!footer!#
<!-- end master -->
<!-- start button_bar -->
<!--end button_bar -->
Loading…
Cancel
Save