Passed
Push — master ( e426a4...c32d1a )
by Anthony
03:18
created

Admin::setActiverCompte()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
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 = null) {
49
			$dbc = \core\App::getDb();
50
			$this->setAllUser(null, null, null, null, null, null, null);
51
52
			if ($archiver == null) {
53
				$query = $dbc->query("SELECT * FROM identite WHERE archiver IS NULL AND ID_identite > 1");
54
			}
55
			else {
56
				$query = $dbc->query("SELECT * FROM identite WHERE archiver IS NOT NULL AND ID_identite > 1");
57
			}
58
59
			if ((is_array($query)) && (count($query) > 0)) {
60
				$id_identite = [];
61
				$nom = [];
62
				$prenom = [];
63
				$pseudo = [];
64
				$mail = [];
65
				$img_profil = [];
66
				$valide = "";
67
68
				foreach ($query as $obj) {
69
					$id_identite[] = $obj->ID_identite;
70
					$nom[] = $obj->nom;
71
					$prenom[] = $obj->prenom;
72
					$pseudo[] = $obj->pseudo;
73
					$mail[] = $obj->mail;
74
					$img_profil[] = $obj->img_profil;
75
					$valide[] = $this->getValideCompteLien($obj->valide);
76
77
				}
78
79
				$this->setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide);
80
			}
81
		}
82
83
		/**
84
		 * @param $valide
85
		 * @return string
86
		 */
87
		private function getValideCompteLien($valide) {
88
			$config = new Configuration();
89
90
			if (($config->getValiderInscription() == 1) && ($valide == 0)) {
91
				return "<a href=".ADMWEBROOT."controller/core/admin/comptes/valider_compte?id_identite=$obj->ID_identite>Valider cet utilisateur</a>";
0 ignored issues
show
Bug introduced by
The variable $obj does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
92
			}
93
			else {
94
				return "Utilisateur validé";
95
			}
96
		}
97
98
		/**
99
		 * Fonctio qui premet de setter les différents élément d'un user
100
		 * @param $id_identite
101
		 */
102
		public function getunUser($id_identite) {
103
			$dbc = \core\App::getDb();
104
105
			$query = $dbc->query("SELECT * FROM identite WHERE ID_identite=".$id_identite);
106
107
			if ((is_array($query)) && (count($query) > 0)) {
108
				foreach ($query as $obj) {
109
					$this->id_identite = $obj->ID_identite;
110
					$this->nom = $obj->nom;
111
					$this->prenom = $obj->prenom;
112
					$this->img = $obj->img_profil;
113
					$this->mail = $obj->mail;
114
					$this->valide = $obj->valide;
115
				}
116
			}
117
		}
118
119
		/**
120
		 * fonction qui si égale a 1 alors il y a une notification dans l'admin du site
121
		 * @return mixed
122
		 */
123
		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...
124
			$dbc = App::getDb();
125
126
			$query = $dbc->query("SELECT admin FROM notification");
127
128
			if ((is_array($query)) && (count($query) > 0)) {
129
				foreach ($query as $obj) {
130
					return $obj->admin;
131
				}
132
			}
133
		}
134
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
135
136
137
138
		//-------------------------- SETTER ----------------------------------------------------------------------------//
139
140
		/**
141
		 * @param null|string $valide
142
		 */
143
		private function setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide) {
144
			$this->id_identite = $id_identite;
145
			$this->nom = $nom;
146
			$this->prenom = $prenom;
147
			$this->mail = $mail;
148
			$this->pseudo = $pseudo;
149
			$this->img = $img_profil;
150
			$this->valide = $valide;
151
		}
152
153
		/**
154
		 * Fonction qui permet de valider un compte utilisateur pour qu'il puisse se conecter au site
155
		 * @param $id_identite
156
		 */
157
		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...
158
			$dbc = \core\App::getDb();
159
160
			$value = array("id_identite" => $id_identite);
161
162
			$dbc->prepare("UPDATE identite SET valide=1 WHERE ID_identite=:id_identite", $value);
163
164
			$this->getunUser($id_identite);
165
		}
166
167
		/**
168
		 * fonction quir genere un mot de passe aleatoire pour le compte spécifié en param
169
		 * @param $id_identite
170
		 */
171
		public function setReinitialiserMdp($id_identite) {
172
			$dbc = \core\App::getDb();
173
174
			$this->getunUser($id_identite);
175
176
			if (($this->mail != "") || ($this->mail != null)) {
177
				$mdp = ChaineCaractere::random(6);
178
				$mdp_encode = Encrypt::setEncryptMdp($mdp, $id_identite);
179
180
				$value = array(
181
					"mdp" => $mdp_encode,
182
					"id_identite" => $id_identite,
183
					"last_change_mdp" => date("Y-m-d")
184
				);
185
186
				FlashMessage::setFlash("Mot de passe réinitialisé avec succès ! L'utilisateur à reçu un E-mail avec son nouveau mot de passe", "success");
187
188
				$dbc->prepare("UPDATE identite SET mdp=:mdp, last_change_mdp=:last_change_mdp WHERE ID_identite=:id_identite", $value);
189
190
				$mail = new Mail($this->mail);
191
				$mail->setEnvoyerMail("Réinitialisation de votre E-mail effectuée", "Votre mot de passe a été réinitialisé");
192
			}
193
			else {
194
				FlashMessage::setFlash("le mot de passe de $this->pseudo ne peu pas être réinitialisé car il ne possède pas d'E-mail");
195
				$this->erreur = true;
196
			}
197
		}
198
199
		/**
200
		 * Supprime le compte en question et enleve l'image de profil aussi
201
		 * @param $id_identite
202
		 */
203
		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...
204
			$dbc = \core\App::getDb();
205
206
			$value = array(
207
				"id_identite" => $id_identite,
208
				"archiver" => 1
209
			);
210
211
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
212
		}
213
214
		/**
215
		 * Supprime le compte en question et enleve l'image de profil aussi
216
		 * @param $id_identite
217
		 */
218
		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...
219
			$dbc = \core\App::getDb();
220
221
			$value = array(
222
				"id_identite" => $id_identite,
223
				"archiver" => NULL
224
			);
225
226
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
227
		}
228
229
		/**
230
		 * Supprime le compte en question et enleve l'image de profil aussi
231
		 * @param $id_identite
232
		 */
233
		public function setSupprimerCompte($id_identite) {
234
			$dbc = \core\App::getDb();
235
236
			$oldimg_profil = "";
237
238
			//test si il y a deja une img
239
			$query = $dbc->query("SELECT img_profil FROM identite where ID_identite=$id_identite");
240
241
			if ((is_array($query)) && (count($query) > 0)) {
242
				foreach ($query as $obj) {
243
					$oldimg_profil = $obj->img_profil;
244
				}
245
			}
246
247
			$oldimg_profil = explode("/", $oldimg_profil);
248
			if (end($oldimg_profil) != "defaut.png") {
249
				unlink("../../images/profil/".$oldimg_profil[7]);
250
			}
251
252
			$value = array(
253
				"id_identite" => $id_identite
254
			);
255
256
			$dbc->prepare("DELETE FROM identite WHERE ID_identite=:id_identite", $value);
257
		}
258
259
		/**
260
		 * permet de dire qu'on a vue une notification dans l'administration du site internet
261
		 */
262
		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...
263
			$dbc = App::getDb();
264
265
			$value = [
266
				"admin" => 0,
267
				"id" => 1
268
			];
269
270
			$dbc->prepare("UPDATE notification SET admin=:admin WHERE ID_notification=:id", $value);
271
		}
272
		//-------------------------- FIN SETTER ----------------------------------------------------------------------------//
273
274
275
276
	}