Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
More alterations made.
[kiriwrite/.git] / cgi-files / Modules / Database / MySQL5.pm
1 #################################################################################
2 # Kiriwrite Database Module - MySQL 5.x Database Module (MySQL5.pm)             #
3 # Database module for mainipulating data in a MySQL 5.x database.               #
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::MySQL5;
26 # Enable strict and use warnings.
28 use strict;
29 use warnings;
30 use Encode qw(decode_utf8);
32 # Load the following Perl modules.
34 use DBI qw(:sql_types);
36 # Set the following values.
38 our $VERSION    = "0.1.0";
39 my ($options, %options);
40 my $database_handle;
41 my $string_handle;
42 my $error;
43 my $errorext;
44 my $database_filename;
45 my $second_database_filename;
47 #################################################################################
48 # Generic Subroutines.                                                          #
49 #################################################################################
51 sub new{
52 #################################################################################
53 # new: Create an instance of Kiriwrite::Database::MySQL                         #
54 #                                                                               #
55 # Usage:                                                                        #
56 #                                                                               #
57 # $dbmodule = Kiriwrite::Database::SQLite->new();                               #
58 #################################################################################
59         
60         # Get the perl module name.
62         my $class = shift;
63         my $self = {};
65         return bless($self, $class);
67 }
69 sub loadsettings{
70 #################################################################################
71 # loadsettings: Loads settings into the SQLite database module                  #
72 #                                                                               #
73 # Usage:                                                                        #
74 #                                                                               #
75 # $dbmodule->loadsettings(Directory, options);                                  #
76 #                                                                               #
77 # options       Specifies the following options (in any order).                 #
78 #                                                                               #
79 # Directory     Specifies the directory to use for getting databases.           #
80 # DateTime      Specifies the date and time format to use.                      #
81 # Server        Specifies the server to use.                                    #
82 # Database      Specifies the database to use.                                  #
83 # Username      Specifies the username to use.                                  #
84 # Password      Specifies the password to use.                                  #
85 # Port          Specifies the server port to use.                               #
86 # Protocol      Specifies the protocol to use.                                  #
87 # TablePrefix   Specifies the table prefix to use.                              #
88 #################################################################################
90         # Get the data passed to the subroutine.
92         my $class = shift;
93         my ($passedoptions)     = @_;
95         # Add the directory setting to the list of options (as it's the only
96         # one needed for this database module).
98         %options = (
99                 "Directory"     => $passedoptions->{"Directory"},
100                 "DateTime"      => $passedoptions->{"DateTime"},
101                 "Server"        => $passedoptions->{"Server"},
102                 "Database"      => $passedoptions->{"Database"},
103                 "Username"      => $passedoptions->{"Username"},
104                 "Password"      => $passedoptions->{"Password"},
105                 "Port"          => $passedoptions->{"Port"},
106                 "Protocol"      => $passedoptions->{"Protocol"},
107                 "TablePrefix"   => $passedoptions->{"TablePrefix"}
108         );
112 sub convert{
113 #################################################################################
114 # convert: Converts data into SQL formatted data.                               #
115 #                                                                               #
116 # Usage:                                                                        #
117 #                                                                               #
118 # $dbmodule->convert(data);                                                     #
119 #                                                                               #
120 # data          Specifies the data to convert.                                  #
121 #################################################################################
123         # Get the data passed to the subroutine.
125         my $class       = shift;
126         my $data        = shift;
128         if (!$data){
129                 $data = "";
130         }
132         $data =~ s/'/''/g;
133         $data =~ s/\b//g;
135         return $data;
139 sub dateconvert{
140 #################################################################################
141 # dateconvert: Converts a SQL date into a proper date.                          #
142 #                                                                               #
143 # Usage:                                                                        #
144 #                                                                               #
145 # $dbmodule->dateconvert(date);                                                 #
146 #                                                                               #
147 # date          Specifies the date to convert.                                  #
148 #################################################################################
150         # Get the date passed to the subroutine.
152         my $class       = shift;
153         my $data        = shift;
155         # Convert the date given into the proper date.
157         # Create the following varialbes to be used later.
159         my $date;
160         my $time;
161         my $day;
162         my $day_full;
163         my $month;
164         my $month_check;
165         my $month_full;
166         my $year;
167         my $year_short;
168         my $hour;
169         my $hour_full;
170         my $minute;
171         my $minute_full;
172         my $second;
173         my $second_full;
174         my $seek = 0;
175         my $timelength;
176         my $datelength;
177         my $daylength;
178         my $secondlength;
179         my $startchar = 0;
180         my $char;
181         my $length;
182         my $count = 0;
184         # Split the date and time.
186         $length = length($data);
188         if ($length > 0){
190                 do {
192                         # Get the character and check if it is a space.
194                         $char = substr($data, $seek, 1);
196                         if ($char eq ' '){
198                                 # The character is a space, so get the date and time.
200                                 $date           = substr($data, 0, $seek);
201                                 $timelength     = $length - $seek - 1;
202                                 $time           = substr($data, $seek + 1, $timelength);
204                         }
206                         $seek++;
208                 } until ($seek eq $length);
210                 # Get the year, month and date.
212                 $length = length($date);
213                 $seek = 0;
215                 do {
217                         # Get the character and check if it is a dash.
219                         $char = substr($date, $seek, 1);
221                         if ($char eq '-'){
223                                 # The character is a dash, so get the year, month or day.
225                                 $datelength = $seek - $startchar;
227                                 if ($count eq 0){
229                                         # Get the year from the date.
231                                         $year           = substr($date, 0, $datelength) + 1900;
232                                         $startchar      = $seek;
233                                         $count = 1;
235                                         # Get the last two characters to get the short year
236                                         # version.
238                                         $year_short     = substr($year, 2, 2);
240                                 } elsif ($count eq 1){
242                                         # Get the month and day from the date.
244                                         $month  = substr($date, $startchar + 1, $datelength - 1) + 1;
246                                         # Check if the month is less then 10, if it is
247                                         # add a zero to the value.
249                                         if ($month < 10){
251                                                 $month_full = '0' . $month;
253                                         } else {
255                                                 $month_full = $month;
257                                         }
259                                         $startchar      = $seek;
260                                         $count = 2;
262                                         $daylength      = $length - $seek + 1;
263                                         $day            = substr($date, $startchar + 1, $daylength);
265                                         $day =~ s/^0//;
267                                         # Check if the day is less than 10, if it is
268                                         # add a zero to the value.
270                                         if ($day < 10){
272                                                 $day_full       = '0' . $day;
274                                         } else {
276                                                 $day_full       = $day;
278                                         }
280                                 }
282                         }
284                         $seek++;
286                 } until ($seek eq $length);
288                 # Get the length of the time value and reset certain
289                 # values to 0.
291                 $length = length($time);
292                 $seek = 0;
293                 $count = 0;
294                 $startchar = 0;
296                 do {
298                         # Get the character and check if it is a colon.
300                         $char = substr($time, $seek, 1);
302                         if ($char eq ':'){
304                                 # The character is a colon, so get the hour, minute and day.
306                                 $timelength = $seek - $startchar;
308                                 if ($count eq 0){
310                                         # Get the hour from the time.
312                                         $hour = substr($time, 0, $timelength);
313                                         $hour =~ s/^0//;
314                                         $count = 1;
315                                         $startchar = $seek;
317                                         # If the hour is less than ten then add a
318                                         # zero.
320                                         if ($hour < 10){
322                                                 $hour_full = '0' . $hour;
324                                         } else {
326                                                 $hour_full = $hour;
328                                         }
330                                 } elsif ($count eq 1){
332                                         # Get the minute and second from the time.
334                                         $minute = substr($time, $startchar + 1, $timelength - 1);
335                                         $minute =~ s/^0//;
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                                         $second =~ s/^0//;
356                                         
357                                         # If the second is less than ten then add a
358                                         # zero.
360                                         if ($second < 10){
362                                                 $second_full = '0' . $second;
364                                         } else {
366                                                 $second_full = $second;
368                                         }
370                                 }
372                         }
374                         $seek++;
376                 } until ($seek eq $length);
378                 # Get the setting for displaying the date and time.
380                 $data = $options{"DateTime"};
382                 # Process the setting for displaying the date and time
383                 # using regular expressions
385                 $data =~ s/DD/$day_full/g;
386                 $data =~ s/D/$day/g;
387                 $data =~ s/MM/$month_full/g;
388                 $data =~ s/M/$month/g;
389                 $data =~ s/YY/$year/g;
390                 $data =~ s/Y/$year_short/g;
392                 $data =~ s/hh/$hour_full/g;
393                 $data =~ s/h/$hour/g;
394                 $data =~ s/mm/$minute_full/g;
395                 $data =~ s/m/$minute/g;
396                 $data =~ s/ss/$second_full/g;
397                 $data =~ s/s/$second/g;
399         }
401         return $data;
405 sub geterror{
406 #################################################################################
407 # geterror: Gets the error message (or extended error message).                 #
408 #                                                                               #
409 # Usage:                                                                        #
410 #                                                                               #
411 # $dbmodule->geterror(extended);                                                #
412 #                                                                               #
413 # Extended      Specifies if the extended error should be retrieved.            #
414 #################################################################################
416         # Get the data passed to the subroutine.
418         my $class       = shift;
419         my $extended    = shift;
421         if (!$extended){
422                 $extended = 0;
423         }
425         if (!$errorext){
426                 $errorext = "";
427         }
429         if (!$error){
430                 $error = "";
431         }
433         # Check to see if extended information should be returned.
435         if ($extended eq 1){
437                 # Extended information should be returned.
439                 return $errorext;
441         } else {
443                 # Basic information should be returned.
445                 return $error;
447         }
451 sub dbpermissions{
452 #################################################################################
453 # dbpermissions: Check if the permissions for the database are valid.           #
454 #                                                                               #
455 # Usage:                                                                        #
456 #                                                                               #
457 # $database->dbpermissions(dbname, read, write);                                #
458 #                                                                               #
459 # dbname        Specifies the database name to check.                           #
460 # read          Check to see if the database can be read.                       #
461 # write         Check to see if the database can be written.                    #
462 #################################################################################
464         # This subroutine is not needed for this database module.
466         return 0;
470 sub dbexists{
471 #################################################################################
472 # dbexists: Check if the database exists.                                       #
473 #                                                                               #
474 # Usage:                                                                        #
475 #                                                                               #
476 # $dbmodule->dbexists(dbname);                                                  #
477 #                                                                               #
478 # dbname        Specifies the database name to check.                           #
479 #################################################################################
481         $error = "";
482         $errorext = "";
484         # Get the value that was passed to the subroutine.
486         my $class       = shift;
487         my ($filename)  = @_;
489         my @table_data;
490         my $table_exists = 0;
492         # Check if the table exists.
494         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($filename) . '_database_info\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
495         $string_handle->execute();
497         while (@table_data = $string_handle->fetchrow_array()){
499                 $table_exists = 1;
501         }
503         # Check if the table really does exist.
505         if ($table_exists eq 1){
507                 # The table exists so return a value of 0.
509                 return 0;
511         } else {
513                 # The table does not exist so return a value of 1.
515                 return 1;
517         }
522 #################################################################################
523 # General subroutines.                                                          #
524 #################################################################################
526 sub connect{
527 #################################################################################
528 # connect: Connect to the server.                                               #
529 #                                                                               #
530 # Usage:                                                                        #
531 #                                                                               #
532 # $dbmodule->connect();                                                         #
533 #################################################################################
535         $error = "";
536         $errorext = "";
538         # Connect to the server.
540         $database_handle = DBI->connect("DBI:mysql:database=" . $options{"Database"} . ";host=" . $options{"Server"} . ";protocol=" . $options{"Protocol"} . "port=" . $options{"Port"}, $options{"Username"}, $options{"Password"}, { "mysql_enable_utf8" => 1 }) or ( $error = "DatabaseConnectionError", $errorext = DBI->errstr, return );
541         $database_handle->do('SET CHARACTER SET utf8');
542         $database_handle->do('SET NAMES utf8');
546 sub disconnect{
547 #################################################################################
548 # connect: Disconnect from the server.                                          #
549 #                                                                               #
550 # Usage:                                                                        #
551 #                                                                               #
552 # $dbmodule->disconnect();                                                      #
553 #################################################################################
554         
555         # Disconnect from the server.
557         if ($string_handle){
559                 $string_handle->finish();
561         }
563         if ($database_handle){
565                 $database_handle->disconnect();
567         }
571 #################################################################################
572 # Database Subroutines.                                                         #
573 #################################################################################
575 sub getdblist{
576 #################################################################################
577 # getdblist: Gets the list of available databases.                              #
578 #                                                                               #
579 # Usage:                                                                        #
580 #                                                                               #
581 # $dbmodule->getdblist();                                                       #
582 #################################################################################
584         $error = "";
585         $errorext = "";
587         # Get the list of databases.
589         $string_handle = $database_handle->prepare("SHOW TABLES") or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
590         $string_handle->execute();
592         my @final_table_list;   
593         my @database_table_list;
594         my @table_name;
595         my $table;
597         while (@table_name = $string_handle->fetchrow_array()){
599                 push(@database_table_list, decode_utf8($table_name[0]));
601         }
603         my $table_prefix = $options{"TablePrefix"};
605         # Find all the database information tables with the correct table prefix.
607         @database_table_list = grep /^$table_prefix/ , @database_table_list;
608         @database_table_list = grep /m*database_info$/ , @database_table_list;
610         foreach $table (@database_table_list){
612                 # Process each table name removing the table prefix name and
613                 # the _database_info part.
615                 $table =~ s/^$table_prefix(_)//g;
616                 $table =~ s/_database_info$//g;
618                 push (@final_table_list, $table);
620         }
622         # Return the final list of databases.
624         return @final_table_list;
628 sub selectdb{
629 #################################################################################
630 # selectdb: Selects the Kiriwrite database.                                     #
631 #                                                                               #
632 # Usage:                                                                        #
633 #                                                                               #
634 # $dbmodule->connect(options);                                                  #
635 #                                                                               #
636 # options       Specifies the following options in any order.                   #
637 #                                                                               #
638 # DatabaseName  Specifies the Kiriwrite database to use.                        #
639 #################################################################################
641         # Get the database name.
643         $error = "";
644         $errorext = "";
646         my $class = shift;
647         my ($passedoptions) = @_;
649         my $dbname = $passedoptions->{"DatabaseName"};
651         my $database_exists = $class->dbexists($dbname);
653         if ($database_exists eq 1){
655                 # The database does not exist so return an error value
656                 # saying that the database does not exist.
658                 $error = "DoesNotExist";
660                 return;
662         }
664         $database_filename = $dbname;
668 sub getdatabaseinfo{
669 #################################################################################
670 # getdatabaseinfo: Get information about the database.                          #
671 #                                                                               #
672 # Usage:                                                                        #
673 #                                                                               #
674 # $dbmodule->getdatabaseinfo();                                                 #
675 #################################################################################
677         # Get the database information.
679         $error = "";
680         $errorext = "";
682         my $class = shift;
683         my ($databaseinfo, %databaseinfo);
684         my ($sqldata, @sqldata);
686         $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
687         $string_handle->execute();
689         @sqldata = $string_handle->fetchrow_array();
691         # Process the database information into a hash.
693         %databaseinfo = (
695                 "DatabaseName"  => decode_utf8($sqldata[0]),
696                 "Description"   => decode_utf8($sqldata[1]),
697                 "Notes"         => decode_utf8($sqldata[2]),
698                 "Categories"    => decode_utf8($sqldata[3]),
699                 "Major"         => decode_utf8($sqldata[4]),
700                 "Minor"         => decode_utf8($sqldata[5]),
701                 "Revision"      => decode_utf8($sqldata[6])
702         );
704         $string_handle->finish();
706         return %databaseinfo;
710 sub selectseconddb{
711 #################################################################################
712 # selectseconddb: Selects a second Kiriwrite database for moving and copying    #
713 # pages to.                                                                     #
714 #                                                                               #
715 # Usage:                                                                        #
716 #                                                                               #
717 # $dbmodule->selectseconddb(options);                                           #
718 #                                                                               #
719 # options       Specifies the following options in any order.                   #
720 #                                                                               #
721 # DatabaseName  Specifies the Kiriwrite database to use.                        #
722 #################################################################################
724         # Get the database name.
726         $error = "";
727         $errorext = "";
729         my $class = shift;
730         my ($passedoptions) = @_;
731         my (%database, $database);
733         my $dbname = $passedoptions->{"DatabaseName"};
735         # Check if the database exists.
737         my $database_exists = $class->dbexists($dbname);
739         if ($database_exists eq 1){
741                 # The database does not exist so return an error value
742                 # saying that the database does not exist.
744                 $error = "DoesNotExist";
746                 return;
748         }
750         # Set the second database filename.
752         $second_database_filename = $dbname;
756 sub getseconddatabaseinfo{
757 #################################################################################
758 # getseconddatabaseinfo: Get information about the database that pages will be  #
759 # moved or copied to.                                                           #
760 #                                                                               #
761 # Usage:                                                                        #
762 #                                                                               #
763 # $dbmodule->getseconddatabaseinfo();                                           #
764 #################################################################################
767         # Get the database information.
769         my $class = shift;
770         my ($databaseinfo, %databaseinfo);
771         my ($sqldata, @sqldata);
773         $error = "";
774         $errorext = "";
776         $string_handle = $database_handle->prepare('SELECT name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
777         $string_handle->execute();
779         @sqldata = $string_handle->fetchrow_array();
781         # Process the database information into a hash.
783         %databaseinfo = (
784                 "DatabaseName"  => decode_utf8($sqldata[0]),
785                 "Description"   => decode_utf8($sqldata[1]),
786                 "Notes"         => decode_utf8($sqldata[2]),
787                 "Categories"    => decode_utf8($sqldata[3]),
788                 "Major"         => decode_utf8($sqldata[4]),
789                 "Minor"         => decode_utf8($sqldata[5]),
790                 "Revision"      => decode_utf8($sqldata[6])
791         );
793         $string_handle->finish();
795         return %databaseinfo;
799 sub adddatabase{
800 #################################################################################
801 # adddatabase: Adds a Kiriwrite database.                                       #
802 #                                                                               #
803 # Usage:                                                                        #
804 #                                                                               #
805 # $dbmodule->adddatabase(options);                                              #
806 #                                                                               #
807 # options       Specifies the following options in any order.                   #
808 #                                                                               #
809 # DatabaseFilename      Specifies the database file/shortname to use.           #
810 # DatabaseName          Specifies the database name to use.                     #
811 # DatabaseDescription   Specifies the database description to use.              #
812 # DatabaseNotes         Specifies the database notes to use.                    #
813 # DatabaseCategories    Specifies the database categories to use.               #
814 # VersionMajor          Specifies the major version.                            #
815 # VersionMinor          Specifies the minor version.                            #
816 # VersionRevision       Specifies the revision version.                         #
817 #################################################################################
819         # Get the database that was passed to the subroutine.
821         $error  = "";
822         $errorext = "";
824         my $class       = shift;
825         my ($passedoptions) = @_;
827         my $dbfilename          = $passedoptions->{"DatabaseFilename"};
828         my $dbname              = $passedoptions->{"DatabaseName"};
829         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
830         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
831         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
832         my $dbmajorver          = $passedoptions->{"VersionMajor"};
833         my $dbminorver          = $passedoptions->{"VersionMinor"};
834         my $dbrevisionver       = $passedoptions->{"VersionRevision"};
836         # Check if the database with the filename given already exists.
838         my $database_exists     = $class->dbexists($dbfilename);
840         if ($database_exists eq 0){
842                 # The database filename exists so set the error value.
844                 $error = "DatabaseExists";
845                 return;
847         }
849         # Create the database structure (info and page tables);
851         $string_handle  = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (
852                         name varchar(256) primary key,
853                         description varchar(512), 
854                         notes mediumtext,
855                         categories varchar(512), 
856                         kiriwrite_version_major int(4), 
857                         kiriwrite_version_minor int(4), 
858                         kiriwrite_version_revision int(4)
859         ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
860         $string_handle->execute();
862         $string_handle  = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_pages (
863                         filename varchar(256) primary key, 
864                         pagename varchar(512), 
865                         pagedescription varchar(512), 
866                         pagesection varchar(256),
867                         pagetemplate varchar(64),
868                         pagedata mediumtext,
869                         pagesettings int(1),
870                         lastmodified datetime
871         ) DEFAULT CHARSET=utf8') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
872         $string_handle->execute();
874         # Convert the values into SQL query formatted values and add an entry
875         # to the kiriwrite_database_info table.
877         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbfilename) . '_database_info (name, description, notes, categories, kiriwrite_version_major, kiriwrite_version_minor, kiriwrite_version_revision) VALUES(
878                 \'' . $class->convert($dbname) . '\',
879                 \'' . $class->convert($dbdescription) . '\',
880                 \'' . $class->convert($dbnotes) . '\',
881                 \'' . $class->convert($dbcategories) . '\',
882                 \'' . $class->convert($dbmajorver) . '\',
883                 \'' . $class->convert($dbminorver) . '\',
884                 \'' . $class->convert($dbrevisionver) . '\'
885         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
886         $string_handle->execute();
890 sub editdatabase{
891 #################################################################################
892 # editdatabase: Edits a Kiriwrite Database.                                     #
893 #                                                                               #
894 # Usage:                                                                        #
895 #                                                                               #
896 # $dbmodule->editdatabase(options);                                             #
897 #                                                                               #
898 # options               Specifies the following options in any order.           #
899 #                                                                               #
900 # NewDatabaseFilename   Specifies the new database filename to use.             #
901 # DatabaseName          Specifies the new database name.                        #
902 # DatabaseDescription   Specifies the new database description.                 #
903 # DatabaseNotes         Specifies the new database notes.                       #
904 # DatabaseCategories    Specifies the new database categories.                  #
905 #################################################################################
907         $error          = "";
908         $errorext       = "";
910         my $class       = shift;
911         my ($passedoptions) = @_;
913         my $dbnewfilename       = $passedoptions->{"DatabaseNewFilename"};
914         my $dbname              = $passedoptions->{"DatabaseName"};
915         my $dbdescription       = $passedoptions->{"DatabaseDescription"};
916         my $dbnotes             = $passedoptions->{"DatabaseNotes"};
917         my $dbcategories        = $passedoptions->{"DatabaseCategories"};
919         # Check if a new database filename has been specified and if a
920         # new database filename has been specified then change the
921         # database filename.
923         if ($database_filename ne $dbnewfilename){
925                 # Check if a table with the filename already exists before using the
926                 # new filename.
928                 my $database_newexists          = $class->dbexists($dbnewfilename);
930                 if ($database_newexists eq 0){
932                         # The database filename exists so set the error value.
934                         $error = "DatabaseExists";
935                         return;
937                 }
939                 # Rename the tables.
941                 $string_handle = $database_handle->prepare('RENAME TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_info TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info, ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages TO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_pages');
942                 $string_handle->execute();
944         }
946         # Get the current database information.
948         $string_handle = $database_handle->prepare('SELECT name FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
949         $string_handle->execute();
951         my @database_oldinfo    = $string_handle->fetchrow_array();
953         my $dboldname           = decode_utf8($database_oldinfo[0]);
955         # Update the database information.
957         $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($dbnewfilename) . '_database_info SET name = \'' . $class->convert($dbname) . '\',
958         description = \'' . $class->convert($dbdescription) . '\',
959         notes = \'' . $class->convert($dbnotes) . '\',
960         categories = \'' . $class->convert($dbcategories) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
961         $string_handle->execute();
965 sub deletedatabase{
966 #################################################################################
967 # deletedatabase: Deletes a Kiriwrite database.                                 #
968 #                                                                               #
969 # Usage:                                                                        #
970 #                                                                               #
971 # $dbmodule->deletedatabase(options);                                           #
972 #                                                                               #
973 # options       Specifies the following options in any order.                   #
974 #                                                                               #
975 # DatabaseName  Specifies the Kiriwrite database to delete.                     #
976 #################################################################################
978         $error          = "";
979         $errorext       = "";
981         # Get the database filename.
983         my $class               = shift;
984         my ($passedoptions)     = shift;
986         my $databasename        = $passedoptions->{"DatabaseName"};
988         my @table_data;
989         my $table_exists;
991         # Check if the database with the filename given already exists.
993         my $database_exists     = $class->dbexists($databasename);
995         if ($database_exists eq 1){
997                 # The database does not exist so set the error value.
999                 $error = "DoesNotExist";
1000                 return;
1002         }
1006         # Delete the database tables.
1008         $string_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_info')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1009         $string_handle->execute();
1011         # Check if the _database_pages table exists and delete it if it exists.
1013         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages\'')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1014         $string_handle->execute();
1016         while (@table_data = $string_handle->fetchrow_array()){
1018                 $table_exists = 1;
1020         }
1022         # Check if the _database_pages table really does exist.
1024         if ($table_exists eq 1){
1026                 # the _database_pages table really does exist so delete it.
1028                 $string_handle = $database_handle->prepare('DROP TABLE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($databasename) . '_database_pages')  or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1029                 $string_handle->execute();
1031         }
1035 #################################################################################
1036 # Template subroutines.                                                         #
1037 #################################################################################
1039 sub connecttemplate{
1040 #################################################################################
1041 # connecttemplate: Connect to the template database.                            #
1042 #                                                                               #
1043 # Usage:                                                                        #
1044 #                                                                               #
1045 # $dbmodule->connecttemplate(missingignore);                                    #
1046 #                                                                               #
1047 # missingignore Ignore errror about database being missing.                     #
1048 #################################################################################
1050         $error = "";
1051         $errorext = "";
1053         my $class = shift;
1054         my $ignoremissing = shift;
1055         my $templatedb_exists = 0;
1056         my @templatedb_check;
1058         # Check if the template database exists.
1060         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1061         $string_handle->execute();
1063         while (@templatedb_check = $string_handle->fetchrow_array()){
1065                 $templatedb_exists = 1;
1067         }
1069         if (!$templatedb_exists){
1071                 if (!$ignoremissing){
1073                         $error = "TemplateDatabaseDoesNotExist";
1074                         return;
1076                 }
1078         }
1082 sub disconnecttemplate{
1083 #################################################################################
1084 # disconnecttemplate: Disconnect from the template database.                    #
1085 #                                                                               #
1086 # Usage:                                                                        #
1087 #                                                                               #
1088 # $dbmodule->disconnecttemplate();                                              #
1089 #################################################################################
1091         # This subroutine is not used.
1095 sub gettemplatelist{
1096 #################################################################################
1097 # gettemplatelist: Gets the list of templates.                                  #
1098 #                                                                               #
1099 # Usage:                                                                        #
1100 #                                                                               #
1101 # $dbmodule->gettemplatelist();                                                 #
1102 #################################################################################
1104         my $error = "";
1105         my $errorext = "";
1107         my $class = shift;
1109         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates ORDER BY filename ASC') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1110         $string_handle->execute();
1112         my @database_template;
1113         my @templates_list;
1114         my $template_filename;
1116         while (@database_template = $string_handle->fetchrow_array()){
1118                 # Get certain values from the array.
1120                 $template_filename      = decode_utf8($database_template[0]);
1122                 # Add the template to the list of templates.
1124                 push(@templates_list, $template_filename);
1126         }
1128         return @templates_list;
1132 sub gettemplateinfo{
1133 #################################################################################
1134 # gettemplateinfo: Get information on a template.                               #
1135 #                                                                               #
1136 # Usage:                                                                        #
1137 #                                                                               #
1138 # $dbmodule->gettemplateinfo(options);                                          #
1139 #                                                                               #
1140 # options       Specifies the following options in any order.                   #
1141 #                                                                               #
1142 # TemplateFilename      Specifies the template filename to use.                 #
1143 #################################################################################
1145         $error = "";
1146         $errorext = "";
1148         # Get the data passed to the subroutine.
1150         my $class = shift;
1151         my ($passedoptions) = @_;
1153         my %page_info;
1154         my @template_data;
1156         my $template_filename;
1157         my $template_name;
1158         my $template_description;
1159         my $template_datemodified;
1160         my $template_layout;
1162         my $template_found = 0;
1164         my $filename    = $passedoptions->{"TemplateFilename"};
1166         $string_handle = $database_handle->prepare('SELECT filename, templatename, templatedescription, templatelayout, datemodified FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1167         $string_handle->execute();
1169         while (@template_data = $string_handle->fetchrow_array()){
1171                 # Get certain values from the array.
1173                 $template_filename      = decode_utf8($template_data[0]);
1174                 $template_name          = decode_utf8($template_data[1]);
1175                 $template_description   = decode_utf8($template_data[2]);
1176                 $template_layout        = decode_utf8($template_data[3]);
1177                 $template_datemodified  = decode_utf8($template_data[4]);
1179                 # Process them into the hash.
1181                 %page_info = (
1182                         "TemplateFilename" => $template_filename,
1183                         "TemplateName" => $template_name,
1184                         "TemplateDescription" => $template_description,
1185                         "TemplateLayout" => $template_layout,
1186                         "TemplateLastModified" => $template_datemodified
1187                 );
1189                 $template_found = 1;
1191         }
1193         if ($template_found eq 0){
1195                 # The template was not found in the template database so
1196                 # write an error value.
1198                 $error = "TemplateDoesNotExist";
1199                 return;
1201         }
1203         return %page_info;      
1208 sub addtemplate{
1209 #################################################################################
1210 # addtemplate: Adds a template to the template database.                        #
1211 #                                                                               #
1212 # Usage:                                                                        #
1213 #                                                                               #
1214 # $dbmodule->addtemplate(options);                                              #
1215 #                                                                               #
1216 # options       Specifies the following options in any order.                   #
1217 #                                                                               #
1218 # TemplateFilename      Specifies the new template filename.                    #
1219 # TemplateName          Specifies the new template name.                        #
1220 # TemplateDescription   Specifies the new template description.                 #
1221 # TemplateLayout        Specifies the new template layout.                      #
1222 #################################################################################
1224         $error = "";
1225         $errorext = "";
1227         # Get the data passed to the subroutine.
1229         my $class = shift;
1230         my ($passedoptions) = @_;
1232         my @page_exists;
1233         my @templatedb_check;
1234         my $templatedb_exists;
1235         my $blankfile = 0;
1237         my $template_filename           = $passedoptions->{"TemplateFilename"};
1238         my $template_name               = $passedoptions->{"TemplateName"};
1239         my $template_description        = $passedoptions->{"TemplateDescription"};
1240         my $template_layout             = $passedoptions->{"TemplateLayout"};
1242         # Check if the template database exists.
1244         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_templates\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1245         $string_handle->execute();
1247         while (@templatedb_check = $string_handle->fetchrow_array()){
1249                 $templatedb_exists = 1;
1251         }
1253         # Check if the template database table exists and if it doesn't
1254         # then create the template database table.
1256         if (!$templatedb_exists){
1258                 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_templates (
1259                         filename varchar(256) primary key,
1260                         templatename varchar(512),
1261                         templatedescription varchar(512),
1262                         templatelayout mediumtext,
1263                         datemodified datetime
1264                 ) DEFAULT CHARSET=utf8') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1265                 $string_handle->execute();
1267         }
1269         # Check if the template already exists before adding.
1271         if (!$templatedb_exists){
1273                 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ($blankfile = 1);
1275                 if ($blankfile eq 0){
1277                         $string_handle->execute();
1279                         while (@page_exists = $string_handle->fetchrow_array()){
1281                                 $error = "TemplatePageExists";
1282                                 return;
1284                         }
1286                 }
1288         }
1290         # Get the current date.
1291  
1292         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1293  
1294         my $template_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1296         # Check if certain values are undefined and if they
1297         # are then set them blank.
1299         if (!$template_name){
1301                 $template_name = "";
1303         }
1305         if (!$template_description){
1307                 $template_description = "";
1309         }
1311         if (!$template_layout){
1313                 $template_layout = "";
1315         }
1317         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_templates (filename, templatename, templatedescription, templatelayout, datemodified) VALUES(
1318                         \'' . $class->convert($template_filename) . '\',
1319                         \'' . $class->convert($template_name) . '\',
1320                         \'' . $class->convert($template_description) . '\',
1321                         \'' . $class->convert($template_layout) . '\',
1322                         \'' . $class->convert($template_date) . '\'
1323         )') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1324         $string_handle->execute();
1328 sub edittemplate{
1329 #################################################################################
1330 # editttemplate: Edits a Kiriwrite template.                                    #
1331 #                                                                               #
1332 # Usage:                                                                        #
1333 #                                                                               #
1334 # $dbmodule->edittemplate(options);                                             #
1335 #                                                                               #
1336 # options       Specifies the following options in any order.                   #
1337 #                                                                               #
1338 # TemplateFilename              Specifies the template filename to edit.        #
1339 # NewTemplateFilename           Specifies the new template filename.            #
1340 # NewTemplateName               Specifies the new template name.                #
1341 # NewTemplateDescription        Specifies the new template description.         #
1342 # NewTemplateLayout             Specifies the new template layout.              #
1343 #################################################################################
1345         # Get the values passed.
1347         my $class = shift;
1348         my ($passedoptions) = @_;
1349         my $template_found = 0;
1350         my @template_info;
1352         # Process the values passed.
1354         my $template_filename           = $passedoptions->{"TemplateFilename"};
1355         my $new_template_filename       = $passedoptions->{"NewTemplateFilename"};
1356         my $new_template_name           = $passedoptions->{"NewTemplateName"};
1357         my $new_template_description    = $passedoptions->{"NewTemplateDescription"};
1358         my $new_template_layout         = $passedoptions->{"NewTemplateLayout"};
1360         # Check if the template exists.
1362         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1363         $string_handle->execute();
1365         while (@template_info = $string_handle->fetchrow_array()){
1367                 $template_found = 1;
1369         }
1371         # Check to see if the template was found and set an error value if
1372         # it wasn't.
1374         if ($template_found eq 0){
1376                 $error = "TemplateDoesNotExist";
1377                 return;
1379         }
1381         # Get the date and time.
1383         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1384         my $templatenewdate = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1386         # Update the template information.
1388         $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_templates SET
1389                 filename = \'' . $class->convert($new_template_filename) . '\',
1390                 templatename = \'' . $class->convert($new_template_name) . '\',
1391                 templatedescription = \'' . $class->convert($new_template_description) . '\',
1392                 templatelayout = \'' . $class->convert($new_template_layout) . '\',
1393                 datemodified = \'' . $class->convert($templatenewdate) . '\'
1394                 WHERE filename = \'' . $class->convert($template_filename) . '\'
1395         ') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1396         $string_handle->execute();
1400 sub deletetemplate{
1401 #################################################################################
1402 # deletetemplate: Deletes a template from the template database.                #
1403 #                                                                               #
1404 # Usage:                                                                        #
1405 #                                                                               #
1406 # $dbmodule->deletetemplate(options);                                           #
1407 #                                                                               #
1408 # options       Specifies the following options in any order.                   #
1409 #                                                                               #
1410 # TemplateFilename      Specifies the template filename to delete.              #
1411 #################################################################################
1413         $error = "";
1414         $errorext = "";
1416         # Get the data passed to the subroutine.
1418         my $class = shift;
1419         my ($passedoptions) = @_;
1421         my @pagedata;
1422         my $template_filename = $passedoptions->{"TemplateFilename"};
1423         my $template_count = 0;
1425         # Check if the template exists.
1427         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\' LIMIT 1') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1428         $string_handle->execute();
1430         while (@pagedata = $string_handle->fetchrow_array()){
1432                 $template_count++;
1434         }
1436         if ($template_count eq 0){
1438                 # No pages were returned so return an error value.
1440                 $error = "TemplateDoesNotExist";
1441                 return;
1443         }
1445         # Delete the template from the template database.
1447         $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_templates WHERE filename = \'' . $class->convert($template_filename) . '\'') or ( $error = "TemplateDatabaseError", $errorext = $database_handle->errstr, return );
1448         $string_handle->execute();
1452 #################################################################################
1453 # Page subroutines.                                                             #
1454 #################################################################################
1456 sub getpagelist{
1457 #################################################################################
1458 # getpagelist: Gets the list of pages from the database.                        #
1459 #                                                                               #
1460 # Usage:                                                                        #
1461 #                                                                               #
1462 # $dbmodule->getpagelist();                                                     #
1463 #################################################################################
1465         $error = "";
1466         $errorext = "";
1468         my $class       = shift;
1469         
1470         $string_handle  = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1471         $string_handle->execute();
1473         my @database_pagefilenames;
1474         my @database_pagefilenames_final;
1476         # Process the collected pages.
1478         while (@database_pagefilenames = $string_handle->fetchrow_array){
1480                 # Add each page to the list of pages in the database.
1482                 push(@database_pagefilenames_final, decode_utf8($database_pagefilenames[0]));
1484         }
1486         return @database_pagefilenames_final;   
1490 sub getpageinfo{
1491 #################################################################################
1492 # getpageinfo: Gets the page information from the filename passed.              #
1493 #                                                                               #
1494 # Usage:                                                                        #
1495 #                                                                               #
1496 # $dbmodule->getpageinfo(options);                                              #
1497 #                                                                               #
1498 # options       Specifies the following options in any order.                   #
1499 #                                                                               #
1500 # PageFilename  Specifies the page filename to get the page information from.   #
1501 #################################################################################
1503         $error = "";
1504         $errorext = "";
1506         my $class               = shift;
1507         my ($passedoptions)     = shift;
1508         my (%database_page, $database_page);
1509         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1511         my @data_page;
1512         my $page_found = 0;
1514         # Get the page from the database.
1516         my $page_filename       = $passedoptions->{"PageFilename"};
1518         $string_handle  = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1519         $string_handle->execute();
1521         # Check if the page exists in the database.
1523         while (@data_page = $string_handle->fetchrow_array()){
1525                 # Get the values from the array.
1527                 $pagefilename           = decode_utf8($data_page[0]);
1528                 $pagename               = decode_utf8($data_page[1]);
1529                 $pagedescription        = decode_utf8($data_page[2]);
1530                 $pagesection            = decode_utf8($data_page[3]);
1531                 $pagetemplate           = decode_utf8($data_page[4]);
1532                 $pagedata               = decode_utf8($data_page[5]);
1533                 $pagesettings           = decode_utf8($data_page[6]);
1534                 $pagelastmodified       = decode_utf8($data_page[7]);
1536                 # Put the values into the page hash.
1538                 %database_page = (
1539                         "PageFilename"          => $pagefilename,
1540                         "PageName"              => $pagename,
1541                         "PageDescription"       => $pagedescription,
1542                         "PageSection"           => $pagesection,
1543                         "PageTemplate"          => $pagetemplate,
1544                         "PageContent"           => $pagedata,
1545                         "PageSettings"          => $pagesettings,
1546                         "PageLastModified"      => $class->dateconvert($pagelastmodified),
1547                 );
1549                 $page_found = 1;
1551         }
1552         
1553         # Check if the page did exist.
1555         if (!$page_found){
1557                 $error = "PageDoesNotExist";
1558                 return;
1560         }
1562         return %database_page;
1567 sub addpage{
1568 #################################################################################
1569 # addpage: Add a page to the selected database.                                 #
1570 #                                                                               #
1571 # Usage:                                                                        #
1572 #                                                                               #
1573 # $dbmodule->addpage(options);                                                  #
1574 #                                                                               #
1575 # options       Specifies the following options in any order.                   #
1576 #                                                                               #
1577 # PageFilename          Specifies the page filename to use.                     #
1578 # PageName              Specifies the page name to use.                         #
1579 # PageDescription       Specifies the page description to use.                  #
1580 # PageSection           Specifies the page section to use.                      #
1581 # PageTemplate          Specifies the page template to use.                     #
1582 # PageContent           Specifies the page content to use.                      #
1583 # PageSettings          Specifies the page settings to use.                     #
1584 #################################################################################
1586         # Get the data that was passed to the subroutine.
1588         $error = "";
1589         $errorext = "";
1591         my $class               = shift;
1592         my ($passedoptions)     = shift;
1594         my @database_page;
1595         my $page_count = 0;
1597         # Get the values passed to the hash.
1599         my $page_filename       = $passedoptions->{"PageFilename"};
1600         my $page_name           = $passedoptions->{"PageName"};
1601         my $page_description    = $passedoptions->{"PageDescription"};
1602         my $page_section        = $passedoptions->{"PageSection"};
1603         my $page_template       = $passedoptions->{"PageTemplate"};
1604         my $page_content        = $passedoptions->{"PageContent"};
1605         my $page_settings       = $passedoptions->{"PageSettings"};
1607         # Check to see if the filename given already exists
1608         # in the page database.
1610         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1611         $string_handle->execute();
1613         # Check if a page with the filename given really does
1614         # exist.
1616         while (@database_page = $string_handle->fetchrow_array()){
1618                 # A page does exist so increment the count to 1.
1620                 $page_count++;
1621                 
1622         }
1624         if ($page_count ne 0){
1626                 # The page does exist so set the error value.
1628                 $error = "PageExists";
1629                 return;
1631         }
1633         # Check if certain values are undefined.
1635         if (!$page_name){
1637                 $page_name = "";
1639         }
1641         if (!$page_description){
1643                 $page_description = "";
1645         }
1647         if (!$page_section){
1649                 $page_section = "";
1651         }
1653         if (!$page_content){
1655                 $page_content = "";
1657         }
1659         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1660         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1662         # Add the page to the selected database.
1664         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1665                 \'' . $class->convert($page_filename) . '\',
1666                 \'' . $class->convert($page_name) . '\',
1667                 \'' . $class->convert($page_description) . '\',
1668                 \'' . $class->convert($page_section) . '\',
1669                 \'' . $class->convert($page_template) . '\',
1670                 \'' . $class->convert($page_content) . '\',
1671                 \'' . $class->convert($page_settings) . '\',
1672                 \'' . $class->convert($page_date) . '\'
1673         )') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1674         $string_handle->execute();
1678 sub deletepage{
1679 #################################################################################
1680 # deletepage: Delete a page from the selected database.                         #
1681 #                                                                               #
1682 # Usage:                                                                        #
1683 #                                                                               #
1684 # $dbmodule->deletepage(options)                                                #
1685 #                                                                               #
1686 # options       Specifies the following options in any order.                   #
1687 #                                                                               #
1688 # PageFilename  Specifies the page filename to delete.                          #
1689 #################################################################################
1691         $error = "";
1692         $errorext = "";
1694         # Get the data that was passed to the subroutine.
1696         my $class = shift;
1697         my ($passedoptions) = @_;
1698         my @page_info;
1699         my $page_found = 0;
1701         # Get the page filename.
1703         my $page_filename = $passedoptions->{"PageFilename"};
1705         # Check if the page exists before deleting it.
1707         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1708         $string_handle->execute();
1710         while (@page_info = $string_handle->fetchrow_array()){
1712                 $page_found = 1;
1714         }
1716         # Check if the page really does exist.
1718         if (!$page_found){
1720                 $error = "PageDoesNotExist";
1721                 return;
1723         }
1725         # Delete the page.
1727         $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\'') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1728         $string_handle->execute();
1732 sub editpage{
1733 #################################################################################
1734 # editpage: Edit a page from the selected database.                             #
1735 #                                                                               #
1736 # Usage:                                                                        #
1737 #                                                                               #
1738 # $dbmodule->editpage(options);                                                 #
1739 #                                                                               #
1740 # options       Specifies the following options in any order.                   #
1741 #                                                                               #
1742 # PageFilename          Specifies the filename to edit.                         #
1743 # PageNewFilename       Specifies the new filename to use.                      #
1744 # PageNewName           Specifies the new page name to use.                     #
1745 # PageNewDescription    Specifies the new page description to use.              #
1746 # PageNewSection        Specifies the new page section to use.                  #
1747 # PageNewTemplate       Specifies the new page template to use.                 #
1748 # PageNewContent        Specifies the new page content to use.                  #
1749 # PageNewSettings       Specifies the new page settings to use.                 #
1750 #################################################################################
1752         $error = "";
1753         $errorext = "";
1755         # Get the values passed to the subroutine.
1757         my $class = shift;
1758         my ($passedoptions) = @_;
1759         my $page_found = 0;
1760         my @page_info;
1762         # Get the data that was passed to the subroutine.
1764         my $page_filename       = $passedoptions->{"PageFilename"};
1765         my $page_newfilename    = $passedoptions->{"PageNewFilename"};
1766         my $page_newname        = $passedoptions->{"PageNewName"};
1767         my $page_newdescription = $passedoptions->{"PageNewDescription"};
1768         my $page_newsection     = $passedoptions->{"PageNewSection"};
1769         my $page_newtemplate    = $passedoptions->{"PageNewTemplate"};
1770         my $page_newcontent     = $passedoptions->{"PageNewContent"};
1771         my $page_newsettings    = $passedoptions->{"PageNewSettings"};
1773         # Check if the page with the filename given exists.
1775         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1776         $string_handle->execute();
1778         # Check if the page really does exist.
1780         while (@page_info = $string_handle->fetchrow_array()){
1782                 # The page information is found.
1784                 $page_found = 1;
1786         }
1788         # Check if the page really does exist.
1790         if (!$page_found){
1792                 $error = "PageDoesNotExist";
1793                 return;
1795         }
1797         # Check if there is a page that already exists with the new
1798         # filename.
1800         if ($page_filename ne $page_newfilename){
1802                 $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_newfilename) . '\' LIMIT 1') or ( $error = "DatabaseError", $errorext = $database_handle->errstr, return );
1803                 $string_handle->execute();
1805                 # Check if a page really is using the new filename.
1807                 while (@page_info = $string_handle->fetchrow_array()){
1809                         # The page information is found.
1811                         $page_found = 1;
1813                 }
1815                 if ($page_found eq 1){
1817                         $error = "PageAlreadyExists";
1818                         return;
1820                 }
1822         }
1824         # Get the current date.
1826         my ($created_second, $created_minute, $created_hour, $created_day, $created_month, $created_year, $created_weekday, $created_yearday, $created_dst) = localtime;
1827         my $page_date = $created_year . '-' . $created_month . '-' . $created_day . ' ' . $created_hour . ':' . $created_minute . ':' . $created_second;
1829         # Edit the selected page.
1831         $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_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 );
1832         $string_handle->execute();
1836 sub movepage{
1837 #################################################################################
1838 # movepage: Moves a page from the old database to the new database.             #
1839 #                                                                               #
1840 # Usage:                                                                        #
1841 #                                                                               #
1842 # $dbmodule->movepage(options);                                                 #
1843 #                                                                               #
1844 # options       Specifies the following options in any order.                   #
1845 #                                                                               #
1846 # PageFilename  Specifies the page with the filename to move.                   #
1847 #################################################################################
1849         $error = "";
1850         $errorext = "";
1852         # Get the values passed to the subroutine.
1854         my $class = shift;
1855         my ($passedoptions) = @_;
1857         my (%database_page, $database_page);
1858         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1859         my @page_info;
1860         my $page_found = 0;
1862         # Get the data that was passed to the subroutine.
1864         my $page_filename = $passedoptions->{"PageFilename"};
1866         # Check if the page with the filename given exists.
1868         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1869         $string_handle->execute();
1871         # Check if the page really does exist.
1873         while (@page_info = $string_handle->fetchrow_array()){
1875                 # Get the values from the array.
1877                 $pagefilename           = decode_utf8($page_info[0]);
1878                 $pagename               = decode_utf8($page_info[1]);
1879                 $pagedescription        = decode_utf8($page_info[2]);
1880                 $pagesection            = decode_utf8($page_info[3]);
1881                 $pagetemplate           = decode_utf8($page_info[4]);
1882                 $pagedata               = decode_utf8($page_info[5]);
1883                 $pagesettings           = decode_utf8($page_info[6]);
1884                 $pagelastmodified       = decode_utf8($page_info[7]);
1886                 # Put the values into the page hash.
1888                 %database_page = (
1889                         "PageFilename"          => $pagefilename,
1890                         "PageName"              => $pagename,
1891                         "PageDescription"       => $pagedescription,
1892                         "PageSection"           => $pagesection,
1893                         "PageTemplate"          => $pagetemplate,
1894                         "PageContent"           => $pagedata,
1895                         "PageSettings"          => $pagesettings,
1896                         "PageLastModified"      => $pagelastmodified,
1897                 );
1899                 # The page information is found.
1901                 $page_found = 1;
1903         }
1905         # Check if the page really does exist.
1907         if (!$page_found){
1909                 $error = "PageDoesNotExist";
1910                 return;
1912         }
1914         # Check if the page with the filename given already exists in
1915         # the database the page is being moved to.
1917         $page_found = 0;
1918         @page_info = ();
1920         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
1921         $string_handle->execute();
1923         while (@page_info = $string_handle->fetchrow_array()){
1925                 $page_found = 1;
1927         }
1929         # Check if the page really does exist.
1931         if ($page_found){
1933                 $error = "PageAlreadyExists";
1934                 return;
1936         }
1938         # Add the page to the new database.
1940         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
1941                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
1942                 \'' . $class->convert($database_page{"PageName"}) . '\',
1943                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
1944                 \'' . $class->convert($database_page{"PageSection"}) . '\',
1945                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
1946                 \'' . $class->convert($database_page{"PageContent"}) . '\',
1947                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
1948                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
1949         )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
1950         $string_handle->execute();
1952         # Delete the page from the old database.
1954         $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($database_page{"PageFilename"}) . '\'') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1955         $string_handle->execute();
1959 sub copypage{
1960 #################################################################################
1961 # copypage: Copies a page from the old database to the new database.            #
1962 #                                                                               #
1963 # Usage:                                                                        #
1964 #                                                                               #
1965 # $dbmodule->copypage(options);                                                 #
1966 #                                                                               #
1967 # options       Specifies the following options in any order.                   #
1968 #                                                                               #
1969 # PageFilename  Specifies the page with the filename to copy.                   #
1970 #################################################################################
1972         $error = "";
1973         $errorext = "";
1975         # Get the values passed to the subroutine.
1977         my $class = shift;
1978         my ($passedoptions) = @_;
1980         my (%database_page, $database_page);
1981         my ($pagefilename, $pagename, $pagedescription, $pagesection, $pagetemplate, $pagedata, $pagesettings, $pagelastmodified);
1982         my @page_info;
1983         my $page_found = 0;
1985         # Get the data that was passed to the subroutine.
1987         my $page_filename = $passedoptions->{"PageFilename"};
1989         # Check if the page with the filename given exists.
1991         $string_handle = $database_handle->prepare('SELECT filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "OldDatabaseError", $errorext = $database_handle->errstr, return );
1992         $string_handle->execute();
1994         # Check if the page really does exist.
1996         while (@page_info = $string_handle->fetchrow_array()){
1998                 # Get the values from the array.
2000                 $pagefilename           = decode_utf8($page_info[0]);
2001                 $pagename               = decode_utf8($page_info[1]);
2002                 $pagedescription        = decode_utf8($page_info[2]);
2003                 $pagesection            = decode_utf8($page_info[3]);
2004                 $pagetemplate           = decode_utf8($page_info[4]);
2005                 $pagedata               = decode_utf8($page_info[5]);
2006                 $pagesettings           = decode_utf8($page_info[6]);
2007                 $pagelastmodified       = decode_utf8($page_info[7]);
2009                 # Put the values into the page hash.
2011                 %database_page = (
2012                         "PageFilename"          => $pagefilename,
2013                         "PageName"              => $pagename,
2014                         "PageDescription"       => $pagedescription,
2015                         "PageSection"           => $pagesection,
2016                         "PageTemplate"          => $pagetemplate,
2017                         "PageContent"           => $pagedata,
2018                         "PageSettings"          => $pagesettings,
2019                         "PageLastModified"      => $pagelastmodified,
2020                 );
2022                 # The page information is found.
2024                 $page_found = 1;
2026         }
2028         # Check if the page really does exist.
2030         if (!$page_found){
2032                 $error = "PageDoesNotExist";
2033                 return;
2035         }
2037         # Check if the page with the filename given already exists in
2038         # the database the page is being moved to.
2040         $page_found = 0;
2041         @page_info = ();
2043         $string_handle = $database_handle->prepare('SELECT filename FROM ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages WHERE filename = \'' . $class->convert($page_filename) . '\' LIMIT 1') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2044         $string_handle->execute();
2046         while (@page_info = $string_handle->fetchrow_array()){
2048                 $page_found = 1;
2050         }
2051         
2052         # Check if the page really does exist.
2054         if ($page_found){
2056                 $error = "PageAlreadyExists";
2057                 return;
2059         }
2061         # Add the page to the new database.
2063         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_' . $class->convert($second_database_filename) . '_database_pages (filename, pagename, pagedescription, pagesection, pagetemplate, pagedata, pagesettings, lastmodified) VALUES (
2064                 \'' . $class->convert($database_page{"PageFilename"}) . '\',
2065                 \'' . $class->convert($database_page{"PageName"}) . '\',
2066                 \'' . $class->convert($database_page{"PageDescription"}) . '\',
2067                 \'' . $class->convert($database_page{"PageSection"}) . '\',
2068                 \'' . $class->convert($database_page{"PageTemplate"}) . '\',
2069                 \'' . $class->convert($database_page{"PageContent"}) . '\',
2070                 \'' . $class->convert($database_page{"PageSettings"}) . '\',
2071                 \'' . $class->convert($database_page{"PageLastModified"}) . '\'
2072         )') or ( $error = "NewDatabaseError", $errorext = $database_handle->errstr, return );
2073         $string_handle->execute();
2077 #################################################################################
2078 # Filter subroutines.                                                           #
2079 #################################################################################
2081 sub connectfilter{
2082 #################################################################################
2083 # connectfilter: Connect to the filter database.                                #
2084 #                                                                               #
2085 # Usage:                                                                        #
2086 #                                                                               #
2087 # $dbmodule->connectfilter(missingignore);                                      #
2088 #                                                                               #
2089 # missingignore Ignore error about database being missing.                      #
2090 #################################################################################
2092         $error = "";
2093         $errorext = "";
2095         my $class = shift;
2096         my $ignoremissing = shift;
2097         my @filterdb_check;
2098         my $filterdb_exists = 0;
2100         # Check if the template database exists.
2102         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2103         $string_handle->execute();
2105         while (@filterdb_check = $string_handle->fetchrow_array()){
2107                 $filterdb_exists = 1;
2109         }
2111         if (!$filterdb_exists){
2113                 if (!$ignoremissing){
2115                         $error = "FilterDatabaseDoesNotExist";
2116                         return;
2118                 }
2120         }
2124 sub disconnectfilter{
2125 #################################################################################
2126 # disconnectfilter: Disconnect from the filter database.                        #
2127 #                                                                               #
2128 # Usage:                                                                        #
2129 #                                                                               #
2130 # $dbmodule->disconnectfilter();                                                #
2131 #################################################################################
2133         # This subroutine is not used.
2137 sub getfilterlist{
2138 #################################################################################
2139 # getfilterlist: Gets the list of filters in the filter database.               #
2140 #                                                                               #
2141 # Usage:                                                                        #
2142 #                                                                               #
2143 # $dbmodule->getfilterlist();                                                   #
2144 #################################################################################
2146         $error = "";
2147         $errorext = "";
2149         my $class = shift;
2151         my @filter_list;
2152         my @filter_data;
2154         # Get the list of filters available.
2156         $string_handle  = $database_handle->prepare('SELECT id, priority FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY priority ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2157         $string_handle->execute();
2159         while (@filter_data = $string_handle->fetchrow_array()){
2161                 # Add the filter to the list of available filters.
2163                 push(@filter_list, decode_utf8($filter_data[0]));
2165         }
2167         return @filter_list;
2171 sub getfilterinfo{
2172 #################################################################################
2173 # getfilterinfo: Gets information about the filter.                             #
2174 #                                                                               #
2175 # Usage:                                                                        #
2176 #                                                                               #
2177 # $dbmodule->getfilterinfo(options);                                            #
2178 #                                                                               #
2179 # options       Specifies the following options in any order.                   #
2180 #                                                                               #
2181 # FilterID      Specifies the filter ID number to get information from.         #
2182 #################################################################################
2184         $error = "";
2185         $errorext = "";
2187         # Get the values passed to the subroutine.
2189         my $class               = shift;
2190         my ($passedoptions)     = @_;
2192         my %filter_info;
2193         my $filter_exists       = 0;
2194         my @filter_data;
2196         # Get the values that are in the hash.
2198         my $filter_id           = $passedoptions->{"FilterID"};
2200         $string_handle = $database_handle->prepare('SELECT id, priority, findsetting, replacesetting, notes FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2201         $string_handle->execute();
2203         # Get the filter information.
2205         while (@filter_data = $string_handle->fetchrow_array()){
2207                 $filter_info{"FilterID"}        = decode_utf8($filter_data[0]);
2208                 $filter_info{"FilterPriority"}  = decode_utf8($filter_data[1]);
2209                 $filter_info{"FilterFind"}      = decode_utf8($filter_data[2]);
2210                 $filter_info{"FilterReplace"}   = decode_utf8($filter_data[3]);
2211                 $filter_info{"FilterNotes"}     = decode_utf8($filter_data[4]);
2213                 $filter_exists = 1;
2215         }
2217         # Check if the filter exists.
2219         if (!$filter_exists){
2221                 # The filter does not exist so return
2222                 # an error value.
2224                 $error = "FilterDoesNotExist";
2225                 return;
2227         }
2229         # Return the filter information.
2231         return %filter_info;
2235 sub addfilter{
2236 #################################################################################
2237 # addfilter: Adds a filter to the filter database.                              #
2238 #                                                                               #
2239 # Usage:                                                                        #
2240 #                                                                               #
2241 # $dbmodule->addfilter(options);                                                #
2242 #                                                                               #
2243 # options       Specifies the following options in any order.                   #
2244 #                                                                               #
2245 # FindFilter    Specifies the find filter to add.                               #
2246 # ReplaceFilter Specifies the replace filter to add.                            #
2247 # Priority      Specifies the filter priority to use.                           #
2248 # Notes         Specifies the notes to use.                                     #
2249 #################################################################################
2251         $error = "";
2252         $errorext = "";
2254         # Get the values passed to the subroutine.
2256         my $class = shift;
2257         my ($passedoptions) = @_;
2259         # Define some variables for later.
2261         my @database_filters;
2262         my @filterdb_check;
2263         my @filterid_list;
2264         my @filterid_check;
2265         my $nofiltertable = 0;
2266         my $filter_found = 0;
2267         my $filter_count = 0;
2268         my $filterdb_exists = 0;
2269         my $filter_id;
2270         my $new_id;
2272         # Get the values from the hash.
2274         my $filter_find         = $passedoptions->{"FindFilter"};
2275         my $filter_replace      = $passedoptions->{"ReplaceFilter"};
2276         my $filter_priority     = $passedoptions->{"Priority"};
2277         my $filter_notes        = $passedoptions->{"Notes"};
2279         # Check if the template database exists.
2281         $string_handle = $database_handle->prepare('SHOW TABLES LIKE \'' . $class->convert($options{"TablePrefix"}) . '_filters\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2282         $string_handle->execute();
2284         while (@filterdb_check = $string_handle->fetchrow_array()){
2286                 $filterdb_exists = 1;
2288         }
2290         # Check if certain values are undefined and if they
2291         # are then set them blank.
2293         if (!$filter_find){
2295                 $filter_find = "";
2297         }
2299         if (!$filter_replace){
2301                 $filter_replace = "";
2303         }
2305         if (!$filter_priority){
2307                 $filter_priority = 1;
2309         }
2311         if (!$filter_notes){
2313                 $filter_notes = "";
2315         }
2317         # Check if there is really no filter table.
2319         if (!$filterdb_exists){
2321                 # Create the filter database table.
2323                 $string_handle = $database_handle->prepare('CREATE TABLE ' . $class->convert($options{"TablePrefix"}) . '_filters (
2324                         id int(7) primary key,
2325                         priority int(5),
2326                         findsetting varchar(1024),
2327                         replacesetting varchar(1024),
2328                         notes text
2329                 ) DEFAULT CHARSET=utf8') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2330                 $string_handle->execute();
2332         }
2334         # Find the lowest filter identification number available.
2336         $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters ORDER BY id ASC') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2337         $string_handle->execute();
2339         while (@database_filters = $string_handle->fetchrow_array()){
2341                 $filter_id      = decode_utf8($database_filters[0]);
2343                 # Add the filter identification to the list of filter IDs.
2345                 push(@filterid_list, $filter_id);
2347         }
2349         $filter_id = "";
2351         # Process each filter looking for a blank available filter.
2353         foreach $filter_id (@filterid_list){
2355                 # Check the next filter ID to see if it's blank.
2357                 $new_id = $filter_id + 1;
2359                 $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters WHERE id = \'' . $class->convert($new_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2360                 $string_handle->execute();
2362                 # Get the filter identification number.
2364                 while (@filterid_check = $string_handle->fetchrow_array()){
2366                         $filter_found = 1;
2368                 }
2370                 # Check if a filter was found.
2372                 if (!$filter_found){
2374                         # No filter was found using this ID so exit the loop.
2376                         last;
2378                 }
2380                 # Increment the filter count and reset the filter found value.
2382                 $filter_count++;
2383                 $filter_found = 0;
2384                 $new_id = 0;
2386         }
2388         # Check if there were any filters in the filters database.
2390         if (!$filter_count && !$new_id){
2392                 # There were no filters in the filters database so set
2393                 # the new filter identification value to 1.
2395                 $new_id = 1;
2397         }
2399         # Add the filter to the filter database.
2401         $string_handle = $database_handle->prepare('INSERT INTO ' . $class->convert($options{"TablePrefix"}) . '_filters (id, priority, findsetting, replacesetting, notes) VALUES (
2402                 \'' . $class->convert($new_id) . '\',
2403                 \'' . $class->convert($filter_priority) . '\',
2404                 \'' . $class->convert($filter_find) . '\',
2405                 \'' . $class->convert($filter_replace) .'\',
2406                 \'' . $class->convert($filter_notes) . '\'
2407         )') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2408         $string_handle->execute();
2413 sub editfilter{
2414 #################################################################################
2415 # editfilter: Edits a filter in the filter database.                            #
2416 #                                                                               #
2417 # Usage:                                                                        #
2418 #                                                                               #
2419 # $dbmodule->editfilter(options);                                               #
2420 #                                                                               #
2421 # options       Specifies the following options in any order.                   #
2422 #                                                                               #
2423 # FilterID              Specifies the filter to edit.                           #
2424 # NewFindFilter         Specifies the new find filter setting.                  #
2425 # NewReplaceFilter      Specifies the new replace filter setting.               #
2426 # NewFilterPriority     Specifies the new filter priority setting.              #
2427 # NewFilterNotes        Specifies the new notes for the filter.                 #
2428 #################################################################################
2430         $error = "";
2431         $errorext = "";
2433         # Get the values passed to the subroutine.
2435         my $class = shift;
2436         my ($passedoptions) = @_;
2438         my @filter_data;
2439         my $filter_exists = 1;
2440         my $blankfile = 0;
2442         # Get the values from the hash.
2444         my $filter_id           = $passedoptions->{"FilterID"};
2445         my $filter_newfind      = $passedoptions->{"NewFindFilter"};
2446         my $filter_newreplace   = $passedoptions->{"NewReplaceFilter"};
2447         my $filter_newpriority  = $passedoptions->{"NewFilterPriority"};
2448         my $filter_newnotes     = $passedoptions->{"NewFilterNotes"};
2450         # Check if the filter exists before editing it.
2452         $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2453         $string_handle->execute();
2455         # Check if the filter exists.
2457         while (@filter_data = $string_handle->fetchrow_array()){
2459                 $filter_exists = 1;
2461         }       
2463         # Check if the filter really does exist.
2465         if (!$filter_exists){
2467                 # The filter does not exist so return
2468                 # an error value.
2470                 $error = "FilterDoesNotExist";
2471                 return;
2473         }
2475         # Edit the selected filter.
2477         $string_handle = $database_handle->prepare('UPDATE ' . $class->convert($options{"TablePrefix"}) . '_filters SET
2478                 findsetting = \'' . $class->convert($filter_newfind) . '\',
2479                 replacesetting = \'' . $class->convert($filter_newreplace) . '\',
2480                 priority = \'' . $class->convert($filter_newpriority) . '\',
2481                 notes = \'' . $class->convert($filter_newnotes) . '\'
2482         WHERE id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );       
2483         $string_handle->execute();
2485         return;
2489 sub deletefilter{
2490 #################################################################################
2491 # deletefilter: Deletes a filter from the filter database.                      #
2492 #                                                                               #
2493 # Usage:                                                                        #
2494 #                                                                               #
2495 # $dbmodule->deletefilter(options);                                             #
2496 #                                                                               #
2497 # options       Specifies the following options in any order.                   #
2498 #                                                                               #
2499 # FilterID      Specifies the filter to delete from the filter database.        #
2500 #################################################################################
2502         $error = "";
2503         $errorext = "";
2505         # Get the values passed to the subroutine.
2507         my $class = shift;
2508         my ($passedoptions) = @_;
2510         my $filter_exists = 0;
2511         my @filter_data;
2513         # Get the values from the hash.
2515         my $filter_id           = $passedoptions->{"FilterID"};
2517         # Check if the filter exists before deleting.
2519         $string_handle = $database_handle->prepare('SELECT id FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2520         $string_handle->execute();
2522         while (@filter_data = $string_handle->fetchrow_array()){
2524                 $filter_exists = 1;
2526         }
2528         # Check to see if the filter really does exist.
2530         if (!$filter_exists){
2532                 $error = "FilterDoesNotExist";
2533                 return;
2535         }
2537         # Delete the filter from the filter database.
2539         $string_handle = $database_handle->prepare('DELETE FROM ' . $class->convert($options{"TablePrefix"}) . '_filters where id = \'' . $class->convert($filter_id) . '\'') or ( $error = "FilterDatabaseError", $errorext = $database_handle->errstr, return );
2540         $string_handle->execute();
2544 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