Skip to content

Commit cc36a89

Browse files
committed
fix: K4 panel redirect on api calls #62
1 parent 8b6a0e1 commit cc36a89

File tree

2 files changed

+44
-42
lines changed

2 files changed

+44
-42
lines changed

index.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,15 +126,15 @@
126126
'action' => function ($episodeSlug) {
127127
$podcast = new Podcast();
128128

129-
return json_encode($podcast->getPodloveRoles($episodeSlug));
129+
return new Response(json_encode($podcast->getPodloveRoles($episodeSlug)), 'application/json');
130130
}
131131
],
132132
[
133133
'pattern' => 'podcaster/podlove/groups/(:all)',
134134
'action' => function ($episodeSlug) {
135135
$podcast = new Podcast();
136136

137-
return json_encode($podcast->getPodloveRoles($episodeSlug));
137+
return new Response(json_encode($podcast->getPodloveRoles($episodeSlug)), 'application/json');
138138
}
139139
],
140140
[
@@ -144,7 +144,7 @@
144144
$podcast = new Podcast();
145145
$episode = $podcast->getPageFromSlug($episodeSlug);
146146

147-
return json_encode($podcast->getPodloveConfigJson($episode));
147+
return new Response(json_encode($podcast->getPodloveConfigJson($episode)), 'application/json');
148148
}
149149
],
150150
[
@@ -153,7 +153,7 @@
153153
$podcast = new Podcast();
154154
$episode = $podcast->getPageFromSlug($episodeSlug);
155155

156-
return json_encode($podcast->getPodloveEpisodeJson($episode));
156+
return new Response(json_encode($podcast->getPodloveEpisodeJson($episode)), 'application/json');
157157
}
158158
],
159159
[
@@ -172,7 +172,8 @@
172172
}
173173

174174
$json = file_get_contents(__DIR__ . '/res/' . $endpoint . '.json');
175-
return $json;
175+
176+
return new Response(json_encode($json), 'application/json');
176177
}
177178
],
178179
],

internal/api.php

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
$results = $stats->getDownloadsGraphData($podcastId, $year, $month);
2222

2323
if ($results === false) {
24-
return ['days' => []];
24+
return new Response(json_encode(['days' => []]), 'application/json');
2525
}
2626

2727
$trackedDays = $results->toArray();
@@ -32,7 +32,7 @@
3232
$days[$dayAsInt] = (int)$day->downloads;
3333
}
3434

35-
return ['days' => $days];
35+
return new Response(json_encode(['days' => $days]), 'application/json');
3636
},
3737
],
3838
[
@@ -50,7 +50,7 @@
5050
$results = $stats->getEpisodesGraphData($podcastId);
5151

5252
if ($results === false) {
53-
return [];
53+
return new Response(json_encode([]), 'application/json');
5454
}
5555

5656
$trackedMonths = $results->toArray();
@@ -62,15 +62,15 @@
6262
$month = (int)$entry->month;
6363
$downloads = (int)$entry->downloads;
6464

65-
if(!isset($downloadArray[$year])) {
65+
if (!isset($downloadArray[$year])) {
6666
$downloadArray[$year] = [];
6767
}
6868

6969
$downloadArray[$year][$month] = $downloads;
7070
}
7171

72-
foreach($downloadArray as $year => $months) {
73-
for($i = 1; $i <= 12; $i++) {
72+
foreach ($downloadArray as $year => $months) {
73+
for ($i = 1; $i <= 12; $i++) {
7474
$downloadJson[] = [
7575
'downloads' => $months[$i] ?? 0,
7676
'year' => $year,
@@ -79,7 +79,7 @@
7979
}
8080
}
8181

82-
return ['downloads' => $downloadJson];
82+
return new Response(json_encode(['downloads' => $downloadJson]), 'application/json');
8383
},
8484
],
8585
[
@@ -97,7 +97,7 @@
9797
$results = $stats->getFeedsGraphData($podcastId);
9898

9999
if ($results === false) {
100-
return [];
100+
return new Response(json_encode([]), 'application/json');
101101
}
102102

103103
$trackedMonths = $results->toArray();
@@ -109,15 +109,15 @@
109109
$month = (int)$entry->month;
110110
$downloads = (int)$entry->downloads;
111111

112-
if(!isset($downloadArray[$year])) {
112+
if (!isset($downloadArray[$year])) {
113113
$downloadArray[$year] = [];
114114
}
115115

116116
$downloadArray[$year][$month] = $downloads;
117117
}
118118

119-
foreach($downloadArray as $year => $months) {
120-
for($i = 1; $i <= 12; $i++) {
119+
foreach ($downloadArray as $year => $months) {
120+
for ($i = 1; $i <= 12; $i++) {
121121
$downloadJson[] = [
122122
'downloads' => $months[$i] ?? 0,
123123
'year' => $year,
@@ -126,7 +126,7 @@
126126
}
127127
}
128128

129-
return ['downloads' => $downloadJson];
129+
return new Response(json_encode(['downloads' => $downloadJson]), 'application/json');
130130
},
131131
],
132132
[
@@ -150,7 +150,7 @@
150150
$results = $stats->getQuickReports($podcastId, $year, $month);
151151

152152
if ($results === false) {
153-
return ['reports' => []];
153+
return new Response(json_encode(['reports' => []]), 'application/json');
154154
}
155155

156156
$trackedDays = $results['detailed']->toArray();
@@ -182,7 +182,7 @@
182182
'overall' => $allTime,
183183
];
184184

185-
return ['reports' => $reports];
185+
return new Response(json_encode(['reports' => $reports]), 'application/json');
186186
},
187187
],
188188
[
@@ -200,17 +200,18 @@
200200
$results = $stats->getEpisodeGraphData($podcastId, $episode);
201201

202202
if ($results === false) {
203-
return [];
203+
return new Response(json_encode([]), 'application/json');
204204
}
205205

206206
$cleandUpResult = [];
207-
foreach($results->toArray() as $result) {
207+
foreach ($results->toArray() as $result) {
208208
$cleandUpResult[] = [
209209
'date' => $result->date,
210210
'downloads' => (int)$result->downloads
211211
];
212212
}
213-
return $cleandUpResult;
213+
214+
return new Response(json_encode($cleandUpResult), 'application/json');
214215
},
215216
],
216217
[
@@ -228,18 +229,18 @@
228229
$results = $stats->getDevicesGraphData($podcastId, $year, $month);
229230

230231
if ($results === false) {
231-
return [];
232+
return new Response(json_encode([]), 'application/json');
232233
}
233234

234235
$total = 0;
235236
foreach ($results->toArray() as $result) {
236237
$total += (int)$result->downloads;
237238
}
238239

239-
return [
240+
return new Response(json_encode([
240241
'total' => $total,
241242
'data' => $results->toArray(),
242-
];
243+
]), 'application/json');
243244
},
244245
],
245246
[
@@ -257,18 +258,18 @@
257258
$results = $stats->getUserAgentGraphData($podcastId, $year, $month);
258259

259260
if ($results === false) {
260-
return [];
261+
return new Response(json_encode([]), 'application/json');
261262
}
262263

263264
$total = 0;
264265
foreach ($results->toArray() as $result) {
265266
$total += (int)$result->downloads;
266267
}
267268

268-
return [
269+
return new Response(json_encode([
269270
'total' => $total,
270271
'data' => $results->toArray(),
271-
];
272+
]), 'application/json');
272273
},
273274
],
274275
[
@@ -286,18 +287,18 @@
286287
$results = $stats->getSystemGraphData($podcastId, $year, $month);
287288

288289
if ($results === false) {
289-
return [];
290+
return new Response(json_encode([]), 'application/json');
290291
}
291292

292293
$total = 0;
293294
foreach ($results->toArray() as $result) {
294295
$total += (int)$result->downloads;
295296
}
296297

297-
return [
298+
return new Response(json_encode([
298299
'total' => $total,
299300
'data' => $results->toArray(),
300-
];
301+
]), 'application/json');
301302
},
302303
],
303304
[
@@ -315,10 +316,10 @@
315316
$results = $stats->getTopEpisodes($podcastId);
316317

317318
if ($results === false) {
318-
return [];
319+
return new Response(json_encode([]), 'application/json');
319320
}
320321

321-
return $results->toArray();
322+
return new Response(json_encode($results->toArray()), 'application/json');
322323
},
323324
],
324325
[
@@ -334,13 +335,13 @@
334335
$rssFeed = $podcastTools->getPodcastFromId($podcastId);
335336

336337
if (!isset($rssFeed)) {
337-
return [];
338+
return new Response(json_encode([]), 'application/json');
338339
}
339340

340341
$episodes = $podcastTools->getEpisodes($rssFeed);
341342

342343
if ($episodes === false) {
343-
return [];
344+
return new Response(json_encode([]), 'application/json');
344345
}
345346

346347
$episodeList = [];
@@ -351,7 +352,7 @@
351352
];
352353
}
353354

354-
return $episodeList;
355+
return new Response(json_encode($episodeList), 'application/json');
355356
},
356357
],
357358
[
@@ -367,13 +368,13 @@
367368
$rssFeed = $podcastTools->getPodcastFromId($podcastId);
368369

369370
if (!isset($rssFeed)) {
370-
return ['estSubscribers' => 0];
371+
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
371372
}
372373

373374
$episodes = $podcastTools->getEpisodes($rssFeed);
374375

375376
if ($episodes === false || !isset($episodes)) {
376-
return ['estSubscribers' => 0];
377+
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
377378
}
378379

379380
$latestEpisodes = $episodes
@@ -382,7 +383,7 @@
382383
});
383384

384385
if (!isset($latestEpisodes)) {
385-
return ['estSubscribers' => 0];
386+
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
386387
}
387388

388389
$latestEpisodes = $latestEpisodes->limit(3);
@@ -393,7 +394,7 @@
393394
}
394395

395396
if (count($episodeList) === 0) {
396-
return ['estSubscribers' => 0];
397+
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
397398
}
398399

399400
$dbType = option('mauricerenck.podcaster.statsType', 'sqlite');
@@ -402,7 +403,7 @@
402403
$results = $stats->getEstimatedSubscribers($podcastId, $episodeList);
403404

404405
if ($results === false || count($results) === 0) {
405-
return ['estSubscribers' => 0];
406+
return new Response(json_encode(['estSubscribers' => 0]), 'application/json');
406407
}
407408

408409
$estSubscribers = 0;
@@ -412,7 +413,7 @@
412413

413414
$subs = $estSubscribers / count($results);
414415

415-
return ['estSubscribers' => $subs];
416+
return new Response(json_encode(['estSubscribers' => $subs]), 'application/json');
416417
},
417418
],
418419
],

0 commit comments

Comments
 (0)