Guest User

Untitled

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