Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Some updates.
[kiriwrite/.git] / cgi-files / Modules / Database / SQLite.pm
1 #################################################################################
2 # Kiriwrite Database Module - SQLite Database Module (SQLite.pm)                #
3 # Database module for mainipulating SQLite databases in the database directory. #
4 #                                                                               #
5 # Copyright (C) 2007 Steve Brokenshire <sbrokenshire@xestia.co.uk>              #
6 #                                                                               #
7 # This module is licensed under the same license as Kiriwrite which is the GPL. #
8 #                                                                               #
9 # This program is free software; you can redistribute it and/or modify it under #
10 # the terms of the GNU General Public License as published by the Free          #
11 # Software Foundation; as version 2 of the License.                             #
12 #                                                                               #
13 # This program is distributed in the hope that it will be useful, but WITHOUT   #
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.#
16 #                                                                               #
17 # You should have received a copy of the GNU General Public License along with  #
18 # this program; if not, write to the Free Software Foundation, Inc., 51         #
19 # Franklin St, Fifth Floor, Boston, MA 02110-1301 USA                           #
20 #################################################################################
22 # Define the package (perl module) name.
24 package Kiriwrite::Database::SQLite;
26 # Enable strict and use warnings.
28 use strict;
29 use warnings;
31 # Load the following Perl modules.
33 use DBI qw(:sql_types);
35 # Set the following values.
37 our $VERSION    = "0.1.0";
38 my ($options, %options);
39 my $database_handle;
40 my $string_handle;
41 my $error = "";
42 my $errorext = "";
43 my $database_filename;
44 my $second_database_filename;
45 my $second_database_handle;
46 my $second_string_handle;
47 my $templatedb_loaded = 0;
48 my $templatedb_exists = 1;
49 my $template_string_handle;
50 my $template_database_handle;
51 my $filterdb_loaded = 0;
52 my $filterdb_exists = 1;
53 my $filterdb_string_handle;
54 my $filterdb_database_handle;
57 #################################################################################
58 # Generic Subroutines.                                                          #
59 #################################################################################
61 sub new{
62 #################################################################################
63 # new: Create an instance of Kiriwrite::Database::SQLite                        #
64 #                                                                               #
65 # Usage:                                                                        #
66 #                                                                               #
67 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
68 #################################################################################
69         
70         # Get the perl module name.
72         my $class = shift;
73         my $self = {};
75         return bless($self, $class);
77 }
79 sub loadsettings{
80 #################################################################################
81 # loadsettings: Loads settings into the SQLite database module                  #
82 #                                                                               #
83 # Usage:                                                                        #
84 #                                                                               #
85 # $dbmodule->loadsettings(Directory, options);                                  #
86 #                                                                               #
87 # options       Specifies the following options (in any order).                 #
88 #                                                                               #
89 # Directory     Specifies the directory to use for getting databases.           #
90 # DateTime      Specifies the date and time format to use.                      #
91 # Server        Specifies the server to use.                                    #
92 # Database      Specifies the database to use.                                  #
93 # Username      Specifies the username to use.                                  #
94 # Password      Specifies the password to use.                                  #
95 # HashType      Specifies the password hash type to use.                        #
96 # Port          Specifies the server port to use.                               #
97 # Protocol      Specifies the protocol to use.                                  #
98 # TablePrefix   Specifies the table prefix to use.                              #
99 #################################################################################
101         # Get the data passed to the subroutine.
103         my $class = shift;
104         my ($passedoptions)     = @_;
106         # Add the directory setting to the list of options (as it's the only
107         # one needed for this database module).
109         %options = (
110                 "Directory"     => $passedoptions->{"Directory"},
111                 "DateTime"      => $passedoptions->{"DateTime"},
112         );
116 sub convert{
117 #################################################################################
118 # convert: Converts data into SQL formatted data.                               #
119 #                                                                               #
120 # Usage:                                                                        #
121 #                                                                               #
122 # $dbmodule->convert(data);                                                     #
123 #                                                                               #
124 # data          Specifies the data to convert.                                  #
125 #################################################################################
127         # Get the data passed to the subroutine.
129         my $class       = shift;
130         my $data        = shift;
132         if (!$data){
133                 $data = "";
134         }
136         $data =~ s/'/''/g;
137         $data =~ s/\b//g;
139         return $data;
143 sub dateconvert{
144 #################################################################################
145 # dateconvert: Converts a SQL date into a proper date.                          #
146 #                                                                               #
147 # Usage:                                                                        #
148 #                                                                               #
149 # $dbmodule->dateconvert(date);                                                 #
150 #                                                                               #
151 # date          Specifies the date to convert.                                  #
152 #################################################################################
154         # Get the date passed to the subroutine.
156         my $class       = shift;
157         my $data        = shift;
159         # Convert the date given into the proper date.
161         # Create the following varialbes to be used later.
163         my $date;
164         my $time;
165         my $day;
166         my $day_full;
167         my $month;
168         my $month_check;
169         my $month_full;
170         my $year;
171         my $year_short;
172         my $hour;
173         my $hour_full;
174         my $minute;
175         my $minute_full;
176         my $second;
177         my $second_full;
178         my $seek = 0;
179         my $timelength;
180         my $datelength;
181         my $daylength;
182         my $secondlength;
183         my $startchar = 0;
184         my $char;
185         my $length;
186         my $count = 0;
188         # Split the date and time.
190         $length = length($data);
192         if ($length > 0){
194                 do {
196                         # Get the character and check if it is a space.
198                         $char = substr($data, $seek, 1);
200                         if ($char eq ' '){
202                                 # The character is a space, so get the date and time.
204                                 $date           = substr($data, 0, $seek);
205                                 $timelength     = $length - $seek - 1;
206                                 $time           = substr($data, $seek + 1, $timelength);
208                         }
210                         $seek++;
212                 } until ($seek eq $length);
214                 # Get the year, month and date.
216                 $length = length($date);
217                 $seek = 0;
219                 do {
221                         # Get the character and check if it is a dash.
223                         $char = substr($date, $seek, 1);
225                         if ($char eq '-'){
227                                 # The character is a dash, so get the year, month or day.
229                                 $datelength = $seek - $startchar;
231                                 if ($count eq 0){
233                                         # Get the year from the date.
235                                         $year           = substr($date, 0, $datelength) + 1900;
236                                         $startchar      = $seek;
237                                         $count = 1;
239                                         # Get the last two characters to get the short year
240                                         # version.
242                                         $year_short     = substr($year, 2, 2);
244                                 } elsif ($count eq 1){
246                                         # Get the month and day from the date.
248                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
250                                         # Check if the month is less then 10, if it is
251                                         # add a zero to the value.
253                                         if ($month < 10){
255                                                 $month_full = '0' . $month;
257                                         } else {
259                                                 $month_full = $month;
261                                         }
263                                         $startchar      = $seek;
264                                         $count = 2;
266                                         $daylength      = $length - $seek + 1;
267                                         $day            = substr($date, $startchar + 1, $daylength);
269                                         # Check if the day is less than 10, if it is
270                                         # add a zero to the value.
272                                         if ($day < 10){
274                                                 $day_full       = '0' . $day;
276                                         } else {
278                                                 $day_full       = $day;
280                                         }
282                                 }
284                         }
286                         $seek++;
288                 } until ($seek eq $length);
290                 # Get the length of the time value and reset certain
291                 # values to 0.
293                 $length = length($time);
294                 $seek = 0;
295                 $count = 0;
296                 $startchar = 0;
298                 do {
300                         # Get the character and check if it is a colon.
302                         $char = substr($time, $seek, 1);
304                         if ($char eq ':'){
306                                 # The character is a colon, so get the hour, minute and day.
308                                 $timelength = $seek - $startchar;
310                                 if ($count eq 0){
312                                         # Get the hour from the time.
314                                         $hour = substr($time, 0, $timelength);
315                                         $count = 1;
316                                         $startchar = $seek;
318                                         # If the hour is less than ten then add a
319                                         # zero.
321                                         if ($hour < 10){
323                                                 $hour_full = '0' . $hour;
325                                         } else {
327                                                 $hour_full = $hour;
329                                         }
331                                 } elsif ($count eq 1){
333                                         # Get the minute and second from the time.
335                                         $minute = substr($time, $startchar + 1, $timelength - 1);
336                                         $count = 2;
337                                                 
338                                         # If the minute is less than ten then add a
339                                         # zero.
341                                         if ($minute < 10){
343                                                 $minute_full = '0' . $minute;
345                                         } else {
347                                                 $minute_full = $minute;
349                                         }
351                                         $startchar = $seek;
353                                         $secondlength = $length - $seek + 1;
354                                         $second = substr($time, $startchar + 1, $secondlength);
355                                         
356                                         # If the second is less than ten then add a
357                                         # zero.
359                                         if ($second < 10){
361                                                 $second_full = '0' . $second;
363                                         } else {
365                                                 $second_full = $second;
367                                         }
369                                 }
371                         }
373                         $seek++;
375                 } until ($seek eq $length);
377                 # Get the setting for displaying the date and time.
379                 $data = $options{"DateTime"};
381                 # Process the setting for displaying the date and time
382                 # using regular expressions
384                 $data =~ s/DD/$day_full/g;
385                 $data =~ s/D/$day/g;
386                 $data =~ s/MM/$month_full/g;
387                 $data =~ s/M/$month/g;
388                 $data =~ s/YY/$year/g;
389                 $data =~ s/Y/$year_short/g;
391                 $data =~ s/hh/$hour_full/g;
392                 $data =~ s/h/$hour/g;
393                 $data =~ s/mm/$minute_full/g;
394                 $data =~ s/m/$minute/g;
395                 $data =~ s/ss/$second_full/g;
396                 $data =~ s/s/$second/g;
398         }
400         return $data;
404 sub geterror{
405 #################################################################################
406 # geterror: Gets the error message (or extended error message).                 #
407 #                                                                               #
408 # Usage:                                                                        #
409 #                                                                               #
410 # $dbmodule->geterror(extended);                                                #
411 #                                                                               #
412 # Extended      Specifies if the extended error should be retrieved.            #
413 #################################################################################
415         # Get the data passed to the subroutine.
417         my $class       = shift;
418         my $extended    = shift;
420         if (!$extended){
421                 $extended = 0;
422         }
424         if (!$errorext){
425                 $errorext = "";
426         }
428         if (!$error){
429                 $error = "";
430         }
432         # Check to see if extended information should be returned.
434         if ($extended eq 1){
436                 # Extended information should be returned.
438                 return $errorext;
440         } else {
442                 # Basic information should be returned.
444                 return $error;
446         }
450 sub dbpermissions{
451 #################################################################################
452 # dbpermissions: Check if the permissions for the database are valid.           #
453 #                                                                               #
454 # Usage:                                                                        #
455 #                                                                               #
456 # $database->dbpermissions(dbname, read, write);                                #
457 #                                                                               #
458 # dbname        Specifies the database name to check.                           #
459 # read          Check to see if the database can be read.                       #
460 # write         Check to see if the database can be written.                    #
461 #################################################################################
463         # Get the database name, read setting and write setting.
465         my ($class, $dbname, $readper, $writeper)       = @_;
467         # Check if the database can be read.
469         if ($readper){
471                 if (-r $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
473                         # The database can be read.
475                 } else {
477                         # The database cannot be read, so return a value
478                         # of 1.
480                         return 1;
482                 }
484         }
486         # Check if the database can be written.
488         if ($writeper){
490                 if (-w $options{"Directory"} . '/' . $dbname . ".db.sqlite"){
492                         # The database can be read.
494                 } else {
496                         # The database cannot be read, so return a value
497                         # of 1.
499                         return 1;
501                 }
503         }
505         # No errors have occured while checking so return a value
506         # of 0.
508         return 0;
512 sub dbexists{
513 #################################################################################
514 # dbexists: Check if the database exists.                                       #
515 #                                                                               #
516 # Usage:                                                                        #
517 #                                                                               #
518 # $dbmodule->dbexists(dbname);                                                  #
519 #                                                                               #
520 # dbname        Specifies the database name to check.                           #
521 #################################################################################
523         # Get the value that was passed to the subroutine.
525         my $class       = shift;
526         my ($filename)  = @_;
528         # Check if the filename exists, if it does, return a value of 1, else
529         # return a value of 0, meaning that the file was not found.
531         if (-e $options{"Directory"} . '/' . $filename . ".db.sqlite"){
533                 # Specified file does exist so return a value of 0.
535                 return 0;
537         } else {
539                 # Specified file does not exist so return a value of 1.
541                 return 1;
543         }
547 #################################################################################
548 # Database Subroutines.                                                         #
549 #################################################################################
551 sub getdblist{
552 #################################################################################
553 # getdblist: Gets the list of available databases.                              #
554 #                                                                               #
555 # Usage:                                                                        #
556 #                                                                               #
557 # $dbmodule->getdblist();                                                       #
558 #################################################################################
560         # Get the list of databases.
562         my @data_directory;
563         my @data_directory_final;
564         my $database;
565         my $database_filename_length;
566         my $database_filename_friendly;
568         # Check if the database directory has valid permission settings.
570         if (-e $options{"Directory"}){
572                 # The database directory does exist. So check if
573                 # the permission settings are valid.
575                 if (-r $options{"Directory"}){
577                         # The permission settings for reading the directory
578                         # are valid.
580                 } else {
582                         # The permission settings for reading the directory
583                         # are invalid so return an error value.
585                         $error = "DataDirInvalidPermissions";
586                         return;
588                 }
590         } else {
592                 # The database directory does not exist, so return an
593                 # error value.
595                 $error = "DataDirMissing";
596                 return;
598         }
600         opendir(DATADIR, $options{"Directory"});
601         @data_directory = grep /m*\.db.sqlite$/, readdir(DATADIR);
602         closedir(DATADIR);
604         # Process the list of databases.
606         foreach $database (@data_directory){
608                 $database =~ s/.db.sqlite$//g;
609                 $database_filename_friendly = $database;
611                 #$database_filename_length = length($database);
612                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 3);
613                 push(@data_directory_final, $database_filename_friendly);
615         }
617         # Return the list of databases.
619         return @data_directory_final;
623 sub getdatabaseinfo{
624 #################################################################################
625 # getdatabaseinfo: Get information about the database.                          #
626 #                                                                               #
627 # Usage:                                                                        #
628 #                                                                               #
629 # $dbmodule->getdatabaseinfo();                                                 #
630 #################################################################################
632         # Get the database information.
634         my $class = shift;
635         my ($databaseinfo, %databaseinfo);
636         my ($sqldata, @sqldata);
638         $error = "";
639         $errorext = "";
641         $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
642                 $error = "DatabaseError", $errorext = $database_handle->errstr, return
643         );
644         $string_handle->execute();
646         @sqldata = $string_handle->fetchrow_array();
648         # Process the database information into a hash.
650         %databaseinfo = (
651                 "DatabaseName"  => $sqldata[0],
652                 "Description"   => $sqldata[1],
653                 "Notes"         => $sqldata[2],
654                 "Categories"    => $sqldata[3],
655                 "Major"         => $sqldata[4],
656                 "Minor"         => $sqldata[5],
657                 "Revision"      => $sqldata[6]
658         );
660         $string_handle->finish();
661         undef $string_handle;
663         return %databaseinfo;
667 sub getseconddatabaseinfo{
668 #################################################################################
669 # getseconddatabaseinfo: Get information about the database that pages will be  #
670 # moved or copied to.                                                           #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getseconddatabaseinfo();                                           #
675 #################################################################################
677         # Get the database information.
679         my $class = shift;
680         my ($databaseinfo, %databaseinfo);
681         my ($sqldata, @sqldata);
683         $error = "";
684         $errorext = "";
686         $second_string_handle = $second_database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM kiriwrite_database_info LIMIT 1') or (
687                 $error = "DatabaseError", $errorext = $second_database_handle->errstr, return
688         );
689         $second_string_handle->execute();
691         @sqldata = $second_string_handle->fetchrow_array();
693         # Process the database information into a hash.
695         %databaseinfo = (
696                 "DatabaseName"  => $sqldata[0],
697                 "Description"   => $sqldata[1],
698                 "Notes"         => $sqldata[2],
699                 "Categories"    => $sqldata[3],
700                 "Major"         => $sqldata[4],
701                 "Minor"         => $sqldata[5],
702                 "Revision"      => $sqldata[6]
703         );
705         $second_string_handle->finish();
706         undef $second_string_handle;
708         return %databaseinfo;
712 sub adddatabase{
713 #################################################################################
714 # adddatabase: Adds a Kiriwrite database.                                       #
715 #                                                                               #
716 # Usage:                                                                        #
717 #                                                                               #
718 # $dbmodule->adddatabase(options);                                              #
719 #                                                                               #
720 # options       Specifies the following options in any order.                   #
721 #                                                                               #
722 # DatabaseFilename      Specifies the database file/shortname to use.           #
723 # DatabaseName          Specifies the database name to use.                     #
724 # DatabaseDescription   Specifies the database description to use.              #
725 # DatabaseNotes         Specifies the database notes to use.                    #
726 # DatabaseCategories    Specifies the database categories to use.               #
727 # VersionMajor          Specifies the major version.                            #
728 # VersionMinor          Specifies the minor version.                            #
729 # VersionRevision       Specifies the revision version.                         #
730 #################################################################################
732         # Get the database that was passed to the subroutine.
734         $error  = "";
735         $errorext = "";
737         my $class       = shift;
738         my ($passedoptions) = @_;
740         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
741         my $dbname              = $passedoptions->{"DatabaseName"};
742         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
743         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
744         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
745         my $dbmajorver          = $passedoptions->{"VersionMajor"};
746         my $dbminorver          = $passedoptions->{"VersionMinor"};
747         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
749         # Check if the database with the filename given already exists.
751         my $database_exists     = $class->dbexists($dbfilename);
753         if ($database_exists eq 0){
755                 # The database filename exists so set the error value.
757                 $error = "DatabaseExists";
758                 return;
760         }
762         # Create the database structure.
764         $database_handle        = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbfilename . ".db.sqlite");
765         $database_handle->{unicode} = 1;
766         $string_handle          = $database_handle->prepare('CREATE TABLE kiriwrite_database_info(
767                         name varchar(256) primary key, 
768                         description varchar(512), 
769                         notes text, 
770                         categories varchar(512), 
771                         kiriwrite_version_major int(4), 
772                         kiriwrite_version_minor int(4), 
773                         kiriwrite_version_revision int(4)
774         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
775         $string_handle->execute();
777         $string_handle  = $database_handle->prepare('CREATE TABLE kiriwrite_database_pages(
778                         filename varchar(256) primary key, 
779                         pagename varchar(512), 
780                         pagedescription varchar(512), 
781                         pagesection varchar(256),
782                         pagetemplate varchar(64),
783                         pagedata text,
784                         pagesettings int(1),
785                         lastmodified datetime
786         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
787         $string_handle->execute();
789         # Convert the values into SQL query formatted values and add an entry
790         # to the kiriwrite_database_info table.
792         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
793                 \'' . $class->convert($dbname) . '\',
794                 \'' . $class->convert($dbdescription) . '\',
795                 \'' . $class->convert($dbnotes) . '\',
796                 \'' . $class->convert($dbcategories) . '\',
797                 \'' . $class->convert($dbmajorver) . '\',
798                 \'' . $class->convert($dbminorver) . '\',
799                 \'' . $class->convert($dbrevisionver) . '\'
800         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
801         $string_handle->execute();
805 sub editdatabase{
806 #################################################################################
807 # editdatabase: Edits a Kiriwrite Database.                                     #
808 #                                                                               #
809 # Usage:                                                                        #
810 #                                                                               #
811 # $dbmodule->editdatabase(options);                                             #
812 #                                                                               #
813 # options               Specifies the following options in any order.           #
814 #                                                                               #
815 # NewDatabaseFilename   Specifies the new database filename to use.             #
816 # DatabaseName          Specifies the new database name.                        #
817 # DatabaseDescription   Specifies the new database description.                 #
818 # DatabaseNotes         Specifies the new database notes.                       #
819 # DatabaseCategories    Specifies the new database categories.                  #
820 #################################################################################
822         $error          = "";
823         $errorext       = "";
825         my $class       = shift;
826         my ($passedoptions) = @_;
828         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
829         my $dbname              = $passedoptions->{"DatabaseName"};
830         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
831         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
832         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
834         # Check if a new database filename has been specified and if a
835         # new database filename has been specified then change the
836         # database filename.
838         if ($database_filename ne $dbnewfilename){
840                 # A new database filename has been given so check if the output
841                 # directory has write access.
843                 if (-r $options{"Directory"}){
844                         
845                         # The directory is readable.
847                 } else {
849                         # The directory is not readable so set the error value.
851                         $error = "DataDirInvalidPermissions";
853                         return;
855                 }
857                 if (-w $options{"Directory"}){
859                         # The directory is writeable.
861                 } else {
863                         # The directory is not writeable so set the error value.
865                         $error = "DataDirInvalidPermissions";
867                         return;
869                 }
871                 # Check if a database filename already exists before using the
872                 # new filename.
874                 my $database_newexists          = $class->dbexists($dbnewfilename);
876                 if ($database_newexists eq 0){
878                         # The database filename exists so set the error value.
880                         $error = "DatabaseExists";
881                         return;
883                 }
885                 # Check if the database can be renamed (has write access).
887                 my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
889                 if ($database_permissions eq 1){
891                         # The database filename exists so set the error value.
893                         $error = "InvalidPermissionsSet";
894                         return;
896                 }
898                 # "Disconnect" from the database.
900                 $database_handle->disconnect();
902                 # Rename the database.
904                 ($database_filename)    = $database_filename =~ /^([a-zA-Z0-9.]+)$/;
905                 ($dbnewfilename)        = $dbnewfilename =~ /^([a-zA-Z0-9.]+)$/;
907                 rename($options{"Directory"} . '/' . $database_filename . '.db.sqlite', $options{"Directory"} . '/' . $dbnewfilename . '.db.sqlite');
909                 # Reload the database from the new filename.
911                 $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbnewfilename . ".db.sqlite");
912                 $database_handle->{unicode} = 1;
913                 $database_filename = $dbnewfilename;
915         }
917         # Check if the database can be altered with the new data.
919         my $database_permissions        = $class->dbpermissions($database_filename, 1, 1);
921         if ($database_permissions eq 1){
923                 # The database filename exists so set the error value.
925                 $error = "InvalidPermissionsSet";
926                 return;
928         }
930         # Get the current database information.
932         $string_handle = $database_handle->prepare('SELECT name FROM kiriwrite_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
933         $string_handle->execute();
935         my @database_oldinfo    = $string_handle->fetchrow_array();
937         my $dboldname           = $database_oldinfo[0];
939         # Update the database information.
941         $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_info SET name = \'' . $class->convert($dbname) . '\',
942         description = \'' . $class->convert($dbdescription) . '\',
943         notes = \'' . $class->convert($dbnotes) . '\',
944         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
945         $string_handle->execute();
947         undef $string_handle;
948         return;
952 sub deletedatabase{
953 #################################################################################
954 # deletedatabase: Deletes a Kiriwrite database.                                 #
955 #                                                                               #
956 # Usage:                                                                        #
957 #                                                                               #
958 # $dbmodule->deletedatabase(options);                                           #
959 #                                                                               #
960 # options       Specifies the following options in any order.                   #
961 #                                                                               #
962 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
963 #################################################################################
965         $error          = "";
966         $errorext       = "";
968         # Get the database filename.
970         my $class               = shift;
971         my ($passedoptions)     = shift;
973         my $databasename        = $passedoptions->{"DatabaseName"};
975         # Check if the database exists.
977         my $database_exists             = $class->dbexists($databasename);
979         if ($database_exists eq 1){
981                 # The database does not exist so set the error value.
983                 $error = "DoesNotExist";
984                 return;
986         }
988         # Check if the database permissions are valid.
990         my $database_permissions        = $class->dbpermissions($databasename);
992         if ($database_permissions eq 1){
994                 # The database permissions are invalid so set the error
995                 # value.
997                 $error = "InvalidPermissionsSet";
998                 return;
1000         }
1002         # Delete the database.
1004         ($databasename) = $databasename =~ /^([a-zA-Z0-9.]+)$/;
1006         unlink($options{"Directory"} . '/' . $databasename . '.db.sqlite');
1010 sub selectdb{
1011 #################################################################################
1012 # selectdb: Selects the Kiriwrite database.                                     #
1013 #                                                                               #
1014 # Usage:                                                                        #
1015 #                                                                               #
1016 # $dbmodule->connect(options);                                                  #
1017 #                                                                               #
1018 # options       Specifies the following options in any order.                   #
1019 #                                                                               #
1020 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1021 #################################################################################
1023         # Get the database name.
1025         $error = "";
1026         $errorext = "";
1028         my $class = shift;
1029         my ($passedoptions) = @_;
1030         my (%database, $database);
1032         my $dbname = $passedoptions->{"DatabaseName"};
1034         # Check if the database exists.
1036         my $database_exists = $class->dbexists($dbname);
1038         if ($database_exists eq 1){
1040                 # The database does not exist so return an error value
1041                 # saying that the database does not exist.
1043                 $error = "DoesNotExist";
1045                 return;
1047         }
1049         # Check if the database has valid permissions set.
1051         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1053         if ($database_permissions eq 1){
1055                 # The database has invalid permissions set so return
1056                 # an error value saying that the database has invalid
1057                 # permissions set.
1059                 $error = "InvalidPermissionsSet";
1060                 
1061                 return;
1063         }
1065         # Connect to the database.
1067         $database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1068         $database_handle->{unicode} = 1;
1069         $database_filename = $dbname;
1073 sub selectseconddb{
1074 #################################################################################
1075 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
1076 # pages to.                                                                     #
1077 #                                                                               #
1078 # Usage:                                                                        #
1079 #                                                                               #
1080 # $dbmodule->selectseconddb(options);                                           #
1081 #                                                                               #
1082 # options       Specifies the following options in any order.                   #
1083 #                                                                               #
1084 # DatabaseName  Specifies the Kiriwrite database to use.                        #
1085 #################################################################################
1087         # Get the database name.
1089         $error = "";
1090         $errorext = "";
1092         my $class = shift;
1093         my ($passedoptions) = @_;
1094         my (%database, $database);
1096         my $dbname = $passedoptions->{"DatabaseName"};
1098         # Check if the database exists.
1100         my $database_exists = $class->dbexists($dbname);
1102         if ($database_exists eq 1){
1104                 # The database does not exist so return an error value
1105                 # saying that the database does not exist.
1107                 $error = "DoesNotExist";
1109                 return;
1111         }
1113         # Check if the database has valid permissions set.
1115         my $database_permissions = $class->dbpermissions($dbname, 1, 0);
1117         if ($database_permissions eq 1){
1119                 # The database has invalid permissions set so return
1120                 # an error value saying that the database has invalid
1121                 # permissions set.
1123                 $error = "InvalidPermissionsSet";
1124                 
1125                 return;
1127         }
1129         # Connect to the database.
1131         $second_database_handle = DBI->connect("dbi:SQLite:dbname=" . $options{"Directory"} . '/' . $dbname . ".db.sqlite");
1132         $second_database_handle->{unicode} = 1;
1133         $second_database_filename = $dbname;    
1137 #################################################################################
1138 # Page subroutines.                                                             #
1139 #################################################################################
1141 sub getpagelist{
1142 #################################################################################
1143 # getpagelist: Gets the list of pages from the database.                        #
1144 #                                                                               #
1145 # Usage:                                                                        #
1146 #                                                                               #
1147 # $dbmodule->getpagelist();                                                     #
1148 #################################################################################
1150         $error = "";
1151         $errorext = "";
1153         my $class       = shift;
1154         
1155         $string_handle  = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1156         $string_handle->execute();
1158         my @database_pagefilenames;
1159         my @database_pagefilenames_final;
1161         # Process the collected pages.
1163         while (@database_pagefilenames = $string_handle->fetchrow_array){
1165                 # Add each page to the list of pages in the database.
1167                 push(@database_pagefilenames_final, $database_pagefilenames[0]);
1169         }
1171         undef $string_handle;
1172         return @database_pagefilenames_final;
1176 sub getpageinfo{
1177 #################################################################################
1178 # getpageinfo: Gets the page information from the filename passed.              #
1179 #                                                                               #
1180 # Usage:                                                                        #
1181 #                                                                               #
1182 # $dbmodule->getpageinfo(options);                                              #
1183 #                                                                               #
1184 # options       Specifies the following options in any order.                   #
1185 #                                                                               #
1186 # PageFilename  Specifies the page filename to get the page information from.   #
1187 # Reduced       Specifies if the reduced version of the page information should #
1188 #               be retrieved.                                                   #
1189 #################################################################################
1191         $error = "";
1192         $errorext = "";
1194         my $class               = shift;
1195         my ($passedoptions)     = shift;
1196         my (%database_page, $database_page);
1197         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1199         my @data_page;
1200         my $page_found = 0;
1202         # Get the page from the database.
1204         my $page_filename       = $passedoptions->{"PageFilename"};
1205         my $page_reduced        = $passedoptions->{"Reduced"};
1207         if ($page_reduced eq 1){
1209                 $string_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 );
1211                 $string_handle->execute();
1213                 # Check if the page exists in the database.
1215                 while (@data_page = $string_handle->fetchrow_array()){
1216         
1217                         # Get the values from the array.
1218         
1219                         $pagefilename           = $data_page[0];
1220                         $pagename               = $data_page[1];
1221                         $pagedescription        = $data_page[2];
1222                         $pagelastmodified       = $data_page[3];
1223         
1224                         # Put the values into the page hash.
1225         
1226                         %database_page = (
1227                                 "PageFilename"          => $pagefilename,
1228                                 "PageName"              => $pagename,
1229                                 "PageDescription"       => $pagedescription,
1230                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1231                         );
1232         
1233                         $page_found = 1;
1234         
1235                 }
1238         } else {
1239                 
1240                 $string_handle  = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1242                 $string_handle->execute();
1244                 # Check if the page exists in the database.
1246                 while (@data_page = $string_handle->fetchrow_array()){
1247         
1248                         # Get the values from the array.
1249         
1250                         $pagefilename           = $data_page[0];
1251                         $pagename               = $data_page[1];
1252                         $pagedescription        = $data_page[2];
1253                         $pagesection            = $data_page[3];
1254                         $pagetemplate           = $data_page[4];
1255                         $pagedata               = $data_page[5];
1256                         $pagesettings           = $data_page[6];
1257                         $pagelastmodified       = $data_page[7];
1258         
1259                         # Put the values into the page hash.
1260         
1261                         %database_page = (
1262                                 "PageFilename"          => $pagefilename,
1263                                 "PageName"              => $pagename,
1264                                 "PageDescription"       => $pagedescription,
1265                                 "PageSection"           => $pagesection,
1266                                 "PageTemplate"          => $pagetemplate,
1267                                 "PageContent"           => $pagedata,
1268                                 "PageSettings"          => $pagesettings,
1269                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1270                         );
1271         
1272                         $page_found = 1;
1273         
1274                 }
1276         }
1277         
1278         # Check if the page did exist.
1280         if (!$page_found){
1282                 $error = "PageDoesNotExist";
1283                 return;
1285         }
1287         undef $string_handle;
1288         return %database_page;
1292 sub addpage{
1293 #################################################################################
1294 # addpage: Add a page to the selected database.                                 #
1295 #                                                                               #
1296 # Usage:                                                                        #
1297 #                                                                               #
1298 # $dbmodule->addpage(options);                                                  #
1299 #                                                                               #
1300 # options       Specifies the following options in any order.                   #
1301 #                                                                               #
1302 # PageFilename          Specifies the page filename to use.                     #
1303 # PageName              Specifies the page name to use.                         #
1304 # PageDescription       Specifies the page description to use.                  #
1305 # PageSection           Specifies the page section to use.                      #
1306 # PageTemplate          Specifies the page template to use.                     #
1307 # PageContent           Specifies the page content to use.                      #
1308 # PageSettings          Specifies the page settings to use.                     #
1309 #################################################################################
1311         # Get the data that was passed to the subroutine.
1313         $error = "";
1314         $errorext = "";
1316         my $class               = shift;
1317         my ($passedoptions)     = shift;
1319         my @database_page;
1320         my $page_count = 0;
1322         # Get the values passed to the hash.
1324         my $page_filename       = $passedoptions->{"PageFilename"};
1325         my $page_name           = $passedoptions->{"PageName"};
1326         my $page_description    = $passedoptions->{"PageDescription"};
1327         my $page_section        = $passedoptions->{"PageSection"};
1328         my $page_template       = $passedoptions->{"PageTemplate"};
1329         my $page_content        = $passedoptions->{"PageContent"};
1330         my $page_settings       = $passedoptions->{"PageSettings"};
1332         # Check to see if the filename given already exists
1333         # in the page database.
1335         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1336         $string_handle->execute();
1338         # Check if a page with the filename given really does
1339         # exist.
1341         while (@database_page = $string_handle->fetchrow_array()){
1343                 # A page does exist so increment the count to 1.
1345                 $page_count++;
1346                 
1347         }
1349         if ($page_count ne 0){
1351                 # The page does exist so set the error value.
1353                 $error = "PageExists";
1354                 return;
1356         }
1358         # Check if certain values are undefined.
1360         if (!$page_name){
1362                 $page_name = "";
1364         }
1366         if (!$page_description){
1368                 $page_description = "";
1370         }
1372         if (!$page_section){
1374                 $page_section = "";
1376         }
1378         if (!$page_content){
1380                 $page_content = "";
1382         }
1384         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1385         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1387         # Add the page to the selected database.
1389         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1390                 \'' . $class->convert($page_filename) . '\',
1391                 \'' . $class->convert($page_name) . '\',
1392                 \'' . $class->convert($page_description) . '\',
1393                 \'' . $class->convert($page_section) . '\',
1394                 \'' . $class->convert($page_template) . '\',
1395                 \'' . $class->convert($page_content) . '\',
1396                 \'' . $class->convert($page_settings) . '\',
1397                 \'' . $class->convert($page_date) . '\'
1398         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1399         $string_handle->execute();
1401         undef $string_handle;
1405 sub deletepage{
1406 #################################################################################
1407 # deletepage: Delete a page from the selected database.                         #
1408 #                                                                               #
1409 # Usage:                                                                        #
1410 #                                                                               #
1411 # $dbmodule->deletepage(options)                                                #
1412 #                                                                               #
1413 # options       Specifies the following options in any order.                   #
1414 #                                                                               #
1415 # PageFilename  Specifies the page filename to delete.                          #
1416 #################################################################################
1418         $error = "";
1419         $errorext = "";
1421         # Get the data that was passed to the subroutine.
1423         my $class = shift;
1424         my ($passedoptions) = @_;
1425         my @page_info;
1426         my $page_found = 0;
1428         # Get the page filename.
1430         my $page_filename = $passedoptions->{"PageFilename"};
1432         # Check if the page exists before deleting it.
1434         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1435         $string_handle->execute();
1437         while (@page_info = $string_handle->fetchrow_array()){
1439                 $page_found = 1;
1441         }
1443         # Check if the page really does exist.
1445         if (!$page_found){
1447                 $error = "PageDoesNotExist";
1448                 return;
1450         }
1452         # Delete the page.
1454         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1455         $string_handle->execute();
1459 sub editpage{
1460 #################################################################################
1461 # editpage: Edit a page from the selected database.                             #
1462 #                                                                               #
1463 # Usage:                                                                        #
1464 #                                                                               #
1465 # $dbmodule->editpage(options);                                                 #
1466 #                                                                               #
1467 # options       Specifies the following options in any order.                   #
1468 #                                                                               #
1469 # PageFilename          Specifies the filename to edit.                         #
1470 # PageNewFilename       Specifies the new filename to use.                      #
1471 # PageNewName           Specifies the new page name to use.                     #
1472 # PageNewDescription    Specifies the new page description to use.              #
1473 # PageNewSection        Specifies the new page section to use.                  #
1474 # PageNewTemplate       Specifies the new page template to use.                 #
1475 # PageNewContent        Specifies the new page content to use.                  #
1476 # PageNewSettings       Specifies the new page settings to use.                 #
1477 #################################################################################
1479         $error = "";
1480         $errorext = "";
1482         # Get the values passed to the subroutine.
1484         my $class = shift;
1485         my ($passedoptions) = @_;
1486         my $page_found = 0;
1487         my @page_info;
1489         # Get the data that was passed to the subroutine.
1491         my $page_filename       = $passedoptions->{"PageFilename"};
1492         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1493         my $page_newname        = $passedoptions->{"PageNewName"};
1494         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1495         my $page_newsection     = $passedoptions->{"PageNewSection"};
1496         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1497         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1498         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1500         # Check if the page with the filename given exists.
1502         $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1503         $string_handle->execute();
1505         # Check if the page really does exist.
1507         while (@page_info = $string_handle->fetchrow_array()){
1509                 # The page information is found.
1511                 $page_found = 1;
1513         }
1515         # Check if the page really does exist.
1517         if (!$page_found){
1519                 $error = "PageDoesNotExist";
1520                 return;
1522         }
1524         # Check if there is a page that already exists with the new
1525         # filename.
1527         if ($page_filename ne $page_newfilename){
1529                 $string_handle = $database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1530                 $string_handle->execute();
1532                 # Check if a page really is using the new filename.
1534                 while (@page_info = $string_handle->fetchrow_array()){
1536                         # The page information is found.
1538                         $page_found = 1;
1540                 }
1542                 if ($page_found eq 1){
1544                         $error = "PageExists";
1545                         return;
1547                 }
1549         }
1551         # Get the current date.
1553         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1554         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1556         # Edit the selected page.
1558         $string_handle = $database_handle->prepare('UPDATE kiriwrite_database_pages SET filename = \'' . $class->convert($page_newfilename) . '\', pagename = \'' . $class->convert($page_newname) . '\', pagedescription = \'' . $class->convert($page_newdescription) . '\', pagesection = \'' . $class->convert($page_newsection) . '\', pagetemplate = \'' . $class->convert($page_newtemplate) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagedata = \'' . $class->convert($page_newcontent) . '\', pagesettings = \'' . $class->convert($page_newsettings) . '\', lastmodified = \'' . $page_date . '\' WHERE filename = \'' . $class->convert($page_filename) . '\'')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1559         $string_handle->execute();
1563 sub movepage{
1564 #################################################################################
1565 # movepage: Moves a page from the old database to the new database.             #
1566 #                                                                               #
1567 # Usage:                                                                        #
1568 #                                                                               #
1569 # $dbmodule->movepage(options);                                                 #
1570 #                                                                               #
1571 # options       Specifies the following options in any order.                   #
1572 #                                                                               #
1573 # PageFilename  Specifies the page with the filename to move.                   #
1574 #################################################################################
1576         $error = "";
1577         $errorext = "";
1579         # Get the values passed to the subroutine.
1581         my $class = shift;
1582         my ($passedoptions) = @_;
1584         my (%database_page, $database_page);
1585         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1586         my @page_info;
1587         my $page_found = 0;
1589         # Get the data that was passed to the subroutine.
1591         my $page_filename = $passedoptions->{"PageFilename"};
1593         # Check if the page with the filename given exists.
1595         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1596         $string_handle->execute();
1598         # Check if the page really does exist.
1600         while (@page_info = $string_handle->fetchrow_array()){
1602                 # Get the values from the array.
1604                 $pagefilename           = $page_info[0];
1605                 $pagename               = $page_info[1];
1606                 $pagedescription        = $page_info[2];
1607                 $pagesection            = $page_info[3];
1608                 $pagetemplate           = $page_info[4];
1609                 $pagedata               = $page_info[5];
1610                 $pagesettings           = $page_info[6];
1611                 $pagelastmodified       = $page_info[7];
1613                 # Put the values into the page hash.
1615                 %database_page = (
1616                         "PageFilename"          => $pagefilename,
1617                         "PageName"              => $pagename,
1618                         "PageDescription"       => $pagedescription,
1619                         "PageSection"           => $pagesection,
1620                         "PageTemplate"          => $pagetemplate,
1621                         "PageContent"           => $pagedata,
1622                         "PageSettings"          => $pagesettings,
1623                         "PageLastModified"      => $pagelastmodified,
1624                 );
1626                 # The page information is found.
1628                 $page_found = 1;
1630         }
1632         # Check if the page really does exist.
1634         if (!$page_found){
1636                 $error = "PageDoesNotExist";
1637                 return;
1639         }
1641         # Check if the page with the filename given already exists in
1642         # the database the page is being moved to.
1644         $page_found = 0;
1645         @page_info = ();
1647         $second_string_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1648         $second_string_handle->execute();
1650         while (@page_info = $second_string_handle->fetchrow_array()){
1652                 $page_found = 1;
1654         }
1655         
1656         # Check if the page really does exist.
1658         if ($page_found){
1660                 $error = "PageAlreadyExists";
1661                 return;
1663         }
1665         # Add the page to the new database.
1667         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1668                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1669                 \'' . $class->convert($database_page{"PageName"}) . '\',
1670                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1671                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1672                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1673                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1674                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1675                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1676         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1677         $second_string_handle->execute();
1679         # Delete the page from the old database.
1681         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1682         $string_handle->execute();
1686 sub copypage{
1687 #################################################################################
1688 # copypage: Copies a page from the old database to the new database.            #
1689 #                                                                               #
1690 # Usage:                                                                        #
1691 #                                                                               #
1692 # $dbmodule->copypage(options);                                                 #
1693 #                                                                               #
1694 # options       Specifies the following options in any order.                   #
1695 #                                                                               #
1696 # PageFilename  Specifies the page with the filename to copy.                   #
1697 #################################################################################
1699         $error = "";
1700         $errorext = "";
1702         # Get the values passed to the subroutine.
1704         my $class = shift;
1705         my ($passedoptions) = @_;
1707         my (%database_page, $database_page);
1708         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1709         my @page_info;
1710         my $page_found = 0;
1712         # Get the data that was passed to the subroutine.
1714         my $page_filename = $passedoptions->{"PageFilename"};
1716         # Check if the page with the filename given exists.
1718         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1719         $string_handle->execute();
1721         # Check if the page really does exist.
1723         while (@page_info = $string_handle->fetchrow_array()){
1725                 # Get the values from the array.
1727                 $pagefilename           = $page_info[0];
1728                 $pagename               = $page_info[1];
1729                 $pagedescription        = $page_info[2];
1730                 $pagesection            = $page_info[3];
1731                 $pagetemplate           = $page_info[4];
1732                 $pagedata               = $page_info[5];
1733                 $pagesettings           = $page_info[6];
1734                 $pagelastmodified       = $page_info[7];
1736                 # Put the values into the page hash.
1738                 %database_page = (
1739                         "PageFilename"          => $pagefilename,
1740                         "PageName"              => $pagename,
1741                         "PageDescription"       => $pagedescription,
1742                         "PageSection"           => $pagesection,
1743                         "PageTemplate"          => $pagetemplate,
1744                         "PageContent"           => $pagedata,
1745                         "PageSettings"          => $pagesettings,
1746                         "PageLastModified"      => $pagelastmodified,
1747                 );
1749                 # The page information is found.
1751                 $page_found = 1;
1753         }
1755         # Check if the page really does exist.
1757         if (!$page_found){
1759                 $error = "PageDoesNotExist";
1760                 return;
1762         }
1764         # Check if the page with the filename given already exists in
1765         # the database the page is being moved to.
1767         $page_found = 0;
1768         @page_info = ();
1770         $second_string_handle = $second_database_handle->prepare('SELECT filename FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1771         $second_string_handle->execute();
1773         while (@page_info = $second_string_handle->fetchrow_array()){
1775                 $page_found = 1;
1777         }
1778         
1779         # Check if the page really does exist.
1781         if ($page_found){
1783                 $error = "PageAlreadyExists";
1784                 return;
1786         }
1788         # Add the page to the new database.
1790         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1791                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1792                 \'' . $class->convert($database_page{"PageName"}) . '\',
1793                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1794                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1795                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1796                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1797                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1798                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1799         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1800         $second_string_handle->execute();
1804 #################################################################################
1805 # Filter subroutines.                                                           #
1806 #################################################################################
1808 sub connectfilter{
1809 #################################################################################
1810 # connectfilter: Connect to the filter database.                                #
1811 #                                                                               #
1812 # Usage:                                                                        #
1813 #                                                                               #
1814 # $dbmodule->connectfilter(missingignore);                                      #
1815 #                                                                               #
1816 # missingignore Ignore error about database being missing.                      #
1817 #################################################################################
1819         $error = "";
1820         $errorext = "";
1822         my $class = shift;
1823         my $ignoremissing = shift;
1825         # Check if the template database exists.
1827         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1828         
1829         if ($filterdatabase_exists eq 1){
1831                 $filterdb_exists = 0;
1833                 if (!$ignoremissing){
1835                         $error = "FilterDatabaseDoesNotExist";
1836                         return;
1838                 }
1840         }
1842         # Check if the permission settings for the template database are valid.
1844         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1846         if ($filterdb_permissions eq 1){
1848                 # The template database has invalid permissions set
1849                 # so return an error value.
1851                 if (!$ignoremissing){
1853                         $error = "FilterDatabaseInvalidPermissionsSet";
1854                         return;
1856                 }
1858         }
1860         # Connect to the template database.
1862         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1863         $filterdb_database_handle->{unicode} = 1;
1864         $filterdb_loaded = 1;
1868 sub disconnectfilter{
1869 #################################################################################
1870 # disconnectfilter: Disconnect from the filter database.                        #
1871 #                                                                               #
1872 # Usage:                                                                        #
1873 #                                                                               #
1874 # $dbmodule->disconnectfilter();                                                #
1875 #################################################################################
1877         # Disconnect the template database.
1879         if ($filterdb_loaded eq 1){
1881                 undef $filterdb_string_handle;
1882                 $filterdb_database_handle->disconnect();
1884         }
1888 sub getfilterlist{
1889 #################################################################################
1890 # getfilterlist: Gets the list of filters in the filter database.               #
1891 #                                                                               #
1892 # Usage:                                                                        #
1893 #                                                                               #
1894 # $dbmodule->getfilterlist();                                                   #
1895 #################################################################################
1897         $error = "";
1898         $errorext = "";
1900         my @filter_list;
1901         my @filter_data;
1903         # Get the list of filters available.
1905         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1906         $filterdb_string_handle->execute();
1908         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1910                 # Add the filter to the list of available filters.
1912                 push(@filter_list, $filter_data[0]);
1914         }
1916         undef $filterdb_string_handle;
1917         return @filter_list;
1921 sub getfilterinfo{
1922 #################################################################################
1923 # getfilterinfo: Gets information about the filter.                             #
1924 #                                                                               #
1925 # Usage:                                                                        #
1926 #                                                                               #
1927 # $dbmodule->getfilterinfo(options);                                            #
1928 #                                                                               #
1929 # options       Specifies the following options in any order.                   #
1930 #                                                                               #
1931 # FilterID      Specifies the filter ID number to get information from.         #
1932 #################################################################################
1934         $error = "";
1935         $errorext = "";
1937         # Get the values passed to the subroutine.
1939         my $class               = shift;
1940         my ($passedoptions)     = @_;
1942         my %filter_info;
1943         my $filter_exists       = 0;
1944         my @filter_data;
1946         # Get the values that are in the hash.
1948         my $filter_id           = $passedoptions->{"FilterID"};
1950         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
1951         $filterdb_string_handle->execute();
1953         # Get the filter information.
1955         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1957                 $filter_info{"FilterID"}        = $filter_data[0];
1958                 $filter_info{"FilterPriority"}  = $filter_data[1];
1959                 $filter_info{"FilterFind"}      = $filter_data[2];
1960                 $filter_info{"FilterReplace"}   = $filter_data[3];
1961                 $filter_info{"FilterNotes"}     = $filter_data[4];
1963                 $filter_exists = 1;
1965         }
1967         # Check if the filter exists.
1969         if (!$filter_exists){
1971                 # The filter does not exist so return
1972                 # an error value.
1974                 $error = "FilterDoesNotExist";
1975                 return;
1977         }
1979         # Return the filter information.
1981         undef $filterdb_string_handle;
1982         return %filter_info;
1986 sub addfilter{
1987 #################################################################################
1988 # addfilter: Adds a filter to the filter database.                              #
1989 #                                                                               #
1990 # Usage:                                                                        #
1991 #                                                                               #
1992 # $dbmodule->addfilter(options);                                                #
1993 #                                                                               #
1994 # options       Specifies the following options in any order.                   #
1995 #                                                                               #
1996 # FindFilter    Specifies the find filter to add.                               #
1997 # ReplaceFilter Specifies the replace filter to add.                            #
1998 # Priority      Specifies the filter priority to use.                           #
1999 # Notes         Specifies the notes to use.                                     #
2000 #################################################################################
2002         $error = "";
2003         $errorext = "";
2005         # Get the values passed to the subroutine.
2007         my $class = shift;
2008         my ($passedoptions) = @_;
2010         # Define some variables for later.
2012         my @database_filters;
2013         my @filterid_list;
2014         my @filterid_check;
2015         my $nofiltertable = 0;
2016         my $filter_found = 0;
2017         my $filter_count = 0;
2018         my $filter_id;
2019         my $new_id;
2021         # Get the values from the hash.
2023         my $filter_find         = $passedoptions->{"FindFilter"};
2024         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2025         my $filter_priority     = $passedoptions->{"Priority"};
2026         my $filter_notes        = $passedoptions->{"Notes"};
2028         # Check if the filter database permissions are valid.
2030         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2031         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2033         if ($filterdb_permissions eq 1){
2035                 if ($filterdb_exists eq 0){
2036                         $error = "FilterDatabaseInvalidPermissionsSet";
2037                         return;
2038                 }
2040         }
2042         # Check if certain values are undefined and if they
2043         # are then set them blank.
2045         if (!$filter_find){
2047                 $filter_find = "";
2049         }
2051         if (!$filter_replace){
2053                 $filter_replace = "";
2055         }
2057         if (!$filter_priority){
2059                 $filter_priority = 1;
2061         }
2063         if (!$filter_notes){
2065                 $filter_notes = "";
2067         }
2069         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2071         if ($directory_permissions eq 1 && $filterdb_exists){
2073                 # The template database cannot be created because of invalid directory
2074                 # permissions so return an error value.
2076                 $error = "FilterDatabaseFileUncreateable";
2077                 return; 
2079         }
2081         # Check if the filter table exists.
2083         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2085         # Check if there is really no filter table.
2087         if ($nofiltertable){
2089                 # Create the filter database table.
2091                 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2092                         id int(7) primary key,
2093                         priority int(5),
2094                         findsetting varchar(1024),
2095                         replacesetting varchar(1024),
2096                         notes text
2097                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2098                 $filterdb_string_handle->execute();
2100         }
2102         # Find the lowest filter identification number available.
2104         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2105         $filterdb_string_handle->execute();
2107         while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2109                 $filter_id      = $database_filters[0];
2111                 # Add the filter identification to the list of filter IDs.
2113                 push(@filterid_list, $filter_id);
2115         }
2117         $filter_id = "";
2119         # Process each filter looking for a blank available filter.
2121         foreach $filter_id (@filterid_list){
2123                 # Check the next filter ID to see if it's blank.
2125                 $new_id = $filter_id + 1;
2127                 $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2128                 $filterdb_string_handle->execute();
2130                 # Get the filter identification number.
2132                 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2134                         $filter_found = 1;
2136                 }
2138                 # Check if a filter was found.
2140                 if (!$filter_found){
2142                         # No filter was found using this ID so exit the loop.
2144                         last;
2146                 }
2148                 # Increment the filter count and reset the filter found value.
2150                 $filter_count++;
2151                 $filter_found = 0;
2152                 $new_id = 0;
2154         }
2156         # Check if there were any filters in the filters database.
2158         if (!$filter_count && !$new_id){
2160                 # There were no filters in the filters database so set
2161                 # the new filter identification value to 1.
2163                 $new_id = 1;
2165         }
2167         # Add the filter to the filter database.
2169         $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2170                 \'' . $class->convert($new_id) . '\',
2171                 \'' . $class->convert($filter_priority) . '\',
2172                 \'' . $class->convert($filter_find) . '\',
2173                 \'' . $class->convert($filter_replace) .'\',
2174                 \'' . $class->convert($filter_notes) . '\'
2175         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2176         $filterdb_string_handle->execute();
2180 sub editfilter{
2181 #################################################################################
2182 # editfilter: Edits a filter in the filter database.                            #
2183 #                                                                               #
2184 # Usage:                                                                        #
2185 #                                                                               #
2186 # $dbmodule->editfilter(options);                                               #
2187 #                                                                               #
2188 # options       Specifies the following options in any order.                   #
2189 #                                                                               #
2190 # FilterID              Specifies the filter to edit.                           #
2191 # NewFindFilter         Specifies the new find filter setting.                  #
2192 # NewReplaceFilter      Specifies the new replace filter setting.               #
2193 # NewFilterPriority     Specifies the new filter priority setting.              #
2194 # NewFilterNotes        Specifies the new notes for the filter.                 #
2195 #################################################################################
2197         $error = "";
2198         $errorext = "";
2200         # Get the values passed to the subroutine.
2202         my $class = shift;
2203         my ($passedoptions) = @_;
2205         my @filter_data;
2206         my $filter_exists = 1;
2207         my $blankfile = 0;
2209         # Get the values from the hash.
2211         my $filter_id           = $passedoptions->{"FilterID"};
2212         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2213         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2214         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2215         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2217         # Check if the filter database permissions are valid.
2219         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2220         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2222         if ($filterdb_permissions eq 1){
2224                 if ($filterdb_exists eq 0){
2225                         $error = "FilterDatabaseInvalidPermissionsSet";
2226                         return;
2227                 }
2229         }
2231         # Check if the filter exists before editing it.
2233         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2234         $filterdb_string_handle->execute();
2236         # Check if the filter exists.
2238         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2240                 $filter_exists = 1;
2242         }       
2244         # Check if the filter really does exist.
2246         if (!$filter_exists){
2248                 # The filter does not exist so return
2249                 # an error value.
2251                 $error = "FilterDoesNotExist";
2252                 return;
2254         }
2256         # Edit the selected filter.
2258         $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2259                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2260                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2261                 priority = \'' . $class->convert($filter_newpriority) . '\',
2262                 notes = \'' . $class->convert($filter_newnotes) . '\'
2263         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2264         $filterdb_string_handle->execute();
2266         undef $filterdb_string_handle;
2267         return;
2271 sub deletefilter{
2272 #################################################################################
2273 # deletefilter: Deletes a filter from the filter database.                      #
2274 #                                                                               #
2275 # Usage:                                                                        #
2276 #                                                                               #
2277 # $dbmodule->deletefilter(options);                                             #
2278 #                                                                               #
2279 # options       Specifies the following options in any order.                   #
2280 #                                                                               #
2281 # FilterID      Specifies the filter to delete from the filter database.        #
2282 #################################################################################
2284         $error = "";
2285         $errorext = "";
2287         # Get the values passed to the subroutine.
2289         my $class = shift;
2290         my ($passedoptions) = @_;
2292         my $filter_exists = 0;
2293         my @filter_data;
2295         # Get the values from the hash.
2297         my $filter_id           = $passedoptions->{"FilterID"};
2299         # Check if the filter exists before deleting.
2301         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2302         $filterdb_string_handle->execute();
2304         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2306                 $filter_exists = 1;
2308         }
2310         # Check to see if the filter really does exist.
2312         if (!$filter_exists){
2314                 $error = "FilterDoesNotExist";
2315                 return;
2317         }
2319         # Delete the filter from the filter database.
2321         $filterdb_string_handle = $filterdb_database_handle->prepare('DELETE FROM kiriwrite_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2322         $filterdb_string_handle->execute();
2324         undef $filterdb_string_handle;
2328 #################################################################################
2329 # Template subroutines.                                                         #
2330 #################################################################################
2332 sub connecttemplate{
2333 #################################################################################
2334 # connecttemplate: Connect to the template database.                            #
2335 #                                                                               #
2336 # Usage:                                                                        #
2337 #                                                                               #
2338 # $dbmodule->connecttemplate(missingignore);                                    #
2339 #                                                                               #
2340 # missingignore Ignore errror about database being missing.                     #
2341 #################################################################################
2343         $error = "";
2344         $errorext = "";
2346         my $class = shift;
2347         my $ignoremissing = shift;
2349         # Check if the template database exists.
2351         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2352         
2353         if ($templatedatabase_exists eq 1){
2355                 $templatedb_exists = 0;
2357                 if (!$ignoremissing){
2359                         $error = "TemplateDatabaseDoesNotExist";
2360                         return;
2362                 }
2364         }
2366         # Check if the permission settings for the template database are valid.
2368         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2370         if ($templatedb_permissions eq 1){
2372                 # The template database has invalid permissions set
2373                 # so return an error value.
2375                 if (!$ignoremissing){
2377                         $error = "TemplateDatabaseInvalidPermissionsSet";
2378                         return;
2380                 }
2382         }
2384         # Connect to the template database.
2386         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2387         $template_database_handle->{unicode} = 1;
2388         $templatedb_loaded = 1;
2392 sub disconnecttemplate{
2393 #################################################################################
2394 # disconnecttemplate: Disconnect from the template database.                    #
2395 #                                                                               #
2396 # Usage:                                                                        #
2397 #                                                                               #
2398 # $dbmodule->disconnecttemplate();                                              #
2399 #################################################################################
2401         # Disconnect the template database.
2403         if ($templatedb_loaded eq 1){
2405                 undef $template_string_handle;
2406                 $template_database_handle->disconnect();
2408         }
2412 sub gettemplatelist{
2413 #################################################################################
2414 # gettemplatelist: Gets the list of templates.                                  #
2415 #                                                                               #
2416 # Usage:                                                                        #
2417 #                                                                               #
2418 # $dbmodule->gettemplatelist();                                                 #
2419 #################################################################################
2421         $error = "";
2422         $errorext = "";
2424         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2425         $template_string_handle->execute();
2427         my @database_template;
2428         my @templates_list;
2429         my $template_filename;
2431         while (@database_template = $template_string_handle->fetchrow_array()){
2433                 # Get certain values from the array.
2435                 $template_filename      = $database_template[0];
2437                 # Add the template to the list of templates.
2439                 push(@templates_list, $template_filename);
2441         }
2443         return @templates_list;
2447 sub gettemplateinfo{
2448 #################################################################################
2449 # gettemplateinfo: Get information on a template.                               #
2450 #                                                                               #
2451 # Usage:                                                                        #
2452 #                                                                               #
2453 # $dbmodule->gettemplateinfo(options);                                          #
2454 #                                                                               #
2455 # options       Specifies the following options in any order.                   #
2456 #                                                                               #
2457 # TemplateFilename      Specifies the template filename to use.                 #
2458 #################################################################################
2460         $error = "";
2461         $errorext = "";
2463         # Get the data passed to the subroutine.
2465         my $class = shift;
2466         my ($passedoptions) = @_;
2468         my %page_info;
2469         my @template_data;
2471         my $template_filename;
2472         my $template_name;
2473         my $template_description;
2474         my $template_datemodified;
2475         my $template_layout;
2477         my $template_found = 0;
2479         my $filename    = $passedoptions->{"TemplateFilename"};
2481         $template_string_handle = $template_database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM kiriwrite_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2482         $template_string_handle->execute();
2484         while (@template_data = $template_string_handle->fetchrow_array()){
2486                 # Get certain values from the array.
2488                 $template_filename      = $template_data[0];
2489                 $template_name          = $template_data[1];
2490                 $template_description   = $template_data[2];
2491                 $template_layout        = $template_data[3];
2492                 $template_datemodified  = $template_data[4];
2494                 # Process them into the hash.
2496                 %page_info = (
2497                         "TemplateFilename" => $template_filename,
2498                         "TemplateName" => $template_name,
2499                         "TemplateDescription" => $template_description,
2500                         "TemplateLayout" => $template_layout,
2501                         "TemplateLastModified" => $template_datemodified
2502                 );
2504                 $template_found = 1;
2506         }
2508         if ($template_found eq 0){
2510                 # The template was not found in the template database so
2511                 # write an error value.
2513                 $error = "TemplateDoesNotExist";
2514                 return;
2516         }
2518         return %page_info;
2522 sub addtemplate{
2523 #################################################################################
2524 # addtemplate: Adds a template to the template database.                        #
2525 #                                                                               #
2526 # Usage:                                                                        #
2527 #                                                                               #
2528 # $dbmodule->addtemplate();                                                     #
2529 #                                                                               #
2530 # options       Specifies the following options in any order.                   #
2531 #                                                                               #
2532 # TemplateFilename      Specifies the new template filename.                    #
2533 # TemplateName          Specifies the new template name.                        #
2534 # TemplateDescription   Specifies the new template description.                 #
2535 # TemplateLayout        Specifies the new template layout.                      #
2536 #################################################################################
2538         $error = "";
2539         $errorext = "";
2541         # Get the data passed to the subroutine.
2543         my $class = shift;
2544         my ($passedoptions) = @_;
2546         my @page_exists;
2547         my $notemplatetable;
2548         my $blankfile = 0;
2550         my $template_filename           = $passedoptions->{"TemplateFilename"};
2551         my $template_name               = $passedoptions->{"TemplateName"};
2552         my $template_description        = $passedoptions->{"TemplateDescription"};
2553         my $template_layout             = $passedoptions->{"TemplateLayout"};
2555         # Check if the template database permissions are valid.
2557         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2558         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2560         if ($templatedb_permissions eq 1){
2562                 if ($templatedb_exists eq 0){
2563                         $error = "TemplateDatabaseInvalidPermissionsSet";
2564                         return;
2565                 }
2567         }
2569         # Check if the template already exists before adding.
2571         if ($templatedb_exists eq 0){
2573                 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2575                 if ($blankfile eq 0){
2577                         $template_string_handle->execute();
2579                         while (@page_exists = $template_string_handle->fetchrow_array()){
2581                                 $error = "TemplatePageExists";
2582                                 return;
2584                         }
2586                 }
2588         }
2590         # Get the current date.
2591  
2592         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2593  
2594         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2596         # Check if certain values are undefined and if they
2597         # are then set them blank.
2599         if (!$template_name){
2601                 $template_name = "";
2603         }
2605         if (!$template_description){
2607                 $template_description = "";
2609         }
2611         if (!$template_layout){
2613                 $template_layout = "";
2615         }
2617         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2619         if ($directory_permissions eq 1 && $templatedb_exists){
2621                 # The template database cannot be created because of invalid directory
2622                 # permissions so return an error value.
2624                 $error = "TemplateDatabaseUncreateable";
2625                 return; 
2627         }
2629         # Check to see if a template can be added.
2631         $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2632                                 \'' . $class->convert($template_filename) . '\',
2633                                 \'' . $class->convert($template_name) . '\',
2634                                 \'' . $class->convert($template_description) . '\',
2635                                 \'' . $class->convert($template_layout) . '\',
2636                                 \'' . $class->convert($template_date) . '\'
2637         )') or ( $notemplatetable = 1 );
2639         if (!$notemplatetable){
2641                 $template_string_handle->execute();
2643         }
2645         # Check to see if there is no template table and attempt to create one.
2647         if ($notemplatetable){
2649                 # Create a template table.
2651                 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2653                 if ($directory_permissions eq 1){
2655                         # The template database cannot be created because of invalid directory
2656                         # permissions so return an error.
2658                         $error = "TemplateDatabaseFileUncreateable";
2659                         return;
2661                 }
2663                 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2664                         filename varchar(256) primary key,
2665                         templatename varchar(512),
2666                         templatedescription varchar(512),
2667                         templatelayout text,
2668                         datemodified datetime
2669                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2670                 $template_string_handle->execute();
2672                 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2673                                 \'' . $class->convert($template_filename) . '\',
2674                                 \'' . $class->convert($template_name) . '\',
2675                                 \'' . $class->convert($template_description) . '\',
2676                                 \'' . $class->convert($template_layout) . '\',
2677                                 \'' . $class->convert($template_date) . '\'
2678                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2679                 $template_string_handle->execute();
2681         }
2685 sub deletetemplate{
2686 #################################################################################
2687 # deletetemplate: Deletes a template from the template database.                #
2688 #                                                                               #
2689 # Usage:                                                                        #
2690 #                                                                               #
2691 # $dbmodule->deletetemplate(options);                                           #
2692 #                                                                               #
2693 # options       Specifies the following options in any order.                   #
2694 #                                                                               #
2695 # TemplateFilename      Specifies the template filename to delete.              #
2696 #################################################################################
2698         $error = "";
2699         $errorext = "";
2701         # Get the data passed to the subroutine.
2703         my $class = shift;
2704         my ($passedoptions) = @_;
2706         my @pagedata;
2707         my $template_filename = $passedoptions->{"TemplateFilename"};
2708         my $template_count = 0;
2710         # Check if the template exists.
2712         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2713         $template_string_handle->execute();
2715         while (@pagedata = $template_string_handle->fetchrow_array()){
2717                 $template_count++;
2719         }
2721         if ($template_count eq 0){
2723                 # No pages were returned so return an error value.
2725                 $error = "TemplateDoesNotExist";
2726                 return;
2728         }
2730         # Delete the template from the template database.
2732         $template_string_handle = $template_database_handle->prepare('DELETE FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2733         $template_string_handle->execute();
2737 sub edittemplate{
2738 #################################################################################
2739 # editttemplate: Edits a Kiriwrite template.                                    #
2740 #                                                                               #
2741 # Usage:                                                                        #
2742 #                                                                               #
2743 # $dbmodule->edittemplate(options);                                             #
2744 #                                                                               #
2745 # options       Specifies the following options in any order.                   #
2746 #                                                                               #
2747 # TemplateFilename              Specifies the template filename to edit.        #
2748 # NewTemplateFilename           Specifies the new template filename.            #
2749 # NewTemplateName               Specifies the new template name.                #
2750 # NewTemplateDescription        Specifies the new template description.         #
2751 # NewTemplateLayout             Specifies the new template layout.              #
2752 #################################################################################
2754         # Get the values passed.
2756         my $class = shift;
2757         my ($passedoptions) = @_;
2758         my $template_found = 0;
2759         my @template_info;
2761         # Process the values passed.
2763         my $template_filename           = $passedoptions->{"TemplateFilename"};
2764         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2765         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2766         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2767         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2769         # Check if the template database permissions are valid.
2771         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2772         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2774         if ($templatedb_permissions eq 1){
2776                 if ($templatedb_exists eq 0){
2777                         $error = "TemplateDatabaseInvalidPermissionsSet";
2778                         return;
2779                 }
2781         }
2783         # Check if the template exists.
2785         $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2786         $template_string_handle->execute();
2788         while (@template_info = $template_string_handle->fetchrow_array()){
2790                 $template_found = 1;
2792         }
2794         # Check to see if the template was found and set an error value if
2795         # it wasn't.
2797         if ($template_found eq 0){
2799                 $error = "TemplateDoesNotExist";
2800                 return;
2802         }
2804         # Get the date and time.
2806         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2807         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2809         # Update the template information.
2811         $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2812                 filename = \'' . $class->convert($new_template_filename) . '\',
2813                 templatename = \'' . $class->convert($new_template_name) . '\',
2814                 templatedescription = \'' . $class->convert($new_template_description) . '\',
2815                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2816                 datemodified = \'' . $class->convert($templatenewdate) . '\'
2817                 WHERE filename = \'' . $class->convert($template_filename) . '\'
2818         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2819         $template_string_handle->execute();
2823 #################################################################################
2824 # General subroutines.                                                          #
2825 #################################################################################
2827 sub connect{
2828 #################################################################################
2829 # connect: Connect to the server.                                               #
2830 #                                                                               #
2831 # Usage:                                                                        #
2832 #                                                                               #
2833 # $dbmodule->connect();                                                         #
2834 #################################################################################
2836         # This function is not needed in this database module.
2840 sub disconnect{
2841 #################################################################################
2842 # connect: Disconnect from the server.                                          #
2843 #                                                                               #
2844 # Usage:                                                                        #
2845 #                                                                               #
2846 # $dbmodule->disconnect();                                                      #
2847 #################################################################################
2849         # This function is not needed in this database module.
2851         undef $string_handle;
2855 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