Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- * @ This file is created by http://DeZender.Net
- * @ deZender (PHP7 Decoder for Plesk Encoder)
- *
- * @ Version : 4.1.0.1
- * @ Author : DeZender
- * @ Release on : 29.08.2020
- * @ Official site : http://DeZender.Net
- *
- */
- final class KeyLimitsChecker
- {
- const VERSION = '18.0';
- const KEY_SERVICE_PROVIDER_VIEW = 'service provider';
- const KEY_POWER_USER_VIEW = 'power user';
- static private function _getBackwardRestriction()
- {
- if (get_os() == WIN_NT) {
- return 1;
- }
- else {
- return 2;
- }
- }
- /**
- * @param array $key_props
- * @throws PleskLicenseKeyMultipleException
- */
- static public function checkKeyLight($key_props = false)
- {
- $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__check_wrong'));
- try {
- self::checkKeyVersion($key_props);
- }
- catch (PleskLicenseInvalidVersionException $e) {
- $me->add($e);
- }
- try {
- self::checkBackwardRestriction($key_props);
- }
- catch (PleskLicenseBackwardRestrictionException $e) {
- $me->add($e);
- }
- try {
- self::checkKeyExpired($key_props);
- }
- catch (PleskLicenseExpiredException $e) {
- $me->add($e);
- }
- try {
- self::checkVirtualizationOnly($key_props);
- }
- catch (PleskLicenseVirtualizationOnlyException $e) {
- $me->add($e);
- }
- if ($me->length()) {
- throw $me;
- }
- }
- /**
- * @param array $key_props
- * @throws PleskLicenseKeyMultipleException
- */
- static public function checkKey($key_props = false)
- {
- $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__check_wrong'));
- try {
- self::checkKeyLight($key_props);
- }
- catch (PleskLicenseKeyMultipleException $ex) {
- foreach ($ex->getAll() as $innerException) {
- $me->add($innerException);
- }
- }
- try {
- self::checkLimitsExceeded($key_props);
- }
- catch (PleskLicenseKeyMultipleException $e) {
- $me->add($e);
- }
- try {
- self::checkLocaleSignature($key_props);
- }
- catch (PleskLicenseInvalidLocaleSignatureException $e) {
- $me->add($e);
- }
- if ($me->length()) {
- throw $me;
- }
- }
- static private function checkLocaleSignature($key_props)
- {
- $allowedLocales = self::getKeyProp('allowed-locales', $key_props);
- if ($allowedLocales == 'any') {
- return NULL;
- }
- foreach (explode(',', $allowedLocales) as $locale) {
- if (!self::_isLocaleSigned($locale)) {
- throw new PleskLicenseInvalidLocaleSignatureException(lmsg('plesk_key__invalid_locale_signature'));
- }
- }
- }
- static private function _isLocaleSigned($locale)
- {
- static $publicKey = null;
- if ($publicKey === NULL) {
- $publicKey = openssl_get_publickey('-----BEGIN PUBLIC KEY-----' . "\n" . 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCdBbe98A0A421ntILlIQw2U4e' . "\n" . 'IrFYvja5IJ9rJb8vcIGlCw3T9gsBdi+4Cli3zKwcS5gFgjSW+214JtGBRKrSHziK' . "\n" . 'jcg/EDzd7zigpvPY+At6laOOGqyiH27UVjJnvvWuQs8GyZ5OJNYiM9SHQGJOQDz5' . "\n" . 'ynsVXpQG41YeWHhQOQIDAQAB' . "\n" . '-----END PUBLIC KEY-----');
- }
- if (!($handle = opendir(PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale)))) {
- return false;
- }
- $ret = false;
- while (($file = readdir($handle)) !== false) {
- $info = $path_parts = pathinfo($file);
- if (!array_key_exists('extension', $info) || ($info['extension'] != 'php')) {
- continue;
- }
- $signatureFile = PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale . '/' . $file . '.signature');
- if (!file_exists($signatureFile)) {
- return false;
- }
- $signature = file_get_contents($signatureFile);
- $data = file_get_contents(PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale . '/') . $file);
- $ret = openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA1) == 1;
- if (!$ret) {
- break;
- }
- }
- closedir($handle);
- return $ret;
- }
- static private function checkKeyVersion($key_props)
- {
- $vers = self::getKeyProp('product_version', $key_props);
- $match = false;
- foreach ($vers as $ver) {
- if (!is_array($ver)) {
- $m1 = (PLESK_KEY_PRODUCT == 'ppa' ? $ver : strtok($ver, '.'));
- $m2 = (PLESK_KEY_PRODUCT == 'ppa' ? '18.0' : strtok('18.0', '.'));
- $match |= $m1 == $m2;
- }
- else {
- $match |= (($ver[0] == 'any') || (version_compare($ver[0], '18.0') <= 0)) && (($ver[1] == 'any') || (0 <= version_compare($ver[1], '18.0')));
- }
- }
- if (!$match) {
- throw new PleskLicenseInvalidVersionException(lmsg('plesk_key__wrong_version'));
- }
- }
- static private function checkBackwardRestriction($key_props = false)
- {
- $backwardRestriction = self::getKeyProp('backward-restriction', $key_props);
- if (self::_getBackwardRestriction() < $backwardRestriction) {
- throw new PleskLicenseBackwardRestrictionException(lmsg('plesk_key__backward_incompatibility'));
- }
- }
- static private function checkVirtualizationOnly($key_props = false)
- {
- $allowed_virtualizations = [];
- foreach (Virtualization::getVirtualizations() as $env => $env_info) {
- if (!empty($env_info['key']) && self::getKeyProp($env_info['key'], $key_props)) {
- if (Virtualization::checkEnvironment($env)) {
- return NULL;
- }
- else {
- $allowed_virtualizations[] = Virtualization::getEnvironmentName($env);
- }
- }
- }
- $allowed_virtualizations = array_unique($allowed_virtualizations);
- if (!count($allowed_virtualizations)) {
- return true;
- }
- else if (count($allowed_virtualizations) == 1) {
- throw new PleskLicenseVirtualizationOnlyException(lmsg('plesk_key__virtualization_only_key', $allowed_virtualizations[0]));
- }
- else {
- throw new PleskLicenseVirtualizationOnlyException(lmsg('plesk_key__virtualizations_only_key', implode(', ', $allowed_virtualizations)));
- }
- }
- static private function checkKeyExpired($key_props = false)
- {
- $lim_date = self::getKeyProp('lim_date', $key_props);
- if ((0 <= $lim_date) && ($lim_date < date('Ymd'))) {
- $buyUrl = Plesk_Config::get()->product->buyUrl;
- $link = '<a href=\'' . $buyUrl . '\' target=\'_blank\'>' . lmsg('plesk_key__key_purchase') . '</a>';
- throw new PleskLicenseExpiredException(lmsg('plesk_key__key_expired', $link));
- }
- }
- static public function checkLimitsExceeded($key_props = false)
- {
- $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__limits_exceeded'));
- if (get_os() == WIN_NT) {
- $limits = ['lim_dom', 'lim_cl', 'lim_mn', 'lim_wu'];
- }
- else {
- $limits = ['lim_dom', 'lim_domain_aliases', 'lim_cl', 'lim_mn', 'lim_wu'];
- }
- foreach ($limits as $limit) {
- try {
- self::checkLimitExceeded($limit, $key_props);
- }
- catch (PleskLicenseLimitException $e) {
- $me->add($e);
- }
- }
- if ($me->length()) {
- throw $me;
- }
- }
- static private function checkLimitExceeded($limit, $key_props = false)
- {
- $limit_value = self::getKeyProp($limit, $key_props);
- if ($limit_value < 0) {
- return NULL;
- }
- switch ($limit) {
- case 'lim_dom':
- $used = KeyLimitsChecker::getDomainsUsed();
- break;
- case 'lim_domain_aliases':
- $used = KeyLimitsChecker::getDomainAliasesUsed();
- break;
- case 'lim_cl':
- $used = KeyLimitsChecker::getClientsUsed();
- break;
- case 'lim_mn':
- $used = KeyLimitsChecker::getMailnamesUsed();
- break;
- case 'lim_wu':
- $used = KeyLimitsChecker::getWebusersUsed();
- break;
- default:
- throw new PleskFatalException('Unknown limit name.');
- }
- if ($limit_value < $used) {
- throw new PleskLicenseLimitException(lmsg('plesk_key__limits_exceeded_' . $limit, $limit_value, $used, $limit_value - $used));
- }
- }
- static public function resetKeyLocks($key_props = false)
- {
- if ((Session::get()->auth()->getUser()->getType() == Session::IS_ADMIN) && self::getKeyProp('key_askupdate', $key_props)) {
- topnote(['login_up__partner_license_message', HTML::renderHref(['href' => '/server/key_info.php', 'close' => false]), HTML::renderHref(['href' => '/server/key_info.php', 'open' => false])], 'partner_license_message', true, MSG_WARNING);
- }
- else {
- clear_messages(MSG_ANY, 0, 'partner_license_message');
- }
- $license_update_date = self::getKeyProp('license_update_date', $key_props);
- $lim_date = self::getKeyProp('lim_date', $key_props);
- if (Session::get()->auth()->isAdmin()) {
- if ($license_update_date && $lim_date && (date('Ymd') <= $lim_date) && (1 < ($lim_date - $license_update_date)) && ($license_update_date <= date('Ymd', time() - 86400)) && !Plesk_Mode::isLicenseDefault()) {
- topnote(['login_up__grace_period', HTML::renderHref(['href' => '/server/key_info.php', 'close' => false]), HTML::renderHref(['href' => '/server/key_info.php', 'open' => false])], 'grace_period_message', true, MSG_WARNING);
- }
- else {
- clear_messages(MSG_ANY, 0, 'grace_period_message');
- }
- }
- try {
- clear_messages(MSG_ANY, 0, 'key_check_wrong');
- KeyLimitsChecker::checkKey($key_props);
- if (get_os() == WIN_NT) {
- unset(Session::get()->license()->isWrong);
- }
- else {
- Session::get()->license()->isWrong = false;
- }
- del_param('PleskLicenseExpiredException');
- }
- catch (PleskLicenseKeyMultipleException $e) {
- $alreadyExpired = get_param('PleskLicenseExpiredException') == 'true';
- del_param('PleskLicenseExpiredException');
- if (count($e->getInstancesOf('PleskLicenseExpiredException'))) {
- if (!$alreadyExpired) {
- try {
- updateKeyAndNotify(getPleskKey());
- getPleskKey(true);
- KeyLimitsChecker::checkKey($key_props);
- return NULL;
- }
- catch (Exception $exception) {
- Plesk_Log::debug($exception);
- Plesk_Log::err('Auto update of license has failed: ' . $exception->getMessage());
- }
- $key = new Key_Primary($key_props);
- $actionLog = new ActionLog('license_expired');
- $actionLog->log('COMP_LICENSE', $key->getProp('plesk_key_id'), $key->getProp('plesk_key_id'), ActionLog_Component::IS_REQ);
- $actionLog->submit();
- }
- put_param('PleskLicenseExpiredException', 'true');
- }
- else if (!Plesk_Mode::isLicenseDefault()) {
- topnote($e->getMultipleMessage(), 'key_check_wrong', true, MSG_ERROR);
- }
- Session::get()->license()->isWrong = true;
- throw $e;
- }
- }
- static public function getKeyProp($name, $key_props = false)
- {
- if (get_os() == WIN_NT) {
- if ($key_props === false) {
- return get_key_prop($name);
- }
- return isset($key_props[$name]) ? $key_props[$name] : false;
- }
- else {
- if ($key_props === false) {
- return getKeyProp($name);
- }
- if (!array_key_exists($name, $key_props)) {
- return getKeyDefaultProp($name);
- }
- return $key_props[$name];
- }
- }
- /**
- * Check locale code and license compatibility
- *
- * @param string $locale
- * @param array|boolean $key_props
- * @return boolean
- */
- static public function isLocaleAllowed($locale, $key_props = false)
- {
- $allowedLocales = self::getKeyProp('allowed-locales', $key_props);
- if ($allowedLocales == 'any') {
- return true;
- }
- $allowedLocalesArray = explode(',', $allowedLocales);
- return in_array($locale, $allowedLocalesArray);
- }
- /**
- * Returns number of domains already created
- * @return integer
- */
- static public function getDomainsUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM domains WHERE parentDomainId = 0');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of subscriptions already created
- * @return integer
- */
- static public function getSubscriptionsUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM domains WHERE webspace_id = 0');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- static public function getDomainAliasesUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM domain_aliases');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of clients already created
- * @return integer
- */
- static public function getClientsUsed()
- {
- $res = db_query_ex('SELECT COUNT(*) FROM clients where type <> :type', ['type' => Client::TYPE_ADMIN]);
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of customers already created
- * @return integer
- */
- static public function getCustomersUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM clients where type = "client"');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of resellers already created
- * @return integer
- */
- static public function getResellersUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM clients where type = "reseller"');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of mailnames used.
- * @return integer
- */
- static public function getMailnamesUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM mail');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Returns number of webusers used.
- * @return integer
- */
- static public function getWebusersUsed()
- {
- $res = db_query('SELECT COUNT(*) FROM web_users');
- list($num) = db_fetch_row($res);
- return (int) $num;
- }
- /**
- * Check if mailname creation is allowed by key limits
- * @return boolean
- */
- static public function chkMailnames()
- {
- $limit = get_key_prop('lim_mn');
- if ($limit <= 0) {
- return true;
- }
- return KeyLimitsChecker::getMailnamesUsed() < $limit;
- }
- /**
- * Return list of potential problems with license key according to current usage
- *
- * @return array
- */
- static public function getPotentialProblems()
- {
- $problems = [];
- $usedSubscriptions = self::getSubscriptionsUsed();
- if (!getKeyProp('can-manage-subscriptions') && (1 < $usedSubscriptions)) {
- $problems[] = lmsg('plesk_key__limits_exceeded_no_subscriptions_management', $usedSubscriptions);
- }
- $usedResellers = self::getResellersUsed();
- if (!getKeyProp('can-manage-resellers') && (0 < $usedResellers)) {
- $problems[] = lmsg('plesk_key__limits_exceeded_no_resellers_management', $usedResellers);
- }
- $usedCustomers = static::getCustomersUsed();
- if (!getKeyProp('can-manage-customers') && (0 < $usedCustomers)) {
- $problems[] = lmsg('plesk_key__limits_exceeded_no_customers_management', $usedCustomers);
- .....................................................................................
- ....................................................
- ........................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement