-
Notifications
You must be signed in to change notification settings - Fork 6
/
AllScanInstallUpdate.php
executable file
·291 lines (258 loc) · 9.42 KB
/
AllScanInstallUpdate.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
#!/usr/bin/php
<?php
$AllScanInstallerUpdaterVersion = "v1.23";
define('NL', "\n");
// Execute this script by running "sudo ./AllScanInstallUpdate.php" from any directory. The script will then determine
// the location of the web root folder on your system, cd to that folder, check if you have AllScan installed and install
// it if not, or if already installed will check the version and update the install if a newer version is available.
//
// NOTE: Updating can result in modified files being overwritten. This script will make a backup copy of the allscan
// folder to allscan.bak.[ver#]/ You may then need to copy any files you added/modified back into the allscan folder.
//
msg("AllScan Installer/Updater Version: $AllScanInstallerUpdaterVersion");
// This should be run from CLI only (SSH), not over the web
if(isset($_SERVER['REMOTE_ADDR']) || isset($_SERVER['HTTP_HOST'])) {
echo "This script must be run from the Command Line Interface only.<br>\n";
exit(0);
}
// Determine web server folder
$dirs = ['/var/www/html', '/srv/http'];
foreach($dirs as $d) {
if(is_dir($d)) {
$webdir = $d;
break;
}
}
msg("Web Server Folder: " . (isset($webdir) ? $webdir : "Not Found."));
// Determine web server group name
$name = ['www-data', 'http', 'apache'];
foreach($name as $n) {
if(`grep "^$n:" /etc/group`) {
$group = $n;
break;
}
}
msg("Web Group Name: " . (isset($group) ? $group : "Not Found."));
if(!isset($webdir) || !isset($group))
exit();
// cd to web root folder
$cwd = getcwd();
if($cwd !== $webdir) {
msg("Changing dir from $cwd to $webdir");
chdir($webdir);
$cwd = getcwd();
if($cwd !== $webdir)
errExit("cd failed.");
}
// Destination dir in web root folder:
$asdir = 'allscan';
// Enable mkdir(..., 0775) to work properly
umask(0002);
// Check if dir exists. If so, see if an update is needed. If not, install AllScan
$dlfiles = true;
if(is_dir($asdir)) {
msg("$asdir dir exists. Checking if update needed...");
$fname = 'common.php';
if(($s = `grep '^\$AllScanVersion' $asdir/include/$fname`)) {
if(preg_match('/"v([0-9\.]{3,5})"/', $s, $m) == 1)
$ver = $m[1];
}
if(empty($ver))
$ver = 'Unknown';
msg("Allscan current version: $ver");
msg("Checking github version...");
if(file_exists($fname)) {
unlink($fname);
if(file_exists($fname))
errExit("$fname already exists in cwd, delete failed.");
}
$url = 'https://raw.githubusercontent.com/davidgsd/AllScan/main/include/' . $fname;
if(!execCmd("wget -q '$url'") || !file_exists($fname))
errExit("wget $fname failed.");
if(($s = `grep '^\$AllScanVersion' $fname`)) {
if(preg_match('/"v([0-9\.]{3,5})"/', $s, $m) == 1)
$gver = $m[1];
}
unlink($fname);
if(empty($gver))
$gver = 'Unknown';
msg("Allscan github version: $gver");
if($gver <= $ver) {
msg("AllScan is up-to-date.");
$dlfiles = false;
} else {
msg("AllScan is out-of-date.");
$s = readline("Ready to Update AllScan. Enter 'y' to confirm, any other key to exit: ");
if($s !== 'y')
exit();
$bak = "$asdir.bak.$ver";
msg("Moving $asdir/ to $bak/...");
if(is_dir($bak))
execCmd("rm -rf $bak");
execCmd("mv $asdir $bak");
}
} else {
msg("$asdir dir not found.");
$s = readline("Ready to Install AllScan. Enter 'y' to confirm, any other key to exit: ");
if($s !== 'y')
exit();
}
if($dlfiles) {
$fname = 'main.zip';
$url = 'https://github.com/davidgsd/AllScan/archive/refs/heads/' . $fname;
$zdir = 'AllScan-main';
if(file_exists($fname))
unlink($fname);
if(is_dir($zdir))
exec("rm -rf $zdir");
if(!execCmd("wget -q '$url'") || !file_exists($fname))
errExit("Retrieve $fname from github failed. Try executing \"wget '$url'\" and check error messages. Also confirm that your node supports https and that its system time/RTC is set correctly.");
if(!execCmd("unzip -q $fname"))
errExit('Unzip failed. Check that you have unzip installed. Try "sudo apt-get install unzip" to install');
unlink($fname);
if(!rename($zdir, $asdir))
msg("ERROR: mv($zdir, $asdir) failed");
// Copy any user .ini files from old version backup folder
if(isset($bak) && is_dir($bak)) {
msg("Checking for .ini files in $bak/...");
execCmd("cp -n $bak/*.ini $asdir/");
}
}
msg("Verifying $asdir dir has $group group writable permissions");
execCmd("chmod 775 $asdir");
execCmd("chgrp $group $asdir");
execCmd("chmod 664 $asdir/*.ini");
execCmd("chgrp $group $asdir/*.ini");
checkDbDir();
checkSmDir();
msg("PHP Version: " . phpversion());
// Confirm necessary php extensions are installed
msg("Checking OS packages and PHP extensions...");
if(!class_exists('SQLite3')) {
msg("NOTE: Required SQLite3 Class not found." . NL
."Try running the update commands below to update your OS and php-sqlite3 package." . NL
."You may also need to enable the pdo_sqlite and sqlite3 extensions in php.ini.");
}
msg("Ready to run OS update/upgrade commands." . NL
."If you have recently updated and upgraded your system you do not need to do this now." . NL
."NOTE: THE UPDATE/UPGRADE PROCESS CAN POTENTIALLY CAUSE OLDER SOFTWARE PACKAGES TO STOP WORKING." . NL
."DO NOT PROCEED WITH THIS STEP IF YOU ARE NOT SURE OR IF YOU ARE NOT AN AUTHORIZED SERVER ADMIN." . NL
."Otherwise it is recommended to ensure your system is up-to-date.");
$s = readline("Enter 'y' to proceed, any other key to skip this step: ");
if($s === 'y') {
if(is_executable('/usr/bin/apt-get')) {
passthruCmd("apt-get -y update");
passthruCmd("apt-get -y upgrade");
passthruCmd("apt-get install -y php-sqlite3 php-curl");
} else if(is_executable('/usr/bin/yum')) {
passthruCmd("yum -y update");
passthruCmd("yum -y upgrade");
} else if(is_executable('/usr/bin/pacman')) {
passthruCmd("pacman -Syu");
passthruCmd("pacman -S php-sqlite");
}
msg("Restarting web server...");
if(is_executable('/usr/bin/apachectl') || is_executable('/usr/sbin/apachectl'))
$cmd = "apachectl restart 2> /dev/null";
else
$cmd = "systemctl restart lighttpd.service 2> /dev/null";
if(!execCmd($cmd))
msg("Restart webserver or restart node now");
}
// if ASL3, make sure astdb.txt is available
if(is_executable('/usr/bin/apt-get') && !is_file('/etc/systemd/system/asl3-update-astdb.service')) {
passthruCmd("sudo apt install -y asl3-update-nodelist 2> /dev/null");
}
if(is_file('/etc/systemd/system/asl3-update-astdb.service')) {
passthruCmd("systemctl enable asl3-update-astdb.service 2> /dev/null");
passthruCmd("systemctl enable asl3-update-astdb.timer 2> /dev/null");
passthruCmd("systemctl start asl3-update-astdb.timer 2> /dev/null");
// Make a readable copy of allmon3.ini (Allmon3 updates can reset the file permissions)
$fname = '/etc/allmon3/allmon3.ini';
if(file_exists($fname)) {
$fname2 = '/etc/asterisk/allmon.ini.php';
execCmd("cp $fname $fname2");
execCmd("chmod 660 $fname2");
execCmd("chgrp $group $fname2");
}
}
msg("Install/Update Complete.");
// Show URLs where AllScan can be accessed and other notes
$ip = exec("wget -t 1 -T 3 -q -O- http://checkip.dyndns.org:8245 | cut -d':' -f2 | cut -d' ' -f2 | cut -d'<' -f1");
$lanip = exec('hostname --all-ip-addresses');
if(!filter_var($lanip, FILTER_VALIDATE_IP)) {
$lanip = exec("ifconfig | grep inet | head -1 | awk '{print $2}'");
if($lanip === '127.0.0.1')
$lanip = exec("ifconfig | grep inet | tail -1 | awk '{print $2}'");
}
msg("AllScan can be accessed at:\n\thttp://$lanip/$asdir/ on the local network, or\n"
."\thttp://$ip/$asdir/ remotely if your router has a port forwarded to this node.");
msg("Be sure to bookmark the above URL(s) in your browser.");
msg("IMPORTANT: After updates do a CTRL-F5 in your browser (or long-press the reload button in mobile browsers), or clear the browser cache, so that all CSS and JavaScript files will properly update.");
exit();
// ---------------------------------------------------
// Execute command, show the command, show the output and return val
function execCmd($cmd) {
msg("Executing cmd: $cmd");
$out = '';
$res = 0;
$ok = (exec($cmd, $out, $res) !== false && !$res);
$s = $ok ? 'OK' : 'ERROR';
msg("Return Code: $s");
return $ok;
}
function passthruCmd($cmd) {
msg("Executing cmd: $cmd");
$res = 0;
$ok = (passthru($cmd, $res) !== false && !$res);
$s = $ok ? 'OK' : 'ERROR';
msg("Return Code: $s");
return $ok;
}
function checkDbDir() {
global $group, $ver;
// Confirm /etc/allscan dir exists and is writable by web server
$asdbdir = '/etc/allscan';
if(!is_dir($asdbdir)) {
msg("Creating $asdbdir dir with 0775 permissions and $group group");
if(!mkdir($asdbdir, 0775))
errExit('mkdir failed');
}
msg("Verifying $asdbdir dir has 0775 permissions and $group group");
if(!chmod($asdbdir, 0775))
msg("ERROR: chmod($asdbdir, 0775) failed");
if(!chgrp($asdbdir, $group))
msg("ERROR: chgrp($asdbdir, $group) failed");
// Backup DB file
$dbfile = $asdbdir . '/allscan.db';
if(file_exists("$asdbdir.db")) {
if(!$ver)
$ver = 'bak';
$bakfile = "$dbfile.$ver";
copy($dbfile, $bakfile);
}
}
function checkSmDir() {
global $group;
// Verify supermon folder favorites.ini and favorites.ini.bak are writable by web server
$smdir = 'supermon';
if(is_dir($smdir)) {
$favsini = 'favorites.ini';
$favsbak = $favsini . '.bak';
msg("Confirming supermon $favsini and $favsbak are writable by web server");
chdir($smdir);
execCmd("touch $favsini $favsbak");
execCmd("chmod 664 $favsini $favsbak");
execCmd("chmod 775 .");
execCmd("chgrp $group $favsini $favsbak .");
chdir('..');
}
}
function msg($s) {
echo $s . NL;
}
function errExit($s) {
msg('ERROR: ' . $s);
msg('Check directory permissions and that this script was run as sudo/root.');
exit();
}