Passed
Push — master ( 9db407...b7ae74 )
by Anthony
02:58
created

Admin::setArchiverCompte()   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)) {
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
			$config = new Configuration();
60
61
			if ((is_array($query)) && (count($query) > 0)) {
62
				$valide = "";
63
				$id_identite = [];
64
				$nom = [];
65
				$prenom = [];
66
				$pseudo = [];
67
				$mail = [];
68
				$img_profil = [];
69
70
				foreach ($query as $obj) {
71
					$id_identite[] = $obj->ID_identite;
72
					$nom[] = $obj->nom;
73
					$prenom[] = $obj->prenom;
74
					$pseudo[] = $obj->pseudo;
75
					$mail[] = $obj->mail;
76
77
					if ($obj->img_profil == "") {
78
						$img_profil[] = "profil/defaut.png";
79
					}
80
					else {
81
						$img_profil[] = $obj->img_profil;
82
					}
83
84
					if ($config->getValiderInscription() == 1) {
85
						if ($obj->valide == 0) {
86
							$valide[] = "<a href=".ADMWEBROOT."controller/core/admin/comptes/valider_compte?id_identite=$obj->ID_identite>Valider cet utilisateur</a>";
87
						}
88
						else {
89
							$valide[] = "Utilisateur validé";
90
						}
91
					}
92
					else {
93
						$valide = "";
94
					}
95
				}
96
97
				$this->setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide);
98
			}
99
		}
100
101
		/**
102
		 * Fonctio qui premet de setter les différents élément d'un user
103
		 * @param $id_identite
104
		 */
105
		public function getunUser($id_identite) {
106
			$dbc = \core\App::getDb();
107
108
			$query = $dbc->query("SELECT * FROM identite WHERE ID_identite=".$id_identite);
109
110
			foreach ($query as $obj) {
0 ignored issues
show
Bug introduced by
The expression $query of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
111
				$this->id_identite = $obj->ID_identite;
112
				$this->nom = $obj->nom;
113
				$this->prenom = $obj->prenom;
114
				$this->img = $obj->img_profil;
115
				$this->mail = $obj->mail;
116
				$this->valide = $obj->valide;
117
			}
118
		}
119
120
		/**
121
		 * fonction qui si égale a 1 alors il y a une notification dans l'admin du site
122
		 * @return mixed
123
		 */
124
		public function getNotification() {
125
			$dbc = App::getDb();
126
127
			$query = $dbc->query("SELECT admin FROM notification");
128
129
			if (count($query) == 1) {
130
				foreach ($query as $obj) {
0 ignored issues
show
Bug introduced by
The expression $query of type array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
131
					return $obj->admin;
132
				}
133
			}
134
		}
135
		//-------------------------- FIN GETTER ----------------------------------------------------------------------------//
136
137
138
139
		//-------------------------- SETTER ----------------------------------------------------------------------------//
140
		private function setAllUser($id_identite, $nom, $prenom, $mail, $pseudo, $img_profil, $valide) {
141
			$this->id_identite = $id_identite;
142
			$this->nom = $nom;
143
			$this->prenom = $prenom;
144
			$this->mail = $mail;
145
			$this->pseudo = $pseudo;
146
			$this->img = $img_profil;
147
			$this->valide = $valide;
148
		}
149
150
		/**
151
		 * Fonction qui permet de valider un compte utilisateur pour qu'il puisse se conecter au site
152
		 * @param $id_identite
153
		 */
154
		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...
155
			$dbc = \core\App::getDb();
156
157
			$value = array("id_identite" => $id_identite);
158
159
			$dbc->prepare("UPDATE identite SET valide=1 WHERE ID_identite=:id_identite", $value);
160
161
			$this->getunUser($id_identite);
162
		}
163
164
		/**
165
		 * fonction quir genere un mot de passe aleatoire pour le compte spécifié en param
166
		 * @param $id_identite
167
		 */
168
		public function setReinitialiserMdp($id_identite) {
169
			$dbc = \core\App::getDb();
170
171
			$this->getunUser($id_identite);
172
173
			if (($this->mail != "") || ($this->mail != null)) {
174
				$mdp = ChaineCaractere::random(6);
175
				$mdp_encode = Encrypt::setEncryptMdp($mdp, $id_identite);
176
177
				$value = array(
178
					"mdp" => $mdp_encode,
179
					"id_identite" => $id_identite,
180
					"last_change_mdp" => date("Y-m-d")
181
				);
182
183
				FlashMessage::setFlash("Mot de passe réinitialisé avec succès ! L'utilisateur à reçu un E-mail avec son nouveau mot de passe", "success");
184
185
				$dbc->prepare("UPDATE identite SET mdp=:mdp, last_change_mdp=:last_change_mdp WHERE ID_identite=:id_identite", $value);
186
187
				$mail = new Mail($this->mail);
188
				$mail->setEnvoyerMail("Réinitialisation de votre E-mail effectuée", "Votre mot de passe a été réinitialisé");
189
			}
190
			else {
191
				FlashMessage::setFlash("le mot de passe de $this->pseudo ne peu pas être réinitialisé car il ne possède pas d'E-mail");
192
				$this->erreur = true;
193
			}
194
		}
195
196
		/**
197
		 * Supprime le compte en question et enleve l'image de profil aussi
198
		 * @param $id_identite
199
		 */
200
		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...
201
			$dbc = \core\App::getDb();
202
203
			$value = array(
204
				"id_identite" => $id_identite,
205
				"archiver" => 1
206
			);
207
208
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
209
		}
210
211
		/**
212
		 * Supprime le compte en question et enleve l'image de profil aussi
213
		 * @param $id_identite
214
		 */
215
		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...
216
			$dbc = \core\App::getDb();
217
218
			$value = array(
219
				"id_identite" => $id_identite,
220
				"archiver" => NULL
221
			);
222
223
			$dbc->prepare("UPDATE identite SET archiver=:archiver WHERE ID_identite=:id_identite", $value);
224
		}
225
226
		/**
227
		 * Supprime le compte en question et enleve l'image de profil aussi
228
		 * @param $id_identite
229
		 */
230
		public function setSupprimerCompte($id_identite) {
231
			$dbc = \core\App::getDb();
232
233
			$oldimg_profil = "";
234
235
			//test si il y a deja une img
236
			$query = $dbc->query("SELECT img_profil FROM identite where ID_identite=$id_identite");
237
238
			if ((is_array($query)) && (count($query) > 0)) {
239
				foreach ($query as $obj) {
240
					$oldimg_profil = $obj->img_profil;
241
				}
242
			}
243
244
245
			if ($oldimg_profil != "") {
246
				$oldimg_profil = explode("/", $oldimg_profil);
247
				if (end($oldimg_profil) != "defaut.png") {
248
					unlink("../../images/profil/".$oldimg_profil[7]);
249
				}
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
	}