Subversion Repositories oidplus

Rev

Rev 1046 | Rev 1116 | 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 (!is_array($json_out)) {
  65.                                 throw new OIDplusException(_L('Plugin with OID %1 did not output array of result data',$_REQUEST['plugin']));
  66.                         }
  67.                         if (!isset($json_out['status'])) $json_out['status'] = -1;
  68.  
  69.                         if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported()) {
  70.                                 OIDplus::db()->transaction_commit();
  71.                         }
  72.                 } else {
  73.                         throw new OIDplusException(_L('Invalid action ID'));
  74.                 }
  75.  
  76.         } else {
  77.  
  78.                 // Actions handled by the system (base functionality like the JS tree)
  79.  
  80.                 OIDplus::authUtils()->checkCSRF();
  81.  
  82.                 if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'get_description')) {
  83.                         // Action:     get_description
  84.                         // Method:     GET / POST
  85.                         // Parameters: id
  86.                         // Outputs:    JSON
  87.                         _CheckParamExists($_REQUEST, 'id');
  88.                         $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false);
  89.                         try {
  90.                                 $json_out = OIDplus::gui()->generateContentPage($_REQUEST['id']);
  91.                         } catch (\Exception $e) {
  92.                                 $json_out = array();
  93.                                 $json_out['title'] = _L('Error');
  94.                                 $json_out['icon'] = 'img/error.png';
  95.                                 $json_out['text'] = $e->getMessage();
  96.                         }
  97.                         $json_out['status'] = 0;
  98.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) {
  99.                         // Action:     tree_search
  100.                         // Method:     GET / POST
  101.                         // Parameters: search
  102.                         // Outputs:    JSON
  103.                         _CheckParamExists($_REQUEST, 'search');
  104.  
  105.                         $found = false;
  106.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  107.                                 $json_out = $plugin->tree_search($_REQUEST['search']);
  108.                                 if ($json_out) {
  109.                                         $found = true;
  110.                                         break;
  111.                                 }
  112.                         }
  113.  
  114.                         if (!$found) {
  115.                                 $json_out = array();
  116.                         }
  117.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) {
  118.                         // Action:     tree_load
  119.                         // Method:     GET / POST
  120.                         // Parameters: id; goto (optional)
  121.                         // Outputs:    JSON
  122.                         _CheckParamExists($_REQUEST, 'id');
  123.                         $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false);
  124.                         $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], isset($_REQUEST['goto']) ? $_REQUEST['goto'] : '');
  125.                 } else {
  126.                         throw new OIDplusException(_L('Invalid action ID'));
  127.                 }
  128.         }
  129.  
  130.         OIDplus::invoke_shutdown();
  131.  
  132.         @header('Content-Type:application/json; charset=utf-8');
  133.         echo json_encode($json_out);
  134.  
  135. } catch (\Exception $e) {
  136.  
  137.         try {
  138.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) {
  139.                         OIDplus::db()->transaction_rollback();
  140.                 }
  141.         } catch (\Exception $e1) {
  142.         }
  143.  
  144.         $errmsg = $e->getMessage();
  145.         $errmsg = strip_tags($errmsg);
  146.         $errmsg = html_entity_decode($errmsg, ENT_QUOTES, 'UTF-8');
  147.  
  148.         $json_out = array();
  149.         $json_out['status'] = -2;
  150.         $json_out['error'] = $errmsg;
  151.         $out = json_encode($json_out);
  152.  
  153.         if ($out === false) {
  154.                 // Some modules (like ODBC) might output non-UTF8 data
  155.                 $json_out['error'] = vts_utf8_encode($errmsg);
  156.                 $out = json_encode($json_out);
  157.         }
  158.  
  159.         @header('Content-Type:application/json; charset=utf-8');
  160.  
  161.         echo $out;
  162. }
  163.