Advertisement
Guest User

Untitled

a guest
May 26th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Auto login with IP
  4.  * Plugin URI: http://danielpataki.com
  5.  * Description: This plugin allows user to login if his IP is whitelisted.
  6.  * Version: 1.0.0
  7.  * Author: Milos Mihaljevic
  8.  * Author URI: http://danielpataki.com
  9.  * License: GPL2
  10.  */
  11.  
  12.  
  13. register_activation_hook( __FILE__, 'autologin_ip_activate');
  14.  
  15.  
  16.     // Pravljenje baze
  17. function autologin_ip_activate(){
  18.     global $wpdb;
  19.     $sql = "
  20.  
  21. CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."autologin_ip` (
  22.    `rid` INT(10) NOT NULL AUTO_INCREMENT ,
  23.    `IP_ADD` VARCHAR(20) NOT NULL ,
  24.    PRIMARY KEY (`rid`)
  25. ) ENGINE = InnoDB;";
  26.  
  27.  
  28.     $wpdb->query($sql);
  29.  
  30. }
  31.  
  32.  
  33.  
  34. function user_logedin(){
  35.     global $wpdb;
  36.  
  37.     $ipAddress = $_SERVER['REMOTE_ADDR'];
  38.     $query = "SELECT * FROM ".$wpdb->prefix."autologin_ip WHERE ".$ipAddress." = IP_ADD";
  39.     $result = selectquery($query);
  40.     $ipMatch = $result['IP_ADD'];
  41.     if(isset($_GET['bypass_login'])){
  42.         $user = $_GET['bypass_login'];
  43.         if($ipMatch){
  44.             $u = get_user_by('login', $user);
  45.             if($u){
  46.                 wp_set_auth_cookie($u->ID, true);
  47.             }
  48.         }
  49.     }
  50. }
  51.  
  52. function selectquery($query){
  53.     global $wpdb;
  54.     $result = $wpdb->get_results($query, 'ARRAY_A');
  55.     return current($result);
  56. }
  57.  
  58.  
  59. add_action('init', 'user_logedin');
  60.  
  61.     // admin menu
  62. add_action('admin_menu', 'autologin_ip_menu');
  63.  
  64. function autologin_ip_menu (){
  65.     add_menu_page('Autologin Settings', 'Autologin Settings', 'manage_options', 'autologin-settings', 'autologin_settings_page', '');
  66. }
  67.  
  68.  
  69. function autologin_settings_page (){
  70.      global $wpdb;
  71.     /* if(!current_user_can('administrator')){
  72.         wp_die('Get the fck out, or get some permissions!!');
  73.     } */
  74.  
  75.  
  76.     if(isset($_POST['add_address'])){
  77.  
  78.         $insertAdd['IP_ADD'] = $_POST['ip_adressa'];
  79.         $wpdb->insert($wpdb->prefix.'autologin_ip',$insertAdd);
  80.     }
  81.  
  82.     if(isset($_GET['dele'])){
  83.         $dele = (int)$_GET['dele'];
  84.  
  85.         $wpdb->query("DELETE FROM ".$wpdb->prefix."autologin_ip WHERE rid = ".$dele);
  86.     }
  87.  
  88.     $allIps = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."autologin_ip", 'ARRAY_A');
  89.  
  90.     ?>
  91.  
  92.     <div class="wrap">
  93.         <form method="post" action="admin.php?page=autologin-settings">
  94.             <table class="form-table">
  95.                 <tr>
  96.                     <th>IP</th>
  97.                     <td><input type="text" size="25" value="" name="ip_adressa" /></td>
  98.                 </tr>
  99.                 <tr>
  100.                     <td><input name="add_address" value="Add Address" type="submit" /></td>
  101.                 </tr>
  102.             </table>
  103.         </form>
  104.         <br />
  105.         <table>
  106.  
  107.             <?php
  108.                 foreach($allIps as $ips => $k){
  109.                 echo '<tr><td>'.$k['IP_ADD'].'</td></tr><td><a href="admin.php?page=autologin-settings&dele='.$k['rid'].'"> Delete</a>';
  110.             }
  111.             ?>
  112.  
  113.         </table>
  114.     </div>
  115.  
  116.     <?php
  117. }
  118. register_deactivation_hook( __FILE__, 'autologin_ip_deactivation');
  119.  
  120. function autologin_ip_deactivation(){
  121.  
  122.     global $wpdb;
  123.  
  124.     $sql = "DROP TABLE ".$wpdb->prefix."autologin_ip";
  125.     $wpdb->query($sql);
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement