Subversion Repositories oidplus

Rev

Rev 320 | Rev 328 | 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 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 {
  27.         if (!isset($_REQUEST['action'])) throw new OIDplusException("Action ID is missing");
  28.  
  29.         if (isset($_REQUEST['plugin']) && ($_REQUEST['plugin'] != '')) {
  30.  
  31.                 // Actions handled by plugins
  32.  
  33.                 $plugin = OIDplus::getPluginByOid($_REQUEST['plugin']);
  34.                 if (!$plugin) {
  35.                         throw new OIDplusException("Plugin with OID '".$_REQUEST['plugin']."' not found");
  36.                 }
  37.  
  38.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) {
  39.                         OIDplus::db()->transaction_begin();
  40.                 }
  41.                
  42.                 $params = $_REQUEST;
  43.                 unset($params['action']);
  44.                 $plugin->action($_REQUEST['action'], $params);
  45.  
  46.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) {
  47.                         OIDplus::db()->transaction_commit();
  48.                 }
  49.  
  50.         } else {
  51.  
  52.                 // Actions handled by the system (base functionality like the JS tree)
  53.  
  54.                 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) {
  55.                         // Action:     get_description
  56.                         // Method:     GET / POST
  57.                         // Parameters: id
  58.                         // Outputs:    JSON
  59.                         $handled = true;
  60.                         if (!isset($_REQUEST['id'])) throw new OIDplusException("Invalid args");
  61.                         try {
  62.                                 $out = OIDplus::gui()::generateContentPage($_REQUEST['id']);
  63.                         } catch(Exception $e) {
  64.                                 $out = array();
  65.                                 $out['title'] = 'Error';
  66.                                 $out['icon'] = 'img/error_big.png';
  67.                                 $out['text'] = $e->getMessage();
  68.                         }
  69.                         echo json_encode($out);
  70.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) {
  71.                         // Action:     tree_search
  72.                         // Method:     GET / POST
  73.                         // Parameters: search
  74.                         // Outputs:    JSON
  75.                         $handled = true;
  76.                         if (!isset($_REQUEST['search'])) throw new OIDplusException("Invalid args");
  77.  
  78.                         $found = false;
  79.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  80.                                 $res = $plugin->tree_search($_REQUEST['search']);
  81.                                 if ($res) {
  82.                                         echo json_encode($res);
  83.                                         $found = true;
  84.                                         break;
  85.                                 }
  86.                         }
  87.  
  88.                         if (!$found) {
  89.                                 echo json_encode(array());
  90.                         }
  91.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) {
  92.                         // Action:     tree_load
  93.                         // Method:     GET / POST
  94.                         // Parameters: id; goto (optional)
  95.                         // Outputs:    JSON
  96.                         $handled = true;
  97.                         if (!isset($_REQUEST['id'])) throw new OIDplusException("Invalid args");
  98.                         $json = OIDplus::menuUtils()->json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
  99.                         echo $json;
  100.                 } else {
  101.                         throw new OIDplusException('Invalid action ID');
  102.                 }
  103.         }
  104. } catch (Exception $e) {
  105.         try {
  106.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) {
  107.                         OIDplus::db()->transaction_rollback();
  108.                 }
  109.         } catch (Exception $e1) {
  110.         }
  111.  
  112.         $ary = array();
  113.         $ary['error'] = $e->getMessage();
  114.         $out = json_encode($ary);
  115.  
  116.         if ($out === false) {
  117.                 // Some modules (like ODBC) might output non-UTF8 data
  118.                 $ary['error'] = utf8_encode($e->getMessage());
  119.                 $out = json_encode($ary);
  120.         }
  121.  
  122.         die($out);
  123. }
  124.