-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell-common.php
More file actions
executable file
·141 lines (127 loc) · 2.63 KB
/
shell-common.php
File metadata and controls
executable file
·141 lines (127 loc) · 2.63 KB
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
<?php
/**
* Returns true if script is running on linux machine
* @return bool
*/
function isLinux()
{
return function_exists('posix_getuid');
}
/**
* Arguments parsing
* @param array $argv
* @return array
*/
function arguments($argv)
{
$_ARG = array('input' => array());
// First param is this scripts name..
$_ARG['name'] = array_shift($argv);
foreach ($argv as $arg) {
if (preg_match('#^-{1,2}([a-zA-Z0-9]*)=?(.*)$#', $arg, $matches)) {
$key = $matches[1];
switch ($matches[2]) {
case '':
case 'true':
$arg = true;
break;
case 'false':
$arg = false;
break;
default:
$arg = $matches[2];
}
$_ARG[$key] = $arg;
} else {
$_ARG['input'][] = $arg;
}
}
return $_ARG;
}
/**
* Parses all conf files and returns array of protocols
* @return array
*/
function getAllTrackers()
{
$files = glob(WORKING_DIR . "conf/handlers/*.conf");
$return = array();
foreach ($files as $file) {
preg_match('/conf\/handlers\/(.*)\.conf/ui', $file, $key);
$name = $key[1];
$return[$name] = readTrackerConfig($name);
}
return array_filter($return);
}
/**
* Parses conf files of given protocols and returns array
* @return array
*/
function getTrackers($names, $port = false)
{
$return = array();
foreach ($names as $name) {
$return[$name] = readTrackerConfig($name, $port);
}
return array_filter($return);
}
/**
* Processes conf file of given protocol
* @return array
*/
function readTrackerConfig($name, $port = false)
{
if ($name == 'balancer') {
return array(
'port' => 0,
'pipeconf' => getPipeConf('balancer')
);
}
$file = WORKING_DIR . "conf/handlers/$name.conf";
if (!file_exists($file)) { return false; }
$return = array('pipeconf' => getPipeConf($name));
if ($port) {
$return['port'] = $port;
} else {
$return['port'] = readIni($file, 'port');
}
return $return['port'] ? $return : false;
}
/**
* Reads option from ini file
* There was a problem with parse_ini function
* @return string
*/
function readIni($file, $option)
{
$data = file($file);
foreach ($data as $line) {
if (preg_match('/^' . $option . '=(.*)/', trim($line), $config)) {
return $config[1];
}
}
return null;
}
/**
* Finds pipe configuration file
*/
function getPipeConf($name)
{
$file = WORKING_DIR . "conf/pipe-$name.conf";
if (file_exists($file)) {
return "conf/pipe-$name.conf";
}
$file = WORKING_DIR . "conf/pipe-default.conf";
if (file_exists($file)) {
return "conf/pipe-default.conf";
}
return "conf/pipe.conf";
}
/**
* Starts command in background
* @param string $command
*/
function startInBackground($command)
{
shell_exec("nohup $command > /dev/null 2>&1");
}