Differences From Artifact [6c7caf4cad]:

To Artifact [4506047015]:


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

128
129
130
131
132
133
134

135



136

137
138
139
140

141
142
143
144
145
146
147
        if (empty($_SESSION['logged_user']))
        {
            return false;
        }

        $membre = $_SESSION['logged_user'];

        if (!\KD2\Security_OTP::TOTP($membre['secret_otp'], $code))
        {
            // Vérifier encore, mais avec le temps NTP
            // au cas où l'horloge du serveur n'est pas à l'heure
            $time = \KD2\Security_OTP::getTimeFromNTP('fr.pool.ntp.org');

            if (!\KD2\Security_OTP::TOTP($membre['secret_otp'], $code, $time))
            {
                return false;
            }
        }

        $_SESSION['otp_required'] = false;

        return true;
    }

    public function setOTP()
    {
        $membre = $this->getLoggedUser();

        $secret = \KD2\Security_OTP::getRandomSecret();

        DB::getInstance()->simpleExec('UPDATE membres SET secret_otp = ? WHERE id = ?;', $secret, $membre['id']);

        $membre['secret_otp'] = $secret;

        $this->updateSessionData($membre);


        return $secret;
    }

    public function disableOTP()
    {
        $membre = $this->getLoggedUser();





        DB::getInstance()->simpleExec('UPDATE membres SET secret_otp = NULL WHERE id = ?;', $membre['id']);


        $membre['secret_otp'] = null;

        $this->updateSessionData($membre);


        return true;
    }

    public function recoverPasswordCheck($id)
    {
        $db = DB::getInstance();







|

<
<
<
<
<
<
|
|
|






|

<
|
|
|
|
|
<
|
<
>

|


|

<
>
|
>
>
>
|
>
|
|
|
<
>







93
94
95
96
97
98
99
100
101






102
103
104
105
106
107
108
109
110
111
112

113
114
115
116
117

118

119
120
121
122
123
124
125

126
127
128
129
130
131
132
133
134
135

136
137
138
139
140
141
142
143
        if (empty($_SESSION['logged_user']))
        {
            return false;
        }

        $membre = $_SESSION['logged_user'];

        if (!$this->checkOTP($membre['secret_otp'], $code))
        {






            return false;
        }


        $_SESSION['otp_required'] = false;

        return true;
    }

    public function getNewOTPSecret()
    {

        $out = [];
        $out['secret'] = \KD2\Security_OTP::getRandomSecret();
        $out['secret_display'] = implode(' ', str_split($out['secret'], 4));
        $out['url'] = \KD2\Security_OTP::getOTPAuthURL(Config::getInstance()->get('nom_asso'), $out['secret']);
    

        $qrcode = new \KD2\QRCode($out['url']);

        $out['qrcode'] = 'data:image/svg+xml;base64,' . base64_encode($qrcode->toSVG());

        return $out;
    }

    public function checkOTP($secret, $code)
    {

        if (!\KD2\Security_OTP::TOTP($secret, $code))
        {
            // Vérifier encore, mais avec le temps NTP
            // au cas où l'horloge du serveur n'est pas à l'heure
            $time = \KD2\Security_OTP::getTimeFromNTP('fr.pool.ntp.org');

            if (!\KD2\Security_OTP::TOTP($secret, $code, $time))
            {
                return false;
            }

        }

        return true;
    }

    public function recoverPasswordCheck($id)
    {
        $db = DB::getInstance();
568
569
570
571
572
573
574
























































575
576
577
578
579
580
581
        if (empty($data))
        {
            return true;
        }

        return $db->simpleUpdate('membres', $data, 'id = '.(int)$id);
    }

























































    public function get($id)
    {
        $db = DB::getInstance();
        $config = Config::getInstance();

        return $db->simpleQuerySingle('SELECT *,







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
        if (empty($data))
        {
            return true;
        }

        return $db->simpleUpdate('membres', $data, 'id = '.(int)$id);
    }

    public function checkPassword($password)
    {
        $user = $this->getLoggedUser();

        if (!$user)
        {
            return false;
        }

        return $this->_checkPassword($password, $user['passe']);
    }

    public function editSecurity(Array $data = [])
    {
        $user = $this->getLoggedUser();

        if (!$user)
        {
            throw new \LogicException('Utilisateur non connecté.');
        }

        $allowed_fields = ['passe', 'clef_pgp', 'secret_otp'];

        foreach ($data as $key=>$value)
        {
            if (!in_array($key, $allowed_fields))
            {
                throw new \RuntimeException(sprintf('Le champ %s n\'est pas autorisé dans cette méthode.', $key));
            }
        }

        if (isset($data['passe']) && trim($data['passe']) !== '')
        {
            if (strlen($data['passe']) < 5)
            {
                throw new UserException('Le mot de passe doit faire au moins 5 caractères.');
            }

            $data['passe'] = $this->_hashPassword($data['passe']);
        }
        else
        {
            unset($data['passe']);
        }

        if (isset($data['clef_pgp']))
        {
            $data['clef_pgp'] = trim($data['clef_pgp']);
        }

        $db->simpleUpdate('membres', $data, 'id = '.(int)$user['id']);
        $this->updateSessionData();

        return true;
    }

    public function get($id)
    {
        $db = DB::getInstance();
        $config = Config::getInstance();

        return $db->simpleQuerySingle('SELECT *,