This repository has been archived by the owner on Mar 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.get.php
258 lines (216 loc) · 5.99 KB
/
api.get.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
/**
* Copyright (c) 2016 AvikB, some rights reserved.
* Copyright under Creative Commons Attribution-ShareAlike 3.0 Unported,
* for details visit: https://creativecommons.org/licenses/by-sa/3.0/
*
* @Contributors:
* Created by AvikB for noncommercial MusicBee project.
* Spelling mistakes and fixes from community members.
*
*/
/**
* @author Avik B
* @version 1.0
*/
/**
* All data are JSON formatted. Currently JSON is the only format we serve, maybe XML in future... :S
* For result list Maximum limit is 20, default is 5
*
*****************************************************************************************************
* API Links:
*****************************************************************************************************
*
* MusicBee beta, stable and patch release info
* http://getmusicbee.com/api/1.0/?type=json&action=release-info
*
* ADDON Data
* http://getmusicbee.com/api/1.0/?type=json&action=addon-info&id=1
*
* ADDON List by UserID
* http://getmusicbee.com/api/1.0/?type=json&action=addon-list&authorid=1&limit=10
*
* ADDON Search by Term
* http://getmusicbee.com/api/1.0/?type=json&action=addon-search&search=windows&page=1&limit=10
*
*/
require_once $_SERVER['DOCUMENT_ROOT'].'/functions.php';
require_once $link['root'] . 'classes/Addon.php';
require_once $link['root'] . 'classes/Search.php';
const default_result_limit = 5;
const max_result_limit = 20;
if(isset($params[3])) {
//remove the ? sign from the string and convert the url paramenter into an array
parse_str (str_replace ("?", "", $params[3]), $url_params);
if (isset($url_params['type']) && isset($url_params['action']))
{
//if the request is for JSON then intialize json parsing
if ($url_params['type'] == "json")
{
//switch between actions
switch ($url_params['action']) {
case 'release-info':
releaseInfo();
break;
case 'addon-info':
addonInfo();
break;
case 'addon-list':
addonList();
break;
case 'addon-search':
addonSearchJson();
break;
default:
# code...
break;
}
}
else if ($url_params['type'] == "html")
{
//switch between actions
switch ($url_params['action']) {
case 'addon-search-autocomplete':
addonSearchAutoComplete();
break;
default:
# code...
break;
}
}
}
}
/**
* Generate MusicBee beta, stable and patch release info
*/
function releaseInfo()
{
global $mb;
printJson(json_encode($mb['musicbee_download']));
}
/**
* Search addons by search term, filter between addon type
*/
function addonSearchJson()
{
printJson(json_encode(addonSearch()));
}
function addonSearchAutoComplete()
{
global $lang;
$searchData = addonSearch();
foreach ($searchData['addon_data']['result'] as $result)
{
$thumb = Format::ImgurResizer($result['thumbnail'],"s");
$title = $result['addon_title'];
$author = $lang['addon_15'].' '.$result['membername'];
$url = addonUrlGenerator($result);
$uid = md5($title.$author);
$html = <<<HTML
<li id="$uid">
<ul>
<li>
<img src="$thumb">
</li>
<li>
<p class="title"><a href="$url">$title</a></p>
<p>$author</p>
</li>
</ul>
</li>
HTML;
echo $html;
//var_dump($result);
}
exit();
}
function addonSearch()
{
global $url_params, $mb, $link;
$limit = (isset($url_params['limit']))?checkResultLimit($url_params['limit']): $mb['view_range']['addon_view_range'];
if(isset($url_params['search']))
{
if(isset($url_params['cat'])){
if($url_params['cat'] == 'all'){
$url_params['cat'] = null;
}
} else {
$url_params['cat'] = null;
}
if (isset($url_params['page']))
{
if(is_int($url_params['page']) || ctype_digit($url_params['page']))
{
$offset = ($url_params['page'] - 1) * $limit;
$data['current_page'] = $url_params['page'];
}
else
{
$offset = 0;
$data['current_page'] = 1;
}
}
else
{
$offset = 0;
$data['current_page'] = 1;
}
$searchinput['query'] = (!empty($url_params['search']))? htmlspecialchars(trim($url_params['search']), ENT_QUOTES, "UTF-8"): '';
$search = new Search();
//get all the addon filtered by category/query and other
$data['addon_data'] = $search->searchAddons($searchinput['query'],$url_params['cat'],'1', null, $offset, $limit);
//Calculate total number of page required
$data['total_page'] = ceil ($data['addon_data']['row_count'] / $limit);
$data['page_url'] = $link['addon']['home'] . "s/?q=" . urlencode ($searchinput['query']) . "&type=" . $url_params['cat'];
$data['prev_page_url'] = ($data['current_page']==1)? null : $data['page_url'].'&p='.($data['current_page']-1);
$data['next_page_url'] = ($data['total_page']==$data['current_page'])? null : $data['page_url'].'&p='.($data['current_page']+1);
return $data;
}
}
/**
* Generate a List of Addon submitted by a member, you will need memberid/userid
*/
function addonList()
{
global $url_params;
if(isset($url_params['authorid']))
{
$addon = new Addon();
$limit = (isset($url_params['limit']))? checkResultLimit($url_params['limit']) : default_result_limit;
$data = $addon->getAddonListByMember ($url_params['authorid'], $limit);
printJson(json_encode($data));
}
}
/**
* Get ALL the info about an addon, you will need addon id
*/
function addonInfo()
{
global $url_params;
if(isset($url_params['id']))
{
$addon = new Addon();
$addon_data = $addon->getAddonData($url_params['id']);
printJson(json_encode($addon_data));
}
}
function checkResultLimit($limit)
{
if(ctype_digit($limit))
{
if($limit > max_result_limit)
{
return max_result_limit;
}
}
return default_result_limit;
}
/**
* Prints the data to a human eye, all in JSON
* @param $encodedData
*/
function printJson($encodedData)
{
header('Content-Type: application/json');
print_r($encodedData);
}