Passed
Push — v5 ( 6adbf6...c0775f )
by Alexey
06:03
created

Users::init()   D

Complexity

Conditions 18
Paths 56

Size

Total Lines 26
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 18
nc 56
nop 0
dl 0
loc 26
rs 4.947
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Inji;
3
/**
4
 * Users module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Users extends Module {
12
13
    public $cookiePrefix = '';
14
15
    public function init() {
16
        if (!empty($this->config['cookieSplit'])) {
17
            $this->cookiePrefix = App::$cur->type;
18
        }
19
        Users\User::$cur = new Users\User(array('group_id' => 1, 'role_id' => 1));
20
        if (!empty($_GET['invite_code']) && is_string($_GET['invite_code'])) {
21
            setcookie('invite_code', $_GET['invite_code'], time() + 360000, "/");
22
        }
23
        if (!App::$cur->db->connect) {
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
24
            return false;
25
        }
26
        if (isset($_GET['logout'])) {
27
            return $this->logOut();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->logOut() targeting Inji\Users::logOut() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
28
        }
29
        if (isset($_GET['passre']) && filter_input(INPUT_GET, 'user_mail')) {
30
            $this->passre(trim(filter_input(INPUT_GET, 'user_mail')));
31
        }
32
        if (!empty($_GET['passrecont']) && filter_input(INPUT_GET, 'hash')) {
33
            $this->passrecont(filter_input(INPUT_GET, 'hash'));
34
        }
35
        if (isset($_POST['autorization']) && trim(filter_input(INPUT_POST, 'user_login')) && trim(filter_input(INPUT_POST, 'user_pass'))) {
36
            unset($_POST['autorization']);
37
            return $this->autorization(trim(filter_input(INPUT_POST, 'user_login')), trim(filter_input(INPUT_POST, 'user_pass')), strpos(filter_input(INPUT_POST, 'user_login'), '@') ? 'mail' : 'login', false, false, trim(filter_input(INPUT_POST, 'ref')));
38
        }
39
        if (!empty($_COOKIE[$this->cookiePrefix . '_user_session_hash']) && is_string($_COOKIE[$this->cookiePrefix . '_user_session_hash']) && !empty($_COOKIE[$this->cookiePrefix . '_user_id']) && is_string($_COOKIE[$this->cookiePrefix . '_user_id'])) {
40
            return $this->cuntinueSession($_COOKIE[$this->cookiePrefix . '_user_session_hash'], $_COOKIE[$this->cookiePrefix . '_user_id']);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->cuntinueSession($...iePrefix . '_user_id']) targeting Inji\Users::cuntinueSession() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
41
        }
42
    }
43
44
    public function logOut($redirect = true) {
45
        if (!empty($_COOKIE[$this->cookiePrefix . "_user_session_hash"]) && !empty($_COOKIE[$this->cookiePrefix . "_user_id"])) {
46
            $session = Users\Session::get([
47
                ['user_id', $_COOKIE[$this->cookiePrefix . "_user_id"]],
48
                ['hash', $_COOKIE[$this->cookiePrefix . "_user_session_hash"]]
49
            ]);
50
            if ($session) {
51
                $session->delete();
52
            }
53
        }
54
        if (!headers_sent()) {
55
            setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
56
            setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
57
        }
58
        if ($redirect) {
59
            if (!empty($this->config['logoutUrl'][$this->app->type])) {
60
                Tools::redirect($this->config['logoutUrl'][$this->app->type]);
61
            }
62
            Tools::redirect('/', \I18n\Text::module('Users', 'Вы вышли из своего профиля'), 'success');
0 ignored issues
show
Bug introduced by
The type I18n\Text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
63
        }
64
    }
65
66
    public function cuntinueSession($hash, $userId) {
67
        $session = Users\Session::get([
68
            ['user_id', $userId],
69
            ['hash', $hash]
70
        ]);
71
        if (!$session) {
72
            if (!headers_sent()) {
73
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
74
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
75
            }
76
            Tools::redirect('/', \I18n\Text::module('Users', 'Произошла непредвиденная ошибка при авторизации сессии'));
77
        }
78
        if ($session->user->id != $userId) {
79
            Tools::redirect('/', \I18n\Text::module('Users', 'Произошла непредвиденная ошибка при авторизации сессии'));
80
        }
81
        if ($session && $session->user && $session->user->blocked) {
82
            if (!headers_sent()) {
83
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
84
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
85
            }
86
            \Inji\Msg::add(\I18n\Text::module('Users', 'Ваш аккаунт заблокирован'), 'info');
87
            return;
88
        }
89
        if ($session && $session->user && !$session->user->blocked) {
90
            if (!empty($this->config['needActivation']) && $session->user->activation) {
91
                if (!headers_sent()) {
92
                    setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
93
                    setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
94
                }
95
                Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>');
96
            } elseif ($session->user->activation) {
97
                \Inji\Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>');
98
            }
99
            if (!$session->user->mail && !empty($this->config['noMailNotify'])) {
100
                \Inji\Msg::add($this->config['noMailNotify']);
101
            }
102
            Users\User::$cur = $session->user;
103
            Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP';
104
            Users\User::$cur->save();
105
        } else {
106
            if (!headers_sent()) {
107
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
108
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
109
            }
110
            \Inji\Msg::add(\I18n\Text::module('Users', 'needrelogin'), 'info');
111
        }
112
    }
113
114
    /**
115
     * @param string $user_mail
116
     */
117
    public function passre($user_mail) {
118
        $user = $this->get($user_mail, 'mail');
119
        if (!$user) {
120
            \Inji\Msg::add(\I18n\Text::module('Users', 'mailnotfound', ['user_mail' => $user_mail]), 'danger');
121
            return false;
122
        }
123
        $passre = Users\Passre::get([['user_id', $user->id], ['status', 1]]);
0 ignored issues
show
Bug introduced by
The property id does not exist on true.
Loading history...
Bug introduced by
The type Inji\Users\Passre was not found. Did you mean Users\Passre? If so, make sure to prefix the type with \.
Loading history...
124
        if ($passre) {
125
            $passre->status = 2;
126
            $passre->save();
127
        }
128
        $hash = $user->id . '_' . Tools::randomString(50);
129
        $passre = new Users\Passre(['user_id' => $user->id, 'status' => 1, 'hash' => $hash]);
130
        $passre->save();
131
        $domainRaw = App::$cur->getDomain();
132
        $domain = App::$cur->getDomain(true);
133
        $title = \I18n\Text::module('Users', 'Восстановление пароля на сайте ${domain}', ['domain' => $domain]);
134
        $text = \I18n\Text::module('Users', 'repassmailtext', ['domain' => $domain, 'hash' => $hash]);
135
        Tools::sendMail('noreply@' . $domainRaw, $user_mail, $title, $text);
136
        Tools::redirect('/', \I18n\Text::module('Users', 'На указанный почтовый ящик была выслана инструкция по восстановлению пароля'), 'success');
137
    }
138
139
    public function passrecont($hash) {
140
        $passre = Users\Passre::get([['hash', $hash]]);
141
        if ($passre) {
142
            if ($passre->status != 1) {
143
                Tools::redirect('/', 'Этот код восстановление более недействителен', 'danger');
144
            }
145
            $passre->status = 3;
146
            $passre->save();
147
            $pass = Tools::randomString(10);
148
            $user = Users\User::get($passre->user_id);
149
            $user->pass = $this->hashpass($pass);
0 ignored issues
show
Bug introduced by
The property pass does not exist on false.
Loading history...
150
            $user->save();
151
            $this->autorization($user->id, $pass, 'id', true, true);
0 ignored issues
show
Bug introduced by
The property id does not exist on false.
Loading history...
152
            $domainRaw = App::$cur->getDomain();
153
            $domain = App::$cur->getDomain(true);
154
            $title = \I18n\Text::module('Users', 'Новый пароль на сайте ${domain}', ['domain' => $domain]);
155
            $text = \I18n\Text::module('Users', 'newpassmail', ['domain' => $domain, 'pass' => $pass]);
156
            Tools::sendMail('noreply@' . $domainRaw, $user->mail, $title, $text);
0 ignored issues
show
Bug introduced by
The property mail does not exist on false.
Loading history...
157
            Tools::redirect('/', \I18n\Text::module('Users', 'Вы успешно сбросили пароль и были авторизованы на сайте. На ваш почтовый ящик был выслан новый пароль'), 'success');
158
        }
159
    }
160
161
    public function autorization($login, $pass, $ltype = 'login', $noMsg = true, $skipErrorCheck = false, $redirect = '') {
162
        $user = $this->get($login, $ltype);
163
        if ($user && !$skipErrorCheck) {
164
            $lastSuccessLogin = \Users\User\LoginHistory::lastSuccessLogin($user->id);
0 ignored issues
show
Bug introduced by
The property id does not exist on true.
Loading history...
165
            $where = [['user_id', $user->id]];
166
            if ($lastSuccessLogin) {
167
                $where[] = ['date_create', $lastSuccessLogin->date_create, '>'];
168
            }
169
            $loginHistoryErrorCount = \Users\User\LoginHistory::getCount(['where' => $where]);
170
            if ($loginHistoryErrorCount > 5) {
171
                \Inji\Msg::add(\I18n\Text::module('Users', 'logintrylimit', ['user_mail' => $user->mail]), 'danger');
0 ignored issues
show
Bug introduced by
The property mail does not exist on true.
Loading history...
172
                return false;
173
            }
174
        }
175
        if ($user && $this->verifypass($pass, $user->pass) && !$user->blocked) {
0 ignored issues
show
Bug introduced by
The property blocked does not exist on true.
Loading history...
Bug introduced by
The property pass does not exist on true.
Loading history...
176
            $loginHistory = new \Users\User\LoginHistory([
177
                'user_id' => $user->id,
178
                'ip' => $_SERVER['REMOTE_ADDR'],
179
                'success' => true
180
            ]);
181
            $loginHistory->save();
182
            if (!empty($this->config['needActivation']) && $user->activation) {
0 ignored issues
show
Bug introduced by
The property activation does not exist on true.
Loading history...
183
                Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>');
184
            } elseif ($user->activation) {
185
                \Inji\Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>');
186
            }
187
            if (!$user->mail && !empty($this->config['noMailNotify'])) {
188
                \Inji\Msg::add($this->config['noMailNotify']);
189
            }
190
            $this->newSession($user);
191
192
            Users\User::$cur = $user;
0 ignored issues
show
Documentation Bug introduced by
It seems like $user can also be of type true. However, the property $cur is declared as type Inji\Users\User. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
193
            Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP';
0 ignored issues
show
Bug introduced by
The property date_last_active does not exist on true.
Loading history...
194
            Users\User::$cur->save();
195
            if (!$noMsg) {
196
                if (!empty($this->config['loginUrl'][$this->app->type]) && !$redirect) {
197
                    $redirect = $this->config['loginUrl'][$this->app->type];
198
                }
199
                Tools::redirect($redirect);
200
            }
201
202
            return true;
203
        }
204
        if (!$noMsg) {
205
            if ($user && $user->blocked) {
206
                \Inji\Msg::add('Вы заблокированы', 'danger');
207
            } elseif ($user) {
208
                $loginHistory = new \Users\User\LoginHistory([
209
                    'user_id' => $user->id,
210
                    'ip' => $_SERVER['REMOTE_ADDR'],
211
                    'success' => false
212
                ]);
213
                $loginHistory->save();
214
                \Inji\Msg::add(\I18n\Text::module('Users', 'loginfail', ['user_mail' => $user->mail]), 'danger');
215
            } else {
216
                \Inji\Msg::add(\I18n\Text::module('Users', 'Данный почтовый ящик не зарегистрирован в системе'), 'danger');
217
            }
218
        }
219
220
        return false;
221
    }
222
223
    public function newSession($user) {
224
        $session = $this->createSession($user);
225
        if (!headers_sent()) {
226
            setcookie($this->cookiePrefix . "_user_session_hash", $session->hash, time() + 360000, "/");
0 ignored issues
show
Bug Best Practice introduced by
The property hash does not exist on Inji\Users\Session. Since you implemented __get, consider adding a @property annotation.
Loading history...
227
            setcookie($this->cookiePrefix . "_user_id", $user->id, time() + 360000, "/");
228
        } else {
229
            \Inji\Msg::add('Не удалось провести авторизацию. Попробуйте позже', 'info');
230
        }
231
    }
232
233
    public function createSession($user) {
234
        do {
235
            $hash = Tools::randomString(255);
236
        } while (Users\Session::get($hash, 'hash'));
237
238
        $session = new Users\Session([
239
            'user_id' => $user->id,
240
            'agent' => $_SERVER['HTTP_USER_AGENT'],
241
            'ip' => $_SERVER['REMOTE_ADDR'],
242
            'hash' => $hash
243
        ]);
244
        $session->save();
245
        return $session;
246
    }
247
248
    /**
249
     * Return user
250
     *
251
     * @param integer|string $idn
252
     * @param string $ltype
253
     * @return boolean|\Users\User
254
     */
255
    public function get($idn, $ltype = 'id') {
256
        if (!$idn)
257
            return false;
258
259
        if (is_numeric($idn) && $ltype != 'login')
260
            $user = Users\User::get($idn, 'id');
261
        elseif ($ltype == 'login')
262
            $user = Users\User::get($idn, 'login');
263
        else
264
            $user = Users\User::get($idn, 'mail');
265
        if (!$user)
266
            return [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array() returns the type array which is incompatible with the documented return type boolean|Users\User.
Loading history...
267
268
        return $user;
269
    }
270
271
    private function msgOrErr($err, $msg) {
272
        if ($msg) {
273
            \Inji\Msg::add($err, 'danger');
274
            return false;
275
        }
276
        return ['success' => false, 'error' => $err];
277
278
    }
279
280
    public function registration($data, $autorization = false, $msg = true) {
281
282
        if (empty($data['user_mail'])) {
283
            return $this->msgOrErr('Вы не ввели E-mail', $msg);
284
        }
285
        if (!empty($this->config['csrf']) && !App::$cur->server->checkCsrfForm($data)) {
0 ignored issues
show
Bug introduced by
The method checkCsrfForm() does not exist on Inji\Module. It seems like you code against a sub-type of Inji\Module such as Inji\Server or Inji\Db. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

285
        if (!empty($this->config['csrf']) && !App::$cur->server->/** @scrutinizer ignore-call */ checkCsrfForm($data)) {
Loading history...
Bug Best Practice introduced by
The property server does not exist on Inji\App. Since you implemented __get, consider adding a @property annotation.
Loading history...
286
            return $this->msgOrErr('Ошибка безопасности, повторите запрос еще раз', $msg);
287
        }
288
        $data['user_mail'] = trim($data['user_mail']);
289
        if (!filter_var($data['user_mail'], FILTER_VALIDATE_EMAIL)) {
290
            return $this->msgOrErr(\I18n\Text::module('Users', 'Вы ввели не корректный E-mail'), $msg);
291
292
        }
293
294
        $user = $this->get($data['user_mail'], 'mail');
295
        if ($user) {
296
            return $this->msgOrErr(\I18n\Text::module('Users', 'Введенный вами E-mail зарегистрирован в нашей системе, войдите или введите другой E-mail'), $msg);
297
        }
298
        if (empty($data['user_login'])) {
299
            $data['user_login'] = $data['user_mail'];
300
        }
301
        $data['user_login'] = trim($data['user_login']);
302
        $user = $this->get($data['user_login'], 'login');
303
        if ($user) {
304
            return $this->msgOrErr('Введенный вами логин зарегистрирован в нашей системе, войдите или введите другой логин', $msg);
305
        }
306
        if (empty($data['first_name'])) {
307
            $data['first_name'] = '';
308
        }
309
        if (empty($data['last_name'])) {
310
            $data['last_name'] = '';
311
        }
312
        if (!empty($data['user_name'])) {
313
            $data['first_name'] = $data['user_name'];
314
        }
315
        if (empty($data['user_city'])) {
316
            $data['user_city'] = '';
317
        }
318
        if (empty($data['user_birthday'])) {
319
            $data['user_birthday'] = '';
320
        }
321
        if (empty($data['user_phone'])) {
322
            $data['user_phone'] = '';
323
        }
324
325
        if (empty($data['user_avatar_id']) || !is_numeric($data['user_avatar_id'])) {
326
            $data['user_avatar_id'] = 0;
327
        } else {
328
            $avatar = \Users\User\Avatar::get($data['user_avatar_id']);
329
            if ($avatar) {
330
                $data['user_avatar_id'] = $avatar->id;
331
            } else {
332
                $data['user_avatar_id'] = 0;
333
            }
334
        }
335
        $invite_code = (!empty($data['invite_code']) ? $data['invite_code'] : (!empty($_POST['invite_code']) ? $_POST['invite_code'] : ((!empty($_COOKIE['invite_code']) ? $_COOKIE['invite_code'] : ((!empty($_GET['invite_code']) ? $_GET['invite_code'] : ''))))));
336
        if (!empty($invite_code)) {
337
            $invite = Users\User\Invite::get($invite_code, 'code');
0 ignored issues
show
Bug introduced by
The type Inji\Users\User\Invite was not found. Did you mean Users\User\Invite? If so, make sure to prefix the type with \.
Loading history...
338
            if (!$invite) {
339
                return $this->msgOrErr('Такой код приглашения не найден', $msg);
340
            }
341
            if ($invite->limit && !($invite->limit - $invite->count)) {
342
                return $this->msgOrErr('Лимит приглашений для данного кода исчерпан', $msg);
343
            }
344
            $data['parent_id'] = $invite->user_id;
345
            $inviter = $data['parent_id'];
346
            $invite->count++;
347
            $invite->save();
348
        }
349
        if (empty($data['parent_id']) && !empty($this->config['defaultPartner'])) {
350
            $data['parent_id'] = $this->config['defaultPartner'];
351
        }
352
        if (!empty($this->config['passwordManualSetup']) && !empty($data['user_pass'])) {
353
            if (empty($data['user_pass'][0])) {
354
                return $this->msgOrErr('Введите пароль', $msg);
355
            }
356
            if (empty($data['user_pass'][1])) {
357
                return $this->msgOrErr('Повторите ввод пароля', $msg);
358
            }
359
            if ($data['user_pass'][0] != $data['user_pass'][1]) {
360
                return $this->msgOrErr('Введенные пароли несовпадают', $msg);
361
            }
362
            $pass = $data['user_pass'][0];
363
        } else {
364
            $pass = Tools::randomString(10);
365
        }
366
        $user = new Users\User([
367
            'pass' => $this->hashpass($pass),
368
            'mail' => $data['user_mail'],
369
            'login' => htmlspecialchars($data['user_login']),
370
            'role_id' => 2,
371
            'group_id' => 2,
372
            'user_avatar_id' => $data['user_avatar_id'],
373
            'parent_id' => !empty($data['parent_id']) ? $data['parent_id'] : 0
374
        ]);
375
        if (!empty($this->config['needActivation'])) {
376
            $user->activation = Tools::randomString();
377
        }
378
        $user->save();
379
        if (!$user->id) {
380
            return $this->msgOrErr('Не удалось зарегистрировать', $msg);
381
        }
382
        $info = new \Users\User\Info([
383
            'user_id' => $user->id,
384
            'first_name' => htmlspecialchars($data['first_name']),
385
            'last_name' => htmlspecialchars($data['last_name']),
386
            'city' => htmlspecialchars($data['user_city']),
387
            'bday' => htmlspecialchars($data['user_birthday']),
388
            'phone' => htmlspecialchars($data['user_phone']),
389
            'photo_file_id' => !empty($_FILES['user_photo']['tmp_name']) ? $this->files->upload($_FILES['user_photo']) : 0
0 ignored issues
show
Bug Best Practice introduced by
The property files does not exist on Inji\Users. Did you maybe forget to declare it?
Loading history...
390
        ]);
391
        $info->save();
392
        if (isset($inviter)) {
393
            $this->AddUserActivity($inviter, 2, "У вас зарегистрировался новый партнер, {$info->first_name} {$info->last_name} (id: {$user->id}, email: {$user->mail})");
0 ignored issues
show
Bug Best Practice introduced by
The property last_name does not exist on Users\User\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property first_name does not exist on Users\User\Info. Since you implemented __get, consider adding a @property annotation.
Loading history...
394
        }
395
        if ($autorization) {
396
            $this->autorization($data['user_mail'], $pass, 'mail');
397
        }
398
        if (!empty($this->config['needActivation'])) {
399
            $from = 'noreply@' . INJI_DOMAIN_NAME;
400
            $to = $data['user_mail'];
401
            $subject = 'Регистрация на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME);
402
            $text = 'Вы были зарегистрированы на сайте ' . idn_to_utf8(INJI_DOMAIN_NAME) . '<br />для входа используйте ваш почтовый ящик в качестве логина и пароль: ' . $pass;
403
            $text .= '<br />';
404
            $text .= '<br />';
405
            $text .= 'Для активации вашего аккаунта перейдите по ссылке <a href = "http://' . INJI_DOMAIN_NAME . '/users/activation/' . $user->id . '/' . $user->activation . '">http://' . idn_to_utf8(INJI_DOMAIN_NAME) . '/users/activation/' . $user->id . '/' . $user->activation . '</a>';
406
            Tools::sendMail($from, $to, $subject, $text);
407
            if ($msg) {
408
                \Inji\Msg::add('Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль и ссылка для активации', 'success');
409
            }
410
        } else {
411
            $from = 'noreply@' . INJI_DOMAIN_NAME;
412
            $to = $data['user_mail'];
413
            $subject = \I18n\Text::module('Users', 'Регистрация на сайте ${sitename}', ['sitename' => idn_to_utf8(INJI_DOMAIN_NAME)]);
414
            $text = \I18n\Text::module('Users', 'sucregmsg', [
415
                'sitename' => idn_to_utf8(INJI_DOMAIN_NAME),
416
                'pass' => $pass
417
            ]);
418
            Tools::sendMail($from, $to, $subject, $text);
419
            if ($msg) {
420
                \Inji\Msg::add(\I18n\Text::module('Users', 'Вы были зарегистрированы. На указанный почтовый ящик был выслан ваш пароль'), 'success');
421
            }
422
        }
423
        return $user->id;
424
    }
425
426
    public function hashpass($pass) {
427
        return password_hash($pass, PASSWORD_DEFAULT);
428
    }
429
430
    public function verifypass($pass, $hash) {
431
        return password_verify($pass, $hash);
432
    }
433
434
    public function getUserPartners($user, $levelsCount = 0) {
435
        $return = [
436
            'users' => [],
437
            'levels' => [],
438
            'count' => 0,
439
            'lastLevel' => 0
440
        ];
441
        $userIds = $user->user_id;
442
        for ($i = 1; $i <= $levelsCount || !$levelsCount; $i++) {
443
            if (!$userIds && $levelsCount) {
444
                $return['levels'][$i] = [];
445
                continue;
446
            } elseif (!$userIds && !$levelsCount) {
447
                break;
448
            }
449
            $usersLevel = \Users\User::getList(['where' => [['parent_id', $userIds, 'IN']]]);
450
            $return['users'] += $usersLevel;
451
            $return['levels'][$i] = array_keys($usersLevel);
452
            $userIds = implode(',', $return['levels'][$i]);
453
            $return['lastLevel'] = $i;
454
        }
455
        $return['count'] = count($return['users']);
456
        return $return;
457
    }
458
459
    /**
460
     * @param integer $cat_id
461
     */
462
    public function addUserActivity($user_id, $cat_id, $text = '') {
463
        $ua = new Users\Activity([
0 ignored issues
show
Bug introduced by
The type Inji\Users\Activity was not found. Did you mean Users\Activity? If so, make sure to prefix the type with \.
Loading history...
464
            'user_id' => $user_id,
465
            'category_id' => $cat_id,
466
            'text' => $text,
467
        ]);
468
        $ua->save();
469
    }
470
}