1. Trang chủ
  2. » Công Nghệ Thông Tin

Secure PHP Development- P132 ppsx

5 148 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 74,08 KB

Nội dung

Listing 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; 626 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 626 } $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 Chapter 17: Apache Virtual Host Maker 627 22 549669 ch17.qxd 4/4/03 9:27 AM Page 627 Listing 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) { $cmd = $GLOBALS[SYSTEM_INFO][mkdir_bin] . “ -m $mode -p $path”; 628 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 628 if (DEBUG) echo “$cmd\n”; exec($cmd); } 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 Chapter 17: Apache Virtual Host Maker 629 22 549669 ch17.qxd 4/4/03 9:27 AM Page 629 Listing 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 $request[vhost] = preg_replace(‘/\.$/’, ‘’, $request[vhost]); $request[vhost] = preg_replace(‘/^\./’, ‘’, $request[vhost]); 630 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 630 . $GLOBALS[SITE_INFO][DOCUMENT_ROOT]); if (! file_exists($testFile)) { return FALSE; 626 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 626 } $status = unlink($testFile); return. = null) { $cmd = $GLOBALS[SYSTEM_INFO][mkdir_bin] . “ -m $mode -p $path”; 628 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 628 if (DEBUG) echo “$cmd ”; exec($cmd); }. $request[vhost]); $request[vhost] = preg_replace(‘/^./’, ‘’, $request[vhost]); 630 Part IV: Using PHP for Sysadmin Tasks 22 549669 ch17.qxd 4/4/03 9:27 AM Page 630

Ngày đăng: 07/07/2014, 07:20

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN