1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb;
15
16class config_php_file
17{
18	/** @var string phpBB Root Path */
19	protected $phpbb_root_path;
20
21	/** @var string php file extension  */
22	protected $php_ext;
23
24	/**
25	* Indicates whether the php config file has been loaded.
26	*
27	* @var bool
28	*/
29	protected $config_loaded = false;
30
31	/**
32	* The content of the php config file
33	*
34	* @var array
35	*/
36	protected $config_data = array();
37
38	/**
39	* The path to the config file. (Default: $phpbb_root_path . 'config.' . $php_ext)
40	*
41	* @var string
42	*/
43	protected $config_file;
44
45	private $defined_vars;
46
47	/**
48	* Constructor
49	*
50	* @param string $phpbb_root_path phpBB Root Path
51	* @param string $php_ext php file extension
52	*/
53	function __construct($phpbb_root_path, $php_ext)
54	{
55		$this->phpbb_root_path = $phpbb_root_path;
56		$this->php_ext = $php_ext;
57		$this->config_file = $this->phpbb_root_path . 'config.' . $this->php_ext;
58	}
59
60	/**
61	* Set the path to the config file.
62	*
63	* @param string $config_file
64	*/
65	public function set_config_file($config_file)
66	{
67		$this->config_file = $config_file;
68		$this->config_loaded = false;
69	}
70
71	/**
72	* Returns an associative array containing the variables defined by the config file.
73	*
74	* @return array Return the content of the config file or an empty array if the file does not exists.
75	*/
76	public function get_all()
77	{
78		$this->load_config_file();
79
80		return $this->config_data;
81	}
82
83	/**
84	* Return the value of a variable defined into the config.php file or null if the variable does not exist.
85	*
86	* @param string $variable The name of the variable
87	* @return mixed Value of the variable or null if the variable is not defined.
88	*/
89	public function get($variable)
90	{
91		$this->load_config_file();
92
93		return isset($this->config_data[$variable]) ? $this->config_data[$variable] : null;
94	}
95
96	/**
97	* Load the config file and store the information.
98	*
99	* @return null
100	*/
101	protected function load_config_file()
102	{
103		if (!$this->config_loaded && file_exists($this->config_file))
104		{
105			$this->defined_vars = get_defined_vars();
106
107			require($this->config_file);
108			$this->config_data = array_diff_key(get_defined_vars(), $this->defined_vars);
109
110			$this->config_loaded = true;
111		}
112	}
113
114	/**
115	* Convert either 3.0 dbms or 3.1 db driver class name to 3.1 db driver class name.
116	*
117	* If $dbms is a valid 3.1 db driver class name, returns it unchanged.
118	* Otherwise prepends phpbb\db\driver\ to the dbms to convert a 3.0 dbms
119	* to 3.1 db driver class name.
120	*
121	* @param string $dbms dbms parameter
122	* @return string driver class
123	* @throws \RuntimeException
124	*/
125	public function convert_30_dbms_to_31($dbms)
126	{
127		// Note: this check is done first because mysqli extension
128		// supplies a mysqli class, and class_exists($dbms) would return
129		// true for mysqli class.
130		// However, per the docblock any valid 3.1 driver name should be
131		// recognized by this function, and have priority over 3.0 dbms.
132		if (strpos($dbms, 'phpbb\db\driver') === false && class_exists('phpbb\db\driver\\' . $dbms))
133		{
134			return 'phpbb\db\driver\\' . $dbms;
135		}
136
137		if (class_exists($dbms))
138		{
139			// Additionally we could check that $dbms extends phpbb\db\driver\driver.
140			// http://php.net/manual/en/class.reflectionclass.php
141			// Beware of possible performance issues:
142			// http://stackoverflow.com/questions/294582/php-5-reflection-api-performance
143			// We could check for interface implementation in all paths or
144			// only when we do not prepend phpbb\db\driver\.
145
146			/*
147			$reflection = new \ReflectionClass($dbms);
148
149			if ($reflection->isSubclassOf('phpbb\db\driver\driver'))
150			{
151				return $dbms;
152			}
153			*/
154
155			return $dbms;
156		}
157
158		// Force use of mysqli when specifying mysql
159		if (preg_match('/(phpbb\\\db\\\driver\\\)?mysql$/i', $dbms))
160		{
161			return 'phpbb\db\driver\mysqli';
162		}
163
164		throw new \RuntimeException("You have specified an invalid dbms driver: $dbms");
165	}
166}
167