Subversion Repositories oidplus

Rev

Rev 1264 | Rev 1365 | 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. 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.                         } catch (\Exception $e) {
  95.                                 $json_out = array();
  96.                                 $json_out['title'] = _L('Error');
  97.                                 $json_out['icon'] = 'img/error.png';
  98.                                 $htmlmsg = $e instanceof OIDplusException ? $e->getHtmlMessage() : htmlentities($e->getMessage());
  99.                                 if (strtolower(substr($htmlmsg, 0, 3)) === '<p ') {
  100.                                         $json_out['text'] = $htmlmsg;
  101.                                 } else {
  102.                                         $json_out['text'] = '<p>'.$htmlmsg.'</p>';
  103.                                 }
  104.                         }
  105.                         $json_out['status'] = 0;
  106.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_search')) {
  107.                         // Action:     tree_search
  108.                         // Method:     GET / POST
  109.                         // Parameters: search
  110.                         // Outputs:    JSON
  111.                         _CheckParamExists($_REQUEST, 'search');
  112.  
  113.                         $found = false;
  114.                         foreach (OIDplus::getPagePlugins() as $plugin) {
  115.                                 $json_out = $plugin->tree_search($_REQUEST['search']);
  116.                                 if ($json_out) {
  117.                                         $found = true;
  118.                                         break;
  119.                                 }
  120.                         }
  121.  
  122.                         if (!$found) {
  123.                                 $json_out = array();
  124.                         }
  125.                 } else if (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'tree_load')) {
  126.                         // Action:     tree_load
  127.                         // Method:     GET / POST
  128.                         // Parameters: id; goto (optional)
  129.                         // Outputs:    JSON
  130.                         _CheckParamExists($_REQUEST, 'id');
  131.                         $_REQUEST['id'] = OIDplus::prefilterQuery($_REQUEST['id'], false);
  132.                         $json_out = OIDplus::menuUtils()->json_tree($_REQUEST['id'], $_REQUEST['goto'] ?? '');
  133.                 } else {
  134.                         throw new OIDplusException(_L('Invalid action ID'));
  135.                 }
  136.         }
  137.  
  138.         OIDplus::invoke_shutdown();
  139.  
  140.         @header('Content-Type:application/json; charset=utf-8');
  141.         echo json_encode($json_out);
  142.  
  143. } catch (\Exception $e) {
  144.  
  145.         try {
  146.                 if (!OIDplus::baseconfig()->getValue('DISABLE_AJAX_TRANSACTIONS',false) && OIDplus::db()->transaction_supported() && (OIDplus::db()->transaction_level() > 0)) {
  147.                         OIDplus::db()->transaction_rollback();
  148.                 }
  149.         } catch (\Exception $e1) {
  150.         }
  151.  
  152.         $errmsg = $e->getMessage();
  153.  
  154.         $json_out = array();
  155.         $json_out['status'] = -2;
  156.         $json_out['error'] = $errmsg;
  157.         $out = json_encode($json_out);
  158.  
  159.         if ($out === false) {
  160.                 // Some modules (like ODBC) might output non-UTF8 data
  161.                 $json_out['error'] = vts_utf8_encode($errmsg);
  162.                 $out = json_encode($json_out);
  163.         }
  164.  
  165.         @header('Content-Type:application/json; charset=utf-8');
  166.  
  167.         echo $out;
  168. }
  169.