forked from arollmann/yii2-yee-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYee.php
394 lines (338 loc) · 10.4 KB
/
Yee.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* @link http://www.yee-soft.com/
* @copyright Copyright (c) 2015 Taras Makitra
* @license http://www.apache.org/licenses/LICENSE-2.0
*/
namespace yeesoft;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\base\Component;
/**
* YeeCMS component. Contains basic settings and functions of YeeCMS.
*/
class Yee extends Component
{
/**
* Version number of the component.
*/
const VERSION = '0.1.0';
/**
* Session ID of last login attempt.
*/
const SESSION_LAST_ATTEMPT_TIME = '_last_attempt';
/**
* Session ID of login attempts count.
*/
const SESSION_ATTEMPTS_COUNT = '_attempt_count';
/**
* Indicates whether it is required to confirm email after registration.
* User's account status will be set to "active" after user confirms his email.
*
* @var boolean
*/
public $emailConfirmationRequired = true;
/**
* Default email FROM sender. If empty it will be set to
* `[Yii::$app->params['adminEmail'] => Yii::$app->name]`
*
* @var string
*/
public $emailSender;
/**
* Email templates. These settings will be merged with `$_defaultEmailTemplates`.
*
* @var array
* @see $_defaultEmailTemplates
*/
public $emailTemplates = [];
/**
* Permission that will be assigned automatically for everyone. You can assign
* routes like "site/index" to this permission and those routes will be
* available for everyone.
*
* @var string
*/
public $commonPermissionName = 'commonPermission';
/**
* After how many seconds confirmation token will be invalid
*
* @var int
*/
public $confirmationTokenExpire = 3600; // 1 hour
/**
* Roles that will be assigned to user after registration.
*
* @var array
*/
public $defaultRoles = [];
/**
* Pattern that will be used to validate usernames on registration. Default
* pattern allows only numbers and letters.
*
* @var string
*/
public $usernameRegexp = '/^(\w|\d)+$/';
/**
* Pattern that describe what names should not be allowed for username on
* registration. Default pattern does not allow anything having "admin".
*
* @var string
*/
public $usernameBlackRegexp = '/^(.)*admin(.)*$/i';
/**
* List of languages used in application.
*
* @var array
*/
public $languages = ['en-US' => 'English'];
/**
* List of language slug redirects. You can use this parameter to redirect
* language slug to another slug. For example `en-US` to `en`.
*
* @var array
*/
public $languageRedirects = ['en-US' => 'en'];
/**
* How much attempts user can made to login, update or recover password
* in `$attemptsTimeout` seconds interval.
*
* @var int
*/
public $maxAttempts = 5;
/**
* Number of seconds after attempt counter to login, update or recover
* password will reset.
*
* @var int
*/
public $attemptsTimeout = 60;
/**
* Captcha action options. Used for registration and password recovery.
*
* @var array
*/
public $captchaAction = [
'class' => 'yii\captcha\CaptchaAction',
'minLength' => 5,
'maxLength' => 5,
'height' => 45,
'width' => 100,
'padding' => 0
];
/**
* User table alias.
*
* @var string
*/
public $user_table = '{{%user}}';
/**
* User visit log table alias.
*
* @var string
*/
public $user_visit_log_table = '{{%user_visit_log}}';
/**
* Auth item table alias.
*
* @var string
*/
public $auth_item_table = '{{%auth_item}}';
/**
* Auth item child table alias.
*
* @var string
*/
public $auth_item_child_table = '{{%auth_item_child}}';
/**
* Auth item group table alias.
*
* @var string
*/
public $auth_item_group_table = '{{%auth_item_group}}';
/**
* Auth assignment table alias.
*
* @var string
*/
public $auth_assignment_table = '{{%auth_assignment}}';
/**
* Auth rule table alias.
*
* @var string
*/
public $auth_rule_table = '{{%auth_rule}}';
/**
* List of languages used in frontend rules. Contains the same values as
* `$languages` but keys is replaced with `$languageRedirects`.
*
* @var array
*/
protected $_displayLanguages;
/**
* Default email templates.
*
* @var array
*/
protected $_defaultEmailTemplates = [
'signup-confirmation' => '/mail/signup-email-confirmation-html',
'password-reset' => '/mail/password-reset-html',
'confirm-email' => '/mail/email-confirmation-html',
];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if (Yii::$app->id != 'console') {
$this->registerTranslations();
$this->initLanguageOptions();
$this->initEmailOptions();
$this->initFormatter();
}
}
/**
* Register YeeCMS DB message translations.
*/
protected function registerTranslations()
{
Yii::$app->i18n->translations['yee*'] = [
'class' => 'yeesoft\db\DbMessageSource',
'sourceLanguage' => 'en-US',
'enableCaching' => true,
];
}
/**
* Prepare mailer options. Merge given email templates options with default.
*/
protected function initLanguageOptions()
{
if (empty($this->languages) || !is_array($this->languages)) {
$this->languages[Yii::$app->language] = Yii::t('yee', 'Default Language');
}
if (!in_array(Yii::$app->language, array_keys($this->languages))) {
throw new InvalidConfigException('Invalid language settings! Default application language should be included into `yeesoft\Yee::$languages` setting.');
}
if(!empty(array_diff(array_keys($this->languageRedirects), array_keys($this->languages)))){
throw new InvalidConfigException('Invalid language redirects settings!');
}
}
/**
* Prepare mailer options. Merge given email templates options with default.
*/
protected function initEmailOptions()
{
if (empty($this->emailSender)) {
$this->emailSender = [Yii::$app->params['adminEmail'] => Yii::$app->name];
}
$this->emailTemplates = ArrayHelper::merge($this->_defaultEmailTemplates, $this->emailTemplates);
}
/**
* Updates formatter to display date and time correcty.
*/
protected function initFormatter()
{
date_default_timezone_set(Yii::$app->settings->get('general.timezone', 'UTC'));
Yii::$app->formatter->timeZone = Yii::$app->settings->get('general.timezone', 'UTC');
Yii::$app->formatter->dateFormat = Yii::$app->settings->get('general.dateformat', "yyyy-MM-dd");
Yii::$app->formatter->timeFormat = Yii::$app->settings->get('general.timeformat', "HH:mm");
Yii::$app->formatter->datetimeFormat = Yii::$app->formatter->dateFormat . " " . Yii::$app->formatter->timeFormat;
}
/**
* Return true if site is multilingual.
*
* @return boolean
*/
public function getIsMultilingual()
{
$languages = Yii::$app->yee->languages;
return count($languages) > 1;
}
/**
* Returns language shortcode that will be displayed on frontend.
*
* @param string $language
* @return string
*/
public function getDisplayLanguageShortcode($language)
{
return (isset($this->languageRedirects[$language])) ? $this->languageRedirects[$language] : $language;
}
/**
* Returns original language shortcode from its redirect.
*
* @param string $language
* @return string
*/
public function getSourceLanguageShortcode($language)
{
if (!isset($this->languageRedirects)) {
return $language;
}
$languageRedirects = array_flip(Yii::$app->yee->languageRedirects);
return (isset($languageRedirects[$language])) ? $languageRedirects[$language] : $language;
}
/**
* Returns list of languages used in frontend rules. Contains the same values
* as `$languages` but keys is replaced with `$languageRedirects`.
*
* @return array
*/
public function getDisplayLanguages()
{
if (!isset($this->_displayLanguages)) {
foreach ($this->languages as $key => $value) {
$key = (isset($this->languageRedirects[$key])) ? $this->languageRedirects[$key] : $key;
$redirects[$key] = $value;
}
$this->_displayLanguages = $redirects;
}
return $this->_displayLanguages;
}
/**
* Check how much attempts to login, reset or update password user has been
* made in `$attemptsTimeout` seconds.
*
* @return boolean
*/
public function checkAttempts()
{
$lastAttemptTime = Yii::$app->session->get(static::SESSION_LAST_ATTEMPT_TIME);
if ($lastAttemptTime) {
$attemptsCount = Yii::$app->session->get(static::SESSION_ATTEMPTS_COUNT, 0);
Yii::$app->session->set(static::SESSION_ATTEMPTS_COUNT, ++$attemptsCount);
// If last attempt was made more than X seconds ago then reset counters
if (($lastAttemptTime + $this->attemptsTimeout) < time()) {
Yii::$app->session->set(static::SESSION_LAST_ATTEMPT_TIME, time());
Yii::$app->session->set(static::SESSION_ATTEMPTS_COUNT, 1);
return true;
} elseif ($attemptsCount > $this->maxAttempts) {
return false;
}
return true;
}
Yii::$app->session->set(static::SESSION_LAST_ATTEMPT_TIME, time());
Yii::$app->session->set(static::SESSION_ATTEMPTS_COUNT, 1);
return true;
}
/**
* Returns an HTML hyperlink that can be displayed on your Web page.
*
* @return string
*/
public static function powered()
{
return '<a href="http://www.yee-soft.com/" rel="external">Yee CMS</a>';
}
/**
* Returns a string representing the current version of the Yee CMS Core.
*
* @return string the version of Yee CMS Core
*/
public static function getVersion()
{
return self::VERSION;
}
}