Subversion Repositories oidplus

Rev

Rev 511 | Rev 558 | 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 {
328 daniel-mar 74
                                $json_out = OIDplus::gui()::generateContentPage($_REQUEST['id']);
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
                        }
81
                } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) {
82
                        // Action:     tree_search
83
                        // Method:     GET / POST
84
                        // Parameters: search
85
                        // Outputs:    JSON
553 daniel-mar 86
                        _CheckParamExists($_REQUEST, 'search');
150 daniel-mar 87
 
320 daniel-mar 88
                        $found = false;
89
                        foreach (OIDplus::getPagePlugins() as $plugin) {
328 daniel-mar 90
                                $json_out = $plugin->tree_search($_REQUEST['search']);
91
                                if ($json_out) {
320 daniel-mar 92
                                        $found = true;
93
                                        break;
94
                                }
95
                        }
317 daniel-mar 96
 
320 daniel-mar 97
                        if (!$found) {
328 daniel-mar 98
                                $json_out = array();
320 daniel-mar 99
                        }
100
                } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) {
101
                        // Action:     tree_load
102
                        // Method:     GET / POST
103
                        // Parameters: id; goto (optional)
104
                        // Outputs:    JSON
553 daniel-mar 105
                        _CheckParamExists($_REQUEST, 'id');
328 daniel-mar 106
                        $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
320 daniel-mar 107
                } else {
360 daniel-mar 108
                        throw new OIDplusException(_L('Invalid action ID'));
320 daniel-mar 109
                }
296 daniel-mar 110
        }
328 daniel-mar 111
 
112
        @header('Content-Type:application/json; charset=utf-8');
113
        echo json_encode($json_out);
114
 
107 daniel-mar 115
} catch (Exception $e) {
328 daniel-mar 116
 
239 daniel-mar 117
        try {
320 daniel-mar 118
                if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) {
296 daniel-mar 119
                        OIDplus::db()->transaction_rollback();
120
                }
239 daniel-mar 121
        } catch (Exception $e1) {
122
        }
256 daniel-mar 123
 
328 daniel-mar 124
        $json_out = array();
125
        $json_out['status'] = -2;
126
        $json_out['error'] = $e->getMessage();
127
        $out = json_encode($json_out);
239 daniel-mar 128
 
129
        if ($out === false) {
130
                // Some modules (like ODBC) might output non-UTF8 data
328 daniel-mar 131
                $json_out['error'] = utf8_encode($e->getMessage());
132
                $out = json_encode($json_out);
239 daniel-mar 133
        }
256 daniel-mar 134
 
328 daniel-mar 135
        @header('Content-Type:application/json; charset=utf-8');
136
        echo $out;
424 daniel-mar 137
}