Subversion Repositories oidplus

Rev

Rev 1130 | Rev 1205 | 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 - 2022 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. use ViaThinkSoft\OIDplus\OIDplus;
  21. use ViaThinkSoft\OIDplus\OIDplusException;
  22.  
  23. require_once __DIR__ . '/includes/oidplus.inc.php';
  24.  
  25. try {
  26.         OIDplus::init(false);
  27.  
  28.         if (isset($_GET['OIDPLUS_AUTH_JWT']) || isset($_POST['OIDPLUS_AUTH_JWT'])) {
  29.                 originHeaders(); // Allows queries from other domains
  30.                 OIDplus::authUtils()->disableCSRF(); // allow access to ajax.php without valid CSRF token
  31.         }
  32.  
  33.         $json_out = null;
  34.  
  35.         if (isset($_REQUEST['plugin']) && ($_REQUEST['plugin'] != '')) {
  36.  
  37.                 // Actions handled by plugins
  38.  
  39.                 $plugin = OIDplus::getPluginByOid($_REQUEST['plugin']);
  40.                 if (!$plugin) {
  41.                         throw new OIDplusException(_L('Plugin with OID "%1" not found',$_REQUEST['plugin']));
  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.                 if (isset($_REQUEST['action']) && ($_REQUEST['action'] != '')) {
  52.                         if ($plugin->csrfUnlock($_REQUEST['action'])) {
  53.                                 originHeaders(); // Allows queries from other domains
  54.                                 OIDplus::authUtils()->disableCSRF(); // allow access to ajax.php without valid CSRF token
  55.                         }
  56.  
  57.                         OIDplus::authUtils()->checkCSRF();
  58.  
  59.                         if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) {
  60.                                 OIDplus::db()->transaction_begin();
  61.                         }
  62.  
  63.                         $json_out = $plugin->action($_REQUEST['action'], $params);
  64.                         if (!isset($json_out['status'])) $json_out['status'] = -1;
  65.  
  66.                         if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) {
  67.                                 OIDplus::db()->transaction_commit();
  68.                         }
  69.                 } else {
  70.                         throw new OIDplusException(_L('Invalid action ID'));
  71.                 }
  72.  
  73.         } else {
  74.  
  75.                 // Actions handled by the system (base functionality like the JS tree)
  76.  
  77.                 OIDplus::authUtils()->checkCSRF();
  78.  
  79.                 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) {
  80.                         // Action:     get_description
  81.                         // Method:     GET / POST
  82.                         // Parameters: id
  83.                         // Outputs:    JSON
  84.                         _CheckParamExists($_REQUEST, 'id');
  85.                         $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false);
  86.                         try {
  87.                                 $json_out = OIDplus::gui()->generateContentPage($_REQUEST['id']);
  88.                         } catch (\Exception $e) {
  89.                                 $json_out = array();
  90.                                 $json_out['title'] = _L('Error');
  91.                                 $json_out['icon'] = 'img/error.png';
  92.                                 $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  93.                                 $json_out['text'] = '<p>'.$htmlmsg.'</p>';
  94.                         }
  95.                         $json_out['status'] = 0;
  96.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) {
  97.                         // Action:     tree_search
  98.                         // Method:     GET / POST
  99.                         // Parameters: search
  100.                         // Outputs:    JSON
  101.                         _CheckParamExists($_REQUEST, 'search');
  102.  
  103.                         $found = false;
  104.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  105.                                 $json_out = $plugin->tree_search($_REQUEST['search']);
  106.                                 if ($json_out) {
  107.                                         $found = true;
  108.                                         break;
  109.                                 }
  110.                         }
  111.  
  112.                         if (!$found) {
  113.                                 $json_out = array();
  114.                         }
  115.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) {
  116.                         // Action:     tree_load
  117.                         // Method:     GET / POST
  118.                         // Parameters: id; goto (optional)
  119.                         // Outputs:    JSON
  120.                         _CheckParamExists($_REQUEST, 'id');
  121.                         $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false);
  122.                         $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], $_REQUEST['goto'] ?? '');
  123.                 } else {
  124.                         throw new OIDplusException(_L('Invalid action ID'));
  125.                 }
  126.         }
  127.  
  128.         OIDplus::invoke_shutdown();
  129.  
  130.         @header('Content-Type:application/json; charset=utf-8');
  131.         echo json_encode($json_out);
  132.  
  133. } catch (\Exception $e) {
  134.  
  135.         try {
  136.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) {
  137.                         OIDplus::db()->transaction_rollback();
  138.                 }
  139.         } catch (\Exception $e1) {
  140.         }
  141.  
  142.         $errmsg = $e->getMessage();
  143.  
  144.         $json_out = array();
  145.         $json_out['status'] = -2;
  146.         $json_out['error'] = $errmsg;
  147.         $out = json_encode($json_out);
  148.  
  149.         if ($out === false) {
  150.                 // Some modules (like ODBC) might output non-UTF8 data
  151.                 $json_out['error'] = vts_utf8_encode($errmsg);
  152.                 $out = json_encode($json_out);
  153.         }
  154.  
  155.         @header('Content-Type:application/json; charset=utf-8');
  156.  
  157.         echo $out;
  158. }
  159.