<?
header('Content-type: application/xml');

// Constants
    $List_Recordings = 11;
    $MythWeb_Site    = "http://your.mythtv.site/mythweb/";

// Which section are we in?
//    define('section', 'tv');

// Initialize the script, database, etc.
    require_once "includes/init.php";
    require_once "includes/programs.php";
    require_once "includes/sorting.php";

// Parse the program list
    $warning    = NULL;
    $recordings = get_backend_rows('QUERY_RECORDINGS Delete');
    while (true) {
        $Total_Used     = 0;
        $Total_Programs = 0;
        $Programs       = array();
        $Groups         = array();
        $Program_Titles = array();

        foreach ($recordings as $key => $record) {
        // Skip the offset
            if ($key === 'offset')  // WHY IN THE WORLD DOES 0 == 'offset'?!?!?  so we use ===
                continue;
        // Get the length (27 == recendts; 26 == recstartts)
            $length = $record[27] - $record[26];
        // keep track of their names and how many episodes we have recorded
            $Total_Programs++;
            $Groups[$record[30]]++;
        // Hide LiveTV recordings from the title list
            if (($_GET['recgroup'] && $_GET['recgroup'] == $record[30]) || (!$_GET['recgroup'] && $record[30] != 'LiveTV'))
                $Program_Titles[$record[0]]++;
        // Skip files with no chanid, or with zero length
            if (!$record[4] || $length < 1)
                continue;
        // Hide livetv recordings from the default view
            if (empty($_GET['recgroup']) && $record[30] == 'LiveTV')
                continue;
        // Make sure that everything we're dealing with is an array
            if (!is_array($Programs[$record[0]]))
                $Programs[$record[0]] = array();
        // Assign a reference to this show to the various arrays
            $Programs[$record[0]][] = $record;
        }

    // Did the best we could to find some programs; let's move on.
        break;
    }

// Now that we've selected only certain shows, load them into objects
    $All_Shows = array();
    foreach ($Programs as $title => $shows) {
        foreach ($shows as $key => $record) {
        // Create a new program object
            $show =& new Program($record);
        // Assign a reference to this show to the various arrays
            $All_Shows[]                         =& $show;
            $Programs[$title][$key]              =& $show;
            $Channels[$show->chanid]->programs[] =& $show;
            unset($show);
        }
    }

// Sort the program titles
    ksort($Program_Titles);


// The default sorting choice isn't so good for recorded programs, so we'll set our own default
    if (!is_array($_SESSION['recorded_sortby']) || !count($_SESSION['recorded_sortby']))
        $_SESSION['recorded_sortby'] = array(array('field' => 'airdate',
                                                   'reverse' => true),
                                             array('field' => 'title',
                                                   'reverse' => false));

// Create the show listings
    if (count($All_Shows)) {

    // Sort the programs
        sort_programs($All_Shows, 'recorded_sortby');

    // Create the top section
        $builddate = date("r", time());
        $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
        $xml .= "<rss version=\"2.0\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
        $xml .= "<channel>\n";
        $xml .= "<title>Latest Recordings</title>\n";
        $xml .= "<link>".$MythWeb_Site."</link>\n";
        $xml .= "<description>List of the Last ".$List_Recordings." MythTV Recordings</description>\n";
        $xml .= "<language>en-us</language>\n";
        $xml .= "<lastBuildDate>{$builddate}</lastBuildDate>\n";

    // Create the per-show listing
        if (sizeof($All_Shows) > 0) {
            $i = 1;
            foreach ($All_Shows as $show) {

            // Create the title from the show title and subtitle (if it exists)
                if ($show->subtitle) $title = $show->title . ' (' . $show->subtitle . ')'; else $title = $show->title;

                $xml .= "<item>\n";
                $xml .= "<title>".htmlspecialchars($title,ENT_QUOTES)."</title>\n";
                $xml .= "<link>".$MythWeb_Site."tv/recorded?title=".urlencode($show->title)."</link>\n";
                $xml .= "<description>".htmlspecialchars($show->description,ENT_QUOTES)."</description>\n";
                $xml .= "<pubDate>".htmlspecialchars(date('c', $show->starttime),ENT_QUOTES)."</pubDate>\n";
                $xml .= "</item>\n";

                if ($i++ >= $List_Recordings) break;
            }
        }

        $xml .= "</channel>\n";
        $xml .= "</rss>\n";

    } else {
    // No shows to list (error?)
        $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
        $xml .= "<sitedata>";
        $xml .= "<error>No recordings could be found</error>";
        $xml .= "</sitedata>";
    }

// Print the listing
    print $xml;

?>