forked from niutech/node.php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.php
More file actions
239 lines (214 loc) · 7 KB
/
Copy pathnode.php
File metadata and controls
239 lines (214 loc) · 7 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
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
<?php
/**
* Node.php v0.4
* (c) 2016 Jerzy Głowacki
* MIT License
*/
error_reporting(E_ALL);
set_time_limit(120);
define("ADMIN_MODE", true); //set to true to allow unsafe operations, set back to false when finished
define("NODE_VER", "v9.1.0");
define("NODE_ARCH", "x" . substr(php_uname("m"), -2)); //x86 or x64
define("NODE_FILE", "node-" . NODE_VER . "-linux-" . NODE_ARCH . ".tar.gz");
define("NODE_URL", "http://nodejs.org/dist/" . NODE_VER . "/" . NODE_FILE);
define("NODE_DIR", "node");
define("NODE_PORT", 49999);
function node_install() {
if(file_exists(NODE_DIR)) {
echo "Node.js is already installed.\n";
return;
}
if(!file_exists(NODE_FILE)) {
echo "Downloading Node.js from " . NODE_URL . ":\n\n";
$fp = fopen(NODE_FILE, "w");
flock($fp, LOCK_EX);
$curl = curl_init(NODE_URL);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_FILE, $fp);
$resp = curl_exec($curl);
curl_close($curl);
flock($fp, LOCK_UN);
fclose($fp);
echo $resp === true ? "Done.\n" : "Failed. Error: curl_error($curl)\n";
}
echo "Installing Node.js:\n";
passthru("tar -xzf " . NODE_FILE . " 2>&1 && mv node-" . NODE_VER . "-linux-" . NODE_ARCH . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret);
echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\nTry putting node folder via (S)FTP, so that " . __DIR__ . "/node/bin/node exists.";
}
function node_uninstall() {
if(!file_exists(NODE_DIR)) {
echo "Node.js is not yet installed.\n";
return;
}
echo "Unnstalling Node.js:\n";
passthru("rm -rfv " . NODE_DIR . " nodepid", $ret);
passthru("rm -rfv node_modules", $ret);
passthru("rm -rfv .npm", $ret);
passthru("rm -rfv nodeout", $ret);
echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";
}
function node_start($file) {
if(!file_exists(NODE_DIR)) {
echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";
return;
}
$node_pid = intval(file_get_contents("nodepid"));
if($node_pid > 0) {
echo "Node.js is already running. <a href='?stop'>Stop it</a>.\n";
return;
}
$file = escapeshellarg($file);
echo "Starting: node $file\n";
$node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!");
echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n";
file_put_contents("nodepid", $node_pid, LOCK_EX);
sleep(1); //Wait for node to spin up
echo file_get_contents("nodeout");
}
function node_stop() {
if(!file_exists(NODE_DIR)) {
echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";
return;
}
$node_pid = intval(file_get_contents("nodepid"));
if($node_pid === 0) {
echo "Node.js is not yet running.\n";
return;
}
echo "Stopping Node.js with PID=$node_pid:\n";
$ret = -1;
passthru("kill $node_pid", $ret);
echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";
file_put_contents("nodepid", '', LOCK_EX);
}
function node_npm($cmd) {
if(!file_exists(NODE_DIR)) {
echo "Node.js is not yet installed. <a href='?install'>Install it</a>.\n";
return;
}
$cmd = escapeshellcmd(NODE_DIR . "/bin/npm --cache ./.npm -- $cmd");
echo "Running: $cmd\n";
$ret = -1;
passthru($cmd, $ret);
echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret. See <a href=\"npm-debug.log\">npm-debug.log</a>\n";
}
function node_serve($path = "") {
if(!file_exists(NODE_DIR)) {
node_head();
echo "Node.js is not yet installed. Switch to Admin Mode and <a href='?install'>Install it</a>.\n";
node_foot();
return;
}
$node_pid = intval(file_get_contents("nodepid"));
if($node_pid === 0) {
node_head();
echo "Node.js is not yet running. Switch to Admin Mode and <a href='?start'>Start it</a>\n";
node_foot();
return;
}
$curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path");
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
foreach(getallheaders() as $key => $value) {
$headers[] = $key . ": " . $value;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]);
if($_SERVER["REQUEST_METHOD"] === "POST") {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($_POST));
}
$resp = curl_exec($curl);
if($resp === false) {
node_head();
echo "Error requesting $path: " . curl_error($curl);
node_foot();
} else {
list($head, $body) = explode("\r\n\r\n", $resp, 2);
$headarr = explode("\n", $head);
foreach($headarr as $headval) {
header($headval);
}
echo $body;
}
curl_close($curl);
}
function start_database() {
$db_pid = exec("../bin/mongod --dbpath ../data/db >dbout 2>&1 & echo $!");
echo $db_pid > 0 ? "Done. PID=$db_pid\n" : "Failed.\n";
file_put_contents("dbpid", $db_pid, LOCK_EX);
sleep(2); // wait for running db
echo file_get_contents("dbout");
}
function start_309() {
if(checkProcessExists("What-To-Eat")) {
echo "309 Node.js is running.\n";
echo '<p><a href="http://309.lesterlyu.com/" target="_blank">http://309.lesterlyu.com/</a></p>';
return;
}
$node309_pid = exec("./node/bin/node ../What-To-Eat/bin/www >node309out 2>&1 & echo $!");
echo '<p><a href="http://309.lesterlyu.com/" target="_blank">http://309.lesterlyu.com/</a></p>';
echo $node309_pid > 0 ? "Done. PID=$node309_pid\n" : 'Failed.\n';
file_put_contents("node309_pid", $node309_pid, LOCK_EX);
sleep(2); // wait for running npde
echo file_get_contents("node309out");
}
function stop_309() {
$node309_pid = intval(file_get_contents("node309_pid"));
if($node309_pid === 0) {
echo "Node.js is not yet running.\n";
return;
}
echo "Stopping Node.js with PID=$node309_pid:\n";
$ret = -1;
passthru("kill $node309_pid", $ret);
echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n";
file_put_contents("node309_pid", '', LOCK_EX);
}
function checkProcessExists($name) {
$exist = false;
exec("ps x | grep What | grep -v grep", $pids);
if (count($pids) > 0) {
$exists = true;
}
return $exists;
}
function node_head() {
echo '<!DOCTYPE html><html><head><title>Node.php</title><meta charset="utf-8"><body style="font-family:Helvetica,sans-serif;"><h1>Node.php</h1><pre>';
}
function node_foot() {
echo '</pre><p><a href="https://github.com/lesterlyu/node.php" target="_blank">Powered by node.php</a></p></body></html>';
}
function node_dispatch() {
if(ADMIN_MODE) {
node_head();
if(isset($_GET['install'])) {
node_install();
} elseif(isset($_GET['uninstall'])) {
node_uninstall();
} elseif(isset($_GET['start'])) {
node_start($_GET['start']);
} elseif(isset($_GET['stop'])) {
node_stop();
} elseif(isset($_GET['npm'])) {
node_npm($_GET['npm']);
} elseif(isset($_GET['database'])) {
start_database();
} elseif(isset($_GET['309'])) {
start_309();
} elseif(isset($_GET['stop309'])) {
stop_309();
} else {
echo "You are in Admin Mode. Switch back to normal mode to serve your node app.";
}
node_foot();
} else {
if(isset($_GET['path'])) {
node_serve($_GET['path']);
} else {
node_serve();
}
}
}
node_dispatch();