Nextrek
@ 374
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 |
<?php require_once('UserRepositoryInterface.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, is_owner) VALUES ('".$user->username."', '".$hashedPwd."', '".$user->nominativo."', '".$user->cell."', '".$user->email."', '".$user->is_owner."')"; //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 setAvatar($user_id, $avatar_url) { $status = array(); try { $query = "UPDATE utente SET avatar='".$avatar_url."' WHERE id=".$user_id; $this->db->exec($query); $status["return"] = 0; $status["avatar_path"] = $avatar_url; return $status; } catch (PDOException $e) { echo $e->getMessage(); $status["return"] = 1; $status["avatar_path"] = ""; return $status; } } } ?> |
Commits for Nextrek/Android/SmartCharging/endPoints/nightly/UserRepository.php
Revision | Author | Commited | Message |
---|---|---|---|
374 | FSallustio | Fri 17 Jul, 2015 08:20:18 +0000 | Creazione “branch” nightly |