Issues (1519)

system/modules/Users/Users.php (26 issues)

1
<?php
2
3
namespace Inji;
4
/**
5
 * Users module
6
 *
7
 * @author Alexey Krupskiy <[email protected]>
8
 * @link http://inji.ru/
9
 * @copyright 2015 Alexey Krupskiy
10
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
11
 */
12
class Users extends Module {
13
    public $name = 'Users';
14
    public $cookiePrefix = '';
15
16
    public function init() {
17
        if (!empty($this->config['cookieSplit'])) {
18
            $this->cookiePrefix = App::$cur->type;
19
        }
20
        Users\User::$cur = new Users\User(array('group_id' => 1, 'role_id' => 1));
21
        if (!empty($_GET['invite_code']) && is_string($_GET['invite_code'])) {
22
            setcookie('invite_code', $_GET['invite_code'], time() + 360000, "/");
23
        }
24
        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...
25
            return false;
26
        }
27
        if (isset($_GET['logout'])) {
28
            return $this->logOut();
0 ignored issues
show
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...
29
        }
30
        if (isset($_GET['passre']) && filter_input(INPUT_GET, 'user_mail')) {
31
            $this->passre(trim(filter_input(INPUT_GET, 'user_mail')));
32
        }
33
        if (!empty($_GET['passrecont']) && filter_input(INPUT_GET, 'hash')) {
34
            $this->passrecont(filter_input(INPUT_GET, 'hash'));
35
        }
36
        if (isset($_POST['autorization']) && trim(filter_input(INPUT_POST, 'user_login')) && trim(filter_input(INPUT_POST, 'user_pass'))) {
37
            unset($_POST['autorization']);
38
            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')));
39
        }
40
        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'])) {
41
            return $this->cuntinueSession($_COOKIE[$this->cookiePrefix . '_user_session_hash'], $_COOKIE[$this->cookiePrefix . '_user_id']);
0 ignored issues
show
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...
42
        }
43
    }
44
45
    public function logOut($redirect = true) {
46
        if (!empty($_COOKIE[$this->cookiePrefix . "_user_session_hash"]) && !empty($_COOKIE[$this->cookiePrefix . "_user_id"])) {
47
            $session = Users\Session::get([
48
                ['user_id', $_COOKIE[$this->cookiePrefix . "_user_id"]],
49
                ['hash', $_COOKIE[$this->cookiePrefix . "_user_session_hash"]]
50
            ]);
51
            if ($session) {
52
                $session->delete();
53
            }
54
        }
55
        if (!headers_sent()) {
56
            setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
57
            setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
58
        }
59
        if ($redirect) {
60
            if (!empty($this->config['logoutUrl'][$this->app->type])) {
61
                Tools::redirect($this->config['logoutUrl'][$this->app->type]);
62
            }
63
            Tools::redirect('/', \I18n\Text::module('Users', 'Вы вышли из своего профиля'), 'success');
0 ignored issues
show
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...
64
        }
65
    }
66
67
    public function cuntinueSession($hash, $userId) {
68
        $session = Users\Session::get([
69
            ['user_id', $userId],
70
            ['hash', $hash]
71
        ]);
72
        if (!$session) {
73
            if (!headers_sent()) {
74
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
75
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
76
            }
77
            Tools::redirect('/', \I18n\Text::module('Users', 'Произошла непредвиденная ошибка при авторизации сессии'));
78
        }
79
        if ($session->user->id != $userId) {
80
            Tools::redirect('/', \I18n\Text::module('Users', 'Произошла непредвиденная ошибка при авторизации сессии'));
81
        }
82
        if ($session && $session->user && $session->user->blocked) {
83
            if (!headers_sent()) {
84
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
85
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
86
            }
87
            \Inji\Msg::add(\I18n\Text::module('Users', 'Ваш аккаунт заблокирован'), 'info');
88
            return;
89
        }
90
        if ($session && $session->user && !$session->user->blocked) {
91
            if (!empty($this->config['needActivation']) && $session->user->activation) {
92
                if (!headers_sent()) {
93
                    setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
94
                    setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
95
                }
96
                Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>');
97
            } elseif ($session->user->activation) {
98
                \Inji\Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $session->user->id . '"><b>повторно выслать ссылку активации</b></a>');
99
            }
100
            if (!$session->user->mail && !empty($this->config['noMailNotify'])) {
101
                \Inji\Msg::add($this->config['noMailNotify']);
102
            }
103
            Users\User::$cur = $session->user;
104
            Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP';
105
            Users\User::$cur->save();
106
        } else {
107
            if (!headers_sent()) {
108
                setcookie($this->cookiePrefix . "_user_session_hash", '', 0, "/");
109
                setcookie($this->cookiePrefix . "_user_id", '', 0, "/");
110
            }
111
            \Inji\Msg::add(\I18n\Text::module('Users', 'needrelogin'), 'info');
112
        }
113
    }
114
115
    /**
116
     * @param string $user_mail
117
     */
118
    public function passre($user_mail) {
119
        $user = $this->get($user_mail, 'mail');
120
        if (!$user) {
121
            \Inji\Msg::add(\I18n\Text::module('Users', 'mailnotfound', ['user_mail' => $user_mail]), 'danger');
122
            return false;
123
        }
124
        $passre = Users\Passre::get([['user_id', $user->id], ['status', 1]]);
0 ignored issues
show
The type Inji\Users\Passre was not found. Did you mean Users\Passre? If so, make sure to prefix the type with \.
Loading history...
The property id does not exist on true.
Loading history...
125
        if ($passre) {
126
            $passre->status = 2;
127
            $passre->save();
128
        }
129
        $hash = $user->id . '_' . Tools::randomString(50);
130
        $passre = new Users\Passre(['user_id' => $user->id, 'status' => 1, 'hash' => $hash]);
131
        $passre->save();
132
        $domainRaw = App::$cur->getDomain();
133
        $domain = App::$cur->getDomain(true);
134
        $title = \I18n\Text::module('Users', 'Восстановление пароля на сайте ${domain}', ['domain' => $domain]);
135
        $text = \I18n\Text::module('Users', 'repassmailtext', ['domain' => $domain, 'hash' => $hash]);
136
        Tools::sendMail('noreply@' . $domainRaw, $user_mail, $title, $text);
137
        Tools::redirect('/', \I18n\Text::module('Users', 'На указанный почтовый ящик была выслана инструкция по восстановлению пароля'), 'success');
138
    }
139
140
    public function passrecont($hash) {
141
        $passre = Users\Passre::get([['hash', $hash]]);
142
        if ($passre) {
143
            if ($passre->status != 1) {
144
                Tools::redirect('/', 'Этот код восстановление более недействителен', 'danger');
145
            }
146
            $passre->status = 3;
147
            $passre->save();
148
            $pass = Tools::randomString(10);
149
            $user = Users\User::get($passre->user_id);
150
            $user->pass = $this->hashpass($pass);
0 ignored issues
show
The property pass does not exist on false.
Loading history...
151
            $user->save();
152
            $this->autorization($user->id, $pass, 'id', true, true);
0 ignored issues
show
The property id does not exist on false.
Loading history...
153
            $domainRaw = App::$cur->getDomain();
154
            $domain = App::$cur->getDomain(true);
155
            $title = \I18n\Text::module('Users', 'Новый пароль на сайте ${domain}', ['domain' => $domain]);
156
            $text = \I18n\Text::module('Users', 'newpassmail', ['domain' => $domain, 'pass' => $pass]);
157
            Tools::sendMail('noreply@' . $domainRaw, $user->mail, $title, $text);
0 ignored issues
show
The property mail does not exist on false.
Loading history...
158
            Tools::redirect('/', \I18n\Text::module('Users', 'Вы успешно сбросили пароль и были авторизованы на сайте. На ваш почтовый ящик был выслан новый пароль'), 'success');
159
        }
160
    }
161
162
    public function autorization($login, $pass, $ltype = 'login', $noMsg = true, $skipErrorCheck = false, $redirect = '') {
163
        $user = $this->get($login, $ltype);
164
        if ($user && !$skipErrorCheck) {
165
            $lastSuccessLogin = \Users\User\LoginHistory::lastSuccessLogin($user->id);
0 ignored issues
show
The property id does not exist on true.
Loading history...
166
            $where = [['user_id', $user->id]];
167
            if ($lastSuccessLogin) {
168
                $where[] = ['date_create', $lastSuccessLogin->date_create, '>'];
169
            }
170
            $loginHistoryErrorCount = \Users\User\LoginHistory::getCount(['where' => $where]);
171
            if ($loginHistoryErrorCount > 5) {
172
                \Inji\Msg::add(\I18n\Text::module('Users', 'logintrylimit', ['user_mail' => $user->mail]), 'danger');
0 ignored issues
show
The property mail does not exist on true.
Loading history...
173
                return false;
174
            }
175
        }
176
        if ($user && $this->verifypass($pass, $user->pass) && !$user->blocked) {
0 ignored issues
show
The property pass does not exist on true.
Loading history...
The property blocked does not exist on true.
Loading history...
177
            $loginHistory = new \Users\User\LoginHistory([
178
                'user_id' => $user->id,
179
                'ip' => $_SERVER['REMOTE_ADDR'],
180
                'success' => true
181
            ]);
182
            $loginHistory->save();
183
            if (!empty($this->config['needActivation']) && $user->activation) {
0 ignored issues
show
The property activation does not exist on true.
Loading history...
184
                Tools::redirect('/', 'Этот аккаунт ещё не активирован. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>');
185
            } elseif ($user->activation) {
186
                \Inji\Msg::add('Этот аккаунт ещё не активирован, не все функции могут быть доступны. <br />Если вы не получали письмо с ссылкой для активации, нажмите на - <a href = "/users/resendActivation/' . $user->id . '"><b>повторно выслать ссылку активации</b></a>');
187
            }
188
            if (!$user->mail && !empty($this->config['noMailNotify'])) {
189
                \Inji\Msg::add($this->config['noMailNotify']);
190
            }
191
            $this->newSession($user);
192
193
            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...
194
            Users\User::$cur->date_last_active = 'CURRENT_TIMESTAMP';
0 ignored issues
show
The property date_last_active does not exist on true.
Loading history...
195
            Users\User::$cur->save();
196
            if (!$noMsg) {
197
                if (!empty($this->config['loginUrl'][$this->app->type]) && !$redirect) {
198
                    $redirect = $this->config['loginUrl'][$this->app->type];
199
                }
200
                Tools::redirect($redirect);
201
            }
202
203
            return true;
204
        }
205
        if (!$noMsg) {
206
            if ($user && $user->blocked) {
207
                \Inji\Msg::add('Вы заблокированы', 'danger');
208
            } elseif ($user) {
209
                $loginHistory = new \Users\User\LoginHistory([
210
                    'user_id' => $user->id,
211
                    'ip' => $_SERVER['REMOTE_ADDR'],
212
                    'success' => false
213
                ]);
214
                $loginHistory->save();
215
                \Inji\Msg::add(\I18n\Text::module('Users', 'loginfail', ['user_mail' => $user->mail]), 'danger');
216
            } else {
217
                \Inji\Msg::add(\I18n\Text::module('Users', 'Данный почтовый ящик не зарегистрирован в системе'), 'danger');
218
            }
219
        }
220
221
        return false;
222
    }
223
224
    public function newSession($user) {
225
        $session = $this->createSession($user);
226
        if (!headers_sent()) {
227
            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...
228
            setcookie($this->cookiePrefix . "_user_id", $user->id, time() + 360000, "/");
229
        } else {
230
            \Inji\Msg::add('Не удалось провести авторизацию. Попробуйте позже', 'info');
231
        }
232
    }
233
234
    public function createSession($user) {
235
        do {
236
            $hash = Tools::randomString(255);
237
        } while (Users\Session::get($hash, 'hash'));
238
239
        $session = new Users\Session([
240
            'user_id' => $user->id,
241
            'agent' => $_SERVER['HTTP_USER_AGENT'],
242
            'ip' => $_SERVER['REMOTE_ADDR'],
243
            'hash' => $hash
244
        ]);
245
        $session->save();
246
        return $session;
247
    }
248
249
    /**
250
     * Return user
251
     *
252
     * @param integer|string $idn
253
     * @param string $ltype
254
     * @return boolean|\Users\User
255
     */
256
    public function get($idn, $ltype = 'id') {
257
        if (!$idn)
258
            return false;
259
260
        if (is_numeric($idn) && $ltype != 'login')
261
            $user = Users\User::get($idn, 'id');
262
        elseif ($ltype == 'login')
263
            $user = Users\User::get($idn, 'login');
264
        else
265
            $user = Users\User::get($idn, 'mail');
266
        if (!$user)
267
            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...
268
269
        return $user;
270
    }
271
272
    private function msgOrErr($err, $msg) {
273
        if ($msg) {
274
            \Inji\Msg::add($err, 'danger');
275
            return false;
276
        }
277
        return ['success' => false, 'error' => $err];
278
279
    }
280
281
    public function registration($data, $autorization = false, $msg = true) {
282
283
        if (empty($data['user_mail'])) {
284
            return $this->msgOrErr('Вы не ввели E-mail', $msg);
285
        }
286
        if (!empty($this->config['csrf']) && !App::$cur->server->checkCsrfForm($data)) {
0 ignored issues
show
The method checkCsrfForm() does not exist on null. ( Ignorable by Annotation )

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

286
        if (!empty($this->config['csrf']) && !App::$cur->server->/** @scrutinizer ignore-call */ checkCsrfForm($data)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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

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