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\Api\Method;
26
27use Ampache\Repository\Model\Preference;
28use Ampache\Repository\Model\User;
29use Ampache\Module\Api\Api;
30use Ampache\Module\Api\Xml_Data;
31use Ampache\Module\System\Session;
32
33/**
34 * Class SystemPreferenceMethod
35 * @package Lib\ApiMethods
36 */
37final class SystemPreferenceMethod
38{
39    private const ACTION = 'system_preference';
40
41    /**
42     * system_preference
43     * MINIMUM_API_VERSION=5.0.0
44     *
45     * Get your system preferences by name
46     *
47     * @param array $input
48     * filter = (string) Preference name e.g ('notify_email', 'ajax_load')
49     * @return boolean
50     */
51    public static function system_preference(array $input)
52    {
53        if (!Api::check_parameter($input, array('filter'), self::ACTION)) {
54            return false;
55        }
56        $user = User::get_from_username(Session::username($input['auth']));
57        if (!Api::check_access('interface', 100, $user->id, self::ACTION, $input['api_format'])) {
58            return false;
59        }
60        $pref_name  = (string) $input['filter'];
61        $preference = Preference::get($pref_name, -1);
62        if (empty($preference)) {
63            /* HINT: Requested object string/id/type ("album", "myusername", "some song title", 1298376) */
64            Api::error(sprintf(T_('Not Found: %s'), $pref_name), '4704', self::ACTION, 'filter', $input['api_format']);
65
66            return false;
67        }
68        switch ($input['api_format']) {
69            case 'json':
70                echo json_encode($preference, JSON_PRETTY_PRINT);
71                break;
72            default:
73                echo Xml_Data::object_array($preference, 'preference');
74        }
75        Session::extend($input['auth']);
76
77        return true;
78    }
79}
80