<?
# you can change the values in this box.
########################################################################
$max_count 20;          # number of entries to display               #
$date_format ="d M Y";    # see formatting at php.net/date             #
$username "tentaizu";   # del.icio.us username                       #
$cache_timeout 30;      # time between hits to del.icio.us servers   #
                          #  (in minutes)                              #
########################################################################
$dorkzilla false;       # enable dorkzilla.org-specific stuff        #
########################################################################
# ignore everything else.

$tree = array();
$entry_title;
$link;
$date;
$thisdate;
$description;
$tags = array();
$initem false;
$current_tag;

function 
startElement($parser$name$attrs)
{
    global 
$tree$initem$tags;
    switch(
$name)
    {
        case 
"RDF:LI":
            if (
$initem && !empty($attrs['RESOURCE']))
            {
                
$parts explode("/",$attrs['RESOURCE']);
                
array_push($tags,end($parts));
            }
            break;
        case 
"ITEM":
            
$initem true;
            break;
    }
    
array_push($tree,$name);
}

function 
endElement($parser,$name)
{
    global 
$entry_title$link$date$description$thisdate;
    global 
$tree,$initem$count,$max_count,$tags;
    
$current end($tree);

    if (
$count $max_count)
        switch(
$current)
        {
            case 
"ITEM":
                if (
$thisdate != $date)
                {
                    print 
"\n<div class=\"date\">$date</div>\n\n";
                    
$thisdate $date;
                }
?>
                
                <div class="entry">
                    <a href="<?=$link?>" class="title"><?=$entry_title?></a>
                    <div class="tags">
                        <?
                        
foreach($tags as $tag)
                            print 
"$tag ";
                    
?>
                    
                    </div>
                    <div class="description"><?=$description?></div>
                </div>
                <?
                $initem 
false;
                
$entry_title null;
                
$link null;
                
$date null;
                
$description null;
                
$tags = array();
                
$count++;
                break;
        }

    
array_pop($tree);
}

function 
characterData($parser$data)
{
    global 
$tree,$initem;
    global 
$entry_title$link$date$description;
    global 
$date_format;
    switch(
end($tree))
    {
        case 
"TITLE":
            if (
$initem)
                
$entry_title.= html_entity_decode($data);
        break;
        case 
"LINK":
            if (
$initem)
                
$link.= $data;
        break;
        case 
"DESCRIPTION":
            if (
$initem)
                
$description.= $data;
        break;
        case 
"DC:DATE":
            
$data preg_replace("/[a-zA-Z]/"," ",$data);
            if (
$initem)
                
$date.= date($date_format,strtotime($data));

        break;
    }
}


$tempdir = empty($_ENV['TMPDIR']) ? "/tmp" rtrim($_ENV['TMPDIR'],"/");

$url "http://del.icio.us/rss/$username";
$cachefile "$tempdir/del.icio.us-rss-$username";

$crusty strtotime("-$cache_timeout minutes");
if (!
is_file($cachefile) or filemtime($cachefile) < $crusty)
{
    
$urlfh fopen($url,"r") or $err "couldn't connect to del.icio.us";
    if (empty(
$err))
    {
        
$cachefh fopen($cachefile,"w") or
                                die (
"couldn't open cachefile");
        while (
$line fread($urlfh,4096))
            
fwrite($cachefh,$line);
        
fclose($urlfh);
        
fclose($cachefh);
    }
    else
        print 
$err;
}


$xml_parser xml_parser_create();
xml_parser_set_option($xml_parserXML_OPTION_CASE_FOLDINGtrue);
xml_parser_set_option($xml_parserXML_OPTION_TARGET_ENCODING'UTF-8');

xml_set_element_handler($xml_parser"startElement""endElement");
xml_set_character_data_handler($xml_parser"characterData");
$fp fopen($cachefile"r") or die("could not open XML input " $file);

if (
$dorkzilla === true)
{
    
$title="del.icio.us :: $username";
    include 
"../header.php";
}
print 
"<div class=\"delicious\">\n";
while (
$data fread($fp4096))
{
    
xml_parse($xml_parser$datafeof($fp)) or 
        die (
sprintf("XML error: %s at line %d",
            
xml_error_string(xml_get_error_code($xml_parser)),
            
xml_get_current_line_number($xml_parser)));
    
}
xml_parser_free($xml_parser);
print 
"</div>";
if (
$dorkzilla === true)
    include 
"../footer.php";
?>