From 6c2aaa45acac5a55a41159d540a5795c8e35ec65 Mon Sep 17 00:00:00 2001 From: Douglas Harrington Muhone Date: Thu, 5 Jul 2018 10:07:40 +0800 Subject: [PATCH 01/10] bumped node version to v10.6.0 --- node.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/node.php b/node.php index b725b9f..5bf54ff 100644 --- a/node.php +++ b/node.php @@ -12,7 +12,7 @@ define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished -define("NODE_VER", "v9.1.0"); +define("NODE_VER", "v10.6.0"); define("NODE_ARCH", "x" . substr(php_uname("m"), -2)); //x86 or x64 From c5f6c91d58e0202fe3337a488a1b538c26f76e24 Mon Sep 17 00:00:00 2001 From: techno-express Date: Wed, 26 Sep 2018 10:10:35 -0400 Subject: [PATCH 02/10] Create index.php - merged from https://github.com/webdev-kiwi/node.php --- index.php | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 index.php diff --git a/index.php b/index.php new file mode 100644 index 0000000..11c754f --- /dev/null +++ b/index.php @@ -0,0 +1,78 @@ + $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) { + + header('HTTP/1.0 404 Not Found'); + exit; + + } 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 node_dispatch() { + isset($_GET['path']) ? node_serve($_GET['path']) : node_serve(); +} + +node_dispatch(); From 520d66156d18a3878a8c0cb27a6dd1f84485454e Mon Sep 17 00:00:00 2001 From: techno-express Date: Wed, 26 Sep 2018 10:24:01 -0400 Subject: [PATCH 03/10] add json put/delete support - merged from https://github.com/weixingsun/node.php --- node.php | 70 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 15 deletions(-) diff --git a/node.php b/node.php index 5bf54ff..32fcebb 100644 --- a/node.php +++ b/node.php @@ -3,26 +3,42 @@ /** * Node.php v0.4 * (c) 2016 Jerzy Głowacki + * 2016/7/21 Add getallheaders() for v5.3 * MIT License */ -error_reporting(E_ALL); - -set_time_limit(120); - +//define("ADMIN_MODE", true); define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished +error_reporting(E_ALL); +set_time_limit(120); define("NODE_VER", "v10.6.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); +//change ADMIN=true +//wget http://download.redis.io/releases/redis-3.2.1.tar.gz && tar zxf redis-3.2.1.tar.gz && cd redis-3.2.1 && make && src/redis-server #start redis on 127.0.0.1:6379 +//git clone https://github.com/weixingsun/docker-redis.git && $HOST/service/node.php?start=docker-redis/src/main.js #start nodejs server +//change ADMIN=false +//wget $HOST/service/node.php?path=api/msg/car:1,2:3 + +if (!function_exists('getallheaders')) +{ + function getallheaders() + { + $headers = ''; + foreach ($_SERVER as $name => $value) + { + if (substr($name, 0, 5) == 'HTTP_') + { + $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; + } + } + return $headers; + } +} function node_install() { if(file_exists(NODE_DIR)) { @@ -122,7 +138,11 @@ function node_serve($path = "") { node_foot(); return; } - $curl = curl_init("http://127.0.0.1:" . NODE_PORT . "/$path"); + $url = "http://127.0.0.1:" . NODE_PORT . "/$path"; + //header('HTTP/1.1 307 Temporary Redirect'); + //header("Location: $url"); + + $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $headers = array(); @@ -132,9 +152,25 @@ function node_serve($path = "") { 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)); - } + curl_setopt($curl, CURLOPT_POST, 1); + if (count($_POST)==0) { //strlen($str_json_params) > 0) && isValidJSON($json_params)) { + $str_json_params = file_get_contents('php://input'); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($curl, CURLOPT_POSTFIELDS, $str_json_params); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + }else{ + //$str_header = implode(",", $headers); + $fields = http_build_query($_POST); + curl_setopt($curl, CURLOPT_POSTFIELDS, $fields); + //error_log("post json=$str_json_params "); + } + } else if($_SERVER["REQUEST_METHOD"] === "PUT" || $_SERVER["REQUEST_METHOD"] === "DELETE"){ + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $_SERVER["REQUEST_METHOD"]); + curl_setopt($curl, CURLOPT_POSTFIELDS, file_get_contents('php://input')); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + } + //error_log("url=$url"); $resp = curl_exec($curl); if($resp === false) { node_head(); @@ -177,11 +213,15 @@ function node_dispatch() { } node_foot(); } else { - if(isset($_GET['path'])) { + $full_url = $_SERVER['REQUEST_URI']; + $path = explode("?path=",$full_url); + //error_log("path=$path[1]"); + node_serve($path[1]); + /*if(isset($_GET['path'])) { node_serve($_GET['path']); } else { node_serve(); - } + }*/ } } From 120bd1cd6f943843703bde388c938368878e575e Mon Sep 17 00:00:00 2001 From: techno-express Date: Wed, 26 Sep 2018 10:37:39 -0400 Subject: [PATCH 04/10] Added a restart for hosting that kills user processes - merged from https://github.com/niutech/node.php/pull/14 --- node.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/node.php b/node.php index 32fcebb..ff099ac 100644 --- a/node.php +++ b/node.php @@ -10,6 +10,9 @@ //define("ADMIN_MODE", true); define("ADMIN_MODE", false); //set to true to allow unsafe operations, set back to false when finished +//set to true for shared hosts that periodically kill user's processes (such as node) +define("RESTART_PROCESS", false); + error_reporting(E_ALL); set_time_limit(120); define("NODE_VER", "v10.6.0"); @@ -90,7 +93,10 @@ function node_start($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); + file_put_contents("nodepid", $node_pid, LOCK_EX); + if($node_pid>0){ + file_put_contents('nodestart', $file, LOCK_EX); + } sleep(1); //Wait for node to spin up echo file_get_contents("nodeout"); } @@ -130,6 +136,17 @@ function node_serve($path = "") { echo "Node.js is not yet installed. Switch to Admin Mode and Install it.\n"; node_foot(); return; + } elseif ($RESTART_PROCESS && $node_pid && !posix_getpgid($node_pid)) { + $nodestart = file_get_contents('nodestart'); + if($nodestart){ + node_start($nodestart); + //wait for node process to start, then retry to node_serve + sleep(5); + node_serve($path); + return; + } + echo "Please switch to Admin Mode and manually restart the server. Start it\n"; + return; } $node_pid = intval(file_get_contents("nodepid")); if($node_pid === 0) { From cb8b56292130984e5ed0f10b23f879fff7e79d6f Mon Sep 17 00:00:00 2001 From: techno-express Date: Wed, 26 Sep 2018 12:22:26 -0400 Subject: [PATCH 05/10] convert to allow script to be run cli mode -- php node.php --admin yourcommand --- node.php | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/node.php b/node.php index ff099ac..e946143 100644 --- a/node.php +++ b/node.php @@ -213,32 +213,37 @@ function node_foot() { } function node_dispatch() { - if(ADMIN_MODE) { + $checkScript = (realpath($_SERVER['argv'][0]) == __FILE__); + $checkArgs = isset($_SERVER['argv'][1]); + $checkAdmin = ($checkScript && $checkArgs) && ($_SERVER['argv'][1] == '--admin'); + $getCommand = (($checkScript && $checkArgs) && !$checkAdmin) ? $_SERVER['argv'][1] : (($checkAdmin) ? $_SERVER['argv'][2] : ''); + if(ADMIN_MODE || $checkAdmin) { node_head(); - if(isset($_GET['install'])) { + if (isset($_GET['install']) || ($getCommand == 'install')){ node_install(); - } elseif(isset($_GET['uninstall'])) { + } elseif (isset($_GET['uninstall']) || ($getCommand == 'uninstall')) { node_uninstall(); - } elseif(isset($_GET['start'])) { + } elseif (isset($_GET['start']) || ($getCommand == 'start')) { node_start($_GET['start']); - } elseif(isset($_GET['stop'])) { + } elseif (isset($_GET['stop']) || ($getCommand == 'stop')) { node_stop(); - } elseif(isset($_GET['npm'])) { - node_npm($_GET['npm']); + } elseif (isset($_GET['npm']) || ($getCommand == 'npm')) { + if (empty($getCommand)) + node_npm($_GET['npm']); + else { + $getCommand = $_SERVER['argv']; + unset($getCommand[0]); + unset($getCommand[1]); + node_npm($getCommand); + } } else { echo "You are in Admin Mode. Switch back to normal mode to serve your node app."; } node_foot(); - } else { + } elseif (isset($_SERVER['REQUEST_URI'])) { $full_url = $_SERVER['REQUEST_URI']; - $path = explode("?path=",$full_url); - //error_log("path=$path[1]"); + $path = explode("?path=",$full_url); node_serve($path[1]); - /*if(isset($_GET['path'])) { - node_serve($_GET['path']); - } else { - node_serve(); - }*/ } } From b0968ee13d7031c1888cfc90e769f2473b15ff1f Mon Sep 17 00:00:00 2001 From: techno-express Date: Wed, 26 Sep 2018 13:43:30 -0400 Subject: [PATCH 06/10] update cli to run handle optional path if passed -- php node.php your/path/to/script.js --- node.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/node.php b/node.php index e946143..ba810da 100644 --- a/node.php +++ b/node.php @@ -244,6 +244,8 @@ function node_dispatch() { $full_url = $_SERVER['REQUEST_URI']; $path = explode("?path=",$full_url); node_serve($path[1]); + } elseif (isset($getCommand)) { + node_serve($getCommand); } } From 3afc0e7830b3290c3ef8b839b1ac71443f4f6c81 Mon Sep 17 00:00:00 2001 From: Lawrence Date: Sat, 29 Sep 2018 13:21:25 -0400 Subject: [PATCH 07/10] added routines to use built-in functions for ectraction, script now usable under windows and mac os, will download for any platform run on, bump version up --- node.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 95 insertions(+), 15 deletions(-) diff --git a/node.php b/node.php index ba810da..da8bf40 100644 --- a/node.php +++ b/node.php @@ -1,7 +1,7 @@ &1 && mv node-" . NODE_VER . "-linux-" . NODE_ARCH . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret); + if (NODE_OS == '-win-') + { + $zip = new ZipArchive; + $ret = $zip->open(NODE_FILE); + if ($ret === TRUE) { + $zip->extractTo('.'); + $zip->close(); + } + } else { + // decompress from gz + $p = new PharData(NODE_FILE); + $p->decompress(); // creates *.tar + + // unarchive from the tar + $archive = new PharData(str_replace(NODE_FILE, 'node-v10.tar', NODE_FILE)); + $archive->extractTo('.'); + unlink('node-v10.tar'); + + //passthru("tar -xzf " . NODE_FILE . " 2>&1 && mv " . NODE_FOLDER . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret); + } + if (recurse_copy(NODE_FOLDER, NODE_DIR)) + $ret = file_put_contents('nodepid', null); + else + $ret = 'Extracting'; + recurse_delete(NODE_FOLDER); echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\nTry putting node folder via (S)FTP, so that " . __DIR__ . "/node/bin/node exists."; } @@ -72,11 +149,14 @@ function node_uninstall() { 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"; + + $ret = recurse_delete(NODE_DIR); + unlink('nodepid'); + //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) ? "Done.\n" : "Failed. Error: $ret\n"; } function node_start($file) { From ea99440f928a1f384564fa5b4c07e1952d416e34 Mon Sep 17 00:00:00 2001 From: techno-express Date: Sun, 30 Sep 2018 08:28:07 -0400 Subject: [PATCH 08/10] added routine to run under windows, clean up code make more psr-2 --- node.php | 73 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/node.php b/node.php index da8bf40..8a38445 100644 --- a/node.php +++ b/node.php @@ -56,7 +56,8 @@ function getallheaders() } } -function recurse_copy($src, $dst) { +function recurse_copy($src, $dst) +{ if ( ! is_dir($src) ) return false; $dir = opendir($src); @@ -64,10 +65,10 @@ function recurse_copy($src, $dst) { while(false !== ( $file = readdir($dir)) ) { if (( $file != '.' ) && ( $file != '..' )) { if ( is_dir($src . _DS . $file) ) { - recurse_copy($src . _DS . $file,$dst . _DS . $file); + recurse_copy($src._DS.$file, $dst._DS.$file); } else { - copy($src . _DS . $file,$dst . _DS . $file); + copy($src._DS.$file, $dst._DS.$file); } } } @@ -75,13 +76,14 @@ function recurse_copy($src, $dst) { return true; } -function recurse_delete($directory, $options = array()) { +function recurse_delete($directory, $options = array()) +{ if(!isset($options['traverseSymlinks'])) - $options['traverseSymlinks']=false; - $files = array_diff(scandir($directory), array('.','..')); + $options['traverseSymlinks'] = false; + $files = array_diff(scandir($directory), array('.', '..')); foreach ($files as $file) { - $dirfile = $directory. _DS .$file; + $dirfile = $directory._DS.$file; if (is_dir($dirfile)) { if(!$options['traverseSymlinks'] && is_link(rtrim($file, _DS))) { @@ -96,13 +98,14 @@ function recurse_delete($directory, $options = array()) { return rmdir($directory); } -function node_install() { +function node_install() +{ if(file_exists(NODE_DIR)) { - echo "Node.js is already installed.\n"; + echo "Node.js is already installed.
\n"; return; } if(!file_exists(NODE_FILE)) { - echo "Downloading Node.js from " . NODE_URL . ":\n\n"; + echo "Downloading Node.js from " . NODE_URL . ":
\n\n"; $fp = fopen(NODE_FILE, "w"); flock($fp, LOCK_EX); $curl = curl_init(NODE_URL); @@ -112,9 +115,9 @@ function node_install() { curl_close($curl); flock($fp, LOCK_UN); fclose($fp); - echo $resp === true ? "Done.\n" : "Failed. Error: curl_error($curl)\n"; + echo $resp === true ? "Done.
\n" : "Failed. Error: curl_error($curl)
\n"; } - echo "Installing Node.js:\n"; + echo "\n
Installing Node.js:
\n"; if (NODE_OS == '-win-') { $zip = new ZipArchive; @@ -136,14 +139,13 @@ function node_install() { //passthru("tar -xzf " . NODE_FILE . " 2>&1 && mv " . NODE_FOLDER . " " . NODE_DIR . " && touch nodepid && rm -f " . NODE_FILE, $ret); } if (recurse_copy(NODE_FOLDER, NODE_DIR)) - $ret = file_put_contents('nodepid', null); - else - $ret = 'Extracting'; + $ret = touch('nodepid'); recurse_delete(NODE_FOLDER); - echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\nTry putting node folder via (S)FTP, so that " . __DIR__ . "/node/bin/node exists."; + echo (!$ret) ? "Done.\n" : "Failed. Error: $ret\nTry putting node folder via (S)FTP, so that " . __DIR__ . "/node/bin/node exists."; } -function node_uninstall() { +function node_uninstall() +{ if(!file_exists(NODE_DIR)) { echo "Node.js is not yet installed.\n"; return; @@ -159,7 +161,8 @@ function node_uninstall() { echo ($ret) ? "Done.\n" : "Failed. Error: $ret\n"; } -function node_start($file) { +function node_start($file) +{ if(!file_exists(NODE_DIR)) { echo "Node.js is not yet installed. Install it.\n"; return; @@ -171,7 +174,14 @@ function node_start($file) { } $file = escapeshellarg($file); echo "Starting: node $file\n"; - $node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!"); + + if (NODE_OS == '-win-') + { + $node_pid = pclose(popen("start /B PORT=" . NODE_PORT . " " . NODE_DIR . "/node ". $file, "r")); + } else { + $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); if($node_pid>0){ @@ -181,7 +191,13 @@ function node_start($file) { echo file_get_contents("nodeout"); } -function node_stop() { +function kill($pid) +{ + return (NODE_OS == '-win-') ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid"); +} + +function node_stop() +{ if(!file_exists(NODE_DIR)) { echo "Node.js is not yet installed. Install it.\n"; return; @@ -193,12 +209,13 @@ function node_stop() { } echo "Stopping Node.js with PID=$node_pid:\n"; $ret = -1; - passthru("kill $node_pid", $ret); + $ret = kill($node_pid); echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret\n"; file_put_contents("nodepid", '', LOCK_EX); } -function node_npm($cmd) { +function node_npm($cmd) +{ if(!file_exists(NODE_DIR)) { echo "Node.js is not yet installed. Install it.\n"; return; @@ -210,7 +227,8 @@ function node_npm($cmd) { echo $ret === 0 ? "Done.\n" : "Failed. Error: $ret. See npm-debug.log\n"; } -function node_serve($path = "") { +function node_serve($path = "") +{ if(!file_exists(NODE_DIR)) { node_head(); echo "Node.js is not yet installed. Switch to Admin Mode and Install it.\n"; @@ -284,15 +302,18 @@ function node_serve($path = "") { curl_close($curl); } -function node_head() { +function node_head() +{ echo 'Node.php

Node.php

';
 }
 
-function node_foot() {
+function node_foot() 
+{
 	echo '

Powered by node.php

'; } -function node_dispatch() { +function node_dispatch() +{ $checkScript = (realpath($_SERVER['argv'][0]) == __FILE__); $checkArgs = isset($_SERVER['argv'][1]); $checkAdmin = ($checkScript && $checkArgs) && ($_SERVER['argv'][1] == '--admin'); From b3e76c31ccbdae381f0190f1db6310dbf85827fb Mon Sep 17 00:00:00 2001 From: Lawrence Date: Sun, 30 Sep 2018 12:45:45 -0400 Subject: [PATCH 09/10] updated prpoer windows background startup routine, use buildin process handleing routine for linux and others --- node.php | 61 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/node.php b/node.php index 8a38445..f74708c 100644 --- a/node.php +++ b/node.php @@ -154,10 +154,6 @@ function node_uninstall() $ret = recurse_delete(NODE_DIR); unlink('nodepid'); - //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) ? "Done.\n" : "Failed. Error: $ret\n"; } @@ -175,20 +171,14 @@ function node_start($file) $file = escapeshellarg($file); echo "Starting: node $file\n"; - if (NODE_OS == '-win-') - { - $node_pid = pclose(popen("start /B PORT=" . NODE_PORT . " " . NODE_DIR . "/node ". $file, "r")); - } else { - $node_pid = exec("PORT=" . NODE_PORT . " " . NODE_DIR . "/bin/node $file >nodeout 2>&1 & echo $!"); - } - + $node_pid = run($file, NODE_DIR, ['PORT' => NODE_PORT]); echo $node_pid > 0 ? "Done. PID=$node_pid\n" : "Failed.\n"; + file_put_contents("nodepid", $node_pid, LOCK_EX); - if($node_pid>0){ + if ($node_pid>0) { file_put_contents('nodestart', $file, LOCK_EX); } sleep(1); //Wait for node to spin up - echo file_get_contents("nodeout"); } function kill($pid) @@ -196,6 +186,51 @@ function kill($pid) return (NODE_OS == '-win-') ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid"); } +function run($cmd, $startDir = null, $env = array()) +{ + if(NODE_OS == '-win-') { + $descriptorspec = array ( + 0 => array("pipe", "r"), + 1 => array("pipe", "w"), + ); + + //proc_open — Execute a command + //'start /b' runs command in the background + if ( is_resource( $prog = proc_open('start /b node '.$cmd, $descriptorspec, $pipes, $startDir, $env) ) ) { + //Get Parent process Id + $ppid = proc_get_status($prog); + $pid = $ppid['pid']; + } else { + echo("Failed to execute!"); + exit(); + } + + $output = array_filter(explode(" ", shell_exec("wmic process get parentprocessid,processid | find \"$pid\""))); + array_pop($output); + + $pid = end($output); + } else { + $descriptorspec = array ( + 0 => array("pipe", "r"), + 1 => array("pipe", "w"), + ); + + //proc_open — Execute a command + //'nohup' command line-utility will allow you to run command/process or shell script that can continue running in the background + if (is_resource($prog = proc_open('nohup bin'._DS.'node '.$cmd, $descriptorspec, $pipes, $startDir, $env) ) ) { + //Get Parent process Id + $ppid = proc_get_status($prog); + $pid = $ppid['pid']; + + $pid = $pid + 1; + } else { + echo("Failed to execute!"); + exit(); + } + } + return $pid; +} + function node_stop() { if(!file_exists(NODE_DIR)) { From ad5df849c85c35df57beb07b70736c95f6fcfba7 Mon Sep 17 00:00:00 2001 From: Lawrence Date: Sun, 30 Sep 2018 18:28:51 -0400 Subject: [PATCH 10/10] update --- node.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/node.php b/node.php index f74708c..e43dfef 100644 --- a/node.php +++ b/node.php @@ -37,7 +37,7 @@ function getDistro() define("NODE_FILE", "node-" . NODE_VER . NODE_OS . NODE_ARCH . $OSDistro[1]); define("NODE_FOLDER", "node-" . NODE_VER . NODE_OS . NODE_ARCH); define("NODE_URL", "http://nodejs.org/dist/" . NODE_VER . "/" . NODE_FILE); -define("NODE_DIR", "node"); +define("NODE_DIR", __DIR__. _DS. "node"); define("NODE_PORT", 49999); if (!function_exists('getallheaders')) @@ -264,7 +264,7 @@ function node_npm($cmd) function node_serve($path = "") { - if(!file_exists(NODE_DIR)) { + if (!file_exists(NODE_DIR)) { node_head(); echo "Node.js is not yet installed. Switch to Admin Mode and Install it.\n"; node_foot(); @@ -281,6 +281,7 @@ function node_serve($path = "") echo "Please switch to Admin Mode and manually restart the server. Start it\n"; return; } + $node_pid = intval(file_get_contents("nodepid")); if($node_pid === 0) { node_head();