// Copy in to SITE_INFO $GLOBALS[SITE_INFO][user] = $request[user]; $GLOBALS[SITE_INFO][group] = $request[group]; $GLOBALS[SITE_INFO][vhost] = $request[vhost]; // Check to see if given vhost is a hostname if (!checkVhostName($request[vhost])) { echo “Error: virtual hostname $request[vhost] is not valid.\n”; return null; } if (empty($request[group])) { $request[group] = $request[user]; } return $request; } function getValue($cmd = null, $short, $long) { return (isset($cmd[$short])) ? $cmd[$short] : $cmd[$long]; } function syntax() { $script = basename($GLOBALS[‘argv’][0]); echo<<<HELP Syntax $script [options] -h help shows this help -u username user username the name of the user account Continued Chapter 17: Apache Virtual Host Maker 631 22 549669 ch17.qxd 4/4/03 9:27 AM Page 631 Listing 17-5 (Continued) -p password pass password the password for the user account -v hostname vhost hostname the virtual hostname -t account_type type account_type sets type of account -r restart restarts Apache after configuration test OK -t test runs test to access http://hostname HELP; } function getCommandLineOptions($options) { $type = gettype($options); if (gettype($options) != “array”) { // Error in command line echo “$options->message \n”; return null; } $cmd = array(); foreach ($options[0] as $argArray) { $argName = preg_replace(‘/[^\w]/’ , ‘’, $argArray[0]); $argValue = $argArray[1]; if ($argValue[0] == ‘-’) { echo “$argName cannot have $argValue\n”; return array(); } 632 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 632 $cmd[$argName] = ($argValue != ‘’) ? $argValue : TRUE; } return (count($cmd) > 0) ? $cmd : null; } function addContents($site = null, $template = null) { $file = sprintf(“%s/%s”, $GLOBALS[TEMPLATE_DIR], $template); if (!file_exists($file)) { echo “Virtual host content template $file does not exists!\n”; return null; } // Load template require_once($file); $results = copyContentsToSite($site); return $results; } function sendMail($info = null, $request = null, $template = null) { $mailTemplate = sprintf(“%s/%s”, $GLOBALS[TEMPLATE_DIR], $template); echo “Sending mail to $to using $mailTemplate\n”; $to = $request[notify_email]; if (! file_exists($mailTemplate)) { echo “Error: mail template $mailTemplate not found! \n”; return FALSE; } $lines = file($mailTemplate); $contentTypeSet = FALSE; $message = array(); Continued Chapter 17: Apache Virtual Host Maker 633 22 549669 ch17.qxd 4/4/03 9:27 AM Page 633 Listing 17-5 (Continued) $headers = array(); foreach ($lines as $str) { $index++; if (preg_match(‘/From:\s*(.+)/i’, $str, $match)) { array_push($headers, “From: $match[1] \r\n”); } else if (preg_match(‘/Subject:\s*(.+)/i’, $str, $match)) { $subject = $match[1]; } else if (preg_match(‘/^CC:\s*(.+)/i’, $str, $match)) { array_push($headers, “Cc: $match[1] \r\n”); } else if (preg_match(‘/Bcc:\s*(.+)/i’, $str, $match)) { array_push($headers, “Bcc: $match[1] \r\n”); } else if (preg_match(‘/Content-Type:\s*(.+)/i’, $str, $match)) { if (preg_match(‘/html/’, $match[1])) { array_push($headers, “Content-Type: text/html\r\n”); } else { array_push($headers, “Content-Type: text/plain\n”); } $contentTypeSet = TRUE; } else if (preg_match(‘/MIME-Version:\s*(.+)/i’, $str, $match)) { array_push($headers, “MIME-Version: $match[1] \r\n”); } else { array_push($message, $str); } } if (! $contentTypeSet) array_push($headers, “Content-Type: text/plain\r\n”); $body = implode(‘’, $message); 634 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 634 $search = array(); $replace = array(); foreach ($info as $key => $value) { array_push($search, ‘/<%’ . strtoupper($key) . ‘%>/’); array_push($replace, $value); } foreach ($request as $key => $value) { array_push($search, ‘/<%’ . strtoupper($key) . ‘%>/’); array_push($replace, $value); } $body = preg_replace($search, $replace, $body); $subject = preg_replace($search, $replace, $subject); $headerStr = implode(‘’, $headers); mail($to, $subject, $body, $headerStr); return TRUE; } ?> This makesite script builds on the command-line examples discussed throughout this portion of the book. The makesite script uses the two types of command-line arguments, short and long. The short arguments that are allowed are defined in the $CMD_SHORT_OPTIONS string, and the long ones are defined in $CMD_LONG_OPTIONS. These strings are needed for the Console_Getopt::getopt() function, which is available from the Console/Getopt.php class in the PEAR package. The makesite script works as follows: 1. It retrieves all the command-line arguments using the getCommandLine Options() function and stores them in the $cmd array. 2. If –add is included as a command-line argument then the script performs the actual task of adding the new virtual Web site. 3. It calls the makeAddRequest() method to create an array called $request with user-supplied information such as virtual host name (supplied using the vhost host name or the -v hostname), account type (-t account_type or type account_type), username ( user username), password (-p password or pass password), and so on. Chapter 17: Apache Virtual Host Maker 635 22 549669 ch17.qxd 4/4/03 9:27 AM Page 635 . ($argValue[0] == ‘-’) { echo “$argName cannot have $argValue
”; return array(); } 632 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 632 $cmd[$argName] = ($argValue !=. array_push($headers, “Content-Type: text/plain
”); $body = implode(‘’, $message); 634 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 634 $search = array(); $replace =. are needed for the Console_Getopt::getopt() function, which is available from the Console/Getopt .php class in the PEAR package. The makesite script works as follows: 1. It retrieves all the command-line