33 $device = static::findByCookie($context);
38 $device = static::findByUserAgent($context);
49 $device = static::add($context);
50 $deviceLogin = static::addDeviceLogin($device, $context);
52 if (Option::get(
'main',
'user_device_notify',
'N') ===
'Y')
55 static::sendEmail($device, $deviceLogin, $user);
61 static::update($device, $cookable);
62 static::addDeviceLogin($device, $context);
65 static::setCookie($device->getDeviceUid());
70 $request = Main\Context::getCurrent()->getRequest();
71 $deviceUid = $request->getCookie(static::COOKIE_NAME);
73 if (is_string($deviceUid) && $deviceUid !=
'')
75 return Internal\UserDeviceTable::query()
78 ->where(
'DEVICE_UID', $deviceUid)
88 $request = Main\Context::getCurrent()->getRequest();
89 $userAgent = $request->getUserAgent();
92 $query = Internal\UserDeviceTable::query()
95 ->where(
'COOKABLE',
false)
99 while ($device = $query->fetchObject())
101 $pattern = preg_replace(
'/\\d+/i',
'\\d+', preg_quote($device->getUserAgent(),
'/'));
102 if (preg_match(
"/{$pattern}/i", $userAgent))
111 protected static function add(
Context $context): EO_UserDevice
114 $device = Internal\UserDeviceTable::createObject();
118 ->setDeviceUid(Security\Random::getString(32))
119 ->setDeviceType($browser->getDeviceType())
120 ->setBrowser($browser->getName())
121 ->setPlatform($browser->getPlatform())
122 ->setUserAgent($browser->getUserAgent())
151 $ip = GeoIp\Manager::getRealIp();
153 $login = Internal\UserDeviceLoginTable::createObject();
156 ->setDeviceId($device->getId())
164 if (Option::get(
'main',
'user_device_geodata',
'N') ===
'Y')
166 $ipData = GeoIp\Manager::getDataResult($ip,
'', [
'cityGeonameId']);
168 if ($ipData && $ipData->isSuccess())
170 $data = $ipData->getGeoData();
173 ->setCityGeoid($data->cityGeonameId)
174 ->setRegionGeoid($data->subRegionGeonameId ?? $data->regionGeonameId)
175 ->setCountryIsoCode($data->countryCode)
185 protected static function sendEmail(EO_UserDevice $device, EO_UserDeviceLogin $deviceLogin, array $user): void
187 $site = $user[
'LID'];
190 $site = Main\Context::getCurrent()->getSite();
193 $site = \CSite::GetDefSite();
197 $currentLang = Main\Context::getCurrent()->getLanguage();
198 $lang = $user[
'LANGUAGE_ID'] !=
'' ? $user[
'LANGUAGE_ID'] : $currentLang;
204 $geoids = array_filter([$deviceLogin->getCityGeoid(), $deviceLogin->getRegionGeoid()]);
209 if (!empty($geonames))
212 if ($user[
'LANGUAGE_ID'] !=
'' && $user[
'LANGUAGE_ID'] != $currentLang)
214 $language = LanguageTable::getList([
215 'filter' => [
'=LID' => $user[
'LANGUAGE_ID'],
'=ACTIVE' =>
'Y'],
216 'cache' => [
'ttl' => 86400],
221 $langCode = $language->getCode();
226 $langCode = Main\Context::getCurrent()->getLanguageObject()->getCode();
229 if (($cityCode = $deviceLogin->getCityGeoid()) > 0)
231 $city = $geonames[$cityCode][$langCode] ?? $geonames[$cityCode][
'en'] ??
'';
233 if (($regionCode = $deviceLogin->getRegionGeoid()) > 0)
235 $region = $geonames[$regionCode][$langCode] ?? $geonames[$regionCode][
'en'] ??
'';
241 if (($countryCode = $deviceLogin->getCountryIsoCode()) !=
'')
243 $countries = \GetCountries($lang);
244 $country = $countries[$countryCode][
'NAME'];
248 $location = implode(
', ', array_filter([$city, $region, $country]));
251 'EMAIL' => $user[
'EMAIL'],
252 'LOGIN' => $user[
'LOGIN'],
253 'NAME' => $user[
'NAME'],
254 'LAST_NAME' => $user[
'LAST_NAME'],
255 'DEVICE' => $deviceTypes[$device->getDeviceType()],
256 'BROWSER' => $device->getBrowser(),
257 'PLATFORM' => $device->getPlatform(),
258 'USER_AGENT' => $device->getUserAgent(),
259 'IP' => $deviceLogin->getIp(),
260 'DATE' => $deviceLogin->getLoginDate(),
261 'COUNTRY' => $country,
264 'LOCATION' => $location,
268 'EVENT_NAME' => self::EMAIL_EVENT,
269 'C_FIELDS' => $fields,
271 'LANGUAGE_ID' => $lang,