<?php
/*******************************************************************************
 *   Install initialization
 *******************************************************************************
 *   GuppY PHP Script - version 6.0
 *   CeCILL Copyright (C) 2004-2020 by Laurent Duveau
 *   Initiated by Laurent Duveau and Nicolas Alves
 *   Web site = https://www.freeguppy.org/
 *   e-mail   = guppy@freeguppy.org
 *   V6 developed by Lud Bienaimé
 *      with the participation of Jean-Michel Misrachi and the GuppY Team
 *******************************************************************************
 *   Latest Changes :
 * v6.00.10 (September 22, 2022) : correction $cryptpass
 ******************************************************************************/

if (stristr($_SERVER['SCRIPT_NAME'], 'init.inc')) {
    header('location:../index.php');
    die();
}

$ctrl_spam = import('code_pseudo', 'POST');
$apseudo   = trim(import('apseudo', 'POST'));
$apseudo   = 1 == preg_match('/^[-a-zA-Z0-9_]{2,'.$serviz[148].'}$/', $apseudo) ? $apseudo : '';
$apass     = import('apass', 'POST', '');
$apass     = !ctrlPasswordOK($apass) ? '' : $apass;
$aemail    = checkEmail(CutLongWord(import('aemail', 'POST', '')));
if (!empty($apseudo) && !empty($apass) && !empty($aemail) && empty($ctrl_spam)) {
//    $cryptpass = crypt($uuid);
		$apachepassword  = Md5Crypt::apache ($uuid, $salt ='');
		$cryptpass     = $apachepassword;
    if (is_file(CHEMIN.'data/config/.htpasswd')) {
        $htpasswd = @file(CHEMIN.'data/config/.htpasswd');
        $protoht  = trim($htpasswd[0]);
        if (!empty($protoht)) $cryptpass = $protoht == '#:0-1-0' ? gy_crypt(CRYPT_KEY, $uuid) : crypt($uuid);
    }
    $userdb        = array();
    $userdb[0][0]  = md5($apass);
 //   $userdb[0][1]  = crypt($apass);
	$userdb[0][1]  =$cryptpass;
    $userdb[1][0]  = '0';
    $userdb[1][1]  = $lng;   
    $userdb[1][2]  = $apseudo;
    $userdb[1][3]  = $aemail;
    $userdb[1][4]  = '';
    $userdb[1][5]  = '';
    $userdb[1][6]  = '';
    $userdb[1][7]  = '';
    $userdb[1][8]  = md5($apass);
    $userdb[1][9]  = '';
    $userdb[1][10] = '';
    $userdb[1][11] = '';
    $userdb[1][12] = '';
    $userdb[1][13] = 'on';
    $userdb[1][14] = GetCurrentDateTime();
    $userdb[1][15] = GetCurrentDateTime();
    $userdb[1][16] = '';
    $userdb[1][17] = '';
    $userdb[1][18] = '';
    $userdb[1][19] = '';
    $userdb[1][20] = '';
    $userdb[1][21] = '';
    $userdb[1][22] = '';
    $userdb[1][23] = '';
    $userdb[1][24] = '';
    $userdb[1][25] = '';
    $userdb[1][26] = '';
    $userdb[1][27] = '';
    $userdb[1][28] = date('Ymd');
    $userdb[1][29] = '';
    $userdb[1][30] = '';
    $userdb[1][31] = 'ok';
    $userdb[2][0]  = $apseudo.CONNECTOR.GetCurrentDateTime().CONNECTOR.'&nbsp; '.$web[253].' '
        .sprintf($web[167], stripslashes($site[($lang[0] == $lng ? 0 : 11)]), $upseudo).CONNECTOR.'new'
        .CONNECTOR.CONNECTOR.CONNECTOR.CONNECTOR.$serviz[43].CONNECTOR.substr($page[23], 4);
    WriteDBFields(MSGREP.$apseudo.DBEXT, $userdb);
    include CONFIG;
    $dirname    = dirname($_SERVER['PHP_SELF']);
    $dirname    = substr($dirname, -1) == '/' ? $dirname : $dirname.'/';
    $site[3]    = (is_ssl() ? 'https://' : 'http://').$_SERVER['HTTP_HOST'].$dirname;
    $serviz[31] = $apseudo;
    include 'admin/inc/confupdt.inc';
    if (isset($_COOKIE[POST_COOKIE])) @setcookie(POST_COOKIE, '', -1, '/', $_DOMAIN, $_SECURE, $_HTTPONLY);
    echo BeginJavascript().'window.location="'.CHEMIN.'connect.php?lng='.$lng.'#z2";'.EndJavascript();
}
echo '
<style>
.modal {
  display: block;
  position: fixed;
  z-index: 99;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0,0,0,0.5);
}
.modal-content {
  background-color: yellow;
  text-align: left;
  width: 420px;
  max-width: 85%;
  margin: auto;
  padding: 10px 20px;
  border: 2px solid #888;
}
.title { margin: 30px 20px; }
.height { margin-top: 8px; }
.usr-password {
  font-size: 9px;
  text-transform: uppercase;
  position: absolute;
  cursor: pointer;
  margin: 6px 0 0 -20px;
}
.infomodal {
  color: #FF0000;
  font-weight: bold;
  text-align: center;
}
.pass button.btn { font-size: 1.62rem; }
.user .input-group-text {
  font-size: 16px;
  line-height: 0.6;
  margin-top: 4px;
}
.menuicon_flag { z-index: 0; }
</style>

<script>
$(document).ready(function(){
  $("#testuid").on("keyup", function(e) {
    var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\\\W).*$", "ig");
    var mediumRegex = new RegExp("^(?=.{8,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
    var okRegex = new RegExp("(?=.{8,}).*", "g");
    if (okRegex.test($(this).val()) === false) {
      $("#testuid-strength").removeClass().addClass("alert alert-error").html("'.$help[21].'");
    } else if (strongRegex.test($(this).val())) {
      $("#testuid-strength").removeClass().addClass("alert alert-success").html("'.$help[22].'");
    } else if (mediumRegex.test($(this).val())) {
      $("#testuid-strength").removeClass().addClass("alert alert-info").html("'.$help[23].'");
    } else {
      $("#testuid-strength").removeClass().addClass("alert alert-error").html("'.$help[24].'");
    }
    return true;
  });
});
function VerifyFormAdmin() {
	var sto = "";
	var erreur = false;
	var pass   = document.useradmin.apass.value;
	var pass2  = document.useradmin.apass2.value;
	regexp = /^[-a-zA-Z0-9_]{2,'.$serviz[148].'}$/;
	if (!regexp.test(document.useradmin.apseudo.value)) {
		sto += "  - '.str_replace('"', '\"', stripslashes($web[266])).'\n";
		document.useradmin.apseudo.className = "errorInputText form-control";
		erreur = true;
	}
    regexp = /^\S{8,40}$/;
    if (!regexp.test(pass) || pass.length < 8) {
      sto += "  - '.str_replace('"', '\"', stripslashes($web[302])).'\n";
      document.useradmin.apass.className = "errorInputText form-control";
      erreur = true;
    }
	if (pass != pass2) {
		sto += "  - '.str_replace('"', '\"', stripslashes($web[466])).'\n";
		document.useradmin.apass.className = "errorInputText form-control";
		document.useradmin.apass2.className = "errorInputText form-control";
		erreur = true;
	}
	regexp = /^[^\.\s]+(\.[^\.\s]+)*@[^\.\s]+(\.[^\.\s]+)+$/;
	if (!regexp.test(document.useradmin.aemail.value)) {
		sto += "  - '.str_replace('"', '\"', stripslashes($web[42])).'\n";
		document.useradmin.aemail.className = "errorInputText form-control";
		erreur = true;
	}
	if (erreur) {
        sto = "'.str_replace('"', '\"', stripslashes($web[265])).'\n\n" + sto;
		alert(sto);
		return false;
	}
}
</script>';
$slng = strpos($endurl, '?lng=') > 0 || strpos($endurl, '&lng=') > 0 ? '' : 'lng='.$lng;
if ($slng != '') $endurl .= (strpos($endurl, '?') > 0) ? '&'.$slng : '?'.$slng;
$flagurl = str_replace('&', '&amp;', $endurl);
if ($lng == $lang[0]) $flagurl = str_replace('lng='.$lang[0], 'lng='.$lang[1], $flagurl); 
if ($lng == $lang[1]) $flagurl = str_replace('lng='.$lang[1], 'lng='.$lang[0], $flagurl); 
$flags = '
  <script>
    $(document).ready(function(){
      $(\'.menuiconflag\').popover({
        title:"'.$web[87].'",
        content:"'.($lng == $lang[0] ? $web[555].': <b>'.$lang[1].'</b>' : $web[556].': <b>'.$lang[0].'</b>').'",
        trigger:"hover",
        placement:"auto",
        html:true
      });
    });
  </script>
  <div class="menuIcons_flag">
    <a href="'.$flagurl.(strpos($flagurl, 'index.php') > 0 || strpos($flagurl, $urlrw[0]) > 0 ? '' 
        : $z2).'" class="menuiconflag" title="'.$web[87].'">
      <i class="flg-'.($lng == $lang[0] ? $lang[1] : $lang[0]).'" title="'.($lng == $lang[0] ? $lang[1] : $lang[0]).'"></i>
    </a>
  </div>';
$titre0 = 'Saisissez ici les identifiants qui seront ceux de l\'administrateur principal';
$titre1 = 'Enter here the IDs that will be those of the main administrator';
$after0 = 'Après validation de vos identifiants vous serez redirigé(e) vers le formulaire de connexion. Il faudra ensuite compléter votre compte.';
$after1 = 'After validating your login details you will be redirected to the login form. You will then need to complete your account.';
echo '
<div id="adminModal" class="modal">
  <div class="modal-content">'.$flags.'
    <h2 class="title text-center">'.($lng == $lang[0] ? $titre0 : $titre1).'</h2>
    <fieldset class="w-100 m-auto">
      <form name="useradmin" action="#" method="post" onsubmit="return VerifyFormAdmin(); return false;">
        <input type="hidden" name="validForm" value="send" />
        <div class="w-100 height">
          &bull; '.($lng == $lang[0] ? 'Votre pseudo' : 'Your login name').' :
          <span class="d-none"><input type="text" name="code_pseudo" value="" /></span>
        </div>
        <div class="user w-90 input-group mb-3">
          <div class="input-group-prepend">
            <span class="input-group-text"><i class="far fa-user"></i></span>
          </div>
          <input type="text" class="texte form-control" name="apseudo" value="'.$apseudo.'" />
        </div>
        <div class="w-100">
		  <div class="" id="testuid-strength"></div>
        </div>
        <div class="w-100 height">&bull; '.$web[300].' :</div>
        <div class="w-80 pass">
          <input data-toggle="password" id="testuid" class="texte form-control" type="password" name="apass" value="'.$apass.'" data-message="" />
        </div>
        <div class="w-100 height">&bull; '.$web[467].' :</div>
        <div class="w-80 pass">
          <input data-toggle="password" id="confirmuid" class="texte form-control" type="password" name="apass2" value="'.$apass2.'" data-message="" />
        </div>
        <div class="w-100 height">&bull; '.$web[50].'</div>
        <div class="user w-90 input-group mb-3">
          <div class="input-group-prepend">
            <span class="input-group-text"><i class="fas fa-at"></i></span>
          </div>
          <input type="text" class="texte form-control" name="aemail" value="'.$aemail.'" />
        </div>
        <p class="infomodal">'.($lng == $lang[0] ? $after0 : $after1).'</p>
        <p class="text-center">'.SubmitButton($web[154]).'</p>
      </form>
    </fieldset>
    <p></p>
  </div>
</div>';
?>
