3 #################################################################################
4 # Kiriwrite (kiriwrite.cgi) #
5 # Main program script #
9 # Copyright (C) 2005-2007 Steve Brokenshire <sbrokenshire@xestia.co.uk> #
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. #
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.#
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.
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 = (
43 #################################################################################
44 # Begin listing the functions needed. #
45 #################################################################################
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");
58 sub kiriwrite_page_add{
59 #################################################################################
60 # kiriwrite_page_add: Adds a page to a database #
64 # kiriwrite_page_add(database, pagefilename, title, description, section, #
65 # template, settings, info, confirm); #
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) = @_;
82 # Load the required Perl modules.
86 # Check if the action to add a new page has been confirmed (or not).
96 kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
98 # Check if the database filename is valid and return an error if
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");
117 # Check the length the database name and return an error if it's
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");
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
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");
176 if ($pagetitle_maxlength_check eq 1){
178 # The page title given is too long, so return an error.
180 kiriwrite_error("pagetitletoolong");
184 if ($pagedescription_maxlength_check eq 1){
186 # The page description given is too long, so return an error.
188 kiriwrite_error("pagedescriptiontoolong");
192 if ($pagesection_maxlength_check eq 1){
194 # The page section given is too long, so return an error.
196 kiriwrite_error("pagesectiontoolong");
200 if ($pagedatabase_maxlength_check eq 1){
202 # The page database given is too long, so return an error.
204 kiriwrite_error("pagedatabasefilenametoolong");
208 if ($pagesettings_maxlength_check eq 1){
210 # The page settings given is too long, so return an error.
212 kiriwrite_error("pagesettingstoolong");
216 if ($pagetemplate_maxlength_check eq 1){
218 # The page template given is too long, so return an error.
220 kiriwrite_error("pagetemplatefilenametoolong");
224 # The action to create a new page has been confirmed, so add the page to the
228 kiriwrite_error("pagefilenameblank");
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;
237 if ($pagetemplate ne "!none"){
239 # A template is being used so check the filename of the
242 $pagetemplate_filename_check = kiriwrite_variablecheck($pagetemplate, "page_filename", 0, 1);
246 if ($pagefilename_filename_check eq 1){
248 # The page filename given is invalid, so return an error.
250 kiriwrite_error("pagefilenameinvalid");
254 if ($pagesettings_setting_check eq 1){
256 # The page settings given is invalid, so return an error.
258 kiriwrite_error("pagesettingsinvalid");
262 if ($pagetemplate_filename_check eq 1){
264 # The template filename given is invalid, so return an error
266 kiriwrite_error("templatefilenameinvalid");
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");
283 if ($pagedatabase_permissions eq 1){
285 # The page database permissions are invalid, so return
288 kiriwrite_error("databaseinvalidpermissions");
292 # Load the SQLite database.
294 my $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $pagedatabase . '.db');
296 # Check if the filename used already exists and return
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();
305 while (@database_page = $string_handle->fetchrow_array()){
307 # The page exists so set the page exists value to 1.
313 if ($page_exists eq 1){
315 # The page exists so return an error.
317 kiriwrite_error("pagefilenameexists");
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();
328 my $database_name = $database_info[0];
330 # Convert the values so they don't cause the SQL query
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 . '\'
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>";
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.
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 = "";
390 my $template_warningmessage;
391 my $template_warning = 0;
393 my @database_template;
396 if ($database_page_exists eq 1){
398 # The page database does not exists so return an
401 kiriwrite_error("databasemissingfile");
405 if ($database_page_permissions eq 1){
407 # The page database permissions are invalid so
410 kiriwrite_error("databaseinvalidpermissions");
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;
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,
432 $template_warningmessage = "Template database has invalid permissions set. No template will be used.";
433 $template_warning = 1;
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 = "";
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>";
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;
489 # Load the page database and get the page database
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 . "";
523 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Template</td><td class=\"tablecell2\">" . $template_data . "</td></tr>";
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><kiriwrite:autosection> - Automatic page section name. (Will be blank if page section name is blank).<br>
530 <kiriwrite:autotitle> - Automatic title and section name based on page setting.<br>
531 <kiriwrite:pagedata> - Page Content<br>
532 <kiriwrite:pagetitle> - Page Title based on page setting.<br>
533 <kiriwrite:pagename> - Page Name<br>
534 <kiriwrite:pagedescription> - Page Description<br>
535 <kiriwrite:pagesection> - 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>";
546 # The confirm value is something else than '1' or '0' so
549 kiriwrite_error("invalidvalue");
557 sub kiriwrite_page_delete{
558 #################################################################################
559 # kiriwrite_page_delete: Deletes a (single) page from a database. #
563 # kiriwrite_page_delete(database, page, [confirm]); #
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.
578 # Check if the database filename is valid and return an error if
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");
597 # Check the length the database name and return an error if it's
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");
610 # Check if the page name is specified is blank and return an error if
615 # The page name is blank, so return an error.
617 kiriwrite_error("blankfilename");
621 # If the confirm value is blank, then set the confirm value to 0.
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
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");
650 if ($database_check_permissions eq 1){
652 # The database file has invalid permissions set, so
655 kiriwrite_error("databaseinvalidpermissions");
659 # Load the SQLite database and get information about the
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();
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
676 $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
677 $string_handle->execute();
682 while (@database_page = $string_handle->fetchrow_array()){
688 # Check if the page wasn't found and return an error if it
691 if ($page_found eq 0){
693 # Page wasn't found in the database so return
696 kiriwrite_error("pagefilenamedoesnotexist");
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();
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>";
729 } elsif ($confirm eq 0){
731 # Check that the database exists and has the correct permissions
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");
747 if ($database_check_permissions eq 1){
749 # The database file has invalid permissions set, so
752 kiriwrite_error("databaseinvalidpermissions");
756 # Load the SQLite database and get information about the
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();
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
773 $string_handle = $database_handle->prepare('SELECT * FROM kiriwrite_database_pages WHERE filename = \'' . $pagefilename_sql . '\' LIMIT 1');
774 $string_handle->execute();
780 while (@database_page = $string_handle->fetchrow_array()){
786 # Check if the page wasn't found and return an error if it
789 if ($page_found eq 0){
791 # Page wasn't found in the database so return
794 kiriwrite_error("pagefilenamedoesnotexist");
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();
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
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>";
833 # Another page deletion type was specified, so return an error.
835 kiriwrite_error("invalidoption");
841 sub kiriwrite_page_edit{
842 #################################################################################
843 # kiriwrite_page_edit: Edits a page from a database. #
847 # kiriwrite_page_edit(database, filename, newname, newfilename, newdescription, #
848 # newsection, newtemplate, newsettings, newpagecontent #
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.
873 # Check if the confirm value is blank and if it is, then set it to '0'.
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);
886 # Check if the database filename is valid and return an error if
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");
905 # Check the length the database name and return an error if it's
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");
918 # Check if the page identification number is blank (which it shouldn't
919 # be) and if it is, then return an error.
923 kiriwrite_error("blankfilename");
927 # Check if the confirm value is '1' and if it is, edit the specified
928 # page in the database.
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");
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");
987 if ($pagenewtitle_maxlength_check eq 1){
989 # The new page title given is too long, so return an error.
991 kiriwrite_error("pagetitletoolong");
995 if ($pagenewdescription_maxlength_check eq 1){
997 # The new page description given is too long, so return an error.
999 kiriwrite_error("pagedescriptiontoolong");
1003 if ($pagenewsection_maxlength_check eq 1){
1005 # The new page section given is too long, so return an error.
1007 kiriwrite_error("pagesectiontoolong");
1011 if ($pagenewsettings_maxlength_check eq 1){
1013 # The new page settings given is too long, so return an error.
1015 kiriwrite_error("pagesettingstoolong");
1019 if ($pagenewtemplate_maxlength_check eq 1){
1021 # The new page template given is too long, so return an error.
1023 kiriwrite_error("pagetemplatefilenametoolong");
1027 # Check if the new page filename and new page settings
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;
1034 if ($pagenewtemplate ne "!none"){
1036 # A template is being used so check the filename of the
1039 $pagetemplate_filename_check = kiriwrite_variablecheck($pagenewtemplate, "page_filename", 0, 1);
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");
1054 if ($pagenewsettings_settings_check eq 1){
1056 # The new page settings is invalid, so return an error.
1058 kiriwrite_error("pagesettingsinvalid");
1062 if ($pagetemplate_filename_check eq 1){
1064 # The template filename given is invalid, so return an error
1066 kiriwrite_error("templatefilenameinvalid");
1070 # Check if the page database exists and the permissions set for
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");
1084 if ($database_permissions eq 1){
1086 # The database permissions are invalid, so return
1089 kiriwrite_error("databaseinvalidpermissions");
1093 # Load the SQLite database.
1095 my $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
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;
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.
1119 if ($page_exists eq 0){
1121 # The page does not exist, so return an error.
1123 kiriwrite_error("pagefilenamedoesnotexist");
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();
1158 @database_info = $string_handle->fetchrow_array();
1159 $database_name = $database_info[0];
1161 # Convert certain values so that they can be displayed
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
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>";
1177 } elsif ($confirm eq 0) {
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");
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");
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();
1212 my @database_templates;
1214 my $template_filename;
1216 my $template_found = 0;
1218 while (@database_pages = $string_handle->fetchrow_array()){
1224 if ($page_count eq 0){
1226 # The page does not exist in the template database, so
1229 kiriwrite_error("pagefilenamedoesnotexist");
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
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.";
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.";
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.";
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;
1319 $template_data = $template_data . "<option value=\"" . $template_filename . "\">" . $template_name . " (" . $template_filename .")</option>";
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
1335 $template_data = $template_data . "<option value=\"!none\" selected>Don't use a template</option>";
1339 # The template filename is not set to use no
1342 $template_data = $template_data . "<option value=\"!none\">Don't use a template</option>";
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>";
1357 $template_data = $template_data . "</select>";
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>";
1383 $pagedata = $pagedata . "<tr><td class=\"tablecell1\">Page Template</td><td class=\"tablecell2\">" . $template_data . "</td></tr>";
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><kiriwrite:autosection> - Automatic page section name. (Will be blank if page section name is blank).<br>
1390 <kiriwrite:autotitle> - Automatic title and section name based on page setting.<br>
1391 <kiriwrite:pagedata> - Page Content<br>
1392 <kiriwrite:pagetitle> - Page Title based on page setting.<br>
1393 <kiriwrite:pagename> - Page Name<br>
1394 <kiriwrite:pagedescription> - Page Description<br>
1395 <kiriwrite:pagesection> - 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>";
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>";
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>";
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>";
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>";
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>";
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>";
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>";
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>";
1475 # The confirm value is a value other than '0' and '1' so
1478 kiriwrite_error("invalidvalue");
1484 sub kiriwrite_page_multidelete{
1485 #################################################################################
1486 # kiriwrite_page_multidelete: Delete mulitple pages from the database. #
1490 # kiriwrite_page_multidelete(database, confirm, filelist); #
1492 # database Specifies the database to delete multiple pages from. #
1493 # confirm Confirms the action to delete the selected pages from the #
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.
1506 # Define a variable for later on.
1510 # Check if the database name is blank and return an error if
1515 # The database name is blank so return an error.
1517 kiriwrite_error("databasenameblank");
1521 # Check if the page database exists and the permissions set for
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);
1527 if ($database_exists eq 1){
1529 # The database does not exist, so return an error.
1531 kiriwrite_error("databasemissingfile");
1535 if ($database_permissions eq 1){
1537 # The database permissions are invalid, so return
1540 kiriwrite_error("databaseinvalidpermissions");
1544 # Check if the file list array has any values and return
1545 # an error if it doesn't.
1549 # The page list really is blank so return
1552 kiriwrite_error("nopagesselected");
1555 # Check if the database filename is valid and return an error if
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");
1574 # Check the length the database name and return an error if it's
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");
1587 # Check if the confirm value is blank and if it is, then
1592 # The confirm value is blank so set the confirm value
1601 # The action to delete multiple pages from the database has
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.
1616 my $filelist_filename;
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()){
1635 if ($page_found eq 0){
1641 # Get the page name.
1643 $string_handle->execute();
1644 @database_page = $string_handle->fetchrow_array();
1645 $page_name = $database_page[1];
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
1655 $deleted_list = $deleted_list . kiriwrite_convert($page_name, "normal_display") . " (" . $filelist_filename . ")" . "<br>";
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>";
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.
1696 my $filelist_filename;
1697 my $filelist_filename_sql;
1698 my $database_name_out;
1700 my $pagenameslist_out;
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()){
1723 if ($pagefound eq 0){
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
1737 $pagenames[$pageseek] = $page_info[1];
1738 $filenames[$pageseek] = $page_info[0];
1740 # Increment the page seek counter and reset the
1750 $pagenameslist = "";
1752 foreach $pagename (@pagenames){
1756 $pagenameslist = $pagenameslist . "<i>No page name</i> (" . $filenames[$pageseek] . ")<br>";
1760 $pagenameslist = $pagenameslist . kiriwrite_convert($pagename, "normal_display") . " (" . kiriwrite_convert($filenames[$pageseek], "normal_display") . ")<br>";
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");
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 . "\">";
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] . "\">";
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>";
1816 # A confirm value other than 0 or 1 is given, so
1819 kiriwrite_error("invaildvalue");
1825 sub kiriwrite_page_multimove{
1826 #################################################################################
1827 # kiriwrite_page_multimove: Move several pages from one database to another #
1832 # kiriwrite_page_multimove(database, newdatabase, confirm, filelist); #
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 #
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.
1849 # Define a variable for later.
1853 # Check if the file list is blank and return an error
1858 # The file list really is blank so return
1861 kiriwrite_error("nopagesselected");
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");
1879 if ($database_permissions eq 1){
1881 # The database permissions are invalid, so return
1884 kiriwrite_error("oldmovedatabasefileinvalidpermissions");
1888 # Check if the confirm value is blank and if it is then
1889 # set the confirm value to 0.
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");
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");
1930 if ($database_permissions eq 1){
1932 # The database permissions are invalid, so return
1935 kiriwrite_error("newmovedatabasefileinvalidpermissions");
1939 # Define some values for later.
1941 my @olddatabase_info;
1942 my @olddatabase_page;
1943 my @newdatabase_info;
1944 my @newdatabase_page;
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 = "";
1965 my $page_filename_sql;
1966 my $page_filename_out;
1970 my $page_description;
1971 my $page_description_sql;
1973 my $page_section_sql;
1975 my $page_template_sql;
1979 my $page_settings_sql;
1980 my $page_lastmodified;
1981 my $page_lastmodified_sql;
1985 # Check if the database filename is valid and return an error if
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");
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
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();
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.
2056 # See if the page was found and write a warning if it
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>";
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();
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.
2088 # See if the page was found and write a warning if it
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>";
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
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 . '\'
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
2153 if (!$page_name_out){
2155 $page_name_out = "<i>No Name</i>";
2159 $movedlist = $movedlist . $page_name_out . " (" . $filename_out . ")" . "<br>";
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>";
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>";
2177 $pagedata = $pagedata . "No pages were moved from the '" . $olddatabase_name_out . "' database to the '" . $newdatabase_name_out . "' database.<br><br>";
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>";
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>";
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.
2216 my $data_file_length;
2218 my $database_filename;
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;
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()){
2253 if ($pagefound eq 0){
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
2267 $pagenames[$pageseek] = $page_info[1];
2268 $filenames[$pageseek] = $page_info[0];
2270 # Increment the page seek counter and reset the
2278 # Check if any pages exust and return an error if
2281 if ($pageseek eq 0){
2283 # None of the selected pages exist, so return
2286 kiriwrite_error("nopagesselected");
2290 # Create the page list of files to move.
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
2307 # The page name is blank.
2309 $pagelist = $pagelist . "<i>No Name</i>" . " (" . $filename . ")<br>";
2313 # Append the page name and filename to the page list.
2315 $pagelist = $pagelist . kiriwrite_convert($pagename, "normal_display") . " (" . $filename . ")<br>";
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 . "\">";
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");
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);
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);
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.
2381 # Load the SQLite database.
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
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>";
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>";
2439 # The confirm value is other than 0 or 1, so return
2442 kiriwrite_error("invalidvariable");
2450 sub kiriwrite_page_multicopy{
2451 #################################################################################
2452 # kiriwrite_page_multicopy: Copy several pages from one database to another #
2457 # kiriwrite_page_multicopy(database, newdatabase, confirm, filelist); #
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.
2473 # Define a variable for later.
2477 # Check if the file list is blank and return an error
2482 # The file list really is blank so return
2485 kiriwrite_error("nopagesselected");
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");
2503 if ($database_permissions eq 1){
2505 # The database permissions are invalid, so return
2508 kiriwrite_error("oldcopydatabasefilenameinvalidpermissions");
2512 # Check if the confirm value is blank and if it is then
2513 # set the confirm value to 0.
2521 # Check if the database filename is valid and return an error if
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");
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");
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");
2573 if ($database_permissions eq 1){
2575 # The database permissions are invalid, so return
2578 kiriwrite_error("newcopydatabasefileinvalidpermissions");
2582 # Define some values for later.
2584 my @olddatabase_info;
2585 my @olddatabase_page;
2586 my @newdatabase_info;
2587 my @newdatabase_page;
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 = "";
2608 my $page_filename_sql;
2609 my $page_filename_out;
2613 my $page_description;
2614 my $page_description_sql;
2616 my $page_section_sql;
2618 my $page_template_sql;
2622 my $page_settings_sql;
2623 my $page_lastmodified;
2624 my $page_lastmodified_sql;
2628 # Check if the database filename is valid and return an error if
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");
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
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();
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.
2698 # See if the page was found and write a warning if it
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>";
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();
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.
2730 # See if the page was found and write a warning if it
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>";
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
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 . '\'
2785 $newdatabase_string_handle->execute();
2787 # Append the copied page (filename and name) to the list of
2790 if (!$page_name_out){
2792 $page_name_out = "<i>No page name</i>";
2796 $copiedlist = $copiedlist . $page_name_out . " (" . $filename_out . ")" . "<br>";
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>";
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>";
2814 $pagedata = $pagedata . "No pages were copied from the '" . $olddatabase_name_out . "' database to the '" . $newdatabase_name_out . "' database.<br><br>";
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>";
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>";
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.
2853 my $data_file_length;
2855 my $database_filename;
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;
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()){
2890 if ($pagefound eq 0){
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
2904 $pagenames[$pageseek] = $page_info[1];
2905 $filenames[$pageseek] = $page_info[0];
2907 # Increment the page seek counter and reset the
2915 # Check if any pages exust and return an error if
2918 if ($pageseek eq 0){
2920 # None of the selected pages exist, so return
2923 kiriwrite_error("nopagesselected");
2927 # Create the page list of files to move.
2931 $pagelist_formdata = "";
2933 foreach $filename (@filenames){
2935 $pagename = $pagenames[$pageseek - 1];
2941 # Write a message saying that there is no page name.
2943 $pagelist = $pagelist . "<i>No Name</i>" . " (" . $filename . ")<br>";
2947 # Append the page name and filename to the page list.
2949 $pagelist = $pagelist . $pagename . " (" . $filename . ")<br>";
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 . "\">";
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");
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);
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);
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.
3017 # Load the SQLite database.
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
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>";
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>";
3075 # The confirm value is other than 0 or 1, so return
3078 kiriwrite_error("invalidvariable");
3084 sub kiriwrite_page_multiedit{
3085 #################################################################################
3086 # kiriwrite_page_multiedit: Edit several pages from a database. #
3090 # kiriwrite_page_multiedit(database, newsection, altersection, newtemplate, #
3091 # altertemplate, newsettings, altersettings #
3092 # confirm, filelist); #
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 #
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.
3114 # Define a variable for later.
3118 # Check if the file list is blank and return an error
3123 # The file list really is blank so return
3126 kiriwrite_error("nopagesselected");
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");
3144 if ($database_permissions eq 1){
3146 # The database permissions are invalid, so return
3149 kiriwrite_error("databaseinvalidpermissions");
3153 # Check if certain values are undefined and define them if
3156 if (!$altersection){
3158 # The alter section value is blank, so set it to
3161 $altersection = "off";
3165 if (!$altertemplate){
3167 # The alter template value is blank, so set it to
3170 $altertemplate = "off";
3174 if (!$altersettings){
3176 # The alter settings value is blank, so set it to
3179 $altersettings = "off";
3183 # Check if the database filename is valid and return an error if
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");
3202 # Check if the confirm value is blank and if it is then
3203 # set the confirm value to 0.
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
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
3247 kiriwrite_error("pagesectiontoolong");
3251 if ($newtemplate_maxlength_check eq 1 && $altertemplate eq "on"){
3253 # The new template name is too long, so return an
3256 kiriwrite_error("templatefilenametoolong");
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
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
3274 kiriwrite_error("templatefilenameinvalid");
3279 if ($newsettings_maxlength_check eq 1 && $altertemplate eq "on"){
3281 # The new settings value is too long, so return
3284 kiriwrite_error("pagesettingstoolong");
3288 if ($newsettings_settings_check eq 1 && $altertemplate eq "on"){
3290 # The new settings value is invalid, so return
3293 kiriwrite_error("pagesettingsinvalid");
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.
3313 my $edited_list = "";
3317 my $sqlquery_options = "";
3319 my $sqlquery_option;
3323 my $newtemplate_sql;
3324 my $newsettings_sql;
3327 my $sqlquery_seek = 0;
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
3341 if ($newtemplate eq "!skip"){
3343 $altertemplate = "off";
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
3355 kiriwrite_error("noeditvaluesselected");
3359 # Check if each value is meant to be changed on
3360 # the selected files. Start with the section
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
3375 if ($sqlquery_seek eq 0){
3377 # This is the first part of the SQL query.
3379 $sqlquery_list[$sqlquery_seek] = "pagesection = \'" . $newsection_sql . "\'";
3384 # This isn't the first part of the SQL query.
3386 $sqlquery_list[$sqlquery_seek] = ", pagesection = \'" . $newsection_sql . "\'";
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
3407 if ($sqlquery_seek eq 0){
3409 # This is the first part of the SQL query.
3411 $sqlquery_list[$sqlquery_seek] = "pagetemplate = \'" . $newtemplate_sql . "\'";
3416 # This isn't the first part of the SQL query.
3418 $sqlquery_list[$sqlquery_seek] = ", pagetemplate = \'" . $newtemplate_sql . "\'";
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
3439 if ($sqlquery_seek eq 0){
3441 # This is the first part of the SQL query.
3443 $sqlquery_list[$sqlquery_seek] = "pagesettings = \'" . $newsettings_sql . "\'";
3448 # This isn't the first part of the SQL query.
3450 $sqlquery_list[$sqlquery_seek] = ", pagesettings = \'" . $newsettings_sql . "\'";
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;
3471 # Edit each filename.
3473 foreach $filename (@filelist){
3475 # Check if the page filename exists in the
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()){
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
3501 $warninglist = $warninglist . "The page \'" . $filename_out . "\'could not be found." . "<br>";
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>";
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>";
3535 # Increment the counter of edited pages.
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>";
3553 # Write out the message saying that the selected pages
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>";
3563 # Check if any warnings have occured and write a message
3564 # if any warnings did occur.
3568 # One or several warnings have occured so
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>";
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>";
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.
3606 my @database_templates;
3610 my $filelist_filename;
3611 my $filelist_filename_sql;
3612 my $filelist_filename_out;
3616 my $pagelistformdata = "";
3621 my $template_filename;
3623 my $template_warning;
3625 # Get the information about each page that is going
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()){
3647 if ($pagefound eq 0){
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
3661 $pagenames[$pageseek] = $page_info[1];
3662 $filenames[$pageseek] = $page_info[0];
3664 # Add the form data for the page filename.
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
3679 # Check if any pages were found in the database and return
3682 if ($pageseek eq 0){
3684 # No pages were found so return an error.
3686 kiriwrite_error("nopagesselected");
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.";
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.";
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.";
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>";
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>";
3754 # Process the list of selected pages.
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.
3768 # Write a message saying there's no page name.
3770 $pagelist = $pagelist . "<i>No Name</i>" . " (" . $page_filename . ")" . "<br>"
3774 # Append the page to the list of pages to edit.
3776 $pagelist = $pagelist . $page_name . " (" . $page_filename . ")" . "<br>";
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>";
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>";
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>";
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>";
3828 # The confirm value is something else other than
3829 # 1 or 0, so return an error.
3831 kiriwrite_error("invalidvariable");
3837 sub kiriwrite_page_list{
3838 #################################################################################
3839 # kiriwrite_page_list: Lists pages from an database. #
3843 # kiriwrite_page_list([database]); #
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) = @_;
3852 # Load the required Perl modules.
3854 my $xsl = XML::Simple->new();
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.
3860 if (!$database_file) {
3862 # Define the variables required for this section.
3865 my $dblistdata = "";
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 = "";
3876 # Open the data directory and get all of the databases.
3878 opendir(DATADIR, $kiriwrite_config{"directory_data_db"});
3879 @data_directory = grep /m*\.db/, readdir(DATADIR);
3882 # Get the information about each database (the short name and friendly name).
3884 foreach $data_file (@data_directory){
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);
3895 if ($file_permissions ne 0){
3897 # The permissions for the database are invalid
3898 # so process the next database.
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.
3918 $dblistdata = $dblistdata . "<option value=\"" . $data_file_friendly . "\">" . $database_name . "</option>";
3922 # Write the page data.
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>";
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> | ";
3933 $pagedata = $pagedata . "<input type=\"submit\" value=\"View\">";
3934 $pagedata = $pagedata . "</form> <br>";
3944 my $pagemultioptions = "";
3945 my $database_handle = "";
3946 my $string_handle = "";
3947 my $db_friendlyname = "";
3948 my $tablestyle = "";
3950 my $page_filename = "";
3952 my $page_description = "";
3953 my $page_modified = "";
3955 my $tablestyletype = 0;
3957 my $db_file_notblank = 0;
3959 # The database_file variable is not blank, so print out a list of pages from
3960 # the selected database.
3962 # Preform a variable check on the database filename to make sure that it is
3963 # valid before using it.
3965 kiriwrite_variablecheck($database_file, "filename", "", 0);
3966 kiriwrite_variablecheck($database_file, "maxlength", 64, 0);
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.
3972 if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_file . '.db'){
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");
3984 # Load the SQLite database.
3986 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $database_file . '.db');
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
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]){
4021 if ($tablestyletype eq 0){
4023 $tablestyle = "tablecell1";
4024 $tablestyletype = 1;
4028 $tablestyle = "tablecell2";
4029 $tablestyletype = 0;
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>";
4043 if ($page_description eq ""){
4045 $page_description = "<i>No Description</i>";
4049 if ($page_modified eq ""){
4051 $page_modified = "<i>No Date</i>";
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>";
4061 # Check if the page count is more than
4063 if ($page_count > 0){
4064 $db_file_notblank = 1;
4067 # Append the closing table part into the tabledata string.
4069 $tabledata = $tabledata . "</table>";
4072 # Either the database file, the database configuration file or both are missing
4073 # so return an error saying that certain files are missing.
4075 kiriwrite_error("databasemissingfile");
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>";
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>";
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. #
4119 # kiriwrite_page_multipleselect(); #
4120 #################################################################################
4122 # Load the required perl modules for this subroutine.
4124 my $query = new CGI;
4126 # Setup some variables that will be used later on.
4128 my $page_select_seek = 1;
4129 my $page_current_value = "";
4130 my $selectionlist = "";
4132 # Get the required variables from the HTTP query.
4134 my $page_count = $query->param('count');
4138 # Get the current value of the selected page's checkbox.
4140 $page_current_value = $query->param('name[' . $page_select_seek . ']');
4142 # If page_current_value is undefinied or blank, set page_current_value
4145 if (!$page_current_value){
4147 $page_current_value = "off";
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"){
4156 # Append the current seek value to the selection list.
4158 $selectionlist = $selectionlist . $page_select_seek . "|";
4162 # Increment the page selection seeking counter.
4164 $page_select_seek++;
4166 } until ($page_select_seek eq $page_count);
4168 return $selectionlist;
4172 sub kiriwrite_template_add{
4173 #################################################################################
4174 # kiriwrite_template_add: Add a template to the template folder #
4178 # kiriwrite_template_add(filename, name, description, layout, confirm); #
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.
4189 my ($templatefilename, $templatename, $templatedescription, $templatelayout, $confirm) = @_;
4191 # Load the needed Perl Modules
4195 # Check if the confirm value is blank and if it is then set confirm to 0.
4199 # The confirm value is blank, so set the value of confirm to 0.
4207 # Check (validate) each of the values.
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){
4228 # The template filename length is too long, so return an error.
4230 kiriwrite_error("templatefilenametoolong");
4234 if ($templatename_length_check eq 1){
4236 # The template name length is too long, so return an error.
4238 kiriwrite_error("templatenametoolong");
4243 if ($templatedescription_length_check eq 1){
4245 # The template description length is too long, so return an error.
4247 kiriwrite_error("templatedescriptiontoolong");
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
4265 kiriwrite_error("templatedatabasefileinvalidpermissions");
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;
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");
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();
4306 # Check if the template filename is already used and if it is then
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");
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 . '\'
4344 $string_handle->execute();
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>";
4354 # No confirmation was made, so print out a form for adding a template.
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>";
4378 sub kiriwrite_template_edit{
4379 #################################################################################
4380 # kiriwrite_template_edit: Edit a template from the template folder. #
4384 # kiriwrite_template_edit(filename, [newfilename], [newname], [newdescription], #
4385 # [templatelayout], [confirm]); #
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 #
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.
4404 # Define the pagedata variable for later on.
4408 # Check if the confirm variable is blank, if it is then
4409 # set confirm to '0'
4413 # confirm is uninitalised/blank, so set the value of confirm
4420 # Check if the template filename is blank and if it is, then return
4423 if (!$templatefilename){
4425 kiriwrite_error("templatefilenameblank");
4431 # Check certain strings to see if they UTF8 compiliant.
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");
4468 if ($template_database_permissions eq 1){
4470 # The template database permissions are set incorrectly so
4473 kiriwrite_error("templatedatabaseinvalidpermissions");
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.
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>";
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;
4537 if ($template_found eq 0){
4539 # The template was not found (doesn't exist) so
4542 kiriwrite_error("templatedoesnotexist");
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");
4566 # Write out the form for editing an template with the current template
4567 # settings put into the correct place.
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>";
4591 sub kiriwrite_template_delete{
4592 #################################################################################
4593 # kiriwrite_template_delete: Delete a template from the template folder. #
4597 # kiriwrite_template_delete(filename, confirm); #
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.
4605 my ($template_filename, $template_confirm) = @_;
4607 if (!$template_confirm){
4608 $template_confirm = 0;
4611 # Load the required Perl modules
4614 # Check the length of the variables.
4615 kiriwrite_variablecheck($template_filename, "maxlength", 64, 0);
4616 kiriwrite_variablecheck($template_confirm, "maxlength", 1, 0);
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
4622 if (!$template_filename){
4624 # The template_filename string really is blank,
4625 # so return an error saying that an empty
4626 # filename was passed (to the subroutine).
4628 kiriwrite_error("templatefilenameblank");
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.
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");
4654 if ($template_database_permissions eq 1){
4656 # The template database permissions are set incorrectly so
4659 kiriwrite_error("templatedatabaseinvalidpermissions");
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();
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>";
4688 } elsif ($template_confirm eq 0) {
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.
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");
4707 if ($template_database_permissions eq 1){
4709 # The template database has invalid permissions set so return
4712 kiriwrite_error("templatedatabaseinvalidpermissions");
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
4726 my $template_found = 0;
4728 while(@template_data = $string_handle->fetchrow_array()){
4730 # A template has been found so increment the counter.
4735 if ($template_found eq 0){
4737 # The template does not exist, so return an error.
4738 kiriwrite_error("templatedoesnotexist");
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.
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>";
4769 $pagedata = $pagedata . "</form>";
4775 kiriwrite_error("invalidvariable");
4781 sub kiriwrite_template_list{
4782 #################################################################################
4783 # kiriwrite_template_list: List the templates in the template folder. #
4787 # kiriwrite_template_list(); #
4788 #################################################################################
4790 # Load the module required for processing the XML configuration files.
4794 # Define certain values for later.
4796 my @database_template;
4798 my $database_handle;
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 = "";
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");
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>";
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){
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;
4882 $template_stylename = "tablecell2";
4883 $template_style = 0;
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>";
4895 if (!$template_description){
4896 $template_description = "<i>No Description</i>";
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>";
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.
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
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>";
4928 # Process the templatedata into pagedata.
4930 $pagedata = "<h2>View Templates</h2>";
4931 $pagedata = $pagedata . $templatedata;
4937 sub kiriwrite_database_add{
4938 #################################################################################
4939 # kiriwrite_database_add: Creates a new database. #
4943 # kiriwrite_database_add(filename, name, description, [confirm]); #
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.
4953 my ($database_filename, $database_name, $database_description, $database_notes, $database_categories, $database_confirm) = @_;
4955 # Load the perl modules for this subroutine.
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;
4971 if ($database_confirm eq 1){
4973 # The action to create a new database is confirmed.
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");
5005 if ($database_description_check_length eq 1){
5007 # The database description length is too long, so return an error.
5008 kiriwrite_error("databasedescriptiontoolong");
5012 if ($database_name_check_blank eq 1){
5014 # The database name is blank, so return an error.
5015 kiriwrite_error("databasenameblank");
5019 if ($database_filename_check_length eq 1){
5021 # The database filename is to long, so return an error.
5022 kiriwrite_error("databasefilenametoolong");
5026 if ($database_categories_check_length eq 1){
5028 # The database categories is too long, so return an error.
5029 kiriwrite_error("databasecategoriestoolong");
5033 # Check if the database filename is blank and if it is then
5034 # generate a filename.
5036 if ($database_filename eq ""){
5038 # Filename is blank so generate a file name from
5039 # the database name.
5041 $database_filename = kiriwrite_processfilename($database_name);
5043 # Check to make sure that the database filename really
5046 kiriwrite_variablecheck($database_filename, "filename", "", 0);
5047 kiriwrite_variablecheck($database_filename, "maxlength", 64, 0);
5049 # Check if the database and database configuration file does
5050 # not already exist.
5052 if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db'){
5054 # A filename exists, so abort the script telling the user that those files exist.
5055 kiriwrite_error("fileexists");
5061 # Filename is not blank, so check if the filenames are already
5062 # being used and if the actual filename itself is valid.
5064 # Check the filename to make sure the database and the database configuration file does not already exist.
5066 if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_filename . '.db' ){
5068 # A filename exists, so abort the script telling the user that those files exist.
5070 kiriwrite_error("fileexists");
5075 # Check to make sure the database filename itself really is
5078 kiriwrite_variablecheck($database_filename, "filename", "", 0);
5079 kiriwrite_variablecheck($database_filename, "maxlength", 64, 0);
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");
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),
5097 categories varchar(512),
5098 kiriwrite_version_major int(4),
5099 kiriwrite_version_minor int(4),
5100 kiriwrite_version_revision int(4)
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),
5111 pagesettings int(1),
5112 lastmodified datetime
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"} . '\'
5127 $string_handle->execute();
5129 $database_name_final = kiriwrite_convert($database_name_final, "normal_display");
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>";
5139 # There is confirm value is not 1, so write a form for creating a database to
5142 my $pagedata = "<h2>Add Database</h2>\r\n";
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 />
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>
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";
5167 # Exit the subroutine taking the data in the pagadata variable with it.
5173 sub kiriwrite_database_edit{
5174 #################################################################################
5175 # kiriwrite_database_edit: Edits an database. #
5179 # kiriwrite_database_edit(filename, name, description, newfilename, newname, #
5180 # newdescription, notes, categories, [confirm]); #
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.
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;
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.
5223 kiriwrite_error("databasemissingfile");
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
5244 kiriwrite_variablecheck($database_shortname, "filename", "", 0);
5246 # Preform the following tests.
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");
5264 if ($database_description_check_length eq 1){
5266 # The database description length is too long, so return an error.
5267 kiriwrite_error("databasedescriptiontoolong");
5271 if ($database_name_check_blank eq 1){
5273 # The database name is blank, so return an error.
5274 kiriwrite_error("databasenameblank");
5278 if ($database_filename_check_length eq 1){
5280 # The database filename is too long, so return an error.
5281 kiriwrite_error("databasefilenametoolong");
5285 if ($database_categories_check_length eq 1){
5287 # The database categories is too long, so return an error.
5288 kiriwrite_error("databasecategoriestoolong");
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");
5305 # Check if the old and new filenames have changed, if they have then rename the database file.
5309 $pagedata = "<h2>Edit Database</h2>";
5311 if ($database_shortname ne $database_newfilename){
5313 # Old name does not match with the new filename, so rename the database file.
5315 # Check if a database with the same shortname already exists, if it does then return with an error.
5317 if (-e $kiriwrite_config{"directory_data_db"} . '/' . $database_newfilename . '.db'){
5318 kiriwrite_error("databasealreadyexists");
5321 # Change the database filename.
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;
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");
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>";
5365 # Check if the database filename given is valid and return an error
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.
5378 kiriwrite_error("databasemissingfile");
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");
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.
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 />";
5437 $pagedata = $pagedata . "<input type=\"submit\" value=\"Edit settings\" /> | <input type=\"reset\" value=\"Reset settings\" />";
5438 $pagedata = $pagedata . "</form>";
5443 # The interpreter should not be here. So return an error saying invalid variable.
5445 kiriwrite_error("invalidvariable");
5449 sub kiriwrite_database_delete{
5450 #################################################################################
5451 # kiriwrite_database_delete: Deletes an database. #
5455 # kiriwrite_database_delete(filename, [confirm]); #
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) = @_;
5463 # Load the CGI module and the XML::Simple module for this subroutine.
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;
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.
5484 kiriwrite_error("databasemissingfile");
5488 # Check if the database filename given is valid and return an error
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.
5496 if ($database_confirm eq 1){
5497 # There is a value in the confirm variable of the HTTP query.
5499 # Before deleting, check the permissions of the database file.
5501 my $deleted_database_file_permissions = 0;
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");
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');
5533 # Write a message saying that the database has been deleted.
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>";
5544 # The action has not been confirmed, so write out a form asking the
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");
5560 # Load the SQLite database to get the database name.
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");
5573 # Write out the form to ask the user to confirm the deletion of the
5574 # selected database.
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";
5590 sub kiriwrite_database_list{
5591 #################################################################################
5592 # kiriwrite_database_list: Lists the databases available. #
5596 # kiriwrite_database_list(); #
5597 #################################################################################
5599 # Load the following Perl modules.
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");
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);
5622 # Declare the following variables that are going to be used before using
5623 # the foreach function.
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 = "";
5633 my $string_handle = "";
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 = "";
5642 # Begin creating the table for the list of databases.
5644 $pagedata = "<h2>Database List</h2>\r\n";
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";
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";
5660 # Load the SQLite database.
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,
5673 $string_handle->execute();
5674 @database_info = $string_handle->fetchrow_array();
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";
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";
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";
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){
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>";
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;
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;
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 #
5758 # kiriwrite_filter_list(); #
5759 #################################################################################
5761 # Load the needed Perl modules.
5765 # Check if the filters database exists and the
5766 # permissions for the filters database are
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;
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");
5792 # Define some variables required for processing the filters list.
5794 my @database_filters;
5795 my $database_handle;
5798 my $filterdata = "";
5799 my $filterswarning = "";
5803 my $filter_find_out;
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
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";
5856 $filter_style_name = "tablecell2";
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>";
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
5881 $filterdata = "<b>Warning</b>: One (or more) of your filters has a blank find filter and needs to be fixed.<br><br>" . $filterdata;
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>";
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.";
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
5913 if ($filtersdb_notexist eq 1){
5915 # The filters database doesn't exist so write
5918 $filterswarning = "The filters database does not exist and will be created when a filter is added.";
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>";
5935 # The filters database exists so write out the
5938 $pagedata = $pagedata . $filterdata;
5946 sub kiriwrite_filter_add{
5947 #################################################################################
5948 # kiriwrite_filter_add: Adds a filter to the filter list. #
5952 # kiriwrite_filter_add(filterfind, filterreplace, filterpriority, #
5953 # filternotes, [confirm]); #
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.
5964 my ($filter_new_find, $filter_new_replace, $filter_new_priority, $filter_new_notes, $confirm) = @_;
5966 # Load the needed Perl modules.
5970 # Check the confirm value to make sure it is no more than
5971 # one character long.
5973 kiriwrite_variablecheck($confirm, "maxlength", 1, 0);
5975 # Define the page data value for later.
5981 # The confirm value is undefined, so set the
5982 # value of the confirm integer to '0'.
5990 # The confirm integer is '1', so add the word
5991 # to the filter list.
5993 # First, check the variables recieved are UTF8
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
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
6010 if (!$filter_new_find){
6012 # The find filter given is blank so return an
6015 kiriwrite_error("blankfindfilter");
6019 if (!$filter_new_priority){
6021 # The filter priority is blank so set it
6024 $filter_new_priority = 1;
6028 # Check the length and contents of the values given
6029 # to make sure they are valid.
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
6039 if ($filterfind_maxlength_check eq 1){
6041 # The find filter is too long, so return
6044 kiriwrite_error("findfiltertoolong");
6048 if ($filterreplace_maxlength_check eq 1){
6050 # The replace filter is too long, so
6053 kiriwrite_error("replacefiltertoolong");
6057 if ($filterpriority_maxlength_check eq 1){
6059 # The length of the filter priority
6060 # given is too long, so return an
6063 kiriwrite_error("filterprioritytoolong");
6067 if ($filterpriority_numbers_check eq 1){
6069 # The priority of the filter given
6070 # contains characters other than
6073 kiriwrite_error("filterpriorityinvalidchars");
6077 # Check if the filter priority is less than 1
6078 # and more than 10000 and return an error
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");
6090 # Define some variables for later on.
6092 my @database_filters;
6095 my $database_handle;
6097 my $chk_database_handle;
6098 my $chk_string_handle;
6101 my $filters_permissions;
6102 my $filters_count = 0;
6103 my $blankid_found = 0;
6106 my $filter_exists = 0;
6108 my $first_filter = 0;
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");
6118 if ($filters_exist eq 0){
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);
6125 if ($filters_permissions eq 1){
6127 # The filters database has invalid permissions so
6130 kiriwrite_error("filtersdbpermissions");
6134 # Load the SQLite database.
6136 $database_handle = DBI->connect("dbi:SQLite:dbname=filters.db");
6140 # The filters database does not exist, so create
6141 # the filters database.
6143 # Check if the filters database can really be
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");
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,
6162 findsetting varchar(1024),
6163 replacesetting varchar(1024),
6167 $string_handle->execute();
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
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
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()){
6199 if ($filter_exists eq 0 && $blankid_found eq 0){
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.
6223 # Set the next identification number as the
6224 # current identification number add one and
6225 # increment the filters count.
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
6245 # Convert the values so that they can be processed
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 . '\'
6263 $string_handle->execute();
6265 # Write out a message saying that the filter was added to the
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>";
6274 } elsif ($confirm ne 0) {
6276 # The confirm integer is another value (which
6277 # it shouldn't be) so return an error.
6279 kiriwrite_error("invalidvalue");
6283 # The confirm integer was blank so print out a form
6284 # for adding a new filter.
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>";
6306 sub kiriwrite_filter_edit{
6307 #################################################################################
6308 # kiriwrite_filter_edit: Edits a filter from the filter list. #
6312 # kiriwrite_filter_edit(filternumber, newfilterfind, newfilterreplace, #
6313 # newfilterpriority, newfilternotes, confirm); #
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.
6326 my ($filter_id, $filter_new_find, $filter_new_replace, $filter_new_priority, $filter_new_notes, $confirm) = @_;
6328 # Load the needed Perl modules.
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.
6341 # Check if the confirm value is blank and if it is
6342 # srt the confirm value to 0.
6346 # The confirm value does not have any value
6347 # set so set it to 0.
6353 # Check if the filter identification number is blank,
6354 # contains characters other than numbers and is more
6355 # than seven characters long.
6359 # The filter identification number is blank,
6360 # so return an error.
6362 kiriwrite_error("filteridblank");
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
6374 kiriwrite_error("filteridinvalid");
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
6386 kiriwrite_error("filteridtoolong");
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
6400 kiriwrite_error("filtersdbmissing");
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");
6415 # Define some variables for later.
6417 my $database_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;
6425 my $filter_find_sql;
6426 my $filter_find_out;
6428 my $filter_replace_sql;
6429 my $filter_replace_out;
6431 my $filter_notes_sql;
6432 my $filter_notes_out;
6434 # Check if the action to edit a filter has been
6437 # Load the SQLite database.
6439 $database_handle = DBI->connect("dbi:SQLite:dbname=filters.db");
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");
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.
6477 # Check if the filter really doesn't exist.
6479 if ($filter_exists eq 0){
6481 # The filter really does not exist so
6484 kiriwrite_error("filterdoesnotexist");
6488 # First, check the variables recieved are UTF8
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
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
6507 if (!$filter_new_find){
6509 # The find filter given is blank so return an
6512 kiriwrite_error("blankfindfilter");
6516 if (!$filter_new_priority){
6518 # The filter priority is blank so set it
6521 $filter_new_priority = 1;
6525 # Check the length and contents of the values given
6526 # to make sure they are valid.
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
6536 if ($filterfind_maxlength_check eq 1){
6538 # The find filter is too long, so return
6541 kiriwrite_error("findfiltertoolong");
6545 if ($filterreplace_maxlength_check eq 1){
6547 # The replace filter is too long, so
6550 kiriwrite_error("replacefiltertoolong");
6554 if ($filterpriority_maxlength_check eq 1){
6556 # The length of the filter priority
6557 # given is too long, so return an
6560 kiriwrite_error("filterprioritytoolong");
6564 if ($filterpriority_numbers_check eq 1){
6566 # The priority of the filter given
6567 # contains characters other than
6570 kiriwrite_error("filterpriorityinvalidchars");
6574 # Check if the filter priority is less than 1
6575 # and more than 10000 and return an error
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");
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>";
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.
6631 # Check if the filter really doesn't exist.
6633 if ($filter_exists eq 0){
6635 # The filter really does not exist so
6638 kiriwrite_error("filterdoesnotexist");
6642 # Get the filter information.
6644 $string_handle->execute();
6646 @database_filter = $string_handle->fetchrow_array();
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");
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>";
6682 # A confirm value other than 0 or 1 has been
6683 # specified, so return an error.
6685 kiriwrite_error("invalidvalue");
6691 sub kiriwrite_filter_delete{
6692 #################################################################################
6693 # kiriwrite_filter_delete: Deletes a filter from the filter list. #
6697 # kiriwrite_filter_delete(number, confirm); #
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.
6705 my ($filter_id, $confirm) = @_;
6707 # Load the needed Perl modules.
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.
6720 # Check if the confirm value is blank and if it is
6721 # srt the confirm value to 0.
6725 # The confirm value does not have any value
6726 # set so set it to 0.
6732 # Check if the filter identification number is blank,
6733 # contains characters other than numbers and is more
6734 # than seven characters long.
6738 # The filter identification number is blank,
6739 # so return an error.
6741 kiriwrite_error("filteridblank");
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
6753 kiriwrite_error("filteridinvalid");
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
6765 kiriwrite_error("filteridtoolong");
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
6779 kiriwrite_error("filtersdbmissing");
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");
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;
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'.
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");
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.
6840 # Check if the filter really doesn't exist.
6842 if ($filter_exists eq 0){
6844 # The filter really does not exist so
6847 kiriwrite_error("filterdoesnotexist");
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) {
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
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.
6882 # Check if the filter really doesn't exist.
6884 if ($filter_exists eq 0){
6886 # The filter really does not exist so
6889 kiriwrite_error("filterdoesnotexist");
6893 # The confirm integer is '0', so continue write out
6894 # a form asking the user to confirm the deletion
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>";
6909 kiriwrite_error("invalidvalue");
6917 sub kiriwrite_compile_makepages{
6918 #################################################################################
6919 # kiriwrite_compile_makepages: Compile the selected pages and place them in the #
6920 # specified output directory. #
6924 # kiriwrite_compile_makepages(type, selectedlist, confirm); #
6926 # type Specifies if single or multiple databases are to be compiled. #
6927 # confirm Specifies if the action to compile the databases should really #
6929 # selectedlist Specifies the databases to compile from as an array. #
6930 #################################################################################
6932 # Get the values that have been passed to the subroutine.
6934 my ($type, $confirm, @selectedlist) = @_;
6936 # Check if the confirm value is more than one
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.
6946 # The confirm value is blank, so set the
6947 # confirm value to 0.
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");
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
6975 kiriwrite_error("variabletoolong");
6979 # Define the following values for later.
6983 # Check if the action to compile the databases
6984 # has been confirmed.
6988 # The action to compile the datavases has
6991 # Define some variables for later.
7000 my @pagedirectories;
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;
7014 my $database_name_out;
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;
7027 my $page_description;
7030 my $page_template_out;
7033 my $page_lastmodified;
7036 my $page_autosection;
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
7064 kiriwrite_error("outputdirectorymissing");
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");
7079 # Check if the filters database exists and has valid
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>";
7095 $filters_permissions = kiriwrite_filepermissions("filters.db", 1, 0);
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>";
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){
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;
7142 # Add each find and replace filter.
7144 $findfilter[$filters_count] = $filterslist[2];
7145 $replacefilter[$filters_count] = $filterslist[3];
7152 $messages = $messages . $information_prefix . "The filters database has been loaded." . "<br>";
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;
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;
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;
7214 $messages = $messages . $information_prefix . "The templates database has been loaded." . "<br>";
7220 # Process each database.
7222 foreach $database (@selectedlist){
7224 # Check if the database filename and length
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>";
7244 if ($database_maxlength_check ne 0){
7246 # The database file is too long, so process the
7249 $messages = $messages . $error_prefix . "The database with the filename '" . $database_out . ".db' is too long. Skipping this database..." . "<br>";
7255 # Check if the database exists and has valid
7258 $database_exists = kiriwrite_fileexists($kiriwrite_config{"directory_data_db"} . '/' . $database . '.db');
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>";
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>";
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>";
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.
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>";
7344 $page_final = $page_content;
7346 } elsif ($page_template eq "!none"){
7348 $page_final = $page_content;
7352 $page_final = $templatefiles{$page_template}{template};
7353 $page_final =~ s/<kiriwrite:pagecontent>/$page_content/g;
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.
7385 $page_autotitle = "(" . $page_section . ")";
7387 $page_final =~ s/<kiriwrite:pagetitle>/$page_section/g;
7391 # Check if the section name is not blank and
7392 # place brackets inbetween if it is.
7396 $page_autosection = "(" . $page_section . ")";
7400 # Replace each <kiriwrite> value with the apporiate page
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){
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;
7428 # Process the page filename and check what directories
7429 # need to be created.
7431 $page_filename_length = int(length($page_filename));
7435 $page_filename_char = substr($page_filename, $page_filename_seek, 1);
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++;
7452 # Append the character to the directory/filename.
7454 $page_filename_directory = $page_filename_directory . $page_filename_char;
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.
7467 $page_directory_path = $page_directory_path . '/' . $page_directory_name;
7469 mkdir($kiriwrite_config{"directory_data_output"} . '/' . $page_directory_path);
7473 # Write the file to the output directory.
7475 open(PAGE, "> " . $kiriwrite_config{"directory_data_output"} . '/' . $page_filename);
7476 print PAGE $page_final;
7479 # Write a message saying the page has been compiled.
7481 $messages = $messages . $information_prefix . "'" . $page_name_out . "' (" . $page_filename_out . ") was compiled." . "<br>";
7484 # Reset certain values.
7486 $page_autotitle = "";
7487 $page_autosection = "";
7488 $page_filename_seek = 0;
7489 $page_filename_dircount = 0;
7491 $page_filename_directory = "";
7492 $page_directory_path = "";
7493 $page_directory_name = "";
7494 @pagedirectories = ();
7498 # Write a message saying that the database has
7501 $messages = $messages . $information_prefix . "Finished compiling pages in the '" . $database_name_out . "' database..." . "<br>";
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>";
7514 } elsif ($confirm eq 0){
7516 # The action to compile the databases has
7517 # not been confirmed so check what type
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;
7530 my $database_filename_check;
7531 my $database_maxlength_check;
7532 my $databasefilename;
7533 my $databasefilename_out;
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
7550 kiriwrite_error("databasefilenameinvalid");
7554 if ($database_maxlength_check ne 0){
7556 # The database filename is too long, so
7559 kiriwrite_error("databasefilenametoolong");
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
7574 kiriwrite_error("databasemissingfile");
7578 if ($database_permissions ne 0){
7580 # The database permissions are invalid, so
7583 kiriwrite_error("databaseinvalidpermissions");
7587 # Load the SQLite database.
7589 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $kiriwrite_config{"directory_data_db"} . '/' . $databasefilename . '.db');
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>";
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;
7625 my $databasename_out;
7627 my $database_filename_check;
7628 my $database_maxlength_check;
7629 my $database_exists;
7630 my $database_permissions;
7631 my $database_count = 0;
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.
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
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.
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>";
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");
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>";
7724 # The type is something else other than
7725 # single or multiple, so return an error.
7727 kiriwrite_error("invalidvariable");
7733 # The confirm value is neither 0 or 1, so
7736 kiriwrite_error("invalidvariable");
7742 sub kiriwrite_compile_all{
7743 #################################################################################
7744 # kiriwrite_compile_all: Compile all of the databases in the database #
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);
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;
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.
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\">";
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");
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>";
7828 sub kiriwrite_selectedlist{
7829 #################################################################################
7830 # kiriwrite_page_selectedlist: Get the list of selected pages to use. #
7834 # kiriwrite_page_selectedlist(); #
7835 #################################################################################
7837 # Load the required Perl modules.
7839 my $query = new CGI;
7841 my $count = $query->param('count');
7843 # Check if the list of files has a value and if not set it 0.
7851 # Define some values for later.
7860 my $final_count = 0;
7863 # Get the list of filenames.
7867 # Get the values from id[]
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.
7882 # Get the values from name[]
7886 $selected = $query->param('name[' . $seek . ']');
7894 $selected_list[$seek] = $selected;
7896 } until ($seek eq $count || $count eq 0);
7898 # Create a final list of filenames to be used for
7905 # Check if the selected value is on and include
7906 # the filename in the final list.
7910 $selected = $selected_list[$seek];
7912 if ($selected eq "on"){
7914 $filename = $filename_list[$seek];
7915 $final_list[$final_count] = $filename;
7920 } until ($seek eq $count || $count eq 0);
7926 sub kiriwrite_compile_list{
7927 #################################################################################
7928 # kiriwrite_compile_list: Shows a list of databases that can be compiled. #
7932 # kiriwrite_compile_list(); #
7933 #################################################################################
7935 # Load the required Perl modules.
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");
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);
7959 # Declare the following variables that are going to be used before using
7960 # the foreach function.
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 = "";
7972 my $string_handle = "";
7974 my $table_style = 0;
7975 my $table_style_name = "";
7977 my $permissions_warning = 0;
7978 my $permissions_list = "";
7979 my $select_menu = "";
7980 my $invalid_warning = 0;
7981 my $invalid_list = "";
7983 # Begin creating the table for the list of databases.
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";
8005 # Load the SQLite database.
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,
8018 $string_handle->execute();
8019 @database_info = $string_handle->fetchrow_array();
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";
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";
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);
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";
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;
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){
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>";
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;
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;
8106 sub kiriwrite_compile_clean{
8107 #################################################################################
8108 # kiriwrite_compile_clean: Deletes the contents of the output directory. #
8112 # kiriwrite_compile_clean(confirm); #
8114 # confirm Confirms the deletion of files from the output directory. #
8115 #################################################################################
8117 # Get the values passed to the subroutine.
8121 # Define some variables for later.
8123 my $file_permissions;
8124 my $output_directory_exists;
8125 my $output_directory_permissions;
8126 my $warning_message;
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
8138 kiriwrite_error("outputdirectorymissing");
8142 # Check if the output directory has invalid
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");
8160 # The action to clean the output directory has been
8163 # Remove the list of files and directories from the
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.
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>";
8183 $pagedata = $pagedata . "The contents of the output directory have been removed.<br><br>";
8187 $pagedata = $pagedata . "<a href=\"kiriwrite.cgi?mode=compile\">Return to the compile databases list.</a>";
8193 # A value other than 1 is set for the confirm value
8194 # (which it shouldn't be) so return an error.
8196 kiriwrite_error("invalidvariable");
8202 # Print out a form for cleaning the output directory.
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>";
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). #
8224 # kiriwrite_compile_clean_helper(directory, removedirectory, [permissions]); #
8226 # directory Specifies the directory to remove files (and #
8227 # sub-directories) from. #
8228 # keepdirectory Keeps the directory itself after all files have been #
8230 # permissions Used recursively for error checking. #
8231 #################################################################################
8233 # Get the values passed to the subroutine.
8235 my ($directory, $directory_keep, $permissions) = @_;
8237 # Check if the directory_keep is only one charater long.
8239 my $directory_file = "";
8241 my $file_permissions = 0;
8244 # Check if the file permissions value is blank.
8248 # The file permissions value is blank.
8254 # Open the directory specified, read the contents of
8255 # the directory and then close the directory.
8257 opendir(DIRECTORY, $directory);
8258 @directory_list = readdir(DIRECTORY);
8259 closedir(DIRECTORY);
8261 # Remove each file and directory.
8263 foreach $directory_file (@directory_list){
8265 # Check if the filename is '.' or '..' and if it
8266 # is skip those files.
8268 if ($directory_file eq "." || $directory_file eq ".."){
8270 # The filename is '.' or '..' so skip processing
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.
8289 # Check if the filename is a directory.
8291 if (-d $directory . '/' . $directory_file){
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);
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);
8314 # Check if the directory should be kept.
8316 if ($directory_keep eq 1){
8318 # The directory_keep value is set as 1 so the directory
8319 # specified should be kept.
8321 } elsif ($directory_keep eq 0) {
8323 # The directory_keep value is set as 0 so remove the
8324 # directory specified.
8326 ($directory) = $directory =~ m/^([a-zA-Z0-9\/.]+)$/ig;
8331 # A value other than 0 or 1 was specified so return
8334 kiriwrite_error('invalidvalue');
8338 return $permissions;
8342 sub kiriwrite_settings_view{
8343 #################################################################################
8344 # kiriwrite_options_view: Writes out the list of options and variables. #
8348 # kiriwrite_settings_view(); #
8349 #################################################################################
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>";
8371 $pagedata = $pagedata . "The current settings being used are the following:";
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>";
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>";
8388 $pagedata = $pagedata . "To alter the current settings, select the Edit Settings option at the top of the page.";
8394 sub kiriwrite_settings_edit{
8395 #################################################################################
8396 # kiriwrite_options_edit: Edits the options. #
8400 # kiriwrite_options_edit(dbdirectory, outputdirectory, imagesuri, #
8401 # languagesystem, outputsystem, confirm); #
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.
8414 my ($settings_dbdirectory, $settings_outputdirectory, $settings_imagesuri, $settings_datetimeformat, $settings_languagesystem, $settings_outputsystem, $confirm) = @_;
8416 # Load the required Perl modules.
8418 my $xsl = XML::Simple->new();
8422 # If the confirm value is blank, then set the confirm
8429 if ($confirm eq "1"){
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.
8435 # Deinfe some variables for later.
8437 my @kiriwrite_new_settings;
8439 # Check the length of the directory names.
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);
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);
8456 if ($kiriwrite_dbdirectory_check eq 1){
8458 # The database directory name is blank, so return
8461 kiriwrite_error("dbdirectoryblank");
8463 } elsif ($kiriwrite_dbdirectory_check eq 2){
8465 # The database directory name is invalid, so return
8468 kiriwrite_error("dbdirectoryinvalid");
8472 if ($kiriwrite_outputdirectory_check eq 1){
8474 # The output directory name is blank, so return
8477 kiriwrite_error("outputdirectoryblank");
8479 } elsif ($kiriwrite_outputdirectory_check eq 2){
8481 # The output directory name is invalid, so return
8484 kiriwrite_error("outputdirectoryinvalid");
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.
8499 kiriwrite_output_xml("kiriwrite.xml", "config", @kiriwrite_new_settings);
8501 # Write a confirmation message.
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>";
8511 # Get the list of languages available.
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 = "";
8526 opendir(LANGUAGEDIR, "lang");
8527 @language_directory = grep /m*\.xml/, readdir(LANGUAGEDIR);
8528 closedir(LANGUAGEDIR);
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.
8534 $language_selectlist = "<select name=\"language\">";
8536 foreach $language_filename (@language_directory){
8538 # Load the language file currently selected.
8540 $language_file_xml = $xsl->XMLin("lang" . '/' . $language_filename, SuppressEmpty => 1);
8542 # Get the system name and the local name of the language.
8544 $language_file_localname = $language_file_xml -> {about} -> {name};
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.
8549 if ($language_file_localname eq ""){
8551 # The system name or the local name is blank so don't add
8552 # the language to the list.
8556 # Get the 'friendly' name of the language file name (basically
8557 # remove the .xml part from the filename.
8559 $language_file_length = length($language_filename);
8563 # Get a character from the language filename and currently
8564 # set by the seek counter.
8566 $language_file_char = substr($language_filename, $language_file_seek, 1);
8568 # Check if the character is a dot and if it is then set the
8569 # last dot value to the seek counter value.
8571 if ($language_file_char eq "."){
8573 # Current chatacter is a dot so set the last dot value
8574 # to what is currently the seek counter.
8576 $language_flie_dot = $language_file_seek;
8580 # Current character is not a dot, so do nothing.
8584 # Increment the seek counter.
8586 $language_file_seek++;
8588 } until ($language_file_seek eq $language_file_length);
8590 # Reset the seek counter.
8592 $language_file_seek = 0;
8594 # Process the file name again and this time process the file
8595 # name until it reaches the last dot found.
8599 # Get the character the seek counter is currently set at.
8601 $language_file_char = substr($language_filename, $language_file_seek, 1);
8603 # Append the character to the friendly file name.
8605 $language_file_friendly = $language_file_friendly . $language_file_char;
8607 # Increment the seek counter.
8609 $language_file_seek++;
8611 } until ($language_file_seek eq $language_flie_dot);
8613 # Append the language to the available languages list.
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>";
8618 # Reset certain counters and values before continuing.
8620 $language_file_seek = 0;
8621 $language_flie_dot = 0;
8622 $language_file_length = 0;
8623 $language_file_char = "";
8624 $language_file_friendly = "";
8630 $language_selectlist = $language_selectlist . "</select>";
8632 # Get the list of output systems available.
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"};
8646 # Open and get the list of output systems (perl modules) by filtering
8649 opendir(OUTPUTSYSTEMDIR, "Modules/Output");
8650 @outputsys_directory = grep /m*\.pm/, readdir(OUTPUTSYSTEMDIR);
8651 closedir(OUTPUTSYSTEMDIR);
8653 $outputsys_selectlist = "<select name=\"outputsys\">";
8655 # Process each output system and add them to the list of available
8658 foreach $outputsys_file (@outputsys_directory){
8660 # Get the length of the output system (perl module) filename.
8662 $outputsys_length = length($outputsys_file);
8664 # Get the friendly name of the Perl module (by getting rid of the
8665 # .pm part of the filename).
8669 $outputsys_char = substr($outputsys_file, $outputsys_seek, 1);
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.
8674 if ($outputsys_char eq "."){
8676 # Put the seek value as the last dot found number.
8678 $outputsys_dot = $outputsys_seek;
8682 # Increment the seek counter.
8686 } until ($outputsys_seek eq $outputsys_length);
8688 # Reset the seek counter as it is going to be used again.
8690 $outputsys_seek = 0;
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.
8697 # Get the character the seek counter is currently set at.
8699 $outputsys_char = substr($outputsys_file, $outputsys_seek, 1);
8701 # Append the character to the friendly name of the output system.
8703 $outputsys_friendly = $outputsys_friendly . $outputsys_char;
8705 # Increment the seek counter.
8709 } until ($outputsys_seek eq $outputsys_dot);
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){
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>";
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>";
8732 # Reset the following values.
8734 $outputsys_seek = 0;
8735 $outputsys_length = 0;
8736 $outputsys_char = "";
8737 $outputsys_friendly = "";
8742 $outputsys_selectlist = $outputsys_selectlist . "</select>";
8744 # Get the directory settings.
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");
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>";
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: / - ( ) :
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>";
8799 sub kiriwrite_settings_load{
8800 #################################################################################
8801 # kiriwrite_settings_load: Load the configuration settings into the global #
8806 # kiriwrite_settings_load(); #
8807 #################################################################################
8809 # Load the required Perl modules.
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");
8819 if ($kiriwrite_conf_exist eq 1){
8821 # The configuration really does not exist so return an critical error.
8823 kiriwrite_critical("configfilemissing");
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");
8842 # Converts the XML file into meaningful data for later on in this subroutine.
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).
8849 %kiriwrite_config = (
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},
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}
8861 # Do a validation check on all of the variables that were loaded into the global configuration hash.
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);
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.
8880 kiriwrite_critical("outputsystemmissing");
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");
8901 sub kiriwrite_variablecheck{
8902 #################################################################################
8903 # kiriwrite_variablecheck: Checks the variables for any invalid characters. #
8907 # kiriwrite_variablecheck(variablename, type, length, noerror); #
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 #
8916 #################################################################################
8918 # Get the values that were passed to the subroutine.
8920 my ($variable_data, $variable_type, $variable_option, $variable_noerror) = @_;
8922 if ($variable_type eq "numbers"){
8924 # Check for numbers and return an error if there is anything else than numebrs.
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.
8933 # The variable is not blank, so check if the no error value is set
8936 if ($variable_noerror eq 1){
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).
8944 } elsif ($variable_noerror eq 0) {
8946 # The validated variable is not blank and the noerror
8947 # value is set to 0.
8949 kiriwrite_error("invalidvariable");
8953 # The variable noerror value is something else
8954 # pther than 1 or 0. So return an error.
8956 kiriwrite_error("invalidvariable");
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.
8976 # The variable is not blank, so check if the no error value is set
8979 if ($variable_noerror eq 1){
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).
8987 } elsif ($variable_noerror eq 0) {
8989 # The validated variable is not blank and the noerror
8990 # value is set to 0.
8992 kiriwrite_error("invalidvariable");
8996 # The variable noerror value is something else
8997 # pther than 1 or 0. So return an error.
8999 kiriwrite_error("invalidvariable");
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.
9010 # Check if the variable_data string is blank, if it is then set the variable_data_length
9013 my $variable_data_length = 0;
9015 if (!$variable_data){
9017 # Set variable_data_length to '0'.
9018 $variable_data_length = 0;
9022 # Get the length of the variable recieved.
9023 $variable_data_length = length($variable_data);
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.
9034 if ($variable_noerror eq 1){
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).
9042 } elsif ($variable_noerror eq 0){
9044 # The no error value is set to 0, so return
9047 kiriwrite_error("variabletoolong");
9051 # The no error value is something else other
9052 # than 0 or 1, so return an error.
9054 kiriwrite_error("variabletoolong");
9060 # The variable length is exactly or shorter than specified, so continue to end of this section where
9061 # the return function should be.
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
9083 } elsif ($variable_noerror eq 0){
9085 # The no error value is set to 0, so return
9088 kiriwrite_error("blankvariable");
9092 # The no error value is something else other
9093 # than 0 or 1, so return an error.
9095 kiriwrite_error("invalidvariable");
9103 } elsif ($variable_type eq "filename"){
9104 # Check for letters and numbers, if anything else than letters and numbers is there (including spaces) return
9107 # Check if the filename passed is blank, if it is then return with an error.
9109 if ($variable_data eq ""){
9111 # The filename specified is blank, so check what the
9112 # noerror value is set.
9114 if ($variable_noerror eq 1){
9116 # The no error value is set to 1 so return
9117 # a value of 1 (meaning that the filename
9122 } elsif ($variable_noerror eq 0){
9124 # The no error value is set to 1 so return
9127 kiriwrite_error("blankfilename");
9131 # The no error value is something else other
9132 # than 0 or 1, so return an error.
9134 kiriwrite_error("invalidvariable");
9143 my $variable_data_validated = $variable_data;
9144 $variable_data_validated =~ tr/a-zA-Z0-9\.//d;
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.
9150 if ($variable_data_validated eq ""){
9152 # The validated data variable is blank, meaning that
9153 # it only contained letters and numbers.
9157 # The validated data variable is not blank, meaning
9158 # that it contains something else, so return an error
9161 if ($variable_noerror eq 1){
9163 # The no error value is set to 1 so return
9164 # an value of 2. (meaning that the filename
9170 } elsif ($variable_noerror eq 0){
9172 # The no error value is set to 0 so return
9175 kiriwrite_error("invalidfilename");
9179 # The no error value is something else other
9180 # than 0 or 1 so return an error.
9182 kiriwrite_error("invalidvariable");
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 ""){
9196 # The filename specified is blank, so check what the
9197 # noerror value is set.
9199 if ($variable_noerror eq 1){
9201 # The no error value is set to 1 so return
9202 # a value of 1 (meaning that the filename
9207 } elsif ($variable_noerror eq 0){
9209 # The no error value is set to 1 so return
9212 kiriwrite_error("blankfilename");
9216 # The no error value is something else other
9217 # than 0 or 1, so return an error.
9219 kiriwrite_error("invalidvariable");
9228 # Set the following variables for later on.
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.
9248 # Get a character from the filename passed to this subroutine.
9250 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
9252 # Check if the current character is the forward slash character.
9254 if ($variable_data_char eq "/"){
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.
9259 if ($variable_data_directorycurrent eq "" && $variable_data_firstlevel eq 1 || $variable_data_directorycurrent eq ".." || $variable_data_directorycurrent eq "."){
9261 # Check if the noerror value is set to 1, if it is return an
9262 # number, else return an proper error.
9264 if ($variable_noerror eq 1){
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
9273 } elsif ($variable_noerror eq 0) {
9275 # Page filename contains invalid characters and
9276 # the no error value is set to 0 so return an
9279 kiriwrite_error("invalidfilename");
9283 # The no error value is something else other
9284 # than 0 or 1 so return an error.
9286 kiriwrite_error("invalidvariable");
9292 # Append the forward slash, clear the current directory name and set
9293 # the first directory level value to 0.
9295 $variable_data_directory = $variable_data_directory . $variable_data_char;
9296 $variable_data_directorycurrent = "";
9297 $variable_data_firstlevel = 0;
9301 # Append the current character to the directory name and to the current
9304 $variable_data_directory = $variable_data_directory . $variable_data_char;
9305 $variable_data_directorycurrent = $variable_data_directorycurrent . $variable_data_char;
9309 # Increment the seek counter.
9311 $variable_data_seek++;
9313 } until ($variable_data_seek eq $variable_data_length);
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){
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).
9330 } elsif ($variable_noerror eq 0){
9332 # The no error value is set to 1 so return
9335 kiriwrite_error("blankdatetimeformat");
9339 # The no error value is something else other
9340 # than 0 or 1, so return an error.
9342 kiriwrite_error("invalidvariable");
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
9358 # The validated data variable is not blank, meaning
9359 # that it contains something else, so return an error
9362 if ($variable_noerror eq 1){
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).
9370 } elsif ($variable_noerror eq 0){
9372 # The no error value is set to 0 so return
9375 kiriwrite_error("invaliddatetimeformat");
9379 # The no error value is something else other
9380 # than 0 or 1 so return an error.
9382 kiriwrite_error("invalidvariable");
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){
9401 # The no error value is set to 1 so return
9402 # a value of 1 (meaning that the directory
9407 } elsif ($variable_noerror eq 0){
9409 # The no error value is set to 1 so return
9412 kiriwrite_error("blankdirectory");
9416 # The no error value is something else other
9417 # than 0 or 1, so return an error.
9419 kiriwrite_error("invalidvariable");
9425 if ($variable_data_validated eq ""){
9427 # The validated data variable is blank, meaning that
9428 # it only contains letters and numbers.
9432 # The validated data variable is not blank, meaning
9433 # that it contains something else, so return an error
9436 if ($variable_noerror eq 1){
9438 # The no error value is set to 1 so return
9439 # an value of 2. (meaning that the directory
9444 } elsif ($variable_noerror eq 0){
9446 # The no error value is set to 0 so return
9449 kiriwrite_error("invaliddirectory");
9453 # The no error value is something else other
9454 # than 0 or 1 so return an error.
9456 kiriwrite_error("invalidvariable");
9464 } elsif ($variable_type eq "language_filename"){
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).
9470 if ($variable_data eq ""){
9472 # The language filename is blank so check the
9473 # no error value and return an error (or value).
9475 if ($variable_noerror eq 1){
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).
9483 } elsif ($variable_noerror eq 0) {
9485 # Language filename is blank and the no error value
9486 # is not set as 1, so return an error.
9488 kiriwrite_error("emptylanguagefilename");
9492 # The noerror value is something else other
9493 # than 0 or 1 so return an error.
9495 kiriwrite_error("invalidvariable");
9501 # Set the following variables for later on.
9503 my $variable_data_length = 0;
9504 my $variable_data_char = "";
9505 my $variable_data_seek = 0;
9507 # Get the length of the language file name.
9509 $variable_data_length = length($variable_data);
9513 # Get a character from the language filename passed to this
9514 # subroutine and the character the seek counter value is set
9517 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
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
9523 if ($variable_data_char eq "/" || $variable_data_char eq "."){
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.
9529 if ($variable_noerror eq 1){
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
9538 } elsif ($variable_noerror eq 0) {
9540 # Language filename contains a forward slash and the no
9541 # error value has not been set to 1, so return an error.
9543 kiriwrite_error("invalidlanguagefilename");
9547 # The noerror value is something else other than
9548 # 1 or 0 so return an error.
9550 kiriwrite_error("invalidvariable");
9556 # Increment the seek counter.
9558 $variable_data_seek++;
9560 } until ($variable_data_seek eq $variable_data_length);
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.
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){
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
9588 } elsif ($variable_noerror eq 0) {
9590 # Page setting is invalid and the no error value
9591 # is not 1, so return an error.
9593 kiriwrite_error("invalidvariable");
9597 # The no error value is something else other
9598 # than 0 or 1 so return an error.
9600 kiriwrite_error("invalidvariable");
9608 } elsif ($variable_type eq "page_filename"){
9610 # The variable type is a page filename type. Check
9611 # if the data is empty and if it is then return an
9614 if ($variable_data eq ""){
9616 # The filename is blank so check the no error
9617 # value and depending on it return an value
9620 if ($variable_noerror eq 1){
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).
9628 } elsif ($variable_noerror eq 0) {
9630 # Page filename is blank and the no error value
9631 # is not 1, so return an error.
9633 kiriwrite_error("emptypagefilename");
9637 # The no error value is something else other
9638 # than 0 or 1 so return an error.
9640 kiriwrite_error("invalidvariable");
9645 # Set the following variables for later on.
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;
9655 # Get the length of the filename.
9657 $variable_data_length = length($variable_data);
9659 # Check that only valid characters should be appearing in
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
9671 kiriwrite_error("invalidfilename");
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.
9681 # Get a character from the filename passed to this subroutine.
9683 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
9685 # Check if the current character is the forward slash character.
9687 if ($variable_data_char eq "/"){
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.
9692 if ($variable_data_directorycurrent eq "" && $variable_data_firstlevel eq 1 || $variable_data_directorycurrent eq ".." || $variable_data_directorycurrent eq "."){
9694 # Check if the noerror value is set to 1, if it is return an
9695 # number, else return an proper error.
9697 if ($variable_noerror eq 1){
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
9706 } elsif ($variable_noerror eq 0) {
9708 # Page filename contains invalid characters and
9709 # the no error value is set to 0 so return an
9712 kiriwrite_error("invalidfilename");
9716 # The no error value is something else other
9717 # than 0 or 1 so return an error.
9719 kiriwrite_error("invalidvariable");
9725 # Append the forward slash, clear the current directory name and set
9726 # the first directory level value to 0.
9728 $variable_data_directory = $variable_data_directory . $variable_data_char;
9729 $variable_data_directorycurrent = "";
9730 $variable_data_firstlevel = 0;
9734 # Append the current character to the directory name and to the current
9737 $variable_data_directory = $variable_data_directory . $variable_data_char;
9738 $variable_data_directorycurrent = $variable_data_directorycurrent . $variable_data_char;
9742 # Increment the seek counter.
9744 $variable_data_seek++;
9746 } until ($variable_data_seek eq $variable_data_length);
9750 } elsif ($variable_type eq "outputmodule"){
9752 # The variable type is a output module filename.
9754 # Check if the variable_data is blank and if it is
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
9767 my $variable_data_validated = $variable_data;
9768 $variable_data_validated =~ tr/a-zA-Z0-9//d;
9770 if ($variable_data_validated eq ""){
9774 kiriwrite_error("outputmoduleinvalid");
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
9797 # The UTF-8 string is valid.
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
9812 } elsif ($variable_noerror eq 0) {
9814 # The no error value has been set to 0, so return
9817 kiriwrite_error("invalidutf8");
9821 # The no error value is something else other than 0
9822 # or 1, so return an error.
9824 kiriwrite_error("invalidoption");
9833 # Another type than the valid ones above has been specified so return an error specifying an invalid option.
9834 kiriwrite_error("invalidoption");
9839 sub kiriwrite_convert{
9840 #################################################################################
9841 # kiriwrite_convert: Converts certain characters. #
9845 # kiriwrite_convert(data, type); #
9847 # data Specifies the data to be used with #
9848 # type The type of conversion to make (convert from or to kiriwrite #
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.
9862 } elsif ($type eq "normal_display"){
9864 # Convert into a viewable format.
9866 $data =~ s/&/&/g;
9867 $data =~ s/\"/"/g;
9869 $data =~ s/>/>/g;
9870 $data =~ s/</</g;
9874 } elsif ($type eq "normal_out"){
9876 # Convert into the original format.
9881 } elsif ($type eq "date"){
9883 # Convert the date given into the proper date.
9885 # Create the following varialbes to be used later.
9912 # Split the date and time.
9914 $length = length($data);
9920 # Get the character and check if it is a space.
9922 $char = substr($data, $seek, 1);
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);
9936 } until ($seek eq $length);
9938 # Get the year, month and date.
9940 $length = length($date);
9945 # Get the character and check if it is a dash.
9947 $char = substr($date, $seek, 1);
9951 # The character is a dash, so get the year, month or day.
9953 $datelength = $seek - $startchar;
9957 # Get the year from the date.
9959 $year = substr($date, 0, $datelength) + 1900;
9963 # Get the last two characters to get the short year
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.
9979 $month_full = '0' . $month;
9983 $month_full = $month;
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.
9998 $day_full = '0' . $day;
10012 } until ($seek eq $length);
10014 # Get the length of the time value and reset certain
10017 $length = length($time);
10024 # Get the character and check if it is a colon.
10026 $char = substr($time, $seek, 1);
10030 # The character is a colon, so get the hour, minute and day.
10032 $timelength = $seek - $startchar;
10036 # Get the hour from the time.
10038 $hour = substr($time, 0, $timelength);
10040 $startchar = $seek;
10042 # If the hour is less than ten then add a
10047 $hour_full = '0' . $hour;
10051 $hour_full = $hour;
10055 } elsif ($count eq 1){
10057 # Get the minute and second from the time.
10059 $minute = substr($time, $startchar + 1, $timelength - 1);
10062 # If the minute is less than ten then add a
10067 $minute_full = '0' . $minute;
10071 $minute_full = $minute;
10075 $startchar = $seek;
10077 $secondlength = $length - $seek + 1;
10078 $second = substr($time, $startchar + 1, $secondlength);
10080 # If the second is less than ten then add a
10085 $second_full = '0' . $second;
10089 $second_full = $second;
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;
10125 kiriwrite_error("invalidoption");
10132 sub kiriwrite_output_header{
10133 #################################################################################
10134 # kiriwrite_output_header: Outputs the header to the browser/stdout/console. #
10138 # kiriwrite_output_header(mimetype); #
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";
10149 print header(-Expires=>'Sunday, 01-Jan-06 00:00:00 GMT', -charset=>'utf-8');
10153 sub kiriwrite_processfilename{
10154 #################################################################################
10155 # kiriwrite_processfilename: Processes a name and turns it into a filename that #
10156 # can be used by Kiriwrite. #
10160 # kiriwrite_processfilename(text); #
10162 # text Specifies the text to be used in the process for creating a new #
10164 #################################################################################
10166 # Get the values that have been passed to the subroutine.
10168 my ($process_text) = @_;
10170 # Define some variables that will be used later on.
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 = "";
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.
10184 $processed_stageone = $process_text;
10185 $processed_stageone =~ tr#a-zA-Z0-9##cd;
10186 $processed_stageone =~ tr/A-Z/a-z/;
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.
10192 $processed_stagetwo = $processed_stageone;
10193 $processed_length = length($processed_stagetwo);
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.
10201 # Get the character that is the seek counter
10204 $processed_char = substr($processed_stagetwo, $processed_seek, 1);
10206 # Append to the final processed filename.
10208 $processed_filename = $processed_filename . $processed_char;
10210 # Increment the seek counter.
10214 } until ($processed_seek eq 64 || $processed_seek eq $processed_length);
10216 return $processed_filename;
10220 sub kiriwrite_error{
10221 #################################################################################
10222 # kiriwrite_error: Prints out an error message. #
10226 # kiriwrite_error(errortype); #
10228 # errortype Specifies the type of error that occured. #
10229 #################################################################################
10231 # Get the error type from the subroutine.
10233 my ($error_type) = @_;
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.",
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";
10353 my $pagedata = "<div class=\"errorbox\"><font class=\"errorheader\">Error!</font><br /><font class=\"errortext\">" . $kiriwrite_error{$error_type} . "</font></div>";
10355 kiriwrite_output_header;
10356 kiriwrite_output_page("Error!", $pagedata, "none");
10362 sub kiriwrite_get_templates{
10363 #################################################################################
10364 # kiriwrite_get_templates: Get the list of templates available (and valid) from #
10365 # the template directory. #
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
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;
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++;
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. #
10448 # kiriwrite_fileexists(filename); #
10450 # filename Specifies the file name to check if it exists or not. #
10451 #################################################################################
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.
10462 # Specified file does exist so return a value of 1.
10468 # Specified file does not exist so return a value of 0.
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. #
10484 # kiriwrite_filepermissions(filename, [read], [write], [filemissingskip]); #
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){
10513 # The file exists so the checks are to be done.
10515 $ignorechecks_result = 0;
10519 # The file does not exist so the checks don't need to
10520 # be done to prevent false positives.
10522 $ignorechecks_result = 1;
10528 $ignorechecks_result = 0;
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.
10541 # The file is readable, so do nothing.
10545 # The file is not readable, so return 1.
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.
10562 # The file is writeable, so do nothing.
10566 # The file is not writeable, so return 1.
10574 # No problems have occured, so 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. #
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.
10598 # Convert the string.
10600 my $finalutf8 = Encode::decode_utf8( $utfstring );
10606 sub kiriwrite_critical{
10607 #################################################################################
10608 # kiriwrite_critical: Displays a critical error message that cannot be #
10609 # normally by the kiriwrite_error subroutine. #
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.
10628 print "Critical Error: The Kiriwrite configuration file is missing! Running the setup script for Kiriwrite is recommended.";
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
10638 print "Critical Error: The Kiriwrite configuration file contains invalid permission settings! Please set the valid permission settings for the configuration file.";
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.
10647 print "Critical Error: The output system module is missing! Running the setup script for Kiriwrite is recommended.";
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.
10656 print "Critical Error: The output system module contains invalid permission settings! Please set the valid permission settings for the output module.";
10661 # The error type is unspecified, so return a generic error message.
10664 print "Critical Error: An unspecified critical error has occured.";
10671 sub kiriwrite_output_page{
10672 #################################################################################
10673 # kiriwrite_output_page: Outputs the page to the browser/stdout/console. #
10677 # kiriwrite_output_page(pagetitle, pagedata, menutype); #
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);
10693 # Define the variables required.
10695 my $scriptpageline = "";
10696 my $pageoutput = "";
10697 my $menuoutput = "";
10699 # Print out the main menu for Kiriwrite.
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 />";
10703 # Check what menu is going to be printed along with the default 'top' menu.
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>";
10709 } elsif ($menutype eq "pages"){
10710 # If the menu type is pages then print out the pages sub-menu.
10712 # First, fetch the database name from the HTTP query string.
10714 my $query = new CGI;
10715 my $db_filename = $query->param('database');
10717 # Check if a value has been placed in the db_filename string.
10719 if (!$db_filename){
10721 # As the database filename is blank, don't add an option to add a page.
10725 # A database file has been specified so add an option to add a page to
10726 # the selected database.
10728 $menuoutput = $menuoutput . "<a href=\"kiriwrite.cgi?mode=page&action=add&database=" . $db_filename . "\">Add Page</a>";
10732 } elsif ($menutype eq "filter"){
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>";
10737 } elsif ($menutype eq "settings"){
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>";
10742 } elsif ($menutype eq "template"){
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>";
10747 } elsif ($menutype eq "compile"){
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>";
10754 # Find <kiriwrite> tages and replace with the apporiate variables.
10756 foreach $scriptpageline (@scriptpage){
10758 $scriptpageline =~ s/<kiriwrite:menu>/$menuoutput/g;
10759 $scriptpageline =~ s/<kiriwrite:pagedata>/$pagedata/g;
10762 # Check if page title specified is blank, otherwise add a page title
10765 if ($pagetitle eq ""){
10766 $scriptpageline =~ s/<kiriwrite:title>//g;
10768 $scriptpageline =~ s/<kiriwrite:title>/ ($pagetitle)/g;
10771 # Append processed line to the pageoutput variable.
10773 $pageoutput = $pageoutput . $scriptpageline;
10777 # Print out the page to the browser/console/stdout.
10785 sub kiriwrite_output_xml{
10786 #################################################################################
10787 # kiriwrite_output_xml: Outputs several types of data to an XML file #
10791 # kiriwrite_output_xml(filename, type, data); #
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) = @_;
10802 # Check if filename is blank, if it is then return an error.
10804 if ($xml_filename eq ""){
10805 # Filename is blank, return an error.
10806 kiriwrite_error("blankfilename");
10809 # Validate the XML filename to make sure nothing supicious is being passed.
10811 kiriwrite_variablecheck($xml_filename, "maxlength", 64, 0);
10812 kiriwrite_variablecheck($xml_filename, "filename", "", 0);
10814 # Check what type of XML data to output.
10816 if ($xml_type eq "config") {
10818 # The type of XML data to output is a Kiriwrite configuration file.
10820 # Get the data that has been passed to xml_data from the subroutine and
10821 # split them into seperate values.
10823 my ($xml_config_databasedir, $xml_config_outputdir, $xml_config_imagesuri, $xml_config_datetime, $xml_config_systemlanguage, $xml_config_outputsystem) = @newsettings;
10825 # Create the XML data layout.
10827 my $xmldata = "<?xml version=\"1.0\"?>\r\n\r\n<kiriwrite-config>\r\n";
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";
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";
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";
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";
10846 $xmldata = $xmldata . "\t</settings>\r\n";
10848 $xmldata = $xmldata . "</kiriwrite-config>";
10850 # Open the Kiriwrite XML configuration file and write the new settings to the
10851 # configuration file.
10853 open(XMLCONFIG, "> kiriwrite.xml");
10854 print XMLCONFIG $xmldata;
10859 # The type of XML data is something else that is not supported by
10860 # Kiriwrite, so return an error.
10862 kiriwrite_error("invalidoption");
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');
10890 if ($http_query_mode eq "db"){
10892 # If mode is 'db' (database), then check what action is required.
10894 if ($query->param('action')){
10895 # An action has been specified, so find out what action has been specified.
10897 my $http_query_action = $query->param('action');
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.
10903 if ($query->param('database')){
10904 # If there is a value in the database variable check if it is a valid database. Otherwise,
10907 my $http_query_database = $query->param('database');
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.
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.
10916 my $http_query_confirm = $query->param('confirm');
10918 if ($http_query_confirm eq "yes"){
10919 # Value is correct, collect the variables to pass onto the database variable.
10921 # Load the XML::Simple module.
10924 my $xsl = XML::Simple->new();
10926 # Get the variables from the HTTP query.
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.
10940 # Pass the variables to the database editing subroutine.
10942 my $pagedata = kiriwrite_database_edit($databaseshortname, $databasename, $databasedescription, $newdatabasefilename, $newdatabasename, $newdatabasedescription, $databasenotes, $databasecategories, 1);
10944 kiriwrite_output_header;
10945 kiriwrite_output_page("Database Edited", $pagedata, "database");
10949 # Value is incorrect, return and error.
10950 kiriwrite_error("invalidvariable");
10955 # Display the form for editing an database.
10956 my $pagedata = kiriwrite_database_edit($http_query_database);
10958 kiriwrite_output_header;
10959 kiriwrite_output_page("Edit Database", $pagedata, "database");
10964 # If there is no value in the database variable, then return an error.
10965 kiriwrite_error("invalidvariable");
10969 } elsif ($http_query_action eq "delete"){
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.
10974 if ($query->param('confirm')){
10976 # User has confirmed to delete a database, pass the parameters to the kiriwrite_database_delete
10979 my $database_filename = $query->param('database');
10980 my $database_confirm = $query->param('confirm');
10981 my $pagedata = kiriwrite_database_delete($database_filename, $database_confirm);
10983 kiriwrite_output_header;
10984 kiriwrite_output_page("Delete Database", $pagedata, "database");
10990 # User has clicked on the delete link (thus hasn't confirmed the action to delete a database).
10992 my $database_filename = $query->param('database');
10993 my $pagedata = kiriwrite_database_delete($database_filename);
10995 kiriwrite_output_header;
10996 kiriwrite_output_page("Delete Database", $pagedata, "database");
11000 } elsif ($http_query_action eq "new"){
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
11006 my $http_query_confirm = $query->param('confirm');
11008 # Check if the confirm value is correct.
11010 if ($http_query_confirm){
11011 if ($http_query_confirm eq 1){
11013 # User has confirmed to create a database, pass the parameters to the
11014 # kiriwrite_database_add subroutine.
11016 my $http_query_confirm = $query->param('confirm');
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');
11024 my $pagedata = kiriwrite_database_add($database_filename, $database_name, $database_description, $database_notes, $database_categories, $http_query_confirm);
11026 kiriwrite_output_header;
11027 kiriwrite_output_page("Add Database", $pagedata, "database");
11032 # The confirm value is something else other than 1 (which it shouldn't be), so
11038 # User has clicked on the 'Add Database' link.
11040 my $pagedata = kiriwrite_database_add;
11042 kiriwrite_output_header;
11043 kiriwrite_output_page("Add Database", $pagedata, "database");
11047 # Another option has been specified, so return an error.
11049 kiriwrite_error("invalidaction");
11053 # No action has been specified, do the default action of displaying a list
11056 my $pagedata = kiriwrite_database_list;
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.
11062 } elsif ($http_query_mode eq "page"){
11064 # If mode is 'page', then check what action is required.
11066 if ($query->param('action')){
11067 my $http_query_action = $query->param('action');
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.
11072 if ($http_query_action eq "view"){
11074 # The action selected was to view pages from a database,
11076 my $database_name = $query->param('database');
11077 my $pagedata = kiriwrite_page_list($database_name);
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.
11083 } elsif ($http_query_action eq "add"){
11085 # The action selected was to add a page to the selected database.
11087 my $http_query_confirm = $query->param('confirm');
11089 if (!$http_query_confirm){
11091 $http_query_confirm = 0;
11095 if ($http_query_confirm eq 1){
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');
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);
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.
11114 my $http_query_database = $query->param('database');
11115 my $pagedata = kiriwrite_page_add($http_query_database);
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.
11121 } elsif ($http_query_action eq "edit"){
11123 # The action selected was to edit a page from a database.
11125 my $http_query_confirm = $query->param('confirm');
11127 if (!$http_query_confirm){
11129 $http_query_confirm = 0;
11133 if ($http_query_confirm eq 1){
11135 # Get the needed values from the HTTP query.
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');
11147 # Pass the values to the editing pages subroutine.
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);
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.
11157 # Get the needed values from the HTTP query.
11159 my $http_query_database = $query->param('database');
11160 my $http_query_filename = $query->param('page');
11162 # Pass the values to the editing pages subroutine.
11164 my $pagedata = kiriwrite_page_edit($http_query_database, $http_query_filename);
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.
11171 } elsif ($http_query_action eq "delete"){
11173 # The action selected was to delete a page from a database.
11175 my $http_query_database = $query->param('database');
11176 my $http_query_page = $query->param('page');
11177 my $http_query_confirm = $query->param('confirm');
11180 my $selectionlist = "";
11182 if ($http_query_confirm){
11184 # The action has been confirmed, so try to delete the selected page
11185 # from the database.
11187 $pagedata = kiriwrite_page_delete($http_query_database, $http_query_page, $http_query_confirm);
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.
11195 $pagedata = kiriwrite_page_delete($http_query_database, $http_query_page);
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.
11202 } elsif ($http_query_action eq "multidelete"){
11204 # The action selected was to delete multiple pages from a
11207 my $http_query_database = $query->param('database');
11208 my $http_query_confirm = $query->param('confirm');
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.
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
11242 my $http_query_database = $query->param('database');
11243 my $http_query_newdatabase = $query->param('newdatabase');
11244 my $http_query_confirm = $query->param('confirm');
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.
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
11278 my $http_query_database = $query->param('database');
11279 my $http_query_newdatabase = $query->param('newdatabase');
11280 my $http_query_confirm = $query->param('confirm');
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.
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
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');
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.
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.
11348 kiriwrite_error("invalidaction");
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.
11356 my $pagedata = kiriwrite_page_list;
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.
11364 # No action has been specified, do the default action of listing pages from
11365 # the first database found in the directory.
11367 } elsif ($http_query_mode eq "filter"){
11369 # If there's a value for action in the HTTP query, then
11370 # get that value that is in the HTTP query.
11372 if ($query->param('action')){
11374 # There is a value for action in the HTTP query,
11375 # so get the value from it.
11377 my $http_query_action = $query->param('action');
11379 if ($http_query_action eq "add"){
11381 # The action the user requested is to add a filter to the
11382 # filters database.
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.
11388 my $http_query_confirm = $query->param('confirm');
11390 if ($http_query_confirm){
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.
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');
11401 my $pagedata = kiriwrite_filter_add($http_query_findwords, $http_query_replacewords, $http_query_priority, $http_query_notes, $http_query_confirm);
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.
11409 my $pagedata = kiriwrite_filter_add();
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.
11415 } elsif ($http_query_action eq "edit"){
11417 # The action the user requested is to edit an filter from
11418 # the filters database.
11420 my $http_query_number = $query->param('filter');
11421 my $http_query_confirm = $query->param('confirm');
11423 if ($http_query_confirm){
11425 # There is a value in http_query_confirm, so pass on the
11426 # new find and replace words so that the filters database
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');
11434 my $pagedata = kiriwrite_filter_edit($http_query_number, $http_query_findwords, $http_query_replacewords, $http_query_priority, $http_query_notes, $http_query_confirm);
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.
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.
11448 } elsif ($http_query_action eq "delete"){
11450 # The action the user requested is to delete an filter
11451 # from the filters database.
11453 my $http_query_number = $query->param('filter');
11454 my $http_query_confirm = $query->param('confirm');
11456 if ($http_query_confirm){
11458 my $pagedata = kiriwrite_filter_delete($http_query_number, $http_query_confirm);
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
11466 my $pagedata = kiriwrite_filter_delete($http_query_number);
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.
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
11479 kiriwrite_error("invalidaction");
11485 my $pagedata = kiriwrite_filter_list();
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.
11495 } elsif ($http_query_mode eq "template"){
11497 # Check if an action has been specified in the HTTP query.
11499 if ($query->param('action')){
11501 # An action has been specified in the HTTP query.
11503 my $http_query_action = $query->param('action');
11505 if ($http_query_action eq "delete"){
11506 # Get the required parameters from the HTTP query.
11508 my $http_query_template = $query->param('template');
11509 my $http_query_confirm = $query->param('confirm');
11511 # Check if a value for confirm has been specified (it shouldn't)
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.
11519 my $pagedata = kiriwrite_template_delete($http_query_template);
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.
11527 my $pagedata = kiriwrite_template_delete($http_query_template, $http_query_confirm);
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.
11535 } elsif ($http_query_action eq "add") {
11537 # Get the variables from the HTTP query in preperation for processing.
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');
11545 # Check if there is a confirmed value in the http_query_confirm variable.
11547 if (!$http_query_confirm){
11549 # Since there is no confirm value, print out a form for creating a new
11552 my $pagedata = kiriwrite_template_add();
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.
11560 # A value in the http_query_confirm value is specified, so pass the
11561 # variables onto the kiriwrite_template_add subroutine.
11563 my $pagedata = kiriwrite_template_add($http_query_templatefilename, $http_query_templatename, $http_query_templatedescription, $http_query_templatelayout, $http_query_confirm);
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.
11571 } elsif ($http_query_action eq "edit") {
11573 # Get the required parameters from the HTTP query.
11575 my $http_query_templatefile = $query->param('template');
11576 my $http_query_confirm = $query->param('confirm');
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.
11581 if (!$http_query_confirm){
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
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.
11592 } elsif ($http_query_confirm eq 1) {
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.
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');
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.
11610 # Another confirm value is there instead of '0' or '1'. Return
11611 # an error saying it is invalid.
11613 kiriwrite_error("invalidvariable");
11619 # Another action was specified and was not one of the ones above, so
11622 kiriwrite_error("invalidaction");
11628 # If the action option is left blank, then print out a form where the list
11629 # of templates are available.
11631 my $pagedata = kiriwrite_template_list;
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.
11639 } elsif ($http_query_mode eq "compile"){
11641 # The mode selected is to compile pages from databases.
11643 # If the action option is left blank, then print out a form where the list
11644 # of databases to compile are available.
11646 if ($query->param('action')){
11648 my $http_query_action = $query->param('action');
11650 if ($http_query_action eq "compile"){
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');
11658 # If it is blank, set the confirm value to 0.
11660 if (!$http_query_confirm){
11662 # The http_query_confirm variable is uninitalised, so place a
11663 # '0' (meaning an unconfirmed action).
11665 $http_query_confirm = 0;
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");
11679 if ($http_query_type eq "multiple"){
11681 if ($http_query_confirm eq 1){
11683 # The action to compile the pages has been confirmed so
11684 # compile the pages.
11686 my @selectedlist = kiriwrite_selectedlist();
11687 my $pagedata = kiriwrite_compile_makepages($http_query_type, $http_query_confirm, @selectedlist);
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.
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.
11699 my @selectedlist = kiriwrite_selectedlist();
11700 my $pagedata = kiriwrite_compile_makepages($http_query_type, $http_query_confirm, @selectedlist);
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.
11708 } elsif ($http_query_type eq "single"){
11710 my $http_query_database = $query->param('database');
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.
11721 kiriwrite_error("invalidcompiletype");
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;
11742 if ($http_query_confirm eq 1){
11744 # The action to compile all the databases has been confirmed.
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");
11754 } elsif ($http_query_action eq "clean") {
11756 # The selected action is to clean the output directory.
11757 # Check if the action to clean the output directory
11758 # has been confirmed.
11760 my $http_query_confirm = $query->param('confirm');
11762 if (!$http_query_confirm){
11764 # The http_query_confirm variable is uninitalised, so place a
11765 # '0' (meaning an unconfirmed action).
11767 $http_query_confirm = 0;
11771 if ($http_query_confirm eq 1){
11773 # The action to clean the output directory has been confirmed.
11775 my $pagedata = kiriwrite_compile_clean($http_query_confirm);
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.
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.
11787 my $pagedata = kiriwrite_compile_clean();
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.
11795 # The action specified was something else other than those
11796 # above, so return an error.
11798 kiriwrite_error("invalidaction");
11803 my $pagedata = kiriwrite_compile_list;
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.
11809 } elsif ($http_query_mode eq "settings"){
11811 # The mode selected is view (and change settings).
11813 # If the action value has been left blank, then view the list of
11814 # current settings.
11816 if ($query->param('action')){
11817 my $http_query_action = $query->param('action');
11819 if ($http_query_action eq "edit"){
11821 # The action specified is to edit the settings. Check if the action
11822 # to edit the settings has been confirmed.
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;
11834 if ($http_query_confirm eq 1){
11836 # The action to edit the settings has been confirmed. Get the
11837 # required settings from the HTTP query.
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');
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);
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.
11854 # The action to edit the settings has not been confirmed.
11856 my $pagedata = kiriwrite_settings_edit;
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.
11864 # The action specified was something else other than those
11865 # above, so return an error.
11867 kiriwrite_error("invalidaction");
11873 # No action has been specified, so print out the list of settings currently being used.
11875 my $pagedata = kiriwrite_settings_view;
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.
11882 # Another mode has been specified than the ones above, so return an error saying that
11883 # an invalid option was specified.
11885 kiriwrite_error("invalidmode");
11890 # No mode has been specified, so print the default "first-run" view of the
11893 my $pagedata = kiriwrite_database_list;
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.
11909 Web-based webpage compiler.
11913 Steve Brokenshire <sbrokenshire@xestia.co.uk>
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).