Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
user.php
1<?php
9namespace Bitrix\Im\Call;
10
14
15class User
16{
17 const MODULE_ID = 'im';
18 const EXTERNAL_AUTH_ID = 'call';
19
20 static private $error = null;
21
22 public static function register($userFields = [])
23 {
24 self::clearError();
25
26 if (isset($userFields['USER_HASH']) &&
27 trim($userFields['USER_HASH']) &&
28 preg_match("/^[a-fA-F0-9]{32}$/i", $userFields['USER_HASH']))
29 {
30 $userCode = $userFields['USER_HASH'];
31
32 $userData = \Bitrix\Main\UserTable::getList(
33 [
34 'select' => ['ID', 'EXTERNAL_AUTH_ID'],
35 'filter' => ['=XML_ID' => 'call|'.$userCode]
36 ]
37 )->fetch();
38 if ($userData && $userData['EXTERNAL_AUTH_ID'] == self::EXTERNAL_AUTH_ID)
39 {
40 return [
41 'ID' => $userData['ID'],
42 'HASH' => $userCode,
43 'CREATED' => false
44 ];
45 }
46 }
47 else
48 {
49 $userCode = self::getUserCode();
50 }
51
52 $fields = [];
53
54 $fields['NAME'] = isset($userFields['NAME']) && trim($userFields['NAME']) ? $userFields['NAME'] : Loc::getMessage('IM_CALL_USER_DEFAULT_NAME');
55 $fields['LAST_NAME'] = isset($userFields['LAST_NAME']) ? trim($userFields['LAST_NAME']) : '';
56
57 if (isset($userFields['AVATAR']) && trim($userFields['AVATAR']))
58 {
59 $userFields['AVATAR'] = self::getPersonalPhoto($userFields['AVATAR']);
60 if ($userFields['AVATAR'])
61 {
62 $fields['PERSONAL_PHOTO'] = $userFields['AVATAR'];
63 }
64 }
65 if (isset($userFields['EMAIL']) && trim($userFields['EMAIL']))
66 {
67 $fields['EMAIL'] = trim($userFields['EMAIL']);
68 }
69 if (isset($userFields['PERSONAL_WWW']) && trim($userFields['PERSONAL_WWW']))
70 {
71 $fields['PERSONAL_WWW'] = trim($userFields['PERSONAL_WWW']);
72 }
73 if (isset($userFields['PERSONAL_GENDER']) && trim($userFields['PERSONAL_GENDER']))
74 {
75 $fields['PERSONAL_GENDER'] = $userFields['PERSONAL_GENDER'] == 'F' ? 'F' : 'M';
76 }
77 if (isset($userFields['WORK_POSITION']) && trim($userFields['WORK_POSITION']))
78 {
79 $fields['WORK_POSITION'] = trim($userFields['WORK_POSITION']);
80 }
81
82 $fields['LOGIN'] = self::MODULE_ID.'_call_'.rand(1000, 9999).randString(5);
83 $fields['PASSWORD'] = md5($fields['LOGIN'].'|'.rand(1000, 9999).'|'.time());
84 $fields['CONFIRM_PASSWORD'] = $fields['PASSWORD'];
85 $fields['EXTERNAL_AUTH_ID'] = self::EXTERNAL_AUTH_ID;
86 $fields['XML_ID'] = 'call|'.$userCode;
87 $fields['ACTIVE'] = 'Y';
88
89 $userManager = new \CUser;
90 $userId = $userManager->Add($fields);
91 if (!$userId)
92 {
93 $errorCode = '';
94 $errorMessage = '';
95
96 global $APPLICATION;
97 if ($exception = $APPLICATION->GetException())
98 {
99 $errorCode = $exception->GetID();
100 $errorMessage = $exception->GetString();
101 }
102
103 self::setError(
104 __METHOD__,
105 'USER_REGISTER_ERROR',
106 Loc::getMessage('IM_CALL_USER_ERROR_CREATE'),
107 ['CODE' => $errorCode, 'MSG' => $errorMessage]
108 );
109
110 return false;
111 }
112
113 return [
114 'ID' => $userId,
115 'HASH' => $userCode,
116 'CREATED' => true
117 ];
118 }
119
120 public static function get($userId)
121 {
122 if (!\Bitrix\Main\Loader::includeModule('im'))
123 {
124 return [];
125 }
126
127 $userData = \Bitrix\Main\UserTable::getById($userId)->fetch();
128
129 $avatar = '';
130 if ($userData['PERSONAL_PHOTO'])
131 {
132 $resizedImage = \CFile::ResizeImageGet(
133 $userData["PERSONAL_PHOTO"],
134 ['width' => 100, 'height' => 100],
135 BX_RESIZE_IMAGE_EXACT,
136 false,
137 false,
138 true
139 );
140 if (!empty($resizedImage['src']))
141 {
142 $avatar = $resizedImage['src'];
143 }
144 }
145
146 if ($userData['NAME'] || $userData['LAST_NAME'])
147 {
148 $name = \Bitrix\Im\User::formatFullNameFromDatabase($userData);
149 $firstName = \Bitrix\Im\User::formatNameFromDatabase($userData);
150 }
151 else
152 {
153 $name = '';
154 $firstName = '';
155 }
156
157 return [
158 'ID' => (int)$userData['ID'],
159 'HASH' => mb_substr($userData['XML_ID'], mb_strlen(Auth::AUTH_TYPE) + 1),
160 'NAME' => $name,
161 'FIRST_NAME' => $firstName,
162 'LAST_NAME' => $userData['LAST_NAME'],
163 'AVATAR' => $avatar,
164 'EMAIL' => $userData['EMAIL'],
165 'PHONE' => $userData['PERSONAL_MOBILE'],
166 'WWW' => $userData['PERSONAL_WWW'],
167 'GENDER' => $userData['PERSONAL_GENDER'],
168 'POSITION' => $userData['WORK_POSITION'],
169 ];
170 }
171
172 public static function getPersonalPhoto($avatarUrl = '')
173 {
174 if (!$avatarUrl)
175 {
176 return '';
177 }
178
179 if (!in_array(mb_strtolower(\GetFileExtension($avatarUrl)), ['png', 'jpg', 'jpeg', 'gif', 'webp']))
180 {
181 return '';
182 }
183
184 $recordFile = \CFile::MakeFileArray($avatarUrl);
185 if (!\CFile::IsImage($recordFile['name'], $recordFile['type']))
186 {
187 return '';
188 }
189
190 if (is_array($recordFile) && $recordFile['size'] && $recordFile['size'] > 0 && $recordFile['size'] < 1000000)
191 {
192 $recordFile = array_merge($recordFile, ['MODULE_ID' => 'im']);
193 }
194 else
195 {
196 $recordFile = '';
197 }
198
199 return $recordFile;
200 }
201
202 public static function getUserCode(): string
203 {
204 if (\Bitrix\Main\ModuleManager::isModuleInstalled('bitrix24') && defined('BX24_HOST_NAME'))
205 {
206 $licence = BX24_HOST_NAME;
207 }
208 else
209 {
210 $licence = Application::getInstance()->getLicense()->getKey();
211 }
212
213 return md5(time().bitrix_sessid().$licence.uniqid());
214 }
215
219 public static function getError()
220 {
221 if (is_null(static::$error))
222 {
223 self::clearError();
224 }
225
226 return static::$error;
227 }
228
237 private static function setError($method, $code, $msg, $params = [])
238 {
239 static::$error = new BasicError($method, $code, $msg, $params);
240
241 return true;
242 }
243
244 private static function clearError()
245 {
246 static::$error = new BasicError(null, '', '');
247
248 return true;
249 }
250}
static getUserCode()
Definition user.php:202
static getPersonalPhoto($avatarUrl='')
Definition user.php:172
const EXTERNAL_AUTH_ID
Definition user.php:18
static getError()
Definition user.php:219
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static isModuleInstalled($moduleName)