Subversion Repositories oidplus

Rev

Rev 427 | Rev 553 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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