Commit 40311bcf authored by Carsten  Rose's avatar Carsten Rose
Browse files

Individual HTTP_STATUS Code implemented.

parent bca8c51e
......@@ -18,7 +18,7 @@ require_once(__DIR__ . '/../core/exceptions/DbException.php');
$restId = array();
$restForm = array();
$status = 'HTTP/1.0 409 Bad Request';
$status = HTTP_400_BAD_REQUEST;
$data = array();
try {
......@@ -35,59 +35,73 @@ try {
$method = $_SERVER['REQUEST_METHOD'];
switch ($method) {
case REQUEST_METHOD_GET:
$status = HTTP_200_OK;
break;
case REQUEST_METHOD_POST:
if ($id != 0) {
throw new UserFormException('Method POST needs no id or id=0', ERROR_REST_INVALID_ID);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id>0 with HTTP method $method",
ERROR_MESSAGE_SUPPORT => '',
ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
]), ERROR_REST_INVALID_ID);
}
$data = json_decode(file_get_contents('php://input'), true);
$status = HTTP_201_CREATED;
break;
case REQUEST_METHOD_PUT:
if ($id == 0) {
throw new UserFormException('Method PUT needs an id>0', ERROR_REST_INVALID_ID);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id==0 with HTTP method $method",
ERROR_MESSAGE_SUPPORT => '',
ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
]), ERROR_REST_INVALID_ID);
}
$data = json_decode(file_get_contents('php://input'), true);
$status = HTTP_200_OK;
break;
case REQUEST_METHOD_DELETE:
if ($id == 0) {
throw new UserFormException('Method DELETE needs an id>0', ERROR_REST_INVALID_ID);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id==0 with HTTP method $method",
ERROR_MESSAGE_SUPPORT => '',
ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
]), ERROR_REST_INVALID_ID);
}
$status = HTTP_200_OK;
break;
default:
break;
}
if ($data === null) {
throw new NotAcceptableResponseException(
json_encode([ERROR_MESSAGE_TO_USER => 'Invalid JSON',
ERROR_MESSAGE_SUPPORT => json_last_error_msg()]), ERROR_INVALID_VALUE);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Missing or broken JSON",
ERROR_MESSAGE_SUPPORT => json_last_error_msg(),
ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
]), ERROR_BROKEN_PARAMETER);
}
if(!empty($data)){
if (!empty($data)) {
$_POST = $data;
}
$qfq = new QuickFormQuery(['bodytext' => $bodytext]);
$answer = $qfq->rest($restId, $restForm);
$status = 'HTTP/1.0 200 OK';
} catch (qfq\CodeException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
$status=$e->getHttpStatus();
$status = $e->getHttpStatus();
} catch (qfq\UserFormException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
$status=$e->getHttpStatus();
$status = $e->getHttpStatus();
} catch (qfq\DbException $e) {
$answer[API_MESSAGE] = $e->formatMessage();
$status=$e->getHttpStatus();
$status = $e->getHttpStatus();
}
} catch (\Exception $e) {
$answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
}
header($status);
header('HTTP/1.0 ' . $status);
header("Content-Type: application/json");
echo json_encode($answer);
......@@ -1763,5 +1763,12 @@ const ATTRIBUTE_DATA_REFERENCE = 'data-reference';
// REST
const HTTP_HEADER_AUTHORIZATION = 'Authorization';
const HTTP_401 = '401 Unauthorized';
const HTTP_403 = '403 Forbidden';
\ No newline at end of file
const HTTP_200_OK = '200 OK';
const HTTP_201_CREATED = '201 Created';
const HTTP_400_BAD_REQUEST = '400 Bad Request';
const HTTP_401_UNAUTHORIZED = '401 Unauthorized';
const HTTP_403_FORBIDDEN = '403 Forbidden';
const HTTP_403_METHOD_NOT_ALLOWED = '405 Method Not Allowed';
const HTTP_404_NOT_FOUND = '404 Not Found';
const HTTP_409_CONFLICT = '409 Conflict';
\ No newline at end of file
......@@ -632,7 +632,6 @@ class QuickFormQuery {
* If not: throw an exception.
*
* @param string|array $serverToken
* @throws RestException
* @throws CodeException
* @throws UserFormException
*/
......@@ -653,15 +652,15 @@ class QuickFormQuery {
sleep($seconds);
if ($clientToken == false) {
throw new RestException(json_encode([ERROR_MESSAGE_TO_USER => 'Missing authorization token',
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Missing authorization token',
ERROR_MESSAGE_SUPPORT => "Missing HTTP Header: " . HTTP_HEADER_AUTHORIZATION,
ERROR_MESSAGE_HTTP_STATUS => HTTP_401
ERROR_MESSAGE_HTTP_STATUS => HTTP_401_UNAUTHORIZED
]), ERROR_REST_AUTHORIZATION);
}
throw new RestException(json_encode([ERROR_MESSAGE_TO_USER => 'Authorization token not accepted',
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Authorization token not accepted',
ERROR_MESSAGE_SUPPORT => "Missing HTTP Header: " . HTTP_HEADER_AUTHORIZATION,
ERROR_MESSAGE_HTTP_STATUS => HTTP_401
ERROR_MESSAGE_HTTP_STATUS => HTTP_401_UNAUTHORIZED
]), ERROR_REST_AUTHORIZATION);
}
......@@ -1435,7 +1434,6 @@ class QuickFormQuery {
* @param string $formMode
*
* @return bool 'true' if SIP exists, else 'false'
* @throws RestException
* @throws CodeException
* @throws UserFormException
*/
......@@ -1470,7 +1468,12 @@ class QuickFormQuery {
$method = $this->store::getVar(CLIENT_REQUEST_METHOD, STORE_CLIENT);
if (false === Support::findInSet(strtolower($method), $this->formSpec[F_REST_METHOD])) {
throw new RestException("Endpoint '" . $this->formSpec[F_NAME] . "' is not allowed with method '$method'", ERROR_FORM_REST);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Invalid HTTP method',
ERROR_MESSAGE_SUPPORT => "Endpoint '" . $this->formSpec[F_NAME] . "' is not allowed with HTTP method '$method'",
ERROR_MESSAGE_HTTP_STATUS => HTTP_401_UNAUTHORIZED
]), ERROR_FORM_REST);
}
$this->restCheckAuthToken($this->formSpec[F_REST_TOKEN] ?? '');
......@@ -1478,20 +1481,21 @@ class QuickFormQuery {
switch ($method) {
case REQUEST_METHOD_GET:
break;
case REQUEST_METHOD_POST:
if ($r != 0) {
throw new RestException('Mode GET with id>0 is forbidden', ERROR_UNKNOWN_MODE);
}
$formModeNew = FORM_SAVE;
break;
case REQUEST_METHOD_PUT:
$formModeNew = FORM_SAVE;
break;
case REQUEST_METHOD_DELETE:
$formModeNew = FORM_DELETE;
break;
default:
throw new RestException('Unknown Request Method: ' . $method, ERROR_UNKNOWN_MODE);
throw new UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Unsupported/unknown HTTP request method',
ERROR_MESSAGE_SUPPORT => 'HTTP Code: ' . $method,
ERROR_MESSAGE_HTTP_STATUS => HTTP_403_METHOD_NOT_ALLOWED
]), ERROR_UNKNOWN_MODE);
}
} else {
......
......@@ -187,7 +187,7 @@ class AbstractException extends \Exception {
* @return string
*/
public function getHttpStatus() {
return $this->$this->httpStatusCode;
return $this->httpStatusCode;
}
/**
......
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 21.02.19
* Time: 23:47
*/
namespace qfq;
require_once(__DIR__ . '/AbstractException.php');
/**
* Class NotAcceptableResponseException
*
* Thrown by API call with bad content type
*
* Throw with ONE message
*
* throw new UserFormException('Failed: chmod ....', ERROR_IO_CHMOD);
*
* Throw with message for User and message for Support.
*
* throw new UserFormException( json_encode(
* [ERROR_MESSAGE_TO_USER => 'Failed: chmod',
* ERROR_MESSAGE_SUPPORT => "Failed: chmod $mode '$pathFileName'"]),
* ERROR_IO_CHMOD);
*
* @package qfq\exceptions
*/
class NotAcceptableResponseException extends AbstractException {
/**
* $this->getMessage() might give a) a simple string or b) an JSON String.
*
* JSON String: There are 3+1 different messages:
* [ERROR_MESSAGE_TO_USER] 'toUser' - shown in the client to the user - no details here!!!
* [ERROR_MESSAGE_SUPPORT] 'support' - help for the developer
* [ERROR_MESSAGE_OS] 'os' - message from the OS, like 'file not found'
*
* @return string HTML formatted error string
* @throws CodeException
* @throws UserFormException
*/
public function formatMessage() {
$this->messageArrayDebug[EXCEPTION_TYPE] = 'Not Acceptable Response';
return parent::formatException();
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 21.02.19
* Time: 23:47
*/
namespace qfq;
require_once(__DIR__ . '/AbstractException.php');
/**
* Class RestException
*
* Thrown by API call with violated access rules
*
* Throw with ONE message
*
* throw new UserFormException('Failed: chmod ....', ERROR_IO_CHMOD);
*
* Throw with message for User and message for Support.
*
* throw new UserFormException( json_encode(
* [ERROR_MESSAGE_TO_USER => 'Failed: chmod',
* ERROR_MESSAGE_SUPPORT => "Failed: chmod $mode '$pathFileName'"]),
* ERROR_IO_CHMOD);
*
* @package qfq\exceptions
*/
class RestException extends AbstractException {
/**
* $this->getMessage() might give a) a simple string or b) an JSON String.
*
* JSON String: There are 3+1 different messages:
* [ERROR_MESSAGE_TO_USER] 'toUser' - shown in the client to the user - no details here!!!
* [ERROR_MESSAGE_SUPPORT] 'support' - help for the developer
* [ERROR_MESSAGE_OS] 'os' - message from the OS, like 'file not found'
*
* @return string HTML formatted error string
* @throws CodeException
* @throws UserFormException
*/
public function formatMessage() {
$this->messageArrayDebug[EXCEPTION_TYPE] = 'Access Forbidden Exception';
return parent::formatException();
}
}
\ No newline at end of file
......@@ -26,6 +26,8 @@ class Logger {
private static $systemSitePath = '';
/**
* Copy the SystemSitePath to a local variable.
*
* @param $path
*/
public static function setSystemSitePath($path) {
......@@ -33,7 +35,7 @@ class Logger {
}
/**
* Append $msg to $filename.
* Append $msg to $filename. Create the file it it not exist.
*
* @param $msg
* @param $filename
......@@ -88,9 +90,12 @@ class Logger {
}
/**
* Prefix every message with linePre().
*
* @param $msg
* @param $filename
* @param string $mode
* @throws CodeException
* @throws UserFormException
*/
public static function logMessageWithPrefix($msg, $filename, $mode = FILE_MODE_APPEND) {
......@@ -127,6 +132,8 @@ class Logger {
}
/**
* Returns a timestamp, IP, cookie.
*
* @return string
*/
public static function linePre() {
......@@ -145,16 +152,14 @@ class Logger {
}
/**
* @param array $fe
* Format details of a FormElement.
*
* @param array $fe
* @return string
*/
public static function formatFormElementName(array $fe) {
Support::setIfNotSet($fe, 'id');
Support::setIfNotSet($fe, FE_NAME);
Support::setIfNotSet($fe, FE_LABEL);
return $fe['id'] . ' / ' . $fe[FE_NAME] . ' / ' . $fe[FE_LABEL];
return ($fe['id']??'') . ' / ' . ($fe[FE_NAME]??'') . ' / ' . ($fe[FE_LABEL]??'');
}
/**
......@@ -165,6 +170,7 @@ class Logger {
* @param $pre
* @param $data
* @param bool $flagNewLineFirst
* @throws CodeException
* @throws UserFormException
*/
public static function logFormLine(array $form, $pre, $data, $flagNewLineFirst = false) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment