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=0);
24
25namespace Ampache\Module\Application\Test;
26
27use Ampache\Config\AmpConfig;
28use Ampache\Config\ConfigContainerInterface;
29use Ampache\Repository\Model\Preference;
30use Ampache\Module\Application\ApplicationActionInterface;
31use Ampache\Module\Authorization\GuiGatekeeperInterface;
32use Exception;
33use Psr\Http\Message\ResponseFactoryInterface;
34use Psr\Http\Message\ResponseInterface;
35use Psr\Http\Message\ServerRequestInterface;
36
37final class ConfigAction implements ApplicationActionInterface
38{
39    public const REQUEST_KEY = 'config';
40
41    private ConfigContainerInterface $configContainer;
42
43    private ResponseFactoryInterface $responseFactory;
44
45    public function __construct(
46        ConfigContainerInterface $configContainer,
47        ResponseFactoryInterface $responseFactory
48    ) {
49        $this->configContainer = $configContainer;
50        $this->responseFactory = $responseFactory;
51    }
52
53    public function run(ServerRequestInterface $request, GuiGatekeeperInterface $gatekeeper): ?ResponseInterface
54    {
55        // Check to see if the config file is working now, if so fall
56        // through to the default, else show the appropriate template
57        $configfile = __DIR__ . '/../../../../config/ampache.cfg.php';
58
59        if (!count(parse_ini_file($configfile))) {
60            require_once __DIR__ . '/../../../../public/templates/show_test_config.inc.php';
61
62            return null;
63        }
64
65        // Load config from file
66        $results = [];
67        if (!file_exists($configfile)) {
68            return $this->responseFactory
69                ->createResponse()
70                ->withHeader(
71                    'Location',
72                    '/install.php'
73                );
74        } else {
75            // Make sure the config file is set up and parsable
76            $results = @parse_ini_file($configfile);
77
78            if (empty($results)) {
79                $link = __DIR__ . '/../../../../public/test.php?action=config';
80            }
81        }
82        /* Temp Fixes */
83        $results = Preference::fix_preferences($results);
84
85        $this->configContainer->updateConfig($results);
86        unset($results);
87
88        // Try to load localization from cookie
89        $session_name = $this->configContainer->getSessionName();
90
91        if (filter_has_var(INPUT_COOKIE, $session_name . '_lang')) {
92            AmpConfig::set('lang', $_COOKIE[$session_name . '_lang']);
93        }
94        if (!class_exists('Gettext\Translations')) {
95            require_once __DIR__ . '/../../../../public/templates/test_error_page.inc.php';
96            throw new Exception('load_gettext()');
97        } else {
98            load_gettext();
99            // Load template
100            require_once __DIR__ . '/../../../../public/templates/show_test.inc.php';
101        }
102
103        return null;
104    }
105}
106