Subversion Repositories oidplus

Rev

Rev 1275 | Rev 1277 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1265 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
namespace ViaThinkSoft\OIDplus;
21
 
22
// TODO: should this be a different plugin type? A page without gui is weird!
23
// phpcs:disable PSR1.Files.SideEffects
24
\defined('INSIDE_OIDPLUS') or die;
25
// phpcs:enable PSR1.Files.SideEffects
26
 
27
class OIDplusPagePublicRestApi extends OIDplusPagePluginPublic {
28
 
29
        /**
30
         * @param string $request
31
         * @return bool
32
         * @throws OIDplusException
33
         */
34
        public function handle404(string $request): bool {
35
 
36
                if (!isset($_SERVER['REQUEST_URI']) || !isset($_SERVER["REQUEST_METHOD"])) return false;
37
 
38
                $rel_url = substr($_SERVER['REQUEST_URI'], strlen(OIDplus::webpath(null, OIDplus::PATH_RELATIVE_TO_ROOT)));
39
                $expect = 'rest/v1/';
40
                if (str_starts_with($rel_url, $expect)) {
41
                        $rel_url = ltrim($rel_url, $expect);
42
 
43
                        $requestMethod = $_SERVER["REQUEST_METHOD"];
44
 
45
                        try {
1275 daniel-mar 46
                                $cont = @file_get_contents('php://input');
47
                                $json_in = empty($cont) ? [] : @json_decode($cont, true);
48
                                if (!is_array($json_in)) throw new OIDplusException(_L('Invalid JSON data received'), null, 400);
49
 
1265 daniel-mar 50
                                $json_out = false;
51
                                foreach (OIDplus::getAllPlugins() as $plugin) {
52
                                        if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_9) {
1275 daniel-mar 53
                                                $json_out = $plugin->restApiCall($requestMethod, $rel_url, $json_in);
1265 daniel-mar 54
                                                if ($json_out !== false) break;
55
                                        }
56
                                }
57
                                if ($json_out === false) {
1275 daniel-mar 58
                                        throw new OIDplusException(_L('REST endpoint not found'), null, 404);
1265 daniel-mar 59
                                }
1276 daniel-mar 60
                                if (!isset($json_out['status'])) $json_out['status'] = -1; // status -1 and -2 like in ajax.php
61
                                if (!isset($json_out['status_bits'])) $json_out['status_bits'] = [];
1265 daniel-mar 62
                        } catch (\Exception $e) {
1269 daniel-mar 63
                                http_response_code($e instanceof OIDplusException ? $e->getHttpStatus() : 500);
1276 daniel-mar 64
                                $json_out = array("status" => -2, "status_bits" => [], "error" => $e->getMessage());
1265 daniel-mar 65
                        }
66
 
67
                        OIDplus::invoke_shutdown();
68
                        @header('Content-Type:application/json; charset=utf-8');
69
                        echo json_encode($json_out);
70
                        die(); // return true;
71
                }
72
 
73
                return false;
74
        }
75
 
76
}