Rev 558 | Rev 573 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 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 | |||
20 | require_once __DIR__ . '/includes/oidplus.inc.php'; |
||
21 | |||
328 | daniel-mar | 22 | try { |
23 | OIDplus::init(false); |
||
107 | daniel-mar | 24 | |
328 | daniel-mar | 25 | $json_out = null; |
427 | daniel-mar | 26 | |
424 | daniel-mar | 27 | OIDplus::authUtils()->checkCSRF(); |
328 | daniel-mar | 28 | |
320 | daniel-mar | 29 | if (isset($_REQUEST['plugin']) && ($_REQUEST['plugin'] != '')) { |
317 | daniel-mar | 30 | |
320 | daniel-mar | 31 | // Actions handled by plugins |
107 | daniel-mar | 32 | |
320 | daniel-mar | 33 | $plugin = OIDplus::getPluginByOid($_REQUEST['plugin']); |
34 | if (!$plugin) { |
||
360 | daniel-mar | 35 | throw new OIDplusException(_L('Plugin with OID "%1" not found',$_REQUEST['plugin'])); |
107 | daniel-mar | 36 | } |
37 | |||
320 | daniel-mar | 38 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
39 | OIDplus::db()->transaction_begin(); |
||
40 | } |
||
108 | daniel-mar | 41 | |
328 | daniel-mar | 42 | $params = array(); |
43 | foreach (array_merge($_POST,$_GET) as $name => $val) { |
||
44 | if (($name != 'action') && ($name != 'plugin')) { |
||
45 | $params[$name] = $val; |
||
46 | } |
||
47 | } |
||
48 | |||
553 | daniel-mar | 49 | if (isset($_REQUEST['action']) && ($_REQUEST['action'] != '')) { |
50 | $json_out = $plugin->action($_REQUEST['action'], $params); |
||
51 | if (!is_array($json_out)) { |
||
52 | throw new OIDplusException(_L('Plugin with OID %1 did not output array of result data',$_REQUEST['plugin'])); |
||
53 | } |
||
54 | if (!isset($json_out['status'])) $json_out['status'] = -1; |
||
328 | daniel-mar | 55 | |
553 | daniel-mar | 56 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
57 | OIDplus::db()->transaction_commit(); |
||
58 | } |
||
59 | } else { |
||
60 | throw new OIDplusException(_L('Invalid action ID')); |
||
107 | daniel-mar | 61 | } |
108 | daniel-mar | 62 | |
320 | daniel-mar | 63 | } else { |
107 | daniel-mar | 64 | |
320 | daniel-mar | 65 | // Actions handled by the system (base functionality like the JS tree) |
107 | daniel-mar | 66 | |
320 | daniel-mar | 67 | if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) { |
68 | // Action: get_description |
||
69 | // Method: GET / POST |
||
70 | // Parameters: id |
||
71 | // Outputs: JSON |
||
553 | daniel-mar | 72 | _CheckParamExists($_REQUEST, 'id'); |
320 | daniel-mar | 73 | try { |
558 | daniel-mar | 74 | $json_out = OIDplus::gui()->generateContentPage($_REQUEST['id']); |
328 | daniel-mar | 75 | } catch (Exception $e) { |
76 | $json_out = array(); |
||
360 | daniel-mar | 77 | $json_out['title'] = _L('Error'); |
328 | daniel-mar | 78 | $json_out['icon'] = 'img/error_big.png'; |
79 | $json_out['text'] = $e->getMessage(); |
||
320 | daniel-mar | 80 | } |
560 | daniel-mar | 81 | $json_out['status'] = 0; |
320 | daniel-mar | 82 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) { |
83 | // Action: tree_search |
||
84 | // Method: GET / POST |
||
85 | // Parameters: search |
||
86 | // Outputs: JSON |
||
553 | daniel-mar | 87 | _CheckParamExists($_REQUEST, 'search'); |
150 | daniel-mar | 88 | |
320 | daniel-mar | 89 | $found = false; |
90 | foreach (OIDplus::getPagePlugins() as $plugin) { |
||
328 | daniel-mar | 91 | $json_out = $plugin->tree_search($_REQUEST['search']); |
92 | if ($json_out) { |
||
320 | daniel-mar | 93 | $found = true; |
94 | break; |
||
95 | } |
||
96 | } |
||
317 | daniel-mar | 97 | |
320 | daniel-mar | 98 | if (!$found) { |
328 | daniel-mar | 99 | $json_out = array(); |
320 | daniel-mar | 100 | } |
101 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) { |
||
102 | // Action: tree_load |
||
103 | // Method: GET / POST |
||
104 | // Parameters: id; goto (optional) |
||
105 | // Outputs: JSON |
||
553 | daniel-mar | 106 | _CheckParamExists($_REQUEST, 'id'); |
328 | daniel-mar | 107 | $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : ''); |
320 | daniel-mar | 108 | } else { |
360 | daniel-mar | 109 | throw new OIDplusException(_L('Invalid action ID')); |
320 | daniel-mar | 110 | } |
296 | daniel-mar | 111 | } |
328 | daniel-mar | 112 | |
113 | @header('Content-Type:application/json; charset=utf-8'); |
||
114 | echo json_encode($json_out); |
||
115 | |||
107 | daniel-mar | 116 | } catch (Exception $e) { |
328 | daniel-mar | 117 | |
239 | daniel-mar | 118 | try { |
320 | daniel-mar | 119 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) { |
296 | daniel-mar | 120 | OIDplus::db()->transaction_rollback(); |
121 | } |
||
239 | daniel-mar | 122 | } catch (Exception $e1) { |
123 | } |
||
256 | daniel-mar | 124 | |
328 | daniel-mar | 125 | $json_out = array(); |
126 | $json_out['status'] = -2; |
||
127 | $json_out['error'] = $e->getMessage(); |
||
128 | $out = json_encode($json_out); |
||
239 | daniel-mar | 129 | |
130 | if ($out === false) { |
||
131 | // Some modules (like ODBC) might output non-UTF8 data |
||
328 | daniel-mar | 132 | $json_out['error'] = utf8_encode($e->getMessage()); |
133 | $out = json_encode($json_out); |
||
239 | daniel-mar | 134 | } |
256 | daniel-mar | 135 | |
328 | daniel-mar | 136 | @header('Content-Type:application/json; charset=utf-8'); |
137 | echo $out; |
||
424 | daniel-mar | 138 | } |