-
Notifications
You must be signed in to change notification settings - Fork 13
/
opensearch.php
227 lines (175 loc) · 9.18 KB
/
opensearch.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
221
222
223
224
225
226
227
<?php
// +------------------------------------------------------------------------+
// | netjukebox, Copyright © 2001-2012 Willem Bartels |
// | |
// | http://www.netjukebox.nl |
// | http://forum.netjukebox.nl |
// | |
// | 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/>. |
// +------------------------------------------------------------------------+
// +------------------------------------------------------------------------+
// | opensearch.php |
// +------------------------------------------------------------------------+
require_once('include/initialize.inc.php');
$action = get('action');
if ($action == 'installAlbumArtist') installAlbumArtist();
elseif ($action == 'installTrackArtist') installTrackArtist();
elseif ($action == 'installTrackTitle') installTrackTitle();
elseif ($action == 'suggestAlbumArtist') suggestAlbumArtist();
elseif ($action == 'suggestTrackArtist') suggestTrackArtist();
elseif ($action == 'suggestTrackTitle') suggestTrackTitle();
exit();
// +------------------------------------------------------------------------+
// | Install album artist plugin |
// +------------------------------------------------------------------------+
function installAlbumArtist() {
global $cfg;
header("Content-type: text/xml");
?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>netjukebox - Album Artist</ShortName>
<Description>netjukebox - Album Artist</Description>
<InputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></InputEncoding>
<OutputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></OutputEncoding>
<Image height="16" width="16" type="image/png"><?php echo NJB_HOME_URL; ?>image/favicon.png</Image>
<Url type="text/html" method="get" template="<?php echo NJB_HOME_URL; ?>index.php?action=view1&filter=exact&artist={searchTerms}"/>
<Url type="application/x-suggestions+json" template="<?php echo NJB_HOME_URL; ?>opensearch.php?action=suggestAlbumArtist&artist={searchTerms}&version=1"/>
</OpenSearchDescription>
<?php
}
// +------------------------------------------------------------------------+
// | Install track artist plugin |
// +------------------------------------------------------------------------+
function installTrackArtist() {
global $cfg;
header("Content-type: text/xml");
?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>netjukebox - Track Artist</ShortName>
<Description>netjukebox - Track Artist</Description>
<InputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></InputEncoding>
<OutputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></OutputEncoding>
<Image height="16" width="16" type="image/png"><?php echo NJB_HOME_URL; ?>image/favicon.png</Image>
<Url type="text/html" method="get" template="<?php echo NJB_HOME_URL; ?>index.php?action=view3all&filter=exact&artist={searchTerms}"/>
<Url type="application/x-suggestions+json" template="<?php echo NJB_HOME_URL; ?>opensearch.php?action=suggestTrackArtist&artist={searchTerms}&version=1"/>
</OpenSearchDescription>
<?php
}
// +------------------------------------------------------------------------+
// | Install track title plugin |
// +------------------------------------------------------------------------+
function installTrackTitle() {
global $cfg;
header("Content-type: text/xml");
?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>netjukebox - Title</ShortName>
<Description>netjukebox - Title</Description>
<InputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></InputEncoding>
<OutputEncoding><?php echo NJB_DEFAULT_CHARSET; ?></OutputEncoding>
<Image height="16" width="16" type="image/png"><?php echo NJB_HOME_URL; ?>image/favicon.png</Image>
<Url type="text/html" method="get" template="<?php echo NJB_HOME_URL; ?>index.php?action=view3all&filter=exact&title={searchTerms}"/>
<Url type="application/x-suggestions+json" template="<?php echo NJB_HOME_URL; ?>opensearch.php?action=suggestTrackTitle&title={searchTerms}&version=1"/>
</OpenSearchDescription>
<?php
}
// +------------------------------------------------------------------------+
// | Suggest album artist |
// +------------------------------------------------------------------------+
function suggestAlbumArtist() {
global $cfg, $db;
header('Content-type: application/json');
$artist = get('artist');
authenticateOpensearch($artist);
$query = mysqli_query($db,'SELECT artist_alphabetic FROM album
WHERE artist_alphabetic LIKE "%' . mysqli_real_escape_like($artist) . '%"
OR artist LIKE "%' . mysqli_real_escape_like($artist) . '%"
OR artist SOUNDS LIKE "' . mysqli_real_escape_string($db,$artist) . '"
GROUP BY artist_alphabetic ORDER BY artist_alphabetic LIMIT ' . (int) $cfg['autosuggest_limit']);
$data = array();
while ($album = mysqli_fetch_assoc($query))
$data[] = (string) $album['artist_alphabetic'];
$data = array($artist, $data);
echo safe_json_encode($data);
}
// +------------------------------------------------------------------------+
// | Suggest track artist |
// +------------------------------------------------------------------------+
function suggestTrackArtist() {
global $cfg, $db;
header('Content-type: application/json');
$artist = get('artist');
authenticateOpensearch($artist);
$query = mysqli_query($db,'SELECT artist FROM track
WHERE artist LIKE "%' . mysqli_real_escape_like($artist) . '%"
OR artist SOUNDS LIKE "' . mysqli_real_escape_string($db,$artist) . '"
GROUP BY artist ORDER BY artist LIMIT ' . (int) $cfg['autosuggest_limit']);
$data = array();
while ($track = mysqli_fetch_assoc($query))
$data[] = (string) $track['artist'];
$data = array($artist, $data);
echo safe_json_encode($data);
}
// +------------------------------------------------------------------------+
// | Suggest track title |
// +------------------------------------------------------------------------+
function suggestTrackTitle() {
global $cfg, $db;
header('Content-type: application/json');
$title = get('title');
authenticateOpensearch($title);
$query = mysqli_query($db,'SELECT title FROM track
WHERE title LIKE "%' . mysqli_real_escape_like($title) . '%"
OR title SOUNDS LIKE "' . mysqli_real_escape_string($db,$title) . '"
GROUP BY title ORDER BY title LIMIT ' . (int) $cfg['autosuggest_limit']);
$data = array();
while ($track = mysqli_fetch_assoc($query))
$data[] = (string) $track['title'];
$data = array($title, $data);
echo safe_json_encode($data);
}
// +------------------------------------------------------------------------+
// | Authenticate opensearch |
// +------------------------------------------------------------------------+
function authenticateOpensearch($input) {
global $cfg, $db;
header('Expires: Mon, 9 Oct 2000 18:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
$sid = cookie('netjukebox_sid');
$version = get('version');
$query = mysqli_query($db,'SELECT logged_in, idle_time, ip, user_agent FROM session WHERE sid = BINARY "' . mysqli_real_escape_string($db,$sid) . '"');
$session = mysqli_fetch_assoc($query);
if ($sid == '') {
$data = array('Allow third-party cookies,', 'or add an exception for this domain!');
$data = array($input, $data);
echo safe_json_encode($data);
exit();
}
if ($version != 1) {
$data = array('Reinstall opensearch plugin!');
$data = array($input, $data);
echo safe_json_encode($data);
exit();
}
if ($session['logged_in'] &&
$session['ip'] == $_SERVER['REMOTE_ADDR'] &&
$session['user_agent'] == substr($_SERVER['HTTP_USER_AGENT'], 0, 255) &&
$session['idle_time'] + $cfg['session_lifetime'] > time())
return true;
$data = array('Login netjukebox!');
$data = array($input, $data);
echo safe_json_encode($data);
exit();
}
?>