1<?php
2
3/*
4 * vim:set softtabstop=4 shiftwidth=4 expandtab:
5 *
6 *  LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
7 * Copyright 2001 - 2020 Ampache.org
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU Affero General Public License for more details.
18 *
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
21 *
22 */
23
24declare(strict_types=0);
25
26namespace Ampache\Module\Api\Method;
27
28use Ampache\Config\AmpConfig;
29use Ampache\Repository\Model\Playlist;
30use Ampache\Repository\Model\User;
31use Ampache\Module\Api\Api;
32use Ampache\Module\Api\Json_Data;
33use Ampache\Module\Api\Xml_Data;
34use Ampache\Module\System\Session;
35
36/**
37 * Class GetIndexesMethod
38 * @package Lib\ApiMethods
39 */
40final class GetIndexesMethod
41{
42    private const ACTION = 'get_indexes';
43
44    /**
45     * get_indexes
46     * MINIMUM_API_VERSION=400001
47     * CHANGED_IN_API_VERSION=5.0.0
48     *
49     * This takes a collection of inputs and returns ID + name for the object type
50     * Added 'include' to allow indexing all song tracks (enabled for xml by default)
51     *
52     * @param array $input
53     * type        = (string) 'song', 'album', 'artist', 'album_artist', 'playlist', 'podcast', 'podcast_episode', 'share' 'video', 'live_stream'
54     * filter      = (string) //optional
55     * exact       = (integer) 0,1, if true filter is exact rather then fuzzy //optional
56     * add         = self::set_filter(date) //optional
57     * update      = self::set_filter(date) //optional
58     * include     = (integer) 0,1 include songs if available for that object //optional
59     * offset      = (integer) //optional
60     * limit       = (integer) //optional
61     * hide_search = (integer) 0,1, if true do not include searches/smartlists in the result //optional
62     * @return boolean
63     */
64    public static function get_indexes(array $input)
65    {
66        if (!Api::check_parameter($input, array('type'), self::ACTION)) {
67            return false;
68        }
69        $type = ((string) $input['type'] == 'album_artist') ? 'artist' : (string) $input['type'];
70        if (!AmpConfig::get('allow_video') && $type == 'video') {
71            Api::error(T_('Enable: video'), '4703', self::ACTION, 'system', $input['api_format']);
72
73            return false;
74        }
75        if (!AmpConfig::get('podcast') && ($type == 'podcast' || $type == 'podcast_episode')) {
76            Api::error(T_('Enable: podcast'), '4703', self::ACTION, 'system', $input['api_format']);
77
78            return false;
79        }
80        if (!AmpConfig::get('share') && $type == 'share') {
81            Api::error(T_('Enable: share'), '4703', self::ACTION, 'system', $input['api_format']);
82
83            return false;
84        }
85        if (!AmpConfig::get('live_stream') && $type == 'live_stream') {
86            Api::error(T_('Enable: live_stream'), '4703', self::ACTION, 'system', $input['api_format']);
87
88            return false;
89        }
90        $user    = User::get_from_username(Session::username($input['auth']));
91        $include = (int) $input['include'] == 1;
92        $hide    = ((int) $input['hide_search'] == 1) || AmpConfig::get('hide_search', false);
93        // confirm the correct data
94        if (!in_array($type, array('song', 'album', 'artist', 'album_artist', 'playlist', 'podcast', 'podcast_episode', 'video', 'live_stream'))) {
95            Api::error(sprintf(T_('Bad Request: %s'), $type), '4710', self::ACTION, 'type', $input['api_format']);
96
97            return false;
98        }
99        $browse = Api::getBrowse();
100        $browse->reset_filters();
101        $browse->set_type($type);
102        $browse->set_sort('name', 'ASC');
103
104        $method = ($input['exact']) ? 'exact_match' : 'alpha_match';
105        Api::set_filter($method, $input['filter'], $browse);
106        Api::set_filter('add', $input['add'], $browse);
107        Api::set_filter('update', $input['update'], $browse);
108        // set the album_artist filter (if enabled)
109        if ((string) $input['type'] == 'album_artist') {
110            Api::set_filter('album_artist', true, $browse);
111        }
112
113        if ($type == 'playlist') {
114            $browse->set_filter('playlist_type', $user->id);
115            if (!$hide) {
116                $objects = array_merge($browse->get_objects(), Playlist::get_smartlists($user->id));
117            } else {
118                $objects = $browse->get_objects();
119            }
120        } else {
121            $objects = $browse->get_objects();
122        }
123        if (empty($objects)) {
124            Api::empty($type, $input['api_format']);
125
126            return false;
127        }
128
129        ob_end_clean();
130        switch ($input['api_format']) {
131            case 'json':
132                JSON_Data::set_offset($input['offset']);
133                JSON_Data::set_limit($input['limit']);
134                echo JSON_Data::indexes($objects, $type, $user->id, $include);
135                break;
136            default:
137                XML_Data::set_offset($input['offset']);
138                XML_Data::set_limit($input['limit']);
139                echo XML_Data::indexes($objects, $type, $user->id, true, $include);
140        }
141        Session::extend($input['auth']);
142
143        return true;
144    }
145}
146