aboutsummaryrefslogtreecommitdiff
path: root/lib/helper.php
blob: 4beef4df9fd5f3e8545acad4268a73f50b462903 (plain)
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
<?php

require_once('program.php');

function link_index()
{
	return '';
}

function link_room($room)
{
	return rawurlencode($room).'/';
}

function link_player($room, $format = 'hd', $translated = false)
{
	$defaultformat = room_has_hd($room) ? 'hd' : 'sd';

	return rawurlencode($room).'/'.(($defaultformat == $format || 'video' == $format) ? '' : rawurlencode($format).'/').($translated ? 'translated/' : '');
}

function link_stream($protocol, $room, $format, $translated = false)
{
	$language = $translated ? 'translated' : 'native';

	switch ($protocol) {
		case 'rtmp':
			return 'rtmp://cdn.c3voc.de/stream/'.rawurlencode(streamname($room)).'_'.rawurlencode($language).'_'.rawurlencode($format);

		case 'hls':
			return 'http://cdn.c3voc.de/hls/'.rawurlencode(streamname($room)).'_'.rawurlencode($language).($format == 'auto' ? '' : '_'.rawurlencode($format)).'.m3u8';

		case 'webm':
			return 'http://cdn.c3voc.de/'.rawurlencode(streamname($room)).'_'.rawurlencode($language).'_'.rawurlencode($format).'.webm';

		case 'music':
			return 'http://cdn.c3voc.de/'.rawurlencode(streamname($room)).'.'.rawurlencode($format);

		case 'audio':
			return 'http://cdn.c3voc.de/'.rawurlencode(streamname($room)).'_'.rawurlencode($language).'.'.rawurlencode($format);

		case 'slide':
			return 'http://cdn.c3voc.de/slides/'.rawurlencode(streamname($room)).'/current.png';
	}

	return '#';
}

function link_vod($id)
{
	return 'relive/'.rawurlencode($id).'/';
}

function streamname($room)
{
	switch($room)
	{
		case 'saal1': return 's1';
		case 'saal2': return 's2';
		case 'saalg': return 's3';
		case 'saal6': return 's4';
		case 'sendezentrum': return 's5';
		default: return $room;
	}
}

function irc_channel($room)
{
	return '31C3-hall-'.strtoupper(substr($room, 4, 1));
}

function twitter_hashtag($room)
{
	return '#hall'.strtoupper(substr($room, 4, 1));
}

function format_text($format)
{
	return @$GLOBALS['CONFIG']['FORMAT_TEXT'][$format] ?: '';
}

function baseurl()
{
	if(isset($GLOBALS['CONFIG']['baseurl']))
		return $GLOBALS['CONFIG']['baseurl'];

	$base  = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) ? 'https://' : 'http://';
	$base .= $_SERVER['HTTP_HOST'];
	$base .=  rtrim(dirname($_SERVER['SCRIPT_NAME']), '/').'/';

	return $base;
}

function strtoduration($str)
{
	$parts = explode(':', $str);
	return ((int)$parts[0] * 60 + (int)$parts[1]) * 60;
}

function has($keychain)
{
	return _has($GLOBALS['CONFIG'], $keychain);
}
function _has($array, $keychain)
{
	if(!is_array($keychain))
		$keychain = explode('.', $keychain);

	$key = $keychain[0];
	if(!isset($array[$key]))
		return false;

	if(count($keychain) == 1)
		return true;

	return _has($array[$key], array_slice($keychain, 1));
}

function get($keychain, $default = null)
{
	return _get($GLOBALS['CONFIG'], $keychain, $default);
}
function _get($array, $keychain, $default)
{
	if(!is_array($keychain))
		$keychain = explode('.', $keychain);

	$key = $keychain[0];
	if(!isset($array[$key]))
		return $default;

	if(count($keychain) == 1)
		return $array[$key];

	return _get($array[$key], array_slice($keychain, 1), $default);
}



function room_has_hd($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('rtmp-hd', 'hls-hd', 'webm-hd'), $formats)) > 0;
}

function room_has_sd($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('rtmp-sd', 'hls-sd', 'webm-sd'), $formats)) > 0;
}

function room_has_video($room)
{
	return room_has_hd($room) || room_has_sd($room);
}

function room_has_audio($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('audio-mp3', 'audio-opus', 'audio-ogg'), $formats)) > 0;
}

function room_has_music($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('music-mp3', 'music-opus', 'music-ogg'), $formats)) > 0;
}

function room_has_slides($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('slides'), $formats)) > 0;
}

function room_has_rtmp($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('rtmp-hd', 'rtmp-sd'), $formats)) > 0;
}

function room_has_webm($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('webm-hd', 'webm-sd'), $formats)) > 0;
}

function room_has_hls($room)
{
	$formats = get("ROOMS.$room.FORMATS");
	return count(array_intersect(array('hls-hd', 'hls-sd'), $formats)) > 0;
}

function room_has_irc($room)
{
	return get("ROOMS.$room.IRC") && has("IRC");
}

function room_has_twitter($room)
{
	return get("ROOMS.$room.TWITTER") && has("TWITTER");
}

function room_has_chat($room)
{
	return room_has_irc($room) || room_has_twitter($room);
}

function room_get_irc_url($room)
{
	$cfg = get("ROOMS.$room.IRC_CONFIG", get("IRC"));
	return sprintf($cfg['URL'], rawurlencode($room));
}

function room_get_irc_display($room)
{
	$cfg = get("ROOMS.$room.IRC_CONFIG", get("IRC"));
	return sprintf($cfg['DISPLAY'], $room);
}

function room_get_twitter_hashtag($room)
{
	$cfg = get("ROOMS.$room.TWITTER_CONFIG", get("TWITTER"));
	return sprintf($cfg['TEXT'], $room);
}

function room_get_twitter_display($room)
{
	$cfg = get("ROOMS.$room.TWITTER_CONFIG", get("TWITTER"));
	return sprintf($cfg['DISPLAY'], $room);
}

function startswith($needle, $haystack)
{
	return substr($haystack, 0, strlen($needle)) == $needle;
}

function relive_talks()
{
	$talks = @file_get_contents(get('OVERVIEW.RELIVE_JSON'));
	$talks = utf8_decode($talks);
	$talks = (array)json_decode($talks, true);

	usort($talks, function($a, $b) {
		$sort = array('live', 'recorded', 'released');
		return array_search($a['status'], $sort) > array_search($b['status'], $sort);
	});

	$talks_by_id = array();
	foreach ($talks as $value)
	{
		$talks_by_id[$value['id']] = $value;
	}

	return $talks_by_id;
}