1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=1);
24
25namespace Ampache\Config\Init;
26
27use Ampache\Config\AmpConfig;
28use Ampache\Config\Init\Exception\ConfigFileNotFoundException;
29use Ampache\Config\Init\Exception\ConfigFileNotParsableException;
30use Ampache\Repository\Model\Preference;
31use Ampache\Module\Util\EnvironmentInterface;
32
33final class InitializationHandlerConfig implements InitializationHandlerInterface
34{
35    private const VERSION        = '5.0.0-release';
36    private const CONFIG_VERSION = '56';
37
38    public const CONFIG_FILE_PATH = __DIR__ . '/../../../config/ampache.cfg.php';
39
40    private EnvironmentInterface $environment;
41
42    public function __construct(
43        EnvironmentInterface $environment
44    ) {
45        $this->environment = $environment;
46    }
47
48    public function init(): void
49    {
50        // Check to make sure the config file exists. If it doesn't then go ahead and
51        if (file_exists(static::CONFIG_FILE_PATH) === false) {
52            throw new ConfigFileNotFoundException();
53        }
54
55        // Make sure the config file is set up and parsable
56        $results = parse_ini_file(static::CONFIG_FILE_PATH);
57
58        if ($results === false) {
59            throw new ConfigFileNotParsableException();
60        }
61
62        /** This is the version.... fluff nothing more... **/
63        $results['version']            = static::VERSION;
64        $results['int_config_version'] = static::CONFIG_VERSION;
65
66        if (!empty($results['force_ssl']) || $this->environment->isSsl() === true) {
67            $protocol = 'https';
68        } else {
69            $protocol = 'http';
70        }
71
72        if ($this->environment->isCli() === false) {
73            $results['raw_web_path'] = $results['web_path'];
74            if (empty($results['http_host'])) {
75                $results['http_host'] = $_SERVER['SERVER_NAME'];
76            }
77            if (empty($results['local_web_path'])) {
78                $results['local_web_path'] = sprintf(
79                    '%s://%s:%d%s',
80                    $protocol,
81                    $_SERVER['SERVER_NAME'],
82                    $_SERVER['SERVER_PORT'],
83                    $results['raw_web_path']
84                );
85            }
86            $results['http_port'] = (!empty($results['http_port']))
87                ? $results['http_port']
88                : $this->environment->getHttpPort();
89
90            $port = ($results['http_port'] != 80 && $results['http_port'] != 443)
91                ? ':' . $results['http_port']
92                : '';
93
94            $results['web_path'] = sprintf(
95                '%s://%s%s%s',
96                $protocol,
97                $results['http_host'],
98                $port,
99                $results['web_path']
100            );
101
102            $results['site_charset'] = $results['site_charset'] ?: 'UTF-8';
103            $results['raw_web_path'] = $results['raw_web_path'] ?: '/';
104            if (!isset($results['max_upload_size'])) {
105                $results['max_upload_size'] = 1048576;
106            }
107            $_SERVER['SERVER_NAME'] = $_SERVER['SERVER_NAME'] ?: '';
108            if (isset($results['user_ip_cardinality']) && !$results['user_ip_cardinality']) {
109                $results['user_ip_cardinality'] = 42;
110            }
111
112            // Variables needed for Auth class
113            $results['cookie_path']   = $results['raw_web_path'];
114            $results['cookie_domain'] = $results['http_host'];
115            $results['cookie_life']   = $results['session_cookielife'];
116            $results['cookie_secure'] = $results['session_cookiesecure'];
117        }
118
119        // Make sure all default preferences are set
120        Preference::set_defaults();
121
122        // Temp Fixes
123        $results = Preference::fix_preferences($results);
124
125        AmpConfig::set_by_array($results, true);
126    }
127}
128