Skip to content

Commit

Permalink
Improve calling context checks on cli.php (#694)
Browse files Browse the repository at this point in the history
This commit uses php_sapi_name() against a definite value, rather than
relying on the absense of various superglobals to determine CLI context.

If HTTP is detected, a 404 is issued.

It also adds some additional CLI error outputs, and exit codes for basic
invocation.

Co-authored-by: Rich Lott / Artful Robot <forums@artfulrobot.uk>
  • Loading branch information
artfulrobot and Rich Lott / Artful Robot committed Oct 21, 2020
1 parent 758ee5c commit 2170d3d
Showing 1 changed file with 32 additions and 24 deletions.
56 changes: 32 additions & 24 deletions cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
// limitations under the License.
//
// $Id$
//

require_once('owa_env.php');
require_once(OWA_DIR.'owa_caller.php');
require_once(OWA_BASE_CLASS_DIR.'cliController.php');
//

/**
* OWA Comand Line Interface (CLI)
Expand All @@ -32,28 +28,40 @@
* @since owa 1.2.1
*/

define('OWA_CLI', true);
// Ensure we are being called as a CLI process before any other processing.
define('OWA_CLI', (php_sapi_name() == 'cli' || (is_numeric($_SERVER['argc']) && $_SERVER['argc'] > 0)));

if (!empty($_POST)) {
exit();
} elseif (!empty($_GET)) {
if (!OWA_CLI)
{
// Fail with 404 if called over HTTP so it looks like the script
// just doesn't exist.
if (isset($_SERVER['SERVER_PROTOCOL'])) {
header("$_SERVER[SERVER_PROTOCOL] 404 Not Found");
}
exit();
} elseif (!empty($argv)) {
$params = array();
// get params from the command line args
// $argv is a php super global variable
}

for ($i=1; $i<count($argv);$i++)
{
$it = explode("=",$argv[$i]);
$params[$it[0]] = $it[1];
}
unset($params['action']);
unset($params['do']);
require_once('owa_env.php');
require_once(OWA_DIR.'owa_caller.php');
require_once(OWA_BASE_CLASS_DIR.'cliController.php');

} else {
// No params found
exit();
$params = [];
// get params from the command line args
// $argv is a php super global variable
for ($i=1; $i<count($argv); $i++)
{
$it = explode("=",$argv[$i]);
if (count($it) !== 2) {
fwrite(STDERR, "Invalid argument '{$argv[$i]}'. Syntax is key=value\n");
exit(1);
}
$params[$it[0]] = $it[1];
}
unset($params['action']);
unset($params['do']);
if (empty($params)) {
fwrite(STDERR, "Arguments required\n");
exit(1);
}

// Initialize owa
Expand Down Expand Up @@ -90,4 +98,4 @@
$owa->restInPeace();
}

?>
?>

0 comments on commit 2170d3d

Please sign in to comment.