<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 1.0.0

Copyright (C) 2006-2014  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smi.no-ip.org
 ****************************************************************************************/

session_start();

require_once("../lib/gettext/gettext.inc");
include_once("../inc/config.inc.php");
include_once("../inc/func_log.inc.php");
if (CheckURLLog(FALSE) == FALSE) header("Location: ../index.php");
if (CheckNivLog("1") == FALSE) header("Location: ../index.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_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../inc/func_date.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(@$_SESSION["code_log"]);

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

$pbm_mdpc = FALSE;
$pbm_expw = FALSE;
$nouvpwok = FALSE;

$row = lit_enr_cli(@$_SESSION["code_log"], TRUE);
if (is_array($row)) {
    $row = encode_str($row);
    extract($row);

    // Récupération du mot de passe
    $db = open_database(DBUSER, DBPASS, DBNAME, DBHOST);
    $rech = "SELECT * FROM `" . TBPW . "` WHERE `pw_code` = '" . @$_SESSION["code_log"] . "'";
    $row = $db->get_row($rech, ARRAY_A);
    if ($row) {
        extract($row);

        if ($pw_actif == 0) // Le compte doit être actif
            $pbm_mdpc = TRUE;
        else {
            // On a validé
            if (@$_POST["val"] == _("Valider")) {
                if ((md5(trim(@$_POST["expw"])) == $pw_pass) AND (trim(@$_POST["expw"]) <> trim(@$_POST["pw1"]))) {
                    $rech2 = "UPDATE `" . TBPW . "` SET `pw_pass`='" . md5(trim(@$_POST["pw1"])) . "' WHERE `pw_code` = '" . @$_SESSION["code_log"] . "'";
                    $db->query($rech2);
                    $nouvpwok = TRUE;
                } else
                    $pbm_expw = TRUE;
            }
        }
    } else
        $pbm_mdpc = TRUE;
    $db->disconnect();
    if ($nouvpwok == TRUE)
        envoi_mail("", _("Mise à jour mot de passe"), "cli", "mdp", "xxx", "cli", @$_SESSION["code_log"], trim(@$_POST["pw1"]), "", TRUE);
} else
    $pbm_mdpc = TRUE;
?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-type=" content="text/html; charset=utf-8">
        <title>SMI - <?PHP echo _("ESPACE PRIVE"); ?> : <?PHP echo _("Changement mot de passe"); ?></title>

        <?PHP include_once("../inc/header.inc.php"); ?>

        <script language="javascript">
            <!--
            var tab_champ = ["nom:expw ; format:txt ; req:Y ; txt:<?PHP echo _("Ancien mot de passe"); ?> ; lmn:1 ; lmx:20; stok:frmok ; stbad:frmbad",
                "nom:pw1 ; format:pw ; pwq:6 ; req:Y ; txt:<?PHP echo _("Nouveau mot de passe"); ?>; lmn:7 ; lmx:20; stok:frmok ; stbad:frmbad",
                "nom:pw2 ; format:pw ; pwq:6 ; req:Y ; txt:<?PHP echo _("Nouveau mot de passe (confirmation)"); ?>; lmn:7 ; lmx:20; comp:pw1 ; stok:frmok ; stbad:frmbad"];
            //-->
        </script>

        <script type="text/javascript">
            <!--
            function init_all() {
                init_Body();
                <?php
                echo "init_valid_form('enr','mdpc','".$_SESSION['date_fmtshort']."',tab_champ,'','');";
                ?>
                init_checkpw('pw1', 'nivpw', true);
                Focus_first();
            }

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

    </head>

<body>

<?PHP
include_once("../inc/entete.inc.php");
if ($pbm_mdpc == FALSE) {
    if ($pbm_expw == FALSE) {
        if ($nouvpwok == FALSE) {
            ?>
            <p align="center"><span class="titrepage"><?PHP echo _("CHANGEMENT MOT DE PASSE"); ?></span></p>
            <br />
            <form name="mdpc" id="mdpc" action="mdpc.php" method="post">
            <input name="nbutton" type="hidden" id="nbutton" value="">
                <table width="650" border="0" align="center">
                    <tr>
                        <td width="300" class="rubfrm" id="f_expw" align="right"><?PHP echo _("Ancien mot de passe"); ?></td>
                        <td>
                            <input name="expw" type="password" id="expw" size="26">
                        </td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td width="150" class="rubfrm" id="f_pw1" align="right"><?PHP echo _("Nouveau mot de passe"); ?></td>
                        <td>
                            <input name="pw1" type="text" id="pw1" size="26">
                        </td>
                        <td class="annot_petit_it"><?PHP echo _("7 caractères minimum"); ?><br />
                            <?PHP echo _("Différent de l'ancien"); ?>
                        </td>
                    </tr>
                    <tr>
                        <td width="150" id="f_pwq">
                            <div align="right" class="annot_petit_it"><?PHP echo _("Qualité du mot de passe"); ?> --></div>
                        </td>
                        <td>
                            <span id="nivpw"><img src="../img/none.gif" width="10" height="17" align="absmiddle"></span>
                        </td>
                        <td class="annot_petit_it">&nbsp;</td>
                    </tr>
                    <tr>
                        <td width="150" class="rubfrm" id="f_pw2" align="right"><?PHP echo _("Nouveau mot de passe (confirmer)"); ?></td>
                        <td>
                            <input name="pw2" type="text" id="pw2" size="26">
                        </td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <p align="center">
                    <?PHP
                    echo '<input name="enr" type="button" class="bton_std" id="enr" value="'._("Valider").'">';
                    echo '<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="'._("Réinitialiser").'">';
                    ?>
                </p>
            </form>
        <?PHP
        } else
            pop_ret_auto(_("Mot de passe incorrect"), 'warn', 'index.php');
    } else
        pop_ret_auto(_("Problème sur la saisie des mots de passe")." < br />"._("Mot de passe original erroné ou")." < br />"._("nouveau mot de passe identique au mot de passe original"),'warn', 'mdpc.php');
}
else
	pop_ret_auto(_("Problème de paramètres"), 'warn', "index.php");
include_once("pied.php");
?>
</body>
</html>