Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
| config_php_file | |
86.36% |
19 / 22 |
|
83.33% |
5 / 6 |
13.43 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| set_config_file | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get_all | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| load_config_file | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| convert_30_dbms_to_31 | |
57.14% |
4 / 7 |
|
0.00% |
0 / 1 |
6.97 | |||
| 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 | |
| 14 | namespace phpbb; |
| 15 | |
| 16 | class 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 | /** |
| 46 | * Constructor |
| 47 | * |
| 48 | * @param string $phpbb_root_path phpBB Root Path |
| 49 | * @param string $php_ext php file extension |
| 50 | */ |
| 51 | function __construct($phpbb_root_path, $php_ext) |
| 52 | { |
| 53 | $this->phpbb_root_path = $phpbb_root_path; |
| 54 | $this->php_ext = $php_ext; |
| 55 | $this->config_file = $this->phpbb_root_path . 'config.' . $this->php_ext; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Set the path to the config file. |
| 60 | * |
| 61 | * @param string $config_file |
| 62 | */ |
| 63 | public function set_config_file($config_file) |
| 64 | { |
| 65 | $this->config_file = $config_file; |
| 66 | $this->config_loaded = false; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Returns an associative array containing the variables defined by the config file. |
| 71 | * |
| 72 | * @return array Return the content of the config file or an empty array if the file does not exists. |
| 73 | */ |
| 74 | public function get_all() |
| 75 | { |
| 76 | $this->load_config_file(); |
| 77 | |
| 78 | return $this->config_data; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Return the value of a variable defined into the config.php file or null if the variable does not exist. |
| 83 | * |
| 84 | * @param string $variable The name of the variable |
| 85 | * @return mixed Value of the variable or null if the variable is not defined. |
| 86 | */ |
| 87 | public function get($variable) |
| 88 | { |
| 89 | $this->load_config_file(); |
| 90 | |
| 91 | return isset($this->config_data[$variable]) ? $this->config_data[$variable] : null; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Load the config file and store the information. |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | protected function load_config_file() |
| 100 | { |
| 101 | if (!$this->config_loaded && file_exists($this->config_file)) |
| 102 | { |
| 103 | $defined_vars = null; // Define variable before call get_defined_vars |
| 104 | $defined_vars = get_defined_vars(); |
| 105 | |
| 106 | require($this->config_file); |
| 107 | $this->config_data = array_diff_key(get_defined_vars(), $defined_vars); |
| 108 | |
| 109 | $this->config_loaded = true; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Convert either 3.0 dbms or 3.1 db driver class name to 3.1 db driver class name. |
| 115 | * |
| 116 | * If $dbms is a valid 3.1 db driver class name, returns it unchanged. |
| 117 | * Otherwise prepends phpbb\db\driver\ to the dbms to convert a 3.0 dbms |
| 118 | * to 3.1 db driver class name. |
| 119 | * |
| 120 | * @param string $dbms dbms parameter |
| 121 | * @return string driver class |
| 122 | * @throws \RuntimeException |
| 123 | */ |
| 124 | public static function convert_30_dbms_to_31($dbms) |
| 125 | { |
| 126 | // Note: this check is done first because mysqli extension |
| 127 | // supplies a mysqli class, and class_exists($dbms) would return |
| 128 | // true for mysqli class. |
| 129 | // However, per the docblock any valid 3.1 driver name should be |
| 130 | // recognized by this function, and have priority over 3.0 dbms. |
| 131 | if (strpos($dbms, 'phpbb\db\driver') === false && class_exists('phpbb\db\driver\\' . $dbms)) |
| 132 | { |
| 133 | return 'phpbb\db\driver\\' . $dbms; |
| 134 | } |
| 135 | |
| 136 | if (class_exists($dbms)) |
| 137 | { |
| 138 | // Additionally we could check that $dbms extends phpbb\db\driver\driver. |
| 139 | // http://php.net/manual/en/class.reflectionclass.php |
| 140 | // Beware of possible performance issues: |
| 141 | // http://stackoverflow.com/questions/294582/php-5-reflection-api-performance |
| 142 | // We could check for interface implementation in all paths or |
| 143 | // only when we do not prepend phpbb\db\driver\. |
| 144 | |
| 145 | /* |
| 146 | $reflection = new \ReflectionClass($dbms); |
| 147 | |
| 148 | if ($reflection->isSubclassOf('phpbb\db\driver\driver')) |
| 149 | { |
| 150 | return $dbms; |
| 151 | } |
| 152 | */ |
| 153 | |
| 154 | return $dbms; |
| 155 | } |
| 156 | |
| 157 | // Force use of mysqli when specifying mysql |
| 158 | if (preg_match('/(phpbb\\\db\\\driver\\\)?mysql$/i', $dbms)) |
| 159 | { |
| 160 | return 'phpbb\db\driver\mysqli'; |
| 161 | } |
| 162 | |
| 163 | throw new \RuntimeException("You have specified an invalid dbms driver: $dbms"); |
| 164 | } |
| 165 | } |