Passed
Push — master ( de0e88...de0e88 )
by Anthony
04:58 queued 02:36
created

Membre::setPseudo()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
eloc 11
nc 3
nop 1
1
<?php
2
	namespace core\auth;
3
4
5
	class Membre {
6
		protected $id_identite;
7
		protected $nom;
8
		protected $prenom;
9
		protected $mail;
10
		protected $pseudo;
11
		protected $img;
12
		protected $mdp;
13
		protected $valide;
14
		protected $erreur;
15
		
16
		private $debut_lien;
17
		
18
		//------------------------------ constructeur-----------------------------------
19
		//Récupérer en base de données les infos du membre
20
		public function __construct($id_identite = null) {
21
			$dbc = \core\App::getDb();
22
23
			$this->debut_lien = IMGROOT."profil/";
24
25
			if ($id_identite != null) {
26
				$query = $dbc->query("SELECT * FROM identite where ID_identite=$id_identite");
27
28
				if ((is_array($query)) && (count($query) > 0)) {
29
					foreach ($query as $obj) {
30
						$this->id_identite = $obj->ID_identite;
31
						$this->nom = $obj->nom;
32
						$this->prenom = $obj->prenom;
33
						$this->mail = $obj->mail;
34
						$this->pseudo = $obj->pseudo;
35
						$this->mdp = $obj->mdp;
36
						$this->valide = $obj->valide;
37
						$this->img = $obj->img_profil;
38
					}
39
				}
40
			}
41
		}
42
		//------------------------------ fin constructeur -----------------------------------
43
		
44
		
45
		
46
		//------------------------------ getter-----------------------------------
47
		public function getIdidentite() {
48
			return $this->id_identite;
49
		}
50
		public function getNom() {
51
			return $this->nom;
52
		}
53
		public function getPrenom() {
54
			return $this->prenom;
55
		}
56
		public function getPseudo() {
57
			return $this->pseudo;
58
		}
59
		public function getMail() {
60
			return $this->mail;
61
		}
62
		public function getImg() {
63
			return $this->img;
64
		}
65
		public function getMdp() {
66
			return $this->mdp;
67
		}
68
		public function getValide() {
69
			return $this->valide;
70
		}
71
		public function getErreur() {
72
			return $this->erreur;
73
		}
74
		//------------------------------ fin getter -----------------------------------
75
		
76
		
77
		
78
		//------------------------------ setter-----------------------------------
79
80
		/**
81
		 * @param string $new_pseudo
82
		 */
83
		public function setPseudo($new_pseudo) {
84
			$dbc = \core\App::getDb();
85
			
86
			//si pseudo trop court
87
			if ((strlen($new_pseudo) < 5) || (strlen($new_pseudo) > 15)) {
88
				$err = "Votre pseudo doit être entre 5 et 15 caractères";
89
				$this->erreur = $err;
90
			}
91
			else if ($dbc->rechercherEgalite("identite", "pseudo", $new_pseudo) == false) {
92
				$err = "Ce pseudo est déjà utilisé, veuillez en choisir un autre";
93
				$this->erreur = $err;
94
			}
95
			else {
96
				$dbc->query("UPDATE identite set pseudo=$new_pseudo WHERE ID_identite=".$_SESSION["idlogin".CLEF_SITE]);
97
				$this->pseudo = $new_pseudo;
98
			}
99
		}
100
101
		/**
102
		 * @param string $old_mdp
103
		 * @param string $new_mdp
104
		 * @param string $verif_new_mdp
105
		 */
106
		public function setMdp($old_mdp, $new_mdp, $verif_new_mdp) {
107
			$dbc = \core\App::getDb();
108
109
			$mdp = Encrypt::setDecryptMdp($this->mdp, $this->id_identite);
110
111
			$testmdp = $this->testpassword($new_mdp);
112
113
			//si mdp trop court
114
			if (md5($old_mdp) != $mdp) {
115
				$err = "Votre mot de passe est incorrect";
116
				$this->erreur = $err;
117
			}
118
			else if ($new_mdp != $verif_new_mdp) {
119
				$err = "Vos mots de passe sont différents";
120
				$this->erreur = $err;
121
			}
122
			else if ((strlen($new_mdp) < 5) || ($testmdp < 40)) {
123
				$err = "Votre mot de passe est trop simple";
124
				$this->erreur = $err;
125
			}
126
			else {
127
				$mdpok = Encrypt::setEncryptMdp($new_mdp, $this->id_identite);
128
				//le nouveau mdp est bon on update
129
				$dbc->query("UPDATE identite SET mdp='$mdpok' WHERE ID_identite=".$this->id_identite);
130
131
				$this->mdp = $mdpok;
132
			}
133
		}
134
		//------------------------------ fin setter -----------------------------------
135
136
137
		//-------------------------- FONCTIONS SPECIFIQUES ----------------------------------------------------------------------------//
138
		//-------------------------- FONCTIONS POUR TESTER SECURITE D'UN MDP ----------------------------------------------------------------------------//
139
		/**
140
		 * Fonction  qui permet de verifier la securite d'un mdp
141
		 * @param string $mdp
142
		 * @return integer
143
		 */
144
		private function testpassword($mdp) {
145
			$longueur = strlen($mdp);
146
			$point = 0;
147
			$point_min = 0;
148
			$point_maj = 0;
149
			$point_chiffre = 0;
150
			$point_caracteres = 0;
151
152
			for ($i = 0; $i < $longueur; $i++) {
153
				$lettre = $mdp[$i];
154
155
				if ($lettre >= 'a' && $lettre <= 'z') {
156
					$point = $point + 1;
157
					$point_min = 1;
158
				}
159
				else if ($lettre >= 'A' && $lettre <= 'Z') {
160
					$point = $point + 2;
161
					$point_maj = 2;
162
				}
163
				else if ($lettre >= '0' && $lettre <= '9') {
164
					$point = $point + 3;
165
					$point_chiffre = 3;
166
				}
167
				else {
168
					$point = $point + 5;
169
					$point_caracteres = 5;
170
				}
171
			}
172
173
			// Calcul du coefficient points/longueur
174
			$etape1 = $point / $longueur;
175
176
			// Calcul du coefficient de la diversite des types de caracteres...
177
			$etape2 = $point_min + $point_maj + $point_chiffre + $point_caracteres;
178
179
			// Multiplication du coefficient de diversite avec celui de la longueur
180
			$resultat = $etape1 * $etape2;
181
182
			// Multiplication du resultat par la longueur de la chaene
183
			$final = $resultat * $longueur;
184
185
			return $final;
186
		}
187
		//-------------------------- FIN FONCTIONS POUR TESTER SECURITE D'UN MDP ----------------------------------------------------------------------------//
188
	}