Plugin Directory

Changeset 2023633


Ignore:
Timestamp:
02/02/2019 06:09:05 AM (7 years ago)
Author:
manishkrag
Message:
  • Tested with upto WP 5.0.x
  • Take any combination of letters, numbers and (_) with first character always alphanumeric.
  • Check first character is always alphanumeric or not.
  • Blank submission of custom table prefix will change prefix with randomly generated 5 characters and a message appears that custom table prefix can not be empty.
Location:
change-table-prefix/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • change-table-prefix/trunk/change-table-prefix.php

    r1413299 r2023633  
    44 *Description: This plug-in will allow you to change your database prefix after installation.
    55 *Author: Manish Kumar Agarwal
    6  *EmailId: manishkrag@yahoo.co.in/skype:mfsi_manish
    7  *Version: 1.4
     6 *EmailId: manishkrag@yahoo.co.in/skype:soumibgb
     7 *Version: 2.0
    88 */
    99
     
    8282            <span class="italic">Would you like to your own custom prefix.</span>
    8383            <br />
    84             <input class="hide" id="new-prefix" type="text" name="new-prefix" value="" placeholder="Enter Custom Prefix"/>
     84            <input class="hide" id="new-prefix" type="text" name="new-prefix" pattern="[a-zA-Z0-9_]+" value="" placeholder="Enter Custom Prefix"/><span style="color: #ff0000;">It contains only combinations of lowercase letters (a-z), uppercase letters (A-Z), numbers (0-9) or underscore(_) and first character is always alphanumeric.</span>
    8585            <br />
    8686            <input id="change-prefix" type="submit" name="change-prefix-do" value="Click To Change Table Prefix"/>
     
    103103        if ( isset( $_POST['checkbox-prefix'] ) && ( $_POST['checkbox-prefix'] == 1 ) ) {
    104104            $table_new_prefix = wp_strip_all_tags( trim( $_POST['new-prefix'] ) );
     105            if( '' == $table_new_prefix  ) {
     106                echo "<p class='message'>Custom table prefix can not be empty.</p>";
     107                $characters = 'abcdefghijklmnopqrstuvwxyz';
     108
     109                //Length of the table prefix
     110                $random_string_length = 5;
     111                $string = '';
     112
     113                //Generate random string
     114                for ( $i = 0; $i < $random_string_length; $i++ ) {
     115                    $string .= $characters[rand(0, strlen($characters) - 1)];
     116                }
     117
     118                //Append '_' with the newly generated string
     119                $table_new_prefix = $string . '_';
     120            }
     121
    105122            global $wpdb;
    106123            $error = $wpdb->set_prefix( $table_new_prefix );
     
    124141            $table_new_prefix = $string . '_';
    125142        }
    126        
    127         echo "<p class='success'>Your new table prefix is: <b>", $table_new_prefix, "</b></p>";
    128        
    129         //Get the table resource
    130         $get_tables_sql = 'SHOW TABLES FROM ' . DB_NAME;
    131         $old_tables = $wpdb->get_results( $get_tables_sql, ARRAY_N );
    132        
    133         //Count the number of tables
    134         $table_count = 0;
    135 
    136         //Rename all the tables name
    137         foreach ( $old_tables as $old_table ) {
    138            
    139             //Get table name with old prefix
    140             $table_old_name = $old_table[0];
    141            
    142             if ( strpos( $table_old_name, $old_table_prefix ) === 0 ) {
    143                
    144                 //Get table name with new prefix
    145                 $table_new_name = $table_new_prefix . substr( $table_old_name, $old_prefix_length );
    146                
    147                 //Write query to rename tables name
    148                 $sql = "RENAME TABLE `$table_old_name` TO `$table_new_name`";
    149                
    150                 //Execute the query
    151                 if ( false === $wpdb->query( $sql ) ) {
    152                     $error = 1;
    153                     echo "<p class='error'>", $table_old_name, " table name not updated.</p>";
     143
     144        //Check whether first character is alphanumeric or not
     145        if (preg_match('/^[a-zA-Z0-9]+$/', $table_new_prefix['0'])) {
     146           
     147            echo "<p class='success'>Your new table prefix is: <b>", $table_new_prefix, "</b></p>";
     148           
     149            //Get the table resource
     150            $get_tables_sql = 'SHOW TABLES FROM ' . DB_NAME;
     151            $old_tables = $wpdb->get_results( $get_tables_sql, ARRAY_N );
     152           
     153            //Count the number of tables
     154            $table_count = 0;
     155
     156            //Rename all the tables name
     157            foreach ( $old_tables as $old_table ) {
     158               
     159                //Get table name with old prefix
     160                $table_old_name = $old_table[0];
     161
     162                if ( strpos( $table_old_name, $old_table_prefix ) === 0 ) {
     163                    //Get table name with new prefix
     164                    $table_new_name = $table_new_prefix . substr( $table_old_name, $old_prefix_length );
     165
     166                    //Write query to rename tables name
     167                    $sql = "RENAME TABLE `$table_old_name` TO `$table_new_name`";
     168
     169                    //Execute the query
     170                    if ( false === $wpdb->query( $sql ) ) {
     171                        $error = 1;
     172                        echo "<p class='error'>", $table_old_name, " table name not updated.</p>";
     173                    } else {
     174                        $table_count++;
     175                    }
    154176                } else {
    155                     $table_count++;
    156                 }
     177                    continue;
     178                }
     179            }
     180            if ( @$error == 1 ) {
     181                echo "<p class='error'>Please change the above tables prefix to ", $table_new_prefix, " manualy.</p>";
    157182            } else {
    158                 continue;
    159             }
     183                echo "<p class='success'>", $table_count, " tables prefix updated successfully.</p>";
     184            }
     185
     186            //Update the wp-config.php file
     187            $path = '../wp-config.php';
     188            $configFile = file( $path );
     189            foreach ( $configFile as $line_num => $line ) {
     190                switch ( substr( $line, 0, 16 ) ) {
     191                    case '$table_prefix  =':
     192                        $configFile[$line_num] = str_replace( $old_table_prefix, $table_new_prefix, $line );
     193                        break;
     194                }
     195            }
     196
     197            //making the the config readable to change the prefix
     198            @chmod( $path, 0777 );
     199            if ( is_writeable( $path ) ) {
     200                $handle = fopen( $path, 'w' );
     201                foreach ( $configFile as $line ) {
     202                    fwrite( $handle, $line );
     203                }
     204                fclose( $handle );
     205               
     206                echo '<p class="success">wp-config.php file updated successfully.</p>';
     207            } else {
     208                echo "<p class='error'>File Not Writeable: Please open wp-config.php file in your favurite editor and search
     209                      for variable", $table_prefix, " and assign ", $table_new_prefix, " to the same variable.";
     210            }
     211
     212            //Create query to update option table
     213            $update_option_table_query = "UPDATE " . $table_new_prefix . "options
     214                                          SET option_name='" . $table_new_prefix . "user_roles'
     215                                          WHERE option_name='" . $old_table_prefix . "user_roles'
     216                                          LIMIT 1";
     217           
     218            //Execute the update query to update option table user_roles row
     219            if ( false === $wpdb->query($update_option_table_query) ) {
     220                echo "<p class='error'>Changing value: ",
     221                     $old_table_prefix,
     222                     "user_roles in table ",
     223                     $table_new_prefix,
     224                     "options to  ",
     225                     $table_new_prefix,
     226                     "user_roles</p>";
     227                     
     228                echo "<p class='error'>End of updating options table data with above error.</p>";
     229            } else {
     230                echo "<p class='success'>Updated options table data successfully.</p>";
     231            }
     232
     233            //Create query to update user_meta table
     234            $custom_sql = "SELECT user_id, meta_key
     235                            FROM " . $table_new_prefix . "usermeta
     236                            WHERE meta_key
     237                            LIKE '" . $old_table_prefix . "%'";
     238
     239            $meta_keys = $wpdb->get_results( $custom_sql );
     240
     241            //Update all the meta_key field value which having the old table prefix in user_meta table
     242            foreach ( $meta_keys as $meta_key ) {
     243               
     244                //Create new meta key
     245                $new_meta_key = $table_new_prefix . substr( $meta_key->meta_key, $old_prefix_length );
     246               
     247                $update_user_meta_sql = "UPDATE " . $table_new_prefix . "usermeta
     248                                        SET meta_key='" . $new_meta_key . "'
     249                                        WHERE meta_key='" . $meta_key->meta_key . "'
     250                                        AND user_id=" . $meta_key->user_id;
     251               
     252                $wpdb->query($update_user_meta_sql);
     253           
     254            }
     255            echo "<p class='success'>Updated usermeta table data successfully.</p>";
    160256        }
    161         if ( @$error == 1 ) {
    162             echo "<p class='error'>Please change the above tables prefix to ", $table_new_prefix, " manualy.</p>";
    163         } else {
    164             echo "<p class='success'>", $table_count, " tables prefix updated successfully.</p>";
    165         }
    166        
    167         //Update the wp-config.php file
    168         $path = '../wp-config.php';
    169         $configFile = file( $path );
    170         foreach ( $configFile as $line_num => $line ) {
    171             switch ( substr( $line, 0, 16 ) ) {
    172                 case '$table_prefix  =':
    173                     $configFile[$line_num] = str_replace( $old_table_prefix, $table_new_prefix, $line );
    174                     break;
    175             }
    176         }
    177        
    178         //making the the config readable to change the prefix
    179         @chmod( $path, 0777 );
    180         if ( is_writeable( $path ) ) {
    181             $handle = fopen( $path, 'w' );
    182             foreach ( $configFile as $line ) {
    183                 fwrite( $handle, $line );
    184             }
    185             fclose( $handle );
    186            
    187             echo '<p class="success">wp-config.php file updated successfully.</p>';
    188         } else {
    189             echo "<p class='error'>File Not Writeable: Please open wp-config.php file in your favurite editor and search
    190                   for variable", $table_prefix, " and assign ", $table_new_prefix, " to the same variable.";
    191         }
    192        
    193         //Create query to update option table
    194         $update_option_table_query = "UPDATE " . $table_new_prefix . "options
    195                                       SET option_name='" . $table_new_prefix . "user_roles'
    196                                       WHERE option_name='" . $old_table_prefix . "user_roles'
    197                                       LIMIT 1";
    198        
    199         //Execute the update query to update option table user_roles row
    200         if ( false === $wpdb->query($update_option_table_query) ) {
    201             echo "<p class='error'>Changing value: ",
    202                  $old_table_prefix,
    203                  "user_roles in table ",
    204                  $table_new_prefix,
    205                  "options to  ",
    206                  $table_new_prefix,
    207                  "user_roles</p>";
    208                  
    209             echo "<p class='error'>End of updating options table data with above error.</p>";
    210         } else {
    211        
    212        
    213        
    214             echo "<p class='success'>Updated options table data successfully.</p>";
    215         }
    216        
    217        
    218        
    219         //Create query to update user_meta table
    220         $custom_sql = "SELECT user_id, meta_key
    221                         FROM " . $table_new_prefix . "usermeta
    222                         WHERE meta_key
    223                         LIKE '" . $old_table_prefix . "%'";
    224 
    225         $meta_keys = $wpdb->get_results( $custom_sql );
    226        
    227         //Update all the meta_key field value which having the old table prefix in user_meta table
    228         foreach ( $meta_keys as $meta_key ) {
    229            
    230             //Create new meta key
    231             $new_meta_key = $table_new_prefix . substr( $meta_key->meta_key, $old_prefix_length );
    232            
    233             $update_user_meta_sql = "UPDATE " . $table_new_prefix . "usermeta
    234                                     SET meta_key='" . $new_meta_key . "'
    235                                     WHERE meta_key='" . $meta_key->meta_key . "'
    236                                     AND user_id=" . $meta_key->user_id;
    237            
    238             $wpdb->query($update_user_meta_sql);
    239        
    240         }
    241         echo "<p class='success'>Updated usermeta table data successfully.</p>";
     257        else
     258            echo "<p class='error'>First character is always alphanumeric.</p>";
    242259    }
    243260}
     
    247264    // Activate WordPress Maintenance Mode
    248265    function wp_maintenance_mode(){
    249         if(!current_user_can('edit_themes') || !is_user_logged_in()){
    250             wp_die('<h1 style="color:red">Website under Maintenance</h1><br />We are performing scheduled maintenance, will be back shortly!');
    251         }
     266        if ( !current_user_can('edit_themes') || !is_user_logged_in() ){
     267            wp_die('<h1 style="color:red">Website under Maintenance</h1><br />We are performing scheduled maintenance, will be back shortly!');
     268        }
    252269    }
    253270   
     
    255272    $is_maintenance_mode = get_option('ctp_maintenance_enabled');
    256273   
    257      if( 1 == $is_maintenance_mode) {
    258         add_action('get_header', 'wp_maintenance_mode');
     274    if ( 1 == $is_maintenance_mode) {
     275        add_action( 'get_header', 'wp_maintenance_mode' );
    259276    }
    260    
     277
    261278    function ctp_maintenance_message($type) {
    262     switch($type) {
    263         case 'maintenance_message':
    264             $default = "<h1>Website Under Maintenance</h1><p>Our Website is currently undergoing scheduled maintenance. Please check back soon.</p>";
    265             break;
    266         case 'maintence_mode_enabled' :
    267             $default = "Maintenance Mode is currently active. To make sure that it works, open your web page in different browser or simply log out. Logged in users are not affected by the Maintenance Mode.";
    268             break;
    269         default:
    270             $default = false;
    271             break;
    272     }
    273 
    274     return $default;
    275 }
     279        switch($type) {
     280            case 'maintenance_message':
     281            $default = "<h1>Website Under Maintenance</h1><p>Our Website is currently undergoing scheduled maintenance. Please check back soon.</p>";
     282            break;
     283            case 'maintence_mode_enabled' :
     284            $default = "Maintenance Mode is currently active. To make sure that it works, open your web page in different browser or simply log out. Logged in users are not affected by the Maintenance Mode.";
     285            break;
     286            default:
     287            $default = false;
     288            break;
     289        }
     290
     291        return $default;
     292    }
    276293
    277294// Deactive the plugin & remove the option
  • change-table-prefix/trunk/css/ctp-style.css

    r593827 r2023633  
    3030    font-family: italic;
    3131}
     32.message {
     33    color: #0066ff;
     34}
  • change-table-prefix/trunk/readme.txt

    r1413299 r2023633  
    44Tags: Prefix, database prefix,wp_,table prefix, random table prefix, custom table prefix, WordPress table prefix, change WordPress table prefix, WordPress database security, how to increase WordPress DB security
    55Requires at least: 3.0
    6 Tested up to: 4.5.2
    7 Stable tag: 1.4
     6Tested up to: 5.0
     7Stable tag: 2.0
    88
    99Increase Your Website Database Security
     
    3333
    3434== Changelog ==
     35= 2.0 =
     36* Tested with upto WP 5.0.x
     37* Take any combination of letters, numbers and (_) with first character always alphanumeric.
     38* Check first character is always alphanumeric or not.
     39* Blank submission of custom table prefix will change prefix with randomly generated 5 characters and a message appears that custom table prefix can not be empty.
    3540= 1.4 =
    3641* Tested with upto WP 4.5.2
Note: See TracChangeset for help on using the changeset viewer.