<?
/*
Copyright (C) 2005 Eric Anderson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

http://www.gnu.org/licenses/gpl.txt

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

class wpdiDeliciousDataSet
{
    var 
$url;
    var 
$description;
    var 
$extended;
    var 
$tags;
    var 
$time;
    
    function 
wpdiDeliciousDataSet($url$description$extended$tags$time)
    {
        
$this->url $url;
        
$this->description $description;
        
$this->extended $extended;
        
$this->tags $tags;
        
$this->time $time;
    }
}

class 
wpdiDeliciousData
{
    var 
$tags;
    var 
$links;
    
    function 
AddLink($url$description$extended$tags$time)
    {
        
$new_tags explode(" "$tags); // Split tags up
        
        
foreach($new_tags as $tag// Loop through each tag, add it to my list
        
{
            
$this->tags[] = $tag;
        }
        
        
$this->links[] = new wpdiDeliciousDataSet($url$description$extended$new_tags$time);
    }
}

class 
wpdiDelicious
{
    var 
$data;
    
    function 
xmlStartTag($parser$name$attrs)
    {
        if (
$name == 'POST')
        {
            
$this->data->AddLink($attrs['HREF'], $attrs['DESCRIPTION'], $attrs['EXTENDED'], $attrs['TAG'], $attrs['TIME']);
        }
    }
    
    function 
xmlCdata($parser$cdata)
    {
    }
    
    function 
xmlEndTag($parser$name)
    {
    
    }
    
    function 
getData()
    {
        
$wpdi_username get_option('wpdi_username');
        
$wpdi_password get_option('wpdi_password');
        
        
$wpdi_cacheminutes get_option('wpdi_cacheminutes'); 
        
$wpdi_cachetime get_option('wpdi_cachetime');
        
        if (
time() - $wpdi_cachetime <= $wpdi_cacheminutes*60)
        {
            
$this->data unserialize(strrev(get_option('wpdi_cache')));

        }
        else
        {
            
// Get XML from del.icio.us
            //$contents = @file_get_contents('http://' . $wpdi_username . ':' . $wpdi_password . '@del.icio.us/api/posts/all');
            
            //  Get XML usign curl modified by LeChuck (bfpdevel@gmail.com)
            
$ch=curl_init("https://api.del.icio.us/v1/posts/all"); 
            
curl_setopt($chCURLOPT_USERPWD$wpdi_username.":".$wpdi_password);
      
curl_setopt($chCURLOPT_HEADER0); 
      
curl_setopt($chCURLOPT_RETURNTRANSFER1);      
      
$contents curl_exec($ch);      
      
curl_close($ch);
            
            
            if (
$contents == false)
            {
                return 
false;
            }
            else
            {        
                
$this->data = new wpdiDeliciousData();
        
                
// Parse XML
                
$xml_parser xml_parser_create();
                
xml_set_object($xml_parser, &$this); 
                
xml_set_element_handler($xml_parser"xmlStartTag""xmlEndTag");
                
xml_set_character_data_handler($xml_parser"xmlCdata");
                
                
$success xml_parse($xml_parser,$contents);
                
                if(!
$success)
                    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);
                
                
// Clean up tags
                
$this->data->tags array_unique($this->data->tags);
                
sort($this->data->tags);
                
                
// Save cache
                
update_option('wpdi_cache'strrev(serialize($this->data)));
                
update_option('wpdi_cachetime'time());
            }
        }
        
        return 
$this->data;
    }

}

?>