Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
92bce19427e861843066985d8528837af760875c
[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();
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 $second_database_handle;
42 my $second_string_handle;
43 my $database_filename;
44 my $second_database_filename;
45 my $templatedb_loaded = 0;
46 my $templatedb_exists = 1;
47 my $template_string_handle;
48 my $template_database_handle;
49 my $filterdb_loaded = 0;
50 my $filterdb_exists = 1;
51 my $filterdb_string_handle;
52 my $filterdb_database_handle;
53 my $error = "";
54 my $errorext = "";
56 #################################################################################
57 # Generic Subroutines.                                                          #
58 #################################################################################
60 sub new{
61 #################################################################################
62 # new: Create an instance of Kiriwrite::Database::SQLite                        #
63 #                                                                               #
64 # Usage:                                                                        #
65 #                                                                               #
66 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
67 #################################################################################
68         
69         # Get the perl module name.
71         my $class = shift;
72         my $self = {};
74         return bless($self, $class);
76 }
78 sub loadsettings{
79 #################################################################################
80 # loadsettings: Loads settings into the SQLite database module                  #
81 #                                                                               #
82 # Usage:                                                                        #
83 #                                                                               #
84 # $dbmodule->loadsettings(Directory, options);                                  #
85 #                                                                               #
86 # options       Specifies the following options (in any order).                 #
87 #                                                                               #
88 # Directory     Specifies the directory to use for getting databases.           #
89 # DateTime      Specifies the date and time format to use.                      #
90 # Server        Specifies the server to use.                                    #
91 # Database      Specifies the database to use.                                  #
92 # Username      Specifies the username to use.                                  #
93 # Password      Specifies the password to use.                                  #
94 # HashType      Specifies the password hash type to use.                        #
95 # Port          Specifies the server port to use.                               #
96 # Protocol      Specifies the protocol to use.                                  #
97 # TablePrefix   Specifies the table prefix to use.                              #
98 #################################################################################
100         # Get the data passed to the subroutine.
102         my $class = shift;
103         my ($passedoptions)     = @_;
105         # Add the directory setting to the list of options (as it's the only
106         # one needed for this database module).
108         %options = (
109                 "Directory"     => $passedoptions->{"Directory"},
110                 "DateTime"      => $passedoptions->{"DateTime"},
111         );
115 sub convert{
116 #################################################################################
117 # convert: Converts data into SQL formatted data.                               #
118 #                                                                               #
119 # Usage:                                                                        #
120 #                                                                               #
121 # $dbmodule->convert(data);                                                     #
122 #                                                                               #
123 # data          Specifies the data to convert.                                  #
124 #################################################################################
126         # Get the data passed to the subroutine.
128         my $class       = shift;
129         my $data        = shift;
131         if (!$data){
132                 $data = "";
133         }
135         $data =~ s/'/''/g;
136         $data =~ s/\b//g;
138         return $data;
142 sub dateconvert{
143 #################################################################################
144 # dateconvert: Converts a SQL date into a proper date.                          #
145 #                                                                               #
146 # Usage:                                                                        #
147 #                                                                               #
148 # $dbmodule->dateconvert(date);                                                 #
149 #                                                                               #
150 # date          Specifies the date to convert.                                  #
151 #################################################################################
153         # Get the date passed to the subroutine.
155         my $class       = shift;
156         my $data        = shift;
158         # Convert the date given into the proper date.
160         # Create the following varialbes to be used later.
162         my $date;
163         my $time;
164         my $day;
165         my $day_full;
166         my $month;
167         my $month_check;
168         my $month_full;
169         my $year;
170         my $year_short;
171         my $hour;
172         my $hour_full;
173         my $minute;
174         my $minute_full;
175         my $second;
176         my $second_full;
177         my $seek = 0;
178         my $timelength;
179         my $datelength;
180         my $daylength;
181         my $secondlength;
182         my $startchar = 0;
183         my $char;
184         my $length;
185         my $count = 0;
187         # Split the date and time.
189         $length = length($data);
191         if ($length > 0){
193                 do {
195                         # Get the character and check if it is a space.
197                         $char = substr($data, $seek, 1);
199                         if ($char eq ' '){
201                                 # The character is a space, so get the date and time.
203                                 $date           = substr($data, 0, $seek);
204                                 $timelength     = $length - $seek - 1;
205                                 $time           = substr($data, $seek + 1, $timelength);
207                         }
209                         $seek++;
211                 } until ($seek eq $length);
213                 # Get the year, month and date.
215                 $length = length($date);
216                 $seek = 0;
218                 do {
220                         # Get the character and check if it is a dash.
222                         $char = substr($date, $seek, 1);
224                         if ($char eq '-'){
226                                 # The character is a dash, so get the year, month or day.
228                                 $datelength = $seek - $startchar;
230                                 if ($count eq 0){
232                                         # Get the year from the date.
234                                         $year           = substr($date, 0, $datelength) + 1900;
235                                         $startchar      = $seek;
236                                         $count = 1;
238                                         # Get the last two characters to get the short year
239                                         # version.
241                                         $year_short     = substr($year, 2, 2);
243                                 } elsif ($count eq 1){
245                                         # Get the month and day from the date.
247                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
249                                         # Check if the month is less then 10, if it is
250                                         # add a zero to the value.
252                                         if ($month < 10){
254                                                 $month_full = '0' . $month;
256                                         } else {
258                                                 $month_full = $month;
260                                         }
262                                         $startchar      = $seek;
263                                         $count = 2;
265                                         $daylength      = $length - $seek + 1;
266                                         $day            = substr($date, $startchar + 1, $daylength);
268                                         # Check if the day is less than 10, if it is
269                                         # add a zero to the value.
271                                         if ($day < 10){
273                                                 $day_full       = '0' . $day;
275                                         } else {
277                                                 $day_full       = $day;
279                                         }
281                                 }
283                         }
285                         $seek++;
287                 } until ($seek eq $length);
289                 # Get the length of the time value and reset certain
290                 # values to 0.
292                 $length = length($time);
293                 $seek = 0;
294                 $count = 0;
295                 $startchar = 0;
297                 do {
299                         # Get the character and check if it is a colon.
301                         $char = substr($time, $seek, 1);
303                         if ($char eq ':'){
305                                 # The character is a colon, so get the hour, minute and day.
307                                 $timelength = $seek - $startchar;
309                                 if ($count eq 0){
311                                         # Get the hour from the time.
313                                         $hour = substr($time, 0, $timelength);
314                                         $count = 1;
315                                         $startchar = $seek;
317                                         # If the hour is less than ten then add a
318                                         # zero.
320                                         if ($hour < 10){
322                                                 $hour_full = '0' . $hour;
324                                         } else {
326                                                 $hour_full = $hour;
328                                         }
330                                 } elsif ($count eq 1){
332                                         # Get the minute and second from the time.
334                                         $minute = substr($time, $startchar + 1, $timelength - 1);
335                                         $count = 2;
336                                                 
337                                         # If the minute is less than ten then add a
338                                         # zero.
340                                         if ($minute < 10){
342                                                 $minute_full = '0' . $minute;
344                                         } else {
346                                                 $minute_full = $minute;
348                                         }
350                                         $startchar = $seek;
352                                         $secondlength = $length - $seek + 1;
353                                         $second = substr($time, $startchar + 1, $secondlength);
354                                         
355                                         # If the second is less than ten then add a
356                                         # zero.
358                                         if ($second < 10){
360                                                 $second_full = '0' . $second;
362                                         } else {
364                                                 $second_full = $second;
366                                         }
368                                 }
370                         }
372                         $seek++;
374                 } until ($seek eq $length);
376                 # Get the setting for displaying the date and time.
378                 $data = $options{"DateTime"};
380                 # Process the setting for displaying the date and time
381                 # using regular expressions
383                 $data =~ s/DD/$day_full/g;
384                 $data =~ s/D/$day/g;
385                 $data =~ s/MM/$month_full/g;
386                 $data =~ s/M/$month/g;
387                 $data =~ s/YY/$year/g;
388                 $data =~ s/Y/$year_short/g;
390                 $data =~ s/hh/$hour_full/g;
391                 $data =~ s/h/$hour/g;
392                 $data =~ s/mm/$minute_full/g;
393                 $data =~ s/m/$minute/g;
394                 $data =~ s/ss/$second_full/g;
395                 $data =~ s/s/$second/g;
397         }
399         return $data;
403 sub geterror{
404 #################################################################################
405 # geterror: Gets the error message (or extended error message).                 #
406 #                                                                               #
407 # Usage:                                                                        #
408 #                                                                               #
409 # $dbmodule->geterror(extended);                                                #
410 #                                                                               #
411 # Extended      Specifies if the extended error should be retrieved.            #
412 #################################################################################
414         # Get the data passed to the subroutine.
416         my $class       = shift;
417         my $extended    = shift;
419         if (!$extended){
420                 $extended = 0;
421         }
423         if (!$errorext){
424                 $errorext = "";
425         }
427         if (!$error){
428                 $error = "";
429         }
431         # Check to see if extended information should be returned.
433         if ($extended eq 1){
435                 # Extended information should be returned.
437                 return $errorext;
439         } else {
441                 # Basic information should be returned.
443                 return $error;
445         }
449 sub dbpermissions{
450 #################################################################################
451 # dbpermissions: Check if the permissions for the database are valid.           #
452 #                                                                               #
453 # Usage:                                                                        #
454 #                                                                               #
455 # $database->dbpermissions(dbname, read, write);                                #
456 #                                                                               #
457 # dbname        Specifies the database name to check.                           #
458 # read          Check to see if the database can be read.                       #
459 # write         Check to see if the database can be written.                    #
460 #################################################################################
462         # Get the database name, read setting and write setting.
464         my ($class, $dbname, $readper, $writeper)       = @_;
466         # Check if the database can be read.
468         if ($readper){
470                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
472                         # The database can be read.
474                 } else {
476                         # The database cannot be read, so return a value
477                         # of 1.
479                         return 1;
481                 }
483         }
485         # Check if the database can be written.
487         if ($writeper){
489                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
491                         # The database can be read.
493                 } else {
495                         # The database cannot be read, so return a value
496                         # of 1.
498                         return 1;
500                 }
502         }
504         # No errors have occured while checking so return a value
505         # of 0.
507         return 0;
511 sub dbexists{
512 #################################################################################
513 # dbexists: Check if the database exists.                                       #
514 #                                                                               #
515 # Usage:                                                                        #
516 #                                                                               #
517 # $dbmodule->dbexists(dbname);                                                  #
518 #                                                                               #
519 # dbname        Specifies the database name to check.                           #
520 #################################################################################
522         # Get the value that was passed to the subroutine.
524         my $class       = shift;
525         my ($filename)  = @_;
527         # Check if the filename exists, if it does, return a value of 1, else
528         # return a value of 0, meaning that the file was not found.
530         if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
532                 # Specified file does exist so return a value of 0.
534                 return 0;
536         } else {
538                 # Specified file does not exist so return a value of 1.
540                 return 1;
542         }
546 #################################################################################
547 # Database Subroutines.                                                         #
548 #################################################################################
550 sub getdblist{
551 #################################################################################
552 # getdblist: Gets the list of available databases.                              #
553 #                                                                               #
554 # Usage:                                                                        #
555 #                                                                               #
556 # $dbmodule->getdblist();                                                       #
557 #################################################################################
559         # Get the list of databases.
561         my @data_directory;
562         my @data_directory_final;
563         my $database;
564         my $database_filename_length;
565         my $database_filename_friendly;
567         # Check if the database directory has valid permission settings.
569         if (-e $options{"Directory"}){
571                 # The database directory does exist. So check if
572                 # the permission settings are valid.
574                 if (-r $options{"Directory"}){
576                         # The permission settings for reading the directory
577                         # are valid.
579                 } else {
581                         # The permission settings for reading the directory
582                         # are invalid so return an error value.
584                         $error = "DataDirInvalidPermissions";
585                         return;
587                 }
589         } else {
591                 # The database directory does not exist, so return an
592                 # error value.
594                 $error = "DataDirMissing";
595                 return;
597         }
599         opendir(DATADIR, $options{"Directory"});
600         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
601         closedir(DATADIR);
603         # Process the list of databases.
605         foreach $database (@data_directory){
607                 $database =~ s/.db.sqlite$//g;
608                 $database_filename_friendly = $database;
610                 #$database_filename_length = length($database);
611                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
612                 push(@data_directory_final, $database_filename_friendly);
614         }
616         # Return the list of databases.
618         return @data_directory_final;
622 sub getdatabaseinfo{
623 #################################################################################
624 # getdatabaseinfo: Get information about the database.                          #
625 #                                                                               #
626 # Usage:                                                                        #
627 #                                                                               #
628 # $dbmodule->getdatabaseinfo();                                                 #
629 #################################################################################
631         # Get the database information.
633         my $class = shift;
634         my ($databaseinfo, %databaseinfo);
635         my ($sqldata, @sqldata);
637         $error = "";
638         $errorext = "";
640         $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 (
641                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
642         );
643         $string_handle->execute();
645         @sqldata = $string_handle->fetchrow_array();
647         # Process the database information into a hash.
649         %databaseinfo = (
650                 "DatabaseName"  => $sqldata[0],
651                 "Description"   => $sqldata[1],
652                 "Notes"         => $sqldata[2],
653                 "Categories"    => $sqldata[3],
654                 "Major"         => $sqldata[4],
655                 "Minor"         => $sqldata[5],
656                 "Revision"      => $sqldata[6]
657         );
659         $string_handle->finish();
660         undef $string_handle;
662         return %databaseinfo;
666 sub getseconddatabaseinfo{
667 #################################################################################
668 # getseconddatabaseinfo: Get information about the database that pages will be  #
669 # moved or copied to.                                                           #
670 #                                                                               #
671 # Usage:                                                                        #
672 #                                                                               #
673 # $dbmodule->getseconddatabaseinfo();                                           #
674 #################################################################################
676         # Get the database information.
678         my $class = shift;
679         my ($databaseinfo, %databaseinfo);
680         my ($sqldata, @sqldata);
682         $error = "";
683         $errorext = "";
685         $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 (
686                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
687         );
688         $second_string_handle->execute();
690         @sqldata = $second_string_handle->fetchrow_array();
692         # Process the database information into a hash.
694         %databaseinfo = (
695                 "DatabaseName"  => $sqldata[0],
696                 "Description"   => $sqldata[1],
697                 "Notes"         => $sqldata[2],
698                 "Categories"    => $sqldata[3],
699                 "Major"         => $sqldata[4],
700                 "Minor"         => $sqldata[5],
701                 "Revision"      => $sqldata[6]
702         );
704         $second_string_handle->finish();
705         undef $second_string_handle;
707         return %databaseinfo;
711 sub adddatabase{
712 #################################################################################
713 # adddatabase: Adds a Kiriwrite database.                                       #
714 #                                                                               #
715 # Usage:                                                                        #
716 #                                                                               #
717 # $dbmodule->adddatabase(options);                                              #
718 #                                                                               #
719 # options       Specifies the following options in any order.                   #
720 #                                                                               #
721 # DatabaseFilename      Specifies the database file/shortname to use.           #
722 # DatabaseName          Specifies the database name to use.                     #
723 # DatabaseDescription   Specifies the database description to use.              #
724 # DatabaseNotes         Specifies the database notes to use.                    #
725 # DatabaseCategories    Specifies the database categories to use.               #
726 # VersionMajor          Specifies the major version.                            #
727 # VersionMinor          Specifies the minor version.                            #
728 # VersionRevision       Specifies the revision version.                         #
729 #################################################################################
731         # Get the database that was passed to the subroutine.
733         $error  = "";
734         $errorext = "";
736         my $class       = shift;
737         my ($passedoptions) = @_;
739         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
740         my $dbname              = $passedoptions->{"DatabaseName"};
741         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
742         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
743         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
744         my $dbmajorver          = $passedoptions->{"VersionMajor"};
745         my $dbminorver          = $passedoptions->{"VersionMinor"};
746         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
748         # Check if the database with the filename given already exists.
750         my $database_exists     = $class->dbexists($dbfilename);
752         if ($database_exists eq 0){
754                 # The database filename exists so set the error value.
756                 $error = "DatabaseExists";
757                 return;
759         }
761         # Create the database structure.
763         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
764         $database_handle->{unicode} = 1;
765         $string_handle          = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
766                         name varchar(256) primary key, 
767                         description varchar(512), 
768                         notes text, 
769                         categories varchar(512), 
770                         kiriwrite_version_major int(4), 
771                         kiriwrite_version_minor int(4), 
772                         kiriwrite_version_revision int(4)
773         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
774         $string_handle->execute();
776         $string_handle  = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
777                         filename varchar(256) primary key, 
778                         pagename varchar(512), 
779                         pagedescription varchar(512), 
780                         pagesection varchar(256),
781                         pagetemplate varchar(64),
782                         pagedata text,
783                         pagesettings int(1),
784                         lastmodified datetime
785         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
786         $string_handle->execute();
788         # Convert the values into SQL query formatted values and add an entry
789         # to the kiriwrite_database_info table.
791         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
792                 \'' . $class->convert($dbname) . '\',
793                 \'' . $class->convert($dbdescription) . '\',
794                 \'' . $class->convert($dbnotes) . '\',
795                 \'' . $class->convert($dbcategories) . '\',
796                 \'' . $class->convert($dbmajorver) . '\',
797                 \'' . $class->convert($dbminorver) . '\',
798                 \'' . $class->convert($dbrevisionver) . '\'
799         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
800         $string_handle->execute();
804 sub editdatabase{
805 #################################################################################
806 # editdatabase: Edits a Kiriwrite Database.                                     #
807 #                                                                               #
808 # Usage:                                                                        #
809 #                                                                               #
810 # $dbmodule->editdatabase(options);                                             #
811 #                                                                               #
812 # options               Specifies the following options in any order.           #
813 #                                                                               #
814 # NewDatabaseFilename   Specifies the new database filename to use.             #
815 # DatabaseName          Specifies the new database name.                        #
816 # DatabaseDescription   Specifies the new database description.                 #
817 # DatabaseNotes         Specifies the new database notes.                       #
818 # DatabaseCategories    Specifies the new database categories.                  #
819 #################################################################################
821         $error          = "";
822         $errorext       = "";
824         my $class       = shift;
825         my ($passedoptions) = @_;
827         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
828         my $dbname              = $passedoptions->{"DatabaseName"};
829         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
830         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
831         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
833         # Check if a new database filename has been specified and if a
834         # new database filename has been specified then change the
835         # database filename.
837         if ($database_filename ne $dbnewfilename){
839                 # A new database filename has been given so check if the output
840                 # directory has write access.
842                 if (-r $options{"Directory"}){
843                         
844                         # The directory is readable.
846                 } else {
848                         # The directory is not readable so set the error value.
850                         $error = "DataDirInvalidPermissions";
852                         return;
854                 }
856                 if (-w $options{"Directory"}){
858                         # The directory is writeable.
860                 } else {
862                         # The directory is not writeable so set the error value.
864                         $error = "DataDirInvalidPermissions";
866                         return;
868                 }
870                 # Check if a database filename already exists before using the
871                 # new filename.
873                 my $database_newexists          = $class->dbexists($dbnewfilename);
875                 if ($database_newexists eq 0){
877                         # The database filename exists so set the error value.
879                         $error = "DatabaseExists";
880                         return;
882                 }
884                 # Check if the database can be renamed (has write access).
886                 my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
888                 if ($database_permissions eq 1){
890                         # The database filename exists so set the error value.
892                         $error = "InvalidPermissionsSet";
893                         return;
895                 }
897                 # "Disconnect" from the database.
899                 $database_handle->disconnect();
901                 # Rename the database.
903                 ($database_filename)    = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
904                 ($dbnewfilename)        = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
906                 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
908                 # Reload the database from the new filename.
910                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
911                 $database_handle->{unicode} = 1;
912                 $database_filename = $dbnewfilename;
914         }
916         # Check if the database can be altered with the new data.
918         my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
920         if ($database_permissions eq 1){
922                 # The database filename exists so set the error value.
924                 $error = "InvalidPermissionsSet";
925                 return;
927         }
929         # Get the current database information.
931         $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
932         $string_handle->execute();
934         my @database_oldinfo    = $string_handle->fetchrow_array();
936         my $dboldname           = $database_oldinfo[0];
938         # Update the database information.
940         $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
941         description = \'' . $class->convert($dbdescription) . '\',
942         notes = \'' . $class->convert($dbnotes) . '\',
943         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
944         $string_handle->execute();
946         undef $string_handle;
947         return;
951 sub deletedatabase{
952 #################################################################################
953 # deletedatabase: Deletes a Kiriwrite database.                                 #
954 #                                                                               #
955 # Usage:                                                                        #
956 #                                                                               #
957 # $dbmodule->deletedatabase(options);                                           #
958 #                                                                               #
959 # options       Specifies the following options in any order.                   #
960 #                                                                               #
961 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
962 #################################################################################
964         $error          = "";
965         $errorext       = "";
967         # Get the database filename.
969         my $class               = shift;
970         my ($passedoptions)     = shift;
972         my $databasename        = $passedoptions->{"DatabaseName"};
974         # Check if the database exists.
976         my $database_exists             = $class->dbexists($databasename);
978         if ($database_exists eq 1){
980                 # The database does not exist so set the error value.
982                 $error = "DoesNotExist";
983                 return;
985         }
987         # Check if the database permissions are valid.
989         my $database_permissions        = $class->dbpermissions($databasename);
991         if ($database_permissions eq 1){
993                 # The database permissions are invalid so set the error
994                 # value.
996                 $error = "InvalidPermissionsSet";
997                 return;
999         }
1001         # Delete the database.
1003         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1005         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1009 sub selectdb{
1010 #################################################################################
1011 # selectdb: Selects the Kiriwrite database.                                     #
1012 #                                                                               #
1013 # Usage:                                                                        #
1014 #                                                                               #
1015 # $dbmodule->connect(options);                                                  #
1016 #                                                                               #
1017 # options       Specifies the following options in any order.                   #
1018 #                                                                               #
1019 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1020 #################################################################################
1022         # Get the database name.
1024         $error = "";
1025         $errorext = "";
1027         my $class = shift;
1028         my ($passedoptions) = @_;
1029         my (%database, $database);
1031         my $dbname = $passedoptions->{"DatabaseName"};
1033         # Check if the database exists.
1035         my $database_exists = $class->dbexists($dbname);
1037         if ($database_exists eq 1){
1039                 # The database does not exist so return an error value
1040                 # saying that the database does not exist.
1042                 $error = "DoesNotExist";
1044                 return;
1046         }
1048         # Check if the database has valid permissions set.
1050         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1052         if ($database_permissions eq 1){
1054                 # The database has invalid permissions set so return
1055                 # an error value saying that the database has invalid
1056                 # permissions set.
1058                 $error = "InvalidPermissionsSet";
1059                 
1060                 return;
1062         }
1064         # Connect to the database.
1066         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1067         $database_handle->{unicode} = 1;
1068         $database_filename = $dbname;
1072 sub selectseconddb{
1073 #################################################################################
1074 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1075 # pages to.                                                                     #
1076 #                                                                               #
1077 # Usage:                                                                        #
1078 #                                                                               #
1079 # $dbmodule->selectseconddb(options);                                           #
1080 #                                                                               #
1081 # options       Specifies the following options in any order.                   #
1082 #                                                                               #
1083 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1084 #################################################################################
1086         # Get the database name.
1088         $error = "";
1089         $errorext = "";
1091         my $class = shift;
1092         my ($passedoptions) = @_;
1093         my (%database, $database);
1095         my $dbname = $passedoptions->{"DatabaseName"};
1097         # Check if the database exists.
1099         my $database_exists = $class->dbexists($dbname);
1101         if ($database_exists eq 1){
1103                 # The database does not exist so return an error value
1104                 # saying that the database does not exist.
1106                 $error = "DoesNotExist";
1108                 return;
1110         }
1112         # Check if the database has valid permissions set.
1114         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1116         if ($database_permissions eq 1){
1118                 # The database has invalid permissions set so return
1119                 # an error value saying that the database has invalid
1120                 # permissions set.
1122                 $error = "InvalidPermissionsSet";
1123                 
1124                 return;
1126         }
1128         # Connect to the database.
1130         $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1131         $second_database_handle->{unicode} = 1;
1132         $second_database_filename = $dbname;    
1136 #################################################################################
1137 # Page subroutines.                                                             #
1138 #################################################################################
1140 sub getpagelist{
1141 #################################################################################
1142 # getpagelist: Gets the list of pages from the database.                        #
1143 #                                                                               #
1144 # Usage:                                                                        #
1145 #                                                                               #
1146 # $dbmodule->getpagelist();                                                     #
1147 #################################################################################
1149         $error = "";
1150         $errorext = "";
1152         my $class       = shift;
1153         
1154         $string_handle  = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1155         $string_handle->execute();
1157         my @database_pagefilenames;
1158         my @database_pagefilenames_final;
1160         # Process the collected pages.
1162         while (@database_pagefilenames = $string_handle->fetchrow_array){
1164                 # Add each page to the list of pages in the database.
1166                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1168         }
1170         undef $string_handle;
1171         return @database_pagefilenames_final;
1175 sub getpageinfo{
1176 #################################################################################
1177 # getpageinfo: Gets the page information from the filename passed.              #
1178 #                                                                               #
1179 # Usage:                                                                        #
1180 #                                                                               #
1181 # $dbmodule->getpageinfo(options);                                              #
1182 #                                                                               #
1183 # options       Specifies the following options in any order.                   #
1184 #                                                                               #
1185 # PageFilename  Specifies the page filename to get the page information from.   #
1186 #################################################################################
1188         $error = "";
1189         $errorext = "";
1191         my $class               = shift;
1192         my ($passedoptions)     = shift;
1193         my (%database_page, $database_page);
1194         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1196         my @data_page;
1197         my $page_found = 0;
1199         # Get the page from the database.
1201         my $page_filename       = $passedoptions->{"PageFilename"};
1203         $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 );
1204         $string_handle->execute();
1206         # Check if the page exists in the database.
1208         while (@data_page = $string_handle->fetchrow_array()){
1210                 # Get the values from the array.
1212                 $pagefilename           = $data_page[0];
1213                 $pagename               = $data_page[1];
1214                 $pagedescription        = $data_page[2];
1215                 $pagesection            = $data_page[3];
1216                 $pagetemplate           = $data_page[4];
1217                 $pagedata               = $data_page[5];
1218                 $pagesettings           = $data_page[6];
1219                 $pagelastmodified       = $data_page[7];
1221                 # Put the values into the page hash.
1223                 %database_page = (
1224                         "PageFilename"          => $pagefilename,
1225                         "PageName"              => $pagename,
1226                         "PageDescription"       => $pagedescription,
1227                         "PageSection"           => $pagesection,
1228                         "PageTemplate"          => $pagetemplate,
1229                         "PageContent"           => $pagedata,
1230                         "PageSettings"          => $pagesettings,
1231                         "PageLastModified"      => $class->dateconvert($pagelastmodified),
1232                 );
1234                 $page_found = 1;
1236         }
1237         
1238         # Check if the page did exist.
1240         if (!$page_found){
1242                 $error = "PageDoesNotExist";
1243                 return;
1245         }
1247         undef $string_handle;
1248         return %database_page;
1252 sub addpage{
1253 #################################################################################
1254 # addpage: Add a page to the selected database.                                 #
1255 #                                                                               #
1256 # Usage:                                                                        #
1257 #                                                                               #
1258 # $dbmodule->addpage(options);                                                  #
1259 #                                                                               #
1260 # options       Specifies the following options in any order.                   #
1261 #                                                                               #
1262 # PageFilename          Specifies the page filename to use.                     #
1263 # PageName              Specifies the page name to use.                         #
1264 # PageDescription       Specifies the page description to use.                  #
1265 # PageSection           Specifies the page section to use.                      #
1266 # PageTemplate          Specifies the page template to use.                     #
1267 # PageContent           Specifies the page content to use.                      #
1268 # PageSettings          Specifies the page settings to use.                     #
1269 #################################################################################
1271         # Get the data that was passed to the subroutine.
1273         $error = "";
1274         $errorext = "";
1276         my $class               = shift;
1277         my ($passedoptions)     = shift;
1279         my @database_page;
1280         my $page_count = 0;
1282         # Get the values passed to the hash.
1284         my $page_filename       = $passedoptions->{"PageFilename"};
1285         my $page_name           = $passedoptions->{"PageName"};
1286         my $page_description    = $passedoptions->{"PageDescription"};
1287         my $page_section        = $passedoptions->{"PageSection"};
1288         my $page_template       = $passedoptions->{"PageTemplate"};
1289         my $page_content        = $passedoptions->{"PageContent"};
1290         my $page_settings       = $passedoptions->{"PageSettings"};
1292         # Check to see if the filename given already exists
1293         # in the page database.
1295         $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 );
1296         $string_handle->execute();
1298         # Check if a page with the filename given really does
1299         # exist.
1301         while (@database_page = $string_handle->fetchrow_array()){
1303                 # A page does exist so increment the count to 1.
1305                 $page_count++;
1306                 
1307         }
1309         if ($page_count ne 0){
1311                 # The page does exist so set the error value.
1313                 $error = "PageExists";
1314                 return;
1316         }
1318         # Check if certain values are undefined.
1320         if (!$page_name){
1322                 $page_name = "";
1324         }
1326         if (!$page_description){
1328                 $page_description = "";
1330         }
1332         if (!$page_section){
1334                 $page_section = "";
1336         }
1338         if (!$page_content){
1340                 $page_content = "";
1342         }
1344         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1345         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1347         # Add the page to the selected database.
1349         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1350                 \'' . $class->convert($page_filename) . '\',
1351                 \'' . $class->convert($page_name) . '\',
1352                 \'' . $class->convert($page_description) . '\',
1353                 \'' . $class->convert($page_section) . '\',
1354                 \'' . $class->convert($page_template) . '\',
1355                 \'' . $class->convert($page_content) . '\',
1356                 \'' . $class->convert($page_settings) . '\',
1357                 \'' . $class->convert($page_date) . '\'
1358         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1359         $string_handle->execute();
1361         undef $string_handle;
1365 sub deletepage{
1366 #################################################################################
1367 # deletepage: Delete a page from the selected database.                         #
1368 #                                                                               #
1369 # Usage:                                                                        #
1370 #                                                                               #
1371 # $dbmodule->deletepage(options)                                                #
1372 #                                                                               #
1373 # options       Specifies the following options in any order.                   #
1374 #                                                                               #
1375 # PageFilename  Specifies the page filename to delete.                          #
1376 #################################################################################
1378         $error = "";
1379         $errorext = "";
1381         # Get the data that was passed to the subroutine.
1383         my $class = shift;
1384         my ($passedoptions) = @_;
1385         my @page_info;
1386         my $page_found = 0;
1388         # Get the page filename.
1390         my $page_filename = $passedoptions->{"PageFilename"};
1392         # Check if the page exists before deleting it.
1394         $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 );
1395         $string_handle->execute();
1397         while (@page_info = $string_handle->fetchrow_array()){
1399                 $page_found = 1;
1401         }
1403         # Check if the page really does exist.
1405         if (!$page_found){
1407                 $error = "PageDoesNotExist";
1408                 return;
1410         }
1412         # Delete the page.
1414         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1415         $string_handle->execute();
1419 sub editpage{
1420 #################################################################################
1421 # editpage: Edit a page from the selected database.                             #
1422 #                                                                               #
1423 # Usage:                                                                        #
1424 #                                                                               #
1425 # $dbmodule->editpage(options);                                                 #
1426 #                                                                               #
1427 # options       Specifies the following options in any order.                   #
1428 #                                                                               #
1429 # PageFilename          Specifies the filename to edit.                         #
1430 # PageNewFilename       Specifies the new filename to use.                      #
1431 # PageNewName           Specifies the new page name to use.                     #
1432 # PageNewDescription    Specifies the new page description to use.              #
1433 # PageNewSection        Specifies the new page section to use.                  #
1434 # PageNewTemplate       Specifies the new page template to use.                 #
1435 # PageNewContent        Specifies the new page content to use.                  #
1436 # PageNewSettings       Specifies the new page settings to use.                 #
1437 #################################################################################
1439         $error = "";
1440         $errorext = "";
1442         # Get the values passed to the subroutine.
1444         my $class = shift;
1445         my ($passedoptions) = @_;
1446         my $page_found = 0;
1447         my @page_info;
1449         # Get the data that was passed to the subroutine.
1451         my $page_filename       = $passedoptions->{"PageFilename"};
1452         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1453         my $page_newname        = $passedoptions->{"PageNewName"};
1454         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1455         my $page_newsection     = $passedoptions->{"PageNewSection"};
1456         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1457         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1458         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1460         # Check if the page with the filename given exists.
1462         $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 );
1463         $string_handle->execute();
1465         # Check if the page really does exist.
1467         while (@page_info = $string_handle->fetchrow_array()){
1469                 # The page information is found.
1471                 $page_found = 1;
1473         }
1475         # Check if the page really does exist.
1477         if (!$page_found){
1479                 $error = "PageDoesNotExist";
1480                 return;
1482         }
1484         # Get the current date.
1486         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1487         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1489         # Edit the selected page.
1491         $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 );
1492         $string_handle->execute();
1496 sub movepage{
1497 #################################################################################
1498 # movepage: Moves a page from the old database to the new database.             #
1499 #                                                                               #
1500 # Usage:                                                                        #
1501 #                                                                               #
1502 # $dbmodule->movepage(options);                                                 #
1503 #                                                                               #
1504 # options       Specifies the following options in any order.                   #
1505 #                                                                               #
1506 # PageFilename  Specifies the page with the filename to move.                   #
1507 #################################################################################
1509         $error = "";
1510         $errorext = "";
1512         # Get the values passed to the subroutine.
1514         my $class = shift;
1515         my ($passedoptions) = @_;
1517         my (%database_page, $database_page);
1518         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1519         my @page_info;
1520         my $page_found = 0;
1522         # Get the data that was passed to the subroutine.
1524         my $page_filename = $passedoptions->{"PageFilename"};
1526         # Check if the page with the filename given exists.
1528         $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 );
1529         $string_handle->execute();
1531         # Check if the page really does exist.
1533         while (@page_info = $string_handle->fetchrow_array()){
1535                 # Get the values from the array.
1537                 $pagefilename           = $page_info[0];
1538                 $pagename               = $page_info[1];
1539                 $pagedescription        = $page_info[2];
1540                 $pagesection            = $page_info[3];
1541                 $pagetemplate           = $page_info[4];
1542                 $pagedata               = $page_info[5];
1543                 $pagesettings           = $page_info[6];
1544                 $pagelastmodified       = $page_info[7];
1546                 # Put the values into the page hash.
1548                 %database_page = (
1549                         "PageFilename"          => $pagefilename,
1550                         "PageName"              => $pagename,
1551                         "PageDescription"       => $pagedescription,
1552                         "PageSection"           => $pagesection,
1553                         "PageTemplate"          => $pagetemplate,
1554                         "PageContent"           => $pagedata,
1555                         "PageSettings"          => $pagesettings,
1556                         "PageLastModified"      => $pagelastmodified,
1557                 );
1559                 # The page information is found.
1561                 $page_found = 1;
1563         }
1565         # Check if the page really does exist.
1567         if (!$page_found){
1569                 $error = "PageDoesNotExist";
1570                 return;
1572         }
1574         # Check if the page with the filename given already exists in
1575         # the database the page is being moved to.
1577         $page_found = 0;
1578         @page_info = ();
1580         $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 );
1581         $second_string_handle->execute();
1583         while (@page_info = $second_string_handle->fetchrow_array()){
1585                 $page_found = 1;
1587         }
1588         
1589         # Check if the page really does exist.
1591         if ($page_found){
1593                 $error = "PageAlreadyExists";
1594                 return;
1596         }
1598         # Add the page to the new database.
1600         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1601                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1602                 \'' . $class->convert($database_page{"PageName"}) . '\',
1603                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1604                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1605                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1606                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1607                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1608                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1609         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1610         $second_string_handle->execute();
1612         # Delete the page from the old database.
1614         $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 );
1615         $string_handle->execute();
1619 sub copypage{
1620 #################################################################################
1621 # copypage: Copies a page from the old database to the new database.            #
1622 #                                                                               #
1623 # Usage:                                                                        #
1624 #                                                                               #
1625 # $dbmodule->copypage(options);                                                 #
1626 #                                                                               #
1627 # options       Specifies the following options in any order.                   #
1628 #                                                                               #
1629 # PageFilename  Specifies the page with the filename to copy.                   #
1630 #################################################################################
1632         $error = "";
1633         $errorext = "";
1635         # Get the values passed to the subroutine.
1637         my $class = shift;
1638         my ($passedoptions) = @_;
1640         my (%database_page, $database_page);
1641         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1642         my @page_info;
1643         my $page_found = 0;
1645         # Get the data that was passed to the subroutine.
1647         my $page_filename = $passedoptions->{"PageFilename"};
1649         # Check if the page with the filename given exists.
1651         $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 );
1652         $string_handle->execute();
1654         # Check if the page really does exist.
1656         while (@page_info = $string_handle->fetchrow_array()){
1658                 # Get the values from the array.
1660                 $pagefilename           = $page_info[0];
1661                 $pagename               = $page_info[1];
1662                 $pagedescription        = $page_info[2];
1663                 $pagesection            = $page_info[3];
1664                 $pagetemplate           = $page_info[4];
1665                 $pagedata               = $page_info[5];
1666                 $pagesettings           = $page_info[6];
1667                 $pagelastmodified       = $page_info[7];
1669                 # Put the values into the page hash.
1671                 %database_page = (
1672                         "PageFilename"          => $pagefilename,
1673                         "PageName"              => $pagename,
1674                         "PageDescription"       => $pagedescription,
1675                         "PageSection"           => $pagesection,
1676                         "PageTemplate"          => $pagetemplate,
1677                         "PageContent"           => $pagedata,
1678                         "PageSettings"          => $pagesettings,
1679                         "PageLastModified"      => $pagelastmodified,
1680                 );
1682                 # The page information is found.
1684                 $page_found = 1;
1686         }
1688         # Check if the page really does exist.
1690         if (!$page_found){
1692                 $error = "PageDoesNotExist";
1693                 return;
1695         }
1697         # Check if the page with the filename given already exists in
1698         # the database the page is being moved to.
1700         $page_found = 0;
1701         @page_info = ();
1703         $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 );
1704         $second_string_handle->execute();
1706         while (@page_info = $second_string_handle->fetchrow_array()){
1708                 $page_found = 1;
1710         }
1711         
1712         # Check if the page really does exist.
1714         if ($page_found){
1716                 $error = "PageAlreadyExists";
1717                 return;
1719         }
1721         # Add the page to the new database.
1723         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1724                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1725                 \'' . $class->convert($database_page{"PageName"}) . '\',
1726                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1727                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1728                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1729                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1730                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1731                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1732         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1733         $second_string_handle->execute();
1737 #################################################################################
1738 # Filter subroutines.                                                           #
1739 #################################################################################
1741 sub connectfilter{
1742 #################################################################################
1743 # connectfilter: Connect to the filter database.                                #
1744 #                                                                               #
1745 # Usage:                                                                        #
1746 #                                                                               #
1747 # $dbmodule->connectfilter(missingignore);                                      #
1748 #                                                                               #
1749 # missingignore Ignore error about database being missing.                      #
1750 #################################################################################
1752         $error = "";
1753         $errorext = "";
1755         my $class = shift;
1756         my $ignoremissing = shift;
1758         # Check if the template database exists.
1760         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1761         
1762         if ($filterdatabase_exists eq 1){
1764                 $filterdb_exists = 0;
1766                 if (!$ignoremissing){
1768                         $error = "FilterDatabaseDoesNotExist";
1769                         return;
1771                 }
1773         }
1775         # Check if the permission settings for the template database are valid.
1777         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1779         if ($filterdb_permissions eq 1){
1781                 # The template database has invalid permissions set
1782                 # so return an error value.
1784                 if (!$ignoremissing){
1786                         $error = "FilterDatabaseInvalidPermissionsSet";
1787                         return;
1789                 }
1791         }
1793         # Connect to the template database.
1795         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1796         $database_handle->{unicode} = 1;
1797         $filterdb_loaded = 1;
1801 sub disconnectfilter{
1802 #################################################################################
1803 # disconnectfilter: Disconnect from the filter database.                        #
1804 #                                                                               #
1805 # Usage:                                                                        #
1806 #                                                                               #
1807 # $dbmodule->disconnectfilter();                                                #
1808 #################################################################################
1810         # Disconnect the template database.
1812         if ($filterdb_loaded eq 1){
1814                 undef $filterdb_string_handle;
1815                 $filterdb_database_handle->disconnect();
1817         }
1821 sub getfilterlist{
1822 #################################################################################
1823 # getfilterlist: Gets the list of filters in the filter database.               #
1824 #                                                                               #
1825 # Usage:                                                                        #
1826 #                                                                               #
1827 # $dbmodule->getfilterlist();                                                   #
1828 #################################################################################
1830         $error = "";
1831         $errorext = "";
1833         my @filter_list;
1834         my @filter_data;
1836         # Get the list of filters available.
1838         $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 );
1839         $filterdb_string_handle->execute();
1841         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1843                 # Add the filter to the list of available filters.
1845                 push(@filter_list, $filter_data[0]);
1847         }
1849         undef $filterdb_string_handle;
1850         return @filter_list;
1854 sub getfilterinfo{
1855 #################################################################################
1856 # getfilterinfo: Gets information about the filter.                             #
1857 #                                                                               #
1858 # Usage:                                                                        #
1859 #                                                                               #
1860 # $dbmodule->getfilterinfo(options);                                            #
1861 #                                                                               #
1862 # options       Specifies the following options in any order.                   #
1863 #                                                                               #
1864 # FilterID      Specifies the filter ID number to get information from.         #
1865 #################################################################################
1867         $error = "";
1868         $errorext = "";
1870         # Get the values passed to the subroutine.
1872         my $class               = shift;
1873         my ($passedoptions)     = @_;
1875         my %filter_info;
1876         my $filter_exists       = 0;
1877         my @filter_data;
1879         # Get the values that are in the hash.
1881         my $filter_id           = $passedoptions->{"FilterID"};
1883         $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 );
1884         $filterdb_string_handle->execute();
1886         # Get the filter information.
1888         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1890                 $filter_info{"FilterID"}        = $filter_data[0];
1891                 $filter_info{"FilterPriority"}  = $filter_data[1];
1892                 $filter_info{"FilterFind"}      = $filter_data[2];
1893                 $filter_info{"FilterReplace"}   = $filter_data[3];
1894                 $filter_info{"FilterNotes"}     = $filter_data[4];
1896                 $filter_exists = 1;
1898         }
1900         # Check if the filter exists.
1902         if (!$filter_exists){
1904                 # The filter does not exist so return
1905                 # an error value.
1907                 $error = "FilterDoesNotExist";
1908                 return;
1910         }
1912         # Return the filter information.
1914         undef $filterdb_string_handle;
1915         return %filter_info;
1919 sub addfilter{
1920 #################################################################################
1921 # addfilter: Adds a filter to the filter database.                              #
1922 #                                                                               #
1923 # Usage:                                                                        #
1924 #                                                                               #
1925 # $dbmodule->addfilter(options);                                                #
1926 #                                                                               #
1927 # options       Specifies the following options in any order.                   #
1928 #                                                                               #
1929 # FindFilter    Specifies the find filter to add.                               #
1930 # ReplaceFilter Specifies the replace filter to add.                            #
1931 # Priority      Specifies the filter priority to use.                           #
1932 # Notes         Specifies the notes to use.                                     #
1933 #################################################################################
1935         $error = "";
1936         $errorext = "";
1938         # Get the values passed to the subroutine.
1940         my $class = shift;
1941         my ($passedoptions) = @_;
1943         # Define some variables for later.
1945         my @database_filters;
1946         my @filterid_list;
1947         my @filterid_check;
1948         my $nofiltertable = 0;
1949         my $filter_found = 0;
1950         my $filter_count = 0;
1951         my $filter_id;
1952         my $new_id;
1954         # Get the values from the hash.
1956         my $filter_find         = $passedoptions->{"FindFilter"};
1957         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
1958         my $filter_priority     = $passedoptions->{"Priority"};
1959         my $filter_notes        = $passedoptions->{"Notes"};
1961         # Check if the filter database permissions are valid.
1963         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
1964         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
1966         if ($filterdb_permissions eq 1){
1968                 if ($filterdb_exists eq 0){
1969                         $error = "FilterDatabaseInvalidPermissionsSet";
1970                         return;
1971                 }
1973         }
1975         # Check if certain values are undefined and if they
1976         # are then set them blank.
1978         if (!$filter_find){
1980                 $filter_find = "";
1982         }
1984         if (!$filter_replace){
1986                 $filter_replace = "";
1988         }
1990         if (!$filter_priority){
1992                 $filter_priority = 1;
1994         }
1996         if (!$filter_notes){
1998                 $filter_notes = "";
2000         }
2002         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2004         if ($directory_permissions eq 1 && $filterdb_exists){
2006                 # The template database cannot be created because of invalid directory
2007                 # permissions so return an error value.
2009                 $error = "FilterDatabaseFileUncreateable";
2010                 return; 
2012         }
2014         # Check if the filter table exists.
2016         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2018         # Check if there is really no filter table.
2020         if ($nofiltertable){
2022                 # Create the filter database table.
2024                 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2025                         id int(7) primary key,
2026                         priority int(5),
2027                         findsetting varchar(1024),
2028                         replacesetting varchar(1024),
2029                         notes text
2030                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2031                 $filterdb_string_handle->execute();
2033         }
2035         # Find the lowest filter identification number available.
2037         $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 );
2038         $filterdb_string_handle->execute();
2040         while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2042                 $filter_id      = $database_filters[0];
2044                 # Add the filter identification to the list of filter IDs.
2046                 push(@filterid_list, $filter_id);
2048         }
2050         $filter_id = "";
2052         # Process each filter looking for a blank available filter.
2054         foreach $filter_id (@filterid_list){
2056                 # Check the next filter ID to see if it's blank.
2058                 $new_id = $filter_id + 1;
2060                 $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 );
2061                 $filterdb_string_handle->execute();
2063                 # Get the filter identification number.
2065                 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2067                         $filter_found = 1;
2069                 }
2071                 # Check if a filter was found.
2073                 if (!$filter_found){
2075                         # No filter was found using this ID so exit the loop.
2077                         last;
2079                 }
2081                 # Increment the filter count and reset the filter found value.
2083                 $filter_count++;
2084                 $filter_found = 0;
2085                 $new_id = 0;
2087         }
2089         # Check if there were any filters in the filters database.
2091         if (!$filter_count && !$new_id){
2093                 # There were no filters in the filters database so set
2094                 # the new filter identification value to 1.
2096                 $new_id = 1;
2098         }
2100         # Add the filter to the filter database.
2102         $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2103                 \'' . $class->convert($new_id) . '\',
2104                 \'' . $class->convert($filter_priority) . '\',
2105                 \'' . $class->convert($filter_find) . '\',
2106                 \'' . $class->convert($filter_replace) .'\',
2107                 \'' . $class->convert($filter_notes) . '\'
2108         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2109         $filterdb_string_handle->execute();
2113 sub editfilter{
2114 #################################################################################
2115 # editfilter: Edits a filter in the filter database.                            #
2116 #                                                                               #
2117 # Usage:                                                                        #
2118 #                                                                               #
2119 # $dbmodule->editfilter(options);                                               #
2120 #                                                                               #
2121 # options       Specifies the following options in any order.                   #
2122 #                                                                               #
2123 # FilterID              Specifies the filter to edit.                           #
2124 # NewFindFilter         Specifies the new find filter setting.                  #
2125 # NewReplaceFilter      Specifies the new replace filter setting.               #
2126 # NewFilterPriority     Specifies the new filter priority setting.              #
2127 # NewFilterNotes        Specifies the new notes for the filter.                 #
2128 #################################################################################
2130         $error = "";
2131         $errorext = "";
2133         # Get the values passed to the subroutine.
2135         my $class = shift;
2136         my ($passedoptions) = @_;
2138         my @filter_data;
2139         my $filter_exists = 1;
2140         my $blankfile = 0;
2142         # Get the values from the hash.
2144         my $filter_id           = $passedoptions->{"FilterID"};
2145         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2146         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2147         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2148         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2150         # Check if the filter database permissions are valid.
2152         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2153         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2155         if ($filterdb_permissions eq 1){
2157                 if ($filterdb_exists eq 0){
2158                         $error = "FilterDatabaseInvalidPermissionsSet";
2159                         return;
2160                 }
2162         }
2164         # Check if the filter exists before editing it.
2166         $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 );
2167         $filterdb_string_handle->execute();
2169         # Check if the filter exists.
2171         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2173                 $filter_exists = 1;
2175         }       
2177         # Check if the filter really does exist.
2179         if (!$filter_exists){
2181                 # The filter does not exist so return
2182                 # an error value.
2184                 $error = "FilterDoesNotExist";
2185                 return;
2187         }
2189         # Edit the selected filter.
2191         $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2192                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2193                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2194                 priority = \'' . $class->convert($filter_newpriority) . '\',
2195                 notes = \'' . $class->convert($filter_newnotes) . '\'
2196         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2197         $filterdb_string_handle->execute();
2199         undef $filterdb_string_handle;
2200         return;
2204 sub deletefilter{
2205 #################################################################################
2206 # deletefilter: Deletes a filter from the filter database.                      #
2207 #                                                                               #
2208 # Usage:                                                                        #
2209 #                                                                               #
2210 # $dbmodule->deletefilter(options);                                             #
2211 #                                                                               #
2212 # options       Specifies the following options in any order.                   #
2213 #                                                                               #
2214 # FilterID      Specifies the filter to delete from the filter database.        #
2215 #################################################################################
2217         $error = "";
2218         $errorext = "";
2220         # Get the values passed to the subroutine.
2222         my $class = shift;
2223         my ($passedoptions) = @_;
2225         my $filter_exists = 0;
2226         my @filter_data;
2228         # Get the values from the hash.
2230         my $filter_id           = $passedoptions->{"FilterID"};
2232         # Check if the filter exists before deleting.
2234         $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 );
2235         $filterdb_string_handle->execute();
2237         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2239                 $filter_exists = 1;
2241         }
2243         # Check to see if the filter really does exist.
2245         if (!$filter_exists){
2247                 $error = "FilterDoesNotExist";
2248                 return;
2250         }
2252         # Delete the filter from the filter database.
2254         $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 );
2255         $filterdb_string_handle->execute();
2257         undef $filterdb_string_handle;
2261 #################################################################################
2262 # Template subroutines.                                                         #
2263 #################################################################################
2265 sub connecttemplate{
2266 #################################################################################
2267 # connecttemplate: Connect to the template database.                            #
2268 #                                                                               #
2269 # Usage:                                                                        #
2270 #                                                                               #
2271 # $dbmodule->connecttemplate(missingignore);                                    #
2272 #                                                                               #
2273 # missingignore Ignore errror about database being missing.                     #
2274 #################################################################################
2276         $error = "";
2277         $errorext = "";
2279         my $class = shift;
2280         my $ignoremissing = shift;
2282         # Check if the template database exists.
2284         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2285         
2286         if ($templatedatabase_exists eq 1){
2288                 $templatedb_exists = 0;
2290                 if (!$ignoremissing){
2292                         $error = "TemplateDatabaseDoesNotExist";
2293                         return;
2295                 }
2297         }
2299         # Check if the permission settings for the template database are valid.
2301         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2303         if ($templatedb_permissions eq 1){
2305                 # The template database has invalid permissions set
2306                 # so return an error value.
2308                 if (!$ignoremissing){
2310                         $error = "TemplateDatabaseInvalidPermissionsSet";
2311                         return;
2313                 }
2315         }
2317         # Connect to the template database.
2319         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2320         $database_handle->{unicode} = 1;
2321         $templatedb_loaded = 1;
2325 sub disconnecttemplate{
2326 #################################################################################
2327 # disconnecttemplate: Disconnect from the template database.                    #
2328 #                                                                               #
2329 # Usage:                                                                        #
2330 #                                                                               #
2331 # $dbmodule->disconnecttemplate();                                              #
2332 #################################################################################
2334         # Disconnect the template database.
2336         if ($templatedb_loaded eq 1){
2338                 undef $template_string_handle;
2339                 $template_database_handle->disconnect();
2341         }
2345 sub gettemplatelist{
2346 #################################################################################
2347 # gettemplatelist: Gets the list of templates.                                  #
2348 #                                                                               #
2349 # Usage:                                                                        #
2350 #                                                                               #
2351 # $dbmodule->gettemplatelist();                                                 #
2352 #################################################################################
2354         $error = "";
2355         $errorext = "";
2357         $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 );
2358         $template_string_handle->execute();
2360         my @database_template;
2361         my @templates_list;
2362         my $template_filename;
2364         while (@database_template = $template_string_handle->fetchrow_array()){
2366                 # Get certain values from the array.
2368                 $template_filename      = $database_template[0];
2370                 # Add the template to the list of templates.
2372                 push(@templates_list, $template_filename);
2374         }
2376         return @templates_list;
2380 sub gettemplateinfo{
2381 #################################################################################
2382 # gettemplateinfo: Get information on a template.                               #
2383 #                                                                               #
2384 # Usage:                                                                        #
2385 #                                                                               #
2386 # $dbmodule->gettemplateinfo(options);                                          #
2387 #                                                                               #
2388 # options       Specifies the following options in any order.                   #
2389 #                                                                               #
2390 # TemplateFilename      Specifies the template filename to use.                 #
2391 #################################################################################
2393         $error = "";
2394         $errorext = "";
2396         # Get the data passed to the subroutine.
2398         my $class = shift;
2399         my ($passedoptions) = @_;
2401         my %page_info;
2402         my @template_data;
2404         my $template_filename;
2405         my $template_name;
2406         my $template_description;
2407         my $template_datemodified;
2408         my $template_layout;
2410         my $template_found = 0;
2412         my $filename    = $passedoptions->{"TemplateFilename"};
2414         $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 );
2415         $template_string_handle->execute();
2417         while (@template_data = $template_string_handle->fetchrow_array()){
2419                 # Get certain values from the array.
2421                 $template_filename      = $template_data[0];
2422                 $template_name          = $template_data[1];
2423                 $template_description   = $template_data[2];
2424                 $template_layout        = $template_data[3];
2425                 $template_datemodified  = $template_data[4];
2427                 # Process them into the hash.
2429                 %page_info = (
2430                         "TemplateFilename" => $template_filename,
2431                         "TemplateName" => $template_name,
2432                         "TemplateDescription" => $template_description,
2433                         "TemplateLayout" => $template_layout,
2434                         "TemplateLastModified" => $template_datemodified
2435                 );
2437                 $template_found = 1;
2439         }
2441         if ($template_found eq 0){
2443                 # The template was not found in the template database so
2444                 # write an error value.
2446                 $error = "TemplateDoesNotExist";
2447                 return;
2449         }
2451         return %page_info;
2455 sub addtemplate{
2456 #################################################################################
2457 # addtemplate: Adds a template to the template database.                        #
2458 #                                                                               #
2459 # Usage:                                                                        #
2460 #                                                                               #
2461 # $dbmodule->addtemplate();                                                     #
2462 #                                                                               #
2463 # options       Specifies the following options in any order.                   #
2464 #                                                                               #
2465 # TemplateFilename      Specifies the new template filename.                    #
2466 # TemplateName          Specifies the new template name.                        #
2467 # TemplateDescription   Specifies the new template description.                 #
2468 # TemplateLayout        Specifies the new template layout.                      #
2469 #################################################################################
2471         $error = "";
2472         $errorext = "";
2474         # Get the data passed to the subroutine.
2476         my $class = shift;
2477         my ($passedoptions) = @_;
2479         my @page_exists;
2480         my $notemplatetable;
2481         my $blankfile = 0;
2483         my $template_filename           = $passedoptions->{"TemplateFilename"};
2484         my $template_name               = $passedoptions->{"TemplateName"};
2485         my $template_description        = $passedoptions->{"TemplateDescription"};
2486         my $template_layout             = $passedoptions->{"TemplateLayout"};
2488         # Check if the template database permissions are valid.
2490         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2491         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2493         if ($templatedb_permissions eq 1){
2495                 if ($templatedb_exists eq 0){
2496                         $error = "TemplateDatabaseInvalidPermissionsSet";
2497                         return;
2498                 }
2500         }
2502         # Check if the template already exists before adding.
2504         if ($templatedb_exists eq 0){
2506                 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2508                 if ($blankfile eq 0){
2510                         $template_string_handle->execute();
2512                         while (@page_exists = $template_string_handle->fetchrow_array()){
2514                                 $error = "TemplatePageExists";
2515                                 return;
2517                         }
2519                 }
2521         }
2523         # Get the current date.
2524  
2525         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2526  
2527         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2529         # Check if certain values are undefined and if they
2530         # are then set them blank.
2532         if (!$template_name){
2534                 $template_name = "";
2536         }
2538         if (!$template_description){
2540                 $template_description = "";
2542         }
2544         if (!$template_layout){
2546                 $template_layout = "";
2548         }
2550         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2552         if ($directory_permissions eq 1 && $templatedb_exists){
2554                 # The template database cannot be created because of invalid directory
2555                 # permissions so return an error value.
2557                 $error = "TemplateDatabaseUncreateable";
2558                 return; 
2560         }
2562         # Check to see if a template can be added.
2564         $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2565                                 \'' . $class->convert($template_filename) . '\',
2566                                 \'' . $class->convert($template_name) . '\',
2567                                 \'' . $class->convert($template_description) . '\',
2568                                 \'' . $class->convert($template_layout) . '\',
2569                                 \'' . $class->convert($template_date) . '\'
2570         )') or ( $notemplatetable = 1 );
2572         if (!$notemplatetable){
2574                 $template_string_handle->execute();
2576         }
2578         # Check to see if there is no template table and attempt to create one.
2580         if ($notemplatetable){
2582                 # Create a template table.
2584         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2586         if ($directory_permissions eq 1){
2588                 # The template database cannot be created because of invalid directory
2589                 # permissions so return an error.
2591                 $error = "TemplateDatabaseFileUncreateable";
2592                 return;
2594         }
2596                 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2597                         filename varchar(256) primary key,
2598                         templatename varchar(512),
2599                         templatedescription varchar(512),
2600                         templatelayout text,
2601                         datemodified datetime
2602                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2603                 $template_string_handle->execute();
2605                 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2606                                 \'' . $class->convert($template_filename) . '\',
2607                                 \'' . $class->convert($template_name) . '\',
2608                                 \'' . $class->convert($template_description) . '\',
2609                                 \'' . $class->convert($template_layout) . '\',
2610                                 \'' . $class->convert($template_date) . '\'
2611                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2612                 $template_string_handle->execute();
2614         }
2618 sub deletetemplate{
2619 #################################################################################
2620 # deletetemplate: Deletes a template from the template database.                #
2621 #                                                                               #
2622 # Usage:                                                                        #
2623 #                                                                               #
2624 # $dbmodule->deletetemplate(options);                                           #
2625 #                                                                               #
2626 # options       Specifies the following options in any order.                   #
2627 #                                                                               #
2628 # TemplateFilename      Specifies the template filename to delete.              #
2629 #################################################################################
2631         $error = "";
2632         $errorext = "";
2634         # Get the data passed to the subroutine.
2636         my $class = shift;
2637         my ($passedoptions) = @_;
2639         my @pagedata;
2640         my $template_filename = $passedoptions->{"TemplateFilename"};
2641         my $template_count = 0;
2643         # Check if the template exists.
2645         $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 );
2646         $template_string_handle->execute();
2648         while (@pagedata = $template_string_handle->fetchrow_array()){
2650                 $template_count++;
2652         }
2654         if ($template_count eq 0){
2656                 # No pages were returned so return an error value.
2658                 $error = "TemplateDoesNotExist";
2659                 return;
2661         }
2663         # Delete the template from the template database.
2665         $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 );
2666         $template_string_handle->execute();
2670 sub edittemplate{
2671 #################################################################################
2672 # editttemplate: Edits a Kiriwrite template.                                    #
2673 #                                                                               #
2674 # Usage:                                                                        #
2675 #                                                                               #
2676 # $dbmodule->edittemplate(options);                                             #
2677 #                                                                               #
2678 # options       Specifies the following options in any order.                   #
2679 #                                                                               #
2680 # TemplateFilename              Specifies the template filename to edit.        #
2681 # NewTemplateFilename           Specifies the new template filename.            #
2682 # NewTemplateName               Specifies the new template name.                #
2683 # NewTemplateDescription        Specifies the new template description.         #
2684 # NewTemplateLayout             Specifies the new template layout.              #
2685 #################################################################################
2687         # Get the values passed.
2689         my $class = shift;
2690         my ($passedoptions) = @_;
2691         my $template_found = 0;
2692         my @template_info;
2694         # Process the values passed.
2696         my $template_filename           = $passedoptions->{"TemplateFilename"};
2697         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2698         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2699         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2700         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2702         # Check if the template database permissions are valid.
2704         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2705         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2707         if ($templatedb_permissions eq 1){
2709                 if ($templatedb_exists eq 0){
2710                         $error = "TemplateDatabaseInvalidPermissionsSet";
2711                         return;
2712                 }
2714         }
2716         # Check if the template exists.
2718         $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 );
2719         $template_string_handle->execute();
2721         while (@template_info = $template_string_handle->fetchrow_array()){
2723                 $template_found = 1;
2725         }
2727         # Check to see if the template was found and set an error value if
2728         # it wasn't.
2730         if ($template_found eq 0){
2732                 $error = "TemplateDoesNotExist";
2733                 return;
2735         }
2737         # Get the date and time.
2739         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2740         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2742         # Update the template information.
2744         $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2745                 filename = \'' . $class->convert($new_template_filename) . '\',
2746                 templatename = \'' . $class->convert($new_template_name) . '\',
2747                 templatedescription = \'' . $class->convert($new_template_description) . '\',
2748                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2749                 datemodified = \'' . $class->convert($templatenewdate) . '\'
2750                 WHERE filename = \'' . $class->convert($template_filename) . '\'
2751         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2752         $template_string_handle->execute();
2756 #################################################################################
2757 # General subroutines.                                                          #
2758 #################################################################################
2760 sub connect{
2761 #################################################################################
2762 # connect: Connect to the server.                                               #
2763 #                                                                               #
2764 # Usage:                                                                        #
2765 #                                                                               #
2766 # $dbmodule->connect();                                                         #
2767 #################################################################################
2769         # This function is not needed in this database module.
2773 sub disconnect{
2774 #################################################################################
2775 # connect: Disconnect from the server.                                          #
2776 #                                                                               #
2777 # Usage:                                                                        #
2778 #                                                                               #
2779 # $dbmodule->disconnect();                                                      #
2780 #################################################################################
2782         # This function is not needed in this database module.
2784         undef $string_handle;
2788 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