/bitrix/coupon_activation.php

https://bitbucket.org/StasPiv/pharmalad.com · PHP · 530 lines · 300 code · 37 blank · 193 comment · 29 complexity · 56c1249c6ec1919669f7535cc31e007e MD5 · raw file

  1. <?
  2. define("UPDATE_SYSTEM_VERSION", "9.0.2");
  3. error_reporting(E_ALL & ~E_NOTICE);
  4. require_once($_SERVER['DOCUMENT_ROOT']."/bitrix/php_interface/dbconn.php");
  5. require_once($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/main/classes/".$DBType."/database.php");
  6. require_once($_SERVER['DOCUMENT_ROOT']."/bitrix/modules/main/tools.php");
  7. if ($_REQUEST['lang'] == 'ru')
  8. define("LANGUAGE_ID", 'ru');
  9. else
  10. define("LANGUAGE_ID", 'en');
  11. $MESS = array();
  12. if (LANGUAGE_ID == 'ru')
  13. {
  14. $MESS['TITLE'] = 'Восстановление';
  15. $MESS['LOGIN_PROMT'] = 'Логин администратора';
  16. $MESS['PASSWORD_PROMT'] = 'Пароль администратора';
  17. $MESS['COUPON_PROMT'] = 'Лицензионный ключ или купон';
  18. $MESS['BUTTON_SUBMIT'] = 'Восстановить';
  19. $MESS['BUTTON_RESET'] = 'Отменить';
  20. $MESS['ERROR_EMPTY_CONTENT'] = 'Сервер не отвечает';
  21. $MESS['ERROR_INVALID_CONTENT'] = 'Ответ сервера не распознан';
  22. $MESS['ERROR_NOT_ADMIN'] = 'Вы не являетесь администратором';
  23. $MESS['ERROR_INVALID_COUPON'] = 'Лицензионный ключ / купон не корректен';
  24. $MESS['ERROR_EMPTY_COUPON'] = 'Лицензионный ключ / купон не указан';
  25. $MESS['SUCCESS_RECOVER'] = "Работоспособность сайта восстановлена";
  26. $MESS['ERROR_NOT_WRITABLE'] = "Ядро продукта не доступно на запись";
  27. $MESS['ERROR_NOT_FOPEN'] = "Не удалось открыть файл на запись";
  28. }
  29. else
  30. {
  31. $MESS['TITLE'] = 'Restore';
  32. $MESS['LOGIN_PROMT'] = 'Administrator\'s Login';
  33. $MESS['PASSWORD_PROMT'] = 'Administrator\'s Password';
  34. $MESS['COUPON_PROMT'] = 'License Key or Coupon';
  35. $MESS['BUTTON_SUBMIT'] = 'Restore';
  36. $MESS['BUTTON_RESET'] = 'Cancel';
  37. $MESS['ERROR_EMPTY_CONTENT'] = 'Server does not respond.';
  38. $MESS['ERROR_INVALID_CONTENT'] = 'Server response is not recognized';
  39. $MESS['ERROR_NOT_ADMIN'] = 'You are not an administrator';
  40. $MESS['ERROR_INVALID_COUPON'] = 'License Key / Coupon is incorrect';
  41. $MESS['ERROR_EMPTY_COUPON'] = 'License Key / Coupon is not specified';
  42. $MESS['SUCCESS_RECOVER'] = "Site restore completed";
  43. $MESS['ERROR_NOT_WRITABLE'] = "Folder is not writable";
  44. $MESS['ERROR_NOT_FOPEN'] = "File open fails";
  45. }
  46. $DB = new CDatabase;
  47. $DB->debug = $DBDebug;
  48. $DB->Connect($DBHost, $DBName, $DBLogin, $DBPassword);
  49. $errorMessage = "";
  50. $successMessage = "";
  51. /**************************************************************************************************************************/
  52. /************************* FUNCTIONS **********************************************************************************/
  53. /**************************************************************************************************************************/
  54. if (!function_exists("file_get_contents"))
  55. {
  56. function file_get_contents($filename)
  57. {
  58. $fd = fopen("$filename", "rb");
  59. $content = fread($fd, filesize($filename));
  60. fclose($fd);
  61. return $content;
  62. }
  63. }
  64. function UpdateGetOption($name, $default = "")
  65. {
  66. global $DB;
  67. $value = "";
  68. $dbOption = $DB->Query("SELECT VALUE FROM b_option WHERE MODULE_ID='main' AND NAME='".$DB->ForSql($name)."'", true);
  69. if ($arOption = $dbOption->Fetch())
  70. $value = $arOption['VALUE'];
  71. if (strlen($value) <= 0)
  72. $value = $default;
  73. return $value;
  74. }
  75. function UpdateSetOption($name, $value)
  76. {
  77. global $DB, $DBType;
  78. $fn = $_SERVER['DOCUMENT_ROOT']."/bitrix/managed_cache/".strtoupper($DBType)."/e5/".md5("b_option").".php";
  79. @chmod($fn, BX_FILE_PERMISSIONS);
  80. @unlink($fn);
  81. $dbResult = $DB->Query("SELECT 'x' FROM b_option WHERE MODULE_ID='main' AND NAME='".$DB->ForSql($name)."'");
  82. if ($dbResult->Fetch())
  83. {
  84. $DB->Query("UPDATE b_option SET VALUE='".$DB->ForSql($value, 2000)."' WHERE MODULE_ID='main' AND NAME='".$DB->ForSql($name)."'");
  85. }
  86. else
  87. {
  88. $DB->Query(
  89. "INSERT INTO b_option(SITE_ID, MODULE_ID, NAME, VALUE) ".
  90. "VALUES(NULL, 'main', '".$DB->ForSql($name, 50)."', '".$DB->ForSql($value, 2000)."') "
  91. );
  92. }
  93. }
  94. function UpdateGetHTTPPage($requestDataAdd, &$errorMessage)
  95. {
  96. global $DB;
  97. $serverIP = UpdateGetOption("update_site", "www.bitrixsoft.com");
  98. $serverPort = 80;
  99. $proxyAddr = UpdateGetOption("update_site_proxy_addr", "");
  100. $proxyPort = 0;
  101. $proxyUserName = "";
  102. $proxyPassword = "";
  103. if (strlen($proxyAddr) > 0)
  104. {
  105. $proxyPort = intval(UpdateGetOption("update_site_proxy_port", ""));
  106. $proxyUserName = UpdateGetOption("update_site_proxy_user", "");
  107. $proxyPassword = UpdateGetOption("update_site_proxy_pass", "");
  108. }
  109. $bUseProxy = (strlen($proxyAddr) > 0 && $proxyPort > 0);
  110. if ($bUseProxy)
  111. {
  112. $requestIP = $proxyAddr;
  113. $requestPort = $proxyPort;
  114. }
  115. else
  116. {
  117. $requestIP = $serverIP;
  118. $requestPort = $serverPort;
  119. }
  120. $FP = fsockopen($requestIP, $requestPort, $errno, $errstr, 120);
  121. if ($FP)
  122. {
  123. $LICENSE_KEY = "demo";
  124. if (file_exists($_SERVER["DOCUMENT_ROOT"]."/bitrix/license_key.php"))
  125. include($_SERVER["DOCUMENT_ROOT"]."/bitrix/license_key.php");
  126. $dbv = $DB->GetVersion();
  127. $usrCnt = 0;
  128. $q = "SELECT COUNT(U.ID) as C FROM b_user U WHERE U.ACTIVE = 'Y' AND U.LAST_LOGIN IS NOT NULL AND EXISTS(SELECT 'x' FROM b_utm_user UF, b_user_field F WHERE F.ENTITY_ID = 'USER' AND F.FIELD_NAME = 'UF_DEPARTMENT' AND UF.FIELD_ID = F.ID AND UF.VALUE_ID = U.ID AND UF.VALUE_INT IS NOT NULL AND UF.VALUE_INT <> 0)";
  129. $dbRes = $DB->Query($q, true);
  130. if ($dbRes && ($arRes = $dbRes->Fetch()))
  131. $usrCnt = $arRes["C"];
  132. $requestData =
  133. "&LICENSE_KEY=".urlencode(md5($LICENSE_KEY)).
  134. "&lang=".urlencode(LANGUAGE_ID).
  135. "&utf=".urlencode(defined('BX_UTF') ? "Y" : "N").
  136. "&stable=".urlencode(UpdateGetOption("stable_versions_only", "Y")).
  137. "&CANGZIP=".urlencode(function_exists("gzcompress") ? "Y" : "N").
  138. "&SUPD_STS=".urlencode("RA").
  139. "&SUPD_DBS=".urlencode($DB->type).
  140. "&XE=".urlencode(($DB->XE) ? "Y" : "N").
  141. "&SUPD_URS=".urlencode($usrCnt).
  142. "&CLIENT_SITE=".urlencode($_SERVER["SERVER_NAME"]).
  143. "&spd=".urlencode(UpdateGetOption("crc_code", "")).
  144. "&dbv=".urlencode($dbv != false ? $dbv : "").
  145. "&SUPD_VER=".urlencode(UPDATE_SYSTEM_VERSION);
  146. if (strlen($requestDataAdd) > 0)
  147. $requestData .= "&".$requestDataAdd;
  148. $requestString = "";
  149. if ($bUseProxy)
  150. {
  151. $requestString .= "POST http://".$serverIP."/bitrix/updates/us_updater_actions.php HTTP/1.0\r\n";
  152. if (strlen($proxyUserName) > 0)
  153. $requestString .= "Proxy-Authorization: Basic ".base64_encode($proxyUserName.":".$proxyPassword)."\r\n";
  154. }
  155. else
  156. $requestString .= "POST /bitrix/updates/us_updater_actions.php HTTP/1.0\r\n";
  157. $requestString .= "User-Agent: BitrixSMUpdater\r\n";
  158. $requestString .= "Accept: */*\r\n";
  159. $requestString .= "Host: ".$serverIP."\r\n";
  160. $requestString .= "Accept-Language: en\r\n";
  161. $requestString .= "Content-type: application/x-www-form-urlencoded\r\n";
  162. $requestString .= "Content-length: ".strlen($requestData)."\r\n\r\n";
  163. $requestString .= "$requestData";
  164. $requestString .= "\r\n";
  165. fputs($FP, $requestString);
  166. while (!feof($FP))
  167. {
  168. $line = fgets($FP, 4096);
  169. if ($line == "\r\n")
  170. break;
  171. }
  172. $content = "";
  173. while ($line = fread($FP, 4096))
  174. $content .= $line;
  175. fclose($FP);
  176. }
  177. else
  178. {
  179. $content = "";
  180. $errorMessage .= "[".$errno."] ".$errstr.". ";
  181. }
  182. return $content;
  183. }
  184. function UpdateHtmlSpecialCharsBack($str)
  185. {
  186. if (strlen($str) > 0)
  187. {
  188. $str = str_replace("&lt;", "<", $str);
  189. $str = str_replace("&gt;", ">", $str);
  190. $str = str_replace("&quot;", "\"", $str);
  191. $str = str_replace("&amp;", "&", $str);
  192. }
  193. return $str;
  194. }
  195. function UpdateParseServerData($content, &$errorMessage)
  196. {
  197. global $MESS;
  198. $arContent = array();
  199. if (substr($content, 0, strlen("<DATA>")) != "<DATA>" && function_exists("gzcompress"))
  200. $content = @gzuncompress($content);
  201. if (substr($content, 0, strlen("<DATA>")) != "<DATA>")
  202. return false;
  203. if (preg_match_all('#<ERROR[^>]*>(.+?)</ERROR>#is', $content, $arMatches))
  204. {
  205. for ($i = 0, $cnt = count($arMatches[1]); $i < $cnt; $i++)
  206. $errorMessage .= UpdateHtmlSpecialCharsBack($arMatches[1][$i]).". ";
  207. return false;
  208. }
  209. if (preg_match('#<RENT\s+([^>]*)/>#i', $content, $arMatches))
  210. {
  211. if (preg_match_all("/(\\S+?)\\s*=\\s*[\"](.*?)[\"]/s", $arMatches[1], $arMatches1))
  212. {
  213. for ($i = 0, $cnt = count($arMatches1[1]); $i < $cnt; $i++)
  214. $arContent[$arMatches1[1][$i]] .= $arMatches1[2][$i];
  215. }
  216. }
  217. if (isset($arContent["V1"]) && isset($arContent["V2"]))
  218. return $arContent;
  219. return false;
  220. }
  221. function UpdateActivateCoupon($coupon, &$errorMessage)
  222. {
  223. global $MESS;
  224. $postDataString = "coupon=".urlencode($coupon)."&query_type=".urlencode("reincarnate");
  225. $content = UpdateGetHTTPPage($postDataString, $errorMessage);
  226. if (strlen($content) <= 0)
  227. {
  228. $errorMessage .= $MESS['ERROR_EMPTY_CONTENT'].". ";
  229. return false;
  230. }
  231. $arContent = UpdateParseServerData($content, $errorMessage);
  232. if (!is_array($arContent) || count($arContent) <= 0)
  233. {
  234. if (strlen($errorMessage) <= 0)
  235. $errorMessage .= $MESS['ERROR_INVALID_CONTENT'].". ";
  236. return false;
  237. }
  238. UpdateSetOption('~SAAS_MODE', "Y");
  239. UpdateSetOption('admin_passwordh', $arContent["V1"]);
  240. if (is_writable($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/admin/define.php"))
  241. {
  242. if ($fp = fopen($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/admin/define.php", 'w'))
  243. {
  244. fwrite($fp, "<"."?Define(\"TEMPORARY_CACHE\", \"".$arContent["V2"]."\");?".">");
  245. fclose($fp);
  246. }
  247. else
  248. {
  249. $errorMessage .= $MESS['ERROR_NOT_FOPEN'].". ";
  250. }
  251. }
  252. else
  253. {
  254. $errorMessage .= $MESS['ERROR_NOT_WRITABLE'].". ";
  255. }
  256. if (isset($arContent["DATE_TO_SOURCE"]))
  257. UpdateSetOption("~support_finish_date", $arContent["DATE_TO_SOURCE"]);
  258. if (isset($arContent["MAX_SITES"]))
  259. UpdateSetOption("PARAM_MAX_SITES", intval($arContent["MAX_SITES"]));
  260. if (isset($arContent["MAX_USERS"]))
  261. UpdateSetOption("PARAM_MAX_USERS", intval($arContent["MAX_USERS"]));
  262. if (isset($arContent["ISLC"]))
  263. {
  264. if (is_writable($_SERVER['DOCUMENT_ROOT']."/bitrix/license_key.php"))
  265. {
  266. if ($fp = fopen($_SERVER['DOCUMENT_ROOT']."/bitrix/license_key.php", "wb"))
  267. {
  268. fputs($fp, '<'.'?$LICENSE_KEY = "'.EscapePHPString($coupon).'";?'.'>');
  269. fclose($fp);
  270. }
  271. else
  272. {
  273. $errorMessage .= $MESS['ERROR_NOT_FOPEN'].". ";
  274. }
  275. }
  276. else
  277. {
  278. $errorMessage .= $MESS['ERROR_NOT_WRITABLE'].". ";
  279. }
  280. }
  281. return true;
  282. }
  283. function UpdateIsAdmin($login, $password)
  284. {
  285. global $DB;
  286. if (strlen($login) <= 0 || strlen($password) <= 0)
  287. return false;
  288. $dbUser = $DB->Query(
  289. "SELECT U.ID, U.PASSWORD ".
  290. "FROM b_user U ".
  291. " INNER JOIN b_user_group UG ON (UG.USER_ID = U.ID) ".
  292. "WHERE U.LOGIN = '".$DB->ForSql($login)."' ".
  293. " AND (U.EXTERNAL_AUTH_ID IS NULL OR U.EXTERNAL_AUTH_ID = '') ".
  294. " AND U.ACTIVE = 'Y' ".
  295. " AND UG.GROUP_ID = 1 ".
  296. " AND ((UG.DATE_ACTIVE_FROM IS NULL) OR (UG.DATE_ACTIVE_FROM <= ".$DB->CurrentTimeFunction().")) ".
  297. " AND ((UG.DATE_ACTIVE_TO IS NULL) OR (UG.DATE_ACTIVE_TO >= ".$DB->CurrentTimeFunction().")) "
  298. );
  299. if ($arUser = $dbUser->Fetch())
  300. {
  301. if (strlen($arUser["PASSWORD"]) > 32)
  302. {
  303. $salt = substr($arUser["PASSWORD"], 0, strlen($arUser["PASSWORD"]) - 32);
  304. $db_password = substr($arUser["PASSWORD"], -32);
  305. }
  306. else
  307. {
  308. $salt = "";
  309. $db_password = $arUser["PASSWORD"];
  310. }
  311. $user_password = md5($salt.$password);
  312. return ($db_password === $user_password);
  313. }
  314. return false;
  315. }
  316. /**************************************************************************************************************************/
  317. /**************************************************************************************************************************/
  318. /**************************************************************************************************************************/
  319. header("Content-Type: text/html; charset=windows-1251");
  320. if ($_SERVER["REQUEST_METHOD"] == "POST")
  321. {
  322. if (strlen($_POST["autoActivateCoupon"]) > 0)
  323. {
  324. $autoActivateCoupon = $_POST["autoActivateCoupon"];
  325. if (preg_match("#^[A-Z0-9]{3}-[A-Z0-9]{10}-[A-Z0-9]{10}$#i", $autoActivateCoupon))
  326. {
  327. if (UpdateActivateCoupon($autoActivateCoupon, $errorMessage))
  328. echo "success";
  329. else
  330. echo $errorMessage;
  331. }
  332. else
  333. {
  334. echo "error";
  335. }
  336. die();
  337. }
  338. if (strlen($_POST["reincarnate"]) > 0)
  339. {
  340. if (strlen($_POST["coupon"]) <= 0)
  341. {
  342. $errorMessage .= $MESS['ERROR_EMPTY_COUPON'].". ";
  343. }
  344. elseif (!preg_match("#^[A-Z0-9]{3}-[A-Z]{2}-?[A-Z0-9]{12,18}$#i", $_POST["coupon"]) && !preg_match("#^[A-Z0-9]{3}-[A-Z0-9]{10}-[A-Z0-9]{10}$#i", $_POST["coupon"]))
  345. {
  346. $errorMessage .= $MESS['ERROR_INVALID_COUPON'].". ";
  347. }
  348. elseif (!UpdateIsAdmin($_POST["login"], $_POST["password"]))
  349. {
  350. $errorMessage .= $MESS['ERROR_NOT_ADMIN'].". ";
  351. }
  352. else
  353. {
  354. if (UpdateActivateCoupon($_POST["coupon"], $errorMessage))
  355. $successMessage .= $MESS['SUCCESS_RECOVER'].". ";
  356. }
  357. }
  358. }
  359. ?>
  360. <html>
  361. <head>
  362. <title><?= $MESS['TITLE'] ?></title>
  363. <link rel="stylesheet" type="text/css" href="/bitrix/themes/.default/sysupdate.css">
  364. <link rel="stylesheet" type="text/css" href="/bitrix/themes/.default/adminstyles.css">
  365. </head>
  366. <body>
  367. <?
  368. if (strlen($errorMessage) > 0)
  369. {
  370. ?><br>
  371. <table width="600" align="center" cellspacing="1" cellpadding="10" bgcolor="red"><tr><td bgcolor="white">
  372. <font style="color:red"><b><?= $errorMessage ?></b></font>
  373. </td></tr></table>
  374. <br>
  375. <?
  376. }
  377. if (strlen($successMessage) > 0)
  378. {
  379. ?><br>
  380. <table width="600" align="center" cellspacing="1" cellpadding="10" bgcolor="green"><tr><td bgcolor="white">
  381. <font style="color:green"><b><?= $successMessage ?></b></font>
  382. </td></tr></table>
  383. <br>
  384. <?
  385. }
  386. ?>
  387. <form method="POST" action="/bitrix/coupon_activation.php">
  388. <input type="hidden" name="lang" value="<?= htmlspecialchars(LANGUAGE_ID) ?>" />
  389. <table width="100%" cellspacing="0" cellpadding="0" border="0">
  390. <tr>
  391. <td width="20%">
  392. </td>
  393. <td width="60%">
  394. <br />
  395. <div class="edit-form">
  396. <table cellpadding="0" cellspacing="0" border="0" width="100%" class="edit-form">
  397. <tr class="top">
  398. <td class="left"><div class="empty"></div></td>
  399. <td><div class="empty"></div></td>
  400. <td class="right"><div class="empty"></div></td>
  401. </tr>
  402. <tr>
  403. <td class="left"><div class="empty"></div></td>
  404. <td class="content">
  405. <table cellspacing="0" class="edit-tabs" width="100%">
  406. <tr>
  407. <td class="tab-indent"><div class="empty"></div></td>
  408. <td class="tab-container-selected">
  409. <table cellspacing="0">
  410. <tr>
  411. <td class="tab-left-selected" id="tab_left_edit1"><div class="empty"></div></td>
  412. <td class="tab-selected" id="tab_edit1"><?= $MESS['TITLE'] ?></td>
  413. <td class="tab-right-selected" id="tab_right_edit1"><div class="empty"></div></td>
  414. </tr>
  415. </table>
  416. </td>
  417. <td width="100%" class="tab-indent"><div class="empty"></div></td>
  418. </tr>
  419. </table>
  420. <table cellspacing="0" class="edit-tab">
  421. <tr>
  422. <td>
  423. <div id="edit1" class="edit-tab-inner"><div style="height: 100%;">
  424. <table cellpadding="0" cellspacing="0" border="0" class="edit-table">
  425. <tr>
  426. <td width="40%" align="right"><?= $MESS['LOGIN_PROMT'] ?>:</td>
  427. <td><input type="text" name="login" value="<?= htmlspecialchars($_POST["login"]) ?>" size="40"></td>
  428. </tr>
  429. <tr>
  430. <td width="40%" align="right"><?= $MESS['PASSWORD_PROMT'] ?>:</td>
  431. <td><input type="password" name="password" value="" size="40"></td>
  432. </tr>
  433. <tr>
  434. <td width="40%" align="right"><?= $MESS['COUPON_PROMT'] ?>:</td>
  435. <td><input type="text" name="coupon" value="<?= htmlspecialchars($_POST["coupon"]) ?>" size="40"></td>
  436. </tr>
  437. </table>
  438. </div></div>
  439. </td>
  440. </tr>
  441. </table>
  442. <div class="buttons">
  443. <input type="submit" class="button" name="reincarnate" value="<?= $MESS['BUTTON_SUBMIT'] ?>">
  444. <input type="reset" class="button" name="reset" value="<?= $MESS['BUTTON_RESET'] ?>">
  445. </div>
  446. </td>
  447. <td class="left"><div class="empty"></div></td>
  448. </tr>
  449. <tr class="bottom">
  450. <td class="left"><div class="empty"></div></td>
  451. <td><div class="empty"></div></td>
  452. <td class="right"><div class="empty"></div></td>
  453. </tr>
  454. </table>
  455. </div>
  456. </td>
  457. <td width="20%">
  458. </td>
  459. </tr>
  460. </table>
  461. </form>
  462. </body>
  463. </html>