forked from poppyred/1.6.x
/
exec.unifi.php
executable file
·453 lines (364 loc) · 15.2 KB
/
exec.unifi.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/php -q
<?php
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
$GLOBALS["FORCE"]=false;
$GLOBALS["RECONFIGURE"]=false;
$GLOBALS["SWAPSTATE"]=false;
$GLOBALS["NOSQUIDOUTPUT"]=true;
$GLOBALS["TITLENAME"]="UniFi controller";
$GLOBALS["PROGRESS"]=false;
$GLOBALS["MIGRATION"]=false;
if(preg_match("#--verbose#",implode(" ",$argv))){$GLOBALS["VERBOSE"]=true;
$GLOBALS["OUTPUT"]=true;$GLOBALS["debug"]=true;ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);}
if(preg_match("#--output#",implode(" ",$argv))){$GLOBALS["OUTPUT"]=true;}
if(preg_match("#schedule-id=([0-9]+)#",implode(" ",$argv),$re)){$GLOBALS["SCHEDULE_ID"]=$re[1];}
if(preg_match("#--force#",implode(" ",$argv),$re)){$GLOBALS["FORCE"]=true;}
if(preg_match("#--reconfigure#",implode(" ",$argv),$re)){$GLOBALS["RECONFIGURE"]=true;}
if(preg_match("#--migration#",implode(" ",$argv),$re)){$GLOBALS["MIGRATION"]=true;}
$GLOBALS["AS_ROOT"]=true;
include_once(dirname(__FILE__).'/ressources/class.ldap.inc');
include_once(dirname(__FILE__).'/ressources/class.squid.inc');
include_once(dirname(__FILE__).'/ressources/class.mysql.inc');
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__).'/framework/frame.class.inc');
include_once(dirname(__FILE__).'/framework/class.settings.inc');
include_once(dirname(__FILE__).'/ressources/class.os.system.inc');
include_once(dirname(__FILE__).'/ressources/class.system.nics.inc');
include_once(dirname(__FILE__).'/ressources/class.influx.inc');
$GLOBALS["ARGVS"]=implode(" ",$argv);
if($argv[1]=="--build"){build();die();}
if($argv[1]=="--stop"){$GLOBALS["OUTPUT"]=true;stop();die();}
if($argv[1]=="--start"){$GLOBALS["OUTPUT"]=true;start();die();}
if($argv[1]=="--restart"){$GLOBALS["OUTPUT"]=true;restart_progress();die();}
if($argv[1]=="--install"){$GLOBALS["OUTPUT"]=true;install();die();}
if($argv[1]=="--install-progress"){$GLOBALS["OUTPUT"]=true;$GLOBALS["PROGRESS"]=true;install();die();}
if($argv[1]=="--restart-progress"){$GLOBALS["OUTPUT"]=true;$GLOBALS["PROGRESS"]=true;restart_progress();die();}
function restart() {
$unix=new unix();
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
stop(true);
build();
sleep(1);
start(true);
}
function DebianVersion(){
$ver=trim(@file_get_contents("/etc/debian_version"));
preg_match("#^([0-9]+)\.#",$ver,$re);
if(preg_match("#squeeze\/sid#",$ver)){return 6;}
return $re[1];
}
function install(){
include_once(dirname(__FILE__)."/ressources/class.ccurl.inc");
$unix=new unix();
$sock=new sockets();
$DebianVersion=DebianVersion();
$aptget=$unix->find_program("apt-get");
$aptkey=$unix->find_program("apt-key");
if($DebianVersion<7){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, influxdb Debian version incompatible!\n";}
build_progress_idb("Incompatible system!",110);
die();
}
if(!is_dir("/etc/apt/sources.list.d")){@mkdir("/etc/apt/sources.list.d");}
@file_put_contents("/etc/apt/sources.list.d/ubiquiti.list", "\ndeb http://www.ubnt.com/downloads/unifi/debian stable ubiquiti\n");
system("$aptkey adv --keyserver keyserver.ubuntu.com --recv C0A52C50");
build_progress_idb("{update_debian_repository}",20);
$cmd="DEBIAN_FRONTEND=noninteractive $aptget -o Dpkg::Options::=\"--force-confnew\" --force-yes -y update 2>&1";
system($cmd);
build_progress_idb("{installing_package}",50);
$cmd="DEBIAN_FRONTEND=noninteractive $aptget -o Dpkg::Options::=\"--force-confnew\" --force-yes -y install unifi 2>&1";
system($cmd);
build_progress_idb("{installing_package} {done}",55);
if(is_file("/usr/lib/unifi/lib/ace.jar")){
$sock->SET_INFO("EnableUnifiController",1);
build_progress_idb("{restarting_services}",60);
system("/etc/init.d/artica-status restart --force");
build_progress_idb("{restarting_services}",70);
restart_progress(true);
build_progress_idb("{done}",100);
return;
}
build_progress_idb("{failed_to_install}",110);
}
function download_progress( $download_size, $downloaded_size, $upload_size, $uploaded_size ){
if(!isset($GLOBALS["previousProgress"])){$GLOBALS["previousProgress"]= 0;}
if ( $download_size == 0 ){
$progress = 0;
}else{
$progress = round( $downloaded_size * 100 / $download_size );
}
if ( $progress > $GLOBALS["previousProgress"]){
if($progress<95){
build_progress_idb("{downloading}",$progress);
}
$GLOBALS["previousProgress"]=$progress;
}
}
function restart_progress($aspid=false){
$unix=new unix();
if(!$aspid){
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
}
@file_put_contents($pidfile, getmypid());
build_progress_rs("{stopping_service}",10);
if(!stop(true)){return;}
sleep(1);
build();
build_progress_rs("{starting_service}",30);
start(true);
}
function GetUUID(){
$f=explode("\n",@file_get_contents("/usr/lib/unifi/data/system.properties"));
while (list ($key, $line) = each ($f) ){
$line=trim($line);
if($line==null){continue;}
if(!preg_match("#^uuid=(.+)#", $line,$re)){continue;}
return trim($re[1]);
}
}
function build(){
$unix=new unix();
$sock=new sockets();
$UnifiListenIP=null;
$NETWORK_ALL_INTERFACES=$unix->NETWORK_ALL_INTERFACES();
$UnifiListenInterface=$sock->GET_INFO("UnifiListenInterface");
if($UnifiListenInterface<>null){
$UnifiListenIP=$NETWORK_ALL_INTERFACES[$UnifiListenIP]["IPADDR"];
if($UnifiListenIP=="0.0.0.0"){$UnifiListenIP=null;}
}
$UnifiHTTPPort=intval($sock->GET_INFO("UnifiHTTPPort"));
$UnifiHTTPSPort=intval($sock->GET_INFO("UnifiHTTPSPort"));
$UnifiPortalPort=intval($sock->GET_INFO("UnifiPortalPort"));
$UnifiPortalSSLPort=intval($sock->GET_INFO("UnifiPortalSSLPort"));
$UnifiUDPPort=intval($sock->GET_INFO("UnifiUDPPort"));
$UnifiUUID=$sock->GET_INFO("UnifiUUID");
if($UnifiUUID==null){$UnifiUUID=GetUUID();}
if($UnifiHTTPPort==0){$UnifiHTTPPort=8088;}
if($UnifiHTTPSPort==0){$UnifiHTTPSPort=8443;}
if($UnifiPortalPort==0){$UnifiPortalPort=8880;}
if($UnifiPortalSSLPort==0){$UnifiPortalSSLPort=8943;}
if($UnifiUDPPort==0){$UnifiUDPPort=3478;}
$f[]="## system.properties";
$f[]="#";
$f[]="# each unifi instance requires a set of ports:";
$f[]="#";
$f[]="unifi.http.port=$UnifiHTTPPort";
$f[]="unifi.https.port=$UnifiHTTPSPort";
$f[]="portal.http.port=$UnifiPortalPort";
$f[]="portal.https.port=$UnifiPortalSSLPort";
$f[]="# unifi.db.port=27117";
$f[]="# unifi.stun.port=$UnifiUDPPort";
$f[]="#";
if($UnifiListenIP<>null){
$f[]="system_ip=$UnifiListenIP # the IP devices should be talking to for inform";
}
$f[]="# unifi.db.nojournal=false # disable mongodb journaling";
$f[]="# unifi.db.extraargs # extra mongod args";
$f[]="#";
$f[]="## HTTPS options";
$f[]="# unifi.https.ciphers=TLS_RSA_WITH_AES_256_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA";
$f[]="# unifi.https.sslEnabledProtocols=TLSv1";
$f[]="#";
$f[]="#";
$f[]="#". date("Y-m-d H:i:s");
$f[]="uuid=$UnifiUUID\n";
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, save settings done\n";}
@file_put_contents("/usr/lib/unifi/data/system.properties", @implode("\n", $f));
}
function start($aspid=false){
$unix=new unix();
$sock=new sockets();
$Masterbin="/opt/influxdb/influxd";
if(!$aspid){
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
}
build_progress_rs("{starting_service}",30);
if(!is_file($Masterbin)){
Install();
if(!is_file($Masterbin)){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]}, influxdb not installed\n";}
build_progress_rs("{failed_to_start_service} ({not_installed})",110);
return;
}
}
$pid=PID_NUM();
$EnableUnifiController=intval($sock->GET_INFO("EnableUnifiController"));
$UnifiHTTPPort=intval($sock->GET_INFO("UnifiHTTPPort"));
if($UnifiHTTPPort==0){$UnifiHTTPPort=8088;}
if($unix->process_exists($pid)){
$timepid=$unix->PROCCESS_TIME_MIN($pid);
if($EnableUnifiController==0){stop(true);}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Service already started $pid since {$timepid}Mn...\n";}
build_progress_rs("{already_running}",100);
return true;
}
if($EnableUnifiController==0){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Disabled\n";}
build_progress_rs("{starting_service} {failed} ({disabled})",110);
return false;
}
$php5=$unix->LOCATE_PHP5_BIN();
$sysctl=$unix->find_program("sysctl");
$echo=$unix->find_program("echo");
$nohup=$unix->find_program("nohup");
$date=$unix->find_program("date");
build_progress_rs("{starting_service}!",35);
build_progress_rs("{starting_service}",45);
$cmd="/etc/init.d/unifi start >/dev/null 2>&1 &";
if($GLOBALS["VERBOSE"]){echo "$cmd\n";}
shell_exec($cmd);
for($i=1;$i<5;$i++){
build_progress_rs("{starting_service}",45+$i);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} waiting $i/5\n";}
sleep(1);
$pid=PID_NUM();
if($unix->process_exists($pid)){break;}
}
$pid=PID_NUM();
if($unix->process_exists($pid)){
build_progress_rs("{starting_service}",50);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Success PID $pid\n";}
build_progress_rs("{starting_service} {success}",100);
return true;
}else{
build_progress_rs("{failed_to_start_service}",110);
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} Failed\n";}
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} $cmd\n";}
return false;
}
}
function test_listen_port(){
$sock=new sockets();
$UnifiHTTPPort=intval($sock->GET_INFO("UnifiHTTPPort"));
if($UnifiHTTPPort==0){$UnifiHTTPPort=8088;}
$unix=new unix();
$sock=new sockets();
$UnifiListenIP=null;
$NETWORK_ALL_INTERFACES=$unix->NETWORK_ALL_INTERFACES();
$UnifiListenInterface=$sock->GET_INFO("UnifiListenInterface");
if($UnifiListenInterface<>null){
$UnifiListenIP=trim($NETWORK_ALL_INTERFACES[$UnifiListenIP]["IPADDR"]);
if($UnifiListenIP=="0.0.0.0"){$UnifiListenIP=null;}
}
if(trim($UnifiListenIP)==null){$UnifiListenIP="127.0.0.1";}
$fp=@fsockopen($UnifiListenIP, $UnifiHTTPPort, $errno, $errstr, 2);
if(!$fp){
if($GLOBALS["OUTPUT"]){echo "Starting......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} $UnifiListenIP:$UnifiHTTPPort ($errstr)\n";}
@socket_close($fp);
return false;
}
@socket_close($fp);
return true;
}
function stop($aspid=false){
$unix=new unix();
if(!$aspid){
$pidfile="/etc/artica-postfix/pids/".basename(__FILE__).".".__FUNCTION__.".pid";
$pid=$unix->get_pid_from_file($pidfile);
if($unix->process_exists($pid,basename(__FILE__))){
$time=$unix->PROCCESS_TIME_MIN($pid);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service Already Artica task running PID $pid since {$time}mn\n";}
return;
}
@file_put_contents($pidfile, getmypid());
}
$pid=PID_NUM();
if(!$unix->process_exists($pid)){
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service already stopped...\n";}
return true;
}
$pid=PID_NUM();
$nohup=$unix->find_program("nohup");
$php5=$unix->LOCATE_PHP5_BIN();
$kill=$unix->find_program("kill");
build_progress_rs("{stopping_service}",15);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service Shutdown pid $pid...\n";}
system("/etc/init.d/unifi stop >/dev/null 2>&1 &");
for($i=0;$i<5;$i++){
build_progress_rs("{stopping_service}",15+$i);
$pid=PID_NUM();
if(!$unix->process_exists($pid)){break;}
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service waiting pid:$pid $i/5...\n";}
sleep(1);
}
$pid=PID_NUM();
if(!$unix->process_exists($pid)){
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service success...\n";}
return true;
}
build_progress_rs("{stopping_service}",30);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service shutdown - force - pid $pid...\n";}
unix_system_kill_force($pid);
for($i=0;$i<5;$i++){
build_progress_rs("{stopping_service}",30+$i);
$pid=PID_NUM();
if(!$unix->process_exists($pid)){break;}
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service waiting pid:$pid $i/5...\n";}
sleep(1);
}
if($unix->process_exists($pid)){
build_progress_rs("{stopping_service} {failed}",110);
squid_admin_mysql(0, "Failed to stop Unifi Controller Engine",__FILE__,__LINE__);
if($GLOBALS["OUTPUT"]){echo "Stopping......: ".date("H:i:s")." [INIT]: {$GLOBALS["TITLENAME"]} service failed...\n";}
return;
}
return true;
}
function PID_NUM(){
if(!isset($GLOBALS["CLASS_UNIX"])){$GLOBALS["CLASS_UNIX"]=new unix();}
$pid=$GLOBALS["CLASS_UNIX"]->get_pid_from_file("/var/run/unifi/unifi.pid");
if($GLOBALS["CLASS_UNIX"]->process_exists($pid)){return $pid;}
return $GLOBALS["CLASS_UNIX"]->PIDOF_PATTERN("/usr/lib/unifi/lib/ace.jar");
}
function build_progress_rdb($pourc,$text){
$GLOBALS["CACHEFILE"]="/usr/share/artica-postfix/ressources/logs/influxdb.remove.progress";
echo "{$pourc}% $text\n";
$cachefile=$GLOBALS["CACHEFILE"];
$array["POURC"]=$pourc;
$array["TEXT"]=$text;
@file_put_contents($cachefile, serialize($array));
@chmod($cachefile,0755);
}
function build_progress_idb($text,$pourc){
$GLOBALS["CACHEFILE"]="/usr/share/artica-postfix/ressources/logs/unifi.install.progress";
echo "{$pourc}% $text\n";
$cachefile=$GLOBALS["CACHEFILE"];
$array["POURC"]=$pourc;
$array["TEXT"]=$text;
@file_put_contents($cachefile, serialize($array));
@chmod($cachefile,0755);
}
function build_progress_rs($text,$pourc){
if(!isset($GLOBALS["PROGRESS"])){return;}
$echotext=$text;
$echotext=str_replace("{reconfigure}", "Reconfigure", $echotext);
echo "Starting......: ".date("H:i:s")." {$pourc}% $echotext\n";
$cachefile="/usr/share/artica-postfix/ressources/logs/unifi.restart.progress";
$array["POURC"]=$pourc;
$array["TEXT"]=$text;
@file_put_contents($cachefile, serialize($array));
@chmod($cachefile,0755);
}
?>