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
<?php
	require_once('UserRepositoryInterface.php');
	require_once(dirname(__FILE__)."/../constants.php");

	class UserRepository implements UserRepositoryInterface {
		protected $db;

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

		public static function checkToken($token, $conn) {
			try {
				$queryToken = "SELECT id FROM utente WHERE token='".$token."'";
				//echo "<br/>".$queryToken."<br/>";
				$result = $conn->query($queryToken);
				return ($result->rowCount()>0);
			} catch (PDOException $e) {
				echo $e->getMessage();
				return false;
			}
		}

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

				if (!$this->checkUserName($user)) {
					$status["id"] = -1;
					$status["return"] = 1;
					return $status;
				}

				if (!$this->checkEmail($user)) {
					$status["id"] = -1;
					$status["return"] = 2;
					return $status;
				}

				$hashedPwd = password_hash($password, PASSWORD_DEFAULT);

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

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

		}

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

			try {
				$query = "SELECT id, password, email FROM utente WHERE username='".$username."'";
				$result = $this->db->query($query);
				$row = ($result->rowCount()>0) ? $result->fetch() : null;

				if (!is_null($row) && password_verify($password, $row["password"])) {
					$newToken = sha1($row["email"].session_id().time());

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

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

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

					$status["return"] = 0;
					$status["id"] = $row["id"];
					$status["token"] = $newToken;
				} else {
					$status["return"] = 1;
					$status["id"] = -1;
					$status["token"] = "";
				}

				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$status["return"] = 2;
				$status["id"] = -1;
				$status["token"] = "";
				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."'";
				$result = $this->db->query($query);
				$row = ($result->rowCount()>0) ? $result->fetch() : null;

				if (!is_null($row)) {
					$status["return"] = 0;
					$status["id"] = $user_id;
					$status["avatar"] = AVATAR_PATH.$row["avatar"];
				} else {
					$status["return"] = 1;
					$status["id"] = -1;
					$status["avatar"] = "";
				}

				return $status;

			} catch (PDOException $e) {
				echo $e->getMessage();
				$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/>";	

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

	}

?>

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

Diff revisions: vs.
Revision Author Commited Message
415 FSallustio picture FSallustio Wed 22 Jul, 2015 08:27:42 +0000

Piccolo refactor dei file php...