~extremepopcorn/dhlib/dhlib_ep

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
<?php
	include 'std.inc.php';
	include 'radio_data.php';
	
	setup_xml_response();
	
	//don't have many songs now, so allow them to play more often
	$remember_song_count = 10;
	
	//use a token to prevent the songs playing multiple times and
	//to alternate between commerical and theme songs
	//FORMAT: token, token, ...
	$tokenstr = get_request_def( 'token', '' );
	//limit length, for input cleaning
	$tokenstr = substr( $tokenstr, 0, 1000 );
	$tokens = explode( ',',  $tokenstr );
	
	//remove those already played
	$remain_comm = array_diff( $comm, $tokens );
	$remain_demo = array_diff( $demo, $tokens );
	
	//restore empty lists
	if( count( $remain_comm ) == 0 )	
		$remain_comm = $comm;
	if( count( $remain_demo ) == 0 )
		$remain_demo = $demo;
	
	//decide what kind to play next (alternative between comm/demo)
	if( count( $tokens ) > 0 ) 
	{
		$last = array_pop( array_slice( $tokens, -1, 1 ) );
		if( array_search( $last, $comm ) !== FALSE )
			$selectfrom = $remain_demo;
		else
			$selectfrom = $remain_comm;
	}
	else
		$selectfrom = $remain_comm;
		
	//choose a track at random
	$select = $selectfrom[ array_rand( $selectfrom ) ];
	$song = $songs[$select];
	
	//append to token so client must not keep track
	$tokens[] = $select;
	if( count( $tokens ) > $remember_song_count )	//remember only X songs?
		$tokens = array_slice( $tokens, -$remember_song_count );
	$tokenstr = implode( ',', $tokens );
	
	print <<<EOT
<track id="$select" token="$tokenstr">
	<sound>${song['sound']}</sound>
	<image>${song['image']}</image>
	<about>${song['about']}</about>
	<title>${song['title']}</title>
	<album>${song['album']}</album>
	<artist>${song['artist']}</artist>
EOT;
	
	foreach( $song['purchase'] as $p ) {
		print( "<purchase name='" . htmlspecialchars( $p['name'], ENT_QUOTES )
			. "' location='" . htmlspecialchars( $p['location'], ENT_QUOTES )
			. "'>" . $p['link'] . "</purchase>\n" );
	}
	
	print( '</track>' );
?>