<?php
#    
# This is yadd.php V1.0 by Marc Nozell (marc@nozell.com) based on
# Stephen Eyre's dailydelicious
# (http://www.dot-totally.co.uk/dailydelicious.txt)
#
# See http://www.nozell.com/blog/ for the latest version of
# 'yet another daily delicious' (yadd.php)
#
# USAGE:

# 1) Edit the section below.  At the very least use your del.icio.us 
#    username and password ($del_user/$del_password)

# 2) Put there file somewhere on the server where you run WordPress.

# 3) Arrange for this page to be hit once a day, say 11:30ish your
#    local time.  del.icio.us seems to track UTC so depending on which
#    timezone you live in, some bookmarks from your yesterday may show
#    up.  Consider using a simple cronjob that looks like this:
#           30 23 * * *       $HOME/bin/yadd.sh
#    where yadd.sh looks like this:
#    #!/bin/bash
#    curl http://www.yoursite.com/yadd.php
# 4) In the morning edit the entry if you wish.  I've tried to generate
#    pretty HTML so it will be simple to edit the entry.

# THINGS TO KEEP IN MIND

# Anyone that hits the URL for this script will cause your current
# bookmarks to be dumped into WordPress.  Clearly this is not
# desirable.

# You have some options.  The easiest is to keep this URL 'secret'.
# Name it something unusual and put it in a non-obvious place.
# Remember that if you display your web hit stats, the url will be
# exposed.  A better solution to use .htaccess to limit access.  If
# you do that remember to update the url wget uses to include the
# username/password, something like this:
#  curl http://someuser:somepassword@www.yoursite.com/yadd.php

# Enjoy,

# -marc
#
# Changes by Leonid Mamchenkov <leonid@mamchenkov.net>
# - Added Wordpress post status configuration
# - Integratd "Don't post empty lists" patch by  Zach Paine (?)
# - Changed Extended field to be printed only when non-empty
# - Added optional tag linking (a bit lower than the rest of configuration).
#
# Configure these:

$wp_path '/var/www/wordpress';            # Absolute path to WP installation, no trailing /
$del_user 'myuser';                    # del.icio.us username
$del_password 'mypass';                # del.icio.us username
$del_showlink true;                    # Show link to your del.icio.us page?
$wp_userid 2;                        # User ID to post in
$wp_catid =  1;                        # Category to put daily del.icio.us links in
$wp_allowcomments 'open';                # Allow comments on all links? (open or closed)
$wp_allowpings 'open';                # Allow pingbacks on all links? (open or closed)
$wp_post_status 'publish';                # Post status.  Use one of the following: 'draft','publish','private'
$wp_posttitle 'Daily del.icio.us bookmarks';    # Title for your daily del.icio.us posts
$wp_postname 'delicious';                # Post name (for permalinks, etc)

# Stop configuring now!
require_once($wp_path '/wp-config.php');

function 
startElement($parser$name$attrs)
{
   global 
$content;
   
### Configure linking
   
$tag_link 'yes';                        # Use 'yes' to link to tag pages or 'no' to leave plain text

   
if ($name == "POSTS") {
    
$content $content 'Shared bookmarks for <a href="http://del.icio.us/">del.icio.us</a> user <a href="http://del.icio.us/' $attrs["USER"] . '"> '
         
$attrs["USER"]
             . 
"</a> on " $attrs["DT"] . "\n<ul>\n";
   }
   else {

    global 
$empty;
    
$empty false;
    
# Print the URL
    
$content $content '    <li><a href="'
             
htmlspecialchars($attrs['HREF'])
             . 
'" title="'
             
htmlspecialchars($attrs['HREF']) 
             . 
'">' 
             
htmlspecialchars($attrs['DESCRIPTION'])
             . 
"</a>";
    
# Print extended if there is any
    
if (strlen($attrs['EXTENDED']) > 0) {
        
$content $content ' -- ' htmlspecialchars($attrs['EXTENDED']);
    }

    
# Print tags
    
$content $content "\n" 'Tagged as: ';
    
# Link tags to global tag pages
    
if ($tag_link == 'yes') {
        
$tags explode(' 'htmlspecialchars($attrs['TAG']));
        foreach (
$tags as $tag) {
            
$content $content '<a href="http://del.icio.us/tag/' $tag '">' $tag '</a> ';
        }
    }
    
# Don't link tags
    
else {
        
$content $content htmlspecialchars($attrs['TAG']);
    }
    
$content $content "</li>\n";

   }
}


function 
endElement($parser$name)
{
   global 
$content;
   if (
$name == "POSTS") {
    
$content $content "</ul>";
   }
}

# Get today, and make timestamp
$today getdate();
$date mktime(0,0,0,$today['mon'], $today['mday'], $today['year']);
# Get Dates for Post
$now current_time('mysql');
$now_gmt current_time('mysql'1);
$empty true;


$dt date("Y-m-d");

$fp fopen("http://$del_user:$del_password@del.icio.us/api/posts/get?dt=$dt""r");

$content "";

$xml_parser xml_parser_create();
xml_set_element_handler($xml_parser"startElement""endElement");

while (
$data fread($fp4096)) {
   if (!
xml_parse($xml_parser$datafeof($fp))) {
       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);

if (
$empty)
        return;

# Build Query
$query  "INSERT INTO $tableposts "
        
"(`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, `post_modified`, `post_modified_gmt`) "
        
"VALUES "
        
"(" $wp_userid ", '" $now "', '" $now_gmt "', '" addslashes($content) . "', '" addslashes($wp_posttitle) . "', '$wp_post_status', '" $wp_allowcomments "', '" $wp_allowpings "', '" $wp_postname "', '" $now "', '" $now_gmt "');";

# Run Query
$wpdb->query($query);

# Get ID for category
$post_ID $wpdb->get_var("SELECT ID FROM $tableposts ORDER BY ID DESC LIMIT 1");

# Build category query
$query "INSERT INTO $tablepost2cat "
       
"(`post_id`, `category_id`) "
       
"VALUES "
       
"('" $post_ID "', '" $wp_catid "');";

# Run Category Query
$wpdb->query($query);

?>