| 1 |
<?php |
| 2 |
#? |
| 3 |
#? NAME |
| 4 |
#? $0 - simple wrapper for o-saft.cgi |
| 5 |
#? |
| 6 |
#? WARNING #################################################################### |
| 7 |
#? This is not tamper-proof code. ## |
| 8 |
#? It passes all received parameters unmodified "as is" to the shell. ## |
| 9 |
#? Hence this script may be subject to code injections. ## |
| 10 |
#? ## |
| 11 |
#? You have been warned! ## |
| 12 |
#? ## |
| 13 |
#? ############################################################################ |
| 14 |
#? |
| 15 |
#? DESCRIPTION |
| 16 |
#? PHP wrapper to call o-saft.cgi. All arguments are passed thru. |
| 17 |
#? The purpose is to support o-saft.cgi on web servers which have not |
| 18 |
#? enabled perl as CGI, or if it is not possible to configure a proper |
| 19 |
#? handler for perl but allows PHP. |
| 20 |
#? o-saft.cgi or o-saft.pl will be search for in various paths related |
| 21 |
#? to $_SERVER['SCRIPT_FILENAME']. |
| 22 |
#? |
| 23 |
#? VERSION |
| 24 |
#? @(#) o-saft.php 1.9 19/11/12 11:26:46 |
| 25 |
#? |
| 26 |
#? AUTHOR |
| 27 |
#? 17-feb-17 Achim Hoffmann |
| 28 |
# ----------------------------------------------------------------------------- |
| 29 |
|
| 30 |
function get_exe($base, $file, $dirs) { |
| 31 |
$exe = $file; # fallback |
| 32 |
foreach ($dirs as $dir) { |
| 33 |
#$exe = realpath(os.path.join($base, $dir, $file)); |
| 34 |
# NOTE: os.path not used to avoid include of os |
| 35 |
$exe = realpath(join(DIRECTORY_SEPARATOR, [$base, $dir, $file])); |
| 36 |
if (! empty($exe)) { break; } # got it |
| 37 |
} |
| 38 |
return $exe; |
| 39 |
} |
| 40 |
|
| 41 |
# list of directories where to search for o-saft.[cgi|pl] ; first one wins |
| 42 |
# NOTE: */O-Saft and ../ used for easy testing in development environment, |
| 43 |
# can/should be removed in production |
| 44 |
$dirs = array('.', 'cgi-bin', '../cgi-bin', 'O-Saft', 'cgi-bin/O-Saft', '../cgi-bin/O-Saft', '..'); |
| 45 |
$path = pathinfo( $_SERVER['SCRIPT_FILENAME']); # analyze myself |
| 46 |
|
| 47 |
if (empty($_SERVER['QUERY_STRING'])) { |
| 48 |
header("HTTP/1.1 406 Not Acceptable"); |
| 49 |
exit(2); |
| 50 |
} |
| 51 |
|
| 52 |
$_SERVER['QUERY_STRING'] .= "&--cgi-no-header"; # HTTP headers are most likely done by PHP module itself, hence disable in CGI |
| 53 |
putenv("QUERY_STRING=".$_SERVER['QUERY_STRING']); # ensure setting, in case of missing |
| 54 |
$cgi = get_exe($path['dirname'], 'o-saft.cgi', $dirs); |
| 55 |
$path = pathinfo($cgi); # need to start in directory so that .o-saft.pl is used |
| 56 |
$cgi = join(DIRECTORY_SEPARATOR, ['.', $path['basename'] ]); # redefine to ./o-saft.cgi |
| 57 |
$call = join(' ', ['cd', $path['dirname'], ';', $cgi]); |
| 58 |
passthru("$call", $err); |
| 59 |
exit(0); |
| 60 |
|
| 61 |
#_________________________________________ alternate methods, NOT RECOMMENDED _ |
| 62 |
|
| 63 |
header('Content-Type: text/plain'); |
| 64 |
$qs = ""; |
| 65 |
if (isset($_SERVER['QUERY_STRING'])) { |
| 66 |
$qs = join(' ', preg_split('/&/', $_SERVER['QUERY_STRING'])); |
| 67 |
} |
| 68 |
$qs = preg_replace('/[;&`>!|$<]/', '', $qs, -1);# remove just a few dangerous characters |
| 69 |
$exe = get_exe($path['dirname'], 'o-saft.pl', $dirs); |
| 70 |
$call = join(' ', ['cd', $path['dirname'], ';', $path['basename'], '--cgi', $qs]); |
| 71 |
#dbx# echo("call=$call\n"); |
| 72 |
passthru( "$call", $err); # pass QUERY_STRING on command line |
| 73 |
#dbx# echo "# ERROR=$err\n"; |
| 74 |
exit(0); |
| 75 |
|
| 76 |
?> |