Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Import Kiriwrite 0.0.2 into the private repository.
[kiriwrite/.git] / cgi-files / kiriwrite.cgi
1 #!/usr/bin/perl -Tw
3 #################################################################################
4 # Kiriwrite (kiriwrite.cgi)                                                     #
5 # Main program script                                                           #
6 #                                                                               #
7 # Version: 0.0.2                                                                #
8 #                                                                               #
9 # Copyright (C) 2005-2007 Steve Brokenshire <sbrokenshire@xestia.co.uk>         #
10 #                                                                               #
11 # This program is free software; you can redistribute it and/or modify it under #
12 # the terms of the GNU General Public License as published by the Free          #
13 # Software Foundation; as version 2 of the License.                             #
14 #                                                                               #
15 # This program is distributed in the hope that it will be useful, but WITHOUT   #
16 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
17 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
18 #                                                                               #
19 # You should have received a copy of the GNU General Public License along with  #
20 # this program; if not, write to the Free Software Foundation, Inc., 51         #
21 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                           #
22 #################################################################################
24 use strict;                             # Throw errors if there's something wrong.
25 use warnings;                           # Write warnings to the HTTP Server Log file.
27 use utf8;
28 use CGI qw(:standard);                  
29 use CGI::Carp('fatalsToBrowser');       # Output errors to the browser.
31 # Declare global variables for Kiriwrite settings and languages.
33 our($kiriwrite_config, %kiriwrite_config, $kiriwrite_lang, %kiriwrite_lang, $kiriwrite_version, %kiriwrite_version);
35 # Setup the version information for Kiriwrite.
37 %kiriwrite_version = (
38         "major"         => 0,
39         "minor"         => 0,
40         "revision"      => 2
41 );
43 #################################################################################
44 # Begin listing the functions needed.                                           #
45 #################################################################################
47 #sub AUTOLOAD{
48 #################################################################################
49 # AUTOLOAD: Catch-all for subroutines that do not exist.                        #
50 #################################################################################
52         # A subroutine given does not exist so return an error.
54 #       kiriwrite_error("internalerror");
56 #}
58 sub kiriwrite_page_add{
59 #################################################################################
60 # kiriwrite_page_add: Adds a page to a database                                 #
61 #                                                                               #
62 # Usage:                                                                        #
63 #                                                                               #
64 # kiriwrite_page_add(database, pagefilename, title, description, section,       #
65 #                       template, settings, info, confirm);                     #
66 #                                                                               #
67 # database      Specifies the database name.                                    #
68 # pagefilename  Specifies the page filename.                                    #
69 # title         Specifies the page title to be used.                            #
70 # description   Specifies the short description of the page.                    #
71 # section       Specifies the section assigned to the page.                     #
72 # template      Specifies the template to use.                                  #
73 # settings      Specifies the settings to be used on the page.                  #
74 # data          Specifies the data which consists the page.                     #
75 # confirm       Confirms action to add an page.                                 #
76 #################################################################################
78         # Fetch the required variables that have been passed to the subroutine.
80         my ($pagedatabase, $pagefilename, $pagetitle, $pagedescription, $pagesection, $pagetemplate, $pagesettings, $pagefiledata, $confirm) = @_;
81         
82         # Load the required Perl modules.
84         use DBI;
85         
86         # Check if the action to add a new page has been confirmed (or not).
87         
88         my $pagedata = "";
89         
90         if (!$confirm){
91         
92                 $confirm = 0;
94         }
96         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
98         # Check if the database filename is valid and return an error if
99         # it isn't.
101         my $pagedatabase_filename_check = kiriwrite_variablecheck($pagedatabase, "filename", 0, 1);
103         if ($pagedatabase_filename_check eq 1){
105                 # The database filename is blank, so return an error.
107                 kiriwrite_error("blankdatabasepageadd");
109         } elsif ($pagedatabase_filename_check eq 2){
111                 # The database filename is invalid, so return an error.
113                 kiriwrite_error("databasefilenameinvalid");
115         }
117         # Check the length the database name and return an error if it's
118         # too long.
120         my $pagedatabase_length_check   = kiriwrite_variablecheck($pagedatabase, "maxlength", 64, 1);
122         if ($pagedatabase_length_check eq 1){
124                 # The database name is too long, so return an error.
126                 kiriwrite_error("databasefilenametoolong");
128         }
129         
130         if ($confirm eq "1"){
132                 # Check all the variables to see if they UTF8 valid.
134                 kiriwrite_variablecheck($pagefilename, "utf8", 0, 0);
135                 kiriwrite_variablecheck($pagetitle, "utf8", 0, 0);
136                 kiriwrite_variablecheck($pagedescription, "utf8", 0, 0);
137                 kiriwrite_variablecheck($pagesection, "utf8", 0, 0);
138                 kiriwrite_variablecheck($pagedatabase, "utf8", 0, 0);
139                 kiriwrite_variablecheck($pagesettings, "utf8", 0, 0);
140                 kiriwrite_variablecheck($pagetemplate, "utf8", 0, 0);
141                 kiriwrite_variablecheck($pagefiledata, "utf8", 0, 0);
143                 # Convert the values into proper UTF8 values.
145                 $pagefilename           = kiriwrite_utf8convert($pagefilename);
146                 $pagetitle              = kiriwrite_utf8convert($pagetitle);
147                 $pagedescription        = kiriwrite_utf8convert($pagedescription);
148                 $pagesection            = kiriwrite_utf8convert($pagesection);
149                 $pagedatabase           = kiriwrite_utf8convert($pagedatabase);
150                 $pagesettings           = kiriwrite_utf8convert($pagesettings);
151                 $pagetemplate           = kiriwrite_utf8convert($pagetemplate);
152                 $pagefiledata           = kiriwrite_utf8convert($pagefiledata);
154                 # Check all the variables (except for the page data, filename and settings 
155                 # will be checked more specifically later if needed) that were passed to 
156                 # the subroutine.
157         
158                 my $pagefilename_maxlength_check                = kiriwrite_variablecheck($pagefilename, "maxlength", 256, 1);
159                 my $pagetitle_maxlength_check                   = kiriwrite_variablecheck($pagetitle, "maxlength", 512, 1);
160                 my $pagedescription_maxlength_check             = kiriwrite_variablecheck($pagedescription, "maxlength", 512, 1);
161                 my $pagesection_maxlength_check                 = kiriwrite_variablecheck($pagesection, "maxlength", 256, 1);
162                 my $pagedatabase_maxlength_check                = kiriwrite_variablecheck($pagedatabase, "maxlength", 64, 1);
163                 my $pagesettings_maxlength_check                = kiriwrite_variablecheck($pagesettings, "maxlength", 1, 1);
164                 my $pagetemplate_maxlength_check                = kiriwrite_variablecheck($pagetemplate, "maxlength", 64, 1);
166                 # Check if an value returned is something else other than 0.
168                 if ($pagefilename_maxlength_check eq 1){
170                         # The page filename given is too long, so return an error.
172                         kiriwrite_error("pagefilenametoolong");
174                 }
176                 if ($pagetitle_maxlength_check eq 1){
178                         # The page title given is too long, so return an error.
180                         kiriwrite_error("pagetitletoolong");
182                 }
184                 if ($pagedescription_maxlength_check eq 1){
186                         # The page description given is too long, so return an error.
188                         kiriwrite_error("pagedescriptiontoolong");
190                 }
192                 if ($pagesection_maxlength_check eq 1){
194                         # The page section given is too long, so return an error.
196                         kiriwrite_error("pagesectiontoolong");
198                 }
200                 if ($pagedatabase_maxlength_check eq 1){
202                         # The page database given is too long, so return an error.
204                         kiriwrite_error("pagedatabasefilenametoolong");
206                 }
208                 if ($pagesettings_maxlength_check eq 1){
210                         # The page settings given is too long, so return an error.
212                         kiriwrite_error("pagesettingstoolong");
214                 }
216                 if ($pagetemplate_maxlength_check eq 1){
218                         # The page template given is too long, so return an error.
220                         kiriwrite_error("pagetemplatefilenametoolong");
222                 }
224                 # The action to create a new page has been confirmed, so add the page to the
225                 # selected database.
226                 
227                 if (!$pagefilename){
228                         kiriwrite_error("pagefilenameblank");           
229                 }
231                 # Check the page filename and page settings.
233                 my $pagefilename_filename_check = kiriwrite_variablecheck($pagefilename, "page_filename", 0, 1);
234                 my $pagesettings_setting_check  = kiriwrite_variablecheck($pagesettings, "pagesetting", 0, 1);
235                 my $pagetemplate_filename_check = 0;
236                 
237                 if ($pagetemplate ne "!none"){
239                         # A template is being used so check the filename of the
240                         # template.
242                         $pagetemplate_filename_check    = kiriwrite_variablecheck($pagetemplate, "page_filename", 0, 1);
244                 }
246                 if ($pagefilename_filename_check eq 1){
248                         # The page filename given is invalid, so return an error.
250                         kiriwrite_error("pagefilenameinvalid");
252                 }
254                 if ($pagesettings_setting_check eq 1){
256                         # The page settings given is invalid, so return an error.
258                         kiriwrite_error("pagesettingsinvalid");
260                 }
262                 if ($pagetemplate_filename_check eq 1){
264                         # The template filename given is invalid, so return an error
266                         kiriwrite_error("templatefilenameinvalid");
268                 }
270                 # Check the permissions of the page database.
272                 my $pagedatabase_exists         = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db');
273                 my $pagedatabase_permissions    = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db', 1, 1, 0);
275                 if ($pagedatabase_exists eq 1){
277                         # The page database does not exist, so return an error.
279                         kiriwrite_error("databasefilemissing");
281                 }
283                 if ($pagedatabase_permissions eq 1){
285                         # The page database permissions are invalid, so return
286                         # an error.
288                         kiriwrite_error("databaseinvalidpermissions");
290                 }
291                 
292                 # Load the SQLite database.
294                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db');
295                 
296                 # Check if the filename used already exists and return
297                 # an error if it is.
299                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
300                 $string_handle->execute();
302                 my @database_page;
303                 my $page_exists = 0;
305                 while (@database_page = $string_handle->fetchrow_array()){
307                         # The page exists so set the page exists value to 1.
309                         $page_exists = 1;
311                 }
313                 if ($page_exists eq 1){
315                         # The page exists so return an error.
317                         kiriwrite_error("pagefilenameexists");
319                 }
321                 # Get the database name.
323                 $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1');
324                 $string_handle->execute();
326                 my @database_info       = $string_handle->fetchrow_array();
327                 
328                 my $database_name       = $database_info[0];
330                 # Convert the values so they don't cause the SQL query
331                 # to break.
333                 my $filename_sql                = kiriwrite_convert($pagefilename, "kiriwrite");
334                 my $pagename_sql                = kiriwrite_convert($pagetitle, "kiriwrite");
335                 my $pagedescription_sql         = kiriwrite_convert($pagedescription, "kiriwrite");
336                 my $pagesection_sql             = kiriwrite_convert($pagesection, "kiriwrite");
337                 my $pagetemplate_sql            = kiriwrite_convert($pagetemplate, "kiriwrite");
338                 my $pagesettings_sql            = kiriwrite_convert($pagesettings, "kiriwrite");
339                 my $pagefiledata_sql            = kiriwrite_convert($pagefiledata, "kiriwrite");
341                 # Create the last modified date.
343                 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
345                 my $pagelastmodified = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
347                 # Add the page to the database.
349                 $string_handle          = $database_handle->prepare('INSERT INTO kiriwrite_database_pages VALUES(
350                         \'' . $filename_sql . '\',
351                         \'' . $pagename_sql . '\',
352                         \'' . $pagedescription_sql . '\',
353                         \'' . $pagesection_sql . '\',
354                         \'' . $pagetemplate_sql . '\',
355                         \'' . $pagefiledata_sql . '\',
356                         \'' . $pagesettings_sql . '\',
357                         \'' . $pagelastmodified . '\'
358                 )');
359                 $string_handle->execute();
361                 # Convert certain variables so that display properly.
363                 my $pagetitle_display           = kiriwrite_convert($pagetitle, "normal_display");
364                 my $database_name_display       = kiriwrite_convert($database_name, "normal_display");
366                 $pagedata = "<h2>Add Page</h2>";
367                 $pagedata = $pagedata . "The page called '" . $pagetitle_display . "' was added to the '" . $database_name_display .  "' database successfully.<br><br>";
368                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $pagedatabase . "\">Return to the page list for the '" . $database_name . "' database.</a>";
369                 
370                 return $pagedata;
371                 
372         } elsif ($confirm eq 0) {
374                 # The action to create a new page has not been confirmed, so print out a form
375                 # for adding a page to a database.
376                 
377                 # Check if the page database and template database exists and has valid permissions 
378                 # set and return an error if they don't.
380                 my $database_page_exists                = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db');
381                 my $database_page_permissions           = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db', 1, 1, 0);
382                 my $database_template_exists            = kiriwrite_fileexists("templates.db");
383                 my $database_template_permissions       = kiriwrite_filepermissions("templates.db", 1, 0, 0);
385                 my $template_data = "";
386                 
387                 my $database_handle;
388                 my $string_handle;
390                 my $template_warningmessage;
391                 my $template_warning = 0;
393                 my @database_template;
394                 my @database_page;
396                 if ($database_page_exists eq 1){
398                         # The page database does not exists so return an 
399                         # error.
401                         kiriwrite_error("databasemissingfile");
403                 }
405                 if ($database_page_permissions eq 1){
407                         # The page database permissions are invalid so
408                         # return an error.
410                         kiriwrite_error("databaseinvalidpermissions");
412                 }
414                 if ($database_template_exists eq 1){
416                         # The template database does not exist so
417                         # write a message to say that the template
418                         # database is missing, none assumed.
420                         $template_warningmessage = "Template database does not exist. No template will be used.";
421                         $template_warning = 1;
423                 }
425                 if ($database_template_permissions eq 1 && $database_template_exists ne 1){
427                         # The template permissions are invalid so
428                         # write a message saying that the template
429                         # database has invalid permissions set,
430                         # none assumed.
432                         $template_warningmessage = "Template database has invalid permissions set. No template will be used.";
433                         $template_warning = 1;
435                 }
437                 # Check if the template_warning value is set to 1 and
438                 # if it isn't then load the template database.
440                 if ($template_warning eq 0){
442                         # The template_warning value is not set to 1, so
443                         # load the template database.
445                         my $page_filename       = "";
446                         my $page_name           = "";
447                         my $template_count      = 0;
449                         # Load the SQLite database.
451                         $database_handle        = DBI->connect("dbi:SQLite:dbname=templates.db");
452                         $string_handle  = $database_handle->prepare("SELECT * FROM kiriwrite_templates ORDER BY templatename asc") or kiriwrite_error("templatedatabaseinvalidformat");
453                         $string_handle->execute();
455                         $template_data = "<select name=\"pagetemplate\">";
457                         # Process each template, getting the filename and template
458                         # name and put them into the list of templates available.
460                         while (@database_template = $string_handle->fetchrow_array()){
462                                 $page_filename  = $database_template[0];
463                                 $page_name      = $database_template[1];
465                                 $page_filename  = kiriwrite_convert($page_filename, "normal_display");
466                                 $page_name      = kiriwrite_convert($page_name, "normal_display");
468                                 $template_data = $template_data . "<option value=\"" . $page_filename . "\">" . $page_name . " (" . $page_filename . ")</option>";
469                                 $template_count++
471                         }
473                         $template_data = $template_data . "<option value=\"!none\">Don't use a template</option>";
474                         $template_data = $template_data . "</select>";
476                         if ($template_count eq 0){
478                                 # There are no templates in the template database
479                                 # so write a message saying that no templates
480                                 # are in the template database.
482                                 $template_warningmessage = "There are no templates in the template database. No template will be used.";
483                                 $template_warning = 1;
485                         }
487                 }
489                 # Load the page database and get the page database
490                 # name.
492                 $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db');
493                 $string_handle          = $database_handle->prepare("SELECT * FROM kiriwrite_database_info limit 1") or kiriwrite_error("databasefileinvalid");
494                 $string_handle->execute();
496                 @database_page          = $string_handle->fetchrow_array();
498                 my $database_name       = $database_page[0];
500                 $database_name          = kiriwrite_convert($database_name, "normal_display");
501                 $pagedatabase           = kiriwrite_convert($pagedatabase, "normal_display");
503                 $pagedata = $pagedata . "<h2>Add Page</h2>";
504                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
505                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
506                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"add\">";
507                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $pagedatabase . "\">";
508                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
509                 $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\">";
510                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
511                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database</td><td class=\"tablecell2\">" . $database_name . "</td></tr>";
512                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Name</td><td class=\"tablecell2\"><input type=\"input\" name=\"pagename\" value=\"\" maxlength=\"512\" size=\"64\"></td></tr>";
513                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Description</td><td class=\"tablecell2\"><input type=\"input\" name=\"pagedescription\" value=\"\" maxlength=\"512\" size=\"64\"></td></tr>";
514                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Section</td><td class=\"tablecell2\"><input type=\"input\" name=\"pagesection\" value=\"\" maxlength=\"256\" size=\"64\"></td></tr>";
516                 if ($template_warning eq 1){
518                         $pagedata = $pagedata . "<tr><td class=\"tablecell1\"><input type=\"hidden\" name=\"pagetemplate\" value=\"!none\">Page Template</td><td class=\"tablecell2\">" . $template_warningmessage . "</td></tr>";
519                         $pagedata = $pagedata . "";
521                 } else {
523                         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Template</td><td class=\"tablecell2\">" . $template_data . "</td></tr>";
525                 }
527                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Filename</td><td class=\"tablecell2\"><input type=\"input\" name=\"pagefilename\" value=\"\" maxlength=\"256\" size=\"64\"></td></tr>";
528                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Content</td><td class=\"tablecell2\"><textarea cols=\"50\" rows=\"10\" name=\"pagecontent\" wrap=\"off\"></textarea><br><br>";
529                 $pagedata = $pagedata . "<div class=\"datalist\"><b>Kiriwrite tags</b><br><br>&lt;kiriwrite:autosection&gt; - Automatic page section name. (Will be blank if page section name is blank).<br>
530                 &lt;kiriwrite:autotitle&gt; - Automatic title and section name based on page setting.<br>
531                 &lt;kiriwrite:pagedata&gt; - Page Content<br>
532                 &lt;kiriwrite:pagetitle&gt; - Page Title based on page setting.<br>
533                 &lt;kiriwrite:pagename&gt; - Page Name<br>
534                 &lt;kiriwrite:pagedescription&gt; - Page Description<br>
535                 &lt;kiriwrite:pagesection&gt; - Page Section<br></div>";
536                 $pagedata = $pagedata . "</td></tr>";
537                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Settings</td><td class=\"tablecell2\"><input type=\"radio\" name=\"pagesettings\" value=\"1\" checked=checked> Use page name and section name.<br><input type=\"radio\" name=\"pagesettings\" value=\"2\"> Use the page name only.<br><input type=\"radio\" name=\"pagesettings\" value=\"3\"> Use the section name only. <br><input type=\"radio\" name=\"pagesettings\" value=\"0\"> Don't use page name or section name.<br></td></tr>";
538                 $pagedata = $pagedata . "</table>";
539                 $pagedata = $pagedata . "<br><input type=\"submit\" value=\"Add Page\"> | <input type=\"reset\" value=\"Clear inputted data\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $pagedatabase . "\">Return to the page list.</a>";
540                 $pagedata = $pagedata . "</form>";
541         
542                 return $pagedata;
543                 
544         } else {
545         
546                 # The confirm value is something else than '1' or '0' so
547                 # return an error.
548                 
549                 kiriwrite_error("invalidvalue");
550         
551         }
552         
557 sub kiriwrite_page_delete{
558 #################################################################################
559 # kiriwrite_page_delete: Deletes a (single) page from a database.               #
560 #                                                                               #
561 # Usage:                                                                        #
562 #                                                                               #
563 # kiriwrite_page_delete(database, page, [confirm]);                             #
564 #                                                                               #
565 # database      Specifies the database to delete from.                          #
566 # page          Specifies the page to delete.                                   #
567 # confirm       Confirms the action to delete the page.                         #
568 #################################################################################
570         my ($database, $page, $confirm) = @_;
572         # Load the required Perl modules.
574         use DBI;
576         my $pagedata;
577         
578         # Check if the database filename is valid and return an error if
579         # it isn't.
581         my $pagedatabase_filename_check = kiriwrite_variablecheck($database, "filename", 0, 1);
583         if ($pagedatabase_filename_check eq 1){
585                 # The database filename is blank, so return an error.
587                 kiriwrite_error("blankdatabasepageadd");
589         } elsif ($pagedatabase_filename_check eq 2){
591                 # The database filename is invalid, so return an error.
593                 kiriwrite_error("databasefilenameinvalid");
595         }
597         # Check the length the database name and return an error if it's
598         # too long.
600         my $pagedatabase_length_check   = kiriwrite_variablecheck($database, "maxlength", 64, 1);
602         if ($pagedatabase_length_check eq 1){
604                 # The database name is too long, so return an error.
606                 kiriwrite_error("databasefilenametoolong");
608         }
610         # Check if the page name is specified is blank and return an error if
611         # it is.
613         if (!$page){
615                 # The page name is blank, so return an error.
617                 kiriwrite_error("blankfilename");
619         }
620         
621         # If the confirm value is blank, then set the confirm value to 0.
623         if (!$confirm){
624                 
625                 $confirm = 0;
626                 
627         }
628         
629         if ($confirm eq 1){
631                 # The action to delete the selected page from the database
632                 # has been confirmed.
634                 # Check that the database exists and has the correct permissions
635                 # set.
637                 my $database_check_exists       = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
638                 my $database_check_permissions  = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
640                 # Check the results and return an error if needed.
642                 if ($database_check_exists eq 1){
644                         # The database file does not exist, so return an error.
646                         kiriwrite_error("databasemissingfile");
648                 }
650                 if ($database_check_permissions eq 1){
652                         # The database file has invalid permissions set, so
653                         # return an error.
655                         kiriwrite_error("databaseinvalidpermissions");
657                 }
659                 # Load the SQLite database and get information about the
660                 # database.
662                 my $database_handle             = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
663                 my $string_handle               = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid") or kiriwrite_error("databasefileinvalid");
664                 $string_handle->execute();
666                 my @database_info;
667                 my $database_name;
668                 @database_info = $string_handle->fetchrow_array();
669                 $database_name = $database_info[0];
671                 my $pagefilename_sql            = kiriwrite_convert($page, "kiriwrite");
673                 # Check if the filename exists and return an error if it
674                 # isn't.
676                 $string_handle                  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
677                 $string_handle->execute();
679                 my @database_page;
680                 my $page_found = 0;
682                 while (@database_page = $string_handle->fetchrow_array()){
684                         $page_found = 1;
686                 }
688                 # Check if the page wasn't found and return an error if it
689                 # didn't.
691                 if ($page_found eq 0){
693                         # Page wasn't found in the database so return
694                         # an error.
696                         kiriwrite_error("pagefilenamedoesnotexist");
698                 }
700                 # Get information about the page (mainly the name).
702                 $string_handle                  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
703                 $string_handle->execute();
705                 my $page_name;
706                 @database_page  = $string_handle->fetchrow_array();
707                 $page_name      = $database_page[1];
709                 # Delete the page from the database.
711                 $string_handle                  = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\'');
712                 $string_handle->execute();
714                 # Convert the values before writing them.
716                 my $database_out        = kiriwrite_convert($database, "normal_display");
717                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
718                 my $page_name_out       = kiriwrite_convert($page_name, "normal_display");
720                 # Write out a message saying that the selected page from
721                 # the database has been deleted.
723                 $pagedata = "<h2>Page Deleted</h2>";
724                 $pagedata = $pagedata . "The page named '" . $page_name_out . "' was deleted from the '" . $database_name_out . "' database. <br><br>";
725                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the '" . $database_name_out . "' database.</a>";
727                 return $pagedata;
729         } elsif ($confirm eq 0){
731                 # Check that the database exists and has the correct permissions
732                 # set.
734                 my $database_check_exists       = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
735                 my $database_check_permissions  = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
737                 # Check the results and return an error if needed.
739                 if ($database_check_exists eq 1){
741                         # The database file does not exist, so return an error.
743                         kiriwrite_error("databasemissingfile");
745                 }
747                 if ($database_check_permissions eq 1){
749                         # The database file has invalid permissions set, so
750                         # return an error.
752                         kiriwrite_error("databaseinvalidpermissions");
754                 }
756                 # Load the SQLite database and get information about the
757                 # database.
759                 my $database_handle             = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
760                 my $string_handle               = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid") or kiriwrite_error("databasefileinvalid");
761                 $string_handle->execute();
763                 my @database_info;
764                 my $database_name;
765                 @database_info = $string_handle->fetchrow_array();
766                 $database_name = $database_info[0];
768                 my $pagefilename_sql            = kiriwrite_convert($page, "kiriwrite");
770                 # Check if the filename exists and return an error if it
771                 # isn't.
773                 $string_handle                  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
774                 $string_handle->execute();
776                 my $page_found = 0;
778                 my @database_page;
780                 while (@database_page = $string_handle->fetchrow_array()){
782                         $page_found = 1;
784                 }
786                 # Check if the page wasn't found and return an error if it
787                 # didn't.
789                 if ($page_found eq 0){
791                         # Page wasn't found in the database so return
792                         # an error.
794                         kiriwrite_error("pagefilenamedoesnotexist");
796                 }
798                 # Get information about the page (mainly the name).
800                 $string_handle                  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
801                 $string_handle->execute();
803                 my $page_name;
804                 @database_page  = $string_handle->fetchrow_array();
805                 $page_name      = $database_page[1];
807                 # Convert the values so that they can be displayed properly.
809                 my $page_name_out       = kiriwrite_convert($page_name, "normal_display");
810                 my $database_out        = kiriwrite_convert($database, "normal_display");
811                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
812                 my $page_out            = kiriwrite_convert($page, "normal_display");
814                 # Write a message asking the user to confirm the deletion of the
815                 # page.
817                 $pagedata = "<form>";
818                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
819                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"delete\">";
820                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_out . "\">";
821                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"page\" value=\"" . $page_out . "\">";
822                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";;
823                 $pagedata = $pagedata . "<h2>Delete page '" . $page_name_out . "'</h2>";
824                 $pagedata = $pagedata . "Are you sure you want to delete '" . $page_name_out . "' from the '" . $database_name_out . "' database?<br><br>";
825                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Yes, delete the page\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out  . "\">No, return to the '" . $database_name_out . "' database.</a>";
826                 $pagedata = $pagedata . "</form>";
827                 
829                 return $pagedata;               
831         } else {
832         
833                 # Another page deletion type was specified, so return an error.
834                 
835                 kiriwrite_error("invalidoption");
836         
837         }
841 sub kiriwrite_page_edit{
842 #################################################################################
843 # kiriwrite_page_edit: Edits a page from a database.                            #
844 #                                                                               #
845 # Usage:                                                                        #
846 #                                                                               #
847 # kiriwrite_page_edit(database, filename, newname, newfilename, newdescription, #
848 #                       newsection, newtemplate,  newsettings, newpagecontent   #
849 #                       confirm);                                               #
850 #                                                                               #
851 # database       Specifies the database to edit from.                           #
852 # filename       Specifies the filename to use.                                 #
853 # newfilename    Specifies the new page filename to use.                        #
854 # newname        Specifies the new page name to use.                            #
855 # newdescription Specifies the new description for the page.                    #
856 # newsection     Specifies the new section name to use.                         #
857 # newtemplate    Specifies the new template filename to use.                    #
858 # newsettings    Specifies the new page settings to use.                        #
859 # newpagecontent Specifies the new page content to use.                         #
860 # confirm        Confirms the action to edit the page.                          #
861 #################################################################################
863         # Get the values that have been passed to the subroutine.
865         my ($database, $pagefilename, $pagenewfilename, $pagenewtitle, $pagenewdescription, $pagenewsection, $pagenewtemplate, $pagenewsettings, $pagenewcontent, $confirm) = @_;
867         # Load the required Perl modules.
868         
869         use DBI;
870         
871         my $pagedata = "";
872                 
873         # Check if the confirm value is blank and if it is, then set it to '0'.
874         
875         if (!$confirm){
876         
877                 $confirm = 0;
878         
879         }
881         # Check if the confirm value is more than one character long and if it
882         # is then return an error.
884         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
885         
886         # Check if the database filename is valid and return an error if
887         # it isn't.
889         my $pagedatabase_filename_check = kiriwrite_variablecheck($database, "filename", 0, 1);
891         if ($pagedatabase_filename_check eq 1){
893                 # The database filename is blank, so return an error.
895                 kiriwrite_error("blankdatabasepageadd");
897         } elsif ($pagedatabase_filename_check eq 2){
899                 # The database filename is invalid, so return an error.
901                 kiriwrite_error("databasefilenameinvalid");
903         }
904         
905         # Check the length the database name and return an error if it's
906         # too long.
908         my $pagedatabase_length_check   = kiriwrite_variablecheck($database, "maxlength", 64, 1);
910         if ($pagedatabase_length_check eq 1){
912                 # The database name is too long, so return an error.
914                 kiriwrite_error("databasefilenametoolong");
916         }
918         # Check if the page identification number is blank (which it shouldn't
919         # be) and if it is, then return an error.
920         
921         if (!$pagefilename){
922         
923                 kiriwrite_error("blankfilename");
924         
925         }
926         
927         # Check if the confirm value is '1' and if it is, edit the specified
928         # page in the database.
929         
930         if ($confirm eq 1){
931                 
932                 # Check if the new page filename is blank.
934                 if (!$pagenewfilename){
936                         # The page filename is blank so return an error.
938                         kiriwrite_error("pagefilenameblank");
940                 }
942                 # The action to edit a page has been confirmed so check the
943                 # variables recieved are UTF8 compiliant before converting.
945                 kiriwrite_variablecheck($database, "utf8", 0, 0);
946                 kiriwrite_variablecheck($pagefilename, "utf8", 0, 0);
947                 kiriwrite_variablecheck($pagenewfilename, "utf8", 0, 0);
948                 kiriwrite_variablecheck($pagenewtitle, "utf8", 0, 0);
949                 kiriwrite_variablecheck($pagenewdescription, "utf8", 0, 0);
950                 kiriwrite_variablecheck($pagenewsection, "utf8", 0, 0);
951                 kiriwrite_variablecheck($pagenewsettings, "utf8", 0, 0);
952                 kiriwrite_variablecheck($pagenewtemplate, "utf8", 0, 0);
953                 kiriwrite_variablecheck($pagenewcontent, "utf8", 0, 0);
955                 # Convert the variables into proper UTF8 variables.
957                 $database               = kiriwrite_utf8convert($database);
958                 $pagefilename           = kiriwrite_utf8convert($pagefilename);
959                 $pagenewfilename        = kiriwrite_utf8convert($pagenewfilename);
960                 $pagenewtitle           = kiriwrite_utf8convert($pagenewtitle);
961                 $pagenewdescription     = kiriwrite_utf8convert($pagenewdescription);
962                 $pagenewsection         = kiriwrite_utf8convert($pagenewsection);
963                 $pagenewsettings        = kiriwrite_utf8convert($pagenewsettings);
964                 $pagenewtemplate        = kiriwrite_utf8convert($pagenewtemplate);
965                 $pagenewcontent         = kiriwrite_utf8convert($pagenewcontent);
967                 # Check the lengths of the variables.
969                 my $pagenewfilename_maxlength_check     = kiriwrite_variablecheck($pagenewfilename, "maxlength", 256, 1);
970                 my $pagenewtitle_maxlength_check        = kiriwrite_variablecheck($pagenewtitle, "maxlength", 256, 1);
971                 my $pagenewdescription_maxlength_check  = kiriwrite_variablecheck($pagenewdescription, "maxlength", 512, 1);
972                 my $pagenewsection_maxlength_check      = kiriwrite_variablecheck($pagenewsection, "maxlength", 256, 1);
973                 my $pagenewsettings_maxlength_check     = kiriwrite_variablecheck($pagenewsettings, "maxlength", 1, 1);
974                 my $pagenewtemplate_maxlength_check     = kiriwrite_variablecheck($pagenewtemplate, "maxlength", 64, 1);
976                 # Check each result to see if the length of the variable
977                 # is valid and return an error if it isn't.
979                 if ($pagenewfilename_maxlength_check eq 1){
981                         # The new page filename given is too long, so return an error.
983                         kiriwrite_error("pagefilenametoolong");
985                 }
987                 if ($pagenewtitle_maxlength_check eq 1){
989                         # The new page title given is too long, so return an error.
991                         kiriwrite_error("pagetitletoolong");
993                 }
995                 if ($pagenewdescription_maxlength_check eq 1){
997                         # The new page description given is too long, so return an error.
999                         kiriwrite_error("pagedescriptiontoolong");
1001                 }
1003                 if ($pagenewsection_maxlength_check eq 1){
1005                         # The new page section given is too long, so return an error.
1007                         kiriwrite_error("pagesectiontoolong");
1009                 }
1011                 if ($pagenewsettings_maxlength_check eq 1){
1013                         # The new page settings given is too long, so return an error.
1015                         kiriwrite_error("pagesettingstoolong");
1017                 }
1019                 if ($pagenewtemplate_maxlength_check eq 1){
1021                         # The new page template given is too long, so return an error.
1023                         kiriwrite_error("pagetemplatefilenametoolong");
1025                 }
1027                 # Check if the new page filename and new page settings
1028                 # are valid.
1030                 my $pagenewfilename_filename_check      = kiriwrite_variablecheck($pagenewfilename, "page_filename", 0, 1);
1031                 my $pagenewsettings_settings_check      = kiriwrite_variablecheck($pagenewsettings, "pagesetting", 0, 1);
1032                 my $pagetemplate_filename_check = 0;
1033                 
1034                 if ($pagenewtemplate ne "!none"){
1036                         # A template is being used so check the filename of the
1037                         # template.
1039                         $pagetemplate_filename_check    = kiriwrite_variablecheck($pagenewtemplate, "page_filename", 0, 1);
1041                 }
1043                 # Check each result to see if the variables have passed
1044                 # their tests and return an error if they haven't.
1046                 if ($pagenewfilename_filename_check eq 1){
1048                         # The new page filename is invalid, so return an error.
1050                         kiriwrite_error("pagefilenameinvalid");
1052                 }
1054                 if ($pagenewsettings_settings_check eq 1){
1056                         # The new page settings is invalid, so return an error.
1058                         kiriwrite_error("pagesettingsinvalid");
1060                 }
1062                 if ($pagetemplate_filename_check eq 1){
1064                         # The template filename given is invalid, so return an error
1066                         kiriwrite_error("templatefilenameinvalid");
1068                 }
1070                 # Check if the page database exists and the permissions set for
1071                 # it are valid.
1073                 my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1074                 my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
1076                 if ($database_exists eq 1){
1078                         # The database does not exist, so return an error.
1080                         kiriwrite_error("databasemissingfile");
1082                 }
1083                 
1084                 if ($database_permissions eq 1){
1086                         # The database permissions are invalid, so return
1087                         # an error.
1089                         kiriwrite_error("databaseinvalidpermissions");
1091                 }
1093                 # Load the SQLite database.
1095                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1096                 
1097                 # Check if the page exists in the page database and return
1098                 # an error if it doesn't.
1100                 my $pagefilename_sql    = kiriwrite_convert($pagefilename, "kiriwrite");
1102                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
1103                 $string_handle->execute();
1105                 my $page_exists = 0;
1106                 my @database_pages;
1108                 # Check if a result has returned (meaning that the
1109                 # page with that filename exists.
1111                 while(@database_pages = $string_handle->fetchrow_array()){
1113                         # The page exists so set the value to 1.
1115                         $page_exists = 1;
1117                 }
1119                 if ($page_exists eq 0){
1121                         # The page does not exist, so return an error.
1123                         kiriwrite_error("pagefilenamedoesnotexist");
1125                 }
1127                 # Get the current date and time.
1129                 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1131                 my $pagelastmodified = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1133                 # Prepare for the page's values to be changed.
1135                 $pagefilename_sql               = kiriwrite_convert($pagefilename, "kiriwrite");
1136                 my $pagenewfilename_sql         = kiriwrite_convert($pagenewfilename, "kiriwrite");
1137                 my $pagenewtitle_sql            = kiriwrite_convert($pagenewtitle, "kiriwrite");
1138                 my $pagenewdescription_sql      = kiriwrite_convert($pagenewdescription, "kiriwrite");
1139                 my $pagenewsection_sql          = kiriwrite_convert($pagenewsection, "kiriwrite");
1140                 my $pagenewtemplate_sql         = kiriwrite_convert($pagenewtemplate, "kiriwrite");
1141                 my $pagenewsettings_sql         = kiriwrite_convert($pagenewsettings, "kiriwrite");
1142                 my $pagenewcontent_sql          = kiriwrite_convert($pagenewcontent, "kiriwrite");
1143                 my $pagelastmodified_sql        = kiriwrite_convert($pagelastmodified, "kiriwrite");
1145                 # Edit the page in the database.
1147                 $string_handle  = $database_handle->prepare('UPDATE kiriwrite_database_pages SET filename = \'' . $pagenewfilename_sql . '\', pagename = \'' . $pagenewtitle_sql . '\', pagedescription = \'' . $pagenewdescription_sql . '\', pagesection = \'' . $pagenewsection_sql . '\', pagetemplate = \'' . $pagenewtemplate_sql . '\', pagedata = \'' . $pagenewcontent_sql . '\', pagesettings = \'' . $pagenewsettings_sql . '\', lastmodified = \'' . $pagelastmodified_sql . '\' WHERE filename = \'' . $pagefilename_sql . '\'');
1148                 $string_handle->execute();
1150                 # Get the database name.
1152                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1');
1153                 $string_handle->execute();
1155                 my @database_info;
1156                 my $database_name;
1158                 @database_info  = $string_handle->fetchrow_array();
1159                 $database_name  = $database_info[0];
1161                 # Convert certain values so that they can be displayed
1162                 # properly.
1164                 my $pagenewtitle_out            = kiriwrite_convert($pagenewtitle, "normal_display");
1165                 my $database_out                = kiriwrite_convert($database, "normal_display");
1166                 my $database_name_out           = kiriwrite_convert($database_name, "normal_display");
1168                 # Write out the message to say that the page has been
1169                 # edited.
1171                 $pagedata = "<h2>Page Edited</h2>";
1172                 $pagedata = $pagedata . "The page '" . $pagenewtitle_out . "' has been edited.<br><br>";
1173                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database</a>";
1175                 return $pagedata;
1176         
1177         } elsif ($confirm eq 0) {
1178         
1179                 # Check if the database exists and that the permissions
1180                 # for the database are valid.
1182                 my $database_file_exists        = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1183                 my $database_file_permissions   = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1185                 if ($database_file_exists eq 1){
1187                         # The database with the filename given does
1188                         # not exist, so return an error.
1190                         kiriwrite_error("databasemissingfile");
1192                 }
1194                 if ($database_file_permissions eq 1){
1196                         # The file permissions of the database given
1197                         # are invalid so return an error.
1199                         kiriwrite_error("databaseinvalidpermissions");
1201                 }
1203                 # Load the SQLite database and check if the file exists.
1205                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1206                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
1207                 $string_handle->execute();
1209                 my $page_count = 0;
1210                 my @database_pages;
1211                 my @database_info;
1212                 my @database_templates;
1213                 my $template_data;
1214                 my $template_filename;
1215                 my $template_name;
1216                 my $template_found = 0;
1218                 while (@database_pages = $string_handle->fetchrow_array()){
1220                         $page_count = 1;
1222                 }
1224                 if ($page_count eq 0){
1226                         # The page does not exist in the template database, so
1227                         # return an error.
1229                         kiriwrite_error("pagefilenamedoesnotexist");
1231                 }
1233                 # Get the database name.
1235                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
1236                 $string_handle->execute();
1237                 @database_info  = $string_handle->fetchrow_array();
1239                 my $database_name = $database_info[0];
1241                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename . '\' LIMIT 1');
1242                 $string_handle->execute();
1243                 @database_pages = $string_handle->fetchrow_array();
1245                 # Get the variables and convert them so that they can be displayed
1246                 # properly.
1248                 my $data_filename               = kiriwrite_convert($database_pages[0], "normal_display");
1249                 my $data_name                   = kiriwrite_convert($database_pages[1], "normal_display");
1250                 my $data_description            = kiriwrite_convert($database_pages[2], "normal_display");
1251                 my $data_section                = kiriwrite_convert($database_pages[3], "normal_display");
1252                 my $data_template               = kiriwrite_convert($database_pages[4], "normal_display");
1253                 my $data_content                = kiriwrite_convert($database_pages[5], "normal_display");
1254                 my $data_settings               = kiriwrite_convert($database_pages[6], "normal_display");
1255                 my $data_lastmodified           = kiriwrite_convert(kiriwrite_convert($database_pages[7], "date"), "normal_display");
1257                 # Get the template list and select the template that is being used by
1258                 # the page, check the permissions and if the template database exists.
1260                 my $template_warning;
1262                 my $template_file_exists        = kiriwrite_fileexists("templates.db");
1263                 my $template_file_permissions   = kiriwrite_filepermissions("templates.db", 1, 0, 0);
1265                 if ($template_file_exists eq 1){
1267                         # The template database does not exist, so write a warning messsage.
1269                         $template_warning = "The template database does not exist. Existing template settings for page kept.";
1271                 }
1273                 if ($template_file_permissions eq 1 && $template_file_exists ne 1){
1275                         # The template database has invalid permissions set, so write a warning message.
1277                         $template_warning = "The template database has invalid permissions set. Existing template settings for page kept.";
1279                 }
1281                 # Get the list of templates.
1283                 if (!$template_warning){
1285                         # Load the templates database and get the templates.
1287                         $database_handle        = DBI->connect("dbi:SQLite:templates.db");
1289                         $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_templates') or $template_warning = "The template database is in an invalid format. Existing template settings kept.";
1290                         
1291                         if (!$template_warning){
1293                                 $string_handle->execute();
1295                                 $template_data = "<select name=\"pagetemplate\">";
1297                                 while (@database_templates = $string_handle->fetchrow_array()){
1299                                         # Get the filename and name of the database.
1301                                         $template_filename      = $database_templates[0];
1302                                         $template_name          = $database_templates[1];
1304                                         # Convert the values so that they can be displayed in the list.
1306                                         $template_filename      = kiriwrite_convert($template_filename, "normal_display");
1307                                         $template_name          = kiriwrite_convert($template_name, "normal_display");
1309                                         # Append the template filename and name and make it the selected
1310                                         # template if that is the template the page is using.
1312                                         if ($data_template eq $template_filename && !$template_found){
1314                                                 $template_data = $template_data .  "<option value=\"" . $template_filename . "\" selected>" . $template_name . " (" . $template_filename .")</option>";
1315                                                 $template_found = 1;
1317                                         } else {
1319                                                 $template_data = $template_data .  "<option value=\"" . $template_filename . "\">" . $template_name . " (" . $template_filename .")</option>";
1321                                         }
1323                                 }
1325                         }
1327                         # Add the option to not use a template at all. Check if the
1328                         # template filename is set to not use a template.
1330                         if ($data_template eq "!none"){
1332                                 # The template filename is set to use no template
1333                                 # at all.
1335                                 $template_data = $template_data . "<option value=\"!none\" selected>Don't use a template</option>";
1337                         } else {
1339                                 # The template filename is not set to use no
1340                                 # template at all.
1342                                 $template_data = $template_data . "<option value=\"!none\">Don't use a template</option>";
1344                         }
1346                         # Check if the template filename was found
1347                         # in the template database and use the
1348                         # current template name if not.
1350                         if ($template_found eq 0 && $data_template ne "!none"){
1352                                 # The template with the filename given was not found.
1354                                 $template_data = $template_data . "<option class=\"warningoption\" value=\"" . $data_template . "\" selected>Keep current template filename (" . $data_template . ")</option>";
1356                         }
1357                         $template_data = $template_data . "</select>";
1359                 }
1361                 # Begin writing out the form for editing the selected page.
1363                 $pagedata = "<h2>Editing Page '" . $data_name . "'</h2>";
1364                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
1365                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
1366                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"edit\">";
1367                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database . "\">";
1368                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"page\" value=\"" . $pagefilename . "\">";
1369                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
1370                 $pagedata = $pagedata . "<table cellspacing=\"0\" cellpadding=\"5\">";
1371                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
1372                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database</td><td class=\"tablecell2\">" . $database_name . "</td></tr>";
1373                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Name</td><td class=\"tablecell2\"><input type=\"text\" name=\"pagename\" maxlength=\"256\" size=\"64\"  value=\"" . $data_name ."\"></td></tr>";
1374                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Description</td><td class=\"tablecell2\"><input type=\"text\" name=\"pagedescription\" maxlength=\"512\" size=\"64\" value=\"" . $data_description . "\"=</td></tr>";
1375                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Section</td><td class=\"tablecell2\"><input type=\"text\" name=\"pagesection\" maxlength=\"256\" size=\"64\" value=\"" . $data_section . "\"></td></tr>";
1377                 if ($template_warning){
1379                         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Template</td><td class=\"tablecell2\"><input type=\"hidden\" name=\"pagetemplate\" value=\"" . $data_template . "\"><b>" . $data_template . "</b><br>" . $template_warning . "</td></tr>";
1381                 } else {
1383                         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Template</td><td class=\"tablecell2\">" . $template_data . "</td></tr>";
1385                 }
1387                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Filename</td><td class=\"tablecell2\"><input type=\"text\" name=\"pagefilename\" value=\"" . $data_filename . "\" maxlength=\"256\" size=\"64\"></td></tr>";
1388                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Content</td><td class=\"tablecell2\"><textarea name=\"pagecontent\" rows=\"10\" cols=\"50\" wrap=\"off\">" . $data_content . "</textarea><br><br>";
1389                 $pagedata = $pagedata . "<div class=\"datalist\"><b>Kiriwrite tags</b><br><br>&lt;kiriwrite:autosection&gt; - Automatic page section name. (Will be blank if page section name is blank).<br>
1390                 &lt;kiriwrite:autotitle&gt; - Automatic title and section name based on page setting.<br>
1391                 &lt;kiriwrite:pagedata&gt; - Page Content<br>
1392                 &lt;kiriwrite:pagetitle&gt; - Page Title based on page setting.<br>
1393                 &lt;kiriwrite:pagename&gt; - Page Name<br>
1394                 &lt;kiriwrite:pagedescription&gt; - Page Description<br>
1395                 &lt;kiriwrite:pagesection&gt; - Page Section<br></div>";
1396                 $pagedata = $pagedata . "</td></tr>";
1397                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Settings</td><td class=\"tablecell2\">";
1399                 # Check the page settings value and set the currently selected
1400                 # option to what the settings value is set to.
1402                 if ($data_settings eq 1){
1404                         # The selected option is to use the page name and
1405                         # section name so make sure it is selected.
1407                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"1\" checked> Use page name and section name.<br>";
1409                 } else {
1411                         # The setting is not the one being checked for
1412                         # so write the option out as normal.
1414                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"1\"> Use page name and section name.<br>";
1416                 }
1418                 if ($data_settings eq 2){
1420                         # The selected option is to use the page name
1421                         # only so make sure it is selected.
1423                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"2\" checked> Use the page name only.<br>";
1425                 } else {
1427                         # The setting is not the one being checked for
1428                         # so write the option out as normal.
1430                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"2\"> Use the page name only.<br>";
1432                 }
1434                 if ($data_settings eq 3){
1436                         # The selected option is to use the section name
1437                         # only so make sure it is selected.
1439                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"3\" checked> Use the section name only. <br>";
1441                 } else {
1443                         # The setting is not the one being checked for
1444                         # so write the option out as normal.
1446                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"3\"> Use the section name only. <br>";
1448                 }
1450                 if ($data_settings eq 0){
1452                         # The selected option is to not use the page name
1453                         # or the section name.
1455                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"0\" checked> Don't use page name or section name.<br></td></tr>";
1457                 } else {
1459                         # The setting is not the one being checked for
1460                         # so write the option out as normal.
1462                         $pagedata = $pagedata . "<input type=\"radio\" name=\"pagesettings\" value=\"0\"> Don't use page name or section name.<br></td></tr>";
1464                 }
1466                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Last Modified</td><td class=\"tablecell2\">" . $data_lastmodified  . "</td></tr>";
1467                 $pagedata = $pagedata . "</table>";
1468                 $pagedata = $pagedata . "<br><input type=\"submit\" value=\"Edit Page\"> | <input type=\"reset\" value=\"Reset page values\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database . "\">Return to the page list for '" . $database_name . "'</a>";
1469                 $pagedata = $pagedata . "</form>";
1471                 return $pagedata;
1473         } else {
1474         
1475                 # The confirm value is a value other than '0' and '1' so
1476                 # return an error.
1477                 
1478                 kiriwrite_error("invalidvalue");
1479         
1480         }
1484 sub kiriwrite_page_multidelete{
1485 #################################################################################
1486 # kiriwrite_page_multidelete: Delete mulitple pages from the database.          #
1487 #                                                                               #
1488 # Usage:                                                                        #
1489 #                                                                               #
1490 # kiriwrite_page_multidelete(database, confirm, filelist);                      #
1491 #                                                                               #
1492 # database      Specifies the database to delete multiple pages from.           #
1493 # confirm       Confirms the action to delete the selected pages from the       #
1494 #               database.                                                       #
1495 # filelist      The list of files to delete from the selected database.         #
1496 #################################################################################
1498         # Get the information passed to the subroutine.
1500         my ($database, $confirm, @filelist) = @_;
1502         # Load the needed Perl modules.
1504         use DBI;
1506         # Define a variable for later on.
1508         my $pagedata;
1510         # Check if the database name is blank and return an error if
1511         # it is.
1513         if (!$database){
1515                 # The database name is blank so return an error.
1517                 kiriwrite_error("databasenameblank");
1519         }
1521         # Check if the page database exists and the permissions set for
1522         # it are valid.
1524         my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1525         my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
1526         
1527         if ($database_exists eq 1){
1529                 # The database does not exist, so return an error.
1531                 kiriwrite_error("databasemissingfile");
1533         }
1534                 
1535         if ($database_permissions eq 1){
1537                 # The database permissions are invalid, so return
1538                 # an error.
1540                 kiriwrite_error("databaseinvalidpermissions");
1542         }
1544         # Check if the file list array has any values and return
1545         # an error if it doesn't.
1547         if (!@filelist){
1549                 # The page list really is blank so return
1550                 # an error.
1552                 kiriwrite_error("nopagesselected");
1553         }
1555         # Check if the database filename is valid and return an error if
1556         # it isn't.
1558         my $pagedatabase_filename_check = kiriwrite_variablecheck($database, "filename", 0, 1);
1560         if ($pagedatabase_filename_check eq 1){
1562                 # The database filename is blank, so return an error.
1564                 kiriwrite_error("blankdatabasepageadd");
1566         } elsif ($pagedatabase_filename_check eq 2){
1568                 # The database filename is invalid, so return an error.
1570                 kiriwrite_error("databasefilenameinvalid");
1572         }
1573         
1574         # Check the length the database name and return an error if it's
1575         # too long.
1577         my $pagedatabase_length_check   = kiriwrite_variablecheck($database, "maxlength", 64, 1);
1579         if ($pagedatabase_length_check eq 1){
1581                 # The database name is too long, so return an error.
1583                 kiriwrite_error("databasefilenametoolong");
1585         }
1587         # Check if the confirm value is blank and if it is, then
1588         # set it to 0.
1590         if (!$confirm){
1592                 # The confirm value is blank so set the confirm value
1593                 # to 0.
1595                 $confirm = 0;
1597         }
1599         if ($confirm eq 1){
1601                 # The action to delete multiple pages from the database has
1602                 # been confirmed.
1604                 # Load the SQLite Database and get info about the database.
1606                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1607                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
1608                 $string_handle->execute();
1610                 my @database_info       = $string_handle->fetchrow_array();
1611                 my $database_name       = $database_info[0];
1613                 # Define some variables for later.
1615                 my @database_page;
1616                 my $filelist_filename;
1617                 my $deleted_list;
1618                 my $page_name;
1619                 my $page_found = 0;
1621                 $deleted_list = "";
1623                 foreach $filelist_filename (@filelist){
1625                         # Check if the page exists and skip to the next
1626                         # file if it doesn't exist.
1628                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename . '\' LIMIT 1');
1629                         $string_handle->execute();
1631                         while (@database_page = $string_handle->fetchrow_array()){
1632                                 $page_found = 1;
1633                         }
1635                         if ($page_found eq 0){
1637                                 next;
1639                         }
1641                         # Get the page name.
1643                         $string_handle->execute();
1644                         @database_page  = $string_handle->fetchrow_array();
1645                         $page_name      = $database_page[1];
1647                         # Delete the page.
1649                         $string_handle  = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename  . '\'');
1650                         $string_handle->execute();
1652                         # Append the page name and page filename to the list
1653                         # of deleted pages.
1655                         $deleted_list   = $deleted_list . kiriwrite_convert($page_name, "normal_display") . " (" . $filelist_filename . ")" . "<br>";
1656                         $page_found = 0;
1658                 }
1660                 $string_handle  = $database_handle->prepare('VACUUM');
1661                 $string_handle->execute();
1663                 my $database_out        = kiriwrite_convert($database, "normal_display");
1664                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
1666                 $pagedata = "<h2>Selected pages deleted</h2>";
1667                 $pagedata = $pagedata . "The following pages were deleted from the '" . $database_name_out . "' database:<br><br>";
1668                 $pagedata = $pagedata . "<div class=\"datalist\">";
1669                 $pagedata = $pagedata . $deleted_list;
1670                 $pagedata = $pagedata . "</div><br>";
1671                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database.</a>";
1673                 return $pagedata;
1675         } elsif ($confirm eq 0){
1677                 # The action to delete multiple pages from the database has
1678                 # not been confirmed, so write a form asking the user to confirm
1679                 # the deletion of those pages.
1681                 # Load the SQLite Database and get info about the database.
1683                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1684                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
1685                 $string_handle->execute();
1687                 my @database_info       = $string_handle->fetchrow_array();
1688                 my $database_name       = $database_info[0];
1690                 # Define some variables for later.
1692                 my @page_info;
1693                 my @pagenames;
1694                 my @filenames;
1695                 my $pagename;
1696                 my $filelist_filename;
1697                 my $filelist_filename_sql;
1698                 my $database_name_out;
1699                 my $pagenameslist;
1700                 my $pagenameslist_out;
1701                 my $pageseek = 0;
1702                 my $pagefound = 0;
1704                 # Process each filename given.
1706                 foreach $filelist_filename (@filelist){
1708                         $filelist_filename_sql  = kiriwrite_convert($filelist_filename, "kiriwrite");
1709                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
1711                         $string_handle->execute();
1713                         # Check if the filename given exists and if it
1714                         # doesn't then skip this filename and go onto
1715                         # the next filename.
1717                         while (@page_info = $string_handle->fetchrow_array()){
1719                                 $pagefound = 1;
1721                         }
1722                         
1723                         if ($pagefound eq 0){
1725                                 next;
1727                         }
1729                         # Get the data again.
1731                         $string_handle->execute();
1732                         @page_info      = $string_handle->fetchrow_array();
1734                         # Add the page name and file name to their seperate
1735                         # arrays.
1737                         $pagenames[$pageseek]   = $page_info[1];
1738                         $filenames[$pageseek]   = $page_info[0];
1740                         # Increment the page seek counter and reset the
1741                         # page found value.
1743                         $pageseek++;
1744                         $pagefound = 0;
1746                 }
1748                 $pageseek = 0;
1750                 $pagenameslist = "";
1752                 foreach $pagename (@pagenames){
1754                         if (!$pagename){
1756                                 $pagenameslist = $pagenameslist . "<i>No page name</i> (" . $filenames[$pageseek] . ")<br>";                            
1758                         } else {
1760                                 $pagenameslist = $pagenameslist . kiriwrite_convert($pagename, "normal_display") . " (" . kiriwrite_convert($filenames[$pageseek], "normal_display") . ")<br>";
1762                         }
1764                         $pageseek++;
1766                 }
1768                 # Check if any files were selected and return
1769                 # an error if there wasn't.
1771                 if ($pageseek eq 0){
1773                         # No pages were selected so return an error.
1775                         kiriwrite_error("nopagesselected");
1777                 }
1779                 # Convert the values so that they display properly.
1781                 $database_name_out      = kiriwrite_convert($database_name, "normal_display");
1782                 my $database_out                = kiriwrite_convert($database, "normal_display");
1784                 # Write the form for displaying pages.
1786                 $pagedata = "<h2>Delete multiple pages</h2>";
1787                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
1788                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
1789                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"multidelete\">";
1790                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_out . "\">";
1791                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
1792                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . $pageseek . "\">";
1794                 $pageseek = 1;
1796                 foreach $filelist_filename (@filenames){
1798                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"name[" . $pageseek . "]\" value=\"on\">";
1799                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"id[" . $pageseek . "]\" value=\"" . $filenames[$pageseek - 1] . "\">";
1801                         $pageseek++;
1803                 }
1805                 
1807                 $pagedata = $pagedata . "Are you sure you want to delete the selected pages below from the '" . $database_name_out . "' database?<br><br>";
1808                 $pagedata = $pagedata . "<div class=\"datalist\">" . $pagenameslist . "</div><br>";
1809                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Yes, delete selected\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">No, return to the list for '" . $database_name_out . "' database.</a>";
1810                 $pagedata = $pagedata . "</form>";
1812                 return $pagedata;
1814         } else {
1816                 # A confirm value other than 0 or 1 is given, so
1817                 # return an error.
1819                 kiriwrite_error("invaildvalue");
1821         }
1825 sub kiriwrite_page_multimove{
1826 #################################################################################
1827 # kiriwrite_page_multimove: Move several pages from one database to another     #
1828 # database.                                                                     #
1829 #                                                                               #
1830 # Usage:                                                                        #
1831 #                                                                               #
1832 # kiriwrite_page_multimove(database, newdatabase, confirm, filelist);           #
1833 #                                                                               #
1834 # database      Specifies the database to move the selected pages from.         #
1835 # newdatabase   Specifies the database to move the selected pages to.           #
1836 # confirm       Confirms the action to move the pages from one database to      #
1837 #               another.                                                        #
1838 # filelist      Specifies the list of pages to move.                            #
1839 #################################################################################
1841         # Get the values that were passed to the subroutine.
1843         my ($database, $newdatabase, $confirm, @filelist) = @_;
1845         # Load the required Perl modules.
1847         use DBI;
1849         # Define a variable for later.
1851         my $pagedata = "";
1853         # Check if the file list is blank and return an error
1854         # if it is.
1856         if (!@filelist){
1858                 # The file list really is blank so return
1859                 # an error.
1861                 kiriwrite_error("nopagesselected");
1863         }
1865         # Check if the file permissions on the original database
1866         # are valid and return an error if they aren't.
1868         my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
1869         my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
1871         if ($database_exists eq 1){
1873                 # The database does not exist, so return an error.
1875                 kiriwrite_error("oldmovedatabasedoesnotexist");
1877         }
1878                 
1879         if ($database_permissions eq 1){
1881                 # The database permissions are invalid, so return
1882                 # an error.
1884                 kiriwrite_error("oldmovedatabasefileinvalidpermissions");
1886         }
1888         # Check if the confirm value is blank and if it is then
1889         # set the confirm value to 0.
1891         if (!$confirm){
1893                 $confirm = 0;
1895         }
1897         if ($confirm eq 1){
1899                 # The action to move several pages from one database
1900                 # to another has been confirmed.
1902                 # Check if the database that the pages are moving from 
1903                 # is the same as the database the pages are moving to.
1904                 # Return an error if it is.
1906                 if ($database eq $newdatabase){
1908                         # The database that the pages are moving from
1909                         # and the database the pages are moving to
1910                         # is the same, so return an error.
1912                         kiriwrite_error("databasemovesame");
1914                 }
1916                 # Check that the new database exists and the permissions
1917                 # for the new database are valid. 
1919                 $database_exists                = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db');
1920                 $database_permissions           = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db', 1, 1, 0);              
1922                 if ($database_exists eq 1){
1924                         # The database does not exist, so return an error.
1926                         kiriwrite_error("newmovedatabasedoesnotexist");
1928                 }
1929                 
1930                 if ($database_permissions eq 1){
1932                         # The database permissions are invalid, so return
1933                         # an error.
1935                         kiriwrite_error("newmovedatabasefileinvalidpermissions");
1937                 }
1939                 # Define some values for later.
1941                 my @olddatabase_info;
1942                 my @olddatabase_page;
1943                 my @newdatabase_info;
1944                 my @newdatabase_page;
1946                 my $filename;
1947                 my $filename_sql;
1948                 my $filename_out;
1950                 my $olddatabase_database_handle;
1951                 my $olddatabase_string_handle;
1952                 my $olddatabase_name;
1953                 my $olddatabase_name_out;
1954                 my $olddatabase_out;
1955                 my $newdatabase_database_handle;
1956                 my $newdatabase_string_handle;
1957                 my $newdatabase_name;
1958                 my $newdatabase_name_out;
1959                 my $newdatabase_out;
1961                 my $warninglist = "";
1962                 my $movedlist = "";
1964                 my $page_filename;
1965                 my $page_filename_sql;
1966                 my $page_filename_out;
1967                 my $page_name;
1968                 my $page_name_sql;
1969                 my $page_name_out;
1970                 my $page_description;
1971                 my $page_description_sql;
1972                 my $page_section;
1973                 my $page_section_sql;
1974                 my $page_template;
1975                 my $page_template_sql;
1976                 my $page_data;
1977                 my $page_data_sql;
1978                 my $page_settings;
1979                 my $page_settings_sql;
1980                 my $page_lastmodified;
1981                 my $page_lastmodified_sql;
1983                 my $page_found = 0;
1985                 # Check if the database filename is valid and return an error if
1986                 # it isn't.
1988                 my $newpagedatabase_filename_check = kiriwrite_variablecheck($newdatabase, "filename", 0, 1);
1990                 if ($newpagedatabase_filename_check eq 1){
1992                         # The database filename is blank, so return an error.
1994                         kiriwrite_error("blankdatabasepageadd");
1996                 } elsif ($newpagedatabase_filename_check eq 2){
1998                         # The database filename is invalid, so return an error.
2000                         kiriwrite_error("databasefilenameinvalid");
2002                 }
2004                 # Load the old and new SQLite databases.
2006                 $olddatabase_database_handle    = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
2007                 $newdatabase_database_handle    = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db');
2009                 # Get the database information from both databases.
2011                 $olddatabase_string_handle      = $olddatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("oldmovedatabasefileinvalid");
2012                 $newdatabase_string_handle      = $newdatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("newmovedatabasefileinvalid");
2013                 $olddatabase_string_handle->execute();
2014                 $newdatabase_string_handle->execute();
2016                 @olddatabase_info       = $olddatabase_string_handle->fetchrow_array();
2017                 @newdatabase_info       = $newdatabase_string_handle->fetchrow_array();
2019                 $olddatabase_name       = $olddatabase_info[0];
2020                 $newdatabase_name       = $newdatabase_info[0];
2022                 # Convert the database names so that they can be displayed
2023                 # properly.
2025                 $olddatabase_name_out   = kiriwrite_convert($olddatabase_name, "normal_display");
2026                 $newdatabase_name_out   = kiriwrite_convert($newdatabase_name, "normal_display");
2027                 $olddatabase_out        = kiriwrite_convert($database, "normal_display");
2028                 $newdatabase_out        = kiriwrite_convert($newdatabase, "normal_display");
2030                 # Get each file in the old database, get the file values,
2031                 # put them into the new database and delete the pages
2032                 # from the old database.
2034                 foreach $filename (@filelist){
2036                         # Check if the filename exists in the old database. 
2037                         # If the filename does not exist then write
2038                         # a warning and process the next file.
2040                         $filename_sql                   = kiriwrite_convert($filename, "kiriwrite");
2041                         $filename_out                   = kiriwrite_convert($filename, "normal_display");
2042                         $olddatabase_string_handle      = $olddatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_pages where filename = \'' . $filename_sql . '\' LIMIT 1');
2043                         $olddatabase_string_handle->execute();
2045                         $page_found = 0;
2047                         while (@olddatabase_page = $olddatabase_string_handle->fetchrow_array()){
2049                                 # The page in the old database does exist so 
2050                                 # set the value to 1.
2052                                 $page_found = 1;
2054                         }
2056                         # See if the page was found and write a warning if it
2057                         # wasn't.
2059                         if ($page_found eq 0){
2061                                 # The page was not found. Write a warning and
2062                                 # process the next page.
2064                                 $filename_out   = kiriwrite_convert($filename, "normal_display");
2065                                 $warninglist    = $warninglist . "The page with the filename '" . $filename_out . "' could not be found in the '" . $olddatabase_name_out . "' database. Page skipped.<br>";
2066                                 next;
2068                         }
2070                         # Check if the filename exists in the new database.
2071                         # If the filename does exist then write
2072                         # a warning and process the next file.
2074                         $newdatabase_string_handle      = $newdatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_pages where filename = \'' . $filename_sql . '\' LIMIT 1');
2075                         $newdatabase_string_handle->execute();
2077                         $page_found = 0;
2079                         while (@newdatabase_page = $newdatabase_string_handle->fetchrow_array()){
2081                                 # The page in the old database does exist so 
2082                                 # set the value to 1.
2084                                 $page_found = 1;
2086                         }
2088                         # See if the page was found and write a warning if it
2089                         # wasn't.
2091                         if ($page_found eq 1){
2093                                 # The page was not found. Write a warning and
2094                                 # process the next page.
2096                                 $filename_out   = kiriwrite_convert($filename, "normal_display");
2097                                 $warninglist    = $warninglist . "The page with the filename '" . $filename_out . "' already exists in '" . $olddatabase_name_out . "' database. Page skipped.<br>";
2098                                 next;
2100                         }
2103                         # Get the information about the page.
2105                         $olddatabase_string_handle->execute();
2106                         @olddatabase_page       = $olddatabase_string_handle->fetchrow_array();
2107                         $page_filename          = $olddatabase_page[0];
2108                         $page_name              = $olddatabase_page[1];
2109                         $page_description       = $olddatabase_page[2];
2110                         $page_section           = $olddatabase_page[3];
2111                         $page_template          = $olddatabase_page[4];
2112                         $page_data              = $olddatabase_page[5];
2113                         $page_settings          = $olddatabase_page[6];
2114                         $page_lastmodified      = $olddatabase_page[7];
2116                         # Convert the values so that they can be processed
2117                         # properly.
2119                         $page_filename_sql      = kiriwrite_convert($page_filename, "kiriwrite");
2120                         $page_name_sql          = kiriwrite_convert($page_name, "kiriwrite");
2121                         $page_description_sql   = kiriwrite_convert($page_description, "kiriwrite");
2122                         $page_section_sql       = kiriwrite_convert($page_section, "kiriwrite");
2123                         $page_template_sql      = kiriwrite_convert($page_template, "kiriwrite");
2124                         $page_data_sql          = kiriwrite_convert($page_data, "kiriwrite");
2125                         $page_settings_sql      = kiriwrite_convert($page_settings, "kiriwrite");
2126                         $page_lastmodified_sql  = kiriwrite_convert($page_lastmodified, "kiriwrite");
2128                         $page_filename_out      = kiriwrite_convert($page_filename, "normal_display");
2129                         $page_name_out          = kiriwrite_convert($page_name, "normal_display");
2131                         # Add the page to the new database.
2133                         $newdatabase_string_handle      = $newdatabase_database_handle->prepare('INSERT INTO kiriwrite_database_pages VALUES(
2134                                 \'' . $page_filename_sql . '\',
2135                                 \'' . $page_name_sql . '\',
2136                                 \'' . $page_description_sql . '\',
2137                                 \'' . $page_section_sql . '\',
2138                                 \'' . $page_template_sql . '\',
2139                                 \'' . $page_data_sql . '\',
2140                                 \'' . $page_settings_sql . '\',
2141                                 \'' . $page_lastmodified_sql . '\'
2142                         )');
2143                         $newdatabase_string_handle->execute();
2145                         # Delete the page from the old database.
2147                         $olddatabase_string_handle      = $olddatabase_database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $filename_sql . '\'');
2148                         $olddatabase_string_handle->execute();
2150                         # Append the moved page (filename and name) to the list of
2151                         # moved pages.
2153                         if (!$page_name_out){
2155                                 $page_name_out = "<i>No Name</i>";
2157                         }
2159                         $movedlist = $movedlist . $page_name_out . " (" . $filename_out . ")" . "<br>";
2161                 }
2163                 # Write out a message saying that the pages were moved (if any)
2164                 # to the new database (and any warnings given).
2166                 $pagedata = $pagedata . "<h2>Move multiple pages</h2>";
2168                 if ($movedlist){
2169                 
2170                 $pagedata = $pagedata . "The following pages from the '" . $olddatabase_name_out . "' database were moved to the '" . $newdatabase_name_out . "' database:<br><br>";
2171                 $pagedata = $pagedata . "<div class=\"datalist\">";
2172                 $pagedata = $pagedata . $movedlist;
2173                 $pagedata = $pagedata . "</div><br><br>";
2174                 
2175                 } else {
2177                         $pagedata = $pagedata . "No pages were moved from the '" . $olddatabase_name_out  . "' database to the '" . $newdatabase_name_out . "' database.<br><br>";
2179                 }
2181                 if ($warninglist){
2183                         $pagedata = $pagedata . "The following errors/warnings have occured while moving the pages:<br><br>";
2184                         $pagedata = $pagedata . "<div class=\"datalist\">";
2185                         $pagedata = $pagedata . $warninglist;
2186                         $pagedata = $pagedata . "</div><br><br>";
2188                 }
2190                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $olddatabase_out . "\">Return to the page list for the '" . $olddatabase_name_out . "' database.</a> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $newdatabase_out . "\">View the page list for the '" . $newdatabase_name_out . "' database.</a>";
2192                 return $pagedata;
2194         } elsif ($confirm eq 0) {
2196                 # The action to move several pages from one database
2197                 # to another has not been confirmed so write a form.
2199                 # Load the SQLite database and get the needed
2200                 # information about the database.
2202                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
2203                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
2204                 $string_handle->execute();
2206                 my @database_info       = $string_handle->fetchrow_array();
2207                 my $database_name       = $database_info[0];
2209                 # Define some values for later.
2211                 my $database_info;
2212                 my @page_info;
2213                 my @pagenames;
2214                 my @filenames;
2215                 my $data_file;
2216                 my $data_file_length;
2217                 my $database_list;
2218                 my $database_filename;
2219                 my $filename;
2220                 my $filename_out;
2221                 my $filelist_filename_sql;
2222                 my $filelist_filename;
2223                 my $pagelist_formdata;
2224                 my $newdatabase_filename;
2225                 my $newdatabase_filename_out;
2226                 my $newdatabase_name;
2227                 my $newdatabase_name_out;
2228                 my $pagelist;
2229                 my $pagename;
2230                 my $pageseek    = 0;
2231                 my $dbseek      = 0;
2232                 my $pagefound   = 0;
2234                 # Process each filename given.
2236                 foreach $filelist_filename (@filelist){
2238                         $filelist_filename_sql  = kiriwrite_convert($filelist_filename, "kiriwrite");
2239                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
2241                         $string_handle->execute();
2243                         # Check if the filename given exists and if it
2244                         # doesn't then skip this filename and go onto
2245                         # the next filename.
2247                         while (@page_info = $string_handle->fetchrow_array()){
2249                                 $pagefound = 1;
2251                         }
2252                         
2253                         if ($pagefound eq 0){
2255                                 next;
2257                         }
2259                         # Get the data again.
2261                         $string_handle->execute();
2262                         @page_info      = $string_handle->fetchrow_array();
2264                         # Add the page name and file name to their seperate
2265                         # arrays.
2267                         $pagenames[$pageseek]   = $page_info[1];
2268                         $filenames[$pageseek]   = $page_info[0];
2270                         # Increment the page seek counter and reset the
2271                         # page found value.
2273                         $pageseek++;
2274                         $pagefound = 0;
2276                 }
2278                 # Check if any pages exust and return an error if
2279                 # there wasn't.
2281                 if ($pageseek eq 0){
2283                         # None of the selected pages exist, so return
2284                         # an error.
2286                         kiriwrite_error("nopagesselected");
2288                 }
2290                 # Create the page list of files to move.
2292                 $pageseek               = 1;
2293                 $pagelist               = "";
2294                 $pagelist_formdata      = "";
2296                 foreach $filename (@filenames){
2300                         $pagename       = $pagenames[$pageseek - 1];
2302                         # Check if the page name is blank and if it is then say
2303                         # no name.
2305                         if (!$pagename){
2307                                 # The page name is blank.
2309                                 $pagelist       = $pagelist . "<i>No Name</i>" . " (" . $filename . ")<br>";
2311                         } else {
2313                                 # Append the page name and filename to the page list. 
2315                                 $pagelist       = $pagelist . kiriwrite_convert($pagename, "normal_display") . " (" . $filename . ")<br>";
2317                         }
2319                         # Append the form data to the page list form data.
2321                         $filename_out           = kiriwrite_convert($filename, "normal_display");
2323                         $pagelist_formdata      = $pagelist_formdata . "<input type=\"hidden\" name=\"name[" . $pageseek . "]\" value=\"on\">";
2324                         $pagelist_formdata      = $pagelist_formdata . "<input type=\"hidden\" name=\"id[" . $pageseek . "]\" value=\"" . $filename . "\">";
2326                         $pageseek++;
2328                 }
2330                 # Get the database filenames and names.
2332                 # Check the directory to make sure the permissions are settings are valid
2333                 # and return an error if the permission settings are invalid.
2335                 my $data_directory_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"}, 1, 0);
2337                 if ($data_directory_permissions eq 1){
2339                         # The data directory has invalid permissions set, so return an error.
2341                         kiriwrite_error("datadirectoryinvalidpermissions");
2343                 }
2345                 # Open the directory and get the list of all files ending with .xml and
2346                 # put them into the data_directory array.
2348                 opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
2349                 my @data_directory = grep /m*\.db/, readdir(DATADIR);
2350                 closedir(DATADIR);
2352                 # Process each database to get the database name.
2354                 $database_list = "<select name=\"newdatabase\">";
2356                 foreach $data_file (@data_directory){
2358                         # Get the length of the filename.
2360                         $data_file_length       = length($data_file);
2361                         
2362                         # Remove the last three characters from the filename.
2364                         $data_file              = substr($data_file, 0, $data_file_length - 3);
2366                         # Check if the permissions are valid before trying
2367                         # to load the database.
2369                         $database_filename      = $kiriwrite_config{"directory_data_db"} . '/' . $data_file . '.db';
2370                         $database_permissions   = kiriwrite_filepermissions($database_filename, 1, 0, 0);
2372                         if ($database_permissions eq 1){
2374                                 # If the database has invalid permissions set then
2375                                 # process the next database.
2377                                 next;
2379                         }
2381                         # Load the SQLite database.
2382                 
2383                         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $database_filename);
2385                         # Query the SQLite database or return an error (meaning that the database is in
2386                         # a invalid format), if it can't then process the next database.
2388                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or next;
2390                         $string_handle->execute();
2391                         @database_info = $string_handle->fetchrow_array();
2393                         # Get the values from the query.
2395                         $newdatabase_name       = $database_info[0];
2397                         # Convert the values so that they can be displayed
2398                         # properly.
2400                         $newdatabase_filename_out       = kiriwrite_convert($data_file, "normal_display");
2401                         $newdatabase_name_out           = kiriwrite_convert($newdatabase_name, "normal_display");
2403                         # Append the database filename and name to the list of databases
2404                         # to move the pages to.
2406                         $database_list = $database_list . "<option value=\"" . $newdatabase_filename_out . "\">" . $newdatabase_name_out . "</option>";
2408                 }
2410                 $database_list = $database_list . "</select>";
2412                 # Convert some values so that they display properly.
2414                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
2415                 my $database_out        = kiriwrite_convert($database, "normal_display");
2417                 # Write out the form.
2419                 $pagedata = "<h2>Move selected pages</h2>";
2420                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
2421                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
2422                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"multimove\">";
2423                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_out . "\">";
2424                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . ($pageseek - 1) . "\">";
2425                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
2426                 $pagedata = $pagedata . $pagelist_formdata;
2427                 $pagedata = $pagedata . "Which database do you want to move the following pages from the '" . $database_name_out . "' database to?<br><br>";
2428                 $pagedata = $pagedata . "<div class=\"datalist\">";
2429                 $pagedata = $pagedata . $pagelist;
2430                 $pagedata = $pagedata . "</div><br>";
2431                 $pagedata = $pagedata . "Move pages to: " . $database_list . "<br><br>";
2432                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Move pages to the selected database.\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database.</a>";
2433                 $pagedata = $pagedata . "</form>";
2435                 return $pagedata;
2437         } else {
2439                 # The confirm value is other than 0 or 1, so return
2440                 # an error.
2442                 kiriwrite_error("invalidvariable");
2444         }
2450 sub kiriwrite_page_multicopy{
2451 #################################################################################
2452 # kiriwrite_page_multicopy: Copy several pages from one database to another     #
2453 # database.                                                                     #
2454 #                                                                               #
2455 # Usage:                                                                        #
2456 #                                                                               #
2457 # kiriwrite_page_multicopy(database, newdatabase, confirm, filelist);           #
2458 #                                                                               #
2459 # database      Specifies the database to copy the selected pages from.         #
2460 # newdatabase   Specifies the database to copy the selected page to.            #
2461 # confirm       Confirms the action to copy the pages.                          #
2462 # filelist      A list of filenames to copy in an array.                        #
2463 #################################################################################
2465         # Get the values that were passed to the subroutine.
2467         my ($database, $newdatabase, $confirm, @filelist) = @_;
2469         # Load the required Perl modules.
2471         use DBI;
2473         # Define a variable for later.
2475         my $pagedata = "";
2477         # Check if the file list is blank and return an error
2478         # if it is.
2480         if (!@filelist){
2482                 # The file list really is blank so return
2483                 # an error.
2485                 kiriwrite_error("nopagesselected");
2487         }
2489         # Check if the file permissions on the original database
2490         # are valid and return an error if they aren't.
2492         my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
2493         my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 0);
2495         if ($database_exists eq 1){
2497                 # The database does not exist, so return an error.
2499                 kiriwrite_error("oldcopydatabasedoesnotexist");
2501         }
2502                 
2503         if ($database_permissions eq 1){
2505                 # The database permissions are invalid, so return
2506                 # an error.
2508                 kiriwrite_error("oldcopydatabasefilenameinvalidpermissions");
2510         }
2512         # Check if the confirm value is blank and if it is then
2513         # set the confirm value to 0.
2515         if (!$confirm){
2517                 $confirm = 0;
2519         }
2521         # Check if the database filename is valid and return an error if
2522         # it isn't.
2524         my $pagedatabase_filename_check = kiriwrite_variablecheck($database, "filename", 0, 1);
2526         if ($pagedatabase_filename_check eq 1){
2528                 # The database filename is blank, so return an error.
2530                 kiriwrite_error("blankdatabasepageadd");
2532         } elsif ($pagedatabase_filename_check eq 2){
2534                 # The database filename is invalid, so return an error.
2536                 kiriwrite_error("databasefilenameinvalid");
2538         }
2540         if ($confirm eq 1){
2542                 # The action to copy several pages from one database
2543                 # to another has been confirmed.
2545                 # Check if the database that the pages are copied from 
2546                 # is the same as the database the pages are copied to.
2547                 # Return an error if it is.
2549                 if ($database eq $newdatabase){
2551                         # The database that the pages are being copied from
2552                         # and the database that the pages are copied to
2553                         # is the same, so return an error.
2555                         kiriwrite_error("databasecopysame");
2557                 }
2559                 # Check that the new database exists and the permissions
2560                 # for the new database are valid. 
2562                 $database_exists                = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db');
2563                 $database_permissions           = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db', 1, 1, 0);              
2565                 if ($database_exists eq 1){
2567                         # The database does not exist, so return an error.
2569                         kiriwrite_error("newcopydatabasedoesnotexist");
2571                 }
2572                 
2573                 if ($database_permissions eq 1){
2575                         # The database permissions are invalid, so return
2576                         # an error.
2578                         kiriwrite_error("newcopydatabasefileinvalidpermissions");
2580                 }
2582                 # Define some values for later.
2584                 my @olddatabase_info;
2585                 my @olddatabase_page;
2586                 my @newdatabase_info;
2587                 my @newdatabase_page;
2589                 my $filename;
2590                 my $filename_sql;
2591                 my $filename_out;
2593                 my $olddatabase_database_handle;
2594                 my $olddatabase_string_handle;
2595                 my $olddatabase_name;
2596                 my $olddatabase_name_out;
2597                 my $olddatabase_out;
2598                 my $newdatabase_database_handle;
2599                 my $newdatabase_string_handle;
2600                 my $newdatabase_name;
2601                 my $newdatabase_name_out;
2602                 my $newdatabase_out;
2604                 my $warninglist = "";
2605                 my $copiedlist = "";
2607                 my $page_filename;
2608                 my $page_filename_sql;
2609                 my $page_filename_out;
2610                 my $page_name;
2611                 my $page_name_sql;
2612                 my $page_name_out;
2613                 my $page_description;
2614                 my $page_description_sql;
2615                 my $page_section;
2616                 my $page_section_sql;
2617                 my $page_template;
2618                 my $page_template_sql;
2619                 my $page_data;
2620                 my $page_data_sql;
2621                 my $page_settings;
2622                 my $page_settings_sql;
2623                 my $page_lastmodified;
2624                 my $page_lastmodified_sql;
2626                 my $page_found = 0;
2628                 # Check if the database filename is valid and return an error if
2629                 # it isn't.
2631                 my $newpagedatabase_filename_check = kiriwrite_variablecheck($newdatabase, "filename", 0, 1);
2633                 if ($newpagedatabase_filename_check eq 1){
2635                         # The database filename is blank, so return an error.
2637                         kiriwrite_error("blankdatabasepageadd");
2639                 } elsif ($newpagedatabase_filename_check eq 2){
2641                         # The database filename is invalid, so return an error.
2643                         kiriwrite_error("databasefilenameinvalid");
2645                 }
2647                 # Load the old and new SQLite databases.
2649                 $olddatabase_database_handle    = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
2650                 $newdatabase_database_handle    = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $newdatabase . '.db');
2652                 # Get the database information from both databases.
2654                 $olddatabase_string_handle      = $olddatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("oldcopydatabasefileinvalid");
2655                 $newdatabase_string_handle      = $newdatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("newcopydatabasefileinvalid");
2656                 $olddatabase_string_handle->execute();
2657                 $newdatabase_string_handle->execute();
2659                 @olddatabase_info       = $olddatabase_string_handle->fetchrow_array();
2660                 @newdatabase_info       = $newdatabase_string_handle->fetchrow_array();
2662                 $olddatabase_name       = $olddatabase_info[0];
2663                 $newdatabase_name       = $newdatabase_info[0];
2665                 # Convert the database names so that they can be displayed
2666                 # properly.
2668                 $olddatabase_name_out   = kiriwrite_convert($olddatabase_name, "normal_display");
2669                 $newdatabase_name_out   = kiriwrite_convert($newdatabase_name, "normal_display");
2670                 $olddatabase_out        = kiriwrite_convert($database, "normal_display");
2671                 $newdatabase_out        = kiriwrite_convert($newdatabase, "normal_display");
2673                 # Get each file in the old database, get the file values,
2674                 # put them into the new database.
2676                 foreach $filename (@filelist){
2678                         # Check if the filename exists in the old database. 
2679                         # If the filename does not exist then write
2680                         # a warning and process the next file.
2682                         $filename_sql                   = kiriwrite_convert($filename, "kiriwrite");
2683                         $filename_out                   = kiriwrite_convert($filename, "normal_display");
2684                         $olddatabase_string_handle      = $olddatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_pages where filename = \'' . $filename_sql . '\' LIMIT 1');
2685                         $olddatabase_string_handle->execute();
2687                         $page_found = 0;
2689                         while (@olddatabase_page = $olddatabase_string_handle->fetchrow_array()){
2691                                 # The page in the old database does exist so 
2692                                 # set the value to 1.
2694                                 $page_found = 1;
2696                         }
2698                         # See if the page was found and write a warning if it
2699                         # wasn't.
2701                         if ($page_found eq 0){
2703                                 # The page was not found. Write a warning and
2704                                 # process the next page.
2706                                 $filename_out   = kiriwrite_convert($filename, "normal_display");
2707                                 $warninglist    = $warninglist . "The page with the filename '" . $filename_out . "' could not be found in the '" . $olddatabase_name_out . "' database. Page skipped.<br>";
2708                                 next;
2710                         }
2712                         # Check if the filename exists in the new database.
2713                         # If the filename does exist then write
2714                         # a warning and process the next file.
2716                         $newdatabase_string_handle      = $newdatabase_database_handle->prepare('SELECT * FROM kiriwrite_database_pages where filename = \'' . $filename_sql . '\' LIMIT 1');
2717                         $newdatabase_string_handle->execute();
2719                         $page_found = 0;
2721                         while (@newdatabase_page = $newdatabase_string_handle->fetchrow_array()){
2723                                 # The page in the old database does exist so 
2724                                 # set the value to 1.
2726                                 $page_found = 1;
2728                         }
2730                         # See if the page was found and write a warning if it
2731                         # wasn't.
2733                         if ($page_found eq 1){
2735                                 # The page was not found. Write a warning and
2736                                 # process the next page.
2738                                 $filename_out   = kiriwrite_convert($filename, "normal_display");
2739                                 $warninglist    = $warninglist . "The page with the filename '" . $filename_out . "' already exists in '" . $newdatabase_name_out . "' database. Page skipped.<br>";
2740                                 next;
2742                         }
2745                         # Get the information about the page.
2747                         $olddatabase_string_handle->execute();
2748                         @olddatabase_page       = $olddatabase_string_handle->fetchrow_array();
2749                         $page_filename          = $olddatabase_page[0];
2750                         $page_name              = $olddatabase_page[1];
2751                         $page_description       = $olddatabase_page[2];
2752                         $page_section           = $olddatabase_page[3];
2753                         $page_template          = $olddatabase_page[4];
2754                         $page_data              = $olddatabase_page[5];
2755                         $page_settings          = $olddatabase_page[6];
2756                         $page_lastmodified      = $olddatabase_page[7];
2758                         # Convert the values so that they can be processed
2759                         # properly.
2761                         $page_filename_sql      = kiriwrite_convert($page_filename, "kiriwrite");
2762                         $page_name_sql          = kiriwrite_convert($page_name, "kiriwrite");
2763                         $page_description_sql   = kiriwrite_convert($page_description, "kiriwrite");
2764                         $page_section_sql       = kiriwrite_convert($page_section, "kiriwrite");
2765                         $page_template_sql      = kiriwrite_convert($page_template, "kiriwrite");
2766                         $page_data_sql          = kiriwrite_convert($page_data, "kiriwrite");
2767                         $page_settings_sql      = kiriwrite_convert($page_settings, "kiriwrite");
2768                         $page_lastmodified_sql  = kiriwrite_convert($page_lastmodified, "kiriwrite");
2770                         $page_filename_out      = kiriwrite_convert($page_filename, "normal_display");
2771                         $page_name_out          = kiriwrite_convert($page_name, "normal_display");
2773                         # Add the page to the new database.
2775                         $newdatabase_string_handle      = $newdatabase_database_handle->prepare('INSERT INTO kiriwrite_database_pages VALUES(
2776                                 \'' . $page_filename_sql . '\',
2777                                 \'' . $page_name_sql . '\',
2778                                 \'' . $page_description_sql . '\',
2779                                 \'' . $page_section_sql . '\',
2780                                 \'' . $page_template_sql . '\',
2781                                 \'' . $page_data_sql . '\',
2782                                 \'' . $page_settings_sql . '\',
2783                                 \'' . $page_lastmodified_sql . '\'
2784                         )');
2785                         $newdatabase_string_handle->execute();
2787                         # Append the copied page (filename and name) to the list of
2788                         # copied pages.
2790                         if (!$page_name_out){
2792                                 $page_name_out = "<i>No page name</i>";
2794                         }
2796                         $copiedlist = $copiedlist . $page_name_out . " (" . $filename_out . ")" . "<br>";
2798                 }
2800                 # Write out a message saying that the pages were moved (if any)
2801                 # to the new database (and any warnings given).
2803                 $pagedata = $pagedata . "<h2>Copy multiple pages</h2>";
2805                 if ($copiedlist){
2806                 
2807                 $pagedata = $pagedata . "The following pages from the '" . $olddatabase_name_out . "' database were copied to the '" . $newdatabase_name_out . "' database:<br><br>";
2808                 $pagedata = $pagedata . "<div class=\"datalist\">";
2809                 $pagedata = $pagedata . $copiedlist;
2810                 $pagedata = $pagedata . "</div><br><br>";
2811                 
2812                 } else {
2814                         $pagedata = $pagedata . "No pages were copied from the '" . $olddatabase_name_out  . "' database to the '" . $newdatabase_name_out . "' database.<br><br>";
2816                 }
2818                 if ($warninglist){
2820                         $pagedata = $pagedata . "The following errors/warnings have occured while copying the pages:<br><br>";
2821                         $pagedata = $pagedata . "<div class=\"datalist\">";
2822                         $pagedata = $pagedata . $warninglist;
2823                         $pagedata = $pagedata . "</div><br><br>";
2825                 }
2827                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $olddatabase_out . "\">Return to the page list for the '" . $olddatabase_name_out . "' database.</a> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $newdatabase_out . "\">View the page list for the '" . $newdatabase_name_out . "' database.</a>";
2829                 return $pagedata;
2831         } elsif ($confirm eq 0) {
2833                 # The action to copy several pages from one database
2834                 # to another has not been confirmed so write a form.
2836                 # Load the SQLite database and get the needed
2837                 # information about the database.
2839                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
2840                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
2841                 $string_handle->execute();
2843                 my @database_info       = $string_handle->fetchrow_array();
2844                 my $database_name       = $database_info[0];
2846                 # Define some values for later.
2848                 my $database_info;
2849                 my @page_info;
2850                 my @pagenames;
2851                 my @filenames;
2852                 my $data_file;
2853                 my $data_file_length;
2854                 my $database_list;
2855                 my $database_filename;
2856                 my $filename;
2857                 my $filename_out;
2858                 my $filelist_filename_sql;
2859                 my $filelist_filename;
2860                 my $pagelist_formdata;
2861                 my $newdatabase_filename;
2862                 my $newdatabase_filename_out;
2863                 my $newdatabase_name;
2864                 my $newdatabase_name_out;
2865                 my $pagelist;
2866                 my $pagename;
2867                 my $pageseek    = 0;
2868                 my $dbseek      = 0;
2869                 my $pagefound   = 0;
2871                 # Process each filename given.
2873                 foreach $filelist_filename (@filelist){
2875                         $filelist_filename_sql  = kiriwrite_convert($filelist_filename, "kiriwrite");
2876                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
2878                         $string_handle->execute();
2880                         # Check if the filename given exists and if it
2881                         # doesn't then skip this filename and go onto
2882                         # the next filename.
2884                         while (@page_info = $string_handle->fetchrow_array()){
2886                                 $pagefound = 1;
2888                         }
2889                         
2890                         if ($pagefound eq 0){
2892                                 next;
2894                         }
2896                         # Get the data again.
2898                         $string_handle->execute();
2899                         @page_info      = $string_handle->fetchrow_array();
2901                         # Add the page name and file name to their seperate
2902                         # arrays.
2904                         $pagenames[$pageseek]   = $page_info[1];
2905                         $filenames[$pageseek]   = $page_info[0];
2907                         # Increment the page seek counter and reset the
2908                         # page found value.
2910                         $pageseek++;
2911                         $pagefound = 0;
2913                 }
2915                 # Check if any pages exust and return an error if
2916                 # there wasn't.
2918                 if ($pageseek eq 0){
2920                         # None of the selected pages exist, so return
2921                         # an error.
2923                         kiriwrite_error("nopagesselected");
2925                 }
2927                 # Create the page list of files to move.
2929                 $pageseek               = 1;
2930                 $pagelist               = "";
2931                 $pagelist_formdata      = "";
2933                 foreach $filename (@filenames){
2935                         $pagename       = $pagenames[$pageseek - 1];
2937                         # Check if the 
2939                         if (!$pagename){
2941                                 # Write a message saying that there is no page name.
2943                                 $pagelist       = $pagelist . "<i>No Name</i>" . " (" . $filename . ")<br>";
2945                         } else {
2947                                 # Append the page name and filename to the page list.                           
2949                                 $pagelist       = $pagelist . $pagename . " (" . $filename . ")<br>";
2951                         }
2955                         # Append the form data to the page list form data.
2957                         $filename_out           = kiriwrite_convert($filename, "normal_display");
2959                         $pagelist_formdata      = $pagelist_formdata . "<input type=\"hidden\" name=\"name[" . $pageseek . "]\" value=\"on\">";
2960                         $pagelist_formdata      = $pagelist_formdata . "<input type=\"hidden\" name=\"id[" . $pageseek . "]\" value=\"" . $filename . "\">";
2962                         $pageseek++;
2964                 }
2966                 # Get the database filenames and names.
2968                 # Check the directory to make sure the permissions are settings are valid
2969                 # and return an error if the permission settings are invalid.
2971                 my $data_directory_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"}, 1, 0);
2973                 if ($data_directory_permissions eq 1){
2975                         # The data directory has invalid permissions set, so return an error.
2977                         kiriwrite_error("datadirectoryinvalidpermissions");
2979                 }
2981                 # Open the directory and get the list of all files ending with .xml and
2982                 # put them into the data_directory array.
2984                 opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
2985                 my @data_directory = grep /m*\.db/, readdir(DATADIR);
2986                 closedir(DATADIR);
2988                 # Process each database to get the database name.
2990                 $database_list = "<select name=\"newdatabase\">";
2992                 foreach $data_file (@data_directory){
2994                         # Get the length of the filename.
2996                         $data_file_length       = length($data_file);
2997                         
2998                         # Remove the last three characters from the filename.
3000                         $data_file              = substr($data_file, 0, $data_file_length - 3);
3002                         # Check if the permissions are valid before trying
3003                         # to load the database.
3005                         $database_filename      = $kiriwrite_config{"directory_data_db"} . '/' . $data_file . '.db';
3006                         $database_permissions   = kiriwrite_filepermissions($database_filename, 1, 0, 0);
3008                         if ($database_permissions eq 1){
3010                                 # If the database has invalid permissions set then
3011                                 # process the next database.
3013                                 next;
3015                         }
3017                         # Load the SQLite database.
3018                 
3019                         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $database_filename);
3021                         # Query the SQLite database or return an error (meaning that the database is in
3022                         # a invalid format), if it can't then process the next database.
3024                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or next;
3026                         $string_handle->execute();
3027                         @database_info = $string_handle->fetchrow_array();
3029                         # Get the values from the query.
3031                         $newdatabase_name       = $database_info[0];
3033                         # Convert the values so that they can be displayed
3034                         # properly.
3036                         $newdatabase_filename_out       = kiriwrite_convert($data_file, "normal_display");
3037                         $newdatabase_name_out           = kiriwrite_convert($newdatabase_name, "normal_display");
3039                         # Append the database filename and name to the list of databases
3040                         # to move the pages to.
3042                         $database_list = $database_list . "<option value=\"" . $newdatabase_filename_out . "\">" . $newdatabase_name_out . "</option>";
3044                 }
3046                 $database_list = $database_list . "</select>";
3048                 # Convert some values so that they display properly.
3050                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
3051                 my $database_out        = kiriwrite_convert($database, "normal_display");
3053                 # Write out the form.
3055                 $pagedata = "<h2>Copy selected pages</h2>";
3056                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
3057                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
3058                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"multicopy\">";
3059                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_out . "\">";
3060                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . ($pageseek - 1) . "\">";
3061                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
3062                 $pagedata = $pagedata . $pagelist_formdata;
3063                 $pagedata = $pagedata . "Which database do you want to copy the following pages from the '" . $database_name_out . "' database to?<br><br>";
3064                 $pagedata = $pagedata . "<div class=\"datalist\">";
3065                 $pagedata = $pagedata . $pagelist;
3066                 $pagedata = $pagedata . "</div><br>";
3067                 $pagedata = $pagedata . "Copy pages to: " . $database_list . "<br><br>";
3068                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Copy pages to the selected database.\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database.</a>";
3069                 $pagedata = $pagedata . "</form>";
3071                 return $pagedata;
3073         } else {
3075                 # The confirm value is other than 0 or 1, so return
3076                 # an error.
3078                 kiriwrite_error("invalidvariable");
3080         }       
3084 sub kiriwrite_page_multiedit{
3085 #################################################################################
3086 # kiriwrite_page_multiedit: Edit several pages from a database.                 #
3087 #                                                                               #
3088 # Usage:                                                                        #
3089 #                                                                               #
3090 # kiriwrite_page_multiedit(database, newsection, altersection, newtemplate,     #
3091 #                               altertemplate, newsettings, altersettings       #
3092 #                               confirm, filelist);                             #
3093 #                                                                               #
3094 # database      Specifies the database to edit the pages from.                  #
3095 # newsection    Specifies the new section name to use on the selected pages.    #
3096 # altersection  Specifies if the section name should be altered.                #
3097 # newtemplate   Specifies the new template filename to use on the selected      #
3098 #               pages.                                                          #
3099 # altertemplate Specifies if the template filename should be altered.           #
3100 # newsettings   Specifies the new settings to use on the selected pages.        #
3101 # altersettings Specifies if the settings should be altered.                    #
3102 # confirm       Confirms the action to edit the selected pages.                 #
3103 # filelist      The list of file names to edit.                                 #
3104 #################################################################################
3106         # Get the values that were passed to the subroutine.
3108         my ($database, $newsection, $altersection, $newtemplate, $altertemplate, $newsettings, $altersettings, $confirm, @filelist) = @_;
3110         # Load the required Perl modules.
3112         use DBI;
3114         # Define a variable for later.
3116         my $pagedata = "";
3118         # Check if the file list is blank and return an error
3119         # if it is.
3121         if (!@filelist){
3123                 # The file list really is blank so return
3124                 # an error.
3126                 kiriwrite_error("nopagesselected");
3128         }
3130         # Check if the file permissions on the original database
3131         # are valid and return an error if they aren't.
3133         my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
3134         my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 1, 0);
3136         if ($database_exists eq 1){
3138                 # The database does not exist, so return an error.
3140                 kiriwrite_error("databasemissingfile");
3142         }
3143                 
3144         if ($database_permissions eq 1){
3146                 # The database permissions are invalid, so return
3147                 # an error.
3149                 kiriwrite_error("databaseinvalidpermissions");
3151         }
3153         # Check if certain values are undefined and define them if
3154         # they are.
3156         if (!$altersection){
3158                 # The alter section value is blank, so set it to
3159                 # off.
3161                 $altersection   = "off";
3163         }
3165         if (!$altertemplate){
3167                 # The alter template value is blank, so set it to
3168                 # off.
3170                 $altertemplate  = "off";
3172         }
3174         if (!$altersettings){
3176                 # The alter settings value is blank, so set it to
3177                 # off.
3179                 $altersettings  = "off";
3181         }
3183         # Check if the database filename is valid and return an error if
3184         # it isn't.
3186         my $pagedatabase_filename_check = kiriwrite_variablecheck($database, "filename", 0, 1);
3188         if ($pagedatabase_filename_check eq 1){
3190                 # The database filename is blank, so return an error.
3192                 kiriwrite_error("blankdatabasepageadd");
3194         } elsif ($pagedatabase_filename_check eq 2){
3196                 # The database filename is invalid, so return an error.
3198                 kiriwrite_error("databasefilenameinvalid");
3200         }
3202         # Check if the confirm value is blank and if it is then
3203         # set the confirm value to 0.
3205         if (!$confirm){
3207                 $confirm = 0;
3209         }
3211         if ($confirm eq 1){
3213                 # The action to edit the template has been confirmed so
3214                 # edit the selected pages.
3216                 # Check the values recieved at UTF8 compliant before
3217                 # converting.
3219                 kiriwrite_variablecheck($newsection, "utf8", 0, 0);
3220                 kiriwrite_variablecheck($newtemplate, "utf8", 0, 0);
3221                 kiriwrite_variablecheck($newsettings, "utf8", 0, 0);
3223                 # Convert the values into proper UTF8 values.
3225                 $newsection     = kiriwrite_utf8convert($newsection);
3226                 $newtemplate    = kiriwrite_utf8convert($newtemplate);
3227                 $newsettings    = kiriwrite_utf8convert($newsettings);
3229                 # Check the length of the variables.
3231                 kiriwrite_variablecheck($altersection, "maxlength", 3, 0);
3232                 kiriwrite_variablecheck($altertemplate, "maxlength", 3, 0);
3233                 kiriwrite_variablecheck($altersettings, "maxlength", 3, 0);
3234                 my $newsection_maxlength_check  = kiriwrite_variablecheck($newsection, "maxlength", 256, 1);
3235                 my $newtemplate_maxlength_check = kiriwrite_variablecheck($newtemplate, "maxlength", 256, 1);
3236                 my $newtemplate_filename_check  = kiriwrite_variablecheck($newtemplate, "filename", 0, 1);
3237                 my $newsettings_maxlength_check = kiriwrite_variablecheck($newsettings, "maxlength", 1, 1);
3238                 my $newsettings_settings_check  = kiriwrite_variablecheck($newsettings, "pagesetting", 0, 1);
3240                 # Check the values and return an error if needed.
3242                 if ($newsection_maxlength_check eq 1 && $altersection eq "on"){
3244                         # The new section name is too long, so return an
3245                         # error.
3247                         kiriwrite_error("pagesectiontoolong");
3249                 }
3251                 if ($newtemplate_maxlength_check eq 1 && $altertemplate eq "on"){
3253                         # The new template name is too long, so return an
3254                         # error.
3256                         kiriwrite_error("templatefilenametoolong");                     
3258                 }
3260                 # Check if the template filename is set to !skip or !none
3261                 # and skip this check if it is.
3263                 if ($newtemplate eq "!skip" || $newtemplate eq "!none"){
3265                         # Skip this check as the template filename is 
3266                         # !skip or !none.
3268                 } else {
3269                         if ($newtemplate_filename_check eq 1 && $altertemplate eq "on" || $newtemplate_filename_check eq 2 && $altertemplate eq "on"){
3271                                 # The new template filename is invalid, so return
3272                                 # an error.
3274                                 kiriwrite_error("templatefilenameinvalid");
3276                         }
3277                 }
3279                 if ($newsettings_maxlength_check eq 1 && $altertemplate eq "on"){
3281                         # The new settings value is too long, so return
3282                         # an error.
3284                         kiriwrite_error("pagesettingstoolong");
3286                 }
3288                 if ($newsettings_settings_check eq 1 && $altertemplate eq "on"){
3290                         # The new settings value is invalid, so return
3291                         # an error.
3293                         kiriwrite_error("pagesettingsinvalid");
3295                 }
3297                 # Load the SQLite Database and get info about the database.
3299                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
3300                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
3301                 $string_handle->execute();
3303                 # Get the database name.
3305                 my @database_info       = $string_handle->fetchrow_array();
3306                 my $database_name       = $database_info[0];
3307                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
3308                 my $database_out        = kiriwrite_convert($database, "normal_display");
3310                 # Define some values for later.
3312                 my @database_page;
3313                 my $edited_list = "";
3314                 my $filename;
3315                 my $filename_sql;
3316                 my $filename_out;
3317                 my $sqlquery_options = "";
3318                 my @sqlquery_list;
3319                 my $sqlquery_option;
3320                 my $page_name;
3321                 my $page_name_out;
3322                 my $newsection_sql;
3323                 my $newtemplate_sql;
3324                 my $newsettings_sql;
3325                 my $pagefound = 0;
3326                 my $pageedited = 0;
3327                 my $sqlquery_seek = 0;
3328                 my $warninglist;
3330                 # Get the date and time information.
3332                 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
3334                 my $pagelastmodified = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
3337                 # Check if the template filename is !skip and
3338                 # set the alter template value to off if it
3339                 # is.
3341                 if ($newtemplate eq "!skip"){
3343                         $altertemplate = "off";
3345                 }
3347                 # Check if all values are not selected and return
3348                 # an error if they are.
3350                 if ($altersection ne "on" && $altertemplate ne "on" && $altersettings ne "on"){
3352                         # All values are not selected so return 
3353                         # an error.
3355                         kiriwrite_error("noeditvaluesselected");
3357                 }
3359                 # Check if each value is meant to be changed on
3360                 # the selected files. Start with the section
3361                 # name.
3363                 if ($altersection eq "on"){
3365                         # Convert the value so that it will work
3366                         # when SQL query is made.
3368                         $newsection_sql = kiriwrite_convert($newsection, "kiriwrite");
3370                         # The section name should be altered so
3371                         # prepare the a part of the SQL query.
3372                         # Check if this is the first part of
3373                         # the SQL query.
3375                         if ($sqlquery_seek eq 0){
3377                                 # This is the first part of the SQL query.
3379                                 $sqlquery_list[$sqlquery_seek] = "pagesection = \'" . $newsection_sql . "\'";
3380                                 $sqlquery_seek++;
3382                         } else {
3384                                 # This isn't the first part of the SQL query.
3386                                 $sqlquery_list[$sqlquery_seek] = ", pagesection = \'" . $newsection_sql . "\'";
3387                                 $sqlquery_seek++;
3389                         }
3391                 }
3393                 # Check if the template name should be changed.
3395                 if ($altertemplate eq "on"){
3397                         # Convert the value so that it will work
3398                         # when SQL query is made.
3400                         $newtemplate_sql        = kiriwrite_convert($newtemplate, "kiriwrite");
3402                         # The section name should be altered so
3403                         # prepare the a part of the SQL query.
3404                         # Check if this is the first part of
3405                         # the SQL query.
3407                         if ($sqlquery_seek eq 0){
3409                                 # This is the first part of the SQL query.
3411                                 $sqlquery_list[$sqlquery_seek] = "pagetemplate = \'" . $newtemplate_sql . "\'";
3412                                 $sqlquery_seek++;
3414                         } else {
3416                                 # This isn't the first part of the SQL query.
3418                                 $sqlquery_list[$sqlquery_seek] = ", pagetemplate = \'" . $newtemplate_sql . "\'";
3419                                 $sqlquery_seek++;
3421                         }
3423                 }
3425                 # Check if the page settings should be changed.
3427                 if ($altersettings eq "on"){
3429                         # Convert the value so that it will work
3430                         # when SQL query is made.
3432                         $newsettings_sql        = kiriwrite_convert($newsettings, "kiriwrite");
3434                         # The section name should be altered so
3435                         # prepare the a part of the SQL query.
3436                         # Check if this is the first part of
3437                         # the SQL query.
3439                         if ($sqlquery_seek eq 0){
3441                                 # This is the first part of the SQL query.
3443                                 $sqlquery_list[$sqlquery_seek] = "pagesettings = \'" . $newsettings_sql . "\'";
3444                                 $sqlquery_seek++;
3446                         } else {
3448                                 # This isn't the first part of the SQL query.
3450                                 $sqlquery_list[$sqlquery_seek] = ", pagesettings = \'" . $newsettings_sql . "\'";
3451                                 $sqlquery_seek++;
3453                         }
3455                 }
3457                 # Add a new modification date.
3459                 $sqlquery_list[$sqlquery_seek] = ", lastmodified = \'" . $pagelastmodified . "\'";
3461                 # Create the line for the needed SQL query.
3463                 foreach $sqlquery_option (@sqlquery_list){
3465                         # Add the line to the needed SQL query.
3467                         $sqlquery_options = $sqlquery_options . $sqlquery_option;
3469                 }
3471                 # Edit each filename.
3473                 foreach $filename (@filelist){
3475                         # Check if the page filename exists in the
3476                         # database.
3478                         $filename_sql   = kiriwrite_convert($filename, "kiriwrite");
3479                         $filename_out   = kiriwrite_convert($filename, "normal_display");
3480                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filename_sql . '\'') or kiriwrite_error("databasefileinvalid");
3481                         $string_handle->execute();
3483                         # Check if the filename given exists and if it
3484                         # doesn't then skip this filename and go onto
3485                         # the next filename.
3487                         while (@database_page = $string_handle->fetchrow_array()){
3489                                 $pagefound = 1;
3491                         }
3492                         
3493                         if ($pagefound eq 0){
3495                                 # The page was not find in the database so
3496                                 # add a warning saying it could not be
3497                                 # found in the database and process the
3498                                 # next file.
3500                                 
3501                                 $warninglist = $warninglist . "The page \'" . $filename_out . "\'could not be found." . "<br>";
3502                                 next;
3504                         }
3506                         # Get the page name.
3508                         $string_handle->execute();
3509                         @database_page  = $string_handle->fetchrow_array();
3510                         $page_name      = $database_page[1];
3511                         $page_name_out  = kiriwrite_convert($page_name, "normal_display");
3513                         # Alter the values of the page using the values recieved.
3515                         $string_handle  = $database_handle->prepare('UPDATE kiriwrite_database_pages SET ' . $sqlquery_options . ' WHERE filename = \'' . $filename_sql . '\'');
3516                         $string_handle->execute();
3518                         # Add the page to the list of edited pages. Check if
3519                         # the page name is blank.
3521                         if (!$page_name_out){
3523                                 # The page name is blank so write no name.
3525                                 $edited_list    = $edited_list . "The page (" . $filename_out . ") was edited." . "<br>";
3527                         } else {
3529                                 # Add the page to the list of edited pages.
3531                                 $edited_list    = $edited_list . "The page called '" . $page_name_out . "' (" . $filename_out . ") was edited." . "<br>";
3533                         }
3535                         # Increment the counter of edited pages.
3537                         $pageedited++;
3539                 }
3541                 $pagedata       = $pagedata . "<h2>Edit selected pages</h2>";
3543                 # Check if the counter of edited pages is 0 and if it is
3544                 # then write a message saying that no pages were edited
3545                 # else write a message listing all of the pages edited.
3547                 if ($pageedited eq 0){
3549                         $pagedata       = $pagedata . "No pages were edited in the '" . $database_name_out . "' database. <br><br>";
3551                 } else {
3553                         # Write out the message saying that the selected pages
3554                         # were edited.
3556                         $pagedata       = $pagedata . "The selected pages in the '" . $database_name_out . "' database have been edited:<br><br>";
3557                         $pagedata       = $pagedata . "<div class=\"datalist\">";
3558                         $pagedata       = $pagedata . $edited_list;
3559                         $pagedata       = $pagedata . "</div><br>";
3561                 }
3563                 # Check if any warnings have occured and write a message
3564                 # if any warnings did occur.
3566                 if ($warninglist){
3568                         # One or several warnings have occured so 
3569                         # write a message.
3571                         $pagedata       = $pagedata . "The following errors/warnings occured while editing the selected pages:<br><br>";
3572                         $pagedata       = $pagedata . "<div class=\"datalist\">";
3573                         $pagedata       = $pagedata . $warninglist;
3574                         $pagedata       = $pagedata . "</div><br>";
3576                 }
3578                 # Write a link going back to the page list for
3579                 # the selected database.
3581                 $pagedata       = $pagedata . "<a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database.</a>";
3583                 return $pagedata;
3585         } elsif ($confirm eq 0){
3587                 # The action to edit the template has not been confirmed
3588                 # so write a form out instead.
3590                 # Load the SQLite Database and get info about the database.
3592                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
3593                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or kiriwrite_error("databasefileinvalid");
3594                 $string_handle->execute();
3596                 # Get the database name.
3598                 my @database_info       = $string_handle->fetchrow_array();
3599                 my $database_name       = $database_info[0];
3600                 my $database_name_out   = kiriwrite_convert($database_name, "normal_display");
3601                 my $database_out        = kiriwrite_convert($database, "normal_display");
3603                 # Define some variables for later.
3605                 my @database_page;
3606                 my @database_templates;
3607                 my @filenames;
3608                 my @page_info;
3609                 my @pagenames;
3610                 my $filelist_filename;
3611                 my $filelist_filename_sql;
3612                 my $filelist_filename_out;
3613                 my $filename;
3614                 my $pagefound;
3615                 my $pagelist = "";
3616                 my $pagelistformdata = "";
3617                 my $pageseek = 0;
3618                 my $page_name;
3619                 my $page_filename;
3620                 my $template_data;
3621                 my $template_filename;
3622                 my $template_name;
3623                 my $template_warning;
3625                 # Get the information about each page that is going
3626                 # to be edited.
3628                 foreach $filelist_filename (@filelist){
3630                         $filelist_filename_sql  = kiriwrite_convert($filelist_filename, "kiriwrite");
3631                         $filelist_filename_out  = kiriwrite_convert($filelist_filename, "normal_display");
3633                         $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $filelist_filename . '\' LIMIT 1') or kiriwrite_error("databasefileinvalid");
3635                         $string_handle->execute();
3637                         # Check if the filename given exists and if it
3638                         # doesn't then skip this filename and go onto
3639                         # the next filename.
3641                         while (@page_info = $string_handle->fetchrow_array()){
3643                                 $pagefound = 1;
3645                         }
3646                         
3647                         if ($pagefound eq 0){
3649                                 next;
3651                         }
3653                         # Get the data again.
3655                         $string_handle->execute();
3656                         @page_info      = $string_handle->fetchrow_array();
3658                         # Add the page name and file name to their seperate
3659                         # arrays.
3661                         $pagenames[$pageseek]   = $page_info[1];
3662                         $filenames[$pageseek]   = $page_info[0];
3664                         # Add the form data for the page filename.
3666                         # BLEARGH
3668                         $pagelistformdata       = $pagelistformdata . "<input type=\"hidden\" name=\"name[" . ($pageseek + 1) . "]\" value=\"on\">";
3669                         $pagelistformdata       = $pagelistformdata . "<input type=\"hidden\" name=\"id[" . ($pageseek + 1) . "]\" value=\"" . $filelist_filename_out . "\">";
3671                         # Increment the page seek counter and reset the
3672                         # page found value.
3674                         $pageseek++;
3675                         $pagefound = 0;
3677                 }
3679                 # Check if any pages were found in the database and return
3680                 # an error if not.
3682                 if ($pageseek eq 0){
3684                         # No pages were found so return an error.
3686                         kiriwrite_error("nopagesselected");
3688                 }
3690                 # Get the template list and select the template that is being used by
3691                 # the page, check the permissions and if the template database exists.
3693                 my $template_file_exists        = kiriwrite_fileexists("templates.db");
3694                 my $template_file_permissions   = kiriwrite_filepermissions("templates.db", 1, 0, 0);
3696                 if ($template_file_exists eq 1){
3698                         # The template database does not exist, so write a warning messsage.
3700                         $template_warning = "The template database does not exist. Existing template settings for selected pages kept.";
3702                 }
3704                 if ($template_file_permissions eq 1 && $template_file_exists ne 1){
3706                         # The template database has invalid permissions set, so write a warning message.
3708                         $template_warning = "The template database has invalid permissions set. Existing template settings for selected pages kept.";
3710                 }
3712                 # Get the list of templates.
3714                 if (!$template_warning){
3716                         # Load the templates database and get the templates.
3718                         $database_handle        = DBI->connect("dbi:SQLite:templates.db");
3719                         $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_templates') or $template_warning = "The template database is in an invalid format. Existing template settings for selected pages kept.";
3720                         
3721                         if (!$template_warning){
3723                                 $string_handle->execute();
3725                                 $template_data = "<select name=\"newtemplate\">";
3727                                 while (@database_templates = $string_handle->fetchrow_array()){
3729                                         # Get the filename and name of the database.
3731                                         $template_filename      = $database_templates[0];
3732                                         $template_name          = $database_templates[1];
3734                                         # Convert the values so that they can be displayed in the list.
3736                                         $template_filename      = kiriwrite_convert($template_filename, "normal_display");
3737                                         $template_name          = kiriwrite_convert($template_name, "normal_display");
3739                                         # Add the template to the list of templates.
3741                                         $template_data = $template_data .  "<option value=\"" . $template_filename . "\">" . $template_name . " (" . $template_filename .")</option>";
3743                                 }
3745                         # Add the option to not use a template at all.
3747                         $template_data = $template_data . "<option value=\"!none\" selected>Don't use a template</option>";
3748                         $template_data = $template_data . "</select>";
3750                         }
3752                 }
3754                 # Process the list of selected pages.
3756                 $pageseek = 1;
3757                 foreach $filename (@filenames){
3759                         # Process each filename.
3761                         $page_name      = $pagenames[$pageseek - 1];
3762                         $page_filename  = $filenames[$pageseek - 1];
3764                         # Check if the page name is blank.
3766                         if (!$page_name){
3768                                 # Write a message saying there's no page name.
3770                                 $pagelist       = $pagelist . "<i>No Name</i>" . " (" . $page_filename . ")" . "<br>"
3772                         } else {
3774                                 # Append the page to the list of pages to edit.
3776                                 $pagelist       = $pagelist . $page_name . " (" . $page_filename . ")" . "<br>";
3778                         }
3780                         $pageseek++;
3782                 }
3784                 # Write a form for editing the selected pages.
3786                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
3787                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">";
3788                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"multiedit\">";
3789                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_out . "\">";
3790                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . ($pageseek - 1) . "\">";
3791                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
3792                 $pagedata = $pagedata . "<h2>Edit selected pages</h2>";
3793                 $pagedata = $pagedata . "The following pages from the '" . $database_name_out . "' database will be altered:<br><br>";
3794                 $pagedata = $pagedata . "<div class=\"datalist\">";
3795                 $pagedata = $pagedata . $pagelist;
3796                 $pagedata = $pagedata . "</div><br>";
3797                 $pagedata = $pagedata . $pagelistformdata;
3798                 $pagedata = $pagedata . "Using the values below (click on the checkbox for each value to be edited on the selected pages):<br><br>";
3799                 $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\">";
3800                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Alter</td><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
3801                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\"><input type=\"checkbox\" name=\"altersection\"></td><td class=\"tablecell1\">Page Section</td><td class=\"tablecell1\"><input type=\"text\" name=\"newsection\" size=\"64\" maxlength=\"256\"></td></tr>";
3802                 
3803                 # Check if a warning for template database has been written
3804                 # and return the warning message if it has else write the
3805                 # list of templates.
3807                 if ($template_warning){
3809                         # There is a message in the template warning variable
3811                         $pagedata = $pagedata . "<tr><td class=\"tablecell2\"><input type=\"checkbox\" name=\"altertemplate\"></td><td class=\"tablecell2\">Page Template</td><td class=\"tablecell2\"><input type=\"hidden\" name=\"newtemplate\" value=\"!skip\">" . $template_warning . "</td></tr>";
3813                 } else {
3814                 
3815                         $pagedata = $pagedata . "<tr><td class=\"tablecell2\"><input type=\"checkbox\" name=\"altertemplate\"></td><td class=\"tablecell2\">Page Template</td><td class=\"tablecell2\">" . $template_data . "</td></tr>";
3817                 }
3819                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\"><input type=\"checkbox\" name=\"altersettings\"></td><td class=\"tablecell1\">Page Settings</td><td class=\"tablecell1\"><input type=\"radio\" name=\"newsettings\" value=\"1\"> Use page name and section name.<br><input type=\"radio\" name=\"newsettings\" value=\"2\"> Use the page name only.<br><input type=\"radio\" name=\"newsettings\" value=\"3\"> Use the section name only.<br><input type=\"radio\" name=\"newsettings\" value=\"0\" checked> Don't use page name or section name.<br></td></tr>";
3820                 $pagedata = $pagedata . "</table><br>";
3821                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Edit selected pages\"> | <a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_out . "\">Return to the page list for the '" . $database_name_out . "' database.</a>";
3822                 $pagedata = $pagedata . "</form>";
3824                 return $pagedata;
3826         } else {
3828                 # The confirm value is something else other than
3829                 # 1 or 0, so return an error.
3831                 kiriwrite_error("invalidvariable");
3833         }
3837 sub kiriwrite_page_list{
3838 #################################################################################
3839 # kiriwrite_page_list: Lists pages from an database.                            #
3840 #                                                                               #
3841 # Usage:                                                                        #
3842 #                                                                               #
3843 # kiriwrite_page_list([database]);                                              #
3844 #                                                                               #
3845 # database      Specifies the database to retrieve the pages from.              #
3846 #################################################################################
3848         # Get the database file name from what was passed to the subroutine.
3850         my ($database_file) = @_;
3851                 
3852         # Load the required Perl modules.
3853         
3854         my $xsl = XML::Simple->new();
3855         use DBI;
3856         
3857         # Check if the database_file variable is empty, if it is then print out a
3858         # select box asking the user to select a database from the list.
3859         
3860         if (!$database_file) {
3861         
3862                 # Define the variables required for this section.
3863         
3864                 my $pagedata            = "";
3865                 my $dblistdata          = "";
3866                 my $data_file           = "";
3867                 my $data_file_length    = 0;
3868                 my $data_file_friendly  = "";
3869                 my $database_handle     = "";
3870                 my $string_handle       = "";
3871                 my $database_name       = "";
3872                 my $file_permissions    = "";
3873                 my @database_info;
3874                 my @data_directory;
3875                                                 
3876                 # Open the data directory and get all of the databases.
3877                 
3878                 opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
3879                 @data_directory = grep /m*\.db/, readdir(DATADIR);
3880                 closedir(DATADIR);
3881                 
3882                 # Get the information about each database (the short name and friendly name).
3883                                 
3884                 foreach $data_file (@data_directory){
3885                         
3886                         # Give the filename a friendly name.
3888                         $data_file_length       = length($data_file);
3889                         $data_file_friendly     = substr($data_file, 0, $data_file_length - 3);
3891                         # Check if the permissions for the database are valid.
3893                         $file_permissions       = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' .$data_file, 1, 0);
3894                         
3895                         if ($file_permissions ne 0){
3897                                 # The permissions for the database are invalid
3898                                 # so process the next database.
3900                                 next;
3902                         }
3904                         # Load the SQLite database.
3906                         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $data_file);
3907                         $string_handle          = $database_handle->prepare("SELECT * FROM kiriwrite_database_info LIMIT 1") or next;
3908                         $string_handle->execute();
3909                         @database_info          = $string_handle->fetchrow_array();
3911                         # Get the database name from the results.
3913                         $database_name          = $database_info[0];
3914                         $database_name          = kiriwrite_convert($database_name, "normal_display");
3916                         # Append the database to the list of databases available.
3917                         
3918                         $dblistdata = $dblistdata . "<option value=\"" . $data_file_friendly . "\">" . $database_name . "</option>";
3920                 }               
3921                 
3922                 # Write the page data.
3923                 
3924                 $pagedata = "<h2>View Pages</h2>\r\n";
3925                 $pagedata = $pagedata . "No database selected. Please select a database from the drop-down list below and then press the \'View\' button to view the pages in the selected database.<br><br>";
3926                 
3927                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">\r\n";
3928                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">\r\n";
3929                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"view\">\r\n";
3930                 $pagedata = $pagedata . "<select name=\"database\">";
3931                 $pagedata = $pagedata . $dblistdata;
3932                 $pagedata = $pagedata . "</select>&nbsp;|&nbsp;";
3933                 $pagedata = $pagedata . "<input type=\"submit\" value=\"View\">";
3934                 $pagedata = $pagedata . "</form>&nbsp;<br>";
3935                 
3936                 return $pagedata;
3937         
3938         } else {
3939                 
3940                 my @database_pages;
3941                 my @database_info;
3942                 my $tabledata           = "";
3943                 my $pagedata            = "";
3944                 my $pagemultioptions    = "";
3945                 my $database_handle     = "";
3946                 my $string_handle       = "";
3947                 my $db_friendlyname     = "";
3948                 my $tablestyle          = "";
3950                 my $page_filename       = "";
3951                 my $page_name           = "";
3952                 my $page_description    = "";
3953                 my $page_modified       = "";
3955                 my $tablestyletype      = 0;
3956                 my $page_count          = 0;
3957                 my $db_file_notblank    = 0;
3958                 
3959                 # The database_file variable is not blank, so print out a list of pages from
3960                 # the selected database.
3961                 
3962                 # Preform a variable check on the database filename to make sure that it is
3963                 # valid before using it.
3964                 
3965                 kiriwrite_variablecheck($database_file, "filename", "", 0);
3966                 kiriwrite_variablecheck($database_file, "maxlength", 64, 0);
3967         
3968                 # Check if the specified database file exists,
3969                 # if it does exist then continue otherwise return an error saying the database
3970                 # could not be found.
3971                 
3972                 if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_file . '.db'){
3973                 
3974                         # Check that the permissions for the database file are valid.
3976                         my $database_db_permissions     = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database_file . '.db', 1, 0, 0);
3978                         if ($database_db_permissions eq 1){
3980                                 kiriwrite_error("databaseinvalidpermissions");
3982                         }
3984                         # Load the SQLite database.
3986                         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_file . '.db');
3987                         
3988                         # Get the database name.
3990                         $string_handle          = $database_handle->prepare("SELECT * FROM kiriwrite_database_info LIMIT 1") or kiriwrite_error("databasenameinvalid");
3991                         $string_handle->execute();
3993                         @database_info = $string_handle->fetchrow_array();
3995                         $db_friendlyname = $database_info[0];
3997                         $string_handle          = $database_handle->prepare("SELECT * FROM kiriwrite_database_pages") or kiriwrite_error("databasenameinvalid");
3998                         $string_handle->execute();
4000                         # Write the table header into the tabledata string.
4002                         $tabledata = "<table cellpadding=\"5\" cellspacing=\"0\">\r\n<tr>\r\n\t<td class=\"tablecellheader\"></td>\r\n\t<td class=\"tablecellheader\">Page Location</td>\r\n\t<td class=\"tablecellheader\">Page Name</td>\r\n\t<td class=\"tablecellheader\">Page Description</td>\r\n\t<td class=\"tablecellheader\">Last Modified</td>\r\n\t<td class=\"tablecellheader\">Options</td>\r\n</tr>";
4004                         # Write the results out as a list and append it to the
4005                         # tabledata string.
4007                         while (@database_pages = $string_handle->fetchrow()){
4009                                 $page_filename          = kiriwrite_convert($database_pages[0], "normal_display");
4010                                 $page_name              = kiriwrite_convert($database_pages[1], "normal_display");
4011                                 $page_description       = kiriwrite_convert($database_pages[2], "normal_display");
4012                                 $page_modified          = kiriwrite_convert($database_pages[7], "date");
4013                                 $page_modified          = kiriwrite_convert($page_modified, "normal_display");
4015                                 if ($database_pages[0]){
4016                                         $page_count++;
4017                                 } else {
4018                                         next;
4019                                 }
4021                                 if ($tablestyletype eq 0){
4023                                         $tablestyle = "tablecell1";
4024                                         $tablestyletype = 1;
4026                                 } else {
4028                                         $tablestyle = "tablecell2";
4029                                         $tablestyletype = 0;
4031                                 }
4033                                 # Check if the page name and description names
4034                                 # are blank and if they are write a message saying that
4035                                 # field contains no data.
4037                                 if ($page_name eq ""){
4039                                         $page_name = "<i>No Name</i>";
4041                                 }
4043                                 if ($page_description eq ""){
4045                                         $page_description = "<i>No Description</i>";
4047                                 }
4049                                 if ($page_modified eq ""){
4051                                         $page_modified = "<i>No Date</i>";
4053                                 }
4055                                 $tabledata = $tabledata . "<tr><td class=\"" . $tablestyle . "\"><input type=\"hidden\" name=\"id[" . $page_count . "]\" value=\"" . $database_pages[0] . "\"><input type=\"checkbox\" name=\"name[" . $page_count . "]\"></td><td class=\"" . $tablestyle . "\">" . $page_filename ."</td><td class=\"" . $tablestyle . "\">" . $page_name . "</td><td class=\"" . $tablestyle . "\">" . $page_description . "</td><td class=\"" . $tablestyle . "\">" . $page_modified . "</td><td class=\"" . $tablestyle . "\"><a href=\"kiriwrite.cgi?mode=page&action=edit&database=" . $database_file . "&page=" . $page_filename ."\">Edit</a> <a href=\"kiriwrite.cgi?mode=page&action=delete&database=" . $database_file . "&page=" . $page_filename . "\">Delete</td></tr>";
4056                                 
4059                         }
4061                         # Check if the page count is more than
4062                         
4063                         if ($page_count > 0){
4064                                 $db_file_notblank = 1;
4065                         }
4067                         # Append the closing table part into the tabledata string.
4069                         $tabledata = $tabledata . "</table>";
4070                         
4071                 } else {
4072                         # Either the database file, the database configuration file or both are missing
4073                         # so return an error saying that certain files are missing.
4074                         
4075                         kiriwrite_error("databasemissingfile");
4076                 }
4077                 
4078                 # Check if the database really was not blank and if it was, write a message saying that
4079                 # no pages existed in the database else write the list of pages in the database.
4081                 $db_friendlyname = kiriwrite_convert($db_friendlyname, "normal_display");
4083                 if ($db_file_notblank eq 0){
4085                         $pagedata = "<h2>Page list for '" . $db_friendlyname . "'</h2>";
4086                         $pagedata = $pagedata . "<div class=\"errorbox\">No pages exist in this database. To create a page for this database, click on Add Page link at the top of the page.</div>";
4088                 } else {
4090                         $pagemultioptions = "<input type=\"reset\" value=\"Select None\"> | <button name=\"action\" value=\"multidelete\">Delete Selected</button> | <button name=\"action\" value=\"multimove\">Move Selected</button> | <button name=\"action\" name=\"action\" value=\"multicopy\">Copy Selected</button> | <button name=\"action\" value=\"multiedit\">Edit Selected</button>";
4092                         $pagedata = "<h2>Page list for '" . $db_friendlyname . "'</h2>";
4093                         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">\r\n";
4094                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"page\">\r\n";             
4095                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . $page_count . "\">\r\n";
4096                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"type\" value=\"multiple\">\r\n";
4097                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_file . "\">\r\n";
4098                         $pagedata = $pagedata . $pagemultioptions . "<p>\r\n";
4099                         $pagedata = $pagedata . $tabledata;
4100                         $pagedata = $pagedata . "<br>" . $pagemultioptions . "<p>\r\n";
4101                         $pagedata = $pagedata . "</form>";
4103                 }
4104                 
4105                 return $pagedata;
4106                 
4107         }
4111 sub kiriwrite_page_multipleselect{
4112 #################################################################################
4113 # kiriwrite_page_multipleselect: Gets the multiple page selections from a       #
4114 # database and processes them into a single which can be used in the            #
4115 # kiriwrite_page_delete.                                                        #
4116 #                                                                               #
4117 # Usage:                                                                        #
4118 #                                                                               #
4119 # kiriwrite_page_multipleselect();                                              #
4120 #################################################################################
4122         # Load the required perl modules for this subroutine.
4124         my $query = new CGI;
4125         
4126         # Setup some variables that will be used later on.
4127         
4128         my $page_select_seek = 1;
4129         my $page_current_value = "";
4130         my $selectionlist = "";
4131                 
4132         # Get the required variables from the HTTP query.
4133         
4134         my $page_count = $query->param('count');
4135         
4136         do {
4137                 
4138                 # Get the current value of the selected page's checkbox.
4139                 
4140                 $page_current_value = $query->param('name[' . $page_select_seek . ']');
4142                 # If page_current_value is undefinied or blank, set page_current_value
4143                 # to off.
4144                 
4145                 if (!$page_current_value){
4146                                 
4147                         $page_current_value = "off";
4148                 
4149                 }
4150                                 
4151                 # Check if the page_current_value is set to 'on' if it is append the
4152                 # current page seek value to the selection list.
4154                 if ($page_current_value eq "on"){
4155                 
4156                         # Append the current seek value to the selection list.
4157                         
4158                         $selectionlist = $selectionlist . $page_select_seek . "|";
4159                 
4160                 }
4161         
4162                 # Increment the page selection seeking counter. 
4163         
4164                 $page_select_seek++;
4165         
4166         } until ($page_select_seek eq $page_count);
4167         
4168         return $selectionlist;
4172 sub kiriwrite_template_add{
4173 #################################################################################
4174 # kiriwrite_template_add: Add a template to the template folder                 #
4175 #                                                                               #
4176 # Usage:                                                                        #
4177 #                                                                               #
4178 # kiriwrite_template_add(filename, name, description, layout, confirm);         #
4179 #                                                                               #
4180 # filename      The filename of the new template.                               #
4181 # name          The name of the template.                                       #
4182 # description   The description of the template.                                #
4183 # layout        The layout of the new template.                                 #
4184 # confirm       Confirm the action of creating a new template.                  #
4185 #################################################################################
4187         # Get the variables that were passed to the subroutine.
4188         
4189         my ($templatefilename, $templatename, $templatedescription, $templatelayout, $confirm) = @_;
4190         
4191         # Load the needed Perl Modules
4193         use DBI;
4195         # Check if the confirm value is blank and if it is then set confirm to 0.
4197         if (!$confirm){
4199                 # The confirm value is blank, so set the value of confirm to 0.
4201                 $confirm = 0;
4203         }
4205         if ($confirm eq 1){
4206                 
4207                 # Check (validate) each of the values.
4208         
4209                 kiriwrite_variablecheck($templatename, "utf8", 0, 0);
4210                 kiriwrite_variablecheck($templatedescription, "utf8", 0, 0);
4211                 kiriwrite_variablecheck($templatelayout, "utf8", 0, 0);
4213                 # Convert the values into proper UTF8 strings that can be used.
4215                 $templatename           = kiriwrite_utf8convert($templatename);
4216                 $templatedescription    = kiriwrite_utf8convert($templatedescription);
4217                 $templatelayout         = kiriwrite_utf8convert($templatelayout);
4219                 # Check the length of the converted UTF8 strings.
4221                 my $templatefilename_length_check       = kiriwrite_variablecheck($templatefilename, "maxlength", 64, 1);
4222                 my $templatename_length_check           = kiriwrite_variablecheck($templatename, "maxlength", 512, 1);
4223                 my $templatedescription_length_check    = kiriwrite_variablecheck($templatedescription, "maxlength", 512, 1);
4224                 kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
4226                 if ($templatefilename_length_check eq 1){
4227                         
4228                         # The template filename length is too long, so return an error.
4230                         kiriwrite_error("templatefilenametoolong");
4232                 }
4234                 if ($templatename_length_check eq 1){
4236                         # The template name length is too long, so return an error.
4238                         kiriwrite_error("templatenametoolong");
4240                 }
4243                 if ($templatedescription_length_check eq 1){
4245                         # The template description length is too long, so return an error.
4247                         kiriwrite_error("templatedescriptiontoolong");
4249                 }
4251                 # Check if the filename specified is a valid filename.
4253                 kiriwrite_variablecheck($templatefilename, "filename", "", 0);
4255                 # Check if the template database file permissions are valid and
4256                 # return an error if they aren't.
4258                 my $template_database_permissions = kiriwrite_filepermissions("templates.db", 1, 1, 1);
4260                 if ($template_database_permissions eq 1){
4262                         # Template database has invalid permissions set, so return
4263                         # an error.
4265                         kiriwrite_error("templatedatabasefileinvalidpermissions");
4266                 }
4268                 # Check if the template database exists and if it doesn't then
4269                 # create the database and populate it.
4271                 my $template_database_exists = kiriwrite_fileexists("templates.db");
4272                 my $database_handle;
4273                 my $string_handle;              
4275                 if ($template_database_exists eq 1){
4277                         # The template database does not exist, so try to create a database.
4279                         # Check if the directory has valid permissions to create files (and
4280                         # thus be able create a template database).
4282                         my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
4284                         if ($directory_permissions eq 1){
4286                                 # The template database cannot be created because of invalid directory
4287                                 # permissions so return an error.
4289                                 kiriwrite_error("templatedatabasefilenotcreated");
4290                         }
4292                         # Create the SQLite database for the templates and populate it.
4294                         $database_handle        = DBI->connect("dbi:SQLite:dbname=templates.db");
4295                         $string_handle          = $database_handle->prepare('CREATE TABLE kiriwrite_templates(
4296                                 filename varchar(64) primary key,
4297                                 templatename varchar(512),
4298                                 templatedescription varchar(512),
4299                                 templatelayout text,
4300                                 datemodified datetime
4301                         )') or die(kiriwrite_error("templatedatabaseinvalidformat")); 
4302                         $string_handle->execute();
4304                 }
4306                 # Check if the template filename is already used and if it is then
4307                 # return an error.
4309                 $database_handle        = DBI->connect("dbi:SQLite:dbname=templates.db");
4310                 $string_handle          = $database_handle->prepare('select * from kiriwrite_templates where filename = \'' . $templatefilename . '\' limit 1') or die(kiriwrite_error("templatedatabaseinvalidformat"));
4311                 $string_handle->execute();
4313                 my @database_template_check     = $string_handle->fetchrow_array();
4314                 my $template_checkname          = $database_template_check[0];
4316                 if ($template_checkname){
4318                         # A template already exists with the filename given, so return an error.
4320                         kiriwrite_error("templatefilenameexists");
4321                 }
4323                 # Get the current date.
4325                 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
4327                 my $templatedate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
4329                 # Convert the values that are going to be added for storing in the database.
4331                 $templatename           = kiriwrite_convert($templatename, "kiriwrite");
4332                 $templatedescription    = kiriwrite_convert($templatedescription, "kiriwrite");
4333                 $templatelayout         = kiriwrite_convert($templatelayout, "kiriwrite");
4335                 # Add the template to the template database.
4337                 $string_handle          = $database_handle->prepare('INSERT INTO kiriwrite_templates values(
4338                                 \'' . $templatefilename . '\',
4339                                 \'' . $templatename . '\',
4340                                 \'' . $templatedescription . '\',
4341                                 \'' . $templatelayout . '\',
4342                                 \'' . $templatedate . '\'
4343                 )');
4344                 $string_handle->execute();
4345                                         
4346                 # Print out the confirmation message.
4348                 my $pagedata = "<h2>Template Added</h2>";
4349                 $pagedata = $pagedata . "The template called '" . $templatename ."' was sucessfully created.<br><br>";
4350                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=template\">Return to the templates list</a>";
4351                 return $pagedata;
4352         
4353         } else {
4354                 # No confirmation was made, so print out a form for adding a template.  
4355                         
4356                 my $pagedata = "<h2>Add Template</h2>";
4357                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"post\">";
4358                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"template\">";
4359                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"add\">";
4360                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
4361                 $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\">";
4362                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
4363                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Name:</td><td class=\"tablecell2\"><input type=\"text\" name=\"templatename\" maxlength=\"512\" size=\"64\"></td></tr>";
4364                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Description:</td><td class=\"tablecell2\"><input type=\"text\" name=\"templatedescription\" maxlength=\"512\" size=\"64\"></td></tr>";
4365                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filename:</td><td class=\"tablecell2\"><input type=\"text\" name=\"templatefilename\" maxlength=\"64\" size=\"32\"></td></tr>";
4366                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Template Layout:</td><td class=\"tablecell2\"><textarea name=\"templatelayout\" rows=\"15\" cols=\"50\" wrap=off></textarea></td></tr>";
4367                 $pagedata = $pagedata . "</table><br>";
4368                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Create New Template\"> | <input type=\"reset\" value=\"Clear Settings\">";
4369                 $pagedata = $pagedata . "</form>";
4370                 
4371                 return $pagedata;
4372                 
4373         }
4374         
4375         
4378 sub kiriwrite_template_edit{
4379 #################################################################################
4380 # kiriwrite_template_edit: Edit a template from the template folder.            #
4381 #                                                                               #
4382 # Usage:                                                                        #
4383 #                                                                               #
4384 # kiriwrite_template_edit(filename, [newfilename], [newname], [newdescription], #
4385 #                               [templatelayout], [confirm]);                   #
4386 #                                                                               #
4387 # filename              The current filename of the template to edit.           #
4388 # newfilename           The new filename of the template to edit.               #
4389 # newname               The new name of the template being edited.              #
4390 # newdescription        The new description of the template being edited.       #
4391 # templatelayout        The modified/altered template layout.                   #
4392 # confirm               Confirms the action to edit a template and its          #
4393 #                       settings.                                               #
4394 #################################################################################
4396         # Get all the variables that have been passed to the subroutine.
4398         my ($templatefilename, $templatenewfilename, $templatenewname, $templatenewdescription, $templatelayout, $confirm) = @_;
4400         # Load the Perl modules required for this subroutine.
4401         
4402         use DBI;
4403                 
4404         # Define the pagedata variable for later on.
4405         
4406         my $pagedata = "";
4407         
4408         # Check if the confirm variable is blank, if it is then
4409         # set confirm to '0'
4410         
4411         if (!$confirm){
4412         
4413                 # confirm is uninitalised/blank, so set the value of confirm
4414                 # to '0'
4415                 
4416                 $confirm = 0;
4417         
4418         }
4420         # Check if the template filename is blank and if it is, then return
4421         # an error.
4423         if (!$templatefilename){
4425                 kiriwrite_error("templatefilenameblank");
4427         }
4428         
4429         if ($confirm eq 1){
4431                 # Check certain strings to see if they UTF8 compiliant.
4432         
4433                 kiriwrite_variablecheck($templatenewname, "utf8", 0, 0);
4434                 kiriwrite_variablecheck($templatenewdescription, "utf8", 0, 0);
4435                 kiriwrite_variablecheck($templatelayout, "utf8", 0, 0);
4437                 # Convert the values into proper UTF8 strings.
4439                 $templatenewname        = kiriwrite_utf8convert($templatenewname);
4440                 $templatenewdescription = kiriwrite_utf8convert($templatenewdescription);
4441                 $templatelayout         = kiriwrite_utf8convert($templatelayout);
4443                 # Check if the filenames recieved are valid filenames.
4445                 kiriwrite_variablecheck($templatenewfilename, "maxlength", 64, 0);
4446                 kiriwrite_variablecheck($templatenewdescription, "maxlength", 512, 0);
4447                 kiriwrite_variablecheck($templatenewname, "maxlength", 512, 0);
4448                 kiriwrite_variablecheck($templatefilename, "maxlength", 64, 0);
4449                 kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
4451                 kiriwrite_variablecheck($templatefilename, "filename", "", 0);
4452                 kiriwrite_variablecheck($templatenewfilename, "filename", "", 0);
4454                 # Check that the template database file exists and the permissions 
4455                 # of the template database are valid.
4457                 my $template_database_exists            = kiriwrite_fileexists("templates.db");
4458                 my $template_database_permissions       = kiriwrite_filepermissions("templates.db", 1, 1, 0);
4460                 if ($template_database_exists eq 1){
4462                         # the template database does not exist so return an error.
4464                         kiriwrite_error("templatedatabasemissing");
4466                 }
4468                 if ($template_database_permissions eq 1){
4469                 
4470                         # The template database permissions are set incorrectly so
4471                         # return an error.
4473                         kiriwrite_error("templatedatabaseinvalidpermissions");
4475                 }
4477                 # Get the date.
4479                 my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
4481                 my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
4483                 # Convert the values so that they don't break when entering them into the SQL query.
4485                 $templatenewname        = kiriwrite_convert($templatenewname, "kiriwrite");
4486                 $templatenewdescription = kiriwrite_convert($templatenewdescription, "kiriwrite");
4487                 $templatelayout         = kiriwrite_convert($templatelayout, "kiriwrite");
4489                 # Load the SQLite database.
4491                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=templates.db");
4492                 my $string_handle       = $database_handle->prepare('UPDATE kiriwrite_templates SET
4493                         filename                = \'' . $templatenewfilename . '\',
4494                         templatename            = \'' . $templatenewname . '\',
4495                         templatedescription     = \'' . $templatenewdescription . '\',
4496                         templatelayout          = \'' . $templatelayout . '\',
4497                         datemodified            = \'' . $templatenewdate . '\'
4498                         WHERE filename          = \'' . $templatefilename . '\'
4499                 ') or die(kiriwrite_error("templatedatabaseinvalidformat"));
4500                 $string_handle->execute();
4502                 # Convert the following values so that display 
4504                 $templatenewname        = kiriwrite_convert($templatenewname, "normal_display");
4506                 # Append a link so that the user can return to the templates list.
4507                 
4508                 $pagedata = $pagedata . "<h2>Edit Template</h2>";
4509                 $pagedata = $pagedata . "The selected template called '" . $templatenewname . "' was edited.<p>";
4510                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=template\">Return to the templates list.</a>";
4511                 
4512         } else {
4513         
4514                 # Load the SQLite database. 
4516                 my $templatefilename_sql        = kiriwrite_convert($templatefilename, "kiriwrite");
4518                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=templates.db");
4519                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_templates where filename = \'' . $templatefilename_sql . '\' limit 1') or die(kiriwrite_error("templatedatabaseinvalidformat"));
4520                 $string_handle->execute();
4522                 # Check if there is a result from the SQL query made (meaning
4523                 # that the template exists).
4525                 my $template_found = 0;
4526                 my @database_template;
4528                 while (@database_template = $string_handle->fetchrow_array()){
4530                         # The template has been found, so set the template
4531                         # found value to 1 so it doesn't return an error.
4533                         $template_found = 1;
4535                 }
4537                 if ($template_found eq 0){
4539                         # The template was not found (doesn't exist) so
4540                         # return an error.
4542                         kiriwrite_error("templatedoesnotexist");
4544                 }
4546                 # Execute the query and get the information again.
4548                 $string_handle->execute();
4549                 @database_template      = $string_handle->fetchrow_array();
4551                 # Get the values from the query results.
4553                 my $template_filename           = $database_template[0];
4554                 my $template_name               = $database_template[1];
4555                 my $template_description        = $database_template[2];
4556                 my $template_layout             = $database_template[3];
4557                 my $template_modified           = $database_template[4];
4559                 # Convert the values that are going to be displayed.
4561                 $template_filename      = kiriwrite_convert($template_filename, "normal_display");
4562                 $template_name          = kiriwrite_convert($template_name, "normal_display");
4563                 $template_description   = kiriwrite_convert($template_description, "normal_display");
4564                 $template_layout        = kiriwrite_convert($template_layout, "normal_display");
4565                                 
4566                 # Write out the form for editing an template with the current template 
4567                 # settings put into the correct place.
4568                         
4569                 $pagedata = $pagedata . "<h2>Edit Template</h2>";
4570                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
4571                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"template\">";
4572                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"edit\">";
4573                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
4574                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"template\" value=\"" . $template_filename . "\">";
4575                 $pagedata = $pagedata . "<table cellspacing=\"0\" cellpadding=\"5\">";
4576                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
4577                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Name:</td><td class=\"tablecell2\"><input type=\"text\" name=\"newname\" value=\"" . $template_name . "\" maxlength=\"512\" size=\"64\"></td></tr>";
4578                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Description:</td><td class=\"tablecell2\"><input type=\"text\" name=\"newdescription\" value=\"" . $template_description . "\" maxlength=\"512\" size=\"64\"></td></tr>";
4579                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filename:</td><td class=\"tablecell2\"><input type=\"text\" name=\"newfilename\" maxlength=\"64\" size=\"32\" value=\"" . $template_filename . "\"></td></tr>";
4580                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Template Layout:</td><td class=\"tablecell2\"><textarea rows=\"15\" cols=\"50\" name=\"newlayout\" wrap=off>" . $template_layout . "</textarea></td></tr>";
4581                 $pagedata = $pagedata . "</table><br>";
4582                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Update template settings\"> | <input type=\"reset\" value=\"Restore current settings\"> | <a href=\"kiriwrite.cgi?mode=template\">Return to the templates list.</a>";
4583                 $pagedata = $pagedata . "</form>";      
4584         
4585         }
4587         return $pagedata;
4588         
4591 sub kiriwrite_template_delete{
4592 #################################################################################
4593 # kiriwrite_template_delete: Delete a template from the template folder.        #
4594 #                                                                               #
4595 # Usage:                                                                        #
4596 #                                                                               #
4597 # kiriwrite_template_delete(filename, confirm);                                 #
4598 #                                                                               #
4599 # filename      Specifies the filename of the database to delete.               #
4600 # confirm       Confirms the action to delete a template.                       #
4601 #################################################################################
4603         # Get the parameters that were passed to the subroutine.
4604         
4605         my ($template_filename, $template_confirm) = @_;
4606         
4607         if (!$template_confirm){
4608                 $template_confirm = 0;
4609         }
4611         # Load the required Perl modules
4612         use DBI;
4613         
4614         # Check the length of the variables.
4615         kiriwrite_variablecheck($template_filename, "maxlength", 64, 0);
4616         kiriwrite_variablecheck($template_confirm, "maxlength", 1, 0);
4617         
4618         # Check if the template_name string is blank and if it is then
4619         # return an error (as the template_name string should not be
4620         # blank.
4621         
4622         if (!$template_filename){
4623         
4624                 # The template_filename string really is blank, 
4625                 # so return an error saying that an empty
4626                 # filename was passed (to the subroutine).
4627         
4628                 kiriwrite_error("templatefilenameblank");
4629                 
4630         }
4631         
4632         # Check if the template_confirm string is blank and if it is, write
4633         # out a form asking the user to confirm the deletion.
4634         
4635         if ($template_confirm eq 1){
4637                 # The action to delete the template from the template database has
4638                 # been confirmed so delete the template.
4640                 # Check if the template database exists and the file permissions
4641                 # are valid and return an error if they aren't.
4643                 my $template_database_exists            = kiriwrite_fileexists("templates.db");
4644                 my $template_database_permissions       = kiriwrite_filepermissions("templates.db", 1, 1, 0);
4646                 if ($template_database_exists eq 1){
4648                         # the template database does not exist so return an error.
4650                         kiriwrite_error("templatedatabasemissing");
4652                 }
4654                 if ($template_database_permissions eq 1){
4655                 
4656                         # The template database permissions are set incorrectly so
4657                         # return an error.
4659                         kiriwrite_error("templatedatabaseinvalidpermissions");
4661                 }
4663                 # Load thw SQLite database.
4665                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=templates.db");
4667                 # Get the template name.
4669                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_templates WHERE filename = \'' . $template_filename . '\' LIMIT 1') or die(kiriwrite_error("templatedatabaseinvalidformat"));
4670                 $string_handle->execute();
4672                 my @database_template_page      = $string_handle->fetchrow_array();
4673                 my $database_template_name      = $database_template_page[1];
4675                 $database_template_name         = kiriwrite_convert($database_template_name, "normal_display");
4677                 # Delete the selected template.
4679                 $string_handle  = $database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $template_filename . '\'');
4680                 $string_handle->execute();
4681                 
4682                 my $pagedata = "<h2>Template Deleted</h2>";
4683                 $pagedata = $pagedata . "The selected template called '" . $database_template_name . "' was deleted.";
4684                 $pagedata = $pagedata . "<br><br><a href=\"kiriwrite.cgi?mode=template\">Return to the templates list</a>";
4686                 return $pagedata;
4687         
4688         } elsif ($template_confirm eq 0) {
4689         
4690                 # The template confirm value is 0 (previously blank and then set to 0), so
4691                 # write out a form asking the user to confirm the deletion of the template.
4692                 
4693                 # Check the templte database file exists and the permissions for the
4694                 # template database are valid.
4696                 my $template_database_exists            = kiriwrite_fileexists("templates.db");
4697                 my $template_database_permissions       = kiriwrite_filepermissions("templates.db", 1, 1, 0);
4699                 if ($template_database_exists eq 1){
4701                         # The template database does not exist so return an error.
4703                         kiriwrite_error("templatedatabasemissing");
4705                 }
4706                 
4707                 if ($template_database_permissions eq 1){
4709                         # The template database has invalid permissions set so return
4710                         # an error.
4712                         kiriwrite_error("templatedatabaseinvalidpermissions");
4714                 }
4716                 # Load the SQLite database.
4718                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=templates.db");
4719                 my $string_handle       = $database_handle->prepare('SELECT * FROM kiriwrite_templates WHERE filename = \'' . $template_filename . '\' LIMIT 1');
4720                 $string_handle->execute();
4722                 # Check if a result had returned and return an error if no result
4723                 # was returned.
4725                 my @template_data;
4726                 my $template_found = 0;
4728                 while(@template_data = $string_handle->fetchrow_array()){
4729                         
4730                         # A template has been found so increment the counter.
4732                         $template_found++;
4733                 }
4735                 if ($template_found eq 0){
4737                         # The template does not exist, so return an error.
4738                         kiriwrite_error("templatedoesnotexist");
4740                 }
4742                 # Get the template file information.
4744                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_templates WHERE filename = \'' . $template_filename . '\' LIMIT 1');
4745                 $string_handle->execute();
4746                 @template_data = $string_handle->fetchrow_array();
4748                 my $template_data_filename      = $template_data[0];
4749                 my $template_data_name          = $template_data[1];
4751                 # Check if the template name is blank and if it is 
4753                 # Convert the strings so that display properly.
4755                 $template_data_name     = kiriwrite_convert($template_data_name, "normal_display");
4756                 $template_data_name     = kiriwrite_convert($template_data_name, "normal_display");
4758                 # Write out the confirmation form.
4759                         
4760                 my $pagedata = "<h2>Template Deletion</h2>";
4761                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
4762                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"template\">";
4763                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"template\" value=\"" . $template_filename ."\">";
4764                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"delete\">";
4765                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
4766                 $pagedata = $pagedata . "Are you sure you want to delete the template called '" . $template_data_name . "' (" . $template_data_filename . ")?<br><br>";
4767                 $pagedata = $pagedata . "<input type=\"submit\" name=\"submit\" value=\"Yes, delete the template\"> | <a href=\"kiriwrite.cgi?mode=template\">No, return to the template list.</a>";
4768                                 
4769                 $pagedata = $pagedata . "</form>";
4770                 
4771                 return $pagedata;
4772         
4773         } else {
4775                         kiriwrite_error("invalidvariable");
4777         }
4781 sub kiriwrite_template_list{
4782 #################################################################################
4783 # kiriwrite_template_list: List the templates in the template folder.           #
4784 #                                                                               #
4785 # Usage:                                                                        #
4786 #                                                                               #
4787 # kiriwrite_template_list();                                                    #
4788 #################################################################################
4790         # Load the module required for processing the XML configuration files.
4791         
4792         use DBI;
4794         # Define certain values for later.
4796         my @database_template;
4797         
4798         my $database_handle;
4799         my $string_handle;
4800         
4801         my $template_filename           = "";
4802         my $template_name               = "";
4803         my $template_description        = "";
4804         my $template_data               = "";
4806         my $template_count = 0;
4808         my $template_style = 0;
4809         my $template_stylename = "";
4810         
4811         my $pagedata = "";
4812         my $templatedata = "";
4814         # Check if the template database exists and the permissions for it are
4815         # valid if the template database doesn't exist then write a message
4816         # saying it will be created when a template is added.
4818         my $template_database_permissions = kiriwrite_filepermissions("templates.db", 1, 0, 1);
4819         my $template_database_exists      = kiriwrite_fileexists("templates.db");
4821         if ($template_database_permissions eq 1){
4823                 # The templates directory has invalid permissions so an
4824                 # error will be returned.
4826                 kiriwrite_error("templatedatabaseinvalidpermissions");
4828         }
4830         if ($template_database_exists eq 1){
4832                 $pagedata = "<h2>View Templates</h2>";
4833                 $pagedata = $pagedata .  "<div class=\"errorbox\">The template database doesn't exist and will be created when a template is added.</div>";
4835                 return $pagedata;
4837         }
4838         
4839         # Place the table cell headings into the templatedata string.
4841         $templatedata = "<table cellspacing=\"0\" cellpadding=\"5\"><tr><td class=\"tablecellheader\">Template Filename</td><td class=\"tablecellheader\">Template Name</td><td class=\"tablecellheader\">Template Description</td><td class=\"tablecellheader\">Options</td></tr>";
4843         # Check if the database exists first before loading it.
4845         if ($template_database_exists eq 1){
4847         } else {
4849                 # Load the SQLite database.
4851                 $database_handle        = DBI->connect("dbi:SQLite:dbname=templates.db");
4852                 $string_handle          = $database_handle->prepare("SELECT * FROM kiriwrite_templates ORDER BY filename ASC") or die(kiriwrite_error("templatedatabaseinvalidformat"));
4853                 $string_handle->execute();
4855                 # Process each template.
4857                 while (@database_template = $string_handle->fetchrow_array()){
4859                         # Get certain values from the array.
4861                         $template_filename      = $database_template[0];
4862                         $template_name          = $database_template[1];
4863                         $template_description   = $database_template[2];
4864                         $template_data          = $database_template[3];
4866                         # Convert the data into information that can be displayed.
4868                         $template_filename      = kiriwrite_convert($template_filename, "normal_display");
4869                         $template_name          = kiriwrite_convert($template_name, "normal_display");
4870                         $template_description   = kiriwrite_convert($template_description, "normal_display");
4871                         $template_data          = kiriwrite_convert($template_data, "normal_display");
4873                         # Check what style the row of table cells should be.
4875                         if ($template_style eq 0){
4877                                 $template_stylename = "tablecell1";
4878                                 $template_style = 1;
4880                         } else {
4882                                 $template_stylename = "tablecell2";
4883                                 $template_style = 0;
4885                         }
4887                         # Check if the template name and descriptions are blank and if
4888                         # they are then insert a message saying that there's no name
4889                         # or no description.
4891                         if (!$template_name){
4892                                 $template_name = "<i>No Name</i>";
4893                         }
4895                         if (!$template_description){
4896                                 $template_description = "<i>No Description</i>";
4897                         }
4899                         # Check if there is no template data and if there isn't write
4900                         # a blank template message.
4902                         if (!$template_data){
4903                                 $template_filename = $template_filename . " <i>[Blank Template]</i>";
4904                         }
4906                         # Append the table row to the templatedata string.
4908                         $templatedata = $templatedata . "<tr><td class=\"" . $template_stylename  . "\">" . $template_filename . "</td><td class=\"" . $template_stylename  . "\">" . $template_name . "</td><td class=\"" . $template_stylename  . "\">" . $template_description . "</td><td class=\"" . $template_stylename  . "\"><a href=\"kiriwrite.cgi?mode=template&action=edit&template=" . $template_filename . "\">Edit</a> <a href=\"kiriwrite.cgi?mode=template&action=delete&template=" . $template_filename . "\">Delete</a></td></tr>";
4910                         # Increment the template counter.
4912                         $template_count++;
4914                 }
4916         }
4917         
4918         $templatedata = $templatedata . "</table>";
4920         # Check if any templates are in the database and if there isn't
4921         # then write a message saying that there are no templates in the
4922         # database.
4924         if ($template_count eq 0){
4925                 $templatedata = "<div class=\"errorbox\">There are no templates in the template database. To add a template click on the Add Template link.</div>";
4926         }
4928         # Process the templatedata into pagedata.
4930         $pagedata = "<h2>View Templates</h2>";
4931         $pagedata = $pagedata . $templatedata;
4932         
4933         return $pagedata;
4934         
4937 sub kiriwrite_database_add{
4938 #################################################################################
4939 # kiriwrite_database_add: Creates a new database.                               #
4940 #                                                                               #
4941 # Usage:                                                                        #
4942 #                                                                               #
4943 # kiriwrite_database_add(filename, name, description, [confirm]);               #
4944 #                                                                               #
4945 # filename      Specifies the filename for the database.                        #
4946 # name          Specifies a (friendly) name for the database.                   #
4947 # description   Specifies a description for the database.                       #
4948 # confirm       Confirms the action to create a database.                       #
4949 #################################################################################
4951         # Get the variables passed from the subroutine.
4952         
4953         my ($database_filename, $database_name, $database_description, $database_notes, $database_categories, $database_confirm) = @_;
4955         # Load the perl modules for this subroutine.
4957         use DBI;
4959         # Check if the confirm value is blank and if it is then
4960         # set the confirm value to 0.
4962         if (!$database_confirm){
4964                 # The confirm value was blank so set the value to 0.
4966                 $database_confirm = 0;
4968         }
4969         
4970         
4971         if ($database_confirm eq 1){
4973                 # The action to create a new database is confirmed.
4974                         
4975                 # Validate the database name and database descriptions.
4977                 my $database_name_check_utf8            = kiriwrite_variablecheck($database_name, "utf8", 0, 0);
4978                 my $database_description_check_utf8     = kiriwrite_variablecheck($database_description, "utf8", 0, 0);
4979                 my $database_notes_check_utf8           = kiriwrite_variablecheck($database_notes, "utf8", 0, 0);
4980                 my $database_categories_check_utf8      = kiriwrite_variablecheck($database_categories, "utf8", 0, 0);
4982                 # Convert the UTF8 strings before checking the length of the strings.
4984                 $database_name                  = kiriwrite_utf8convert($database_name);
4985                 $database_description           = kiriwrite_utf8convert($database_description);
4986                 $database_notes                 = kiriwrite_utf8convert($database_notes);
4987                 $database_categories            = kiriwrite_utf8convert($database_categories);
4989                 my $database_name_check_blank           = kiriwrite_variablecheck($database_name, "blank", 0, 1);               
4990                 my $database_name_check_length          = kiriwrite_variablecheck($database_name, "maxlength", 256, 1);
4991                 my $database_description_check_length   = kiriwrite_variablecheck($database_description, "maxlength", 512, 1);
4992                 my $database_filename_check_length      = kiriwrite_variablecheck($database_filename, "maxlength", 64, 1);
4993                 my $database_categories_check_length    = kiriwrite_variablecheck($database_categories, "maxlength", 512, 1);
4995                 # Check if values returned contains any values that would
4996                 # result in a specific error message being returned.
4998                 if ($database_name_check_length eq 1){
5000                         # The length of the database name is too long, so return an error.
5001                         kiriwrite_error("databasenametoolong");
5003                 }
5005                 if ($database_description_check_length eq 1){
5007                         # The database description length is too long, so return an error.
5008                         kiriwrite_error("databasedescriptiontoolong");
5010                 }
5012                 if ($database_name_check_blank eq 1){
5014                         # The database name is blank, so return an error.
5015                         kiriwrite_error("databasenameblank");
5017                 }
5019                 if ($database_filename_check_length eq 1){
5021                         # The database filename is to long, so return an error.
5022                         kiriwrite_error("databasefilenametoolong");
5024                 }
5026                 if ($database_categories_check_length eq 1){
5028                         # The database categories is too long, so return an error.
5029                         kiriwrite_error("databasecategoriestoolong");
5031                 }
5033                 # Check if the database filename is blank and if it is then
5034                 # generate a filename.
5035                         
5036                 if ($database_filename eq ""){
5037                         
5038                         # Filename is blank so generate a file name from
5039                         # the database name.
5040                                 
5041                         $database_filename = kiriwrite_processfilename($database_name);
5042                                 
5043                         # Check to make sure that the database filename really
5044                         # is valid.
5045                                 
5046                         kiriwrite_variablecheck($database_filename, "filename", "", 0);
5047                         kiriwrite_variablecheck($database_filename, "maxlength", 64, 0);
5048                                 
5049                         # Check if the database and database configuration file does
5050                         # not already exist.
5051                                 
5052                         if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db'){
5053                                         
5054                                 # A filename exists, so abort the script telling the user that those files exist.
5055                                 kiriwrite_error("fileexists");
5056                                         
5057                         }                               
5058                         
5059                 } else {
5060                         
5061                         # Filename is not blank, so check if the filenames are already
5062                         # being used and if the actual filename itself is valid.
5063                         
5064                         # Check the filename to make sure the database and the database configuration file does not already exist.
5065                         
5066                         if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db' ){
5067                                 
5068                                 # A filename exists, so abort the script telling the user that those files exist.
5069                                         
5070                                 kiriwrite_error("fileexists");
5071                                         
5072                                 
5073                         }
5074                                 
5075                         # Check to make sure the database filename itself really is 
5076                         # valid.
5077                                 
5078                         kiriwrite_variablecheck($database_filename, "filename", "", 0);
5079                         kiriwrite_variablecheck($database_filename, "maxlength", 64, 0);
5080                                 
5081                 }
5083                 # Convert certain data so that the SQL query doesn't break.
5085                 my $database_name_final = $database_name;
5086                 $database_name          = kiriwrite_convert($database_name, "kiriwrite");
5087                 $database_description   = kiriwrite_convert($database_description, "kiriwrite");
5088                 $database_categories    = kiriwrite_convert($database_categories, "kiriwrite");
5089                         
5090                 # Populate the database with the infomration and page tables.
5092                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . ".db");
5093                 my $string_handle       = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
5094                         name varchar(256) primary key, 
5095                         description varchar(512), 
5096                         notes text, 
5097                         categories varchar(512), 
5098                         kiriwrite_version_major int(4), 
5099                         kiriwrite_version_minor int(4), 
5100                         kiriwrite_version_revision int(4)
5101                 )');
5102                 $string_handle->execute();
5104                 $string_handle  = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
5105                         filename varchar(256) primary key, 
5106                         pagename varchar(512), 
5107                         pagedescription varchar(512), 
5108                         pagesection varchar(256),
5109                         pagetemplate varchar(64),
5110                         pagedata text,
5111                         pagesettings int(1),
5112                         lastmodified datetime
5113                 )');
5114                 $string_handle->execute();
5116                 # Add an entry to the kiriwrite_database_info table.
5118                 $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info values(
5119                         \'' . $database_name . '\',
5120                         \'' . $database_description . '\',
5121                         \'' . $database_notes . '\',
5122                         \'' . $database_categories . '\',
5123                         \'' . $kiriwrite_version{"major"} . '\',
5124                         \'' . $kiriwrite_version{"minor"} . '\',
5125                         \'' . $kiriwrite_version{"revision"} . '\'
5126                 )');
5127                 $string_handle->execute();
5128                 
5129                 $database_name_final = kiriwrite_convert($database_name_final, "normal_display");
5130         
5131                 my $pagedata    = "<h2>Add Database</h2>";
5132                 $pagedata       = $pagedata . "Database '" . $database_name_final  . "' has been created.<br><br>\r\n";
5133                 $pagedata       = $pagedata . "<a href=\"kiriwrite.cgi?mode=db\">Return to database list</a>";
5134                         
5135                 return $pagedata;
5136                                 
5137         }
5138                 
5139         # There is confirm value is not 1, so write a form for creating a database to
5140         # store pages in.
5141                         
5142         my $pagedata    = "<h2>Add Database</h2>\r\n";
5143                 
5144         $pagedata       = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
5145         $pagedata       = $pagedata . "\t\t\t<table cellpadding=\"5\" cellspacing=\"0\">\r\n";
5146         $pagedata       = $pagedata . "\t\t<input type=\"hidden\" name=\"mode\" value=\"db\">\r\n";
5147         $pagedata       = $pagedata . "\t\t<input type=\"hidden\" name=\"action\" value=\"new\">\r\n";
5148         $pagedata       = $pagedata . "\t\t<input type=\"hidden\" name=\"confirm\" value=\"1\">\r\n";
5149         $pagedata       = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
5150         $pagedata       = $pagedata . "<tr><td class=\"tablecell1\">Database name</td><td class=\"tablecell2\"><input type=\"text\" size=\"64\" maxlength=\"256\" name=\"databasename\"></td></tr>";
5151         $pagedata       = $pagedata . "<tr><td class=\"tablecell1\">Database description</td><td class=\"tablecell2\"><input type=\"text\" size=\"64\" maxlength=\"512\" name=\"databasedescription\"></td></tr>";
5152         $pagedata       = $pagedata . "<tr><td class=\"tablecell1\">Database notes</td><td class=\"tablecell2\"><textarea name=\"databasenotes\" wrap=\"off\" cols=\"50\" rows=\"10\"></textarea></td></tr>";
5153         $pagedata       = $pagedata . "<tr><td class=\"tablecell1\">Database categories</td><td class=\"tablecell2\"><input type=\"text\" size=\"64\" maxlength=\"512\" name=\"databasecategories\"></td></tr>";
5154         $pagedata       = $pagedata . "<tr><td class=\"tablecell1\">Database filename</td><td class=\"tablecell2\"><input type=\"text\" size=\"32\" maxlength=\"64\" name=\"databasefilename\"><br />
5155                 <ul>
5156                         <li>Leave the filename blank to automatically generate a filename.</li>
5157                         <li>Don't include .db as it will be appended automatically.</li>
5158                         <li>The filename cannot be any more than 64 characters long.</li>
5159                         <li>The filename should only contain letters and numbers, no spaces.</li>
5160                 </ul>           
5161         </td></tr>";
5162         $pagedata       = $pagedata . "\t\t\t</table>\r\n";
5163         $pagedata       = $pagedata . "\t\t\t<br />\r\n";
5164         $pagedata       = $pagedata . "\t\t\t<input type=\"submit\" value=\"Add Database\"> | <input type=\"reset\" value=\"Clear values\">";
5165         $pagedata       = $pagedata . "\t\t</form>\r\n";
5166                 
5167         # Exit the subroutine taking the data in the pagadata variable with it.
5168                 
5169         return $pagedata;
5173 sub kiriwrite_database_edit{
5174 #################################################################################
5175 # kiriwrite_database_edit: Edits an database.                                   #
5176 #                                                                               #
5177 # Usage:                                                                        #
5178 #                                                                               #
5179 # kiriwrite_database_edit(filename, name, description, newfilename, newname,    #
5180 #                               newdescription, notes, categories, [confirm]);  #
5181 #                                                                               #
5182 # filename              Specifies the filename of the database.                 #
5183 # name                  Specifies the name of the database.                     #
5184 # description           Specifies the description of the database.              #
5185 # newfilename           Specifies the new filename of the database.             #
5186 # newname               Specifies the new name of the database.                 #
5187 # newdescription        Specifies the new description of the database.          #
5188 # notes                 Specifies the new notes of the database.                #
5189 # categories            Specifies the new categories of the database.           #
5190 # confirm               Confirms the action to edit a database.                 #
5191 #################################################################################
5193         # First, get all the variables passed to the subroutine.
5195         my ($database_shortname, $database_name, $database_description, $database_newfilename, $database_newname, $database_newdescription, $database_notes, $database_categories, $database_confirm) = @_;
5197         # Load the needed Perl modules.
5198         
5199         use DBI;
5200         
5201         # Check if the database confirm value is blank and if it is
5202         # set the confirm value to 0.
5204         if (!$database_confirm){
5206                 $database_confirm = 0;
5208         }
5210         # Check if the confirm variable has a value in it, if it has, check again to make sure it really is the correct value (Perl moans
5211         # if $database_confirm was used directly).
5213         if ($database_confirm eq 1){
5215                 # Check if the database exists before trying to edit it.
5217                 my $database_exists     = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db');
5219                 if ($database_exists eq 1){
5221                         # The database does not exist so return an error.
5222         
5223                         kiriwrite_error("databasemissingfile");
5225                 }
5226                 
5227                 # Check if the new data passes the validation tests below. First, check the length of the variables.
5229                 my $database_name_check_utf8            = kiriwrite_variablecheck($database_newname, "utf8", 0, 0);
5230                 my $database_description_check_utf8     = kiriwrite_variablecheck($database_newdescription, "utf8", 0, 0);
5231                 my $database_notes_check_utf8           = kiriwrite_variablecheck($database_notes, "utf8", 0, 0);
5232                 my $database_categories_check_utf8      = kiriwrite_variablecheck($database_categories, "utf8", 0, 0);
5234                 # Convert the UTF8 strings to make sure their length is accurate.
5236                 $database_newname               = kiriwrite_utf8convert($database_newname);
5237                 $database_newdescription        = kiriwrite_utf8convert($database_newdescription);
5238                 $database_notes                 = kiriwrite_utf8convert($database_notes);
5239                 $database_categories            = kiriwrite_utf8convert($database_categories);
5241                 # Check if the database filename given is valid and return an error
5242                 # if it isn't.
5244                 kiriwrite_variablecheck($database_shortname, "filename", "", 0);
5246                 # Preform the following tests.
5247                 
5248                 my $database_filename_check_length      = kiriwrite_variablecheck($database_newfilename, "maxlength", 64, 1);
5249                 my $database_filename_letnum            = kiriwrite_variablecheck($database_newfilename, "filename", 0, 0);
5250                 my $database_name_check_length          = kiriwrite_variablecheck($database_newname, "maxlength", 256, 1);
5251                 my $database_description_check_length   = kiriwrite_variablecheck($database_newdescription, "maxlength", 512, 1);
5252                 my $database_categories_check_length    = kiriwrite_variablecheck($database_categories, "maxlength", 512, 1);
5253                 my $database_name_check_blank           = kiriwrite_variablecheck($database_newname, "blank", 0, 1);
5255                 # Check if the data is valid and return a specific error if it doesn't.
5257                 if ($database_name_check_length eq 1){
5259                         # The length of the database name is too long, so return an error.
5260                         kiriwrite_error("databasenametoolong");
5262                 }
5264                 if ($database_description_check_length eq 1){
5266                         # The database description length is too long, so return an error.
5267                         kiriwrite_error("databasedescriptiontoolong");
5269                 }
5271                 if ($database_name_check_blank eq 1){
5273                         # The database name is blank, so return an error.
5274                         kiriwrite_error("databasenameblank");
5276                 }
5278                 if ($database_filename_check_length eq 1){
5280                         # The database filename is too long, so return an error.
5281                         kiriwrite_error("databasefilenametoolong");
5283                 }
5285                 if ($database_categories_check_length eq 1){
5287                         # The database categories is too long, so return an error.
5288                         kiriwrite_error("databasecategoriestoolong");
5290                 }
5292                 # Check the permissions of the database file to see if it is valid
5293                 # and return an error if it isn't.
5295                 my $database_file_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db', 1, 1, 1);
5297                 if ($database_file_permissions eq 1){
5299                         # The database file itself has invalid permissions.
5301                         kiriwrite_error("databaseinvalidpermissions");
5303                 }
5305                 # Check if the old and new filenames have changed, if they have then rename the database file.
5306                         
5307                 my $pagedata;
5308                         
5309                 $pagedata = "<h2>Edit Database</h2>";
5310                 
5311                 if ($database_shortname ne $database_newfilename){
5313                         # Old name does not match with the new filename, so rename the database file.
5314                         
5315                         # Check if a database with the same shortname already exists, if it does then return with an error.
5316                                 
5317                         if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_newfilename . '.db'){
5318                                 kiriwrite_error("databasealreadyexists");
5319                         }
5321                         # Change the database filename.
5322                 
5323                         rename($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db', $kiriwrite_config{"directory_data_db"} . '/' . $database_newfilename . '.db');
5324                         unlink($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db');
5326                         # Set the filename to the new filename.
5328                         $database_shortname = $database_newfilename;
5330                 }
5331                         
5332                 # Load the SQLite database.
5334                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . ".db");
5336                 # Convert the UTF8 strings so that they can be properly updated.
5338                 $database_name                  = kiriwrite_utf8convert($database_name);
5339                 $database_description           = kiriwrite_utf8convert($database_description);
5341                 # Convert the strings that are going to put into the database so that
5342                 # the database query doesn't break.
5344                 $database_name                  = kiriwrite_convert($database_name, "kiriwrite");                               
5345                 $database_newname               = kiriwrite_convert($database_newname, "kiriwrite");
5346                 $database_description           = kiriwrite_convert($database_description, "kiriwrite");        
5347                 $database_newdescription        = kiriwrite_convert($database_newdescription, "kiriwrite");
5348                         
5349                 my $string_handle       = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $database_newname . '\', description = \'' . $database_newdescription . '\', notes = \'' . $database_notes . '\', categories = \'' . $database_categories . '\' WHERE name = \'' . $database_name . '\' AND description = \'' . $database_description . '\'') or kiriwrite_error("databasefileinvalid");
5350                 $string_handle->execute();
5352                 # Convert a string so that it can be displayed properly.
5354                 $database_newname = kiriwrite_convert($database_newname, "normal_display");
5356                 # Write out a message saying that the database has been updated.
5358                 $pagedata = $pagedata . "Database '" . $database_newname . "' updated.<br>";
5359                 $pagedata = $pagedata . "<br>\r\n<a href=\"kiriwrite.cgi?mode=db\">Return to database list.</a>";
5360                         
5361                 return $pagedata;
5362                 
5363         } else {
5365                 # Check if the database filename given is valid and return an error
5366                 # if it isn't.
5368                 kiriwrite_variablecheck($database_shortname, "filenameindir", "", 0);
5370                 # Check if the database exists before trying to edit it.
5372                 my $database_exists     = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db');
5374                 if ($database_exists eq 1){
5376                         # The database does not exist so return an error.
5377         
5378                         kiriwrite_error("databasemissingfile");
5380                 }
5383                 # Check the permissions of the database file to see if it is valid
5384                 # and return an error if it isn't.
5386                 my $database_file_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . '.db', 1, 0, 1);
5388                 if ($database_file_permissions eq 1){
5390                         # The database file itself has invalid permissions.
5392                         kiriwrite_error("databaseinvalidpermissions");
5394                 }
5396                 # Load the SQLite database.
5398                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_shortname . ".db");
5399                 my $string_handle       = $database_handle->prepare("SELECT * FROM kiriwrite_database_info LIMIT 1") or kiriwrite_error("databasefileinvalid");
5400                 $string_handle->execute();
5401                 my @database_info               = $string_handle->fetchrow_array();
5403                 # Get the values needed from the kiriwrite_database_info table.
5405                 my $database_oldname            = $database_info[0];
5406                 my $database_olddescription     = $database_info[1];
5407                 my $database_notes              = $database_info[2];
5408                 my $database_categories         = $database_info[3];
5410                 # Convert the values in the kieiwrite format into the format that can be understood
5411                 # with a web browser.
5413                 $database_oldname               = kiriwrite_convert($database_oldname, "normal_display");
5414                 $database_olddescription        = kiriwrite_convert($database_olddescription, "normal_display");
5415                 $database_notes                 = kiriwrite_convert($database_notes, "normal_display");
5416                 $database_categories            = kiriwrite_convert($database_categories, "normal_display");
5418                 # Print out the form for editing a database's settings.
5419                                 
5420                 my $pagedata = "<h2>Edit Database '" . $database_oldname . "'</h2>";
5421                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">\r\n";
5422                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"db\">";
5423                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"edit\">";
5424                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"database\" value=\"" . $database_shortname . "\">";
5425                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"yes\">";
5426                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"olddatabasename\" value=\"" . $database_oldname . "\">";
5427                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"olddatabasedescription\" value=\"" . $database_olddescription . "\">";
5428                 $pagedata = $pagedata . "<table cellspacing=\"0\" cellpadding=\"5\">\r\n";
5429                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
5430                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database name</td><td class=\"tablecell2\"><input type=\"text\" name=\"databasename\" value=\"" . $database_oldname . "\" maxlength=\"256\" size=\"64\"></td></tr>";
5431                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database description</td><td class=\"tablecell2\"><input name=\"databasedescription\" maxlength=\"512\" size=\"64\" value=\"" . $database_olddescription . "\"></td></tr>";
5432                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database notes</td><td class=\"tablecell2\"><textarea name=\"databasenotes\" cols=\"50\" rows=\"10\" wrap=\"off\">" . $database_notes . "</textarea></td></tr>";
5433                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database categories</td><td class=\"tablecell2\"><input name=\"databasecategories\"  value=\"" . $database_categories . "\"  size=\"64\" maxlength=\"512\"></td></tr>";
5434                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database filename</td><td class=\"tablecell2\"><input name=\"databasefilename\" maxlength=\"64\" size=\"64\" value=\"" . $database_shortname . "\"></td></tr>";           
5435                 $pagedata = $pagedata . "</table><p />";
5436                 
5437                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Edit settings\" /> | <input type=\"reset\" value=\"Reset settings\" />";
5438                 $pagedata = $pagedata . "</form>";
5439                 return $pagedata;
5440         
5441         }
5442         
5443         # The interpreter should not be here. So return an error saying invalid variable.
5444         
5445         kiriwrite_error("invalidvariable");
5449 sub kiriwrite_database_delete{
5450 #################################################################################
5451 # kiriwrite_database_delete: Deletes an database.                               #
5452 #                                                                               #
5453 # Usage:                                                                        #
5454 #                                                                               #
5455 # kiriwrite_database_delete(filename, [confirm]);                               #
5456 #                                                                               #
5457 # filename      Specifies the filename for the database to be deleted.          #
5458 # confirm       Confirms the action to delete a database.                       #
5459 #################################################################################
5461         my ($database_filename, $database_confirm) = @_;
5462         
5463         # Load the CGI module and the XML::Simple module for this subroutine.
5464         
5465         use DBI;
5466         
5467         # Check if the confirm value is blank and if it is then set the
5468         # confirm value to 0.
5470         if (!$database_confirm){
5472                 $database_confirm = 0;
5474         }
5476         # Check if the database exists before trying to edit it.
5478         my $database_exists     = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db');
5480         if ($database_exists eq 1){
5482                 # The database does not exist so return an error.
5483         
5484                 kiriwrite_error("databasemissingfile");
5486         }
5488         # Check if the database filename given is valid and return an error
5489         # if it isn't.
5491         kiriwrite_variablecheck($database_filename, "filename", "", 0);
5493         # Check if the request to delete a database has been confirmed. If it has, 
5494         # then delete the database itself.
5495         
5496         if ($database_confirm eq 1){
5497                 # There is a value in the confirm variable of the HTTP query.
5498                 
5499                 # Before deleting, check the permissions of the database file.
5500                         
5501                 my $deleted_database_file_permissions                   = 0;
5502                         
5503                 my $pagedata = "";
5505                 $deleted_database_file_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db', 1, 1, 1);
5507                 if ($deleted_database_file_permissions eq 1){
5509                         # The database file itself has invalid permissions.
5511                         kiriwrite_error("databaseinvalidpermissions");
5513                 }
5514                 
5515                 # Load the SQLite database to get the database name.
5517                 my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db');
5518                 my $string_handle       = $database_handle->prepare("SELECT * FROM kiriwrite_database_info LIMIT 1");
5519                 $string_handle->execute;
5520                 my @database_info       = $string_handle->fetchrow_array();
5522                 my $database_name = $database_info[0];
5523                 $database_name = kiriwrite_convert($database_name, "normal_display");
5525                 # Untaint the database filename given.
5527                 ($database_filename) = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
5529                 # Delete the database file.
5531                 unlink($kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db');
5532         
5533                 # Write a message saying that the database has been deleted.
5534                 
5535                 $pagedata = "<h2>Database Deleted</h2>";
5536                 $pagedata = $pagedata . "Database '" . $database_name . "' was deleted.<br />\r\n";
5538                 $pagedata = $pagedata . "<br><a href=\"kiriwrite.cgi?mode=db\">Return to database list.</a>"; 
5539                         
5540                 return $pagedata;
5541                 
5542         }
5543         
5544         # The action has not been confirmed, so write out a form asking the 
5545         # user to confirm.
5546         
5547         # Check the permissions of the database file to see if they are valid
5548         # and return an error if it isn't.
5550         my $delete_database_file_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db', 1, 1, 1);
5552         if ($delete_database_file_permissions eq 1){
5554                 # The database file itself has invalid permissions.
5556                 kiriwrite_error("databaseinvalidpermissions");
5558         }
5560         # Load the SQLite database to get the database name.
5561         
5562         my $database_handle     = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db');
5563         my $string_handle       = $database_handle->prepare("SELECT * FROM kiriwrite_database_info LIMIT 1");
5564         $string_handle->execute;
5565         my @database_info       = $string_handle->fetchrow_array();
5567         # Convert the database name so that it can be displayed properly.
5569         my $database_name = $database_info[0];
5570         $database_filename      = kiriwrite_convert($database_filename, "normal_display");
5571         $database_name          = kiriwrite_convert($database_name, "normal_display");
5572         
5573         # Write out the form to ask the user to confirm the deletion of the 
5574         # selected database.
5575         
5576         my $pagedata = "<h2>Database Deletion</h2>";
5577         $pagedata = $pagedata . "\t\tAre you sure you want to delete '". $database_name . "'\?<br><br>";
5578         $pagedata = $pagedata . "\t\t<form action=\"kiriwrite.cgi\" method=\"POST\">\r\n";
5579         $pagedata = $pagedata . "\t\t<input type=\"hidden\" name=\"mode\" value=\"db\">";
5580         $pagedata = $pagedata . "\t\t<input type=\"hidden\" name=\"action\" value=\"delete\">";
5581         $pagedata = $pagedata . "\t\t<input type=\"hidden\" name=\"database\" value=\"" . $database_filename . "\">";
5582         $pagedata = $pagedata . "\t\t<input type=\"hidden\" name=\"confirm\" value=\"1\">";
5583         $pagedata = $pagedata . "\t\t<input type=\"submit\" value=\"Delete Database\"> | <a href=\"kiriwrite.cgi?mode=db\">Return to database list.</a>";
5584         $pagedata = $pagedata . "\t\t</form>\r\n";
5585                 
5586         return $pagedata;
5590 sub kiriwrite_database_list{
5591 #################################################################################
5592 # kiriwrite_database_list: Lists the databases available.                       #
5593 #                                                                               #
5594 # Usage:                                                                        #
5595 #                                                                               #
5596 # kiriwrite_database_list();                                                    #
5597 #################################################################################
5599         # Load the following Perl modules.
5600         use DBI;
5602         # Check the directory to make sure the permissions are settings are valid
5603         # and return an error if the permission settings are invalid.
5605         my $data_directory_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"}, 1, 0);
5607         if ($data_directory_permissions eq 1){
5609                 # The data directory has invalid permissions set, so return an error.
5611                 kiriwrite_error("datadirectoryinvalidpermissions");
5613         }
5615         # Open the directory and get the list of all files ending with .xml and
5616         # put them into the data_directory array.
5618         opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
5619         my @data_directory = grep /m*\.db/, readdir(DATADIR);
5620         closedir(DATADIR);
5621         
5622         # Declare the following variables that are going to be used before using 
5623         # the foreach function.
5624         
5625         my $pagedata = "";
5626         my $database_count = 0;
5627         my $database_handle = "";
5628         my $database_filename = "";
5629         my $database_filename_friendly = "";
5630         my $database_filename_length = 0;
5631         my $database_permissions = "";
5632         my $data_file = "";
5633         my $string_handle = "";
5634         my @database_info;
5635         my $table_style = 0;
5636         my $table_style_name = "";
5637         my $permissions_warning = 0;
5638         my $permissions_list = "";
5639         my $invalid_warning = 0;
5640         my $invalid_list = "";
5641         
5642         # Begin creating the table for the list of databases.
5643         
5644         $pagedata = "<h2>Database List</h2>\r\n";
5645         
5646         $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\"><tr><td class=\"tablecellheader\">Database Name</td><td class=\"tablecellheader\">Database Description</td><td class=\"tablecellheader\">Options</td></tr>\r\n";
5647         
5648         foreach $data_file (@data_directory){
5649                 # Check the database file permissions before opening it.
5651                 $database_filename = $kiriwrite_config{"directory_data_db"} . '/' . $data_file;
5652                 $database_permissions = kiriwrite_filepermissions($database_filename, 1, 0, 0);
5654                 if ($database_permissions eq 1){
5655                         $permissions_warning = 1;
5656                         $permissions_list = $permissions_list . $data_file . "<br>\r\n";
5657                         next;
5658                 }
5660                 # Load the SQLite database.
5661                 
5662                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $database_filename);
5664                 # Query the SQLite database or return an error (meaning that the database is in
5665                 # a invalid format).
5667                 $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or (
5668                         $invalid_list = $invalid_list . $data_file . "<br>\r\n",
5669                         $invalid_warning = 1,
5670                         next
5671                 );
5673                 $string_handle->execute();
5674                 @database_info = $string_handle->fetchrow_array();
5675                 
5676                 # Check the style to be used with.
5678                 if ($table_style eq 0){
5680                         # Use the first style and set the style value
5681                         # to use the next style, the next time the
5682                         # if statement is checked.
5684                         $table_style_name = "tablecell1";
5685                         $table_style = 1;
5686                 } else {
5688                         # Use the second style and set the style
5689                         # value to use the first style, the next
5690                         # time if statement is checked.
5692                         $table_style_name = "tablecell2";
5693                         $table_style = 0;
5694                 }
5696                 $database_info[0] = kiriwrite_convert($database_info[0], "normal_display");
5697                 $database_info[1] = kiriwrite_convert($database_info[1], "normal_display");
5699                 # Create a friendly name for the database.
5701                 $database_filename_length = length($data_file);
5702                 $database_filename_friendly = substr($data_file, 0, $database_filename_length - 3);
5704                 # Append the database information to the table.
5706                 $pagedata = $pagedata . "<tr><td class=\"" . $table_style_name . "\"><a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_filename_friendly . "\">" . $database_info[0] . "</a></td><td class=\"" . $table_style_name . "\">" . $database_info[1] . "</td><td class=\"" . $table_style_name . "\"><a href=\"kiriwrite.cgi?mode=db&action=edit&&database=" . $database_filename_friendly . "\">Edit</a> <a href=\"kiriwrite.cgi?mode=db&action=delete&database=" . $database_filename_friendly . "\">Delete</a> <a href=\"kiriwrite.cgi?mode=compile&action=compile&type=single&database=" . $database_filename_friendly . "\">Compile</a></td></tr>\r\n";
5708                 $database_count++;
5709                 
5710         }
5711         
5712         $pagedata = $pagedata . "</table>\r\n<br />";
5714         # Check if there are no valid databases are if there is no
5715         # valid databases then write a message saying that no
5716         # valid databases are available.
5718         if ($database_count eq 0){
5720                 # 
5722                 $pagedata = "<h2>Database List</h2><p>";
5723                 $pagedata = $pagedata . "<div class=\"errorbox\">There are no databases that can be used. To create a database click on the Add Database link.</div><p>";
5725         }
5727         # Check if any databases with problems have appeared and if they
5728         # have, print out a message saying which databases have problems.
5730         if ($permissions_warning eq 1){
5732                 $pagedata = $pagedata . "<h4>Databases with invalid permissions</h4>";
5733                 $pagedata = $pagedata . "The following databases have invalid permissions set:<br><br>";
5734                 $pagedata = $pagedata . $permissions_list;
5736         }
5737         
5738         if ($invalid_warning eq 1){
5740                 $pagedata = $pagedata . "<h4>Databases with invalid formats</h4>";
5741                 $pagedata = $pagedata . "The following databases are in a invalid format:<br><br>";
5742                 $pagedata = $pagedata . $invalid_list;
5744         }
5746         return $pagedata;       # Return to the main part of the script with the processed information.
5751 sub kiriwrite_filter_list{
5752 #################################################################################
5753 # kiriwrite_filter_list: Lists the filters that will be used when compiling a   #
5754 # webpage.                                                                      #
5755 #                                                                               #
5756 # Usage:                                                                        #
5757 #                                                                               #
5758 # kiriwrite_filter_list();                                                      #
5759 #################################################################################
5760         
5761         # Load the needed Perl modules.
5763         use DBI;
5765         # Check if the filters database exists and the
5766         # permissions for the filters database are
5767         # valid.
5769         my $database_exists             = kiriwrite_fileexists("filters.db");
5770         my $database_permissions        = kiriwrite_filepermissions("filters.db", 1, 0, 0);
5771         my $filtersdb_notexist = 0;
5773         if ($database_exists eq 1){
5775                 # The filters database does not exist so
5776                 # set the value for the filters database
5777                 # not existing to 1.
5779                 $filtersdb_notexist = 1;                
5781         } 
5783         if ($database_permissions eq 1 && $database_exists ne 1){
5785                 # The filters database has invalid
5786                 # permissions set, so return an error.
5788                 kiriwrite_error("filtersdbpermissions");
5790         }
5792         # Define some variables required for processing the filters list.
5794         my @database_filters;
5795         my $database_handle;
5796         my $string_handle;
5797         my $pagedata = "";
5798         my $filterdata = "";
5799         my $filterswarning = "";
5800         my $filter_id;
5801         my $filter_id_out;
5802         my $filter_find;
5803         my $filter_find_out;
5804         my $filter_replace;
5805         my $filter_replace_out;
5806         my $filter_count = 0;
5807         my $filter_priority;
5808         my $filter_priority_out;
5809         my $filter_style = 0;
5810         my $filter_find_blank = 0;
5811         my $filter_style_name = "";
5813         $pagedata = $pagedata . "<h2>View Filters</h2>";
5815         # If the filters database exists then get the list of filters,
5816         # otherwise write a message saying that the filters database
5817         # does not exist and will be created when a filter is added.
5819         if ($filtersdb_notexist eq 0){
5821                 # Get the SQLite database and get the filters ordered
5822                 # by priority.
5824                 $database_handle        = DBI->connect("dbi:SQLite:dbname=filters.db");
5825                 $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_filters ORDER BY priority ASC') or kiriwrite_error("filtersdbinvalidformat");
5826                 $string_handle->execute();
5828                 $filterdata = $filterdata . "<table cellspacing=\"0\" cellpadding=\"5\">";
5829                 $filterdata = $filterdata . "<tr><td class=\"tablecellheader\">Priority</td><td class=\"tablecellheader\">Find Setting</td><td class=\"tablecellheader\">Replace Setting</td><td class=\"tablecellheader\">Options</td></tr>";
5831                 while (@database_filters = $string_handle->fetchrow_array()){
5833                         # Get the values from the from the array.
5835                         $filter_id              = $database_filters[0];
5836                         $filter_priority        = $database_filters[1];
5837                         $filter_find            = $database_filters[2];
5838                         $filter_replace         = $database_filters[3];
5840                         # Convert the values into values that can be displayed.
5842                         $filter_id_out          = kiriwrite_convert($filter_id, "normal_display");
5843                         $filter_priority_out    = kiriwrite_convert($filter_priority, "normal_display");
5844                         $filter_find_out        = kiriwrite_convert($filter_find, "normal_display");
5845                         $filter_replace_out     = kiriwrite_convert($filter_replace, "normal_display");
5847                         # Check which style should be used.
5849                         if ($filter_style eq 0){
5851                                 $filter_style_name = "tablecell1";
5852                                 $filter_style = 1;
5854                         } else {
5856                                 $filter_style_name = "tablecell2";
5857                                 $filter_style = 0;
5859                         }
5861                         # Check if the replace filter is blank and if it is
5862                         # then say no replace setting is given.
5864                         if (!$filter_replace){
5866                                 # No replace filter is specified, so write a message
5867                                 # saying that there is no replace filter.
5869                                 $filter_replace_out = "<i>Replace filter is blank.</i>";
5871                         }
5873                         # Check if the find filter is blank.
5875                         if (!$filter_find && $filter_find_blank eq 0){
5877                                 # No find filter is specified, So write a message saying
5878                                 # that one of your find filters is blank and needs to be 
5879                                 # fixed.
5881                                 $filterdata = "<b>Warning</b>: One (or more) of your filters has a blank find filter and needs to be fixed.<br><br>" . $filterdata;
5883                         }
5885                         # Add the filter to the list of filters in the database.
5887                         $filterdata     = $filterdata . "<tr><td class=\"" . $filter_style_name . "\">" . $filter_priority_out . "</td><td class=\"" . $filter_style_name . "\">" . $filter_find_out . "</td><td class=\"" . $filter_style_name . "\">" . $filter_replace_out . "</td><td class=\"" . $filter_style_name . "\"><a href=\"\"><a href=\"kiriwrite.cgi?mode=filter&action=edit&filter=" . $filter_id_out . "\">Edit</a></a> <a href=\"kiriwrite.cgi?mode=filter&action=delete&filter=" . $filter_id_out . "\">Delete</a></td></tr>";
5889                         $filter_count++;
5891                 }
5893                 $filterdata = $filterdata . "</table>";
5895                 # Check if there are filters in the filters database and
5896                 # write a message if there isn't.
5898                 if ($filter_count eq 0){
5900                         # There are no filters in the filters database.
5902                         $filterswarning = "There are no filters available in the filters database. To add a filter, click on the Add Filter link.";
5904                 }
5906         }
5908         # Check if the database wasn't found and if it
5909         # wasn't then write a message saying that the
5910         # database will be created when a filter is
5911         # added.
5913         if ($filtersdb_notexist eq 1){
5915                 # The filters database doesn't exist so write
5916                 # a message.
5918                 $filterswarning = "The filters database does not exist and will be created when a filter is added.";
5921         }
5923         # Check if there is a warning message and if
5924         # there is then write that warning message
5925         # else write the list of filters.
5927         if ($filterswarning){
5929                 $pagedata = $pagedata . "<div class=\"errorbox\">";
5930                 $pagedata = $pagedata . $filterswarning;
5931                 $pagedata = $pagedata . "</div>";
5933         } else {
5935                 # The filters database exists so write out the
5936                 # list of filters.
5938                 $pagedata = $pagedata . $filterdata;
5940         }
5942         return $pagedata;
5946 sub kiriwrite_filter_add{
5947 #################################################################################
5948 # kiriwrite_filter_add: Adds a filter to the filter list.                       #
5949 #                                                                               #
5950 # Usage:                                                                        #
5951 #                                                                               #
5952 # kiriwrite_filter_add(filterfind, filterreplace, filterpriority,               #
5953 #                       filternotes, [confirm]);                                #
5954 #                                                                               #
5955 # filterfind            Specifies the new word(s) to find.                      #
5956 # filterreplace         Specifies the new word(s) to replace.                   #
5957 # filterpriority        Specifies the new priority to use.                      #
5958 # filternotes           Specifies the new notes to use.                         #
5959 # confirm               Confirms the action to add a filter.                    #
5960 #################################################################################
5962         # Get the values that have been passed to the subroutine.
5963         
5964         my ($filter_new_find, $filter_new_replace, $filter_new_priority, $filter_new_notes, $confirm) = @_;
5966         # Load the needed Perl modules.
5968         use DBI;
5970         # Check the confirm value to make sure it is no more than
5971         # one character long.
5972         
5973         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
5974         
5975         # Define the page data value for later.
5977         my $pagedata = "";
5978         
5979         if (!$confirm){
5980         
5981                 # The confirm value is undefined, so set the
5982                 # value of the confirm integer to '0'.
5983         
5984                 $confirm = 0;
5985                 
5986         }
5987         
5988         if ($confirm eq 1){
5989         
5990                 # The confirm integer is '1', so add the word
5991                 # to the filter list.
5992         
5993                 # First, check the variables recieved are UTF8
5994                 # copliant.
5996                 kiriwrite_variablecheck($filter_new_find, "utf8", 0, 0);
5997                 kiriwrite_variablecheck($filter_new_replace, "utf8", 0, 0);
5998                 kiriwrite_variablecheck($filter_new_priority, "utf8", 0, 0);
6000                 # Convert the UTF8 values so that the length can
6001                 # checked properly.
6003                 $filter_new_find        = kiriwrite_utf8convert($filter_new_find);
6004                 $filter_new_replace     = kiriwrite_utf8convert($filter_new_replace);
6005                 $filter_new_priority    = kiriwrite_utf8convert($filter_new_priority);
6007                 # Check if the find filter is blank and return an error
6008                 # if it is.
6010                 if (!$filter_new_find){
6012                         # The find filter given is blank so return an
6013                         # error.
6015                         kiriwrite_error("blankfindfilter");
6017                 }
6019                 if (!$filter_new_priority){
6021                         # The filter priority is blank so set it
6022                         # to 1.
6024                         $filter_new_priority = 1;
6026                 }
6028                 # Check the length and contents of the values given
6029                 # to make sure they are valid.
6030                 
6031                 my $filterfind_maxlength_check          = kiriwrite_variablecheck($filter_new_find, "maxlength", 1024, 1);
6032                 my $filterreplace_maxlength_check       = kiriwrite_variablecheck($filter_new_replace, "maxlength", 1024, 1);
6033                 my $filterpriority_maxlength_check      = kiriwrite_variablecheck($filter_new_priority, "maxlength", 5, 1);
6034                 my $filterpriority_numbers_check        = kiriwrite_variablecheck($filter_new_priority, "numbers", 0, 1);
6036                 # Check if the result of the tests to see if they
6037                 # are valid.
6039                 if ($filterfind_maxlength_check eq 1){
6041                         # The find filter is too long, so return
6042                         # an error.
6044                         kiriwrite_error("findfiltertoolong");
6046                 }
6048                 if ($filterreplace_maxlength_check eq 1){
6050                         # The replace filter is too long, so
6051                         # return an error.
6053                         kiriwrite_error("replacefiltertoolong");
6055                 }
6057                 if ($filterpriority_maxlength_check eq 1){
6059                         # The length of the filter priority
6060                         # given is too long, so return an
6061                         # error.
6063                         kiriwrite_error("filterprioritytoolong");
6065                 }
6067                 if ($filterpriority_numbers_check eq 1){
6069                         # The priority of the filter given
6070                         # contains characters other than
6071                         # numbers.
6073                         kiriwrite_error("filterpriorityinvalidchars");
6075                 }
6077                 # Check if the filter priority is less than 1
6078                 # and more than 10000 and return an error
6079                 # if it is.
6081                 if ($filter_new_priority < 1 || $filter_new_priority > 50000){
6083                         # The filter priority is less than 1 and
6084                         # more than 10000, so return an error.
6086                         kiriwrite_error("filterpriorityinvalid");
6088                 }
6089                 
6090                 # Define some variables for later on.
6092                 my @database_filters;
6093                 my @database_check;
6095                 my $database_handle;
6096                 my $string_handle;
6097                 my $chk_database_handle;
6098                 my $chk_string_handle;
6100                 my $filters_exist;
6101                 my $filters_permissions;
6102                 my $filters_count       = 0;
6103                 my $blankid_found       = 0;
6104                 my $current_id          = 0;
6105                 my $next_id             = 0;
6106                 my $filter_exists       = 0;
6108                 my $first_filter        = 0;
6110                 my $new_id;
6112                 # Check if the filters database exists, has valid
6113                 # permission settings and if it doesn't create 
6114                 # the database first.
6116                 $filters_exist = kiriwrite_fileexists("filters.db");
6117                 
6118                 if ($filters_exist eq 0){
6119                 
6120                         # The filters database does exist, so don't
6121                         # bother trying to create another one.
6123                         $filters_permissions = kiriwrite_filepermissions("filters.db", 1, 1, 1);
6124                         
6125                         if ($filters_permissions eq 1){
6127                                 # The filters database has invalid permissions so
6128                                 # return an error.
6130                                 kiriwrite_error("filtersdbpermissions");                                
6132                         }
6134                         # Load the SQLite database.
6136                         $database_handle        = DBI->connect("dbi:SQLite:dbname=filters.db");
6137                 
6138                 } else {
6139                 
6140                         # The filters database does not exist, so create
6141                         # the filters database.
6142                         
6143                         # Check if the filters database can really be
6144                         # created.
6146                         my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
6148                         if ($directory_permissions eq 1){
6150                                 # The filters database cannot be created because of invalid directory
6151                                 # permissions so return an error.
6153                                 kiriwrite_error("filtersdatabasefilenotcreated");
6154                         }
6156                         # Create the filters database.
6158                         $database_handle        = DBI->connect("dbi:SQLite:dbname=filters.db");
6159                         $string_handle          = $database_handle->prepare('create table kiriwrite_filters(
6160                                         id int(7) primary key,
6161                                         priority int(5),
6162                                         findsetting varchar(1024),
6163                                         replacesetting varchar(1024),
6164                                         notes text
6165                                 );
6166                         ');
6167                         $string_handle->execute();
6168                 
6169                 }
6171                 $chk_database_handle    = DBI->connect("dbi:SQLite:dbname=filters.db");
6173                 # Find the lowest identification number available.
6175                 $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_filters ORDER BY id ASC') or kiriwrite_error("filtersdbinvalidformat");;
6176                 $string_handle->execute();
6178                 # Process each filter to find a blank identification
6179                 # number.
6181                 while(@database_filters = $string_handle->fetchrow_array()){
6183                         # Check the next filter identification number to see
6184                         # if it exists and use that identification number if
6185                         # it doesn't exist.
6187                         $current_id             = $database_filters[0];
6188                         $next_id                = $current_id + 1;
6189                         $chk_string_handle      = $chk_database_handle->prepare('SELECT * FROM kiriwrite_filters WHERE id = \'' . $next_id . '\' LIMIT 1');
6190                         $chk_string_handle->execute();
6192                         while(@database_check = $chk_string_handle->fetchrow_array()){
6194                                 $filter_exists = 1;
6197                         }
6199                         if ($filter_exists eq 0 && $blankid_found eq 0){
6201                                 $new_id         = $next_id;
6202                                 $blankid_found  = 1;
6204                         }
6206                         # Check if this is the first filter and the current
6207                         # identification number is more than 1 and if it
6208                         # is then set the new identification number to 1.
6210                         if ($first_filter eq 0 && $current_id > 1 && $blankid_found eq 0){
6212                                 # The first filter has an identification
6213                                 # number more than 1, so set the new
6214                                 # identification number to 1.
6216                                 $new_id         = 1;
6217                                 $blankid_found  = 1;
6218                                 $first_filter   = 1;
6221                         }
6223                         # Set the next identification number as the
6224                         # current identification number add one and
6225                         # increment the filters count.
6227                         $first_filter = 1;
6228                         $filters_count++;
6229                         $filter_exists = 0;
6231                 }
6233                 # Check if there are any filters in the database
6234                 # and if not set the identification number to 1.
6236                 if ($filters_count eq 0){
6238                         # No filters were really in the filters
6239                         # database.
6241                         $new_id = 1;
6243                 }
6245                 # Convert the values so that they can be processed
6246                 # properly.
6248                 my $filter_new_id_sql           = kiriwrite_convert($new_id, "kiriwrite");
6249                 my $filter_new_priority_sql     = kiriwrite_convert($filter_new_priority, "kiriwrite");
6250                 my $filter_new_find_sql         = kiriwrite_convert($filter_new_find, "kiriwrite");
6251                 my $filter_new_replace_sql      = kiriwrite_convert($filter_new_replace, "kiriwrite");
6252                 my $filter_new_notes_sql        = kiriwrite_convert($filter_new_notes, "kiriwrite");
6254                 # Add the new filter to the filters database.
6256                 $string_handle  = $database_handle->prepare('INSERT INTO kiriwrite_filters VALUES(
6257                         \'' . $filter_new_id_sql . '\',
6258                         \'' . $filter_new_priority_sql . '\',
6259                         \'' . $filter_new_find_sql . '\',
6260                         \'' . $filter_new_replace_sql . '\',
6261                         \'' . $filter_new_notes_sql . '\'
6262                 )');
6263                 $string_handle->execute();
6265                 # Write out a message saying that the filter was added to the
6266                 # filters database.
6267                 
6268                 $pagedata = $pagedata . "<h2>Filter Added</h2>";
6269                 $pagedata = $pagedata . "The filter was added sucessfully to the filters list.<br><br>";
6270                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=filter\">Return to the filters list.</a>";
6271                                 
6272                 return $pagedata;
6273                 
6274         } elsif ($confirm ne 0) {
6275         
6276                 # The confirm integer is another value (which
6277                 # it shouldn't be) so return an error.
6278                 
6279                 kiriwrite_error("invalidvalue");
6280         
6281         }
6282         
6283         # The confirm integer was blank so print out a form
6284         # for adding a new filter.
6285         
6286         $pagedata = "<h2>Add Filter</h2>";
6287         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
6288         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"filter\">";
6289         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"add\">";
6290         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
6291         $pagedata = $pagedata . "<table cellspacing=0 cellpadding=5>";
6292         $pagedata = $pagedata . "<Tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
6293         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Find...</td><td class=\"tablecell2\"><input type=\"input\" name=\"findword\" size=\"64\" maxlength=\"1024\"></td></tr>";
6294         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Replace with...</td><td class=\"tablecell2\"><input type=\"input\" name=\"replaceword\" size=\"64\" maxlength=\"1024\"></td></tr>";
6295         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filter Priority</td><td class=\"tablecell2\"><input type=\"input\" name=\"priority\" size=\"5\" maxlength=\"5\"><br><ul><li>If no filter priority is specified, the filter priority will be set to 1.</li></ul></td></tr>";
6296         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filter Notes</td><td class=\"tablecell2\"><textarea name=\"notes\" cols=\"50\" rows=\"10\"></textarea></td></tr>";
6297         $pagedata = $pagedata . "</table>";
6298         $pagedata = $pagedata . "<br>";
6299         $pagedata = $pagedata . "<input type=\"submit\" value=\"Add Filter\"> | <input type=\"reset\" value=\"Clear values\">";
6300         $pagedata = $pagedata . "</form>";
6301         
6302         return $pagedata;
6306 sub kiriwrite_filter_edit{
6307 #################################################################################
6308 # kiriwrite_filter_edit: Edits a filter from the filter list.                   #
6309 #                                                                               #
6310 # Usage:                                                                        #
6311 #                                                                               #
6312 # kiriwrite_filter_edit(filternumber, newfilterfind, newfilterreplace,          #
6313 #                       newfilterpriority, newfilternotes, confirm);            #
6314 #                                                                               #
6315 # filterid              Specifies the filter number (line number) in the        #
6316 #                       filters database.                                       #
6317 # newfilterfind         Specifies the new word to find.                         #
6318 # newfilterreplace      Specifies the new word to replace.                      #
6319 # newfilterpriority     Specifies the new filter priority.                      #
6320 # newfilternotes        Specifies the new filter notes.                         #
6321 # confirm               Confirms the action to edit a filter.                   #
6322 #################################################################################
6324         # Get the values that have been passed to the subroutine.
6325         
6326         my ($filter_id, $filter_new_find, $filter_new_replace, $filter_new_priority, $filter_new_notes, $confirm) = @_; 
6328         # Load the needed Perl modules.
6330         use DBI;
6332         # Check the confirm value to make sure it is no more than
6333         # one character long.
6335         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
6337         # Define the page data value for later.
6339         my $pagedata = "";
6341         # Check if the confirm value is blank and if it is
6342         # srt the confirm value to 0.
6344         if (!$confirm){
6346                 # The confirm value does not have any value
6347                 # set so set it to 0.
6349                 $confirm = 0;
6351         }
6353         # Check if the filter identification number is blank,
6354         # contains characters other than numbers and is more
6355         # than seven characters long.
6357         if (!$filter_id){
6359                 # The filter identification number is blank,
6360                 # so return an error.
6362                 kiriwrite_error("filteridblank");
6364         }
6366         my $filter_id_numbers_check     = kiriwrite_variablecheck($filter_id, "numbers", 0, 1);
6368         if ($filter_id_numbers_check eq 1){
6370                 # The filter identification number contains
6371                 # characters other than numbers, so return
6372                 # an error.
6374                 kiriwrite_error("filteridinvalid");
6376         }
6378         my $filter_id_maxlength_check   = kiriwrite_variablecheck($filter_id, "maxlength", 7, 1);
6380         if ($filter_id_maxlength_check eq 1){
6382                 # The filter identification number given
6383                 # is more than seven characters long, so
6384                 # return an error.
6386                 kiriwrite_error("filteridtoolong");
6388         }
6390         # Check if the filters database exists and the
6391         # permissions for it are valid.
6393         my $filters_exists      = kiriwrite_fileexists("filters.db");
6395         if ($filters_exists eq 1){
6397                 # The filters database does not exist, so
6398                 # return an error.
6400                 kiriwrite_error("filtersdbmissing");
6402         }
6404         my $filters_permissions = kiriwrite_filepermissions("filters.db", 1, 0);
6406         if ($filters_permissions eq 1){
6408                 # The filters database has invalid permissions
6409                 # set, so return an error.
6411                 kiriwrite_error("filtersdbpermissions");
6413         }
6415         # Define some variables for later.
6417         my $database_handle;
6418         my $string_handle;
6419         my $filter_id_sql       = kiriwrite_convert($filter_id, "kiriwrite");
6420         my $filter_id_out       = kiriwrite_convert($filter_id, "normal_display");
6421         my $filter_priority;
6422         my $filter_priority_sql;
6423         my $filter_priority_out;
6424         my $filter_find;
6425         my $filter_find_sql;
6426         my $filter_find_out;
6427         my $filter_replace;
6428         my $filter_replace_sql;
6429         my $filter_replace_out;
6430         my $filter_notes;
6431         my $filter_notes_sql;
6432         my $filter_notes_out;
6434         # Check if the action to edit a filter has been
6435         # confirmed.
6437         # Load the SQLite database.
6439         $database_handle        = DBI->connect("dbi:SQLite:dbname=filters.db");
6441         if ($confirm eq 1){
6443                 # The action to edit a filter has been confirmed so
6444                 # edit the selected filter.
6446                 # Check if changes to the database can be written to.
6448                 $filters_permissions    = kiriwrite_filepermissions("filters.db", 1, 1);
6450                 if ($filters_permissions eq 1){
6452                         # The filters database has invalid permissions
6453                         # set, so return an error.
6455                         kiriwrite_error("filtersdbpermissions");
6457                 }
6459                 # Define some variables for later.
6461                 my @database_filter;
6462                 my $filter_exists = 0;
6464                 # Check if the filter exists.
6466                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_filters WHERE id = \'' . $filter_id_sql . '\' LIMIT 1') or kiriwrite_error("filtersdbinvalidformat");
6467                 $string_handle->execute();
6469                 while (@database_filter = $string_handle->fetchrow_array()){
6471                         # The filter exists.
6473                         $filter_exists = 1;
6475                 }
6477                 # Check if the filter really doesn't exist.
6479                 if ($filter_exists eq 0){
6481                         # The filter really does not exist so
6482                         # return an error.
6484                         kiriwrite_error("filterdoesnotexist");
6486                 }
6488                 # First, check the variables recieved are UTF8
6489                 # copliant.
6491                 kiriwrite_variablecheck($filter_new_find, "utf8", 0, 0);
6492                 kiriwrite_variablecheck($filter_new_replace, "utf8", 0, 0);
6493                 kiriwrite_variablecheck($filter_new_priority, "utf8", 0, 0);
6494                 kiriwrite_variablecheck($filter_new_notes, "utf8", 0, 0);
6496                 # Convert the UTF8 values so that the length can
6497                 # checked properly.
6499                 $filter_find            = kiriwrite_utf8convert($filter_new_find);
6500                 $filter_replace         = kiriwrite_utf8convert($filter_new_replace);
6501                 $filter_priority        = kiriwrite_utf8convert($filter_new_priority);
6502                 $filter_notes           = kiriwrite_utf8convert($filter_new_notes);
6504                 # Check if the find filter is blank and return an error
6505                 # if it is.
6507                 if (!$filter_new_find){
6509                         # The find filter given is blank so return an
6510                         # error.
6512                         kiriwrite_error("blankfindfilter");
6514                 }
6516                 if (!$filter_new_priority){
6518                         # The filter priority is blank so set it
6519                         # to 1.
6521                         $filter_new_priority = 1;
6523                 }
6525                 # Check the length and contents of the values given
6526                 # to make sure they are valid.
6527                 
6528                 my $filterfind_maxlength_check          = kiriwrite_variablecheck($filter_new_find, "maxlength", 1024, 1);
6529                 my $filterreplace_maxlength_check       = kiriwrite_variablecheck($filter_new_replace, "maxlength", 1024, 1);
6530                 my $filterpriority_maxlength_check      = kiriwrite_variablecheck($filter_new_priority, "maxlength", 5, 1);
6531                 my $filterpriority_numbers_check        = kiriwrite_variablecheck($filter_new_priority, "numbers", 0, 1);
6533                 # Check if the result of the tests to see if they
6534                 # are valid.
6536                 if ($filterfind_maxlength_check eq 1){
6538                         # The find filter is too long, so return
6539                         # an error.
6541                         kiriwrite_error("findfiltertoolong");
6543                 }
6545                 if ($filterreplace_maxlength_check eq 1){
6547                         # The replace filter is too long, so
6548                         # return an error.
6550                         kiriwrite_error("replacefiltertoolong");
6552                 }
6554                 if ($filterpriority_maxlength_check eq 1){
6556                         # The length of the filter priority
6557                         # given is too long, so return an
6558                         # error.
6560                         kiriwrite_error("filterprioritytoolong");
6562                 }
6564                 if ($filterpriority_numbers_check eq 1){
6566                         # The priority of the filter given
6567                         # contains characters other than
6568                         # numbers.
6570                         kiriwrite_error("filterpriorityinvalidchars");
6572                 }
6574                 # Check if the filter priority is less than 1
6575                 # and more than 10000 and return an error
6576                 # if it is.
6578                 if ($filter_new_priority < 1 || $filter_new_priority > 50000){
6580                         # The filter priority is less than 1 and
6581                         # more than 10000, so return an error.
6583                         kiriwrite_error("filterpriorityinvalid");
6585                 }
6587                 # Convert the new information about the filter given
6588                 # so that it can be added to the SQL query properly.
6590                 $filter_find_sql        = kiriwrite_convert($filter_find, "kiriwrite");
6591                 $filter_replace_sql     = kiriwrite_convert($filter_replace, "kiriwrite");
6592                 $filter_priority_sql    = kiriwrite_convert($filter_priority, "kiriwrite");
6593                 $filter_notes_sql       = kiriwrite_convert($filter_notes, "kiriwrite");
6595                 # Edit the filter using the information provided.
6597                 $string_handle = $database_handle->prepare('UPDATE kiriwrite_filters SET priority = \'' . $filter_priority_sql . '\', findsetting = \'' . $filter_find_sql . '\', replacesetting = \'' . $filter_replace_sql . '\', notes = \'' . $filter_notes_sql . '\' WHERE id = \'' . $filter_id_sql . '\'');
6598                 $string_handle->execute();
6600                 # Write a message saying that the filter was edited.
6602                 $pagedata = $pagedata . "<h2>Filter edited</h2>";
6603                 $pagedata = $pagedata . "The selected filter was edited.<br><br>";
6604                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=filter\">Return to the list of filters.</a>";
6606                 return $pagedata;
6608         } elsif ($confirm eq 0){
6610                 # The action to edit a filter has not been confirmed
6611                 # so write a form for editing the filter with.
6613                 # Define some variables for later.
6615                 my @database_filter;
6616                 my $filter_exists = 0;
6618                 # Check if the filter exists.
6620                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_filters WHERE id = \'' . $filter_id_sql . '\' LIMIT 1') or kiriwrite_error("filtersdbinvalidformat");
6621                 $string_handle->execute();
6623                 while (@database_filter = $string_handle->fetchrow_array()){
6625                         # The filter exists.
6627                         $filter_exists = 1;
6629                 }
6631                 # Check if the filter really doesn't exist.
6633                 if ($filter_exists eq 0){
6635                         # The filter really does not exist so
6636                         # return an error.
6638                         kiriwrite_error("filterdoesnotexist");
6640                 }
6642                 # Get the filter information.
6644                 $string_handle->execute();
6646                 @database_filter = $string_handle->fetchrow_array();
6647                 
6648                 # Get the required information.
6650                 $filter_priority        = $database_filter[1];
6651                 $filter_find            = $database_filter[2];
6652                 $filter_replace         = $database_filter[3];
6653                 $filter_notes           = $database_filter[4];
6655                 $filter_priority_out    = kiriwrite_convert($filter_priority, "normal_display");
6656                 $filter_find_out        = kiriwrite_convert($filter_find, "normal_display");
6657                 $filter_replace_out     = kiriwrite_convert($filter_replace, "normal_display");
6658                 $filter_notes_out       = kiriwrite_convert($filter_notes, "normal_display");
6659                 
6660                 # Write out the form.
6662                 $pagedata = $pagedata . "<h2>Edit Filter</h2>";
6663                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
6664                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"filter\">";
6665                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"edit\">";
6666                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"filter\" value=\"" . $filter_id_out . "\">";
6667                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
6668                 $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\">";
6669                 $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
6670                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Find...</td><td class=\"tablecell2\"><input type=\"text\" name=\"filterfind\" size=\"64\" maxlength=\"1024\" value=\"" . $filter_find_out . "\"></td><tr>";
6671                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Replace With...</td><td class=\"tablecell2\"><input type=\"text\" name=\"filterreplace\" size=\"64\" maxlength=\"1024\" value=\"" . $filter_replace_out . "\"></td><tr>";
6672                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filter Priority</td><td class=\"tablecell2\"><input type=\"text\" name=\"priority\" size=\"5\" maxlength=\"5\" value=\"" . $filter_priority_out . "\"><ul><li>If no filter priority is specified, the filter priority will be set to 1.</li></ul></td><tr>";
6673                 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Filter Notes</td><td class=\"tablecell2\"><textarea name=\"notes\" rows=\"10\" cols=\"50\">" . $filter_notes_out . "</textarea></td><tr>";
6674                 $pagedata = $pagedata . "</table><br>";
6675                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Edit filter\"> | <input type=\"reset\" value=\"Restore filter settings\"> | <a href=\"kiriwrite.cgi?mode=filter\">Return to the filter database.</a>";
6676                 $pagedata = $pagedata . "</form>";
6678                 return $pagedata; 
6680         } else {
6682                 # A confirm value other than 0 or 1 has been
6683                 # specified, so return an error.
6685                 kiriwrite_error("invalidvalue");
6687         }
6691 sub kiriwrite_filter_delete{
6692 #################################################################################
6693 # kiriwrite_filter_delete: Deletes a filter from the filter list.               #
6694 #                                                                               #
6695 # Usage:                                                                        #
6696 #                                                                               #
6697 # kiriwrite_filter_delete(number, confirm);                                     #
6698 #                                                                               #
6699 # number        Specifies the filter line number to delete.                     #
6700 # confirm       Confirms the deletion of the selected filter.                   #
6701 #################################################################################
6703         # Get the values that were passed to this subroutine.
6704         
6705         my ($filter_id, $confirm) = @_;
6706         
6707         # Load the needed Perl modules.
6709         use DBI;
6711         # Check the confirm value to make sure it is no more than
6712         # one character long.
6714         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
6716         # Define the page data value for later.
6718         my $pagedata = "";
6720         # Check if the confirm value is blank and if it is
6721         # srt the confirm value to 0.
6723         if (!$confirm){
6725                 # The confirm value does not have any value
6726                 # set so set it to 0.
6728                 $confirm = 0;
6730         }
6732         # Check if the filter identification number is blank,
6733         # contains characters other than numbers and is more
6734         # than seven characters long.
6736         if (!$filter_id){
6738                 # The filter identification number is blank,
6739                 # so return an error.
6741                 kiriwrite_error("filteridblank");
6743         }
6745         my $filter_id_numbers_check     = kiriwrite_variablecheck($filter_id, "numbers", 0, 1);
6747         if ($filter_id_numbers_check eq 1){
6749                 # The filter identification number contains
6750                 # characters other than numbers, so return
6751                 # an error.
6753                 kiriwrite_error("filteridinvalid");
6755         }
6757         my $filter_id_maxlength_check   = kiriwrite_variablecheck($filter_id, "maxlength", 7, 1);
6759         if ($filter_id_maxlength_check eq 1){
6761                 # The filter identification number given
6762                 # is more than seven characters long, so
6763                 # return an error.
6765                 kiriwrite_error("filteridtoolong");
6767         }
6769         # Check if the filters database exists and the
6770         # permissions for it are valid.
6772         my $filters_exists      = kiriwrite_fileexists("filters.db");
6774         if ($filters_exists eq 1){
6776                 # The filters database does not exist, so
6777                 # return an error.
6779                 kiriwrite_error("filtersdbmissing");
6781         }
6783         my $filters_permissions = kiriwrite_filepermissions("filters.db", 1, 0);
6785         if ($filters_permissions eq 1){
6787                 # The filters database has invalid permissions
6788                 # set, so return an error.
6790                 kiriwrite_error("filtersdbpermissions");
6792         }
6793         
6794         # Define some values for later.
6796         my @database_filter;
6797         my $filterid_sql        = kiriwrite_convert($filter_id, "kiriwrite");
6798         my $filterid_out        = kiriwrite_convert($filter_id, "normal_display");
6799         my $database_handle;
6800         my $string_handle;
6801         my $filter_exists = 0;
6803         # Load the SQLite database.
6805         $database_handle        = DBI->connect("dbi:SQLite:dbname=filters.db");
6807         # Check if the confirm integer has a value of '1'.
6808         
6809         if ($confirm eq 1){
6810                 
6811                 # The action to delete a filter has been confirmed.
6813                 # Check the permissions of the filters database is
6814                 # valid (for writing).
6816                 $filters_permissions    = kiriwrite_filepermissions("filters.db", 1, 1);
6818                 if ($filters_permissions eq 1){
6820                         # The filters database has invalid permissions
6821                         # set, so return an error.
6823                         kiriwrite_error("filtersdbpermissions");
6825                 }
6827                 # Check if the filter exists.
6829                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_filters WHERE id = \'' . $filterid_sql . '\' LIMIT 1') or kiriwrite_error("filtersdbinvalidformat");
6830                 $string_handle->execute();
6832                 while (@database_filter = $string_handle->fetchrow_array()){
6834                         # The filter exists.
6836                         $filter_exists = 1;
6838                 }
6840                 # Check if the filter really doesn't exist.
6842                 if ($filter_exists eq 0){
6844                         # The filter really does not exist so
6845                         # return an error.
6847                         kiriwrite_error("filterdoesnotexist");
6849                 }
6851                 # Delete the filter from the filters database.
6853                 $string_handle  = $database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $filterid_sql . '\'') or kiriwrite_error("filtersdbinvalidformat");         
6854                 $string_handle->execute();
6856                 # Write a message saying that the filter was deleted
6857                 # from the filters database.
6859                 $pagedata = $pagedata . "<h2>Delete Filter</h2>";
6860                 $pagedata = $pagedata . "The selected filter was deleted from the filters database.<br><br>";
6861                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=filter\">Return to the filters database.</a>";
6863         } elsif ($confirm eq 0) {
6864         
6865                 # The action to delete a filter has not been confirmed so write
6866                 # a form asking for the user to confirm the deletion of the
6867                 # filter.
6869                 # Check if the filter exists.
6871                 $string_handle  = $database_handle->prepare('SELECT * FROM kiriwrite_filters WHERE id = \'' . $filterid_sql . '\' LIMIT 1') or kiriwrite_error("filtersdbinvalidformat");
6872                 $string_handle->execute();
6874                 while (@database_filter = $string_handle->fetchrow_array()){
6876                         # The filter exists.
6878                         $filter_exists = 1;
6880                 }
6882                 # Check if the filter really doesn't exist.
6884                 if ($filter_exists eq 0){
6886                         # The filter really does not exist so
6887                         # return an error.
6889                         kiriwrite_error("filterdoesnotexist");
6891                 }
6893                 # The confirm integer is '0', so continue write out
6894                 # a form asking the user to confirm the deletion
6895                 # pf the filter.
6896                 
6897                 $pagedata = $pagedata . "<h2>Confirm deletion</h2>";
6898                 $pagedata = $pagedata . "Are you sure you want to delete the selected filter?<br><br>";
6899                 $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
6900                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"filter\">";
6901                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"delete\">";
6902                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"filter\" value=\"" . $filterid_out . "\">";
6903                 $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
6904                 $pagedata = $pagedata . "<input type=\"submit\" value=\"Yes, delete the selected filter\"> | <a href=\"kiriwrite.cgi?mode=filter\">No, return to the filters list.</a>";
6905                 $pagedata = $pagedata . "</form>";
6906         
6907         } else {
6908         
6909                 kiriwrite_error("invalidvalue");
6910         
6911         }
6913         return $pagedata;
6917 sub kiriwrite_compile_makepages{
6918 #################################################################################
6919 # kiriwrite_compile_makepages: Compile the selected pages and place them in the #
6920 # specified output directory.                                                   #
6921 #                                                                               #
6922 # Usage:                                                                        #
6923 #                                                                               #
6924 # kiriwrite_compile_makepages(type, selectedlist, confirm);                     #
6925 #                                                                               #
6926 # type          Specifies if single or multiple databases are to be compiled.   #
6927 # confirm       Specifies if the action to compile the databases should really  #
6928 #               be done.                                                        #
6929 # selectedlist  Specifies the databases to compile from as an array.            #
6930 #################################################################################
6932         # Get the values that have been passed to the subroutine.
6933         
6934         my ($type, $confirm, @selectedlist) = @_;
6936         # Check if the confirm value is more than one
6937         # character long.
6939         kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
6941         # Check if the confirm value is blank and if it
6942         # is then set the confirm value to 0.
6944         if (!$confirm){
6946                 # The confirm value is blank, so set the
6947                 # confirm value to 0.
6949                 $confirm = 0;
6951         }       
6953         # Check if there are any databases selected
6954         # and return an error if there isn't.
6956         if (!@selectedlist){
6958                 # There are no databases in the array
6959                 # so return an error.
6961                 kiriwrite_error("nodatabaseselected");
6963         }
6965         # Check if the type given is no more than
6966         # 7 characters long.
6968         my $type_maxlength_check = kiriwrite_variablecheck($type, "maxlength", 8, 1);
6970         if ($type_maxlength_check eq 1){
6972                 # The type length given is too long so
6973                 # return an error.
6975                 kiriwrite_error("variabletoolong");
6977         }
6979         # Define the following values for later.
6981         my $pagedata = "";
6983         # Check if the action to compile the databases
6984         # has been confirmed.
6986         if ($confirm eq 1){
6988                 # The action to compile the datavases has
6989                 # been confirmed.
6991                 # Define some variables for later.
6993                 my %templatefiles;
6994                 my @databaseinfo;
6995                 my @databasepages;
6996                 my @filterslist;
6997                 my @findfilter;
6998                 my @replacefilter;
6999                 my @templateslist;
7000                 my @pagedirectories;
7001                 my $messages                    = "";
7002                 my $warning_count               = 0;
7003                 my $error_count                 = 0;
7004                 my $pages_count                 = 0;
7005                 my $filters_count               = 0;
7006                 my $filters_find_blank_warning  = 0;
7007                 my $database_handle;
7008                 my $string_handle;
7009                 my $filter_find;
7010                 my $filter_replace;
7011                 my $database;
7012                 my $database_out;
7013                 my $database_name;
7014                 my $database_name_out;
7015                 my $page_filename;
7016                 my $page_filename_out;
7017                 my $page_filename_check;
7018                 my $page_filename_char          = "";
7019                 my $page_filename_directory;
7020                 my $page_filename_length        = 0;
7021                 my $page_filename_seek          = 0;
7022                 my $page_filename_dircount      = 0;
7023                 my $page_directory_name;
7024                 my $page_directory_path;
7025                 my $page_name;
7026                 my $page_name_out;
7027                 my $page_description;
7028                 my $page_section;
7029                 my $page_template;
7030                 my $page_template_out;
7031                 my $page_content;
7032                 my $page_settings;
7033                 my $page_lastmodified;
7034                 my $page_title;
7035                 my $page_final;
7036                 my $page_autosection;
7037                 my $page_autotitle;
7038                 my $database_filename_check     = 0;
7039                 my $database_maxlength_check    = 0;            
7040                 my $output_exists               = 0;
7041                 my $output_permissions          = 0;
7042                 my $filters_exists              = 0;
7043                 my $filters_permissions         = 0;
7044                 my $filters_skip                = 0;
7045                 my $templates_exists            = 0;
7046                 my $templates_permissions       = 0;
7047                 my $templates_skip              = 0;
7048                 my $database_exists             = 0;
7049                 my $database_permissions        = 0;
7050                 my $information_prefix          = "[Information] ";
7051                 my $error_prefix                = "[Error] ";
7052                 my $warning_prefix              = "[Warning] ";
7054                 # Check if the output directory exists and has
7055                 # valid permissions set.
7057                 $output_exists          = kiriwrite_fileexists($kiriwrite_config{'directory_data_output'});
7059                 if ($output_exists ne 0){
7061                         # The output directory does not exist so
7062                         # return an error.
7064                         kiriwrite_error("outputdirectorymissing");
7066                 }
7068                 $output_permissions     = kiriwrite_filepermissions($kiriwrite_config{'directory_data_output'}, 1, 1);
7070                 if ($output_permissions ne 0){
7072                         # The output directory has invalid
7073                         # permissions set so return an error.
7075                         kiriwrite_error("outputdirectoryinvalidpermissions");
7077                 }
7079                 # Check if the filters database exists and has valid
7080                 # permissions set.
7082                 $filters_exists         = kiriwrite_fileexists("filters.db");
7084                 if ($filters_exists ne 0){
7086                         # The filters database does not exist
7087                         # so write a warning.
7089                         $messages = $messages . $warning_prefix . "The filters database does not exist. No filters will be used." . "<br>";
7090                         $filters_skip = 1;
7091                         $warning_count++;
7093                 }
7095                 $filters_permissions    = kiriwrite_filepermissions("filters.db", 1, 0);
7096                 
7097                 if ($filters_permissions ne 0 && $filters_exists eq 0){
7099                         # The filters database has invalid
7100                         # permissions set so write a warning.
7102                         $messages = $messages . $warning_prefix . "The filters database has invalid permissions set. No filters will be used." . "<br>";
7103                         $filters_skip = 1;
7104                         $warning_count++;
7106                 }
7108                 # Load the filters database (if the filters skip
7109                 # value isn't set to 1).
7111                 if ($filters_skip eq 0){
7113                         # Load the filters database.
7115                         $database_handle = DBI->connect("dbi:SQLite:dbname=filters.db");
7116                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_filters ORDER BY priority ASC') or $messages = $messages . $error_prefix . "The filters database is in an invalid format. No filters will be used." . "<br>", $error_count++, $filters_skip = 1;
7118                         # Check if the filters skip value is set to 0
7119                         # before executing the query.
7121                         if ($filters_skip eq 0){
7123                                 # Get the filters.
7125                                 $string_handle->execute();
7126                                 while (@filterslist = $string_handle->fetchrow_array()){
7128                                         # Check if the find filter is blank and
7129                                         # if it is then write a warning message.
7131                                         if (!$filterslist[2]){
7133                                                 if ($filters_find_blank_warning ne 1){
7135                                                         $messages = $messages . $warning_prefix . "One (or more) of the find filters from the filters database is blank." . "<br>";
7136                                                         $filters_find_blank_warning = 1;
7137                                                 }
7138                                                 next;
7140                                         } else {
7142                                                 # Add each find and replace filter.
7144                                                 $findfilter[$filters_count]     = $filterslist[2];
7145                                                 $replacefilter[$filters_count]  = $filterslist[3];
7147                                         }
7149                                         $filters_count++;
7151                                 }
7152                                 $messages = $messages . $information_prefix . "The filters database has been loaded." . "<br>";
7154                         }
7156                 }
7158                 # Check if the templates database exists and has
7159                 # valid permissions set.
7161                 $templates_exists       = kiriwrite_fileexists("templates.db");
7163                 if ($templates_exists ne 0){
7165                         # The template database does not exist
7166                         # so write a warning.
7168                         $messages = $messages . $warning_prefix . "The templates database does not exist. Pages will be compiled without templates being used." . "<br>";
7169                         $templates_skip = 1;
7170                         $warning_count++;
7172                 }
7174                 $templates_permissions  = kiriwrite_filepermissions("templates.db", 1, 0);
7176                 if ($templates_permissions ne 0 && $templates_exists eq 0){
7178                         # The template database has invalid
7179                         # permissions set so write a warning.
7181                         $messages = $messages . $warning_prefix . "The templates database has invalid permissions set. Pages will be compiled without templates being used." . "<br>";
7182                         $templates_skip = 1;
7183                         $warning_count++;
7185                 }
7187                 # Load the templates database (if the template
7188                 # skip value isn't set to 1).
7190                 if ($templates_skip eq 0){
7192                         # Load the templates database.
7194                         $database_handle = DBI->connect('dbi:SQLite:dbname=templates.db');
7195                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_templates') or $messages = $messages . $error_prefix . "The templates database is in an invalid format. Pages will be compiled without templates being used." . "<br>", $error_count++, $templates_skip = 1;
7197                         # Check if the templates skip value is set to
7198                         # 0 before executing the query.
7200                         if ($templates_skip eq 0){
7202                                 # Get the templates and place them into the
7203                                 # template files hash.
7205                                 $string_handle->execute();
7206                                 while(@templateslist = $string_handle->fetchrow_array()){
7208                                         # Place each template file into the hash.
7210                                         $templatefiles{$templateslist[0]}{template}     = $templateslist[3];
7211                                         $templatefiles{$templateslist[0]}{valid}        = 1;
7213                                 }
7214                                 $messages = $messages . $information_prefix . "The templates database has been loaded." . "<br>";
7216                         }
7218                 }
7220                 # Process each database.
7222                 foreach $database (@selectedlist){
7224                         # Check if the database filename and length
7225                         # are valid.
7227                         $messages = $messages . "<hr>";
7229                         $database_out   = kiriwrite_convert($database, "normal_display");
7230                         $database_filename_check        = kiriwrite_variablecheck($database, "page_filename", "", 1);
7231                         $database_maxlength_check       = kiriwrite_variablecheck($database, "maxlength", 64, 1);
7233                         if ($database_filename_check ne 0){
7235                                 # The database filename is invalid, so process
7236                                 # the next database.
7238                                 $messages       = $messages . $error_prefix . "The database filename '" . $database_out . ".db' is invalid. Skipping this database..." . "<br>";
7239                                 $error_count++;
7240                                 next;
7242                         }
7244                         if ($database_maxlength_check ne 0){
7246                                 # The database file is too long, so process the
7247                                 # next database.
7249                                 $messages       = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' is too long. Skipping this database..." . "<br>";
7250                                 $error_count++;
7251                                 next;
7253                         }
7255                         # Check if the database exists and has valid
7256                         # permissions set.
7258                         $database_exists        = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
7259                         
7260                         if ($database_exists ne 0){
7262                                 # The database does not exist so process
7263                                 # the next database.
7265                                 $messages       = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' does not exist. Skipping this database..." . "<br>";
7266                                 $error_count++;
7267                                 next;
7269                         }
7270                         
7271                         $database_permissions   = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db', 1, 0);
7273                         if ($database_permissions ne 0){
7275                                 # The permissions for the database are invalid
7276                                 # so process the next database.
7278                                 $messages       = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' has invalid permissions set. Skipping this database..." . "<br>";
7279                                 $error_count++;
7280                                 next;
7282                         }
7284                         # Load the SQLite database.
7286                         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
7288                         # Get the database name.
7290                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or $messages = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' is in an invalid format. Skipping this database..." . "<br>", next;
7291                         $string_handle->execute();
7292                         @databaseinfo = $string_handle->fetchrow_array();
7293                         $database_name = $databaseinfo[0];
7294                         $database_name_out = kiriwrite_convert($database_name, "normal_display");
7295                         $messages = $messages . $information_prefix . "Compiling pages in the '" . $database_name_out . "' database..." . "<br>";
7297                         # Get the pages in the database.
7299                         $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages') or $messages = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' is in an invalid format. Skipping this database..." . "<br>", next;
7300                         $string_handle->execute();
7302                         while(@databasepages = $string_handle->fetchrow_array()){
7304                                 # Convert the values so that they can be
7305                                 # used in the messages list.
7307                                 $page_filename          = $databasepages[0];
7308                                 $page_name              = $databasepages[1];
7309                                 $page_description       = $databasepages[2];
7310                                 $page_section           = $databasepages[3];
7311                                 $page_template          = $databasepages[4];
7312                                 $page_content           = $databasepages[5];
7313                                 $page_settings          = $databasepages[6];
7314                                 $page_lastmodified      = $databasepages[7];
7316                                 $page_filename_out      = kiriwrite_convert($page_filename, "normal_display");
7317                                 $page_name_out          = kiriwrite_convert($page_name, "normal_display");
7318                                 $page_template_out      = kiriwrite_convert($page_template, "normal_display");
7320                                 # Check if the filename is valid.
7322                                 $page_filename_check = kiriwrite_variablecheck($page_filename, "page_filename");
7324                                 if ($page_filename_check ne 0){
7326                                         # The file name is not valid so write a
7327                                         # error and process the next page.
7329                                         $messages = $messages . $error_prefix . "The page '" . $page_name_out . "' has an invalid filename. Page skipped." . "<br>";
7330                                         next;
7332                                 }
7334                                 # Check if the template with the filename does not exist
7335                                 # in the template files hash and write a message and
7336                                 # process the next page.
7337                                 
7338                                 if (!$templatefiles{$page_template}{valid} && $page_template ne "!none" && $templates_skip eq 0){
7340                                         $messages = $messages . $error_prefix . "The template with the filename '" . $page_template_out . "' for '" . $page_name_out . "' (" . $page_filename_out . ") does not exist." . "<br>";
7341                                         $error_count++;
7342                                         next;
7344                                         $page_final = $page_content;
7346                                 } elsif ($page_template eq "!none"){
7348                                         $page_final = $page_content;
7350                                 } else {
7352                                         $page_final = $templatefiles{$page_template}{template};
7353                                         $page_final =~ s/<kiriwrite:pagecontent>/$page_content/g;
7355                                 }
7357                                 # Create the combined page title (if needed).
7359                                 if ($page_settings eq 0){
7361                                         # Don't use page name or section name.
7363                                         $page_final =~ s/<kiriwrite:pagetitle>//g;
7365                                 } elsif ($page_settings eq 1){
7367                                         # Use the page name and section name.
7369                                         $page_autotitle = "(" . $page_section . " - " . $page_name . ")";
7370                                         $page_title = $page_section . " - " . $page_name;
7371                                         $page_final =~ s/<kiriwrite:pagetitle>/$page_title/g;
7373                                 } elsif ($page_settings eq 2){
7375                                         # Use the page name only.
7377                                         $page_autotitle = "(" . $page_name . ")";
7378                                         $page_final =~ s/<kiriwrite:pagetitle>/$page_name/g;
7380                                 } elsif ($page_settings eq 3){
7382                                         # Use the section name only.
7384                                         if ($page_section){
7385                                                 $page_autotitle = "(" . $page_section . ")";
7386                                         }
7387                                         $page_final =~ s/<kiriwrite:pagetitle>/$page_section/g;
7389                                 }
7391                                 # Check if the section name is not blank and
7392                                 # place brackets inbetween if it is.
7394                                 if ($page_section){
7396                                         $page_autosection = "(" . $page_section . ")";
7398                                 }
7400                                 # Replace each <kiriwrite> value with the apporiate page
7401                                 # values.
7403                                 $page_final =~ s/<kiriwrite:pagename>/$page_name/g;
7404                                 $page_final =~ s/<kiriwrite:pagedescription>/$page_description/g;
7405                                 $page_final =~ s/<kiriwrite:pagesection>/$page_section/g;
7406                                 $page_final =~ s/<kiriwrite:autosection>/$page_autosection/g;
7407                                 $page_final =~ s/<kiriwrite:autotitle>/$page_autotitle/g;
7409                                 # Process the filters on the page data.
7411                                 if ($filters_skip eq 0){
7413                                         $filters_count = 0;
7415                                         foreach $filter_find (@findfilter){
7417                                                 # Get the replace filter and process each
7418                                                 # filter on the page.
7420                                                 $filter_replace = $replacefilter[$filters_count];
7421                                                 $page_final =~ s/$filter_find/$filter_replace/g;
7422                                                 $filters_count++;
7424                                         }
7426                                 }
7428                                 # Process the page filename and check what directories
7429                                 # need to be created.
7431                                 $page_filename_length = int(length($page_filename));
7433                                 do {
7435                                         $page_filename_char = substr($page_filename, $page_filename_seek, 1);
7436                                         
7437                                         # Check if a forward slash appears and add it to
7438                                         # the list of directories array.
7440                                         if ($page_filename_char eq '/'){
7442                                                 # Append the directory name to the list of
7443                                                 # directories array.
7445                                                 $pagedirectories[$page_filename_dircount] = $page_filename_directory;
7446                                                 $page_filename_directory        = "";
7447                                                 $page_filename_char             = "";
7448                                                 $page_filename_dircount++;
7450                                         } else {
7452                                                 # Append the character to the directory/filename.
7454                                                 $page_filename_directory = $page_filename_directory . $page_filename_char;
7456                                         }
7458                                         $page_filename_seek++;
7460                                 } until ($page_filename_length eq $page_filename_seek);
7462                                 foreach $page_directory_name (@pagedirectories){
7464                                         # Check if the directory exists and create 
7465                                         # the directory if it doesn't exist.
7466                                 
7467                                         $page_directory_path = $page_directory_path . '/' . $page_directory_name;
7469                                         mkdir($kiriwrite_config{"directory_data_output"} . '/' . $page_directory_path);
7471                                 }
7473                                 # Write the file to the output directory.
7475                                 open(PAGE, "> " . $kiriwrite_config{"directory_data_output"} . '/' . $page_filename);
7476                                 print PAGE $page_final;
7477                                 close(PAGE);
7479                                 # Write a message saying the page has been compiled.
7481                                 $messages = $messages . $information_prefix . "'" . $page_name_out . "' (" . $page_filename_out . ") was compiled." . "<br>";
7482                                 $pages_count++;
7484                                 # Reset certain values.
7486                                 $page_autotitle = "";
7487                                 $page_autosection = "";
7488                                 $page_filename_seek = 0;
7489                                 $page_filename_dircount = 0;
7490                                 
7491                                 $page_filename_directory = "";
7492                                 $page_directory_path = "";
7493                                 $page_directory_name = "";
7494                                 @pagedirectories = ();
7496                         }
7498                         # Write a message saying that the database has
7499                         # been processed.
7501                         $messages = $messages . $information_prefix . "Finished compiling pages in the '" . $database_name_out . "' database..." . "<br>";
7503                 }
7505                 $messages = $messages . "<hr>";
7506                 $messages = $messages . $pages_count . " pages compiled, " . $error_count . " errors, " . $warning_count . " warnings.";
7507                 $pagedata = $pagedata . "<h2>Compile databases</h2>";
7508                 $pagedata = $pagedata . "<div class=\"datalist\">";
7509                 $pagedata = $pagedata . $messages;
7510                 $pagedata = $pagedata . "</div>";
7512                 return $pagedata;
7514         } elsif ($confirm eq 0){
7516                 # The action to compile the databases has
7517                 # not been confirmed so check what type
7518                 # is being used.
7520                 if ($type eq "single"){
7522                         # The type is a single database selected so
7523                         # process that database.
7525                         # Define some variables for later.
7527                         my $database_handle;
7528                         my $string_handle;
7529                         my @database_info;
7530                         my $database_filename_check;
7531                         my $database_maxlength_check;
7532                         my $databasefilename;
7533                         my $databasefilename_out;
7534                         my $database_name;
7535                         my $database_name_out;
7537                         # Check that the database name and length are
7538                         # valid and return an error if they aren't.
7540                         $databasefilename = $selectedlist[0];
7541                         $databasefilename_out = kiriwrite_convert($databasefilename, "normal_display");
7542                         $database_filename_check        = kiriwrite_variablecheck($databasefilename, "filename", "", 1);
7543                         $database_maxlength_check       = kiriwrite_variablecheck($databasefilename, "maxlength", 64, 1);
7545                         if ($database_filename_check ne 0){
7547                                 # The database filename is invalid, so
7548                                 # return an error.
7550                                 kiriwrite_error("databasefilenameinvalid");
7552                         }
7554                         if ($database_maxlength_check ne 0){
7556                                 # The database filename is too long, so
7557                                 # return an error.
7559                                 kiriwrite_error("databasefilenametoolong");
7561                         }
7563                         # Check if the database exists and has valid permissions
7564                         # set and skip them if they don't.
7566                         my $database_exists             = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $databasefilename . '.db');
7567                         my $database_permissions        = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $databasefilename . '.db', 1, 0);
7569                         if ($database_exists ne 0){
7571                                 # The database filename does not exist, so
7572                                 # return an error.
7574                                 kiriwrite_error("databasemissingfile");
7576                         }
7578                         if ($database_permissions ne 0){
7580                                 # The database permissions are invalid, so
7581                                 # return an error.
7583                                 kiriwrite_error("databaseinvalidpermissions");
7585                         }
7587                         # Load the SQLite database.
7589                         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $databasefilename . '.db');
7590                         
7591                         # Get the database name.
7593                         $string_handle          = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1');
7594                         $string_handle->execute();
7595                         @database_info = $string_handle->fetchrow_array();
7596                         $database_name          = $database_info[0];
7597                         $database_name_out      = kiriwrite_convert($database_name, "normal_display");
7599                         # Write out a form asking the user to confirm if the
7600                         # user wants to compile the selected database.
7602                         $pagedata = $pagedata . "<h2>Compile database</h2>";
7603                         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
7604                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"compile\">";
7605                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"compile\">";
7606                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"type\" value=\"multiple\">";
7607                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"id[1]\" value=\"" . $databasefilename_out . "\">";
7608                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"name[1]\" value=\"on\">";
7609                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
7610                         $pagedata = $pagedata . "Are you sure you want to compile the '" . $database_name_out . "' database?<br><br>";
7611                         $pagedata = $pagedata . "<input type=\"submit\" value=\"Compile database\"> | <a href=\"kiriwrite.cgi?mode=compile\">Return to the list of databases for compiling.</a>";
7612                         $pagedata = $pagedata . "</form>";
7613                         
7614                         return $pagedata;
7616                 } elsif ($type eq "multiple"){
7618                         # The type is multiple databases selected
7619                         # so process each database.
7621                         # Define some variables for later.
7623                         my $database_handle;
7624                         my $databasename;
7625                         my $databasename_out;
7626                         my @database_info;
7627                         my $database_filename_check;
7628                         my $database_maxlength_check;
7629                         my $database_exists;
7630                         my $database_permissions;
7631                         my $database_count = 0;
7632                         my $string_handle;
7633                         my $database_info_name;
7634                         my $database_info_name_out;
7635                         my $databaselist = "";
7637                         foreach $databasename (@selectedlist){
7639                                 # Check if the database is in the database
7640                                 # directory and skip it if it isn't.
7642                                 $database_filename_check        = kiriwrite_variablecheck($databasename, "filename", "", 1);
7643                                 $database_maxlength_check       = kiriwrite_variablecheck($databasename, "maxlength", 64, 1);
7645                                 if ($database_filename_check ne 0 || $database_maxlength_check ne 0){
7647                                         # The database filename given is invalid or
7648                                         # the database filename given is too long
7649                                         # so process the next database.
7651                                         next;
7653                                 }
7655                                 # Check if the database exists and has valid permissions
7656                                 # set and skip them if they don't.
7658                                 $database_exists        = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $databasename . '.db');
7659                                 $database_permissions   = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"} . '/' . $databasename . '.db', 1, 0);
7661                                 if ($database_exists ne 0 || $database_permissions ne 0){
7663                                         # The database does not exist or the permissions
7664                                         # for the database are invalid so process the
7665                                         # next database.
7667                                         next;
7669                                 }
7671                                 # Get the database name.
7673                                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $databasename . '.db');
7674                                 $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or next;
7675                                 $string_handle->execute();
7677                                 # Increment the amount of databases to compile.
7679                                 $database_count++;
7681                                 # Get the database name, convert it so that it can
7682                                 # be displayed properly and add it to the list of
7683                                 # databases to be compiled.
7685                                 @database_info = $string_handle->fetchrow_array();
7686                                 $database_info_name = $database_info[0];
7687                                 $database_info_name_out = kiriwrite_convert($database_info_name, "normal_display");
7688                                 $databasename_out = kiriwrite_convert($databasename, "normal_display");
7690                                 $databaselist = $databaselist . "<input type=\"hidden\" name=\"id[" . $database_count . "]\" value=\"" . $databasename_out . "\"><input type=\"hidden\" name=\"name[" . $database_count . "]\" value=\"on\">" . $database_info_name_out . "<br>";
7692                         }
7694                         # Check if any databases are available to be compiled.
7696                         if ($database_count eq 0){
7698                                 # No databases are available to be compiled.
7700                                 kiriwrite_error("nodatabaseselected");
7702                         }
7704                         # Write out the form for compiling the database.
7706                         $pagedata = $pagedata . "<h2>Compile selected databases</h2>";
7707                         $pagedata = $pagedata . "Do you want to compile the following databases?<br><br>";
7708                         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
7709                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"compile\">";
7710                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"compile\">";
7711                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"type\" value=\"multiple\">";
7712                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . $database_count . "\">";
7713                         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
7714                         $pagedata = $pagedata . "<div class=\"datalist\">";
7715                         $pagedata = $pagedata . $databaselist;
7716                         $pagedata = $pagedata . "</div><br>";
7717                         $pagedata = $pagedata . "<input type=\"submit\" value=\"Compile selected databases\"> | <a href=\"kiriwrite.cgi?mode=compile\">Return to the database list.</a>";
7718                         $pagedata = $pagedata . "</form>";
7720                         return $pagedata; 
7722                 } else {
7724                         # The type is something else other than
7725                         # single or multiple, so return an error.
7727                         kiriwrite_error("invalidvariable");
7729                 }
7731         } else {
7733                 # The confirm value is neither 0 or 1, so
7734                 # return an error.
7736                 kiriwrite_error("invalidvariable");
7738         }
7742 sub kiriwrite_compile_all{
7743 #################################################################################
7744 # kiriwrite_compile_all: Compile all of the databases in the database           #
7745 # directory.                                                                    #
7746 #                                                                               #
7747 # Usage:                                                                        #
7748 #                                                                               #
7749 # kiriwrite_compile_all();                                                      #
7750 #################################################################################
7752         # Get the list of databases to compile.
7754         opendir(DATABASE, $kiriwrite_config{"directory_data_db"});
7755         my @database_list = grep /m*\.db/, readdir(DATABASE);
7756         closedir(DATABASE);
7758         # Define some variables for later.
7760         my $databaseformdata;
7761         my $database_name               = "";
7762         my $database_name_filename_check;
7763         my $database_name_length        = 0;
7764         my $database_name_final_length  = 0;
7765         my $database_name_final         = "";
7766         my $database_count              = 1;
7767         
7768         foreach $database_name (@database_list){
7770                 # Check if the database filename is valid before
7771                 # using the database.
7773                 $database_name_filename_check   = kiriwrite_variablecheck($database_name, "filename", 0, 1);
7775                 if ($database_name_filename_check ne 0){
7777                         # The database filename is invalid so process
7778                         # the next database.
7780                         next;
7782                 }
7784                 # Get the database name without the extension.
7786                 $database_name_length           = length($database_name);
7787                 $database_name_final_length     = $database_name_length - 3;
7788                 $database_name_final            = substr($database_name, 0, $database_name_final_length);
7790                 # Process the final database name into form values.
7792                 $databaseformdata = $databaseformdata . "<input type=\"hidden\" name=\"id[" . $database_count . "]\" value=\"" . $database_name_final . "\">";
7793                 $databaseformdata = $databaseformdata . "<input type=\"hidden\" name=\"name[" . $database_count . "]\" value=\"on\">";
7795                 $database_count++;
7797         }
7799         # Check the list of databases to compile to see if it is blank,
7800         # if it is then return an error.
7802         if ($database_count eq 0){
7804                 # The list of database is blank so return an error.
7806                 kiriwrite_error("nodatabasesavailable");
7808         }
7810         # Write out a form for confirming the action to compile all of the databases.
7812         my $pagedata = "<h2>Compile all databases</h2>";
7813         $pagedata = $pagedata . "Do you want to compile all of the databases in the database directory?<br><br>";
7814         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
7815         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"compile\">";
7816         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"compile\">";
7817         $pagedata = $pagedata . "<input type=\"hidden\" name=\"type\" value=\"multiple\">";
7818         $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . $database_count . "\">";
7819         $pagedata = $pagedata . $databaseformdata;
7820         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
7821         $pagedata = $pagedata . "<input type=\"submit\" value=\"Compile all databases\"> | <a href=\"kiriwrite.cgi?mode=compile\">Return to the compile database list.</a>";
7822         $pagedata = $pagedata . "</form>";
7824         return $pagedata;
7828 sub kiriwrite_selectedlist{
7829 #################################################################################
7830 # kiriwrite_page_selectedlist: Get the list of selected pages to use.           #
7831 #                                                                               #
7832 # Usage:                                                                        #
7833 #                                                                               #
7834 # kiriwrite_page_selectedlist();                                                #
7835 #################################################################################
7837         # Load the required Perl modules.
7838         
7839         my $query = new CGI;
7840         
7841         my $count       = $query->param('count');
7842         
7843         # Check if the list of files has a value and if not set it 0.
7845         if (!$count){
7847                 $count = 0;
7849         }
7851         # Define some values for later.
7853         my @filename_list; 
7854         my @selected_list;
7855         my @final_list;
7857         my $filename;
7858         my $selected;
7859         
7860         my $final_count = 0;
7861         my $seek = 0;
7863         # Get the list of filenames.
7865         do {
7867                 # Get the values from id[]
7869                 $seek++;
7871                 $filename               = $query->param('id[' . $seek . ']');
7872                 $filename_list[$seek]   = $filename;
7874         } until ($seek eq $count || $count eq 0);
7876         # Get the list of selected filenames.
7878         $seek = 0;
7880         do {
7882                 # Get the values from name[]
7884                 $seek++;
7886                 $selected       = $query->param('name[' . $seek . ']');
7888                 if (!$selected){
7890                         $selected = 'off';
7892                 }
7894                 $selected_list[$seek]   = $selected;
7896         } until ($seek eq $count || $count eq 0);
7898         # Create a final list of filenames to be used for
7899         # processing.
7901         $seek = 0;
7903         do {
7905                 # Check if the selected value is on and include
7906                 # the filename in the final list.
7908                 $seek++;
7910                 $selected       = $selected_list[$seek];
7912                 if ($selected eq "on"){
7914                         $filename       = $filename_list[$seek];
7915                         $final_list[$final_count] = $filename;
7916                         $final_count++;
7918                 }
7920         } until ($seek eq $count || $count eq 0);
7922         return @final_list;
7926 sub kiriwrite_compile_list{
7927 #################################################################################
7928 # kiriwrite_compile_list: Shows a list of databases that can be compiled.       #
7929 #                                                                               #
7930 # Usage:                                                                        #
7931 #                                                                               #
7932 # kiriwrite_compile_list();                                                     #
7933 #################################################################################
7935         # Load the required Perl modules.
7936         
7937         use DBI;
7939         # Check the directory to make sure the permissions are settings are valid
7940         # and return an error if the permission settings are invalid.
7942         my $data_directory_permissions = kiriwrite_filepermissions($kiriwrite_config{"directory_data_db"}, 1, 0);
7944         if ($data_directory_permissions eq 1){
7946                 # The data directory has invalid permissions set, so return an error.
7948                 kiriwrite_error("datadirectoryinvalidpermissions");
7950         }
7952         # Open the directory and get the list of all files ending with .xml and
7953         # put them into the data_directory array.
7955         opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
7956         my @data_directory = grep /m*\.db/, readdir(DATADIR);
7957         closedir(DATADIR);
7959         # Declare the following variables that are going to be used before using 
7960         # the foreach function.
7961         
7962         my $pagedata = "";
7963         my $database_count = 0;
7964         my $database_handle = "";
7965         my $database_filename = "";
7966         my $database_filename_friendly = "";
7967         my $database_filename_length = 0;
7968         my $database_permissions = "";
7969         my $database_name_out = "";
7970         my $database_description_out = "";
7971         my $data_file = "";
7972         my $string_handle = "";
7973         my @database_info;
7974         my $table_style = 0;
7975         my $table_style_name = "";
7976         my $tabledata = "";
7977         my $permissions_warning = 0;
7978         my $permissions_list = "";
7979         my $select_menu = "";
7980         my $invalid_warning = 0;
7981         my $invalid_list = "";
7982         
7983         # Begin creating the table for the list of databases.
7984         
7985         $pagedata = "<h2>Compile Pages</h2>\r\n";
7986         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
7987         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"compile\">";
7988         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"compile\">";
7989         $pagedata = $pagedata . "<input type=\"hidden\" name=\"type\" value=\"multiple\">";
7991         $select_menu = "<input type=\"reset\" value=\"Select None\"> | <input type=\"submit\" value=\"Compile selected\">";
7993         foreach $data_file (@data_directory){
7994                 # Check the database file permissions before opening it.
7996                 $database_filename = $kiriwrite_config{"directory_data_db"} . '/' . $data_file;
7997                 $database_permissions = kiriwrite_filepermissions($database_filename, 1, 0, 0);
7999                 if ($database_permissions eq 1){
8000                         $permissions_warning = 1;
8001                         $permissions_list = $permissions_list . $data_file . "<br>\r\n";
8002                         next;
8003                 }
8005                 # Load the SQLite database.
8006                 
8007                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $database_filename);
8009                 # Query the SQLite database or return an error (meaning that the database is in
8010                 # a invalid format).
8012                 $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_info LIMIT 1') or (
8013                         $invalid_list = $invalid_list . $data_file . "<br>\r\n",
8014                         $invalid_warning = 1,
8015                         next
8016                 );
8018                 $string_handle->execute();
8019                 @database_info = $string_handle->fetchrow_array();
8020                 
8021                 # Check the style to be used with.
8023                 if ($table_style eq 0){
8025                         # Use the first style and set the style value
8026                         # to use the next style, the next time the
8027                         # if statement is checked.
8029                         $table_style_name = "tablecell1";
8030                         $table_style = 1;
8031                 } else {
8033                         # Use the second style and set the style
8034                         # value to use the first style, the next
8035                         # time if statement is checked.
8037                         $table_style_name = "tablecell2";
8038                         $table_style = 0;
8039                 }
8041                 $database_name_out              = kiriwrite_convert($database_info[0], "normal_display");
8042                 $database_description_out       = kiriwrite_convert($database_info[1], "normal_display");
8044                 # Create a friendly name for the database.
8046                 $database_filename_length = length($data_file);
8047                 $database_filename_friendly = substr($data_file, 0, $database_filename_length - 3);
8049                 $database_count++;
8051                 # Append the database information to the table.
8053                 $tabledata = $tabledata . "<tr><td class=\"" . $table_style_name . "\"><input type=\"hidden\" name=\"id[" . $database_count . "]\" value=\"" . $database_filename_friendly . "\"><input type=\"checkbox\" name=\"name[" . $database_count . "]\"></td><td class=\"" . $table_style_name . "\"><a href=\"kiriwrite.cgi?mode=page&action=view&database=" . $database_filename_friendly . "\">" . $database_name_out . "</a></td><td class=\"" . $table_style_name . "\">" . $database_description_out . "</td><td class=\"" . $table_style_name . "\"><a href=\"kiriwrite.cgi?mode=compile&action=compile&type=single&database=" . $database_filename_friendly . "\">Compile</a></td></tr>\r\n";
8054                 
8055         }
8057         $pagedata = $pagedata . "<input type=\"hidden\" name=\"count\" value=\"" . $database_count . "\">";
8059         $pagedata = $pagedata . $select_menu . "<br><br>";
8061         $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\"><tr><td class=\"tablecellheader\"></td><td class=\"tablecellheader\">Database Name</td><td class=\"tablecellheader\">Database Description</td><td class=\"tablecellheader\">Options</td></tr>\r\n";
8063         $pagedata = $pagedata . $tabledata;
8064         
8065         $pagedata = $pagedata . "</table>\r\n<br />";
8067         $pagedata = $pagedata . $select_menu;
8068         $pagedata = $pagedata . "</form>";
8070         # Check if there are no valid databases are if there is no
8071         # valid databases then write a message saying that no
8072         # valid databases are available.
8074         if ($database_count eq 0){
8076                 # 
8078                 $pagedata = "<h2>Database List</h2><p>";
8079                 $pagedata = $pagedata . "<div class=\"errorbox\">There are no databases that can be used for compiling.</div><p>";
8081         }
8083         # Check if any databases with problems have appeared and if they
8084         # have, print out a message saying which databases have problems.
8086         if ($permissions_warning eq 1){
8088                 $pagedata = $pagedata . "<h4>Databases with invalid permissions</h4>";
8089                 $pagedata = $pagedata . "The following databases have invalid permissions set:<br><br>";
8090                 $pagedata = $pagedata . $permissions_list;
8092         }
8093         
8094         if ($invalid_warning eq 1){
8096                 $pagedata = $pagedata . "<h4>Databases with invalid formats</h4>";
8097                 $pagedata = $pagedata . "The following databases are in a invalid format:<br><br>";
8098                 $pagedata = $pagedata . $invalid_list;
8100         }
8102         return $pagedata;
8106 sub kiriwrite_compile_clean{
8107 #################################################################################
8108 # kiriwrite_compile_clean: Deletes the contents of the output directory.        #
8109 #                                                                               #
8110 # Usage:                                                                        #
8111 #                                                                               #
8112 # kiriwrite_compile_clean(confirm);                                             #
8113 #                                                                               #
8114 # confirm       Confirms the deletion of files from the output directory.       #
8115 #################################################################################
8117         # Get the values passed to the subroutine.
8118         
8119         my ($confirm) = @_;
8121         # Define some variables for later.
8123         my $file_permissions;
8124         my $output_directory_exists;
8125         my $output_directory_permissions;
8126         my $warning_message;
8127         my $pagedata = "";
8129         # Check if the output directory exists.
8131         $output_directory_exists         = kiriwrite_fileexists($kiriwrite_config{"directory_data_output"});            
8133         if ($output_directory_exists eq 1){
8135                 # The output directory does not exist so return
8136                 # an error.
8138                 kiriwrite_error("outputdirectorymissing");
8140         }
8142         # Check if the output directory has invalid
8143         # permissions set.
8145         $output_directory_permissions   = kiriwrite_filepermissions($kiriwrite_config{"directory_data_output"});
8147         if ($output_directory_permissions eq 1){
8149                 # The output directory has invalid permissions
8150                 # set, so return an error.
8152                 kiriwrite_error("outputdirectoryinvalidpermissions");
8154         }
8155         
8156         if ($confirm) {
8157         
8158                 if ($confirm eq 1){
8159         
8160                         # The action to clean the output directory has been
8161                         # confirmed.
8162                         
8163                         # Remove the list of files and directories from the
8164                         # output directory.
8166                         $file_permissions = kiriwrite_compile_clean_helper($kiriwrite_config{"directory_data_output"}, 1);
8168                         if ($file_permissions eq 1){
8170                                 # One of the files or directories has invalid
8171                                 # permissions set so write a warning message.
8173                         }
8175                         $pagedata = "<h2>Clean Output Directory</h2>";
8177                         if ($file_permissions eq 1){
8179                                 $pagedata = $pagedata . "Some of the contents of the directory were removed. However, not all of the files in the output directory were deleted due to invalid permissions.<br><br>";
8181                         } else {
8183                                 $pagedata = $pagedata . "The contents of the output directory have been removed.<br><br>";
8185                         }
8187                         $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=compile\">Return to the compile databases list.</a>";
8188                         
8189                         return $pagedata;
8190                 
8191                 } else {
8192                 
8193                         # A value other than 1 is set for the confirm value
8194                         # (which it shouldn't be) so return an error.
8195                         
8196                         kiriwrite_error("invalidvariable");
8197                 
8198                 }
8199         
8200         }
8201         
8202         # Print out a form for cleaning the output directory.
8203         
8204         $pagedata = "<h2>Clean Output Directory</h2>";
8205         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
8206         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"compile\">";
8207         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"clean\">";
8208         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
8209         $pagedata = $pagedata . "Are you sure you want to clean the output directory?<br><br>";
8210         $pagedata = $pagedata . "<input type=\"submit\" value=\"Yes, clean output directory\"> | <a href=\"kiriwrite.cgi?mode=compile\">Return to compile list.</a>";
8211         $pagedata = $pagedata . "</form>";
8212         
8213         return $pagedata;
8217 sub kiriwrite_compile_clean_helper{
8218 #################################################################################
8219 # kiriwrite_compile_clean_helper: Helper for cleaning out the output directory. #
8220 # This command sometimes is called recursively (when a directory is found).     #
8221 #                                                                               #
8222 # Usage:                                                                        #
8223 #                                                                               #
8224 # kiriwrite_compile_clean_helper(directory, removedirectory, [permissions]);    #
8225 #                                                                               #
8226 # directory             Specifies the directory to remove files (and            #
8227 #                       sub-directories) from.                                  #
8228 # keepdirectory         Keeps the directory itself after all files have been    #
8229 #                       removed.                                                #
8230 # permissions           Used recursively for error checking.                    #
8231 #################################################################################
8233         # Get the values passed to the subroutine.
8234         
8235         my ($directory, $directory_keep, $permissions) = @_;    
8236         
8237         # Check if the directory_keep is only one charater long.
8238         
8239         my $directory_file = "";
8240         my @directory_list;
8241         my $file_permissions = 0;
8242         my $debug = 0;
8244         # Check if the file permissions value is blank.
8246         if (!$permissions){
8248                 # The file permissions value is blank.
8250                 $permissions = 0;
8252         }
8253         
8254         # Open the directory specified, read the contents of
8255         # the directory and then close the directory.
8256         
8257         opendir(DIRECTORY, $directory);
8258         @directory_list = readdir(DIRECTORY);
8259         closedir(DIRECTORY);
8260         
8261         # Remove each file and directory.
8262         
8263         foreach $directory_file (@directory_list){
8264         
8265                 # Check if the filename is '.' or '..' and if it
8266                 # is skip those files.
8267                 
8268                 if ($directory_file eq "." || $directory_file eq ".."){
8269                         
8270                         # The filename is '.' or '..' so skip processing
8271                         # these files.
8272                 
8273                 } else {
8275                         # Check if the permissions on the file or directory has
8276                         # valid permissions set.
8278                         $file_permissions = kiriwrite_filepermissions($directory . '/' . $directory_file, 1, 1);
8280                         if ($file_permissions eq 1){
8282                                 # The file or directory has invalid permissions set.
8284                                 $permissions = 1;
8285                                 next;
8287                         }
8289                         # Check if the filename is a directory.
8290                 
8291                         if (-d $directory . '/' . $directory_file){
8292                 
8293                                 # The filename is a directory so send the directory name
8294                                 # and this subroutine again (recursively).
8296                                 kiriwrite_compile_clean_helper($directory . '/' . $directory_file, 0, $permissions);
8297                 
8298                         } else {
8299                         
8300                                 # The file is not a directory but an actual file so
8301                                 # remove as normal (in terms of the Perl language).
8303                                 ($directory) = $directory =~ m/^([a-zA-Z0-9\/.]+)$/ig;
8304                                 ($directory_file) = $directory_file =~ m/^([a-zA-Z0-9.]+)$/ig;
8306                                 unlink($directory . '/' . $directory_file);
8307                         
8308                         }
8309         
8310                 }
8311                         
8312         }
8313         
8314         # Check if the directory should be kept.
8315         
8316         if ($directory_keep eq 1){
8317         
8318                 # The directory_keep value is set as 1 so the directory
8319                 # specified should be kept.
8320         
8321         } elsif ($directory_keep eq 0) {
8322         
8323                 # The directory_keep value is set as 0 so remove the
8324                 # directory specified.
8325                 
8326                 ($directory) = $directory =~ m/^([a-zA-Z0-9\/.]+)$/ig;
8327                 rmdir($directory);
8328         
8329         } else {
8330         
8331                 # A value other than 0 or 1 was specified so return
8332                 # an error,
8334                 kiriwrite_error('invalidvalue');
8335         
8336         }
8338         return $permissions;
8342 sub kiriwrite_settings_view{
8343 #################################################################################
8344 # kiriwrite_options_view: Writes out the list of options and variables.         #
8345 #                                                                               #
8346 # Usage:                                                                        #
8347 #                                                                               #
8348 # kiriwrite_settings_view();                                                    #
8349 #################################################################################
8351         # Get the settings.
8353         my $settings_directory_db       = $kiriwrite_config{"directory_data_db"};
8354         my $settings_directory_output   = $kiriwrite_config{"directory_data_output"};
8355         my $settings_noncgi_images      = $kiriwrite_config{"directory_noncgi_images"};
8356         my $settings_system_datetime    = $kiriwrite_config{"system_datetime"};
8357         my $settings_system_language    = $kiriwrite_config{"system_language"};
8358         my $settings_system_output      = $kiriwrite_config{"system_output"};
8360         # CONTINUE: Write out convert things.
8362         my $settings_directory_db_out           = kiriwrite_convert($settings_directory_db, "normal_display");
8363         my $settings_directory_output_out       = kiriwrite_convert($settings_directory_output, "normal_display");
8364         my $settings_noncgi_images_out          = kiriwrite_convert($settings_noncgi_images, "normal_display");
8365         my $settings_system_datetime_out        = kiriwrite_convert($settings_system_datetime, "normal_display");
8366         my $settings_system_language_out        = kiriwrite_convert($settings_system_language, "normal_display");
8367         my $settings_system_output_out          = kiriwrite_convert($settings_system_output, "normal_display");
8369         my $pagedata = "<h2>View Settings</h2>";
8370         
8371         $pagedata = $pagedata . "The current settings being used are the following:";
8372         
8373         $pagedata = $pagedata . "<br><br><table cellpadding=\"5\" cellspacing=\"0\">";
8374         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Setting</td><td class=\"tablecellheader\">Value</td></tr>";
8375         
8376         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Directories</td><td class=\"tablecellheader\"></td></tr>";
8377         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database Directory</td><td class=\"tablecell2\">" . $settings_directory_db_out . "</td></tr>";
8378         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Output Directory</td><td class=\"tablecell2\">" . $settings_directory_output_out . "</td></tr>";
8379         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Images (URI path)</td><td class=\"tablecell2\">" . $settings_noncgi_images_out . "</td></tr>";
8380         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Date</td><td class=\"tablecellheader\"></td></tr>";
8381         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Date format</td><td class=\"tablecell2\">" . $settings_system_datetime_out . "</td></tr>";
8382         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Language</td><td class=\"tablecellheader\"></td></tr>";
8383         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">System Language</td><td class=\"tablecell2\">" . $settings_system_language_out . "</td></tr>";
8384         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Output</td><td class=\"tablecellheader\"></td></tr>";
8385         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Output System</td><td class=\"tablecell2\">" . $settings_system_output_out . "</td></tr>";
8386         $pagedata = $pagedata . "</table><br>";
8387         
8388         $pagedata = $pagedata . "To alter the current settings, select the Edit Settings option at the top of the page.";
8390         return $pagedata;
8394 sub kiriwrite_settings_edit{
8395 #################################################################################
8396 # kiriwrite_options_edit: Edits the options.                                    #
8397 #                                                                               #
8398 # Usage:                                                                        #
8399 #                                                                               #
8400 # kiriwrite_options_edit(dbdirectory, outputdirectory, imagesuri,               #
8401 #                       languagesystem, outputsystem, confirm);                 #
8402 #                                                                               #
8403 # dbdirectory           Specifies the new database directory to use.            #
8404 # outputdirectory       Specifies the new output directory to use.              #
8405 # imagesuri             Specifies the new URI path for images.                  #
8406 # datetimeformat        Specifies the new date and time format.                 #
8407 # languagesystem        Specifies the new language to use for Kiriwrite.        #
8408 # outputsystem          Specifies the new output system to use for Kiriwrite.   #
8409 # confirm               Confirms the action to edit the settings.               #
8410 #################################################################################
8412         # Get the values that have been passed to the subroutine.
8413         
8414         my ($settings_dbdirectory, $settings_outputdirectory, $settings_imagesuri, $settings_datetimeformat, $settings_languagesystem, $settings_outputsystem, $confirm) = @_;
8415         
8416         # Load the required Perl modules.
8417         
8418         my $xsl = XML::Simple->new();
8420         if (!$confirm){
8421         
8422                 # If the confirm value is blank, then set the confirm
8423                 # value to 0.
8424         
8425                 $confirm = 0;
8426         
8427         }
8428         
8429         if ($confirm eq "1"){
8430         
8431                 # The action to edit the settings has been confirmed.
8432                 # Start by checking each variable about to be placed
8433                 # in the settings file is valid.
8434                 
8435                 # Deinfe some variables for later.
8437                 my @kiriwrite_new_settings;
8439                 # Check the length of the directory names.
8440                 
8441                 kiriwrite_variablecheck($settings_dbdirectory, "maxlength", 64, 0);
8442                 kiriwrite_variablecheck($settings_outputdirectory, "maxlength", 64, 0);
8443                 kiriwrite_variablecheck($settings_imagesuri, "maxlength", 512, 0);
8444                 kiriwrite_variablecheck($settings_datetimeformat, "maxlength", 32, 0);
8445                                 
8446                 kiriwrite_variablecheck($settings_languagesystem, "language_filename", "", 0);
8447                 kiriwrite_variablecheck($settings_outputsystem, "outputmodule", 0, 1);
8449                 # Check if the directory names only contain letters and numbers and
8450                 # return a specific error if they don't.
8452                 my $kiriwrite_dbdirectory_check         = kiriwrite_variablecheck($settings_dbdirectory, "directory", 0, 1);
8453                 my $kiriwrite_outputdirectory_check     = kiriwrite_variablecheck($settings_outputdirectory, "directory", 0, 1);
8454                 kiriwrite_variablecheck($settings_datetimeformat, "datetime", 0, 0);
8455                 
8456                 if ($kiriwrite_dbdirectory_check eq 1){
8458                         # The database directory name is blank, so return
8459                         # an error.
8461                         kiriwrite_error("dbdirectoryblank");
8463                 } elsif ($kiriwrite_dbdirectory_check eq 2){
8465                         # The database directory name is invalid, so return
8466                         # an error.
8468                         kiriwrite_error("dbdirectoryinvalid");
8470                 }
8472                 if ($kiriwrite_outputdirectory_check eq 1){
8474                         # The output directory name is blank, so return
8475                         # an error.
8477                         kiriwrite_error("outputdirectoryblank");
8479                 } elsif ($kiriwrite_outputdirectory_check eq 2){
8481                         # The output directory name is invalid, so return
8482                         # an error.
8484                         kiriwrite_error("outputdirectoryinvalid");
8486                 }
8488                 # Place the settings into the array.
8490                 $kiriwrite_new_settings[0] = $settings_dbdirectory;
8491                 $kiriwrite_new_settings[1] = $settings_outputdirectory;
8492                 $kiriwrite_new_settings[2] = $settings_imagesuri;
8493                 $kiriwrite_new_settings[3] = $settings_datetimeformat;
8494                 $kiriwrite_new_settings[4] = $settings_languagesystem;
8495                 $kiriwrite_new_settings[5] = $settings_outputsystem;
8497                 # Write the new settings to the XML file.
8498                 
8499                 kiriwrite_output_xml("kiriwrite.xml", "config", @kiriwrite_new_settings);
8500                 
8501                 # Write a confirmation message.
8502                 
8503                 my $pagedata = "<h2>Edit Settings</h2>";
8504                 $pagedata = $pagedata . "The page settings have been changed and will take effect on the next page load of Kiriwrite.<br><br>";
8505                 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=settings\">Return to the list of settings.</a>"; 
8506                 
8507                 return $pagedata;
8508         
8509         }
8510         
8511         # Get the list of languages available.
8512         
8513         my @language_directory          = "";
8514         my $language_filename           = "";
8515         my $language_file_xml           = "";
8516         my $language_file_systemname    = "";
8517         my $language_file_localname     = "";
8518         my $language_file_localname_out = "";
8519         my $language_file_seek          = 0;
8520         my $language_flie_dot           = 0;
8521         my $language_file_length        = 0;
8522         my $language_file_char          = "";
8523         my $language_file_friendly      = "";
8524         my $language_selectlist         = "";
8525         
8526         opendir(LANGUAGEDIR, "lang");
8527         @language_directory = grep /m*\.xml/, readdir(LANGUAGEDIR);
8528         closedir(LANGUAGEDIR);
8529         
8530         # Process each language by loading the XML configuration file
8531         # used for each language and then get the System name and 
8532         # the local name of the language.
8533         
8534         $language_selectlist = "<select name=\"language\">";
8535         
8536         foreach $language_filename (@language_directory){
8537         
8538                 # Load the language file currently selected.
8539         
8540                 $language_file_xml = $xsl->XMLin("lang" . '/' . $language_filename, SuppressEmpty => 1);
8541                 
8542                 # Get the system name and the local name of the language.
8543                 
8544                 $language_file_localname = $language_file_xml -> {about} -> {name};
8545                 
8546                 # Check if either the system name or the local name of the language
8547                 # is blank and if it is, then don't add the language to the list.
8548                 
8549                 if ($language_file_localname eq ""){
8550                         
8551                         # The system name or the local name is blank so don't add
8552                         # the language to the list.
8553                                                                 
8554                 } else {
8555                 
8556                         # Get the 'friendly' name of the language file name (basically
8557                         # remove the .xml part from the filename.
8558                 
8559                         $language_file_length = length($language_filename);
8560                         
8561                         do {
8562                         
8563                                 # Get a character from the language filename and currently
8564                                 # set by the seek counter.
8565                                 
8566                                 $language_file_char = substr($language_filename, $language_file_seek, 1);
8567                                 
8568                                 # Check if the character is a dot and if it is then set the
8569                                 # last dot value to the seek counter value.
8570                                 
8571                                 if ($language_file_char eq "."){
8572                                 
8573                                         # Current chatacter is a dot so set the last dot value 
8574                                         # to what is currently the seek counter.
8575                                         
8576                                         $language_flie_dot = $language_file_seek;
8577                                 
8578                                 } else {
8579                                 
8580                                         # Current character is not a dot, so do nothing.
8581                                 
8582                                 }
8583                         
8584                                 # Increment the seek counter.
8585                         
8586                                 $language_file_seek++;
8587                         
8588                         } until ($language_file_seek eq $language_file_length);
8589                         
8590                         # Reset the seek counter.
8591                         
8592                         $language_file_seek = 0;
8593                         
8594                         # Process the file name again and this time process the file
8595                         # name until it reaches the last dot found.
8596                         
8597                         do {
8598                         
8599                                 # Get the character the seek counter is currently set at.
8600                         
8601                                 $language_file_char = substr($language_filename, $language_file_seek, 1);
8603                                 # Append the character to the friendly file name.
8604                                 
8605                                 $language_file_friendly = $language_file_friendly . $language_file_char;
8606                                 
8607                                 # Increment the seek counter.
8608                                                         
8609                                 $language_file_seek++;
8610                         
8611                         } until ($language_file_seek eq $language_flie_dot);
8612                         
8613                         # Append the language to the available languages list.
8614                         
8615                         $language_file_localname_out = kiriwrite_convert($language_file_localname, "normal_display");
8616                         $language_selectlist = $language_selectlist . "<option value=\"" . $language_file_friendly . "\">" . $language_file_localname_out . "</option>";
8617                 
8618                         # Reset certain counters and values before continuing.
8619                         
8620                         $language_file_seek     = 0;
8621                         $language_flie_dot      = 0;
8622                         $language_file_length   = 0;
8623                         $language_file_char     = "";
8624                         $language_file_friendly = "";
8625                         
8626                 }
8627                 
8628         }       
8629         
8630         $language_selectlist = $language_selectlist . "</select>";
8631         
8632         # Get the list of output systems available.
8633         
8634         my @outputsys_directory         = "";
8635         my $outputsys_file              = "";
8636         my $outputsys_char              = "";
8637         my $outputsys_dot               = 0;
8638         my $outputsys_firstdot          = 0;
8639         my $outputsys_firstdotfound     = "";
8640         my $outputsys_seek              = 0;
8641         my $outputsys_length            = 0;
8642         my $outputsys_friendly          = "";
8643         my $outputsys_selectlist        = "";
8644         my $outputsys_config            = $kiriwrite_config{"system_output"};
8645         
8646         # Open and get the list of output systems (perl modules) by filtering
8647         # out the 
8648         
8649         opendir(OUTPUTSYSTEMDIR, "Modules/Output");
8650         @outputsys_directory = grep /m*\.pm/, readdir(OUTPUTSYSTEMDIR);
8651         closedir(OUTPUTSYSTEMDIR);
8652                 
8653         $outputsys_selectlist = "<select name=\"outputsys\">";  
8654         
8655         # Process each output system and add them to the list of available
8656         # output systems.
8657         
8658         foreach $outputsys_file (@outputsys_directory){
8659                 
8660                 # Get the length of the output system (perl module) filename.   
8662                 $outputsys_length = length($outputsys_file);
8663                 
8664                 # Get the friendly name of the Perl module (by getting rid of the
8665                 # .pm part of the filename).
8666                                 
8667                 do {
8668                 
8669                         $outputsys_char = substr($outputsys_file, $outputsys_seek, 1);
8670                         
8671                         # Check if the current character is a dot and if it is then
8672                         # set the last dot found number to the current seek number.
8673                         
8674                         if ($outputsys_char eq "."){
8675                         
8676                                 # Put the seek value as the last dot found number.
8677                         
8678                                 $outputsys_dot = $outputsys_seek;
8679                         
8680                         }
8681                         
8682                         # Increment the seek counter.
8683                         
8684                         $outputsys_seek++;
8685                 
8686                 } until ($outputsys_seek eq $outputsys_length);
8687                 
8688                 # Reset the seek counter as it is going to be used again.
8689                 
8690                 $outputsys_seek = 0;
8691                 
8692                 # Get the friendly name of the Perl module by the processing the file
8693                 # name to the last dot the previous 'do' tried to find.
8694                 
8695                 do {
8696                         
8697                         # Get the character the seek counter is currently set at.
8698                 
8699                         $outputsys_char = substr($outputsys_file, $outputsys_seek, 1);
8700                         
8701                         # Append the character to the friendly name of the output system.
8702                         
8703                         $outputsys_friendly = $outputsys_friendly . $outputsys_char;
8704                         
8705                         # Increment the seek counter.
8706                         
8707                         $outputsys_seek++;
8708                 
8709                 } until ($outputsys_seek eq $outputsys_dot);
8710                 
8711                 # Append the option to tbe list of available output systems.
8713                 # Check if the current friendly output module name matches with the
8714                 # output module name used in the configuration file.
8716                 if ($outputsys_friendly eq $outputsys_config){
8717                 
8718                         # The output module friendly name matches with the output
8719                         # module name used in the configuration file.
8721                         $outputsys_selectlist = $outputsys_selectlist . "<option value=\"" . $outputsys_friendly . "\" selected>" . $outputsys_friendly . "</option>";
8723                 } else {
8725                         # The output module friendly name does not match with the
8726                         # output module name used in the configuration file.
8728                         $outputsys_selectlist = $outputsys_selectlist . "<option value=\"" . $outputsys_friendly . "\">" . $outputsys_friendly . "</option>";
8730                 }
8731                 
8732                 # Reset the following values.
8733                 
8734                 $outputsys_seek         = 0;
8735                 $outputsys_length       = 0;
8736                 $outputsys_char         = "";
8737                 $outputsys_friendly     = "";
8738                 
8739                 
8740         }
8741         
8742         $outputsys_selectlist = $outputsys_selectlist . "</select>";    
8743         
8744         # Get the directory settings.
8745         
8746         my $directory_settings_database                 = $kiriwrite_config{"directory_data_db"};
8747         my $directory_settings_output                   = $kiriwrite_config{"directory_data_output"};
8748         my $directory_settings_imagesuri                = $kiriwrite_config{"directory_noncgi_images"};
8749         my $datetime_setting                            = $kiriwrite_config{"system_datetime"};
8751         my $directory_settings_database_out             = kiriwrite_convert($directory_settings_database, "normal_display");
8752         my $directory_settings_output_out               = kiriwrite_convert($directory_settings_output, "normal_display");
8753         my $directory_settings_imagesuri_out            = kiriwrite_convert($directory_settings_imagesuri, "normal_display");
8754         my $datetime_setting_out                        = kiriwrite_convert($datetime_setting, "normal_display");
8755         
8756         # Print out a form for editing the settings.
8758         my $pagedata = "<h2>Edit Settings</h2>";
8759         $pagedata = $pagedata . "<b>Warning:</b> Settings that have changed take effect after clicking on the 'Change Settings' button and viewing the confirmation message.<br><br>";
8760         
8761         $pagedata = $pagedata . "<form action=\"kiriwrite.cgi\" method=\"POST\">";
8762         $pagedata = $pagedata . "<input type=\"hidden\" name=\"mode\" value=\"settings\">";
8763         $pagedata = $pagedata . "<input type=\"hidden\" name=\"action\" value=\"edit\">";
8764         $pagedata = $pagedata . "<input type=\"hidden\" name=\"confirm\" value=\"1\">";
8765         $pagedata = $pagedata . "<table cellpadding=\"5\" cellspacing=\"0\">";
8766         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Directories</td><td class=\"tablecellheader\"></td></tr>";
8767         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Database Directory</td><td class=\"tablecell2\"><input type=\"text\" name=\"databasedir\" value=\"" . $directory_settings_database_out . "\" size=\"32\" maxlength=\"64\"></td></tr>";
8768         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Output Directory</td><td class=\"tablecell2\"><input type=\"text\" name=\"outputdir\" value=\"" . $directory_settings_output_out . "\" size=\"32\" maxlength=\"64\"></td></tr>";
8769         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Images (URI path)</td><td class=\"tablecell2\"><input type=\"text\" name=\"imagesuripath\" value=\"" . $directory_settings_imagesuri_out . "\" size=\"32\" maxlength=\"64\"></td></tr>";
8770         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Date</td><td class=\"tablecellheader\"></td></tr>";
8771         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Date format</td><td class=\"tablecell2\"><input type=\"text\" name=\"datetime\" value=\"" . $datetime_setting_out . "\" size=\"32\" maxlength=\"32\"><br><br>";
8772         $pagedata = $pagedata . "<div class=\"datalist\">D - Show single digit day if day value is less than 10.<br>
8773         DD - Show double digit day if day value is less than 10.<br>
8774         M - Show single digit month if month value is less than 10.<br>
8775         MM - Show double digit month if month value is less than 10.<br>
8776         Y - Show double digit year value.<br>
8777         YY - Show four digit year value.<br><br>
8778         h - Show single digit hour if hour value is less than 10.<br>
8779         hh - Show double digit hour if hour value is less than 10.<br>
8780         m - Show single digit minute if minute value is less than 10.<br>
8781         mm - Show double digit minute if minute value is less than 10.<br>
8782         s - Show single digit second if second value is less than 10.<br>
8783         ss - Show double digit second if second value is less than 10.<br><br>
8784         Other Characters: / - ( ) :
8785         </div>";
8786         $pagedata = $pagedata . "</td></tr>";
8787         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Languages</td><td class=\"tablecellheader\"></td></tr>";
8788         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">System Language</td><td class=\"tablecell2\">" . $language_selectlist . "</td></tr>";
8789         $pagedata = $pagedata . "<tr><td class=\"tablecellheader\">Output</td><td class=\"tablecellheader\"></td></tr>";
8790         $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Output System</td><td class=\"tablecell2\">" . $outputsys_selectlist . "</td></tr>";
8791         $pagedata = $pagedata . "</table><br>";
8792         $pagedata = $pagedata . "<input type=\"submit\" value=\"Change Settings\"> | <input type=\"reset\" value=\"Revert Settings\">";
8793         $pagedata = $pagedata . "</form>";
8794         
8795         return $pagedata;
8799 sub kiriwrite_settings_load{
8800 #################################################################################
8801 # kiriwrite_settings_load: Load the configuration settings into the global      #
8802 # variables.                                                                    #
8803 #                                                                               #
8804 # Usage:                                                                        #
8805 #                                                                               #
8806 # kiriwrite_settings_load();                                                    #
8807 #################################################################################
8809         # Load the required Perl modules.
8811         use XML::Simple;
8812         my $xsl = XML::Simple->new();   
8814         # Check if the Kiriwrite configuration file exists before using it and
8815         # return a critical error if it doesn't exist.
8817         my $kiriwrite_conf_exist = kiriwrite_fileexists("kiriwrite.xml");
8818         
8819         if ($kiriwrite_conf_exist eq 1){
8821                 # The configuration really does not exist so return an critical error.
8823                 kiriwrite_critical("configfilemissing");
8825         }
8827         # Check if the Kiriwrite configuration file has valid permission settings
8828         # before using it and return a critical error if it doesn't have the
8829         # valid permission settings.
8831         my $kiriwrite_conf_permissions = kiriwrite_filepermissions("kiriwrite.xml", 1, 0);
8833         if ($kiriwrite_conf_permissions eq 1){
8835                 # The permission settings for the Kiriwrite configuration file are
8836                 # invalid, so return an critical error.
8838                 kiriwrite_critical("configfileinvalidpermissions");
8840         }
8842         # Converts the XML file into meaningful data for later on in this subroutine.
8843         
8844         my $kiriwrite_conf_file = 'kiriwrite.xml';
8845         my $kiriwrite_conf_data = $xsl->XMLin($kiriwrite_conf_file, SuppressEmpty => 1);        
8847         # Go and fetch the settings and place them into a hash (that is global).
8848         
8849         %kiriwrite_config = (
8850         
8851                 "directory_data_db"             => $kiriwrite_conf_data->{settings}->{directories}->{database},
8852                 "directory_data_output"         => $kiriwrite_conf_data->{settings}->{directories}->{output},
8853                 "directory_noncgi_images"       => $kiriwrite_conf_data->{settings}->{directories}->{images},
8854                 
8855                 "system_language"               => $kiriwrite_conf_data->{settings}->{language}->{type},
8856                 "system_output"                 => $kiriwrite_conf_data->{settings}->{system}->{output},
8857                 "system_datetime"               => $kiriwrite_conf_data->{settings}->{system}->{datetime}
8858                 
8859         );      
8860         
8861         # Do a validation check on all of the variables that were loaded into the global configuration hash.
8862         
8863         kiriwrite_variablecheck($kiriwrite_config{"directory_data_db"}, "maxlength", 64, 0);
8864         kiriwrite_variablecheck($kiriwrite_config{"directory_data_output"}, "maxlength", 64, 0);
8865         kiriwrite_variablecheck($kiriwrite_config{"directory_noncgi_images"}, "maxlength", 512, 0);
8866         kiriwrite_variablecheck($kiriwrite_config{"directory_data_template"}, "maxlength", 64, 0);
8867         
8868         kiriwrite_variablecheck($kiriwrite_config{"system_language"}, "language_filename", "", 0);
8869         kiriwrite_variablecheck($kiriwrite_config{"system_output"}, "outputmodule", 0, 0);
8871         # Check if the output system module does exist before loading it and return an critical error
8872         # if the output system module does not exist.
8874         my $kiriwrite_config_systemoutput_fileexists = kiriwrite_fileexists("Modules/Output/" . $kiriwrite_config{"system_output"} . ".pm");
8876         if ($kiriwrite_config_systemoutput_fileexists eq 1){
8878                 # Output system module does not exist so return an critical error.
8879                 
8880                 kiriwrite_critical("outputsystemmissing");
8882         }
8884         # Check if the output system module does have the valid permission settings and return an
8885         # critical error if the output system module contains invalid permission settings.
8887         my $kiriwrite_config_systemoutput_permissions = kiriwrite_filepermissions("Modules/Output/" . $kiriwrite_config{"system_output"} . ".pm", 1, 0);
8889         if ($kiriwrite_config_systemoutput_permissions eq 1){
8891                 # Output system contains invalid permissions so return an critical error.
8893                 kiriwrite_critical("outputsysteminvalidpermissions");
8895         }
8897         return;
8898         
8901 sub kiriwrite_variablecheck{
8902 #################################################################################
8903 # kiriwrite_variablecheck: Checks the variables for any invalid characters.     #
8904 #                                                                               #
8905 # Usage:                                                                        #
8906 #                                                                               #                       
8907 # kiriwrite_variablecheck(variablename, type, length, noerror);                 #
8908 #                                                                               #
8909 # variablename  Specifies the variable to be checked.                           #
8910 # type          Specifies what type the variable is.                            #
8911 # option        Specifies the maximum/minimum length of the variable            #
8912 #               (if minlength/maxlength is used) or if the filename should be   #
8913 #               checked to see if it is blank.                                  #
8914 # noerror       Specifies if Kiriwrite should return an error or not on         #
8915 #               certain values.                                                 #
8916 #################################################################################
8918         # Get the values that were passed to the subroutine.
8920         my ($variable_data, $variable_type, $variable_option, $variable_noerror) = @_;
8921         
8922         if ($variable_type eq "numbers"){
8923                 
8924                 # Check for numbers and return an error if there is anything else than numebrs.
8925                                 
8926                 my $variable_data_validated = $variable_data;   # Copy the variable_data to variable_data_validated.
8927                 $variable_data_validated =~ tr/0-9//d;          # Take away all of the numbers and from the variable. 
8928                                                                 # If it only contains numbers then it should be blank.
8930                 if ($variable_data_validated eq ""){
8931                         # The validated variable is blank. So continue to the end of this section where the return function should be.
8932                 } else {
8933                         # The variable is not blank, so check if the no error value is set
8934                         # to 1 or not.
8935                         
8936                         if ($variable_noerror eq 1){
8937                         
8938                                 # The validated variable is not blank and the noerror
8939                                 # value is set to 1. So return an value of 1.
8940                                 # (meaning that the data is invalid).
8941                                 
8942                                 return 1;
8943                         
8944                         } elsif ($variable_noerror eq 0) {
8945                         
8946                                 # The validated variable is not blank and the noerror
8947                                 # value is set to 0.
8948                         
8949                                 kiriwrite_error("invalidvariable");
8950                                 
8951                         } else {
8952                         
8953                                 # The variable noerror value is something else
8954                                 # pther than 1 or 0. So return an error.
8955                         
8956                                 kiriwrite_error("invalidvariable");
8957                         
8958                         }
8959                         
8960                 }
8961                 
8962                 return 0;               
8963         
8964         } elsif ($variable_type eq "lettersnumbers"){
8966                 # Check for letters and numbers and return an error if there is anything else other
8967                 # than letters and numbers.
8969                 my $variable_data_validated = $variable_data;   # Copy the variable_data to variable_data_validated
8970                 $variable_data_validated =~ tr/a-zA-Z0-9.//d;
8971                 $variable_data_validated =~ s/\s//g;
8973                 if ($variable_data_validated eq ""){
8974                         # The validated variable is blank. So continue to the end of this section where the return function should be.
8975                 } else {
8976                         # The variable is not blank, so check if the no error value is set
8977                         # to 1 or not.
8979                         if ($variable_noerror eq 1){
8980                         
8981                                 # The validated variable is not blank and the noerror
8982                                 # value is set to 1. So return an value of 1.
8983                                 # (meaning that the data is invalid).
8984                                 
8985                                 return 1;
8986                         
8987                         } elsif ($variable_noerror eq 0) {
8988                         
8989                                 # The validated variable is not blank and the noerror
8990                                 # value is set to 0.
8991                         
8992                                 kiriwrite_error("invalidvariable");
8993                                 
8994                         } else {
8995                         
8996                                 # The variable noerror value is something else
8997                                 # pther than 1 or 0. So return an error.
8998                         
8999                                 kiriwrite_error("invalidvariable");
9000                         
9001                         }
9003                 }
9005                 return 0;
9007         } elsif ($variable_type eq "maxlength"){
9008                 # Check for the length of the variable, return an error if it is longer than the length specified.
9009                 
9010                 # Check if the variable_data string is blank, if it is then set the variable_data_length
9011                 # to '0'.
9012                 
9013                 my $variable_data_length = 0;           
9014                 
9015                 if (!$variable_data){
9016                 
9017                         # Set variable_data_length to '0'.
9018                         $variable_data_length = 0;
9019                 
9020                 } else {
9021                 
9022                         # Get the length of the variable recieved.
9023                         $variable_data_length = length($variable_data); 
9024                         
9025                 }
9029                 if ($variable_data_length > $variable_option){
9031                         # The variable length is longer than it should be so check if
9032                         # the no error value is set 1.
9033                         
9034                         if ($variable_noerror eq 1){
9035                         
9036                                 # The no error value is set to 1, so return an
9037                                 # value of 1 (meaning tha the variable is
9038                                 # too long to be used).
9039                         
9040                                 return 1;       
9041                         
9042                         } elsif ($variable_noerror eq 0){
9043                         
9044                                 # The no error value is set to 0, so return
9045                                 # an error.
9046                         
9047                                 kiriwrite_error("variabletoolong");
9048                         
9049                         } else {
9050                         
9051                                 # The no error value is something else other
9052                                 # than 0 or 1, so return an error.
9053                         
9054                                 kiriwrite_error("variabletoolong");
9055                         
9056                         }
9057                         
9058                 } else {
9059                 
9060                         # The variable length is exactly or shorter than specified, so continue to end of this section where
9061                         # the return function should be.
9062                         
9063                 }
9065                 return 0;
9066                 
9067         } elsif ($variable_type eq "blank"){
9068                 # Check if the variable is blank and if it is blank, then return an error.
9070                 if (!$variable_data){
9072                         # The variable data really is blank, so check what
9073                         # the no error value is set.
9075                         if ($variable_noerror eq 1){
9077                                 # The no error value is set to 1, so return
9078                                 # a value of 1 (saying that the variable was
9079                                 # blank).
9081                                 return 1;
9083                         } elsif ($variable_noerror eq 0){
9085                                 # The no error value is set to 0, so return
9086                                 # an error.
9088                                 kiriwrite_error("blankvariable");
9090                         } else {
9092                                 # The no error value is something else other
9093                                 # than 0 or 1, so return an error.
9095                                 kiriwrite_error("invalidvariable");
9097                         }
9099                 }
9101                 return 0;
9103         } elsif ($variable_type eq "filename"){
9104                 # Check for letters and numbers, if anything else than letters and numbers is there (including spaces) return
9105                 # an error.
9106                 
9107                 # Check if the filename passed is blank, if it is then return with an error.
9108                 
9109                 if ($variable_data eq ""){
9110                 
9111                         # The filename specified is blank, so check what the
9112                         # noerror value is set.
9113                 
9114                         if ($variable_noerror eq 1){
9115                         
9116                                 # The no error value is set to 1 so return
9117                                 # a value of 1 (meaning that the filename
9118                                 # was blank).
9119                         
9120                                 return 1;
9121                         
9122                         } elsif ($variable_noerror eq 0){
9123                         
9124                                 # The no error value is set to 1 so return
9125                                 # an error.
9126                         
9127                                 kiriwrite_error("blankfilename");
9128                         
9129                         } else {
9130                         
9131                                 # The no error value is something else other
9132                                 # than 0 or 1, so return an error.
9133                         
9134                                 kiriwrite_error("invalidvariable");
9135                         
9136                         }
9137                         
9138                 } else {
9139                 
9140                 
9141                 }
9142                 
9143                 my $variable_data_validated = $variable_data;
9144                 $variable_data_validated =~ tr/a-zA-Z0-9\.//d;
9145                 
9146                 # Check if the validated data variable is blank, if it is 
9147                 # then continue to the end of this section where the return 
9148                 # function should be, otherwise return an error.
9149                 
9150                 if ($variable_data_validated eq ""){
9151                         
9152                         # The validated data variable is blank, meaning that 
9153                         # it only contained letters and numbers.
9154                         
9155                 } else {
9156                         
9157                         # The validated data variable is not blank, meaning 
9158                         # that it contains something else, so return an error
9159                         # (or a value).
9160                         
9161                         if ($variable_noerror eq 1){
9162                         
9163                                 # The no error value is set to 1 so return
9164                                 # an value of 2. (meaning that the filename
9165                                 # is invalid).
9166                                 
9168                                 return 2;
9169                         
9170                         } elsif ($variable_noerror eq 0){
9171                         
9172                                 # The no error value is set to 0 so return
9173                                 # an error.
9174                                 
9175                                 kiriwrite_error("invalidfilename");
9176                         
9177                         } else {
9178                         
9179                                 # The no error value is something else other
9180                                 # than 0 or 1 so return an error.
9181                                 
9182                                 kiriwrite_error("invalidvariable");
9183                         
9184                         }
9185                         
9186                 }
9187                 
9188                 return 0;
9189         
9190         } elsif ($variable_type eq "filenameindir"){
9191                 # Check if the filename is in the directory and return an
9192                 # error if it isn't.
9194                 if ($variable_data eq ""){
9195                 
9196                         # The filename specified is blank, so check what the
9197                         # noerror value is set.
9198                 
9199                         if ($variable_noerror eq 1){
9200                         
9201                                 # The no error value is set to 1 so return
9202                                 # a value of 1 (meaning that the filename
9203                                 # was blank).
9204                         
9205                                 return 1;
9206                         
9207                         } elsif ($variable_noerror eq 0){
9208                         
9209                                 # The no error value is set to 1 so return
9210                                 # an error.
9211                         
9212                                 kiriwrite_error("blankfilename");
9213                         
9214                         } else {
9215                         
9216                                 # The no error value is something else other
9217                                 # than 0 or 1, so return an error.
9218                         
9219                                 kiriwrite_error("invalidvariable");
9220                         
9221                         }
9222                         
9223                 } else {
9224                 
9225                 
9226                 }
9228                 # Set the following variables for later on.
9229                 
9230                 my $variable_data_length = 0;
9231                 my $variable_data_char = "";
9232                 my $variable_data_validated = "";
9233                 my $variable_data_seek = 0;
9234                 my $variable_data_directory = "";
9235                 my $variable_data_directorycurrent = "";
9236                 my $variable_data_firstlevel = 1;
9238                 # Get the length of the variable recieved.
9240                 $variable_data_length = length($variable_data);
9242                 # Check if the database filename contains the directory command
9243                 # for up a directory level and if it is, return an error
9244                 # or return with a number.
9245                 
9246                 do {
9247                         
9248                         # Get a character from the filename passed to this subroutine.
9249                 
9250                         $variable_data_char = substr($variable_data, $variable_data_seek, 1);
9251                         
9252                         # Check if the current character is the forward slash character.
9253                         
9254                         if ($variable_data_char eq "/"){
9255                         
9256                                 # Check if the current directory is blank (and on the first level), or if the
9257                                 # current directory contains two dots or one dot, if it does return an error.
9258                         
9259                                 if ($variable_data_directorycurrent eq "" && $variable_data_firstlevel eq 1 || $variable_data_directorycurrent eq ".." || $variable_data_directorycurrent eq "."){
9260                                         
9261                                         # Check if the noerror value is set to 1, if it is return an
9262                                         # number, else return an proper error.
9263                                         
9264                                         if ($variable_noerror eq 1){
9265                                         
9266                                                 # Page filename contains invalid characters and
9267                                                 # the no error value is set to 1 so return a 
9268                                                 # value of 2 (meaning that the page filename
9269                                                 # is invalid).
9270                                         
9271                                                 return 2;
9272                                                 
9273                                         } elsif ($variable_noerror eq 0) {
9274                                         
9275                                                 # Page filename contains invalid characters and
9276                                                 # the no error value is set to 0 so return an
9277                                                 # error.
9278                                         
9279                                                 kiriwrite_error("invalidfilename");
9280                                                 
9281                                         } else {
9282                                         
9283                                                 # The no error value is something else other
9284                                                 # than 0 or 1 so return an error.
9285                                         
9286                                                 kiriwrite_error("invalidvariable");
9287                                                 
9288                                         }
9289                                         
9290                                 }
9291                                 
9292                                 # Append the forward slash, clear the current directory name and set
9293                                 # the first directory level value to 0.
9294                                 
9295                                 $variable_data_directory = $variable_data_directory . $variable_data_char;
9296                                 $variable_data_directorycurrent = "";
9297                                 $variable_data_firstlevel = 0;
9298                         
9299                         } else {
9300                         
9301                                 # Append the current character to the directory name and to the current
9302                                 # directory name.
9303                         
9304                                 $variable_data_directory = $variable_data_directory . $variable_data_char;
9305                                 $variable_data_directorycurrent = $variable_data_directorycurrent . $variable_data_char;
9306                                                 
9307                         }
9308                         
9309                         # Increment the seek counter.
9310                         
9311                         $variable_data_seek++;
9312                                         
9313                 } until ($variable_data_seek eq $variable_data_length);
9315                 return 0;       
9317         } elsif ($variable_type eq "datetime"){
9318                 # Check if the date and time setting format is valid.
9320                 if ($variable_data eq ""){
9322                         if ($variable_noerror eq 1){
9323                         
9324                                 # The no error value is set to 1 so return
9325                                 # a value of 1 (meaning that the date and
9326                                 # time format was blank).
9327                         
9328                                 return 1;
9329                         
9330                         } elsif ($variable_noerror eq 0){
9331                         
9332                                 # The no error value is set to 1 so return
9333                                 # an error.
9334                         
9335                                 kiriwrite_error("blankdatetimeformat");
9336                         
9337                         } else {
9338                         
9339                                 # The no error value is something else other
9340                                 # than 0 or 1, so return an error.
9341                         
9342                                 kiriwrite_error("invalidvariable");
9343                         
9344                         }
9346                 }
9348                 my $variable_data_validated = $variable_data;
9349                 $variable_data_validated =~ tr|dDmMyYhms/():[ ]||d;
9351                 if ($variable_data_validated eq ""){
9353                         # The date and time format is valid. So
9354                         # skip this bit.
9356                 } else {
9358                         # The validated data variable is not blank, meaning 
9359                         # that it contains something else, so return an error
9360                         # (or a value).
9361                         
9362                         if ($variable_noerror eq 1){
9363                         
9364                                 # The no error value is set to 1 so return
9365                                 # an value of 2. (meaning that the date and
9366                                 # time format was invalid).
9367                                 
9368                                 return 2;
9369                         
9370                         } elsif ($variable_noerror eq 0){
9371                         
9372                                 # The no error value is set to 0 so return
9373                                 # an error.
9374                                 
9375                                 kiriwrite_error("invaliddatetimeformat");
9376                         
9377                         } else {
9378                         
9379                                 # The no error value is something else other
9380                                 # than 0 or 1 so return an error.
9381                                 
9382                                 kiriwrite_error("invalidvariable");
9383                         
9384                         }
9386                 }
9388                 return 0;
9390         } elsif ($variable_type eq "directory"){
9391                 # Check if the directory only contains letters and numbers and
9392                 # return an error if anything else appears.
9394                 my $variable_data_validated = $variable_data;
9395                 $variable_data_validated =~ tr/a-zA-Z0-9//d;
9397                 if ($variable_data eq ""){
9399                         if ($variable_noerror eq 1){
9400                         
9401                                 # The no error value is set to 1 so return
9402                                 # a value of 1 (meaning that the directory
9403                                 # name was blank).
9404                         
9405                                 return 1;
9406                         
9407                         } elsif ($variable_noerror eq 0){
9408                         
9409                                 # The no error value is set to 1 so return
9410                                 # an error.
9411                         
9412                                 kiriwrite_error("blankdirectory");
9413                         
9414                         } else {
9415                         
9416                                 # The no error value is something else other
9417                                 # than 0 or 1, so return an error.
9418                         
9419                                 kiriwrite_error("invalidvariable");
9420                         
9421                         }
9423                 }
9425                 if ($variable_data_validated eq ""){
9427                         # The validated data variable is blank, meaning that
9428                         # it only contains letters and numbers.
9430                 } else {
9432                         # The validated data variable is not blank, meaning 
9433                         # that it contains something else, so return an error
9434                         # (or a value).
9435                         
9436                         if ($variable_noerror eq 1){
9437                         
9438                                 # The no error value is set to 1 so return
9439                                 # an value of 2. (meaning that the directory
9440                                 # name is invalid).
9441                                 
9442                                 return 2;
9443                         
9444                         } elsif ($variable_noerror eq 0){
9445                         
9446                                 # The no error value is set to 0 so return
9447                                 # an error.
9448                                 
9449                                 kiriwrite_error("invaliddirectory");
9450                         
9451                         } else {
9452                         
9453                                 # The no error value is something else other
9454                                 # than 0 or 1 so return an error.
9455                                 
9456                                 kiriwrite_error("invalidvariable");
9457                         
9458                         }
9460                 }
9462                 return 0;
9464         } elsif ($variable_type eq "language_filename"){
9465         
9466                 # The variable type is a language filename type.
9467                 # Check if the language file name is blank and 
9468                 # if it is then return an error (or value).
9469                 
9470                 if ($variable_data eq ""){
9471                 
9472                         # The language filename is blank so check the
9473                         # no error value and return an error (or value).
9474                 
9475                         if ($variable_noerror eq 1){
9476                                 
9477                                 # Language filename is blank and the no error value
9478                                 # is set as 1, so return a value of 1 (saying that
9479                                 # the language filename is blank).
9480                                 
9481                                 return 1;
9482                         
9483                         } elsif ($variable_noerror eq 0) {
9484                         
9485                                 # Language filename is blank and the no error value
9486                                 # is not set as 1, so return an error.
9487                                 
9488                                 kiriwrite_error("emptylanguagefilename");
9489                         
9490                         } else {
9491                         
9492                                 # The noerror value is something else other
9493                                 # than 0 or 1 so return an error.
9494                                 
9495                                 kiriwrite_error("invalidvariable");
9496                         
9497                         }
9498                 
9499                 }
9500                 
9501                 # Set the following variables for later on.
9502                 
9503                 my $variable_data_length = 0;
9504                 my $variable_data_char = "";
9505                 my $variable_data_seek = 0;
9506                 
9507                 # Get the length of the language file name.
9508                 
9509                 $variable_data_length = length($variable_data);
9510                 
9511                 do {
9512                 
9513                         # Get a character from the language filename passed to this 
9514                         # subroutine and the character the seek counter value is set
9515                         # to.
9516                 
9517                         $variable_data_char = substr($variable_data, $variable_data_seek, 1);
9518                         
9519                         # Check if the language filename contains a forward slash or a dot, 
9520                         # if the selected character is a forward slash then return an error
9521                         # (or value).
9522                         
9523                         if ($variable_data_char eq "/" || $variable_data_char eq "."){
9524                         
9525                                 # The language filename contains a forward slash or
9526                                 # a dot so depending on the no error value, return
9527                                 # an error or a value.
9528                         
9529                                 if ($variable_noerror eq 1){
9530                                         
9531                                         # Language filename contains a forward slash or a dot
9532                                         # and the no error value has been set to 1, so return 
9533                                         # an value of 2 (saying that the language file name is 
9534                                         # invalid).
9535                                         
9536                                         return 2;
9537                                 
9538                                 } elsif ($variable_noerror eq 0) {
9539                                 
9540                                         # Language filename contains a forward slash and the no
9541                                         # error value has not been set to 1, so return an error.
9542                                 
9543                                         kiriwrite_error("invalidlanguagefilename");
9544                                         
9545                                 } else {
9546                                 
9547                                         # The noerror value is something else other than
9548                                         # 1 or 0 so return an error.
9549                                         
9550                                         kiriwrite_error("invalidvariable");
9551                                 
9552                                 }
9553                         
9554                         }
9555                         
9556                         # Increment the seek counter.
9558                         $variable_data_seek++;
9559                 
9560                 } until ($variable_data_seek eq $variable_data_length);
9561                 
9562                 return 0;
9563                                         
9564         } elsif ($variable_type eq "pagesetting"){
9566                 # The variable type is a page setting, so check if the page
9567                 # setting has one of the valid options.
9569                 if ($variable_data eq 0 || $variable_data eq 1 || $variable_data eq 2 || $variable_data eq 3){
9571                         # The variable is one of the options above, so continue
9572                         # to the end of this section.   
9574                 } else {
9576                         # The variable is not one of the options above, so check
9577                         # and see if a error or a value should be returned.
9579                         if ($variable_noerror eq 1){
9580                         
9581                                 # The page setting is invalid and the no error
9582                                 # value is set 1, so return a value of 1
9583                                 # (saying that the page setting value is
9584                                 # invalid).
9585                                 
9586                                 return 1;
9587                                 
9588                         } elsif ($variable_noerror eq 0) {
9589                                 
9590                                 # Page setting is invalid and the no error value
9591                                 # is not 1, so return an error.
9592                         
9593                                 kiriwrite_error("invalidvariable");
9594                                 
9595                         } else {
9596                         
9597                                 # The no error value is something else other
9598                                 # than 0 or 1 so return an error.
9599                         
9600                                 kiriwrite_error("invalidvariable");
9601                         
9602                         }
9604                 }
9606                 return 0;
9608         } elsif ($variable_type eq "page_filename"){
9609          
9610                 # The variable type is a page filename type. Check
9611                 # if the data is empty and if it is then return an
9612                 # error (or value).
9613         
9614                 if ($variable_data eq ""){
9615                 
9616                         # The filename is blank so check the no error
9617                         # value and depending on it return an value
9618                         # or an error.
9619                 
9620                         if ($variable_noerror eq 1){
9621                         
9622                                 # Page filename is blank and the no error value
9623                                 # is set as 1, so return a value of 1 (saying
9624                                 # the filename is blank).
9625                                 
9626                                 return 1;
9627                                 
9628                         } elsif ($variable_noerror eq 0) {
9629                                 
9630                                 # Page filename is blank and the no error value
9631                                 # is not 1, so return an error.
9632                         
9633                                 kiriwrite_error("emptypagefilename");
9634                                 
9635                         } else {
9636                         
9637                                 # The no error value is something else other
9638                                 # than 0 or 1 so return an error.
9639                         
9640                                 kiriwrite_error("invalidvariable");
9641                         
9642                         }
9643                 }
9644                 
9645                 # Set the following variables for later on.
9646                 
9647                 my $variable_data_length = 0;
9648                 my $variable_data_char = "";
9649                 my $variable_data_validated = "";
9650                 my $variable_data_seek = 0;
9651                 my $variable_data_directory = "";
9652                 my $variable_data_directorycurrent = "";
9653                 my $variable_data_firstlevel = 1;
9654                 
9655                 # Get the length of the filename.
9656                 
9657                 $variable_data_length = length($variable_data);
9658                 
9659                 # Check that only valid characters should be appearing in
9660                 # the filename.
9662                 $variable_data_validated = $variable_data;
9663                 $variable_data_validated =~ tr|a-zA-Z0-9\.\/||d;
9665                 if ($variable_data_validated ne ""){
9667                         # The validated variable is not blank, meaning the
9668                         # variable contains invalid characters, so return
9669                         # an error.
9671                         kiriwrite_error("invalidfilename");
9673                 }
9675                 # Check if the page filename contains the directory command
9676                 # for up a directory level and if it is, return an error
9677                 # or return with a number.
9678                 
9679                 do {
9680                         
9681                         # Get a character from the filename passed to this subroutine.
9682                 
9683                         $variable_data_char = substr($variable_data, $variable_data_seek, 1);
9684                         
9685                         # Check if the current character is the forward slash character.
9686                         
9687                         if ($variable_data_char eq "/"){
9688                         
9689                                 # Check if the current directory is blank (and on the first level), or if the
9690                                 # current directory contains two dots or one dot, if it does return an error.
9691                         
9692                                 if ($variable_data_directorycurrent eq "" && $variable_data_firstlevel eq 1 || $variable_data_directorycurrent eq ".." || $variable_data_directorycurrent eq "."){
9693                                         
9694                                         # Check if the noerror value is set to 1, if it is return an
9695                                         # number, else return an proper error.
9696                                         
9697                                         if ($variable_noerror eq 1){
9698                                         
9699                                                 # Page filename contains invalid characters and
9700                                                 # the no error value is set to 1 so return a 
9701                                                 # value of 2 (meaning that the page filename
9702                                                 # is invalid).
9703                                         
9704                                                 return 2;
9705                                                 
9706                                         } elsif ($variable_noerror eq 0) {
9707                                         
9708                                                 # Page filename contains invalid characters and
9709                                                 # the no error value is set to 0 so return an
9710                                                 # error.
9711                                         
9712                                                 kiriwrite_error("invalidfilename");
9713                                                 
9714                                         } else {
9715                                         
9716                                                 # The no error value is something else other
9717                                                 # than 0 or 1 so return an error.
9718                                         
9719                                                 kiriwrite_error("invalidvariable");
9720                                                 
9721                                         }
9722                                         
9723                                 }
9724                                 
9725                                 # Append the forward slash, clear the current directory name and set
9726                                 # the first directory level value to 0.
9727                                 
9728                                 $variable_data_directory = $variable_data_directory . $variable_data_char;
9729                                 $variable_data_directorycurrent = "";
9730                                 $variable_data_firstlevel = 0;
9731                         
9732                         } else {
9733                         
9734                                 # Append the current character to the directory name and to the current
9735                                 # directory name.
9736                         
9737                                 $variable_data_directory = $variable_data_directory . $variable_data_char;
9738                                 $variable_data_directorycurrent = $variable_data_directorycurrent . $variable_data_char;
9739                                                 
9740                         }
9741                         
9742                         # Increment the seek counter.
9743                         
9744                         $variable_data_seek++;
9745                                         
9746                 } until ($variable_data_seek eq $variable_data_length);
9747                 
9748                 return 0;
9749         
9750         } elsif ($variable_type eq "outputmodule"){
9751                 
9752                 # The variable type is a output module filename.
9754                 # Check if the variable_data is blank and if it is
9755                 # return an error.
9757                 if ($variable_data eq ""){
9759                         # The output module is blank so check if an error
9760                         # value should be returned or a number should be
9761                         # returned.
9763                 } else {
9765                 }
9767                 my $variable_data_validated = $variable_data;
9768                 $variable_data_validated =~ tr/a-zA-Z0-9//d;
9770                 if ($variable_data_validated eq ""){
9772                 } else {
9774                         kiriwrite_error("outputmoduleinvalid");
9776                 }
9778                 return 0;
9780         } elsif ($variable_type eq "utf8"){
9782                 # The variable type is a UTF8 string.
9784                 # Check if the string is a valid UTF8 string.
9786                 if ($variable_data =~ m/^(
9787                         [\x09\x0A\x0D\x20-\x7E]              # ASCII
9788                         | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
9789                         |  \xE0[\xA0-\xBF][\x80-\xBF]        # excluding overlongs
9790                         | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
9791                         |  \xED[\x80-\x9F][\x80-\xBF]        # excluding surrogates
9792                         |  \xF0[\x90-\xBF][\x80-\xBF]{2}     # planes 1-3
9793                         | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
9794                         |  \xF4[\x80-\x8F][\x80-\xBF]{2}     # plane 16
9795                 )*$/x){
9797                         # The UTF-8 string is valid.
9799                 } else {
9801                         # The UTF-8 string is not valid, check if the no error
9802                         # value is set to 1 and return an error if it isn't.
9804                         if ($variable_noerror eq 1){
9806                                 # The no error value has been set to 1, so return
9807                                 # a value of 1 (meaning that the UTF-8 string is
9808                                 # invalid).
9810                                 return 1; 
9812                         } elsif ($variable_noerror eq 0) {
9814                                 # The no error value has been set to 0, so return
9815                                 # an error.
9817                                 kiriwrite_error("invalidutf8");
9818                         
9819                         } else {
9821                                 # The no error value is something else other than 0
9822                                 # or 1, so return an error.
9824                                 kiriwrite_error("invalidoption");
9826                         }
9828                 }
9830                 return 0;
9832         } else {
9833                 # Another type than the valid ones above has been specified so return an error specifying an invalid option.
9834                 kiriwrite_error("invalidoption");
9835         }
9839 sub kiriwrite_convert{
9840 #################################################################################
9841 # kiriwrite_convert: Converts certain characters.                               #
9842 #                                                                               #
9843 # Usage;                                                                        #
9844 #                                                                               #
9845 # kiriwrite_convert(data, type);                                                #
9846 #                                                                               #
9847 # data          Specifies the data to be used with                              #
9848 # type          The type of conversion to make (convert from or to kiriwrite    #
9849 #               values.                                                         #
9850 #################################################################################
9852         # Get the data that was passed to the subroutine.
9853         my ($data, $type) = @_;
9855         if ($type eq "kiriwrite"){
9857                 # Convert to into the Kiriwrite format.
9859                 $data =~ s/'/''/g;
9860                 $data =~ s/\b//g;
9862         } elsif ($type eq "normal_display"){
9864                 # Convert into a viewable format.
9866                 $data =~ s/&/&amp;/g;
9867                 $data =~ s/\"/&quot;/g;
9868                 $data =~ s/'/'/g;
9869                 $data =~ s/>/&gt;/g;
9870                 $data =~ s/</&lt;/g;
9871                 $data =~ s/\0//g;
9872                 $data =~ s/\b//g;
9874         } elsif ($type eq "normal_out"){
9876                 # Convert into the original format.
9878                 $data =~ s/\0//g;
9879                 $data =~ s/\b//g;
9881         } elsif ($type eq "date"){
9883                 # Convert the date given into the proper date.
9885                 # Create the following varialbes to be used later.
9887                 my $date;
9888                 my $time;
9889                 my $day;
9890                 my $day_full;
9891                 my $month;
9892                 my $month_check;
9893                 my $month_full;
9894                 my $year;
9895                 my $year_short;
9896                 my $hour;
9897                 my $hour_full;
9898                 my $minute;
9899                 my $minute_full;
9900                 my $second;
9901                 my $second_full;
9902                 my $seek = 0;
9903                 my $timelength;
9904                 my $datelength;
9905                 my $daylength;
9906                 my $secondlength;
9907                 my $startchar = 0;
9908                 my $char;
9909                 my $length;
9910                 my $count = 0;
9912                 # Split the date and time.
9914                 $length = length($data);
9916                 if ($length > 0){
9918                         do {
9920                                 # Get the character and check if it is a space.
9922                                 $char = substr($data, $seek, 1);
9924                                 if ($char eq ' '){
9926                                         # The character is a space, so get the date and time.
9928                                         $date           = substr($data, 0, $seek);
9929                                         $timelength     = $length - $seek - 1;
9930                                         $time           = substr($data, $seek + 1, $timelength);
9932                                 }
9934                                 $seek++;
9936                         } until ($seek eq $length);
9938                         # Get the year, month and date.
9940                         $length = length($date);
9941                         $seek = 0;
9943                         do {
9945                                 # Get the character and check if it is a dash.
9947                                 $char = substr($date, $seek, 1);
9949                                 if ($char eq '-'){
9951                                         # The character is a dash, so get the year, month or day.
9953                                         $datelength = $seek - $startchar;
9955                                         if ($count eq 0){
9957                                                 # Get the year from the date.
9959                                                 $year           = substr($date, 0, $datelength) + 1900;
9960                                                 $startchar      = $seek;
9961                                                 $count = 1;
9963                                                 # Get the last two characters to get the short year
9964                                                 # version.
9966                                                 $year_short     = substr($year, 2, 2);
9968                                         } elsif ($count eq 1){
9970                                                 # Get the month and day from the date.
9972                                                 $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
9974                                                 # Check if the month is less then 10, if it is
9975                                                 # add a zero to the value.
9977                                                 if ($month < 10){
9979                                                         $month_full = '0' . $month;
9981                                                 } else {
9983                                                         $month_full = $month;
9985                                                 }
9987                                                 $startchar      = $seek;
9988                                                 $count = 2;
9990                                                 $daylength      = $length - $seek + 1;
9991                                                 $day            = substr($date, $startchar + 1, $daylength);
9993                                                 # Check if the day is less than 10, if it is
9994                                                 # add a zero to the value.
9996                                                 if ($day < 10){
9998                                                         $day_full       = '0' . $day;
10000                                                 } else {
10002                                                         $day_full       = $day;
10004                                                 }
10006                                         }
10008                                 }
10010                                 $seek++;
10012                         } until ($seek eq $length);
10014                         # Get the length of the time value and reset certain
10015                         # values to 0.
10017                         $length = length($time);
10018                         $seek = 0;
10019                         $count = 0;
10020                         $startchar = 0;
10022                         do {
10024                                 # Get the character and check if it is a colon.
10026                                 $char = substr($time, $seek, 1);
10028                                 if ($char eq ':'){
10030                                         # The character is a colon, so get the hour, minute and day.
10032                                         $timelength = $seek - $startchar;
10034                                         if ($count eq 0){
10036                                                 # Get the hour from the time.
10038                                                 $hour = substr($time, 0, $timelength);
10039                                                 $count = 1;
10040                                                 $startchar = $seek;
10042                                                 # If the hour is less than ten then add a
10043                                                 # zero.
10045                                                 if ($hour < 10){
10047                                                         $hour_full = '0' . $hour;
10049                                                 } else {
10051                                                         $hour_full = $hour;
10053                                                 }
10055                                         } elsif ($count eq 1){
10057                                                 # Get the minute and second from the time.
10059                                                 $minute = substr($time, $startchar + 1, $timelength - 1);
10060                                                 $count = 2;
10061                                                 
10062                                                 # If the minute is less than ten then add a
10063                                                 # zero.
10065                                                 if ($minute < 10){
10067                                                         $minute_full = '0' . $minute;
10069                                                 } else {
10071                                                         $minute_full = $minute;
10073                                                 }
10075                                                 $startchar = $seek;
10077                                                 $secondlength = $length - $seek + 1;
10078                                                 $second = substr($time, $startchar + 1, $secondlength);
10079                                                 
10080                                                 # If the second is less than ten then add a
10081                                                 # zero.
10083                                                 if ($second < 10){
10085                                                         $second_full = '0' . $second;
10087                                                 } else {
10089                                                         $second_full = $second;
10091                                                 }
10093                                         }
10095                                 }
10097                                 $seek++;
10099                         } until ($seek eq $length);
10101                         # Get the setting for displaying the date and time.
10103                         $data = $kiriwrite_config{"system_datetime"};
10105                         # Process the setting for displaying the date and time
10106                         # using regular expressions
10108                         $data =~ s/DD/$day_full/g;
10109                         $data =~ s/D/$day/g;
10110                         $data =~ s/MM/$month_full/g;
10111                         $data =~ s/M/$month/g;
10112                         $data =~ s/YY/$year/g;
10113                         $data =~ s/Y/$year_short/g;
10115                         $data =~ s/hh/$hour_full/g;
10116                         $data =~ s/h/$hour/g;
10117                         $data =~ s/mm/$minute_full/g;
10118                         $data =~ s/m/$minute/g;
10119                         $data =~ s/ss/$second_full/g;
10120                         $data =~ s/s/$second/g;
10122                 }
10124         } else {
10125                 kiriwrite_error("invalidoption");
10126         }
10128         return $data;
10132 sub kiriwrite_output_header{
10133 #################################################################################
10134 # kiriwrite_output_header: Outputs the header to the browser/stdout/console.    #
10135 #                                                                               #
10136 # Usage:                                                                        #
10137 #                                                                               #
10138 # kiriwrite_output_header(mimetype);                                            #
10139 #                                                                               #
10140 # mimetype      Specifies the mime type of the header.                          #
10141 #################################################################################
10143         # Print a header saying that the page expires immediately since the
10144         # date is set in the past.
10146         #print "Expires: Sunday, 01-Jan-06 00:00:00 GMT\n";
10147         #print "Content-Type: text/html; charset=utf-8\n";
10148         #print header();
10149         print header(-Expires=>'Sunday, 01-Jan-06 00:00:00 GMT', -charset=>'utf-8');    
10150         return;
10153 sub kiriwrite_processfilename{
10154 #################################################################################
10155 # kiriwrite_processfilename: Processes a name and turns it into a filename that #
10156 # can be used by Kiriwrite.                                                     #
10157 #                                                                               #
10158 # Usage:                                                                        #
10159 #                                                                               #
10160 # kiriwrite_processfilename(text);                                              #
10161 #                                                                               #
10162 # text          Specifies the text to be used in the process for creating a new #
10163 #               filename.                                                       #
10164 #################################################################################
10166         # Get the values that have been passed to the subroutine.
10167         
10168         my ($process_text) = @_;
10169         
10170         # Define some variables that will be used later on.
10171         
10172         my $processed_stageone  = "";
10173         my $processed_stagetwo  = "";
10174         my $processed_length    = "";
10175         my $processed_char      = "";
10176         my $processed_seek      = 0;
10177         my $processed_filename  = "";
10178         
10179         # Set the first stage value of the processed filename to the
10180         # process filename and then filter it out to only contain
10181         # numbers and letters (no spaces) and then convert the
10182         # capitals to small letters.
10183         
10184         $processed_stageone = $process_text;
10185         $processed_stageone =~ tr#a-zA-Z0-9##cd;
10186         $processed_stageone =~ tr/A-Z/a-z/;
10187         
10188         # Now set the second stage value of the processed filename
10189         # to the first stage value of the processed filename and
10190         # then limit the filename down to 64 characters.
10191         
10192         $processed_stagetwo = $processed_stageone;
10193         $processed_length = length($processed_stagetwo);
10194         
10195         # Process the second stage filename into the final 
10196         # filename and do so until the seek counter is 64
10197         # or reaches the length of the second stage filename.
10198         
10199         do {
10200         
10201                 # Get the character that is the seek counter
10202                 # is set at.
10203         
10204                 $processed_char = substr($processed_stagetwo, $processed_seek, 1);
10205                 
10206                 # Append to the final processed filename.
10207                 
10208                 $processed_filename = $processed_filename . $processed_char;
10209                 
10210                 # Increment the seek counter.
10211                 
10212                 $processed_seek++;
10213         
10214         } until ($processed_seek eq 64 || $processed_seek eq $processed_length);
10215                 
10216         return $processed_filename;
10220 sub kiriwrite_error{
10221 #################################################################################
10222 # kiriwrite_error: Prints out an error message.                                 #
10223 #                                                                               #
10224 # Usage:                                                                        #
10225 #                                                                               #
10226 # kiriwrite_error(errortype);                                                   #
10227 #                                                                               #
10228 # errortype     Specifies the type of error that occured.                       #
10229 #################################################################################
10231         # Get the error type from the subroutine.
10233         my ($error_type) = @_;
10234         
10235         # Load the list of error messages.
10237         my ($kiriwrite_error, %kiriwrite_error);
10239         %kiriwrite_error = (
10240                 # Catch all error message.
10241                 "generic"                               => "An error has occured but not an error that is known to Kiriwrite.",
10243                 # Standard error messages.
10244                 "blankfilename"                         => "The filename specified was blank.",
10245                 "blankvariable"                         => "A blank variable was specified.",
10246                 "fileexists"                            => "A filename specified already exists.",
10247                 "internalerror"                         => "An internal error has occured within Kiriwrite.",
10248                 "invalidoption"                         => "An invalid option was given.",
10249                 "invalidaction"                         => "An invalid action was specified.",
10250                 "invalidfilename"                       => "The filename given contains invalid characters.",
10251                 "invalidmode"                           => "An invalid mode was specified.",
10252                 "invalidutf8"                           => "A UTF-8 string is invalid.",
10253                 "invalidvariable"                       => "An variable with invalid data has been found.",
10254                 "variabletoolong"                       => "A variable given is too long.",
10256                 # Specific error messages.
10257                 "blankcompiletype"                      => "The compile type specified is blank",
10258                 "blankdatabasepageadd"                  => "No database was specified when trying to add a page.",
10259                 "blankdirectory"                        => "The directory name specified was blank.",
10260                 "blankfindfilter"                       => "The find filter was blank.",
10261                 "blankdatetimeformat"                   => "The date and time format given is blank.",
10262                 "databasecategoriestoolong"             => "The database categories list is too long.",
10263                 "databasecopysame"                      => "The database that the pages are being copied to is the same database that the pages are copied from.",
10264                 "datadirectoryinvalidpermissions"       => "The database directory has invalid permission settings.",
10265                 "databasedescriptiontoolong"            => "The database description is too long.",
10266                 "databasefilenameinvalid"               => "The database filename is invalid.",
10267                 "databasefilenametoolong"               => "The database filename is too long.",
10268                 "databasefileinvalid"                   => "The database file is in an invalid format.",
10269                 "databaseinvalidpermissions"            => "The database has invalid permission settings.",
10270                 "databasenameinvalid"                   => "The database name contains invalid characters.",
10271                 "databasenametoolong"                   => "The database name is too long.",
10272                 "databasenameblank"                     => "The database name is blank.",
10273                 "databasemissingfile"                   => "The database file is missing.",
10274                 "databasemovemissingfile"               => "The database that the pages are moving to is missing.",
10275                 "databasemovesame"                      => "The database that the pages are being moved to is the same database that the pages are moving from.",
10276                 "dbdirectoryblank"                      => "The database directory name given was blank.",
10277                 "dbdirectoryinvalid"                    => "The database directory name given was invalid.",
10278                 "filtersdatabasefilenotcreated"         => "The filters database was not created because of the invalid permissions set for directory where Kiriwrite is being run from.",
10279                 "filtersdbinvalidformat"                => "The filters database is an invalid format.",
10280                 "filtersdbpermissions"                  => "The filters database has invalid permission settings.",
10281                 "filtersdbmissing"                      => "The filters database is missing.",
10282                 "filteridblank"                         => "The filter identification number given is blank.",
10283                 "filterdoesnotexist"                    => "The filter with the identification number given does not exist.",
10284                 "filteridinvalid"                       => "The filter identification number given is invalid.",
10285                 "filteridtoolong"                       => "The filter identification number given is too long.",
10286                 "findfiltertoolong"                     => "The find filter given is too long.",
10287                 "filterpriorityinvalid"                 => "The filter priority number given is invalid.",
10288                 "filterpriorityinvalidchars"            => "The filter priority given contains invalid characters.",
10289                 "filterprioritytoolong"                 => "The filter priority given is too long.",
10290                 "invalidcompiletype"                    => "The compile type given is invalid.",
10291                 "invalidpagenumber"                     => "The page number specified is invalid.",
10292                 "nopagesselected"                       => "No pages were selected.",
10293                 "invaliddirectory"                      => "The directory name specified was invalid.",
10294                 "invaliddatetimeformat"                 => "The date and time format given is invalid.",
10295                 "invalidlanguagefilename"               => "An invalid language filename was given.",
10296                 "newcopydatabasedoesnotexist"           => "The database that the selected pages are being copied to does not exist.",
10297                 "newcopydatabasefileinvalidpermissions" => "The database that the selected pages are being copied has invalid permissions set.",
10298                 "newcopydatabasefileinvalid"            => "The database that the selected pages are being copied to is in an invalid format.",
10299                 "newmovedatabasedoesnotexist"           => "The database that the selected pages are moving to does not exist.",
10300                 "newmovedatabasefileinvalidpermissions" => "The database that the selected pages are moving to has invalid permissions set.",
10301                 "newmovedatabasefileinvalid"            => "The database that the selected pages are moving to is in an invalid format.",
10302                 "nodatabasesavailable"                  => "No databases are available for compiling.",
10303                 "nodatabaseselected"                    => "No databases were selected for compiling.",
10304                 "noeditvaluesselected"                  => "No values will be changed on the selected pages as no values for changing were selected.",
10305                 "oldcopydatabasedoesnotexist"           => "The database that the selected pages are being copied to does not exist.",
10306                 "oldcopydatabasefileinvalidpermissions" => "The database that the selected pages are being copied to has invalid permissions set.",
10307                 "oldcopydatabasefileinvalid"            => "The database that the selected pages are being copied to is in an invalid format.",
10308                 "oldmovedatabasedoesnotexist"           => "The database that the selected pages are moving from does not exist.",
10309                 "oldmovedatabasefileinvalidpermissions" => "The database that the selected pages are moving from has invalid permissions set.",
10310                 "oldmovedatabasefileinvalid"            => "The database that the selected pages are moving from is in an invalid format.",
10311                 "outputdirectoryblank"                  => "The output directory name given was blank.",
10312                 "outputdirectoryinvalid"                => "The output directory name given was invalid.",
10313                 "outputdirectorymissing"                => "The output directory is missing",
10314                 "outputdirectoryinvalidpermissions"     => "The output directory has invalid permissions set.",
10315                 "outputmoduleblank"                     => "The output module name given is blank.",
10316                 "outputmoduleinvalid"                   => "The output module name given is invalid.",
10317                 "pagefilenamedoesnotexist"              => "The page with the filename given does not exist.",
10318                 "pagefilenameexists"                    => "The page filename given already exists",
10319                 "pagefilenameinvalid"                   => "The page filename given is invalid.",
10320                 "pagefilenametoolong"                   => "The page filename given is too long.",
10321                 "pagefilenameblank"                     => "The page filename given is blank.",
10322                 "pagetitletoolong"                      => "The page title given is too long.",
10323                 "pagedescriptiontoolong"                => "The page description given is too long.",
10324                 "pagesectiontoolong"                    => "The page section given is too long.",
10325                 "pagedatabasefilenametoolong"           => "The page database filename given is too long.",
10326                 "pagesettingstoolong"                   => "The page settings given is too long.",
10327                 "pagesettingsinvalid"                   => "The page settings given are invalid.",
10328                 "pagetemplatefilenametoolong"           => "The page template filename given was too long.",
10329                 "replacefiltertoolong"                  => "The replace filter given is too long",
10330                 "templatenameblank"                     => "The template name given is blank.",
10331                 "templatefilenameexists"                => "A template with the given filename already exists.",
10332                 "templatefilenameinvalid"               => "The template filename given is invalid.",
10333                 "templatedatabaseinvalidpermissions"    => "The template database has invalid permissions.",
10334                 "templatedatabaseinvalidformat"         => "The template database is in a invalid format.",
10335                 "templatedirectoryblank"                => "The template directory name given was blank.",
10336                 "templatedirectoryinvalid"              => "The template directory name given was invalid.",
10337                 "templatedatabasefilenotcreated"        => "The template database was not created because of the invalid permissions set for the directory where Kiriwrite is being run from.",
10338                 "templatefilenametoolong"               => "The template filename given is too long",
10339                 "templatenametoolong"                   => "The template name given is too long",
10340                 "templatedescriptiontoolong"            => "The template description given is too long",
10341                 "templatedatabasemissing"               => "The template database is missing.",
10342                 "templatedoesnotexist"                  => "The template filename given does not exist in the templates database.",
10343                 "templatefilenameblank"                 => "The template filename given was blank.",
10344         );
10346         # Check if the specified error is blank and if it is
10347         # use the generic error messsage.
10349         if (!$kiriwrite_error{$error_type}){
10350                 $error_type = "generic";
10351         }
10353         my $pagedata = "<div class=\"errorbox\"><font class=\"errorheader\">Error!</font><br /><font class=\"errortext\">" . $kiriwrite_error{$error_type} . "</font></div>";
10354         
10355         kiriwrite_output_header;
10356         kiriwrite_output_page("Error!", $pagedata, "none");
10357                 
10358         exit;
10362 sub kiriwrite_get_templates{
10363 #################################################################################
10364 # kiriwrite_get_templates: Get the list of templates available (and valid) from #
10365 # the template directory.                                                       #
10366 #                                                                               #
10367 # Usage:                                                                        #
10368 #                                                                               #
10369 # kiriwrite_get_templates();                                                    #
10370 #################################################################################
10372         # Open the template folder and get the list of database configuration files.
10374         opendir(TEMPLATEDIR, $kiriwrite_config{"directory_data_template"});
10375         my @templatedir = grep /m*\.xml/, readdir(TEMPLATEDIR);
10376         closedir(TEMPLATEDIR);
10378         # Get the actual filename of the template by removing the .xml part from
10379         # the filename.
10381         my $templatedir_filename                = "";
10382         my $templatedir_filename_originallength = "";
10383         my $templatedir_filename_char           = "";
10384         my $templatedir_filename_processed      = "";
10385         my $templatedir_filename_seek           = 0;
10386         my $templatedir_filename_length         = 0;
10387         my $templatedir_filename_finallength    = 0;
10388         my @templatelist_processed;
10389         my $templatedir_processed_filename      = "";
10390         my @templatelist_final;
10391         my $templatelist_final_count            = 0;
10393         foreach $templatedir_filename (@templatedir){
10395                 # Get the length of the original filename.
10397                 $templatedir_filename_length            = length($templatedir_filename);
10398                 $templatedir_filename_finallength       = $templatedir_filename_length - 4;
10400                 # Get the filename minus the last four characters and put the processed 
10401                 # filename into the array of processed filenames.
10403                 $templatedir_filename_processed         = substr($templatedir_filename, 0, $templatedir_filename_finallength);
10405                 $templatelist_processed[$templatedir_filename_seek] = $templatedir_filename_processed;
10407                 # Increment the counter.
10409                 $templatedir_filename_seek++;
10411                 # Clear certain variables before using them again.
10413                 $templatedir_filename_processed         = "";
10414                 $templatedir_filename_length            = 0;
10415                 $templatedir_filename_finallength       = 0;
10417         }
10419         # Check that each template really does exist and put each valid
10420         # template into the final list.
10422         foreach $templatedir_processed_filename (@templatelist_processed){
10424                 if (-e $kiriwrite_config{"directory_data_template"} . '/' . $templatedir_processed_filename){
10426                         # The template does exist, so add the template to the list of valid templates.
10428                         $templatelist_final[$templatelist_final_count] = $templatedir_processed_filename;
10429                         $templatelist_final_count++;
10431                 }
10433         }
10435         # Return the list of valid templates.
10437         return @templatelist_final;
10441 sub kiriwrite_fileexists{
10442 #################################################################################
10443 # kiriwrite_fileexists: Check if a file exists and returns a value depending on #
10444 # if the file exists or not.                                                    #
10445 #                                                                               # 
10446 # Usage:                                                                        #
10447 #                                                                               #
10448 # kiriwrite_fileexists(filename);                                               #
10449 #                                                                               #
10450 # filename      Specifies the file name to check if it exists or not.           #
10451 #################################################################################
10452         
10453         # Get the value that was passed to the subroutine.
10455         my ($filename) = @_;
10457         # Check if the filename exists, if it does, return a value of 1, else
10458         # return a value of 0, meaning that the file was not found.
10460         if (-e $filename){
10462                 # Specified file does exist so return a value of 1.
10464                 return 0;
10466         } else {
10468                 # Specified file does not exist so return a value of 0.
10470                 return 1;
10472         }       
10476 sub kiriwrite_filepermissions{
10477 #################################################################################
10478 # kiriwrite_filepermissions: Check if the file permissions of a file and return #
10479 # either a 1 saying that the permissions are valid or return a 0 saying that    #
10480 # the permissions are invalid.                                                  #
10481 #                                                                               #
10482 # Usage:                                                                        #
10483 #                                                                               #
10484 # kiriwrite_filepermissions(filename, [read], [write], [filemissingskip]);      #
10485 #                                                                               #
10486 # filename              Specifies the filename to check for permissions.        #
10487 # read                  Preform check that the file is readable.                #
10488 # write                 Preform check that the file is writeable.               #
10489 # filemissingskip       Skip the check of seeing if it can read or write if the #
10490 #                       file is missing.                                        #
10491 #################################################################################
10493         # Get the values that was passed to the subroutine.
10495         my ($filename, $readpermission, $writepermission, $ignorechecks) = @_;
10497         # Check to make sure that the read permission and write permission values
10498         # are only 1 character long.
10500         kiriwrite_variablecheck($readpermission, "maxlength", 1, 0);
10501         kiriwrite_variablecheck($writepermission, "maxlength", 1, 0);
10502         kiriwrite_variablecheck($ignorechecks, "maxlength", 1, 0);
10504         my $ignorechecks_result = 0;
10506         # Check if the file should be ignored for read and write checking if 
10507         # it doesn't exist.
10509         if ($ignorechecks){
10511                 if (-e $filename){
10513                         # The file exists so the checks are to be done.
10515                         $ignorechecks_result = 0;
10517                 } else {
10519                         # The file does not exist so the checks don't need to
10520                         # be done to prevent false positives.
10522                         $ignorechecks_result = 1;
10524                 }
10525         
10526         } else {
10528                 $ignorechecks_result = 0;
10530         }
10532         # Check if the file should be checked to see if it can be read.
10534         if ($readpermission && $ignorechecks_result eq 0){
10536                 # The file should be checked to see if it does contain read permissions
10537                 # and return a 0 if it is invalid.
10539                 if (-r $filename){
10541                         # The file is readable, so do nothing.
10543                 } else {
10545                         # The file is not readable, so return 1.
10547                         return 1;
10549                 }
10551         }
10553         # Check if the file should be checked to see if it can be written.
10555         if ($writepermission && $ignorechecks_result eq 0){
10557                 # The file should be checked to see if it does contain write permissions
10558                 # and return a 0 if it is invalid.
10560                 if (-w $filename){
10562                         # The file is writeable, so do nothing.
10564                 } else {
10566                         # The file is not writeable, so return 1.
10568                         return 1;
10570                 }
10572         }
10574         # No problems have occured, so return 0.
10576         return 0;
10580 sub kiriwrite_utf8convert{
10581 #################################################################################
10582 # kiriwrite_utf8convert: Properly converts values into UTF-8 and make sure      #
10583 # that the size of the string is correct when doing input validation.           #
10584 #                                                                               #
10585 # Usage:                                                                        #
10586 #                                                                               #
10587 # utfstring     # The UTF-8 string to convert.                                  #
10588 #################################################################################
10590         # Get the values passed to the subroutine.
10592         my ($utfstring) = @_;
10594         # Load the Encode perl module.
10596         use Encode;
10598         # Convert the string.
10600         my $finalutf8 = Encode::decode_utf8( $utfstring );
10602         return $finalutf8;
10606 sub kiriwrite_critical{
10607 #################################################################################
10608 # kiriwrite_critical: Displays a critical error message that cannot be          #
10609 # normally by the kiriwrite_error subroutine.                                   #
10610 #                                                                               #
10611 # Usage:                                                                        #
10612 #                                                                               #
10613 # errortype     Specifies the type of critical error that has occured.          #
10614 #################################################################################
10616         # Get the value that was passed to the subroutine.
10618         my ($error_type) = @_;
10620         # Get the error type from the errortype string.
10622         if ($error_type eq "configfilemissing"){
10624                 # The error is that the Kiriwrite configuration file is missing. 
10625                 # Print the header, error message and then end the script.
10627                 print header();
10628                 print "Critical Error: The Kiriwrite configuration file is missing! Running the setup script for Kiriwrite is recommended.";
10629                 exit;
10631         } elsif ($error_type eq "configfileinvalidpermissions") {
10633                 # The error is that the Kiriwrite configuration file has invalid
10634                 # permission settings. Print the header, error messsage and then
10635                 # end the script.
10637                 print header();
10638                 print "Critical Error: The Kiriwrite configuration file contains invalid permission settings! Please set the valid permission settings for the configuration file.";
10639                 exit; 
10641         } elsif ($error_type eq "outputsystemmissing") {
10643                 # The error is that the output system module is missing. Print the
10644                 # header, error message and then end the script.
10646                 print header();
10647                 print "Critical Error: The output system module is missing! Running the setup script for Kiriwrite is recommended.";
10648                 exit;
10650         } elsif ($error_type eq "outputsysteminvalidpermissions"){
10652                 # The error is that the output system module has invalid permissions
10653                 # set. Print the header, error message and then end the script.
10655                 print header();
10656                 print "Critical Error: The output system module contains invalid permission settings! Please set the valid permission settings for the output module.";
10657                 exit;
10659         }else {
10661                 # The error type is unspecified, so return a generic error message.
10663                 print header();
10664                 print "Critical Error: An unspecified critical error has occured.";
10665                 exit;
10667         }
10671 sub kiriwrite_output_page{
10672 #################################################################################
10673 # kiriwrite_output_page: Outputs the page to the browser/stdout/console.        #
10674 #                                                                               #
10675 # Usage:                                                                        #
10676 #                                                                               #
10677 # kiriwrite_output_page(pagetitle, pagedata, menutype);                         #
10678 #                                                                               #
10679 # pagetitle     Specifies the page title.                                       #
10680 # pagedata      Specifies the page data.                                        #
10681 # menutype      Prints out which menu to use.                                   #
10682 #################################################################################
10684         my ($pagetitle, $pagedata, $menutype) = @_;
10686         # Open the script page template and load it into the scriptpage variable,
10687         # while declaring the variable.
10689         open (SCRIPTPAGE, 'page.html');
10690         my @scriptpage = <SCRIPTPAGE>;
10691         close (SCRIPTPAGE);
10692         
10693         # Define the variables required.
10694         
10695         my $scriptpageline = "";
10696         my $pageoutput = "";
10697         my $menuoutput = "";
10698         
10699         # Print out the main menu for Kiriwrite.
10700         
10701         $menuoutput = "<a href=\"kiriwrite.cgi?mode=db\">View Databases</a> | <a href=\"kiriwrite.cgi?mode=page\">View Pages</a> | <a href=\"kiriwrite.cgi?mode=filter\">View Filters</a> | <a href=\"kiriwrite.cgi?mode=template\">View Templates</a> | <a href=\"kiriwrite.cgi?mode=compile\">Compile Pages</a> | <a href=\"kiriwrite.cgi?mode=settings\">View Settings</a>\r\n<br />";
10702         
10703         # Check what menu is going to be printed along with the default 'top' menu.
10704         
10705         if ($menutype eq "database"){
10706                 # If the menu type is database then print out the database sub-menu.
10707                 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=db\">Show Databases</a> | <a href=\"kiriwrite.cgi?mode=db&action=new\">Add Database</a>";     
10708                 
10709         } elsif ($menutype eq "pages"){
10710                 # If the menu type is pages then print out the pages sub-menu.
10711                 
10712                 # First, fetch the database name from the HTTP query string.
10713                 
10714                 my $query = new CGI;
10715                 my $db_filename = $query->param('database');
10716                 
10717                 # Check if a value has been placed in the db_filename string.
10718                 
10719                 if (!$db_filename){
10720                 
10721                         # As the database filename is blank, don't add an option to add a page.
10722                                 
10723                 } else {
10724                                         
10725                         # A database file has been specified so add an option to add a page to
10726                         # the selected database.
10727                 
10728                         $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=page&action=add&database=" . $db_filename . "\">Add Page</a>";
10729                 
10730                 }
10731         
10732         } elsif ($menutype eq "filter"){
10733         
10734                 # If the menu type is filters then print out the filter sub-menu.
10735                 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=filter\">Show Filters</a> | <a href=\"kiriwrite.cgi?mode=filter&action=add\">Add Filter</a>";
10736                 
10737         } elsif ($menutype eq "settings"){
10738         
10739                 # If the menu type is options then print out the options sub-menu.
10740                 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=settings\">Show Settings</a> | <a href=\"kiriwrite.cgi?mode=settings&action=edit\">Edit Settings</a>";
10741                 
10742         } elsif ($menutype eq "template"){
10743                 
10744                 # If the menu type is template then print out the template sub-menu.
10745                 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=template\">Show Templates</a> | <a href=\"kiriwrite.cgi?mode=template&action=add\">Add Template</a>"; 
10746         
10747         } elsif ($menutype eq "compile"){
10748         
10749                 # If the menu type is compile then print out the compile sub-menu.
10750                 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=compile&action=all\">Compile All</a> | <a href=\"kiriwrite.cgi?mode=compile&action=clean\">Clean Output Directory</a>";
10751         
10752         }
10753                         
10754         # Find <kiriwrite> tages and replace with the apporiate variables.
10755                 
10756         foreach $scriptpageline (@scriptpage){
10757                 
10758                 $scriptpageline =~ s/<kiriwrite:menu>/$menuoutput/g;
10759                 $scriptpageline =~ s/<kiriwrite:pagedata>/$pagedata/g;
10760                 
10761                 
10762                 # Check if page title specified is blank, otherwise add a page title
10763                 # to the title.
10764                 
10765                 if ($pagetitle eq ""){
10766                         $scriptpageline =~ s/<kiriwrite:title>//g;
10767                 } else {
10768                         $scriptpageline =~ s/<kiriwrite:title>/ ($pagetitle)/g;
10769                 }               
10771                 # Append processed line to the pageoutput variable.
10772                                 
10773                 $pageoutput = $pageoutput . $scriptpageline;
10774                 
10775         }
10776         
10777         # Print out the page to the browser/console/stdout.
10778         
10779         print $pageoutput;
10780         
10781         return;
10785 sub kiriwrite_output_xml{
10786 #################################################################################
10787 # kiriwrite_output_xml: Outputs several types of data to an XML file            #
10788 #                                                                               #
10789 # Usage:                                                                        #
10790 #                                                                               #
10791 # kiriwrite_output_xml(filename, type, data);                                   #
10792 #                                                                               #
10793 # filename      Specifies the filename of the XML file.                         #
10794 # type          Specifies the type of the XML file to be written.               #
10795 # newsettings   Specifies the new settings for the XML file.                    #
10796 #################################################################################
10798         # Get the variables passed from the subroutine.
10800         my ($xml_filename, $xml_type, @newsettings) = @_;
10801         
10802         # Check if filename is blank, if it is then return an error.
10803         
10804         if ($xml_filename eq ""){
10805                 # Filename is blank, return an error.
10806                 kiriwrite_error("blankfilename");
10807         }
10808         
10809         # Validate the XML filename to make sure nothing supicious is being passed.
10810         
10811         kiriwrite_variablecheck($xml_filename, "maxlength", 64, 0);
10812         kiriwrite_variablecheck($xml_filename, "filename", "", 0);
10813         
10814         # Check what type of XML data to output.
10815         
10816         if ($xml_type eq "config") {
10817                 
10818                 # The type of XML data to output is a Kiriwrite configuration file.
10819                 
10820                 # Get the data that has been passed to xml_data from the subroutine and
10821                 # split them into seperate values.
10822                 
10823                 my ($xml_config_databasedir, $xml_config_outputdir, $xml_config_imagesuri, $xml_config_datetime, $xml_config_systemlanguage, $xml_config_outputsystem) = @newsettings;
10824                 
10825                 # Create the XML data layout.
10826                 
10827                 my $xmldata = "<?xml version=\"1.0\"?>\r\n\r\n<kiriwrite-config>\r\n";
10828                 
10829                 $xmldata = $xmldata . "<!-- This file was automatically generated by Kiriwrite, please feel free to edit to your own needs. -->\r\n";
10830                 $xmldata = $xmldata . "\t<settings>\r\n\t\t<directories>\r\n";
10831                 
10832                 $xmldata = $xmldata . "\t\t\t<database>" . $xml_config_databasedir . "</database>\r\n";
10833                 $xmldata = $xmldata . "\t\t\t<output>" . $xml_config_outputdir  . "</output>\r\n";
10834                 $xmldata = $xmldata . "\t\t\t<images>" . $xml_config_imagesuri . "</images>\r\n";
10835                 $xmldata = $xmldata . "\t\t</directories>\r\n";
10836                 
10837                 $xmldata = $xmldata . "\t\t<language>\r\n";
10838                 $xmldata = $xmldata . "\t\t\t<type>" . $xml_config_systemlanguage . "</type>\r\n";
10839                 $xmldata = $xmldata . "\t\t</language>\r\n";
10840                 
10841                 $xmldata = $xmldata . "\t\t<system>\r\n";
10842                 $xmldata = $xmldata . "\t\t\t<output>" . $xml_config_outputsystem . "</output>\r\n";
10843                 $xmldata = $xmldata . "\t\t\t<datetime>" . $xml_config_datetime . "</datetime>\r\n";
10844                 $xmldata = $xmldata . "\t\t</system>\r\n";
10845                 
10846                 $xmldata = $xmldata . "\t</settings>\r\n";
10847                 
10848                 $xmldata = $xmldata . "</kiriwrite-config>";
10850                 # Open the Kiriwrite XML configuration file and write the new settings to the
10851                 # configuration file.
10852                 
10853                 open(XMLCONFIG, "> kiriwrite.xml");
10854                 print XMLCONFIG $xmldata;
10855                 close(XMLCONFIG);
10856                 
10857         } else {
10858         
10859                 # The type of XML data is something else that is not supported by
10860                 # Kiriwrite, so return an error.
10861                 
10862                 kiriwrite_error("invalidoption");
10863                 
10864         }
10865         
10866         return;
10870 #################################################################################
10871 # End listing the functions needed.                                             #
10872 #################################################################################
10874 #################################################################################
10875 # Begin proper script execution.                                                #
10876 #################################################################################
10878 kiriwrite_settings_load;        # Load the configuration options.
10880 my $query = new CGI;            # Easily fetch variables from the HTTP string.
10884 # Check if a mode has been specified and if a mode has been specified, continue
10885 # and work out what mode has been specified.
10887 if ($query->param('mode')){
10888         my $http_query_mode = $query->param('mode');
10889         
10890         if ($http_query_mode eq "db"){
10891         
10892                 # If mode is 'db' (database), then check what action is required.
10893         
10894                 if ($query->param('action')){
10895                         # An action has been specified, so find out what action has been specified.
10896                         
10897                         my $http_query_action = $query->param('action');
10898                         
10899                         if ($http_query_action eq "edit"){
10900                                 # The edit action (which mean edit the settings for the selected database) has been specified,
10901                                 # get the database name and check if the action to edit an database has been confirmed.
10902                                 
10903                                 if ($query->param('database')){
10904                                         # If there is a value in the database variable check if it is a valid database. Otherwise,
10905                                         # return an error.
10906                                         
10907                                         my $http_query_database = $query->param('database');
10908                                                                                 
10909                                         # Check if a value for confirm has been specified, if there is, check if it is the correct
10910                                         # value, otherwise return an error.
10911                                         
10912                                         if ($query->param('confirm')){
10913                                                 # A value for confirm has been specified, find out what value it is. If the value is correct
10914                                                 # then edit the database settings, otherwise return an error.
10915                                         
10916                                                 my $http_query_confirm = $query->param('confirm');
10917                                                 
10918                                                 if ($http_query_confirm eq "yes"){
10919                                                         # Value is correct, collect the variables to pass onto the database variable.
10920                                                         
10921                                                         # Load the XML::Simple module.
10922                                                         
10923                                                         use XML::Simple;
10924                                                         my $xsl = XML::Simple->new();
10925                                                         
10926                                                         # Get the variables from the HTTP query.
10927                                                         
10928                                                         my $newdatabasename             = $query->param('databasename');
10929                                                         my $newdatabasedescription      = $query->param('databasedescription');
10930                                                         my $newdatabasefilename         = $query->param('databasefilename');
10931                                                         my $databasename                = $query->param('olddatabasename');     
10932                                                         my $databasedescription         = $query->param('olddatabasedescription');
10933                                                         my $databaseshortname           = $query->param('database');
10934                                                         my $databasenotes               = $query->param('databasenotes');
10935                                                         my $databasecategories          = $query->param('databasecategories');
10937                                                         # Check the permissions of the database configuration file and return
10938                                                         # an error if the database permissions are invalid.
10939                                                         
10940                                                         # Pass the variables to the database editing subroutine.
10941                                                         
10942                                                         my $pagedata = kiriwrite_database_edit($databaseshortname, $databasename, $databasedescription, $newdatabasefilename, $newdatabasename, $newdatabasedescription, $databasenotes, $databasecategories, 1);
10943                                                         
10944                                                         kiriwrite_output_header;
10945                                                         kiriwrite_output_page("Database Edited", $pagedata, "database");
10946                                                         exit;
10947                                                         
10948                                                 } else {
10949                                                         # Value is incorrect, return and error.
10950                                                         kiriwrite_error("invalidvariable");
10951                                                 } 
10952                                                 
10953                                         }
10954                                         
10955                                         # Display the form for editing an database.
10956                                         my $pagedata = kiriwrite_database_edit($http_query_database);
10957                                         
10958                                         kiriwrite_output_header;
10959                                         kiriwrite_output_page("Edit Database", $pagedata, "database");
10960                                         exit;
10961                                         
10962                                 } else {
10963                                 
10964                                         # If there is no value in the database variable, then return an error.
10965                                         kiriwrite_error("invalidvariable");
10966                                         
10967                                 }       
10968                                 
10969                         } elsif ($http_query_action eq "delete"){
10970                         
10971                                 # Action equested is to delete a database, find out if the user has already confirmed deletion of the database
10972                                 # and if the deletion of the database has been confirmed, delete the database.
10973                                 
10974                                 if ($query->param('confirm')){
10975                                         
10976                                         # User has confirmed to delete a database, pass the parameters to the kiriwrite_database_delete
10977                                         # subroutine.
10978                                         
10979                                         my $database_filename = $query->param('database');
10980                                         my $database_confirm = $query->param('confirm');
10981                                         my $pagedata = kiriwrite_database_delete($database_filename, $database_confirm);
10982                                 
10983                                         kiriwrite_output_header;
10984                                         kiriwrite_output_page("Delete Database", $pagedata, "database");
10985                                         
10986                                         exit;
10987                                         
10988                                 }
10989                                 
10990                                 # User has clicked on the delete link (thus hasn't confirmed the action to delete a database).
10991                                 
10992                                 my $database_filename = $query->param('database');
10993                                 my $pagedata = kiriwrite_database_delete($database_filename);
10994                                         
10995                                 kiriwrite_output_header;
10996                                 kiriwrite_output_page("Delete Database", $pagedata, "database");
10997                                 
10998                                 exit;
10999                         
11000                         } elsif ($http_query_action eq "new"){
11001                         
11002                                 # Action requested is to create a new database, find out if the user has already entered the information needed
11003                                 # to create a database and see if the user has confirmed the action, otherwise printout a form for adding a
11004                                 # database.
11005                         
11006                                 my $http_query_confirm = $query->param('confirm');
11007                                 
11008                                 # Check if the confirm value is correct.
11009                                 
11010                                 if ($http_query_confirm){
11011                                         if ($http_query_confirm eq 1){
11012                                         
11013                                                 # User has confirmed to create a database, pass the parameters to the 
11014                                                 # kiriwrite_database_add subroutine.
11015                                 
11016                                                 my $http_query_confirm = $query->param('confirm');
11017                                         
11018                                                 my $database_name               = $query->param('databasename');
11019                                                 my $database_description        = $query->param('databasedescription');
11020                                                 my $database_filename           = $query->param('databasefilename');
11021                                                 my $database_notes              = $query->param('databasenotes');
11022                                                 my $database_categories         = $query->param('databasecategories');
11023                                         
11024                                                 my $pagedata = kiriwrite_database_add($database_filename, $database_name, $database_description, $database_notes, $database_categories, $http_query_confirm);
11025                                         
11026                                                 kiriwrite_output_header;
11027                                                 kiriwrite_output_page("Add Database", $pagedata, "database");
11028                                                 exit;
11029                                         
11030                                         } else {
11031                                         
11032                                                 # The confirm value is something else other than 1 (which it shouldn't be), so
11033                                                 # return an error.
11034                                         
11035                                         }
11036                                 }
11037                                 
11038                                 # User has clicked on the 'Add Database' link.
11039                                 
11040                                 my $pagedata = kiriwrite_database_add;
11041                                 
11042                                 kiriwrite_output_header;
11043                                 kiriwrite_output_page("Add Database", $pagedata, "database");
11044                                 exit;
11045                         
11046                         } else {
11047                                 # Another option has been specified, so return an error.
11048                                 
11049                                 kiriwrite_error("invalidaction");
11050                         }
11051                 }
11052         
11053                 # No action has been specified, do the default action of displaying a list
11054                 # of databases.
11055                 
11056                 my $pagedata = kiriwrite_database_list;
11057         
11058                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11059                 kiriwrite_output_page("", $pagedata, "database");       # Output the page to browser/console/stdout.
11060                 exit;                                   # End the script.       
11061                         
11062         } elsif ($http_query_mode eq "page"){
11063         
11064                 # If mode is 'page', then check what action is required.
11065         
11066                 if ($query->param('action')){
11067                         my $http_query_action = $query->param('action');
11068                 
11069                         # Check if the action requested matches with one of the options below. If it does,
11070                         # go to that section, otherwise return an error.
11071                         
11072                         if ($http_query_action eq "view"){
11073                                 
11074                                 # The action selected was to view pages from a database, 
11075                         
11076                                 my $database_name       = $query->param('database');                    
11077                                 my $pagedata            = kiriwrite_page_list($database_name);
11078                         
11079                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11080                                 kiriwrite_output_page("Viewing Database", $pagedata, "pages");  # Output the page to browser/console/stdout.
11081                                 exit;                   # End the script.
11082                                 
11083                         } elsif ($http_query_action eq "add"){
11084                                 
11085                                 # The action selected was to add a page to the selected database.
11087                                 my $http_query_confirm  = $query->param('confirm');
11088                                 
11089                                 if (!$http_query_confirm){
11090                                 
11091                                         $http_query_confirm = 0;
11092                                 
11093                                 }
11094                                 
11095                                 if ($http_query_confirm eq 1){
11096                                                                 
11097                                         my $http_query_database         = $query->param('database');
11098                                         my $http_query_filename         = $query->param('pagefilename');
11099                                         my $http_query_name             = $query->param('pagename');
11100                                         my $http_query_description      = $query->param('pagedescription');
11101                                         my $http_query_section          = $query->param('pagesection');
11102                                         my $http_query_template         = $query->param('pagetemplate');
11103                                         my $http_query_settings         = $query->param('pagesettings');
11104                                         my $http_query_content          = $query->param('pagecontent');
11105                                                                         
11106                                         my $pagedata                    = kiriwrite_page_add($http_query_database, $http_query_filename, $http_query_name, $http_query_description, $http_query_section, $http_query_template, $http_query_settings, $http_query_content, $http_query_confirm);
11107                                         
11108                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11109                                         kiriwrite_output_page("Add Page", $pagedata, "pages");  # Output the page to browser/console/stdout.
11110                                         exit;                           # End the script.
11111                                 
11112                                 }
11113                                                                 
11114                                 my $http_query_database = $query->param('database');
11115                                 my $pagedata            = kiriwrite_page_add($http_query_database);
11116                                 
11117                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11118                                 kiriwrite_output_page("Add Page", $pagedata, "pages");  # Output the page to browser/console/stdout.
11119                                 exit;                           # End the script.
11120                         
11121                         } elsif ($http_query_action eq "edit"){
11122                          
11123                                 # The action selected was to edit a page from a database.
11124                                 
11125                                 my $http_query_confirm = $query->param('confirm');
11126                                 
11127                                 if (!$http_query_confirm){
11128                                 
11129                                         $http_query_confirm = 0;
11130                                 
11131                                 }
11132                                 
11133                                 if ($http_query_confirm eq 1){
11134                                 
11135                                         # Get the needed values from the HTTP query.
11136                                         
11137                                         my $http_query_database         = $query->param('database');
11138                                         my $http_query_filename         = $query->param('page');
11139                                         my $http_query_newfilename      = $query->param('pagefilename');
11140                                         my $http_query_name             = $query->param('pagename');
11141                                         my $http_query_description      = $query->param('pagedescription');
11142                                         my $http_query_section          = $query->param('pagesection');
11143                                         my $http_query_template         = $query->param('pagetemplate');
11144                                         my $http_query_settings         = $query->param('pagesettings');
11145                                         my $http_query_content          = $query->param('pagecontent');                         
11146                                         
11147                                         # Pass the values to the editing pages subroutine.
11148                                         
11149                                         my $pagedata = kiriwrite_page_edit($http_query_database, $http_query_filename, $http_query_newfilename, $http_query_name, $http_query_description, $http_query_section, $http_query_template, $http_query_settings, $http_query_content, $http_query_confirm);
11150                                         
11151                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11152                                         kiriwrite_output_page("Edit Page", $pagedata, "pages"); # Output the page to browser/console/stdout.
11153                                         exit;                           # End the script.       
11154                                 
11155                                 }
11156                                 
11157                                 # Get the needed values from the HTTP query.
11158                                 
11159                                 my $http_query_database = $query->param('database');
11160                                 my $http_query_filename = $query->param('page');
11161                         
11162                                 # Pass the values to the editing pages subroutine.
11163                                 
11164                                 my $pagedata = kiriwrite_page_edit($http_query_database, $http_query_filename);
11165                                 
11166                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11167                                 kiriwrite_output_page("Edit Page", $pagedata, "pages"); # Output the page to browser/console/stdout.
11168                                 exit;                           # End the script.
11169                                 
11170                                 
11171                         } elsif ($http_query_action eq "delete"){
11172                         
11173                                 # The action selected was to delete a page from a database.
11174                                 
11175                                 my $http_query_database = $query->param('database');
11176                                 my $http_query_page     = $query->param('page');
11177                                 my $http_query_confirm  = $query->param('confirm');
11178                                 
11179                                 my $pagedata = "";
11180                                 my $selectionlist = "";
11181                                 
11182                                 if ($http_query_confirm){
11183                                 
11184                                         # The action has been confirmed, so try to delete the selected page
11185                                         # from the database.
11186                                         
11187                                         $pagedata = kiriwrite_page_delete($http_query_database, $http_query_page, $http_query_confirm);
11188                                         
11189                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11190                                         kiriwrite_output_page("Delete Page", $pagedata, "pages"); # Output the page to browser/console/stdout.
11191                                         exit;                           # End the script.
11192                                 
11193                                 }
11195                                 $pagedata = kiriwrite_page_delete($http_query_database, $http_query_page);
11196                                 
11197                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11198                                 kiriwrite_output_page("Delete Page", $pagedata, "pages"); # Output the page to browser/console/stdout.
11199                                 exit;                           # End the script.
11200                                 
11201                                 
11202                         } elsif ($http_query_action eq "multidelete"){
11204                                 # The action selected was to delete multiple pages from a
11205                                 # database.
11207                                 my $http_query_database = $query->param('database');
11208                                 my $http_query_confirm  = $query->param('confirm');
11210                                 my @filelist;
11211                                 my $pagedata;
11213                                 if ($http_query_confirm){
11215                                         # The action to delete multiple pages from the selected
11216                                         # database has been confirmed.
11218                                         @filelist       = kiriwrite_selectedlist();
11219                                         $pagedata       = kiriwrite_page_multidelete($http_query_database, $http_query_confirm, @filelist);
11221                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11222                                         kiriwrite_output_page("Delete selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11223                                         exit;                           # End the script.
11225                                 }
11227                                 # Get the list of selected pages and pass them to the
11228                                 # multiple page delete subroutine.
11230                                 @filelist       = kiriwrite_selectedlist();
11231                                 $pagedata       = kiriwrite_page_multidelete($http_query_database, $http_query_confirm, @filelist);
11233                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11234                                 kiriwrite_output_page("Delete selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11235                                 exit;                           # End the script.
11237                         } elsif ($http_query_action eq "multimove"){
11239                                 # The action selected was to move multiple pages from a
11240                                 # database.
11242                                 my $http_query_database         = $query->param('database');
11243                                 my $http_query_newdatabase      = $query->param('newdatabase');
11244                                 my $http_query_confirm          = $query->param('confirm');
11246                                 my @filelist;
11247                                 my $pagedata;
11249                                 if ($http_query_confirm){
11251                                         # The action to move multiple pages from the selected
11252                                         # database has been confirmed.
11254                                         @filelist       = kiriwrite_selectedlist();
11255                                         $pagedata       = kiriwrite_page_multimove($http_query_database, $http_query_newdatabase, $http_query_confirm, @filelist);
11257                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11258                                         kiriwrite_output_page("Move selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11259                                         exit;                           # End the script.
11261                                 }
11263                                 # Get the list of selected pages and pass them to the
11264                                 # multiple page move subroutine.
11266                                 @filelist       = kiriwrite_selectedlist();
11267                                 $pagedata       = kiriwrite_page_multimove($http_query_database, $http_query_newdatabase, $http_query_confirm, @filelist);
11269                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11270                                 kiriwrite_output_page("Move selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11271                                 exit;                           # End the script.
11273                         } elsif ($http_query_action eq "multicopy"){
11275                                 # The action selected was to copy multiple pages from a
11276                                 # database.
11278                                 my $http_query_database         = $query->param('database');
11279                                 my $http_query_newdatabase      = $query->param('newdatabase');
11280                                 my $http_query_confirm          = $query->param('confirm');
11282                                 my @filelist;
11283                                 my $pagedata;
11285                                 if ($http_query_confirm){
11287                                         # The action to copy multiple pages from the selected
11288                                         # database has been confirmed.
11290                                         @filelist       = kiriwrite_selectedlist();
11291                                         $pagedata       = kiriwrite_page_multicopy($http_query_database, $http_query_newdatabase, $http_query_confirm, @filelist);
11293                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11294                                         kiriwrite_output_page("Copy selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11295                                         exit;                           # End the script.
11297                                 }
11299                                 # Get the list of selected pages and pass them to the
11300                                 # multiple page copy subroutine.
11302                                 @filelist       = kiriwrite_selectedlist();
11303                                 $pagedata       = kiriwrite_page_multicopy($http_query_database, $http_query_newdatabase, $http_query_confirm, @filelist);
11305                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11306                                 kiriwrite_output_page("Copy selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11307                                 exit;                           # End the script.
11309                         } elsif ($http_query_action eq "multiedit"){
11311                                 # The action selected was to edit multiple pages from a
11312                                 # database.
11314                                 my $http_query_database         = $query->param('database');
11315                                 my $http_query_newsection       = $query->param('newsection');
11316                                 my $http_query_altersection     = $query->param('altersection');
11317                                 my $http_query_newtemplate      = $query->param('newtemplate');
11318                                 my $http_query_altertemplate    = $query->param('altertemplate');
11319                                 my $http_query_newsettings      = $query->param('newsettings');
11320                                 my $http_query_altersettings    = $query->param('altersettings');
11321                                 my $http_query_confirm          = $query->param('confirm');
11323                                 my @filelist;
11324                                 my $pagedata;
11326                                 if (!$http_query_confirm){
11328                                         @filelist       = kiriwrite_selectedlist();
11329                                         $pagedata       = kiriwrite_page_multiedit($http_query_database, $http_query_newsection, $http_query_altersection, $http_query_newtemplate, $http_query_altertemplate, $http_query_newsettings, $http_query_altersettings, $http_query_confirm, @filelist);
11331                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11332                                         kiriwrite_output_page("Edit selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11333                                         exit;   
11335                                 }
11337                                 # Get the list of selected pages and pass them to the
11338                                 # multiple page edit subroutine.
11340                                 @filelist       = kiriwrite_selectedlist();
11341                                 $pagedata       = kiriwrite_page_multiedit($http_query_database, $http_query_newsection, $http_query_altersection, $http_query_newtemplate, $http_query_altertemplate, $http_query_newsettings, $http_query_altersettings, $http_query_confirm, @filelist);
11343                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11344                                 kiriwrite_output_page("Edit selected pages", $pagedata, "pages"); # Output the page to browser/console/stdout.
11345                                 exit;                           # End the script.       
11347                         } else {
11348                                 kiriwrite_error("invalidaction");
11349                         }                               
11350                         
11351                 } else {
11352                         
11353                         # If there the action option is left blank, then print out a form where the database
11354                         # can be selected to view pages from.
11355                         
11356                         my $pagedata = kiriwrite_page_list;
11357                         
11358                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11359                         kiriwrite_output_page("Database Selection", $pagedata, "pages");        # Output the page to browser/console/stdout.
11360                         exit;                           # End the script.
11361                         
11362                 }
11363         
11364                 # No action has been specified, do the default action of listing pages from
11365                 # the first database found in the directory.
11366                 
11367         } elsif ($http_query_mode eq "filter"){
11368         
11369                 # If there's a value for action in the HTTP query, then
11370                 # get that value that is in the HTTP query.
11371                 
11372                 if ($query->param('action')){
11373                 
11374                         # There is a value for action in the HTTP query,
11375                         # so get the value from it.
11376                         
11377                         my $http_query_action = $query->param('action');
11378                         
11379                         if ($http_query_action eq "add"){
11380                 
11381                                 # The action the user requested is to add a filter to the
11382                                 # filters database.
11383                                 
11384                                 # Check if there is a value in confirm and if there is
11385                                 # then pass it on to the new find and replace words
11386                                 # to add to the filter database.
11387                                 
11388                                 my $http_query_confirm = $query->param('confirm');
11389                                 
11390                                 if ($http_query_confirm){
11391                                 
11392                                         # There is a value in http_query_confirm, so pass on the
11393                                         # new find and replace words so that they can be added
11394                                         # to the filters database.
11395                                         
11396                                         my $http_query_findwords        = $query->param('findword');
11397                                         my $http_query_replacewords     = $query->param('replaceword');
11398                                         my $http_query_priority         = $query->param('priority');
11399                                         my $http_query_notes            = $query->param('notes');
11400                                                                                 
11401                                         my $pagedata = kiriwrite_filter_add($http_query_findwords, $http_query_replacewords, $http_query_priority, $http_query_notes, $http_query_confirm);
11402                                         
11403                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11404                                         kiriwrite_output_page("Add Filters", $pagedata, "filter"); # Output the page to browser/console/stdout.
11405                                         exit;                           # End the script.
11406                                 
11407                                 }
11408                                 
11409                                 my $pagedata = kiriwrite_filter_add();
11410                                 
11411                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11412                                 kiriwrite_output_page("Add Filters", $pagedata, "filter"); # Output the page to browser/console/stdout.
11413                                 exit;                           # End the script.
11414                 
11415                         } elsif ($http_query_action eq "edit"){
11417                                 # The action the user requested is to edit an filter from
11418                                 # the filters database.
11419                                 
11420                                 my $http_query_number = $query->param('filter');
11421                                 my $http_query_confirm = $query->param('confirm');
11422                                 
11423                                 if ($http_query_confirm){
11424                                 
11425                                         # There is a value in http_query_confirm, so pass on the
11426                                         # new find and replace words so that the filters database
11427                                         # can be edited.
11428                                         
11429                                         my $http_query_findwords        = $query->param('filterfind');
11430                                         my $http_query_replacewords     = $query->param('filterreplace');
11431                                         my $http_query_priority         = $query->param('priority');
11432                                         my $http_query_notes            = $query->param('notes');
11433                                         
11434                                         my $pagedata = kiriwrite_filter_edit($http_query_number, $http_query_findwords, $http_query_replacewords, $http_query_priority, $http_query_notes, $http_query_confirm);
11435                                         
11436                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11437                                         kiriwrite_output_page("Edit Filter", $pagedata, "filter");      # Output the page to browser/console/stdout.
11438                                         exit;                           # End the script.
11439                                 
11440                                 }
11441                                 
11442                                 my $pagedata = kiriwrite_filter_edit($http_query_number);
11444                                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11445                                 kiriwrite_output_page("Edit Filter", $pagedata, "filter");      # Output the page to browser/console/stdout.
11446                                 exit;                                   # End the script.
11447                                 
11448                         } elsif ($http_query_action eq "delete"){
11449                 
11450                                 # The action the user requested is to delete an filter
11451                                 # from the filters database.
11452                                 
11453                                 my $http_query_number = $query->param('filter');
11454                                 my $http_query_confirm = $query->param('confirm');
11455                                 
11456                                 if ($http_query_confirm){
11457                                 
11458                                         my $pagedata = kiriwrite_filter_delete($http_query_number, $http_query_confirm);
11459                                         
11460                                         kiriwrite_output_header;                # Output the header to browser/console/stdout.
11461                                         kiriwrite_output_page("Delete Filter", $pagedata, "filter"); # Output the page to browser/console/stdout.
11462                                         exit;                                   # End the script
11463                                 
11464                                 }
11465                                 
11466                                 my $pagedata = kiriwrite_filter_delete($http_query_number);
11467                                 
11468                                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11469                                 kiriwrite_output_page("Delete Filter", $pagedata, "filter");    # Output the page to browser/console/stdout.
11470                                 exit;                                   # End the script.
11471                 
11472                         } else {
11473                 
11474                                 # Another action was requested that was not one of 
11475                                 # the ones prcedding this catch all, so return
11476                                 # an error saying that an invalid option was
11477                                 # specified.
11478                         
11479                                 kiriwrite_error("invalidaction");
11480                 
11481                         }
11482                 
11483                 } else {
11484                 
11485                         my $pagedata = kiriwrite_filter_list();
11486                         
11487                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11488                         kiriwrite_output_page("View Filters", $pagedata, "filter"); # Output the page to browser/console/stdout.
11489                         exit;                           # End the script.
11490                 
11491                 }
11492                 
11493                 
11494                 
11495         } elsif ($http_query_mode eq "template"){
11496         
11497                 # Check if an action has been specified in the HTTP query.
11498         
11499                 if ($query->param('action')){
11500                         
11501                         # An action has been specified in the HTTP query.
11502                 
11503                         my $http_query_action = $query->param('action');
11504                         
11505                         if ($http_query_action eq "delete"){
11506                                 # Get the required parameters from the HTTP query.
11507                         
11508                                 my $http_query_template = $query->param('template');
11509                                 my $http_query_confirm  = $query->param('confirm');
11510                                 
11511                                 # Check if a value for confirm has been specified (it shouldn't)
11512                                 # be blank.
11513                                 
11514                                 if ($http_query_confirm eq ""){
11515                                         # The confirm parameter of the HTTP query is blank, so
11516                                         # write out a form asking the user to confirm the deletion
11517                                         # of the selected template.
11518                                         
11519                                         my $pagedata = kiriwrite_template_delete($http_query_template);
11520                         
11521                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11522                                         kiriwrite_output_page("Delete Template", $pagedata, "template");        # Output the page to browser/console/stdout.
11523                                         exit;                           # End the script.
11524                                         
11525                                 } else {
11526                                         
11527                                         my $pagedata = kiriwrite_template_delete($http_query_template, $http_query_confirm);
11528                                         
11529                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11530                                         kiriwrite_output_page("Delete Template", $pagedata, "template");        # Output the page to browser/console/stdout.
11531                                         exit;                           # End the script.                               
11532                                         
11533                                 }
11534                                 
11535                         } elsif ($http_query_action eq "add") {
11536                         
11537                                 # Get the variables from the HTTP query in preperation for processing.
11538                         
11539                                 my $http_query_confirm  = $query->param('confirm');
11540                                 my $http_query_templatelayout   = $query->param('templatelayout');
11541                                 my $http_query_templatename     = $query->param('templatename');
11542                                 my $http_query_templatedescription = $query->param('templatedescription');
11543                                 my $http_query_templatefilename = $query->param('templatefilename');
11544                                 
11545                                 # Check if there is a confirmed value in the http_query_confirm variable.
11546                                 
11547                                 if (!$http_query_confirm){
11548                                 
11549                                         # Since there is no confirm value, print out a form for creating a new
11550                                         # template.
11551                                 
11552                                         my $pagedata = kiriwrite_template_add();
11553                                 
11554                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11555                                         kiriwrite_output_page("Add Template", $pagedata, "template");   # Output the page to browser/console/stdout.
11556                                         exit;                           # End the script.
11557                                 
11558                                 } else {
11559                                 
11560                                         # A value in the http_query_confirm value is specified, so pass the
11561                                         # variables onto the kiriwrite_template_add subroutine.
11562                                         
11563                                         my $pagedata = kiriwrite_template_add($http_query_templatefilename, $http_query_templatename, $http_query_templatedescription, $http_query_templatelayout, $http_query_confirm);
11564                                         
11565                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11566                                         kiriwrite_output_page("Add Template", $pagedata, "template");   # Output the page to browser/console/stdout.
11567                                         exit;                           # End the script.
11568                                 
11569                                 }
11570                         
11571                         } elsif ($http_query_action eq "edit") {
11572                         
11573                                 # Get the required parameters from the HTTP query.      
11574                         
11575                                 my $http_query_templatefile     = $query->param('template');
11576                                 my $http_query_confirm          = $query->param('confirm');
11577                                 
11578                                 # Check to see if http_query_confirm has a value of '1' in it and
11579                                 # if it does, edit the template using the settings providied.
11580                                 
11581                                 if (!$http_query_confirm){
11582                                 
11583                                         # Since there is no confirm value, open the template configuration
11584                                         # file and the template file itself then print out the data on to
11585                                         # the form.
11586                                         
11587                                         my $pagedata = kiriwrite_template_edit($http_query_templatefile);
11588                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11589                                         kiriwrite_output_page("Edit Template", $pagedata, "template");  # Output the page to browser/console/stdout.
11590                                         exit;                           # End the script.
11591                                 
11592                                 } elsif ($http_query_confirm eq 1) {
11593                                 
11594                                         # Since there is a confirm value of 1, the user has confirm the
11595                                         # action of editing of a template so get the other variables 
11596                                         # that were also sent and pass the variables to the subroutine.
11597                                         
11598                                         my $http_query_newfilename      = $query->param('newfilename');
11599                                         my $http_query_newname          = $query->param('newname');
11600                                         my $http_query_newdescription   = $query->param('newdescription');
11601                                         my $http_query_newlayout        = $query->param('newlayout');
11602                                         
11603                                         my $pagedata = kiriwrite_template_edit($http_query_templatefile, $http_query_newfilename, $http_query_newname, $http_query_newdescription, $http_query_newlayout, $http_query_confirm);
11604                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11605                                         kiriwrite_output_page("Edit Template", $pagedata, "template");  # Output the page to browser/console/stdout.
11606                                         exit;                           # End the script.
11607                                 
11608                                 } else {
11609                                 
11610                                         # Another confirm value is there instead of '0' or '1'. Return
11611                                         # an error saying it is invalid.
11612                                         
11613                                         kiriwrite_error("invalidvariable");
11614                                 
11615                                 }
11616                                 
11617                         } else {
11619                                 # Another action was specified and was not one of the ones above, so
11620                                 # return an error.
11622                                 kiriwrite_error("invalidaction");
11624                         }
11625                         
11626                 } else {
11627                 
11628                         # If the action option is left blank, then print out a form where the list
11629                         # of templates are available.
11630                         
11631                         my $pagedata = kiriwrite_template_list;
11632                         
11633                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11634                         kiriwrite_output_page("View Templates", $pagedata, "template"); # Output the page to browser/console/stdout.
11635                         exit;                           # End the script.
11636                 
11637                 }
11638         
11639         } elsif ($http_query_mode eq "compile"){
11640                 
11641                 # The mode selected is to compile pages from databases.
11642         
11643                 # If the action option is left blank, then print out a form where the list
11644                 # of databases to compile are available.
11645                 
11646                 if ($query->param('action')){
11647                 
11648                         my $http_query_action = $query->param('action');
11649                 
11650                         if ($http_query_action eq "compile"){
11651                         
11652                                 # The specified action is to compile the pages, check if the
11653                                 # action to compile the page has been confirmed.
11655                                 my $http_query_confirm  = $query->param('confirm');
11656                                 my $http_query_type     = $query->param('type');
11657                                 
11658                                 # If it is blank, set the confirm value to 0.
11659                                 
11660                                 if (!$http_query_confirm){
11661                                 
11662                                         # The http_query_confirm variable is uninitalised, so place a
11663                                         # '0' (meaning an unconfirmed action).
11664                                 
11665                                         $http_query_confirm = 0;
11666                                 
11667                                 }
11669                                 # If the compile type is blank then return an error.
11671                                 if (!$http_query_type){
11673                                         # Compile type is blank so return an error.
11675                                         kiriwrite_error("blankcompiletype");
11677                                 }
11679                                 if ($http_query_type eq "multiple"){    
11680         
11681                                         if ($http_query_confirm eq 1){
11682                                                 
11683                                                 # The action to compile the pages has been confirmed so
11684                                                 # compile the pages.
11685                                         
11686                                                 my @selectedlist = kiriwrite_selectedlist();
11687                                                 my $pagedata = kiriwrite_compile_makepages($http_query_type, $http_query_confirm, @selectedlist);
11688                                                 
11689                                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11690                                                 kiriwrite_output_page("Compiled Pages", $pagedata, "compile"); # Output the page to browser/console/stdout.
11691                                                 exit;                           # End the script.
11692                                         
11693                                         } else {
11694                                         
11695                                                 # The action to compile the pages has not been confirmed
11696                                                 # so write a form asking the user to confirm the action
11697                                                 # of compiling the pages.
11698                                                 
11699                                                 my @selectedlist = kiriwrite_selectedlist();
11700                                                 my $pagedata = kiriwrite_compile_makepages($http_query_type, $http_query_confirm, @selectedlist);
11701                                 
11702                                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11703                                                 kiriwrite_output_page("Compile mulitple databases", $pagedata, "compile"); # Output the page to browser/console/stdout.
11704                                                 exit;                           # End the script.
11705                                                 
11706                                         }
11708                                 } elsif ($http_query_type eq "single"){
11710                                         my $http_query_database = $query->param('database');
11711                                         my @selectedlist;
11712                                         $selectedlist[0] = $http_query_database;
11713                                         my $pagedata = kiriwrite_compile_makepages($http_query_type, $http_query_confirm, @selectedlist);
11715                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11716                                         kiriwrite_output_page("Compile one database", $pagedata, "compile");
11717                                         exit;                           # End the script.
11719                                 } else {
11721                                         kiriwrite_error("invalidcompiletype");
11723                                 }
11724                         
11725                         } elsif ($http_query_action eq "all"){
11727                                 # The selected action is to compile all of the databases
11728                                 # in the database directory. Check if the action to
11729                                 # compile all of the databases has been confirmed.
11731                                 my $http_query_confirm = $query->param('confirm');
11733                                 if (!$http_query_confirm){
11735                                         # The http_query_confirm variable is uninitalised, so place a
11736                                         # '0' (meaning an unconfirmed action).
11738                                         $http_query_confirm = 0;
11740                                 }
11742                                 if ($http_query_confirm eq 1){
11744                                         # The action to compile all the databases has been confirmed.
11746                                 }
11748                                 my $pagedata = kiriwrite_compile_all();
11750                                 kiriwrite_output_header;                        # Output the header to browser/console/stdout.
11751                                 kiriwrite_output_page("Compile All Databases", $pagedata, "compile");
11752                                 exit;
11754                         } elsif ($http_query_action eq "clean") {
11755                                 
11756                                 # The selected action is to clean the output directory.
11757                                 # Check if the action to clean the output directory
11758                                 # has been confirmed.
11759                                 
11760                                 my $http_query_confirm = $query->param('confirm');
11761                                 
11762                                 if (!$http_query_confirm){
11763                                 
11764                                         # The http_query_confirm variable is uninitalised, so place a
11765                                         # '0' (meaning an unconfirmed action).
11766                                         
11767                                         $http_query_confirm = 0;
11768                                 
11769                                 }
11770                                 
11771                                 if ($http_query_confirm eq 1){
11772                                 
11773                                         # The action to clean the output directory has been confirmed.
11775                                         my $pagedata = kiriwrite_compile_clean($http_query_confirm);
11776                         
11777                                         kiriwrite_output_header;                # Output the header to browser/console/stdout.
11778                                         kiriwrite_output_page("Clean Output Directory", $pagedata, "compile");  # Output the page to browser/console/stdout.
11779                                         exit;                                   # End the script.
11780                                                                         
11781                                 }
11782                                 
11783                                 # The action to clean the output directory is not
11784                                 # confirmed, so write a page asking the user
11785                                 # to confirm cleaning the output directory.
11786                         
11787                                 my $pagedata = kiriwrite_compile_clean();
11788                         
11789                                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11790                                 kiriwrite_output_page("Clean Output Directory", $pagedata, "compile");  # Output the page to browser/console/stdout.
11791                                 exit;                                   # End the script.
11792                         
11793                         } else {
11795                                 # The action specified was something else other than those
11796                                 # above, so return an error.
11798                                 kiriwrite_error("invalidaction");
11800                         }
11801                 }
11802                 
11803                 my $pagedata = kiriwrite_compile_list;
11804                 
11805                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11806                 kiriwrite_output_page("Compile Pages", $pagedata, "compile");   # Output the page to browser/console/stdout.
11807                 exit;                                   # End the script.
11808                 
11809         } elsif ($http_query_mode eq "settings"){
11810         
11811                 # The mode selected is view (and change settings).
11812         
11813                 # If the action value has been left blank, then view the list of
11814                 # current settings.
11815                 
11816                 if ($query->param('action')){
11817                         my $http_query_action = $query->param('action');
11818                         
11819                         if ($http_query_action eq "edit"){
11820                         
11821                                 # The action specified is to edit the settings. Check if the action
11822                                 # to edit the settings has been confirmed.
11823                                 
11824                                 my $http_query_confirm = $query->param('confirm');
11826                                 if (!$http_query_confirm){
11828                                         # The confirm value is blank, so set it to 0.
11830                                         $http_query_confirm = 0;
11832                                 }
11833                                 
11834                                 if ($http_query_confirm eq 1){
11835                                 
11836                                         # The action to edit the settings has been confirmed. Get the
11837                                         # required settings from the HTTP query.
11838                                 
11839                                         my $http_query_database         = $query->param('databasedir');
11840                                         my $http_query_output           = $query->param('outputdir');
11841                                         my $http_query_imagesuri        = $query->param('imagesuripath');
11842                                         my $http_query_datetimeformat   = $query->param('datetime');
11843                                         my $http_query_systemlanguage   = $query->param('language');
11844                                         my $http_query_outputsystem     = $query->param('outputsys');
11845                                         
11846                                         my $pagedata = kiriwrite_settings_edit($http_query_database, $http_query_output, $http_query_imagesuri, $http_query_datetimeformat, $http_query_systemlanguage, $http_query_outputsystem, $http_query_confirm);
11847                                         
11848                                         kiriwrite_output_header;        # Output the header to browser/console/stdout.
11849                                         kiriwrite_output_page("Edit Settings", $pagedata, "settings");  # Output the page to browser/console/stdout.
11850                                         exit;                           # End the script.
11851                                 
11852                                 }
11853                                 
11854                                 # The action to edit the settings has not been confirmed.
11855                         
11856                                 my $pagedata = kiriwrite_settings_edit;
11857                         
11858                                 kiriwrite_output_header;        # Output the header to browser/console/stdout.
11859                                 kiriwrite_output_page("Edit Settings", $pagedata, "settings");  # Output the page to browser/console/stdout.
11860                                 exit;                           # End the script.
11861                         
11862                         } else {
11864                                 # The action specified was something else other than those
11865                                 # above, so return an error.
11867                                 kiriwrite_error("invalidaction");
11869                         }
11870                         
11871                 }
11872         
11873                 # No action has been specified, so print out the list of settings currently being used.
11874                 
11875                 my $pagedata = kiriwrite_settings_view;
11876                 
11877                 kiriwrite_output_header;                # Output the header to browser/console/stdout.
11878                 kiriwrite_output_page("View Settings", $pagedata, "settings");  # Output the page to browser/console/stdout.
11879                 exit;                                   # End the script.
11880                 
11881         } else {
11882                 # Another mode has been specified than the ones above, so return an error saying that
11883                 # an invalid option was specified.
11884                 
11885                 kiriwrite_error("invalidmode");
11886         }
11887                 
11888 } else {
11890         # No mode has been specified, so print the default "first-run" view of the
11891         # database list.
11893         my $pagedata = kiriwrite_database_list;
11894         
11895         kiriwrite_output_header;                # Output the header to browser/console/stdout.
11896         kiriwrite_output_page("", $pagedata, "database");       # Output the page to browser/console/stdout.
11897         exit;                                   # End the script.
11898         
11901 __END__
11903 =head1 NAME
11905 Kiriwrite
11907 =head1 DESCRIPTION
11909 Web-based webpage compiler.
11911 =head1 AUTHOR
11913 Steve Brokenshire <sbrokenshire@xestia.co.uk>
11915 =head1 USAGE
11917 This perl script is intended to be used on a web server which has CGI with Perl support or with mod_perl support.
11919 =head1 DOCUMENTATION
11921 For more information on how to use Kiriwrite, please see the documentation that was included with Kiriwrite.
11923 - From the Xestia Documentation website: http://documentation.xestia.co.uk and click on the Kiriwrite link on the page.
11925 - From the Documentation directory from the Kiriwrite source packages (.tar/.tar.gz/.tar.bz2).
11927 - In the /usr/share/doc/kiriwrite directory if you installed the distribution-specific packages (and also have access to the server itself).
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy