Passed
Push — master ( c22ee8...1dd91c )
by Anthony
03:04
created

Admin::setValideCompte()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
	namespace core\admin;
3
4
	use core\App;
5
	use core\auth\Encrypt;
6
	use core\auth\Membre;
7
	use core\Configuration;
8
	use core\functions\ChaineCaractere;
9
	use core\HTML\flashmessage\FlashMessage;
10
	use core\mail\Mail;
11
12
	class Admin extends Membre {
13
		private $acces_admin;
14
15
16
		//-------------------------- CONSTRUCTEUR ----------------------------------------------------------------------------//
17
		public function __construct($id_identite) {
18
			$dbc = \core\App::getDb();
19
20
			//on récupere le lvl de l'admin
21
			$query = $dbc->query("SELECT acces_admin FROM identite WHERE ID_identite=".$id_identite);
22
			if ((is_array($query)) && (count($query) > 0)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
				foreach ($query as $obj) {
24
					$this->acces_admin = $obj->acces_admin;
25
				}
26
			}
27
28
			//si on ne passe pas dans le foreach -> on est pas admin donc on deco le compte
29
			if ((!isset($this->acces_admin)) || ($this->acces_admin != 1)) {
30
				FlashMessage::setFlash("Vous n'êtes pas un administrateur, vous ne pouvez pas accéder à cette page");
31
				header("location:".WEBROOT."index.php");
32
			}
33
		}
34
		//-------------------------- FIN CONSTRUCTEUR ----------------------------------------------------------------------------//
35
36
37
38
		//-------------------------- GETTER ----------------------------------------------------------------------------//
39
		public function getAccesAdmin() {
40
			return $this->acces_admin;
41
		}
42
43
44
		/**
45
		 * Pour récupérer la liste de tous les users afin d'activer un compte ou modifier des trucs dessus
46
		 * si archiver == null on récupère les utilisateurs actifs sur le site sinon on récupere les utilisateurs archives
47
		 */
48
		public function getAllUser($archiver = 0) {
49
			$dbc = \core\App::getDb();
50
			$this->setAllUser(null, null, null, null, null, null, null);
51
52
			$query = $dbc->query("SELECT * FROM identite WHERE archiver = $archiver AND ID_identite > 1");
53
54
			if ((is_array($query)) && (count($query) > 0)) {
55
				$id_identite = [];
56
				$nom = [];
57
				$prenom = [];
58
				$pseudo = [];
59
				$mail = [];
60
				$img_profil = [];
61
				$valide = "";
62
63
				foreach ($query as $obj) {
64
					$id_identite[] = $obj->ID_identite;
65
					$nom[] = $obj->nom;
66
					$prenom[] = $obj->prenom;
67
					$pseudo[] = $obj->pseudo;
68
					$mail[] = $obj->mail;
69
					$img_profil[] = $obj->img_profil;
70
					$valide[] = $this->getValideCompteLien($obj->valide, $obj->ID_identite);
71
72
				}
73
74
				$this->setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide);
75
			}
76
		}
77
78
		/**
79
		 * @param $valide
80
		 * @return string
81
		 */
82
		private function getValideCompteLien($valide, $id_identite) {
83
			$config = new Configuration();
84
85
			if (($config->getValiderInscription() == 1) && ($valide == 0)) {
86
				return "<a href=".ADMWEBROOT."controller/core/admin/comptes/valider_compte?id_identite=$id_identite>Valider cet utilisateur</a>";
87
			}
88
			else {
89
				return "Utilisateur validé";
90
			}
91
		}
92
93
		/**
94
		 * Fonctio qui premet de setter les différents élément d'un user
95
		 * @param $id_identite
96
		 */
97
		public function getunUser($id_identite) {
98
			$dbc = \core\App::getDb();
99
100
			$query = $dbc->query("SELECT * FROM identite WHERE ID_identite=".$id_identite);
101
102
			if ((is_array($query)) && (count($query) > 0)) {
103
				foreach ($query as $obj) {
104
					$this->id_identite = $obj->ID_identite;
105
					$this->nom = $obj->nom;
106
					$this->prenom = $obj->prenom;
107
					$this->img = $obj->img_profil;
108
					$this->mail = $obj->mail;
109
					$this->valide = $obj->valide;
110
				}
111
			}
112
		}
113
114
		/**
115
		 * fonction qui si égale a 1 alors il y a une notification dans l'admin du site
116
		 * @return mixed
117
		 */
118
		public function getNotification() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
			$dbc = App::getDb();
120
121
			$query = $dbc->query("SELECT admin FROM notification");
122
123
			if ((is_array($query)) && (count($query) > 0)) {
124
				foreach ($query as $obj) {
125
					return $obj->admin;
126
				}
127
			}
128
		}
129
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
130
131
132
133
		//-------------------------- SETTER ----------------------------------------------------------------------------//
134
135
		/**
136
		 * @param null|string $valide
137
		 */
138
		private function setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide) {
139
			$this->id_identite = $id_identite;
140
			$this->nom = $nom;
141
			$this->prenom = $prenom;
142
			$this->mail = $mail;
143
			$this->pseudo = $pseudo;
144
			$this->img = $img_profil;
145
			$this->valide = $valide;
146
		}
147
148
		/**
149
		 * Fonction qui permet de valider un compte utilisateur pour qu'il puisse se conecter au site
150
		 * @param $id_identite
151
		 */
152
		public function setValideCompte($id_identite) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
			$dbc = \core\App::getDb();
154
155
			$value = array("id_identite" => $id_identite);
156
157
			$dbc->prepare("UPDATE identite SET valide=1 WHERE ID_identite=:id_identite", $value);
158
159
			$this->getunUser($id_identite);
160
		}
161
162
		/**
163
		 * fonction quir genere un mot de passe aleatoire pour le compte spécifié en param
164
		 * @param $id_identite
165
		 */
166
		public function setReinitialiserMdp($id_identite) {
167
			$dbc = \core\App::getDb();
168
169
			$this->getunUser($id_identite);
170
171
			if (($this->mail != "") || ($this->mail != null)) {
172
				$mdp = ChaineCaractere::random(6);
173
				$mdp_encode = Encrypt::setEncryptMdp($mdp, $id_identite);
174
175
				$value = array(
176
					"mdp" => $mdp_encode,
177
					"id_identite" => $id_identite,
178
					"last_change_mdp" => date("Y-m-d")
179
				);
180
181
				FlashMessage::setFlash("Mot de passe réinitialisé avec succès ! L'utilisateur à reçu un E-mail avec son nouveau mot de passe", "success");
182
183
				$dbc->prepare("UPDATE identite SET mdp=:mdp, last_change_mdp=:last_change_mdp WHERE ID_identite=:id_identite", $value);
184
185
				$mail = new Mail($this->mail);
186
				$mail->setEnvoyerMail("Réinitialisation de votre E-mail effectuée", "Votre mot de passe a été réinitialisé");
187
			}
188
			else {
189
				FlashMessage::setFlash("le mot de passe de $this->pseudo ne peu pas être réinitialisé car il ne possède pas d'E-mail");
190
				$this->erreur = true;
191
			}
192
		}
193
194
		/**
195
		 * Supprime le compte en question et enleve l'image de profil aussi
196
		 * @param $id_identite
197
		 */
198
		public function setArchiverCompte($id_identite) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
			$dbc = \core\App::getDb();
200
201
			$value = array(
202
				"id_identite" => $id_identite,
203
				"archiver" => 1
204
			);
205
206
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
207
		}
208
209
		/**
210
		 * Supprime le compte en question et enleve l'image de profil aussi
211
		 * @param $id_identite
212
		 */
213
		public function setActiverCompte($id_identite) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
214
			$dbc = \core\App::getDb();
215
216
			$value = array(
217
				"id_identite" => $id_identite,
218
				"archiver" => NULL
219
			);
220
221
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
222
		}
223
224
		/**
225
		 * Supprime le compte en question et enleve l'image de profil aussi
226
		 * @param $id_identite
227
		 */
228
		public function setSupprimerCompte($id_identite) {
229
			$dbc = \core\App::getDb();
230
231
			$oldimg_profil = "";
232
233
			//test si il y a deja une img
234
			$query = $dbc->query("SELECT img_profil FROM identite where ID_identite=$id_identite");
235
236
			if ((is_array($query)) && (count($query) > 0)) {
237
				foreach ($query as $obj) {
238
					$oldimg_profil = $obj->img_profil;
239
				}
240
			}
241
242
			$oldimg_profil = explode("/", $oldimg_profil);
243
			if (end($oldimg_profil) != "defaut.png") {
244
				unlink("../../images/profil/".$oldimg_profil[7]);
245
			}
246
247
			$value = array(
248
				"id_identite" => $id_identite
249
			);
250
251
			$dbc->prepare("DELETE FROM identite WHERE ID_identite=:id_identite", $value);
252
		}
253
254
		/**
255
		 * permet de dire qu'on a vue une notification dans l'administration du site internet
256
		 */
257
		public static function setNotificationVue() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
258
			$dbc = App::getDb();
259
260
			$value = [
261
				"admin" => 0,
262
				"id" => 1
263
			];
264
265
			$dbc->prepare("UPDATE notification SET admin=:admin WHERE ID_notification=:id", $value);
266
		}
267
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
268
269
270
271
	}