<?php
/*File: xbel.php
*This script imports links from a .xbel format file into the link manager of a WordPress v1.2 "Mingus"  weblog.
* This takes care not to import duplicates, and creates categories corresponding to the folders in the xbel file.
* You have to upload the bookmark file to your server, ideally to the wp-admin folder,
* and edit a line in the source (see comment in code).
* You should then access this script using your browser to import the links.
* Author : Carthik Sharma http://blog.carthik.net
* Thanks to Roberto Giungato http://www.giungato.it for the php source at http://www.giungato.it/roberto/links/xml 
* Enjoy!!
*/
require_once('../wp-config.php');
//edit the following line and include the relative path of your bookmarks (.xbelor .xml) file, for example, if your bookmarks file is named bookmarks.xml and is located in the wp-admin folder, the following line should be $file = "bookmarks.xml"; 
$file "bookmarks.xml";

include_once(
'admin-header.php');
if (
$user_level get_settings('links_minadminlevel'))
              die (
__("Cheatin&#8217; uh?"));
$depth = array();
$link_cat;

$open_tags = array(
    
'XBEL' => '<XBEL>',
    
'INFO' => '<INFO>',
    
'FOLDER' => '<FOLDER>',
    
'TITLE' => '<TITLE>',
    
'BOOKMARK' => '<BOOKMARK>',
    
'DESC' => '<DESC>'
    
);

$close_tags = array(
        
'XBEL' => '</XBEL>',
        
'INFO' => '</INFO>',
        
'FOLDER' => '</FOLDER>',
        
'TITLE' => '</TITLE>',
        
'BOOKMARK' => '</BOOKMARK>',
    
'DESC' => '</DESC>'
    
);


function 
startElement($parser$name$attrs) {
    global 
$open_tags$current_tag$depth$newlink;
    
$current_tag $name;
    switch(
$name) {
        case 
'XBEL':
            break;
        case 
'FOLDER':
            break;
        case 
'BOOKMARK':
            
$newlink['url'] = $attrs['HREF'];
            break;
        default:
            break;
    }
    
array_push($depth$name);
}

function 
endElement($parser$name) {
    global 
$close_tags$newlink$current_tag$depth;
        switch(
$name) {
            case 
'XBEL':
                break;
            case 
'FOLDER':
                break;
            case 
'BOOKMARK':
                
insert_newlink($newlink);
                
$newlink '';
                break;
            default:
                break;
        }
        
array_pop($depth);
}

function 
characterData($parser$data) {
        global 
$current_tag$newlink$catID$depth$link_cat;
        switch(
$current_tag) {
                case 
'BOOKMARK':
                        
$newlink['bookmark'] .= $data;
                        
$current_tag '';
                        break;
                case 
'TITLE':
                        
$tpp array_pop($depth);
                        if (
end($depth) == 'FOLDER') {
                
$tpp2 array_pop($depth);
                if (
end($depth) == 'XBEL') {
                                    
$link_cat $data;
                }
                
array_push($depth$tpp2);
                        } else {
                                
$newlink['title'] .= $data;
                                
$current_tag '';
                        }
                        
array_push($depth$tpp);
                        break;
                case 
'DESC':
                        
$tpp array_pop($depth);
                        if (
end($depth) == 'BOOKMARK') {
                                
$newlink['desc'] .= $data;
                                
$current_tag '';
                        }
                        
array_push($depth$tpp);
                        break;
                case 
'URL':
                        
$newlink['url'] .= $data;
                        
$current_tag '';
                        break;
                default:
                        break;
                }
}

function 
insert_newlink() {
    global 
$wpdb$user_ID$newlink$link_cat$tablelinkcategories$tablelinks;
    if (
$newlink['title']) {
        
$cat_id $wpdb->get_var("SELECT cat_id FROM $tablelinkcategories WHERE cat_name = '$link_cat'");
        if (!
$cat_id) {
        
$wpdb->query("INSERT INTO $tablelinkcategories (cat_name) VALUES ('$link_cat')");
        
$cat_id $wpdb->get_var("SELECT cat_id FROM $tablelinkcategories WHERE cat_name = '$link_cat'");
        }
        
$link_url $newlink['url'];
        
$link_name $newlink['title'];
        
$link_desc $newlink['desc'];
        if (
$wpdb->get_var("SELECT link_id FROM $tablelinks WHERE link_name = '$link_name' AND link_category = '$cat_id'")) {
        echo 
"Link already imported.<br />";
        } else {
        
$query "INSERT INTO $tablelinks (link_url, link_name, link_category, link_description, link_owner) VALUES('$link_url', '".addslashes($link_name)."', '$cat_id', '".addslashes($link_desc)."', '$user_ID')\n";
        
$result $wpdb->query($query);
        echo 
sprintf(__("<p>Inserted <strong>%s</strong></p>"), $link_name);
            }
    }
}

$xml_parser xml_parser_create($type);
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");
if (!(
$fp fopen($file"r"))) {
    die(
"could not open XML input");
}

while (
$data fread($fp4096)) {
//    $data = ereg_replace('&', '&amp;', $data);
    
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);
?>