<?php
// $Id: links.import.bookmarks.php,v 0.1 30-11-2003 mikelittle Exp $
//
// This is based on the Blogroll importer by Mike Little -- mike@zed1.com
// **********************************************************************
// Copyright (C) 2003 Tobias Zimmer
//
// 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.
//
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// Tobias Zimmer (zimmer@teco.edu)
// *****************************************************************
require_once('../wp-config.php');
include_once(
"../wp-links/links.php");



$title 'Import Bookmarks';
$this_file 'links.import.bookmarks.php';

$step $HTTP_GET_VARS['step'];
if (!
$step$step 0;
?>
<?php
switch ($step) {
    case 
0:
    {
        
$standalone 0;
        include_once(
'b2header.php');
        if (
$user_level get_settings('links_minadminlevel'))
            die (
"Cheatin&#8217; uh?");
?>
<div class="wrap">

    <h3>On this page you can import your browser bookmarks.</h3>
    <form name="browser" action="links.import.bookmarks.php" method="get">
    <ol>
    <li>Export your browser bookmarks to a file and convert it to "Netscape-Bookmark-File" format if necessary.</li>

    <li>Upload the bookmark file to your web server and put the url into the box below.<br />
    
       <input type="hidden" name="step" value="1" />
       The url of your bookmarks: <input type="text" name="bookmark_url" size="65" />
       </li>


    <li><input type="submit" name="submit" value="Import!" /></li>
    </ol>
    </form>

</div>


<?php
                
break;
            } 
// end case 0

    
case 1: {
                
$standalone 0;
                include_once(
'b2header.php');
                if (
$user_level get_settings('links_minadminlevel'))
                    die (
"Cheatin' uh ?");
?>
<div class="wrap">

     <h3>Importing...</h3>
<?php
                $bookmark_url 
$HTTP_GET_VARS['bookmark_url'];
                if (
$bookmark_url == '') {
                    echo 
"<p>You need to supply your bookmark url. Press back on your browser and try again</p>\n";
                }
                else
                {
                    
$bookmark implode(''file($bookmark_url));
                    
$bookmark preg_replace('/\'/''_'$bookmark);
                    
$bookmark preg_replace('/<p>\s\n\s\s\s\s\s(\s*)<DT><H3 FOLDED ADD_DATE="(.*?)">(.*?)<\/H3>*\s\n*\s(.*?)<DL><p>/''<p>'$bookmark);
                    
$bookmark preg_replace('/<\/A>\s\n\s\s\s\s\s(\s*)<\/DL><p>/''</A>'$bookmark);
                    
//echo"$bookmark";
                    
preg_match_all('/\s\n\s\s\s\s<DT><A HREF="(.*?)" ADD_DATE="(.*?)" LAST_VISIT="(.*?)" LAST_MODIFIED="(.*?)">(.*?)<\/A>/'$bookmark$general_links);
                    
                    
$general_url $general_links[1];
            
$general_name $general_links[5];
                                    
                    
$general_link_count count($general_url);
                    
$over_all_cat_count 0;
                    
$over_all_link_count 0;
                    
                    
//echo"<br>gen_link_count: $general_link_count<br><p>";
                    
            
for ($j 0$j $general_link_count$j++)
                    {
                        
//echo"general_url $j: $general_url[$j]   name: $general_name[$j]<br>";
                        
$query "SELECT * FROM $tablelinks WHERE link_url='$general_url[$j]'";
            
$result $wpdb->query($query);
            
//echo "res: $result<br>";
                        
if ($result != 1)
                        {    
                            
//$results = $wpdb->get_results("SELECT * FROM $tablelinkcategories WHERE cat_name='cat_ID[$i]'");
                            
$query "SELECT * FROM $tablelinkcategories WHERE cat_name='General'";
                
$result $wpdb->query($query);

                            if(
$result == 1)
                            {
                                
$query "SELECT * FROM $tablelinkcategories WHERE cat_name='General'";
                    
$results $wpdb->get_row($query);

                                
$general_cat_ID $results->cat_id;
                            }
                            else
                            {
                                echo
"<p>Inserting category <b>General</b><br>";
                                
$over_all_cat_count 1;
                    
$query "INSERT INTO $tablelinkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit ) VALUES ('0', 'General', 'N', 'Y', 'Y', 'Y', 'Y', 'name', 'N', '<li>', '<br />', '</li>', -1)";  
                        
$result $wpdb->query($query);
                                                
                        
$query "SELECT * FROM $tablelinkcategories WHERE cat_name='General'";
                        
$results $wpdb->get_row($query);
                        
$general_cat_ID $results->cat_id;
                            }
                            
//echo"results: $general_cat_ID <br>";
                            
                            
                            
$over_all_link_count $over_all_link_count 1;
                                                
                            
//echo "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner) VALUES('$general_url[$j]', '$general_name[$j]', '', $general_cat_ID, '', $user_ID)";
                            
$query "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner) VALUES('$general_url[$j]', '$general_name[$j]', '', $general_cat_ID, '', $user_ID)";
                            
$result $wpdb->query($query);
                        }    
                    }
                    
                    
                    
preg_match_all('/<H3 FOLDED ADD_DATE="(.*?)">(.*?)<\/H3>*\s\n*\s(.*?)<DL>((.|\n)*?)<\/DL>/'$bookmark$items);
                    
//preg_match_all('/<H3 FOLDED ADD_DATE="(.*?)">(.*?)<\/H3>/', $bookmark, $items);
                    //preg_match_all('/<H3 FOLDED ADD_DATE="(.*?)">(.*?)<\/H3>*\s\n*\s(.*?)<DL><p>*\s\n*\s(.*?)(<DT><A HREF="(.*?)" (.*?)">(.*?)<\/A>*\s\n*\s(.*?))/', $bookmark, $items);
                    
                    
                    
$cat_ID $items[2];
                    
$folder $items[4];
                    
                    
                    
$cat_count count($cat_ID);
                    
                    
                    for (
$i 0$i $cat_count$i++) 
                    {
                        
//echo"<p><br>imp: $cat_ID[$i]<br>";
                        //echo"folder: $folder[$i]<br>";
                    
                    
                   
                        //$results = $wpdb->get_results("SELECT cat_id FROM $tablelinkcategories WHERE cat_name="$cat_ID");
                  
                        
$query "SELECT * FROM $tablelinkcategories WHERE cat_name='$cat_ID[$i]'";
                        
$result $wpdb->query($query);
                        
//echo "res: $result<br>";
                        
if ($result != 1)
                        {
                            echo
"<p>Inserting category <b>$cat_ID[$i]</b>.<br>";
                            
$over_all_cat_count $over_all_cat_count 1;
                            
//echo "INSERT INTO $tablelinkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit) VALUES ('0', '$cat_ID[$i]', 'N', 'Y', 'Y', 'Y', 'Y', 'name', 'N', '<li>', '<br />', '</li>', -1)";  
                            
$query "INSERT INTO $tablelinkcategories (cat_id, cat_name, auto_toggle, show_images, show_description, show_rating, show_updated, sort_order, sort_desc, text_before_link, text_after_link, text_after_all, list_limit ) VALUES ('0', '$cat_ID[$i]', 'N', 'Y', 'Y', 'Y', 'Y', 'name', 'N', '<li>', '<br />', '</li>', -1)";  
                            
$result $wpdb->query($query);
                            
                        }
                        else
                        {
                            echo
"<p>";
                        }
                        
                        
preg_match_all('/<A HREF="(.*?)" ADD_DATE="(.*?)" LAST_VISIT="(.*?)" LAST_MODIFIED="(.*?)">(.*?)<\/A>/'$folder[$i], $b_links);
                        
                        
$b_url $b_links[1];
                        
$b_name $b_links[5];
                        
                        
$link_count count($b_url);
                        
                        
                        
//echo"<br>folder: $folder[$i]<br>link_count: $link_count<br>";
                        
                        
for ($j 0$j $link_count$j++)
                        {
                            
//echo"url $j: $b_url[$j]   name: $b_name[$j]<br>";
                            
$query "SELECT * FROM $tablelinks WHERE link_url='$b_url[$j]'";
                
$result $wpdb->query($query);
                
//echo "res: $result<br>";
                            
if ($result != 1)
                            {    
                                
                                
//$results = $wpdb->get_results("SELECT * FROM $tablelinkcategories WHERE cat_name='cat_ID[$i]'");
                                
$query "SELECT * FROM $tablelinkcategories WHERE cat_name='$cat_ID[$i]'";
                                
$results $wpdb->get_row($query);
                                
//echo"results: $results->cat_id <br>";
                                
                                
$over_all_link_count $over_all_link_count 1;
                                
                                
//echo "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner) VALUES('$b_url[$j]', '$b_name[$j]', '', $results->cat_id, '', $user_ID)";
                                
$query "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner) VALUES('$b_url[$j]', '$b_name[$j]', '', $results->cat_id, '', $user_ID)";
                                
$result $wpdb->query($query);
                            }
                    
                            
                        }
                        
                        
/*$query = "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner)
                        VALUES('{$urls[$i]}', '".addslashes($names[$i])."', '{$targets[$i]}', $cat_id, '".addslashes($titles[$i])."', $user_ID)\n";
                        $result = $wpdb->query($query);
                        echo "<p>Inserted <strong>{$names[$i]}</strong></p>";*/
                    
}
?>
     <p>All done! We added <?php echo"<b>$over_all_cat_count</b>"?> categories and <?php echo"<b>$over_all_link_count</b>"?> links. Go <a href="linkmanager.php">manage your links</a>.</p>
<?php
                
// end else got url
?>

</div>
<?php
                
break;
            } 
// end case 1
// end switch
?>
</body>
</html>