Subversion Repositories oidplus

Rev

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