echo “Error: Apache could not be restarted!\n”; return FALSE; } if (isset($request[test]) && !testNewSite($request[vhost])) { echo “Error: site test failed!\n”; return FALSE; } } // Now link the user account to the web site document root if ($addOK && DEFAULT_SYMLINK_USER_TO_WEBSITE && ! createSymLink($request[user], $request[vhost])) { echo “Error: could not create symbolic link to site in user account!\n”; return FALSE; } // Now process content configuration if ($addOK && ! addContents($SITE_INFO, $account[master_contents])) { echo “Error: could not add contents!\n”; return FALSE; } // Now process content configration if ($addOK && isset($request[notify_email]) && ! sendMail($SITE_INFO, $request, $account[mail_template])) { echo “Error: could not send mail!\n”; return FALSE; } } } if (isset($cmd[‘enable’])) { echo “Enable named site \n”; enableSite($siteName); } Continued Chapter 17: Apache Virtual Host Maker 621 22 549669 ch17.qxd 4/4/03 9:27 AM Page 621 Listing 17-5 (Continued) if (isset($cmd[‘disable’])) { echo “Disable named site \n”; } //print_r($cmd); exit; function createUser($user = null, $pass = null, $shell =null) { echo “Creating user account: $user with password $pass shell=$shell\n”; if (empty($pass) || strlen($pass) < $GLOBALS[SYSTEM_INFO][min_passwd_length]) { echo “Error: Password is missing or too short.\n”; return FALSE; } if ($shell != null) { $shell = “-s $shell”; } $cmd = $GLOBALS[SYSTEM_INFO][useradd_bin]; exec(“$cmd -p $pass $shell $user”); return TRUE; } function userExists($user = null) { $passwdFile = $GLOBALS[SYSTEM_INFO][passwd_file]; $lines = file($passwdFile); foreach($lines as $record) { $str = explode(‘:’, $record); if (!strcmp($str[0], $user)) return TRUE; } return FALSE; } 622 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 622 function groupExists($group = null) { $groupFile = $GLOBALS[SYSTEM_INFO][group_file]; $lines = file($groupFile); foreach($lines as $record) { $str = explode(‘:’, $record); if (!strcmp($str[0], $group)) return TRUE; } return FALSE; } function addSite($request = null) { $vhost = $request[vhost]; $type = $request[type]; $user = $request[user]; $group = $request[group]; echo “Creating $vhost configuration\n”; // config file $vhostConfigFile = sprintf(“%s/%s/%s”, $GLOBALS[‘APACHE_INFO’][‘path’], $GLOBALS[‘APACHE_INFO’][‘vhost_conf_dir’], $vhost); // See if this virtual host already exists or not if (file_exists($vhostConfigFile)) { echo “Error: $vhostConfigFile already exists. Cannot add site!\n”; return FALSE; } $account = $GLOBALS[‘ACCOUNT_TYPE’][$type]; if (!isset($account)) { echo “Error: given account type ($type) not defined in makesite.conf\n”; return FALSE; } Continued Chapter 17: Apache Virtual Host Maker 623 22 549669 ch17.qxd 4/4/03 9:27 AM Page 623 Listing 17-5 (Continued) // Configure Apache Virtual Host $GLOBALS[SERVER_NAME] = $vhost; $results = loadVhostTemplate($account[vhost_template]); if ($results == null) return FALSE; $success = writeVirtualConfigFile($results[config], $vhostConfigFile); if (! $success) return FALSE; // Create directories if (DEBUG) echo “Create directories\n”; foreach($results[makedir] as $dirName => $dirPath) { makeDirectory($GLOBALS[SYSTEM_INFO][permission],$dirPath); setOwnerAndGroup($user, $group, $dirPath); setPermissions($GLOBALS[SYSTEM_INFO][permission], $dirPath); $GLOBALS[SITE_INFO][$dirName] = $dirPath; } // Perform apache syntax check for vhost configuration $success = checkApacheSyntax($vhostConfigFile); if (! $success) return FALSE; $success = appendVhostConfigToApacheConfig($vhostConfigFile); if (! $success) return FALSE; return TRUE; } function checkApacheSyntax($file = null) { $serverBin = sprintf(“%s/%s/%s”, $GLOBALS[APACHE_INFO][path], $GLOBALS[APACHE_INFO][bin_dir], $GLOBALS[APACHE_INFO][server_bin] ); if (! file_exists($serverBin)) { echo “Error: could not find $serverBin\n”; return FALSE; } $cmd = “$serverBin “ . $GLOBALS[APACHE_INFO][config_chk_opt] . “ “ . $file ; 624 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 624 echo “Checking syntax: $file\n”; exec($cmd, $output, $status); return ($status) ? FALSE : TRUE; } function restartApache() { $serverBin = sprintf(“%s/%s/%s”, $GLOBALS[APACHE_INFO][path], $GLOBALS[APACHE_INFO][bin_dir], $GLOBALS[APACHE_INFO][server_bin] ); if (! file_exists($serverBin)) { echo “Error: could not find $serverBin\n”; return FALSE; } $cmd = “$serverBin “ . $GLOBALS[APACHE_INFO][restart_opt]; echo “Restarting Apache: $cmd\n”; exec($cmd, $output, $status); return ($status) ? FALSE : TRUE; } function writeTestPage() { $testFile = sprintf(“%s/test.txt”, $GLOBALS[SITE_INFO][DOCUMENT_ROOT]); echo “Writing test page: $testFile \n”; $fp = fopen($testFile, ‘w’); if ($fp) { fputs($fp, “SUCCESS”); fclose($fp); Continued Chapter 17: Apache Virtual Host Maker 625 22 549669 ch17.qxd 4/4/03 9:27 AM Page 625 . explode(‘:’, $record); if (!strcmp($str[0], $user)) return TRUE; } return FALSE; } 622 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 622 function groupExists($group =. FALSE; } $cmd = “$serverBin “ . $GLOBALS[APACHE_INFO][config_chk_opt] . “ “ . $file ; 624 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 624 echo “Checking syntax: $file ”; exec($cmd,