/** * This function handle a request binding it to a given object * * @param Request $request * @param object $object * @return array|null * @throws \Exception */ public static function handle(Request $request, $object) { // user-defined error handler to catch annoying php errors and throw them as exceptions set_error_handler(function ($errno, $errstr, $errfile, $errline) { throw new ErrorHandler($errstr, 0, $errno, $errfile, $errline); }, E_ALL); // checks if a JSON-RPC request has been received if ($_SERVER['REQUEST_METHOD'] != 'POST' || empty($_SERVER['CONTENT_TYPE']) || strrpos($_SERVER['CONTENT_TYPE'], "application/json") === false && strrpos($_SERVER['CONTENT_TYPE'], "application/xml") === false) { throw new \Exception("Not a JSON-RPC request: ct: '" . @$_SERVER['CONTENT_TYPE'] . "'"); } // executes the task on local object try { // TODO: refactor to use an error dto $object->checkPermissions($request->request->get('method'), $request->request->get('params')); if (method_exists($object, $request->request->get('method'))) { $result = call_user_func_array(array($object, $request->request->get('method')), $request->request->get('params')); $response = array('jsonrpc' => '2.0', 'id' => $request->request->get('id'), 'result' => $result, 'error' => NULL); } else { $response = array('jsonrpc' => '2.0', 'id' => $request->request->get('id'), 'result' => NULL, 'error' => array('type' => 'UnknownMethod', 'message' => sprintf("unknown method '%s' on class '%s'", $request->request->get('method'), get_class($object)))); } } catch (\Exception $e) { $response = array('id' => $request->request->get('id'), 'result' => NULL, 'error' => array('type' => get_class($e), 'message' => $e->getMessage() . " line " . $e->getLine() . " " . $e->getFile() . " " . CodeGuard::getStackTrace($e->getTrace()))); if ($e instanceof ResourceNotAvailableException) { $response['error']['type'] = 'ResourceNotAvailableException'; $response['error']['message'] = $e->getMessage(); } elseif ($e instanceof UserNotAuthenticatedException) { $response['error']['type'] = 'UserNotAuthenticatedException'; $response['error']['message'] = $e->getMessage(); } elseif ($e instanceof UserUnauthorizedException) { $response['error']['type'] = 'UserUnauthorizedException'; $response['error']['message'] = $e->getMessage(); } $message = ''; $message .= $e->getMessage() . "\n"; $message .= $e->getTraceAsString() . "\n"; error_log($message); } if (!$request->request->get('id')) { // notifications don't want response return null; } return $response; }
public function receive(Application $app, $appType, $mediaType) { // e.g. 'lf', 'entry-audio' // user-defined error handler to catch annoying php errors and throw them as exceptions ini_set('xdebug.show_exception_trace', 0); set_error_handler(function ($errno, $errstr, $errfile, $errline) { throw new ErrorHandler($errstr, 0, $errno, $errfile, $errline); }, E_ALL); $response = array(); $status = 201; try { // check for mocked E2E upload if (array_key_exists('file', $_POST)) { $filePath = sys_get_temp_dir() . '/' . $_POST['file']['name']; if (file_exists($filePath) && !is_dir($filePath)) { $file = $_POST['file']; $file['error'] = UPLOAD_ERR_OK; $tmpFilePath = $filePath; $_FILES['file'] = $file; } else { $file = $_FILES['file']; } } else { $file = $_FILES['file']; } if ($file['error'] == UPLOAD_ERR_OK) { if (!isset($tmpFilePath)) { $tmpFilePath = $this->moveUploadedFile(); } if ($appType == 'sf-checks') { $api = new Sf($app); $api->checkPermissions('sfChecks_uploadFile', array($mediaType, $tmpFilePath)); $response = $api->sfChecks_uploadFile($mediaType, $tmpFilePath); } elseif ($appType == 'lf-lexicon') { $api = new Sf($app); switch ($mediaType) { case 'audio': $api->checkPermissions('lex_uploadAudioFile', array($mediaType, $tmpFilePath)); $response = $api->lex_uploadAudioFile($mediaType, $tmpFilePath); break; case 'sense-image': $api->checkPermissions('lex_uploadImageFile', array($mediaType, $tmpFilePath)); $response = $api->lex_uploadImageFile($mediaType, $tmpFilePath); break; case 'import-zip': $api->checkPermissions('lex_upload_importProjectZip', array($mediaType, $tmpFilePath)); $response = $api->lex_upload_importProjectZip($mediaType, $tmpFilePath); break; case 'import-lift': $api->checkPermissions('lex_upload_importLift', array($mediaType, $tmpFilePath)); $response = $api->lex_upload_importLift($mediaType, $tmpFilePath); break; default: throw new \Exception("Unsupported upload type: {$mediaType}"); } } else { throw new \Exception("Unsupported upload app: {$appType}"); } // cleanup uploaded file if it hasn't been moved if ($tmpFilePath && file_exists($tmpFilePath)) { @unlink($tmpFilePath); } } } catch (\Exception $e) { $response = array('result' => false, 'data' => array('errorType' => get_class($e), 'errorMessage' => $e->getMessage() . " line " . $e->getLine() . " " . $e->getFile() . " " . CodeGuard::getStackTrace($e->getTrace()))); $status = 400; if ($e instanceof ResourceNotAvailableException) { $response['data']['errorType'] = 'ResourceNotAvailableException'; $response['data']['errorMessage'] = $e->getMessage(); $status = 404; } elseif ($e instanceof UserNotAuthenticatedException) { $response['data']['errorType'] = 'UserNotAuthenticatedException'; $response['data']['errorMessage'] = $e->getMessage(); $status = 401; } elseif ($e instanceof UserUnauthorizedException) { $response['data']['errorType'] = 'UserUnauthorizedException'; $response['data']['errorMessage'] = $e->getMessage(); $status = 403; } $message = ''; $message .= $e->getMessage() . "\n"; $message .= $e->getTraceAsString() . "\n"; error_log($message); } return $app->json($response, $status); }