Subversion Repositories oidplus

Rev

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