Subversion Repositories oidplus

Rev

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

  1. function btoa(bin) {
  2.         var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  3.         var table = tableStr.split("");
  4.         for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
  5.                 var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
  6.                 if ((a | b | c) > 255) throw new Error("String contains an invalid character");
  7.                 base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
  8.                                        (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
  9.                                        (isNaN(b + c) ? "=" : table[c & 63]);
  10.         }
  11.         return base64.join("");
  12. };
  13.  
  14. function hexToBase64(str) {
  15.         return btoa(String.fromCharCode.apply(null,
  16.                     str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  17. }
  18.  
  19. function b64EncodeUnicode(str) {
  20.         // first we use encodeURIComponent to get percent-encoded UTF-8,
  21.         // then we convert the percent encodings into raw bytes which
  22.         // can be fed into btoa.
  23.         return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  24.         function toSolidBytes(match, p1) {
  25.                 return String.fromCharCode('0x' + p1);
  26.         }));
  27. }
  28.  
  29. function generateRandomString(length) {
  30.         var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  31.         retVal = "";
  32.         for (var i = 0, n = charset.length; i < length; ++i) {
  33.                 retVal += charset.charAt(Math.floor(Math.random() * n));
  34.         }
  35.         return retVal;
  36. }
  37.  
  38. String.prototype.replaceAll = function(search, replacement) {
  39.         var target = this;
  40.         return target.replace(new RegExp(search, 'g'), replacement);
  41. };
  42.  
  43. min_password_length = 5;
  44.  
  45. function rebuild() {
  46.         var error = false;
  47.  
  48.         // Check 1: Do the passwords match?
  49.         if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
  50.                 document.getElementById('password_warn2').innerHTML = '<font color="red">The passwords do not match!</font>';
  51.                 error = true;
  52.         } else {
  53.                 document.getElementById('password_warn2').innerHTML = '';
  54.         }
  55.  
  56.         // Check 2: Has the password the correct length?
  57.         if (document.getElementById('admin_password').value.length < min_password_length)
  58.         {
  59.                 document.getElementById('password_warn').innerHTML = '<font color="red">Password must be at least '+min_password_length+' characters long</font>';
  60.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br><i>// ERROR: Password must be at least '+min_password_length+' characters long</i>';
  61.                 error = true;
  62.         }
  63.         else
  64.         {
  65.                 document.getElementById('password_warn').innerHTML = '';
  66.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br>' +
  67.                         '<i>// To renew this file, please run setup/ in your browser.</i><br>' +
  68.                         '<i>// If you don\'t want to run setup again, you can also change most of the settings directly in this file.</i><br>' +
  69.                         '<br>' +
  70.                         '<b>define</b>(\'OIDPLUS_CONFIG_VERSION\',   0.1);<br>' +
  71.                         '<br>' +
  72.                         '<b>define</b>(\'OIDPLUS_ADMIN_PASSWORD\',   \'' + hexToBase64(sha3_512(document.getElementById('admin_password').value)) + '\'); // base64 encoded SHA3-512 hash<br>' +
  73.                         '<b>define</b>(\'OIDPLUS_ADMIN_EMAIL\',      \'' + document.getElementById('admin_email').value + '\');<br>' +
  74.                         '<br>' +
  75.                         '<b>define</b>(\'OIDPLUS_MYSQL_HOST\',       \''+document.getElementById('mysql_host').value+'\');<br>' +
  76.                         '<b>define</b>(\'OIDPLUS_MYSQL_USERNAME\',   \''+document.getElementById('mysql_username').value+'\');<br>' +
  77.                         '<b>define</b>(\'OIDPLUS_MYSQL_PASSWORD\',   \''+b64EncodeUnicode(document.getElementById('mysql_password').value)+'\'); // base64 encoded<br>' +
  78.                         '<b>define</b>(\'OIDPLUS_MYSQL_DATABASE\',   \''+document.getElementById('mysql_database').value+'\');<br>' +
  79.                         '<br>' +
  80.                         '<b>define</b>(\'OIDPLUS_TABLENAME_PREFIX\', \''+document.getElementById('tablename_prefix').value+'\');<br>' +
  81.                         '<br>' +
  82.                         '<b>define</b>(\'OIDPLUS_SESSION_SECRET\',   \''+generateRandomString(32)+'\');<br>' +
  83.                         '<br>' +
  84.                         '<b>define</b>(\'RECAPTCHA_ENABLED\',        '+(document.getElementById('recaptcha_enabled').checked ? 1 : 0)+');<br>' +
  85.                         '<b>define</b>(\'RECAPTCHA_PUBLIC\',         \''+document.getElementById('recaptcha_public').value+'\');<br>' +
  86.                         '<b>define</b>(\'RECAPTCHA_PRIVATE\',        \''+document.getElementById('recaptcha_private').value+'\');<br>';
  87.  
  88.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML.replaceAll(' ', '&nbsp;');
  89.         }
  90.  
  91.         // In case something is not good, do not allow the user to continue with the other configuration steps:
  92.         if (error) {
  93.                 document.getElementById('step2').style.visibility='hidden';
  94.                 document.getElementById('step3').style.visibility='hidden';
  95.                 document.getElementById('step4').style.visibility='hidden';
  96.         } else {
  97.                 document.getElementById('step2').style.visibility='visible';
  98.                 document.getElementById('step3').style.visibility='visible';
  99.                 document.getElementById('step4').style.visibility='visible';
  100.         }
  101.  
  102.         if (document.getElementById('tablename_prefix').value == '') {
  103.                 document.getElementById('struct_1').href = 'struct_empty.sql.php';
  104.                 document.getElementById('struct_2').href = 'struct_with_examples.sql.php';
  105.         } else {
  106.                 document.getElementById('struct_1').href = 'struct_empty.sql.php?prefix='+encodeURI(document.getElementById('tablename_prefix').value);
  107.                 document.getElementById('struct_2').href = 'struct_with_examples.sql.php?prefix='+encodeURI(document.getElementById('tablename_prefix').value);
  108.         }
  109. }
  110.