function _pugpig_package_test_endpoints($endpoints, $timestamp, $tmp_root)
{
    pugpig_interface_output_header("Pugpig - Endpoint Checker");
    print_r("<h1>Checking Pugpig End Points</h1>");
    $tmp_root = str_replace(DIRECTORY_SEPARATOR, '/', $tmp_root);
    $tmp_path = $tmp_root . 'package-' . $timestamp . '/';
    $entries = array();
    $c = 0;
    foreach ($endpoints as $endpoint) {
        if ($endpoint != '') {
            $save_path = $tmp_path . 'opds/' . hash('md5', $endpoint) . '.xml';
            $entries[$endpoint] = $save_path;
        }
    }
    $debug = FALSE;
    $concurrent = 1;
    $entries = _pugpig_package_download_batch("OPDS Feeds", $entries, $debug, $concurrent);
    $format_failures = array();
    foreach (array_keys($entries) as $entry) {
        // print_r($entry . " ---> " . $entries[$entry] . "<br />");
        // Read the ATOM from the file
        $fhandle = fopen($entries[$entry], 'r');
        $opds_atom = fread($fhandle, filesize($entries[$entry]));
        fclose($fhandle);
        $msg = check_xml_is_valid($opds_atom);
        if ($msg != '') {
            $format_failures[$entry] = "OPDS XML Invalid: " . $msg;
            $opds_atom = '';
        }
        $opds_ret = _pugpig_package_parse_opds($opds_atom);
        $edition_roots = array();
        $package_roots = array();
        print_r("<h2>" . $entry . "(" . $opds_ret['title'] . ")</h2>");
        foreach ($opds_ret['editions'] as $edition) {
            $cover = url_to_absolute($entry, $edition['cover']);
            print_r("<img class='cover " . ($edition['free'] ? "free" : "paid") . "' height='60' title='" . $edition['title'] . ': ' . $edition['summary'] . "' src='" . $cover . "' />");
            $edition_root = url_to_absolute($entry, $edition['url']);
            $save_path = $tmp_path . $edition['type'] . '/' . hash('md5', $edition_root) . '.xml';
            $edition_roots[$edition_root] = $save_path;
            if ($edition['type'] == 'package') {
                $package_roots[] = $edition_root;
            }
        }
        $edition_roots = _pugpig_package_download_batch("Edition Roots", $edition_roots, $debug, $concurrent);
        $format_failures = array();
        foreach ($package_roots as $package_root) {
            $save_path = $edition_roots[$package_root];
            $fhandle = fopen($save_path, 'r');
            $package_xml_body = fread($fhandle, filesize($save_path));
            fclose($fhandle);
            $msg = check_xml_is_valid($package_xml_body);
            if ($msg != '') {
                $format_failures[$package_root] = "Package XML Invalid: " . $msg;
                $opds_atom = '';
            }
        }
        // Show package format errros
        _pugpig_package_show_failures($format_failures);
    }
    _pugpig_package_show_failures($format_failures);
}
function showEditionsAndCovers($user, $opds, $edition_file_root)
{
    $entries = array();
    $save_path = pugpig_get_local_save_path($edition_file_root, $opds);
    // Remove the query string
    $save_path = preg_replace('/\\/?\\?.*/', '', $save_path);
    $entries[$opds] = $save_path;
    $entries = _pugpig_package_download_batch("OPDS Feeds", $entries);
    $format_failures = array();
    foreach (array_keys($entries) as $entry) {
        // Read the ATOM from the file
        $fhandle = fopen($entries[$entry], 'r');
        $opds_body = fread($fhandle, filesize($entries[$entry]));
        fclose($fhandle);
        // Parse the OPDS file
        $opds_ret = _pugpig_package_parse_opds($opds_body);
        if (!empty($opds_ret['failure'])) {
            echo "<font color='red'>Not Valid OPDS: " . $opds_ret['failure'] . "</font>";
            return;
        }
        echo "<h1>Your Editions</h1>";
        $covers = array();
        echo "<table>";
        foreach ($opds_ret['editions'] as $edition) {
            echo "<tr>";
            $cover_url = url_to_absolute($opds, $edition['cover']);
            $atom_url = url_to_absolute($opds, $edition['url']);
            $cover_save_path = pugpig_get_local_save_path($edition_file_root, $cover_url);
            // $save_path = $edition_file_root . 'cover/' . hash('md5', $edition['cover']). '.jpg';
            if (count($covers) < 10) {
                $covers[$cover_url] = $cover_save_path;
                // showSingleEdition($user, $opds, $atom_url, $edition_file_root);
            }
            echo "<td><img height='80' src='" . $cover_url . "' /></td>";
            echo "<td>";
            echo "<b>" . $edition['title'] . "</b><br />";
            echo "<i>" . $edition['summary'] . "</i><br />";
            $updated_ts = strtotime($edition['updated']);
            echo _ago($updated_ts) . " ago) - (" . $edition['updated'] . ") ({$updated_ts})<br />";
            echo $edition['draft'] ? "<font color='orange'>DRAFT</font> " : "";
            echo ($edition['free'] ? "free" : "paid") . ($edition['samples'] ? " with samples" : "");
            echo "<br />";
            echo "</td>";
            echo "<td>";
            //echo count($edition['categories']) . " categories";
            foreach ($edition['categories'] as $schema => $term) {
                echo "<b>{$schema}</b>: {$term}<br />";
            }
            echo "</td>";
            echo "<td>";
            if ($edition['type'] == 'atom') {
                $q = http_build_query(array('opds' => $opds, 'atom' => $atom_url, 'user' => $user));
                echo "<a href='?{$q}'>TEST PAGES</a><br />\n";
            } else {
                echo "EPUB<br />";
            }
            echo "<a href='" . url_to_absolute($opds, $atom_url) . "' target='_blank'>FEED</a></br />";
            echo "FLATPLAN</br />";
            echo "PREVIEW IN WEB<br />";
            echo "</tr>";
        }
        echo "</table>";
        $entries = _pugpig_package_download_batch("Valdating Covers (only 10)", $covers);
    }
}