Subversion Repositories oidplus

Rev

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