Repository navigation
Expand file tree
/
Copy pathOutput.php
More file actions
199 lines (172 loc) · 5.62 KB
/
Copy pathOutput.php
File metadata and controls
199 lines (172 loc) · 5.62 KB
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
<?php
/**
* Radio-API
* https://github.com/KIMB-technologies/Radio-API
*
* (c) 2019 - 2026 KIMB-technologies
* https://github.com/KIMB-technologies/
*
* released under the terms of GNU Public License Version 3
* https://www.gnu.org/licenses/gpl-3.0.txt
*/
defined('HAMARadio') or die('Invalid Endpoint');
enum OutputPlayStatus {
case Info;
case Full;
case Play;
}
/**
* Radio-API XML Style Output Class
*/
mb_substitute_character("none");
abstract class Output {
protected array $items = array();
protected array $itemsSortKeys = array();
protected string $prevUrl = '';
protected string $selfUrl = Config::RADIO_DOMAIN . '?go=initial';
protected string $selfTitle = 'Index';
protected int $language;
protected RadioLogo $logo;
const MAX_ITEMS = 200; // to many items will cause the radio to crash (one could add paging, but until then, we remove too much items)
const ALL_LANGUAGES = array(
'eng',
'ger'
);
const TRANSLATIONS = array(
'Index' => ['Index', 'Index'],
'Podcast' => ['Podcast', 'Podcast'],
'Radio' => ['Radio stations', 'Radiosender'],
'Radio-Browser' => ['Radio-Browser', 'Radio-Browser'],
'Stream' => ['Stream', 'Stream'],
'GUI-Code' => ['GUI-Code', 'GUI-Code'],
//
'Play Station' => ['Play Station', 'Sender abspielen'],
'Play Episode' => ['Play Episode', 'Episode abspielen'],
'Play Stream' => ['Play Stream', 'Stream abspielen'],
'Error' => ['Error', 'Fehler'],
'Episodes' => ['Episodes', 'Episoden'],
'List' => ['List', 'Liste'],
'Categories' => ['Categories', 'Kategorien'],
//
'Countries' => ['Countries', 'Länder'],
'All States' => ['All States', 'Alle Bundesländer'],
'Languages' => ['Languages', 'Sprachen'],
'My Last' => ['My Last', 'Meine zuletzt gehörten'],
'Tags' => ['Tags', 'Kategorien'],
'Top Click' => ['Top Click', 'Am meisten gehört'],
'Top Vote' => ['Top Vote', 'Am höchsten bewertet'],
'Next Page' => ['Next Page', 'Nächste Seite'],
'You do not have last stations.' => ['You do not have last stations.', 'Sie haben keine zuletzt gehörten Sender!']
);
/**
* Create Outputter
*/
public function __construct(){
$this->language = $this->detectLanguage();
$this->logo = new RadioLogo();
}
abstract protected function detectLanguage() : int;
/**
* Add a station
*/
abstract public function addStation(
int|string $id, string $name, string $url,
OutputPlayStatus $status = OutputPlayStatus::Full,
string $desc = '', string $logo = '', int|string $sortKey = ""
) : void;
/**
* Add a podcast
*/
abstract public function addPodcast(
int $podcastid, string $name, string $url,
int|string $sortKey = ""
) : void;
/**
* Add a podcast episode
*/
abstract public function addEpisode(
int $podcastid, int|null $episodeid, string $podcastname, string $episodename, string $url,
string $desc = '', string $logo = '', bool $top = false,
OutputPlayStatus $status = OutputPlayStatus::Full
) : void;
/**
* Add a folder
*/
abstract public function addDir(
string $name, string $url,
bool $isLast = false, int|string $sortKey = ""
) : void;
/**
* Set or override a Previous (<- Back URL)
*/
public function prevUrl(string $url) : void {
$this->prevUrl = $url;
}
/**
* Set or override the current URL and title
*/
public function currentUrl(string $url, string $title) : void {
$this->selfUrl = $url;
$this->selfTitle = $title;
}
protected function cleanText( string $s, bool $translate = false ): string {
if($translate){
$pos = strpos($s, ':');
$suffix = '';
if($pos !== false){
$suffix = substr($s, $pos);
$s = substr($s, 0, $pos);
}
if(array_key_exists($s, self::TRANSLATIONS)){
$s = self::TRANSLATIONS[$s][$this->language];
}
$s .= $suffix;
}
return mb_substr( mb_convert_encoding(str_replace( str_split('"&<>/'), '', $s ), 'UTF-8', 'UTF-8'), 0, 100 );
}
protected function cleanUrl( string $s, bool $addHttpS = true ): string {
$url = mb_convert_encoding(str_replace( str_split('<>'), '', $s ), 'UTF-8', 'UTF-8');
return str_starts_with($url, 'http://') && !$addHttpS ?
$url : // do not add an S here
'http' . ( empty($_SERVER['HTTPS']) ? ':' : 's:' ) . substr( $url, strpos( $url, '//') );
}
abstract protected function getItemName(array $item) : string;
protected function applyFavorites() : void {
$favorites = array_map('trim', explode(',', Config::FAVORITE_ITEMS));
foreach($this->items as $id => $item ){
list($type, $name) = explode( '==', $this->itemsSortKeys[$id] );
if(in_array($name, $favorites) || in_array($this->getItemName($item), $favorites)){
$this->itemsSortKeys[$id] = 'A'. $type . '==' . $name;
}
}
}
abstract protected function formatItems(array $items) : string;
/**
* Creates the xml/ json response
* and sends it!
*/
public function __destruct(){
$this->applyFavorites();
array_multisort($this->itemsSortKeys, SORT_ASC, SORT_NATURAL|SORT_FLAG_CASE, $this->items);
if( count( $this->items ) > self::MAX_ITEMS ){
$this->items = array_slice($this->items, 0, self::MAX_ITEMS);
}
$this->sendAnswer($this->formatItems($this->items), $this->contentType());
}
protected function contentType() : string {
return 'text/plain;charset=UTF-8';
}
/**
* Sends the given string to the radio, settings all headers and ends script!
*/
public static function sendAnswer(string $out, string $contentType = 'text/plain;charset=UTF-8') : void {
// header setup
header('Content-Type: ' . $contentType);
header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Pragma: no-cache');
header('Content-Length: ' . strlen( $out ));
die( $out );
}
}
?>