-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_domain.php
165 lines (157 loc) · 5.12 KB
/
create_domain.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
require 'config.php';
$parser = new PEAR2\Console\CommandLine(array(
'description' => 'Creates new domain',
'version' => '0.0.1', // the version of your program
));
$parser->addOption(
'interactive',
array(
'short_name' => '-i',
'long_name' => '--interactive',
'description' => 'Run interactively',
'action' => 'StoreTrue'
)
);
$parser->addOption(
'name',
array(
'short_name' => '-n',
'long_name' => '--name',
'description' => 'domain name',
'action' => 'StoreString',
'help_name' => 'domain_name'
)
);
$parser->addOption(
'owner',
array(
'short_name' => '-o',
'long_name' => '--owner',
'description' => 'system user name',
'action' => 'StoreString',
'help_name' => 'system_user_name'
)
);
$parser->addOption(
'address',
array(
'short_name' => '-a',
'long_name' => '--ip-address',
'description' => 'domain IP address',
'action' => 'StoreString',
'help_name' => 'ip_address'
)
);
try {
$result = $parser->parse();
if ($result->options["interactive"]){
//TODO Запуск в интерактивном режиме
}
if (empty(array_filter($result->options))){
$parser->displayUsage();
}
try {
$cmd = escapeshellcmd('id -u ' . $result->options["owner"]);
$uid = exec($cmd . " 2>&1",$aResult, $return_val);
if ($return_val<>0)
throw new \Exception ("Can't get user uid");
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
$cmd = escapeshellcmd('id -g ' . $result->options["owner"]);
$gid = exec($cmd . " 2>&1",$aResult, $return_val);
if ($return_val<>0)
throw new \Exception ("Can't get user gid");
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
if(! @mkdir('/home/' . $result->options["owner"] . '/domains/' . $result->options["name"], 0755)) {
$mkdirErrorArray = error_get_last();
throw new Exception('Cant create directory ' .$mkdirErrorArray['message'], 1);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
if(! @chown('/home/' . $result->options["owner"] . '/domains/' . $result->options["name"], $result->options["owner"])) {
$mkdirErrorArray = error_get_last();
throw new Exception('Cant change owner ' .$mkdirErrorArray['message'], 1);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
$filename = '/etc/apache2/sites-available/' . $result->options["owner"];
if (!file_exists($filename)) {
try {
if(! @mkdir($filename, 0755)) {
$mkdirErrorArray = error_get_last();
throw new Exception('Cant create directory ' .$mkdirErrorArray['message'], 1);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
}
unset($filename);
$filename = '/etc/apache2/sites-available/' . $result->options["owner"] . '/' . $result->options["name"] . '.conf';
try {
if(! @copy('templates/apache.template', $filename)) {
$mkdirErrorArray = error_get_last();
throw new Exception('Cant create config file ' .$mkdirErrorArray['message'], 1);
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
if (($domain_tpl = file_get_contents($filename)) === FALSE) {
throw new \Exception ("Can't open template file");
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
$fixed_domain_tpl = str_replace("%%ip%%", $result->options["address"], $domain_tpl);
$fixed_domain_tpl = str_replace("%%domain%%", $result->options["name"], $fixed_domain_tpl);
$fixed_domain_tpl = str_replace("%%user%%", $result->options["owner"], $fixed_domain_tpl);
$fixed_domain_tpl = str_replace("%%group%%", $result->options["owner"], $fixed_domain_tpl);
try {
if (($domain_tpl = file_put_contents($filename,$fixed_domain_tpl)) === FALSE) {
throw new \Exception ("Can't write to template file");
}
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
if (!symlink($filename,"/etc/apache2/sites-enabled/" . $result->options["name"] . ".conf"))
throw new \Exception ("Can't create symlink");
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
try {
$cmd = escapeshellcmd('service apache2 reload');
$gid = exec($cmd . " 2>&1",$aResult, $return_val);
if ($return_val<>0)
throw new \Exception ("Can't reload Apache");
} catch (Exception $ex) {
echo 'Error: ', $ex->getMessage(), "\n";
exit(1);
}
}
catch (Exception $exc) {
$parser->displayError($exc->getMessage());
}