Rev 317 | Rev 321 | 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 |
||
5 | * Copyright 2019 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 | require_once __DIR__ . '/includes/oidplus.inc.php'; |
||
21 | |||
22 | OIDplus::init(false); |
||
23 | |||
24 | header('Content-Type:application/json; charset=utf-8'); |
||
25 | |||
26 | try { |
||
320 | daniel-mar | 27 | if (!isset($_REQUEST['action'])) throw new OIDplusException("Action ID is missing"); |
107 | 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) { |
||
35 | throw new OIDplusException("Plugin with OID '".$_REQUEST['plugin']."' not found"); |
||
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 | |
320 | daniel-mar | 42 | $plugin->actionBefore(); |
43 | $handled = false; |
||
44 | $plugin->action($handled); |
||
45 | if (!$handled) OIDplusException('Invalid action ID'); |
||
46 | $plugin->actionAfter(); |
||
108 | daniel-mar | 47 | |
320 | daniel-mar | 48 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) { |
49 | OIDplus::db()->transaction_commit(); |
||
107 | daniel-mar | 50 | } |
108 | daniel-mar | 51 | |
320 | daniel-mar | 52 | } else { |
107 | daniel-mar | 53 | |
320 | daniel-mar | 54 | // Actions handled by the system (base functionality like the JS tree) |
107 | daniel-mar | 55 | |
320 | daniel-mar | 56 | if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) { |
57 | // Action: get_description |
||
58 | // Method: GET / POST |
||
59 | // Parameters: id |
||
60 | // Outputs: JSON |
||
61 | $handled = true; |
||
62 | if (!isset($_REQUEST['id'])) throw new OIDplusException("Invalid args"); |
||
63 | try { |
||
64 | $out = OIDplus::gui()::generateContentPage($_REQUEST['id']); |
||
65 | } catch(Exception $e) { |
||
66 | $out = array(); |
||
67 | $out['title'] = 'Error'; |
||
68 | $out['icon'] = 'img/error_big.png'; |
||
69 | $out['text'] = $e->getMessage(); |
||
70 | } |
||
71 | echo json_encode($out); |
||
72 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) { |
||
73 | // Action: tree_search |
||
74 | // Method: GET / POST |
||
75 | // Parameters: search |
||
76 | // Outputs: JSON |
||
77 | $handled = true; |
||
78 | if (!isset($_REQUEST['search'])) throw new OIDplusException("Invalid args"); |
||
150 | daniel-mar | 79 | |
320 | daniel-mar | 80 | $found = false; |
81 | foreach (OIDplus::getPagePlugins() as $plugin) { |
||
82 | $res = $plugin->tree_search($_REQUEST['search']); |
||
83 | if ($res) { |
||
84 | echo json_encode($res); |
||
85 | $found = true; |
||
86 | break; |
||
87 | } |
||
88 | } |
||
317 | daniel-mar | 89 | |
320 | daniel-mar | 90 | if (!$found) { |
91 | echo json_encode(array()); |
||
92 | } |
||
93 | } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) { |
||
94 | // Action: tree_load |
||
95 | // Method: GET / POST |
||
96 | // Parameters: id; goto (optional) |
||
97 | // Outputs: JSON |
||
98 | $handled = true; |
||
99 | if (!isset($_REQUEST['id'])) throw new OIDplusException("Invalid args"); |
||
100 | $json = OIDplus::menuUtils()->json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : ''); |
||
101 | echo $json; |
||
102 | } else { |
||
103 | throw new OIDplusException('Invalid action ID'); |
||
104 | } |
||
296 | daniel-mar | 105 | } |
107 | daniel-mar | 106 | } catch (Exception $e) { |
239 | daniel-mar | 107 | try { |
320 | daniel-mar | 108 | if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) { |
296 | daniel-mar | 109 | OIDplus::db()->transaction_rollback(); |
110 | } |
||
239 | daniel-mar | 111 | } catch (Exception $e1) { |
112 | } |
||
256 | daniel-mar | 113 | |
107 | daniel-mar | 114 | $ary = array(); |
115 | $ary['error'] = $e->getMessage(); |
||
239 | daniel-mar | 116 | $out = json_encode($ary); |
117 | |||
118 | if ($out === false) { |
||
119 | // Some modules (like ODBC) might output non-UTF8 data |
||
120 | $ary['error'] = utf8_encode($e->getMessage()); |
||
121 | $out = json_encode($ary); |
||
122 | } |
||
256 | daniel-mar | 123 | |
239 | daniel-mar | 124 | die($out); |
107 | daniel-mar | 125 | } |