BxDolStudioIpBlockList.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @package Dolphin Core
  4. * @copyright Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  5. * @license CC-BY - http://creativecommons.org/licenses/by/3.0/
  6. */
  7. defined('BX_DOL') or die('hack attempt');
  8. // TODO: move HTML to templates
  9. // TODO: move SQL queries to separate class
  10. // TODO: maybe it makes sence to move it to separate module ?
  11. class BxDolAdminIpBlockList extends BxDol {
  12. var $_oDb;
  13. var $_sActionUrl;
  14. /**
  15. * constructor
  16. */
  17. function BxDolAdminIpBlockList($sActionUrl = '') {
  18. parent::BxDol();
  19. $this->_oDb = BxDolDb::getInstance();
  20. $this->_sActionUrl = !empty($sActionUrl) ? $sActionUrl : $_SERVER['PHP_SELF'] . (!empty($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '');
  21. }
  22. function GenStoredMemIPs() {
  23. $sFromC = _t('_From');
  24. $sMemberC = _t('_Member');
  25. $sDatatimeC = _t('_Date');
  26. $sCaptionC = _t('_adm_ipbl_Stored_members_caption');
  27. $sRes = '<br /><h2>'.$sCaptionC.'</h2>';
  28. $sTableRes .= <<<EOF
  29. <table style="width:99%;" border="1" cellpadding="2" cellspacing="1" style="border-collapse: collapse">
  30. <tr>
  31. <td>{$sFromC}</td>
  32. <td>{$sMemberC}</td>
  33. <td>{$sDatatimeC}</td>
  34. </tr>
  35. EOF;
  36. $sCntSQL = "SELECT COUNT(*) FROM `sys_ip_members_visits`";
  37. ////////////////////////////
  38. $iTotalNum = db_value( $sCntSQL );
  39. if( !$iTotalNum ) {
  40. return $sRes . MsgBox(_t('_Empty'));
  41. }
  42. $iPerPage = (int)$_GET['per_page'];
  43. if( !$iPerPage )
  44. $iPerPage = 10;
  45. $iCurPage = (int)$_GET['page'];
  46. if( $iCurPage < 1 )
  47. $iCurPage = 1;
  48. $sLimitFrom = ( $iCurPage - 1 ) * $iPerPage;
  49. $sqlLimit = "LIMIT {$sLimitFrom}, {$iPerPage}";
  50. ////////////////////////////
  51. $sSQL = "SELECT * FROM `sys_ip_members_visits` ORDER BY `DateTime` DESC {$sqlLimit}";
  52. $aIPsList = BxDolDb::getInstance()->getAll($sSQL);
  53. foreach ($aIPsList as $aIPList) {
  54. $iID = (int)$aIPList['ID'];
  55. $sFrom = long2ip($aIPList['From']);
  56. $sLastDT = $aIPList['DateTime'];
  57. $sMember = $aIPList['MemberID'] ? '<a href="' . getProfileLink($aIPList['MemberID']) . '">' . bx_process_output(getNickname($aIPList['MemberID'])) . '</a>' : '';
  58. $sTableRes .= "<tr><td>{$sFrom}</td><td>{$sMember}</td><td>{$sLastDT}</td></tr>";
  59. }
  60. $sTableRes .= <<<EOF
  61. </table>
  62. <div class="clear_both"></div>
  63. <br />
  64. EOF;
  65. $sRequest = BX_DOL_URL_STUDIO . 'ip_blacklist.php?page={page}&per_page={per_page}';
  66. $oPaginate = new BxDolPaginate (
  67. array
  68. (
  69. 'page_url' => $sRequest,
  70. 'count' => $iTotalNum,
  71. 'per_page' => $iPerPage,
  72. 'page' => $iCurPage,
  73. 'per_page_changer' => true,
  74. 'page_reloader' => true,
  75. 'on_change_page' => null,
  76. 'on_change_per_page' => null,
  77. )
  78. );
  79. return $sRes . $sTableRes . $oPaginate -> getPaginate();
  80. }
  81. function GenIPBlackListTable() {
  82. $sSitePluginsUrl = BX_DOL_URL_PLUGINS;
  83. $sFromC = _t('_From');
  84. $sToC = _t('_To');
  85. $sTypeC = _t('_adm_ipbl_IP_Role');
  86. $sDescriptionC = _t('_Description');
  87. $sDatatimeC = _t('_adm_ipbl_Date_of_finish');
  88. $sActionC = _t('_Action');
  89. $sEditC = _t('_Edit');
  90. $sDeleteC = _t('_Delete');
  91. $sType0C = _t('_adm_ipbl_Type0_desc');
  92. $sType1C = _t('_adm_ipbl_Type1_desc');
  93. $sType2C = _t('_adm_ipbl_Type2_desc');
  94. $sTitle = '';
  95. $iIpListType = (int)getParam('bx_antispam_ip_list_type');
  96. switch ($iIpListType) {
  97. case 1:
  98. $sTitle = '<h2>'.$sType1C.'</h2>';
  99. break;
  100. case 2:
  101. $sTitle = '<h2>'.$sType2C.'</h2>';
  102. break;
  103. case 0:
  104. default:
  105. $sTitle = '<h2>'.$sType0C.'</h2>';
  106. break;
  107. }
  108. $sRows = '';
  109. $aIPsList = BxDolDb::getInstance()->getALl("SELECT *, FROM_UNIXTIME(`LastDT`) AS `LastDT_U` FROM `sys_ip_list` ORDER BY `From` ASC");
  110. foreach ($aIPsList as $aIPList) {
  111. $iID = (int)$aIPList['ID'];
  112. $sFrom = long2ip($aIPList['From']);
  113. $sTo = ($aIPList['To'] == 0) ? '' : long2ip($aIPList['To']);
  114. $sType = bx_js_string($aIPList['Type']);
  115. $sLastDT = $aIPList['LastDT_U'];
  116. $sDesc = bx_js_string($aIPList['Desc']);
  117. $sActionUrl = bx_html_attribute($this->_sActionUrl);
  118. $sRows .= <<<EOF
  119. <tr>
  120. <td>{$sFrom}</td>
  121. <td>{$sTo}</td>
  122. <td>{$sType}</td>
  123. <td>{$sLastDT}</td>
  124. <td>{$sDesc}</td>
  125. <td>
  126. <a href="javascript:void(0)" onclick="ip_accept_values_to_form('{$iID}', '{$sFrom}', '{$sTo}', '{$sType}', '{$sLastDT}', '{$sDesc}'); return false;">{$sEditC}</a>
  127. |
  128. <a href="$sActionUrl?action=apply_delete&id={$iID}">{$sDeleteC}</a>
  129. </td>
  130. </tr>
  131. EOF;
  132. }
  133. return <<<EOF
  134. {$sTitle}
  135. <table style="width:99%;" border="1" cellpadding="2" cellspacing="1" style="border-collapse: collapse">
  136. <tr>
  137. <td>{$sFromC}</td>
  138. <td>{$sToC}</td>
  139. <td>{$sTypeC}</td>
  140. <td>{$sDatatimeC}</td>
  141. <td>{$sDescriptionC}</td>
  142. <td>{$sActionC}</td>
  143. </tr>
  144. {$sRows}
  145. </table>
  146. <div class="clear_both"></div>
  147. <script type="text/javascript">
  148. function ip_accept_values_to_form(id_val, from_val, to_val, type_val, lastdt_val, desc_val) {
  149. $('.form_input_hidden[name="id"]').val(id_val);
  150. $('.form_input_text[name="from"]').val(from_val);
  151. $('.form_input_text[name="to"]').val(to_val);
  152. $('.form_input_select[name="type"]').val(type_val);
  153. $('.form_input_datetime[name="LastDT"]').val(lastdt_val);
  154. $('.form_input_text[name="desc"]').val(desc_val);
  155. }
  156. </script>
  157. EOF;
  158. }
  159. function getManagingForm() {
  160. $sApplyChangesC = _t('_sys_admin_apply');
  161. $sFromC = _t('_From');
  162. $sToC = _t('_To');
  163. $sSampleC = _t('_adm_ipbl_sample');
  164. $sTypeC = _t('_adm_ipbl_IP_Role');
  165. $sDescriptionC = _t('_Description');
  166. $sDatatimeC = _t('_adm_ipbl_Date_of_finish');
  167. $sErrorC = _t('_Error Occured');
  168. $aForm = array(
  169. 'form_attrs' => array(
  170. 'name' => 'apply_ip_list_form',
  171. 'action' => $this->_sActionUrl,
  172. 'method' => 'post',
  173. ),
  174. 'params' => array (
  175. 'db' => array(
  176. 'table' => 'sys_ip_list',
  177. 'key' => 'ID',
  178. 'submit_name' => 'add_button',
  179. ),
  180. ),
  181. 'inputs' => array(
  182. 'FromIP' => array(
  183. 'type' => 'text',
  184. 'name' => 'from',
  185. 'caption' => $sFromC,
  186. 'label' => $sSampleC . ': 10.0.0.0',
  187. 'required' => true,
  188. 'checker' => array (
  189. 'func' => 'length',
  190. 'params' => array(7,15),
  191. 'error' => $sErrorC,
  192. ),
  193. ),
  194. 'ToIP' => array(
  195. 'type' => 'text',
  196. 'name' => 'to',
  197. 'caption' => $sToC,
  198. 'label' => $sSampleC . ': 10.0.0.100',
  199. 'required' => true,
  200. 'checker' => array (
  201. 'func' => 'length',
  202. 'params' => array(7,15),
  203. 'error' => $sErrorC,
  204. ),
  205. ),
  206. 'IPRole' => array(
  207. 'type' => 'select',
  208. 'name' => 'type',
  209. 'caption' => $sTypeC,
  210. 'values' => array('allow', 'deny'),
  211. 'required' => true,
  212. ),
  213. 'DateTime' => array(
  214. 'type' => 'datetime',
  215. 'name' => 'LastDT',
  216. 'caption' => $sDatatimeC,
  217. 'required' => true,
  218. 'checker' => array (
  219. 'func' => 'DateTime',
  220. 'error' => $sErrorC,
  221. ),
  222. 'db' => array (
  223. 'pass' => 'DateTime',
  224. ),
  225. 'display' => 'filterDate',
  226. ),
  227. 'Desc' => array(
  228. 'type' => 'text',
  229. 'name' => 'desc',
  230. 'caption' => $sDescriptionC,
  231. 'required' => true,
  232. 'checker' => array (
  233. 'func' => 'length',
  234. 'params' => array(2,128),
  235. 'error' => $sErrorC,
  236. ),
  237. 'db' => array (
  238. 'pass' => 'Xss',
  239. ),
  240. ),
  241. 'ID' => array(
  242. 'type' => 'hidden',
  243. 'value' => '0',
  244. 'name' => 'id',
  245. ),
  246. 'add_button' => array(
  247. 'type' => 'submit',
  248. 'name' => 'add_button',
  249. 'value' => $sApplyChangesC,
  250. ),
  251. ),
  252. );
  253. $sResult = '';
  254. bx_import('BxTemplFormView');
  255. $oForm = new BxTemplFormView($aForm);
  256. $oForm->initChecker();
  257. if ($oForm->isSubmittedAndValid()) {
  258. $sFrom = sprintf("%u", ip2long($_REQUEST['from']));
  259. $sTo = sprintf("%u", ip2long($_REQUEST['to']));
  260. $sType = ((int)$_REQUEST['type']==1) ? 'deny' : 'allow';
  261. $aValsAdd = array (
  262. 'From' => $sFrom,
  263. 'To' => $sTo,
  264. 'Type' => $sType
  265. );
  266. $iLastId = ((int)$_REQUEST['id']) > 0 ? (int)$_REQUEST['id'] : -1;
  267. if ($iLastId > 0) {
  268. $oForm->update($iLastId, $aValsAdd);
  269. } else {
  270. $iLastId = $oForm->insert($aValsAdd);
  271. }
  272. $sResult = ($iLastId > 0) ? MsgBox(_t('_Success')) : MsgBox($sErrorC);
  273. }
  274. return $sResult . $oForm->getCode();
  275. }
  276. function ActionApplyDelete() {
  277. $oDb = BxDolDb::getInstance();
  278. $iID = (int)$_REQUEST['id'];
  279. if ($iID > 0) {
  280. $sDeleteSQL = $oDb->prepare("DELETE FROM `sys_ip_list` WHERE `ID`=? LIMIT 1", $iID);
  281. $oDb->res($sDeleteSQL);
  282. }
  283. }
  284. function deleteExpired () {
  285. $oDb = BxDolDb::getInstance();
  286. $sQuery = $oDb->prepare("DELETE FROM `sys_ip_list` WHERE `LastDT` <= ?", time());
  287. $iAffectedRows = $oDb->query($sQuery);
  288. if ($iAffectedRows) {
  289. db_res("OPTIMIZE TABLE `sys_ip_list`");
  290. return $iAffectedRows;
  291. } else {
  292. return 0;
  293. }
  294. }
  295. }