Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Yet even more additions and corrections.
[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$//og;
609                 $database_filename_friendly = $database;
611                 #$database_filename_length = length($database);
612                 #$database_filename_friendly = substr($database, 0, $database_filename_length - 10);
613                 push(@data_directory_final, $database_filename_friendly);
615         }
617         # Return the list of databases.
619         return @data_directory_final;
623 sub getdatabaseinfo{
624 #################################################################################
625 # getdatabaseinfo: Get information about the database.                          #
626 #                                                                               #
627 # Usage:                                                                        #
628 #                                                                               #
629 # $dbmodule->getdatabaseinfo();                                                 #
630 #################################################################################
632         # Get the database information.
634         my $class = shift;
635         my ($databaseinfo, %databaseinfo);
636         my ($sqldata, @sqldata);
638         $error = "";
639         $errorext = "";
641         $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){
1209                 $page_reduced = 0;
1211         }
1213         if ($page_reduced eq 1){
1215                 $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 );
1217                 $string_handle->execute();
1219                 # Check if the page exists in the database.
1221                 while (@data_page = $string_handle->fetchrow_array()){
1222         
1223                         # Get the values from the array.
1224         
1225                         $pagefilename           = $data_page[0];
1226                         $pagename               = $data_page[1];
1227                         $pagedescription        = $data_page[2];
1228                         $pagelastmodified       = $data_page[3];
1229         
1230                         # Put the values into the page hash.
1231         
1232                         %database_page = (
1233                                 "PageFilename"          => $pagefilename,
1234                                 "PageName"              => $pagename,
1235                                 "PageDescription"       => $pagedescription,
1236                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1237                         );
1238         
1239                         $page_found = 1;
1240         
1241                 }
1244         } else {
1245                 
1246                 $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 );
1248                 $string_handle->execute();
1250                 # Check if the page exists in the database.
1252                 while (@data_page = $string_handle->fetchrow_array()){
1253         
1254                         # Get the values from the array.
1255         
1256                         $pagefilename           = $data_page[0];
1257                         $pagename               = $data_page[1];
1258                         $pagedescription        = $data_page[2];
1259                         $pagesection            = $data_page[3];
1260                         $pagetemplate           = $data_page[4];
1261                         $pagedata               = $data_page[5];
1262                         $pagesettings           = $data_page[6];
1263                         $pagelastmodified       = $data_page[7];
1264         
1265                         # Put the values into the page hash.
1266         
1267                         %database_page = (
1268                                 "PageFilename"          => $pagefilename,
1269                                 "PageName"              => $pagename,
1270                                 "PageDescription"       => $pagedescription,
1271                                 "PageSection"           => $pagesection,
1272                                 "PageTemplate"          => $pagetemplate,
1273                                 "PageContent"           => $pagedata,
1274                                 "PageSettings"          => $pagesettings,
1275                                 "PageLastModified"      => $class->dateconvert($pagelastmodified),
1276                         );
1277         
1278                         $page_found = 1;
1279         
1280                 }
1282         }
1283         
1284         # Check if the page did exist.
1286         if (!$page_found){
1288                 $error = "PageDoesNotExist";
1289                 return;
1291         }
1293         undef $string_handle;
1294         return %database_page;
1298 sub addpage{
1299 #################################################################################
1300 # addpage: Add a page to the selected database.                                 #
1301 #                                                                               #
1302 # Usage:                                                                        #
1303 #                                                                               #
1304 # $dbmodule->addpage(options);                                                  #
1305 #                                                                               #
1306 # options       Specifies the following options in any order.                   #
1307 #                                                                               #
1308 # PageFilename          Specifies the page filename to use.                     #
1309 # PageName              Specifies the page name to use.                         #
1310 # PageDescription       Specifies the page description to use.                  #
1311 # PageSection           Specifies the page section to use.                      #
1312 # PageTemplate          Specifies the page template to use.                     #
1313 # PageContent           Specifies the page content to use.                      #
1314 # PageSettings          Specifies the page settings to use.                     #
1315 #################################################################################
1317         # Get the data that was passed to the subroutine.
1319         $error = "";
1320         $errorext = "";
1322         my $class               = shift;
1323         my ($passedoptions)     = shift;
1325         my @database_page;
1326         my $page_count = 0;
1328         # Get the values passed to the hash.
1330         my $page_filename       = $passedoptions->{"PageFilename"};
1331         my $page_name           = $passedoptions->{"PageName"};
1332         my $page_description    = $passedoptions->{"PageDescription"};
1333         my $page_section        = $passedoptions->{"PageSection"};
1334         my $page_template       = $passedoptions->{"PageTemplate"};
1335         my $page_content        = $passedoptions->{"PageContent"};
1336         my $page_settings       = $passedoptions->{"PageSettings"};
1338         # Check to see if the filename given already exists
1339         # in the page database.
1341         $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 );
1342         $string_handle->execute();
1344         # Check if a page with the filename given really does
1345         # exist.
1347         while (@database_page = $string_handle->fetchrow_array()){
1349                 # A page does exist so increment the count to 1.
1351                 $page_count++;
1352                 
1353         }
1355         if ($page_count ne 0){
1357                 # The page does exist so set the error value.
1359                 $error = "PageExists";
1360                 return;
1362         }
1364         # Check if certain values are undefined.
1366         if (!$page_name){
1368                 $page_name = "";
1370         }
1372         if (!$page_description){
1374                 $page_description = "";
1376         }
1378         if (!$page_section){
1380                 $page_section = "";
1382         }
1384         if (!$page_content){
1386                 $page_content = "";
1388         }
1390         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1391         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1393         # Add the page to the selected database.
1395         $string_handle = $database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1396                 \'' . $class->convert($page_filename) . '\',
1397                 \'' . $class->convert($page_name) . '\',
1398                 \'' . $class->convert($page_description) . '\',
1399                 \'' . $class->convert($page_section) . '\',
1400                 \'' . $class->convert($page_template) . '\',
1401                 \'' . $class->convert($page_content) . '\',
1402                 \'' . $class->convert($page_settings) . '\',
1403                 \'' . $class->convert($page_date) . '\'
1404         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1405         $string_handle->execute();
1407         undef $string_handle;
1411 sub deletepage{
1412 #################################################################################
1413 # deletepage: Delete a page from the selected database.                         #
1414 #                                                                               #
1415 # Usage:                                                                        #
1416 #                                                                               #
1417 # $dbmodule->deletepage(options)                                                #
1418 #                                                                               #
1419 # options       Specifies the following options in any order.                   #
1420 #                                                                               #
1421 # PageFilename  Specifies the page filename to delete.                          #
1422 #################################################################################
1424         $error = "";
1425         $errorext = "";
1427         # Get the data that was passed to the subroutine.
1429         my $class = shift;
1430         my ($passedoptions) = @_;
1431         my @page_info;
1432         my $page_found = 0;
1434         # Get the page filename.
1436         my $page_filename = $passedoptions->{"PageFilename"};
1438         # Check if the page exists before deleting it.
1440         $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 );
1441         $string_handle->execute();
1443         while (@page_info = $string_handle->fetchrow_array()){
1445                 $page_found = 1;
1447         }
1449         # Check if the page really does exist.
1451         if (!$page_found){
1453                 $error = "PageDoesNotExist";
1454                 return;
1456         }
1458         # Delete the page.
1460         $string_handle = $database_handle->prepare('DELETE FROM kiriwrite_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1461         $string_handle->execute();
1465 sub editpage{
1466 #################################################################################
1467 # editpage: Edit a page from the selected database.                             #
1468 #                                                                               #
1469 # Usage:                                                                        #
1470 #                                                                               #
1471 # $dbmodule->editpage(options);                                                 #
1472 #                                                                               #
1473 # options       Specifies the following options in any order.                   #
1474 #                                                                               #
1475 # PageFilename          Specifies the filename to edit.                         #
1476 # PageNewFilename       Specifies the new filename to use.                      #
1477 # PageNewName           Specifies the new page name to use.                     #
1478 # PageNewDescription    Specifies the new page description to use.              #
1479 # PageNewSection        Specifies the new page section to use.                  #
1480 # PageNewTemplate       Specifies the new page template to use.                 #
1481 # PageNewContent        Specifies the new page content to use.                  #
1482 # PageNewSettings       Specifies the new page settings to use.                 #
1483 #################################################################################
1485         $error = "";
1486         $errorext = "";
1488         # Get the values passed to the subroutine.
1490         my $class = shift;
1491         my ($passedoptions) = @_;
1492         my $page_found = 0;
1493         my @page_info;
1495         # Get the data that was passed to the subroutine.
1497         my $page_filename       = $passedoptions->{"PageFilename"};
1498         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1499         my $page_newname        = $passedoptions->{"PageNewName"};
1500         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1501         my $page_newsection     = $passedoptions->{"PageNewSection"};
1502         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1503         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1504         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1506         # Check if the page with the filename given exists.
1508         $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 );
1509         $string_handle->execute();
1511         # Check if the page really does exist.
1513         while (@page_info = $string_handle->fetchrow_array()){
1515                 # The page information is found.
1517                 $page_found = 1;
1519         }
1521         # Check if the page really does exist.
1523         if (!$page_found){
1525                 $error = "PageDoesNotExist";
1526                 return;
1528         }
1530         # Check if there is a page that already exists with the new
1531         # filename.
1533         $page_found = 0;
1535         if ($page_filename ne $page_newfilename){
1537                 $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 );
1538                 $string_handle->execute();
1540                 # Check if a page really is using the new filename.
1542                 while (@page_info = $string_handle->fetchrow_array()){
1544                         # The page information is found.
1546                         $page_found = 1;
1548                 }
1550                 if ($page_found eq 1){
1552                         $error = "PageExists";
1553                         return;
1555                 }
1557         }
1559         # Get the current date.
1561         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1562         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1564         # Edit the selected page.
1566         $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 );
1567         $string_handle->execute();
1571 sub movepage{
1572 #################################################################################
1573 # movepage: Moves a page from the old database to the new database.             #
1574 #                                                                               #
1575 # Usage:                                                                        #
1576 #                                                                               #
1577 # $dbmodule->movepage(options);                                                 #
1578 #                                                                               #
1579 # options       Specifies the following options in any order.                   #
1580 #                                                                               #
1581 # PageFilename  Specifies the page with the filename to move.                   #
1582 #################################################################################
1584         $error = "";
1585         $errorext = "";
1587         # Get the values passed to the subroutine.
1589         my $class = shift;
1590         my ($passedoptions) = @_;
1592         my (%database_page, $database_page);
1593         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1594         my @page_info;
1595         my $page_found = 0;
1597         # Get the data that was passed to the subroutine.
1599         my $page_filename = $passedoptions->{"PageFilename"};
1601         # Check if the page with the filename given exists.
1603         $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 );
1604         $string_handle->execute();
1606         # Check if the page really does exist.
1608         while (@page_info = $string_handle->fetchrow_array()){
1610                 # Get the values from the array.
1612                 $pagefilename           = $page_info[0];
1613                 $pagename               = $page_info[1];
1614                 $pagedescription        = $page_info[2];
1615                 $pagesection            = $page_info[3];
1616                 $pagetemplate           = $page_info[4];
1617                 $pagedata               = $page_info[5];
1618                 $pagesettings           = $page_info[6];
1619                 $pagelastmodified       = $page_info[7];
1621                 # Put the values into the page hash.
1623                 %database_page = (
1624                         "PageFilename"          => $pagefilename,
1625                         "PageName"              => $pagename,
1626                         "PageDescription"       => $pagedescription,
1627                         "PageSection"           => $pagesection,
1628                         "PageTemplate"          => $pagetemplate,
1629                         "PageContent"           => $pagedata,
1630                         "PageSettings"          => $pagesettings,
1631                         "PageLastModified"      => $pagelastmodified,
1632                 );
1634                 # The page information is found.
1636                 $page_found = 1;
1638         }
1640         # Check if the page really does exist.
1642         if (!$page_found){
1644                 $error = "PageDoesNotExist";
1645                 return;
1647         }
1649         # Check if the page with the filename given already exists in
1650         # the database the page is being moved to.
1652         $page_found = 0;
1653         @page_info = ();
1655         $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 );
1656         $second_string_handle->execute();
1658         while (@page_info = $second_string_handle->fetchrow_array()){
1660                 $page_found = 1;
1662         }
1663         
1664         # Check if the page really does exist.
1666         if ($page_found){
1668                 $error = "PageAlreadyExists";
1669                 return;
1671         }
1673         # Add the page to the new database.
1675         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1676                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1677                 \'' . $class->convert($database_page{"PageName"}) . '\',
1678                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1679                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1680                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1681                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1682                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1683                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1684         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1685         $second_string_handle->execute();
1687         # Delete the page from the old database.
1689         $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 );
1690         $string_handle->execute();
1694 sub copypage{
1695 #################################################################################
1696 # copypage: Copies a page from the old database to the new database.            #
1697 #                                                                               #
1698 # Usage:                                                                        #
1699 #                                                                               #
1700 # $dbmodule->copypage(options);                                                 #
1701 #                                                                               #
1702 # options       Specifies the following options in any order.                   #
1703 #                                                                               #
1704 # PageFilename  Specifies the page with the filename to copy.                   #
1705 #################################################################################
1707         $error = "";
1708         $errorext = "";
1710         # Get the values passed to the subroutine.
1712         my $class = shift;
1713         my ($passedoptions) = @_;
1715         my (%database_page, $database_page);
1716         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1717         my @page_info;
1718         my $page_found = 0;
1720         # Get the data that was passed to the subroutine.
1722         my $page_filename = $passedoptions->{"PageFilename"};
1724         # Check if the page with the filename given exists.
1726         $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 );
1727         $string_handle->execute();
1729         # Check if the page really does exist.
1731         while (@page_info = $string_handle->fetchrow_array()){
1733                 # Get the values from the array.
1735                 $pagefilename           = $page_info[0];
1736                 $pagename               = $page_info[1];
1737                 $pagedescription        = $page_info[2];
1738                 $pagesection            = $page_info[3];
1739                 $pagetemplate           = $page_info[4];
1740                 $pagedata               = $page_info[5];
1741                 $pagesettings           = $page_info[6];
1742                 $pagelastmodified       = $page_info[7];
1744                 # Put the values into the page hash.
1746                 %database_page = (
1747                         "PageFilename"          => $pagefilename,
1748                         "PageName"              => $pagename,
1749                         "PageDescription"       => $pagedescription,
1750                         "PageSection"           => $pagesection,
1751                         "PageTemplate"          => $pagetemplate,
1752                         "PageContent"           => $pagedata,
1753                         "PageSettings"          => $pagesettings,
1754                         "PageLastModified"      => $pagelastmodified,
1755                 );
1757                 # The page information is found.
1759                 $page_found = 1;
1761         }
1763         # Check if the page really does exist.
1765         if (!$page_found){
1767                 $error = "PageDoesNotExist";
1768                 return;
1770         }
1772         # Check if the page with the filename given already exists in
1773         # the database the page is being moved to.
1775         $page_found = 0;
1776         @page_info = ();
1778         $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 );
1779         $second_string_handle->execute();
1781         while (@page_info = $second_string_handle->fetchrow_array()){
1783                 $page_found = 1;
1785         }
1786         
1787         # Check if the page really does exist.
1789         if ($page_found){
1791                 $error = "PageAlreadyExists";
1792                 return;
1794         }
1796         # Add the page to the new database.
1798         $second_string_handle = $second_database_handle->prepare('INSERT INTO kiriwrite_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1799                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1800                 \'' . $class->convert($database_page{"PageName"}) . '\',
1801                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1802                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1803                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1804                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1805                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1806                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1807         )') or ( $error = "NewDatabaseError", $errorext = $second_database_handle->errstr, return );
1808         $second_string_handle->execute();
1812 #################################################################################
1813 # Filter subroutines.                                                           #
1814 #################################################################################
1816 sub connectfilter{
1817 #################################################################################
1818 # connectfilter: Connect to the filter database.                                #
1819 #                                                                               #
1820 # Usage:                                                                        #
1821 #                                                                               #
1822 # $dbmodule->connectfilter(missingignore);                                      #
1823 #                                                                               #
1824 # missingignore Ignore error about database being missing.                      #
1825 #################################################################################
1827         $error = "";
1828         $errorext = "";
1830         my $class = shift;
1831         my $ignoremissing = shift;
1833         # Check if the template database exists.
1835         my $filterdatabase_exists = main::kiriwrite_fileexists("filters.db.sqlite");
1836         
1837         if ($filterdatabase_exists eq 1){
1839                 $filterdb_exists = 0;
1841                 if (!$ignoremissing){
1843                         $error = "FilterDatabaseDoesNotExist";
1844                         return;
1846                 }
1848         }
1850         # Check if the permission settings for the template database are valid.
1852         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 0);
1854         if ($filterdb_permissions eq 1){
1856                 # The template database has invalid permissions set
1857                 # so return an error value.
1859                 if (!$ignoremissing){
1861                         $error = "FilterDatabaseInvalidPermissionsSet";
1862                         return;
1864                 }
1866         }
1868         # Connect to the template database.
1870         $filterdb_database_handle = DBI->connect("dbi:SQLite:dbname=filters.db.sqlite");
1871         $filterdb_database_handle->{unicode} = 1;
1872         $filterdb_loaded = 1;
1876 sub disconnectfilter{
1877 #################################################################################
1878 # disconnectfilter: Disconnect from the filter database.                        #
1879 #                                                                               #
1880 # Usage:                                                                        #
1881 #                                                                               #
1882 # $dbmodule->disconnectfilter();                                                #
1883 #################################################################################
1885         # Disconnect the template database.
1887         if ($filterdb_loaded eq 1){
1889                 undef $filterdb_string_handle;
1890                 $filterdb_database_handle->disconnect();
1892         }
1896 sub getfilterlist{
1897 #################################################################################
1898 # getfilterlist: Gets the list of filters in the filter database.               #
1899 #                                                                               #
1900 # Usage:                                                                        #
1901 #                                                                               #
1902 # $dbmodule->getfilterlist();                                                   #
1903 #################################################################################
1905         $error = "";
1906         $errorext = "";
1908         my @filter_list;
1909         my @filter_data;
1911         # Get the list of filters available.
1913         $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 );
1914         $filterdb_string_handle->execute();
1916         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1918                 # Add the filter to the list of available filters.
1920                 push(@filter_list, $filter_data[0]);
1922         }
1924         undef $filterdb_string_handle;
1925         return @filter_list;
1929 sub getfilterinfo{
1930 #################################################################################
1931 # getfilterinfo: Gets information about the filter.                             #
1932 #                                                                               #
1933 # Usage:                                                                        #
1934 #                                                                               #
1935 # $dbmodule->getfilterinfo(options);                                            #
1936 #                                                                               #
1937 # options       Specifies the following options in any order.                   #
1938 #                                                                               #
1939 # FilterID      Specifies the filter ID number to get information from.         #
1940 #################################################################################
1942         $error = "";
1943         $errorext = "";
1945         # Get the values passed to the subroutine.
1947         my $class               = shift;
1948         my ($passedoptions)     = @_;
1950         my %filter_info;
1951         my $filter_exists       = 0;
1952         my @filter_data;
1954         # Get the values that are in the hash.
1956         my $filter_id           = $passedoptions->{"FilterID"};
1958         $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 );
1959         $filterdb_string_handle->execute();
1961         # Get the filter information.
1963         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
1965                 $filter_info{"FilterID"}        = $filter_data[0];
1966                 $filter_info{"FilterPriority"}  = $filter_data[1];
1967                 $filter_info{"FilterFind"}      = $filter_data[2];
1968                 $filter_info{"FilterReplace"}   = $filter_data[3];
1969                 $filter_info{"FilterNotes"}     = $filter_data[4];
1971                 $filter_exists = 1;
1973         }
1975         # Check if the filter exists.
1977         if (!$filter_exists){
1979                 # The filter does not exist so return
1980                 # an error value.
1982                 $error = "FilterDoesNotExist";
1983                 return;
1985         }
1987         # Return the filter information.
1989         undef $filterdb_string_handle;
1990         return %filter_info;
1994 sub addfilter{
1995 #################################################################################
1996 # addfilter: Adds a filter to the filter database.                              #
1997 #                                                                               #
1998 # Usage:                                                                        #
1999 #                                                                               #
2000 # $dbmodule->addfilter(options);                                                #
2001 #                                                                               #
2002 # options       Specifies the following options in any order.                   #
2003 #                                                                               #
2004 # FindFilter    Specifies the find filter to add.                               #
2005 # ReplaceFilter Specifies the replace filter to add.                            #
2006 # Priority      Specifies the filter priority to use.                           #
2007 # Notes         Specifies the notes to use.                                     #
2008 #################################################################################
2010         $error = "";
2011         $errorext = "";
2013         # Get the values passed to the subroutine.
2015         my $class = shift;
2016         my ($passedoptions) = @_;
2018         # Define some variables for later.
2020         my @database_filters;
2021         my @filterid_list;
2022         my @filterid_check;
2023         my $nofiltertable = 0;
2024         my $filter_found = 0;
2025         my $filter_count = 0;
2026         my $filter_id;
2027         my $new_id;
2029         # Get the values from the hash.
2031         my $filter_find         = $passedoptions->{"FindFilter"};
2032         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2033         my $filter_priority     = $passedoptions->{"Priority"};
2034         my $filter_notes        = $passedoptions->{"Notes"};
2036         # Check if the filter database permissions are valid.
2038         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2039         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2041         if ($filterdb_permissions eq 1){
2043                 if ($filterdb_exists eq 0){
2044                         $error = "FilterDatabaseInvalidPermissionsSet";
2045                         return;
2046                 }
2048         }
2050         # Check if certain values are undefined and if they
2051         # are then set them blank.
2053         if (!$filter_find){
2055                 $filter_find = "";
2057         }
2059         if (!$filter_replace){
2061                 $filter_replace = "";
2063         }
2065         if (!$filter_priority){
2067                 $filter_priority = 1;
2069         }
2071         if (!$filter_notes){
2073                 $filter_notes = "";
2075         }
2077         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2079         if ($directory_permissions eq 1 && $filterdb_exists){
2081                 # The template database cannot be created because of invalid directory
2082                 # permissions so return an error value.
2084                 $error = "FilterDatabaseFileUncreateable";
2085                 return; 
2087         }
2089         # Check if the filter table exists.
2091         $filterdb_string_handle = $filterdb_database_handle->prepare('SELECT id FROM kiriwrite_filters ORDER BY id ASC') or ( $nofiltertable = 1 );
2093         # Check if there is really no filter table.
2095         if ($nofiltertable){
2097                 # Create the filter database table.
2099                 $filterdb_string_handle = $filterdb_database_handle->prepare('CREATE TABLE kiriwrite_filters (
2100                         id int(7) primary key,
2101                         priority int(5),
2102                         findsetting varchar(1024),
2103                         replacesetting varchar(1024),
2104                         notes text
2105                 )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2106                 $filterdb_string_handle->execute();
2108         }
2110         # Find the lowest filter identification number available.
2112         $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 );
2113         $filterdb_string_handle->execute();
2115         while (@database_filters = $filterdb_string_handle->fetchrow_array()){
2117                 $filter_id      = $database_filters[0];
2119                 # Add the filter identification to the list of filter IDs.
2121                 push(@filterid_list, $filter_id);
2123         }
2125         $filter_id = "";
2127         # Process each filter looking for a blank available filter.
2129         foreach $filter_id (@filterid_list){
2131                 # Check the next filter ID to see if it's blank.
2133                 $new_id = $filter_id + 1;
2135                 $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 );
2136                 $filterdb_string_handle->execute();
2138                 # Get the filter identification number.
2140                 while (@filterid_check = $filterdb_string_handle->fetchrow_array()){
2142                         $filter_found = 1;
2144                 }
2146                 # Check if a filter was found.
2148                 if (!$filter_found){
2150                         # No filter was found using this ID so exit the loop.
2152                         last;
2154                 }
2156                 # Increment the filter count and reset the filter found value.
2158                 $filter_count++;
2159                 $filter_found = 0;
2160                 $new_id = 0;
2162         }
2164         # Check if there were any filters in the filter database.
2166         if (!$filter_count && !$new_id){
2168                 # There were no filters in the filter database so set
2169                 # the new filter identification value to 1.
2171                 $new_id = 1;
2173         }
2175         # Add the filter to the filter database.
2177         $filterdb_string_handle = $filterdb_database_handle->prepare('INSERT INTO kiriwrite_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2178                 \'' . $class->convert($new_id) . '\',
2179                 \'' . $class->convert($filter_priority) . '\',
2180                 \'' . $class->convert($filter_find) . '\',
2181                 \'' . $class->convert($filter_replace) .'\',
2182                 \'' . $class->convert($filter_notes) . '\'
2183         )') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );
2184         $filterdb_string_handle->execute();
2188 sub editfilter{
2189 #################################################################################
2190 # editfilter: Edits a filter in the filter database.                            #
2191 #                                                                               #
2192 # Usage:                                                                        #
2193 #                                                                               #
2194 # $dbmodule->editfilter(options);                                               #
2195 #                                                                               #
2196 # options       Specifies the following options in any order.                   #
2197 #                                                                               #
2198 # FilterID              Specifies the filter to edit.                           #
2199 # NewFindFilter         Specifies the new find filter setting.                  #
2200 # NewReplaceFilter      Specifies the new replace filter setting.               #
2201 # NewFilterPriority     Specifies the new filter priority setting.              #
2202 # NewFilterNotes        Specifies the new notes for the filter.                 #
2203 #################################################################################
2205         $error = "";
2206         $errorext = "";
2208         # Get the values passed to the subroutine.
2210         my $class = shift;
2211         my ($passedoptions) = @_;
2213         my @filter_data;
2214         my $filter_exists = 1;
2215         my $blankfile = 0;
2217         # Get the values from the hash.
2219         my $filter_id           = $passedoptions->{"FilterID"};
2220         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2221         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2222         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2223         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2225         # Check if the filter database permissions are valid.
2227         my $filterdb_exists = main::kiriwrite_fileexists("filters.db.sqlite", 1, 1);
2228         my $filterdb_permissions = main::kiriwrite_filepermissions("filters.db.sqlite", 1, 1);
2230         if ($filterdb_permissions eq 1){
2232                 if ($filterdb_exists eq 0){
2233                         $error = "FilterDatabaseInvalidPermissionsSet";
2234                         return;
2235                 }
2237         }
2239         # Check if the filter exists before editing it.
2241         $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 );
2242         $filterdb_string_handle->execute();
2244         # Check if the filter exists.
2246         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2248                 $filter_exists = 1;
2250         }       
2252         # Check if the filter really does exist.
2254         if (!$filter_exists){
2256                 # The filter does not exist so return
2257                 # an error value.
2259                 $error = "FilterDoesNotExist";
2260                 return;
2262         }
2264         # Edit the selected filter.
2266         $filterdb_string_handle = $filterdb_database_handle->prepare('UPDATE kiriwrite_filters SET
2267                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2268                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2269                 priority = \'' . $class->convert($filter_newpriority) . '\',
2270                 notes = \'' . $class->convert($filter_newnotes) . '\'
2271         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $filterdb_database_handle->errstr, return );      
2272         $filterdb_string_handle->execute();
2274         undef $filterdb_string_handle;
2275         return;
2279 sub deletefilter{
2280 #################################################################################
2281 # deletefilter: Deletes a filter from the filter database.                      #
2282 #                                                                               #
2283 # Usage:                                                                        #
2284 #                                                                               #
2285 # $dbmodule->deletefilter(options);                                             #
2286 #                                                                               #
2287 # options       Specifies the following options in any order.                   #
2288 #                                                                               #
2289 # FilterID      Specifies the filter to delete from the filter database.        #
2290 #################################################################################
2292         $error = "";
2293         $errorext = "";
2295         # Get the values passed to the subroutine.
2297         my $class = shift;
2298         my ($passedoptions) = @_;
2300         my $filter_exists = 0;
2301         my @filter_data;
2303         # Get the values from the hash.
2305         my $filter_id           = $passedoptions->{"FilterID"};
2307         # Check if the filter exists before deleting.
2309         $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 );
2310         $filterdb_string_handle->execute();
2312         while (@filter_data = $filterdb_string_handle->fetchrow_array()){
2314                 $filter_exists = 1;
2316         }
2318         # Check to see if the filter really does exist.
2320         if (!$filter_exists){
2322                 $error = "FilterDoesNotExist";
2323                 return;
2325         }
2327         # Delete the filter from the filter database.
2329         $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 );
2330         $filterdb_string_handle->execute();
2332         undef $filterdb_string_handle;
2336 #################################################################################
2337 # Template subroutines.                                                         #
2338 #################################################################################
2340 sub connecttemplate{
2341 #################################################################################
2342 # connecttemplate: Connect to the template database.                            #
2343 #                                                                               #
2344 # Usage:                                                                        #
2345 #                                                                               #
2346 # $dbmodule->connecttemplate(missingignore);                                    #
2347 #                                                                               #
2348 # missingignore Ignore errror about database being missing.                     #
2349 #################################################################################
2351         $error = "";
2352         $errorext = "";
2354         my $class = shift;
2355         my $ignoremissing = shift;
2357         # Check if the template database exists.
2359         my $templatedatabase_exists = main::kiriwrite_fileexists("templates.db.sqlite");
2360         
2361         if ($templatedatabase_exists eq 1){
2363                 $templatedb_exists = 0;
2365                 if (!$ignoremissing){
2367                         $error = "TemplateDatabaseDoesNotExist";
2368                         return;
2370                 }
2372         }
2374         # Check if the permission settings for the template database are valid.
2376         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 0);
2378         if ($templatedb_permissions eq 1){
2380                 # The template database has invalid permissions set
2381                 # so return an error value.
2383                 if (!$ignoremissing){
2385                         $error = "TemplateDatabaseInvalidPermissionsSet";
2386                         return;
2388                 }
2390         }
2392         # Connect to the template database.
2394         $template_database_handle = DBI->connect("dbi:SQLite:dbname=templates.db.sqlite");
2395         $template_database_handle->{unicode} = 1;
2396         $templatedb_loaded = 1;
2400 sub disconnecttemplate{
2401 #################################################################################
2402 # disconnecttemplate: Disconnect from the template database.                    #
2403 #                                                                               #
2404 # Usage:                                                                        #
2405 #                                                                               #
2406 # $dbmodule->disconnecttemplate();                                              #
2407 #################################################################################
2409         # Disconnect the template database.
2411         if ($templatedb_loaded eq 1){
2413                 undef $template_string_handle;
2414                 $template_database_handle->disconnect();
2416         }
2420 sub gettemplatelist{
2421 #################################################################################
2422 # gettemplatelist: Gets the list of templates.                                  #
2423 #                                                                               #
2424 # Usage:                                                                        #
2425 #                                                                               #
2426 # $dbmodule->gettemplatelist();                                                 #
2427 #################################################################################
2429         $error = "";
2430         $errorext = "";
2432         $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 );
2433         $template_string_handle->execute();
2435         my @database_template;
2436         my @templates_list;
2437         my $template_filename;
2439         while (@database_template = $template_string_handle->fetchrow_array()){
2441                 # Get certain values from the array.
2443                 $template_filename      = $database_template[0];
2445                 # Add the template to the list of templates.
2447                 push(@templates_list, $template_filename);
2449         }
2451         return @templates_list;
2455 sub gettemplateinfo{
2456 #################################################################################
2457 # gettemplateinfo: Get information on a template.                               #
2458 #                                                                               #
2459 # Usage:                                                                        #
2460 #                                                                               #
2461 # $dbmodule->gettemplateinfo(options);                                          #
2462 #                                                                               #
2463 # options       Specifies the following options in any order.                   #
2464 #                                                                               #
2465 # TemplateFilename      Specifies the template filename to use.                 #
2466 #################################################################################
2468         $error = "";
2469         $errorext = "";
2471         # Get the data passed to the subroutine.
2473         my $class = shift;
2474         my ($passedoptions) = @_;
2476         my %page_info;
2477         my @template_data;
2479         my $template_filename;
2480         my $template_name;
2481         my $template_description;
2482         my $template_datemodified;
2483         my $template_layout;
2485         my $template_found = 0;
2487         my $filename    = $passedoptions->{"TemplateFilename"};
2489         $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 );
2490         $template_string_handle->execute();
2492         while (@template_data = $template_string_handle->fetchrow_array()){
2494                 # Get certain values from the array.
2496                 $template_filename      = $template_data[0];
2497                 $template_name          = $template_data[1];
2498                 $template_description   = $template_data[2];
2499                 $template_layout        = $template_data[3];
2500                 $template_datemodified  = $template_data[4];
2502                 # Process them into the hash.
2504                 %page_info = (
2505                         "TemplateFilename" => $template_filename,
2506                         "TemplateName" => $template_name,
2507                         "TemplateDescription" => $template_description,
2508                         "TemplateLayout" => $template_layout,
2509                         "TemplateLastModified" => $template_datemodified
2510                 );
2512                 $template_found = 1;
2514         }
2516         if ($template_found eq 0){
2518                 # The template was not found in the template database so
2519                 # write an error value.
2521                 $error = "TemplateDoesNotExist";
2522                 return;
2524         }
2526         return %page_info;
2530 sub addtemplate{
2531 #################################################################################
2532 # addtemplate: Adds a template to the template database.                        #
2533 #                                                                               #
2534 # Usage:                                                                        #
2535 #                                                                               #
2536 # $dbmodule->addtemplate();                                                     #
2537 #                                                                               #
2538 # options       Specifies the following options in any order.                   #
2539 #                                                                               #
2540 # TemplateFilename      Specifies the new template filename.                    #
2541 # TemplateName          Specifies the new template name.                        #
2542 # TemplateDescription   Specifies the new template description.                 #
2543 # TemplateLayout        Specifies the new template layout.                      #
2544 #################################################################################
2546         $error = "";
2547         $errorext = "";
2549         # Get the data passed to the subroutine.
2551         my $class = shift;
2552         my ($passedoptions) = @_;
2554         my @page_exists;
2555         my $notemplatetable;
2556         my $blankfile = 0;
2558         my $template_filename           = $passedoptions->{"TemplateFilename"};
2559         my $template_name               = $passedoptions->{"TemplateName"};
2560         my $template_description        = $passedoptions->{"TemplateDescription"};
2561         my $template_layout             = $passedoptions->{"TemplateLayout"};
2563         # Check if the template database permissions are valid.
2565         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2566         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2568         if ($templatedb_permissions eq 1){
2570                 if ($templatedb_exists eq 0){
2571                         $error = "TemplateDatabaseInvalidPermissionsSet";
2572                         return;
2573                 }
2575         }
2577         # Check if the template already exists before adding.
2579         if ($templatedb_exists eq 0){
2581                 $template_string_handle = $template_database_handle->prepare('SELECT filename FROM kiriwrite_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
2583                 if ($blankfile eq 0){
2585                         $template_string_handle->execute();
2587                         while (@page_exists = $template_string_handle->fetchrow_array()){
2589                                 $error = "TemplatePageExists";
2590                                 return;
2592                         }
2594                 }
2596         }
2598         # Get the current date.
2599  
2600         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2601  
2602         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2604         # Check if certain values are undefined and if they
2605         # are then set them blank.
2607         if (!$template_name){
2609                 $template_name = "";
2611         }
2613         if (!$template_description){
2615                 $template_description = "";
2617         }
2619         if (!$template_layout){
2621                 $template_layout = "";
2623         }
2625         my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2627         if ($directory_permissions eq 1 && $templatedb_exists){
2629                 # The template database cannot be created because of invalid directory
2630                 # permissions so return an error value.
2632                 $error = "TemplateDatabaseUncreateable";
2633                 return; 
2635         }
2637         # Check to see if a template can be added.
2639         $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2640                                 \'' . $class->convert($template_filename) . '\',
2641                                 \'' . $class->convert($template_name) . '\',
2642                                 \'' . $class->convert($template_description) . '\',
2643                                 \'' . $class->convert($template_layout) . '\',
2644                                 \'' . $class->convert($template_date) . '\'
2645         )') or ( $notemplatetable = 1 );
2647         if (!$notemplatetable){
2649                 $template_string_handle->execute();
2651         }
2653         # Check to see if there is no template table and attempt to create one.
2655         if ($notemplatetable){
2657                 # Create a template table.
2659                 my $directory_permissions = main::kiriwrite_filepermissions(".", 1, 1, 0);
2661                 if ($directory_permissions eq 1){
2663                         # The template database cannot be created because of invalid directory
2664                         # permissions so return an error.
2666                         $error = "TemplateDatabaseFileUncreateable";
2667                         return;
2669                 }
2671                 $template_string_handle = $template_database_handle->prepare('create table kiriwrite_templates(
2672                         filename varchar(256) primary key,
2673                         templatename varchar(512),
2674                         templatedescription varchar(512),
2675                         templatelayout text,
2676                         datemodified datetime
2677                 );') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2678                 $template_string_handle->execute();
2680                 $template_string_handle = $template_database_handle->prepare('INSERT INTO kiriwrite_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
2681                                 \'' . $class->convert($template_filename) . '\',
2682                                 \'' . $class->convert($template_name) . '\',
2683                                 \'' . $class->convert($template_description) . '\',
2684                                 \'' . $class->convert($template_layout) . '\',
2685                                 \'' . $class->convert($template_date) . '\'
2686                 )') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2687                 $template_string_handle->execute();
2689         }
2693 sub deletetemplate{
2694 #################################################################################
2695 # deletetemplate: Deletes a template from the template database.                #
2696 #                                                                               #
2697 # Usage:                                                                        #
2698 #                                                                               #
2699 # $dbmodule->deletetemplate(options);                                           #
2700 #                                                                               #
2701 # options       Specifies the following options in any order.                   #
2702 #                                                                               #
2703 # TemplateFilename      Specifies the template filename to delete.              #
2704 #################################################################################
2706         $error = "";
2707         $errorext = "";
2709         # Get the data passed to the subroutine.
2711         my $class = shift;
2712         my ($passedoptions) = @_;
2714         my @pagedata;
2715         my $template_filename = $passedoptions->{"TemplateFilename"};
2716         my $template_count = 0;
2718         # Check if the template exists.
2720         $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 );
2721         $template_string_handle->execute();
2723         while (@pagedata = $template_string_handle->fetchrow_array()){
2725                 $template_count++;
2727         }
2729         if ($template_count eq 0){
2731                 # No pages were returned so return an error value.
2733                 $error = "TemplateDoesNotExist";
2734                 return;
2736         }
2738         # Delete the template from the template database.
2740         $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 );
2741         $template_string_handle->execute();
2745 sub edittemplate{
2746 #################################################################################
2747 # editttemplate: Edits a Kiriwrite template.                                    #
2748 #                                                                               #
2749 # Usage:                                                                        #
2750 #                                                                               #
2751 # $dbmodule->edittemplate(options);                                             #
2752 #                                                                               #
2753 # options       Specifies the following options in any order.                   #
2754 #                                                                               #
2755 # TemplateFilename              Specifies the template filename to edit.        #
2756 # NewTemplateFilename           Specifies the new template filename.            #
2757 # NewTemplateName               Specifies the new template name.                #
2758 # NewTemplateDescription        Specifies the new template description.         #
2759 # NewTemplateLayout             Specifies the new template layout.              #
2760 #################################################################################
2762         # Get the values passed.
2764         my $class = shift;
2765         my ($passedoptions) = @_;
2766         my $template_found = 0;
2767         my @template_info;
2769         # Process the values passed.
2771         my $template_filename           = $passedoptions->{"TemplateFilename"};
2772         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
2773         my $new_template_name           = $passedoptions->{"NewTemplateName"};
2774         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
2775         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
2777         # Check if the template database permissions are valid.
2779         my $templatedb_exists = main::kiriwrite_fileexists("templates.db.sqlite", 1, 1);
2780         my $templatedb_permissions = main::kiriwrite_filepermissions("templates.db.sqlite", 1, 1);
2782         if ($templatedb_permissions eq 1){
2784                 if ($templatedb_exists eq 0){
2785                         $error = "TemplateDatabaseInvalidPermissionsSet";
2786                         return;
2787                 }
2789         }
2791         # Check if the template exists.
2793         $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 );
2794         $template_string_handle->execute();
2796         while (@template_info = $template_string_handle->fetchrow_array()){
2798                 $template_found = 1;
2800         }
2802         # Check to see if the template was found and set an error value if
2803         # it wasn't.
2805         if ($template_found eq 0){
2807                 $error = "TemplateDoesNotExist";
2808                 return;
2810         }
2812         # Get the date and time.
2814         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
2815         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
2817         # Update the template information.
2819         $template_string_handle = $template_database_handle->prepare('UPDATE kiriwrite_templates SET
2820                 filename = \'' . $class->convert($new_template_filename) . '\',
2821                 templatename = \'' . $class->convert($new_template_name) . '\',
2822                 templatedescription = \'' . $class->convert($new_template_description) . '\',
2823                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
2824                 datemodified = \'' . $class->convert($templatenewdate) . '\'
2825                 WHERE filename = \'' . $class->convert($template_filename) . '\'
2826         ') or ( $error = "TemplateDatabaseError", $errorext = $template_database_handle->errstr, return );
2827         $template_string_handle->execute();
2831 #################################################################################
2832 # General subroutines.                                                          #
2833 #################################################################################
2835 sub connect{
2836 #################################################################################
2837 # connect: Connect to the server.                                               #
2838 #                                                                               #
2839 # Usage:                                                                        #
2840 #                                                                               #
2841 # $dbmodule->connect();                                                         #
2842 #################################################################################
2844         # This function is not needed in this database module.
2848 sub disconnect{
2849 #################################################################################
2850 # connect: Disconnect from the server.                                          #
2851 #                                                                               #
2852 # Usage:                                                                        #
2853 #                                                                               #
2854 # $dbmodule->disconnect();                                                      #
2855 #################################################################################
2857         # This function is not needed in this database module.
2859         undef $string_handle;
2863 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