Plugin Directory

Changeset 2977661


Ignore:
Timestamp:
10/11/2023 02:13:12 PM (2 years ago)
Author:
senpaisoftware
Message:

Add namespace

Location:
senpai-software-2fa
Files:
19 added
4 edited

Legend:

Unmodified
Added
Removed
  • senpai-software-2fa/trunk/admin/senpai-software-2fa-admin.php

    r2938444 r2977661  
    11<?php
     2
     3namespace SenpaiSoftware2FA;
     4
    25/**
    36 * Enqueue CSS and JS for admin area.
    47 */
    5 function senpai_software_2fa_css_js(){
     8function css_js(){
    69    // loading css
    710    wp_register_style( 'senpai-software-2fa-admin', plugin_dir_url( __DIR__ ) . 'css/senpai-software-2fa.css', false);
     
    1215    wp_enqueue_script( 'senpai-software-2fa-admin' );
    1316}
    14 add_action( 'admin_enqueue_scripts', 'senpai_software_2fa_css_js' );
     17add_action( 'admin_enqueue_scripts', __NAMESPACE__.'\css_js' );
    1518
    1619/**
    1720 * Display user profile fields.
    1821 */
    19 function senpai_software_2fa_profile_fields($user){
     22function profile_fields($user){
    2023
    2124    $user_id = $user->ID;
     
    5962                    <div id="senpai_software_2fa_block">
    6063                        <p>
    61                         <label>
    62                             <span class="dashicons dashicons-admin-network"></span> <?php echo esc_html(__( 'Select key file (max file size 1 GB)','senpai-software-2fa' )); ?>
    63                             <input type="file" id="senpai_software_2fa_file" onchange="senpai_software_2fa_upload();">
    64                         </label>
    65                         <input type="hidden" id="senpai_software_2fa_hash" name="senpai_software_2fa_hash">
     64                            <label>
     65                                <span class="dashicons dashicons-admin-network"></span> <?php echo esc_html(__( 'Select key file (max file size 1 GB)','senpai-software-2fa' )); ?>
     66                                <input type="file" id="senpai_software_2fa_file" onchange="senpai_software_2fa_upload();">
     67                            </label>
     68                            <input type="hidden" id="senpai_software_2fa_hash" name="senpai_software_2fa_hash">
    6669                        </p>
    6770                        <p id="senpai_software_2fa_name"></p>
     
    7780    <?php
    7881}
    79 add_action('show_user_profile', 'senpai_software_2fa_profile_fields');
    80 add_action('edit_user_profile', 'senpai_software_2fa_profile_fields');
     82add_action('show_user_profile', __NAMESPACE__.'\profile_fields');
     83add_action('edit_user_profile', __NAMESPACE__.'\profile_fields');
    8184
    8285/**
    8386 * Update user profile fields.
    8487 */
    85 function senpai_software_2fa_profile_fields_save($user_id){
     88function profile_fields_save($user_id){
    8689
    8790    if (!current_user_can('edit_user', $user_id)) {
     
    108111    }
    109112}
    110 add_action('personal_options_update', 'senpai_software_2fa_profile_fields_save');
    111 add_action('edit_user_profile_update', 'senpai_software_2fa_profile_fields_save');
     113add_action('personal_options_update', __NAMESPACE__.'\profile_fields_save');
     114add_action('edit_user_profile_update', __NAMESPACE__.'\profile_fields_save');
    112115
    113116/**
    114117 * Add plugin page
    115118 */
    116 function senpai_software_2fa_menu() {
    117 
    118     add_options_page(
    119         esc_html(__( '2FA Settings','senpai-software-2fa' )),
    120         esc_html(__( '2FA Settings','senpai-software-2fa' )),
    121         'manage_options',
    122         'snp_2fa',
    123         'senpai_software_2fa_set',
    124         99
    125     );
    126 }
    127 
    128 add_action('admin_menu', 'senpai_software_2fa_menu');
    129 
    130 function senpai_software_2fa_set() {
    131 
    132     if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    133 
    134         $xmlrpc = sanitize_text_field( $_POST['snp_2fa_xmlrpc'] );
    135         $hints = sanitize_text_field( $_POST['snp_2fa_hint'] );
    136         $attempts = sanitize_text_field( $_POST['snp_2fa_attempts'] );
    137         $block_period = sanitize_text_field( $_POST['snp_2fa_block_period'] );
    138 
    139         update_option( 'snp_2fa_xmlrpc', $xmlrpc );
    140         update_option( 'snp_2fa_hint', $hints );
    141         update_option( 'snp_2fa_attempts', $attempts );
    142         update_option( 'snp_2fa_block_period', $block_period );
    143 
    144         if ( get_option( 'snp_2fa_attempts' ) !== null ) {
    145 
    146             global $wpdb;
    147             $table_name = $wpdb->prefix . 'snp_2fa_ip';
    148 
    149             if ( $wpdb->get_var("show tables like '".$table_name."'") != $table_name ) {
    150 
    151                 $charset_collate = $wpdb->get_charset_collate();
    152 
    153                 $sql = "CREATE TABLE $table_name (
     119function menu() {
     120
     121    add_options_page(
     122        esc_html(__( '2FA Settings','senpai-software-2fa' )),
     123        esc_html(__( '2FA Settings','senpai-software-2fa' )),
     124        'manage_options',
     125        'snp_2fa',
     126        __NAMESPACE__.'\set',
     127        99
     128    );
     129}
     130
     131add_action('admin_menu', __NAMESPACE__.'\menu');
     132
     133function set() {
     134
     135    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
     136
     137        $xmlrpc = sanitize_text_field( $_POST['snp_2fa_xmlrpc'] );
     138        $hints = sanitize_text_field( $_POST['snp_2fa_hint'] );
     139        $attempts = sanitize_text_field( $_POST['snp_2fa_attempts'] );
     140        $block_period = sanitize_text_field( $_POST['snp_2fa_block_period'] );
     141
     142        update_option( 'snp_2fa_xmlrpc', $xmlrpc );
     143        update_option( 'snp_2fa_hint', $hints );
     144        update_option( 'snp_2fa_attempts', $attempts );
     145        update_option( 'snp_2fa_block_period', $block_period );
     146
     147        if ( get_option( 'snp_2fa_attempts' ) !== null ) {
     148
     149            global $wpdb;
     150            $table_name = $wpdb->prefix . 'snp_2fa_ip';
     151
     152            if ( $wpdb->get_var("show tables like '".$table_name."'") != $table_name ) {
     153
     154                $charset_collate = $wpdb->get_charset_collate();
     155
     156                $sql = "CREATE TABLE $table_name (
    154157                  id int(10) NOT NULL AUTO_INCREMENT,
    155158                  ip varchar(40) NOT NULL,
     
    159162                ) $charset_collate;";
    160163
    161                 require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    162                 dbDelta( $sql );
    163 
    164             }
    165         }
    166 
    167         add_settings_error(
    168             'snp-2fa-settings',
    169             'settings-saved',
    170             __('Settings saved.', 'default'),
    171             'updated'
    172         );
    173         settings_errors('snp-2fa-settings');
    174 
    175     }
    176 
    177     $xmlrpc_disable=null;
    178     $xmlrpc_enable=null;
    179 
    180     $xmlrpc=get_option( 'snp_2fa_xmlrpc' );
    181 
    182     if($xmlrpc==1){
    183         $xmlrpc_disable="checked";
    184     } else {
    185         $xmlrpc_enable="checked";
    186     }
    187 
    188     $hint=null;
    189     $hint=get_option( 'snp_2fa_hint' );
    190 
    191     $attempts=null;
    192     $attempts=get_option( 'snp_2fa_attempts' );
    193 
    194     $block_period=null;
    195     $block_period=get_option( 'snp_2fa_block_period' );
    196     ?>
     164                require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
     165                dbDelta( $sql );
     166
     167            }
     168        }
     169
     170        add_settings_error(
     171            'snp-2fa-settings',
     172            'settings-saved',
     173            __('Settings saved.', 'default'),
     174            'updated'
     175        );
     176        settings_errors('snp-2fa-settings');
     177
     178    }
     179
     180    $xmlrpc_disable=null;
     181    $xmlrpc_enable=null;
     182
     183    $xmlrpc=get_option( 'snp_2fa_xmlrpc' );
     184
     185    if($xmlrpc==1){
     186        $xmlrpc_disable="checked";
     187    } else {
     188        $xmlrpc_enable="checked";
     189    }
     190
     191    $hint=null;
     192    $hint=get_option( 'snp_2fa_hint' );
     193
     194    $attempts=null;
     195    $attempts=get_option( 'snp_2fa_attempts' );
     196
     197    $block_period=null;
     198    $block_period=get_option( 'snp_2fa_block_period' );
     199    ?>
    197200
    198201    <div class="wrap">
     
    210213                        <label for="snp_2fa_xmlrpc_enable"><?php echo esc_html(__( 'Enable','senpai-software-2fa' )); ?></label>
    211214                        <p class="description">
    212                             <?php echo esc_html(__( 'XML-RPC creates serious vulnerabilities for the site. For full protection, it must be disabled.','senpai-software-2fa' )); ?>
     215                            <?php echo esc_html(__( 'XML-RPC creates serious vulnerabilities for the site. For full protection, it must be disabled.','senpai-software-2fa' )); ?>
    213216                        </p>
    214217                    </td>
     
    218221                    <td><textarea class="regular-text" name="snp_2fa_hint"><?php echo sanitize_text_field($hint); ?></textarea>
    219222                        <p class="description">
    220                             <?php echo esc_html(__( ' Default hints help hackers crack your credentials. Replace hints with neutral text, such as "Invalid data".','senpai-software-2fa' )); ?>
     223                            <?php echo esc_html(__( ' Default hints help hackers crack your credentials. Replace hints with neutral text, such as "Invalid data".','senpai-software-2fa' )); ?>
    221224                        </p>
    222225                    </td>
     
    227230                        <input type="number" min="1" placeholder="For example: 3" name="snp_2fa_attempts" value="<?php echo sanitize_text_field($attempts); ?>">
    228231                        <p class="description">
    229                             <?php echo esc_html(__( 'The number of failed login attempts after which the IP will be blocked. To remove restrictions, leave the field blank.','senpai-software-2fa' )); ?>
     232                            <?php echo esc_html(__( 'The number of failed login attempts after which the IP will be blocked. To remove restrictions, leave the field blank.','senpai-software-2fa' )); ?>
    230233                        </p>
    231234                        <br/>
    232235                        <input type="number" min="1" placeholder="For example: 15" name="snp_2fa_block_period" value="<?php echo sanitize_text_field($block_period); ?>">
    233236                        <p class="description">
    234                             <?php echo esc_html(__( 'The period for which the IP will be blocked (in minutes).','senpai-software-2fa' )); ?>
     237                            <?php echo esc_html(__( 'The period for which the IP will be blocked (in minutes).','senpai-software-2fa' )); ?>
    235238                        </p>
    236239                    </td>
     
    242245    </div>
    243246
    244     <?php
    245 }
     247    <?php
     248}
  • senpai-software-2fa/trunk/readme.txt

    r2955650 r2977661  
    66Requires PHP: 5.6
    77Tested up to: 6.3
    8 Stable tag: 2.0.0
     8Stable tag: 2.0.1
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    6161== Changelog ==
    6262
     63= 2.0.1 =
     64* Added namespace
     65
    6366= 2.0.0 =
    6467* Added ability to disable XML-RPC.
  • senpai-software-2fa/trunk/senpai-software-2fa-core.php

    r2938444 r2977661  
    11<?php
     2
     3namespace SenpaiSoftware2FA;
     4
    25/**
    36 * Load CSS and JS files for login page
    47 */
    5 function senpai_software_2fa_login_css_js() {
     8function login_css_js() {
    69    // Load CSS
    710    wp_register_style( 'senpai-software-2fa', plugin_dir_url( __FILE__ ) . 'css/senpai-software-2fa.css', false );
     
    1215    wp_enqueue_script( 'senpai-software-2fa' );
    1316}
    14 add_action( 'login_enqueue_scripts', 'senpai_software_2fa_login_css_js' );
     17add_action( 'login_enqueue_scripts', __NAMESPACE__.'\login_css_js' );
    1518
    1619/**
    1720 * Add form field for uploading key file
    1821 */
    19 function senpai_software_2fa_form_field() {
     22function form_field() {
    2023    ?>
    2124    <div id="senpai_software_2fa_block">
     
    3336    <?php
    3437}
    35 add_action( 'login_form', 'senpai_software_2fa_form_field' );
     38add_action( 'login_form', __NAMESPACE__.'\form_field' );
    3639
    3740/**
    3841 * Validate key file
    3942 */
    40 function senpai_software_2fa_validation( $user, $password ) {
     43function validation( $user, $password ) {
    4144    $status = get_user_meta( $user->ID, 'senpai_software_2fa_status', true );
    4245    $db_hash = get_user_meta( $user->ID, 'senpai_software_2fa_hash', true );
     
    5154
    5255                if ($db_hash !== $file_hash) {
    53                     return new WP_Error('access denied', __('Wrong key file','senpai-software-2fa'));
     56                    return new \WP_Error('access denied', __('Wrong key file','senpai-software-2fa'));
    5457                }
    5558            } else {
    56                 return new WP_Error('access denied', __('File error','senpai-software-2fa'));
     59                return new \WP_Error('access denied', __('File error','senpai-software-2fa'));
    5760            }
    5861        } else {
    59             return new WP_Error( 'access denied', __( 'Upload your key file','senpai-software-2fa' ) );
     62            if(!empty($db_hash)) {
     63                return new \WP_Error( 'access denied', __( 'Upload your key file', 'senpai-software-2fa' ) );
     64            }
    6065        }
    6166    }
    6267    return $user;
    6368}
    64 add_action( 'wp_authenticate_user', 'senpai_software_2fa_validation', 10, 3 );
     69add_action( 'wp_authenticate_user', __NAMESPACE__.'\validation', 10, 3 );
    6570
    6671/**
     
    6873 */
    6974if(get_option( 'snp_2fa_xmlrpc' )==1){
    70     add_filter('xmlrpc_enabled', '__return_false');
     75    add_filter('xmlrpc_enabled', '__return_false');
    7176}
    7277
     
    7681if(!empty(get_option( 'snp_2fa_hint' ))) {
    7782
    78     function senpai_software_2fa_hints(){
    79         return __( sanitize_text_field(get_option( 'snp_2fa_hint' )),'default' );
    80     }
    81     add_filter( 'login_errors', 'senpai_software_2fa_hints' );
     83    function hints(){
     84        return __( sanitize_text_field(get_option( 'snp_2fa_hint' )),'default' );
     85    }
     86    add_filter( 'login_errors', __NAMESPACE__.'\hints' );
    8287}
    8388
     
    8792if ( $GLOBALS['pagenow'] === 'wp-login.php' ) {
    8893
    89     function senpai_software_2fa_check() {
     94    function check() {
    9095
    91         $attempts=get_option('snp_2fa_attempts');
    92         $ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
     96        $attempts=get_option('snp_2fa_attempts');
     97        $ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
    9398
    94         global $wpdb;
    95         $table = $wpdb->prefix . 'snp_2fa_ip';
     99        global $wpdb;
     100        $table = $wpdb->prefix . 'snp_2fa_ip';
    96101
    97         $results = $wpdb->get_results( "SELECT `counter`,`blockdate` FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
     102        $results = $wpdb->get_results( "SELECT `counter`,`blockdate` FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
    98103
    99         if($results && (!empty($attempts))) {
     104        if($results && (!empty($attempts))) {
    100105
    101             $counter    = $results[0]->counter;
    102             $block_time = $results[0]->blockdate;
     106            $counter    = $results[0]->counter;
     107            $block_time = $results[0]->blockdate;
    103108
    104             if ( $counter >= $attempts ) {
     109            if ( $counter >= $attempts ) {
    105110
    106                 $duration=sanitize_text_field(get_option('snp_2fa_block_period'));
    107                 if(empty($duration)){ $duration=15; }
    108                 $duration='PT'.$duration.'M';
     111                $duration=sanitize_text_field(get_option('snp_2fa_block_period'));
     112                if(empty($duration)){ $duration=15; }
     113                $duration='PT'.$duration.'M';
    109114
    110                 $currentDateTime   = new DateTime();
    111                 $specifiedDateTime = new DateTime( $block_time );
    112                 $specifiedDateTime->add( new DateInterval( $duration ) );
     115                $currentDateTime   = new \DateTime();
     116                $specifiedDateTime = new \DateTime( $block_time );
     117                $specifiedDateTime->add( new \DateInterval( $duration ) );
    113118
    114                 if ( $currentDateTime > $specifiedDateTime ) {
    115                     $wpdb->get_results( "DELETE FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
    116                 } else {
    117                     wp_die( 'Access temporarily restricted', 'Blocked', array( 'response' => 403 ) );
    118                 }
    119             }
    120         }
    121     }
    122     add_filter( 'init', 'senpai_software_2fa_check' );
     119                if ( $currentDateTime > $specifiedDateTime ) {
     120                    $wpdb->get_results( "DELETE FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
     121                } else {
     122                    wp_die( 'Access temporarily restricted', 'Blocked', array( 'response' => 403 ) );
     123                }
     124            }
     125        }
     126    }
     127    add_filter( 'init', __NAMESPACE__.'\check' );
    123128}
    124129
    125 function senpai_software_2fa_login_failed(){
     130function login_failed(){
    126131
    127     $attempts=get_option('snp_2fa_attempts');
    128     $ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
     132    $attempts=get_option('snp_2fa_attempts');
     133    $ip=sanitize_text_field($_SERVER['REMOTE_ADDR']);
    129134
    130     global $wpdb;
    131     $table = $wpdb->prefix . 'snp_2fa_ip';
     135    global $wpdb;
     136    $table = $wpdb->prefix . 'snp_2fa_ip';
    132137
    133     $results = $wpdb->get_results( "SELECT `counter`,`blockdate` FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
     138    $results = $wpdb->get_results( "SELECT `counter`,`blockdate` FROM `{$table}` WHERE `ip`='{$ip}' LIMIT 1" );
    134139
    135     $currentDateTime = new DateTime();
    136     $date=$currentDateTime->format('Y-m-d H:i:s');
     140    $currentDateTime = new \DateTime();
     141    $date=$currentDateTime->format('Y-m-d H:i:s');
    137142
    138     if(!empty($attempts)) {
     143    if(!empty($attempts)) {
    139144
    140         if ( $results ) {
     145        if ( $results ) {
    141146
    142             $counter = $results[0]->counter;
    143             $counter ++;
     147            $counter = $results[0]->counter;
     148            $counter ++;
    144149
    145             $block_date = new DateTime( $results[0]->blockdate );
    146             $block_date->add( new DateInterval( 'PT5M' ) );
     150            $block_date = new \DateTime( $results[0]->blockdate );
     151            $block_date->add( new \DateInterval( 'PT5M' ) );
    147152
    148             if ( $currentDateTime > $block_date ) {
    149                 $counter = 1;
    150             }
     153            if ( $currentDateTime > $block_date ) {
     154                $counter = 1;
     155            }
    151156
    152             $data  = array(
    153                 'counter'   => $counter,
    154                 'blockdate' => $date
    155             );
    156             $where = array(
    157                 'ip' => $ip
    158             );
    159             $wpdb->update( $table, $data, $where );
     157            $data  = array(
     158                'counter'   => $counter,
     159                'blockdate' => $date
     160            );
     161            $where = array(
     162                'ip' => $ip
     163            );
     164            $wpdb->update( $table, $data, $where );
    160165
    161         } else {
    162             $data = array(
    163                 'ip'        => $ip,
    164                 'counter'   => 1,
    165                 'blockdate' => $date
    166             );
    167             $wpdb->insert( $table, $data );
    168         }
    169     }
     166        } else {
     167            $data = array(
     168                'ip'        => $ip,
     169                'counter'   => 1,
     170                'blockdate' => $date
     171            );
     172            $wpdb->insert( $table, $data );
     173        }
     174    }
    170175}
    171 add_action('wp_login_failed', 'senpai_software_2fa_login_failed');
     176add_action('wp_login_failed', __NAMESPACE__.'\login_failed');
  • senpai-software-2fa/trunk/senpai-software-2fa.php

    r2938444 r2977661  
    44 * Plugin URI:        https://senpai.software/wp-plugins/2fa/
    55 * Description:       Unique method two-factor auth (2FA). Limit Login Attempts. Disable XML-RPC. Protection against brute force attacks.
    6  * Version:           2.0.0
     6 * Version:           2.0.1
    77 * Author:            Senpai Software
    88 * Author URI:        https://senpai.software
     
    1313 * Requires at least: 5.0
    1414 * Requires PHP:      5.6
    15 */
     15 */
    1616
    1717/*
     
    3131*/
    3232
     33namespace SenpaiSoftware2FA;
     34
    3335if (!defined('ABSPATH')) {
    3436    exit; // Exit if accessed directly
     
    3840 * The code that runs during plugin activation.
    3941 */
    40 function senpai_software_2fa_activation(){
     42function activation(){
    4143    global $wpdb;
    4244    $users = get_users();
     
    5052 * The code that runs during plugin deactivation.
    5153 */
    52 function senpai_software_2fa_deactivation(){
     54function deactivation(){
    5355    global $wpdb;
    5456    $users = get_users();
     
    6264 * The code that runs during plugin uninstall.
    6365 */
    64 function senpai_software_2fa_uninstall(){
     66function uninstall(){
    6567    global $wpdb;
    6668    $users = get_users();
     
    7072    }
    7173
    72     delete_option( 'snp_2fa_xmlrpc');
    73     delete_option( 'snp_2fa_hint');
    74     delete_option( 'snp_2fa_attempts');
    75     delete_option( 'snp_2fa_block_period');
     74    delete_option( 'snp_2fa_xmlrpc');
     75    delete_option( 'snp_2fa_hint');
     76    delete_option( 'snp_2fa_attempts');
     77    delete_option( 'snp_2fa_block_period');
    7678
    77     $table_name = $wpdb->prefix . 'snp_2fa_ip';
    78     $query = "DROP TABLE IF EXISTS $table_name";
    79     $wpdb->query($query);
     79    $table_name = $wpdb->prefix . 'snp_2fa_ip';
     80    $query = "DROP TABLE IF EXISTS $table_name";
     81    $wpdb->query($query);
    8082}
    8183
    82 register_activation_hook(__FILE__, 'senpai_software_2fa_activation');
    83 register_deactivation_hook(__FILE__, 'senpai_software_2fa_deactivation');
    84 register_uninstall_hook(__FILE__, 'senpai_software_2fa_uninstall');
     84register_activation_hook(__FILE__, __NAMESPACE__.'\activation');
     85register_deactivation_hook(__FILE__, __NAMESPACE__.'\deactivation');
     86register_uninstall_hook(__FILE__, __NAMESPACE__.'\uninstall');
    8587
    8688/**
    8789 * Languages
    8890 */
    89 function senpai_software_2fa_load_textdomain() {
     91function load_textdomain() {
    9092    load_plugin_textdomain( 'senpai-software-2fa', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
    9193}
    92 add_action( 'plugins_loaded', 'senpai_software_2fa_load_textdomain' );
     94add_action( 'plugins_loaded', __NAMESPACE__.'\load_textdomain' );
    9395
    9496/**
Note: See TracChangeset for help on using the changeset viewer.