Rev 1205 | Rev 1264 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
107 | daniel-mar | 1 | <?php |
2 | |||
3 | /* |
||
4 | * OIDplus 2.0 |
||
773 | daniel-mar | 5 | * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft |
107 | daniel-mar | 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 | |||
1050 | daniel-mar | 20 | use ViaThinkSoft\OIDplus\OIDplus; |
21 | use ViaThinkSoft\OIDplus\OIDplusException; |
||
22 | |||
107 | daniel-mar | 23 | require_once __DIR__ . '/includes/oidplus.inc.php'; |
24 | |||
328 | daniel-mar | 25 | try { |
26 | OIDplus::init(false); |
||
107 | daniel-mar | 27 | |
579 | daniel-mar | 28 | if (isset($_GET['OIDPLUS_AUTH_JWT']) || isset($_POST['OIDPLUS_AUTH_JWT'])) { |
573 | daniel-mar | 29 | originHeaders(); // Allows queries from other domains |
30 | OIDplus::authUtils()->disableCSRF(); // allow access to ajax.php without valid CSRF token |
||
31 | } |
||
32 | |||
328 | daniel-mar | 33 | $json_out = null; |
427 | daniel-mar | 34 | |
320 | daniel-mar | 35 | if (isset($_REQUEST['plugin']) && ($_REQUEST['plugin'] != '')) { |
317 | daniel-mar | 36 | |
320 | daniel-mar | 37 | // Actions handled by plugins |
107 | daniel-mar | 38 | |
320 | daniel-mar | 39 | $plugin = OIDplus::getPluginByOid($_REQUEST['plugin']); |
40 | if (!$plugin) { |
||
360 | daniel-mar | 41 | throw new OIDplusException(_L('Plugin with OID "%1" not found',$_REQUEST['plugin'])); |
107 | daniel-mar | 42 | } |
43 | |||
328 | daniel-mar | 44 | $params = array(); |
45 | foreach (array_merge($_POST,$_GET) as $name => $val) { |
||
46 | if (($name != 'action') && ($name != 'plugin')) { |
||
47 | $params[$name] = $val; |
||
48 | } |
||
49 | } |
||
50 | |||
553 | daniel-mar | 51 | if (isset($_REQUEST['action']) && ($_REQUEST['action'] != '')) { |
607 | daniel-mar | 52 | if ($plugin->csrfUnlock($_REQUEST['action'])) { |
53 | originHeaders(); // Allows queries from other domains |
||
54 | OIDplus::authUtils()->disableCSRF(); // allow access to ajax.php without valid CSRF token |
||
55 | } |
||
56 | |||
57 | OIDplus::authUtils()->checkCSRF(); |
||
58 | |||
59 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
||
60 | OIDplus::db()->transaction_begin(); |
||
61 | } |
||
1231 | daniel-mar | 62 | try { |
63 | $json_out = $plugin->action($_REQUEST['action'], $params); |
||
64 | if (!isset($json_out['status'])) $json_out['status'] = -1; |
||
65 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
||
66 | OIDplus::db()->transaction_commit(); |
||
67 | } |
||
68 | } catch (\Exception $e) { |
||
69 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
||
70 | if (OIDplus::db()->transaction_supported()) OIDplus::db()->transaction_rollback(); |
||
71 | } |
||
72 | throw $e; |
||
553 | daniel-mar | 73 | } |
74 | } else { |
||
75 | throw new OIDplusException(_L('Invalid action ID')); |
||
107 | daniel-mar | 76 | } |
108 | daniel-mar | 77 | |
320 | daniel-mar | 78 | } else { |
107 | daniel-mar | 79 | |
320 | daniel-mar | 80 | // Actions handled by the system (base functionality like the JS tree) |
107 | daniel-mar | 81 | |
607 | daniel-mar | 82 | OIDplus::authUtils()->checkCSRF(); |
83 | |||
320 | daniel-mar | 84 | if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) { |
85 | // Action: get_description |
||
86 | // Method: GET / POST |
||
87 | // Parameters: id |
||
88 | // Outputs: JSON |
||
553 | daniel-mar | 89 | _CheckParamExists($_REQUEST, 'id'); |
775 | daniel-mar | 90 | $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false); |
320 | daniel-mar | 91 | try { |
558 | daniel-mar | 92 | $json_out = OIDplus::gui()->generateContentPage($_REQUEST['id']); |
1050 | daniel-mar | 93 | } catch (\Exception $e) { |
328 | daniel-mar | 94 | $json_out = array(); |
360 | daniel-mar | 95 | $json_out['title'] = _L('Error'); |
800 | daniel-mar | 96 | $json_out['icon'] = 'img/error.png'; |
1201 | daniel-mar | 97 | $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage()); |
1205 | daniel-mar | 98 | if (strtolower(substr($htmlmsg, 0, 3)) === '<p ') { |
99 | $json_out['text'] = $htmlmsg; |
||
100 | } else { |
||
101 | $json_out['text'] = '<p>'.$htmlmsg.'</p>'; |
||
102 | } |
||
320 | daniel-mar | 103 | } |
560 | daniel-mar | 104 | $json_out['status'] = 0; |
320 | daniel-mar | 105 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) { |
106 | // Action: tree_search |
||
107 | // Method: GET / POST |
||
108 | // Parameters: search |
||
109 | // Outputs: JSON |
||
553 | daniel-mar | 110 | _CheckParamExists($_REQUEST, 'search'); |
150 | daniel-mar | 111 | |
320 | daniel-mar | 112 | $found = false; |
113 | foreach (OIDplus::getPagePlugins() as $plugin) { |
||
328 | daniel-mar | 114 | $json_out = $plugin->tree_search($_REQUEST['search']); |
115 | if ($json_out) { |
||
320 | daniel-mar | 116 | $found = true; |
117 | break; |
||
118 | } |
||
119 | } |
||
317 | daniel-mar | 120 | |
320 | daniel-mar | 121 | if (!$found) { |
328 | daniel-mar | 122 | $json_out = array(); |
320 | daniel-mar | 123 | } |
124 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) { |
||
125 | // Action: tree_load |
||
126 | // Method: GET / POST |
||
127 | // Parameters: id; goto (optional) |
||
128 | // Outputs: JSON |
||
553 | daniel-mar | 129 | _CheckParamExists($_REQUEST, 'id'); |
775 | daniel-mar | 130 | $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false); |
1130 | daniel-mar | 131 | $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], $_REQUEST['goto'] ?? ''); |
320 | daniel-mar | 132 | } else { |
360 | daniel-mar | 133 | throw new OIDplusException(_L('Invalid action ID')); |
320 | daniel-mar | 134 | } |
296 | daniel-mar | 135 | } |
328 | daniel-mar | 136 | |
639 | daniel-mar | 137 | OIDplus::invoke_shutdown(); |
138 | |||
328 | daniel-mar | 139 | @header('Content-Type:application/json; charset=utf-8'); |
140 | echo json_encode($json_out); |
||
141 | |||
1050 | daniel-mar | 142 | } catch (\Exception $e) { |
328 | daniel-mar | 143 | |
239 | daniel-mar | 144 | try { |
320 | daniel-mar | 145 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) { |
296 | daniel-mar | 146 | OIDplus::db()->transaction_rollback(); |
147 | } |
||
1050 | daniel-mar | 148 | } catch (\Exception $e1) { |
239 | daniel-mar | 149 | } |
256 | daniel-mar | 150 | |
575 | daniel-mar | 151 | $errmsg = $e->getMessage(); |
152 | |||
328 | daniel-mar | 153 | $json_out = array(); |
154 | $json_out['status'] = -2; |
||
575 | daniel-mar | 155 | $json_out['error'] = $errmsg; |
328 | daniel-mar | 156 | $out = json_encode($json_out); |
239 | daniel-mar | 157 | |
158 | if ($out === false) { |
||
159 | // Some modules (like ODBC) might output non-UTF8 data |
||
1046 | daniel-mar | 160 | $json_out['error'] = vts_utf8_encode($errmsg); |
328 | daniel-mar | 161 | $out = json_encode($json_out); |
239 | daniel-mar | 162 | } |
256 | daniel-mar | 163 | |
328 | daniel-mar | 164 | @header('Content-Type:application/json; charset=utf-8'); |
575 | daniel-mar | 165 | |
328 | daniel-mar | 166 | echo $out; |
424 | daniel-mar | 167 | } |