Subversion Repositories oidplus

Rev

Rev 149 | Go to most recent revision | View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * OIDplus 2.0
  3.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. min_password_length = 10; // see also plugins/publicPages/092_forgot_password_admin/script.js
  19.  
  20. function btoa(bin) {
  21.         var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  22.         var table = tableStr.split("");
  23.         for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
  24.                 var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
  25.                 if ((a | b | c) > 255) throw new Error("String contains an invalid character");
  26.                 base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
  27.                                        (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
  28.                                        (isNaN(b + c) ? "=" : table[c & 63]);
  29.         }
  30.         return base64.join("");
  31. };
  32.  
  33. function hexToBase64(str) {
  34.         return btoa(String.fromCharCode.apply(null,
  35.                     str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  36. }
  37.  
  38. function b64EncodeUnicode(str) {
  39.         // first we use encodeURIComponent to get percent-encoded UTF-8,
  40.         // then we convert the percent encodings into raw bytes which
  41.         // can be fed into btoa.
  42.         return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  43.         function toSolidBytes(match, p1) {
  44.                 return String.fromCharCode('0x' + p1);
  45.         }));
  46. }
  47.  
  48. function generateRandomString(length) {
  49.         var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  50.         retVal = "";
  51.         for (var i = 0, n = charset.length; i < length; ++i) {
  52.                 retVal += charset.charAt(Math.floor(Math.random() * n));
  53.         }
  54.         return retVal;
  55. }
  56.  
  57. String.prototype.replaceAll = function(search, replacement) {
  58.         var target = this;
  59.         return target.replace(new RegExp(search, 'g'), replacement);
  60. };
  61.  
  62. function rebuild() {
  63.         var error = false;
  64.  
  65.         if (document.getElementById('config') == null) return;
  66.  
  67.         // Check 1: Has the password the correct length?
  68.         if (document.getElementById('admin_password').value.length < min_password_length)
  69.         {
  70.                 document.getElementById('password_warn').innerHTML = '<font color="red">Password must be at least '+min_password_length+' characters long</font>';
  71.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br><i>// ERROR: Password must be at least '+min_password_length+' characters long</i>';
  72.                 error = true;
  73.         } else {
  74.                 document.getElementById('password_warn').innerHTML = '';
  75.         }
  76.  
  77.         // Check 2: Do the passwords match?
  78.         if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
  79.                 document.getElementById('password_warn2').innerHTML = '<font color="red">The passwords do not match!</font>';
  80.                 error = true;
  81.         } else {
  82.                 document.getElementById('password_warn2').innerHTML = '';
  83.         }
  84.  
  85.         // Check 3: Ask the database plugins for verification of their data
  86.         for (var i = 0; i < rebuild_callbacks.length; i++) {
  87.                 var f = rebuild_callbacks[i];
  88.                 if (!f()) {
  89.                         error = true;
  90.                 }
  91.         }
  92.  
  93.         // Continue
  94.         if (!error)
  95.         {
  96.                 var e = document.getElementById("db_plugin");
  97.                 var strPlugin = e.options[e.selectedIndex].value;
  98.  
  99.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br>' +
  100.                         '<i>// To renew this file, please run setup/ in your browser.</i><br>' +
  101.                         '<i>// If you don\'t want to run setup again, you can also change most of the settings directly in this file.</i><br>' +
  102.                         '<br>' +
  103.                         '<b>define</b>(\'OIDPLUS_CONFIG_VERSION\',   2.0);<br>' +
  104.                         '<br>' +
  105.                         // Passwords are Base64 encoded to avoid that passwords can be read upon first sight,
  106.                         // e.g. if collegues are looking over your shoulder while you accidently open (and quickly close) config.inc.php
  107.                         '<b>define</b>(\'OIDPLUS_ADMIN_PASSWORD\',   \'' + hexToBase64(sha3_512(document.getElementById('admin_password').value)) + '\'); // base64 encoded SHA3-512 hash<br>' +
  108.                         '<br>' +
  109.                         '<b>define</b>(\'OIDPLUS_DATABASE_PLUGIN\',  \''+strPlugin+'\');<br>';
  110.                 for (var i = 0; i < rebuild_config_callbacks.length; i++) {
  111.                         var f = rebuild_config_callbacks[i];
  112.                         var cont = f();
  113.                         if (cont) {
  114.                                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML + cont;
  115.                         }
  116.                 }
  117.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML +
  118.                         '<br>' +
  119.                         '<b>define</b>(\'OIDPLUS_TABLENAME_PREFIX\', \''+document.getElementById('tablename_prefix').value+'\');<br>' +
  120.                         '<br>' +
  121.                         '<b>define</b>(\'OIDPLUS_SESSION_SECRET\',   \''+generateRandomString(32)+'\');<br>' +
  122.                         '<br>' +
  123.                         '<b>define</b>(\'RECAPTCHA_ENABLED\',        '+(document.getElementById('recaptcha_enabled').checked ? 1 : 0)+');<br>' +
  124.                         '<b>define</b>(\'RECAPTCHA_PUBLIC\',         \''+document.getElementById('recaptcha_public').value+'\');<br>' +
  125.                         '<b>define</b>(\'RECAPTCHA_PRIVATE\',        \''+document.getElementById('recaptcha_private').value+'\');<br>' +
  126.                         '<br>' +
  127.                         '<b>define</b>(\'OIDPLUS_ENFORCE_SSL\',      \''+document.getElementById('enforce_ssl').value+'\');<br>';
  128.  
  129.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML.replaceAll(' ', '&nbsp;');
  130.         }
  131.  
  132.         // In case something is not good, do not allow the user to continue with the other configuration steps:
  133.         if (error) {
  134.                 document.getElementById('step2').style.display = "None";
  135.                 document.getElementById('step3').style.display = "None";
  136.                 document.getElementById('step4').style.display = "None";
  137.         } else {
  138.                 document.getElementById('step2').style.display = "Block";
  139.                 document.getElementById('step3').style.display = "Block";
  140.                 document.getElementById('step4').style.display = "Block";
  141.         }
  142. }
  143.