Subversion Repositories oidplus

Rev

Rev 1143 | Rev 1206 | 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 - 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. // ATTENTION: If you change something, please make sure that the changes
  21. //            are synchronous with OIDplusPageRaAutomatedAJAXCalls
  22.  
  23. namespace ViaThinkSoft\OIDplus;
  24.  
  25. // phpcs:disable PSR1.Files.SideEffects
  26. \defined('INSIDE_OIDPLUS') or die;
  27. // phpcs:enable PSR1.Files.SideEffects
  28.  
  29. class OIDplusPageAdminAutomatedAJAXCalls extends OIDplusPagePluginAdmin {
  30.  
  31.         /**
  32.          * @param string $actionID
  33.          * @param array $params
  34.          * @return array
  35.          * @throws OIDplusException
  36.          */
  37.         public function action(string $actionID, array $params): array {
  38.                 if ($actionID == 'blacklistJWT') {
  39.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  40.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')));
  41.                         }
  42.  
  43.                         if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
  44.                                 throw new OIDplusException(_L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN'));
  45.                         }
  46.  
  47.                         $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
  48.                         $sub = 'admin';
  49.  
  50.                         OIDplusAuthContentStoreJWT::jwtBlacklist($gen, $sub);
  51.  
  52.                         return array("status" => 0);
  53.                 } else {
  54.                         return parent::action($actionID, $params);
  55.                 }
  56.         }
  57.  
  58.         /**
  59.          * @param string $id
  60.          * @param array $out
  61.          * @param bool $handled
  62.          * @return void
  63.          * @throws OIDplusException
  64.          */
  65.         public function gui(string $id, array &$out, bool &$handled) {
  66.                 if ($id === 'oidplus:automated_ajax_information_admin') {
  67.                         $handled = true;
  68.                         $out['title'] = _L('Automated AJAX calls');
  69.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  70.  
  71.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  72.                                 $out['icon'] = 'img/error.png';
  73.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  74.                                 return;
  75.                         }
  76.  
  77.                         if (!OIDplus::baseConfig()->getValue('JWT_ALLOW_AJAX_ADMIN', true)) {
  78.                                 $out['text'] = '<p>'._L('The administrator has disabled this feature. (Base configuration setting %1).','JWT_ALLOW_AJAX_ADMIN').'</p>';
  79.                                 return;
  80.                         }
  81.  
  82.                         $gen = OIDplusAuthContentStoreJWT::JWT_GENERATOR_AJAX;
  83.                         $sub = 'admin';
  84.  
  85.                         $authSimulation = new OIDplusAuthContentStoreJWT();
  86.                         $authSimulation->adminLogin();
  87.                         $authSimulation->setValue('oidplus_generator', $gen);
  88.                         $token = $authSimulation->getJWTToken();
  89.  
  90.                         $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
  91.                         $out['text'] .= '<p>'._L('The URL for the AJAX script is:').'</p>';
  92.                         $out['text'] .= '<p><b>'.OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php</b></p>';
  93.                         $out['text'] .= '<p>'._L('You must at least provide following fields:').'</p>';
  94.                         $out['text'] .= '<p><pre id="oidplus_auth_jwt">';
  95.                         $out['text'] .= 'OIDPLUS_AUTH_JWT = "'.htmlentities($token).'"'."\n";
  96.                         $out['text'] .= '</pre></p>';
  97.                         $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(oidplus_auth_jwt)"></p>';
  98.                         $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
  99.                         $out['text'] .= '<p>'._L('The JWT-token (secret!) will automatically perform a one-time-login to fulfill the request. The other fields are the normal fields which are called during the usual operation of OIDplus.').'</p>';
  100.                         $out['text'] .= '<p>'._L('Currently, there is no documentation for the AJAX calls. However, you can look at the <b>script.js</b> files of the plugins to see the field names being used. You can also enable network analysis in your web browser debugger (F12) to see the request headers sent to the server during the operation of OIDplus.').'</p>';
  101.  
  102.                         $out['text'] .= '<h2>'._L('Blacklisted tokens').'</h2>';
  103.                         $bl_time = OIDplusAuthContentStoreJWT::jwtGetBlacklistTime($gen, $sub);
  104.                         if ($bl_time == 0) {
  105.                                 $out['text'] .= '<p>'._L('None of the previously generated JWT tokens have been blacklisted.').'</p>';
  106.                         } else {
  107.                                 $out['text'] .= '<p>'._L('All tokens generated before %1 have been blacklisted.',date('d F Y, H:i:s',$bl_time+1)).'</p>';
  108.                         }
  109.                         $out['text'] .= '<button type="button" name="btn_blacklist_jwt" id="btn_blacklist_jwt" class="btn btn-danger btn-xs" onclick="OIDplusPageAdminAutomatedAJAXCalls.blacklistJWT()">'._L('Blacklist all previously generated tokens').'</button>';
  110.  
  111.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
  112.                         $cont = file_get_contents(__DIR__.'/examples/example_js.html');
  113.                         $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
  114.                         $cont = str_replace('<token>', $token, $cont);
  115.                         $out['text'] .= '<pre id="example_js">'.htmlentities($cont).'</pre>';
  116.                         $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_js)"></p>';
  117.  
  118.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
  119.                         $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
  120.                         $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
  121.                         $cont = str_replace('<token>', $token, $cont);
  122.                         $out['text'] .= '<pre id="example_php">'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
  123.                         $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_php)"></p>';
  124.  
  125.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using Python').'</h2>';
  126.                         $cont = file_get_contents(__DIR__.'/examples/example_python.py');
  127.                         $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
  128.                         $cont = str_replace('<token>', $token, $cont);
  129.                         $out['text'] .= '<pre id="example_python">'.htmlentities($cont).'</pre>';
  130.                         $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_python)"></p>';
  131.  
  132.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
  133.                         $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
  134.                         $cont = str_replace('<url>', OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).'ajax.php', $cont);
  135.                         $cont = str_replace('<token>', $token, $cont);
  136.                         $out['text'] .= '<pre id="example_vbs">'.htmlentities($cont).'</pre>';
  137.                         $out['text'] .= '<p><input type="button" value="'._L('Copy to clipboard').'" onClick="copyToClipboard(example_vbs)"></p>';
  138.                 }
  139.         }
  140.  
  141.         /**
  142.          * @param array $json
  143.          * @param string|null $ra_email
  144.          * @param bool $nonjs
  145.          * @param string $req_goto
  146.          * @return bool
  147.          * @throws OIDplusException
  148.          */
  149.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  150.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  151.  
  152.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  153.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  154.                 } else {
  155.                         $tree_icon = null; // default icon (folder)
  156.                 }
  157.  
  158.                 $json[] = array(
  159.                         'id' => 'oidplus:automated_ajax_information_admin',
  160.                         'icon' => $tree_icon,
  161.                         'text' => _L('Automated AJAX calls')
  162.                 );
  163.  
  164.                 return true;
  165.         }
  166.  
  167.         /**
  168.          * @param string $request
  169.          * @return array|false
  170.          */
  171.         public function tree_search(string $request) {
  172.                 return false;
  173.         }
  174. }
  175.