# Use configuration tools instead! STR; $hosts_path = "/etc/hosts"; $hosts = array(); $hosts[] = preg_replace('/\\s*#\\/vhosts.*?#\\\\vhosts/s', "", file_get_contents($hosts_path)); $hosts[] = $HOSTS_START_MARKER; $hosts[] = $data; $hosts[] = $HOSTS_STOP_MARKER; file_put_contents($hosts_path, join("\n", $hosts)); } function update_httpd_conf($vhosts) { $conf_file = '/etc/apache2/vhosts/auto_generated.conf'; if (ALLOW_PRECONFIGURE_HTTPD) { if (!file_exists('/etc/apache2/vhosts')) { mkdir('/etc/apache2/vhosts'); file_put_contents('/etc/apache2/httpd.conf', "\n\nInclude /etc/apache2/other/*.conf", FILE_APPEND); } } @`touch {$conf_file}`; if (file_put_contents($conf_file, $vhosts)) { `httpd -k graceful`; } } if (!check_admin_privileges()) { fprintf(STDERR, "You don't have admin rights"); exit(1); } $service_dir = dirname(__FILE__); update_hosts(file_get_contents("{$service_dir}/var/hosts.conf")); update_httpd_conf(file_get_contents("{$service_dir}/var/vhosts.conf"));
function run() { global $argv; if ($argv[1] == '--install') { $install_mode = true; $sites_dir = $argv[2]; array_shift($argv); } else { $install_mode = false; $sites_dir = $argv[1]; } if (empty($sites_dir)) { $sites_dir = $_ENV['HOME'] . '/Sites'; } if (!is_dir($sites_dir)) { error("'{$sites_dir}' is invalid dir. You must specify path with sites folder"); exit(1); } $sites_dir = dirname($sites_dir) . '/' . basename($sites_dir); if ($install_mode) { install($sites_dir); message("Vhost configurator agent installed. \n" . "Run agent.php maunally or change something in sites root folder\n", 'install'); echo `launchctl load ~/Library/LaunchAgents`; exit(0); } $sites = array_filter(explode("\n", `ls -lt {$sites_dir} | grep ^d | awk '{print \$9}'`)); $domain_re = '/([a-z][a-z0-9_-]*(?:\\.[a-z][a-z0-9_-]*)*):?(\\d+)?/i'; $hosts = array(); $vhosts = array(); $valid_sites = array(); $invalid_sites = array(); foreach ($sites as $site) { if (preg_match($domain_re, $site, $matches)) { $site_name = $matches[1]; $port = isset($matches[2]) ? $matches[2] : 80; if ($site_name == 'localhost' && $port == 80) { error("Can`nt create vhost 'localhost[:80]'"); continue; } $valid_sites[] = $site; $hosts[] = generate_host_entry($site_name); $vhosts[] = generate_vhost_config($sites_dir, $site_name, $port); } else { $invalid_sites[] = $site; } } if (count($invalid_sites)) { message("'" . join("', '", $invalid_sites) . "' is invalid domain names."); } if (!update_if_modified_sites_list($valid_sites)) { # nothing changes. exit exit(0); } if (update_hosts($hosts, $vhosts)) { message("Apache have been succefully reconfigured", 'success'); exit(0); } else { error("Something is wrong.\nCheck log in Console.app"); exit(1); } }