Gglcptch_Whitelist

The Google Captcha (reCAPTCHA) by BestWebSoft Gglcptch Whitelist class.

Defined (1)

The class is defined in the following location(s).

/includes/whitelist.php  
  1. class Gglcptch_Whitelist extends WP_List_Table { 
  2. private 
  3. $basename,  
  4. $order_by,  
  5. $per_page,  
  6. $paged,  
  7. $order,  
  8. $s; 
  9.  
  10. /** 
  11. * Constructor of class 
  12. */ 
  13. function __construct( $plugin_basename ) { 
  14. global $gglcptch_options; 
  15. if ( empty( $gglcptch_options ) ) 
  16. $gglcptch_options = get_option( 'gglcptch_options' ); 
  17. parent::__construct( array( 
  18. 'singular' => 'IP',  
  19. 'plural' => 'IP',  
  20. 'ajax' => true,  
  21. ); 
  22. $this->basename = $plugin_basename; 
  23. $this->display_notices(); 
  24.  
  25. /** 
  26. * Display content 
  27. * @return void 
  28. */ 
  29. function display_content() { 
  30. global $wp_version, $gglcptch_options; 
  31. if ( isset( $_SERVER ) ) { 
  32. $sever_vars = array( 'HTTP_X_REAL_IP', 'HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'REMOTE_ADDR' ); 
  33. foreach ( $sever_vars as $var ) { 
  34. if ( isset( $_SERVER[ $var ] ) && ! empty( $_SERVER[ $var ] ) ) { 
  35. if ( filter_var( $_SERVER[ $var ], FILTER_VALIDATE_IP ) ) { 
  36. $my_ip = $_SERVER[ $var ]; 
  37. break; 
  38. } else { /** if proxy */ 
  39. $ip_array = explode( ', ', $_SERVER[ $var ] ); 
  40. if ( is_array( $ip_array ) && ! empty( $ip_array ) && filter_var( $ip_array[0], FILTER_VALIDATE_IP ) ) { 
  41. $my_ip = $ip_array[0]; 
  42. break; 
  43.  
  44. $this->prepare_items(); ?> 
  45. <p><strong><?php _e( 'For IP addresses from the whitelist reCAPTCHA will not be displayed', 'google-captcha' ); ?></strong></p> 
  46. <?php if ( ! ( isset( $_REQUEST['gglcptch_show_whitelist_form'] ) || isset( $_REQUEST['gglcptch_add_to_whitelist'] ) ) ) { ?> 
  47. <form method="post" action="admin.php?page=google-captcha.php&action=whitelist" style="margin: 10px 0;"> 
  48. <table> 
  49. <tr> 
  50. <td class="gglcptch_whitelist_buttons"> 
  51. <div class="alignleft"> 
  52. <button class="button" name="gglcptch_show_whitelist_form" value="on"><?php _e( 'Add IP to the whitelist', 'google-captcha' ); ?></button> 
  53. </div> 
  54. <noscript> 
  55. <div class="alignleft"> 
  56. <input type="submit" name="gglcptch_save_add_ip_form_button" class="button-primary" value="<?php _e( 'Save changes', 'google-captcha' ); ?>" /> 
  57. </div> 
  58. </noscript> 
  59. <?php wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> 
  60. <input type="hidden" name="gglcptch_save_add_ip_form" value="1"/> 
  61. <td> 
  62. </tr> 
  63. </table> 
  64. </form> 
  65. <?php } ?> 
  66. <form class="form-table gglcptch_whitelist_form" method="post" action="admin.php?page=google-captcha.php&action=whitelist" style="margin: 10px 0;<?php echo ! ( isset( $_REQUEST['gglcptch_show_whitelist_form'] ) || isset( $_REQUEST['gglcptch_add_to_whitelist'] ) ) ? 'display: none;': ''; ?>"> 
  67. <div style="margin: 10px 0; position: relative;"> 
  68. <input type="text" maxlength="31" name="gglcptch_add_to_whitelist" /> 
  69. <?php if ( isset( $my_ip ) ) { ?> 
  70. <br /> 
  71. <label id="gglcptch_add_my_ip"> 
  72. <input type="checkbox" name="gglcptch_add_to_whitelist_my_ip" value="1" /> 
  73. <?php _e( 'My IP', 'google-captcha' ); ?> 
  74. <input type="hidden" name="gglcptch_add_to_whitelist_my_ip_value" value="<?php echo $my_ip; ?>" /> 
  75. </label> 
  76. <?php } ?> 
  77. <br /><input type="submit" class="button-secondary" value="<?php _e( 'Save IP to the whitelist', 'google-captcha' ) ?>" /> 
  78. <?php wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> 
  79. </div> 
  80. <div style="margin: 10px 0;"> 
  81. <span class="bws_info" style="line-height: 2;"><?php _e( "Allowed formats", 'google-captcha' ); ?>: <code>192.168.0.1</code></span><br/> 
  82. <span class="bws_info" style="line-height: 2;"><?php _e( "Allowed diapason", 'google-captcha' ); ?>: <code>0.0.0.0 - 255.255.255.255</code></span> 
  83. </div> 
  84. <?php gglcptch_pro_block( 'gglcptch_whitelist_banner' ); ?> 
  85. </form> 
  86. <form id="gglcptch_whitelist_search" method="post" action="admin.php?page=google-captcha.php&action=whitelist"> 
  87. <?php $this->search_box( __( 'Search IP', 'google-captcha' ), 'search_whitelisted_ip' ); 
  88. wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> 
  89. </form> 
  90. <form id="gglcptch_whitelist" method="post" action="admin.php?page=google-captcha.php&action=whitelist"> 
  91. <?php $this->display(); 
  92. wp_nonce_field( $this->basename, 'gglcptch_nonce_name' ); ?> 
  93. </form> 
  94. <?php } 
  95.  
  96. /** 
  97. * Function to prepare data before display 
  98. * @return void 
  99. */ 
  100. function prepare_items() { 
  101. if ( isset( $_GET['orderby'] ) && in_array( $_GET['orderby'], array_keys( $this->get_sortable_columns() ) ) ) { 
  102. switch ( $_GET['orderby'] ) { 
  103. case 'ip': 
  104. $this->order_by = 'ip_from_int'; 
  105. break; 
  106. case 'ip_from': 
  107. $this->order_by = 'ip_from_int'; 
  108. break; 
  109. case 'ip_to': 
  110. $this->order_by = 'ip_to_int'; 
  111. break; 
  112. default: 
  113. $this->order_by = esc_sql( $_GET['orderby'] ); 
  114. break; 
  115. } else { 
  116. $this->order_by = 'add_time'; 
  117. $this->order = isset( $_REQUEST['order'] ) && in_array( strtoupper( $_REQUEST['order'] ), array( 'ASC', 'DESC' ) ) ? $_REQUEST['order'] : ''; 
  118. $this->paged = isset( $_REQUEST['paged'] ) && is_numeric( $_REQUEST['paged'] ) ? $_REQUEST['paged'] : ''; 
  119. $this->s = isset( $_REQUEST['s'] ) ? esc_html( trim( $_REQUEST['s'] ) ) : ''; 
  120. $this->per_page = $this->get_items_per_page( 'gglcptch_per_page', 20 ); 
  121.  
  122. $columns = $this->get_columns(); 
  123. $hidden = array(); 
  124. $sortable = $this->get_sortable_columns(); 
  125. $primary = 'ip'; 
  126. $this->_column_headers = array( $columns, $hidden, $sortable, $primary ); 
  127. $this->items = $this->get_content(); 
  128. $this->set_pagination_args( array( 
  129. 'total_items' => $this->get_items_number(),  
  130. 'per_page' => 20,  
  131. ); 
  132. /** 
  133. * Function to show message if empty list 
  134. * @return void 
  135. */ 
  136. function no_items() { 
  137. $label = isset( $_REQUEST['s'] ) ? __( 'Nothing found', 'google-captcha' ) : __( 'No IP in the whitelist', 'google-captcha' ); ?> 
  138. <p><?php echo $label; ?></p> 
  139. <?php } 
  140.  
  141. function get_columns() { 
  142. $columns = array( 
  143. 'cb' => '<input type="checkbox" />',  
  144. 'ip' => __( 'IP address', 'google-captcha' ),  
  145. 'add_time' => __( 'Date added', 'google-captcha' ) 
  146. ); 
  147. return $columns; 
  148. /** 
  149. * Get a list of sortable columns. 
  150. * @return array list of sortable columns 
  151. */ 
  152. function get_sortable_columns() { 
  153. $sortable_columns = array( 
  154. 'ip' => array( 'ip', true ),  
  155. 'add_time' => array( 'add_time', false ) 
  156. ); 
  157. return $sortable_columns; 
  158. /** 
  159. * Fires when the default column output is displayed for a single row. 
  160. * @param string $column_name The custom column's name. 
  161. * @param array $item The cuurrent letter data. 
  162. * @return void 
  163. */ 
  164. function column_default( $item, $column_name ) { 
  165. switch ( $column_name ) { 
  166. case 'ip': 
  167. case 'add_time': 
  168. return $item[ $column_name ]; 
  169. default: 
  170. /** Show whole array for bugfix */ 
  171. return print_r( $item, true ); 
  172. /** 
  173. * Function to manage content of column with checboxes 
  174. * @param array $item The cuurrent letter data. 
  175. * @return string with html-structure of <input type=['checkbox']> 
  176. */ 
  177. function column_cb( $item ) { 
  178. /** customize displaying cb collumn */ 
  179. return sprintf( 
  180. '<input type="checkbox" name="id[]" value="%s"/>', $item['id'] 
  181. ); 
  182. /** 
  183. * Function to manage content of column with IP-adresses 
  184. * @param array $item The cuurrent letter data. 
  185. * @return string with html-structure of <input type=['checkbox']> 
  186. */ 
  187. function column_ip( $item ) { 
  188. $order_by = empty( $this->order_by ) ? '' : "&orderby={$this->order_by}"; 
  189. $order = empty( $this->order ) ? '' : "&order={$this->order}"; 
  190. $paged = empty( $this->paged ) ? '' : "&paged={$this->paged}"; 
  191. $s = empty( $this->s ) ? '' : "&s={$this->s}"; 
  192. $url = "?page=google-captcha.php&action=whitelist&gglcptch_remove={$item['id']}{$order_by}{$order}{$paged}{$s}"; 
  193. $actions = array( 
  194. 'remove' => '<a href="' . wp_nonce_url( $url, "gglcptch_nonce_remove_{$item['id']}" ) . '">' . __( 'Remove from the whitelist', 'google-captcha' ) . '</a>' 
  195. ); 
  196. return sprintf('%1$s %2$s', $item['ip'], $this->row_actions( $actions ) ); 
  197. /** 
  198. * List with bulk action for IP 
  199. * @return array $actions 
  200. */ 
  201. function get_bulk_actions() { 
  202. /** adding bulk action */ 
  203. return array( 'gglcptch_remove'=> __( 'Remove from the whitelist', 'google-captcha' ) ); 
  204. /** 
  205. * Get content for table 
  206. * @return array 
  207. */ 
  208. function get_content() { 
  209. global $wpdb; 
  210.  
  211. if ( empty( $this->s ) ) { 
  212. $where = ''; 
  213. } else { 
  214. $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; 
  215. $where = 
  216. 0 == $ip_int 
  217. " WHERE `ip` LIKE '%{$this->s}%' OR `ip_to` LIKE '%{$this->s}%' OR `ip_from` LIKE '%{$this->s}%'" 
  218. " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; 
  219. $order_by = empty( $this->order_by ) ? '' : " ORDER BY `{$this->order_by}`"; 
  220. $order = empty( $this->order ) ? '' : strtoupper( " {$this->order}" ); 
  221. $offset = empty( $this->paged ) ? '' : " OFFSET " . ( $this->per_page * ( absint( $this->paged ) - 1 ) ); 
  222.  
  223. return $wpdb->get_results( "SELECT * FROM `{$wpdb->prefix}gglcptch_whitelist`{$where}{$order_by}{$order} LIMIT {$this->per_page}{$offset}", ARRAY_A ); 
  224.  
  225. /** 
  226. * Get number of all IPs which were added to database 
  227. * @since 1.6.9 
  228. * @param void 
  229. * @return int the number of IPs 
  230. */ 
  231. private function get_items_number() { 
  232. global $wpdb; 
  233. if ( empty( $this->s ) ) { 
  234. $where = ''; 
  235. } else { 
  236. $ip_int = filter_var( $this->s, FILTER_VALIDATE_IP ) ? sprintf( '%u', ip2long( $this->s ) ) : 0; 
  237. $where = 
  238. 0 == $ip_int 
  239. " WHERE `ip` LIKE '%{$this->s}%' OR `ip_to` LIKE '%{$this->s}%' OR `ip_from` LIKE '%{$this->s}%'" 
  240. " WHERE ( `ip_from_int` <= {$ip_int} AND `ip_to_int` >= {$ip_int} )"; 
  241. return absint( $wpdb->get_var( "SELECT COUNT(`id`) FROM `{$wpdb->prefix}gglcptch_whitelist`{$where}" ) ); 
  242.  
  243. /** 
  244. * Handle necessary reqquests and display notices 
  245. * @return void 
  246. */ 
  247. function display_notices() { 
  248. global $wpdb, $gglcptch_options; 
  249. $error = $message = ''; 
  250.  
  251. $bulk_action = isset( $_REQUEST['action'] ) && 'gglcptch_remove' == $_REQUEST['action'] ? true : false; 
  252. if ( ! $bulk_action ) 
  253. $bulk_action = isset( $_REQUEST['action2'] ) && 'gglcptch_remove' == $_REQUEST['action2'] ? true : false; 
  254.  
  255. /** Add IP to the database */ 
  256. if ( 
  257. isset( $_POST['gglcptch_add_to_whitelist'] ) && 
  258. ( ! empty( $_POST['gglcptch_add_to_whitelist'] ) || isset( $_POST['gglcptch_add_to_whitelist_my_ip'] ) ) && 
  259. check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) 
  260. ) { 
  261. $add_ip = isset( $_POST['gglcptch_add_to_whitelist_my_ip'] ) ? $_POST['gglcptch_add_to_whitelist_my_ip_value'] : $_POST['gglcptch_add_to_whitelist']; 
  262.  
  263. $valid_ip = filter_var( stripslashes( esc_html( trim( $add_ip ) ) ), FILTER_VALIDATE_IP ); 
  264. if ( $valid_ip ) { 
  265. $ip_int = sprintf( '%u', ip2long( $valid_ip ) ); 
  266. $id = $wpdb->get_var( "SELECT `id` FROM " . $wpdb->prefix . "gglcptch_whitelist WHERE ( `ip_from_int` <= " . $ip_int . " AND `ip_to_int` >= " . $ip_int . " ) OR `ip` LIKE '" . $valid_ip . "' LIMIT 1;" ); 
  267. /** check if IP already in database */ 
  268. if ( is_null( $id ) ) { 
  269. $time = current_time( 'mysql' ); 
  270. $wpdb->insert( 
  271. $wpdb->prefix . "gglcptch_whitelist",  
  272. array( 
  273. 'ip' => $valid_ip,  
  274. 'ip_from_int' => $ip_int,  
  275. 'ip_to_int' => $ip_int,  
  276. 'add_time' => $time 
  277. ); 
  278. if ( ! $wpdb->last_error ) 
  279. $message = __( 'IP added to the whitelist successfully', 'google-captcha' ); 
  280. else 
  281. $error = __( 'Some errors occured', 'google-captcha' ); 
  282. } else { 
  283. $error = __( 'IP is already in the whitelist', 'google-captcha' ); 
  284. } else { 
  285. $error = __( 'Invalid IP. See allowed formats.', 'google-captcha' ); 
  286. if ( empty( $error ) ) { 
  287. $gglcptch_options['whitelist_is_empty'] = false; 
  288. update_option( 'gglcptch_options', $gglcptch_options ); 
  289. /** Remove IP from database */ 
  290. } elseif ( $bulk_action && check_admin_referer( $this->basename, 'gglcptch_nonce_name' ) ) { 
  291. if ( ! empty( $_REQUEST['id'] ) ) { 
  292. $list = implode( ', ', $_REQUEST['id'] ); 
  293. $result = $wpdb->query( "DELETE FROM `" . $wpdb->prefix . "gglcptch_whitelist` WHERE `id` IN (" . $list . ");" ); 
  294. if ( ! $wpdb->last_error ) { 
  295. $message = sprintf( _n( "%s IP was deleted successfully", "%s IPs were deleted successfully", $result, 'google-captcha' ), $result ); 
  296. $gglcptch_options['whitelist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_whitelist` LIMIT 1" ) ) ? true : false; 
  297. update_option( 'gglcptch_options', $gglcptch_options ); 
  298. } else { 
  299. $error = __( 'Some errors occured', 'google-captcha' ); 
  300. } elseif ( isset( $_GET['gglcptch_remove'] ) && check_admin_referer( 'gglcptch_nonce_remove_' . $_GET['gglcptch_remove'] ) ) { 
  301. $wpdb->delete( $wpdb->prefix . "gglcptch_whitelist", array( 'id' => $_GET['gglcptch_remove'] ) ); 
  302. if ( ! $wpdb->last_error ) { 
  303. $message = __( "One IP was deleted successfully", 'google-captcha' ); 
  304. $gglcptch_options['whitelist_is_empty'] = is_null( $wpdb->get_var( "SELECT `id` FROM `{$wpdb->prefix}gglcptch_whitelist` LIMIT 1" ) ) ? true : false; 
  305. update_option( 'gglcptch_options', $gglcptch_options ); 
  306. } else { 
  307. $error = __( 'Some errors occured', 'google-captcha' ); 
  308. } elseif ( isset( $_POST['gglcptch_add_to_whitelist'] ) && empty( $_POST['gglcptch_add_to_whitelist'] ) && ! isset( $_POST['bws_hide_premium_options'] ) ) { 
  309. $error = __( 'You have not entered any IP', 'google-captcha' ); 
  310. } elseif ( isset( $_REQUEST['s'] ) ) { 
  311. if ( '' == $_REQUEST['s'] ) { 
  312. $error = __( 'You have not entered any IP in to the search form', 'google-captcha' ); 
  313. } else { 
  314. $message = __( 'Search results for', 'google-captcha' ) . ' : ' . esc_html( $_REQUEST['s'] ); 
  315. if ( ! empty( $message ) ) { ?> 
  316. <div class="updated fade below-h2"><p><strong><?php echo $message; ?></strong></p></div> 
  317. <?php } 
  318. if ( ! empty( $error ) ) { ?> 
  319. <div class="error below-h2"><p><strong><?php echo $error; ?></strong></p></div> 
  320. <?php }