Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
More alterations made.
[kiriwrite/.git] / cgi-files / Modules / Database / SQLite.pm
1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm)                #
3 # Database module for mainipulating SQLite databases in the database directory. #
4 #                                                                               #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk>              #
6 #                                                                               #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
8 #                                                                               #
9 # This program is free software; you can redistribute it and/or modify it under #
10 # the terms of the GNU General Public License as published by the Free          #
11 # Software Foundation; as version 2 of the License.                             #
12 #                                                                               #
13 # This program is distributed in the hope that it will be useful, but WITHOUT   #
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
16 #                                                                               #
17 # You should have received a copy of the GNU General Public License along with  #
18 # this program; if not, write to the Free Software Foundation, Inc., 51         #
19 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                           #
20 #################################################################################
22 # Define the package (perl module) name.
24 package Kiriwrite::Database::SQLite;
26 # Enable strict and use warnings.
28 use strict;
29 use warnings;
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION    = "0.1.0";
38 my ($options, %options);
39 my $database_handle;
40 my $string_handle;
41 my $error = "";
42 my $errorext = "";
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_string_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_string_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_string_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines.                                                          #
59 #################################################################################
61 sub new{
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite                        #
64 #                                                                               #
65 # Usage:                                                                        #
66 #                                                                               #
67 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
68 #################################################################################
69         
70         # Get the perl module name.
72         my $class = shift;
73         my $self = {};
75         return bless($self, $class);
77 }
79 sub loadsettings{
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module                  #
82 #                                                                               #
83 # Usage:                                                                        #
84 #                                                                               #
85 # $dbmodule->loadsettings(Directory, options);                                  #
86 #                                                                               #
87 # options       Specifies the following options (in any order).                 #
88 #                                                                               #
89 # Directory     Specifies the directory to use for getting databases.           #
90 # DateTime      Specifies the date and time format to use.                      #
91 # Server        Specifies the server to use.                                    #
92 # Database      Specifies the database to use.                                  #
93 # Username      Specifies the username to use.                                  #
94 # Password      Specifies the password to use.                                  #
95 # HashType      Specifies the password hash type to use.                        #
96 # Port          Specifies the server port to use.                               #
97 # Protocol      Specifies the protocol to use.                                  #
98 # TablePrefix   Specifies the table prefix to use.                              #
99 #################################################################################
101         # Get the data passed to the subroutine.
103         my $class = shift;
104         my ($passedoptions)     = @_;
106         # Add the directory setting to the list of options (as it's the only
107         # one needed for this database module).
109         %options = (
110                 "Directory"     => $passedoptions->{"Directory"},
111                 "DateTime"      => $passedoptions->{"DateTime"},
112         );
116 sub convert{
117 #################################################################################
118 # convert: Converts data into SQL formatted data.                               #
119 #                                                                               #
120 # Usage:                                                                        #
121 #                                                                               #
122 # $dbmodule->convert(data);                                                     #
123 #                                                                               #
124 # data          Specifies the data to convert.                                  #
125 #################################################################################
127         # Get the data passed to the subroutine.
129         my $class       = shift;
130         my $data        = shift;
132         if (!$data){
133                 $data = "";
134         }
136         $data =~ s/'/''/g;
137         $data =~ s/\b//g;
139         return $data;
143 sub dateconvert{
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date.                          #
146 #                                                                               #
147 # Usage:                                                                        #
148 #                                                                               #
149 # $dbmodule->dateconvert(date);                                                 #
150 #                                                                               #
151 # date          Specifies the date to convert.                                  #
152 #################################################################################
154         # Get the date passed to the subroutine.
156         my $class       = shift;
157         my $data        = shift;
159         # Convert the date given into the proper date.
161         # Create the following varialbes to be used later.
163         my $date;
164         my $time;
165         my $day;
166         my $day_full;
167         my $month;
168         my $month_check;
169         my $month_full;
170         my $year;
171         my $year_short;
172         my $hour;
173         my $hour_full;
174         my $minute;
175         my $minute_full;
176         my $second;
177         my $second_full;
178         my $seek = 0;
179         my $timelength;
180         my $datelength;
181         my $daylength;
182         my $secondlength;
183         my $startchar = 0;
184         my $char;
185         my $length;
186         my $count = 0;
188         # Split the date and time.
190         $length = length($data);
192         if ($length > 0){
194                 do {
196                         # Get the character and check if it is a space.
198                         $char = substr($data, $seek, 1);
200                         if ($char eq ' '){
202                                 # The character is a space, so get the date and time.
204                                 $date           = substr($data, 0, $seek);
205                                 $timelength     = $length - $seek - 1;
206                                 $time           = substr($data, $seek + 1, $timelength);
208                         }
210                         $seek++;
212                 } until ($seek eq $length);
214                 # Get the year, month and date.
216                 $length = length($date);
217                 $seek = 0;
219                 do {
221                         # Get the character and check if it is a dash.
223                         $char = substr($date, $seek, 1);
225                         if ($char eq '-'){
227                                 # The character is a dash, so get the year, month or day.
229                                 $datelength = $seek - $startchar;
231                                 if ($count eq 0){
233                                         # Get the year from the date.
235                                         $year           = substr($date, 0, $datelength) + 1900;
236                                         $startchar      = $seek;
237                                         $count = 1;
239                                         # Get the last two characters to get the short year
240                                         # version.
242                                         $year_short     = substr($year, 2, 2);
244                                 } elsif ($count eq 1){
246                                         # Get the month and day from the date.
248                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
250                                         # Check if the month is less then 10, if it is
251                                         # add a zero to the value.
253                                         if ($month < 10){
255                                                 $month_full = '0' . $month;
257                                         } else {
259                                                 $month_full = $month;
261                                         }
263                                         $startchar      = $seek;
264                                         $count = 2;
266                                         $daylength      = $length - $seek + 1;
267                                         $day            = substr($date, $startchar + 1, $daylength);
269                                         # Check if the day is less than 10, if it is
270                                         # add a zero to the value.
272                                         if ($day < 10){
274                                                 $day_full       = '0' . $day;
276                                         } else {
278                                                 $day_full       = $day;
280                                         }
282                                 }
284                         }
286                         $seek++;
288                 } until ($seek eq $length);
290                 # Get the length of the time value and reset certain
291                 # values to 0.
293                 $length = length($time);
294                 $seek = 0;
295                 $count = 0;
296                 $startchar = 0;
298                 do {
300                         # Get the character and check if it is a colon.
302                         $char = substr($time, $seek, 1);
304                         if ($char eq ':'){
306                                 # The character is a colon, so get the hour, minute and day.
308                                 $timelength = $seek - $startchar;
310                                 if ($count eq 0){
312                                         # Get the hour from the time.
314                                         $hour = substr($time, 0, $timelength);
315                                         $count = 1;
316                                         $startchar = $seek;
318                                         # If the hour is less than ten then add a
319                                         # zero.
321                                         if ($hour < 10){
323                                                 $hour_full = '0' . $hour;
325                                         } else {
327                                                 $hour_full = $hour;
329                                         }
331                                 } elsif ($count eq 1){
333                                         # Get the minute and second from the time.
335                                         $minute = substr($time, $startchar + 1, $timelength - 1);
336                                         $count = 2;
337                                                 
338                                         # If the minute is less than ten then add a
339                                         # zero.
341                                         if ($minute < 10){
343                                                 $minute_full = '0' . $minute;
345                                         } else {
347                                                 $minute_full = $minute;
349                                         }
351                                         $startchar = $seek;
353                                         $secondlength = $length - $seek + 1;
354                                         $second = substr($time, $startchar + 1, $secondlength);
355                                         
356                                         # If the second is less than ten then add a
357                                         # zero.
359                                         if ($second < 10){
361                                                 $second_full = '0' . $second;
363                                         } else {
365                                                 $second_full = $second;
367                                         }
369                                 }
371                         }
373                         $seek++;
375                 } until ($seek eq $length);
377                 # Get the setting for displaying the date and time.
379                 $data = $options{"DateTime"};
381                 # Process the setting for displaying the date and time
382                 # using regular expressions
384                 $data =~ s/DD/$day_full/g;
385                 $data =~ s/D/$day/g;
386                 $data =~ s/MM/$month_full/g;
387                 $data =~ s/M/$month/g;
388                 $data =~ s/YY/$year/g;
389                 $data =~ s/Y/$year_short/g;
391                 $data =~ s/hh/$hour_full/g;
392                 $data =~ s/h/$hour/g;
393                 $data =~ s/mm/$minute_full/g;
394                 $data =~ s/m/$minute/g;
395                 $data =~ s/ss/$second_full/g;
396                 $data =~ s/s/$second/g;
398         }
400         return $data;
404 sub geterror{
405 #################################################################################
406 # geterror: Gets the error message (or extended error message).                 #
407 #                                                                               #
408 # Usage:                                                                        #
409 #                                                                               #
410 # $dbmodule->geterror(extended);                                                #
411 #                                                                               #
412 # Extended      Specifies if the extended error should be retrieved.            #
413 #################################################################################
415         # Get the data passed to the subroutine.
417         my $class       = shift;
418         my $extended    = shift;
420         if (!$extended){
421                 $extended = 0;
422         }
424         if (!$errorext){
425                 $errorext = "";
426         }
428         if (!$error){
429                 $error = "";
430         }
432         # Check to see if extended information should be returned.
434         if ($extended eq 1){
436                 # Extended information should be returned.
438                 return $errorext;
440         } else {
442                 # Basic information should be returned.
444                 return $error;
446         }
450 sub dbpermissions{
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid.           #
453 #                                                                               #
454 # Usage:                                                                        #
455 #                                                                               #
456 # $database->dbpermissions(dbname, read, write);                                #
457 #                                                                               #
458 # dbname        Specifies the database name to check.                           #
459 # read          Check to see if the database can be read.                       #
460 # write         Check to see if the database can be written.                    #
461 #################################################################################
463         # Get the database name, read setting and write setting.
465         my ($class, $dbname, $readper, $writeper)       = @_;
467         # Check if the database can be read.
469         if ($readper){
471                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473                         # The database can be read.
475                 } else {
477                         # The database cannot be read, so return a value
478                         # of 1.
480                         return 1;
482                 }
484         }
486         # Check if the database can be written.
488         if ($writeper){
490                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492                         # The database can be read.
494                 } else {
496                         # The database cannot be read, so return a value
497                         # of 1.
499                         return 1;
501                 }
503         }
505         # No errors have occured while checking so return a value
506         # of 0.
508         return 0;
512 sub dbexists{
513 #################################################################################
514 # dbexists: Check if the database exists.                                       #
515 #                                                                               #
516 # Usage:                                                                        #
517 #                                                                               #
518 # $dbmodule->dbexists(dbname);                                                  #
519 #                                                                               #
520 # dbname        Specifies the database name to check.                           #
521 #################################################################################
523         # Get the value that was passed to the subroutine.
525         my $class       = shift;
526         my ($filename)  = @_;
528         # Check if the filename exists, if it does, return a value of 1, else
529         # return a value of 0, meaning that the file was not found.
531         if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
533                 # Specified file does exist so return a value of 0.
535                 return 0;
537         } else {
539                 # Specified file does not exist so return a value of 1.
541                 return 1;
543         }
547 #################################################################################
548 # Database Subroutines.                                                         #
549 #################################################################################
551 sub getdblist{
552 #################################################################################
553 # getdblist: Gets the list of available databases.                              #
554 #                                                                               #
555 # Usage:                                                                        #
556 #                                                                               #
557 # $dbmodule->getdblist();                                                       #
558 #################################################################################
560         # Get the list of databases.
562         my @data_directory;
563         my @data_directory_final;
564         my $database;
565         my $database_filename_length;
566         my $database_filename_friendly;
568         # Check if the database directory has valid permission settings.
570         if (-e $options{"Directory"}){
572                 # The database directory does exist. So check if
573                 # the permission settings are valid.
575                 if (-r $options{"Directory"}){
577                         # The permission settings for reading the directory
578                         # are valid.
580                 } else {
582                         # The permission settings for reading the directory
583                         # are invalid so return an error value.
585                         $error = "DataDirInvalidPermissions";
586                         return;
588                 }
590         } else {
592                 # The database directory does not exist, so return an
593                 # error value.
595                 $error = "DataDirMissing";
596                 return;
598         }
600         opendir(DATADIR, $options{"Directory"});
601         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
602         closedir(DATADIR);
604         # Process the list of databases.
606         foreach $database (@data_directory){
608                 $database =~ s/.db.sqlite$//g;
609                 $database_filename_friendly = $database;
611                 #$database_filename_length = length($database);
612                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
613                 push(@data_directory_final, $database_filename_friendly);
615         }
617         # Return the list of databases.
619         return @data_directory_final;
623 sub getdatabaseinfo{
624 #################################################################################
625 # getdatabaseinfo: Get information about the database.                          #
626 #                                                                               #
627 # Usage:                                                                        #
628 #                                                                               #
629 # $dbmodule->getdatabaseinfo();                                                 #
630 #################################################################################
632         # Get the database information.
634         my $class = shift;
635         my ($databaseinfo, %databaseinfo);
636         my ($sqldata, @sqldata);
638         $error = "";
639         $errorext = "";
641         $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
642                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
643         );
644         $string_handle->execute();
646         @sqldata = $string_handle->fetchrow_array();
648         # Process the database information into a hash.
650         %databaseinfo = (
651                 "DatabaseName"  => $sqldata[0],
652                 "Description"   => $sqldata[1],
653                 "Notes"         => $sqldata[2],
654                 "Categories"    => $sqldata[3],
655                 "Major"         => $sqldata[4],
656                 "Minor"         => $sqldata[5],
657                 "Revision"      => $sqldata[6]
658         );
660         $string_handle->finish();
661         undef $string_handle;
663         return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be  #
670 # moved or copied to.                                                           #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getseconddatabaseinfo();                                           #
675 #################################################################################
677         # Get the database information.
679         my $class = shift;
680         my ($databaseinfo, %databaseinfo);
681         my ($sqldata, @sqldata);
683         $error = "";
684         $errorext = "";
686         $second_string_handle = $second_database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
687                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
688         );
689         $second_string_handle->execute();
691         @sqldata = $second_string_handle->fetchrow_array();
693         # Process the database information into a hash.
695         %databaseinfo = (
696                 "DatabaseName"  => $sqldata[0],
697                 "Description"   => $sqldata[1],
698                 "Notes"         => $sqldata[2],
699                 "Categories"    => $sqldata[3],
700                 "Major"         => $sqldata[4],
701                 "Minor"         => $sqldata[5],
702                 "Revision"      => $sqldata[6]
703         );
705         $second_string_handle->finish();
706         undef $second_string_handle;
708         return %databaseinfo;
712 sub adddatabase{
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database.                                       #
715 #                                                                               #
716 # Usage:                                                                        #
717 #                                                                               #
718 # $dbmodule->adddatabase(options);                                              #
719 #                                                                               #
720 # options       Specifies the following options in any order.                   #
721 #                                                                               #
722 # DatabaseFilename      Specifies the database file/shortname to use.           #
723 # DatabaseName          Specifies the database name to use.                     #
724 # DatabaseDescription   Specifies the database description to use.              #
725 # DatabaseNotes         Specifies the database notes to use.                    #
726 # DatabaseCategories    Specifies the database categories to use.               #
727 # VersionMajor          Specifies the major version.                            #
728 # VersionMinor          Specifies the minor version.                            #
729 # VersionRevision       Specifies the revision version.                         #
730 #################################################################################
732         # Get the database that was passed to the subroutine.
734         $error  = "";
735         $errorext = "";
737         my $class       = shift;
738         my ($passedoptions) = @_;
740         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
741         my $dbname              = $passedoptions->{"DatabaseName"};
742         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
743         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
744         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
745         my $dbmajorver          = $passedoptions->{"VersionMajor"};
746         my $dbminorver          = $passedoptions->{"VersionMinor"};
747         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
749         # Check if the database with the filename given already exists.
751         my $database_exists     = $class->dbexists($dbfilename);
753         if ($database_exists eq 0){
755                 # The database filename exists so set the error value.
757                 $error = "DatabaseExists";
758                 return;
760         }
762         # Create the database structure.
764         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765         $database_handle->{unicode} = 1;
766         $string_handle          = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767                         name varchar(256) primary key, 
768                         description varchar(512), 
769                         notes text, 
770                         categories varchar(512), 
771                         kiriwrite_version_major int(4), 
772                         kiriwrite_version_minor int(4), 
773                         kiriwrite_version_revision int(4)
774         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775         $string_handle->execute();
777         $string_handle  = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778                         filename varchar(256) primary key, 
779                         pagename varchar(512), 
780                         pagedescription varchar(512), 
781                         pagesection varchar(256),
782                         pagetemplate varchar(64),
783                         pagedata text,
784                         pagesettings int(1),
785                         lastmodified datetime
786         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787         $string_handle->execute();
789         # Convert the values into SQL query formatted values and add an entry
790         # to the kiriwrite_database_info table.
792         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793                 \'' . $class->convert($dbname) . '\',
794                 \'' . $class->convert($dbdescription) . '\',
795                 \'' . $class->convert($dbnotes) . '\',
796                 \'' . $class->convert($dbcategories) . '\',
797                 \'' . $class->convert($dbmajorver) . '\',
798                 \'' . $class->convert($dbminorver) . '\',
799                 \'' . $class->convert($dbrevisionver) . '\'
800         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801         $string_handle->execute();
805 sub editdatabase{
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database.                                     #
808 #                                                                               #
809 # Usage:                                                                        #
810 #                                                                               #
811 # $dbmodule->editdatabase(options);                                             #
812 #                                                                               #
813 # options               Specifies the following options in any order.           #
814 #                                                                               #
815 # NewDatabaseFilename   Specifies the new database filename to use.             #
816 # DatabaseName          Specifies the new database name.                        #
817 # DatabaseDescription   Specifies the new database description.                 #
818 # DatabaseNotes         Specifies the new database notes.                       #
819 # DatabaseCategories    Specifies the new database categories.                  #
820 #################################################################################
822         $error          = "";
823         $errorext       = "";
825         my $class       = shift;
826         my ($passedoptions) = @_;
828         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
829         my $dbname              = $passedoptions->{"DatabaseName"};
830         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
831         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
832         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
834         # Check if a new database filename has been specified and if a
835         # new database filename has been specified then change the
836         # database filename.
838         if ($database_filename ne $dbnewfilename){
840                 # A new database filename has been given so check if the output
841                 # directory has write access.
843                 if (-r $options{"Directory"}){
844                         
845                         # The directory is readable.
847                 } else {
849                         # The directory is not readable so set the error value.
851                         $error = "DataDirInvalidPermissions";
853                         return;
855                 }
857                 if (-w $options{"Directory"}){
859                         # The directory is writeable.
861                 } else {
863                         # The directory is not writeable so set the error value.
865                         $error = "DataDirInvalidPermissions";
867                         return;
869                 }
871                 # Check if a database filename already exists before using the
872                 # new filename.
874                 my $database_newexists          = $class->dbexists($dbnewfilename);
876                 if ($database_newexists eq 0){
878                         # The database filename exists so set the error value.
880                         $error = "DatabaseExists";
881                         return;
883                 }
885                 # Check if the database can be renamed (has write access).
887                 my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
889                 if ($database_permissions eq 1){
891                         # The database filename exists so set the error value.
893                         $error = "InvalidPermissionsSet";
894                         return;
896                 }
898                 # "Disconnect" from the database.
900                 $database_handle->disconnect();
902                 # Rename the database.
904                 ($database_filename)    = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
905                 ($dbnewfilename)        = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
907                 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
909                 # Reload the database from the new filename.
911                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
912                 $database_handle->{unicode} = 1;
913                 $database_filename = $dbnewfilename;
915         }
917         # Check if the database can be altered with the new data.
919         my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
921         if ($database_permissions eq 1){
923                 # The database filename exists so set the error value.
925                 $error = "InvalidPermissionsSet";
926                 return;
928         }
930         # Get the current database information.
932         $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933         $string_handle->execute();
935         my @database_oldinfo    = $string_handle->fetchrow_array();
937         my $dboldname           = $database_oldinfo[0];
939         # Update the database information.
941         $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942         description = \'' . $class->convert($dbdescription) . '\',
943         notes = \'' . $class->convert($dbnotes) . '\',
944         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945         $string_handle->execute();
947         undef $string_handle;
948         return;
952 sub deletedatabase{
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database.                                 #
955 #                                                                               #
956 # Usage:                                                                        #
957 #                                                                               #
958 # $dbmodule->deletedatabase(options);                                           #
959 #                                                                               #
960 # options       Specifies the following options in any order.                   #
961 #                                                                               #
962 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
963 #################################################################################
965         $error          = "";
966         $errorext       = "";
968         # Get the database filename.
970         my $class               = shift;
971         my ($passedoptions)     = shift;
973         my $databasename        = $passedoptions->{"DatabaseName"};
975         # Check if the database exists.
977         my $database_exists             = $class->dbexists($databasename);
979         if ($database_exists eq 1){
981                 # The database does not exist so set the error value.
983                 $error = "DoesNotExist";
984                 return;
986         }
988         # Check if the database permissions are valid.
990         my $database_permissions        = $class->dbpermissions($databasename);
992         if ($database_permissions eq 1){
994                 # The database permissions are invalid so set the error
995                 # value.
997                 $error = "InvalidPermissionsSet";
998                 return;
1000         }
1002         # Delete the database.
1004         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1010 sub selectdb{
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database.                                     #
1013 #                                                                               #
1014 # Usage:                                                                        #
1015 #                                                                               #
1016 # $dbmodule->connect(options);                                                  #
1017 #                                                                               #
1018 # options       Specifies the following options in any order.                   #
1019 #                                                                               #
1020 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1021 #################################################################################
1023         # Get the database name.
1025         $error = "";
1026         $errorext = "";
1028         my $class = shift;
1029         my ($passedoptions) = @_;
1030         my (%database, $database);
1032         my $dbname = $passedoptions->{"DatabaseName"};
1034         # Check if the database exists.
1036         my $database_exists = $class->dbexists($dbname);
1038         if ($database_exists eq 1){
1040                 # The database does not exist so return an error value
1041                 # saying that the database does not exist.
1043                 $error = "DoesNotExist";
1045                 return;
1047         }
1049         # Check if the database has valid permissions set.
1051         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1053         if ($database_permissions eq 1){
1055                 # The database has invalid permissions set so return
1056                 # an error value saying that the database has invalid
1057                 # permissions set.
1059                 $error = "InvalidPermissionsSet";
1060                 
1061                 return;
1063         }
1065         # Connect to the database.
1067         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1068         $database_handle->{unicode} = 1;
1069         $database_filename = $dbname;
1073 sub selectseconddb{
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1076 # pages to.                                                                     #
1077 #                                                                               #
1078 # Usage:                                                                        #
1079 #                                                                               #
1080 # $dbmodule->selectseconddb(options);                                           #
1081 #                                                                               #
1082 # options       Specifies the following options in any order.                   #
1083 #                                                                               #
1084 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1085 #################################################################################
1087         # Get the database name.
1089         $error = "";
1090         $errorext = "";
1092         my $class = shift;
1093         my ($passedoptions) = @_;
1094         my (%database, $database);
1096         my $dbname = $passedoptions->{"DatabaseName"};
1098         # Check if the database exists.
1100         my $database_exists = $class->dbexists($dbname);
1102         if ($database_exists eq 1){
1104                 # The database does not exist so return an error value
1105                 # saying that the database does not exist.
1107                 $error = "DoesNotExist";
1109                 return;
1111         }
1113         # Check if the database has valid permissions set.
1115         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1117         if ($database_permissions eq 1){
1119                 # The database has invalid permissions set so return
1120                 # an error value saying that the database has invalid
1121                 # permissions set.
1123                 $error = "InvalidPermissionsSet";
1124                 
1125                 return;
1127         }
1129         # Connect to the database.
1131         $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1132         $second_database_handle->{unicode} = 1;
1133         $second_database_filename = $dbname;    
1137 #################################################################################
1138 # Page subroutines.                                                             #
1139 #################################################################################
1141 sub getpagelist{
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database.                        #
1144 #                                                                               #
1145 # Usage:                                                                        #
1146 #                                                                               #
1147 # $dbmodule->getpagelist();                                                     #
1148 #################################################################################
1150         $error = "";
1151         $errorext = "";
1153         my $class       = shift;
1154         
1155         $string_handle  = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1156         $string_handle->execute();
1158         my @database_pagefilenames;
1159         my @database_pagefilenames_final;
1161         # Process the collected pages.
1163         while (@database_pagefilenames = $string_handle->fetchrow_array){
1165                 # Add each page to the list of pages in the database.
1167                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1169         }
1171         undef $string_handle;
1172         return @database_pagefilenames_final;
1176 sub getpageinfo{
1177 #################################################################################
1178 # getpageinfo: Gets the page information from the filename passed.              #
1179 #                                                                               #
1180 # Usage:                                                                        #
1181 #                                                                               #
1182 # $dbmodule->getpageinfo(options);                                              #
1183 #                                                                               #
1184 # options       Specifies the following options in any order.                   #
1185 #                                                                               #
1186 # PageFilename  Specifies the page filename to get the page information from.   #
1187 #################################################################################
1189         $error = "";
1190         $errorext = "";
1192         my $class               = shift;
1193         my ($passedoptions)     = shift;
1194         my (%database_page, $database_page);
1195         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1197         my @data_page;
1198         my $page_found = 0;
1200         # Get the page from the database.
1202         my $page_filename       = $passedoptions->{"PageFilename"};
1204         $string_handle  = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1205         $string_handle->execute();
1207         # Check if the page exists in the database.
1209         while (@data_page = $string_handle->fetchrow_array()){
1211                 # Get the values from the array.
1213                 $pagefilename           = $data_page[0];
1214                 $pagename               = $data_page[1];
1215                 $pagedescription        = $data_page[2];
1216                 $pagesection            = $data_page[3];
1217                 $pagetemplate           = $data_page[4];
1218                 $pagedata               = $data_page[5];
1219                 $pagesettings           = $data_page[6];
1220                 $pagelastmodified       = $data_page[7];
1222                 # Put the values into the page hash.
1224                 %database_page = (
1225                         "PageFilename"          => $pagefilename,
1226                         "PageName"              => $pagename,
1227                         "PageDescription"       => $pagedescription,
1228                         "PageSection"           => $pagesection,
1229                         "PageTemplate"          => $pagetemplate,
1230                         "PageContent"           => $pagedata,
1231                         "PageSettings"          => $pagesettings,
1232                         "PageLastModified"      => $class->dateconvert($pagelastmodified),
1233                 );
1235                 $page_found = 1;
1237         }
1238         
1239         # Check if the page did exist.
1241         if (!$page_found){
1243                 $error = "PageDoesNotExist";
1244                 return;
1246         }
1248         undef $string_handle;
1249         return %database_page;
1253 sub addpage{
1254 #################################################################################
1255 # addpage: Add a page to the selected database.                                 #
1256 #                                                                               #
1257 # Usage:                                                                        #
1258 #                                                                               #
1259 # $dbmodule->addpage(options);                                                  #
1260 #                                                                               #
1261 # options       Specifies the following options in any order.                   #
1262 #                                                                               #
1263 # PageFilename          Specifies the page filename to use.                     #
1264 # PageName              Specifies the page name to use.                         #
1265 # PageDescription       Specifies the page description to use.                  #
1266 # PageSection           Specifies the page section to use.                      #
1267 # PageTemplate          Specifies the page template to use.                     #
1268 # PageContent           Specifies the page content to use.                      #
1269 # PageSettings          Specifies the page settings to use.                     #
1270 #################################################################################
1272         # Get the data that was passed to the subroutine.
1274         $error = "";
1275         $errorext = "";
1277         my $class               = shift;
1278         my ($passedoptions)     = shift;
1280         my @database_page;
1281         my $page_count = 0;
1283         # Get the values passed to the hash.
1285         my $page_filename       = $passedoptions->{"PageFilename"};
1286         my $page_name           = $passedoptions->{"PageName"};
1287         my $page_description    = $passedoptions->{"PageDescription"};
1288         my $page_section        = $passedoptions->{"PageSection"};
1289         my $page_template       = $passedoptions->{"PageTemplate"};
1290         my $page_content        = $passedoptions->{"PageContent"};
1291         my $page_settings       = $passedoptions->{"PageSettings"};
1293         # Check to see if the filename given already exists
1294         # in the page database.
1296         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1297         $string_handle->execute();
1299         # Check if a page with the filename given really does
1300         # exist.
1302         while (@database_page = $string_handle->fetchrow_array()){
1304                 # A page does exist so increment the count to 1.
1306                 $page_count++;
1307                 
1308         }
1310         if ($page_count ne 0){
1312                 # The page does exist so set the error value.
1314                 $error = "PageExists";
1315                 return;
1317         }
1319         # Check if certain values are undefined.
1321         if (!$page_name){
1323                 $page_name = "";
1325         }
1327         if (!$page_description){
1329                 $page_description = "";
1331         }
1333         if (!$page_section){
1335                 $page_section = "";
1337         }
1339         if (!$page_content){
1341                 $page_content = "";
1343         }
1345         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1346         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1348         # Add the page to the selected database.
1350         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1351                 \'' . $class->convert($page_filename) . '\',
1352                 \'' . $class->convert($page_name) . '\',
1353                 \'' . $class->convert($page_description) . '\',
1354                 \'' . $class->convert($page_section) . '\',
1355                 \'' . $class->convert($page_template) . '\',
1356                 \'' . $class->convert($page_content) . '\',
1357                 \'' . $class->convert($page_settings) . '\',
1358                 \'' . $class->convert($page_date) . '\'
1359         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1360         $string_handle->execute();
1362         undef $string_handle;
1366 sub deletepage{
1367 #################################################################################
1368 # deletepage: Delete a page from the selected database.                         #
1369 #                                                                               #
1370 # Usage:                                                                        #
1371 #                                                                               #
1372 # $dbmodule->deletepage(options)                                                #
1373 #                                                                               #
1374 # options       Specifies the following options in any order.                   #
1375 #                                                                               #
1376 # PageFilename  Specifies the page filename to delete.                          #
1377 #################################################################################
1379         $error = "";
1380         $errorext = "";
1382         # Get the data that was passed to the subroutine.
1384         my $class = shift;
1385         my ($passedoptions) = @_;
1386         my @page_info;
1387         my $page_found = 0;
1389         # Get the page filename.
1391         my $page_filename = $passedoptions->{"PageFilename"};
1393         # Check if the page exists before deleting it.
1395         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1396         $string_handle->execute();
1398         while (@page_info = $string_handle->fetchrow_array()){
1400                 $page_found = 1;
1402         }
1404         # Check if the page really does exist.
1406         if (!$page_found){
1408                 $error = "PageDoesNotExist";
1409                 return;
1411         }
1413         # Delete the page.
1415         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1416         $string_handle->execute();
1420 sub editpage{
1421 #################################################################################
1422 # editpage: Edit a page from the selected database.                             #
1423 #                                                                               #
1424 # Usage:                                                                        #
1425 #                                                                               #
1426 # $dbmodule->editpage(options);                                                 #
1427 #                                                                               #
1428 # options       Specifies the following options in any order.                   #
1429 #                                                                               #
1430 # PageFilename          Specifies the filename to edit.                         #
1431 # PageNewFilename       Specifies the new filename to use.                      #
1432 # PageNewName           Specifies the new page name to use.                     #
1433 # PageNewDescription    Specifies the new page description to use.              #
1434 # PageNewSection        Specifies the new page section to use.                  #
1435 # PageNewTemplate       Specifies the new page template to use.                 #
1436 # PageNewContent        Specifies the new page content to use.                  #
1437 # PageNewSettings       Specifies the new page settings to use.                 #
1438 #################################################################################
1440         $error = "";
1441         $errorext = "";
1443         # Get the values passed to the subroutine.
1445         my $class = shift;
1446         my ($passedoptions) = @_;
1447         my $page_found = 0;
1448         my @page_info;
1450         # Get the data that was passed to the subroutine.
1452         my $page_filename       = $passedoptions->{"PageFilename"};
1453         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1454         my $page_newname        = $passedoptions->{"PageNewName"};
1455         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1456         my $page_newsection     = $passedoptions->{"PageNewSection"};
1457         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1458         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1459         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1461         # Check if the page with the filename given exists.
1463         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1464         $string_handle->execute();
1466         # Check if the page really does exist.
1468         while (@page_info = $string_handle->fetchrow_array()){
1470                 # The page information is found.
1472                 $page_found = 1;
1474         }
1476         # Check if the page really does exist.
1478         if (!$page_found){
1480                 $error = "PageDoesNotExist";
1481                 return;
1483         }
1485         # Check if there is a page that already exists with the new
1486         # filename.
1488         if ($page_filename ne $page_newfilename){
1490                 $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1491                 $string_handle->execute();
1493                 # Check if a page really is using the new filename.
1495                 while (@page_info = $string_handle->fetchrow_array()){
1497                         # The page information is found.
1499                         $page_found = 1;
1501                 }
1503                 if ($page_found eq 1){
1505                         $error = "PageExists";
1506                         return;
1508                 }
1510         }
1512         # Get the current date.
1514         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1515         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1517         # Edit the selected page.
1519         $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_pages SET filename = \'' . $class->convert($page_newfilename) . '\', pagename = \'' . $class->convert($page_newname) . '\', pagedescription = \'' . $class->convert($page_newdescription) . '\', pagesection = \'' . $class->convert($page_newsection) . '\', pagetemplate = \'' . $class->convert($page_newtemplate) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagesettings = \'' . $class->convert($page_newsettings) . '\', lastmodified = \'' . $page_date . '\' WHERE filename = \'' . $class->convert($page_filename) . '\'')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1520         $string_handle->execute();
1524 sub movepage{
1525 #################################################################################
1526 # movepage: Moves a page from the old database to the new database.             #
1527 #                                                                               #
1528 # Usage:                                                                        #
1529 #                                                                               #
1530 # $dbmodule->movepage(options);                                                 #
1531 #                                                                               #
1532 # options       Specifies the following options in any order.                   #
1533 #                                                                               #
1534 # PageFilename  Specifies the page with the filename to move.                   #
1535 #################################################################################
1537         $error = "";
1538         $errorext = "";
1540         # Get the values passed to the subroutine.
1542         my $class = shift;
1543         my ($passedoptions) = @_;
1545         my (%database_page, $database_page);
1546         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1547         my @page_info;
1548         my $page_found = 0;
1550         # Get the data that was passed to the subroutine.
1552         my $page_filename = $passedoptions->{"PageFilename"};
1554         # Check if the page with the filename given exists.
1556         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1557         $string_handle->execute();
1559         # Check if the page really does exist.
1561         while (@page_info = $string_handle->fetchrow_array()){
1563                 # Get the values from the array.
1565                 $pagefilename           = $page_info[0];
1566                 $pagename               = $page_info[1];
1567                 $pagedescription        = $page_info[2];
1568                 $pagesection            = $page_info[3];
1569                 $pagetemplate           = $page_info[4];
1570                 $pagedata               = $page_info[5];
1571                 $pagesettings           = $page_info[6];
1572                 $pagelastmodified       = $page_info[7];
1574                 # Put the values into the page hash.
1576                 %database_page = (
1577                         "PageFilename"          => $pagefilename,
1578                         "PageName"              => $pagename,
1579                         "PageDescription"       => $pagedescription,
1580                         "PageSection"           => $pagesection,
1581                         "PageTemplate"          => $pagetemplate,
1582                         "PageContent"           => $pagedata,
1583                         "PageSettings"          => $pagesettings,
1584                         "PageLastModified"      => $pagelastmodified,
1585                 );
1587                 # The page information is found.
1589                 $page_found = 1;
1591         }
1593         # Check if the page really does exist.
1595         if (!$page_found){
1597                 $error = "PageDoesNotExist";
1598                 return;
1600         }
1602         # Check if the page with the filename given already exists in
1603         # the database the page is being moved to.
1605         $page_found = 0;
1606         @page_info = ();
1608         $second_string_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1609         $second_string_handle->execute();
1611         while (@page_info = $second_string_handle->fetchrow_array()){
1613                 $page_found = 1;
1615         }
1616         
1617         # Check if the page really does exist.
1619         if ($page_found){
1621                 $error = "PageAlreadyExists";
1622                 return;
1624         }
1626         # Add the page to the new database.
1628         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1629                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1630                 \'' . $class->convert($database_page{"PageName"}) . '\',
1631                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1632                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1633                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1634                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1635                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1636                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1637         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1638         $second_string_handle->execute();
1640         # Delete the page from the old database.
1642         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1643         $string_handle->execute();
1647 sub copypage{
1648 #################################################################################
1649 # copypage: Copies a page from the old database to the new database.            #
1650 #                                                                               #
1651 # Usage:                                                                        #
1652 #                                                                               #
1653 # $dbmodule->copypage(options);                                                 #
1654 #                                                                               #
1655 # options       Specifies the following options in any order.                   #
1656 #                                                                               #
1657 # PageFilename  Specifies the page with the filename to copy.                   #
1658 #################################################################################
1660         $error = "";
1661         $errorext = "";
1663         # Get the values passed to the subroutine.
1665         my $class = shift;
1666         my ($passedoptions) = @_;
1668         my (%database_page, $database_page);
1669         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1670         my @page_info;
1671         my $page_found = 0;
1673         # Get the data that was passed to the subroutine.
1675         my $page_filename = $passedoptions->{"PageFilename"};
1677         # Check if the page with the filename given exists.
1679         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1680         $string_handle->execute();
1682         # Check if the page really does exist.
1684         while (@page_info = $string_handle->fetchrow_array()){
1686                 # Get the values from the array.
1688                 $pagefilename           = $page_info[0];
1689                 $pagename               = $page_info[1];
1690                 $pagedescription        = $page_info[2];
1691                 $pagesection            = $page_info[3];
1692                 $pagetemplate           = $page_info[4];
1693                 $pagedata               = $page_info[5];
1694                 $pagesettings           = $page_info[6];
1695                 $pagelastmodified       = $page_info[7];
1697                 # Put the values into the page hash.
1699                 %database_page = (
1700                         "PageFilename"          => $pagefilename,
1701                         "PageName"              => $pagename,
1702                         "PageDescription"       => $pagedescription,
1703                         "PageSection"           => $pagesection,
1704                         "PageTemplate"          => $pagetemplate,
1705                         "PageContent"           => $pagedata,
1706                         "PageSettings"          => $pagesettings,
1707                         "PageLastModified"      => $pagelastmodified,
1708                 );
1710                 # The page information is found.
1712                 $page_found = 1;
1714         }
1716         # Check if the page really does exist.
1718         if (!$page_found){
1720                 $error = "PageDoesNotExist";
1721                 return;
1723         }
1725         # Check if the page with the filename given already exists in
1726         # the database the page is being moved to.
1728         $page_found = 0;
1729         @page_info = ();
1731         $second_string_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1732         $second_string_handle->execute();
1734         while (@page_info = $second_string_handle->fetchrow_array()){
1736                 $page_found = 1;
1738         }
1739         
1740         # Check if the page really does exist.
1742         if ($page_found){
1744                 $error = "PageAlreadyExists";
1745                 return;
1747         }
1749         # Add the page to the new database.
1751         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1752                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1753                 \'' . $class->convert($database_page{"PageName"}) . '\',
1754                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1755                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1756                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1757                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1758                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1759                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1760         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1761         $second_string_handle->execute();
1765 #################################################################################
1766 # Filter subroutines.                                                           #
1767 #################################################################################
1769 sub connectfilter{
1770 #################################################################################
1771 # connectfilter: Connect to the filter database.                                #
1772 #                                                                               #
1773 # Usage:                                                                        #
1774 #                                                                               #
1775 # $dbmodule->connectfilter(missingignore);                                      #
1776 #                                                                               #
1777 # missingignore Ignore error about database being missing.                      #
1778 #################################################################################
1780         $error = "";
1781         $errorext = "";
1783         my $class = shift;
1784         my $ignoremissing = shift;
1786         # Check if the template database exists.
1788         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1789         
1790         if ($filterdatabase_exists eq 1){
1792                 $filterdb_exists = 0;
1794                 if (!$ignoremissing){
1796                         $error = "FilterDatabaseDoesNotExist";
1797                         return;
1799                 }
1801         }
1803         # Check if the permission settings for the template database are valid.
1805         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1807         if ($filterdb_permissions eq 1){
1809                 # The template database has invalid permissions set
1810                 # so return an error value.
1812                 if (!$ignoremissing){
1814                         $error = "FilterDatabaseInvalidPermissionsSet";
1815                         return;
1817                 }
1819         }
1821         # Connect to the template database.
1823         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1824         $filterdb_database_handle->{unicode} = 1;
1825         $filterdb_loaded = 1;
1829 sub disconnectfilter{
1830 #################################################################################
1831 # disconnectfilter: Disconnect from the filter database.                        #
1832 #                                                                               #
1833 # Usage:                                                                        #
1834 #                                                                               #
1835 # $dbmodule->disconnectfilter();                                                #
1836 #################################################################################
1838         # Disconnect the template database.
1840         if ($filterdb_loaded eq 1){
1842                 undef $filterdb_string_handle;
1843                 $filterdb_database_handle->disconnect();
1845         }
1849 sub getfilterlist{
1850 #################################################################################
1851 # getfilterlist: Gets the list of filters in the filter database.               #
1852 #                                                                               #
1853 # Usage:                                                                        #
1854 #                                                                               #
1855 # $dbmodule->getfilterlist();                                                   #
1856 #################################################################################
1858         $error = "";
1859         $errorext = "";
1861         my @filter_list;
1862         my @filter_data;
1864         # Get the list of filters available.
1866         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1867         $filterdb_string_handle->execute();
1869         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1871                 # Add the filter to the list of available filters.
1873                 push(@filter_list, $filter_data[0]);
1875         }
1877         undef $filterdb_string_handle;
1878         return @filter_list;
1882 sub getfilterinfo{
1883 #################################################################################
1884 # getfilterinfo: Gets information about the filter.                             #
1885 #                                                                               #
1886 # Usage:                                                                        #
1887 #                                                                               #
1888 # $dbmodule->getfilterinfo(options);                                            #
1889 #                                                                               #
1890 # options       Specifies the following options in any order.                   #
1891 #                                                                               #
1892 # FilterID      Specifies the filter ID number to get information from.         #
1893 #################################################################################
1895         $error = "";
1896         $errorext = "";
1898         # Get the values passed to the subroutine.
1900         my $class               = shift;
1901         my ($passedoptions)     = @_;
1903         my %filter_info;
1904         my $filter_exists       = 0;
1905         my @filter_data;
1907         # Get the values that are in the hash.
1909         my $filter_id           = $passedoptions->{"FilterID"};
1911         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1912         $filterdb_string_handle->execute();
1914         # Get the filter information.
1916         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1918                 $filter_info{"FilterID"}        = $filter_data[0];
1919                 $filter_info{"FilterPriority"}  = $filter_data[1];
1920                 $filter_info{"FilterFind"}      = $filter_data[2];
1921                 $filter_info{"FilterReplace"}   = $filter_data[3];
1922                 $filter_info{"FilterNotes"}     = $filter_data[4];
1924                 $filter_exists = 1;
1926         }
1928         # Check if the filter exists.
1930         if (!$filter_exists){
1932                 # The filter does not exist so return
1933                 # an error value.
1935                 $error = "FilterDoesNotExist";
1936                 return;
1938         }
1940         # Return the filter information.
1942         undef $filterdb_string_handle;
1943         return %filter_info;
1947 sub addfilter{
1948 #################################################################################
1949 # addfilter: Adds a filter to the filter database.                              #
1950 #                                                                               #
1951 # Usage:                                                                        #
1952 #                                                                               #
1953 # $dbmodule->addfilter(options);                                                #
1954 #                                                                               #
1955 # options       Specifies the following options in any order.                   #
1956 #                                                                               #
1957 # FindFilter    Specifies the find filter to add.                               #
1958 # ReplaceFilter Specifies the replace filter to add.                            #
1959 # Priority      Specifies the filter priority to use.                           #
1960 # Notes         Specifies the notes to use.                                     #
1961 #################################################################################
1963         $error = "";
1964         $errorext = "";
1966         # Get the values passed to the subroutine.
1968         my $class = shift;
1969         my ($passedoptions) = @_;
1971         # Define some variables for later.
1973         my @database_filters;
1974         my @filterid_list;
1975         my @filterid_check;
1976         my $nofiltertable = 0;
1977         my $filter_found = 0;
1978         my $filter_count = 0;
1979         my $filter_id;
1980         my $new_id;
1982         # Get the values from the hash.
1984         my $filter_find         = $passedoptions->{"FindFilter"};
1985         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
1986         my $filter_priority     = $passedoptions->{"Priority"};
1987         my $filter_notes        = $passedoptions->{"Notes"};
1989         # Check if the filter database permissions are valid.
1991         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
1992         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
1994         if ($filterdb_permissions eq 1){
1996                 if ($filterdb_exists eq 0){
1997                         $error = "FilterDatabaseInvalidPermissionsSet";
1998                         return;
1999                 }
2001         }
2003         # Check if certain values are undefined and if they
2004         # are then set them blank.
2006         if (!$filter_find){
2008                 $filter_find = "";
2010         }
2012         if (!$filter_replace){
2014                 $filter_replace = "";
2016         }
2018         if (!$filter_priority){
2020                 $filter_priority = 1;
2022         }
2024         if (!$filter_notes){
2026                 $filter_notes = "";
2028         }
2030         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2032         if ($directory_permissions eq 1 && $filterdb_exists){
2034                 # The template database cannot be created because of invalid directory
2035                 # permissions so return an error value.
2037                 $error = "FilterDatabaseFileUncreateable";
2038                 return; 
2040         }
2042         # Check if the filter table exists.
2044         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2046         # Check if there is really no filter table.
2048         if ($nofiltertable){
2050                 # Create the filter database table.
2052                 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2053                         id int(7) primary key,
2054                         priority int(5),
2055                         findsetting varchar(1024),
2056                         replacesetting varchar(1024),
2057                         notes text
2058                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2059                 $filterdb_string_handle->execute();
2061         }
2063         # Find the lowest filter identification number available.
2065         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2066         $filterdb_string_handle->execute();
2068         while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2070                 $filter_id      = $database_filters[0];
2072                 # Add the filter identification to the list of filter IDs.
2074                 push(@filterid_list, $filter_id);
2076         }
2078         $filter_id = "";
2080         # Process each filter looking for a blank available filter.
2082         foreach $filter_id (@filterid_list){
2084                 # Check the next filter ID to see if it's blank.
2086                 $new_id = $filter_id + 1;
2088                 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2089                 $filterdb_string_handle->execute();
2091                 # Get the filter identification number.
2093                 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2095                         $filter_found = 1;
2097                 }
2099                 # Check if a filter was found.
2101                 if (!$filter_found){
2103                         # No filter was found using this ID so exit the loop.
2105                         last;
2107                 }
2109                 # Increment the filter count and reset the filter found value.
2111                 $filter_count++;
2112                 $filter_found = 0;
2113                 $new_id = 0;
2115         }
2117         # Check if there were any filters in the filters database.
2119         if (!$filter_count && !$new_id){
2121                 # There were no filters in the filters database so set
2122                 # the new filter identification value to 1.
2124                 $new_id = 1;
2126         }
2128         # Add the filter to the filter database.
2130         $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2131                 \'' . $class->convert($new_id) . '\',
2132                 \'' . $class->convert($filter_priority) . '\',
2133                 \'' . $class->convert($filter_find) . '\',
2134                 \'' . $class->convert($filter_replace) .'\',
2135                 \'' . $class->convert($filter_notes) . '\'
2136         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2137         $filterdb_string_handle->execute();
2141 sub editfilter{
2142 #################################################################################
2143 # editfilter: Edits a filter in the filter database.                            #
2144 #                                                                               #
2145 # Usage:                                                                        #
2146 #                                                                               #
2147 # $dbmodule->editfilter(options);                                               #
2148 #                                                                               #
2149 # options       Specifies the following options in any order.                   #
2150 #                                                                               #
2151 # FilterID              Specifies the filter to edit.                           #
2152 # NewFindFilter         Specifies the new find filter setting.                  #
2153 # NewReplaceFilter      Specifies the new replace filter setting.               #
2154 # NewFilterPriority     Specifies the new filter priority setting.              #
2155 # NewFilterNotes        Specifies the new notes for the filter.                 #
2156 #################################################################################
2158         $error = "";
2159         $errorext = "";
2161         # Get the values passed to the subroutine.
2163         my $class = shift;
2164         my ($passedoptions) = @_;
2166         my @filter_data;
2167         my $filter_exists = 1;
2168         my $blankfile = 0;
2170         # Get the values from the hash.
2172         my $filter_id           = $passedoptions->{"FilterID"};
2173         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2174         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2175         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2176         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2178         # Check if the filter database permissions are valid.
2180         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2181         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2183         if ($filterdb_permissions eq 1){
2185                 if ($filterdb_exists eq 0){
2186                         $error = "FilterDatabaseInvalidPermissionsSet";
2187                         return;
2188                 }
2190         }
2192         # Check if the filter exists before editing it.
2194         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2195         $filterdb_string_handle->execute();
2197         # Check if the filter exists.
2199         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2201                 $filter_exists = 1;
2203         }       
2205         # Check if the filter really does exist.
2207         if (!$filter_exists){
2209                 # The filter does not exist so return
2210                 # an error value.
2212                 $error = "FilterDoesNotExist";
2213                 return;
2215         }
2217         # Edit the selected filter.
2219         $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2220                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2221                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2222                 priority = \'' . $class->convert($filter_newpriority) . '\',
2223                 notes = \'' . $class->convert($filter_newnotes) . '\'
2224         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2225         $filterdb_string_handle->execute();
2227         undef $filterdb_string_handle;
2228         return;
2232 sub deletefilter{
2233 #################################################################################
2234 # deletefilter: Deletes a filter from the filter database.                      #
2235 #                                                                               #
2236 # Usage:                                                                        #
2237 #                                                                               #
2238 # $dbmodule->deletefilter(options);                                             #
2239 #                                                                               #
2240 # options       Specifies the following options in any order.                   #
2241 #                                                                               #
2242 # FilterID      Specifies the filter to delete from the filter database.        #
2243 #################################################################################
2245         $error = "";
2246         $errorext = "";
2248         # Get the values passed to the subroutine.
2250         my $class = shift;
2251         my ($passedoptions) = @_;
2253         my $filter_exists = 0;
2254         my @filter_data;
2256         # Get the values from the hash.
2258         my $filter_id           = $passedoptions->{"FilterID"};
2260         # Check if the filter exists before deleting.
2262         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2263         $filterdb_string_handle->execute();
2265         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2267                 $filter_exists = 1;
2269         }
2271         # Check to see if the filter really does exist.
2273         if (!$filter_exists){
2275                 $error = "FilterDoesNotExist";
2276                 return;
2278         }
2280         # Delete the filter from the filter database.
2282         $filterdb_string_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2283         $filterdb_string_handle->execute();
2285         undef $filterdb_string_handle;
2289 #################################################################################
2290 # Template subroutines.                                                         #
2291 #################################################################################
2293 sub connecttemplate{
2294 #################################################################################
2295 # connecttemplate: Connect to the template database.                            #
2296 #                                                                               #
2297 # Usage:                                                                        #
2298 #                                                                               #
2299 # $dbmodule->connecttemplate(missingignore);                                    #
2300 #                                                                               #
2301 # missingignore Ignore errror about database being missing.                     #
2302 #################################################################################
2304         $error = "";
2305         $errorext = "";
2307         my $class = shift;
2308         my $ignoremissing = shift;
2310         # Check if the template database exists.
2312         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2313         
2314         if ($templatedatabase_exists eq 1){
2316                 $templatedb_exists = 0;
2318                 if (!$ignoremissing){
2320                         $error = "TemplateDatabaseDoesNotExist";
2321                         return;
2323                 }
2325         }
2327         # Check if the permission settings for the template database are valid.
2329         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2331         if ($templatedb_permissions eq 1){
2333                 # The template database has invalid permissions set
2334                 # so return an error value.
2336                 if (!$ignoremissing){
2338                         $error = "TemplateDatabaseInvalidPermissionsSet";
2339                         return;
2341                 }
2343         }
2345         # Connect to the template database.
2347         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2348         $template_database_handle->{unicode} = 1;
2349         $templatedb_loaded = 1;
2353 sub disconnecttemplate{
2354 #################################################################################
2355 # disconnecttemplate: Disconnect from the template database.                    #
2356 #                                                                               #
2357 # Usage:                                                                        #
2358 #                                                                               #
2359 # $dbmodule->disconnecttemplate();                                              #
2360 #################################################################################
2362         # Disconnect the template database.
2364         if ($templatedb_loaded eq 1){
2366                 undef $template_string_handle;
2367                 $template_database_handle->disconnect();
2369         }
2373 sub gettemplatelist{
2374 #################################################################################
2375 # gettemplatelist: Gets the list of templates.                                  #
2376 #                                                                               #
2377 # Usage:                                                                        #
2378 #                                                                               #
2379 # $dbmodule->gettemplatelist();                                                 #
2380 #################################################################################
2382         $error = "";
2383         $errorext = "";
2385         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2386         $template_string_handle->execute();
2388         my @database_template;
2389         my @templates_list;
2390         my $template_filename;
2392         while (@database_template = $template_string_handle->fetchrow_array()){
2394                 # Get certain values from the array.
2396                 $template_filename      = $database_template[0];
2398                 # Add the template to the list of templates.
2400                 push(@templates_list, $template_filename);
2402         }
2404         return @templates_list;
2408 sub gettemplateinfo{
2409 #################################################################################
2410 # gettemplateinfo: Get information on a template.                               #
2411 #                                                                               #
2412 # Usage:                                                                        #
2413 #                                                                               #
2414 # $dbmodule->gettemplateinfo(options);                                          #
2415 #                                                                               #
2416 # options       Specifies the following options in any order.                   #
2417 #                                                                               #
2418 # TemplateFilename      Specifies the template filename to use.                 #
2419 #################################################################################
2421         $error = "";
2422         $errorext = "";
2424         # Get the data passed to the subroutine.
2426         my $class = shift;
2427         my ($passedoptions) = @_;
2429         my %page_info;
2430         my @template_data;
2432         my $template_filename;
2433         my $template_name;
2434         my $template_description;
2435         my $template_datemodified;
2436         my $template_layout;
2438         my $template_found = 0;
2440         my $filename    = $passedoptions->{"TemplateFilename"};
2442         $template_string_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2443         $template_string_handle->execute();
2445         while (@template_data = $template_string_handle->fetchrow_array()){
2447                 # Get certain values from the array.
2449                 $template_filename      = $template_data[0];
2450                 $template_name          = $template_data[1];
2451                 $template_description   = $template_data[2];
2452                 $template_layout        = $template_data[3];
2453                 $template_datemodified  = $template_data[4];
2455                 # Process them into the hash.
2457                 %page_info = (
2458                         "TemplateFilename" => $template_filename,
2459                         "TemplateName" => $template_name,
2460                         "TemplateDescription" => $template_description,
2461                         "TemplateLayout" => $template_layout,
2462                         "TemplateLastModified" => $template_datemodified
2463                 );
2465                 $template_found = 1;
2467         }
2469         if ($template_found eq 0){
2471                 # The template was not found in the template database so
2472                 # write an error value.
2474                 $error = "TemplateDoesNotExist";
2475                 return;
2477         }
2479         return %page_info;
2483 sub addtemplate{
2484 #################################################################################
2485 # addtemplate: Adds a template to the template database.                        #
2486 #                                                                               #
2487 # Usage:                                                                        #
2488 #                                                                               #
2489 # $dbmodule->addtemplate();                                                     #
2490 #                                                                               #
2491 # options       Specifies the following options in any order.                   #
2492 #                                                                               #
2493 # TemplateFilename      Specifies the new template filename.                    #
2494 # TemplateName          Specifies the new template name.                        #
2495 # TemplateDescription   Specifies the new template description.                 #
2496 # TemplateLayout        Specifies the new template layout.                      #
2497 #################################################################################
2499         $error = "";
2500         $errorext = "";
2502         # Get the data passed to the subroutine.
2504         my $class = shift;
2505         my ($passedoptions) = @_;
2507         my @page_exists;
2508         my $notemplatetable;
2509         my $blankfile = 0;
2511         my $template_filename           = $passedoptions->{"TemplateFilename"};
2512         my $template_name               = $passedoptions->{"TemplateName"};
2513         my $template_description        = $passedoptions->{"TemplateDescription"};
2514         my $template_layout             = $passedoptions->{"TemplateLayout"};
2516         # Check if the template database permissions are valid.
2518         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2519         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2521         if ($templatedb_permissions eq 1){
2523                 if ($templatedb_exists eq 0){
2524                         $error = "TemplateDatabaseInvalidPermissionsSet";
2525                         return;
2526                 }
2528         }
2530         # Check if the template already exists before adding.
2532         if ($templatedb_exists eq 0){
2534                 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2536                 if ($blankfile eq 0){
2538                         $template_string_handle->execute();
2540                         while (@page_exists = $template_string_handle->fetchrow_array()){
2542                                 $error = "TemplatePageExists";
2543                                 return;
2545                         }
2547                 }
2549         }
2551         # Get the current date.
2552  
2553         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2554  
2555         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2557         # Check if certain values are undefined and if they
2558         # are then set them blank.
2560         if (!$template_name){
2562                 $template_name = "";
2564         }
2566         if (!$template_description){
2568                 $template_description = "";
2570         }
2572         if (!$template_layout){
2574                 $template_layout = "";
2576         }
2578         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2580         if ($directory_permissions eq 1 && $templatedb_exists){
2582                 # The template database cannot be created because of invalid directory
2583                 # permissions so return an error value.
2585                 $error = "TemplateDatabaseUncreateable";
2586                 return; 
2588         }
2590         # Check to see if a template can be added.
2592         $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2593                                 \'' . $class->convert($template_filename) . '\',
2594                                 \'' . $class->convert($template_name) . '\',
2595                                 \'' . $class->convert($template_description) . '\',
2596                                 \'' . $class->convert($template_layout) . '\',
2597                                 \'' . $class->convert($template_date) . '\'
2598         )') or ( $notemplatetable = 1 );
2600         if (!$notemplatetable){
2602                 $template_string_handle->execute();
2604         }
2606         # Check to see if there is no template table and attempt to create one.
2608         if ($notemplatetable){
2610                 # Create a template table.
2612         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2614         if ($directory_permissions eq 1){
2616                 # The template database cannot be created because of invalid directory
2617                 # permissions so return an error.
2619                 $error = "TemplateDatabaseFileUncreateable";
2620                 return;
2622         }
2624                 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2625                         filename varchar(256) primary key,
2626                         templatename varchar(512),
2627                         templatedescription varchar(512),
2628                         templatelayout text,
2629                         datemodified datetime
2630                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2631                 $template_string_handle->execute();
2633                 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2634                                 \'' . $class->convert($template_filename) . '\',
2635                                 \'' . $class->convert($template_name) . '\',
2636                                 \'' . $class->convert($template_description) . '\',
2637                                 \'' . $class->convert($template_layout) . '\',
2638                                 \'' . $class->convert($template_date) . '\'
2639                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2640                 $template_string_handle->execute();
2642         }
2646 sub deletetemplate{
2647 #################################################################################
2648 # deletetemplate: Deletes a template from the template database.                #
2649 #                                                                               #
2650 # Usage:                                                                        #
2651 #                                                                               #
2652 # $dbmodule->deletetemplate(options);                                           #
2653 #                                                                               #
2654 # options       Specifies the following options in any order.                   #
2655 #                                                                               #
2656 # TemplateFilename      Specifies the template filename to delete.              #
2657 #################################################################################
2659         $error = "";
2660         $errorext = "";
2662         # Get the data passed to the subroutine.
2664         my $class = shift;
2665         my ($passedoptions) = @_;
2667         my @pagedata;
2668         my $template_filename = $passedoptions->{"TemplateFilename"};
2669         my $template_count = 0;
2671         # Check if the template exists.
2673         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2674         $template_string_handle->execute();
2676         while (@pagedata = $template_string_handle->fetchrow_array()){
2678                 $template_count++;
2680         }
2682         if ($template_count eq 0){
2684                 # No pages were returned so return an error value.
2686                 $error = "TemplateDoesNotExist";
2687                 return;
2689         }
2691         # Delete the template from the template database.
2693         $template_string_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2694         $template_string_handle->execute();
2698 sub edittemplate{
2699 #################################################################################
2700 # editttemplate: Edits a Kiriwrite template.                                    #
2701 #                                                                               #
2702 # Usage:                                                                        #
2703 #                                                                               #
2704 # $dbmodule->edittemplate(options);                                             #
2705 #                                                                               #
2706 # options       Specifies the following options in any order.                   #
2707 #                                                                               #
2708 # TemplateFilename              Specifies the template filename to edit.        #
2709 # NewTemplateFilename           Specifies the new template filename.            #
2710 # NewTemplateName               Specifies the new template name.                #
2711 # NewTemplateDescription        Specifies the new template description.         #
2712 # NewTemplateLayout             Specifies the new template layout.              #
2713 #################################################################################
2715         # Get the values passed.
2717         my $class = shift;
2718         my ($passedoptions) = @_;
2719         my $template_found = 0;
2720         my @template_info;
2722         # Process the values passed.
2724         my $template_filename           = $passedoptions->{"TemplateFilename"};
2725         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2726         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2727         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2728         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2730         # Check if the template database permissions are valid.
2732         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2733         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2735         if ($templatedb_permissions eq 1){
2737                 if ($templatedb_exists eq 0){
2738                         $error = "TemplateDatabaseInvalidPermissionsSet";
2739                         return;
2740                 }
2742         }
2744         # Check if the template exists.
2746         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2747         $template_string_handle->execute();
2749         while (@template_info = $template_string_handle->fetchrow_array()){
2751                 $template_found = 1;
2753         }
2755         # Check to see if the template was found and set an error value if
2756         # it wasn't.
2758         if ($template_found eq 0){
2760                 $error = "TemplateDoesNotExist";
2761                 return;
2763         }
2765         # Get the date and time.
2767         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2768         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2770         # Update the template information.
2772         $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2773                 filename = \'' . $class->convert($new_template_filename) . '\',
2774                 templatename = \'' . $class->convert($new_template_name) . '\',
2775                 templatedescription = \'' . $class->convert($new_template_description) . '\',
2776                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2777                 datemodified = \'' . $class->convert($templatenewdate) . '\'
2778                 WHERE filename = \'' . $class->convert($template_filename) . '\'
2779         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2780         $template_string_handle->execute();
2784 #################################################################################
2785 # General subroutines.                                                          #
2786 #################################################################################
2788 sub connect{
2789 #################################################################################
2790 # connect: Connect to the server.                                               #
2791 #                                                                               #
2792 # Usage:                                                                        #
2793 #                                                                               #
2794 # $dbmodule->connect();                                                         #
2795 #################################################################################
2797         # This function is not needed in this database module.
2801 sub disconnect{
2802 #################################################################################
2803 # connect: Disconnect from the server.                                          #
2804 #                                                                               #
2805 # Usage:                                                                        #
2806 #                                                                               #
2807 # $dbmodule->disconnect();                                                      #
2808 #################################################################################
2810         # This function is not needed in this database module.
2812         undef $string_handle;
2816 1;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy