forked from ampache/ampache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.php
192 lines (177 loc) · 6.57 KB
/
stream.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/* vim:set softtabstop=4 shiftwidth=4 expandtab: */
/**
*
* LICENSE: GNU Affero General Public License, version 3 (AGPLv3)
* Copyright 2001 - 2017 Ampache.org
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once 'lib/init.php';
if (!isset($_REQUEST['action']) || empty($_REQUEST['action'])) {
debug_event("stream.php", "Asked without action. Exiting...", 5);
exit;
}
if (!defined('NO_SESSION')) {
/* If we are running a demo, quick while you still can! */
if (AmpConfig::get('demo_mode') || (AmpConfig::get('use_auth')) && !Access::check('interface', '25')) {
UI::access_denied();
exit;
}
}
$media_ids = array();
$web_path = AmpConfig::get('web_path');
debug_event("stream.php", "Asked for {" . $_REQUEST['action'] . "}.", 5);
/**
* action switch
*/
switch ($_REQUEST['action']) {
case 'basket':
// Pull in our items (multiple types)
$media_ids = $GLOBALS['user']->playlist->get_items();
// Check to see if 'clear' was passed if it was then we need to reset the basket
if (($_REQUEST['playlist_method'] == 'clear' || AmpConfig::get('playlist_method') == 'clear')) {
$GLOBALS['user']->playlist->clear();
}
break;
/* This is run if we need to gather info based on a tmp playlist */
case 'tmp_playlist':
$tmp_playlist = new Tmp_Playlist($_REQUEST['tmpplaylist_id']);
$media_ids = $tmp_playlist->get_items();
break;
case 'play_favorite':
$data = $GLOBALS['user']->get_favorites($_REQUEST['type']);
$media_ids = array();
switch ($_REQUEST['type']) {
case 'artist':
case 'album':
foreach ($data as $value) {
$songs = $value->get_songs();
$media_ids = array_merge($media_ids, $songs);
}
break;
case 'song':
foreach ($data as $value) {
$media_ids[] = $value->id;
}
break;
} // end switch on type
break;
case 'play_item':
$object_type = $_REQUEST['object_type'];
$object_ids = explode(',', $_REQUEST['object_id']);
if (Core::is_playable_item($object_type)) {
foreach ($object_ids as $object_id) {
$item = new $object_type($object_id);
$media_ids = array_merge($media_ids, $item->get_medias());
if ($_REQUEST['custom_play_action']) {
foreach ($media_ids as $media_id) {
if (is_array($media_id)) {
$media_id['custom_play_action'] = $_REQUEST['custom_play_action'];
}
}
}
}
}
break;
case 'artist_random':
$artist = new Artist($_REQUEST['artist_id']);
$media_ids = $artist->get_random_songs();
break;
case 'album_random':
$album = new Album($_REQUEST['album_id']);
$media_ids = $album->get_random_songs();
break;
case 'playlist_random':
$playlist = new Playlist($_REQUEST['playlist_id']);
$media_ids = $playlist->get_random_items();
break;
case 'random':
$matchlist = array();
if ($_REQUEST['genre'][0] != '-1') {
$matchlist['genre'] = $_REQUEST['genre'];
}
if ($_REQUEST['catalog'] != '-1') {
$matchlist['catalog'] = $_REQUEST['catalog'];
}
/* Setup the options array */
$options = array('limit' => $_REQUEST['random'], 'random_type' => $_REQUEST['random_type'],'size_limit' => $_REQUEST['size_limit']);
$media_ids = get_random_songs($options, $matchlist);
break;
case 'democratic':
$democratic = new Democratic($_REQUEST['democratic_id']);
$urls = array($democratic->play_url());
break;
case 'download':
if (isset($_REQUEST['song_id'])) {
$media_ids[] = array(
'object_type' => 'song',
'object_id' => scrub_in($_REQUEST['song_id'])
);
} elseif (isset($_REQUEST['video_id'])) {
$media_ids[] = array(
'object_type' => 'video',
'object_id' => scrub_in($_REQUEST['video_id'])
);
} elseif (isset($_REQUEST['podcast_episode_id'])) {
$media_ids[] = array(
'object_type' => 'podcast_episode',
'object_id' => scrub_in($_REQUEST['podcast_episode_id'])
);
}
break;
default:
break;
} // end action switch
// See if we need a special streamtype
switch ($_REQUEST['action']) {
case 'download':
$stream_type = 'download';
break;
case 'democratic':
// Don't let them loop it
// FIXME: This looks hacky
if (AmpConfig::get('play_type') == 'democratic') {
AmpConfig::set('play_type', 'stream', true);
}
default:
$stream_type = AmpConfig::get('play_type');
if ($stream_type == 'stream') {
$stream_type = AmpConfig::get('playlist_type');
}
break;
}
debug_event('stream.php', 'Stream Type: ' . $stream_type . ' Media IDs: ' . json_encode($media_ids), 5);
if (count($media_ids) || isset($urls)) {
if ($stream_type != 'democratic') {
if (!User::stream_control($media_ids)) {
debug_event('UI::access_denied', 'Stream control failed for user ' . $GLOBALS['user']->username, 3);
UI::access_denied();
exit;
}
}
if ($GLOBALS['user']->id > -1) {
Session::update_username(Stream::get_session(), $GLOBALS['user']->username);
}
$playlist = new Stream_Playlist();
$playlist->add($media_ids);
if (isset($urls)) {
$playlist->add_urls($urls);
}
// Depending on the stream type, will either generate a redirect or actually do the streaming.
$playlist->generate_playlist($stream_type, false);
} else {
debug_event('stream.php', 'No item. Ignoring...', 5);
}