<?php
// Links
// Copyright (C) 2002 Mike Little -- mike@zed1.com

require_once('../wp-config.php');

$parent_file = 'link-manager.php';
$title = 'Import Blogroll';
$this_file = 'link-import.php';

$step = $_POST['step'];
if (!$step) $step = 0;
?>
<?php
switch ($step) {
    case 0:
    {
        $standalone = 0;
        include_once('admin-header.php');
        if ($user_level < get_settings('links_minadminlevel'))
            die (_LANG_P_CHEATING_ERROR);

        $opmltype = 'blogrolling'; // default.
?>

<ul id="adminmenu2">
        <li><a href="link-manager.php" ><?php echo _LANG_WLA_MANAGE_LINK; ?></a></li>
	<li><a href="link-add.php"><?php echo _LANG_WLA_ADD_LINK; ?></a></li>
	<li><a href="link-categories.php"><?php echo _LANG_WLA_LINK_CATE; ?></a></li>
	<li class="last"><a href="link-import.php"  class="current"><?php echo _LANG_WLA_IMPORT_BLOG; ?></a></li>
</ul>

<div class="wrap">

    <h2><?php echo _LANG_WLI_ROLL_DESC; ?></h2>
	<!-- <form name="blogroll" action="link-import.php" method="get"> -->
	<form enctype="multipart/form-data" action="link-import.php" method="post" name="blogroll">

	<ol>
    <li><?php printf(_LANG_WLI_ROLL_OPMLCODE, gethelp_link(_LANG_DOC_LINK, '#opml_code')); ?></li>
    <li><?php printf(_LANG_WLI_ROLL_OPMLLINK, gethelp_link(_LANG_DOC_LINK, '#opml_code')); ?></li>
    <li><?php echo _LANG_WLI_ROLL_BELOW; ?><br />
       <input type="hidden" name="step" value="1" />
       <?php printf(_LANG_WLI_ROLL_YOURURL, gethelp_link(_LANG_DOC_LINK, '#opml_code')); ?> <input type="text" name="opml_url" size="65" />
	</li>
    <li>
	   <?php echo _LANG_WLI_ROLL_UPLOAD; ?><br />
       <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
       <label><?php echo _LANG_WLI_ROLL_THISFILE; ?> <input name="userfile" type="file" /></label>
    </li>

    <li><?php printf(_LANG_WLI_ROLL_CATEGORY, gethelp_link(_LANG_DOC_LINK, '#link_category')); ?> <select name="cat_id">
<?php
	$categories = $wpdb->get_results("SELECT cat_id, cat_name, auto_toggle FROM $tablelinkcategories ORDER BY cat_id");
	foreach ($categories as $category) {
?>
    <option value="<?php echo $category->cat_id; ?>"><?php echo $category->cat_id.': '.$category->cat_name; ?></option>
<?php
        } // end foreach
?>
    </select>

	</li>

    <li><input type="submit" name="submit" value="<?php echo _LANG_WLI_ROLL_BUTTONTEXT; ?>" /> : <?php printf(_LANG_WLI_ROLL_HELP, gethelp_link(_LANG_DOC_LINK, '#import')); ?></li>
	</ol>
    </form>

</div>
<?php
                break;
            } // end case 0

    case 1: {
                $standalone = 0;
                include_once('admin-header.php');
                if ($user_level < get_settings('links_minadminlevel'))
                    die (_LANG_P_CHEATING_ERROR);
?>
<div class="wrap">

     <h2><?php echo _LANG_WLI_ROLL_ING; ?></h2>
<?php
                $cat_id = $_POST['cat_id'];
                if (($cat_id == '') || ($cat_id == 0)) {
                    $cat_id  = 1;
                }

                $opml_url = $_POST['opml_url'];
                if (isset($opml_url) && $opml_url != '') {
					$blogrolling = true;
                }
                else // try to get the upload file.
				{
					$uploaddir = get_settings('fileupload_realpath');
					$uploadfile = $uploaddir.'/'.$_FILES['userfile']['name'];

					if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
					{
						//echo "Upload successful.";
						$blogrolling = false;
						$opml_url = $uploadfile;
					} else {
						echo _LANG_WLI_ROLL_ERROR;
					}
				}

                if (isset($opml_url) && $opml_url != '') {
                    $opml = implode('', file($opml_url));
                    include_once('link-parse-opml.php');

                    $link_count = count($names);
                    for ($i = 0; $i < $link_count; $i++) {
                        if ('Last' == substr($titles[$i], 0, 4))
                            $titles[$i] = '';
                        if ('http' == substr($titles[$i], 0, 4))
                            $titles[$i] = '';
                        $query = "INSERT INTO $tablelinks (link_url, link_name, link_target, link_category, link_description, link_owner, link_rss)
                                VALUES('{$urls[$i]}', '".addslashes($names[$i])."', '', $cat_id, '".addslashes($descriptions[$i])."', $user_id, '{$feeds[$i]}')\n";
                        $result = $wpdb->query($query);
                        echo sprintf(_LANG_WLI_ROLL_INSERTED, $names[$i]);
                    }
?>
     <p><?php printf(_LANG_WLI_ROLL_ALLDONE, $link_count, $cat_id, 'link-manager.php'); ?></p>
<?php
                } // end if got url
                else
                {
                    echo "<p>" . _LANG_WLI_ROLL_AGAIN . "</p>\n";
                } // end else

?>
<?php
                break;
            } // end case 1
} // end switch
?>
</div>
<?php
require('admin-footer.php');
?>
