Exemple #1
0
 public static function handle_request()
 {
     global $g_error_log;
     global $config;
     /* handle debug test mode; ?io=test */
     $debug = false;
     if (isset($_REQUEST['debug']) && $_REQUEST['debug'] == true) {
         $debug = true;
     }
     if (!$config->debug && $debug) {
         Util::respond_with_http_error(403, 'Forbidden');
     }
     if ($_REQUEST['io'] == 'test') {
         $debug = true;
         if (!$config->debug && $debug) {
             Util::respond_with_http_error(403, 'Forbidden');
         }
         Gateway::print_test_form('');
         exit;
     }
     /* normal processing of AJAX request */
     Util::response_is_ajax_only();
     /* log errors with custom handler and process at conclusion of request */
     //set_error_handler(array('Gateway', 'custom_error_handler'));
     /* in testing, it may be useful to be able to submit a request in this way */
     if (isset($_REQUEST['request'])) {
         $inbound = $_REQUEST['request'];
     } else {
         $inbound = '';
     }
     /* invoke the method as specified in the cmd field of the request */
     $outbound = array();
     try {
         if ($inbound != '') {
             $inbound = json_decode($inbound, true);
         } else {
             $inbound = json_decode(@file_get_contents('php://input'), true);
         }
         if ($inbound === null) {
             CinsImpError::malformed('JSON input malformed');
         }
         $outbound['cmd'] = $inbound['cmd'];
         try {
             $action_method = new ReflectionMethod('Gateway', $inbound['cmd']);
         } catch (Exception $err) {
             throw new Exception("Gateway: Command " . $inbound['cmd'] . " unrecognised.");
         }
         $outbound = $action_method->invoke(null, $inbound, $outbound);
     } catch (Exception $err) {
         $err = new CinsImpError($err);
         $outbound = array();
         $outbound['cmd'] = 'error';
         $outbound['msg'] = 'Server: ' . $err->getMessage() . ': ' . $err->getDetail();
         $outbound['cde'] = $err->getID();
     }
     /* if we're debugging the gateway, output the response on the test form,
     		otherwise send a standard JSON response */
     if ($debug) {
         Gateway::print_test_form(json_encode($outbound, JSON_PRETTY_PRINT));
     } else {
         header('Content-type: application/json');
         print json_encode($outbound);
     }
 }