forked from shinovon/mpgram-web
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qrlogin.php
159 lines (148 loc) · 4.38 KB
/
qrlogin.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
session_start();
$logout = isset($_GET['logout']);
if($logout) {
$_SESSION = array();
}
$user = null;
$nouser = true;
$confirm = $_GET['confirm'] ?? $_POST['confirm'] ?? null;
$post = isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER['HTTP_USER_AGENT'], 'Series60/3') === false;
function exceptions_error_handler($severity, $message, $filename, $lineno) {
throw new ErrorException($message, 0, $severity, $filename, $lineno);
}
set_error_handler('exceptions_error_handler');
include 'mp.php';
$user = MP::getUser();
$nouser = $user == null || empty($user) || strlen($user) < 32 || strlen($user) > 200 || !file_exists(sessionspath.$user.'.madeline');
$theme = 0;
$ua = '';
$iev = MP::getIEVersion();
if($iev > 0 && $iev < 4) $theme = 1;
$theme = MP::getSettingInt('theme', $theme);
$lng = MP::initLocale();
MP::cookie('theme', $theme, time() + (86400 * 365));
include 'themes.php';
Themes::setTheme($theme);
function base64_encode_url($string) {
return str_replace(['+','/','='], ['-','_',''], base64_encode($string));
}
function htmlStart() {
global $lng;
header("Content-Type: text/html; charset=utf-8");
echo '<head><title>'.MP::x($lng['login']).'</title>';
echo Themes::head();
echo '</head>';
echo Themes::bodyStart();
}
$MP = null;
if($logout && !$nouser) {
$nouser = true;
$logout = true;
MP::delcookie('user');
MP::delcookie('code');
try {
// Remove all session files
if(file_exists(sessionspath.$user.'.madeline')) {
try {
if(PHP_OS_FAMILY === "Linux") {
exec('kill -9 `ps -ef | grep -v grep | grep '.$user.'.madeline | awk \'{print $2}\'`');
}
} catch (Exception $e) {
}
MP::deleteSessionFile($user);
}
} catch (Exception $e) {
echo $e;
}
}
if($user === null || $nouser) {
if($confirm === null || empty($confirm) || !isset($_SESSION['captcha']) || strtolower($confirm) !== $_SESSION['captcha']) {
unset($_SESSION['captcha']);
htmlStart();
echo 'CAPTCHA:<br>';
echo '<p><img src="captcha.php?r='.time().'"></p>';
echo '<form action="qrlogin.php"'.($post?' method="post"':'').'>';
echo '<input type="text" value="" name="confirm">';
echo '<input type="submit">';
echo '</form>';
echo Themes::bodyEnd();
die;
} else {
unset($_SESSION['captcha']);
$user = 'qr_'.hash('sha384', sha1(random_bytes(32).rand(0,1000)).sha1(random_bytes(32)));
MP::cookie('user', $user, time() + (86400 * 365));
$MP = MP::getMadelineAPI($user, true);
}
} else {
$MP = MP::getMadelineAPI($user, true);
}
$qr = $MP->qrLogin();
if(!$qr) {
if(isset($_POST['pass']) || isset($_GET['pass'])) {
// 2fa check
try {
$password = null;
if(isset($_POST['pass'])) {
$password = $_POST['pass'];
} elseif(isset($_GET['pass'])) {
$password = $_GET['pass'];
}
$MP->complete2faLogin($password);
unset($_SESSION['qr_token']);
MP::cookie('code', '1', time() + (86400 * 365));
header('Location: chats.php');
die;
} catch (Exception $e) {
if(strpos($e->getMessage(), 'PASSWORD_HASH_INVALID') !== false) {
htmlStart();
echo MP::x($lng['pass_code']).':<br>';
echo '<form action="qrlogin.php"'.($post?' method="post"':'').'>';
echo '<input type="text" name="pass">';
if($phone !== null)
echo '<input type="hidden" name="phone" value="'.$phone.'">';
echo '<input type="submit">';
echo '</form>';
echo '<b>'.MP::x($lng['password_hash_invalid']).'</b><br>';
echo Themes::bodyEnd();
die;
} else {
echo '<xmp>';
echo $e;
echo '</xmp>';
die;
}
die;
}
}
if ($MP->getAuthorization() === \danog\MadelineProto\API::WAITING_PASSWORD) {
// 2fa start
htmlStart();
echo MP::x($lng['pass_code']).':<br>';
echo '<form action="qrlogin.php"'.($post?' method="post"':'').'>';
echo '<input type="text" name="pass">';
if($phone !== null)
echo '<input type="hidden" name="phone" value="'.$phone.'">';
echo '<input type="submit">';
echo '</form>';
echo Themes::bodyEnd();
die;
}
if(isset($_SESSION['qr_token'])) {
unset($_SESSION['qr_token']);
MP::cookie('user', $user, time() + (86400 * 365));
MP::cookie('code', '1', time() + (86400 * 365));
}
header('Location: chats.php');
die;
}
$qrtext = $qr->{'link'};
$_SESSION['qr_token'] = base64_encode($qrtext);
htmlStart();
echo $qrtext;
echo '<br><img src="qrcode.php"><br>';
echo '<a href="qrlogin.php?check">Check</a>';
echo Themes::bodyEnd();