Created
February 18, 2017 18:13
-
-
Save deadbok/18d89ce33777ef5c922299372eb69bd3 to your computer and use it in GitHub Desktop.
Python class to work with Wordpress wp-config.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Manipulate a wp-config.php file. | |
MIT License | |
Copyright (c) 2017 Martin Bo Kristensen Grønholdt | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE | |
""" | |
import string | |
import random | |
import base64 | |
class WPConfig(object): | |
def __init__(self): | |
# Set the initial value of all the fields in the config file, that this can change, | |
self.db_name = '' | |
self.db_user = '' | |
self.db_passwd = '' | |
self.db_host = '' | |
self.db_charset = '' | |
self.db_collate = '' | |
self.auth_key = '' | |
self.secure_auth_key = '' | |
self.logged_in_key = '' | |
self.nonce_key = '' | |
self.auth_salt = '' | |
self.secure_auth_salt = '' | |
self.logged_in_salt = '' | |
self.nonce_salt = '' | |
self.table_prefix = '' | |
# This is were a raw config file, read from disk is kept. | |
self.raw_config = '' | |
def _parse_value(self, start): | |
""" | |
Read a config value with define PHP syntax. | |
""" | |
start = start.replace(' ', '') | |
start = start.replace('\t', '') | |
# Run through every line. | |
for i in range(0, len(self.raw_config)): | |
# Remove spaces and tabs | |
line = self.raw_config[i].replace(' ', '') | |
line = line.replace('\t', '') | |
if line.startswith(start): | |
# We found the value. | |
parts = line[len(start) - 1:-1].split("'") | |
return parts[1] | |
return False | |
def _generate_salt(self, length=64): | |
""" | |
Generate a new random salt. | |
:param length: Length of the random string. | |
:return: | |
""" | |
chars = string.letters + string.digits + string.punctuation | |
return base64.b64encode(''.join((random.choice(chars)) for x in range(length))) | |
def read_config(self, path): | |
""" | |
Read configuration values from a wp-config.php file. | |
:param path: Path to the configuration file. | |
:return: True on success. | |
""" | |
try: | |
with open(path, mode='r') as config_file: | |
self.raw_config = config_file.readlines() | |
except: | |
print('Error loading configuration file: ' + path) | |
return False | |
value = self._parse_value("define('DB_NAME', '") | |
if value != False: | |
self.db_name = value | |
else: | |
return False | |
value = self._parse_value("define('DB_USER', '") | |
if value != False: | |
self.db_user = value | |
else: | |
return False | |
value = self._parse_value("define('DB_PASSWORD', '") | |
if value != False: | |
self.db_passwd = value | |
else: | |
return False | |
value = self._parse_value("define('DB_HOST', '") | |
if value != False: | |
self.db_host = value | |
else: | |
return False | |
value = self._parse_value("define('DB_CHARSET', '") | |
if value != False: | |
self.db_charset = value | |
else: | |
return False | |
value = self._parse_value("define('DB_COLLATE', '") | |
if value != False: | |
self.db_collate = value | |
else: | |
return False | |
value = self._parse_value("define('AUTH_KEY', '") | |
if value != False: | |
self.auth_key = value | |
else: | |
return False | |
value = self._parse_value("define('SECURE_AUTH_KEY', '") | |
if value != False: | |
self.secure_auth_key = value | |
else: | |
return False | |
value = self._parse_value("define('LOGGED_IN_KEY', '") | |
if value != False: | |
self.logged_in_key = value | |
else: | |
return False | |
value = self._parse_value("define('NONCE_KEY', '") | |
if value != False: | |
self.nonce_key = value | |
else: | |
return False | |
value = self._parse_value("define('AUTH_SALT', '") | |
if value != False: | |
self.auth_salt = value | |
else: | |
return False | |
value = self._parse_value("define('SECURE_AUTH_SALT', '") | |
if value != False: | |
self.secure_auth_salt = value | |
else: | |
return False | |
value = self._parse_value("define('LOGGED_IN_SALT', '") | |
if value != False: | |
self.logged_in_salt = value | |
else: | |
return False | |
value = self._parse_value("define('NONCE_SALT', '") | |
if value != False: | |
self.nonce_salt = value | |
else: | |
return False | |
value = self._parse_value("$table_prefix='") | |
if value != False: | |
self.table_prefix = value | |
else: | |
return False | |
return True | |
def write_config(self, path): | |
""" | |
Write the configuration file to path, using the values stored in ths object. | |
:param path: Path to the configuration file. | |
:return: True on success. | |
""" | |
config = """ | |
<?php | |
define('DB_NAME', '{0}'); | |
define('DB_USER', '{1}'); | |
define('DB_PASSWORD', '{2}'); | |
define('DB_HOST', '{3}'); | |
define('DB_CHARSET', '{4}'); | |
define('DB_COLLATE', '{5}'); | |
define('AUTH_KEY', '{6}'); | |
define('SECURE_AUTH_KEY', '{7}'); | |
define('LOGGED_IN_KEY', '{8}'); | |
define('NONCE_KEY', '{9}'); | |
define('AUTH_SALT', '{10}'); | |
define('SECURE_AUTH_SALT', '{11}'); | |
define('LOGGED_IN_SALT', '{11}'); | |
define('NONCE_SALT', '{12}'); | |
$table_prefix = '{13}'; | |
define('WP_DEBUG', false); | |
if ( !defined('ABSPATH') ) | |
define('ABSPATH', dirname(__FILE__) . '/'); | |
require_once(ABSPATH . 'wp-settings.php'); | |
""".format(self.db_name, | |
self.db_user, | |
self.db_passwd, | |
self.db_host, | |
self.db_charset, | |
self.db_collate, | |
self.auth_key, | |
self.secure_auth_key, | |
self.logged_in_key, | |
self.nonce_key, | |
self.auth_salt, | |
self.logged_in_salt, | |
self.nonce_salt, | |
self.table_prefix) | |
with open(path, mode='w') as config_file: | |
config_file.write(config) | |
def replace_salts(self): | |
""" | |
Generate new salts for the config. | |
""" | |
self.auth_key = self._generate_salt() | |
self.secure_auth_key = self._generate_salt() | |
self.logged_in_key = self._generate_salt() | |
self.nonce_key = self._generate_salt() | |
self.auth_salt = self._generate_salt() | |
self.logged_in_salt = self._generate_salt() | |
self.nonce_salt = self._generate_salt() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment