Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Version 0.3.0
[kiriwrite/.git] / cgi-files / Modules / Database / SQLite.pm
1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm)                #
3 # Database module for mainipulating SQLite databases in the database directory. #
4 #                                                                               #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk>              #
6 #                                                                               #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
8 #                                                                               #
9 # This program is free software; you can redistribute it and/or modify it under #
10 # the terms of the GNU General Public License as published by the Free          #
11 # Software Foundation; as version 2 of the License.                             #
12 #                                                                               #
13 # This program is distributed in the hope that it will be useful, but WITHOUT   #
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
16 #                                                                               #
17 # You should have received a copy of the GNU General Public License along with  #
18 # this program; if not, write to the Free Software Foundation, Inc., 51         #
19 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                           #
20 #################################################################################
22 # Define the package (perl module) name.
24 package Kiriwrite::Database::SQLite;
26 # Enable strict and use warnings.
28 use strict;
29 use warnings;
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION    = "0.2.0";
38 my ($options, %options);
39 my $database_handle;
40 my $statement_handle;
41 my $error = "";
42 my $errorext = "";
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_statement_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_statement_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_statement_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines.                                                          #
59 #################################################################################
61 sub new{
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite                        #
64 #                                                                               #
65 # Usage:                                                                        #
66 #                                                                               #
67 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
68 #################################################################################
69         
70         # Get the perl module name.
72         my $class = shift;
73         my $self = {};
75         return bless($self, $class);
77 }
79 sub loadsettings{
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module                  #
82 #                                                                               #
83 # Usage:                                                                        #
84 #                                                                               #
85 # $dbmodule->loadsettings(Directory, options);                                  #
86 #                                                                               #
87 # options       Specifies the following options (in any order).                 #
88 #                                                                               #
89 # Directory     Specifies the directory to use for getting databases.           #
90 # DateTime      Specifies the date and time format to use.                      #
91 # Server        Specifies the server to use.                                    #
92 # Database      Specifies the database to use.                                  #
93 # Username      Specifies the username to use.                                  #
94 # Password      Specifies the password to use.                                  #
95 # HashType      Specifies the password hash type to use.                        #
96 # Port          Specifies the server port to use.                               #
97 # Protocol      Specifies the protocol to use.                                  #
98 # TablePrefix   Specifies the table prefix to use.                              #
99 #################################################################################
101         # Get the data passed to the subroutine.
103         my $class = shift;
104         my ($passedoptions)     = @_;
106         # Add the directory setting to the list of options (as it's the only
107         # one needed for this database module).
109         %options = (
110                 "Directory"     => $passedoptions->{"Directory"},
111                 "DateTime"      => $passedoptions->{"DateTime"},
112         );
116 sub convert{
117 #################################################################################
118 # convert: Converts data into SQL formatted data.                               #
119 #                                                                               #
120 # Usage:                                                                        #
121 #                                                                               #
122 # $dbmodule->convert(data);                                                     #
123 #                                                                               #
124 # data          Specifies the data to convert.                                  #
125 #################################################################################
127         # Get the data passed to the subroutine.
129         my $class       = shift;
130         my $data        = shift;
132         if (!$data){
133                 $data = "";
134         }
136         $data =~ s/'/''/g;
137         $data =~ s/\b//g;
139         return $data;
143 sub dateconvert{
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date.                          #
146 #                                                                               #
147 # Usage:                                                                        #
148 #                                                                               #
149 # $dbmodule->dateconvert(date);                                                 #
150 #                                                                               #
151 # date          Specifies the date to convert.                                  #
152 #################################################################################
154         # Get the date passed to the subroutine.
156         my $class       = shift;
157         my $data        = shift;
159         # Convert the date given into the proper date.
161         # Create the following varialbes to be used later.
163         my $date;
164         my $time;
165         my $day;
166         my $day_full;
167         my $month;
168         my $month_check;
169         my $month_full;
170         my $year;
171         my $year_short;
172         my $hour;
173         my $hour_full;
174         my $minute;
175         my $minute_full;
176         my $second;
177         my $second_full;
178         my $seek = 0;
179         my $timelength;
180         my $datelength;
181         my $daylength;
182         my $secondlength;
183         my $startchar = 0;
184         my $char;
185         my $length;
186         my $count = 0;
188         # Split the date and time.
190         $length = length($data);
192         if ($length > 0){
194                 do {
196                         # Get the character and check if it is a space.
198                         $char = substr($data, $seek, 1);
200                         if ($char eq ' '){
202                                 # The character is a space, so get the date and time.
204                                 $date           = substr($data, 0, $seek);
205                                 $timelength     = $length - $seek - 1;
206                                 $time           = substr($data, $seek + 1, $timelength);
208                         }
210                         $seek++;
212                 } until ($seek eq $length);
214                 # Get the year, month and date.
216                 $length = length($date);
217                 $seek = 0;
219                 do {
221                         # Get the character and check if it is a dash.
223                         $char = substr($date, $seek, 1);
225                         if ($char eq '-'){
227                                 # The character is a dash, so get the year, month or day.
229                                 $datelength = $seek - $startchar;
231                                 if ($count eq 0){
233                                         # Get the year from the date.
235                                         $year           = substr($date, 0, $datelength) + 1900;
236                                         $startchar      = $seek;
237                                         $count = 1;
239                                         # Get the last two characters to get the short year
240                                         # version.
242                                         $year_short     = substr($year, 2, 2);
244                                 } elsif ($count eq 1){
246                                         # Get the month and day from the date.
248                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
250                                         # Check if the month is less then 10, if it is
251                                         # add a zero to the value.
253                                         if ($month < 10){
255                                                 $month_full = '0' . $month;
257                                         } else {
259                                                 $month_full = $month;
261                                         }
263                                         $startchar      = $seek;
264                                         $count = 2;
266                                         $daylength      = $length - $seek + 1;
267                                         $day            = substr($date, $startchar + 1, $daylength);
269                                         # Check if the day is less than 10, if it is
270                                         # add a zero to the value.
272                                         if ($day < 10){
274                                                 $day_full       = '0' . $day;
276                                         } else {
278                                                 $day_full       = $day;
280                                         }
282                                 }
284                         }
286                         $seek++;
288                 } until ($seek eq $length);
290                 # Get the length of the time value and reset certain
291                 # values to 0.
293                 $length = length($time);
294                 $seek = 0;
295                 $count = 0;
296                 $startchar = 0;
298                 do {
300                         # Get the character and check if it is a colon.
302                         $char = substr($time, $seek, 1);
304                         if ($char eq ':'){
306                                 # The character is a colon, so get the hour, minute and day.
308                                 $timelength = $seek - $startchar;
310                                 if ($count eq 0){
312                                         # Get the hour from the time.
314                                         $hour = substr($time, 0, $timelength);
315                                         $count = 1;
316                                         $startchar = $seek;
318                                         # If the hour is less than ten then add a
319                                         # zero.
321                                         if ($hour < 10){
323                                                 $hour_full = '0' . $hour;
325                                         } else {
327                                                 $hour_full = $hour;
329                                         }
331                                 } elsif ($count eq 1){
333                                         # Get the minute and second from the time.
335                                         $minute = substr($time, $startchar + 1, $timelength - 1);
336                                         $count = 2;
337                                                 
338                                         # If the minute is less than ten then add a
339                                         # zero.
341                                         if ($minute < 10){
343                                                 $minute_full = '0' . $minute;
345                                         } else {
347                                                 $minute_full = $minute;
349                                         }
351                                         $startchar = $seek;
353                                         $secondlength = $length - $seek + 1;
354                                         $second = substr($time, $startchar + 1, $secondlength);
355                                         
356                                         # If the second is less than ten then add a
357                                         # zero.
359                                         if ($second < 10){
361                                                 $second_full = '0' . $second;
363                                         } else {
365                                                 $second_full = $second;
367                                         }
369                                 }
371                         }
373                         $seek++;
375                 } until ($seek eq $length);
377                 # Get the setting for displaying the date and time.
379                 $data = $options{"DateTime"};
381                 # Process the setting for displaying the date and time
382                 # using regular expressions
384                 $data =~ s/DD/$day_full/g;
385                 $data =~ s/D/$day/g;
386                 $data =~ s/MM/$month_full/g;
387                 $data =~ s/M/$month/g;
388                 $data =~ s/YY/$year/g;
389                 $data =~ s/Y/$year_short/g;
391                 $data =~ s/hh/$hour_full/g;
392                 $data =~ s/h/$hour/g;
393                 $data =~ s/mm/$minute_full/g;
394                 $data =~ s/m/$minute/g;
395                 $data =~ s/ss/$second_full/g;
396                 $data =~ s/s/$second/g;
398         }
400         return $data;
404 sub geterror{
405 #################################################################################
406 # geterror: Gets the error message (or extended error message).                 #
407 #                                                                               #
408 # Usage:                                                                        #
409 #                                                                               #
410 # $dbmodule->geterror(extended);                                                #
411 #                                                                               #
412 # Extended      Specifies if the extended error should be retrieved.            #
413 #################################################################################
415         # Get the data passed to the subroutine.
417         my $class       = shift;
418         my $extended    = shift;
420         if (!$extended){
421                 $extended = 0;
422         }
424         if (!$errorext){
425                 $errorext = "";
426         }
428         if (!$error){
429                 $error = "";
430         }
432         # Check to see if extended information should be returned.
434         if ($extended eq 1){
436                 # Extended information should be returned.
438                 return $errorext;
440         } else {
442                 # Basic information should be returned.
444                 return $error;
446         }
450 sub dbpermissions{
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid.           #
453 #                                                                               #
454 # Usage:                                                                        #
455 #                                                                               #
456 # $database->dbpermissions(dbname, read, write);                                #
457 #                                                                               #
458 # dbname        Specifies the database name to check.                           #
459 # read          Check to see if the database can be read.                       #
460 # write         Check to see if the database can be written.                    #
461 #################################################################################
463         # Get the database name, read setting and write setting.
465         my ($class, $dbname, $readper, $writeper)       = @_;
467         # Check if the database can be read.
469         if ($readper){
471                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473                         # The database can be read.
475                 } else {
477                         # The database cannot be read, so return a value
478                         # of 1.
480                         return 1;
482                 }
484         }
486         # Check if the database can be written.
488         if ($writeper){
490                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492                         # The database can be read.
494                 } else {
496                         # The database cannot be read, so return a value
497                         # of 1.
499                         return 1;
501                 }
503         }
505         # No errors have occured while checking so return a value
506         # of 0.
508         return 0;
512 sub dbexists{
513 #################################################################################
514 # dbexists: Check if the database exists.                                       #
515 #                                                                               #
516 # Usage:                                                                        #
517 #                                                                               #
518 # $dbmodule->dbexists(dbname);                                                  #
519 #                                                                               #
520 # dbname        Specifies the database name to check.                           #
521 #################################################################################
523         # Get the value that was passed to the subroutine.
525         my $class       = shift;
526         my ($filename)  = @_;
528         # Check if the filename exists, if it does, return a value of 1, else
529         # return a value of 0, meaning that the file was not found.
531         if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
533                 # Specified file does exist so return a value of 0.
535                 return 0;
537         } else {
539                 # Specified file does not exist so return a value of 1.
541                 return 1;
543         }
547 #################################################################################
548 # Database Subroutines.                                                         #
549 #################################################################################
551 sub getdblist{
552 #################################################################################
553 # getdblist: Gets the list of available databases.                              #
554 #                                                                               #
555 # Usage:                                                                        #
556 #                                                                               #
557 # $dbmodule->getdblist();                                                       #
558 #################################################################################
560         # Get the list of databases.
562         my @data_directory;
563         my @data_directory_final;
564         my $database;
565         my $database_filename_length;
566         my $database_filename_friendly;
568         # Check if the database directory has valid permission settings.
570         if (-e $options{"Directory"}){
572                 # The database directory does exist. So check if
573                 # the permission settings are valid.
575                 if (-r $options{"Directory"}){
577                         # The permission settings for reading the directory
578                         # are valid.
580                 } else {
582                         # The permission settings for reading the directory
583                         # are invalid so return an error value.
585                         $error = "DataDirInvalidPermissions";
586                         return;
588                 }
590         } else {
592                 # The database directory does not exist, so return an
593                 # error value.
595                 $error = "DataDirMissing";
596                 return;
598         }
600         opendir(DATADIR, $options{"Directory"});
601         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
602         closedir(DATADIR);
604         # Process the list of databases.
606         foreach $database (@data_directory){
608                 $database =~ s/.db.sqlite$//og;
609                 $database_filename_friendly = $database;
611                 #$database_filename_length = length($database);
612                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
613                 push(@data_directory_final, $database_filename_friendly);
615         }
617         # Return the list of databases.
619         return @data_directory_final;
623 sub getdatabaseinfo{
624 #################################################################################
625 # getdatabaseinfo: Get information about the database.                          #
626 #                                                                               #
627 # Usage:                                                                        #
628 #                                                                               #
629 # $dbmodule->getdatabaseinfo();                                                 #
630 #################################################################################
632         # Get the database information.
634         my $class = shift;
635         my ($databaseinfo, %databaseinfo);
636         my ($sqldata, @sqldata);
638         $error = "";
639         $errorext = "";
641         $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 (
642                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
643         );
644         $statement_handle->execute();
646         @sqldata = $statement_handle->fetchrow_array();
648         # Process the database information into a hash.
650         %databaseinfo = (
651                 "DatabaseName"  => $sqldata[0],
652                 "Description"   => $sqldata[1],
653                 "Notes"         => $sqldata[2],
654                 "Categories"    => $sqldata[3],
655                 "Major"         => $sqldata[4],
656                 "Minor"         => $sqldata[5],
657                 "Revision"      => $sqldata[6]
658         );
660         $statement_handle->finish();
661         undef $statement_handle;
663         return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be  #
670 # moved or copied to.                                                           #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getseconddatabaseinfo();                                           #
675 #################################################################################
677         # Get the database information.
679         my $class = shift;
680         my ($databaseinfo, %databaseinfo);
681         my ($sqldata, @sqldata);
683         $error = "";
684         $errorext = "";
686         $second_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 (
687                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
688         );
689         $second_statement_handle->execute();
691         @sqldata = $second_statement_handle->fetchrow_array();
693         # Process the database information into a hash.
695         %databaseinfo = (
696                 "DatabaseName"  => $sqldata[0],
697                 "Description"   => $sqldata[1],
698                 "Notes"         => $sqldata[2],
699                 "Categories"    => $sqldata[3],
700                 "Major"         => $sqldata[4],
701                 "Minor"         => $sqldata[5],
702                 "Revision"      => $sqldata[6]
703         );
705         $second_statement_handle->finish();
706         undef $second_statement_handle;
708         return %databaseinfo;
712 sub adddatabase{
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database.                                       #
715 #                                                                               #
716 # Usage:                                                                        #
717 #                                                                               #
718 # $dbmodule->adddatabase(options);                                              #
719 #                                                                               #
720 # options       Specifies the following options in any order.                   #
721 #                                                                               #
722 # DatabaseFilename      Specifies the database file/shortname to use.           #
723 # DatabaseName          Specifies the database name to use.                     #
724 # DatabaseDescription   Specifies the database description to use.              #
725 # DatabaseNotes         Specifies the database notes to use.                    #
726 # DatabaseCategories    Specifies the database categories to use.               #
727 # VersionMajor          Specifies the major version.                            #
728 # VersionMinor          Specifies the minor version.                            #
729 # VersionRevision       Specifies the revision version.                         #
730 #################################################################################
732         # Get the database that was passed to the subroutine.
734         $error  = "";
735         $errorext = "";
737         my $class       = shift;
738         my ($passedoptions) = @_;
740         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
741         my $dbname              = $passedoptions->{"DatabaseName"};
742         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
743         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
744         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
745         my $dbmajorver          = $passedoptions->{"VersionMajor"};
746         my $dbminorver          = $passedoptions->{"VersionMinor"};
747         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
749         # Check if the database with the filename given already exists.
751         my $database_exists     = $class->dbexists($dbfilename);
753         if ($database_exists eq 0){
755                 # The database filename exists so set the error value.
757                 $error = "DatabaseExists";
758                 return;
760         }
762         # Create the database structure.
764         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765         $database_handle->{unicode} = 1;
766         $statement_handle               = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767                         name varchar(256) primary key, 
768                         description varchar(512), 
769                         notes text, 
770                         categories varchar(512), 
771                         kiriwrite_version_major int(4), 
772                         kiriwrite_version_minor int(4), 
773                         kiriwrite_version_revision int(4)
774         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775         $statement_handle->execute();
777         $statement_handle       = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778                         filename varchar(256) primary key, 
779                         pagename varchar(512), 
780                         pagedescription varchar(512), 
781                         pagesection varchar(256),
782                         pagetemplate varchar(64),
783                         pagedata text,
784                         pagesettings int(1),
785                         lastmodified datetime
786         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787         $statement_handle->execute();
789         # Convert the values into SQL query formatted values and add an entry
790         # to the kiriwrite_database_info table.
792         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793                 \'' . $class->convert($dbname) . '\',
794                 \'' . $class->convert($dbdescription) . '\',
795                 \'' . $class->convert($dbnotes) . '\',
796                 \'' . $class->convert($dbcategories) . '\',
797                 \'' . $class->convert($dbmajorver) . '\',
798                 \'' . $class->convert($dbminorver) . '\',
799                 \'' . $class->convert($dbrevisionver) . '\'
800         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801         $statement_handle->execute();
805 sub editdatabase{
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database.                                     #
808 #                                                                               #
809 # Usage:                                                                        #
810 #                                                                               #
811 # $dbmodule->editdatabase(options);                                             #
812 #                                                                               #
813 # options               Specifies the following options in any order.           #
814 #                                                                               #
815 # NewDatabaseFilename   Specifies the new database filename to use.             #
816 # DatabaseName          Specifies the new database name.                        #
817 # DatabaseDescription   Specifies the new database description.                 #
818 # DatabaseNotes         Specifies the new database notes.                       #
819 # DatabaseCategories    Specifies the new database categories.                  #
820 #################################################################################
822         $error          = "";
823         $errorext       = "";
825         my $class       = shift;
826         my ($passedoptions) = @_;
828         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
829         my $dbname              = $passedoptions->{"DatabaseName"};
830         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
831         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
832         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
834         # Check if a new database filename has been specified and if a
835         # new database filename has been specified then change the
836         # database filename.
838         if ($database_filename ne $dbnewfilename){
840                 # A new database filename has been given so check if the output
841                 # directory has write access.
843                 if (-r $options{"Directory"}){
844                         
845                         # The directory is readable.
847                 } else {
849                         # The directory is not readable so set the error value.
851                         $error = "DataDirInvalidPermissions";
853                         return;
855                 }
857                 if (-w $options{"Directory"}){
859                         # The directory is writeable.
861                 } else {
863                         # The directory is not writeable so set the error value.
865                         $error = "DataDirInvalidPermissions";
867                         return;
869                 }
871                 # Check if a database filename already exists before using the
872                 # new filename.
874                 my $database_newexists          = $class->dbexists($dbnewfilename);
876                 if ($database_newexists eq 0){
878                         # The database filename exists so set the error value.
880                         $error = "DatabaseExists";
881                         return;
883                 }
885                 # Check if the database can be renamed (has write access).
887                 my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
889                 if ($database_permissions eq 1){
891                         # The database filename exists so set the error value.
893                         $error = "InvalidPermissionsSet";
894                         return;
896                 }
898                 # "Disconnect" from the database.
900                 $database_handle->disconnect();
902                 # Rename the database.
904                 ($database_filename)    = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
905                 ($dbnewfilename)        = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
907                 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
909                 # Reload the database from the new filename.
911                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
912                 $database_handle->{unicode} = 1;
913                 $database_filename = $dbnewfilename;
915         }
917         # Check if the database can be altered with the new data.
919         my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
921         if ($database_permissions eq 1){
923                 # The database filename exists so set the error value.
925                 $error = "InvalidPermissionsSet";
926                 return;
928         }
930         # Get the current database information.
932         $statement_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933         $statement_handle->execute();
935         my @database_oldinfo    = $statement_handle->fetchrow_array();
937         my $dboldname           = $database_oldinfo[0];
939         # Update the database information.
941         $statement_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942         description = \'' . $class->convert($dbdescription) . '\',
943         notes = \'' . $class->convert($dbnotes) . '\',
944         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945         $statement_handle->execute();
947         undef $statement_handle;
948         return;
952 sub deletedatabase{
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database.                                 #
955 #                                                                               #
956 # Usage:                                                                        #
957 #                                                                               #
958 # $dbmodule->deletedatabase(options);                                           #
959 #                                                                               #
960 # options       Specifies the following options in any order.                   #
961 #                                                                               #
962 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
963 #################################################################################
965         $error          = "";
966         $errorext       = "";
968         # Get the database filename.
970         my $class               = shift;
971         my ($passedoptions)     = shift;
973         my $databasename        = $passedoptions->{"DatabaseName"};
975         # Check if the database exists.
977         my $database_exists             = $class->dbexists($databasename);
979         if ($database_exists eq 1){
981                 # The database does not exist so set the error value.
983                 $error = "DoesNotExist";
984                 return;
986         }
988         # Check if the database permissions are valid.
990         my $database_permissions        = $class->dbpermissions($databasename);
992         if ($database_permissions eq 1){
994                 # The database permissions are invalid so set the error
995                 # value.
997                 $error = "InvalidPermissionsSet";
998                 return;
1000         }
1002         # Delete the database.
1004         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1010 sub selectdb{
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database.                                     #
1013 #                                                                               #
1014 # Usage:                                                                        #
1015 #                                                                               #
1016 # $dbmodule->connect(options);                                                  #
1017 #                                                                               #
1018 # options       Specifies the following options in any order.                   #
1019 #                                                                               #
1020 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1021 #################################################################################
1023         # Get the database name.
1025         $error = "";
1026         $errorext = "";
1028         my $class = shift;
1029         my ($passedoptions) = @_;
1030         my (%database, $database);
1032         my $dbname = $passedoptions->{"DatabaseName"};
1034         # Check if the database exists.
1036         my $database_exists = $class->dbexists($dbname);
1038         if ($database_exists eq 1){
1040                 # The database does not exist so return an error value
1041                 # saying that the database does not exist.
1043                 $error = "DoesNotExist";
1045                 return;
1047         }
1049         # Check if the database has valid permissions set.
1051         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1053         if ($database_permissions eq 1){
1055                 # The database has invalid permissions set so return
1056                 # an error value saying that the database has invalid
1057                 # permissions set.
1059                 $error = "InvalidPermissionsSet";
1060                 
1061                 return;
1063         }
1065         # Connect to the database.
1067         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1068         $database_handle->{unicode} = 1;
1069         $database_filename = $dbname;
1073 sub selectseconddb{
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1076 # pages to.                                                                     #
1077 #                                                                               #
1078 # Usage:                                                                        #
1079 #                                                                               #
1080 # $dbmodule->selectseconddb(options);                                           #
1081 #                                                                               #
1082 # options       Specifies the following options in any order.                   #
1083 #                                                                               #
1084 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1085 #################################################################################
1087         # Get the database name.
1089         $error = "";
1090         $errorext = "";
1092         my $class = shift;
1093         my ($passedoptions) = @_;
1094         my (%database, $database);
1096         my $dbname = $passedoptions->{"DatabaseName"};
1098         # Check if the database exists.
1100         my $database_exists = $class->dbexists($dbname);
1102         if ($database_exists eq 1){
1104                 # The database does not exist so return an error value
1105                 # saying that the database does not exist.
1107                 $error = "DoesNotExist";
1109                 return;
1111         }
1113         # Check if the database has valid permissions set.
1115         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1117         if ($database_permissions eq 1){
1119                 # The database has invalid permissions set so return
1120                 # an error value saying that the database has invalid
1121                 # permissions set.
1123                 $error = "InvalidPermissionsSet";
1124                 
1125                 return;
1127         }
1129         # Connect to the database.
1131         $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1132         $second_database_handle->{unicode} = 1;
1133         $second_database_filename = $dbname;    
1137 #################################################################################
1138 # Page subroutines.                                                             #
1139 #################################################################################
1141 sub getpagelist{
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database.                        #
1144 #                                                                               #
1145 # Usage:                                                                        #
1146 #                                                                               #
1147 # $dbmodule->getpagelist(options);                                              #
1148 #                                                                               #
1149 # options       Specifies the following options in any order.                   #
1150 #                                                                               #
1151 # StartFrom     Start from the specified page in the database.                  #
1152 # Limit         Get the amount of pages given.                                  #
1153 #################################################################################
1155         $error = "";
1156         $errorext = "";
1158         my $class               = shift;
1159         my ($passedoptions)     = shift;
1161         my $start_from  = $passedoptions->{"StartFrom"};
1162         my $limit       = $passedoptions->{"Limit"};
1164         if (defined($start_from)){
1166                 if (!$limit){
1167                         
1168                         $limit = 0;
1170                 }
1172                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages LIMIT ' . $start_from . ',' . $limit) or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1173                 $statement_handle->execute();
1175         } else {
1177                 $statement_handle       = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1178                 $statement_handle->execute();
1180         }
1182         my @database_pagefilenames;
1183         my @database_pagefilenames_final;
1185         # Process the collected pages.
1187         while (@database_pagefilenames = $statement_handle->fetchrow_array){
1189                 # Add each page to the list of pages in the database.
1191                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1193         }
1195         undef $statement_handle;
1196         return @database_pagefilenames_final;
1200 sub getpagecount{
1201 #################################################################################
1202 # getpagecount: Get the count of pages that are in the database.                #
1203 #                                                                               #
1204 # Usage:                                                                        #
1205 #                                                                               #
1206 # $dbmodule->getpagecount();                                                    #
1207 #################################################################################
1209         $error = "";
1210         $errorext = "";
1212         my $class       = shift;
1214         $statement_handle       = $database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return);
1215         $statement_handle->execute();
1217         my $count = $statement_handle->fetchrow_array();
1219         return $count;
1223 sub getpageinfo{
1224 #################################################################################
1225 # getpageinfo: Gets the page information from the filename passed.              #
1226 #                                                                               #
1227 # Usage:                                                                        #
1228 #                                                                               #
1229 # $dbmodule->getpageinfo(options);                                              #
1230 #                                                                               #
1231 # options       Specifies the following options in any order.                   #
1232 #                                                                               #
1233 # PageFilename  Specifies the page filename to get the page information from.   #
1234 # Reduced       Specifies if the reduced version of the page information should #
1235 #               be retrieved.                                                   #
1236 #################################################################################
1238         $error = "";
1239         $errorext = "";
1241         my $class               = shift;
1242         my ($passedoptions)     = shift;
1243         my (%database_page, $database_page);
1244         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1246         my @data_page;
1247         my $page_found = 0;
1249         # Get the page from the database.
1251         my $page_filename       = $passedoptions->{"PageFilename"};
1252         my $page_reduced        = $passedoptions->{"Reduced"};
1254         if (!$page_reduced){
1256                 $page_reduced = 0;
1258         }
1260         if ($page_reduced eq 1){
1262                 $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 );
1264                 $statement_handle->execute();
1266                 # Check if the page exists in the database.
1268                 while (@data_page = $statement_handle->fetchrow_array()){
1269         
1270                         # Get the values from the array.
1271         
1272                         $pagefilename           = $data_page[0];
1273                         $pagename               = $data_page[1];
1274                         $pagedescription        = $data_page[2];
1275                         $pagelastmodified       = $data_page[3];
1276         
1277                         # Put the values into the page hash.
1278         
1279                         %database_page = (
1280                                 "PageFilename"          => $pagefilename,
1281                                 "PageName"              => $pagename,
1282                                 "PageDescription"       => $pagedescription,
1283                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1284                         );
1285         
1286                         $page_found = 1;
1287         
1288                 }
1291         } else {
1292                 
1293                 $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 );
1295                 $statement_handle->execute();
1297                 # Check if the page exists in the database.
1299                 while (@data_page = $statement_handle->fetchrow_array()){
1300         
1301                         # Get the values from the array.
1302         
1303                         $pagefilename           = $data_page[0];
1304                         $pagename               = $data_page[1];
1305                         $pagedescription        = $data_page[2];
1306                         $pagesection            = $data_page[3];
1307                         $pagetemplate           = $data_page[4];
1308                         $pagedata               = $data_page[5];
1309                         $pagesettings           = $data_page[6];
1310                         $pagelastmodified       = $data_page[7];
1311         
1312                         # Put the values into the page hash.
1313         
1314                         %database_page = (
1315                                 "PageFilename"          => $pagefilename,
1316                                 "PageName"              => $pagename,
1317                                 "PageDescription"       => $pagedescription,
1318                                 "PageSection"           => $pagesection,
1319                                 "PageTemplate"          => $pagetemplate,
1320                                 "PageContent"           => $pagedata,
1321                                 "PageSettings"          => $pagesettings,
1322                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1323                         );
1324         
1325                         $page_found = 1;
1326         
1327                 }
1329         }
1330         
1331         # Check if the page did exist.
1333         if (!$page_found){
1335                 $error = "PageDoesNotExist";
1336                 return;
1338         }
1340         undef $statement_handle;
1341         return %database_page;
1345 sub addpage{
1346 #################################################################################
1347 # addpage: Add a page to the selected database.                                 #
1348 #                                                                               #
1349 # Usage:                                                                        #
1350 #                                                                               #
1351 # $dbmodule->addpage(options);                                                  #
1352 #                                                                               #
1353 # options       Specifies the following options in any order.                   #
1354 #                                                                               #
1355 # PageFilename          Specifies the page filename to use.                     #
1356 # PageName              Specifies the page name to use.                         #
1357 # PageDescription       Specifies the page description to use.                  #
1358 # PageSection           Specifies the page section to use.                      #
1359 # PageTemplate          Specifies the page template to use.                     #
1360 # PageContent           Specifies the page content to use.                      #
1361 # PageSettings          Specifies the page settings to use.                     #
1362 #################################################################################
1364         # Get the data that was passed to the subroutine.
1366         $error = "";
1367         $errorext = "";
1369         my $class               = shift;
1370         my ($passedoptions)     = shift;
1372         my @database_page;
1373         my $page_count = 0;
1375         # Get the values passed to the hash.
1377         my $page_filename       = $passedoptions->{"PageFilename"};
1378         my $page_name           = $passedoptions->{"PageName"};
1379         my $page_description    = $passedoptions->{"PageDescription"};
1380         my $page_section        = $passedoptions->{"PageSection"};
1381         my $page_template       = $passedoptions->{"PageTemplate"};
1382         my $page_content        = $passedoptions->{"PageContent"};
1383         my $page_settings       = $passedoptions->{"PageSettings"};
1385         # Check to see if the filename given already exists
1386         # in the page database.
1388         $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 );
1389         $statement_handle->execute();
1391         # Check if a page with the filename given really does
1392         # exist.
1394         while (@database_page = $statement_handle->fetchrow_array()){
1396                 # A page does exist so increment the count to 1.
1398                 $page_count++;
1399                 
1400         }
1402         if ($page_count ne 0){
1404                 # The page does exist so set the error value.
1406                 $error = "PageExists";
1407                 return;
1409         }
1411         # Check if certain values are undefined.
1413         if (!$page_name){
1415                 $page_name = "";
1417         }
1419         if (!$page_description){
1421                 $page_description = "";
1423         }
1425         if (!$page_section){
1427                 $page_section = "";
1429         }
1431         if (!$page_content){
1433                 $page_content = "";
1435         }
1437         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1438         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1440         # Add the page to the selected database.
1442         $statement_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1443                 \'' . $class->convert($page_filename) . '\',
1444                 \'' . $class->convert($page_name) . '\',
1445                 \'' . $class->convert($page_description) . '\',
1446                 \'' . $class->convert($page_section) . '\',
1447                 \'' . $class->convert($page_template) . '\',
1448                 \'' . $class->convert($page_content) . '\',
1449                 \'' . $class->convert($page_settings) . '\',
1450                 \'' . $class->convert($page_date) . '\'
1451         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1452         $statement_handle->execute();
1454         undef $statement_handle;
1458 sub deletepage{
1459 #################################################################################
1460 # deletepage: Delete a page from the selected database.                         #
1461 #                                                                               #
1462 # Usage:                                                                        #
1463 #                                                                               #
1464 # $dbmodule->deletepage(options)                                                #
1465 #                                                                               #
1466 # options       Specifies the following options in any order.                   #
1467 #                                                                               #
1468 # PageFilename  Specifies the page filename to delete.                          #
1469 #################################################################################
1471         $error = "";
1472         $errorext = "";
1474         # Get the data that was passed to the subroutine.
1476         my $class = shift;
1477         my ($passedoptions) = @_;
1478         my @page_info;
1479         my $page_found = 0;
1481         # Get the page filename.
1483         my $page_filename = $passedoptions->{"PageFilename"};
1485         # Check if the page exists before deleting it.
1487         $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 );
1488         $statement_handle->execute();
1490         while (@page_info = $statement_handle->fetchrow_array()){
1492                 $page_found = 1;
1494         }
1496         # Check if the page really does exist.
1498         if (!$page_found){
1500                 $error = "PageDoesNotExist";
1501                 return;
1503         }
1505         # Delete the page.
1507         $statement_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1508         $statement_handle->execute();
1512 sub editpage{
1513 #################################################################################
1514 # editpage: Edit a page from the selected database.                             #
1515 #                                                                               #
1516 # Usage:                                                                        #
1517 #                                                                               #
1518 # $dbmodule->editpage(options);                                                 #
1519 #                                                                               #
1520 # options       Specifies the following options in any order.                   #
1521 #                                                                               #
1522 # PageFilename          Specifies the filename to edit.                         #
1523 # PageNewFilename       Specifies the new filename to use.                      #
1524 # PageNewName           Specifies the new page name to use.                     #
1525 # PageNewDescription    Specifies the new page description to use.              #
1526 # PageNewSection        Specifies the new page section to use.                  #
1527 # PageNewTemplate       Specifies the new page template to use.                 #
1528 # PageNewContent        Specifies the new page content to use.                  #
1529 # PageNewSettings       Specifies the new page settings to use.                 #
1530 #################################################################################
1532         $error = "";
1533         $errorext = "";
1535         # Get the values passed to the subroutine.
1537         my $class = shift;
1538         my ($passedoptions) = @_;
1539         my $page_found = 0;
1540         my @page_info;
1542         # Get the data that was passed to the subroutine.
1544         my $page_filename       = $passedoptions->{"PageFilename"};
1545         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1546         my $page_newname        = $passedoptions->{"PageNewName"};
1547         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1548         my $page_newsection     = $passedoptions->{"PageNewSection"};
1549         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1550         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1551         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1553         # Check if the page with the filename given exists.
1555         $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 );
1556         $statement_handle->execute();
1558         # Check if the page really does exist.
1560         while (@page_info = $statement_handle->fetchrow_array()){
1562                 # The page information is found.
1564                 $page_found = 1;
1566         }
1568         # Check if the page really does exist.
1570         if (!$page_found){
1572                 $error = "PageDoesNotExist";
1573                 return;
1575         }
1577         # Check if there is a page that already exists with the new
1578         # filename.
1580         $page_found = 0;
1582         if ($page_filename ne $page_newfilename){
1584                 $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 );
1585                 $statement_handle->execute();
1587                 # Check if a page really is using the new filename.
1589                 while (@page_info = $statement_handle->fetchrow_array()){
1591                         # The page information is found.
1593                         $page_found = 1;
1595                 }
1597                 if ($page_found eq 1){
1599                         $error = "PageExists";
1600                         return;
1602                 }
1604         }
1606         # Get the current date.
1608         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1609         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1611         # Edit the selected page.
1613         $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 );
1614         $statement_handle->execute();
1618 sub movepage{
1619 #################################################################################
1620 # movepage: Moves a page from the old database to the new database.             #
1621 #                                                                               #
1622 # Usage:                                                                        #
1623 #                                                                               #
1624 # $dbmodule->movepage(options);                                                 #
1625 #                                                                               #
1626 # options       Specifies the following options in any order.                   #
1627 #                                                                               #
1628 # PageFilename  Specifies the page with the filename to move.                   #
1629 #################################################################################
1631         $error = "";
1632         $errorext = "";
1634         # Get the values passed to the subroutine.
1636         my $class = shift;
1637         my ($passedoptions) = @_;
1639         my (%database_page, $database_page);
1640         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1641         my @page_info;
1642         my $page_found = 0;
1644         # Get the data that was passed to the subroutine.
1646         my $page_filename = $passedoptions->{"PageFilename"};
1648         # Check if the page with the filename given exists.
1650         $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 );
1651         $statement_handle->execute();
1653         # Check if the page really does exist.
1655         while (@page_info = $statement_handle->fetchrow_array()){
1657                 # Get the values from the array.
1659                 $pagefilename           = $page_info[0];
1660                 $pagename               = $page_info[1];
1661                 $pagedescription        = $page_info[2];
1662                 $pagesection            = $page_info[3];
1663                 $pagetemplate           = $page_info[4];
1664                 $pagedata               = $page_info[5];
1665                 $pagesettings           = $page_info[6];
1666                 $pagelastmodified       = $page_info[7];
1668                 # Put the values into the page hash.
1670                 %database_page = (
1671                         "PageFilename"          => $pagefilename,
1672                         "PageName"              => $pagename,
1673                         "PageDescription"       => $pagedescription,
1674                         "PageSection"           => $pagesection,
1675                         "PageTemplate"          => $pagetemplate,
1676                         "PageContent"           => $pagedata,
1677                         "PageSettings"          => $pagesettings,
1678                         "PageLastModified"      => $pagelastmodified,
1679                 );
1681                 # The page information is found.
1683                 $page_found = 1;
1685         }
1687         # Check if the page really does exist.
1689         if (!$page_found){
1691                 $error = "PageDoesNotExist";
1692                 return;
1694         }
1696         # Check if the page with the filename given already exists in
1697         # the database the page is being moved to.
1699         $page_found = 0;
1700         @page_info = ();
1702         $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 );
1703         $second_statement_handle->execute();
1705         while (@page_info = $second_statement_handle->fetchrow_array()){
1707                 $page_found = 1;
1709         }
1710         
1711         # Check if the page really does exist.
1713         if ($page_found){
1715                 $error = "PageAlreadyExists";
1716                 return;
1718         }
1720         # Add the page to the new database.
1722         $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1723                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1724                 \'' . $class->convert($database_page{"PageName"}) . '\',
1725                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1726                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1727                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1728                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1729                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1730                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1731         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1732         $second_statement_handle->execute();
1734         # Delete the page from the old database.
1736         $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 );
1737         $statement_handle->execute();
1741 sub copypage{
1742 #################################################################################
1743 # copypage: Copies a page from the old database to the new database.            #
1744 #                                                                               #
1745 # Usage:                                                                        #
1746 #                                                                               #
1747 # $dbmodule->copypage(options);                                                 #
1748 #                                                                               #
1749 # options       Specifies the following options in any order.                   #
1750 #                                                                               #
1751 # PageFilename  Specifies the page with the filename to copy.                   #
1752 #################################################################################
1754         $error = "";
1755         $errorext = "";
1757         # Get the values passed to the subroutine.
1759         my $class = shift;
1760         my ($passedoptions) = @_;
1762         my (%database_page, $database_page);
1763         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1764         my @page_info;
1765         my $page_found = 0;
1767         # Get the data that was passed to the subroutine.
1769         my $page_filename = $passedoptions->{"PageFilename"};
1771         # Check if the page with the filename given exists.
1773         $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 );
1774         $statement_handle->execute();
1776         # Check if the page really does exist.
1778         while (@page_info = $statement_handle->fetchrow_array()){
1780                 # Get the values from the array.
1782                 $pagefilename           = $page_info[0];
1783                 $pagename               = $page_info[1];
1784                 $pagedescription        = $page_info[2];
1785                 $pagesection            = $page_info[3];
1786                 $pagetemplate           = $page_info[4];
1787                 $pagedata               = $page_info[5];
1788                 $pagesettings           = $page_info[6];
1789                 $pagelastmodified       = $page_info[7];
1791                 # Put the values into the page hash.
1793                 %database_page = (
1794                         "PageFilename"          => $pagefilename,
1795                         "PageName"              => $pagename,
1796                         "PageDescription"       => $pagedescription,
1797                         "PageSection"           => $pagesection,
1798                         "PageTemplate"          => $pagetemplate,
1799                         "PageContent"           => $pagedata,
1800                         "PageSettings"          => $pagesettings,
1801                         "PageLastModified"      => $pagelastmodified,
1802                 );
1804                 # The page information is found.
1806                 $page_found = 1;
1808         }
1810         # Check if the page really does exist.
1812         if (!$page_found){
1814                 $error = "PageDoesNotExist";
1815                 return;
1817         }
1819         # Check if the page with the filename given already exists in
1820         # the database the page is being moved to.
1822         $page_found = 0;
1823         @page_info = ();
1825         $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 );
1826         $second_statement_handle->execute();
1828         while (@page_info = $second_statement_handle->fetchrow_array()){
1830                 $page_found = 1;
1832         }
1833         
1834         # Check if the page really does exist.
1836         if ($page_found){
1838                 $error = "PageAlreadyExists";
1839                 return;
1841         }
1843         # Add the page to the new database.
1845         $second_statement_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1846                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1847                 \'' . $class->convert($database_page{"PageName"}) . '\',
1848                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1849                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1850                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1851                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1852                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1853                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1854         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1855         $second_statement_handle->execute();
1859 #################################################################################
1860 # Filter subroutines.                                                           #
1861 #################################################################################
1863 sub connectfilter{
1864 #################################################################################
1865 # connectfilter: Connect to the filter database.                                #
1866 #                                                                               #
1867 # Usage:                                                                        #
1868 #                                                                               #
1869 # $dbmodule->connectfilter(missingignore);                                      #
1870 #                                                                               #
1871 # missingignore Ignore error about database being missing.                      #
1872 #################################################################################
1874         $error = "";
1875         $errorext = "";
1877         my $class = shift;
1878         my $ignoremissing = shift;
1880         # Check if the template database exists.
1882         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1883         
1884         if ($filterdatabase_exists eq 1){
1886                 $filterdb_exists = 0;
1888                 if (!$ignoremissing){
1890                         $error = "FilterDatabaseDoesNotExist";
1891                         return;
1893                 }
1895         }
1897         # Check if the permission settings for the template database are valid.
1899         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1901         if ($filterdb_permissions eq 1){
1903                 # The template database has invalid permissions set
1904                 # so return an error value.
1906                 if (!$ignoremissing){
1908                         $error = "FilterDatabaseInvalidPermissionsSet";
1909                         return;
1911                 }
1913         }
1915         # Connect to the template database.
1917         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1918         $filterdb_database_handle->{unicode} = 1;
1919         $filterdb_loaded = 1;
1923 sub disconnectfilter{
1924 #################################################################################
1925 # disconnectfilter: Disconnect from the filter database.                        #
1926 #                                                                               #
1927 # Usage:                                                                        #
1928 #                                                                               #
1929 # $dbmodule->disconnectfilter();                                                #
1930 #################################################################################
1932         # Disconnect the template database.
1934         if ($filterdb_loaded eq 1){
1936                 undef $filterdb_statement_handle;
1937                 $filterdb_database_handle->disconnect();
1939         }
1943 sub getfiltercount{
1944 #################################################################################
1945 # getfiltercount: Gets the count of filters in the filters database.            #
1946 #                                                                               #
1947 # Usage:                                                                        #
1948 #                                                                               #
1949 # $dbmodule->getfiltercount();                                                  #
1950 #################################################################################
1952         $error = "";
1953         $errorext = "";
1955         my $class       = shift;
1957         $filterdb_statement_handle      = $filterdb_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_filters') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return);
1958         $filterdb_statement_handle->execute();
1960         my $count = $filterdb_statement_handle->fetchrow_array();
1962         return $count;  
1966 sub getfilterlist{
1967 #################################################################################
1968 # getfilterlist: Gets the list of filters in the filter database.               #
1969 #                                                                               #
1970 # Usage:                                                                        #
1971 #                                                                               #
1972 # $dbmodule->getfilterlist(options);                                            #
1973 #                                                                               #
1974 # StartFrom     Specifies where the list of filters should start from.          #
1975 # Limit         Specifies the amount of the filters to get.                     #
1976 #################################################################################
1978         $error = "";
1979         $errorext = "";
1981         my $class               = shift;
1982         my ($passedoptions)     = shift;
1983         
1984         my @filter_list;
1985         my @filter_data;
1987         my $start_from  = $passedoptions->{"StartFrom"};
1988         my $limit       = $passedoptions->{"Limit"};
1990         if (defined($start_from)){
1992                 if (!$limit){
1993                         
1994                         $limit = 0;
1996                 }
1998                 $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 );
1999                 $filterdb_statement_handle->execute();
2001         } else {
2003                 $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 );
2004                 $filterdb_statement_handle->execute();
2006         }
2008         # Get the list of filters available.
2010         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2012                 # Add the filter to the list of available filters.
2014                 push(@filter_list, $filter_data[0]);
2016         }
2018         undef $filterdb_statement_handle;
2019         return @filter_list;
2023 sub getfilterinfo{
2024 #################################################################################
2025 # getfilterinfo: Gets information about the filter.                             #
2026 #                                                                               #
2027 # Usage:                                                                        #
2028 #                                                                               #
2029 # $dbmodule->getfilterinfo(options);                                            #
2030 #                                                                               #
2031 # options       Specifies the following options in any order.                   #
2032 #                                                                               #
2033 # FilterID      Specifies the filter ID number to get information from.         #
2034 # Reduced       Specifies if the reduced version of the filter information      #
2035 #               should be retrieved.                                            #
2036 #################################################################################
2038         $error = "";
2039         $errorext = "";
2041         # Get the values passed to the subroutine.
2043         my $class               = shift;
2044         my ($passedoptions)     = @_;
2046         my %filter_info;
2047         my $filter_exists       = 0;
2048         my @filter_data;
2050         # Get the values that are in the hash.
2052         my $filter_id           = $passedoptions->{"FilterID"};
2053         my $reduced             = $passedoptions->{"Reduced"};
2054         
2055         if ($reduced && $reduced eq 1){
2057                 $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 );
2058                 $filterdb_statement_handle->execute();
2060         } else {
2062                 $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 );
2063                 $filterdb_statement_handle->execute();
2065         }
2067         # Get the filter information.
2069         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2071                 $filter_info{"FilterID"}        = $filter_data[0];
2072                 $filter_info{"FilterPriority"}  = $filter_data[1];
2073                 $filter_info{"FilterFind"}      = $filter_data[2];
2074                 $filter_info{"FilterReplace"}   = $filter_data[3];
2075                 $filter_info{"FilterEnabled"}   = $filter_data[4];
2076                 $filter_info{"FilterNotes"}     = $filter_data[5];
2078                 $filter_exists = 1;
2080         }
2082         # Check if the filter exists.
2084         if (!$filter_exists){
2086                 # The filter does not exist so return
2087                 # an error value.
2089                 $error = "FilterDoesNotExist";
2090                 return;
2092         }
2094         # Return the filter information.
2096         undef $filterdb_statement_handle;
2097         return %filter_info;
2101 sub addfilter{
2102 #################################################################################
2103 # addfilter: Adds a filter to the filter database.                              #
2104 #                                                                               #
2105 # Usage:                                                                        #
2106 #                                                                               #
2107 # $dbmodule->addfilter(options);                                                #
2108 #                                                                               #
2109 # options       Specifies the following options in any order.                   #
2110 #                                                                               #
2111 # FindFilter    Specifies the find filter to add.                               #
2112 # ReplaceFilter Specifies the replace filter to add.                            #
2113 # Priority      Specifies the filter priority to use.                           #
2114 # Notes         Specifies the notes to use.                                     #
2115 #################################################################################
2117         $error = "";
2118         $errorext = "";
2120         # Get the values passed to the subroutine.
2122         my $class = shift;
2123         my ($passedoptions) = @_;
2125         # Define some variables for later.
2127         my @database_filters;
2128         my @filterid_list;
2129         my @filterid_check;
2130         my $nofiltertable = 0;
2131         my $filter_found = 0;
2132         my $filter_count = 0;
2133         my $filter_id;
2134         my $new_id;
2136         # Get the values from the hash.
2138         my $filter_find         = $passedoptions->{"FindFilter"};
2139         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2140         my $filter_priority     = $passedoptions->{"Priority"};
2141         my $filter_enabled      = $passedoptions->{"Enabled"};
2142         my $filter_notes        = $passedoptions->{"Notes"};
2144         # Check if the filter database permissions are valid.
2146         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2147         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2149         if ($filterdb_permissions eq 1){
2151                 if ($filterdb_exists eq 0){
2152                         $error = "FilterDatabaseInvalidPermissionsSet";
2153                         return;
2154                 }
2156         }
2158         # Check if certain values are undefined and if they
2159         # are then set them blank.
2161         if (!$filter_find){
2163                 $filter_find = "";
2165         }
2167         if (!$filter_replace){
2169                 $filter_replace = "";
2171         }
2173         if (!$filter_priority){
2175                 $filter_priority = 1;
2177         }
2179         if (!$filter_notes){
2181                 $filter_notes = "";
2183         }
2185         if (!$filter_enabled){
2187                 $filter_enabled = "";
2189         }
2191         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2193         if ($directory_permissions eq 1 && $filterdb_exists){
2195                 # The template database cannot be created because of invalid directory
2196                 # permissions so return an error value.
2198                 $error = "FilterDatabaseFileUncreateable";
2199                 return; 
2201         }
2203         # Check if the filter table exists.
2205         $filterdb_statement_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2207         # Check if there is really no filter table.
2209         if ($nofiltertable){
2211                 # Create the filter database table.
2213                 $filterdb_statement_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2214                         id int(7) primary key,
2215                         priority int(5),
2216                         findsetting varchar(1024),
2217                         replacesetting varchar(1024),
2218                         enabled boolean,
2219                         notes text
2220                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2221                 $filterdb_statement_handle->execute();
2223         }
2225         # Find the lowest filter identification number available.
2227         $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 );
2228         $filterdb_statement_handle->execute();
2230         while (@database_filters = $filterdb_statement_handle->fetchrow_array()){
2232                 $filter_id      = $database_filters[0];
2234                 # Add the filter identification to the list of filter IDs.
2236                 push(@filterid_list, $filter_id);
2238         }
2240         $filter_id = "";
2242         # Process each filter looking for a blank available filter.
2244         foreach $filter_id (@filterid_list){
2246                 # Check the next filter ID to see if it's blank.
2248                 $new_id = $filter_id + 1;
2250                 $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 );
2251                 $filterdb_statement_handle->execute();
2253                 # Get the filter identification number.
2255                 while (@filterid_check = $filterdb_statement_handle->fetchrow_array()){
2257                         $filter_found = 1;
2259                 }
2261                 # Check if a filter was found.
2263                 if (!$filter_found){
2265                         # No filter was found using this ID so exit the loop.
2267                         last;
2269                 }
2271                 # Increment the filter count and reset the filter found value.
2273                 $filter_count++;
2274                 $filter_found = 0;
2275                 $new_id = 0;
2277         }
2279         # Check if there were any filters in the filter database.
2281         if (!$filter_count && !$new_id){
2283                 # There were no filters in the filter database so set
2284                 # the new filter identification value to 1.
2286                 $new_id = 1;
2288         }
2290         # Add the filter to the filter database.
2292         $filterdb_statement_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, enabled, notes) VALUES (
2293                 \'' . $class->convert($new_id) . '\',
2294                 \'' . $class->convert($filter_priority) . '\',
2295                 \'' . $class->convert($filter_find) . '\',
2296                 \'' . $class->convert($filter_replace) .'\',
2297                 \'' . $class->convert($filter_enabled) . '\',
2298                 \'' . $class->convert($filter_notes) . '\'
2299         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2300         $filterdb_statement_handle->execute();
2304 sub editfilter{
2305 #################################################################################
2306 # editfilter: Edits a filter in the filter database.                            #
2307 #                                                                               #
2308 # Usage:                                                                        #
2309 #                                                                               #
2310 # $dbmodule->editfilter(options);                                               #
2311 #                                                                               #
2312 # options       Specifies the following options in any order.                   #
2313 #                                                                               #
2314 # FilterID              Specifies the filter to edit.                           #
2315 # NewFindFilter         Specifies the new find filter setting.                  #
2316 # NewReplaceFilter      Specifies the new replace filter setting.               #
2317 # NewFilterPriority     Specifies the new filter priority setting.              #
2318 # NewEnabled            Specifies if the filter should be enabled.              #
2319 # NewFilterNotes        Specifies the new notes for the filter.                 #
2320 #################################################################################
2322         $error = "";
2323         $errorext = "";
2325         # Get the values passed to the subroutine.
2327         my $class = shift;
2328         my ($passedoptions) = @_;
2330         my @filter_data;
2331         my $filter_exists = 1;
2332         my $blankfile = 0;
2334         # Get the values from the hash.
2336         my $filter_id           = $passedoptions->{"FilterID"};
2337         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2338         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2339         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2340         my $filter_enabled      = $passedoptions->{"NewEnabled"};
2341         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2343         # Check if the filter database permissions are valid.
2345         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2346         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2348         if ($filterdb_permissions eq 1){
2350                 if ($filterdb_exists eq 0){
2351                         $error = "FilterDatabaseInvalidPermissionsSet";
2352                         return;
2353                 }
2355         }
2357         # Check if the filter exists before editing it.
2359         $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 );
2360         $filterdb_statement_handle->execute();
2362         # Check if the filter exists.
2364         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2366                 $filter_exists = 1;
2368         }       
2370         # Check if the filter really does exist.
2372         if (!$filter_exists){
2374                 # The filter does not exist so return
2375                 # an error value.
2377                 $error = "FilterDoesNotExist";
2378                 return;
2380         }
2382         # Edit the selected filter.
2384         $filterdb_statement_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2385                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2386                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2387                 priority = \'' . $class->convert($filter_newpriority) . '\',
2388                 enabled = \'' . $class->convert($filter_enabled) . '\',
2389                 notes = \'' . $class->convert($filter_newnotes) . '\'
2390         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2391         $filterdb_statement_handle->execute();
2393         undef $filterdb_statement_handle;
2394         return;
2398 sub deletefilter{
2399 #################################################################################
2400 # deletefilter: Deletes a filter from the filter database.                      #
2401 #                                                                               #
2402 # Usage:                                                                        #
2403 #                                                                               #
2404 # $dbmodule->deletefilter(options);                                             #
2405 #                                                                               #
2406 # options       Specifies the following options in any order.                   #
2407 #                                                                               #
2408 # FilterID      Specifies the filter to delete from the filter database.        #
2409 #################################################################################
2411         $error = "";
2412         $errorext = "";
2414         # Get the values passed to the subroutine.
2416         my $class = shift;
2417         my ($passedoptions) = @_;
2419         my $filter_exists = 0;
2420         my @filter_data;
2422         # Get the values from the hash.
2424         my $filter_id           = $passedoptions->{"FilterID"};
2426         # Check if the filter exists before deleting.
2428         $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 );
2429         $filterdb_statement_handle->execute();
2431         while (@filter_data = $filterdb_statement_handle->fetchrow_array()){
2433                 $filter_exists = 1;
2435         }
2437         # Check to see if the filter really does exist.
2439         if (!$filter_exists){
2441                 $error = "FilterDoesNotExist";
2442                 return;
2444         }
2446         # Delete the filter from the filter database.
2448         $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 );
2449         $filterdb_statement_handle->execute();
2451         undef $filterdb_statement_handle;
2455 #################################################################################
2456 # Template subroutines.                                                         #
2457 #################################################################################
2459 sub connecttemplate{
2460 #################################################################################
2461 # connecttemplate: Connect to the template database.                            #
2462 #                                                                               #
2463 # Usage:                                                                        #
2464 #                                                                               #
2465 # $dbmodule->connecttemplate(missingignore);                                    #
2466 #                                                                               #
2467 # missingignore Ignore errror about database being missing.                     #
2468 #################################################################################
2470         $error = "";
2471         $errorext = "";
2473         my $class = shift;
2474         my $ignoremissing = shift;
2476         # Check if the template database exists.
2478         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2479         
2480         if ($templatedatabase_exists eq 1){
2482                 $templatedb_exists = 0;
2484                 if (!$ignoremissing){
2486                         $error = "TemplateDatabaseDoesNotExist";
2487                         return;
2489                 }
2491         }
2493         # Check if the permission settings for the template database are valid.
2495         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2497         if ($templatedb_permissions eq 1){
2499                 # The template database has invalid permissions set
2500                 # so return an error value.
2502                 if (!$ignoremissing){
2504                         $error = "TemplateDatabaseInvalidPermissionsSet";
2505                         return;
2507                 }
2509         }
2511         # Connect to the template database.
2513         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2514         $template_database_handle->{unicode} = 1;
2515         $templatedb_loaded = 1;
2519 sub disconnecttemplate{
2520 #################################################################################
2521 # disconnecttemplate: Disconnect from the template database.                    #
2522 #                                                                               #
2523 # Usage:                                                                        #
2524 #                                                                               #
2525 # $dbmodule->disconnecttemplate();                                              #
2526 #################################################################################
2528         # Disconnect the template database.
2530         if ($templatedb_loaded eq 1){
2532                 undef $template_statement_handle;
2533                 $template_database_handle->disconnect();
2535         }
2539 sub gettemplatecount{
2540 #################################################################################
2541 # gettemplatecount: Gets the count of templates in the template database.       #
2542 #                                                                               #
2543 # Usage:                                                                        #
2544 #                                                                               #
2545 # $dbmodule->gettemplatecount();                                                #
2546 #################################################################################
2548         $error = "";
2549         $errorext = "";
2550  
2551         my $class       = shift;
2552  
2553         $template_statement_handle      = $template_database_handle->prepare('SELECT COUNT(*) FROM kiriwrite_templates') or ( $error = "FilterDatabaseError", $errorext = $template_database_handle->errstr, return);
2554         $template_statement_handle->execute();
2555  
2556         my $count = $template_statement_handle->fetchrow_array();
2557  
2558         return $count;
2562 sub gettemplatelist{
2563 #################################################################################
2564 # gettemplatelist: Gets the list of templates.                                  #
2565 #                                                                               #
2566 # Usage:                                                                        #
2567 #                                                                               #
2568 # $dbmodule->gettemplatelist(options);                                          #
2569 #                                                                               #
2570 # options       Specifies the following options as a hash (in any order).       #
2571 #                                                                               #
2572 # StartFrom     Specifies where the list of templates will start from.          #
2573 # Limit         Specifies how many templates should be retrieved.               #
2574 #################################################################################
2576         $error = "";
2577         $errorext = "";
2579         my $class               = shift;
2580         my ($passedoptions)     = @_;
2582         my $start_from          = $passedoptions->{"StartFrom"};
2583         my $limit               = $passedoptions->{"Limit"};
2585         if (defined($start_from)){
2587                 if (!$limit){
2588                         
2589                         $limit = 0;
2591                 }
2593                 $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 );
2594                 $template_statement_handle->execute();          
2596         } else {
2598                 $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 );
2599                 $template_statement_handle->execute();
2601         }
2603         my @database_template;
2604         my @templates_list;
2605         my $template_filename;
2607         while (@database_template = $template_statement_handle->fetchrow_array()){
2609                 # Get certain values from the array.
2611                 $template_filename      = $database_template[0];
2613                 # Add the template to the list of templates.
2615                 push(@templates_list, $template_filename);
2617         }
2619         return @templates_list;
2623 sub gettemplateinfo{
2624 #################################################################################
2625 # gettemplateinfo: Get information on a template.                               #
2626 #                                                                               #
2627 # Usage:                                                                        #
2628 #                                                                               #
2629 # $dbmodule->gettemplateinfo(options);                                          #
2630 #                                                                               #
2631 # options       Specifies the following options in any order.                   #
2632 #                                                                               #
2633 # TemplateFilename      Specifies the template filename to use.                 #
2634 # Reduced               Specifies a reduced version of template information to  #
2635 #                       get.                                                    #
2636 #################################################################################
2638         $error = "";
2639         $errorext = "";
2641         # Get the data passed to the subroutine.
2643         my $class = shift;
2644         my ($passedoptions) = @_;
2646         my %page_info;
2647         my @template_data;
2649         my $template_filename;
2650         my $template_name;
2651         my $template_description;
2652         my $template_datemodified;
2653         my $template_layout;
2655         my $template_found = 0;
2657         my $filename    = $passedoptions->{"TemplateFilename"};
2658         my $reduced     = $passedoptions->{"Reduced"};
2660         if ($reduced && $reduced eq 1){
2662                 $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 );
2663                 $template_statement_handle->execute();
2665         } else {
2667                 $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 );
2668                 $template_statement_handle->execute();
2670         }
2672         while (@template_data = $template_statement_handle->fetchrow_array()){
2674                 # Get certain values from the array.
2676                 $template_filename      = $template_data[0];
2677                 $template_name          = $template_data[1];
2678                 $template_description   = $template_data[2];
2679                 $template_layout        = $template_data[3];
2680                 $template_datemodified  = $template_data[4];
2682                 # Process them into the hash.
2684                 %page_info = (
2685                         "TemplateFilename" => $template_filename,
2686                         "TemplateName" => $template_name,
2687                         "TemplateDescription" => $template_description,
2688                         "TemplateLayout" => $template_layout,
2689                         "TemplateLastModified" => $template_datemodified
2690                 );
2692                 $template_found = 1;
2694         }
2696         if ($template_found eq 0){
2698                 # The template was not found in the template database so
2699                 # write an error value.
2701                 $error = "TemplateDoesNotExist";
2702                 return;
2704         }
2706         return %page_info;
2710 sub addtemplate{
2711 #################################################################################
2712 # addtemplate: Adds a template to the template database.                        #
2713 #                                                                               #
2714 # Usage:                                                                        #
2715 #                                                                               #
2716 # $dbmodule->addtemplate();                                                     #
2717 #                                                                               #
2718 # options       Specifies the following options in any order.                   #
2719 #                                                                               #
2720 # TemplateFilename      Specifies the new template filename.                    #
2721 # TemplateName          Specifies the new template name.                        #
2722 # TemplateDescription   Specifies the new template description.                 #
2723 # TemplateLayout        Specifies the new template layout.                      #
2724 #################################################################################
2726         $error = "";
2727         $errorext = "";
2729         # Get the data passed to the subroutine.
2731         my $class = shift;
2732         my ($passedoptions) = @_;
2734         my @page_exists;
2735         my $notemplatetable;
2736         my $blankfile = 0;
2738         my $template_filename           = $passedoptions->{"TemplateFilename"};
2739         my $template_name               = $passedoptions->{"TemplateName"};
2740         my $template_description        = $passedoptions->{"TemplateDescription"};
2741         my $template_layout             = $passedoptions->{"TemplateLayout"};
2743         # Check if the template database permissions are valid.
2745         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2746         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2748         if ($templatedb_permissions eq 1){
2750                 if ($templatedb_exists eq 0){
2751                         $error = "TemplateDatabaseInvalidPermissionsSet";
2752                         return;
2753                 }
2755         }
2757         # Check if the template already exists before adding.
2759         if ($templatedb_exists eq 0){
2761                 $template_statement_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2763                 if ($blankfile eq 0){
2765                         $template_statement_handle->execute();
2767                         while (@page_exists = $template_statement_handle->fetchrow_array()){
2769                                 $error = "TemplatePageExists";
2770                                 return;
2772                         }
2774                 }
2776         }
2778         # Get the current date.
2779  
2780         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2781  
2782         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2784         # Check if certain values are undefined and if they
2785         # are then set them blank.
2787         if (!$template_name){
2789                 $template_name = "";
2791         }
2793         if (!$template_description){
2795                 $template_description = "";
2797         }
2799         if (!$template_layout){
2801                 $template_layout = "";
2803         }
2805         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2807         if ($directory_permissions eq 1 && $templatedb_exists){
2809                 # The template database cannot be created because of invalid directory
2810                 # permissions so return an error value.
2812                 $error = "TemplateDatabaseUncreateable";
2813                 return; 
2815         }
2817         # Check to see if a template can be added.
2819         $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2820                                 \'' . $class->convert($template_filename) . '\',
2821                                 \'' . $class->convert($template_name) . '\',
2822                                 \'' . $class->convert($template_description) . '\',
2823                                 \'' . $class->convert($template_layout) . '\',
2824                                 \'' . $class->convert($template_date) . '\'
2825         )') or ( $notemplatetable = 1 );
2827         if (!$notemplatetable){
2829                 $template_statement_handle->execute();
2831         }
2833         # Check to see if there is no template table and attempt to create one.
2835         if ($notemplatetable){
2837                 # Create a template table.
2839                 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2841                 if ($directory_permissions eq 1){
2843                         # The template database cannot be created because of invalid directory
2844                         # permissions so return an error.
2846                         $error = "TemplateDatabaseFileUncreateable";
2847                         return;
2849                 }
2851                 $template_statement_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2852                         filename varchar(256) primary key,
2853                         templatename varchar(512),
2854                         templatedescription varchar(512),
2855                         templatelayout text,
2856                         datemodified datetime
2857                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2858                 $template_statement_handle->execute();
2860                 $template_statement_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2861                                 \'' . $class->convert($template_filename) . '\',
2862                                 \'' . $class->convert($template_name) . '\',
2863                                 \'' . $class->convert($template_description) . '\',
2864                                 \'' . $class->convert($template_layout) . '\',
2865                                 \'' . $class->convert($template_date) . '\'
2866                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2867                 $template_statement_handle->execute();
2869         }
2873 sub deletetemplate{
2874 #################################################################################
2875 # deletetemplate: Deletes a template from the template database.                #
2876 #                                                                               #
2877 # Usage:                                                                        #
2878 #                                                                               #
2879 # $dbmodule->deletetemplate(options);                                           #
2880 #                                                                               #
2881 # options       Specifies the following options in any order.                   #
2882 #                                                                               #
2883 # TemplateFilename      Specifies the template filename to delete.              #
2884 #################################################################################
2886         $error = "";
2887         $errorext = "";
2889         # Get the data passed to the subroutine.
2891         my $class = shift;
2892         my ($passedoptions) = @_;
2894         my @pagedata;
2895         my $template_filename = $passedoptions->{"TemplateFilename"};
2896         my $template_count = 0;
2898         # Check if the template exists.
2900         $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 );
2901         $template_statement_handle->execute();
2903         while (@pagedata = $template_statement_handle->fetchrow_array()){
2905                 $template_count++;
2907         }
2909         if ($template_count eq 0){
2911                 # No pages were returned so return an error value.
2913                 $error = "TemplateDoesNotExist";
2914                 return;
2916         }
2918         # Delete the template from the template database.
2920         $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 );
2921         $template_statement_handle->execute();
2925 sub edittemplate{
2926 #################################################################################
2927 # editttemplate: Edits a Kiriwrite template.                                    #
2928 #                                                                               #
2929 # Usage:                                                                        #
2930 #                                                                               #
2931 # $dbmodule->edittemplate(options);                                             #
2932 #                                                                               #
2933 # options       Specifies the following options in any order.                   #
2934 #                                                                               #
2935 # TemplateFilename              Specifies the template filename to edit.        #
2936 # NewTemplateFilename           Specifies the new template filename.            #
2937 # NewTemplateName               Specifies the new template name.                #
2938 # NewTemplateDescription        Specifies the new template description.         #
2939 # NewTemplateLayout             Specifies the new template layout.              #
2940 #################################################################################
2942         # Get the values passed.
2944         my $class = shift;
2945         my ($passedoptions) = @_;
2946         my $template_found = 0;
2947         my @template_info;
2949         # Process the values passed.
2951         my $template_filename           = $passedoptions->{"TemplateFilename"};
2952         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2953         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2954         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2955         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2957         # Check if the template database permissions are valid.
2959         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2960         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2962         if ($templatedb_permissions eq 1){
2964                 if ($templatedb_exists eq 0){
2965                         $error = "TemplateDatabaseInvalidPermissionsSet";
2966                         return;
2967                 }
2969         }
2971         # Check if the template exists.
2973         $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 );
2974         $template_statement_handle->execute();
2976         while (@template_info = $template_statement_handle->fetchrow_array()){
2978                 $template_found = 1;
2980         }
2982         # Check to see if the template was found and set an error value if
2983         # it wasn't.
2985         if ($template_found eq 0){
2987                 $error = "TemplateDoesNotExist";
2988                 return;
2990         }
2992         # Get the date and time.
2994         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2995         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2997         # Update the template information.
2999         $template_statement_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
3000                 filename = \'' . $class->convert($new_template_filename) . '\',
3001                 templatename = \'' . $class->convert($new_template_name) . '\',
3002                 templatedescription = \'' . $class->convert($new_template_description) . '\',
3003                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
3004                 datemodified = \'' . $class->convert($templatenewdate) . '\'
3005                 WHERE filename = \'' . $class->convert($template_filename) . '\'
3006         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
3007         $template_statement_handle->execute();
3011 #################################################################################
3012 # General subroutines.                                                          #
3013 #################################################################################
3015 sub connect{
3016 #################################################################################
3017 # connect: Connect to the server.                                               #
3018 #                                                                               #
3019 # Usage:                                                                        #
3020 #                                                                               #
3021 # $dbmodule->connect();                                                         #
3022 #################################################################################
3024         # This function is not needed in this database module.
3028 sub disconnect{
3029 #################################################################################
3030 # connect: Disconnect from the server.                                          #
3031 #                                                                               #
3032 # Usage:                                                                        #
3033 #                                                                               #
3034 # $dbmodule->disconnect();                                                      #
3035 #################################################################################
3037         # This function is not needed in this database module.
3039         undef $statement_handle;
3043 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