Advertisement
Guest User

Untitled

a guest
Nov 16th, 2020
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.29 KB | None | 0 0
  1. /**
  2. *
  3. * @ This file is created by http://DeZender.Net
  4. * @ deZender (PHP7 Decoder for Plesk Encoder)
  5. *
  6. * @ Version : 4.1.0.1
  7. * @ Author : DeZender
  8. * @ Release on : 29.08.2020
  9. * @ Official site : http://DeZender.Net
  10. *
  11. */
  12.  
  13. final class KeyLimitsChecker
  14. {
  15. const VERSION = '18.0';
  16. const KEY_SERVICE_PROVIDER_VIEW = 'service provider';
  17. const KEY_POWER_USER_VIEW = 'power user';
  18.  
  19. static private function _getBackwardRestriction()
  20. {
  21. if (get_os() == WIN_NT) {
  22. return 1;
  23. }
  24. else {
  25. return 2;
  26. }
  27. }
  28.  
  29. /**
  30. * @param array $key_props
  31. * @throws PleskLicenseKeyMultipleException
  32. */
  33. static public function checkKeyLight($key_props = false)
  34. {
  35. $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__check_wrong'));
  36.  
  37. try {
  38. self::checkKeyVersion($key_props);
  39. }
  40. catch (PleskLicenseInvalidVersionException $e) {
  41. $me->add($e);
  42. }
  43.  
  44. try {
  45. self::checkBackwardRestriction($key_props);
  46. }
  47. catch (PleskLicenseBackwardRestrictionException $e) {
  48. $me->add($e);
  49. }
  50.  
  51. try {
  52. self::checkKeyExpired($key_props);
  53. }
  54. catch (PleskLicenseExpiredException $e) {
  55. $me->add($e);
  56. }
  57.  
  58. try {
  59. self::checkVirtualizationOnly($key_props);
  60. }
  61. catch (PleskLicenseVirtualizationOnlyException $e) {
  62. $me->add($e);
  63. }
  64.  
  65. if ($me->length()) {
  66. throw $me;
  67. }
  68. }
  69.  
  70. /**
  71. * @param array $key_props
  72. * @throws PleskLicenseKeyMultipleException
  73. */
  74. static public function checkKey($key_props = false)
  75. {
  76. $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__check_wrong'));
  77.  
  78. try {
  79. self::checkKeyLight($key_props);
  80. }
  81. catch (PleskLicenseKeyMultipleException $ex) {
  82. foreach ($ex->getAll() as $innerException) {
  83. $me->add($innerException);
  84. }
  85. }
  86.  
  87. try {
  88. self::checkLimitsExceeded($key_props);
  89. }
  90. catch (PleskLicenseKeyMultipleException $e) {
  91. $me->add($e);
  92. }
  93.  
  94. try {
  95. self::checkLocaleSignature($key_props);
  96. }
  97. catch (PleskLicenseInvalidLocaleSignatureException $e) {
  98. $me->add($e);
  99. }
  100.  
  101. if ($me->length()) {
  102. throw $me;
  103. }
  104. }
  105.  
  106. static private function checkLocaleSignature($key_props)
  107. {
  108. $allowedLocales = self::getKeyProp('allowed-locales', $key_props);
  109.  
  110. if ($allowedLocales == 'any') {
  111. return NULL;
  112. }
  113.  
  114. foreach (explode(',', $allowedLocales) as $locale) {
  115. if (!self::_isLocaleSigned($locale)) {
  116. throw new PleskLicenseInvalidLocaleSignatureException(lmsg('plesk_key__invalid_locale_signature'));
  117. }
  118. }
  119. }
  120.  
  121. static private function _isLocaleSigned($locale)
  122. {
  123. static $publicKey = null;
  124.  
  125. if ($publicKey === NULL) {
  126. $publicKey = openssl_get_publickey('-----BEGIN PUBLIC KEY-----' . "\n" . 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCdBbe98A0A421ntILlIQw2U4e' . "\n" . 'IrFYvja5IJ9rJb8vcIGlCw3T9gsBdi+4Cli3zKwcS5gFgjSW+214JtGBRKrSHziK' . "\n" . 'jcg/EDzd7zigpvPY+At6laOOGqyiH27UVjJnvvWuQs8GyZ5OJNYiM9SHQGJOQDz5' . "\n" . 'ynsVXpQG41YeWHhQOQIDAQAB' . "\n" . '-----END PUBLIC KEY-----');
  127. }
  128.  
  129. if (!($handle = opendir(PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale)))) {
  130. return false;
  131. }
  132.  
  133. $ret = false;
  134.  
  135. while (($file = readdir($handle)) !== false) {
  136. $info = $path_parts = pathinfo($file);
  137. if (!array_key_exists('extension', $info) || ($info['extension'] != 'php')) {
  138. continue;
  139. }
  140.  
  141. $signatureFile = PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale . '/' . $file . '.signature');
  142.  
  143. if (!file_exists($signatureFile)) {
  144. return false;
  145. }
  146.  
  147. $signature = file_get_contents($signatureFile);
  148. $data = file_get_contents(PRODUCT_ADMIN_DIR . ('/plib/locales/' . $locale . '/') . $file);
  149. $ret = openssl_verify($data, $signature, $publicKey, OPENSSL_ALGO_SHA1) == 1;
  150.  
  151. if (!$ret) {
  152. break;
  153. }
  154. }
  155.  
  156. closedir($handle);
  157. return $ret;
  158. }
  159.  
  160. static private function checkKeyVersion($key_props)
  161. {
  162. $vers = self::getKeyProp('product_version', $key_props);
  163. $match = false;
  164.  
  165. foreach ($vers as $ver) {
  166. if (!is_array($ver)) {
  167. $m1 = (PLESK_KEY_PRODUCT == 'ppa' ? $ver : strtok($ver, '.'));
  168. $m2 = (PLESK_KEY_PRODUCT == 'ppa' ? '18.0' : strtok('18.0', '.'));
  169. $match |= $m1 == $m2;
  170. }
  171. else {
  172. $match |= (($ver[0] == 'any') || (version_compare($ver[0], '18.0') <= 0)) && (($ver[1] == 'any') || (0 <= version_compare($ver[1], '18.0')));
  173. }
  174. }
  175.  
  176. if (!$match) {
  177. throw new PleskLicenseInvalidVersionException(lmsg('plesk_key__wrong_version'));
  178. }
  179. }
  180.  
  181. static private function checkBackwardRestriction($key_props = false)
  182. {
  183. $backwardRestriction = self::getKeyProp('backward-restriction', $key_props);
  184.  
  185. if (self::_getBackwardRestriction() < $backwardRestriction) {
  186. throw new PleskLicenseBackwardRestrictionException(lmsg('plesk_key__backward_incompatibility'));
  187. }
  188. }
  189.  
  190. static private function checkVirtualizationOnly($key_props = false)
  191. {
  192. $allowed_virtualizations = [];
  193.  
  194. foreach (Virtualization::getVirtualizations() as $env => $env_info) {
  195. if (!empty($env_info['key']) && self::getKeyProp($env_info['key'], $key_props)) {
  196. if (Virtualization::checkEnvironment($env)) {
  197. return NULL;
  198. }
  199. else {
  200. $allowed_virtualizations[] = Virtualization::getEnvironmentName($env);
  201. }
  202. }
  203. }
  204.  
  205. $allowed_virtualizations = array_unique($allowed_virtualizations);
  206.  
  207. if (!count($allowed_virtualizations)) {
  208. return true;
  209. }
  210. else if (count($allowed_virtualizations) == 1) {
  211. throw new PleskLicenseVirtualizationOnlyException(lmsg('plesk_key__virtualization_only_key', $allowed_virtualizations[0]));
  212. }
  213. else {
  214. throw new PleskLicenseVirtualizationOnlyException(lmsg('plesk_key__virtualizations_only_key', implode(', ', $allowed_virtualizations)));
  215. }
  216. }
  217.  
  218. static private function checkKeyExpired($key_props = false)
  219. {
  220. $lim_date = self::getKeyProp('lim_date', $key_props);
  221. if ((0 <= $lim_date) && ($lim_date < date('Ymd'))) {
  222. $buyUrl = Plesk_Config::get()->product->buyUrl;
  223. $link = '<a href=\'' . $buyUrl . '\' target=\'_blank\'>' . lmsg('plesk_key__key_purchase') . '</a>';
  224. throw new PleskLicenseExpiredException(lmsg('plesk_key__key_expired', $link));
  225. }
  226. }
  227.  
  228. static public function checkLimitsExceeded($key_props = false)
  229. {
  230. $me = new PleskLicenseKeyMultipleException(lmsg('plesk_key__limits_exceeded'));
  231.  
  232. if (get_os() == WIN_NT) {
  233. $limits = ['lim_dom', 'lim_cl', 'lim_mn', 'lim_wu'];
  234. }
  235. else {
  236. $limits = ['lim_dom', 'lim_domain_aliases', 'lim_cl', 'lim_mn', 'lim_wu'];
  237. }
  238.  
  239. foreach ($limits as $limit) {
  240. try {
  241. self::checkLimitExceeded($limit, $key_props);
  242. }
  243. catch (PleskLicenseLimitException $e) {
  244. $me->add($e);
  245. }
  246. }
  247.  
  248. if ($me->length()) {
  249. throw $me;
  250. }
  251. }
  252.  
  253. static private function checkLimitExceeded($limit, $key_props = false)
  254. {
  255. $limit_value = self::getKeyProp($limit, $key_props);
  256.  
  257. if ($limit_value < 0) {
  258. return NULL;
  259. }
  260.  
  261. switch ($limit) {
  262. case 'lim_dom':
  263. $used = KeyLimitsChecker::getDomainsUsed();
  264. break;
  265. case 'lim_domain_aliases':
  266. $used = KeyLimitsChecker::getDomainAliasesUsed();
  267. break;
  268. case 'lim_cl':
  269. $used = KeyLimitsChecker::getClientsUsed();
  270. break;
  271. case 'lim_mn':
  272. $used = KeyLimitsChecker::getMailnamesUsed();
  273. break;
  274. case 'lim_wu':
  275. $used = KeyLimitsChecker::getWebusersUsed();
  276. break;
  277. default:
  278. throw new PleskFatalException('Unknown limit name.');
  279. }
  280.  
  281. if ($limit_value < $used) {
  282. throw new PleskLicenseLimitException(lmsg('plesk_key__limits_exceeded_' . $limit, $limit_value, $used, $limit_value - $used));
  283. }
  284. }
  285.  
  286. static public function resetKeyLocks($key_props = false)
  287. {
  288. if ((Session::get()->auth()->getUser()->getType() == Session::IS_ADMIN) && self::getKeyProp('key_askupdate', $key_props)) {
  289. 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);
  290. }
  291. else {
  292. clear_messages(MSG_ANY, 0, 'partner_license_message');
  293. }
  294.  
  295. $license_update_date = self::getKeyProp('license_update_date', $key_props);
  296. $lim_date = self::getKeyProp('lim_date', $key_props);
  297.  
  298. if (Session::get()->auth()->isAdmin()) {
  299. 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()) {
  300. 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);
  301. }
  302. else {
  303. clear_messages(MSG_ANY, 0, 'grace_period_message');
  304. }
  305. }
  306.  
  307. try {
  308. clear_messages(MSG_ANY, 0, 'key_check_wrong');
  309. KeyLimitsChecker::checkKey($key_props);
  310.  
  311. if (get_os() == WIN_NT) {
  312. unset(Session::get()->license()->isWrong);
  313. }
  314. else {
  315. Session::get()->license()->isWrong = false;
  316. }
  317.  
  318. del_param('PleskLicenseExpiredException');
  319. }
  320. catch (PleskLicenseKeyMultipleException $e) {
  321. $alreadyExpired = get_param('PleskLicenseExpiredException') == 'true';
  322. del_param('PleskLicenseExpiredException');
  323.  
  324. if (count($e->getInstancesOf('PleskLicenseExpiredException'))) {
  325. if (!$alreadyExpired) {
  326. try {
  327. updateKeyAndNotify(getPleskKey());
  328. getPleskKey(true);
  329. KeyLimitsChecker::checkKey($key_props);
  330. return NULL;
  331. }
  332. catch (Exception $exception) {
  333. Plesk_Log::debug($exception);
  334. Plesk_Log::err('Auto update of license has failed: ' . $exception->getMessage());
  335. }
  336.  
  337. $key = new Key_Primary($key_props);
  338. $actionLog = new ActionLog('license_expired');
  339. $actionLog->log('COMP_LICENSE', $key->getProp('plesk_key_id'), $key->getProp('plesk_key_id'), ActionLog_Component::IS_REQ);
  340. $actionLog->submit();
  341. }
  342.  
  343. put_param('PleskLicenseExpiredException', 'true');
  344. }
  345. else if (!Plesk_Mode::isLicenseDefault()) {
  346. topnote($e->getMultipleMessage(), 'key_check_wrong', true, MSG_ERROR);
  347. }
  348.  
  349. Session::get()->license()->isWrong = true;
  350. throw $e;
  351. }
  352. }
  353.  
  354. static public function getKeyProp($name, $key_props = false)
  355. {
  356. if (get_os() == WIN_NT) {
  357. if ($key_props === false) {
  358. return get_key_prop($name);
  359. }
  360.  
  361. return isset($key_props[$name]) ? $key_props[$name] : false;
  362. }
  363. else {
  364. if ($key_props === false) {
  365. return getKeyProp($name);
  366. }
  367.  
  368. if (!array_key_exists($name, $key_props)) {
  369. return getKeyDefaultProp($name);
  370. }
  371.  
  372. return $key_props[$name];
  373. }
  374. }
  375.  
  376. /**
  377. * Check locale code and license compatibility
  378. *
  379. * @param string $locale
  380. * @param array|boolean $key_props
  381. * @return boolean
  382. */
  383. static public function isLocaleAllowed($locale, $key_props = false)
  384. {
  385. $allowedLocales = self::getKeyProp('allowed-locales', $key_props);
  386.  
  387. if ($allowedLocales == 'any') {
  388. return true;
  389. }
  390.  
  391. $allowedLocalesArray = explode(',', $allowedLocales);
  392. return in_array($locale, $allowedLocalesArray);
  393. }
  394.  
  395. /**
  396. * Returns number of domains already created
  397. * @return integer
  398. */
  399. static public function getDomainsUsed()
  400. {
  401. $res = db_query('SELECT COUNT(*) FROM domains WHERE parentDomainId = 0');
  402. list($num) = db_fetch_row($res);
  403. return (int) $num;
  404. }
  405.  
  406. /**
  407. * Returns number of subscriptions already created
  408. * @return integer
  409. */
  410. static public function getSubscriptionsUsed()
  411. {
  412. $res = db_query('SELECT COUNT(*) FROM domains WHERE webspace_id = 0');
  413. list($num) = db_fetch_row($res);
  414. return (int) $num;
  415. }
  416.  
  417. static public function getDomainAliasesUsed()
  418. {
  419. $res = db_query('SELECT COUNT(*) FROM domain_aliases');
  420. list($num) = db_fetch_row($res);
  421. return (int) $num;
  422. }
  423.  
  424. /**
  425. * Returns number of clients already created
  426. * @return integer
  427. */
  428. static public function getClientsUsed()
  429. {
  430. $res = db_query_ex('SELECT COUNT(*) FROM clients where type <> :type', ['type' => Client::TYPE_ADMIN]);
  431. list($num) = db_fetch_row($res);
  432. return (int) $num;
  433. }
  434.  
  435. /**
  436. * Returns number of customers already created
  437. * @return integer
  438. */
  439. static public function getCustomersUsed()
  440. {
  441. $res = db_query('SELECT COUNT(*) FROM clients where type = "client"');
  442. list($num) = db_fetch_row($res);
  443. return (int) $num;
  444. }
  445.  
  446. /**
  447. * Returns number of resellers already created
  448. * @return integer
  449. */
  450. static public function getResellersUsed()
  451. {
  452. $res = db_query('SELECT COUNT(*) FROM clients where type = "reseller"');
  453. list($num) = db_fetch_row($res);
  454. return (int) $num;
  455. }
  456.  
  457. /**
  458. * Returns number of mailnames used.
  459. * @return integer
  460. */
  461. static public function getMailnamesUsed()
  462. {
  463. $res = db_query('SELECT COUNT(*) FROM mail');
  464. list($num) = db_fetch_row($res);
  465. return (int) $num;
  466. }
  467.  
  468. /**
  469. * Returns number of webusers used.
  470. * @return integer
  471. */
  472. static public function getWebusersUsed()
  473. {
  474. $res = db_query('SELECT COUNT(*) FROM web_users');
  475. list($num) = db_fetch_row($res);
  476. return (int) $num;
  477. }
  478.  
  479. /**
  480. * Check if mailname creation is allowed by key limits
  481. * @return boolean
  482. */
  483. static public function chkMailnames()
  484. {
  485. $limit = get_key_prop('lim_mn');
  486.  
  487. if ($limit <= 0) {
  488. return true;
  489. }
  490.  
  491. return KeyLimitsChecker::getMailnamesUsed() < $limit;
  492. }
  493.  
  494. /**
  495. * Return list of potential problems with license key according to current usage
  496. *
  497. * @return array
  498. */
  499. static public function getPotentialProblems()
  500. {
  501. $problems = [];
  502. $usedSubscriptions = self::getSubscriptionsUsed();
  503. if (!getKeyProp('can-manage-subscriptions') && (1 < $usedSubscriptions)) {
  504. $problems[] = lmsg('plesk_key__limits_exceeded_no_subscriptions_management', $usedSubscriptions);
  505. }
  506.  
  507. $usedResellers = self::getResellersUsed();
  508. if (!getKeyProp('can-manage-resellers') && (0 < $usedResellers)) {
  509. $problems[] = lmsg('plesk_key__limits_exceeded_no_resellers_management', $usedResellers);
  510. }
  511.  
  512. $usedCustomers = static::getCustomersUsed();
  513. if (!getKeyProp('can-manage-customers') && (0 < $usedCustomers)) {
  514. $problems[] = lmsg('plesk_key__limits_exceeded_no_customers_management', $usedCustomers);
  515. .....................................................................................
  516. ....................................................
  517. ........................
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement