This repository was archived by the owner on May 10, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathapp.php
More file actions
169 lines (151 loc) · 5.08 KB
/
Copy pathapp.php
File metadata and controls
169 lines (151 loc) · 5.08 KB
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
<?php
/**
* Tutorial PHP
*
* @author jslirola <jslirola@andalucia-devs.com>
* @license http://opensource.org/licenses/mit.php MIT License
* @link https://github.com/jslirola/tutorial-php
*/
session_start();
header('Content-Type: text/html; charset=UTF-8');
$config = "include/db.php";
if(file_exists($config))
require_once("include/db.php");
else
die("Debe renombrar el fichero include/db_example.php a include/db.php para iniciar el sistema.");
class Tutorial_PHP
{
private $link;
/**
* Instantiate the connection
*/
public function __construct()
{
if (DB_NAME == "")
die("Configure la conexión a la base de datos en el fichero `include/db.php` antes de continuar.");
else {
$this->link = new mysqli(SERVER_DB, USER_DB, PASS_DB, DB_NAME);
if ($this->link->connect_error)
die("Connect Error (" . $this->link->connect_errno . ") " . $this->link->connect_error);
}
}
/**
* Generate a hash for the password. NEVER IN PLAIN-TEXT!
* @param string $str The original password
* @param string $salt String to reinforce the security
* @return string
*/
public static function genHash($str, $salt = "")
{
$hash = sha1(md5(sha1($salt.$str.$salt)).$salt);
return $hash;
}
/**
* Check if an user exists through an email
* @param string $email The email user
* @return int
*/
private function checkEmail($email)
{
$email = $this->link->real_escape_string($email);
$this->link->query("SELECT email FROM users WHERE email = '$email'");
return $this->link->affected_rows;
}
/**
* Retrieve all data user
* @param string $email The email user
* @return array
*/
private function getUserInfo($email)
{
$email = $this->link->real_escape_string($email);
$result = $this->link->query("SELECT * FROM users WHERE email = '$email'");
$row = $result->fetch_array(MYSQLI_ASSOC);
return $row;
}
/**
* Check if an user is logged
* @return boolean
*/
public function isLogged()
{
return (isset($_SESSION["logged"]) && $_SESSION["logged"] == 1);
}
/**
* Insert new row on table users
* @param string $username The username
* @param string $email The email
* @param string $password The original password
* @return string
*/
public function registerUser($username, $email, $password)
{
if (!empty($email) && $this->checkEmail($email) == 0) {
if (empty($username) || empty($password))
return "Todos los campos son obligatorios.";
$username = $this->link->real_escape_string($username);
$email = $this->link->real_escape_string($email);
$regdate = time();
$password = $this->genHash($password, $regdate);
$ip = $_SERVER["REMOTE_ADDR"];
if ($this->link->query("INSERT INTO users (username, password, email, regdate, ip) VALUES
('$username','$password','$email','$regdate','$ip')"))
return "Alta realizada correctamente.";
else
return "Ha ocurrido un error durante su registro.";
} else
return "El email introducido no es correcto o ya existe.";
}
/**
* Try login a user within the system
* @param string $username The username
* @param string $email The email
* @param string $password The original password
* @return string
*/
public function loginUser($email, $password)
{
if (!empty($email) && $this->checkEmail($email) == 1) {
$email = $this->link->real_escape_string($email);
$info = $this->getUserInfo($email);
if ($info != null) {
$password = $this->genHash($password, $info["regdate"]);
if($password == $info["password"]) {
$_SESSION["logged"] = 1;
$_SESSION["user"] = $info;
return "Se ha identificado correctamente.";
}
}
}
return "El email o la contraseña introducida no son correctos.";
}
/**
* Logout a user
* @return string
*/
public function logout()
{
if ($this->isLogged()) {
session_unset();
session_destroy();
return "Se ha desconectado correctamente.";
} else
return "Ha ocurrido un error al desconectarse.";
}
}
$app = new Tutorial_PHP;
if (isset($_POST["sent"])) { // A form has been sent
if ($_POST["sent"] == "register") {
$msgForm = $app->registerUser($_POST["username"], $_POST["email"], $_POST["password"]);
} elseif ($_POST["sent"] == "login") {
$msgForm = $app->loginUser($_POST["email"], $_POST["password"]);
} elseif ($_POST["sent"] == "logout") {
$msgForm = $app->logout();
} else
die();
} else if (isset($_GET['action'])) {
if ($_GET['action'] == "logout") {
$msgForm = $app->logout();
} else
die();
}