forked from ArturSierzant/OMPD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax-playlist-save.php
220 lines (190 loc) · 7.43 KB
/
ajax-playlist-save.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
// +------------------------------------------------------------------------+
// | O!MPD, Copyright © 2015-2020 Artur Sierzant |
// | http://www.ompd.pl |
// | |
// | |
// | This program is free software: you can redistribute it and/or modify |
// | it under the terms of the GNU 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 General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program. If not, see <http://www.gnu.org/licenses/>. |
// +------------------------------------------------------------------------+
global $cfg, $db;
require_once('include/initialize.inc.php');
require_once('include/library.inc.php');
require_once('include/play.inc.php');
$data = array();
$data['action_status'] = false;
$data['not_compatible'] = false;
$action = $_GET['action'];
$name = $_GET['name'];
$saveAs = (int) $_GET['saveAs'];
$comment = $_GET['comment'];
$host = $_GET['host'];
$port = $_GET['port'];
$saveTrackId = $_GET['saveTrackId'];
$saveTrackMpdUrl = getTrackMpdUrl($_GET['track_mpd_url']);
//add to/save as from track submenu for streams (Tidal/YT)
if (!$saveTrackMpdUrl){
$saveTrackMpdUrl = createStreamUrlMpd($saveTrackId);
}
$saveTrack = $_GET['saveTrack'];
//avoid adding empty record when mpd is in unknown state
if ($saveTrack == 'true' && !$saveTrackMpdUrl && !$saveTrackId) {
$data['not_compatible'] = true;
echo safe_json_encode($data);
return;
}
/* if ($cfg['username'] == '') {
$data['user'] = $cfg;
echo safe_json_encode($data);
return;
} */
if ($action == 'SaveAs') {
if ($name != '') {
authenticate('access_admin');
mysqli_query($db,'INSERT INTO favorite (name,comment) VALUES ("' . mysqli_real_escape_string($db,$name) . '","' . mysqli_real_escape_string($db,$comment) . '")');
$favorite_id = mysqli_insert_id($db);
if ($saveTrack == 'true')
importTrack($favorite_id, $saveTrackId, $saveTrackMpdUrl);
else
importFavorite($favorite_id, $host, $port, 'import');
$data['action_status'] = true;
$data['select_options']= listOfFavorites(true,true,$saveTrackId);
}
}
elseif ($action == 'AddTo') {
if ($saveAs) {
authenticate('access_admin');
if ($saveTrack == 'true') {
if ($saveAs > 0) {//add track only if playlist is file playlist
importTrack($saveAs, $saveTrackId, $saveTrackMpdUrl);
$data['select_options']= listOfFavorites(true,true,$saveTrackId);
}
else {
$data['not_compatible'] = true;
}
}
else {
importFavorite($saveAs, $host, $port, 'add');
}
$data['action_status'] = true;
}
}
echo safe_json_encode($data);
function importTrack($favorite_id, $track_id, $track_mpd_url) {
global $cfg, $db, $data;
$track_id = $track_id ? $track_id : '';
$track_mpd_url = $track_mpd_url ? $track_mpd_url : '';
if ($track_id) {
//from Tidal album view or search results
if (isTidal($track_id)){
$tidal_track_id = getTidalId($track_id);
$quertT = mysqli_query($db,"SELECT track_id FROM tidal_track WHERE track_id = '" . $tidal_track_id . "'");
if (mysqli_affected_rows($db) == 0) {
//track not in DB, e.g. result of search - get album and add it to DB
$album_id = getTrackAlbumFromTidal($tidal_track_id);
$get_album_tracks = getTracksFromTidalAlbum($album_id);
}
$track_mpd_url = createStreamUrlMpd($track_id);
$track_id = '';
$addTidalPrefix = true;
}
else {
//check if track_id is from local files indexed by DB
$query = mysqli_query($db,"SELECT track_id FROM track WHERE track_id = '" .$track_id . "'");
if (mysqli_num_rows($query) == 0) {
//if not then use $track_mpd_url as stream source
$track_id = '';
}
else {
$track_mpd_url = '';
}
}
}
$query = mysqli_query($db,"SELECT MAX(position) as maxPosition FROM favoriteitem WHERE favorite_id = '" .$favorite_id . "'");
$favoriteitem = mysqli_fetch_assoc($query);
$maxPosition = (int) $favoriteitem['maxPosition'];
$maxPosition++;
mysqli_query($db,"INSERT INTO favoriteitem (track_id, stream_url, position, favorite_id) VALUES(
'" . $track_id . "',
'" . $track_mpd_url . "',
'" . $maxPosition . "',
'" . $favorite_id . "'
)");
$data['affRows'] = mysqli_affected_rows($db);
$data['favorite_id'] = $favorite_id;
// Update favorite stream status
updateFavoriteStreamStatus($favorite_id);
};
function importFavorite($favorite_id, $host, $port, $mode) {
global $cfg, $db, $data;
if ($favorite_id < 0) $isStreamPlaylist = 1;
elseif ($favorite_id >= 0) $isStreamPlaylist = 0;
$favorite_id = abs($favorite_id);
if ($cfg['player_type'] == NJB_MPD) {
$file = mpd('playlist',$host, $port);
$file = implode('<seperation>', $file);
$file = iconv(NJB_DEFAULT_CHARSET, 'UTF-8', $file);
$file = explode('<seperation>', $file);
}
else
message(__FILE__, __LINE__, 'error', '[b]Player not supported[/b]');
$stream = 0;
$streamCount = 0;
for ($i = 0; $i < count($file); $i++) {
if (preg_match('#^(tidal|ftp|http|https|mms|mmst|pnm|rtp|rtsp|sdp)://#', $file[$i])) {
//$stream = 1;
$streamCount = $streamCount + 1 ;
}
}
if (count($file) > 0) {
/* $query = mysqli_query($db,'SELECT stream FROM favorite WHERE favorite_id = ' . (int) $favorite_id);
$favType = mysqli_fetch_assoc($query);
$isFavStream = $favType['stream']; */
if ($mode == 'import') {
mysqli_query($db,'DELETE FROM favoriteitem WHERE favorite_id = ' . (int) $favorite_id);
$offset = 0;
}
if ($mode == 'add') {
$query = mysqli_query($db,'SELECT position FROM favoriteitem WHERE favorite_id = ' . (int) $favorite_id . ' ORDER BY position DESC');
$track = mysqli_fetch_assoc($query);
$offset = $track['position'];
}
}
for ($i = 0; $i < count($file); $i++) {
$query = mysqli_query($db,'SELECT track_id FROM track WHERE relative_file = "' . mysqli_real_escape_string($db,$file[$i]) . '"');
$track = mysqli_fetch_assoc($query);
$isStream = 0;
if (preg_match('#^(tidal|ftp|http|https|mms|mmst|pnm|rtp|rtsp|sdp)://#', $file[$i])) {
$isStream = 1;
}
if ($isStream == 0 && $track['track_id']) {
$position = $i + $offset + 1;
mysqli_query($db,'INSERT INTO favoriteitem (track_id, position, favorite_id)
VALUES ("' . mysqli_real_escape_string($db,$track['track_id']) . '",
' . (int) $position . ',
' . (int) $favorite_id . ')');
}
if ($isStream == 1) {
$position = $i + $offset + 1;
mysqli_query($db,'INSERT INTO favoriteitem (stream_url, position, favorite_id)
VALUES ("' . mysqli_real_escape_string($db,getTrackMpdUrl($file[$i])) . '",
' . (int) $position . ',
' . (int) $favorite_id . ')');
}
/* else { //trying to add file to stream playlist or stream to file playlist
$data['not_compatible'] = true;
} */
}
updateFavoriteStreamStatus($favorite_id);
}
?>