<?php
namespace Bitrix\Main;
use Bitrix\Main\Config\Option;
use Bitrix\Main\Type\Date;
final class License
{
private ?string $_licenseKey = null;
private const DOMAINS_STORE_LICENSE = [
'ru' => 'https://util.1c-bitrix.ru',
'ua' => 'https://util.bitrix.ua',
'en' => 'https://util.bitrixsoft.com',
'kz' => 'https://util.1c-bitrix.kz',
'by' => 'https://util.1c-bitrix.by',
];
public const URL_BUS_EULA = [
'ru' => 'https://www.1c-bitrix.ru/download/law/eula_bus.pdf',
'by' => 'https://www.1c-bitrix.by/download/law/eula_bus.pdf',
'kz' => 'https://www.1c-bitrix.kz/download/law/eula_bus.pdf',
'ua' => 'https://www.bitrix.ua/download/law/eula_bus.pdf',
];
public const URL_CP_EULA = [
'ru' => 'https://www.1c-bitrix.ru/download/law/eula_cp.pdf',
'by' => 'https://www.1c-bitrix.by/download/law/eula_cp.pdf',
'kz' => 'https://www.1c-bitrix.kz/download/law/eula_cp.pdf',
'en' => 'https://www.bitrix24.com/eula/',
'br' => 'https://www.bitrix24.com.br/eula/',
'fr' => 'https://www.bitrix24.fr/eula/',
'pl' => 'https://www.bitrix24.pl/eula/',
'it' => 'https://www.bitrix24.it/eula/',
'la' => 'https://www.bitrix24.es/eula/',
];
public const URL_RENEWAL_LICENSE = [
'com' => 'https://store.bitrix24.com/profile/license-keys.php',
'eu' => 'https://store.bitrix24.eu/profile/license-keys.php',
'de' => 'https://store.bitrix24.de/profile/license-keys.php',
'ru' => 'https://www.1c-bitrix.ru/buy/products/b24.php#tab-section-2',
'by' => 'https://www.1c-bitrix.by/buy/products/b24.php#tab-section-2',
'kz' => 'https://www.1c-bitrix.kz/buy/products/b24.php#tab-section-2',
];
public function getKey(): string
{
if ($this->_licenseKey === null) {
$licenseKeyFile = Loader::getDocumentRoot() . '/bitrix/license_key.php';
$LICENSE_KEY = '';
if (file_exists($licenseKeyFile)) {
include($licenseKeyFile);
}
$this->_licenseKey = ($LICENSE_KEY == '' || strtoupper($LICENSE_KEY) == 'DEMO') ? 'DEMO' : $LICENSE_KEY;
}
return $this->_licenseKey;
}
public function getHashLicenseKey(): string
{
return md5($this->getKey());
}
public function getPublicHashKey(): string
{
return md5('BITRIX' . $this->getKey() . 'LICENCE');
}
public function isDemoKey(): bool
{
return $this->getKey() == 'DEMO';
}
public function getBuyLink(): string
{
return $this->getDomainStoreLicense() . '/key_update.php?license_key=' . $this->getHashLicenseKey() . '&tobasket=y&lang=' . LANGUAGE_ID;
}
public function getDocumentationLink(): string
{
$region = $this->getRegion();
if (in_array($region, ['ru', 'kz', 'by'])) {
return 'https://dev.1c-bitrix.ru/learning/course/index.php?COURSE_ID=135&LESSON_ID=25720';
}
return 'https://training.bitrix24.com/support/training/course/index.php?COURSE_ID=178&LESSON_ID=25932&LESSON_PATH=17520.17562.25930.25932';
}
public function getRenewalLink(): string
{
$region = $this->getRegion();
if (in_array($region, ['ru', 'by', 'kz', 'de'])) {
return self::URL_RENEWAL_LICENSE[$region];
}
if (in_array($region, ['eu', 'fr', 'pl', 'it', 'uk'])) {
return self::URL_RENEWAL_LICENSE['eu'];
}
return self::URL_RENEWAL_LICENSE['com'];
}
public function getDomainStoreLicense(): string
{
return self::DOMAINS_STORE_LICENSE[$this->getRegion()] ?? self::DOMAINS_STORE_LICENSE['ru'];
}
public function isDemo(): bool
{
return defined('DEMO') && DEMO === 'Y';
}
public function isTimeBound(): bool
{
return defined('TIMELIMIT_EDITION') && TIMELIMIT_EDITION === 'Y';
}
public function isEncoded(): bool
{
return defined('ENCODE') && ENCODE === 'Y';
}
public function getExpireDate(): ?Date
{
$timestamp = (int)($GLOBALS['SiteExpireDate'] ?? 0);
if ($timestamp > 0) {
return Date::createFromTimestamp($timestamp);
}
return null;
}
public function getSupportExpireDate(): ?Date
{
$dateString = Option::get('main', '~support_finish_date');
if (Date::isCorrect($dateString, 'Y-m-d')) {
return new Date($dateString, 'Y-m-d');
}
return null;
}
public function getRegion(): ?string
{
if (Loader::includeModule('bitrix24')) {
return \CBitrix24::getPortalZone();
}
$region = Option::get('main', '~PARAM_CLIENT_LANG');
if (!empty($region)) {
return $region;
}
$region = $this->getRegionFromPortalSettings();
if (!empty($region)) {
return $region;
}
return $this->getRegionFromLangFiles();
}
public function getEulaLink(): string
{
if (ModuleManager::isModuleInstalled('intranet')) {
return self::URL_CP_EULA[$this->getRegion()] ?? self::URL_CP_EULA['en'];
}
return self::URL_BUS_EULA[$this->getRegion()] ?? self::URL_BUS_EULA['ru'];
}
private function getRegionFromPortalSettings(): ?string
{
$vendorValue = Option::get('main', 'vendor');
if ($vendorValue === 'ua_bitrix_portal') {
return 'ua';
}
if ($vendorValue === 'bitrix_portal') {
return 'en';
}
if ($vendorValue === '1c_bitrix_portal') {
return 'ru';
}
return null;
}
private function getRegionFromLangFiles(): ?string
{
$documentRoot = Application::getDocumentRoot();
if (file_exists($documentRoot . '/bitrix/modules/main/lang/ua')) {
return 'ua';
}
if (file_exists($documentRoot . '/bitrix/modules/main/lang/by')) {
return 'by';
}
if (file_exists($documentRoot . '/bitrix/modules/main/lang/kz')) {
return 'kz';
}
if (file_exists($documentRoot . '/bitrix/modules/main/lang/ru')) {
return 'ru';
}
return null;
}
public function getPartnerId(): int
{
return (int)Option::get('main', '~PARAM_PARTNER_ID', 0);
}
public function getMaxUsers(): int
{
return (int)Option::get('main', 'PARAM_MAX_USERS', 0);
}
public function isExtraCountable(): bool
{
return Option::get('main', '~COUNT_EXTRA', 'N') === 'Y' && ModuleManager::isModuleInstalled('extranet');
}
public function getActiveUsersCount(Date $dateLimit = null)
{
$connection = Application::getConnection();
if ($dateLimit !== null) {
$dateCondition = 'AND U.LAST_LOGIN > ' . $connection->getSqlHelper()->convertToDbDate($dateLimit);
} else {
$dateCondition = 'AND U.LAST_LOGIN IS NOT NULL';
}
$userCount = 0;
if (ModuleManager::isModuleInstalled('intranet')) {
$query = '
SELECT COUNT(DISTINCT U.ID)
FROM
b_user U
INNER JOIN b_user_field F ON F.ENTITY_ID = \'USER\' AND F.FIELD_NAME = \'UF_DEPARTMENT\'
INNER JOIN b_utm_user UF ON
UF.FIELD_ID = F.ID
AND UF.VALUE_ID = U.ID
AND UF.VALUE_INT > 0
WHERE U.ACTIVE = \'Y\'
' . $dateCondition;
$userCount = (int)$connection->queryScalar($query);
if ($this->isExtraCountable()) {
$extranetGroupId = (int)Option::get('extranet', 'extranet_group');
if ($extranetGroupId > 0) {
$query = '
SELECT COUNT(1)
FROM
b_user U
INNER JOIN b_user_group UG ON UG.USER_ID = U.ID AND UG.GROUP_ID = ' . $extranetGroupId . '
LEFT JOIN (
SELECT UF.VALUE_ID
FROM
b_user_field F
INNER JOIN b_utm_user UF ON UF.FIELD_ID = F.ID AND UF.VALUE_INT > 0
WHERE F.ENTITY_ID = \'USER\' AND F.FIELD_NAME = \'UF_DEPARTMENT\'
) D ON D.VALUE_ID = U.ID
WHERE U.ACTIVE = \'Y\'
' . $dateCondition . '
AND D.VALUE_ID IS NULL
';
$userCount += (int)$connection->queryScalar($query);
}
}
}
return $userCount;
}
}
?>