From ba72326a1a30682bd077302748ca037dbf6be704 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Tue, 5 Mar 2013 21:46:17 -0500
Subject: [PATCH] Implements a more permissive way of detecting command-line
 execution. Change /api/task/* -> /api/tasks/*

---
 api/cron.php               | 7 +++++--
 api/http.php               | 2 +-
 api/pipe.php               | 8 ++++----
 include/class.api.php      | 4 ++--
 include/class.osticket.php | 7 +++++++
 5 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/api/cron.php b/api/cron.php
index 43ff2b285..0ca641b52 100644
--- a/api/cron.php
+++ b/api/cron.php
@@ -13,8 +13,11 @@
 
     vim: expandtab sw=4 ts=4 sts=4:
 **********************************************************************/
-if (substr(php_sapi_name(), 0, 3) != 'cli')
-    die('cron.php only supports local cron jobs - use http -> api/task/cron');
+@chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
+require('api.inc.php');
+
+if (!osTicket::is_cli())
+    die('cron.php only supports local cron calls - use http -> api/tasks/cron');
 
 @chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
 require('api.inc.php');
diff --git a/api/http.php b/api/http.php
index 985d8e498..0fc32444a 100644
--- a/api/http.php
+++ b/api/http.php
@@ -20,7 +20,7 @@ require_once INCLUDE_DIR."class.dispatcher.php";
 
 $dispatcher = patterns('',
         url_post("^/tickets\.(?P<format>xml|json|email)$", array('api.tickets.php:TicketApiController','create')),
-        url('^/task/', patterns('',
+        url('^/tasks/', patterns('',
                 url_post("^cron$", array('api.cron.php:CronApiController', 'execute'))
          ))
         );
diff --git a/api/pipe.php b/api/pipe.php
index aa35f8984..7cf1ad1b4 100644
--- a/api/pipe.php
+++ b/api/pipe.php
@@ -14,14 +14,14 @@
 
     vim: expandtab sw=4 ts=4 sts=4:
 **********************************************************************/
+ini_set('memory_limit', '256M'); //The concern here is having enough mem for emails with attachments.
+@chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
+require('api.inc.php');
 
 //Only local piping supported via pipe.php
-if (substr(php_sapi_name(), 0, 3) != 'cli')
+if (!osTicket::is_cli())
     die('pipe.php only supports local piping - use http -> api/tickets.email');
 
-ini_set('memory_limit', '256M'); //The concern here is having enough mem for emails with attachments.
-@chdir(realpath(dirname(__FILE__)).'/'); //Change dir.
-require('api.inc.php');
 require_once(INCLUDE_DIR.'api.tickets.php');
 PipeApiController::process();
 ?>
diff --git a/include/class.api.php b/include/class.api.php
index 6403222cc..9185093b4 100644
--- a/include/class.api.php
+++ b/include/class.api.php
@@ -193,8 +193,8 @@ class ApiController {
      */
     function getRequest($format) {
         global $ost;
-        
-        $input = (substr(php_sapi_name(), 0, 3) == 'cli')?'php://stdin':'php://input';
+
+        $input = $ost->is_cli()?'php://stdin':'php://input';
 
         if (!($stream = @fopen($input, 'r')))
             $this->exerr(400, "Unable to read request body");
diff --git a/include/class.osticket.php b/include/class.osticket.php
index 4445aca6c..4891b8d58 100644
--- a/include/class.osticket.php
+++ b/include/class.osticket.php
@@ -338,6 +338,13 @@ class osTicket {
 
         return $ost;
     }
+
+    /* is_cli */
+    function is_cli() {
+        return (!strcasecmp(substr(php_sapi_name(), 0, 3), 'cli')
+                || (!$_SERVER['REQUEST_METHOD'] && !$_SERVER['HTTP_HOST']) //Fallback when php-cgi binary is used via cli
+                );
+    }
 }
 
 ?>
-- 
GitLab