<?php
include("../../../functions.inc.php");

dbConnect();
if(!get_ente($_GET['for']))
	die();

if(isset($_POST['cellulare'])){
	
	if(!is_cellulare($_POST['cellulare']))
		printMsg("Numero di Cellulare non corretto","danger");
	
	//captcha
    if(empty($_SESSION['captcha']) || trim(strtolower($_POST['captcha_code'])) != $_SESSION['captcha'])
		printMsg("<strong>Codice visualizzato</strong> errato","danger");

		
	dbConnect();

	//controllo se il numero esiste
	$result = $mysqli->query("
	SELECT user_nome, user_pass, ente_anagr FROM users 
	LEFT JOIN enti ON users.ente_id = enti.ente_id
	WHERE user_cell = ".dbClean($_POST['cellulare'])." AND enti.ente_url = ".dbClean($_GET['for']));
	if($result->num_rows == 1){
		$row = $result->fetch_assoc();
	}else{
		printMsg("Il numero di Cellulare non risulta registrato","warning");
	}
	
	include(ROOT.'/includes/sms.inc.php');
	$sms_text = "La tua password per accedere al servizio Cittadino Facile - ".$row['ente_anagr']." è: ".$row['user_pass'];
	
	if(!sendSMS($_GET['for'],$sms_text,$_POST['cellulare']))
		printMsg("Errore durante l'invio della password tramite SMS. Ricontrolla il numero di Cellulare inserito","danger");

	Js('resetForm("pass_form");$("#n-block,#okButton").hide();');
	printMsg("Password inviata al tuo numero di cellulare","success");
}
?>
<div>
    <div class="modal-d-title">Recupera password</div>
    <form id="pass_form" role="form" class="form-horizontal" action="<?php echo $_SERVER['REQUEST_URI'];?>" onSubmit="return post(this,'f-result-pass')">
    
        <div class="modal-body modal-body-diag">    
            <div id="n-block">
                <div class="form-group has-feedback">
                    <label for="cellulare" class="control-label col-sm-4">N. Cellulare</label>
                    <div class="col-sm-6">
                        <input class="form-control" type="text" maxlength="10" name="cellulare" value="" />
                        <span class="glyphicon glyphicon-phone form-control-feedback"></span>
                   </div>
                   <p class="help-block col-xs-12 col-sm-9 col-sm-offset-4">Ti verr&agrave; inviato un SMS con la password di accesso</p>
                </div>
                <div class="form-group">
                    <label class="control-label col-xs-12 col-sm-4">Codice visualizzato</label>
                    <div class="col-xs-6 col-sm-4 has-feedback">
                        <input class="form-control" type="text" maxlength="6" name="captcha_code" />
                        <span class="glyphicon glyphicon-captcha glyphicon-arrow-right form-control-feedback"></span>
                    </div>
                    <div class="col-xs-6 col-sm-4">
                        <img id="capimg" src="/captcha/?sid=<?php echo md5(uniqid()) ?>" />
                        <a href="javascript:;" onClick="reloadCaptcha()"><span class="glyphicon glyphicon-repeat"></span></a>
                    </div>
                </div>
            </div>
            <div id="f-result-pass"></div>
        </div>
        
        <div class="modal-footer">
            <button id="okButton" type="submit" class="btn btn-primary">Invia</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
        </div>
    </form>
    
</div>