rest.php 2.28 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');

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

Carsten  Rose's avatar
Carsten Rose committed
21
22
$status='HTTP/1.0 409 Bad Request';

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`
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;

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

59
        $qfq = new QuickFormQuery(['bodytext' => $bodytext]);
60
        $answer = $qfq->rest($restId, $restForm);
Carsten  Rose's avatar
Carsten Rose committed
61
        $status='HTTP/1.0 200 OK';
Carsten  Rose's avatar
Carsten Rose committed
62

63
64
    } catch (qfq\CodeException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
Carsten  Rose's avatar
Carsten Rose committed
65

66
67
    } catch (qfq\UserFormException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
Carsten  Rose's avatar
Carsten Rose committed
68

69
70
    } catch (qfq\DbException $e) {
        $answer[API_MESSAGE] = $e->formatMessage();
Carsten  Rose's avatar
Carsten Rose committed
71

72
73
74
    }
} catch (\Exception $e) {
    $answer[API_MESSAGE] = "Generic Exception: " . $e->getMessage();
Carsten  Rose's avatar
Carsten Rose committed
75
}
Carsten  Rose's avatar
Carsten Rose committed
76
77

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