Subversion Repositories oidplus

Rev

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