hosts); if (file_exists($this->tmp . "/hostsbackup")) throw new Exception('denwer already running. stop it or delete ' . $this->tmp . '/hostsbackup file'); if (!file_put_contents($this->tmp . "/hostsbackup", $hosts)) throw new Exception('unable to backup hosts file'); echo "Backing up hosts:\t[OK]\r\n"; $folders = array(); foreach (array_diff(scandir($this->dir . "/"), array( ".", ".." )) as $directory) { if (is_dir($this->dir . "/" . $directory)) $folders[] = $directory; } if (count($folders) < 1) throw new Exception('no folders in ' . $this->dir); $pattern = "\r\n"; $pattern .= "ServerName {host}\r\n"; $pattern .= "ServerAlias www.{host}\r\n"; $pattern .= "DocumentRoot {dir}\r\n"; $pattern .= "\r\n\r\n"; $vhosts = ""; foreach ($folders as $vhost) { $temp = str_replace("{dir}", $this->dir . "/" . $vhost, $pattern); $vhosts .= str_replace("{host}", $vhost, $temp); $hosts .= "127.0.0.1 $vhost\r\n"; } if (!file_put_contents($this->vhosts, $vhosts)) throw new Exception('cant write ' . $this->vhosts); if (!file_put_contents($this->hosts, $hosts)) throw new Exception('cant write ' . $this->hosts); $exec = `/etc/init.d/apache2 reload`; if (!substr_count($exec, 'done.')) throw new Exception('apache wont start'); echo "Starting Denwer:\t[OK]\r\n"; } public function stop() { if (!file_exists($this->tmp . "/hostsbackup")) throw new Exception('cant locate hostsbackup file'); echo "Restoring hosts:\t[OK]\r\n"; if (!file_put_contents($this->hosts, file_get_contents($this->tmp . "/hostsbackup"))) throw new Exception('cant write ' . $this->hosts . ' file'); if (!file_put_contents($this->vhosts, " ")) throw new Exception('cant write ' . $this->vhosts . ' file'); $exec = `/etc/init.d/apache2 reload`; if (!substr_count($exec, 'done.')) throw new Exception('apache wont start'); echo "Stopping Denwer:\t[OK]\r\n"; unlink($this->tmp . '/hostsbackup'); } } if (!isset($argv[1])) { die('Available actions: start|stop' . "\r\n"); } $actions = array( "start", "stop" ); if (!in_array($argv[1], $actions)) { echo 'Invalid action: ' . $argv[1] . "\r\n"; die('Available actions: start|stop' . "\r\n"); } if (!@touch('/etc/init.d/a')) { die('This sсript must be runned as root' . "\r\n"); } try { $instance = new Denwer(); $instance->$argv[1](); } catch (Exception $e) { die('Error: ' . $e->getMessage() . "\r\n"); }