Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
0a88d273083fa66e4de1f82e3d8dd6022e03547a
[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 Modules::Database::SQLite;
26 # Enable strict and use warnings.
28 use Modules::System::Common;
29 use strict;
30 use warnings;
32 # Load the following Perl modules.
34 use DBI qw(:sql_types);
36 # Set the following values.
38 our $VERSION    = "0.2.0";
39 my ($options, %options);
40 my $database_handle;
41 my $statement_handle;
42 my $error = "";
43 my $errorext = "";
44 my $database_filename;
45 my $second_database_filename;
46 my $second_database_handle;
47 my $second_statement_handle;
48 my $templatedb_loaded = 0;
49 my $templatedb_exists = 1;
50 my $template_statement_handle;
51 my $template_database_handle;
52 my $filterdb_loaded = 0;
53 my $filterdb_exists = 1;
54 my $filterdb_statement_handle;
55 my $filterdb_database_handle;
58 #################################################################################
59 # Generic Subroutines.                                                          #
60 #################################################################################
62 sub new{
63 #################################################################################
64 # new: Create an instance of Kiriwrite::Database::SQLite                        #
65 #                                                                               #
66 # Usage:                                                                        #
67 #                                                                               #
68 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
69 #################################################################################
70         
71         # Get the perl module name.
73         my $class = shift;
74         my $self = {};
76         return bless($self, $class);
78 }
80 sub loadsettings{
81 #################################################################################
82 # loadsettings: Loads settings into the SQLite database module                  #
83 #                                                                               #
84 # Usage:                                                                        #
85 #                                                                               #
86 # $dbmodule->loadsettings(Directory, options);                                  #
87 #                                                                               #
88 # options       Specifies the following options (in any order).                 #
89 #                                                                               #
90 # Directory     Specifies the directory to use for getting databases.           #
91 # DateTime      Specifies the date and time format to use.                      #
92 # Server        Specifies the server to use.                                    #
93 # Database      Specifies the database to use.                                  #
94 # Username      Specifies the username to use.                                  #
95 # Password      Specifies the password to use.                                  #
96 # HashType      Specifies the password hash type to use.                        #
97 # Port          Specifies the server port to use.                               #
98 # Protocol      Specifies the protocol to use.                                  #
99 # TablePrefix   Specifies the table prefix to use.                              #
100 #################################################################################
102         # Get the data passed to the subroutine.
104         my $class = shift;
105         my ($passedoptions)     = @_;
107         # Add the directory setting to the list of options (as it's the only
108         # one needed for this database module).
110         %options = (
111                 "Directory"     => $passedoptions->{"Directory"},
112                 "DateTime"      => $passedoptions->{"DateTime"},
113         );
117 sub convert{
118 #################################################################################
119 # convert: Converts data into SQL formatted data.                               #
120 #                                                                               #
121 # Usage:                                                                        #
122 #                                                                               #
123 # $dbmodule->convert(data);                                                     #
124 #                                                                               #
125 # data          Specifies the data to convert.                                  #
126 #################################################################################
128         # Get the data passed to the subroutine.
130         my $class       = shift;
131         my $data        = shift;
133         if (!$data){
134                 $data = "";
135         }
137         $data =~ s/'/''/g;
138         $data =~ s/\b//g;
140         return $data;
144 sub dateconvert{
145 #################################################################################
146 # dateconvert: Converts a SQL date into a proper date.                          #
147 #                                                                               #
148 # Usage:                                                                        #
149 #                                                                               #
150 # $dbmodule->dateconvert(date);                                                 #
151 #                                                                               #
152 # date          Specifies the date to convert.                                  #
153 #################################################################################
155         # Get the date passed to the subroutine.
157         my $class       = shift;
158         my $data        = shift;
160         # Convert the date given into the proper date.
162         # Create the following varialbes to be used later.
164         my $date;
165         my $time;
166         my $day;
167         my $day_full;
168         my $month;
169         my $month_check;
170         my $month_full;
171         my $year;
172         my $year_short;
173         my $hour;
174         my $hour_full;
175         my $minute;
176         my $minute_full;
177         my $second;
178         my $second_full;
179         my $seek = 0;
180         my $timelength;
181         my $datelength;
182         my $daylength;
183         my $secondlength;
184         my $startchar = 0;
185         my $char;
186         my $length;
187         my $count = 0;
189         # Split the date and time.
191         $length = length($data);
193         if ($length > 0){
195                 do {
197                         # Get the character and check if it is a space.
199                         $char = substr($data, $seek, 1);
201                         if ($char eq ' '){
203                                 # The character is a space, so get the date and time.
205                                 $date           = substr($data, 0, $seek);
206                                 $timelength     = $length - $seek - 1;
207                                 $time           = substr($data, $seek + 1, $timelength);
209                         }
211                         $seek++;
213                 } until ($seek eq $length);
215                 # Get the year, month and date.
217                 $length = length($date);
218                 $seek = 0;
220                 do {
222                         # Get the character and check if it is a dash.
224                         $char = substr($date, $seek, 1);
226                         if ($char eq '-'){
228                                 # The character is a dash, so get the year, month or day.
230                                 $datelength = $seek - $startchar;
232                                 if ($count eq 0){
234                                         # Get the year from the date.
236                                         $year           = substr($date, 0, $datelength) + 1900;
237                                         $startchar      = $seek;
238                                         $count = 1;
240                                         # Get the last two characters to get the short year
241                                         # version.
243                                         $year_short     = substr($year, 2, 2);
245                                 } elsif ($count eq 1){
247                                         # Get the month and day from the date.
249                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
251                                         # Check if the month is less then 10, if it is
252                                         # add a zero to the value.
254                                         if ($month < 10){
256                                                 $month_full = '0' . $month;
258                                         } else {
260                                                 $month_full = $month;
262                                         }
264                                         $startchar      = $seek;
265                                         $count = 2;
267                                         $daylength      = $length - $seek + 1;
268                                         $day            = substr($date, $startchar + 1, $daylength);
270                                         # Check if the day is less than 10, if it is
271                                         # add a zero to the value.
273                                         if ($day < 10){
275                                                 $day_full       = '0' . $day;
277                                         } else {
279                                                 $day_full       = $day;
281                                         }
283                                 }
285                         }
287                         $seek++;
289                 } until ($seek eq $length);
291                 # Get the length of the time value and reset certain
292                 # values to 0.
294                 $length = length($time);
295                 $seek = 0;
296                 $count = 0;
297                 $startchar = 0;
299                 do {
301                         # Get the character and check if it is a colon.
303                         $char = substr($time, $seek, 1);
305                         if ($char eq ':'){
307                                 # The character is a colon, so get the hour, minute and day.
309                                 $timelength = $seek - $startchar;
311                                 if ($count eq 0){
313                                         # Get the hour from the time.
315                                         $hour = substr($time, 0, $timelength);
316                                         $count = 1;
317                                         $startchar = $seek;
319                                         # If the hour is less than ten then add a
320                                         # zero.
322                                         if ($hour < 10){
324                                                 $hour_full = '0' . $hour;
326                                         } else {
328                                                 $hour_full = $hour;
330                                         }
332                                 } elsif ($count eq 1){
334                                         # Get the minute and second from the time.
336                                         $minute = substr($time, $startchar + 1, $timelength - 1);
337                                         $count = 2;
338                                                 
339                                         # If the minute is less than ten then add a
340                                         # zero.
342                                         if ($minute < 10){
344                                                 $minute_full = '0' . $minute;
346                                         } else {
348                                                 $minute_full = $minute;
350                                         }
352                                         $startchar = $seek;
354                                         $secondlength = $length - $seek + 1;
355                                         $second = substr($time, $startchar + 1, $secondlength);
356                                         
357                                         # If the second is less than ten then add a
358                                         # zero.
360                                         if ($second < 10){
362                                                 $second_full = '0' . $second;
364                                         } else {
366                                                 $second_full = $second;
368                                         }
370                                 }
372                         }
374                         $seek++;
376                 } until ($seek eq $length);
378                 # Get the setting for displaying the date and time.
380                 $data = $options{"DateTime"};
382                 # Process the setting for displaying the date and time
383                 # using regular expressions
385                 $data =~ s/DD/$day_full/g;
386                 $data =~ s/D/$day/g;
387                 $data =~ s/MM/$month_full/g;
388                 $data =~ s/M/$month/g;
389                 $data =~ s/YY/$year/g;
390                 $data =~ s/Y/$year_short/g;
392                 $data =~ s/hh/$hour_full/g;
393                 $data =~ s/h/$hour/g;
394                 $data =~ s/mm/$minute_full/g;
395                 $data =~ s/m/$minute/g;
396                 $data =~ s/ss/$second_full/g;
397                 $data =~ s/s/$second/g;
399         }
401         return $data;
405 sub geterror{
406 #################################################################################
407 # geterror: Gets the error message (or extended error message).                 #
408 #                                                                               #
409 # Usage:                                                                        #
410 #                                                                               #
411 # $dbmodule->geterror(extended);                                                #
412 #                                                                               #
413 # Extended      Specifies if the extended error should be retrieved.            #
414 #################################################################################
416         # Get the data passed to the subroutine.
418         my $class       = shift;
419         my $extended    = shift;
421         if (!$extended){
422                 $extended = 0;
423         }
425         if (!$errorext){
426                 $errorext = "";
427         }
429         if (!$error){
430                 $error = "";
431         }
433         # Check to see if extended information should be returned.
435         if ($extended eq 1){
437                 # Extended information should be returned.
439                 return $errorext;
441         } else {
443                 # Basic information should be returned.
445                 return $error;
447         }
451 sub dbpermissions{
452 #################################################################################
453 # dbpermissions: Check if the permissions for the database are valid.           #
454 #                                                                               #
455 # Usage:                                                                        #
456 #                                                                               #
457 # $database->dbpermissions(dbname, read, write);                                #
458 #                                                                               #
459 # dbname        Specifies the database name to check.                           #
460 # read          Check to see if the database can be read.                       #
461 # write         Check to see if the database can be written.                    #
462 #################################################################################
464         # Get the database name, read setting and write setting.
466         my ($class, $dbname, $readper, $writeper)       = @_;
468         # Check if the database can be read.
470         if ($readper){
472                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
474                         # The database can be read.
476                 } else {
478                         # The database cannot be read, so return a value
479                         # of 1.
481                         return 1;
483                 }
485         }
487         # Check if the database can be written.
489         if ($writeper){
491                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
493                         # The database can be read.
495                 } else {
497                         # The database cannot be read, so return a value
498                         # of 1.
500                         return 1;
502                 }
504         }
506         # No errors have occured while checking so return a value
507         # of 0.
509         return 0;
513 sub dbexists{
514 #################################################################################
515 # dbexists: Check if the database exists.                                       #
516 #                                                                               #
517 # Usage:                                                                        #
518 #                                                                               #
519 # $dbmodule->dbexists(dbname);                                                  #
520 #                                                                               #
521 # dbname        Specifies the database name to check.                           #
522 #################################################################################
524         # Get the value that was passed to the subroutine.
526         my $class       = shift;
527         my ($filename)  = @_;
529         # Check if the filename exists, if it does, return a value of 1, else
530         # return a value of 0, meaning that the file was not found.
532         if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
534                 # Specified file does exist so return a value of 0.
536                 return 0;
538         } else {
540                 # Specified file does not exist so return a value of 1.
542                 return 1;
544         }
548 #################################################################################
549 # Database Subroutines.                                                         #
550 #################################################################################
552 sub getdblist{
553 #################################################################################
554 # getdblist: Gets the list of available databases.                              #
555 #                                                                               #
556 # Usage:                                                                        #
557 #                                                                               #
558 # $dbmodule->getdblist();                                                       #
559 #################################################################################
561         # Get the list of databases.
563         my @data_directory;
564         my @data_directory_final;
565         my $database;
566         my $database_filename_length;
567         my $database_filename_friendly;
569         # Check if the database directory has valid permission settings.
571         if (-e $options{"Directory"}){
573                 # The database directory does exist. So check if
574                 # the permission settings are valid.
576                 if (-r $options{"Directory"}){
578                         # The permission settings for reading the directory
579                         # are valid.
581                 } else {
583                         # The permission settings for reading the directory
584                         # are invalid so return an error value.
586                         $error = "DataDirInvalidPermissions";
587                         return;
589                 }
591         } else {
593                 # The database directory does not exist, so return an
594                 # error value.
596                 $error = "DataDirMissing";
597                 return;
599         }
601         opendir(DATADIR, $options{"Directory"});
602         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
603         closedir(DATADIR);
605         # Process the list of databases.
607         foreach $database (@data_directory){
609                 $database =~ s/.db.sqlite$//og;
610                 $database_filename_friendly = $database;
612                 #$database_filename_length = length($database);
613                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
614                 push(@data_directory_final, $database_filename_friendly);
616         }
618         # Return the list of databases.
620         return @data_directory_final;
624 sub getdatabaseinfo{
625 #################################################################################
626 # getdatabaseinfo: Get information about the database.                          #
627 #                                                                               #
628 # Usage:                                                                        #
629 #                                                                               #
630 # $dbmodule->getdatabaseinfo();                                                 #
631 #################################################################################
633         # Get the database information.
635         my $class = shift;
636         my ($databaseinfo, %databaseinfo);
637         my ($sqldata, @sqldata);
639         $error = "";
640         $errorext = "";
642         $statement_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 (
643                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
644         );
645         $statement_handle->execute();
647         @sqldata = $statement_handle->fetchrow_array();
649         # Process the database information into a hash.
651         %databaseinfo = (
652                 "DatabaseName"  => $sqldata[0],
653                 "Description"   => $sqldata[1],
654                 "Notes"         => $sqldata[2],
655                 "Categories"    => $sqldata[3],
656                 "Major"         => $sqldata[4],
657                 "Minor"         => $sqldata[5],
658                 "Revision"      => $sqldata[6]
659         );
661         $statement_handle->finish();
662         undef $statement_handle;
664         return %databaseinfo;
668 sub getseconddatabaseinfo{
669 #################################################################################
670 # getseconddatabaseinfo: Get information about the database that pages will be  #
671 # moved or copied to.                                                           #
672 #                                                                               #
673 # Usage:                                                                        #
674 #                                                                               #
675 # $dbmodule->getseconddatabaseinfo();                                           #
676 #################################################################################
678         # Get the database information.
680         my $class = shift;
681         my ($databaseinfo, %databaseinfo);
682         my ($sqldata, @sqldata);
684         $error = "";
685         $errorext = "";
687         $second_statement_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 (
688                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
689         );
690         $second_statement_handle->execute();
692         @sqldata = $second_statement_handle->fetchrow_array();
694         # Process the database information into a hash.
696         %databaseinfo = (
697                 "DatabaseName"  => $sqldata[0],
698                 "Description"   => $sqldata[1],
699                 "Notes"         => $sqldata[2],
700                 "Categories"    => $sqldata[3],
701                 "Major"         => $sqldata[4],
702                 "Minor"         => $sqldata[5],
703                 "Revision"      => $sqldata[6]
704         );
706         $second_statement_handle->finish();
707         undef $second_statement_handle;
709         return %databaseinfo;
713 sub adddatabase{
714 #################################################################################
715 # adddatabase: Adds a Kiriwrite database.                                       #
716 #                                                                               #
717 # Usage:                                                                        #
718 #                                                                               #
719 # $dbmodule->adddatabase(options);                                              #
720 #                                                                               #
721 # options       Specifies the following options in any order.                   #
722 #                                                                               #
723 # DatabaseFilename      Specifies the database file/shortname to use.           #
724 # DatabaseName          Specifies the database name to use.                     #
725 # DatabaseDescription   Specifies the database description to use.              #
726 # DatabaseNotes         Specifies the database notes to use.                    #
727 # DatabaseCategories    Specifies the database categories to use.               #
728 # VersionMajor          Specifies the major version.                            #
729 # VersionMinor          Specifies the minor version.                            #
730 # VersionRevision       Specifies the revision version.                         #
731 #################################################################################
733         # Get the database that was passed to the subroutine.
735         $error  = "";
736         $errorext = "";
738         my $class       = shift;
739         my ($passedoptions) = @_;
741         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
742         my $dbname              = $passedoptions->{"DatabaseName"};
743         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
744         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
745         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
746         my $dbmajorver          = $passedoptions->{"VersionMajor"};
747         my $dbminorver          = $passedoptions->{"VersionMinor"};
748         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
750         # Check if the database with the filename given already exists.
752         my $database_exists     = $class->dbexists($dbfilename);
754         if ($database_exists eq 0){
756                 # The database filename exists so set the error value.
758                 $error = "DatabaseExists";
759                 return;
761         }
763         # Create the database structure.
765         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
766         $database_handle->{unicode} = 1;
767         $statement_handle               = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
768                         name varchar(256) primary key, 
769                         description varchar(512), 
770                         notes text, 
771                         categories varchar(512), 
772                         kiriwrite_version_major int(4), 
773                         kiriwrite_version_minor int(4), 
774                         kiriwrite_version_revision int(4)
775         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
776         $statement_handle->execute();
778         $statement_handle       = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
779                         filename varchar(256) primary key, 
780                         pagename varchar(512), 
781                         pagedescription varchar(512), 
782                         pagesection varchar(256),
783                         pagetemplate varchar(64),
784                         pagedata text,
785                         pagesettings int(1),
786                         lastmodified datetime
787         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
788         $statement_handle->execute();
790         # Convert the values into SQL query formatted values and add an entry
791         # to the kiriwrite_database_info table.
793         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
794                 \'' . $class->convert($dbname) . '\',
795                 \'' . $class->convert($dbdescription) . '\',
796                 \'' . $class->convert($dbnotes) . '\',
797                 \'' . $class->convert($dbcategories) . '\',
798                 \'' . $class->convert($dbmajorver) . '\',
799                 \'' . $class->convert($dbminorver) . '\',
800                 \'' . $class->convert($dbrevisionver) . '\'
801         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
802         $statement_handle->execute();
806 sub editdatabase{
807 #################################################################################
808 # editdatabase: Edits a Kiriwrite Database.                                     #
809 #                                                                               #
810 # Usage:                                                                        #
811 #                                                                               #
812 # $dbmodule->editdatabase(options);                                             #
813 #                                                                               #
814 # options               Specifies the following options in any order.           #
815 #                                                                               #
816 # NewDatabaseFilename   Specifies the new database filename to use.             #
817 # DatabaseName          Specifies the new database name.                        #
818 # DatabaseDescription   Specifies the new database description.                 #
819 # DatabaseNotes         Specifies the new database notes.                       #
820 # DatabaseCategories    Specifies the new database categories.                  #
821 #################################################################################
823         $error          = "";
824         $errorext       = "";
826         my $class       = shift;
827         my ($passedoptions) = @_;
829         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
830         my $dbname              = $passedoptions->{"DatabaseName"};
831         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
832         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
833         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
835         # Check if a new database filename has been specified and if a
836         # new database filename has been specified then change the
837         # database filename.
839         if ($database_filename ne $dbnewfilename){
841                 # A new database filename has been given so check if the output
842                 # directory has write access.
844                 if (-r $options{"Directory"}){
845                         
846                         # The directory is readable.
848                 } else {
850                         # The directory is not readable so set the error value.
852                         $error = "DataDirInvalidPermissions";
854                         return;
856                 }
858                 if (-w $options{"Directory"}){
860                         # The directory is writeable.
862                 } else {
864                         # The directory is not writeable so set the error value.
866                         $error = "DataDirInvalidPermissions";
868                         return;
870                 }
872                 # Check if a database filename already exists before using the
873                 # new filename.
875                 my $database_newexists          = $class->dbexists($dbnewfilename);
877                 if ($database_newexists eq 0){
879                         # The database filename exists so set the error value.
881                         $error = "DatabaseExists";
882                         return;
884                 }
886                 # Check if the database can be renamed (has write access).
888                 my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
890                 if ($database_permissions eq 1){
892                         # The database filename exists so set the error value.
894                         $error = "InvalidPermissionsSet";
895                         return;
897                 }
899                 # "Disconnect" from the database.
901                 $database_handle->disconnect();
903                 # Rename the database.
905                 ($database_filename)    = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
906                 ($dbnewfilename)        = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
908                 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
910                 # Reload the database from the new filename.
912                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
913                 $database_handle->{unicode} = 1;
914                 $database_filename = $dbnewfilename;
916         }
918         # Check if the database can be altered with the new data.
920         my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
922         if ($database_permissions eq 1){
924                 # The database filename exists so set the error value.
926                 $error = "InvalidPermissionsSet";
927                 return;
929         }
931         # Get the current database information.
933         $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
934         $statement_handle->execute();
936         my @database_oldinfo    = $statement_handle->fetchrow_array();
938         my $dboldname           = $database_oldinfo[0];
940         # Update the database information.
942         $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
943         description = \'' . $class->convert($dbdescription) . '\',
944         notes = \'' . $class->convert($dbnotes) . '\',
945         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
946         $statement_handle->execute();
948         undef $statement_handle;
949         return;
953 sub deletedatabase{
954 #################################################################################
955 # deletedatabase: Deletes a Kiriwrite database.                                 #
956 #                                                                               #
957 # Usage:                                                                        #
958 #                                                                               #
959 # $dbmodule->deletedatabase(options);                                           #
960 #                                                                               #
961 # options       Specifies the following options in any order.                   #
962 #                                                                               #
963 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
964 #################################################################################
966         $error          = "";
967         $errorext       = "";
969         # Get the database filename.
971         my $class               = shift;
972         my ($passedoptions)     = shift;
974         my $databasename        = $passedoptions->{"DatabaseName"};
976         # Check if the database exists.
978         my $database_exists             = $class->dbexists($databasename);
980         if ($database_exists eq 1){
982                 # The database does not exist so set the error value.
984                 $error = "DoesNotExist";
985                 return;
987         }
989         # Check if the database permissions are valid.
991         my $database_permissions        = $class->dbpermissions($databasename);
993         if ($database_permissions eq 1){
995                 # The database permissions are invalid so set the error
996                 # value.
998                 $error = "InvalidPermissionsSet";
999                 return;
1001         }
1003         # Delete the database.
1005         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1007         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1011 sub selectdb{
1012 #################################################################################
1013 # selectdb: Selects the Kiriwrite database.                                     #
1014 #                                                                               #
1015 # Usage:                                                                        #
1016 #                                                                               #
1017 # $dbmodule->connect(options);                                                  #
1018 #                                                                               #
1019 # options       Specifies the following options in any order.                   #
1020 #                                                                               #
1021 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1022 #################################################################################
1024         # Get the database name.
1026         $error = "";
1027         $errorext = "";
1029         my $class = shift;
1030         my ($passedoptions) = @_;
1031         my (%database, $database);
1033         my $dbname = $passedoptions->{"DatabaseName"};
1035         # Check if the database exists.
1037         my $database_exists = $class->dbexists($dbname);
1039         if ($database_exists eq 1){
1041                 # The database does not exist so return an error value
1042                 # saying that the database does not exist.
1044                 $error = "DoesNotExist";
1046                 return;
1048         }
1050         # Check if the database has valid permissions set.
1052         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1054         if ($database_permissions eq 1){
1056                 # The database has invalid permissions set so return
1057                 # an error value saying that the database has invalid
1058                 # permissions set.
1060                 $error = "InvalidPermissionsSet";
1061                 
1062                 return;
1064         }
1066         # Connect to the database.
1068         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1069         $database_handle->{unicode} = 1;
1070         $database_filename = $dbname;
1074 sub selectseconddb{
1075 #################################################################################
1076 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1077 # pages to.                                                                     #
1078 #                                                                               #
1079 # Usage:                                                                        #
1080 #                                                                               #
1081 # $dbmodule->selectseconddb(options);                                           #
1082 #                                                                               #
1083 # options       Specifies the following options in any order.                   #
1084 #                                                                               #
1085 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1086 #################################################################################
1088         # Get the database name.
1090         $error = "";
1091         $errorext = "";
1093         my $class = shift;
1094         my ($passedoptions) = @_;
1095         my (%database, $database);
1097         my $dbname = $passedoptions->{"DatabaseName"};
1099         # Check if the database exists.
1101         my $database_exists = $class->dbexists($dbname);
1103         if ($database_exists eq 1){
1105                 # The database does not exist so return an error value
1106                 # saying that the database does not exist.
1108                 $error = "DoesNotExist";
1110                 return;
1112         }
1114         # Check if the database has valid permissions set.
1116         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1118         if ($database_permissions eq 1){
1120                 # The database has invalid permissions set so return
1121                 # an error value saying that the database has invalid
1122                 # permissions set.
1124                 $error = "InvalidPermissionsSet";
1125                 
1126                 return;
1128         }
1130         # Connect to the database.
1132         $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1133         $second_database_handle->{unicode} = 1;
1134         $second_database_filename = $dbname;    
1138 #################################################################################
1139 # Page subroutines.                                                             #
1140 #################################################################################
1142 sub getpagelist{
1143 #################################################################################
1144 # getpagelist: Gets the list of pages from the database.                        #
1145 #                                                                               #
1146 # Usage:                                                                        #
1147 #                                                                               #
1148 # $dbmodule->getpagelist(options);                                              #
1149 #                                                                               #
1150 # options       Specifies the following options in any order.                   #
1151 #                                                                               #
1152 # StartFrom     Start from the specified page in the database.                  #
1153 # Limit         Get the amount of pages given.                                  #
1154 #################################################################################
1156         $error = "";
1157         $errorext = "";
1159         my $class               = shift;
1160         my ($passedoptions)     = shift;
1162         my $start_from  = $passedoptions->{"StartFrom"};
1163         my $limit       = $passedoptions->{"Limit"};
1165         if (defined($start_from)){
1167                 if (!$limit){
1168                         
1169                         $limit = 0;
1171                 }
1173                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1174                 $statement_handle->execute();
1176         } else {
1178                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1179                 $statement_handle->execute();
1181         }
1183         my @database_pagefilenames;
1184         my @database_pagefilenames_final;
1186         # Process the collected pages.
1188         while (@database_pagefilenames = $statement_handle->fetchrow_array){
1190                 # Add each page to the list of pages in the database.
1192                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1194         }
1196         undef $statement_handle;
1197         return @database_pagefilenames_final;
1201 sub getpagecount{
1202 #################################################################################
1203 # getpagecount: Get the count of pages that are in the database.                #
1204 #                                                                               #
1205 # Usage:                                                                        #
1206 #                                                                               #
1207 # $dbmodule->getpagecount();                                                    #
1208 #################################################################################
1210         $error = "";
1211         $errorext = "";
1213         my $class       = shift;
1215         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1216         $statement_handle->execute();
1218         my $count = $statement_handle->fetchrow_array();
1220         return $count;
1224 sub getpageinfo{
1225 #################################################################################
1226 # getpageinfo: Gets the page information from the filename passed.              #
1227 #                                                                               #
1228 # Usage:                                                                        #
1229 #                                                                               #
1230 # $dbmodule->getpageinfo(options);                                              #
1231 #                                                                               #
1232 # options       Specifies the following options in any order.                   #
1233 #                                                                               #
1234 # PageFilename  Specifies the page filename to get the page information from.   #
1235 # Reduced       Specifies if the reduced version of the page information should #
1236 #               be retrieved.                                                   #
1237 #################################################################################
1239         $error = "";
1240         $errorext = "";
1242         my $class               = shift;
1243         my ($passedoptions)     = shift;
1244         my (%database_page, $database_page);
1245         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1247         my @data_page;
1248         my $page_found = 0;
1250         # Get the page from the database.
1252         my $page_filename       = $passedoptions->{"PageFilename"};
1253         my $page_reduced        = $passedoptions->{"Reduced"};
1255         if (!$page_reduced){
1257                 $page_reduced = 0;
1259         }
1261         if ($page_reduced eq 1){
1263                 $statement_handle       = $database_handle->prepare('SELECT filename, pagename, pagedescription, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1265                 $statement_handle->execute();
1267                 # Check if the page exists in the database.
1269                 while (@data_page = $statement_handle->fetchrow_array()){
1270         
1271                         # Get the values from the array.
1272         
1273                         $pagefilename           = $data_page[0];
1274                         $pagename               = $data_page[1];
1275                         $pagedescription        = $data_page[2];
1276                         $pagelastmodified       = $data_page[3];
1277         
1278                         # Put the values into the page hash.
1279         
1280                         %database_page = (
1281                                 "PageFilename"          => $pagefilename,
1282                                 "PageName"              => $pagename,
1283                                 "PageDescription"       => $pagedescription,
1284                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1285                         );
1286         
1287                         $page_found = 1;
1288         
1289                 }
1292         } else {
1293                 
1294                 $statement_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 );
1296                 $statement_handle->execute();
1298                 # Check if the page exists in the database.
1300                 while (@data_page = $statement_handle->fetchrow_array()){
1301         
1302                         # Get the values from the array.
1303         
1304                         $pagefilename           = $data_page[0];
1305                         $pagename               = $data_page[1];
1306                         $pagedescription        = $data_page[2];
1307                         $pagesection            = $data_page[3];
1308                         $pagetemplate           = $data_page[4];
1309                         $pagedata               = $data_page[5];
1310                         $pagesettings           = $data_page[6];
1311                         $pagelastmodified       = $data_page[7];
1312         
1313                         # Put the values into the page hash.
1314         
1315                         %database_page = (
1316                                 "PageFilename"          => $pagefilename,
1317                                 "PageName"              => $pagename,
1318                                 "PageDescription"       => $pagedescription,
1319                                 "PageSection"           => $pagesection,
1320                                 "PageTemplate"          => $pagetemplate,
1321                                 "PageContent"           => $pagedata,
1322                                 "PageSettings"          => $pagesettings,
1323                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1324                         );
1325         
1326                         $page_found = 1;
1327         
1328                 }
1330         }
1331         
1332         # Check if the page did exist.
1334         if (!$page_found){
1336                 $error = "PageDoesNotExist";
1337                 return;
1339         }
1341         undef $statement_handle;
1342         return %database_page;
1346 sub addpage{
1347 #################################################################################
1348 # addpage: Add a page to the selected database.                                 #
1349 #                                                                               #
1350 # Usage:                                                                        #
1351 #                                                                               #
1352 # $dbmodule->addpage(options);                                                  #
1353 #                                                                               #
1354 # options       Specifies the following options in any order.                   #
1355 #                                                                               #
1356 # PageFilename          Specifies the page filename to use.                     #
1357 # PageName              Specifies the page name to use.                         #
1358 # PageDescription       Specifies the page description to use.                  #
1359 # PageSection           Specifies the page section to use.                      #
1360 # PageTemplate          Specifies the page template to use.                     #
1361 # PageContent           Specifies the page content to use.                      #
1362 # PageSettings          Specifies the page settings to use.                     #
1363 #################################################################################
1365         # Get the data that was passed to the subroutine.
1367         $error = "";
1368         $errorext = "";
1370         my $class               = shift;
1371         my ($passedoptions)     = shift;
1373         my @database_page;
1374         my $page_count = 0;
1376         # Get the values passed to the hash.
1378         my $page_filename       = $passedoptions->{"PageFilename"};
1379         my $page_name           = $passedoptions->{"PageName"};
1380         my $page_description    = $passedoptions->{"PageDescription"};
1381         my $page_section        = $passedoptions->{"PageSection"};
1382         my $page_template       = $passedoptions->{"PageTemplate"};
1383         my $page_content        = $passedoptions->{"PageContent"};
1384         my $page_settings       = $passedoptions->{"PageSettings"};
1386         # Check to see if the filename given already exists
1387         # in the page database.
1389         $statement_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 );
1390         $statement_handle->execute();
1392         # Check if a page with the filename given really does
1393         # exist.
1395         while (@database_page = $statement_handle->fetchrow_array()){
1397                 # A page does exist so increment the count to 1.
1399                 $page_count++;
1400                 
1401         }
1403         if ($page_count ne 0){
1405                 # The page does exist so set the error value.
1407                 $error = "PageExists";
1408                 return;
1410         }
1412         # Check if certain values are undefined.
1414         if (!$page_name){
1416                 $page_name = "";
1418         }
1420         if (!$page_description){
1422                 $page_description = "";
1424         }
1426         if (!$page_section){
1428                 $page_section = "";
1430         }
1432         if (!$page_content){
1434                 $page_content = "";
1436         }
1438         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1439         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1441         # Add the page to the selected database.
1443         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1444                 \'' . $class->convert($page_filename) . '\',
1445                 \'' . $class->convert($page_name) . '\',
1446                 \'' . $class->convert($page_description) . '\',
1447                 \'' . $class->convert($page_section) . '\',
1448                 \'' . $class->convert($page_template) . '\',
1449                 \'' . $class->convert($page_content) . '\',
1450                 \'' . $class->convert($page_settings) . '\',
1451                 \'' . $class->convert($page_date) . '\'
1452         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1453         $statement_handle->execute();
1455         undef $statement_handle;
1459 sub deletepage{
1460 #################################################################################
1461 # deletepage: Delete a page from the selected database.                         #
1462 #                                                                               #
1463 # Usage:                                                                        #
1464 #                                                                               #
1465 # $dbmodule->deletepage(options)                                                #
1466 #                                                                               #
1467 # options       Specifies the following options in any order.                   #
1468 #                                                                               #
1469 # PageFilename  Specifies the page filename to delete.                          #
1470 #################################################################################
1472         $error = "";
1473         $errorext = "";
1475         # Get the data that was passed to the subroutine.
1477         my $class = shift;
1478         my ($passedoptions) = @_;
1479         my @page_info;
1480         my $page_found = 0;
1482         # Get the page filename.
1484         my $page_filename = $passedoptions->{"PageFilename"};
1486         # Check if the page exists before deleting it.
1488         $statement_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 );
1489         $statement_handle->execute();
1491         while (@page_info = $statement_handle->fetchrow_array()){
1493                 $page_found = 1;
1495         }
1497         # Check if the page really does exist.
1499         if (!$page_found){
1501                 $error = "PageDoesNotExist";
1502                 return;
1504         }
1506         # Delete the page.
1508         $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1509         $statement_handle->execute();
1513 sub editpage{
1514 #################################################################################
1515 # editpage: Edit a page from the selected database.                             #
1516 #                                                                               #
1517 # Usage:                                                                        #
1518 #                                                                               #
1519 # $dbmodule->editpage(options);                                                 #
1520 #                                                                               #
1521 # options       Specifies the following options in any order.                   #
1522 #                                                                               #
1523 # PageFilename          Specifies the filename to edit.                         #
1524 # PageNewFilename       Specifies the new filename to use.                      #
1525 # PageNewName           Specifies the new page name to use.                     #
1526 # PageNewDescription    Specifies the new page description to use.              #
1527 # PageNewSection        Specifies the new page section to use.                  #
1528 # PageNewTemplate       Specifies the new page template to use.                 #
1529 # PageNewContent        Specifies the new page content to use.                  #
1530 # PageNewSettings       Specifies the new page settings to use.                 #
1531 #################################################################################
1533         $error = "";
1534         $errorext = "";
1536         # Get the values passed to the subroutine.
1538         my $class = shift;
1539         my ($passedoptions) = @_;
1540         my $page_found = 0;
1541         my @page_info;
1543         # Get the data that was passed to the subroutine.
1545         my $page_filename       = $passedoptions->{"PageFilename"};
1546         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1547         my $page_newname        = $passedoptions->{"PageNewName"};
1548         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1549         my $page_newsection     = $passedoptions->{"PageNewSection"};
1550         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1551         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1552         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1554         # Check if the page with the filename given exists.
1556         $statement_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 );
1557         $statement_handle->execute();
1559         # Check if the page really does exist.
1561         while (@page_info = $statement_handle->fetchrow_array()){
1563                 # The page information is found.
1565                 $page_found = 1;
1567         }
1569         # Check if the page really does exist.
1571         if (!$page_found){
1573                 $error = "PageDoesNotExist";
1574                 return;
1576         }
1578         # Check if there is a page that already exists with the new
1579         # filename.
1581         $page_found = 0;
1583         if ($page_filename ne $page_newfilename){
1585                 $statement_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1586                 $statement_handle->execute();
1588                 # Check if a page really is using the new filename.
1590                 while (@page_info = $statement_handle->fetchrow_array()){
1592                         # The page information is found.
1594                         $page_found = 1;
1596                 }
1598                 if ($page_found eq 1){
1600                         $error = "PageExists";
1601                         return;
1603                 }
1605         }
1607         # Get the current date.
1609         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1610         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1612         # Edit the selected page.
1614         $statement_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 );
1615         $statement_handle->execute();
1619 sub movepage{
1620 #################################################################################
1621 # movepage: Moves a page from the old database to the new database.             #
1622 #                                                                               #
1623 # Usage:                                                                        #
1624 #                                                                               #
1625 # $dbmodule->movepage(options);                                                 #
1626 #                                                                               #
1627 # options       Specifies the following options in any order.                   #
1628 #                                                                               #
1629 # PageFilename  Specifies the page with the filename to move.                   #
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         $statement_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         $statement_handle->execute();
1654         # Check if the page really does exist.
1656         while (@page_info = $statement_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_statement_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_statement_handle->execute();
1706         while (@page_info = $second_statement_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_statement_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_statement_handle->execute();
1735         # Delete the page from the old database.
1737         $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1738         $statement_handle->execute();
1742 sub copypage{
1743 #################################################################################
1744 # copypage: Copies a page from the old database to the new database.            #
1745 #                                                                               #
1746 # Usage:                                                                        #
1747 #                                                                               #
1748 # $dbmodule->copypage(options);                                                 #
1749 #                                                                               #
1750 # options       Specifies the following options in any order.                   #
1751 #                                                                               #
1752 # PageFilename  Specifies the page with the filename to copy.                   #
1753 #################################################################################
1755         $error = "";
1756         $errorext = "";
1758         # Get the values passed to the subroutine.
1760         my $class = shift;
1761         my ($passedoptions) = @_;
1763         my (%database_page, $database_page);
1764         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1765         my @page_info;
1766         my $page_found = 0;
1768         # Get the data that was passed to the subroutine.
1770         my $page_filename = $passedoptions->{"PageFilename"};
1772         # Check if the page with the filename given exists.
1774         $statement_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 );
1775         $statement_handle->execute();
1777         # Check if the page really does exist.
1779         while (@page_info = $statement_handle->fetchrow_array()){
1781                 # Get the values from the array.
1783                 $pagefilename           = $page_info[0];
1784                 $pagename               = $page_info[1];
1785                 $pagedescription        = $page_info[2];
1786                 $pagesection            = $page_info[3];
1787                 $pagetemplate           = $page_info[4];
1788                 $pagedata               = $page_info[5];
1789                 $pagesettings           = $page_info[6];
1790                 $pagelastmodified       = $page_info[7];
1792                 # Put the values into the page hash.
1794                 %database_page = (
1795                         "PageFilename"          => $pagefilename,
1796                         "PageName"              => $pagename,
1797                         "PageDescription"       => $pagedescription,
1798                         "PageSection"           => $pagesection,
1799                         "PageTemplate"          => $pagetemplate,
1800                         "PageContent"           => $pagedata,
1801                         "PageSettings"          => $pagesettings,
1802                         "PageLastModified"      => $pagelastmodified,
1803                 );
1805                 # The page information is found.
1807                 $page_found = 1;
1809         }
1811         # Check if the page really does exist.
1813         if (!$page_found){
1815                 $error = "PageDoesNotExist";
1816                 return;
1818         }
1820         # Check if the page with the filename given already exists in
1821         # the database the page is being moved to.
1823         $page_found = 0;
1824         @page_info = ();
1826         $second_statement_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 );
1827         $second_statement_handle->execute();
1829         while (@page_info = $second_statement_handle->fetchrow_array()){
1831                 $page_found = 1;
1833         }
1834         
1835         # Check if the page really does exist.
1837         if ($page_found){
1839                 $error = "PageAlreadyExists";
1840                 return;
1842         }
1844         # Add the page to the new database.
1846         $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1847                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1848                 \'' . $class->convert($database_page{"PageName"}) . '\',
1849                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1850                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1851                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1852                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1853                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1854                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1855         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1856         $second_statement_handle->execute();
1860 #################################################################################
1861 # Filter subroutines.                                                           #
1862 #################################################################################
1864 sub connectfilter{
1865 #################################################################################
1866 # connectfilter: Connect to the filter database.                                #
1867 #                                                                               #
1868 # Usage:                                                                        #
1869 #                                                                               #
1870 # $dbmodule->connectfilter(missingignore);                                      #
1871 #                                                                               #
1872 # missingignore Ignore error about database being missing.                      #
1873 #################################################################################
1875         $error = "";
1876         $errorext = "";
1878         my $class = shift;
1879         my $ignoremissing = shift;
1881         # Check if the template database exists.
1883         my $filterdatabase_exists = kiriwrite_fileexists("filters.db.sqlite");
1884         
1885         if ($filterdatabase_exists eq 1){
1887                 $filterdb_exists = 0;
1889                 if (!$ignoremissing){
1891                         $error = "FilterDatabaseDoesNotExist";
1892                         return;
1894                 }
1896         }
1898         # Check if the permission settings for the template database are valid.
1900         my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1902         if ($filterdb_permissions eq 1){
1904                 # The template database has invalid permissions set
1905                 # so return an error value.
1907                 if (!$ignoremissing){
1909                         $error = "FilterDatabaseInvalidPermissionsSet";
1910                         return;
1912                 }
1914         }
1916         # Connect to the template database.
1918         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1919         $filterdb_database_handle->{unicode} = 1;
1920         $filterdb_loaded = 1;
1924 sub disconnectfilter{
1925 #################################################################################
1926 # disconnectfilter: Disconnect from the filter database.                        #
1927 #                                                                               #
1928 # Usage:                                                                        #
1929 #                                                                               #
1930 # $dbmodule->disconnectfilter();                                                #
1931 #################################################################################
1933         # Disconnect the template database.
1935         if ($filterdb_loaded eq 1){
1937                 undef $filterdb_statement_handle;
1938                 $filterdb_database_handle->disconnect();
1940         }
1944 sub getfiltercount{
1945 #################################################################################
1946 # getfiltercount: Gets the count of filters in the filters database.            #
1947 #                                                                               #
1948 # Usage:                                                                        #
1949 #                                                                               #
1950 # $dbmodule->getfiltercount();                                                  #
1951 #################################################################################
1953         $error = "";
1954         $errorext = "";
1956         my $class       = shift;
1958         $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_filters') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return);
1959         $filterdb_statement_handle->execute();
1961         my $count = $filterdb_statement_handle->fetchrow_array();
1963         return $count;  
1967 sub getfilterlist{
1968 #################################################################################
1969 # getfilterlist: Gets the list of filters in the filter database.               #
1970 #                                                                               #
1971 # Usage:                                                                        #
1972 #                                                                               #
1973 # $dbmodule->getfilterlist(options);                                            #
1974 #                                                                               #
1975 # StartFrom     Specifies where the list of filters should start from.          #
1976 # Limit         Specifies the amount of the filters to get.                     #
1977 #################################################################################
1979         $error = "";
1980         $errorext = "";
1982         my $class               = shift;
1983         my ($passedoptions)     = shift;
1984         
1985         my @filter_list;
1986         my @filter_data;
1988         my $start_from  = $passedoptions->{"StartFrom"};
1989         my $limit       = $passedoptions->{"Limit"};
1991         if (defined($start_from)){
1993                 if (!$limit){
1994                         
1995                         $limit = 0;
1997                 }
1999                 $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC LIMIT ' . $start_from . ',' . $limit) or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2000                 $filterdb_statement_handle->execute();
2002         } else {
2004                 $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2005                 $filterdb_statement_handle->execute();
2007         }
2009         # Get the list of filters available.
2011         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2013                 # Add the filter to the list of available filters.
2015                 push(@filter_list, $filter_data[0]);
2017         }
2019         undef $filterdb_statement_handle;
2020         return @filter_list;
2024 sub getfilterinfo{
2025 #################################################################################
2026 # getfilterinfo: Gets information about the filter.                             #
2027 #                                                                               #
2028 # Usage:                                                                        #
2029 #                                                                               #
2030 # $dbmodule->getfilterinfo(options);                                            #
2031 #                                                                               #
2032 # options       Specifies the following options in any order.                   #
2033 #                                                                               #
2034 # FilterID      Specifies the filter ID number to get information from.         #
2035 # Reduced       Specifies if the reduced version of the filter information      #
2036 #               should be retrieved.                                            #
2037 #################################################################################
2039         $error = "";
2040         $errorext = "";
2042         # Get the values passed to the subroutine.
2044         my $class               = shift;
2045         my ($passedoptions)     = @_;
2047         my %filter_info;
2048         my $filter_exists       = 0;
2049         my @filter_data;
2051         # Get the values that are in the hash.
2053         my $filter_id           = $passedoptions->{"FilterID"};
2054         my $reduced             = $passedoptions->{"Reduced"};
2055         
2056         if ($reduced && $reduced eq 1){
2058                 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2059                 $filterdb_statement_handle->execute();
2061         } else {
2063                 $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, enabled, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2064                 $filterdb_statement_handle->execute();
2066         }
2068         # Get the filter information.
2070         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2072                 $filter_info{"FilterID"}        = $filter_data[0];
2073                 $filter_info{"FilterPriority"}  = $filter_data[1];
2074                 $filter_info{"FilterFind"}      = $filter_data[2];
2075                 $filter_info{"FilterReplace"}   = $filter_data[3];
2076                 $filter_info{"FilterEnabled"}   = $filter_data[4];
2077                 $filter_info{"FilterNotes"}     = $filter_data[5];
2079                 $filter_exists = 1;
2081         }
2083         # Check if the filter exists.
2085         if (!$filter_exists){
2087                 # The filter does not exist so return
2088                 # an error value.
2090                 $error = "FilterDoesNotExist";
2091                 return;
2093         }
2095         # Return the filter information.
2097         undef $filterdb_statement_handle;
2098         return %filter_info;
2102 sub addfilter{
2103 #################################################################################
2104 # addfilter: Adds a filter to the filter database.                              #
2105 #                                                                               #
2106 # Usage:                                                                        #
2107 #                                                                               #
2108 # $dbmodule->addfilter(options);                                                #
2109 #                                                                               #
2110 # options       Specifies the following options in any order.                   #
2111 #                                                                               #
2112 # FindFilter    Specifies the find filter to add.                               #
2113 # ReplaceFilter Specifies the replace filter to add.                            #
2114 # Priority      Specifies the filter priority to use.                           #
2115 # Notes         Specifies the notes to use.                                     #
2116 #################################################################################
2118         $error = "";
2119         $errorext = "";
2121         # Get the values passed to the subroutine.
2123         my $class = shift;
2124         my ($passedoptions) = @_;
2126         # Define some variables for later.
2128         my @database_filters;
2129         my @filterid_list;
2130         my @filterid_check;
2131         my $nofiltertable = 0;
2132         my $filter_found = 0;
2133         my $filter_count = 0;
2134         my $filter_id;
2135         my $new_id;
2137         # Get the values from the hash.
2139         my $filter_find         = $passedoptions->{"FindFilter"};
2140         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2141         my $filter_priority     = $passedoptions->{"Priority"};
2142         my $filter_enabled      = $passedoptions->{"Enabled"};
2143         my $filter_notes        = $passedoptions->{"Notes"};
2145         # Check if the filter database permissions are valid.
2147         my $filterdb_exists = kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2148         my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2150         if ($filterdb_permissions eq 1){
2152                 if ($filterdb_exists eq 0){
2153                         $error = "FilterDatabaseInvalidPermissionsSet";
2154                         return;
2155                 }
2157         }
2159         # Check if certain values are undefined and if they
2160         # are then set them blank.
2162         if (!$filter_find){
2164                 $filter_find = "";
2166         }
2168         if (!$filter_replace){
2170                 $filter_replace = "";
2172         }
2174         if (!$filter_priority){
2176                 $filter_priority = 1;
2178         }
2180         if (!$filter_notes){
2182                 $filter_notes = "";
2184         }
2186         if (!$filter_enabled){
2188                 $filter_enabled = "";
2190         }
2192         my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2194         if ($directory_permissions eq 1 && $filterdb_exists){
2196                 # The template database cannot be created because of invalid directory
2197                 # permissions so return an error value.
2199                 $error = "FilterDatabaseFileUncreateable";
2200                 return; 
2202         }
2204         # Check if the filter table exists.
2206         $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2208         # Check if there is really no filter table.
2210         if ($nofiltertable){
2212                 # Create the filter database table.
2214                 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2215                         id int(7) primary key,
2216                         priority int(5),
2217                         findsetting varchar(1024),
2218                         replacesetting varchar(1024),
2219                         enabled boolean,
2220                         notes text
2221                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2222                 $filterdb_statement_handle->execute();
2224         }
2226         # Find the lowest filter identification number available.
2228         $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2229         $filterdb_statement_handle->execute();
2231         while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2233                 $filter_id      = $database_filters[0];
2235                 # Add the filter identification to the list of filter IDs.
2237                 push(@filterid_list, $filter_id);
2239         }
2241         $filter_id = "";
2243         # Process each filter looking for a blank available filter.
2245         foreach $filter_id (@filterid_list){
2247                 # Check the next filter ID to see if it's blank.
2249                 $new_id = $filter_id + 1;
2251                 $filterdb_statement_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 );
2252                 $filterdb_statement_handle->execute();
2254                 # Get the filter identification number.
2256                 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2258                         $filter_found = 1;
2260                 }
2262                 # Check if a filter was found.
2264                 if (!$filter_found){
2266                         # No filter was found using this ID so exit the loop.
2268                         last;
2270                 }
2272                 # Increment the filter count and reset the filter found value.
2274                 $filter_count++;
2275                 $filter_found = 0;
2276                 $new_id = 0;
2278         }
2280         # Check if there were any filters in the filter database.
2282         if (!$filter_count && !$new_id){
2284                 # There were no filters in the filter database so set
2285                 # the new filter identification value to 1.
2287                 $new_id = 1;
2289         }
2291         # Add the filter to the filter database.
2293         $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, enabled, notes) VALUES (
2294                 \'' . $class->convert($new_id) . '\',
2295                 \'' . $class->convert($filter_priority) . '\',
2296                 \'' . $class->convert($filter_find) . '\',
2297                 \'' . $class->convert($filter_replace) .'\',
2298                 \'' . $class->convert($filter_enabled) . '\',
2299                 \'' . $class->convert($filter_notes) . '\'
2300         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2301         $filterdb_statement_handle->execute();
2305 sub editfilter{
2306 #################################################################################
2307 # editfilter: Edits a filter in the filter database.                            #
2308 #                                                                               #
2309 # Usage:                                                                        #
2310 #                                                                               #
2311 # $dbmodule->editfilter(options);                                               #
2312 #                                                                               #
2313 # options       Specifies the following options in any order.                   #
2314 #                                                                               #
2315 # FilterID              Specifies the filter to edit.                           #
2316 # NewFindFilter         Specifies the new find filter setting.                  #
2317 # NewReplaceFilter      Specifies the new replace filter setting.               #
2318 # NewFilterPriority     Specifies the new filter priority setting.              #
2319 # NewEnabled            Specifies if the filter should be enabled.              #
2320 # NewFilterNotes        Specifies the new notes for the filter.                 #
2321 #################################################################################
2323         $error = "";
2324         $errorext = "";
2326         # Get the values passed to the subroutine.
2328         my $class = shift;
2329         my ($passedoptions) = @_;
2331         my @filter_data;
2332         my $filter_exists = 1;
2333         my $blankfile = 0;
2335         # Get the values from the hash.
2337         my $filter_id           = $passedoptions->{"FilterID"};
2338         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2339         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2340         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2341         my $filter_enabled      = $passedoptions->{"NewEnabled"};
2342         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2344         # Check if the filter database permissions are valid.
2346         my $filterdb_exists = kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2347         my $filterdb_permissions = kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2349         if ($filterdb_permissions eq 1){
2351                 if ($filterdb_exists eq 0){
2352                         $error = "FilterDatabaseInvalidPermissionsSet";
2353                         return;
2354                 }
2356         }
2358         # Check if the filter exists before editing it.
2360         $filterdb_statement_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 );
2361         $filterdb_statement_handle->execute();
2363         # Check if the filter exists.
2365         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2367                 $filter_exists = 1;
2369         }       
2371         # Check if the filter really does exist.
2373         if (!$filter_exists){
2375                 # The filter does not exist so return
2376                 # an error value.
2378                 $error = "FilterDoesNotExist";
2379                 return;
2381         }
2383         # Edit the selected filter.
2385         $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2386                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2387                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2388                 priority = \'' . $class->convert($filter_newpriority) . '\',
2389                 enabled = \'' . $class->convert($filter_enabled) . '\',
2390                 notes = \'' . $class->convert($filter_newnotes) . '\'
2391         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2392         $filterdb_statement_handle->execute();
2394         undef $filterdb_statement_handle;
2395         return;
2399 sub deletefilter{
2400 #################################################################################
2401 # deletefilter: Deletes a filter from the filter database.                      #
2402 #                                                                               #
2403 # Usage:                                                                        #
2404 #                                                                               #
2405 # $dbmodule->deletefilter(options);                                             #
2406 #                                                                               #
2407 # options       Specifies the following options in any order.                   #
2408 #                                                                               #
2409 # FilterID      Specifies the filter to delete from the filter database.        #
2410 #################################################################################
2412         $error = "";
2413         $errorext = "";
2415         # Get the values passed to the subroutine.
2417         my $class = shift;
2418         my ($passedoptions) = @_;
2420         my $filter_exists = 0;
2421         my @filter_data;
2423         # Get the values from the hash.
2425         my $filter_id           = $passedoptions->{"FilterID"};
2427         # Check if the filter exists before deleting.
2429         $filterdb_statement_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 );
2430         $filterdb_statement_handle->execute();
2432         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2434                 $filter_exists = 1;
2436         }
2438         # Check to see if the filter really does exist.
2440         if (!$filter_exists){
2442                 $error = "FilterDoesNotExist";
2443                 return;
2445         }
2447         # Delete the filter from the filter database.
2449         $filterdb_statement_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2450         $filterdb_statement_handle->execute();
2452         undef $filterdb_statement_handle;
2456 #################################################################################
2457 # Template subroutines.                                                         #
2458 #################################################################################
2460 sub connecttemplate{
2461 #################################################################################
2462 # connecttemplate: Connect to the template database.                            #
2463 #                                                                               #
2464 # Usage:                                                                        #
2465 #                                                                               #
2466 # $dbmodule->connecttemplate(missingignore);                                    #
2467 #                                                                               #
2468 # missingignore Ignore errror about database being missing.                     #
2469 #################################################################################
2471         $error = "";
2472         $errorext = "";
2474         my $class = shift;
2475         my $ignoremissing = shift;
2477         # Check if the template database exists.
2479         my $templatedatabase_exists = kiriwrite_fileexists("templates.db.sqlite");
2480         
2481         if ($templatedatabase_exists eq 1){
2483                 $templatedb_exists = 0;
2485                 if (!$ignoremissing){
2487                         $error = "TemplateDatabaseDoesNotExist";
2488                         return;
2490                 }
2492         }
2494         # Check if the permission settings for the template database are valid.
2496         my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2498         if ($templatedb_permissions eq 1){
2500                 # The template database has invalid permissions set
2501                 # so return an error value.
2503                 if (!$ignoremissing){
2505                         $error = "TemplateDatabaseInvalidPermissionsSet";
2506                         return;
2508                 }
2510         }
2512         # Connect to the template database.
2514         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2515         $template_database_handle->{unicode} = 1;
2516         $templatedb_loaded = 1;
2520 sub disconnecttemplate{
2521 #################################################################################
2522 # disconnecttemplate: Disconnect from the template database.                    #
2523 #                                                                               #
2524 # Usage:                                                                        #
2525 #                                                                               #
2526 # $dbmodule->disconnecttemplate();                                              #
2527 #################################################################################
2529         # Disconnect the template database.
2531         if ($templatedb_loaded eq 1){
2533                 undef $template_statement_handle;
2534                 $template_database_handle->disconnect();
2536         }
2540 sub gettemplatecount{
2541 #################################################################################
2542 # gettemplatecount: Gets the count of templates in the template database.       #
2543 #                                                                               #
2544 # Usage:                                                                        #
2545 #                                                                               #
2546 # $dbmodule->gettemplatecount();                                                #
2547 #################################################################################
2549         $error = "";
2550         $errorext = "";
2551  
2552         my $class       = shift;
2553  
2554         $template_statement_handle      = $template_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_templates') or ( $error = "FilterDatabaseError", $errorext = $template_database_handle->errstr, return);
2555         $template_statement_handle->execute();
2556  
2557         my $count = $template_statement_handle->fetchrow_array();
2558  
2559         return $count;
2563 sub gettemplatelist{
2564 #################################################################################
2565 # gettemplatelist: Gets the list of templates.                                  #
2566 #                                                                               #
2567 # Usage:                                                                        #
2568 #                                                                               #
2569 # $dbmodule->gettemplatelist(options);                                          #
2570 #                                                                               #
2571 # options       Specifies the following options as a hash (in any order).       #
2572 #                                                                               #
2573 # StartFrom     Specifies where the list of templates will start from.          #
2574 # Limit         Specifies how many templates should be retrieved.               #
2575 #################################################################################
2577         $error = "";
2578         $errorext = "";
2580         my $class               = shift;
2581         my ($passedoptions)     = @_;
2583         my $start_from          = $passedoptions->{"StartFrom"};
2584         my $limit               = $passedoptions->{"Limit"};
2586         if (defined($start_from)){
2588                 if (!$limit){
2589                         
2590                         $limit = 0;
2592                 }
2594                 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC LIMIT ' . $start_from . ',' .  $limit ) or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2595                 $template_statement_handle->execute();          
2597         } else {
2599                 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2600                 $template_statement_handle->execute();
2602         }
2604         my @database_template;
2605         my @templates_list;
2606         my $template_filename;
2608         while (@database_template = $template_statement_handle->fetchrow_array()){
2610                 # Get certain values from the array.
2612                 $template_filename      = $database_template[0];
2614                 # Add the template to the list of templates.
2616                 push(@templates_list, $template_filename);
2618         }
2620         return @templates_list;
2624 sub gettemplateinfo{
2625 #################################################################################
2626 # gettemplateinfo: Get information on a template.                               #
2627 #                                                                               #
2628 # Usage:                                                                        #
2629 #                                                                               #
2630 # $dbmodule->gettemplateinfo(options);                                          #
2631 #                                                                               #
2632 # options       Specifies the following options in any order.                   #
2633 #                                                                               #
2634 # TemplateFilename      Specifies the template filename to use.                 #
2635 # Reduced               Specifies a reduced version of template information to  #
2636 #                       get.                                                    #
2637 #################################################################################
2639         $error = "";
2640         $errorext = "";
2642         # Get the data passed to the subroutine.
2644         my $class = shift;
2645         my ($passedoptions) = @_;
2647         my %page_info;
2648         my @template_data;
2650         my $template_filename;
2651         my $template_name;
2652         my $template_description;
2653         my $template_datemodified;
2654         my $template_layout;
2656         my $template_found = 0;
2658         my $filename    = $passedoptions->{"TemplateFilename"};
2659         my $reduced     = $passedoptions->{"Reduced"};
2661         if ($reduced && $reduced eq 1){
2663                 $template_statement_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2664                 $template_statement_handle->execute();
2666         } else {
2668                 $template_statement_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 );
2669                 $template_statement_handle->execute();
2671         }
2673         while (@template_data = $template_statement_handle->fetchrow_array()){
2675                 # Get certain values from the array.
2677                 $template_filename      = $template_data[0];
2678                 $template_name          = $template_data[1];
2679                 $template_description   = $template_data[2];
2680                 $template_layout        = $template_data[3];
2681                 $template_datemodified  = $template_data[4];
2683                 # Process them into the hash.
2685                 %page_info = (
2686                         "TemplateFilename" => $template_filename,
2687                         "TemplateName" => $template_name,
2688                         "TemplateDescription" => $template_description,
2689                         "TemplateLayout" => $template_layout,
2690                         "TemplateLastModified" => $template_datemodified
2691                 );
2693                 $template_found = 1;
2695         }
2697         if ($template_found eq 0){
2699                 # The template was not found in the template database so
2700                 # write an error value.
2702                 $error = "TemplateDoesNotExist";
2703                 return;
2705         }
2707         return %page_info;
2711 sub addtemplate{
2712 #################################################################################
2713 # addtemplate: Adds a template to the template database.                        #
2714 #                                                                               #
2715 # Usage:                                                                        #
2716 #                                                                               #
2717 # $dbmodule->addtemplate();                                                     #
2718 #                                                                               #
2719 # options       Specifies the following options in any order.                   #
2720 #                                                                               #
2721 # TemplateFilename      Specifies the new template filename.                    #
2722 # TemplateName          Specifies the new template name.                        #
2723 # TemplateDescription   Specifies the new template description.                 #
2724 # TemplateLayout        Specifies the new template layout.                      #
2725 #################################################################################
2727         $error = "";
2728         $errorext = "";
2730         # Get the data passed to the subroutine.
2732         my $class = shift;
2733         my ($passedoptions) = @_;
2735         my @page_exists;
2736         my $notemplatetable;
2737         my $blankfile = 0;
2739         my $template_filename           = $passedoptions->{"TemplateFilename"};
2740         my $template_name               = $passedoptions->{"TemplateName"};
2741         my $template_description        = $passedoptions->{"TemplateDescription"};
2742         my $template_layout             = $passedoptions->{"TemplateLayout"};
2744         # Check if the template database permissions are valid.
2746         my $templatedb_exists = kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2747         my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2749         if ($templatedb_permissions eq 1){
2751                 if ($templatedb_exists eq 0){
2752                         $error = "TemplateDatabaseInvalidPermissionsSet";
2753                         return;
2754                 }
2756         }
2758         # Check if the template already exists before adding.
2760         if ($templatedb_exists eq 0){
2762                 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2764                 if ($blankfile eq 0){
2766                         $template_statement_handle->execute();
2768                         while (@page_exists = $template_statement_handle->fetchrow_array()){
2770                                 $error = "TemplatePageExists";
2771                                 return;
2773                         }
2775                 }
2777         }
2779         # Get the current date.
2780  
2781         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2782  
2783         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2785         # Check if certain values are undefined and if they
2786         # are then set them blank.
2788         if (!$template_name){
2790                 $template_name = "";
2792         }
2794         if (!$template_description){
2796                 $template_description = "";
2798         }
2800         if (!$template_layout){
2802                 $template_layout = "";
2804         }
2806         my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2808         if ($directory_permissions eq 1 && $templatedb_exists){
2810                 # The template database cannot be created because of invalid directory
2811                 # permissions so return an error value.
2813                 $error = "TemplateDatabaseUncreateable";
2814                 return; 
2816         }
2818         # Check to see if a template can be added.
2820         $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2821                                 \'' . $class->convert($template_filename) . '\',
2822                                 \'' . $class->convert($template_name) . '\',
2823                                 \'' . $class->convert($template_description) . '\',
2824                                 \'' . $class->convert($template_layout) . '\',
2825                                 \'' . $class->convert($template_date) . '\'
2826         )') or ( $notemplatetable = 1 );
2828         if (!$notemplatetable){
2830                 $template_statement_handle->execute();
2832         }
2834         # Check to see if there is no template table and attempt to create one.
2836         if ($notemplatetable){
2838                 # Create a template table.
2840                 my $directory_permissions = kiriwrite_filepermissions(".", 1, 1, 0);
2842                 if ($directory_permissions eq 1){
2844                         # The template database cannot be created because of invalid directory
2845                         # permissions so return an error.
2847                         $error = "TemplateDatabaseFileUncreateable";
2848                         return;
2850                 }
2852                 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2853                         filename varchar(256) primary key,
2854                         templatename varchar(512),
2855                         templatedescription varchar(512),
2856                         templatelayout text,
2857                         datemodified datetime
2858                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2859                 $template_statement_handle->execute();
2861                 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2862                                 \'' . $class->convert($template_filename) . '\',
2863                                 \'' . $class->convert($template_name) . '\',
2864                                 \'' . $class->convert($template_description) . '\',
2865                                 \'' . $class->convert($template_layout) . '\',
2866                                 \'' . $class->convert($template_date) . '\'
2867                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2868                 $template_statement_handle->execute();
2870         }
2874 sub deletetemplate{
2875 #################################################################################
2876 # deletetemplate: Deletes a template from the template database.                #
2877 #                                                                               #
2878 # Usage:                                                                        #
2879 #                                                                               #
2880 # $dbmodule->deletetemplate(options);                                           #
2881 #                                                                               #
2882 # options       Specifies the following options in any order.                   #
2883 #                                                                               #
2884 # TemplateFilename      Specifies the template filename to delete.              #
2885 #################################################################################
2887         $error = "";
2888         $errorext = "";
2890         # Get the data passed to the subroutine.
2892         my $class = shift;
2893         my ($passedoptions) = @_;
2895         my @pagedata;
2896         my $template_filename = $passedoptions->{"TemplateFilename"};
2897         my $template_count = 0;
2899         # Check if the template exists.
2901         $template_statement_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 );
2902         $template_statement_handle->execute();
2904         while (@pagedata = $template_statement_handle->fetchrow_array()){
2906                 $template_count++;
2908         }
2910         if ($template_count eq 0){
2912                 # No pages were returned so return an error value.
2914                 $error = "TemplateDoesNotExist";
2915                 return;
2917         }
2919         # Delete the template from the template database.
2921         $template_statement_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2922         $template_statement_handle->execute();
2926 sub edittemplate{
2927 #################################################################################
2928 # editttemplate: Edits a Kiriwrite template.                                    #
2929 #                                                                               #
2930 # Usage:                                                                        #
2931 #                                                                               #
2932 # $dbmodule->edittemplate(options);                                             #
2933 #                                                                               #
2934 # options       Specifies the following options in any order.                   #
2935 #                                                                               #
2936 # TemplateFilename              Specifies the template filename to edit.        #
2937 # NewTemplateFilename           Specifies the new template filename.            #
2938 # NewTemplateName               Specifies the new template name.                #
2939 # NewTemplateDescription        Specifies the new template description.         #
2940 # NewTemplateLayout             Specifies the new template layout.              #
2941 #################################################################################
2943         # Get the values passed.
2945         my $class = shift;
2946         my ($passedoptions) = @_;
2947         my $template_found = 0;
2948         my @template_info;
2950         # Process the values passed.
2952         my $template_filename           = $passedoptions->{"TemplateFilename"};
2953         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2954         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2955         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2956         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2958         # Check if the template database permissions are valid.
2960         my $templatedb_exists = kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2961         my $templatedb_permissions = kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2963         if ($templatedb_permissions eq 1){
2965                 if ($templatedb_exists eq 0){
2966                         $error = "TemplateDatabaseInvalidPermissionsSet";
2967                         return;
2968                 }
2970         }
2972         # Check if the template exists.
2974         $template_statement_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 );
2975         $template_statement_handle->execute();
2977         while (@template_info = $template_statement_handle->fetchrow_array()){
2979                 $template_found = 1;
2981         }
2983         # Check to see if the template was found and set an error value if
2984         # it wasn't.
2986         if ($template_found eq 0){
2988                 $error = "TemplateDoesNotExist";
2989                 return;
2991         }
2993         # Get the date and time.
2995         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2996         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2998         # Update the template information.
3000         $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
3001                 filename = \'' . $class->convert($new_template_filename) . '\',
3002                 templatename = \'' . $class->convert($new_template_name) . '\',
3003                 templatedescription = \'' . $class->convert($new_template_description) . '\',
3004                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
3005                 datemodified = \'' . $class->convert($templatenewdate) . '\'
3006                 WHERE filename = \'' . $class->convert($template_filename) . '\'
3007         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
3008         $template_statement_handle->execute();
3012 #################################################################################
3013 # General subroutines.                                                          #
3014 #################################################################################
3016 sub connect{
3017 #################################################################################
3018 # connect: Connect to the server.                                               #
3019 #                                                                               #
3020 # Usage:                                                                        #
3021 #                                                                               #
3022 # $dbmodule->connect();                                                         #
3023 #################################################################################
3025         # This function is not needed in this database module.
3029 sub disconnect{
3030 #################################################################################
3031 # connect: Disconnect from the server.                                          #
3032 #                                                                               #
3033 # Usage:                                                                        #
3034 #                                                                               #
3035 # $dbmodule->disconnect();                                                      #
3036 #################################################################################
3038         # This function is not needed in this database module.
3040         undef $statement_handle;
3044 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