Subversion Repositories oidplus

Rev

Rev 363 | 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. // DEFAULT_LANGUAGE will be set by setup.js.php
  19. // language_messages will be set by setup.js.php
  20. // language_tblprefix will be set by setup.js.php
  21.  
  22. min_password_length = 10; // see also plugins/publicPages/092_forgot_password_admin/script.js
  23.  
  24. function btoa(bin) {
  25.         var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  26.         var table = tableStr.split("");
  27.         for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
  28.                 var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
  29.                 if ((a | b | c) > 255) throw new Error(_L('String contains an invalid character'));
  30.                 base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
  31.                                        (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
  32.                                        (isNaN(b + c) ? "=" : table[c & 63]);
  33.         }
  34.         return base64.join("");
  35. };
  36.  
  37. function hexToBase64(str) {
  38.         return btoa(String.fromCharCode.apply(null,
  39.                     str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  40. }
  41.  
  42. function b64EncodeUnicode(str) {
  43.         // first we use encodeURIComponent to get percent-encoded UTF-8,
  44.         // then we convert the percent encodings into raw bytes which
  45.         // can be fed into btoa.
  46.         return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  47.         function toSolidBytes(match, p1) {
  48.                 return String.fromCharCode('0x' + p1);
  49.         }));
  50. }
  51.  
  52. function generateRandomString(length) {
  53.         var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  54.         retVal = "";
  55.         for (var i = 0, n = charset.length; i < length; ++i) {
  56.                 retVal += charset.charAt(Math.floor(Math.random() * n));
  57.         }
  58.         return retVal;
  59. }
  60.  
  61. String.prototype.replaceAll = function(search, replacement) {
  62.         var target = this;
  63.         return target.replace(new RegExp(search, 'g'), replacement);
  64. };
  65.  
  66. function rebuild() {
  67.         var error = false;
  68.  
  69.         if (document.getElementById('config') == null) return;
  70.  
  71.         // Check 1: Has the password the correct length?
  72.         if (document.getElementById('admin_password').value.length < min_password_length)
  73.         {
  74.                 document.getElementById('password_warn').innerHTML = '<font color="red">'+_L('Password must be at least %1 characters long',min_password_length)+'</font>';
  75.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br><i>// ERROR: Password must be at least '+min_password_length+' characters long</i>'; // do not translate
  76.                 error = true;
  77.         } else {
  78.                 document.getElementById('password_warn').innerHTML = '';
  79.         }
  80.  
  81.         // Check 2: Do the passwords match?
  82.         if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
  83.                 document.getElementById('password_warn2').innerHTML = '<font color="red">'+_L('The passwords do not match!')+'</font>';
  84.                 error = true;
  85.         } else {
  86.                 document.getElementById('password_warn2').innerHTML = '';
  87.         }
  88.  
  89.         // Check 3: Ask the database plugins for verification of their data
  90.         for (var i = 0; i < rebuild_callbacks.length; i++) {
  91.                 var f = rebuild_callbacks[i];
  92.                 if (!f()) {
  93.                         error = true;
  94.                 }
  95.         }
  96.  
  97.         // Continue
  98.         if (!error)
  99.         {
  100.                 var e = document.getElementById("db_plugin");
  101.                 var strPlugin = e.options[e.selectedIndex].value;
  102.  
  103.                 document.getElementById('config').innerHTML = '<b>&lt?php</b><br><br>' +
  104.                         '<i>// To renew this file, please run setup/ in your browser.</i><br>' + // do not translate
  105.                         '<i>// If you don\'t want to run setup again, you can also change most of the settings directly in this file.</i><br>' + // do not translate
  106.                         '<br>' +
  107.                         'OIDplus::baseConfig()->setValue(\'CONFIG_VERSION\',    2.1);<br>' +
  108.                         '<br>' +
  109.                         // Passwords are Base64 encoded to avoid that passwords can be read upon first sight,
  110.                         // e.g. if collegues are looking over your shoulder while you accidently open (and quickly close) userdata/baseconfig/config.inc.php
  111.                         'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\',    \'' + hexToBase64(sha3_512(document.getElementById('admin_password').value)) + '\'); // base64 encoded SHA3-512 hash<br>' +
  112.                         '<br>' +
  113.                         'OIDplus::baseConfig()->setValue(\'DATABASE_PLUGIN\',   \''+strPlugin+'\');<br>';
  114.                 for (var i = 0; i < rebuild_config_callbacks.length; i++) {
  115.                         var f = rebuild_config_callbacks[i];
  116.                         var cont = f();
  117.                         if (cont) {
  118.                                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML + cont;
  119.                         }
  120.                 }
  121.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML +
  122.                         '<br>' +
  123.                         'OIDplus::baseConfig()->setValue(\'TABLENAME_PREFIX\',  \''+document.getElementById('tablename_prefix').value+'\');<br>' +
  124.                         '<br>' +
  125.                         'OIDplus::baseConfig()->setValue(\'SERVER_SECRET\',     \''+generateRandomString(32)+'\');<br>' +
  126.                         '<br>' +
  127.                         'OIDplus::baseConfig()->setValue(\'RECAPTCHA_ENABLED\', '+(document.getElementById('recaptcha_enabled').checked ? 'true' : 'false')+');<br>' +
  128.                         'OIDplus::baseConfig()->setValue(\'RECAPTCHA_PUBLIC\',  \''+document.getElementById('recaptcha_public').value+'\');<br>' +
  129.                         'OIDplus::baseConfig()->setValue(\'RECAPTCHA_PRIVATE\', \''+document.getElementById('recaptcha_private').value+'\');<br>' +
  130.                         '<br>' +
  131.                         'OIDplus::baseConfig()->setValue(\'ENFORCE_SSL\',       '+document.getElementById('enforce_ssl').value+');<br>';
  132.  
  133.                 document.getElementById('config').innerHTML = document.getElementById('config').innerHTML.replaceAll(' ', '&nbsp;');
  134.         }
  135.  
  136.         // In case something is not good, do not allow the user to continue with the other configuration steps:
  137.         if (error) {
  138.                 document.getElementById('step2').style.display = "None";
  139.                 document.getElementById('step3').style.display = "None";
  140.                 document.getElementById('step4').style.display = "None";
  141.         } else {
  142.                 document.getElementById('step2').style.display = "Block";
  143.                 document.getElementById('step3').style.display = "Block";
  144.                 document.getElementById('step4').style.display = "Block";
  145.         }
  146. }
  147.  
  148. function RemoveLastDirectoryPartOf(the_url) {
  149.         var the_arr = the_url.split('/');
  150.         if (the_arr.pop() == '') the_arr.pop();
  151.         return( the_arr.join('/') );
  152. }
  153.  
  154. function checkAccess(dir) {
  155.         var url = '../' + dir;
  156.         var visibleUrl = RemoveLastDirectoryPartOf(window.location.href) + '/' + dir; // xhr.responseURL not available in IE
  157.  
  158.         var xhr = new XMLHttpRequest();
  159.         xhr.onreadystatechange = function() {
  160.                 if (xhr.readyState === 4) {
  161.                         if (xhr.status === 200) {
  162.                                 document.getElementById('systemCheckCaption').style.display = 'block';
  163.                                 document.getElementById('dirAccessWarning').innerHTML = document.getElementById('dirAccessWarning').innerHTML + _L('Attention: The following directory is world-readable: %1 ! You need to configure your web server to restrict access to this directory! (For Apache see <i>.htaccess</i>, for Microsoft IIS see <i>web.config</i>, for Nginx see <i>nginx.conf</i>).','<a target="_blank" href="'+url+'">'+visibleUrl+'</a>') + '<br>';
  164.                         }
  165.                 }
  166.         };
  167.  
  168.         xhr.open('GET', url);
  169.         xhr.send();
  170. }
  171.  
  172. function dbplugin_changed() {
  173.         var e = document.getElementById("db_plugin");
  174.         var strPlugin = e.options[e.selectedIndex].value;
  175.  
  176.         for (var i = 0; i < plugin_combobox_change_callbacks.length; i++) {
  177.                 var f = plugin_combobox_change_callbacks[i];
  178.                 f(strPlugin);
  179.         }
  180.  
  181.         rebuild();
  182. }
  183.  
  184. function performAccessCheck() {
  185.         document.getElementById("dirAccessWarning").innerHTML = "";
  186.         checkAccess("userdata/index.html");
  187.         checkAccess("dev/index.html");
  188.         checkAccess("includes/index.html");
  189.         //checkAccess("plugins/publicPages/100_whois/whois/cli/index.html");
  190. }
  191.  
  192. function setupOnLoad() {
  193.         rebuild();
  194.         dbplugin_changed();
  195.         performAccessCheck();
  196. }
  197.  
  198. function getCookie(cname) {
  199.         // Source: https://www.w3schools.com/js/js_cookies.asp
  200.         var name = cname + "=";
  201.         var decodedCookie = decodeURIComponent(document.cookie);
  202.         var ca = decodedCookie.split(';');
  203.         for(var i = 0; i <ca.length; i++) {
  204.                 var c = ca[i];
  205.                 while (c.charAt(0) == ' ') {
  206.                         c = c.substring(1);
  207.                 }
  208.                 if (c.indexOf(name) == 0) {
  209.                         return c.substring(name.length, c.length);
  210.                 }
  211.         }
  212.         return undefined;
  213. }
  214.  
  215. function getCurrentLang() {
  216.         // Note: If the argument "?lang=" is used, PHP will automatically set a Cookie, so it is OK when we only check for the cookie
  217.         var lang = getCookie('LANGUAGE');
  218.         return (typeof lang != "undefined") ? lang : DEFAULT_LANGUAGE;
  219. }
  220.  
  221. function _L() {
  222.         var args = Array.prototype.slice.call(arguments);
  223.         var str = args.shift();
  224.  
  225.         var tmp = "";
  226.         if (typeof language_messages[getCurrentLang()] == "undefined") {
  227.                 tmp = str;
  228.         } else {
  229.                 var msg = language_messages[getCurrentLang()][str];
  230.                 if (typeof msg != "undefined") {
  231.                         tmp = msg;
  232.                 } else {
  233.                         tmp = str;
  234.                 }
  235.         }
  236.  
  237.         tmp = tmp.replace('###', language_tblprefix);
  238.  
  239.         var n = 1;
  240.         while (args.length > 0) {
  241.                 var val = args.shift();
  242.                 tmp = tmp.replace("%"+n, val);
  243.                 n++;
  244.         }
  245.  
  246.         tmp = tmp.replace("%%", "%");
  247.  
  248.         return tmp;
  249. }
  250.  
  251. window.onload = setupOnLoad;
  252.