-
Notifications
You must be signed in to change notification settings - Fork 1
/
login.php
executable file
·137 lines (126 loc) · 3.97 KB
/
login.php
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
<?php
session_start();
include('includes/globals.php');
include('includes/functions.php');
$database = new Database();
$username = trim($_POST['username']);
$passwd = trim($_POST['password']);
$ipaddress = $_SERVER["REMOTE_ADDR"];
$captcha_test=intval($_POST['captcha_challenge']);
$sql="INSERT INTO LoginAttempts (ip,attempts,login,Last) VALUES ( :ipaddress, :attempts, :username, NOW())";
$database->query($sql);
$database->bind(':ipaddress', $ipaddress);
$database->bind(':attempts', 1);
$database->bind(':username', $username);
$database->execute();
//Check IP
$ipcheck=checkIP($ipaddress, $username);
if($ipcheck==true) {
$sql="SELECT * FROM users WHERE user_login=:user_login";
$database->query($sql);
$database->bind(':user_login', $username);
$row_user = $database->single();
$check_passwd = password_verify($passwd, $row_user['user_passwd']);
if ($check_passwd) {
//Cookie
if ($_POST["remember"]=="1") {
$identifier = hash('sha256', $row_user['user_id'].KEY);
$token = md5(uniqid(rand(), TRUE));
$timeout = time() + 60 * 60 * 24 * 365;
$date = date("Y-m-d H:i:s", $timeout);
setcookie("oauth", "$identifier:$token", $timeout);
$sql="INSERT INTO auth_tokens (identifier,token,userid,expires) VALUES (:identifier,:token, :userid, :expires)";
$database->query($sql);
$database->bind(':identifier', $identifier);
$database->bind(':token', $token);
$database->bind(':userid', $row_user['user_id']);
$database->bind(':expires', $date);
$database->execute();
}
$_SESSION['id'] = $row_user['user_id'];
$_SESSION['login'] = $row_user['user_login'];
$_SESSION['fullname'] = $row_user['user_fullname'];
//Avatar Fix
if($row_user['user_avatar']=="") {
$_SESSION['avatar']=AVATAR_DIR."/avatar_default.jpg";
} else {
$_SESSION['avatar'] = AVATAR_DIR."/".$row_user['user_avatar'];
}
//end avatar
$_SESSION['level'] = $row_user['user_level'];
$_SESSION['logged'] = 1;
//Clear IP log
$sql="DELETE FROM LoginAttempts WHERE ip=:ipaddress";
$database->query($sql);
$database->bind(':ipaddress', $ipaddress);
$database->execute();
$sql="OPTIMIZE TABLE `LoginAttempts`";
$database->query($sql);
$database->execute();
header("Location: dashboard.php");
exit();
} else {
$_SESSION['logged'] = 0;
header("Location: index.php?e=1");
exit();
}
} else {
//Viene con captcha
if($captcha_test==1) {
include_once 'securimage/securimage.php';
$securimage = new Securimage();
if ($securimage->check($_POST['captcha_code']) == false) {
//captcha mal
$_SESSION['logged'] = 0;
$_SESSION['captcha'] = 1;
header("Location: login.php");
exit();
} else {
//captcha bien
$sql="SELECT * FROM users WHERE user_login=:user_login";
$database->query($sql);
$database->bind(':user_login', $username);
$row_user = $database->single();
$check_passwd = password_verify($passwd, $row_user['user_passwd']);
//Acceso
if ($check_passwd) {
$_SESSION['id'] = $row_user['user_id'];
$_SESSION['login'] = $row_user['user_login'];
$_SESSION['fullname'] = $row_user['user_fullname'];
//Avatar Fix
if($row_user['user_avatar']=="") {
$_SESSION['avatar']=AVATAR_DIR."/avatar_default.jpg";
} else {
$_SESSION['avatar'] = AVATAR_DIR."/".$row_user['user_avatar'];
}
//end avatar
$_SESSION['level'] = $row_user['user_level'];
$_SESSION['logged'] = 1;
$_SESSION['captcha'] = 0;
//Clear IP log
$sql="DELETE FROM LoginAttempts WHERE ip=:ipaddress";
$database->query($sql);
$database->bind(':ipaddress', $ipaddress);
$database->execute();
$sql="OPTIMIZE TABLE `LoginAttempts`";
$database->query($sql);
$database->execute();
//redirect
header("Location: dashboard.php");
exit();
//Mal acceso
} else {
$_SESSION['logged'] = 0;
$_SESSION['captcha'] = 1;
header("Location: login.php");
exit();
}
}
} else {
$_SESSION['logged'] = 0;
$_SESSION['captcha'] = 1;
header("Location: login.php");
exit();
}
}
?>