Removing need for register_globals
[adorno.git] / inc / always.php
blob4553af7503900e45b0499460cbf4a9c53997b9a1
1 <?php
2 $client_messages = array();
4 error_log( "=============================================== $PHP_SELF" );
5 include_once("../config/config.php");
6 include_once("PgQuery.php");
8 $letter_get = ( $_GET['altr'] == "" ? "" : "&altr=".$_GET['altr'] ) . ( $_GET['lltr'] == "" ? "" : "&lltr=".$_GET['lltr'] );
9 if ( isset($_GET['search']) && $_GET['search'] != "" ) $letter_get .= "&search=".urlencode($_GET['search']);
12 function nice_track_name( $track ) {
13 $track = ereg_replace( "^/", "", $track );
14 $track = eregi_replace( "^music/", "", $track );
15 $track = eregi_replace( "^extra/", "", $track );
16 $track = eregi_replace( "^Classified/", "", $track );
17 $track = str_replace( "http://", "Stream/ ", $track );
18 $track = eregi_replace( "\.(mp3)|(ogg)$", "", $track );
19 $track = eregi_replace( "^/+", "", $track );
20 $track = eregi_replace( "/+$", "", $track );
21 $track = str_replace( "_", " ", $track );
22 $track = str_replace( "/", " &raquo; ", $track );
23 return $track;
26 function show_queue() {
27 if ( ! file_exists( "/var/run/adorno/queue.txt" ) ) return;
28 $fd = fopen( "/var/run/adorno/queue.txt", "r" );
29 if ( $fd ) {
30 $track = fgets( $fd, 300 ); // Skip the "now playing" - it showed at the top.
31 // echo "<table width=100% bgcolor=#ffffa0><tr><td><h2>Now Playing:&nbsp; &nbsp;</h2></td><td align=right><h2>" . nice_track_name($track) . "</h2></td></tr></table>\n<p>";
32 $track = fgets( $fd, 300 ); // Skip the start time...
33 echo "<h3>Coming up...</h3>\n<p class=\"track_queue\">";
34 while( !feof( $fd ) ) {
35 $track = fgets( $fd, 300 );
36 echo nice_track_name($track) . "<br>\n";
38 fclose($fd);
39 echo "</p>";
43 ///////////////////////////////////////////////////////////////////////////////////////////////
44 // Log the query
45 ///////////////////////////////////////////////////////////////////////////////////////////////
46 function log_query( $locn, $tag, $took, $query ) {
47 global $sysabbr;
49 while( strlen( $query ) > 0 ) {
50 error_log( "$sysabbr $locn $tag: $took" . "S for: " . substr( $query, 0, 220) , 0);
51 $query = substr( "$query", 220 );
53 return "";
57 ///////////////////////////////////////////////////////////////////////////////////////////////
58 // Should be a drop-in replacement for awm_pgexec($conn,$query)
59 // - can set global variable $sysabbr to identify system in logs
60 // - successful queries are timed and logged to syslog.
61 // - failed queries are logged and contain string "QF:"
62 ///////////////////////////////////////////////////////////////////////////////////////////////
63 $total_query_time = 0.0 ;
64 $query_time_warning = 6;
65 function awm_pgexec( $myconn, $query, $location="", $abort_on_fail=false, $mydbg=0 ) {
66 global $sysabbr, $debuglevel, $total_query_time, $REQUEST_URI, $query_time_warning;
68 $a1 = microtime();
69 $result = pg_Exec( $myconn, $query );
70 $a2 = microtime();
71 $locn = sprintf( "%-12.12s", $location);
72 $taken = sprintf( "%2.06lf", duration( $a1, $a2 ));
73 $total_query_time += $taken;
74 if ( !$result && $abort_on_fail ) {
75 $result = pg_Exec( $dbconnection, "ABORT;" );
76 if ( $debuglevel > 4 || $mydbg > 4) log_query( $locn, "QFABRT", $taken, $query );
78 else if ( !$result ) {
79 log_query( $locn, "QF", pg_errormessage($myconn), $query );
81 else if ( $debuglevel > 4 || $mydbg > 4 ) {
82 error_log( "$sysabbr $locn URI: $REQUEST_URI", 0);
83 log_query( $locn, "QT", $taken, $query );
85 else if ( $debuglevel > 2 || $mydbg > 2 ) {
86 log_query( $locn, "QT", $taken, substr( $query, 0,200) );
88 else if ( $taken > $query_time_warning ) {
89 log_query( $locn, "SQ", $taken, $query );
92 return $result;
95 function current_track() {
96 global $dbconn;
98 if ( ! file_exists( "/var/run/adorno/queue.txt" ) ) return;
99 $fd = fopen( "/var/run/adorno/queue.txt", "r" );
100 if ( $fd ) {
101 $track = fgets( $fd, 300 );
102 $track = trim( $track, "\n" );
103 $started = fgets( $fd, 300 );
104 $started = trim( $started, "\n" );
105 fclose($fd);
107 $query = "SELECT *, extract( EPOCH FROM duration)::int AS secs ";
108 if ( ereg( '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}', $started ) ) {
109 $query .= ", extract( EPOCH FROM ('$started'::timestamp + duration))::int AS finishing ";
111 $query .= "FROM tracks WHERE path_name = '" . str_replace("'","''", str_replace("\\", "\\\\", $track)) . "' ; ";
112 // echo "$query<br>\n";
113 $result = awm_pgexec( $dbconn, $query, 'currtrack', false, 7 );
114 if ( $result && pg_NumRows($result) > 0 ) {
115 // echo "Query successful<br>\n";
116 $track = pg_Fetch_Object( $result, 0);
117 return $track;
119 return;