<?php
/****************************************************************************************
 * SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV
 *
 * Version 1.0.4
 *
 * Copyright (C) 2006-2016  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_fic.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(valsession('code_log'));

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

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

$row = lit_enr_cli(valsession('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, 'utf8');
    $rech = 'SELECT * FROM `' . TBPW . "` WHERE `pw_code` = '" . valsession('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 (valpost('val') == _('Valider')) {
                if ((md5(trim(valpost('expw'))) == $pw_pass) AND (trim(valpost('expw')) <> trim(valpost('pw1')))) {
                    $rech2 = 'UPDATE `' . TBPW . "` SET `pw_pass`='" . md5(trim(valpost('pw1'))) . "' WHERE `pw_code` = '" . valsession('code_log') . "'";
                    $db->query($rech2);
                    $nouvpwok = TRUE;
                } else {
                    $pbm_expw = TRUE;
                }
            }
        }
    } else {
        $pbm_mdpc = TRUE;
    }
    close_database();
    if ($nouvpwok == TRUE) {
        envoi_mail('', _('Mise à jour mot de passe'), 'cli', 'mdp', 'xxx', 'cli', valsession('code_log'), trim(valpost('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','".valsession('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) {
            cre_ent_form(_('CHANGEMENT MOT DE PASSE'), '', '', '', FALSE);
            ?>
            <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>