/
update_hosts.php
59 lines (51 loc) · 1.22 KB
/
update_hosts.php
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
<?php
require_once 'commons.php';
require_once 'config.php';
function update_hosts($data)
{
$HOSTS_STOP_MARKER="#\\vhosts\n";
$HOSTS_START_MARKER=<<<STR
#/vhosts
# Please. Dont modify this lines directrly
# 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"));