Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<?php
	require_once('UserRepositoryInterface.php');
	require_once(dirname(__FILE__)."/../constants.php");
	require_once(dirname(__FILE__)."/../utilities.php");
	define('FACEBOOK_SDK_V4_SRC_DIR', dirname(__FILE__).'/../fb_SDK/src/Facebook/');
	require_once(dirname(__FILE__)."/../fb_SDK/autoload.php");
	use Facebook\FacebookSession;
	use Facebook\FacebookRedirectLoginHelper;
	use Facebook\FacebookRequest;
	use Facebook\FacebookResponse;
	use Facebook\FacebookSDKException;
	use Facebook\FacebookRequestException;
	use Facebook\FacebookAuthorizationException;
	use Facebook\GraphObject;

	class UserRepository implements UserRepositoryInterface {
		protected $db;

		public function __construct($db) {
			$this->db = $db;
		}

		public static function checkToken($token, $conn) { //utile per il control panel
			try {
				//$queryToken = "SELECT id FROM utente WHERE token='".$token."'";
				$queryToken = "SELECT id FROM utente WHERE token=:token";
				$stmt = $conn->prepare($queryToken);
				$stmt->bindParam(':token', $token);
				$result = $stmt->execute();
				return ($stmt->rowCount()>0);
			} catch (PDOException $e) {
				echo $e->getMessage();
				return false;
			}
		}

		public static function checkUserByToken($user_id, $token, $conn) {
			try {
				//$queryToken = "SELECT id FROM utente WHERE token='".$token."'";
				$queryToken = "SELECT id FROM utente WHERE token=:token AND id=:id";
				$stmt = $conn->prepare($queryToken);
				$stmt->bindParam(':token', $token);
				$stmt->bindParam(':id', $user_id);
				$result = $stmt->execute();
				return ($stmt->rowCount()>0);
			} catch (PDOException $e) {
				echo $e->getMessage();
				return false;
			}
		}

		public static function checkAdminByToken($token, $conn) {
			try {
				//$queryToken = "SELECT id FROM utente WHERE token='".$token."'";
				$queryToken = "SELECT id FROM utente WHERE token=:token AND is_admin=1";
				$stmt = $conn->prepare($queryToken);
				$stmt->bindParam(':token', $token);
				$result = $stmt->execute();
				return ($stmt->rowCount()>0);
			} catch (PDOException $e) {
				echo $e->getMessage();
				return false;
			}
		}

		public static function getUserByToken($token, $conn) {
			try {
				//$queryToken = "SELECT id FROM utente WHERE token='".$token."'";
				$queryToken = "SELECT id FROM utente WHERE token=:token";
				$stmt = $conn->prepare($queryToken);
				$stmt->bindParam(':token', $token);
				$result = $stmt->execute();
				return ($stmt->rowCount()>0) ? $stmt->fetchColumn() : -1;
			} catch (PDOException $e) {
				echo $e->getMessage();
				return -1;
			}
		}

		public function registerUser($user, $password) {
			$status = array();
			
			try {

				if (!$this->checkUserName($user)) { //username già registrato
					$status = createErrorMessage(1, "Username già registrato");
					$status["id"] = -1;
					$status["return"] = 1;
					$status["token"] = "";
					return $status;
				}

				if (!$this->checkEmail($user)) { //email già registrata
					$status = createErrorMessage(2, "Email già registrata");
					$status["id"] = -1;
					//$status["return"] = 2;	
					$status["token"] = "";
					return $status;
				}

				$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
				$newToken = sha1($user->username.session_id().time());

				/*$query = "INSERT INTO utente(username, password, nome, cognome, cell, email, token)
	    				VALUES ('".$user->username."',
	    						'".$hashedPwd."',
	    						'".$user->nome."',
	    						'".$user->cognome."',
	    						'".$user->cell."',
	    						'".$user->email."',
	    						'".$newToken."')";*/
				
				//echo "<br/>".$query."<br/>";

				$query = "INSERT INTO utente(username, password, nome, cognome, cell, email, token)
	    				VALUES (:username, :password, :nome, :cognome, :cell, :email, :token)";

	    		$stmt = $this->db->prepare($query);

				$stmt->bindParam(':username', $user->username);
				$stmt->bindParam(':password', $hashedPwd);
				$stmt->bindParam(':nome', $user->nome);
				$stmt->bindParam(':cognome', $user->cognome);
				$stmt->bindParam(':cell', $user->cell);
				$stmt->bindParam(':email', $user->email);
				$stmt->bindParam(':token', $newToken);

				$result = $stmt->execute();

				//$this->db->exec($query);
				$status["id"] = $this->db->lastInsertId();
				$status["return"] = 0;
				$status["token"] = $newToken;
				return $status;
				//echo "New record created successfully";
			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(3, "Errore DB");
				$status["id"] = -1;
				//$status["return"] = 3;
				$status["token"] = "";
				return $status;
			}

		}

		public function loginUser($username, $password, $is_admin) {
			$status = array();

			try {
				//$query = "SELECT id, password, email, is_owner FROM utente WHERE username='".$username."' AND is_active=1";

				$query = "SELECT id, password, email, is_owner FROM utente WHERE username=:username AND is_active=1";

				if ($is_admin) {
					$query = $query." AND is_admin=1";
				}

				$stmt = $this->db->prepare($query);
				$stmt->bindParam(':username', $username);
				$result = $stmt->execute();

				/*$result = $this->db->query($query);
				$row = ($result->rowCount()>0) ? $result->fetch() : null;*/

				$row = ($stmt->rowCount()>0) ? $stmt->fetch() : null;

				if (is_null($row)) {
					$status = createErrorMessage(1, "Username non valido");
					//$status["return"] = 1;
					$status["id"] = -1;
					$status["token"] = "";
				} else if (!password_verify($password, $row["password"])) {
					$status = createErrorMessage(1, "Password non valida");
					//$status["return"] = 1;
					$status["id"] = -1;
					$status["token"] = "";
				} else {
					//$newToken = sha1($row["email"].session_id().time());
					$newToken = sha1($username.session_id().time());

					/*$queryToken = "UPDATE utente
									SET token='".$newToken."'
									WHERE id=".$row["id"];*/

					//echo "<br/>".$queryToken."<br/>";

					$queryToken = "UPDATE utente SET token=:token WHERE id=:id";

					//$this->db->exec($queryToken);

					$stmt = $this->db->prepare($queryToken);
					$stmt->bindParam(':token', $newToken);
					$stmt->bindParam(':id', $row["id"]);
					$stmt->execute();

					$status["return"] = 0;
					$status["is_owner"] = $row["is_owner"];
					$status["id"] = $row["id"];
					$status["token"] = $newToken;
				}

				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(2, "Errore DB");
				//$status["return"] = 2;
				$status["id"] = -1;
				$status["token"] = "";
				return $status;
			}
		}

		public function logoutUser($user_id) {
			$status = array();
			try {
				/*$queryToken = "UPDATE utente
								SET token = ''
								WHERE id=".$user_id;*/

				//echo "<br/>".$queryToken."<br/>";

				$queryToken = "UPDATE utente SET token='' WHERE id=:id";

				//$this->db->exec($queryToken);

				$stmt = $this->db->prepare($queryToken);
				$stmt->bindParam(':id', $user_id);
				$stmt->execute();

				$status["return"] = 0;
				return $status;
			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				return $status;
			}
		}

		public function facebookLogin($fb_user, $fb_token, $user_email = null, $username = null) {
			$status = array();

			FacebookSession::setDefaultApplication(
	            '1444964019146825', //$appId
	            '1596f53e44c38ff927a9491ec6bff882' //$appSecret
	        );

	        try {
	        	$session = new FacebookSession($fb_token);
            	$session->validate();

            	if (!isset($session)) {
            		$status = createErrorMessage(1, "Sessione FB non valida");
            		//$status["return"] = 1;
					$status["id"] = -1;
					$status["token"] = "";
					return $status;	
            	}

            	//richiesta alla facebook graph, richiedendo id_utente, nome ed email
	            $request     = new FacebookRequest($session, 'GET', '/me?fields=id,first_name,last_name,email');
	            $response    = $request->execute();
	            $graphObject = $response->getGraphObject();

	            if ($fb_user != $graphObject->getProperty('id')) { //Il token ricevuto non coincide con quello di FB
	            	$status = createErrorMessage(1, "Il token ricevuto non coincide con quello di FB");
	            	//$status["return"] = 1;
					$status["id"] = -1;
					$status["token"] = "";
					return $status;
	            }

	            $u_first_name  = $graphObject->getProperty('first_name');
	            $u_last_name  = $graphObject->getProperty('last_name');
            	$u_email = $graphObject->getProperty('email');

            	//$sql = "SELECT id, username FROM utente WHERE email='".$user_email."'";
            	$sql = "SELECT id, username FROM utente WHERE email=:email";

            	//$result = $this->db->query($sql);

            	$stmt = $this->db->prepare($sql);
				$stmt->bindParam(':email', is_null($user_email) ? $u_email : $user_email);
				$stmt->execute();

				$row = ($stmt->rowCount()>0) ? $stmt->fetch() : null;

				if (!is_null($row)) { //utente già esistente

					/*$sql = "UPDATE utente
							SET nome='".$u_first_name."', cognome='".$u_last_name."', email='".$u_email."', token='".$fb_token."'
							WHERE id=".$row["id"];*/

					$sql = "UPDATE utente
							SET nome=:nome, cognome=:cognome, email=:email, token=:token
							WHERE id=:id";

					$stmt = $this->db->prepare($sql);
					$stmt->bindParam(':nome', $u_first_name);
					$stmt->bindParam(':cognome', $u_last_name);
					$stmt->bindParam(':email', $u_email);
					$stmt->bindParam(':token', $fb_token);
					$stmt->bindParam(':id', $row["id"]);
					$stmt->execute();

					//$this->db->exec($sql);
					$status["id"] = $row["id"];
					$status["username"] = $row["username"];

				} else { //creazione nuova utenza

					$u_id = $graphObject->getProperty('id');
					
					$fb_avatar_data = file_get_contents("http://graph.facebook.com/".$u_id."/picture?type=large");

        			$image_filename = uniqid().".jpg";

        			$file = fopen($_SERVER['DOCUMENT_ROOT'].AVATAR_PATH.$image_filename, 'w+');
					fputs($file, $fb_avatar_data);
					fclose($file);

					/*$sql = "INSERT INTO utente (nome,cognome,email,token,username,avatar)
							VALUES ('".$u_first_name."','".$u_last_name."','".$u_email."','".$fb_token."','".$username."','".$image_filename."')";*/

					//echo "<br/>".$sql."<br/>";

					$sql = "INSERT INTO utente (nome,cognome,email,token,username,avatar)
							VALUES (:nome,:cognome,:email,:token,:username,:avatar)";

					$usernameToPut = (is_null($username)) ? $u_first_name."$".uniqid() : $username."$".uniqid();

					$stmt = $this->db->prepare($sql);
					$stmt->bindParam(':nome', $u_first_name);
					$stmt->bindParam(':cognome', $u_last_name);
					$stmt->bindParam(':email', $u_email);
					$stmt->bindParam(':token', $fb_token);
					$stmt->bindParam(':username', $usernameToPut);
					$stmt->bindParam(':avatar', $image_filename);
					$stmt->execute();

					//$this->db->exec($sql);
					$status["id"] = $this->db->lastInsertId();
					$status["username"] = $usernameToPut;

				}

				$status["return"] = 0;
				$status["email"] = $u_email;
				$status["token"] = $fb_token;		
                return $status;

	        } catch (Exception $e) {
	        	echo $e->getMessage();
	        	$status = createErrorMessage(1, "Errore DB");
	        	//$status["return"] = 1;
				$status["id"] = -1;
				$status["token"] = "";
				$status["email"] = "";
				$status["username"] = "";
				return $status;
	        }
		}

		//true se username è disponibile, false altrimenti
		protected function checkUserName($user) {
			$query = "SELECT count(*) from utente WHERE username='".$user->username."'";
			$result = $this->db->query($query);
			return ($result->fetchColumn() == "0");
		}

		//true se email è disponibile, false altrimenti
		protected function checkEmail($user) {
			$query = "SELECT count(*) from utente WHERE email='".$user->email."'";
			$result = $this->db->query($query);
			return ($result->fetchColumn() == "0");
		}

		public function getAvatar($user_id) {
			$status = array();
			try {
				//$query = "SELECT avatar FROM utente WHERE id='".$user_id."'";
				$query = "SELECT avatar FROM utente WHERE id=:id";

				$stmt = $this->db->prepare($query);
				$stmt->bindParam(':id', $user_id);
				$result = $stmt->execute();

				//$result = $this->db->query($query);
				$row = ($stmt->rowCount()>0) ? $stmt->fetch() : null;

				if (!is_null($row)) {
					$status["return"] = 0;
					$status["id"] = $user_id;
					$status["avatar"] = ($row["avatar"] != "") ? IMAGE_SERVER.AVATAR_PATH.$row["avatar"] : "";
				} else {
					$status = createErrorMessage(1, "User ID non disponibile");
					//$status["return"] = 1;
					$status["id"] = -1;
					$status["avatar"] = "";
				}

				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				$status["id"] = -1;
				$status["avatar"] = "";
				return $status;
			}
		}

		public function setAvatar($user_id, $avatar_url) {
			$status = array();

			try {
				/*$query = "UPDATE utente
							SET avatar='".$avatar_url."'
	    					WHERE id=".$user_id;*/

	    		//echo "<br/>".$query."<br/>";

	    		$query = "UPDATE utente SET avatar=:avatar WHERE id=:id";

	    		$stmt = $this->db->prepare($query);
	    		$stmt->bindParam(':avatar', $avatar_url);
				$stmt->bindParam(':id', $user_id);
				$stmt->execute();

				//$this->db->exec($query);
				$status["return"] = 0;
				$status["avatar_name"] = $avatar_url;
				return $status;
			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				$status["avatar_name"] = "";
				return $status;
			}
		}

		public function editUser($user) { //$user è l'oggetto Utente
			$status = array();
			$array_params = array();

			try {

				$setString = "";

				if (isset($user->nome) && !is_null($user->nome)) {
					//$setString = ($setString == "") ? "nome='".$user->nome."'" : $setString.",nome='".$user->nome."'";
					$setString = ($setString == "") ? "nome=:nome" : $setString.",nome=:nome";
					$array_params[":nome"] = $user->nome;
				}

				if (isset($user->cognome) && !is_null($user->cognome)) {
					//$setString = ($setString == "") ? "cognome='".$user->cognome."'" : $setString.",cognome='".$user->cognome."'";
					$setString = ($setString == "") ? "cognome=:cognome" : $setString.",cognome=:cognome";
					$array_params[":cognome"] = $user->cognome;
				}

				if (isset($user->cell) && !is_null($user->cell)) {
					//$setString = ($setString == "") ? "cell='".$user->cell."'" : $setString.",cell='".$user->cell."'";
					$setString = ($setString == "") ? "cell=:cell" : $setString.",cell=:cell";
					$array_params[":cell"] = $user->cell;
				}

				if (isset($user->email) && !is_null($user->email)) {
					//$setString = ($setString == "") ? "email='".$user->email."'" : $setString.",email='".$user->email."'";
					$setString = ($setString == "") ? "email=:email" : $setString.",email=:email";
					$array_params[":email"] = $user->email;
				}

				/*$query = "UPDATE utente
							SET ".$setString."
	    					WHERE id=".$user->id;*/

	    		//echo "<br/>".$query."<br/>";

				//$this->db->exec($query);

	    		$query = "UPDATE utente
							SET ".$setString."
	    					WHERE id=:id";

	    		$array_params[":id"] = $user->id;

	    		$stmt = $this->db->prepare($query);
				$stmt->execute($array_params);

				$status["return"] = 0;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				return $status;
			}

			return $status;

		}

		public function deleteUser($user_id) {
			$status = array();
			/*$query = "UPDATE utente,locale
						SET utente.is_active=0, utente.token='', locale.hidden=1 
						WHERE utente.id=".$user_id." AND locale.id_utente_owner=".$user_id;*/

			$queryUtente = "UPDATE utente
						SET is_active=0, token=''
						WHERE id=:id_utente";

			try {
				$stmt = $this->db->prepare($queryUtente);
	    		$stmt->bindParam(':id_utente', $user_id);
				$resultUtente = $stmt->execute();

				if ($resultUtente) {
					$queryLocale = "UPDATE locale
									SET hidden=1 
									WHERE id_utente_owner=:id_utente_owner";

					$stmt = $this->db->prepare($queryLocale);
					$stmt->bindParam(':id_utente_owner', $user_id);
					$stmt->execute();
				} else {
					$status = createErrorMessage(1, "Errore DB");
					return $status;
				}
				//$this->db->exec($query);
				$status["return"] = 0;
				return $status;
			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				return $status;
			}
		}

		public function enableUser($user_id) {
			$status = array();
			/*$query = "UPDATE utente,locale
						SET utente.is_active=1, locale.hidden=0
						WHERE utente.id=".$user_id." AND locale.id_utente_owner=".$user_id;*/

			$queryUtente = "UPDATE utente
						SET is_active=1
						WHERE id=:id_utente";

			try {
				$stmt = $this->db->prepare($queryUtente);
	    		$stmt->bindParam(':id_utente', $user_id);
				$resultUtente = $stmt->execute();

				if ($resultUtente) {
					$queryLocale = "UPDATE locale
									SET hidden=0
									WHERE id_utente_owner=:id_utente_owner";

					$stmt = $this->db->prepare($queryLocale);
					$stmt->bindParam(':id_utente_owner', $user_id);
					$stmt->execute();
				} else {
					$status = createErrorMessage(1, "Errore DB");
					return $status;
				}

				//$this->db->exec($query);
				$status["return"] = 0;
				return $status;
			} catch (PDOException $e) {
				echo $e->getMessage();
				$status = createErrorMessage(1, "Errore DB");
				//$status["return"] = 1;
				return $status;
			}
		}

	}

?>

Commits for Nextrek/Android/SmartCharging/endPoints/repositories/UserRepository.php

Diff revisions: vs.
Revision Author Commited Message
764 Diff Diff FSallustio picture FSallustio Tue 22 Sep, 2015 09:56:52 +0000
763 Diff Diff FSallustio picture FSallustio Tue 22 Sep, 2015 09:46:08 +0000

Aggiunto campo “email” nell’output della login_facebook.php

730 Diff Diff FSallustio picture FSallustio Tue 15 Sep, 2015 08:03:22 +0000
728 Diff Diff FSallustio picture FSallustio Mon 14 Sep, 2015 06:08:22 +0000
630 Diff Diff FSallustio picture FSallustio Tue 01 Sep, 2015 09:59:59 +0000
626 Diff Diff FSallustio picture FSallustio Tue 01 Sep, 2015 07:42:57 +0000
612 Diff Diff FSallustio picture FSallustio Mon 31 Aug, 2015 08:38:16 +0000

Locali nascosti quando l’utente proprietario viene “rimosso”/disabilitato.

579 Diff Diff FSallustio picture FSallustio Thu 27 Aug, 2015 08:25:11 +0000
510 Diff Diff FSallustio picture FSallustio Thu 20 Aug, 2015 12:50:58 +0000

Merge ramo stable con la nightly.

507 FSallustio picture FSallustio Thu 20 Aug, 2015 07:51:22 +0000

Merge ramo stable con la nightly.