Subversion Repositories oidplus

Rev

Rev 78 | Rev 81 | Go to most recent revision | View as "text/javascript" | Blame | Compare with Previous | 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;
  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.         // Check 1: Do the passwords match?
  66.         if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
  67.                 document.getElementById('password_warn2').innerHTML = '<font color="red">The passwords do not match!</font>';
  68.                 error = true;
  69.         } else {
  70.                 document.getElementById('password_warn2').innerHTML = '';
  71.         }
  72.  
  73.         // Check 2: Has the password the correct length?
  74.         if (document.getElementById('admin_password').value.length < min_password_length)
  75.         {
  76.                 document.getElementById('password_warn').innerHTML = '<font color="red">Password must be at least '+min_password_length+' characters long</font>';
  77.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br><i>// ERROR: Password must be at least '+min_password_length+' characters long</i>';
  78.                 error = true;
  79.         }
  80.         else
  81.         {
  82.                 document.getElementById('password_warn').innerHTML = '';
  83.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br>' +
  84.                         '<i>// To renew this file, please run setup/ in your browser.</i><br>' +
  85.                         '<i>// If you don\'t want to run setup again, you can also change most of the settings directly in this file.</i><br>' +
  86.                         '<br>' +
  87.                         '<b>define</b>(\'OIDPLUS_CONFIG_VERSION\',   2.0);<br>' +
  88.                         '<br>' +
  89.                         '<b>define</b>(\'OIDPLUS_ADMIN_PASSWORD\',   \'' + hexToBase64(sha3_512(document.getElementById('admin_password').value)) + '\'); // base64 encoded SHA3-512 hash<br>' +
  90.                         '<br>' +
  91.                         '<b>define</b>(\'OIDPLUS_MYSQL_HOST\',       \''+document.getElementById('mysql_host').value+'\');<br>' +
  92.                         '<b>define</b>(\'OIDPLUS_MYSQL_USERNAME\',   \''+document.getElementById('mysql_username').value+'\');<br>' +
  93.                         '<b>define</b>(\'OIDPLUS_MYSQL_PASSWORD\',   \''+b64EncodeUnicode(document.getElementById('mysql_password').value)+'\'); // base64 encoded<br>' +
  94.                         '<b>define</b>(\'OIDPLUS_MYSQL_DATABASE\',   \''+document.getElementById('mysql_database').value+'\');<br>' +
  95.                         '<br>' +
  96.                         '<b>define</b>(\'OIDPLUS_TABLENAME_PREFIX\', \''+document.getElementById('tablename_prefix').value+'\');<br>' +
  97.                         '<br>' +
  98.                         '<b>define</b>(\'OIDPLUS_SESSION_SECRET\',   \''+generateRandomString(32)+'\');<br>' +
  99.                         '<br>' +
  100.                         '<b>define</b>(\'RECAPTCHA_ENABLED\',        '+(document.getElementById('recaptcha_enabled').checked ? 1 : 0)+');<br>' +
  101.                         '<b>define</b>(\'RECAPTCHA_PUBLIC\',         \''+document.getElementById('recaptcha_public').value+'\');<br>' +
  102.                         '<b>define</b>(\'RECAPTCHA_PRIVATE\',        \''+document.getElementById('recaptcha_private').value+'\');<br>' +
  103.                         '<br>' +
  104.                         '<b>define</b>(\'OIDPLUS_ENFORCE_SSL\',      \''+document.getElementById('enforce_ssl').value+'\');<br>';
  105.  
  106.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML.replaceAll(' ', '&nbsp;');
  107.         }
  108.  
  109.         // In case something is not good, do not allow the user to continue with the other configuration steps:
  110.         if (error) {
  111.                 document.getElementById('step2').style.visibility='hidden';
  112.                 document.getElementById('step3').style.visibility='hidden';
  113.                 document.getElementById('step4').style.visibility='hidden';
  114.         } else {
  115.                 document.getElementById('step2').style.visibility='visible';
  116.                 document.getElementById('step3').style.visibility='visible';
  117.                 document.getElementById('step4').style.visibility='visible';
  118.         }
  119.  
  120.         document.getElementById('struct_1').href = 'struct_empty.sql.php?prefix='+encodeURI(document.getElementById('tablename_prefix').value)+'&database='+encodeURI(document.getElementById('mysql_database').value);
  121.         document.getElementById('struct_2').href = 'struct_with_examples.sql.php?prefix='+encodeURI(document.getElementById('tablename_prefix').value)+'&database='+encodeURI(document.getElementById('mysql_database').value);
  122. }
  123.