<?php
session_start();
require_once('lib/gettext/gettext.inc');
include_once('inc/config.inc.php');
include_once('inc/fic_age.inc.php');
include_once('inc/fic_cli.inc.php');
include_once('inc/fic_do.inc.php');
include_once('inc/fic_hum.inc.php');
include_once('inc/fic_st.inc.php');
include_once('inc/fic_tec.inc.php');
include_once('inc/fic_txt.inc.php');
include_once('inc/func_geo.inc.php');
include_once('inc/func.inc.php');
include_once('inc/func_date.inc.php');
include_once('inc/func_fic.inc.php');
include_once('inc/func_mail.inc.php');
include_once('inc/func_txt.inc.php');
include_once('inc/func_txtcm.inc.php');
include_once('inc/func_log.inc.php');
include_once('classes/phpmailer/class.phpmailer.php');
include_once('classes/phpmailer/class.smtp.php');
include_once('inc/mail.inc.php');
include_once('classes/ezSQL/shared/ez_sql_core.php');
include_once('classes/ezSQL/mysqli/ez_sql_mysqli.php');
include_once('inc/func_sql.inc.php');
Archivelog(valsession('code_log'));

// Variables Formulaire
fix_magic_quotes();
$_POST = clean_str($_POST);

$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP

$validsaisie = True;
$validcode = False;
$validemail = False;
$validverif = False;
$emailsent = False;
$typdest = '';

if (count($_POST) > 0) {
    $pass_code = @$_POST['pass_code'];
    $pass_email = strtolower(@$_POST['pass_email']);
    $pass_verif = @$_POST['pass_verif'];

    if ($pass_code == '') {
        $validsaisie = false;
    }
    if ($pass_email == '') {
        $validsaisie = false;
    }
    if ($pass_verif == '') {
        $validsaisie = false;
    }

    if ($validsaisie == true) {
        if ($validcode == false) {
            // Contrôle du code (et vérification si actif)
            $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
            $rech = 'SELECT * FROM `' . TBPW . "` WHERE `pw_code` = '" . $db->escape($pass_code) . "' AND `pw_actif` = '1'";
            $row = $db->get_row($rech, ARRAY_A);
            if ($row) // Il ne peut y avoir qu'un seul résultat
            {
                $validcode = true;

                extract($row);

                // Contrôle de l'adresse email
                $rech2 = '';

                switch ($pw_level) {
                    // Client
                    case 1 :
                        $rech2 = 'SELECT cli_email FROM `' . TBCLI . "` WHERE `cli_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Opérateur
                    case 2 :
                        $rech2 = 'SELECT ope_email FROM `' . TBOPE . "` WHERE `ope_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Technicien
                    case 3 :
                        $rech2 = 'SELECT tec_email FROM `' . TBTEC . "` WHERE `tec_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Agence
                    case 4 :
                        $rech2 = 'SELECT age_email FROM `' . TBAGE . "` WHERE `age_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Centre de gestion
                    case 5 :
                        $rech2 = 'SELECT cge_email FROM `' . TBCG . "` WHERE `cge_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Donneur d'ordre
                    case 6 :
                        $rech2 = 'SELECT do_email FROM `' . TBCG . "` WHERE `do_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    // Sous-traitant
                    case 10 :
                        $rech2 = 'SELECT st_email FROM `' . TBCG . "` WHERE `st_code` = '" . $db->escape($pass_code) . "'";
                        break;
                    default :
                        break;
                }

                if ($rech2 <> '') {
                    $row2 = $db->get_row($rech2, ARRAY_A);
                    if ($row2) {
                        extract($row2);
                        $email = '';

                        switch ($pw_level) {
                            // Client
                            case 1 :
                                $email = $cli_email;
                                $typdest = 'cli';
                                break;
                            // Opérateur
                            case 2 :
                                $email = $ope_email;
                                $typdest = 'ope';
                                break;
                            // Technicien
                            case 3 :
                                $email = $tec_email;
                                $typdest = 'tec';
                                break;
                            // Agence
                            case 4 :
                                $email = $age_email;
                                $typdest = 'age';
                                break;
                            // Centre de gestion
                            case 5 :
                                $email = $cge_email;
                                $typdest = 'cge';
                                break;
                            // Donneur d'ordres
                            case 6 :
                                $email = $do_email;
                                $typdest = 'do';
                                break;
                            // Sous-traitant
                            case 10 :
                                $email = $st_email;
                                $typdest = 'st';
                                break;
                            default :
                                break;
                        }
                        if ($pass_email == $email) {
                            $validemail = true;
                        }

                        // Contrôle du code captcha
                        if ($_SESSION['captcha_keystring'] == $pass_verif) {
                            $validverif = true;
                        }
                    }
                }
            }
            close_database();
        }
    }
}

if (($validsaisie == true) AND ($validcode == true) AND ($validemail == true) AND ($validverif == true)) {
    // Récupération des informations centre de gestion
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST, 'utf8');
    $rech = 'SELECT * FROM `' . TBCG . '`';
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);
    }
    close_database();

    envoi_mail('',_('Renouvellement de votre mot de passe'),'mdp','ren','xxx',$typdest,$pass_code,'','',TRUE);

    $emailsent = true;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <title>SMI : <?php echo _('Mot de passe perdu ?'); ?></title>

    <?php include_once('header.inc.php'); ?>

    <script type="text/javascript">
        <!--
        function init_all() {
            init_Body();
            Focus_first();
        }

        $(document).ready(function () {
            init_all();
        });
        //-->
    </script>
</head>

<body>
<table width="100%" height="300" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <td valign="top">
            <?php
            if ($emailsent == false)
            {
            ?>
            <form name="pass" action="pass.php" method="post">
                <?php
                }
                ?>
                <div align="center">
                    <p><img src="img/logo.gif" alt="<?php echo _('logo SMI'); ?>"></p>

                    <p><img src="img/login.png" alt="<?php echo _('connexion'); ?>></p>
        <p class=" annot_std_it"><?php echo _('Toutes les connexions sont archivées'); ?></p>
                </div>
                <table width="500" border="0" align="center">
                    <tr bgcolor="#FF0000">
                        <td colspan="2">
                            <div align="center" class="titre_std_gras_jaune_frouge">
                                <strong><?php echo _('Mot de passe perdu ?'); ?></strong></div>
                        </td>
                    </tr>
                    <?php
                    if ($emailsent == false) {
                        ?>
                        <tr>
                            <td colspan="2">
                                <div align="center" class="annot_petit_it">
                                    <p><?php echo _('Merci de saisir les informations suivantes') . '<br />' . _('Une fois que vous aurez validé, un email de confirmation vous sera expédié'); ?></p>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_code" width="134"
                                align="right"><?php echo _('Code Utilisateur'); ?></td>
                            <td width="222">
                                <input name="pass_code" type="text" id="pass_code" maxlength="20">
                            </td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_email" align="right"><?php echo _('Votre email'); ?></td>
                            <td>
                                <input name="pass_email" type="text" id="pass_email" size="50" maxlength="50">
                            </td>
                        </tr>
                        <tr>
                            <td class="rubfrm" id="f_verif" align="right"><?php echo _('Code ci-dessous'); ?></td>
                            <td>
                                <input name="pass_verif" type="text" id="pass_verif" maxlength="20">
                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>
                                <img src="imggen.php?<?php echo session_name() ?>=<?php echo session_id() ?>"
                                     align="absmiddle">
                                <input name="code" type="button" class="bton_std" id="code"
                                       value="<?php echo _('Autre code'); ?>" onclick="location.href='pass.php'">
                            </td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;

                            </td>
                        </tr>
                        <?php
                    }
                    ?>
                </table>
                <?php
                if ($emailsent == false)
                {
                ?>
                <p align="center">
                    <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?php echo _('Réinitialiser'); ?>">
                    <input type="submit" class="bton_std" name="submit" id="submit" value="<?php echo _('Valider'); ?>">
                    <input name="annul" type="button" class="bton_std" id="annul"
                           value="<?php echo _('Retour accueil'); ?>" onclick="location.href='index.php'">
                </p>
            </form>

        <?php
        if ($validsaisie == false) {
            if (@$_POST['submit'] <> '') {
                ?>
                <p align="center"><span class="titre_std_gras_jaune_frouge"><?php echo _('Erreur de saisie'); ?></span>
                </p>
                <?php
            }
        } else {
            if (($validcode == false) OR ($validemail == false)) {
                if (@$_POST['submit'] <> '') {
                    ?>
                    <p align="center"><span class="titre_std_gras_jaune_frouge"><?php echo _('Problème code utilisateur et/ou email'); ?></span>
                    </p>
                    <?php
                }
            }

            if ($validverif == false) {
                if (@$_POST['submit'] <> '') {
                    ?>
                    <p align="center"><span
                            class="titre_std_gras_jaune_frouge"><?php echo _('Mauvais code de validation'); ?></span>
                    </p>
                    <?php
                }
            }
        }
        ?>
        <?php
        }
        else {
            unset($_SESSION['captcha_keystring'], $_POST['submit']);

            $validsaisie = True;
            $validcode = False;
            $validemail = False;
            $validverif = False;

            pop_ret_auto(_('Email de confirmation expédié'), 'ok', 'index.php');
        }
        ?>
        </td>
    </tr>
</table>
</body>
</html>