Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
create_config_file
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 5
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
56
 get_config_data
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 1
42
 get_step_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_task_lang_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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\install\module\install_filesystem\task;
15
16use phpbb\install\exception\user_interaction_required_exception;
17
18/**
19 * Dumps config file
20 */
21class create_config_file extends \phpbb\install\task_base
22{
23    /**
24     * @var \phpbb\filesystem\filesystem_interface
25     */
26    protected $filesystem;
27
28    /**
29     * @var \phpbb\install\helper\database
30     */
31    protected $db_helper;
32
33    /**
34     * @var \phpbb\install\helper\config
35     */
36    protected $install_config;
37
38    /**
39     * @var \phpbb\install\helper\iohandler\iohandler_interface
40     */
41    protected $iohandler;
42
43    /**
44     * @var string
45     */
46    protected $phpbb_root_path;
47
48    /**
49     * @var string
50     */
51    protected $php_ext;
52
53    /**
54     * @var array
55     */
56    protected $options;
57
58    /**
59     * Constructor
60     *
61     * @param \phpbb\filesystem\filesystem_interface                $filesystem
62     * @param \phpbb\install\helper\config                            $install_config
63     * @param \phpbb\install\helper\database                        $db_helper
64     * @param \phpbb\install\helper\iohandler\iohandler_interface    $iohandler
65     * @param string                                                $phpbb_root_path
66     * @param string                                                $php_ext
67     * @param array                                                    $options
68     */
69    public function __construct(\phpbb\filesystem\filesystem_interface $filesystem,
70                                \phpbb\install\helper\config $install_config,
71                                \phpbb\install\helper\database $db_helper,
72                                \phpbb\install\helper\iohandler\iohandler_interface $iohandler,
73                                $phpbb_root_path,
74                                $php_ext,
75                                $options = array())
76    {
77        $this->install_config    = $install_config;
78        $this->db_helper        = $db_helper;
79        $this->filesystem        = $filesystem;
80        $this->iohandler        = $iohandler;
81        $this->phpbb_root_path    = $phpbb_root_path;
82        $this->php_ext            = $php_ext;
83        $this->options            = array_merge(array(
84            'debug' => false,
85            'debug_container' => false,
86            'environment' => null,
87        ), $options);
88
89        parent::__construct(true);
90    }
91
92    /**
93     * {@inheritdoc}
94     */
95    public function run()
96    {
97        $config_written = true;
98
99        // Create config.php
100        $path_to_config = $this->phpbb_root_path . 'config.' . $this->php_ext;
101
102        $fp = @fopen($path_to_config, 'w');
103        if (!$fp)
104        {
105            $config_written = false;
106        }
107
108        $config_content = $this->get_config_data($this->options['debug'], $this->options['debug_container'], $this->options['environment']);
109
110        if (!@fwrite($fp, $config_content))
111        {
112            $config_written = false;
113        }
114
115        @fclose($fp);
116
117        // chmod config.php to be only readable
118        if ($config_written)
119        {
120            try
121            {
122                $this->filesystem->phpbb_chmod($path_to_config, \phpbb\filesystem\filesystem_interface::CHMOD_READ);
123            }
124            catch (\phpbb\filesystem\exception\filesystem_exception $e)
125            {
126                // Do nothing, the user will get a notice later
127            }
128        }
129        else
130        {
131            $this->iohandler->add_error_message('UNABLE_TO_WRITE_CONFIG_FILE');
132            throw new user_interaction_required_exception();
133        }
134
135        // Create a lock file to indicate that there is an install in progress
136        $fp = @fopen($this->phpbb_root_path . 'cache/install_lock', 'wb');
137        if ($fp === false)
138        {
139            // We were unable to create the lock file - abort
140            $this->iohandler->add_error_message('UNABLE_TO_WRITE_LOCK');
141            throw new user_interaction_required_exception();
142        }
143        @fclose($fp);
144
145        try
146        {
147            $this->filesystem->phpbb_chmod($this->phpbb_root_path . 'cache/install_lock', 0777);
148        }
149        catch (\phpbb\filesystem\exception\filesystem_exception $e)
150        {
151            // Do nothing, the user will get a notice later
152        }
153    }
154
155    /**
156     * Returns the content which should be dumped to config.php
157     *
158     * @param bool        $debug                 If the debug constants should be enabled by default or not
159     * @param bool        $debug_container     If the container should be compiled on
160     *                                        every page load or not
161     * @param string    $environment        The environment to use
162     *
163     * @return string    content to be written to the config file
164     */
165    protected function get_config_data($debug = false, $debug_container = false, $environment = null)
166    {
167        $config_content = "<?php\n";
168        $config_content .= "// phpBB 3.3.x auto-generated configuration file\n// Do not change anything in this file!\n";
169
170        $dbms = $this->install_config->get('dbms');
171        $db_driver = $this->db_helper->get_available_dbms($dbms);
172        $db_driver = $db_driver[$dbms]['DRIVER'];
173
174        $config_data_array = array(
175            'dbms'            => $db_driver,
176            'dbhost'        => $this->install_config->get('dbhost'),
177            'dbport'        => $this->install_config->get('dbport'),
178            'dbname'        => $this->install_config->get('dbname'),
179            'dbuser'        => $this->install_config->get('dbuser'),
180            'dbpasswd'        => $this->install_config->get('dbpasswd'),
181            'table_prefix'    => $this->install_config->get('table_prefix'),
182
183            'phpbb_adm_relative_path'    => 'adm/',
184
185            'acm_type'        => 'phpbb\cache\driver\file',
186        );
187
188        foreach ($config_data_array as $key => $value)
189        {
190            $config_content .= "\${$key} = '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $value)) . "';\n";
191        }
192
193        $config_content .= "\n@define('PHPBB_INSTALLED', true);\n";
194
195        if ($environment)
196        {
197            $config_content .= "@define('PHPBB_ENVIRONMENT', 'test');\n";
198        }
199        else if ($debug)
200        {
201            $config_content .= "@define('PHPBB_ENVIRONMENT', 'development');\n";
202        }
203        else
204        {
205            $config_content .= "@define('PHPBB_ENVIRONMENT', 'production');\n";
206        }
207
208        if ($debug_container)
209        {
210            $config_content .= "@define('DEBUG_CONTAINER', true);\n";
211        }
212        else
213        {
214            $config_content .= "// @define('DEBUG_CONTAINER', true);\n";
215        }
216
217        if ($environment === 'test')
218        {
219            $config_content .= "@define('DEBUG_TEST', true);\n";
220
221            // Mandatory for the functional tests, will be removed by PHPBB3-12623
222            $config_content .= "@define('DEBUG', true);\n";
223        }
224
225        return $config_content;
226    }
227
228    /**
229     * {@inheritdoc}
230     */
231    public static function get_step_count()
232    {
233        return 1;
234    }
235
236    /**
237     * {@inheritdoc}
238     */
239    public function get_task_lang_name()
240    {
241        return 'TASK_CREATE_CONFIG_FILE';
242    }
243}