rest.php 2.74 KB
Newer Older
Carsten  Rose's avatar
Carsten Rose committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 17.02.19
 * Time: 15:40
 */

namespace qfq;

use qfq;

require_once(__DIR__ . '/../core/QuickFormQuery.php');
require_once(__DIR__ . '/../core/exceptions/UserFormException.php');
require_once(__DIR__ . '/../core/exceptions/CodeException.php');
require_once(__DIR__ . '/../core/exceptions/DbException.php');

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

Carsten  Rose's avatar
Carsten Rose committed
21
$status = 'HTTP/1.0 409 Bad Request';
22
$data = array();
Carsten  Rose's avatar
Carsten Rose committed
23

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

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

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

Carsten  Rose's avatar
Carsten Rose committed
35
36
        $method = $_SERVER['REQUEST_METHOD'];
        switch ($method) {
37
38
39
            case REQUEST_METHOD_GET:
                break;
            case REQUEST_METHOD_POST:
Carsten  Rose's avatar
Carsten Rose committed
40
                if ($id != 0) {
41
42
                    throw new UserFormException('Method POST needs no id or id=0', ERROR_REST_INVALID_ID);
                }
43
                $data = json_decode(file_get_contents('php://input'), true);
44
45
                break;
            case REQUEST_METHOD_PUT:
Carsten  Rose's avatar
Carsten Rose committed
46
                if ($id == 0) {
47
48
                    throw new UserFormException('Method PUT needs an id>0', ERROR_REST_INVALID_ID);
                }
49
                $data = json_decode(file_get_contents('php://input'), true);
50
51
                break;
            case REQUEST_METHOD_DELETE:
Carsten  Rose's avatar
Carsten Rose committed
52
                if ($id == 0) {
53
54
55
56
57
58
59
                    throw new UserFormException('Method DELETE needs an id>0', ERROR_REST_INVALID_ID);
                }
                break;
            default:
                break;
        }

60
61
62
63
64
65
66
67
68
69
        if ($data === null) {
            throw new NotAcceptableResponseException(
                json_encode([ERROR_MESSAGE_TO_USER => 'Invalid JSON',
                    ERROR_MESSAGE_SUPPORT => json_last_error_msg()]), ERROR_INVALID_VALUE);
        }

        if(!empty($data)){
            $_POST = $data;
        }

70
        $qfq = new QuickFormQuery(['bodytext' => $bodytext]);
71
        $answer = $qfq->rest($restId, $restForm);
Carsten  Rose's avatar
Carsten Rose committed
72
        $status = 'HTTP/1.0 200 OK';
Carsten  Rose's avatar
Carsten Rose committed
73

74
75
    } catch (qfq\CodeException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
76
        $status=$e->getHttpStatus();
Carsten  Rose's avatar
Carsten Rose committed
77

78
79
    } catch (qfq\UserFormException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
80
        $status=$e->getHttpStatus();
Carsten  Rose's avatar
Carsten Rose committed
81

82
83
    } catch (qfq\DbException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
84
        $status=$e->getHttpStatus();
85
    }
86

87
88
} catch (\Exception $e) {
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
Carsten  Rose's avatar
Carsten Rose committed
89
}
Carsten  Rose's avatar
Carsten Rose committed
90
91

header($status);
Carsten  Rose's avatar
Carsten Rose committed
92
93
header("Content-Type: application/json");
echo json_encode($answer);