Skip to content

Commit 2b7c82c

Browse files
committed
added: a bunch of PHP related CGI files
1 parent 314dea5 commit 2b7c82c

File tree

13 files changed

+346
-7
lines changed

13 files changed

+346
-7
lines changed

config.h

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,61 @@
8282
** If this many are already running, then attempts to run more will
8383
** return an HTTP 503 error. If this is not defined then there's
8484
** no limit (and you'd better have a lot of memory). This can also be
85-
** set in the runtime config file.
85+
** set in the runtime config file. Default 50 is limited for RPi.
8686
*/
87-
#ifdef notdef
8887
#define CGI_LIMIT 50
88+
89+
/* CONFIGURE: PHP scripts must match this pattern to get executed. It's
90+
** a simple shell-style wildcard pattern, with * meaning any string not
91+
** containing a slash, ** meaning any string at all, and ? meaning any
92+
** single character; or multiple such patterns separated by |. The
93+
** patterns get checked against the filename part of the incoming URL.
94+
**
95+
** Restricting PHP scripts to a single directory lets the site administrator
96+
** review them for security holes, and is strongly recommended. If there
97+
** are individual users that you trust, you can enable their directories too.
98+
**
99+
** You can also specify a PHP pattern on the command line, with the -a flag.
100+
** Such a pattern overrides this compiled-in default.
101+
**
102+
** If no PHP pattern is specified, neither here nor on the command line,
103+
** then PHP scripts cannot be run at all. If you want to disable PHP
104+
** as a security measure that's how you do it, just don't define any
105+
** pattern here and don't run with the -a flag.
106+
**
107+
** By using multiple CGI paterns and CGI PHP programs you can run multiple
108+
** versions of PHP, however using the PHP pattern will only execute the
109+
** first 'php-cgi' in $PATH. A combination is also possible, and for testing
110+
** or speed comparison purposes, it is possible to run v5.0.3 & 5.6.33
111+
*/
112+
#ifdef notdef
113+
/* Some sample patterns. Allow scripts only in one central directory: */
114+
#define PHP_PATTERN "/wordpress/*"
115+
/* Allow scriptss in a central directory, or anywhere in a trusted
116+
** user's tree: */
117+
#define PHP_PATTERN "/testing/**.php|/jef/**.php4"
118+
/* Allow any PHP script anywhere ending with a .php: */
119+
#define PHP_PATTERN "**.php"
120+
/* When virtual hosting, enable the central directory on every host: */
121+
#define PHP_PATTERN "/*/php5/**.php5"
89122
#endif
90123
124+
/* CONFIGURE: How many seconds to allow PHP programs to run before killing
125+
** them. This is in case someone writes a PHP program that goes into an
126+
** infinite loop, or does a massive database lookup that would take hours,
127+
** or whatever. If you don't want any limit, comment this out, but that's
128+
** probably a really bad idea. Default uses standard PHP timeout (60 sec).
129+
*/
130+
#define PHP_TIMELIMIT 60
131+
132+
/* CONFIGURE: Maximum number of simultaneous PHP programs allowed.
133+
** If this many are already running, then attempts to run more will
134+
** return an HTTP 503 error. If this is not defined then there's
135+
** no limit (and you'd better have a lot of memory). This can also be
136+
** set in the runtime config file. Default 10 is limited for RPi.
137+
*/
138+
#define PHP_LIMIT 10
139+
91140
/* CONFIGURE: How many seconds to allow for reading the initial request
92141
** on a new connection.
93142
*/

php-cgi/mthttpd.php.conf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## usage: sudo mthttpd -u _user_ -C ~/www/thttpd.php.conf
2+
## note: /home/_user_/www/cgi-bin/php -=> /usr/bin/php-cgi
3+
## replace _user_ with your username
4+
dir=/home/_user_/www
5+
#phpspat=**.php
6+
cgipat=/php3|**.php3|/php4|**.php4|/php5|**.php5|/php6|**.php6|/php7|**.php7|/php503|**.php
7+
logfile=/home/_user_/tmp/mthttpd.php.log
8+
nochroot
9+
port=80
10+

php-cgi/multi-php.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
In mthttpd it is relatively easy to run PHP from '/usr/bin/php-cgi' with the included script 'mthttpd/www/cgi-bin/php.cgi'. It will even work with thttpd and sthttpd. NOT is uses CGI "path overloading".
2+
3+
But what if you develop, test, compare or backport across different versions of PHP. What if you are reviewing or doing time trials.
4+
5+
If any of the CGI programs from 'mthttpd/php-cgi/*' are placed in the root of the webserver, and a CGI pattern is applied, you can run any number of PHP versions, including sub-versions. As long as all the related binaries and libraries are seperated and locatable from the command line, they all can be used from mthttpd at the same time.
6+
7+
The PHP pattern with execute the first 'php-cgi' in the $PATH. The crucial part for multiple PHP versoins is the CGI pattern.
8+
9+
mthttpd -c "/php3|**.php3|/php4|**.php4|/php5|**.php5|/php6|**.php6|/php7|**.php7|/php503|**.php" -d /home/_user_/test/www
10+
11+
NOTE: in order for this to work in a secure way, the PHP script must be marked as executable on the filesystem, becuase you will use CGI "path overloading" and it will check that the actual "script" to be executed has the "executable" bit set for that file, only then will it allow the CGI program to execute that script.

php-cgi/php3

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php3-cgi
3+
# eg. http:/localhost/php3/test/index.php
4+
# == php3-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php4" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php3-cgi
34+
fi

php-cgi/php4

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php4-cgi
3+
# eg. http:/localhost/php4/test/index.php
4+
# == php4-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php4" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php4-cgi
34+
fi

php-cgi/php5

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php5-cgi
3+
# eg. http:/localhost/php5/test/index.php
4+
# == php5-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php5" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php5-cgi
34+
fi

php-cgi/php503

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php5-cgi v5.0.3
3+
# eg. http:/localhost/php503/test/index.php
4+
# == php503-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php5" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php503-cgi
34+
fi

php-cgi/php6

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php6-cgi
3+
# eg. http:/localhost/php6/test/index.php
4+
# == php6-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php6" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php6-cgi
34+
fi

php-cgi/php7

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/sh
2+
# "special" cgi->php redirect for thttpd & php7-cgi
3+
# eg. http:/localhost/php7/test/index.php
4+
# == php7-cgi /home/sysop/devel/www/test/index.php
5+
6+
if [ "$PHP_AUTH_USER" = "" ]; then
7+
export PHP_AUTH_USER="admin" ## FIXME:
8+
fi
9+
if [ "$PHP_AUTH_PW" = "" ]; then
10+
export PHP_AUTH_PW="admin" ## FIXME:
11+
fi
12+
13+
if [ "$DOCUMENT_ROOT" = "" ]; then
14+
export DOCUMENT_ROOT=/usr/local/www
15+
fi
16+
17+
if [ "$PATH_TRANSLATED" = "" ]; then
18+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.php
19+
fi
20+
21+
if [ "$SCRIPT_FILENAME" = "" ]; then
22+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
23+
fi
24+
25+
EXT3=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
26+
EXT4=${PATH_TRANSLATED#"${PATH_TRANSLATED%?????}"}
27+
if [ ! "$EXT3" = ".php" -a ! "$EXT4"= ".php7" ]; then
28+
printf "Content-Type: "
29+
file -b -i ${PATH_TRANSLATED}
30+
echo
31+
cat ${PATH_TRANSLATED}
32+
else
33+
exec /usr/bin/php7-cgi
34+
fi

php.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The following works with thttpd, sthttpd and mthttpd and PHP CGI as FastCGI, which is plenty fast enough for even heavy load on a RPi, if all it is doing is serving web pages (1Gb restriction).
2+
3+
ATM the only way to get PHP functioning properly, in such a way that the same code can be used unmodified with Apache or another PHP SAPI web server, is by using CGI "path overloading" and a PHP bounce script that presets a few missing variables, most notably SCRIPT_FILENAME, which although not part of any CGI standard, makes PHP unuable with most CGI web servers (including thttpd) if not set.
4+
5+
This PHP bounce script must also preparse PATH_TRANSLATED when its empty, replacing it with "DOCUMENT_ROOT/index.php", so that SCRIPT_FILENAME gets the correct value (and PHP knows what the hell is going on).
6+
7+
thttpd is a strict CGI/1.1 web server, so it also does not pass along PHP_AUTH_USER or PHP_AUTH_PW. If you are having issues getting this to work with PHP Basic Authentication, you will probably need to provide thttp a .htpasssd which can be generated with the 'htpasswd' tool.
8+
9+
You can use 'mthttpd/www/cgi-bin/php.cgi' where the 'www' would be the web server root, and a test url would be:
10+
http://127.0.0.1/cgi-bin/php.cgi/test.php
11+
12+
Note that 'mthttpd/www/test.php' must have executable writes for the same _user_ as the '-u _user' thttpd was started with. This just simplifies security for thttpd, while placing responsibility for who can execute what back on the webserver developer, operator or server admin. The default mthttpd user group is 'www-data' to simplify Apache & NgenX transitions.
13+
14+
CGI "path overloading" simply means "everything in the url after the cgi name is a path from the root of the webserver". With the above example test url, the default thttpd filesystem translation (presented as SCRIPT_FILENAME in the bounce script) would be '/usr/local/www/test.php', and the CGI program would be at '/usr/local/www/cgi-bin/php.cgi', and both would have their executable bit set.
15+
16+
If, for what ever reason, you choose not to have a '/cgi-bin/' folder, you can look a what can be done with the bounce scripts in 'mthttpd/pgp-cgi/*' and the thttpd conf file there.
17+
18+
The last thing the bounce scripts does before passing everything along to PHP, is check the extension, and dump the html, text or download instead.
19+
20+
NOTE: one of the main jobs of mthttpd is to pass common non-CGI standard environment variable, ones that SAPI modules mostly use, eg DOCUMENT_ROOT & SCRIPT_FILENAME.
21+
22+
NOTE: the other main goal of mthttpd is to apply the v2.21 SAPI patch to modern versions of thttpd. This is currently a moot point, because as of late v5.?.? (v6?) the thttpd SAPI code was removed from PHP, because the current developers did not know what thttpd was, or how to enable a SAPI module for it. Plainly they were dickheads, however I did make a patch to reapply it to PHP. ATM Apache is the only PHP SAPI module server.

thttpd.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,11 @@ parse_args( int argc, char** argv )
868868
#else /* CGI_PATTERN */
869869
cgi_pattern = (char*) 0;
870870
#endif /* CGI_PATTERN */
871+
#ifdef PHP_PATTERN
872+
php_pattern = PHP_PATTERN;
873+
#else /* PHP_PATTERN */
874+
php_pattern = (char*) 0;
875+
#endif /* PHP_PATTERN */
871876
#ifdef CGI_LIMIT
872877
cgi_limit = CGI_LIMIT;
873878
#else /* CGI_LIMIT */
@@ -936,6 +941,11 @@ parse_args( int argc, char** argv )
936941
++argn;
937942
cgi_pattern = argv[argn];
938943
}
944+
else if ( strcmp( argv[argn], "-a" ) == 0 && argn + 1 < argc )
945+
{
946+
++argn;
947+
php_pattern = argv[argn];
948+
}
939949
else if ( strcmp( argv[argn], "-t" ) == 0 && argn + 1 < argc )
940950
{
941951
++argn;
@@ -993,12 +1003,9 @@ parse_args( int argc, char** argv )
9931003
static void
9941004
usage( void )
9951005
{
996-
/*
9971006
(void) fprintf( stderr,
998-
"usage: %s [-C configfile] [-p port] [-d dir] [-r|-nor] [-dd data_dir] [-s|-nos] [-v|-nov] [-g|-nog] [-u user] [-c cgipat] [-t throttles] [-h host] [-l logfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage] [-V] [-D]\n",
1007+
"usage: %s [-C configfile] [-p port] [-d dir] [-r|-nor] [-dd data_dir] [-s|-nos] [-v|-nov] [-g|-nog] [-u user] [-c cgipat] [-a phppat] [-t throttles] [-h host] [-l logfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage] [-V] [-D]\n",
9991008
argv0 );
1000-
*/
1001-
(void) fprintf( stderr, "usage: paged [-C configfile] [-p port] [-d dir] [-r|-nor] [-dd data_dir] [-s|-nos] [-v|-nov] [-g|-nog] [-u user] [-c cgipat] [-t throttles] [-h host] [-l logfile] [-i pidfile] [-T charset] [-P P3P] [-M maxage] [-V] [-D]\n" );
10021009
exit( 1 );
10031010
}
10041011

@@ -2178,7 +2185,7 @@ thttpd_logstats( long secs )
21782185
{
21792186
if ( secs > 0 )
21802187
syslog( LOG_NOTICE,
2181-
" paged - %ld connections (%g/sec), %d max simultaneous, %lld bytes (%g/sec), %d httpd_conns allocated",
2188+
" mthttpd - %ld connections (%g/sec), %d max simultaneous, %lld bytes (%g/sec), %d httpd_conns allocated",
21822189
stats_connections, (float) stats_connections / secs,
21832190
stats_simultaneous, (long long) stats_bytes,
21842191
(float) stats_bytes / secs, httpd_conn_count );

www/cgi-bin/bas.cgi

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/sh
2+
# "special" cgi->bas redirect for thttpd & bas ANSI BASIC Interpreter
3+
# eg. http:/localhost/cgi-bin/php.cgi/test/index.bas
4+
# == bas /home/sysop/devel/www/test/index.bas
5+
6+
export DOCUMENT_ROOT=/home/sysop/devel/www
7+
if [ "$PATH_TRANSLATED" = "" ]; then
8+
export PATH_TRANSLATED=${DOCUMENT_ROOT}/index.bas
9+
fi
10+
export SCRIPT_FILENAME=${PATH_TRANSLATED}
11+
12+
EXT=${PATH_TRANSLATED#"${PATH_TRANSLATED%????}"}
13+
if [ ! "$EXT" = ".bas" ]; then
14+
printf "Content-Type: "
15+
file -b -i ${PATH_TRANSLATED}
16+
echo
17+
cat ${PATH_TRANSLATED}
18+
else
19+
date=`date -u '+%a, %d %b %Y %H:%M:%S %Z'`
20+
cat << EOF
21+
Content-type: text/html
22+
Expires: $date
23+
24+
EOF
25+
exec /usr/bin/bas ${PATH_TRANSLATED}
26+
fi

0 commit comments

Comments
 (0)