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
|
|
|
|
10
|
|
|
use samson\activerecord\dbQuery; |
11
|
|
|
use samson\cms\CMS; |
12
|
|
|
use samson\social\email\EmailStatus; |
13
|
|
|
use samsonframework\core\RequestInterface; |
14
|
|
|
use samsonframework\core\ResourcesInterface; |
15
|
|
|
use samsonframework\core\SystemInterface; |
16
|
|
|
use samsonphp\event\Event; |
17
|
|
|
use samsonframework\orm\QueryInterface; |
18
|
|
|
use samson\social\email\Email; |
19
|
|
|
use samson\core\Core; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Generic class for user sign in |
23
|
|
|
* @author Olexandr Nazarenko <[email protected]> |
24
|
|
|
* @copyright 2014 SamsonOS |
25
|
|
|
*/ |
26
|
|
|
class Application extends \samson\core\CompressableExternalModule |
27
|
|
|
{ |
28
|
|
|
/** @var string Identifier */ |
29
|
|
|
public $id = 'signin'; |
30
|
|
|
|
31
|
|
|
/** @var Email Pointer to social email module */ |
32
|
|
|
protected $social; |
33
|
|
|
|
34
|
|
|
/** @var QueryInterface Database query instance */ |
35
|
|
|
protected $query; |
36
|
|
|
|
37
|
|
|
/** @var RequestInterface Request instance */ |
38
|
|
|
protected $request; |
39
|
|
|
|
40
|
|
|
public function authorize($cms) |
41
|
|
|
{ |
42
|
|
|
if ($cms->isCMS()) { |
43
|
|
|
if ( ! $this->social->authorized()) { |
44
|
|
|
if ( ! $this->social->cookieVerification()) { |
45
|
|
|
if ( ! $this->request->is('signin')) { |
46
|
|
|
$this->request->redirect('/'.$cms->id.'/signin'); |
47
|
|
|
} |
48
|
|
|
} else { |
49
|
|
|
$this->request->redirect('/'.$cms->id.'/signin'); |
50
|
|
|
} |
51
|
|
|
} else { |
52
|
|
|
if ($this->request->is('signin')) { |
53
|
|
|
$this->request->redirect('/'.$cms->id); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function init(array $params = array()) |
60
|
|
|
{ |
61
|
|
|
//trace('cmsInit'); |
62
|
|
|
// Old applications main page rendering |
63
|
|
|
Event::subscribe(\samsoncms\cms\Application::EVENT_IS_CMS, array($this, 'authorize')); |
64
|
|
|
|
65
|
|
|
// Call parent initialization |
66
|
|
|
return parent::init($params); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Application constructor. |
71
|
|
|
* |
72
|
|
|
* @param string $path |
73
|
|
|
* @param ResourcesInterface $resources |
74
|
|
|
* @param SystemInterface $system |
75
|
|
|
*/ |
76
|
|
|
public function __construct($path, ResourcesInterface $resources, SystemInterface $system) |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
// Inject dependencies |
79
|
|
|
$this->social = m('socialemail'); |
|
|
|
|
80
|
|
|
$this->request = url(); |
|
|
|
|
81
|
|
|
$this->query = new dbQuery(); |
82
|
|
|
|
83
|
|
|
parent::__construct($path, $resources, $system); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//[PHPCOMPRESSOR(remove,start)] |
87
|
|
|
/** Module preparation */ |
88
|
|
|
public function prepare() |
89
|
|
|
{ |
90
|
|
|
// Create default user for first logins |
91
|
|
|
$adminUser = '[email protected]'; |
92
|
|
|
$hashedEmailValue = $this->social->hash($adminUser); |
93
|
|
|
|
94
|
|
|
// Try to find generic user |
95
|
|
|
$admin = $this->query |
96
|
|
|
->entity($this->social->dbTable) |
97
|
|
|
->where($this->social->dbEmailField, $adminUser) |
98
|
|
|
->first(); |
99
|
|
|
|
100
|
|
|
// Create user record if missing |
101
|
|
|
if ( ! isset($admin)) { |
102
|
|
|
$admin = new $this->social->dbTable(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Fill in user credentials according to config |
106
|
|
|
$admin[$this->social->dbEmailField] = $adminUser; |
107
|
|
|
$admin[$this->social->dbHashEmailField] = $hashedEmailValue; |
108
|
|
|
$admin[$this->social->dbHashPasswordField] = $hashedEmailValue; |
109
|
|
|
$admin->save(); |
110
|
|
|
} |
111
|
|
|
//[PHPCOMPRESSOR(remove,end)] |
112
|
|
|
|
113
|
|
|
/** Check the user's authorization */ |
114
|
|
|
public function __HANDLER() |
115
|
|
|
{ |
116
|
|
|
$this->authorize($this->social); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** Main sign in template */ |
120
|
|
|
public function __base() |
121
|
|
|
{ |
122
|
|
|
// Change template |
123
|
|
|
$this->system->template('www/signin/signin_template.vphp'); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Render template with sign in form |
126
|
|
|
$this->html($this->view('www/signin/signin_form.vphp')->output()) |
127
|
|
|
->title(t('Авторизация', true)); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** User asynchronous sign in */ |
131
|
|
|
public function __async_login() |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$user = null; |
134
|
|
|
$error = ''; |
135
|
|
|
|
136
|
|
|
if (isset($_POST['email']) && isset($_POST['password'])) { |
137
|
|
|
$email = $this->social->hash($_POST['email']); |
138
|
|
|
$password = $this->social->hash($_POST['password']); |
139
|
|
|
$remember = isset($_POST['remember']) ? true : false; |
140
|
|
|
|
141
|
|
|
/** @var EmailStatus Perform email authorization */ |
142
|
|
|
$auth = $this->social->authorizeWithEmail($email, $password, $remember, $user); |
143
|
|
|
|
144
|
|
|
if ($auth->code === EmailStatus::SUCCESS_EMAIL_AUTHORIZE) { |
145
|
|
|
// Fire login success event |
146
|
|
|
Event::fire('samson.cms.signin.login', array(&$user)); |
147
|
|
|
|
148
|
|
|
return array('status' => '1'); |
149
|
|
|
} else { |
150
|
|
|
$error .= $this->view('www/signin/signin_form.vphp') |
151
|
|
|
->errorClass('errorAuth') |
152
|
|
|
->userEmail("{$_POST['email']}") |
153
|
|
|
->focus('autofocus') |
154
|
|
|
->output(); |
155
|
|
|
|
156
|
|
|
return array('status' => '0', 'html' => $error); |
157
|
|
|
} |
158
|
|
|
} else { |
159
|
|
|
$error .= $this->view('www/signin/signin_form')->errorClass('errorAuth')->output(); |
160
|
|
|
|
161
|
|
|
return array('status' => '0', 'html' => $error); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** User logout */ |
166
|
|
|
public function __logout() |
167
|
|
|
{ |
168
|
|
|
$this->social->deauthorize(); |
169
|
|
|
|
170
|
|
|
// Fire logout event |
171
|
|
|
Event::fire('samson.cms.signin.logout'); |
172
|
|
|
|
173
|
|
|
$this->request->redirect('cms/signin'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** Sending email with the correct address */ |
177
|
|
|
public function __mail() |
178
|
|
|
{ |
179
|
|
|
if (isset($_POST['email'])) { |
180
|
|
|
/** @var \samson\activerecord\user $user */ |
181
|
|
|
$user = null; |
182
|
|
|
$result = ''; |
183
|
|
|
if ($this->query->entity('user')->where('email', $_POST['email'])->first($user)) { |
184
|
|
|
$user->confirmed = $this->social->hash(generate_password(20) . time()); |
|
|
|
|
185
|
|
|
$user->save(); |
|
|
|
|
186
|
|
|
$message = $this->view('www/signin/email/pass_recovery')->code($user->confirmed)->output(); |
|
|
|
|
187
|
|
|
|
188
|
|
|
mail_send($user->Email, '[email protected]', $message, t('Восстановление пароля!', true), 'SamsonCMS'); |
|
|
|
|
189
|
|
|
|
190
|
|
|
$result .= $this->view('www/signin/pass_recovery_mailsend')->output(); |
191
|
|
|
$this->system->template('www/signin/signin_template.vphp'); |
|
|
|
|
192
|
|
|
$this->html($result)->title(t('Восстановление пароля', true)); |
193
|
|
|
} else { |
194
|
|
|
$this->request->redirect(); |
195
|
|
|
} |
196
|
|
|
} else { |
197
|
|
|
$this->request->redirect(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* New password form. |
203
|
|
|
* |
204
|
|
|
* @param string $code Code password recovery |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function __confirm($code) |
209
|
|
|
{ |
210
|
|
|
if ($this->query->entity('user')->where($this->social->dbConfirmField, $code)->first()) { |
211
|
|
|
$this->system->template('www/signin/signin_template.vphp'); |
|
|
|
|
212
|
|
|
$this->html($this->view('www/signin/new_pass_form')->code($code)->output()) |
213
|
|
|
->title(t('Восстановление пароля', true)); |
214
|
|
|
} else { |
215
|
|
|
return A_FAILED; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Setting new password and sign in |
221
|
|
|
* |
222
|
|
|
* @param string $code Code password recovery |
223
|
|
|
*/ |
224
|
|
|
public function __recovery($code) |
225
|
|
|
{ |
226
|
|
|
if (isset($_POST['password']) && isset($_POST['confirm_password']) |
227
|
|
|
&& $_POST['password'] == $_POST['confirm_password'] |
228
|
|
|
) { |
229
|
|
|
/** @var \samson\activerecord\user $user */ |
230
|
|
|
$user = null; |
231
|
|
|
if ($this->query->where('confirmed', $code)->first($user)) { |
232
|
|
|
$user->confirmed = 1; |
|
|
|
|
233
|
|
|
$user->md5_password = md5($_POST['password']); |
|
|
|
|
234
|
|
|
$user->Password = $_POST['password']; |
|
|
|
|
235
|
|
|
$user->save(); |
|
|
|
|
236
|
|
|
|
237
|
|
|
$auth = $this->social->authorizeWithEmail($user->md5_email, $user->md5_password, $user); |
|
|
|
|
238
|
|
|
if ($auth->code === EmailStatus::SUCCESS_EMAIL_AUTHORIZE) { |
239
|
|
|
$this->request->redirect(); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} else { |
243
|
|
|
$result = ''; |
244
|
|
|
$result .= m()->view('www/signin/pass_error') |
245
|
|
|
->message(t('Вы ввели некорректный пароль либо пароли не совпадают', true)) |
246
|
|
|
->output(); |
247
|
|
|
$this->system->template('www/signin/signin_template.vphp'); |
|
|
|
|
248
|
|
|
$this->html($result)->title(t('Ошибка восстановление пароля', true)); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
|