Using Regex with Search & Replace to update WordPress autoinstall perl script with data from webpage

StackOverflow https://stackoverflow.com/questions/22735600

  •  23-06-2023
  •  | 
  •  

Question

I'm not coder but have been trying to modify a script by Chris Gilligan which automates WordPress installations on Virtualmin (A Webmin module similar to cPanel) servers.

Chris' script is described in detail on his site and works really well. It can be viewed by downloading it from here

I've read the Virtualmin scripting guide at bit.ly/1dYlL81 and various tutorials on perl but I'm just not getting anywhere.

I have also asked Chris, the script author, himself and he has suggested I ask here as he doesn't quite know how to achieve this either.

The two things I would like to add to the script are related to the standard WordPress wp-config.php file:

  1. Chris' script doesn't generate the random keys/salts and leaves that section of the file intact so it looks like:

    define('AUTH_KEY',         'put your unique phrase here');
    define('SECURE_AUTH_KEY',  'put your unique phrase here');
    define('LOGGED_IN_KEY',    'put your unique phrase here');
    define('NONCE_KEY',        'put your unique phrase here');
    define('AUTH_SALT',        'put your unique phrase here');
    define('SECURE_AUTH_SALT', 'put your unique phrase here');
    define('LOGGED_IN_SALT',   'put your unique phrase here');
    define('NONCE_SALT',       'put your unique phrase here');
    

How can I modify the script to it replaces the above with the output from the WordPress key/generator webpage at http://api.wordpress.org/secret-key/1.1/salt

  1. The script leaves the MySQL database tables prefix as:

    $table_prefix  = 'wp_';
    

but I would like this to be replaced with a slightly more random 'wp_???_' where ??? is a random three-character alphanumeric string. What would be the necessary modification to the script to achieve that?

Was it helpful?

Solution

Just for your information this is not tested code so don't hold it against me but here is a thought on how you may be able to achieve what you are looking for.

After line 220 you could try to do something like this

if ($l =~ /^define\('AUTH_KEY',/) { $l = ""; }
if ($l =~ /^define\('SECURE_AUTH_KEY',/) { $l = ""; }
if ($l =~ /^define\('LOGGED_IN_KEY',/) { $l = ""; }
if ($l =~ /^define\('NONCE_KEY',/) { $l = ""; }
if ($l =~ /^define\('AUTH_SALT',/) { $l = ""; }
if ($l =~ /^define\('SECURE_AUTH_SALT',/) { $l = ""; }
if ($l =~ /^define\('LOGGED_IN_SALT',/) { $l = ""; }
if ($l =~ /^define\('NONCE_SALT',/) {  
  use LWP::Simple;
  my $salt_info = get('http://api.wordpress.org/secret-key/1.1/salt')
    or die 'Unable to get salt info';
  $l = $salt_info;
}                                      
if ($l =~ /^\$table_prefix/) {
  my @chars = ('0'..'9', 'A'..'Z', 'a'..'z');
  my $len = 3;

  my $wp_random;
  while ($len--) {
          $wp_random .= $chars[rand @chars];
  };                                    
  $wp_db_prefix = "wp_" . $wp_random . "_";
  $l = "\$table_prefix  = '".$wp_db_prefix."';";
}

You could also do something like this to ensure that you may be able to pull information prior to making the changes in a test.pl file and run perl test.pl

#!/usr/bin/perl  
print "LWP Version: ". LWP->VERSION;     

print "\n\nSalt Info: \n";
use LWP::Simple;
my $salt_info = get('http://api.wordpress.org/secret-key/1.1/salt')
        or die 'Unable to get salt info';
print $salt_info;

my @chars = ('0'..'9', 'A'..'Z', 'a'..'z');
my $len = 3;

my $wp_random;
while ($len--) {
        $wp_random .= $chars[rand @chars];
};

print "\n\nRandom String: $wp_random \n\n";

My old answer did not consider removing the old values of the salt keys as well as if statement for the table_prefix and rndStr not functioning properly.

if ($l =~ /^define\('DB_COLLATE',/) { 
use strict;
use warnings;
use LWP::Simple;

  my $salt_info = get('http://api.wordpress.org/secret-key/1.1/salt') or die 'Unable to get salt info';
    $l = "('DB_COLLATE', '');\n\n" . $salt_info;
}

if ($l =~ /^\\$table_prefix/) {   
     $wp_random = rndStr 3, 'a'..'z', 0..9;
     $wp_db_prefix = "wp_" . $wp_random . "_";
     $l = "\\$table_prefix  = '".
             $wp_db_prefix."';";

  }  
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top