forked from sendya/shadowsocks-panel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
240 lines (222 loc) · 8.03 KB
/
index.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
#!env php
<?php
/**
* KK-Framework install
* Author: kookxiang <[email protected]>
*/
use Helper\Option;
if (php_sapi_name() != 'cli') {
exit('Sorry, This page is not available due to incorrect server configuration.');
}
// Initialize constants
define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
define('LIBRARY_PATH', ROOT_PATH . 'Library/');
define('DATA_PATH', ROOT_PATH . 'Data/');
define('TIMESTAMP', time());
@ini_set('display_errors', 'on');
@ini_set('expose_php', false);
@date_default_timezone_set('Asia/Shanghai');
@ini_set('date.timezone', 'Asia/Shanghai');
set_time_limit(0);
function command_exists($command)
{
$whereIsCommand = (PHP_OS == 'WINNT') ? 'where' : 'which';
$process = proc_open(
"$whereIsCommand $command",
array(
0 => array("pipe", "r"), //STDIN
1 => array("pipe", "w"), //STDOUT
2 => array("pipe", "w"), //STDERR
),
$pipes
);
if ($process !== false) {
$stdout = stream_get_contents($pipes[1]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
return $stdout != '';
}
return false;
}
function copyDir($strSrcDir, $strDstDir)
{
$dir = opendir($strSrcDir);
if (!$dir) {
return false;
}
if (!is_dir($strDstDir)) {
if (!mkdir($strDstDir)) {
return false;
}
}
while (false !== ($file = readdir($dir))) {
if (($file != '.') && ($file != '..')) {
if (is_dir($strSrcDir . '/' . $file)) {
if (!copydir($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
return false;
}
} else {
if (!copy($strSrcDir . '/' . $file, $strDstDir . '/' . $file)) {
return false;
}
}
}
}
closedir($dir);
return true;
}
function delDir($dir) {
//先删除目录下的文件:
$dh=@opendir($dir);
while ($file=@readdir($dh)) {
if($file!="." && $file!="..") {
$fullpath=$dir."/".$file;
if(!is_dir($fullpath)) {
@unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
@closedir($dh);
//删除当前文件夹:
if(@rmdir($dir)) {
return true;
} else {
return false;
}
}
function colorize($text, $status) {
$out = "";
switch($status) {
case "SUCCESS":
$out = "[44m"; //Blue fonts
break;
case "FAILURE":
$out = "[1;137;41m"; //Red background
break;
case "WARNING":
$out = "[1;37;31m"; //Red fonts
break;
case "NOTE":
$out = "[1;134;34m"; //Blue background
break;
default:
throw new Exception("Invalid status: " . $status);
}
return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}
function download_composer($url) {
echo 'Downloading composer...';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$binary = curl_exec($ch);
if (curl_errno($ch)) {
echo colorize('FAILED!' . PHP_EOL . curl_error($ch), 'FAILURE');
curl_close($ch);
return false;
}
$fp = fopen(ROOT_PATH . 'composer.phar', 'wb');
fputs($fp, $binary);
fclose($fp);
curl_close($ch);
echo 'Done!' . PHP_EOL;
}
function print_arr($arr) {
foreach($arr as $ret) {
echo $ret . PHP_EOL;
}
}
switch ($argv[1]) {
case 'install':
if (!file_exists(ROOT_PATH . 'composer.phar')) {
if (download_composer("http://getcomposer.org/composer.phar") === false) {
download_composer("http://static.loacg.com/soft/composer.phar");
}
}
exec(PHP_BINARY . ' ' . ROOT_PATH . 'composer.phar -V', $return_arr);
print_arr($return_arr);
if(!file_exists(ROOT_PATH . 'composer.phar') || stripos($return_arr[count($return_arr)-1], 'Composer') === false) {
@unlink(ROOT_PATH . 'composer.phar');
echo colorize('Failed to download composer binary!', 'FAILURE') . PHP_EOL;
break;
}
unset($return_arr);
echo 'Now installing dependencies...' . PHP_EOL;
if(!function_exists('system') || !function_exists('exec')) {
echo colorize('FAILED! system() or exec() function is disabled!', 'FAILURE') . PHP_EOL;
echo 'Please run command: ' . colorize('php -d disable_functions=\'\' index.php install', 'FAILURE') . PHP_EOL;
break;
}
system(PHP_BINARY . ' ' . ROOT_PATH . 'composer.phar install');
if (!file_exists(ROOT_PATH . 'Package/autoload.php')) {
echo colorize('It seems composer failed to install package', 'FAILURE') . PHP_EOL;
break;
}
echo 'Now reloading packages and config...'. PHP_EOL;
$configFile = DATA_PATH . 'Config.php';
if (!file_exists($configFile)) {
echo 'Config Unknown... copying..' . PHP_EOL;
copy(DATA_PATH . 'Config.simple.php', $configFile);
echo colorize('Please modify ./Data/Config.php and try again', 'WARNING') . PHP_EOL;
break;
}
@include ROOT_PATH . 'Package/autoload.php';
try {
@include DATA_PATH . 'Config.php';
} catch (PDOException $e) {
echo colorize('Database not available! Please modify ./Data/Config.php and try again', 'WARNING') . PHP_EOL;
break;
}
if (Option::getConfig('ENCRYPT_KEY') == 'Please generate key and paste here') {
Option::setConfig('ENCRYPT_KEY', Option::createKey());
}
if (Option::getConfig('COOKIE_KEY') === null) {
$str = file_get_contents(DATA_PATH . 'Config.php');
preg_match("/define\\('ENCRYPT_KEY', '(.*)'\\);/", $str, $res);
if (count($res) >= 1) {
$str2 = preg_replace("/define\\('ENCRYPT_KEY', '(.*)'\\);/",
$res[0] . PHP_EOL . "define('COOKIE_KEY', '" . Option::createKey() . "');", $str);
}
file_put_contents(DATA_PATH . 'Config.php', $str2);
} elseif (Option::getConfig('COOKIE_KEY') == 'Please generate key and paste here' || Option::getConfig('COOKIE_KEY') == '') {
Option::setConfig('COOKIE_KEY', Option::createKey());
}
echo 'Done!' . PHP_EOL;
echo 'Now migrating database...' . PHP_EOL;
if (PATH_SEPARATOR != ':') {
$phinxCommand = ROOT_PATH . 'Package\bin\phinx.bat';
} else {
$phinxCommand = PHP_BINARY . ' ' . ROOT_PATH . 'Package/robmorgan/phinx/bin/phinx';
}
exec($phinxCommand . ' migrate', $return_arr, $return_arr2);
print_arr($return_arr);
if(stripos($return_arr[count($return_arr)-1], 'All Done.') === false) {
echo colorize(PHP_EOL. PHP_EOL.'Failed to migrate database, you can try it manually: ', 'WARNING') . colorize('./Package/bin/phinx migrate', 'WARNING') . PHP_EOL;
// rollback
exec($phinxCommand . ' rollback', $return_arr, $return_arr2);
break;
}
echo 'Now installing resources...' . PHP_EOL;
echo 'Deleting old resources... ' . PHP_EOL;
echo delDir(ROOT_PATH . 'Public/Resource') ? 'Done.' . PHP_EOL : 'old resources not exist.' . PHP_EOL;
echo 'Copying resources...' . PHP_EOL;
copyDir(ROOT_PATH . 'Resource', ROOT_PATH . 'Public/Resource');
echo colorize('All done~ Cheers! open site: '.BASE_URL . 'yourdomain.com/', 'NOTE') . PHP_EOL;
break;
case 'import-sspanel':
// TODO: 从 ss-panel 导入用户数据
break;
case 'test':
echo colorize('FAILED!' . PHP_EOL, 'SUCCESS');
echo colorize('FAILED!' . PHP_EOL, 'FAILURE');
echo colorize('FAILED!' . PHP_EOL, 'WARNING');
echo colorize('FAILED!' . PHP_EOL, 'NOTE');
break;
default:
echo 'Unknown command';
}
echo PHP_EOL;