* Updated Tools->new() to check if it is being invoked in a browser and set the environment appropriately. It also sets the PWD environmental variable to the DOCUMENT_ROOT variable.

* Renamed 'defaults::languages' to 'defaults::language'.
* Created Storage->change_mode(), Storage->change_owner(), Storage->make_directory() and Storage->write_file() that does what their names imply, using the shell commands instead of the built-in commands (may switch to them later). The ->make_directory() method will create any needed parent directories.
* Moved System->read_file() to the Storage module as it made more sense there. For now, System is empty.
* Created Words->language to set or check the active output language.
* Fixed Template->get() to use Words->language.
* Updated Words->string() to take the new 'string' parameter which, when set, is used instead of the 'key' parameter and is treated as a pre-retrieved string, so replacement keys are directly injected.
* Updated Template->get() to take the 'variables' parameter and then uses Words->string({string => ...}) to process the replacement keys.
* Updated tools/scancore-update-states to write out the network interface states to the status.xml file.

Signed-off-by: Digimer <digimer@alteeve.ca>
main
Digimer 8 years ago
parent f93f8a10e5
commit bea6bc5e69
  1. 21
      AN/Tools.pm
  2. 498
      AN/Tools/Storage.pm
  3. 69
      AN/Tools/System.pm
  4. 98
      AN/Tools/Template.pm
  5. 108
      AN/Tools/Words.pm
  6. 30
      AN/an-tools.xml
  7. 27
      cgi-bin/home
  8. 3
      html/skins/alteeve/main.html
  9. 0
      html/skins/alteeve/main.js
  10. 42
      tools/scancore-update-states

@ -142,8 +142,18 @@ sub new
$an->_set_paths;
$an->_set_defaults;
# This checks the environment this program is running in.
$an->environment;
# This sets the environment this program is running in.
if ($ENV{SERVER_NAME})
{
$an->environment("html");
# There is no PWD environment variable, so we'll use 'DOCUMENT_ROOT' as 'PWD'
$ENV{PWD} = $ENV{DOCUMENT_ROOT};
}
else
{
$an->environment("cli");
}
# Setup my '$an->data' hash right away so that I have a place to store the strings hash.
$an->data($parameter->{data}) if $parameter->{data};
@ -493,7 +503,7 @@ sub _set_defaults
my ($an) = shift;
$an->data->{defaults} = {
languages => {
language => {
# Default language for all output shown to a user.
output => 'en_CA',
},
@ -531,15 +541,20 @@ sub _set_paths
# Executables
$an->data->{path} = {
directories => {
'cgi-bin' => "/var/www/cgi-bin",
html => "/var/www/html",
skins => "/var/www/html/skins",
tools => "/usr/sbin/striker",
units => "/usr/lib/systemd/system",
},
exe => {
'chmod' => "/usr/bin/chmod",
'chown' => "/usr/bin/chown",
dmidecode => "/usr/sbin/dmidecode",
gethostip => "/usr/bin/gethostip",
hostname => "/bin/hostname",
logger => "/usr/bin/logger",
'mkdir' => "/usr/bin/mkdir",
},
sysfs => {
network_interfaces => "/sys/class/net",

@ -11,9 +11,14 @@ our $VERSION = "3.0.0";
my $THIS_FILE = "Storage.pm";
### Methods;
# change_mode
# change_owner
# find
# make_directory
# read_config
# read_file
# search_directories
# write_file
=pod
@ -72,6 +77,144 @@ sub parent
#############################################################################################################
=head2 change_mode
This changes the mode of a file or directory.
$an->Storage->change_mode({target => "/tmp/foo", mode => "0644"});
If it fails to write the file, an alert will be logged.
Parameters;
=head3 target (required)
This is the file or directory to change the mode on.
=head3 mode (required)
This is the numeric mode to set on the file. It expects four digits to cover the sticky bit, but will work with three digits.
=cut
sub change_mode
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $target = defined $parameter->{target} ? $parameter->{target} : "";
my $mode = defined $parameter->{mode} ? $parameter->{mode} : "";
my $error = 0;
if (not $target)
{
# No target...
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0036"});
$error = 1;
}
if (not $mode)
{
# No mode...
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0037"});
$error = 1;
}
elsif (($mode !~ /^\d\d\d$/) && ($mode !~ /^\d\d\d\d$/))
{
# Invalid mode
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0038", variables => { mode => $mode }});
$error = 1;
}
if (not $error)
{
my $shell_call = $an->data->{path}{exe}{'chmod'}." $mode $target";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0011", variables => { shell_call => $shell_call }});
open (my $file_handle, $shell_call." 2>&1 |") or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0017", variables => { line => $line }});
}
close $file_handle;
}
return(0);
}
=head2 change_owner
This changes the owner and/or group of a file or directory.
$an->Storage->change_owner({target => "/tmp/foo", mode => "0644"});
If it fails to write the file, an alert will be logged.
Parameters;
=head3 target (required)
This is the file or directory to change the mode on.
=head3 group (optional)
This is the group name or UID to set the target to.
=head3 user (optional)
This is the user name or UID to set the target to.
=cut
sub change_owner
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $target = defined $parameter->{target} ? $parameter->{target} : "";
my $group = defined $parameter->{group} ? $parameter->{group} : "";
my $user = defined $parameter->{user} ? $parameter->{user} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
target => $target,
group => $group,
user => $user,
}});
my $string = "";
my $error = 0;
if (not $target)
{
# No target...
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0039"});
$error = 1;
}
if ($user)
{
$string = $user;
}
if ($group)
{
$string .= ":".$group;
}
if ((not $error) && ($string))
{
my $shell_call = $an->data->{path}{exe}{'chown'}." $string $target";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0011", variables => { shell_call => $shell_call }});
open (my $file_handle, $shell_call." 2>&1 |") or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0017", variables => { line => $line }});
}
close $file_handle;
}
return(0);
}
=head2 find
This searches for the given file on the system. It will search in the directories returned by C<< $an->Storage->search_directories() >>.
@ -91,10 +234,6 @@ If it fails to find the file and C<< fatal >> isn't set to 'C<< 1 >>', 'C<< 0 >>
Parameters;
=head3 fatal (optional)
This can be set to 'C<< 1 >>' to tell the method to throw an error and exit if the file is not found. Default is 'C<< 0 >>' which only triggers a warning of the file isn't found.
=head3 file (required)
This is the name of the file to search for.
@ -107,61 +246,128 @@ sub find
my $an = $self->parent;
# Setup default values
my $fatal = defined $parameter->{fatal} ? $parameter->{fatal} : 0;
my $file = defined $parameter->{file} ? $parameter->{file} : "";
# Each full path and file name will be stored here before the test.
my $full_path = "#!not_found!#";
foreach my $directory (@{$an->Storage->search_directories()})
if ($file)
{
# If "directory" is ".", expand it.
if (($directory eq ".") && ($ENV{PWD}))
foreach my $directory (@{$an->Storage->search_directories()})
{
$directory = $ENV{PWD};
}
# Put together the initial path
my $test_path = $directory."/".$file;
# If "directory" is ".", expand it.
if (($directory eq ".") && ($ENV{PWD}))
{
$directory = $ENV{PWD};
}
# Put together the initial path
my $test_path = $directory."/".$file;
# Clear double-delimiters.
$test_path =~ s/\/+/\//g;
#print $THIS_FILE." ".__LINE__."; [ Debug ] - Test path: [$test_path] - ";
if (-f $test_path)
{
# Found it!
#print "Found!\n";
$full_path = $test_path;
last;
# Clear double-delimiters.
$test_path =~ s/\/+/\//g;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { test_path => $test_path }});
if (-f $test_path)
{
# Found it!
$full_path = $test_path;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 1, list => { full_path => $full_path }});
last;
}
}
else
# Log if we failed to find the path.
if ($full_path !~ /^\//)
{
#print "Not found...\n";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0029", variables => { file => $file }});
}
}
else
{
# No file name passed in.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0030"});
}
# Die if we didn't find the file and fatal is set.
if ($full_path !~ /^\//)
# Return
return ($full_path);
}
=head2 make_directory
This creates a directory (and any parent directories).
$an->Storage->make_directory({directory => "/foo/bar/baz", owner => "me", grou[ => "me", group => 755});
If it fails to create the directory, an alert will be logged.
Parameters;
=head3 directory (required)
This is the name of the directory to create.
=head3 group (optional)
This is the group name or group ID to set the ownership of the directory to.
=head3 mode (optional)
This is the numeric mode to set on the file. It expects four digits to cover the sticky bit, but will work with three digits.
=head3 user (optional)
This is the user name or user ID to set the ownership of the directory to.
=cut
sub make_directory
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $directory = defined $parameter->{directory} ? $parameter->{directory} : "";
my $group = defined $parameter->{group} ? $parameter->{group} : "";
my $mode = defined $parameter->{mode} ? $parameter->{mode} : "";
my $user = defined $parameter->{user} ? $parameter->{user} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
directory => $directory,
group => $group,
mode => $mode,
user => $user,
}});
# Break the directories apart.
my $working_directory = "";
foreach my $directory (split, /\//, $directory)
{
if ($fatal)
next if not $directory;
$working_directory .= "/$directory";
if (-e $working_directory)
{
### TODO: Make this $an->Alert->error() later
print $THIS_FILE." ".__LINE__."; [ Error ] - Failed to find: [$file].\n";
}
else
{
### TODO: Make this $an->Alert->warning() later
print $THIS_FILE." ".__LINE__."; [ Warning ] - Failed to find: [$file].\n";
}
if ($fatal)
{
print "Exiting on errors.\n";
exit(2);
# Directory doesn't exist, so create it.
my $shell_call = $an->data->{path}{exe}{'mkdir'}." ".$working_directory;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0011", variables => { shell_call => $shell_call }});
open (my $file_handle, $shell_call." 2>&1 |") or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0014", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0017", variables => { line => $line }});
}
close $file_handle;
if ($mode)
{
$an->Storage->change_mode({target => $working_directory, mode => $mode});
}
if (($user) or ($group))
{
$an->Storage->change_owner({target => $working_directory, user => $user, group => $group});
}
}
}
# Return
return ($full_path);
return(0);
}
=head2 read_config
@ -217,12 +423,12 @@ sub read_config
# Setup default values
my $file = defined $parameter->{file} ? $parameter->{file} : 0;
my $return_code = 0;
#print $THIS_FILE." ".__LINE__."; [ Debug ] - file: [$file].\n";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { file => $file }});
if (not $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' called without a file name to read.\n";
# No file to read
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0032"});
$return_code = 1;
}
@ -232,12 +438,12 @@ sub read_config
# Find the file, if possible. If not found, we'll not alter what the user passed in and hope
# it is relative to where we are.
my $path = $an->Storage->find({ file => $file });
#print $THIS_FILE." ".__LINE__."; [ Debug ] - path: [$path].\n";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { path => $path }});
if ($path ne "#!not_found!#")
{
# Update the file
$file = $path;
#print $THIS_FILE." ".__LINE__."; [ Debug ] - file: [$file].\n";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { file => $file }});
}
}
@ -245,14 +451,18 @@ sub read_config
{
if (not -e $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' asked to read: [$file] which was not found.\n";
# The file doesn't exist
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0033", variables => { file => $file }});
$return_code = 2;
}
elsif (not -r $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' asked to read: [$file] which was not readable by: [".getpwuid($<)."/".getpwuid($>)."] (uid/euid: [".$<."/".$>."]).\n";
# The file can't be read
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0034", variables => {
file => $file,
user => getpwuid($<),
uid => $<,
}});
$return_code = 3;
}
else
@ -274,7 +484,11 @@ sub read_config
$value =~ s/^\s+//;
if (not $variable)
{
print $THIS_FILE." ".__LINE__."; [ Warning ] - The config file: [$file] appears to have a malformed line: [$count:$line].\n";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0035", variables => {
file => $file,
count => $count,
line => $line,
}});
}
$an->_make_hash_reference($an->data, $variable, $value);
@ -286,6 +500,64 @@ sub read_config
return($return_code);
}
=head2 read_file
This reads in a file and returns the contents of the file as a single string variable.
$an->Storage->read_file({file => "/tmp/foo"});
If it fails to find the file, or the file is not readable, 'C<< undef >>' is returned.
Parameters;
=head3 file (required)
This is the name of the file to read.
=cut
sub read_file
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $body = "";
my $file = defined $parameter->{file} ? $parameter->{file} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { file => $file }});
if (not $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020"});
return(undef);
}
elsif (not -e $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0021", variables => { file => $file }});
return(undef);
}
elsif (not -r $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0022", variables => { file => $file }});
return(undef);
}
my $shell_call = $file;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0012", variables => { shell_call => $shell_call }});
open (my $file_handle, "<", $shell_call) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0015", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0023", variables => { line => $line }});
$body .= $line."\n";
}
close $file_handle;
$body =~ s/\n$//s;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { body => $body }});
return($body);
}
=head2 search_directories
This method returns an array reference of directories to search within for files and directories.
@ -326,8 +598,8 @@ sub search_directories
{
if (not $initialize)
{
# TODO: Make this a $an->Alert->warning().
print $THIS_FILE." ".__LINE__."; [ Warning ] - The passed in array: [$array] wasn't actually an array. Using \@INC + \$ENV{'PATH'} for the list of directories to search instead.\n";
# Not initializing and an array was passed that isn't.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0031", variables => { array => $array }});
}
# Create a new array containing the '$ENV{'PATH'}' directories and the @INC directories.
@ -373,9 +645,127 @@ sub search_directories
$self->{SEARCH_DIRECTORIES} = $array;
}
# Debug
foreach my $directory (@{$self->{SEARCH_DIRECTORIES}})
{
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { directory => $directory }});
}
return ($self->{SEARCH_DIRECTORIES});
}
=head2 write_file
This writes out a file on the local system. It can optionally set the mode as well.
$an->Storage->write_file({file => "/tmp/foo", body => "some data", mode => 0644});
If it fails to write the file, an alert will be logged.
Parameters;
=head3 body (optional)
This is the contents of the file. If it is blank, an empty file will be created (similar to using 'C<< touch >>' on the command line).
=head3 file (required)
This is the name of the file to write.
NOTE: The file must include the full directory it will be written into.
=head3 group (optional)
This is the group name or group ID to set the ownership of the file to.
=head3 mode (optional)
This is the numeric mode to set on the file. It expects four digits to cover the sticky bit, but will work with three digits.
=head3 overwrite (optional)
Normally, if the file already exists, it won't be overwritten. Setting this to 'C<< 1 >>' will cause the file to be overwritten.
=head3 user (optional)
This is the user name or user ID to set the ownership of the file to.
=cut
sub write_file
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $body = defined $parameter->{body} ? $parameter->{body} : "";
my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $group = defined $parameter->{group} ? $parameter->{group} : "";
my $mode = defined $parameter->{mode} ? $parameter->{mode} : "";
my $overwrite = defined $parameter->{overwrite} ? $parameter->{overwrite} : 0;
my $user = defined $parameter->{user} ? $parameter->{user} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
body => $body,
file => $file,
group => $group,
mode => $mode,
overwrite => $overwrite,
user => $user,
}});
my $error = 0;
if ((-e $file) && (not $overwrite))
{
# Nope.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0040", variables => { file => $file }});
$error = 1;
}
if ($file !~ /^\/\w/)
{
# Not a fully defined path, abort.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0041", variables => { file => $file }});
$error = 1;
}
if (not $error)
{
# Break the directory off the file.
my ($directory, $file_name) = ($file =~ /^(\/.*)\/(.*)$/);
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
directory => $directory,
file_name => $file_name,
}});
if (not -d $directory)
{
$an->Storage->make_directory({
directory => $directory,
group => $group,
mode => $mode,
user => $user,
});
}
# Now write the file.
my $shell_call = $file;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0013", variables => { shell_call => $shell_call }});
open (my $file_handle, ">", $shell_call) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0016", variables => { shell_call => $shell_call, error => $! }});
print $file_handle $body;
close $file_handle;
if ($mode)
{
$an->Storage->change_mode({target => $file, mode => $mode});
}
if (($user) or ($group))
{
$an->Storage->change_owner({target => $file, user => $user, group => $group});
}
}
return(0);
}
# =head3
#

@ -11,7 +11,7 @@ our $VERSION = "3.0.0";
my $THIS_FILE = "System.pm";
### Methods;
# read_file
#
=pod
@ -32,8 +32,8 @@ Provides all methods related to storage on a system.
# Access to methods using '$an->System->X'.
#
# Example using 'read_file()';
my $data = $an->System->read_file({file => "/tmp/foo"});
# Example using '...()';
my $data = $an->System->...({file => "/tmp/foo"});
=head1 METHODS
@ -68,59 +68,14 @@ sub parent
#############################################################################################################
=head2 read_file
This reads in a file and returns the contents of the file as a single string variable.
$an->System->read_file({file => "/tmp/foo"});
If it fails to find the file, or the file is not readable, 'C<< undef >>' is returned.
Parameters;
=head3 file (required)
# =head3
#
# Private Functions;
#
# =cut
This is the name of the file to read.
#############################################################################################################
# Private functions #
#############################################################################################################
=cut
sub read_file
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $body = "";
my $file = defined $parameter->{file} ? $parameter->{file} : "";
if (not $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0020"});
return(undef);
}
elsif (not -e $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0021", variables => { file => $file }});
return(undef);
}
elsif (not -r $file)
{
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0022", variables => { file => $file }});
return(undef);
}
my $shell_call = $file;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0012", variables => { shell_call => $shell_call }});
open (my $file_handle, "<", $shell_call) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0015", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0023", variables => { line => $line }});
$body .= $line."\n";
}
close $file_handle;
$body =~ s/\n$//s;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { body => $body }});
return($body);
}
1;

@ -83,13 +83,23 @@ This method takes a template file name and a template section name and returns t
This is the name of the template file containing the template section you want to read.
=head3 language (optional)
This is the language (iso code) to use when inserting strings into the template. When not specified, 'C<< Words->language >>' is used.
=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
By default, the active skin is set by 'C<< defaults::template::html >>' ('C<< alteeve >>' by default). This can be checked or set using 'C<< Template->skin >>'.
This parameter allows for an override to use another skin.
=head3 variables (optional)
If there are variables to inject into the template, pass them as a hash referencce using this paramter.
=cut
sub get
@ -98,62 +108,81 @@ sub get
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 $file = defined $parameter->{file} ? $parameter->{file} : "";
my $language = defined $parameter->{language} ? $parameter->{language} : $an->Words->language;
my $name = defined $parameter->{name} ? $parameter->{name} : "";
my $skin = defined $parameter->{skin} ? $parameter->{skin} : "";
my $variables = defined $parameter->{variables} ? $parameter->{variables} : "";
my $template = "";
my $source = "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
file => $file,
language => $language,
name => $name,
skin => $skin,
}});
# If the user passed the skin, prepend the skins directory. Otherwise use the active skin.
if (not $skin)
{
$skin = $an->Template->skin;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { skin => $skin }});
}
else
{
$skin = $an->data->{path}{directories}{skins}."/".$skin;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { skin => $skin }});
}
my $error = 0;
if (not $file)
{
print $THIS_FILE." ".__LINE__."; [ Error ] - No template file passed to Template->get().\n";
# No file passed.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0024"});
$error = 1;
}
else
{
# Make sure the file exists.
if ($skin)
{
$source = $an->data->{path}{directories}{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";
}
$source = $skin."/".$file;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { source => $source }});
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";
# Source doesn't exist
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0025", variables => { source => $source }});
$error = 1;
}
elsif (not -r $source)
{
# Source isn't readable.
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";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0026", variables => { source => $source, user_name => $user_name }});
$error = 1;
}
}
if (not $name)
{
print $THIS_FILE." ".__LINE__."; [ Error ] - No template file passed to Template->get().\n";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0027"});
$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";
my $shell_call = $source;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0012", variables => { shell_call => $shell_call }});
open (my $file_handle, "<", $shell_call) or $an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", key => "log_0015", variables => { shell_call => $shell_call, error => $! }});
while(<$file_handle>)
{
chomp;
my $line = $_;
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0023", variables => { line => $line }});
if ($line =~ /^<!-- start $name -->/)
{
$in_template = 1;
next;
}
if ($in_template)
{
@ -169,6 +198,14 @@ sub get
}
}
close $file_handle;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { source => $source }});
# Now that I have the skin, inject my variables. We'll use Words->string() to do this for us.
$template = $an->Words->string({
string => $template,
variables => $variables,
});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { source => $source }});
}
return($template);
@ -177,22 +214,18 @@ sub get
=head2 skin
This sets or returns the active skin used when rendering web output.
This sets or returns the active skin used when rendering web output. The returned string is the full path to the skin directory, including the active skin name.
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;
Get the active skin directory;
my $skin = $an->Template->skin;
Set the active skin to 'C<< foo >>'.
Set the active skin to 'C<< foo >>'. Only pass the skin name, not the full path.
$an->Template->skin({set => "foo"});
Disable sensitive log entry recording.
$an->Log->secure(0);
=cut
sub skin
{
@ -201,17 +234,20 @@ sub skin
my $an = $self->parent;
my $set = defined $parameter->{skin} ? $parameter->{skin} : "";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { file => $set }});
if ($set)
{
my $skin_directory = $an->data->{path}{directories}{skins}."/".$set;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { skin_directory => $skin_directory }});
if (-d $skin_directory)
{
$self->{SKIN}{HTML} = $skin_directory
$self->{SKIN}{HTML} = $skin_directory;
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { 'SKIN::HTML' => $self->{SKIN}{HTML} }});
}
else
{
print $THIS_FILE." ".__LINE__."; [ Warning ] - Asked to set the skin: [$set], but the source directory: [$skin_directory] doesn't exist. Ignoring.\n";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "alert", key => "log_0031", variables => { set => $set, skin_directory => $skin_directory }});
}
}

@ -18,6 +18,7 @@ my $THIS_FILE = "Words.pm";
### Methods;
# clean_spaces
# key
# language
# read
# string
@ -51,7 +52,11 @@ Methods in this module;
sub new
{
my $class = shift;
my $self = {};
my $self = {
WORDS => {
LANGUAGE => "",
},
};
bless $self, $class;
@ -146,7 +151,7 @@ This is the key to return the string for.
This is the ISO code for the language you wish to read. For example, 'en_CA' to get the Canadian English string, or 'jp' for the Japanese string.
When no language is passed, 'C<< $an->data->{defaults}{languages}{output} >>' is used.
When no language is passed, 'C<< Words->language >>' is used.
=cut
sub key
@ -157,7 +162,7 @@ sub key
# Setup default values
my $key = defined $parameter->{key} ? $parameter->{key} : "";
my $language = defined $parameter->{language} ? $parameter->{language} : $an->data->{defaults}{languages}{output};
my $language = defined $parameter->{language} ? $parameter->{language} : $an->Words->language;
my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $string = "#!not_found!#";
my $error = 0;
@ -194,6 +199,40 @@ sub key
return($string);
}
=head2 language
This sets or returns the output language ISO code.
Get the current log language;
my $language = $an->Words->language;
Set the output langauge to Japanese;
$an->Words->language({set => "jp"});
=cut
sub language
{
my $self = shift;
my $parameter = shift;
my $an = $self->parent;
my $set = defined $parameter->{set} ? $parameter->{set} : "";
if ($set)
{
$self->{WORDS}{LANGUAGE} = $set;
}
if (not $self->{WORDS}{LANGUAGE})
{
$self->{WORDS}{LANGUAGE} = $an->data->{defaults}{language}{output};
}
return($self->{WORDS}{LANGUAGE});
}
=head2 read
This reads in a words file containing translated strings used to generated output for the user.
@ -231,20 +270,20 @@ sub read
if (not $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' called without a file name to read.\n";
# NOTE: Log the problem, do not translate.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' called without a file name to read."});
$return_code = 1;
}
elsif (not -e $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' asked to read: [$file] which was not found.\n";
# NOTE: Log the problem, do not translate.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' asked to read: [$file] which was not found."});
$return_code = 2;
}
elsif (not -r $file)
{
# TODO: Log the problem, do not translate.
print $THIS_FILE." ".__LINE__."; [ Warning ] - AN::Tools::Words->read()' asked to read: [$file] which was not readable by: [".getpwuid($<)."/".getpwuid($>)."] (uid/euid: [".$<."/".$>."]).\n";
# NOTE: Log the problem, do not translate.
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => "[ Error ] - Words->read()' asked to read: [$file] which was not readable by: [".getpwuid($<)."/".getpwuid($>)."] (uid/euid: [".$<."/".$>."])."});
$return_code = 3;
}
else
@ -255,28 +294,16 @@ sub read
if ($@)
{
chomp $@;
print $THIS_FILE." ".__LINE__."; [ Error ] - The was a problem reading: [$file]. The error was:\n";
print "===========================================================\n";
print $@."\n";
print "===========================================================\n";
my $error = "[ Error ] - The was a problem reading: [$file]. The error was:\n";
$error .= "===========================================================\n";
$error .= $@."\n";
$error .= "===========================================================\n";
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 0, priority => "err", raw => $error});
$return_code = 4;
}
else
{
# Successfully read.
### Some debug stuff
# Read the meta data
#my $version = $an->data->{words}{$file}{meta}{version};
#my $languages = $an->data->{words}{$file}{meta}{languages};
#print $THIS_FILE." ".__LINE__."; [ Debug ] - Version: [$version], languages: [$languages]\n";
#foreach my $this_language (sort {$a cmp $b} keys %{$an->data->{words}{$file}{language}})
#{
# my $long_name = $an->data->{words}{$file}{language}{$this_language}{long_name};
# print $THIS_FILE." ".__LINE__."; [ Debug ] - this_language: [$this_language], long_name: [$long_name]\n";
# print $THIS_FILE." ".__LINE__."; [ Debug ] - "$this_language:t_0001: [".$an->data->{words}{$file}{language}{$this_language}{key}{t_0001}{content}."]\n";
#}
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0028", variables => { file => $file }});
}
}
@ -336,12 +363,18 @@ This is the specific file to read the string from. It should generally not be ne
This is the key to return the string for.
NOTE: This is ignored when 'C<< string >>' is used.
=head3 language (optional)
This is the ISO code for the language you wish to read the string from. For example, 'en_CA' to get the Canadian English string, or 'jp' for the Japanese string.
When no language is passed, 'C<< defaults::languages::output >>' is used.
=head3 string (optional)
If this is passed, it is treated as a raw string that needs variables inserted. When this is used, the 'C<< key >>' parameter is ignored.
=head3 variables (depends)
If the string being requested has one or more 'C<< #!variable!x!# >>' replacement keys, then you must pass a hash reference containing the keys / value pairs where the key matches the replacement string.
@ -355,17 +388,22 @@ sub string
# Setup default values
my $key = defined $parameter->{key} ? $parameter->{key} : "";
my $language = defined $parameter->{language} ? $parameter->{language} : $an->data->{defaults}{languages}{output};
my $language = defined $parameter->{language} ? $parameter->{language} : $an->Words->language;
my $file = defined $parameter->{file} ? $parameter->{file} : "";
my $string = defined $parameter->{string} ? $parameter->{string} : "";
my $variables = defined $parameter->{variables} ? $parameter->{variables} : "";
# We'll get the string from our ->key() method, the inject any variables, if needed. This also
# handles the initial sanity checks. If we get back '#!not_found!#', we'll exit.
my $string = $an->Words->key({
key => $key,
language => $language,
file => $file,
});
# If we weren't passed a raw string, we'll get the string from our ->key() method, the inject any
# variables, if needed. This also handles the initial sanity checks. If we get back '#!not_found!#',
# we'll exit.
if (not $string)
{
$string = $an->Words->key({
key => $key,
language => $language,
file => $file,
});
}
if (($string ne "#!not_found!#") && ($string =~ /#!([^\s]+?)!#/))
{

@ -33,16 +33,34 @@ It also has replacement variables: [#!variable!first!#] and [#!variable!second!#
<key name="log_0011">About to run the shell command: [#!variable!shell_call!#]</key>
<key name="log_0012">About to read the file: [#!variable!shell_call!#]</key>
<key name="log_0013">About to write the file: [#!variable!shell_call!#]</key>
<key name="log_0014">There was a problem running the shell command: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0015">There was a problem reading the file: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0016">There was a problem writing the file: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0014">[ Error ] - There was a problem running the shell command: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0015">[ Error ] - There was a problem reading the file: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0016">[ Error ] - There was a problem writing the file: [#!variable!shell_call!#]. The error was: [#!variable!error!#].</key>
<key name="log_0017">Output: [#!variable!line!#].</key>
<key name="log_0018">About to open the directory: [#!variable!directory!#]</key>
<key name="log_0019">Variables:</key>
<key name="log_0020"><![CDATA[The module System->read_file() was called without a 'file' parameter, or the parameter was empty.]]></key>
<key name="log_0021"><![CDATA[The module System->read_file() was asked to read the file: [#!variable!file!#], but that file does not exist.]]></key>
<key name="log_0022"><![CDATA[The module System->read_file() was asked to read the file: [#!variable!file!#] which exists but can't be read.]]></key>
<key name="log_0020"><![CDATA[[ Error ] - The module Storage->read_file() was called without a 'file' parameter, or the parameter was empty.]]></key>
<key name="log_0021"><![CDATA[[ Error ] - The module Storage->read_file() was asked to read the file: [#!variable!file!#], but that file does not exist.]]></key>
<key name="log_0022"><![CDATA[[ Error ] - The module Storage->read_file() was asked to read the file: [#!variable!file!#] which exists but can't be read.]]></key>
<key name="log_0023">Reading: [#!variable!line!#].</key>
<key name="log_0024"><![CDATA[[ Error ] - No template file passed to Template->get().]]></key>
<key name="log_0025"><![CDATA[[ Error ] - No requested template file: [#!variable!source!#] does not exist. Is it missing in the active skin?]]></key>
<key name="log_0026"><![CDATA[[ Error ] - The requested template file: [#!variable!source!#] is not readable. Please check that it is readable by the webserver user: [#!variable!user_name!#]]]></key>
<key name="log_0027"><![CDATA[[ Error ] - No template name passed to Template->get().]]></key>
<key name="log_0028">Successfully read the words file: [#!variable!file!#].</key>
<key name="log_0029"><![CDATA[[ Error ] - Storage->find() failed to find: [#!variable!file!#].]]></key>
<key name="log_0030"><![CDATA[[ Warning ] - Template->skin() was asked to set the skin: [#!variable!set!#], but the source directory: [#!variable!skin_directory!#] doesn't exist. Ignoring.]]></key>
<key name="log_0031"><![CDATA[[ Warning ] - Storage->search_directories() was passed the array: [#!variable!array!#], but it wasn't actually an array. Using \@INC + path::directories::tools + \$ENV{'PATH'} for the list of directories to search instead.]]></key>
<key name="log_0032"><![CDATA[[ Warning ] - Words->read()' called without a file name to read.]]></key>
<key name="log_0033"><![CDATA[[ Warning ] - Words->read()' asked to read: [#!variable!file!#] which was not found.]]></key>
<key name="log_0034"><![CDATA[[ Warning ] - AN::Tools::Words->read()' asked to read: [#!variable!file!#] which was not readable by: [#!variable!user!#] (uid/euid: [#!variable!uid!#]).]]></key>
<key name="log_0035"><![CDATA[[ Warning ] - The config file: [#!variable!file!#] appears to have a malformed line: [#!variable!count!#:#!variable!line!#].]]></key>
<key name="log_0036"><![CDATA[[ Error ] - The module Storage->change_mode() was called without a 'target' parameter, or the parameter was empty.]]></key>
<key name="log_0037"><![CDATA[[ Error ] - The module Storage->change_mode() was called without a 'mode' parameter, or the parameter was empty.]]></key>
<key name="log_0038"><![CDATA[[ Error ] - The module Storage->change_mode() was called without an invalid 'mode' parameter. It should have been three or four digits, but: [#!variable!mode!#] was passed.]]></key>
<key name="log_0039"><![CDATA[[ Error ] - The module Storage->change_owner() was called without a 'target' parameter, or the parameter was empty.]]></key>
<key name="log_0040"><![CDATA[[ Error ] - The module Storage->write_file() was asked to write the file: [#!variable!file!#] but it already exists and 'overwrite' was not set. Aborting.]]></key>
<key name="log_0041"><![CDATA[[ Error ] - The module Storage->write_file() was asked to write the file: [#!variable!file!#] but it is not a full path. Aborting.]]></key>
<!-- Test words. Do NOT change unless you update 't/Words.t' or tests will needlessly fail. -->
<key name="t_0000">Test</key>

@ -11,17 +11,34 @@ if (($running_directory =~ /^\./) && ($ENV{PWD}))
$running_directory =~ s/^\./$ENV{PWD}/;
}
print "Content-type: text/html; charset=utf-8\n\n";
my $an = AN::Tools->new();
# Set the log level to 2
$an->Log->level(2);
# Read in our words file.
$an->Words->read({file => $an->data->{path}{directories}{'cgi-bin'}."/words.xml"});
# 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";
my $header = $an->Template->get({file => "main.html", name => "header", variables => {
language =>
my $header = $an->Template->get({file => "main.html", name => "header", variables => {
language => $an->Words->language,
skin_directory => $an->Template->skin,
}});
my $template = $an->Template->get({file => "main.html", name => "master", variables => {
header => $header,
left_top_bar => "",
center_top_bar => "",
right_top_bar => "",
center_body => "",
left_bottom_bar => "",
center_bottom_bar => "",
right_bottom_bar => "",
footer => "",
}});
my $template = $an->Template->get({file => "main.html", name => "master"});
print $template;

@ -41,7 +41,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>#!string!brand_0001!# - #!string!brand_0003!#</title>
<link rel="stylesheet" href="#!variable!skin_directory!#/main.css" media="screen" />
<script type="text/javascript" src="#!variable!html_directory!#/jquery-latest.js"></script>
<script type="text/javascript" src="#!data!path::directories::html!#/jquery-latest.js"></script>
<script type="text/javascript" src="#!variable!skin_directory!#/main.js"></script>
</head>
<!-- end header -->

@ -33,7 +33,23 @@ $| = 1;
# }
# close
report_network($an);
my $status = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
$status .= "<status>\n";
$status .= report_network($an);
$status .= "</status>\n";
### TODO: Set the 'status.xml' name into 'striker.conf'
# Write the file.
my $output_file = $an->data->{path}{directories}{html}."/status.xml";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { output_file => $output_file }});
$an->Storage->write_file({
file => $output_file,
body => $status,
overwrite => 1,
mode => "0644",
user => "apache",
group => "apache"
});
exit(0);
@ -46,9 +62,10 @@ sub report_network
{
my ($an) = @_;
my $network = " <network>\n";
my $directory = $an->data->{path}{sysfs}{network_interfaces};
local(*DIRECTORY);
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 2, key => "log_0018", variables => { directory => $directory }});
$an->Log->entry({source => $THIS_FILE, line => __LINE__, level => 3, key => "log_0018", variables => { directory => $directory }});
opendir(DIRECTORY, $directory);
while(my $file = readdir(DIRECTORY))
{
@ -56,18 +73,18 @@ sub report_network
next if $file eq "..";
next if $file eq "lo";
my $full_path = "$directory/$file";
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => { full_path => $full_path }});
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { full_path => $full_path }});
if (-d $full_path)
{
# Pull out the data I want.
my $interface = $file;
my $mac_address = $an->System->read_file({file => $full_path."/address"});
my $link_state = $an->System->read_file({file => $full_path."/carrier"});
my $mtu = $an->System->read_file({file => $full_path."/mtu"});
my $duplex = $an->System->read_file({file => $full_path."/duplex"}); # full or half?
my $operational = $an->System->read_file({file => $full_path."/operstate"}); # up or down
my $speed = $an->System->read_file({file => $full_path."/speed"}); # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 2, list => {
my $mac_address = $an->Storage->read_file({file => $full_path."/address"});
my $link_state = $an->Storage->read_file({file => $full_path."/carrier"});
my $mtu = $an->Storage->read_file({file => $full_path."/mtu"});
my $duplex = $an->Storage->read_file({file => $full_path."/duplex"}); # full or half?
my $operational = $an->Storage->read_file({file => $full_path."/operstate"}); # up or down
my $speed = $link_state ? $an->Storage->read_file({file => $full_path."/speed"}) : 0; # Mbps (ie: 1000 = Gbps), gives a very high number for unplugged link
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => {
interface => $interface,
mac_address => $mac_address,
link_state => $link_state,
@ -76,9 +93,12 @@ sub report_network
operational => $operational,
speed => $speed,
}});
$network .= " <interface name=\"$interface\" mac=\"$mac_address\" link=\"$link_state\" duplex=\"$duplex\" state=\"$operational\" speed=\"$speed\">\n";
}
}
closedir(DIRECTORY);
$network .= " <network>\n";
return(0);
$an->Log->variables({source => $THIS_FILE, line => __LINE__, level => 3, list => { network => $network }});
return($network);
}

Loading…
Cancel
Save