PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/security/admin/security_iprule_admin.php

https://gitlab.com/alexprowars/bitrix
PHP | 284 lines | 102 code | 13 blank | 169 comment | 16 complexity | 369b07b960b95fe5dc8ef624e2cf45de MD5 | raw file
  1. <?
  2. define("ADMIN_MODULE_NAME", "security");
  3. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_before.php");
  4. CModule::IncludeModule('security');
  5. IncludeModuleLangFile(__FILE__);
  6. /**
  7. * @global CUser $USER
  8. * @global CMain $APPLICATION
  9. **/
  10. $canRead = $USER->CanDoOperation('security_iprule_admin_settings_read');
  11. $canWrite = $USER->CanDoOperation('security_iprule_admin_settings_write');
  12. if(!$canRead && !$canWrite)
  13. $APPLICATION->AuthForm(GetMessage("ACCESS_DENIED"));
  14. $aTabs = array(
  15. array(
  16. "DIV" => "main",
  17. "TAB" => GetMessage("SEC_IPRULE_ADMIN_MAIN_TAB"),
  18. "ICON"=>"main_user_edit",
  19. "TITLE"=>GetMessage("SEC_IPRULE_ADMIN_MAIN_TAB_TITLE"),
  20. ),
  21. );
  22. $tabControl = new CAdminTabControl("tabControl", $aTabs, false, true);
  23. $rsIPRule = CSecurityIPRule::GetList(array(), array(
  24. "=RULE_TYPE" => "A",
  25. "=ADMIN_SECTION" => "Y",
  26. "=SITE_ID" => false,
  27. "=SORT" => 10,
  28. "=ACTIVE_FROM" => false,
  29. "=ACTIVE_TO" => false,
  30. ), array("ID" => "ASC"));
  31. $arIPRule = $rsIPRule->Fetch();
  32. if($arIPRule)
  33. {
  34. $ID = $arIPRule["ID"];
  35. $ACTIVE = $arIPRule["ACTIVE"];
  36. }
  37. else
  38. {
  39. $ID = 0;
  40. $ACTIVE = "N";
  41. }
  42. $exclMasks=array(
  43. '/bitrix/admin/user_options.php',
  44. );
  45. foreach(GetModuleEvents("security", "OnIPRuleAdmin", true) as $event)
  46. {
  47. $exclMasks = array_merge($exclMasks,ExecuteModuleEventEx($event));
  48. }
  49. $strError = "";
  50. $bVarsFromForm = false;
  51. $bShowForce = false;
  52. $message = CSecurityIPRule::CheckAntiFile(true);
  53. if($_SERVER["REQUEST_METHOD"] == "POST" && $_REQUEST["save"].$_REQUEST["apply"].$_REQUEST["activate_iprule"].$_REQUEST["deactivate_iprule"] !="" && $canWrite && check_bitrix_sessid())
  54. {
  55. $ob = new CSecurityIPRule;
  56. if(!$_REQUEST["activate_iprule"] && $_REQUEST["deactivate_iprule"])
  57. {
  58. //When rule is going to be deactivated we will no check for IP
  59. $noExclIPS = false;
  60. $selfBlock = false;
  61. }
  62. else
  63. {
  64. //Otherwise check if ANY input supplied
  65. $noExclIPS = true;
  66. foreach($_POST["EXCL_IPS"] as $ip)
  67. {
  68. if(trim($ip) <> '')
  69. {
  70. $noExclIPS = false;
  71. break;
  72. }
  73. }
  74. //AND it is not selfblocking rule
  75. $INCL_IPS = array("0.0.0.1-255.255.255.255");
  76. $selfBlock = $ob->CheckIP($INCL_IPS, $_POST["EXCL_IPS"]);
  77. }
  78. if($noExclIPS)
  79. {
  80. $message = new CAdminMessage(GetMessage("SEC_IPRULE_ADMIN_NO_IP"));
  81. $bVarsFromForm = true;
  82. }
  83. elseif($selfBlock && (COption::GetOptionString("security", "ipcheck_allow_self_block")!=="Y"))
  84. {
  85. if($e = $APPLICATION->GetException())
  86. $message = new CAdminMessage(GetMessage("SEC_IPRULE_ADMIN_SAVE_ERROR"), $e);
  87. $bVarsFromForm = true;
  88. }
  89. elseif($selfBlock && $_POST["USE_THE_FORCE_LUK"]!=="Y")
  90. {
  91. if($e = $APPLICATION->GetException())
  92. $message = new CAdminMessage(GetMessage("SEC_IPRULE_ADMIN_SAVE_ERROR"), $e);
  93. $bVarsFromForm = true;
  94. $bShowForce = true;
  95. }
  96. else
  97. {
  98. $arFields = array(
  99. "RULE_TYPE" => "A",
  100. "ACTIVE" => $_REQUEST["activate_iprule"]? "Y": ($_REQUEST["deactivate_iprule"]? "N": $ACTIVE),
  101. "ADMIN_SECTION" => "Y",
  102. "SITE_ID" => false,
  103. "SORT" => 10,
  104. "NAME" => GetMessage("SEC_IPRULE_ADMIN_RULE_NAME"),
  105. "ACTIVE_FROM" => false,
  106. "ACTIVE_TO" => false,
  107. "INCL_IPS" => $INCL_IPS,
  108. "EXCL_IPS" => $_POST["EXCL_IPS"],
  109. "INCL_MASKS" => array("/bitrix/admin/*"),
  110. "EXCL_MASKS" => $exclMasks,
  111. );
  112. if($ID > 0)
  113. {
  114. $res = $ob->Update($ID, $arFields);
  115. }
  116. else
  117. {
  118. $ID = $ob->Add($arFields);
  119. $res = ($ID>0);
  120. }
  121. if($res)
  122. {
  123. if($_REQUEST["save"] != "" && $_GET["return_url"]!="")
  124. LocalRedirect($_GET["return_url"]);
  125. $returnUrl = $_GET["return_url"]? "&return_url=".urlencode($_GET["return_url"]): "";
  126. LocalRedirect("/bitrix/admin/security_iprule_admin.php?lang=".LANGUAGE_ID.$returnUrl."&".$tabControl->ActiveTabParam());
  127. }
  128. else
  129. {
  130. if($e = $APPLICATION->GetException())
  131. $message = new CAdminMessage(GetMessage("SEC_IPRULE_ADMIN_SAVE_ERROR"), $e);
  132. $bVarsFromForm = true;
  133. }
  134. }
  135. }
  136. $messageDetails = "";
  137. if ($ID > 0 && $ACTIVE=="Y")
  138. {
  139. $messageType = "OK";
  140. $messageText = GetMessage("SEC_IPRULE_ADMIN_ON");
  141. } else
  142. {
  143. $messageType = "ERROR";
  144. $messageText = GetMessage("SEC_IPRULE_ADMIN_OFF");
  145. }
  146. $APPLICATION->SetTitle(GetMessage("SEC_IPRULE_ADMIN_TITLE"));
  147. CUtil::InitJSCore();
  148. $APPLICATION->AddHeadScript('/bitrix/js/security/admin/interface.js');
  149. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_admin_after.php");
  150. if($message)
  151. echo $message->Show();
  152. CAdminMessage::ShowMessage(array(
  153. "MESSAGE"=>$messageText,
  154. "TYPE"=>$messageType,
  155. "DETAILS"=>$messageDetails,
  156. "HTML"=>true
  157. ));
  158. ?>
  159. <form method="POST" action="security_iprule_admin.php?lang=<?echo LANGUAGE_ID?><?echo $_GET["return_url"]? "&amp;return_url=".urlencode($_GET["return_url"]): ""?>" enctype="multipart/form-data" name="editform">
  160. <?
  161. $tabControl->Begin();
  162. ?>
  163. <?
  164. $tabControl->BeginNextTab();
  165. ?>
  166. <tr>
  167. <td colspan="2" align="left">
  168. <?if($ID > 0 && $ACTIVE=="Y"):?>
  169. <input type="submit" name="deactivate_iprule" value="<?echo GetMessage("SEC_IPRULE_ADMIN_BUTTON_OFF")?>"<?if(!$canWrite) echo " disabled"?>>
  170. <?else:?>
  171. <input type="submit" name="activate_iprule" value="<?echo GetMessage("SEC_IPRULE_ADMIN_BUTTON_ON")?>"<?if(!$canWrite) echo " disabled"?> class="adm-btn-save">
  172. <?endif?>
  173. </td>
  174. </tr>
  175. <tr>
  176. <td colspan="2">
  177. <?echo BeginNote();?><?echo GetMessage("SEC_IPRULE_ADMIN_NOTE", array("#IP#" => $_SERVER["REMOTE_ADDR"]))?>
  178. <?echo EndNote(); ?>
  179. </td>
  180. </tr>
  181. <?
  182. $arExclIPs = array();
  183. if($bVarsFromForm)
  184. {
  185. if(is_array($_POST["EXCL_IPS"]))
  186. foreach($_POST["EXCL_IPS"] as $i => $ip)
  187. $arExclIPs[] = htmlspecialcharsbx($ip);
  188. }
  189. elseif($ID > 0)
  190. {
  191. $ar = CSecurityIPRule::GetRuleExclIPs($ID);
  192. foreach($ar as $i => $ip)
  193. $arExclIPs[] = htmlspecialcharsbx($ip);
  194. }
  195. ?>
  196. <tr>
  197. <td class="adm-detail-valign-top" width="40%"><?echo GetMessage("SEC_IPRULE_ADMIN_EXCL_IPS")?>:<br><?echo GetMessage("SEC_IPRULE_ADMIN_EXCL_IPS_SAMPLE")?></td>
  198. <td width="60%">
  199. <table cellpadding="0" cellspacing="0" border="0" class="nopadding" width="100%" id="tbEXCL_IPS">
  200. <?foreach($arExclIPs as $i => $ip):?>
  201. <tr><td nowrap style="padding-bottom: 3px;">
  202. <input type="text" size="45" name="EXCL_IPS[<?echo $i?>]" value="<?echo $ip?>">
  203. </td></tr>
  204. <?endforeach;?>
  205. <?if(!$bVarsFromForm):?>
  206. <tr class="security-addable-row"><td nowrap style="padding-bottom: 3px;">
  207. <input type="text" size="45" name="EXCL_IPS[n0]" value="">
  208. </td></tr>
  209. <?endif;?>
  210. <tr><td>
  211. <br><input type="button" id="add-button" value="<?echo GetMessage("SEC_IPRULE_ADMIN_ADD")?>">
  212. </td></tr>
  213. </table>
  214. </td>
  215. </tr>
  216. <?
  217. if (count($exclMasks) > 0)
  218. {
  219. ?>
  220. <tr>
  221. <td class="adm-detail-valign-top" width="40%"><?echo GetMessage("SEC_IPRULE_ADMIN_EXCL_FILES_".(($ACTIVE == 'Y')?'ACTIVE':'INACTIVE'))?></td>
  222. <td width="60%">
  223. <table cellpadding="0" cellspacing="0" border="0" class="nopadding" width="100%" id="tbEXCL_FILES">
  224. <?foreach($exclMasks as $mask):?>
  225. <tr><td nowrap>
  226. <?echo htmlspecialcharsbx($mask)?>
  227. </td></tr>
  228. <?endforeach;?>
  229. </table>
  230. </td>
  231. </tr>
  232. <?
  233. }
  234. ?>
  235. <script id="security-interface-settings" type="application/json">
  236. {
  237. "addableRows": [{
  238. "tableId": "tbEXCL_IPS",
  239. "buttonId": "add-button"
  240. }]
  241. }
  242. </script>
  243. <?
  244. $tabControl->Buttons(
  245. array(
  246. "disabled"=>(!$canWrite),
  247. "back_url"=>$_GET["return_url"]? $_GET["return_url"]: "security_iprule_admin.php?lang=".LANG,
  248. )
  249. );
  250. ?>
  251. <?echo bitrix_sessid_post();?>
  252. <input type="hidden" name="lang" value="<?echo LANG?>">
  253. <?if($bShowForce && (COption::GetOptionString("security", "ipcheck_allow_self_block")==="Y")):?>
  254. <input type="hidden" name="USE_THE_FORCE_LUK" value="Y">
  255. <?endif;?>
  256. <?
  257. $tabControl->End();
  258. ?>
  259. </form>
  260. <?
  261. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/epilog_admin.php");
  262. ?>