We are unlikely to be sending XHTML, or want to be.
[adorno.git] / www / index.php
blob1858029799b4f3626c91c54f293783764765a116
1 <?php
2 require_once("always.php");
3 require_once("Session.php");
4 require_once("PlayTracks.php");
6 $title = $system_name;
7 require_once("header.php");
9 function track_link( $trk, $link_title, $row_class = "" ) {
10 global $letter_get;
12 $track_link_url = sprintf( "?l=%s&a=%s&t=%s", urlencode($trk->album), urlencode($trk->artist)
13 , urlencode($trk->title) );
14 $safe_title = htmlspecialchars($link_title);
15 $duration = preg_replace( "/^[0:]{1,4}/", "", $trk->duration );
16 $link = <<<EOHTML
17 <tr class="track$row_class">
18 <td class="track$row_class">
19 <a class="track$row_class" href="artist.php$track_link_url$letter_get" title="$trk->path_na
20 me">$safe_title</a>
21 </td>
22 <td class="track$row_class">$duration</td>
23 <th class="track$row_class">
24 <a class="alphabetica" href="edit_track.php$track_link_url" title="Edit Track Info">E</a>
25 </th>
26 </tr>
28 EOHTML;
30 return $link;
33 function current_queue() {
34 if ( ! file_exists( "/var/run/adorno/queue.txt" ) ) return;
35 $fd = fopen( "/var/run/adorno/queue.txt", "r" );
37 /**
38 * Build an array of the tracks
40 if ( $fd ) {
41 $track = fgets( $fd, 300 ); // Skip the "now playing" - it showed at the top.
42 $track = fgets( $fd, 300 ); // Skip the start time...
43 $queue_pos = array();
44 $position = 0;
45 $in_list = "";
46 while( !feof( $fd ) ) {
47 $track = fgets( $fd, 300 );
48 $track = rtrim( $track, "\r\n");
49 $in_list .= ($position == 0 ? "" : ", ") . qpg($track);
50 $queue_pos[$track] = $position++;
52 fclose($fd);
55 /**
56 * Select the track information from the database
58 $sql = sprintf("SELECT *, EXTRACT( 'epoch' FROM duration ) AS dur_secs FROM tracks WHERE path_name IN ( %s );", $in_list );
59 $qry = new PgQuery( $sql );
60 $queue = array();
61 if ( $qry->Exec("current_queue",__LINE__,__FILE__) && $qry->rows ) {
62 while( $track = $qry->Fetch() ) {
63 $position = $queue_pos[$track->path_name];
64 $queue[$position] = $track;
68 return $queue;
71 $album_artist_fmt = <<<EOHTML
73 <tr class="q_headline">
74 <td class="q_cell" colspan="3">
76 </td>
77 </tr>
78 EOHTML;
80 $artist_link_fmt = '<a href="/artist.php?a=%s"><span class="q_artist">%s</span></a>';
81 $album_link_fmt = '<a href="/album.php?l=%s"><span class="q_album">%s</span></a>';
83 $track_fmt = <<<EOHTML
85 <tr class="q_row">
86 <td class="q_cell" title="Position in queue">%d</td>
87 <td class="q_cell" title="Track number and name">
88 <span class="q_title">%d - %s</span>
89 </td>
90 <td class="q_cell" title="When this track will start playing">%s</td>
91 </tr>
92 EOHTML;
94 $tracks = current_queue();
95 echo '<table id="queue">';
96 $last_artist = "";
97 $last_album = "";
98 $when = $current_track->finishing;
99 foreach( $tracks AS $k => $v ) {
100 $position = $k + 1;
101 $escape_artist = urlencode($v->artist);
102 $escape_album = urlencode($v->album);
104 if ( $escape_artist != $last_artist || $escape_album != $last_album ) {
105 if ( $escape_artist != $last_artist && $escape_album != $last_album ) {
106 $links = sprintf( $album_link_fmt, $escape_album, $v->album);
107 $links .= "/" . sprintf( $artist_link_fmt, $escape_artist, $v->artist );
109 else if ( $escape_artist != $last_artist ) {
110 $links = sprintf( $artist_link_fmt, $escape_artist, $v->artist );
112 else if ( $escape_album != $last_album ) {
113 $links = sprintf( $album_link_fmt, $escape_album, $v->album);
115 printf( $album_artist_fmt, $links );
116 $last_artist = $escape_artist;
117 $last_album = $escape_album;
119 printf( $track_fmt, $position, $v->tracknum, $v->title, date('H:i:s', $when ) );
120 $when += $v->dur_secs;
123 echo "</table>\n";
126 </body>
127 </html>