diff options
Diffstat (limited to 'examples/ajax-broker/ajax.php')
-rw-r--r-- | examples/ajax-broker/ajax.php | 50 |
1 files changed, 23 insertions, 27 deletions
diff --git a/examples/ajax-broker/ajax.php b/examples/ajax-broker/ajax.php index 68d252f..63679ec 100644 --- a/examples/ajax-broker/ajax.php +++ b/examples/ajax-broker/ajax.php @@ -1,41 +1,37 @@ <?php require_once $_SERVER['DOCUMENT_ROOT'] . '/src/Broker.php'; -function send_error($message) { - header("Content-Type: application/json"); - header("HTTP/1.1 406 Not Acceptable"); - echo '{error: "$message"}'; -} +$command = $_REQUEST['command']; +$broker = new Jasny\SSO\Broker('http://localhost:9000/examples/server/', 'BrokerApi', 'BrokerApi'); if (empty($_REQUEST['command'])) { - send_error('command not specified'); - exit(); -} -else if ($_REQUEST['command'] == 'on') { - send_error('unsupported command'); + header("Content-Type: application/json"); + header("HTTP/1.1 406 Not Acceptable"); + echo json_encode(['error' => 'Command not specified']); exit(); } - -$command = $_REQUEST['command']; -$broker = new Jasny\SSO\Broker('http://localhost:9000/examples/server/', 'Alice', 'Bob'); - -if (!empty($_REQUEST['token'])) { - $broker->token = $_REQUEST['token']; -} - -if (realpath($_SERVER["SCRIPT_FILENAME"]) == realpath(__FILE__) && isset($_REQUEST['command'])) { +else if (realpath($_SERVER["SCRIPT_FILENAME"]) == realpath(__FILE__)) { error_log('executing: '. $_REQUEST['command']); try { - $result = $broker->$_GET['command'](); - } - catch (\Exception $ex) { - $result = $ex->getMessage(); + $result = $broker->$_REQUEST['command'](); + header("Content-Type: application/json"); + echo json_encode($result); + } catch (Exception $ex) { + $errorCode = $ex->getCode(); + error_log('error code' . $errorCode); + + header("Content-Type: application/json"); + if ($errorCode == 401) header("HTTP/1.1 401 Unauthorized"); + if ($errorCode == 406) header("HTTP/1.1 406 Not Acceptable"); + + echo json_encode(['error' => $ex->getMessage()]); } } else { error_log('nothing to execute'); -} -header("Content-Type: application/json"); -echo json_encode($result); -?>
\ No newline at end of file + header("Content-Type: application/json"); + header("HTTP/1.1 406 Not Acceptable"); + echo json_encode(['error' => 'Command not supported']); + exit(); +} |