File: //homepages/oneclick/WordPress/6.9/601/scripts/core/wpconfigreader.php
<?php
namespace Core;
use \Exceptions\ConfigurationException;
class WPConfigReader
{
/**
* basically at least: strlen(<?php require('c.php');)
*/
const MIN_LENGTH_CONFIGURATION = 25;
/**
* default wp config filename
*/
const DEFAULT_FILENAME = 'wp-config.php';
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $fileName;
/**
* @var string
*/
protected $content;
/**
* @param string $pathToWPConfig
* @param string $fileName
* @throws ConfigurationException
*/
public function __construct($pathToWPConfig, $fileName = self::DEFAULT_FILENAME)
{
$this->setPathToWPConfig($pathToWPConfig, $fileName);
}
/**
* @throws ConfigurationException
*/
public function readConfig()
{
$this->content = file_get_contents($this->path . $this->fileName);
if (strlen(trim($this->content)) < self::MIN_LENGTH_CONFIGURATION) {
throw ConfigurationException::createException(ConfigurationException::CONFIG_EMPTY, $this->path);
}
}
/**
* @param string $pathToWPConfig
* @param string $fileName
* @throws ConfigurationException
*/
public function setPathToWPConfig($pathToWPConfig, $fileName = self::DEFAULT_FILENAME)
{
$pathToWPConfig = rtrim($pathToWPConfig,'/') . '/';
if (!is_dir($pathToWPConfig)) {
throw ConfigurationException::createException(ConfigurationException::CONFIG_PATH_NOT_EXISTING, $pathToWPConfig);
}
if (!file_exists($pathToWPConfig . $fileName)) {
throw ConfigurationException::createException(ConfigurationException::CONFIG_NOT_FOUND, $pathToWPConfig);
}
$this->path = $pathToWPConfig;
$this->fileName = $fileName;
}
/**
* e.g.: getConfigurationConstant('DB_NAME') => 'wordpress'
*
* @param string $name
* @return string
*/
public function getConfigurationConstant($name)
{
if (empty($this->content)) {
$this->readConfig();
}
$pattern = sprintf('#define\(.+%s.+,(.+)\);#', preg_quote($name, '#'));
if (preg_match($pattern, $this->content, $res)) {
return trim($res[1],' "\'');
}
return null;
}
/**
* e.g. getVariableValue('table_prefix') => 'wp_'
*
* @param string $name ( without leading $ )
* @return string
*/
public function getVariableValue($name)
{
if (empty($this->content)) {
$this->readConfig();
}
$pattern = sprintf('#\$%s.+=(.+);#', preg_quote($name, '#'));
if (preg_match($pattern, $this->content, $res)) {
return trim($res[1],' "\'');
}
return null;
}
}