Subversion Repositories oidplus

Rev

Rev 1365 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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