Subversion Repositories oidplus

Rev

Rev 496 | Go to most recent revision | Blame | 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. // ATTENTION: If you change something, please make sure that the changes
  21. //            are synchronous with OIDplusPageAdminAutomatedAJAXCalls
  22.  
  23. if (!defined('INSIDE_OIDPLUS')) die();
  24.  
  25. class OIDplusPageRaAutomatedAJAXCalls extends OIDplusPagePluginRa {
  26.  
  27.         private static function getUnlockKey($user) {
  28.                 // This key prevents that the system gets hacked with brute
  29.                 // force of the user passwords.
  30.                 return sha3_512('ANTI-BRUTEFORCE-AJAX/'.$user.'/'.OIDplus::baseConfig()->getValue('SERVER_SECRET',''));
  31.         }
  32.  
  33.         private $autoLoginList = array();
  34.  
  35.         // Attention: Needs to be public, because otherwise register_shutdown_function() won't work
  36.         public function shutdownLogout() {
  37.                 foreach ($this->autoLoginList as $username) {
  38.                         OIDplus::authUtils()::raLogout($username);
  39.                 }
  40.         }
  41.  
  42.         public function init($html=true) {
  43.                 if (isset($_SERVER['SCRIPT_FILENAME']) && (basename($_SERVER['SCRIPT_FILENAME']) == 'ajax.php')) {
  44.                         $input = array_merge($_POST,$_GET);
  45.  
  46.                         if (isset($input['batch_ajax_unlock_key']) && isset($input['batch_login_username']) && isset($input['batch_login_password'])) {
  47.                                 originHeaders(); // Allows queries from other domains
  48.                                 OIDplus::authUtils()->disableCSRF(); // allow access to ajax.php without valid CSRF token
  49.  
  50.                                 if ($input['batch_login_username'] != 'admin') {
  51.                                         if ($input['batch_ajax_unlock_key'] != self::getUnlockKey($input['batch_login_username'])) {
  52.                                                 throw new OIDplusException(_L('Invalid AJAX unlock key'));
  53.                                         }
  54.  
  55.                                         if (OIDplus::authUtils()::raCheckPassword($input['batch_login_username'], $input['batch_login_password'])) {
  56.                                                 OIDplus::sesHandler()->simulate = true; // do not change the user session
  57.                                                 OIDplus::authUtils()::raLogin($input['batch_login_username']);
  58.                                                 $this->autoLoginList[] = $input['batch_login_username'];
  59.                                                 register_shutdown_function(array($this,'shutdownLogout'));
  60.                                         } else {
  61.                                                 throw new OIDplusException(_L('Wrong RA username or password'));
  62.                                         }
  63.                                 }
  64.                         }
  65.                 }
  66.         }
  67.  
  68.         public function gui($id, &$out, &$handled) {
  69.                 if (explode('$',$id)[0] == 'oidplus:automated_ajax_information_ra') {
  70.                         $handled = true;
  71.  
  72.                         $ra_email = explode('$',$id)[1];
  73.  
  74.                         $out['title'] = _L('Automated AJAX calls');
  75.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? OIDplus::webpath(__DIR__).'icon_big.png' : '';
  76.  
  77.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  78.                                 $out['icon'] = 'img/error_big.png';
  79.                                 $out['text'] = '<p>'._L('You need to <a %1>log in</a> as the requested RA %2 or as admin.',OIDplus::gui()->link('oidplus:login'),'<b>'.htmlentities($ra_email).'</b>').'</p>';
  80.                                 return;
  81.                         }
  82.  
  83.                         $out['text'] .= '<p>'._L('You can make automated calls to your OIDplus account by calling the AJAX API.').'</p>';
  84.                         $out['text'] .= '<p>'._L('The URL for the AJAX script is:').':</p>';
  85.                         $out['text'] .= '<p><b>'.OIDplus::webpath(null,false).'ajax.php</b></p>';
  86.                         $out['text'] .= '<p>'._L('You must at least provide following fields').':</p>';
  87.                         $out['text'] .= '<p><pre>';
  88.                         $out['text'] .= 'batch_login_username  = "'.htmlentities($ra_email).'"'."\n";
  89.                         $out['text'] .= 'batch_login_password  = "........."'."\n";
  90.                         $out['text'] .= 'batch_ajax_unlock_key = "'.$this->getUnlockKey($ra_email).'"'."\n";
  91.                         $out['text'] .= '</pre></p>';
  92.                         $out['text'] .= '<p>'._L('Please keep this information confidential!').'</p>';
  93.                         $out['text'] .= '<p>'._L('The batch-fields 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>';
  94.                         $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>';
  95.  
  96.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using JavaScript').'</h2>';
  97.                         $cont = file_get_contents(__DIR__.'/examples/example_js.html');
  98.                         $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
  99.                         $cont = str_replace('<username>', $ra_email, $cont);
  100.                         $cont = str_replace('<password>', '.........', $cont);
  101.                         $cont = str_replace('<unlock key>', $this->getUnlockKey($ra_email), $cont);
  102.                         $out['text'] .= '<pre>'.htmlentities($cont).'</pre>';
  103.  
  104.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using PHP (located at a foreign server)').'</h2>';
  105.                         $cont = file_get_contents(__DIR__.'/examples/example_php.phps');
  106.                         $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
  107.                         $cont = str_replace('<username>', $ra_email, $cont);
  108.                         $cont = str_replace('<password>', '.........', $cont);
  109.                         $cont = str_replace('<unlock key>', $this->getUnlockKey($ra_email), $cont);
  110.                         $out['text'] .= '<pre>'.preg_replace("@<br.*>@ismU","",highlight_string($cont,true)).'</pre>';
  111.  
  112.                         $out['text'] .= '<h2>'._L('Example for adding OID 2.999.123 using VBScript').'</h2>';
  113.                         $cont = file_get_contents(__DIR__.'/examples/example_vbs.vbs');
  114.                         $cont = str_replace('<url>', OIDplus::webpath(null,false).'ajax.php', $cont);
  115.                         $cont = str_replace('<username>', $ra_email, $cont);
  116.                         $cont = str_replace('<password>', '.........', $cont);
  117.                         $cont = str_replace('<unlock key>', $this->getUnlockKey($ra_email), $cont);
  118.                         $out['text'] .= '<pre>'.htmlentities($cont).'</pre>';
  119.                 }
  120.         }
  121.  
  122.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  123.                 if (!$ra_email) return false;
  124.                 if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) return false;
  125.  
  126.                 if (file_exists(__DIR__.'/treeicon.png')) {
  127.                         $tree_icon = OIDplus::webpath(__DIR__).'treeicon.png';
  128.                 } else {
  129.                         $tree_icon = null; // default icon (folder)
  130.                 }
  131.  
  132.                 $json[] = array(
  133.                         'id' => 'oidplus:automated_ajax_information_ra$'.$ra_email,
  134.                         'icon' => $tree_icon,
  135.                         'text' => _L('Automated AJAX calls')
  136.                 );
  137.  
  138.                 return true;
  139.         }
  140.  
  141.         public function tree_search($request) {
  142.                 return false;
  143.         }
  144. }
  145.