<?php
/*
Plugin Name: del.icio.us links
Version: 1.1
Plugin URI: http://linuxbrit.co.uk/
Description: Provides a function to display your most recent del.icio.us links
Author: Tom Gilbert
Author URI: http://linuxbrit.co.uk/

Copyright (c) 2004
Released under the GPL license
http://www.gnu.org/licenses/gpl.txt
*/

/*
 usage: just call the delicious_links function somewhere in your
        index.php, and style the output using CSS. Don't forget to enable
        the plugin on the wordpress admin page.
*/

function delicious_links(
  
$user,                   # your del.icio.us username
  
$pass,                   # your del.icio.us password
  
$count=10,               # display this many links
  
$display_tags=false,     # list the tags under each link
  
$display_extended=false# display extended description if present
  
$display_metalinks=true# display links to del.icio.us and your feed
  
$cache_time=3600,        # how long to cache the results for
  
$cache_file ""         # cache file (defaults to
                           # /tmp/del.icio.us.$user.$count.$cache_time.cache)
) {
  global 
$_fpwrite;
  global 
$_del_user;
  global 
$_display_tags;
  global 
$_display_extended;
  global 
$_curl_error_code;
  
$_del_user $user;
  
$_display_tags $display_tags;
  
$api_url "http://del.icio.us/api/posts/recent?count=$count";

  if (
$cache_file == "") {
    
$cache_file  =  "/tmp/del.icio.us.$_del_user.$count.$cache_time.cache";
  }
  
$cache_file_tmp "$cache_file.tmp";

  
$time split(" "microtime());
  
srand((double)microtime()*1000000);
  
# randomise a bit, between 30 and 60s "off the mark" to avoid a bunch of
  # requests all simultaneously deciding to refresh the file
  
$cache_time_rnd 30 rand(060);

  if (
      !
file_exists($cache_file)
    || !
filesize($cache_file) > 20
    
|| ((filemtime($cache_file) + $cache_time $time[1]) + $cache_time_rnd 0)
    || (
filemtime(__FILE__) > filemtime($cache_file))
    ) {
    
$c curl_init($api_url);
    
curl_setopt($cCURLOPT_RETURNTRANSFER,1); 
    
curl_setopt($cCURLOPT_USERPWD,"$user:$pass"); 
    
curl_setopt($cCURLOPT_CONNECTTIMEOUT2);
    
curl_setopt($cCURLOPT_TIMEOUT4);
    
curl_setopt($cCURLOPT_USERAGENT"del.icio.us WordPress Plugin v1.1 http://linuxbrit.co.uk/");
    
$response curl_exec($c);
    
$info curl_getinfo($c);
    
$_curl_error_code $info['http_code'];
    
curl_close($c);
    if (
$_curl_error_code == 200) {
      
$_fpwrite fopen($cache_file_tmp'w');
      if(
$_fpwrite) {
        
# parse the XML, then write out includable html to the cache file.
        
if (!($xml_parser xml_parser_create()))
          die(
"Couldn't create parser.");

        
xml_set_element_handler($xml_parser,
                                
"_delicious_startelem",
                                
"_delicious_endelem");
        
fputs($_fpwrite"<ul>\n");
        
xml_parse($xml_parser$response);
        if (
$display_metalinks) {
          
fputs($_fpwrite"<li><a href='http://del.icio.us/"
                          
."$user'><strong>more</strong></a> | "
                          
."<a href='http://del.icio.us/rss/$user'>"
                          
."<strong>feed</strong></a></li>");
        }
        
fputs($_fpwrite"</ul>\n");

        
xml_parser_free($xml_parser);
        
fclose($_fpwrite);
        
# be atomic
        
rename($cache_file_tmp$cache_file);
      }
    }
  }
  if ((
file_exists($cache_file)) && filesize($cache_file) > 20) {
    include(
$cache_file);
  } elseif (
$_curl_error_code) {
    echo 
"<ul><li>No links (error $_curl_error_code)</li></ul>";
  } else {
    echo 
"<ul><li>No links</li></ul>";
  }
}
function 
_delicious_startelem ($parser,$name,$attrib) {
  global 
$_fpwrite;
  global 
$_del_user;

  switch (
$name) {
    case 
$name=="POST" : {
      
$href htmlspecialchars($attrib["HREF"]);
      
$desc htmlspecialchars($attrib["DESCRIPTION"]);
      
$extended htmlspecialchars($attrib["EXTENDED"]);
      
$tag htmlspecialchars($attrib["TAG"]);
      
$time htmlspecialchars($attrib["TIME"]);
      
$time preg_replace("/[TZ]/"" "$time);
      
$line "<li>\n";
      
$line .= "  <a href='$href' title='$time tag(s): $tag'>$desc</a>\n";
      if (
$_display_extended && $extended && $extended != $desc) {
        
$line .= "  <p>$extended</p>\n";
      }
      if (
$_display_tags) {
        
$line .= "  <ul>\n";
        foreach (
split(" "$tag) as $t) {
          
$line .= "    <li><a href='http://del.icio.us/"
                  
."$_del_user/$t'>$t</a></li>\n";
        }
        
$line .= "  </ul>\n";
      }
      
$line .= "</li>\n";
      
fputs($_fpwrite$line);
      break;
    }
  }
}

function 
_delicious_endelem ($parser,$name){
}

?>