1 package Modules::System::Common;
8 our @ISA = qw(Exporter);
9 our @EXPORT = qw(kiriwrite_initalise kiriwrite_settings_load kiriwrite_fileexists kiriwrite_filepermissions kiriwrite_output_header kiriwrite_output_page kiriwrite_variablecheck kiriwrite_processconfig kiriwrite_processfilename kiriwrite_utf8convert kiriwrite_language kiriwrite_error kiriwrite_selectedlist);
11 sub kiriwrite_selectedlist{
12 #################################################################################
13 # kiriwrite_page_selectedlist: Get the list of selected pages to use. #
17 # kiriwrite_page_selectedlist(); #
18 #################################################################################
20 my $form_data = shift;
22 my $count = $form_data->{'count'};
24 # Check if the list of files has a value and if not set it 0.
32 # Define some values for later.
44 # Get the list of filenames.
48 # Get the values from id[]
52 $filename = $form_data->{'id[' . $seek . ']'};
53 $filename_list[$seek] = $filename;
55 } until ($seek eq $count || $count eq 0);
57 # Get the list of selected filenames.
63 # Get the values from name[]
67 $selected = $form_data->{'name[' . $seek . ']'};
75 $selected_list[$seek] = $selected;
77 } until ($seek eq $count || $count eq 0);
79 # Create a final list of filenames to be used for
86 # Check if the selected value is on and include
87 # the filename in the final list.
91 $selected = $selected_list[$seek];
93 if ($selected eq "on"){
95 $filename = $filename_list[$seek];
96 $final_list[$final_count] = $filename;
101 } until ($seek eq $count || $count eq 0);
107 sub kiriwrite_initalise{
108 #################################################################################
109 # kiriwrite_initalise: Get the enviroment stuff. #
110 #################################################################################
112 # Get the script filename.
114 my $env_script_name = $ENV{'SCRIPT_NAME'};
116 # Process the script filename until there is only the
119 my $env_script_name_length = length($env_script_name);
120 my $filename_seek = 0;
121 my $filename_char = "";
122 my $filename_last = 0;
125 $filename_char = substr($env_script_name, $filename_seek, 1);
126 if ($filename_char eq "/"){
127 $filename_last = $filename_seek + 1;
130 } until ($filename_seek eq $env_script_name_length || $env_script_name_length eq 0);
132 my $env_script_name_finallength = $env_script_name_length - $filename_last;
133 my $script_filename = substr($env_script_name, $filename_last, $env_script_name_finallength);
135 # Setup the needed enviroment variables for Kiriwrite.
137 %main::kiriwrite_env = (
138 "script_filename" => $script_filename,
143 sub kiriwrite_settings_load{
144 #################################################################################
145 # kiriwrite_settings_load: Load the configuration settings into the global #
150 # kiriwrite_settings_load(); #
151 #################################################################################
153 # Check if the Kiriwrite configuration file exists before using it and
154 # return an critical error if it doesn't exist.
156 my ($kiriwrite_settingsfilehandle, @config_data, %config, $config);
157 my $kiriwrite_conf_exist = kiriwrite_fileexists("kiriwrite.cfg");
159 if ($kiriwrite_conf_exist eq 1){
161 # The configuration really does not exist so return an critical error.
163 kiriwrite_critical("configfilemissing");
167 # Check if the Kiriwrite configuration file has valid permission settings
168 # before using it and return an critical error if it doesn't have the
169 # valid permission settings.
171 my $kiriwrite_conf_permissions = kiriwrite_filepermissions("kiriwrite.cfg", 1, 0);
173 if ($kiriwrite_conf_permissions eq 1){
175 # The permission settings for the Kiriwrite configuration file are
176 # invalid, so return an critical error.
178 kiriwrite_critical("configfileinvalidpermissions");
182 # Converts the XML file into meaningful data for later on in this subroutine.
184 my $kiriwrite_conf_file = 'kiriwrite.cfg';
186 open($kiriwrite_settingsfilehandle, $kiriwrite_conf_file);
187 binmode $kiriwrite_settingsfilehandle, ':utf8';
188 @config_data = <$kiriwrite_settingsfilehandle>;
189 %config = kiriwrite_processconfig(@config_data);
190 close($kiriwrite_settingsfilehandle);
192 # Go and fetch the settings and place them into a hash (that is global).
194 %main::kiriwrite_config = (
196 "directory_data_db" => $config{config}{directory_data_db},
197 "directory_data_output" => $config{config}{directory_data_output},
198 "directory_noncgi_images" => $config{config}{directory_noncgi_images},
200 "system_language" => $config{config}{system_language},
201 "system_presmodule" => $config{config}{system_presmodule},
202 "system_dbmodule" => $config{config}{system_dbmodule},
203 "system_datetime" => $config{config}{system_datetime},
205 "display_textarearows" => $config{config}{display_textarearows},
206 "display_textareacols" => $config{config}{display_textareacols},
207 "display_pagecount" => $config{config}{display_pagecount},
208 "display_templatecount" => $config{config}{display_templatecount},
209 "display_filtercount" => $config{config}{display_filtercount},
211 "database_server" => $config{config}{database_server},
212 "database_port" => $config{config}{database_port},
213 "database_protocol" => $config{config}{database_protocol},
214 "database_sqldatabase" => $config{config}{database_sqldatabase},
215 "database_username" => $config{config}{database_username},
216 "database_password" => $config{config}{database_password},
217 "database_tableprefix" => $config{config}{database_tableprefix}
221 # Do a validation check on all of the variables that were loaded into the global configuration hash.
223 kiriwrite_variablecheck($main::kiriwrite_config{"directory_data_db"}, "maxlength", 64, 0);
224 kiriwrite_variablecheck($main::kiriwrite_config{"directory_data_output"}, "maxlength", 64, 0);
225 kiriwrite_variablecheck($main::kiriwrite_config{"directory_noncgi_images"}, "maxlength", 512, 0);
226 kiriwrite_variablecheck($main::kiriwrite_config{"directory_data_template"}, "maxlength", 64, 0);
228 my $kiriwrite_config_language_filename = kiriwrite_variablecheck($main::kiriwrite_config{"system_language"}, "language_filename", "", 1);
229 my $kiriwrite_config_presmodule_filename = kiriwrite_variablecheck($main::kiriwrite_config{"system_presmodule"}, "module", 0, 1);
230 my $kiriwrite_config_dbmodule_filename = kiriwrite_variablecheck($main::kiriwrite_config{"system_dbmodule"}, "module", 0, 1);
232 my $kiriwrite_config_textarearows_maxlength = kiriwrite_variablecheck($main::kiriwrite_config{"display_textarearows"}, "maxlength", 3, 1);
233 my $kiriwrite_config_textarearows_number = kiriwrite_variablecheck($main::kiriwrite_config{"display_textareacols"}, "numbers", 0, 1);
234 my $kiriwrite_config_textareacols_maxlength = kiriwrite_variablecheck($main::kiriwrite_config{"display_textareacols"}, "maxlength", 3, 1);
235 my $kiriwrite_config_textareacols_number = kiriwrite_variablecheck($main::kiriwrite_config{"display_textareacols"}, "numbers", 0, 1);
236 my $kiriwrite_config_pagecount_maxlength = kiriwrite_variablecheck($main::kiriwrite_config{"display_pagecount"}, "maxlength", 4, 1);
237 my $kiriwrite_config_pagecount_number = kiriwrite_variablecheck($main::kiriwrite_config{"display_pagecount"}, "numbers", 0, 1);
238 my $kiriwrite_config_templatecount_maxlength = kiriwrite_variablecheck($main::kiriwrite_config{"display_templatecount"}, "maxlength", 4, 1);
239 my $kiriwrite_config_templatecount_number = kiriwrite_variablecheck($main::kiriwrite_config{"display_templatecount"}, "numbers", 0, 1);
240 my $kiriwrite_config_filtercount_maxlength = kiriwrite_variablecheck($main::kiriwrite_config{"display_filtercount"}, "maxlength", 4, 1);
241 my $kiriwrite_config_filtercount_number = kiriwrite_variablecheck($main::kiriwrite_config{"display_filtercount"}, "numbers", 0, 1);
243 # Check if the language filename is valid and return an critical error if
246 if ($kiriwrite_config_language_filename eq 1){
248 # The language filename is blank so return an critical error.
250 kiriwrite_critical("languagefilenameblank");
252 } elsif ($kiriwrite_config_language_filename eq 2){
254 # The language filename is invalid so return an critical error.
256 kiriwrite_critical("languagefilenameinvalid");
260 # Check if the presentation and database module names are valid and return a critical
261 # error if they aren't.
263 if ($kiriwrite_config_presmodule_filename eq 1){
265 # The presentation module filename given is blank so return an
268 kiriwrite_critical("presmoduleblank");
272 if ($kiriwrite_config_presmodule_filename eq 2){
274 # The presentation module filename is invalid so return an
277 kiriwrite_critical("presmoduleinvalid");
281 if ($kiriwrite_config_dbmodule_filename eq 1){
283 # The database module filename given is blank so return an
286 kiriwrite_critical("dbmoduleblank");
290 if ($kiriwrite_config_dbmodule_filename eq 2){
292 # The database module filename given is invalid so return
295 kiriwrite_critical("dbmoduleinvalid");
299 # Check if the text area column and row values are blank and return a critical
302 if (!$main::kiriwrite_config{"display_textarearows"}){
304 # The text area row value is blank so return
307 kiriwrite_critical("textarearowblank");
311 if (!$main::kiriwrite_config{"display_textareacols"}){
313 # The text area column value is blank so return
316 kiriwrite_critical("textareacolblank");
320 # Check if the text area column and row values to see if they are valid and return
321 # a critical error if they aren't.
323 if ($kiriwrite_config_textarearows_maxlength eq 1){
325 # The text area row value is too long so return an
328 kiriwrite_critical("textarearowtoolong");
332 if ($kiriwrite_config_textarearows_number eq 1){
334 # The text area row value is invalid so return an
337 kiriwrite_critical("textarearowinvalid");
341 if ($kiriwrite_config_textareacols_maxlength eq 1){
343 # The text area column value is too long so return
346 kiriwrite_critical("textareacoltoolong");
350 if ($kiriwrite_config_textareacols_number eq 1){
352 # The text area column value is invalid so return
355 kiriwrite_critical("textareacolinvalid");
359 # Check if the amount of items per view settings are blank and return a critical
362 if (!$main::kiriwrite_config{"display_pagecount"}){
364 # The display page count is blank so return a
367 kiriwrite_critical("pagecountblank");
371 if (!$main::kiriwrite_config{"display_templatecount"}){
373 # The display template count is blank so return
376 kiriwrite_critical("templatecountblank");
380 if (!$main::kiriwrite_config{"display_filtercount"}){
382 # The display filter count is blank so return a
385 kiriwrite_critical("filtercountblank");
389 # Check if the amount of items per view settings are valid and return a critical
390 # error message if they aren't.
392 if ($kiriwrite_config_pagecount_maxlength eq 1){
394 # The length of the page count value is too long
395 # so return a critical error.
397 kiriwrite_critical("pagecounttoolong");
401 if ($kiriwrite_config_pagecount_number eq 1){
403 # The page count value is invalid so return
406 kiriwrite_critical("pagecountinvalid");
410 if ($kiriwrite_config_templatecount_maxlength eq 1){
412 # The length of the template count value is too
413 # long so return a critical error.
415 kiriwrite_critical("filtercounttoolong");
419 if ($kiriwrite_config_templatecount_number eq 1){
421 # The template count value is invalid so return
424 kiriwrite_critical("filtercountinvalid");
428 if ($kiriwrite_config_filtercount_maxlength eq 1){
430 # The length of the filter count value is too
431 # long so return a critical error.
433 kiriwrite_critical("templatecounttoolong");
437 if ($kiriwrite_config_filtercount_number eq 1){
439 # The filter count value is invalid so return
442 kiriwrite_critical("templatecountinvalid");
446 # Check if the language file does exist before loading it and return an critical error
447 # if it does not exist.
449 my $kiriwrite_config_language_fileexists = kiriwrite_fileexists("lang/" . $main::kiriwrite_config{"system_language"} . ".lang");
451 if ($kiriwrite_config_language_fileexists eq 1){
453 # Language file does not exist so return an critical error.
455 kiriwrite_critical("languagefilemissing");
459 # Check if the language file has valid permission settings and return an critical error if
460 # the language file has invalid permissions settings.
462 my $kiriwrite_config_language_filepermissions = kiriwrite_filepermissions("lang/" . $main::kiriwrite_config{"system_language"} . ".lang", 1, 0);
464 if ($kiriwrite_config_language_filepermissions eq 1){
466 # Language file contains invalid permissions so return an critical error.
468 kiriwrite_critical("languagefilenameinvalidpermissions");
472 # Load the language file.
474 my ($kiriwrite_languagefilehandle, @lang_data);
476 open($kiriwrite_languagefilehandle, "lang/" . $main::kiriwrite_config{"system_language"} . ".lang");
477 @lang_data = <$kiriwrite_languagefilehandle>;
478 %main::kiriwrite_lang = kiriwrite_processconfig(@lang_data);
479 close($kiriwrite_languagefilehandle);
481 # Check if the presentation module does exist before loading it and return an critical error
482 # if the presentation module does not exist.
484 my $kiriwrite_config_presmodule_fileexists = kiriwrite_fileexists("Modules/Presentation/" . $main::kiriwrite_config{"system_presmodule"} . ".pm");
486 if ($kiriwrite_config_presmodule_fileexists eq 1){
488 # Presentation module does not exist so return an critical error.
490 kiriwrite_critical("presmodulemissing");
494 # Check if the presentation module does have the valid permission settings and return a
495 # critical error if the presentation module contains invalid permission settings.
497 my $kiriwrite_config_presmodule_permissions = kiriwrite_filepermissions("Modules/Presentation/" . $main::kiriwrite_config{"system_presmodule"} . ".pm", 1, 0);
499 if ($kiriwrite_config_presmodule_permissions eq 1){
501 # Presentation module contains invalid permissions so return an critical error.
503 kiriwrite_critical("presmoduleinvalidpermissions");
507 # Check if the database module does exist before loading it and return an critical error
508 # if the database module does not exist.
510 my $kiriwrite_config_dbmodule_fileexists = kiriwrite_fileexists("Modules/Database/" . $main::kiriwrite_config{"system_dbmodule"} . ".pm");
512 if ($kiriwrite_config_dbmodule_fileexists eq 1){
514 # Database module does not exist so return an critical error.
516 kiriwrite_critical("dbmodulemissing");
520 # Check if the database module does have the valid permission settings and return an
521 # critical error if the database module contains invalid permission settings.
523 my $kiriwrite_config_dbmodule_permissions = kiriwrite_filepermissions("Modules/Database/" . $main::kiriwrite_config{"system_dbmodule"} . ".pm", 1, 0);
525 if ($kiriwrite_config_dbmodule_permissions eq 1){
527 # Presentation module contains invalid permissions so return an critical error.
529 kiriwrite_critical("dbmoduleinvalidpermissions");
533 # Include the Modules directory.
537 # Load the presentation module.
539 my $presmodulename = "Presentation::" . $main::kiriwrite_config{"system_presmodule"};
540 ($presmodulename) = $presmodulename =~ m/^(.*)$/g;
541 eval "use " . $presmodulename;
542 $presmodulename = "Modules::Presentation::" . $main::kiriwrite_config{"system_presmodule"};
543 $main::kiriwrite_presmodule = $presmodulename->new();
544 $main::kiriwrite_presmodule->clear();
546 # Load the database module.
548 my $dbmodulename = "Database::" . $main::kiriwrite_config{"system_dbmodule"};
549 ($dbmodulename) = $dbmodulename =~ m/^(.*)$/g;
550 eval "use " . $dbmodulename;
551 $dbmodulename = "Modules::Database::" . $main::kiriwrite_config{"system_dbmodule"};
552 $main::kiriwrite_dbmodule = $dbmodulename->new();
554 ($main::kiriwrite_config{"directory_data_db"}) = $main::kiriwrite_config{"directory_data_db"} =~ /^([a-zA-Z0-9.]+)$/;
555 ($main::kiriwrite_config{"directory_data_output"}) = $main::kiriwrite_config{"directory_data_output"} =~ /^([a-zA-Z0-9.]+)$/;
557 # Load the following settings to the database module.
559 $main::kiriwrite_dbmodule->loadsettings({ Directory => $main::kiriwrite_config{"directory_data_db"}, DateTime => $main::kiriwrite_config{"system_datetime"}, Server => $main::kiriwrite_config{"database_server"}, Port => $main::kiriwrite_config{"database_port"}, Protocol => $main::kiriwrite_config{"database_protocol"}, Database => $main::kiriwrite_config{"database_sqldatabase"}, Username => $main::kiriwrite_config{"database_username"}, Password => $main::kiriwrite_config{"database_password"}, TablePrefix => $main::kiriwrite_config{"database_tableprefix"} });
565 sub kiriwrite_language{
566 #################################################################################
567 # kiriwrite_language: Process language strings that needs certain text inserted.#
571 # kiriwrite_language(string, [text, text, ...]); #
573 # string Specifies the string to process. #
574 # text Specifies the text to pass to the string (can be repeated many #
576 #################################################################################
583 $string =~ s/%s/$item/;
592 #################################################################################
593 # kiriwrite_error: Prints out an error message. #
597 # kiriwrite_error(errortype, errorext); #
599 # errortype Specifies the type of error that occured. #
600 # errorext Specifies the extended error information. #
601 #################################################################################
603 # Get the error type from the subroutine.
605 my ($error_type, $error_extended) = @_;
607 # Disconnect from the database server.
609 if ($main::kiriwrite_dbmodule){
610 $main::kiriwrite_dbmodule->disconnect();
613 # Load the list of error messages.
615 my @kiriwrite_error = (
617 # Catch all error message.
620 # Standard error messages.
621 "blankfilename", "blankvariable", "fileexists", "internalerror", "invalidoption", "invalidaction", "invalidfilename", "invalidmode", "invalidutf8", "invalidvariable", "variabletoolong",
623 # Specific error messages.
624 "blankcompiletype", "blankdatabasepageadd", "blankdirectory", "blankfindfilter", "blankdatetimeformat", "browsenumbertoolong", "browsenumberinvalid", "databaseconnectionerror", "databasecategoriestoolong", "databasecopysame", "databasealreadyexists", "datadirectorymissing", "datadirectoryinvalidpermissions", "databasedescriptiontoolong", "databasefilenameinvalid", "databasefilenametoolong", "databaseerror", "databaseinvalidpermissions", "databasenameinvalid", "databasenametoolong", "databasenameblank", "databasemissingfile", "databasemovemissingfile", "databasenorename", "databasemovesame", "dbmoduleblank", "dbmoduleinvalid", "dbdirectoryblank", "dbdirectoryinvalid", "dbmodulemissing", "filtercountinvalid", "filtercounttoolong", "filtersdatabasenotcreated", "filtersdbdatabaseerror", "filtersdbpermissions", "filtersdbmissing", "filteridblank", "filterdoesnotexist", "filteridinvalid", "filteridtoolong", "findfiltertoolong", "filterpriorityinvalid", "filterpriorityinvalidchars", "filterprioritytoolong", "invalidcompiletype", "invalidpagenumber", "nopagesselected", "invaliddirectory", "invaliddatetimeformat", "invalidlanguagefilename", "languagefilenamemissing", "moduleblank", "moduleinvalid", "newcopydatabasedatabaseerror", "newcopydatabasedoesnotexist", "newcopydatabasefileinvalidpermissions", "newmovedatabasedatabaseerror", "newmovedatabasedoesnotexist", "newmovedatabasefileinvalidpermissions", "nodatabasesavailable", "nodatabaseselected", "noeditvaluesselected", "oldcopydatabasedatabaseerror", "oldcopydatabasedoesnotexist", "oldcopydatabasefileinvalidpermissions", "oldmovedatabasedatabaseerror", "oldmovedatabasedoesnotexist", "oldmovedatabasefileinvalidpermissions", "outputdirectoryblank", "outputdirectoryinvalid", "outputdirectorymissing", "outputdirectoryinvalidpermissions", "overridetemplatevalueinvalid", "overridetemplatetoolong", "overridetemplateinvalid", "presmoduleblank", "presmoduleinvalid", "presmodulemissing", "pagecountinvalid", "pagecounttoolong", "pagefilenamedoesnotexist", "pagefilenameexists", "pagefilenameinvalid", "pagefilenametoolong", "pagefilenameblank", "pagetitletoolong", "pagedescriptiontoolong", "pagesectiontoolong", "pagedatabasefilenametoolong", "pagesettingstoolong", "pagesettingsinvalid", "pagetemplatefilenametoolong", "replacefiltertoolong", "servernameinvalid", "servernametoolong", "serverdatabasenameinvalid", "serverdatabasenametoolong", "serverdatabaseusernameinvalid", "serverdatabaseusernametoolong", "serverdatabasepasswordtoolong", "serverdatabasetableprefixinvalid", "serverdatabasetableprefixtoolong", "serverportnumberinvalid", "serverportnumberinvalidcharacters", "serverportnumbertoolong", "serverprotocolnametoolong", "serverprotocolinvalid", "templatecountinvalid", "templatecounttoolong", "templatenameblank", "templatefilenameexists", "templatefilenameinvalid", "templatedatabaseerror", "templatedatabaseinvalidpermissions", "templatedatabaseinvalidformat", "templatedirectoryblank", "templatedirectoryinvalid", "templatedatabasenotcreated", "templatefilenametoolong", "templatenametoolong", "templatedescriptiontoolong", "templatedatabasemissing", "templatedoesnotexist", "templatefilenameblank", "textarearowblank", "textarearowtoolong", "textarearowinvalid", "textareacolblank", "textareacoltoolong", "textareacolinvalid"
628 # Check if the error message name is a valid error message name
629 # and return the generic error message if it isn't.
631 my $error_string = "";
633 if (grep /^$error_type$/, @kiriwrite_error){
635 # The error type is valid so get the error language string
636 # associated with this error messsage name.
638 $error_string = $main::kiriwrite_lang{error}{$error_type};
642 # The error type is invalid so set the error language
643 # string using the generic error message name.
645 $error_string = $main::kiriwrite_lang{error}{generic};
649 $main::kiriwrite_presmodule->clear();
651 $main::kiriwrite_presmodule->startbox("errorbox");
652 $main::kiriwrite_presmodule->addtext($main::kiriwrite_lang{error}{error}, { Style => "errorheader" });
653 $main::kiriwrite_presmodule->addlinebreak();
654 $main::kiriwrite_presmodule->addtext($error_string, { Style => "errortext" });
656 # Check to see if extended error information was passed.
658 if ($error_extended){
660 # Write the extended error information.
662 $main::kiriwrite_presmodule->addlinebreak();
663 $main::kiriwrite_presmodule->addlinebreak();
664 $main::kiriwrite_presmodule->addtext($main::kiriwrite_lang{error}{extendederror});
665 $main::kiriwrite_presmodule->addlinebreak();
666 $main::kiriwrite_presmodule->addlinebreak();
667 $main::kiriwrite_presmodule->startbox("datalist");
668 $main::kiriwrite_presmodule->addtext($error_extended);
669 $main::kiriwrite_presmodule->endbox();
673 $main::kiriwrite_presmodule->endbox();
675 &kiriwrite_output_header;
676 kiriwrite_output_page($main::kiriwrite_lang{error}{error}, $main::kiriwrite_presmodule->grab(), "none");
682 sub kiriwrite_fileexists{
683 #################################################################################
684 # kiriwrite_fileexists: Check if a file exists and returns a value depending on #
685 # if the file exists or not. #
689 # kiriwrite_fileexists(filename); #
691 # filename Specifies the file name to check if it exists or not. #
692 #################################################################################
694 # Get the value that was passed to the subroutine.
698 # Check if the filename exists, if it does, return a value of 0, else
699 # return a value of 1, meaning that the file was not found.
703 # Specified file does exist so return a value of 0.
709 # Specified file does not exist so return a value of 1.
717 sub kiriwrite_filepermissions{
718 #################################################################################
719 # kiriwrite_filepermissions: Check if the file permissions of a file and return #
720 # either a 1 saying that the permissions are valid or return a 0 saying that #
721 # the permissions are invalid. #
725 # kiriwrite_filepermissions(filename, [read], [write], [filemissingskip]); #
727 # filename Specifies the filename to check for permissions. #
728 # read Preform check that the file is readable. #
729 # write Preform check that the file is writeable. #
730 # filemissingskip Skip the check of seeing if it can read or write if the #
732 #################################################################################
734 # Get the values that was passed to the subroutine.
736 my ($filename, $readpermission, $writepermission, $ignorechecks) = @_;
738 # Check to make sure that the read permission and write permission values
739 # are only 1 character long.
741 kiriwrite_variablecheck($readpermission, "maxlength", 1, 0);
742 kiriwrite_variablecheck($writepermission, "maxlength", 1, 0);
743 kiriwrite_variablecheck($ignorechecks, "maxlength", 1, 0);
745 my $ignorechecks_result = 0;
747 # Check if the file should be ignored for read and write checking if
754 # The file exists so the checks are to be done.
756 $ignorechecks_result = 0;
760 # The file does not exist so the checks don't need to
761 # be done to prevent false positives.
763 $ignorechecks_result = 1;
769 $ignorechecks_result = 0;
773 # Check if the file should be checked to see if it can be read.
775 if ($readpermission && $ignorechecks_result eq 0){
777 # The file should be checked to see if it does contain read permissions
778 # and return a 0 if it is invalid.
782 # The file is readable, so do nothing.
786 # The file is not readable, so return 1.
794 # Check if the file should be checked to see if it can be written.
796 if ($writepermission && $ignorechecks_result eq 0){
798 # The file should be checked to see if it does contain write permissions
799 # and return a 0 if it is invalid.
803 # The file is writeable, so do nothing.
807 # The file is not writeable, so return 1.
815 # No problems have occured, so return 0.
821 sub kiriwrite_utf8convert{
822 #################################################################################
823 # kiriwrite_utf8convert: Properly converts values into UTF-8 values. #
827 # utfstring # The UTF-8 string to convert. #
828 #################################################################################
830 # Get the values passed to the subroutine.
832 my ($utfstring) = @_;
834 # Load the Encode perl module.
836 use Encode qw(decode_utf8);
838 # Convert the string.
840 my $finalutf8 = Encode::decode_utf8( $utfstring );
847 sub kiriwrite_critical{
848 #################################################################################
849 # kiriwrite_critical: Displays an critical error message that cannot be #
850 # normally by the kiriwrite_error subroutine. #
854 # errortype Specifies the type of critical error that has occured. #
855 #################################################################################
857 # Get the value that was passed to the subroutine.
859 my ($error_type) = @_;
863 # Get the error type from the errortype string.
867 # Generic critical error message.
869 "generic" => "A critical error has occured but the error is not known to Kiriwrite.",
871 # Specific critical error messages.
873 "configfilemissing" => "The Kiriwrite configuration file is missing! Running the installer script for Kiriwrite is recommended.",
874 "configfileinvalidpermissions" => "The Kiriwrite configuration file has invalid permission settings set! Please set the valid permission settings for the configuration file.",
875 "dbmodulemissing" => "The database module is missing! Running the installer script for Kiriwrite is recommended.",
876 "dbmoduleinvalidpermissions" => "The database module cannot be used as it has invalid permission settings set! Please set the valid permission settings for the configuration file.",
877 "dbmoduleinvalid" => "The database module name given is invalid. Running the installer script for Kiriwrite is recommended.",
878 "invalidvalue" => "An invalid value was passed.",
879 "languagefilenameblank" => "The language filename given is blank! Running the installer script for Kiriwrite is recommended.",
880 "languagefilenameinvalid" => "The language filename given is invalid! Running the installer script for Kiriwrite is recommended.",
881 "languagefilemissing" => "The language filename given does not exist. Running the installer script for Kiriwrite is recommended.",
882 "languagefilenameinvalidpermissions" => "The language file with the filename given has invalid permissions set. Please set the valid permission settings for the language file.",
883 "presmodulemissing" => "The presentation module is missing! Running the installer script for Kiriwrite is recommended.",
884 "presmoduleinvalidpermissions" => "The presentation module cannot be used as it has invalid permission settings set! Please set the valid permission settings for the presentation module.",
885 "presmoduleinvalid" => "The presentation module name given is invalid. Running the installer script for Kiriwrite is recommended.",
886 "textarearowblank" => "The text area row value given is blank. Running the installer script for Kiriwrite is recommended.",
887 "textarearowtoolong" => "The text area row value is too long. Running the installer script for Kiriwrite is recommended.",
888 "textarearowinvalid" => "The text area row value is invalid. Running the installer script for Kiriwrite is recommended.",
889 "textareacolblank" => "The text area row value given is blank. Running the installer script for Kiriwrite is recommended.",
890 "textareacoltoolong" => "The text area column value is too long. Running the installer script for Kiriwrite is recommended.",
891 "textareacolinvalid" => "The text area column value is invalid. Running the installer script for Kiriwrite is recommended.",
892 "pagecountblank" => "The page count value is blank. Running the installer script for Kiriwrite is recommended.",
893 "templatecountblank" => "The template count value is blank. Running the installer script for Kiriwrite is recommended.",
894 "filtercountblank" => "The filter count value is blank. Running the installer script for Kiriwrite is recommended.",
895 "pagecounttoolong" => "The page count value is too long. Running the installer script for Kiriwrite is recommended.",
896 "templatecounttoolong" => "The template count value is too long. Running the installer script for Kiriwrite is recommended.",
897 "filtercounttoolong" => "The filter count value is too long. Running the installer script for Kiriwrite is recommended.",
898 "pagecountinvalid" => "The page count value is invalid. Running the installer script for Kiriwrite is recommended.",
899 "templatecountinvalid" => "The template count value is invalid. Running the installer script for Kiriwrite is recommended.",
900 "filtercountinvalid" => "The filter count is invalid. Running the installer script for Kiriwrite is recommended."
904 if (!$error_list{$error_type}){
906 $error_type = "generic";
910 print "Content-Type: text/html; charset=utf-8;\r\n";
911 print "Expires: Sun, 01 Jan 2008 00:00:00 GMT\r\n\r\n";
912 print "Critical Error: " . $error_list{$error_type};
917 sub kiriwrite_variablecheck{
918 #################################################################################
919 # kiriwrite_variablecheck: Checks the variables for any invalid characters. #
923 # kiriwrite_variablecheck(variable, type, length, noerror); #
925 # variable Specifies the variable to be checked. #
926 # type Specifies what type the variable is. #
927 # option Specifies the maximum/minimum length of the variable #
928 # (if minlength/maxlength is used) or if the filename should be #
929 # checked to see if it is blank. #
930 # noerror Specifies if Kiriwrite should return an error or not on #
932 #################################################################################
934 # Get the values that were passed to the subroutine.
936 my ($variable_data, $variable_type, $variable_option, $variable_noerror) = @_;
938 if (!$variable_data){
942 if ($variable_type eq "numbers"){
944 # Check for numbers and return an error if there is anything else than numebrs.
946 my $variable_data_validated = $variable_data; # Copy the variable_data to variable_data_validated.
947 $variable_data_validated =~ tr/0-9//d; # Take away all of the numbers and from the variable.
948 # If it only contains numbers then it should be blank.
950 if ($variable_data_validated eq ""){
951 # The validated variable is blank. So continue to the end of this section where the return function should be.
953 # The variable is not blank, so check if the no error value is set
956 if ($variable_noerror eq 1){
958 # The validated variable is not blank and the noerror
959 # value is set to 1. So return an value of 1.
960 # (meaning that the data is invalid).
964 } elsif ($variable_noerror eq 0) {
966 # The validated variable is not blank and the noerror
969 kiriwrite_error("invalidvariable");
973 # The variable noerror value is something else
974 # pther than 1 or 0. So return an error.
976 kiriwrite_error("invalidvariable");
984 } elsif ($variable_type eq "lettersnumbers"){
986 # Check for letters and numbers and return an error if there is anything else other
987 # than letters and numbers.
989 my $variable_data_validated = $variable_data; # Copy the variable_data to variable_data_validated
990 $variable_data_validated =~ tr/a-zA-Z0-9.//d;
991 $variable_data_validated =~ s/\s//g;
993 if ($variable_data_validated eq ""){
994 # The validated variable is blank. So continue to the end of this section where the return function should be.
996 # The variable is not blank, so check if the no error value is set
999 if ($variable_noerror eq 1){
1001 # The validated variable is not blank and the noerror
1002 # value is set to 1. So return an value of 1.
1003 # (meaning that the data is invalid).
1007 } elsif ($variable_noerror eq 0) {
1009 # The validated variable is not blank and the noerror
1010 # value is set to 0.
1012 kiriwrite_error("invalidvariable");
1016 # The variable noerror value is something else
1017 # pther than 1 or 0. So return an error.
1019 kiriwrite_error("invalidvariable");
1027 } elsif ($variable_type eq "maxlength"){
1028 # Check for the length of the variable, return an error if it is longer than the length specified.
1030 # Check if the variable_data string is blank, if it is then set the variable_data_length
1033 my $variable_data_length = 0;
1035 if (!$variable_data){
1037 # Set variable_data_length to '0'.
1038 $variable_data_length = 0;
1042 # Get the length of the variable recieved.
1043 $variable_data_length = length($variable_data);
1049 if ($variable_data_length > $variable_option){
1051 # The variable length is longer than it should be so check if
1052 # the no error value is set 1.
1054 if ($variable_noerror eq 1){
1056 # The no error value is set to 1, so return an
1057 # value of 1 (meaning tha the variable is
1058 # too long to be used).
1062 } elsif ($variable_noerror eq 0){
1064 # The no error value is set to 0, so return
1067 kiriwrite_error("variabletoolong");
1071 # The no error value is something else other
1072 # than 0 or 1, so return an error.
1074 kiriwrite_error("variabletoolong");
1080 # The variable length is exactly or shorter than specified, so continue to end of this section where
1081 # the return function should be.
1087 } elsif ($variable_type eq "blank"){
1088 # Check if the variable is blank and if it is blank, then return an error.
1090 if (!$variable_data){
1092 # The variable data really is blank, so check what
1093 # the no error value is set.
1095 if ($variable_noerror eq 1){
1097 # The no error value is set to 1, so return
1098 # a value of 1 (saying that the variable was
1103 } elsif ($variable_noerror eq 0){
1105 # The no error value is set to 0, so return
1108 kiriwrite_error("blankvariable");
1112 # The no error value is something else other
1113 # than 0 or 1, so return an error.
1115 kiriwrite_error("invalidvariable");
1123 } elsif ($variable_type eq "filename"){
1124 # Check for letters and numbers, if anything else than letters and numbers is there (including spaces) return
1127 # Check if the filename passed is blank, if it is then return with an error.
1129 if ($variable_data eq ""){
1131 # The filename specified is blank, so check what the
1132 # noerror value is set.
1134 if ($variable_noerror eq 1){
1136 # The no error value is set to 1 so return
1137 # a value of 1 (meaning that the filename
1142 } elsif ($variable_noerror eq 0){
1144 # The no error value is set to 1 so return
1147 kiriwrite_error("blankfilename");
1151 # The no error value is something else other
1152 # than 0 or 1, so return an error.
1154 kiriwrite_error("invalidvariable");
1163 my $variable_data_validated = $variable_data;
1164 $variable_data_validated =~ tr/a-zA-Z0-9\.//d;
1166 # Check if the validated data variable is blank, if it is
1167 # then continue to the end of this section where the return
1168 # function should be, otherwise return an error.
1170 if ($variable_data_validated eq ""){
1172 # The validated data variable is blank, meaning that
1173 # it only contained letters and numbers.
1177 # The validated data variable is not blank, meaning
1178 # that it contains something else, so return an error
1181 if ($variable_noerror eq 1){
1183 # The no error value is set to 1 so return
1184 # an value of 2. (meaning that the filename
1190 } elsif ($variable_noerror eq 0){
1192 # The no error value is set to 0 so return
1195 kiriwrite_error("invalidfilename");
1199 # The no error value is something else other
1200 # than 0 or 1 so return an error.
1202 kiriwrite_error("invalidvariable");
1210 } elsif ($variable_type eq "filenameindir"){
1211 # Check if the filename is in the directory and return an
1212 # error if it isn't.
1214 if ($variable_data eq ""){
1216 # The filename specified is blank, so check what the
1217 # noerror value is set.
1219 if ($variable_noerror eq 1){
1221 # The no error value is set to 1 so return
1222 # a value of 1 (meaning that the filename
1227 } elsif ($variable_noerror eq 0){
1229 # The no error value is set to 1 so return
1232 kiriwrite_error("blankfilename");
1236 # The no error value is something else other
1237 # than 0 or 1, so return an error.
1239 kiriwrite_error("invalidvariable");
1248 # Set the following variables for later on.
1250 my $variable_data_length = 0;
1251 my $variable_data_char = "";
1252 my $variable_data_validated = "";
1253 my $variable_data_seek = 0;
1254 my $variable_database_list = "";
1255 my $variable_database_listcurrent = "";
1256 my $variable_data_firstlevel = 1;
1258 # Get the length of the variable recieved.
1260 $variable_data_length = length($variable_data);
1262 # Check if the database filename contains the directory command
1263 # for up a directory level and if it is, return an error
1264 # or return with a number.
1268 # Get a character from the filename passed to this subroutine.
1270 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
1272 # Check if the current character is the forward slash character.
1274 if ($variable_data_char eq "/"){
1276 # Check if the current directory is blank (and on the first level), or if the
1277 # current directory contains two dots or one dot, if it does return an error.
1279 if ($variable_database_listcurrent eq "" && $variable_data_firstlevel eq 1 || $variable_database_listcurrent eq ".." || $variable_database_listcurrent eq "."){
1281 # Check if the noerror value is set to 1, if it is return an
1282 # number, else return an proper error.
1284 if ($variable_noerror eq 1){
1286 # Page filename contains invalid characters and
1287 # the no error value is set to 1 so return a
1288 # value of 2 (meaning that the page filename
1293 } elsif ($variable_noerror eq 0) {
1295 # Page filename contains invalid characters and
1296 # the no error value is set to 0 so return an
1299 kiriwrite_error("invalidfilename");
1303 # The no error value is something else other
1304 # than 0 or 1 so return an error.
1306 kiriwrite_error("invalidvariable");
1312 # Append the forward slash, clear the current directory name and set
1313 # the first directory level value to 0.
1315 $variable_database_list = $variable_database_list . $variable_data_char;
1316 $variable_database_listcurrent = "";
1317 $variable_data_firstlevel = 0;
1321 # Append the current character to the directory name and to the current
1324 $variable_database_list = $variable_database_list . $variable_data_char;
1325 $variable_database_listcurrent = $variable_database_listcurrent . $variable_data_char;
1329 # Increment the seek counter.
1331 $variable_data_seek++;
1333 } until ($variable_data_seek eq $variable_data_length);
1337 } elsif ($variable_type eq "datetime"){
1338 # Check if the date and time setting format is valid.
1340 if ($variable_data eq ""){
1342 if ($variable_noerror eq 1){
1344 # The no error value is set to 1 so return
1345 # a value of 1 (meaning that the date and
1346 # time format was blank).
1350 } elsif ($variable_noerror eq 0){
1352 # The no error value is set to 1 so return
1355 kiriwrite_error("blankdatetimeformat");
1359 # The no error value is something else other
1360 # than 0 or 1, so return an error.
1362 kiriwrite_error("invalidvariable");
1368 my $variable_data_validated = $variable_data;
1369 $variable_data_validated =~ tr|dDmMyYhms/():[ ]||d;
1371 if ($variable_data_validated eq ""){
1373 # The date and time format is valid. So
1378 # The validated data variable is not blank, meaning
1379 # that it contains something else, so return an error
1382 if ($variable_noerror eq 1){
1384 # The no error value is set to 1 so return
1385 # an value of 2. (meaning that the date and
1386 # time format was invalid).
1390 } elsif ($variable_noerror eq 0){
1392 # The no error value is set to 0 so return
1395 kiriwrite_error("invaliddatetimeformat");
1399 # The no error value is something else other
1400 # than 0 or 1 so return an error.
1402 kiriwrite_error("invalidvariable");
1410 } elsif ($variable_type eq "directory"){
1411 # Check if the directory only contains letters and numbers and
1412 # return an error if anything else appears.
1414 my $variable_data_validated = $variable_data;
1415 $variable_data_validated =~ tr/a-zA-Z0-9//d;
1417 if ($variable_data eq ""){
1419 if ($variable_noerror eq 1){
1421 # The no error value is set to 1 so return
1422 # a value of 1 (meaning that the directory
1427 } elsif ($variable_noerror eq 0){
1429 # The no error value is set to 1 so return
1432 kiriwrite_error("blankdirectory");
1436 # The no error value is something else other
1437 # than 0 or 1, so return an error.
1439 kiriwrite_error("invalidvariable");
1445 if ($variable_data_validated eq ""){
1447 # The validated data variable is blank, meaning that
1448 # it only contains letters and numbers.
1452 # The validated data variable is not blank, meaning
1453 # that it contains something else, so return an error
1456 if ($variable_noerror eq 1){
1458 # The no error value is set to 1 so return
1459 # an value of 2. (meaning that the directory
1464 } elsif ($variable_noerror eq 0){
1466 # The no error value is set to 0 so return
1469 kiriwrite_error("invaliddirectory");
1473 # The no error value is something else other
1474 # than 0 or 1 so return an error.
1476 kiriwrite_error("invalidvariable");
1484 } elsif ($variable_type eq "language_filename"){
1486 # The variable type is a language filename type.
1487 # Check if the language file name is blank and
1488 # if it is then return an error (or value).
1490 if ($variable_data eq ""){
1492 # The language filename is blank so check the
1493 # no error value and return an error (or value).
1495 if ($variable_noerror eq 1){
1497 # Language filename is blank and the no error value
1498 # is set as 1, so return a value of 1 (saying that
1499 # the language filename is blank).
1503 } elsif ($variable_noerror eq 0) {
1505 # Language filename is blank and the no error value
1506 # is not set as 1, so return an error.
1508 kiriwrite_critical("languagefilenameblank");
1512 # The noerror value is something else other
1513 # than 0 or 1 so return an error.
1515 kiriwrite_error("invalidvariable");
1521 # Set the following variables for later on.
1523 my $variable_data_length = 0;
1524 my $variable_data_char = "";
1525 my $variable_data_seek = 0;
1527 # Get the length of the language file name.
1529 $variable_data_length = length($variable_data);
1533 # Get a character from the language filename passed to this
1534 # subroutine and the character the seek counter value is set
1537 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
1539 # Check if the language filename contains a forward slash or a dot,
1540 # if the selected character is a forward slash then return an error
1543 if ($variable_data_char eq "/" || $variable_data_char eq "."){
1545 # The language filename contains a forward slash or
1546 # a dot so depending on the no error value, return
1547 # an error or a value.
1549 if ($variable_noerror eq 1){
1551 # Language filename contains a forward slash or a dot
1552 # and the no error value has been set to 1, so return
1553 # an value of 2 (saying that the language file name is
1558 } elsif ($variable_noerror eq 0) {
1560 # Language filename contains a forward slash and the no
1561 # error value has not been set to 1, so return an error.
1563 kiriwrite_critical("languagefilenameinvalid");
1567 # The noerror value is something else other than
1568 # 1 or 0 so return an error.
1570 kiriwrite_error("invalidvariable");
1576 # Increment the seek counter.
1578 $variable_data_seek++;
1580 } until ($variable_data_seek eq $variable_data_length);
1584 } elsif ($variable_type eq "pagesetting"){
1586 # The variable type is a page setting, so check if the page
1587 # setting has one of the valid options.
1589 if ($variable_data eq 0 || $variable_data eq 1 || $variable_data eq 2 || $variable_data eq 3){
1591 # The variable is one of the options above, so continue
1592 # to the end of this section.
1596 # The variable is not one of the options above, so check
1597 # and see if a error or a value should be returned.
1599 if ($variable_noerror eq 1){
1601 # The page setting is invalid and the no error
1602 # value is set 1, so return a value of 1
1603 # (saying that the page setting value is
1608 } elsif ($variable_noerror eq 0) {
1610 # Page setting is invalid and the no error value
1611 # is not 1, so return an error.
1613 kiriwrite_error("invalidvariable");
1617 # The no error value is something else other
1618 # than 0 or 1 so return an error.
1620 kiriwrite_error("invalidvariable");
1628 } elsif ($variable_type eq "page_filename"){
1630 # The variable type is a page filename type. Check
1631 # if the data is empty and if it is then return an
1634 if ($variable_data eq ""){
1636 # The filename is blank so check the no error
1637 # value and depending on it return an value
1640 if ($variable_noerror eq 1){
1642 # Page filename is blank and the no error value
1643 # is set as 1, so return a value of 1 (saying
1644 # the filename is blank).
1648 } elsif ($variable_noerror eq 0) {
1650 # Page filename is blank and the no error value
1651 # is not 1, so return an error.
1653 kiriwrite_error("emptypagefilename");
1657 # The no error value is something else other
1658 # than 0 or 1 so return an error.
1660 kiriwrite_error("invalidvariable");
1665 # Set the following variables for later on.
1668 my $variable_data_length = 0;
1669 my $variable_data_slash = 0;
1670 my $variable_data_char = "";
1671 my $variable_data_validated = "";
1672 my $variable_data_seek = 0;
1673 my $variable_database_list = "";
1674 my $variable_database_listcurrent = "";
1675 my $variable_data_firstlevel = 1;
1677 # Get the length of the filename.
1679 $variable_data_length = length($variable_data);
1681 # Check that only valid characters should be appearing in
1684 $variable_data_validated = $variable_data;
1685 $variable_data_validated =~ tr|a-zA-Z0-9\.\/\-_||d;
1687 if ($variable_data_validated ne ""){
1689 # The validated variable is not blank, meaning the
1690 # variable contains invalid characters, so return
1693 if ($variable_noerror eq 1){
1695 # Page filename contains invalid characters and
1696 # the no error value is set to 1 so return a
1697 # value of 2 (meaning that the page filename
1702 } elsif ($variable_noerror eq 0) {
1704 # Page filename contains invalid characters and
1705 # the no error value is set to 0 so return an
1708 kiriwrite_error("invalidfilename");
1712 # The no error value is something else other
1713 # than 0 or 1 so return an error.
1715 kiriwrite_error("invalidvariable");
1721 # Check if the page filename contains the directory command
1722 # for up a directory level and if it is, return an error
1723 # or return with a number.
1727 # Get a character from the filename passed to this subroutine.
1729 $variable_data_char = substr($variable_data, $variable_data_seek, 1);
1731 # Check if the current character is the forward slash character.
1733 if ($variable_data_char eq "/"){
1735 # Check if the current directory is blank (and on the first level), or if the
1736 # current directory contains two dots or one dot, if it does return an error.
1738 $variable_data_slash = 1;
1740 if ($variable_database_listcurrent eq "" && $variable_data_firstlevel eq 1 || $variable_database_listcurrent eq ".." || $variable_database_listcurrent eq "."){
1742 # Check if the noerror value is set to 1, if it is return an
1743 # number, else return an proper error.
1745 if ($variable_noerror eq 1){
1747 # Page filename contains invalid characters and
1748 # the no error value is set to 1 so return a
1749 # value of 2 (meaning that the page filename
1754 } elsif ($variable_noerror eq 0) {
1756 # Page filename contains invalid characters and
1757 # the no error value is set to 0 so return an
1760 kiriwrite_error("invalidfilename");
1764 # The no error value is something else other
1765 # than 0 or 1 so return an error.
1767 kiriwrite_error("invalidvariable");
1773 # Append the forward slash, clear the current directory name and set
1774 # the first directory level value to 0.
1776 $variable_database_list = $variable_database_list . $variable_data_char;
1777 $variable_database_listcurrent = "";
1778 $variable_data_firstlevel = 0;
1782 # Append the current character to the directory name and to the current
1785 $variable_data_slash = 0;
1787 $variable_database_list = $variable_database_list . $variable_data_char;
1788 $variable_database_listcurrent = $variable_database_listcurrent . $variable_data_char;
1792 # Increment the seek counter.
1794 $variable_data_seek++;
1796 } until ($variable_data_seek eq $variable_data_length);
1798 # Check if the last character is a slash and return an
1801 if ($variable_data_slash eq 1){
1803 if ($variable_noerror eq 1){
1805 # Last character is a slash and the no error
1806 # value is set to 1 so return a value of 2
1807 # (meaning that the page filename is invalid).
1811 } elsif ($variable_noerror eq 0) {
1813 # Page filename contains a slash for the last
1814 # character and the no error value is set to 0
1815 # so return an error.
1817 kiriwrite_error("invalidfilename");
1821 # The no error value is something else other
1822 # than 0 or 1 so return an error.
1824 kiriwrite_error("invalidvariable");
1832 } elsif ($variable_type eq "module"){
1834 # The variable type is a presentation module filename.
1836 # Check if the variable_data is blank and if it is
1839 if ($variable_data eq ""){
1841 # The presentation module is blank so check if an error
1842 # value should be returned or a number should be
1845 if ($variable_noerror eq 1){
1847 # Module name is blank and the no error value
1848 # is set to 1 so return a value of 2 (meaning
1849 # that the page filename is blank).
1853 } elsif ($variable_noerror eq 0) {
1855 # Module name contains is blank and the no error
1856 # value is set to 0 so return an error.
1858 kiriwrite_critical("moduleblank");
1862 # The no error value is something else other
1863 # than 0 or 1 so return an error.
1865 kiriwrite_critical("invalidvalue");
1873 my $variable_data_validated = $variable_data;
1874 $variable_data_validated =~ tr/a-zA-Z0-9//d;
1876 if ($variable_data_validated eq ""){
1880 if ($variable_noerror eq 1){
1882 # Module name contains invalid characters and
1883 # the no error value is set to 1 so return a
1884 # value of 2 (meaning that the page filename
1889 } elsif ($variable_noerror eq 0) {
1891 # Module name contains invalid characters and
1892 # the no error value is set to 0 so return an
1895 kiriwrite_critical("moduleinvalid");
1899 # The no error value is something else other
1900 # than 0 or 1 so return an error.
1902 kiriwrite_error("invalidvalue");
1910 } elsif ($variable_type eq "utf8"){
1912 # The variable type is a UTF8 string.
1914 if (!$variable_data){
1916 $variable_data = "";
1925 while ($chunk < $length){
1927 $chunkdata = substr($variable_data, $chunk, $process);
1929 if ($chunkdata =~ m/\A(
1930 [\x09\x0A\x0D\x20-\x7E] # ASCII
1931 | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1932 | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1933 | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1934 | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1935 | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1936 | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1937 | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1940 # The UTF-8 string is valid.
1944 # The UTF-8 string is not valid, check if the no error
1945 # value is set to 1 and return an error if it isn't.
1947 if ($variable_noerror eq 1){
1949 # The no error value has been set to 1, so return
1950 # a value of 1 (meaning that the UTF-8 string is
1955 } elsif ($variable_noerror eq 0) {
1957 # The no error value has been set to 0, so return
1960 kiriwrite_error("invalidutf8");
1964 # The no error value is something else other than 0
1965 # or 1, so return an error.
1967 kiriwrite_error("invalidoption");
1974 $chunk = $chunk + $process;
1978 # # Check if the string is a valid UTF8 string.
1980 # if ($variable_data =~ m/^(
1981 # [\x09\x0A\x0D\x20-\x7E] # ASCII
1982 # | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
1983 # | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
1984 # | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
1985 # | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
1986 # | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
1987 # | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
1988 # | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
1991 # # The UTF-8 string is valid.
1995 # # The UTF-8 string is not valid, check if the no error
1996 # # value is set to 1 and return an error if it isn't.
1998 # if ($variable_noerror eq 1){
2000 # # The no error value has been set to 1, so return
2001 # # a value of 1 (meaning that the UTF-8 string is
2006 # } elsif ($variable_noerror eq 0) {
2008 # # The no error value has been set to 0, so return
2011 # kiriwrite_error("invalidutf8");
2015 # # The no error value is something else other than 0
2016 # # or 1, so return an error.
2018 # kiriwrite_error("invalidoption");
2026 } elsif ($variable_type eq "serverprotocol"){
2028 # Check if the server protocol is TCP or UDP and return
2029 # an error if it isn't.
2031 if ($variable_data ne "tcp" && $variable_data ne "udp"){
2033 # The protocol given is not valid, check if the no
2034 # error value is set to 1 and return an error if it isn't.
2036 if ($variable_noerror eq 1){
2038 # The no error value has been set to 1, so return a
2039 # value of 1 (meaning that the server protocol is
2044 } elsif ($variable_noerror eq 0){
2046 # The no error value has been set to 0, so return
2049 kiriwrite_error("serverprotocolinvalid");
2053 # The no error value is something else other than 0
2054 # or 1, so return an error.
2056 kiriwrite_error("invalidoption");
2064 } elsif ($variable_type eq "port"){
2066 # Check if the port number given is less than 0 or more than 65535
2067 # and return an error if it is.
2069 if ($variable_data < 0 || $variable_data > 65535){
2071 # The port number is less than 0 and more than 65535 so
2072 # check if the no error value is set to 1 and return an
2073 # error if it isn't.
2075 if ($variable_noerror eq 1){
2077 # The no error value has been set to 1, so return a
2078 # value of 1 (meaning that the port number is invalid).
2082 } elsif ($variable_noerror eq 0){
2084 # The no error value has been set to 0, so return
2087 kiriwrite_error("serverportnumberinvalid");
2091 # The no error value is something else other than 0
2092 # or 1, so return an error.
2094 kiriwrite_error("invalidoption");
2104 # Another type than the valid ones above has been specified so return an error specifying an invalid option.
2105 kiriwrite_error("invalidoption");
2109 sub kiriwrite_output_header{
2110 #################################################################################
2111 # kiriwrite_output_header: Outputs the header to the browser/stdout/console. #
2115 # kiriwrite_output_header(); #
2116 #################################################################################
2118 # Print a header saying that the page expires immediately since the
2119 # date is set in the past.
2121 print "Content-Type: text/html; charset=utf8\r\n";
2122 print "Expires: Sun, 01 Jan 2007 00:00:00 GMT\r\n\r\n";
2127 sub kiriwrite_processfilename{
2128 #################################################################################
2129 # kiriwrite_processfilename: Processes a name and turns it into a filename that #
2130 # can be used by Kiriwrite. #
2134 # kiriwrite_processfilename(text); #
2136 # text Specifies the text to be used in the process for creating a new #
2138 #################################################################################
2140 # Get the values that have been passed to the subroutine.
2142 my ($process_text) = @_;
2144 # Define some variables that will be used later on.
2146 my $processed_stageone = "";
2147 my $processed_stagetwo = "";
2148 my $processed_length = "";
2149 my $processed_char = "";
2150 my $processed_seek = 0;
2151 my $processed_filename = "";
2153 # Set the first stage value of the processed filename to the
2154 # process filename and then filter it out to only contain
2155 # numbers and letters (no spaces) and then convert the
2156 # capitals to small letters.
2158 $processed_stageone = $process_text;
2159 $processed_stageone =~ tr#a-zA-Z0-9##cd;
2160 $processed_stageone =~ tr/A-Z/a-z/;
2162 # Now set the second stage value of the processed filename
2163 # to the first stage value of the processed filename and
2164 # then limit the filename down to 32 characters.
2166 $processed_stagetwo = $processed_stageone;
2167 $processed_length = length($processed_stagetwo);
2169 # Process the second stage filename into the final
2170 # filename and do so until the seek counter is 32
2171 # or reaches the length of the second stage filename.
2175 # Get the character that is the seek counter
2178 $processed_char = substr($processed_stagetwo, $processed_seek, 1);
2180 # Append to the final processed filename.
2182 $processed_filename = $processed_filename . $processed_char;
2184 # Increment the seek counter.
2188 } until ($processed_seek eq 32 || $processed_seek eq $processed_length);
2190 return $processed_filename;
2195 sub kiriwrite_processconfig{
2196 #################################################################################
2197 # kiriwrite_processconfig: Processes an INI style configuration file. #
2201 # kiriwrite_processconfig(data); #
2203 # data Specifies the data to process. #
2204 #################################################################################
2206 my (@settings) = @_;
2208 my ($settings_line, %settings, $settings, $sectionname, $setting_name, $setting_value);
2210 foreach $settings_line (@settings){
2212 next if !$settings_line;
2214 # Check if the first character is a bracket.
2216 if (substr($settings_line, 0, 1) eq "["){
2217 $settings_line =~ s/\[//;
2218 $settings_line =~ s/\](.*)//;
2219 $settings_line =~ s/\n//;
2220 $sectionname = $settings_line;
2224 $setting_name = $settings_line;
2225 $setting_value = $settings_line;
2226 $setting_name =~ s/\=(.*)//;
2227 $setting_name =~ s/\n//;
2228 $setting_value =~ s/(.*)\=//;
2229 $setting_value =~ s/\n//;
2231 # Remove the spacing before and after the '=' sign.
2233 $setting_name =~ s/\s+$//;
2234 $setting_value =~ s/^\s+//;
2235 $setting_value =~ s/\r//;
2237 $settings{$sectionname}{$setting_name} = $setting_value;
2245 sub kiriwrite_output_page{
2246 #################################################################################
2247 # kiriwrite_output_page: Outputs the page to the browser/stdout/console. #
2251 # kiriwrite_output_page(pagetitle, pagedata, menutype); #
2253 # pagetitle Specifies the page title. #
2254 # pagedata Specifies the page data. #
2255 # menutype Prints out which menu to use. #
2256 #################################################################################
2258 my ($pagetitle, $pagedata, $menutype, $db_filename) = @_;
2260 # Open the script page template and load it into the scriptpage variable,
2261 # while declaring the variable.
2263 open (my $filehandle_scriptpage, "<:utf8", 'page.html');
2264 my @scriptpage = <$filehandle_scriptpage>;
2265 binmode $filehandle_scriptpage, ':utf8';
2266 close ($filehandle_scriptpage);
2268 my $query_lite = new CGI::Lite;
2269 my $form_data = $query_lite->parse_form_data;
2271 # Define the variables required.
2273 my $scriptpageline = "";
2274 my $pageoutput = "";
2276 $main::kiriwrite_presmodule->clear();
2278 # Print out the main menu for Kiriwrite.
2280 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=db", { Text => $main::kiriwrite_lang{menu}{viewdatabases} });
2281 $main::kiriwrite_presmodule->addtext(" | ");
2282 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=page", { Text => $main::kiriwrite_lang{menu}{viewpages} });
2283 $main::kiriwrite_presmodule->addtext(" | ");
2284 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=filter", { Text => $main::kiriwrite_lang{menu}{viewfilters} });
2285 $main::kiriwrite_presmodule->addtext(" | ");
2286 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=template", { Text => $main::kiriwrite_lang{menu}{viewtemplates} });
2287 $main::kiriwrite_presmodule->addtext(" | ");
2288 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=compile", { Text => $main::kiriwrite_lang{menu}{compilepages} });
2289 $main::kiriwrite_presmodule->addtext(" | ");
2290 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=settings", { Text => $main::kiriwrite_lang{menu}{viewsettings} });
2291 $main::kiriwrite_presmodule->addlinebreak();
2293 # Check what menu is going to be printed along with the default 'top' menu.
2295 if ($menutype eq "database"){
2297 # If the menu type is database then print out the database sub-menu.
2299 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=db", { Text => $main::kiriwrite_lang{database}{submenu_viewdatabases} });
2300 $main::kiriwrite_presmodule->addtext(" | ");
2301 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=db&action=new", { Text => $main::kiriwrite_lang{database}{submenu_adddatabase} });
2303 } elsif ($menutype eq "pages"){
2304 # If the menu type is pages then print out the pages sub-menu.
2306 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=page&action=add&database=" . $db_filename, { Text => $main::kiriwrite_lang{pages}{submenu_addpage} });
2308 } elsif ($menutype eq "filter"){
2310 # If the menu type is filters then print out the filter sub-menu.
2312 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=filter", { Text => $main::kiriwrite_lang{filter}{submenu_showfilters} });
2313 $main::kiriwrite_presmodule->addtext(" | ");
2314 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=filter&action=add", { Text => $main::kiriwrite_lang{filter}{submenu_addfilter} });
2316 } elsif ($menutype eq "settings"){
2318 # If the menu type is options then print out the options sub-menu.
2320 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=settings", { Text => $main::kiriwrite_lang{setting}{submenu_viewsettings} });
2321 $main::kiriwrite_presmodule->addtext(" | ");
2322 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=settings&action=edit", { Text => $main::kiriwrite_lang{setting}{submenu_editsettings} });
2324 } elsif ($menutype eq "template"){
2326 # If the menu type is template then print out the template sub-menu.
2328 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=template", { Text => $main::kiriwrite_lang{template}{submenu_showtemplates} });
2329 $main::kiriwrite_presmodule->addtext(" | ");
2330 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=template&action=add", { Text => $main::kiriwrite_lang{template}{submenu_addtemplate} });
2332 } elsif ($menutype eq "compile"){
2334 # If the menu type is compile then print out the compile sub-menu.
2336 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=compile", { Text => $main::kiriwrite_lang{compile}{submenu_listdatabases} });
2337 $main::kiriwrite_presmodule->addtext(" | ");
2338 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=compile&action=all", { Text => $main::kiriwrite_lang{compile}{submenu_compileall} });
2339 $main::kiriwrite_presmodule->addtext(" | ");
2340 $main::kiriwrite_presmodule->addlink($main::kiriwrite_env{'script_filename'} . "?mode=compile&action=clean", { Text => $main::kiriwrite_lang{compile}{submenu_cleanoutputdirectory} });
2344 my $menuoutput = $main::kiriwrite_presmodule->grab();
2346 # Find <kiriwrite> tages and replace with the apporiate variables.
2348 foreach $scriptpageline (@scriptpage){
2350 $scriptpageline =~ s/<kiriwrite:menu>/$menuoutput/g;
2351 $scriptpageline =~ s/<kiriwrite:imagespath>/$main::kiriwrite_config{"directory_noncgi_images"}/g;
2352 $scriptpageline =~ s/<kiriwrite:pagedata>/$pagedata/g;
2354 # Check if page title specified is blank, otherwise add a page title
2357 if (!$pagetitle || $pagetitle eq ""){
2358 $scriptpageline =~ s/<kiriwrite:title>//g;
2360 $scriptpageline =~ s/<kiriwrite:title>/ ($pagetitle)/g;
2365 # Append processed line to the pageoutput variable.
2367 $pageoutput = $pageoutput . $scriptpageline;