Skip to content

Commit 32aeb6d

Browse files
author
Ganesh B
committed
Minor changes to options for ini and cgi paths
1 parent 8663ce6 commit 32aeb6d

File tree

3 files changed

+157
-20
lines changed

3 files changed

+157
-20
lines changed

main.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ function find_file(url, php_dir, callback) {
2424
}
2525
if (file.includes(__dirname)) {
2626
fs.exists(file, function (exists) {
27-
console.log("Path join", file, exists)
27+
console.log("File path exists:", file, exists)
2828
callback(exists && file);
2929
});
3030
} else {
3131
fs.exists(path.join(__dirname, file), function (exists) {
32-
console.log("No path join", file, exists)
32+
console.log("No file path exists:", file, exists)
3333
callback(exists && file);
3434
});
3535
}
@@ -41,15 +41,24 @@ function find_file(url, php_dir, callback) {
4141
});
4242
}
4343

44-
function run_php(req, response, next, url, file, php_cgi_path) {
44+
function strOptions(options) {
45+
let optionsArray = Object.entries(options);
46+
let newArray = " ";
47+
for (let i = 0; i < optionsArray.length; i++) {
48+
newArray = newArray + (optionsArray[i][0] + " " + optionsArray[i][1] + " ");
49+
}
50+
return newArray;
51+
}
52+
53+
function run_php(req, response, next, url, file, exe_config) {
4554
var pathinfo = '';
4655
var i = req.url.indexOf('.php');
4756
if (i > 0) {
4857
pathinfo = url.pathname.substring(i + 4)
4958
} else {
5059
pathinfo = url.pathname
5160
};
52-
61+
5362
// console.log("run_php pathinfo", pathinfo)
5463
// console.log("run_php req", req)
5564

@@ -161,25 +170,30 @@ function run_php(req, response, next, url, file, php_cgi_path) {
161170

162171
if (/.*?\.php$/.test(path.join(__dirname, file))) {
163172
var res = '', err = '';
164-
165173
var php;
166-
if (!!php_cgi_path && php_cgi_path !== '') {
167-
php = child.spawn(php_cgi_path + "/php-cgi", [], {
174+
175+
if (!!exe_config.cgi_path && exe_config.cgi_path !== '') {
176+
console.log((exe_config.cgi_path.lastIndexOf("/") == exe_config.cgi_path.length - 1 ? exe_config.cgi_path : exe_config.cgi_path + "/") + "php-cgi" + strOptions(exe_config.options));
177+
178+
php = child.spawn(
179+
(exe_config.cgi_path.lastIndexOf("/") == exe_config.cgi_path.length - 1 ?
180+
exe_config.cgi_path : exe_config.cgi_path + "/") + "php-cgi",
181+
[strOptions(exe_config.options)], {
168182
env: env
169183
});
170184
} else {
171185
if (!PHP_CGI) {
172186
throw new Error('"php-cgi" cannot be found');
173187
}
174-
php = child.spawn(PHP_CGI, [], {
188+
php = child.spawn(PHP_CGI, [strOptions(exe_config)], {
175189
env: env
176190
});
177191
}
178192

179193
// php.stdin.resume();
180194
// console.log(req.rawBody);
181195
// (new Stream(req.rawBody)).pipe(php.stdin);
182-
196+
183197
php.stdin.on('error', function () {
184198
console.error("Error from server")
185199
});
@@ -201,7 +215,7 @@ function run_php(req, response, next, url, file, php_cgi_path) {
201215
console.error("error", err);
202216
});
203217
php.on('exit', function () {
204-
218+
205219
// extract headers
206220
php.stdin.end();
207221

@@ -225,9 +239,9 @@ function run_php(req, response, next, url, file, php_cgi_path) {
225239
} else {
226240
html = res;
227241
}
228-
242+
229243
// console.log('STATUS: '+response.statusCode);
230-
244+
231245
response.status(response.statusCode).send(html);
232246
response.end();
233247
});
@@ -238,15 +252,15 @@ function run_php(req, response, next, url, file, php_cgi_path) {
238252

239253
exports.cgi = function (php_root, exe_config) {
240254
return function (req, res, next) {
241-
255+
242256
// stop stream until child-process is opened
243257
req.pause();
244258
var url = URL.parse(req.url);
245259

246260
file = find_file(url, php_root, function (file) {
247261
if (file) {
248-
// console.log("find_file call", php_cgi_path, file);
249-
run_php(req, res, next, url, file, exe_config.cgi_path);
262+
// console.log("find_file call", exe_config.php_cgi_path, file);
263+
run_php(req, res, next, url, file, exe_config);
250264
} else {
251265
next();
252266
}

readme-php-options.txt

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
NAME
2+
php - PHP Command Line Interface 'CLI'
3+
4+
php-cgi - PHP Common Gateway Interface 'CGI' command
5+
6+
SYNOPSIS
7+
php [options] [ -f ] file [[--] args...]
8+
9+
php [options] -r code [[--] args...]
10+
11+
php [options] [-B code] -R code [-E code] [[--] args...]
12+
13+
php [options] [-B code] -F file [-E code] [[--] args...]
14+
15+
php [options] -- [ args...]
16+
17+
php [options] -a
18+
19+
php [options] -S addr:port [-t docroot]
20+
21+
DESCRIPTION
22+
PHP is a widely-used general-purpose scripting language that is especially suited for
23+
Web development and can be embedded into HTML. This is the command line interface that
24+
enables you to do the following:
25+
26+
You can parse and execute files by using parameter -f followed by the name of the
27+
file to be executed.
28+
29+
Using parameter -r you can directly execute PHP code simply as you would do inside
30+
a .php file when using the eval() function.
31+
32+
It is also possible to process the standard input line by line using either the parameter
33+
-R or -F. In this mode each separate input line causes the code specified by -R or the
34+
file specified by -F to be executed. You can access the input line by $argn. While
35+
processing the input lines $argi contains the number of the actual line being processed.
36+
Further more the parameters -B and -E can be used to execute code (see -r) before and
37+
after all input lines have been processed respectively. Notice that the input is read
38+
from STDIN and therefore reading from STDIN explicitly changes the next input line or
39+
skips input lines.
40+
41+
PHP also contains an embedded web server for application development purpose. By using
42+
the -S option where addr:port point to a local address and port PHP will listen to HTTP
43+
requests on that address and port and serve files from the current working directory or
44+
the docroot passed by the -t option.
45+
46+
If none of -r -f -B -R -F -E or -S is present but a single parameter is given then this
47+
parameter is taken as the filename to parse and execute (same as with -f). If no parameter
48+
is present then the standard input is read and executed.
49+
50+
OPTIONS
51+
52+
--interactive
53+
-a Run PHP interactively. This lets you enter snippets of PHP code that directly get
54+
executed. When readline support is enabled you can edit the lines and also have history support.
55+
--bindpath address:port|port
56+
-b address:port|port Bind Path for external FASTCGI Server mode (CGI only).
57+
--no-chdir
58+
-C Do not chdir to the script's directory (CGI only).
59+
--no-header
60+
-q Quiet-mode. Suppress HTTP header output (CGI only).
61+
--timing count
62+
-T count Measure execution time of script repeated count times (CGI only).
63+
--php-ini path|file
64+
-c path|file Look for php.ini file in the directory path or use the specified file
65+
--no-php-ini
66+
-n No php.ini file will be used
67+
--define foo[=bar]
68+
-d foo[=bar] Define INI entry foo with value bar
69+
-e
70+
Generate extended information for debugger/profiler
71+
--file file
72+
-f file Parse and execute file
73+
--global name
74+
-g name Make variable name global in script.
75+
--help
76+
-h This help
77+
--hide-args
78+
-H Hide script name (file) and parameters (args...) from external tools. For example
79+
you may want to use this when a php script is started as a daemon and the command line
80+
contains sensitive data such as passwords.
81+
--info
82+
-i PHP information and configuration
83+
--syntax-check
84+
-l Syntax check only (lint)
85+
--modules
86+
-m Show compiled in modules
87+
--run code
88+
-r code Run PHP code without using script tags '<?..?>'
89+
--process-begin code
90+
-B code Run PHP code before processing input lines
91+
--process-code code
92+
-R code Run PHP code for every input line
93+
--process-file file
94+
-F file Parse and execute file for every input line
95+
--process-end code
96+
-E code Run PHP code after processing all input lines
97+
--syntax-highlight
98+
-s Output HTML syntax highlighted source
99+
--server addr:port
100+
-S addr:port Start embedded Webserver on the given local address and port
101+
--docroot docroot
102+
-t docroot Specify the document root to be used by the embedded web server
103+
--version
104+
-v Version number
105+
--stripped
106+
-w Output source with stripped comments and whitespace
107+
--zend-extension file
108+
-z file Load Zend extension file
109+
110+
args...
111+
Arguments passed to script. Use '--' args when first argument starts with '-' or script is
112+
read from stdin
113+
--rfunction name
114+
--rf name Shows information about function name
115+
--rclass name
116+
--rc name Shows information about class name
117+
--rextension name
118+
--re name Shows information about extension name
119+
--rzendextension name
120+
--rz name Shows information about Zend extension name
121+
--rextinfo name
122+
--ri name Shows configuration for extension name
123+
--ini
124+
Show configuration file names

server.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
var express = require('express');
2-
var php = require("./main");
2+
var php = require("./main");
33
// var php = require("phpcgijs");
4-
var path = require("path");
4+
var path = require("path");
55

66
var app = express();
7+
var p = path.join("test/php");
78

8-
var p = path.join("test/php")
9-
10-
app.use("/", php.cgi(p, {cgi_path: '', ini_path: ''}));
9+
app.use("/", php.cgi(p, { cgi_path: '/usr/bin/', options: { "-c": "/etc/php.ini" } }));
1110
app.listen(9090, '127.0.0.1');
1211
console.log("Server listening at 9090!");

0 commit comments

Comments
 (0)