<?php
/*
Plugin Name: del.icio.us tagging
Plugin URI: http://www.tumultco.com/
Description: Will show a link after del.icio.us tags that goes to the tag's page.
Version: 1.0.1
Author: Jonathan Deutsch
Author URI: http://www.tumultco.com/
*/

// the login information for the del.icio.us service
$kDeliciousUsername = "fill in here";
$kDeliciousPassword = "fill in here too";

// the location of the cached file.  Make sure there is write permission.
$kCachedFileDirectory = "wp-content/plugins";


//     put del.icio.us tag links in the text provided
//     there are a few problems with this:
//        1. there has to be an HTML tag first for it to work
//        2. it does not respect things found inside <a> tags
//        3. it should also respect when it is inside a blockquote or code
//    If you can create the regular expressions/parsing voodoo to get that to work, please
//    send the fix to Jonathan Deutsch <jonathan@tumultco.com>.  Thanks!
function taggedString($text, $tags, $username)
{
    
// error, it must start with a tag... :-(
    
foreach ($tags as $tag) {
        if (!empty(
$tag) && $tag != ' ') {
            
$path = "http://del.icio.us/$username/$tag";
            
$deliciousLink = "<a href = \"$path\"><img src = \"".get_settings('siteurl')."/wp-content/plugins/deliciousIcon.gif\" border = \"0\" style = \"margin: 0px; padding: 0px; vertical-align: middle;\"></a>";
            
            if (!
preg_match('/<.+>/',$text)) {
                
$text = preg_replace('/(\b'.$tag.'\b)/i','$1'.$deliciousLink,$text);
            } else {
                
$text = preg_replace('/(?<=>)([^<]+)?(\b'.$tag.'\b)/i','$1$2'.$deliciousLink,$text);
            }
        }    
    }

    return
$text;
}

//    fetches data from the url if need be, and caches the results for quick lookup
function getData($url)
{    
    global
$kCachedFileDirectory;
    
    
$contents = "";
    
$cachedFile = "$kCachedFileDirectory/cache.".md5($url).".txt";

    
//    use a cached version if it exists and is less than 30 minutes old
    
if(file_exists($cachedFile) && (time() - filemtime($cachedFile)) < 1800) {
        if(
$file = fopen($cachedFile, "r")) {
            while(!
feof($file)) {
                
$contents .= fgets($file, 4096);
            }
            
            
fclose($file);
        }
    } else {
        
//    read the data from the url
        
if($file = fopen($url, "r")) {
            while(!
feof($file)) {
                
$contents .= fgets($file, 4096);
            }
            
            
fclose($file);
            
            
// make sure there is permission to write here
            
if($file = fopen($cachedFile, "w+")) {
                
//    write to the cache
                
fwrite($file, $contents, strlen($contents));
                
fclose($file);
            }
        }
    }
    
    return
$contents;  
}


//    returns a list of tags from the xml document located at $url
function getTagsFromURL($url)
{
    global
$kParsedTags;

    
//    if the kParsedTags list already exists, then there is no need to read and parse the data
    
if(empty($kParsedTags)) {
        
$parsed_tags = array();
    
        
$data = getData($url);
    
        
$xml_parser = xml_parser_create();
        
xml_set_element_handler($xml_parser, "startElement", "endElement");
        
xml_parse($xml_parser, $data, true);
        
xml_parser_free($xml_parser);
    }
    
    return
$kParsedTags;
}

//    adds the tag to the list of tags
function startElement($parser, $name, $attrs)
{
    global
$kParsedTags;
    
    if(
$name == "TAG") {
        
$kParsedTags[] = $attrs["TAG"];
    }
}

//    null-op
function endElement($parser, $name)
{    }

// entry-point for the plugin
function tagText($text)
{
    global
$kDeliciousUsername, $kDeliciousPassword;
    
    
$tags = getTagsFromURL("http://$kDeliciousUsername:$kDeliciousPassword@del.icio.us/api/tags/get?");
    
    return
taggedString($text, $tags, $kDeliciousUsername);
}

// set the entry-point
add_filter('the_content', 'tagText');

?>