1<?php
2
3namespace ComboStrap\Api;
4
5use Api\AjaxHandler;
6use ComboStrap\ExceptionInternal;
7use ComboStrap\ExceptionNotFound;
8use ComboStrap\ExceptionRuntimeInternal;
9use ComboStrap\ExecutionContext;
10use ComboStrap\HttpResponseStatus;
11use ComboStrap\IFetcher;
12use ComboStrap\Mime;
13use ComboStrap\PluginUtility;
14use ComboStrap\Web\Url;
15use dokuwiki\Extension\Event;
16
17class ApiRouter
18{
19    public const CANONICAL = "ajax";
20
21    /**
22     * The generic call that should be used for {@link \action_plugin_combo_ajax call}
23     */
24    public const AJAX_CALL_VALUE = "combo";
25    const AJAX_CALL_ATTRIBUTE = 'call';
26
27    /**
28     * @param Event $event
29     * @return void
30     */
31    public static function handle(Event $event)
32    {
33
34        $call = $event->data;
35        switch ($call) {
36            case QualityMessageHandler::CALL_ID:
37                QualityMessageHandler::handle($event);
38                return;
39            case MetaManagerHandler::META_MANAGER_CALL_ID:
40            case MetaManagerHandler::META_VIEWER_CALL_ID:
41                MetaManagerHandler::handle($event);
42                return;
43        }
44
45        $fetchUrl = Url::createFromGetOrPostGlobalVariable();
46        if ($call !== self::AJAX_CALL_VALUE && !$fetchUrl->hasProperty(IFetcher::FETCHER_KEY)) {
47            return;
48        }
49
50        // no other ajax call handlers needed
51        $event->stopPropagation();
52        $event->preventDefault();
53
54
55        $executionContext = ExecutionContext::getActualOrCreateFromEnv();
56        try {
57            $fetcher = $executionContext->createStringMainFetcherFromRequestedUrl($fetchUrl);
58        } catch (\Exception $e) {
59            if (PluginUtility::isTest()) {
60                throw new ExceptionRuntimeInternal("Error while creating the ajax fetcher.", self::CANONICAL, 1, $e);
61            }
62            $executionContext
63                ->response()
64                ->setException($e)
65                ->setBody("Error while creating the fetcher for the fetch Url ($fetchUrl)", Mime::getText())
66                ->end();
67            return;
68        }
69
70        $executionContext
71            ->response()
72            ->setStatus(HttpResponseStatus::ALL_GOOD)
73            ->setBody($fetcher->getFetchString(), $fetcher->getMime())
74            ->end();
75
76
77    }
78
79    /**
80     * @throws ExceptionNotFound
81     */
82    public static function getRequestParameter(string $parameter)
83    {
84        /**
85         * Shared check between post and get HTTP method
86         */
87        if (array_key_exists($parameter, $_GET)) {
88            /**
89             * May be null value with boolean
90             */
91            return $_GET[$parameter];
92        }
93
94        /**
95         * With {@link TestRequest}
96         * for instance
97         */
98        if (array_key_exists($parameter, $_REQUEST)) {
99            return $_REQUEST[$parameter];
100        }
101
102        global $INPUT;
103        if ($INPUT->has($parameter)) {
104            return $INPUT->str($parameter);
105        }
106
107        if (defined('DOKU_UNITTEST')) {
108            global $COMBO;
109            if (array_key_exists($parameter, $COMBO)) {
110                return $COMBO[$parameter];
111            }
112        }
113
114        throw new ExceptionNotFound("The parameter ($parameter) was not found for this request");
115
116    }
117
118    public static function hasRequestParameter(string $parameter): bool
119    {
120        try {
121            self::getRequestParameter($parameter);
122            return true;
123        } catch (ExceptionNotFound $e) {
124            return false;
125        }
126    }
127}
128