rest.php 3.79 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 17.02.19
 * Time: 15:40
 */

Marc Egger's avatar
Marc Egger committed
9
namespace IMATHUZH\Qfq\Api;
Carsten  Rose's avatar
Carsten Rose committed
10

Marc Egger's avatar
Marc Egger committed
11
require_once(__DIR__ . '/../../vendor/autoload.php');
Carsten  Rose's avatar
Carsten Rose committed
12

13
use IMATHUZH\Qfq\Core\Helper\Path;
Marc Egger's avatar
Marc Egger committed
14
15
use IMATHUZH\Qfq\Core\QuickFormQuery;
use IMATHUZH\Qfq\Core\Helper\OnString;
Carsten  Rose's avatar
Carsten Rose committed
16

Carsten  Rose's avatar
Carsten Rose committed
17
18
$restId = array();
$restForm = array();
Carsten  Rose's avatar
Carsten Rose committed
19

20
$status = HTTP_400_BAD_REQUEST;
21
$data = array();
Carsten  Rose's avatar
Carsten Rose committed
22

Carsten  Rose's avatar
Carsten Rose committed
23
try {
24
    try {
Carsten  Rose's avatar
Carsten Rose committed
25
        $form = OnString::splitPathInfoToIdForm($_SERVER['PATH_INFO'] ?? '', $restId, $restForm);
26

27
        // get latest `ìd`
Carsten  Rose's avatar
Carsten Rose committed
28
        $id = end($restId);
29
30

        // Fake Bodytext setup
31
32
33
        $bodytext = TYPO3_RECORD_ID . '=' . $id . PHP_EOL;
        $bodytext .= TYPO3_FORM . '=' . $form . PHP_EOL;

Carsten  Rose's avatar
Carsten Rose committed
34
        $method = $_SERVER['REQUEST_METHOD'] ?? '';
Carsten  Rose's avatar
Carsten Rose committed
35
        switch ($method) {
36
            case REQUEST_METHOD_GET:
37
                $status = HTTP_200_OK;
38
                break;
39

40
            case REQUEST_METHOD_POST:
Carsten  Rose's avatar
Carsten Rose committed
41
                if ($id != 0) {
Marc Egger's avatar
Marc Egger committed
42
                    throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id>0 with HTTP method $method",
Marc Egger's avatar
Marc Egger committed
43
                        ERROR_MESSAGE_TO_DEVELOPER => '',
44
45
                        ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
                    ]), ERROR_REST_INVALID_ID);
46
                }
47

48
                $data = json_decode(file_get_contents('php://input'), true);
49
                $status = HTTP_201_CREATED;
50
                break;
51

52
            case REQUEST_METHOD_PUT:
Carsten  Rose's avatar
Carsten Rose committed
53
                if ($id == 0) {
Marc Egger's avatar
Marc Egger committed
54
                    throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id==0 with HTTP method $method",
Marc Egger's avatar
Marc Egger committed
55
                        ERROR_MESSAGE_TO_DEVELOPER => '',
56
57
                        ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
                    ]), ERROR_REST_INVALID_ID);
58
                }
59
                $data = json_decode(file_get_contents('php://input'), true);
60
                $status = HTTP_200_OK;
61
                break;
62

63
            case REQUEST_METHOD_DELETE:
Carsten  Rose's avatar
Carsten Rose committed
64
                if ($id == 0) {
Marc Egger's avatar
Marc Egger committed
65
                    throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Forbidden: id==0 with HTTP method $method",
Marc Egger's avatar
Marc Egger committed
66
                        ERROR_MESSAGE_TO_DEVELOPER => '',
67
68
                        ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
                    ]), ERROR_REST_INVALID_ID);
69
                }
70
                $status = HTTP_200_OK;
71
                break;
72

73
            default:
Marc Egger's avatar
Marc Egger committed
74
                throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => 'Unsupported/unknown HTTP request method',
Marc Egger's avatar
Marc Egger committed
75
                    ERROR_MESSAGE_TO_DEVELOPER => 'HTTP Code: ' . $method,
76
77
                    ERROR_MESSAGE_HTTP_STATUS => HTTP_403_METHOD_NOT_ALLOWED
                ]), ERROR_UNKNOWN_MODE);
78
79
80
                break;
        }

81
        if ($data === null) {
Marc Egger's avatar
Marc Egger committed
82
            throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Missing or broken JSON",
Marc Egger's avatar
Marc Egger committed
83
                ERROR_MESSAGE_TO_DEVELOPER => json_last_error_msg(),
84
85
                ERROR_MESSAGE_HTTP_STATUS => HTTP_400_BAD_REQUEST
            ]), ERROR_BROKEN_PARAMETER);
86
87
        }

88
        if (!empty($data)) {
89
90
91
            $_POST = $data;
        }

92
        $qfq = new QuickFormQuery(['bodytext' => $bodytext]);
93
        $answer = $qfq->rest($restId, $restForm);
Carsten  Rose's avatar
Carsten Rose committed
94

Marc Egger's avatar
Marc Egger committed
95
    } catch (\CodeException $e) {
96
        $answer[API_MESSAGE] = $e->formatMessage();
97
        $status = $e->getHttpStatus();
Carsten  Rose's avatar
Carsten Rose committed
98

Marc Egger's avatar
Marc Egger committed
99
    } catch (\UserFormException $e) {
100
        $answer[API_MESSAGE] = $e->formatMessage();
101
        $status = $e->getHttpStatus();
Carsten  Rose's avatar
Carsten Rose committed
102

Marc Egger's avatar
Marc Egger committed
103
    } catch (\DbException $e) {
104
        $answer[API_MESSAGE] = $e->formatMessage();
105
        $status = $e->getHttpStatus();
106
    }
107

108
109
} catch (\Exception $e) {
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
Carsten  Rose's avatar
Carsten Rose committed
110
}
Carsten  Rose's avatar
Carsten Rose committed
111

112
header('HTTP/1.0 ' . $status);
Carsten  Rose's avatar
Carsten Rose committed
113
114
header("Content-Type: application/json");
echo json_encode($answer);