Skip to content

Commit

Permalink
Add WordPress configuration and salt generation methods
Browse files Browse the repository at this point in the history
Three utility functions are introduced in utils.js to enhance security and ease of use for WordPress installations. replaceDbConstant(configContent, constantName, userDefinedValue) replaces a constant in wp-config.php with a user-defined value. generateSalt() creates a random salt code, and replaceEmptySalts(configContent) replaces empty salt place holders in the wp-config.php with generated salt codes. These functions allow users to easily replace database constants and salts in WordPress configurations without having to manually edit wp-config.php.

The exported functions of utils.js were also updated to incorporate these new methods.

In the Package class, the way of updating WordPress configurations was changed to use these new utility functions, making the code cleaner and more readable.

wp-package.json was modified to change the name, DB_NAME, DB_USER, DB_PASSWORD, and DB_HOST to more meaningful, user-defined values.

New scripts field was also added to package.json to standardize the entry point for the application.

The readme.md file has been updated to reflect these changes, with the version number incremented accordingly.

Overall, this commit improves the security and usability of the tool, by allowing automatic generation and configuration of constants and salts in the wp-config.php file.
  • Loading branch information
erikyo committed Dec 1, 2023
1 parent b555912 commit 2f71fc2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 16 deletions.
9 changes: 8 additions & 1 deletion lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ class WordPressInstaller {
async installPackages () {
const { wordpress, plugins, themes } = this.config;

// Create temp folder
makeDir(this.tempDir);

const defaultPaths = { rootFolder: this.rootFolder, tempDir: this.tempDir, baseFolder: this.baseFolder, destFolder: this.baseFolder };
// the default paths for the packages
const defaultPaths = {
rootFolder: this.rootFolder,
tempDir: this.tempDir,
baseFolder: this.baseFolder,
destFolder: this.baseFolder
};

if (wordpress) {
const wpPackage = new WordPressPackage(this.config, defaultPaths);
Expand Down
18 changes: 11 additions & 7 deletions lib/package.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const {exec} = require('child_process');
const {
downloadFile,
extractZip,
getDownloadUrl,
getWordPressDownloadUrl,
installNpmPackages,
renameFolder
renameFolder,
replaceDbConstant,
replaceEmptySalts
} = require('./utils');

class Package {
Expand Down Expand Up @@ -188,11 +190,13 @@ class WordPressPackage extends Package {
let configContent = fs.readFileSync(configPath, 'utf8');

// Update database name, username, password, and other settings based on user-defined config
configContent = configContent.replace(/database_name_here/, this.config.wordpress.config.DB_NAME);
configContent = configContent.replace(/username_here/, this.config.wordpress.config.DB_USER);
configContent = configContent.replace(/password_here/, this.config.wordpress.config.DB_PASSWORD);
configContent = configContent.replace(/localhost/, this.config.wordpress.config.DB_HOST);
configContent = configContent.replace(/utf8/, this.config.wordpress.config.DB_CHARSET);
configContent = replaceDbConstant(configContent, 'DB_NAME', this.config.wordpress.config.DB_NAME);
configContent = replaceDbConstant(configContent, 'DB_USER', this.config.wordpress.config.DB_USER);
configContent = replaceDbConstant(configContent, 'DB_PASSWORD', this.config.wordpress.config.DB_PASSWORD);
configContent = replaceDbConstant(configContent, 'DB_HOST', this.config.wordpress.config.DB_HOST);
configContent = replaceDbConstant(configContent, 'DB_CHARSET', this.config.wordpress.config.DB_CHARSET);

configContent = replaceEmptySalts(configContent);

// Write the updated content back to wp-config.php
fs.writeFileSync(configPath, configContent, 'utf8');
Expand Down
55 changes: 54 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,57 @@ async function installNpmPackages (packageDirectory) {
});
}

/**
* Replaces a constant in the wp-config.php file with a user-defined value.
*
* @param {string} configContent - The content of the wp-config.php file.
* @param {string} constantName - The name of the constant to replace.
* @param {string} userDefinedValue - The user-defined value to set for the constant.
* @return {string} - The updated content with the replaced constant.
*/
function replaceDbConstant(configContent, constantName, userDefinedValue) {
const regex = new RegExp(`define\\(\\s*'${constantName}'\\s*,\\s*'[^']*'\\s*\\);`);
return configContent.replace(regex, `define( '${constantName}', '${userDefinedValue}' );`);
}

/**
* Generates a random salt code for WordPress configuration.
*
* @return {string} - The generated salt code.
*/
function generateSalt() {
const charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/';
const saltLength = 64;
return Array.from({ length: saltLength }, () => charset[Math.floor(Math.random() * charset.length)]).join('');
}

/**
* Replaces empty salts in the WordPress configuration with generated salt codes.
*
* @param {string} configContent - The content of the wp-config.php file.
* @return {string} - The updated content with replaced salts.
*/
function replaceEmptySalts(configContent) {
const saltConstants = [
'AUTH_KEY',
'SECURE_AUTH_KEY',
'LOGGED_IN_KEY',
'NONCE_KEY',
'AUTH_SALT',
'SECURE_AUTH_SALT',
'LOGGED_IN_SALT',
'NONCE_SALT',
];

saltConstants.forEach((constant) => {
const emptySaltRegex = new RegExp(`define\\(\\s*'${constant}'\\s*,\\s*'put your unique phrase here'\\s*\\);`);
const generatedSalt = generateSalt();
configContent = configContent.replace(emptySaltRegex, `define( '${constant}', '${generatedSalt}' );`);
});

return configContent;
}

module.exports = {
getConfig,
makeDir,
Expand All @@ -233,5 +284,7 @@ module.exports = {
getWordPressDownloadUrl,
getDownloadUrl,
extractZip,
installNpmPackages
installNpmPackages,
replaceDbConstant,
replaceEmptySalts,
};
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Edit the `wp-package.json` file to define the WordPress version, language, theme

```json
{
"name": "modul-r-blog",
"name": "my-blog",
"wordpress": {
"version": "5.8.1",
"version": "6.4.1",
"language": "en_US",
"config": {
"DB_NAME": "your_database_name",
Expand Down
10 changes: 5 additions & 5 deletions wp-package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "modul-r-blog",
"name": "wordpress",
"wordpress": {
"version": "6.4.1",
"language": "en_US",
"config": {
"DB_NAME": "your_database_name",
"DB_USER": "your_database_user",
"DB_PASSWORD": "your_database_password",
"DB_HOST": "localhost",
"DB_NAME": "my_db_name",
"DB_USER": "my_username",
"DB_PASSWORD": "my_password",
"DB_HOST": "127.0.0.1",
"DB_CHARSET": "utf8",
"DB_COLLATE": "",
"table_prefix": "wp_",
Expand Down

0 comments on commit 2f71fc2

Please sign in to comment.