aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/install/module/install_filesystem/task/create_config_file.php
blob: 5bc425b9295106eeb621a13241870ad76053f796 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\install\module\install_filesystem\task;

use phpbb\install\exception\user_interaction_required_exception;

/**
 * Dumps config file
 */
class create_config_file extends \phpbb\install\task_base
{
	/**
	 * @var \phpbb\filesystem\filesystem_interface
	 */
	protected $filesystem;

	/**
	 * @var \phpbb\install\helper\database
	 */
	protected $db_helper;

	/**
	 * @var \phpbb\install\helper\config
	 */
	protected $install_config;

	/**
	 * @var \phpbb\install\helper\iohandler\iohandler_interface
	 */
	protected $iohandler;

	/**
	 * @var string
	 */
	protected $phpbb_root_path;

	/**
	 * @var string
	 */
	protected $php_ext;

	/**
	 * @var array
	 */
	protected $options;

	/**
	 * Constructor
	 *
	 * @param \phpbb\filesystem\filesystem_interface				$filesystem
	 * @param \phpbb\install\helper\config							$install_config
	 * @param \phpbb\install\helper\database						$db_helper
	 * @param \phpbb\install\helper\iohandler\iohandler_interface	$iohandler
	 * @param string												$phpbb_root_path
	 * @param string												$php_ext
	 * @param array													$options
	 */
	public function __construct(\phpbb\filesystem\filesystem_interface $filesystem,
								\phpbb\install\helper\config $install_config,
								\phpbb\install\helper\database $db_helper,
								\phpbb\install\helper\iohandler\iohandler_interface $iohandler,
								$phpbb_root_path,
								$php_ext,
								$options = array())
	{
		$this->install_config	= $install_config;
		$this->db_helper		= $db_helper;
		$this->filesystem		= $filesystem;
		$this->iohandler		= $iohandler;
		$this->phpbb_root_path	= $phpbb_root_path;
		$this->php_ext			= $php_ext;
		$this->options			= array_merge(array(
			'debug' => false,
			'debug_container' => false,
			'environment' => null,
		), $options);

		parent::__construct(true);
	}

	/**
	 * {@inheritdoc}
	 */
	public function run()
	{
		$config_written = true;

		// Create config.php
		$path_to_config = $this->phpbb_root_path . 'config.' . $this->php_ext;

		$fp = @fopen($path_to_config, 'w');
		if (!$fp)
		{
			$config_written = false;
		}

		$config_content = $this->get_config_data($this->options['debug'], $this->options['debug_container'], $this->options['environment']);

		if (!@fwrite($fp, $config_content))
		{
			$config_written = false;
		}

		@fclose($fp);

		// chmod config.php to be only readable
		if ($config_written)
		{
			try
			{
				$this->filesystem->phpbb_chmod($path_to_config, \phpbb\filesystem\filesystem_interface::CHMOD_READ);
			}
			catch (\phpbb\filesystem\exception\filesystem_exception $e)
			{
				// Do nothing, the user will get a notice later
			}
		}
		else
		{
			$this->iohandler->add_error_message('UNABLE_TO_WRITE_CONFIG_FILE');
			throw new user_interaction_required_exception();
		}

		// Create a lock file to indicate that there is an install in progress
		$fp = @fopen($this->phpbb_root_path . 'cache/install_lock', 'wb');
		if ($fp === false)
		{
			// We were unable to create the lock file - abort
			$this->iohandler->add_error_message('UNABLE_TO_WRITE_LOCK');
			throw new user_interaction_required_exception();
		}
		@fclose($fp);

		try
		{
			$this->filesystem->phpbb_chmod($this->phpbb_root_path . 'cache/install_lock', 0777);
		}
		catch (\phpbb\filesystem\exception\filesystem_exception $e)
		{
			// Do nothing, the user will get a notice later
		}
	}

	/**
	 * Returns the content which should be dumped to config.php
	 *
	 * @param bool		$debug 				If the debug constants should be enabled by default or not
	 * @param bool		$debug_container 	If the container should be compiled on
	 *										every page load or not
	 * @param string	$environment		The environment to use
	 *
	 * @return string	content to be written to the config file
	 */
	protected function get_config_data($debug = false, $debug_container = false, $environment = null)
	{
		$config_content = "<?php\n";
		$config_content .= "// phpBB 3.2.x auto-generated configuration file\n// Do not change anything in this file!\n";

		$dbms = $this->install_config->get('dbms');
		$db_driver = $this->db_helper->get_available_dbms($dbms);
		$db_driver = $db_driver[$dbms]['DRIVER'];

		$config_data_array = array(
			'dbms'			=> $db_driver,
			'dbhost'		=> $this->install_config->get('dbhost'),
			'dbport'		=> $this->install_config->get('dbport'),
			'dbname'		=> $this->install_config->get('dbname'),
			'dbuser'		=> $this->install_config->get('dbuser'),
			'dbpasswd'		=> $this->install_config->get('dbpasswd'),
			'table_prefix'	=> $this->install_config->get('table_prefix'),

			'phpbb_adm_relative_path'	=> 'adm/',

			'acm_type'		=> 'phpbb\cache\driver\file',
		);

		foreach ($config_data_array as $key => $value)
		{
			$config_content .= "\${$key} = '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $value)) . "';\n";
		}

		$config_content .= "\n@define('PHPBB_INSTALLED', true);\n";
		$config_content .= "// @define('PHPBB_DISPLAY_LOAD_TIME', true);\n";

		if ($environment)
		{
			$config_content .= "@define('PHPBB_ENVIRONMENT', 'test');\n";
		}
		else if ($debug)
		{
			$config_content .= "@define('PHPBB_ENVIRONMENT', 'development');\n";
		}
		else
		{
			$config_content .= "@define('PHPBB_ENVIRONMENT', 'production');\n";
		}

		if ($debug_container)
		{
			$config_content .= "@define('DEBUG_CONTAINER', true);\n";
		}
		else
		{
			$config_content .= "// @define('DEBUG_CONTAINER', true);\n";
		}

		if ($environment === 'test')
		{
			$config_content .= "@define('DEBUG_TEST', true);\n";

			// Mandatory for the functional tests, will be removed by PHPBB3-12623
			$config_content .= "@define('DEBUG', true);\n";
		}

		return $config_content;
	}

	/**
	 * {@inheritdoc}
	 */
	static public function get_step_count()
	{
		return 1;
	}

	/**
	 * {@inheritdoc}
	 */
	public function get_task_lang_name()
	{
		return 'TASK_CREATE_CONFIG_FILE';
	}
}