<!-- $Id: 18listen.php 1036 2009-07-21 12:57:49Z bb $ -->

<?php

/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
| Scriptname : Info Sender's PHP Sidekick v1.0
| Author     : appel [-at-] nr78.net
| Website    : http://nr78.net/sidekick
* ----------------------------------------------------------------

Keeps a list of recently played songs. Can be fed by the InfoSender
Winamp plugin.

@param song: optional string
    The name of the artist and the song title, separated by " - ".

This script stores the song titles sent by Info Sender into a
flat file. Simply include this file on the spot where you want
it to appear. Also, be sure to create a file and chmod it 666,
or it will choke.

The file records one song per line, in the format

Artist||Title||Time||IP-address

A song that has not been playing for at least 30 seconds is removed
from the list, so that skipped songs are not displayed.

Parameters are configurable below.

This script is obviously free, so use/modify it anyway you want,
just let me in on any changes so I can update this puppy.
A link to nr78.net is of course appreciated, but not mandatory.
Contact me if you like to change something, but don't know how.

Tested with Info Sender for Winamp 1.1. For more information
on Info Sender, check out the author's website at:
http://www.neuro-tech.net/infosender.html

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= */

// Configuration:

// File where the song list is stored:
$filename = "{$_SERVER['DOCUMENT_ROOT']}/lib/winamp.txt";

// Number of songs to keep in the list:
$nkeep = 1000;

// Number of songs to display:
$ndisp = 8;

// Song must have been playing at least:
$minplay = 30;    // seconds

// End of configuration


// Read the song list into an array
$songs = explode("\n", @file_get_contents($filename));
$nsongs = sizeof($songs);

// Check what parameters have been passed to the script and whether the
// script runs stand-alone
$song = urldecode($_SERVER['QUERY_STRING']);
$lastfm = (isset($_GET['lastfm']) && $_GET['lastfm'] == '1');

if (
    ($lastfm || $song && strstr($song, ' - ')) &&
    strstr($_SERVER['SCRIPT_URL'], 'listen.php')
) {
    if (!is_writable($filename)) {
    echo "- The song store is not writable";
    exit;
    }

    if ($lastfm) {
    $song = $_GET['title'];
    $when = $_GET['when'];
    }
    else {
    // convert to UTF-8
    $song = mb_convert_encoding($song, "UTF-8", "auto");
    $when = strftime('%Y-%m-%dT%H:%M%z');
    }
    $song = preg_replace('/ - /', '||', $song, 1);

    if ($lastfm)
    // Don't add the song if there's one with the same time
    // or the song was played before the first song in the store
    for ($i = 0; $i < $nsongs; $i++) {
        $fields = explode('||', $songs[$i]);
        if ($fields[2] == $when || $i == 0 && $when < $fields[2]) {
        echo "- $song was already played on $when";
        return;
        }
    }
    else
    // Remove the last song if it has not been playing long enough
    // or if it is the same as the current one
    if (filemtime($filename) + $minplay > time() || strpos($songs[$nsongs - 1], $song) === 0) {
        array_pop($songs);
        echo "- Removed song played last<br />";
    }

    // Format the new song record
    $songrec = "$song||$when||" . $_SERVER['REMOTE_ADDR'];

    // Append the new song and keep the list length limited
    array_push($songs, htmlentities(stripslashes($songrec)));
    $songs = array_slice($songs, -$nkeep);

    // Write the new song list
    $fh = fopen($filename, 'w');
    fwrite($fh, implode("\n", $songs));
    fclose($fh);
    echo "+ $songrec added";
}
elseif ($nsongs > 0) {

    // Show the full list if we were called with a "full" query string
    if ($song == 'full')
        $ndisp = $nkeep;

    // Return the formatted song list
    echo "<h3 style=\"font-size: 150%\" title=\"The last few songs I listened to\">♫<!-- U+266B BEAMED EIGHTH NOTES --></h3>

<dl>\n";
    foreach (array_reverse(array_slice($songs, -$ndisp)) as $song) {
    list($artist, $title, $time, $ip) = explode('||', $song);
    echo "  <dt>$artist</dt><dd title=\"at $time on $ip\">$title</dd>\n";
    }
    echo "</dl>\n";
}

?>