Listing 17-5 Continuedreturn TRUE; } return FALSE; } function createSymLink$user = null, $vhost = null { $link = sprintf“%s/%s/%s”, $GLOBALS[SYSTEM_INFO][home_dir],$user, $vhost; $cmd =
Trang 1Listing 17-5 (Continued)
return TRUE;
} return FALSE;
}
function createSymLink($user = null, $vhost = null) {
$link = sprintf(“%s/%s/%s”, $GLOBALS[SYSTEM_INFO][home_dir],$user, $vhost);
$cmd = sprintf(“%s %s %s %s”, $GLOBALS[SYSTEM_INFO][symlink_bin],
$GLOBALS[SYSTEM_INFO][symlink_opt],
$GLOBALS[SITE_INFO][SERVER_ROOT],
$link );
echo “Creating symbolic link using $cmd\n”;
if (file_exists($link)) {
echo “Warning! Symlink: $link already exists.\n”;
return TRUE;
}
exec($cmd, $output, $status);
if ($status) {
echo “Error: could not make symbolic link: $cmd\n”;
return FALSE;
} return TRUE;
}
function removeTestPage() {
$testFile = sprintf(“%s/test.txt”, $GLOBALS[SITE_INFO][DOCUMENT_ROOT]);
if (! file_exists($testFile)) {
return FALSE;
Trang 2$status = unlink($testFile);
return $status;
}
function testNewSite($host = null) {
// Write a test page in document root // of the new site
if (! writeTestPage()) {
echo “Could not write test page in $GLOBALS[SITE_INFO][DOCUMENT_ROOT]
\n”;
return FALSE;
}
$url = sprintf(“http://%s/test.txt”, $host);
echo “Testing: requesting $url ”;
$fp = fopen($url, ‘r’);
if ($fp) { while(!feof($fp)) {
$buffer = fgets($fp, 1024);
}
removeTestPage();
if (preg_match(‘/SUCCESS/’, $buffer)) {
echo “successful.\n”;
return TRUE;
} else { echo “failed.\n”;
} }
Continued
Trang 3Listing 17-5 (Continued)
return FALSE;
}
function appendVhostConfigToApacheConfig($vhostFile = null) {
$httpdConf = sprintf(“%s/%s/%s”, $GLOBALS[APACHE_INFO][path],
$GLOBALS[APACHE_INFO][conf_dir],
$GLOBALS[APACHE_INFO][conf_flie] );
if (! file_exists($httpdConf)) {
echo “Error: could not find $httpdConf\n”;
return FALSE;
}
$newDirective = “#\n#\n# Following line loads configuration\n”;
$newDirective = “# for the “ basename($vhostFile) “ virtual host\n”;
$newDirective = “Include $vhostFile \n\n\n”;
echo “Appending Include $vhostFile in $httpdConf\n”;
if (DEBUG) echo $newDirective;
$fp = fopen($httpdConf, ‘a’);
if (! $fp) {
echo “Error: could not open $httpdConf in append mode.\n”;
return FALSE;
}
fputs($fp, $newDirective);
fclose($fp);
return TRUE;
}
function makeDirectory($mode = ‘0750’, $path = null)
Trang 4}
function setPermissions($mode = null, $path = null) {
$cmd = $GLOBALS[SYSTEM_INFO][chmod_bin] “ -R $mode $path”;
if (DEBUG) echo “$cmd\n”;
exec($cmd);
}
function setOwnerAndGroup($user = null, $group = null, $path = null) {
$cmd = $GLOBALS[SYSTEM_INFO][chown_bin] “ -R $user:$group $path”;
if (DEBUG) echo “$cmd\n”;
exec($cmd);
}
function writeVirtualConfigFile($contents = null, $file = null) {
$fp = fopen($file, ‘w’);
if (! $fp) {
echo “Cannot write $file !\n”;
return FALSE;
}
fputs($fp, $contents);
fclose($fp);
return TRUE;
}
function loadVhostTemplate($template = null) {
$file = sprintf(“%s/%s”, $GLOBALS[TEMPLATE_DIR], $template);
$contents = null;
if (!file_exists($file)) {
echo “Virtual host template $file does not exists!\n”;
return null;
}
Continued
Trang 5Listing 17-5 (Continued)
// Load template require_once($file);
$contents = makeVirtualHost();
return $contents;
}
function checkVhostName($host = null) {
$hostParts = explode(‘.’, $host);
// host must be at least: domain.tld return (count($hostParts) <= 1) ? FALSE : TRUE;
}
function makeAddRequest($cmd = null) {
$request = array();
$request[vhost] = strtolower(getValue($cmd, ‘v’, ‘vhost’));
$request[user] = strtolower(getValue($cmd, ‘u’, ‘user’));
$request[group] = strtolower(getValue($cmd, ‘g’, ‘group’));
$request[restart] = getValue($cmd, ‘r’, ‘restart’);
$request[test] = $cmd[test];
$request[passwd] = getValue($cmd, ‘p’, ‘pass’);
$request[type] = getValue($cmd, ‘t’, ‘type’);
$request[type] = ($request[type] != null) ?
$request[type] : DEFAULT_ACCOUNT_TYPE;
$request[notify_email] = (isset($cmd[notify_email])) ? strtolower($cmd[notify_email]) : null;
if (empty($request[vhost]) ||
empty($request[user]) ||
empty($request[type]) )
{ echo “You must provide vhost user type “
“values to create a new virtual host.\n”;
return null;
}
// Remove leading or trailing dots from hostname