-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeoIpBackground.php
66 lines (52 loc) · 1.75 KB
/
geoIpBackground.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
<?php
require_once __DIR__ . "/func-proxy.php";
if (function_exists('header')) {
// Allow from any origin
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: application/json; charset=utf-8');
if (isset($_REQUEST['uid'])) {
setUserId($_REQUEST['uid']);
}
// only allow user with Google Analytics cookie
if (!isset($_COOKIE['_ga'])) {
exit('Access Denied');
}
// check admin
$isAdmin = !empty($_SESSION['admin']) && $_SESSION['admin'] === true;
}
// Run a long-running process in the background
$lock_files = [];
$file = __DIR__ . "/geoIp.php";
$output_file = __DIR__ . '/proxyChecker.txt';
$pid_file = __DIR__ . '/filterPorts.pid';
$lock_files[] = $pid_file;
setMultiPermissions([$file, $output_file, $pid_file]);
$isWin = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
$cmd = "php " . escapeshellarg($file);
$uid = getUserId();
$cmd .= " --userId=" . escapeshellarg($uid);
if (isset($_REQUEST['proxy'])) {
$cmd .= " --str=" . escapeshellarg(rawurldecode($_REQUEST['proxy']));
}
// validate lock files
$lock_file = tmp() . '/runners/geoIp.lock';
$lock_files[] = $lock_file;
if (file_exists($lock_file) && !$isAdmin) {
exit(date(DATE_RFC3339) . ' another process still running' . PHP_EOL);
}
echo $cmd . "\n\n";
$cmd = sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, escapeshellarg($output_file), escapeshellarg($pid_file));
$runner = __DIR__ . "/tmp/runners/" . basename(__FILE__, '.php') . ($isWin ? '.bat' : "");
setMultiPermissions($runner);
write_file($runner, $cmd);
runBashOrBatch($runner);
function exitProcess()
{
global $lock_files;
foreach ($lock_files as $file) {
delete_path($file);
}
}
register_shutdown_function('exitProcess');