1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: nazarenko |
5
|
|
|
* Date: 20.10.2014 |
6
|
|
|
* Time: 11:43 |
7
|
|
|
*/ |
8
|
|
|
namespace samsoncms\app\signin; |
9
|
|
|
use samson\social\email\EmailStatus; |
10
|
|
|
use samsonphp\event\Event; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Generic class for user sign in |
14
|
|
|
* @author Olexandr Nazarenko <[email protected]> |
15
|
|
|
* @copyright 2014 SamsonOS |
16
|
|
|
*/ |
17
|
|
|
class Application extends \samson\core\CompressableExternalModule |
18
|
|
|
{ |
19
|
|
|
/** @var string Identifier */ |
20
|
|
|
public $id = 'cms-signin'; |
21
|
|
|
|
22
|
|
|
public function authorize($social) |
23
|
|
|
{ |
24
|
|
|
if (m('cms')->isCMS()) { |
25
|
|
|
if ($social->id == 'socialemail') { |
26
|
|
|
if (!m('social')->authorized()) { |
27
|
|
|
if (!m('socialemail')->cookieVerification()) { |
28
|
|
|
if (!url()->is('cms-signin')) { |
29
|
|
|
url()->redirect('/cms/signin'); |
30
|
|
|
} |
31
|
|
|
} else { |
32
|
|
|
url()->redirect('/cms/signin'); |
33
|
|
|
} |
34
|
|
|
} else { |
35
|
|
|
if (url()->is('cms-signin')) { |
36
|
|
|
url()->redirect('/cms'); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** Check the user's authorization */ |
44
|
|
|
public function __HANDLER() |
45
|
|
|
{ |
46
|
|
|
$this->authorize(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** Main sign in template */ |
50
|
|
|
public function __base() |
51
|
|
|
{ |
52
|
|
|
// m('social')->prepare(); |
53
|
|
|
s()->template('www/signin/signin_template.vphp'); |
54
|
|
|
$result = ''; |
55
|
|
|
$result .= m()->view('www/signin/signin_form.vphp')->output(); |
56
|
|
|
m()->html($result)->title(t('Авторизация', true)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** User asynchronous sign in */ |
60
|
|
|
public function __async_login() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
$user = null; |
63
|
|
|
$remember = false; |
64
|
|
|
$error = ''; |
65
|
|
|
if (isset($_POST['email']) && isset($_POST['password'])) { |
66
|
|
|
$email = md5($_POST['email']); |
67
|
|
|
$password = md5($_POST['password']); |
68
|
|
|
if (isset($_POST['remember'])) { |
69
|
|
|
$remember = true; |
70
|
|
|
} |
71
|
|
|
$auth = m('socialemail')->authorizeWithEmail($email, $password, $remember, $user); |
72
|
|
|
if ($auth->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE) { |
73
|
|
|
if (dbQuery('user')->cond('user_id', $user->id)->first()) { |
|
|
|
|
74
|
|
|
// Fire login success event |
75
|
|
|
Event::fire('samson.cms.signin.login', array(& $user)); |
76
|
|
|
return array('status' => '1'); |
77
|
|
View Code Duplication |
} else { |
|
|
|
|
78
|
|
|
$error .= m()->view('www/signin/signin_form.vphp') |
79
|
|
|
->focus('autofocus') |
80
|
|
|
->errorClass('errorAuth') |
81
|
|
|
->output(); |
82
|
|
|
return array('status' => '0', 'html' => $error); |
83
|
|
|
} |
84
|
|
View Code Duplication |
} else { |
|
|
|
|
85
|
|
|
$error .= m()->view('www/signin/signin_form.vphp') |
86
|
|
|
->errorClass('errorAuth') |
87
|
|
|
->userEmail("{$_POST['email']}") |
88
|
|
|
->focus('autofocus') |
89
|
|
|
->output(); |
90
|
|
|
return array('status' => '0', 'html' => $error); |
91
|
|
|
} |
92
|
|
View Code Duplication |
} else { |
|
|
|
|
93
|
|
|
$error .= m()->view('www/signin/signin_form')->errorClass('errorAuth')->output(); |
94
|
|
|
return array('status' => '0', 'html' => $error); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** User logout */ |
99
|
|
|
public function __logout() |
100
|
|
|
{ |
101
|
|
|
m('socialemail')->deauthorize(); |
102
|
|
|
// Fire logout event |
103
|
|
|
Event::fire('samson.cms.signin.logout'); |
104
|
|
|
//url()->redirect(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** Sending email with the correct address */ |
108
|
|
|
public function __mail() |
109
|
|
|
{ |
110
|
|
|
if (isset($_POST['email'])) { |
111
|
|
|
/** @var \samson\activerecord\user $user */ |
112
|
|
|
$user = null; |
113
|
|
|
$result = ''; |
114
|
|
|
if (dbQuery('user')->email($_POST['email'])->first($user)) { |
115
|
|
|
$user->confirmed = md5(generate_password(20) . time()); |
116
|
|
|
$user->save(); |
117
|
|
|
$message = m()->view('www/signin/email/pass_recovery')->code($user->confirmed)->output(); |
118
|
|
|
|
119
|
|
|
mail_send($user->Email, '[email protected]', $message, t('Восстановление пароля!', true), 'SamsonCMS'); |
120
|
|
|
|
121
|
|
|
$result .= m()->view('www/signin/pass_recovery_mailsend')->output(); |
122
|
|
|
s()->template('www/signin/signin_template.vphp'); |
123
|
|
|
m()->html($result)->title(t('Восстановление пароля', true)); |
124
|
|
|
} else { |
125
|
|
|
url()->redirect(); |
126
|
|
|
} |
127
|
|
|
} else { |
128
|
|
|
url()->redirect(); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* New password form |
134
|
|
|
* @param string $code Code password recovery |
135
|
|
|
*/ |
136
|
|
|
public function __confirm($code) |
137
|
|
|
{ |
138
|
|
|
if (dbQuery('user')->confirmed($code)->first()) { |
139
|
|
|
$result = ''; |
140
|
|
|
$result .= m()->view('www/signin/new_pass_form')->code($code)->output(); |
141
|
|
|
s()->template('www/signin/signin_template.vphp'); |
142
|
|
|
m()->html($result)->title(t('Восстановление пароля', true)); |
143
|
|
|
} else { |
144
|
|
|
e404(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Setting new password and sign in |
150
|
|
|
* @param string $code Code password recovery |
151
|
|
|
*/ |
152
|
|
|
public function __recovery($code) |
153
|
|
|
{ |
154
|
|
|
if (isset($_POST['password']) && isset($_POST['confirm_password']) |
155
|
|
|
&& $_POST['password'] == $_POST['confirm_password'] |
156
|
|
|
) { |
157
|
|
|
/** @var \samson\activerecord\user $user */ |
158
|
|
|
$user = null; |
159
|
|
|
if (dbQuery('user')->confirmed($code)->first($user)) { |
160
|
|
|
$user->confirmed = 1; |
161
|
|
|
$user->md5_password = md5($_POST['password']); |
162
|
|
|
$user->Password = $_POST['password']; |
163
|
|
|
$user->save(); |
164
|
|
|
if (m('socialemail')->authorizeWithEmail($user->md5_email, $user->md5_password, $user) |
165
|
|
|
->code == EmailStatus::SUCCESS_EMAIL_AUTHORIZE |
166
|
|
|
) { |
167
|
|
|
url()->redirect(); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
} else { |
171
|
|
|
$result = ''; |
172
|
|
|
$result .= m()->view('www/signin/pass_error') |
173
|
|
|
->message(t('Вы ввели некорректный пароль либо пароли не совпадают', true)) |
174
|
|
|
->output(); |
175
|
|
|
s()->template('www/signin/signin_template.vphp'); |
176
|
|
|
m()->html($result)->title(t('Ошибка восстановление пароля', true)); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
This check looks for function calls that miss required arguments.