php Denwer sсript class Denwer private hosts etc hosts private dir hom

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
<?php
/* Denwer sсript */
class Denwer
{
private $hosts = '/etc/hosts';
private $dir = '/home/anton/server';
private $tmp = '/tmp';
private $vhosts = '/etc/apache2/sites-enabled/vhosts.conf';
public function start()
{
$hosts = file_get_contents($this->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 = "<VirtualHost *:80>\r\n";
$pattern .= "ServerName {host}\r\n";
$pattern .= "ServerAlias www.{host}\r\n";
$pattern .= "DocumentRoot {dir}\r\n";
$pattern .= "</VirtualHost>\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");
}