Im currently having some trouble with a rewrite program that was made for me. here is the script along with some information about it.
- Code: Select all
You need to create a new MySQL database in the script I called it rewrite. I created a username called rewrite with password rewrite.
# Creates a blank database
CREATE DATABASE rewrite;
# Creates the user with the permissions
GRANT all on rewrite.* to locahost@rewrite identified by ‘rewrite’;
# Create the table
CREATE TABLE rewrite (
name varchar(100),
rewrite varchar(100),
sort_order int
);
The Perl script is as follows:
#!/usr/bin/perl
#
# IT Integrated Business Solutions (c) 2008
#
# Author: Ronan Cashell
# There is no warranty that goes with this script.
#
# For this to work we must connect to the database and await STDIN. The input is then cross checked against
# the database for a valid entry. An example:
#
# If entry provided is mail.domain.com we need to rewrite to /var/www/html/mail
# In a one to one match this is easy. However, wildcards are also supposed to work
# with this utility. So if mail.domain.com is entered then we need to cross check against
# the database to see the first match in the database against this.
#
# So lets imagine in the database we have *.domain.com this means that anything requesting
# www.domain.com/mail.domain.com ... should have the rewrite. A sort order is included to
# configure the first match that it picks up.
#
# the MySQL instr function will help check if the entry exists in the database
use strict;
use DBI;
$| = 1;
my $dbh = DBI->connect( 'dbi:mysql:rewrite:localhost',
'rewrite',
'rewrite') || die "Database connection not made: $DBI::errstr";
my $in;
while ($in = <STDIN>) {
#$in = 'mail.domain.com';
print "Stdin: " . $in;
my $sql = "SELECT name, rewrite FROM rewrite WHERE INSTR('" . $in . "', name) > 0 ORDER BY sort_order ASC";
my $sth = $dbh->prepare( $sql );
$sth->execute();
my( $name, $rewrite);
$sth->bind_columns( undef, \$name, \$rewrite );
if($sth->fetch()) {
print $rewrite . "\n";
} else {
# Important to have a default if there are no matches. Otherwise Apache will hang.
print "/var/www/html";
}
$sth->finish();
}
$dbh->disconnect();
The entries in the database consist of the following:
+-----------------+--------------------+------------+
| name | rewrite | sort_order |
+-----------------+--------------------+------------+
| mail.domain.com | /var/www/html/mail | 10 |
| www.domain.com | /var/www/html | 20 |
| root.domain.com | /user/root/home | 30 |
| .domain.com | /var/www/new/html | 40 |
+-----------------+--------------------+------------+
The sort order is used to determine the first occurrence that matches the criteria. I have used a simple INSTR function available in database to see if the entry in the database can be found in the string that you are passing in. For instance, in the above table, if you had user.domain.com then the last entry .domain.com will match and be returned. This will be the only entry with a fix. However, if you entered mail.domain.com this matches both the first and last entry. The sort order determines which one should be taken first.
Does anyone have any sugestions on how i could get this to work? what i have tried didnt work, and ive been working on it for a while. Thanks