Web Hosting Talk







View Full Version : Perl to PHP help,,


geosters
11-29-2003, 03:45 PM
O.k i need the below translated to php, i need this for a project i am going to make, this is the only part i am having problems in. Can you PLEASE help?



#!/usr/local/bin/perl

# apache.pl was used to export apache related config file for Apache from Database
# You need to add follow line in httpd.conf:
#
# include "pvirthostas.conf"
#
# This program will create a pvirthostas.conf

# p.s. The code is little bit dirty~ I will clean it up :-)

use strict;
use DBI;

use lib ("../lib");
require "config.inc.pl";


my $conf_content;
my @row;
my @row2;
my @row3;
my %val;
my $dbh = DBI->connect("DBI:mysql:$config::sql_root_database", $config::sql_root_username, $config::sql_root_password);
my $sth = $dbh->prepare("select domain, hostip, email_admin, doc_root, log_dir from http");
$sth->execute();

while(@row = $sth->fetchrow_array) {

$conf_content = $conf_content."include \"".$config::apache_conf_path."\/$row[0]\"\n";

if(!open(HOST_CONF, ">$config::apache_conf_path\/$row[0]"))
{ print("$row[0] open failed"); die;}
if(!flock(HOST_CONF, 2))
{ print("File Lock failed"); die;}

if(!is_file_exist($row[3])){write_to_log("apache", "Directory $row[3] not exist so $row[0] will forward to $config::doc_root");$row[3]=$config::doc_root;}
print HOST_CONF "<VirtualHost $row[1]>\n";
print HOST_CONF "\tServerName $row[0]\n";
print HOST_CONF "\tServerAlias $row[0]\n";
print HOST_CONF "\tServerAdmin $row[2]\n";
print HOST_CONF "\tDocumentRoot $row[3]\n";
print HOST_CONF "\tErrorLog $row[4]/$config::apache_error_log\n";
print HOST_CONF "\tCustomLog $row[4]/$config::apache_access_log common\n";
print HOST_CONF "</VirtualHost>\n";

my $sth2 = $dbh->prepare("select vhost, dir_name , host_ip from subdomain where domain=\"$row[0]\"");
$sth2->execute();

while(@row2 = $sth2->fetchrow_array) {
if($row2[1]) {
if(!is_file_exist($row2[1])){write_to_log("apache", "Directory $row2[1] not exist so $row2[0]\.$row[0] will forward to $config::doc_root");$row2[1]=$config::doc_root;}
if($row2[2]) {
print HOST_CONF "<VirtualHost $row[2]>\n";
}
else {
print HOST_CONF "<VirtualHost $config::apache_host_ip>\n";
}
print HOST_CONF "\tServerName $row2[0]\.$row[0]\n";
print HOST_CONF "\tServerAlias $row2[0]\.$row[0]\n";
print HOST_CONF "\tServerAdmin $row[2]\n";
print HOST_CONF "\tDocumentRoot $row2[1]\n";
print HOST_CONF "\tErrorLog $row[4]/$config::apache_error_log\n";
print HOST_CONF "\tCustomLog $row[4]/$config::apache_access_log common\n";
print HOST_CONF "</VirtualHost>\n";
}
}
flock(HOST_CONF, 8);
close(HOST_CONF);
}

$sth->finish();
$dbh->disconnect();

write_to_file($config::apache_conf_path."/pvirthostas.conf", $conf_content);

if(system($config::apache_restart) != 0)
{print("\nApache Restart Error");die;}

Sheps
11-29-2003, 03:53 PM
Okay, a little more information, what do you need help with, specifically? Do you not understand a few things, or?

here is a quick rundown of what this script does:

Sets VARS and Connects to the DB
SELECTS the config info from the table "http"
Opens and Locks the Apache Config File
Checks for the Doc Root
Creates the VHost
$row[x] corresponds to each of the fields in the select statement. Ie, $row[0] is the domain
Creates the Subdomain VHOST in the same fashion as above
unlocks the file
closes the file
clears the MySQL query and closes the DB Connection
Writes some config contents to a file.
Restarts Apache, and prints a error if it doesn't restart

geosters
11-29-2003, 04:03 PM
i just need that to translate to php, i have no clue on how to turn that to php, i understand what it does :-)

Sheps
11-29-2003, 04:47 PM
Fairly easily done. http://www.php.net/manual/en/ You need mysql functions, file/dir functions and search for the system or exec or shell_exec function for the restart of Apache.

Sheps
11-29-2003, 04:57 PM
Ooops, hit enter before I was done.

MySQL Selects are relatively simple. Can be exactly the same as the Perl Select.

For printing to the file. It is a little different. You can do a:

fwrite(filepointer,$contents); Where contents can be:

$content = '
<VirtualHost>
...
...
...
</VirtualHost>';

Anyways, hope this helps a little. I would like to help more, but:
1. I need to write a Essay
2. You learn best by trial and error