<?php

@( (bool)FALSE === strpos( $_SERVER['REQUEST_URI'], basename( __FILE__ ) ) ) OR exit( '!! ACCESS DENIED !!' );
error_reporting( _E_VALUE );


/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | myPHPNuke Version 188_8 RC2                                          |
// +----------------------------------------------------------------------+
// | License: GNU/GPL - http://www.gnu.org/copyleft/gpl.html              |
// +----------------------------------------------------------------------+
// | Support: http://myphpnuke.com/                                       |
// +----------------------------------------------------------------------+
// | Author: myPHPNuke Dev Team                                           |
// +----------------------------------------------------------------------+
// | Copyright: © 2001-2004 myPHPNuke Dev Team                            |
// +----------------------------------------------------------------------+
// $Id: stories.php,v 1.17.2.11 2004/08/15 12:58:04 kirilt Exp $

/************************************************************************/
/* PHP-NUKE: Web Portal System                                          */
/* ===========================                                          */
/*                                                                      */
/* Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)            */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* 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.       */
/************************************************************************/

checkAccess( 'articles', array( 'article' ) );

/*********************************************************/
/* Story/News Functions                                  */
/*********************************************************/

function puthome($ihome) {
    echo "<td><b>".translate("Publish in Home?")."</b></td><td>";
    if (($ihome == 0) OR ($ihome == "") OR is_null($ihome)) {
    $sel1 = "checked";
    $sel2 = "";
    }
    if ($ihome == 1) {
    $sel1 = "";
    $sel2 = "checked";
    }
    echo "<input type=radio name=\"ihome\" value=0 $sel1>".translate("Yes")."&nbsp;";
    echo "<input type=radio name=\"ihome\" value=1 $sel2>".translate("No")."";
    echo "&nbsp;&nbsp;<font size=2>[ " .translate("Only works if <i>Articles</i> category isn't selected")." ]</font></td></tr>\n";
}

function deleteStory($qid) {
    global $mpnTables,$phpEx;
    $result = mysql_query("delete from ".$mpnTables['queue']." where qid=$qid");
        if (!$result)
        {
            echo mysql_errno(). ": ".mysql_error(). "<br>";
            return;
        }

        $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=submissions' );

}

function SelectCategory($cat) {
    global $mpnTables,$phpEx;
    $selcat = mysql_query("select catid, title from ".$mpnTables['stories_cat']);
    $a = 1;
    echo '<td><b>' . translate('Category') . '</b></td><td> ';
    echo "<select name=\"catid\">";
    if ($cat == 0) {
    $sel = "selected";
    } else {
    $sel = "";
    }

    echo "<option name=\"catid\" value=\"0\" $sel>Articles</option>";
    while(list($catid, $title) = mysql_fetch_row($selcat)) {

    if ($catid == $cat) {
        $sel = "selected";
    } else {
        $sel = "";
    }
    echo "<option name=\"catid\" value=\"$catid\" $sel>$title</option>";
    $a++;
    }
    echo "</select> [ <a href=admin.$phpEx?op=AddCategory>" .translate("Add")."</a> | <a href=admin.$phpEx?op=EditCategory>" .translate("Edit")."</a> | <a href=admin.$phpEx?op=DelCategory>" .translate("Delete")."</a> ]</td></tr>\n";
}

function AddCategory () {
    global $phpEx;
    include ("header.$phpEx");
    if (!isset($hlpfile))
    {
        $hlpfile = null;
    }

    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"3\"><b>" .translate("Add a New Category")."</b></font><br><br><br>";
    echo "<form action=admin.$phpEx method=post>";
    echo "<b>" .translate("New Category Name:")."</b> ";
    echo "<input type=text name=title size=22 maxlength=20> ";
    echo "<input type=hidden name=op value=SaveCategory>";
    echo "<input class='button' type=submit value=\"" .translate("Save Changes")."\">";
    echo "</form></center>";
    CloseTable();
    include("footer.$phpEx");
}

function EditCategory($catid) {
    global $mpnTables, $phpEx, $hlpfile;
    $result = mysql_query("select title from ".$mpnTables['stories_cat']." where catid='$catid'");
    list($title) = mysql_fetch_row($result);
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"3\"><b>" .translate("Edit Category")."</b></font><br>";
    if (!$catid) {
    $selcat = mysql_query("select catid, title from ".$mpnTables['stories_cat']);
    echo "<form action=admin.$phpEx method=post>";
    echo "<b>" .translate("Select a Category")."</b>";
    echo "<select name=\"catid\">";
    echo "<option name=\"catid\" value=\"0\" $sel>Articles</option>";
    while(list($catid, $title) = mysql_fetch_row($selcat))
    {
        $title = stripslashes( $title );

        echo "<option name=\"catid\" value=\"$catid\">$title</option>";
    }
    echo "</select>";
    echo "<input type='hidden' name='op' value='EditCategory'>";
    echo "<input class='button' type='submit' value=\"" .translate("Edit")."\">";
    } else {
    echo "<form action=admin.$phpEx method=post>";
    echo "<b>" .translate("Category Name:")."</b> ";
    echo "<input type='text' name='title' size=22 maxlength=20 value=\"$title\"> ";
    echo "<input type='hidden' name='catid' value=\"$catid\">";
    echo "<input type='hidden' name='op' value='SaveEditCategory'>";
    echo "<input class='button' type='submit' value=\"" .translate("Save Changes")."\">";
    echo "</form>";
    }
    echo "</center>";
    CloseTable();
    include("footer.$phpEx");
}

function DelCategory($cat) {
    global $mpnTables,$phpEx;
    $result = mysql_query("select title from ".$mpnTables['stories_cat']." where catid='$cat'");
    list($title) = mysql_fetch_row($result);
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"3\"><b>" .translate("Delete Category")."</b></font><br>";
    if (!$cat) {
    $selcat = mysql_query("select catid, title from ".$mpnTables['stories_cat']);
    echo "<form action=admin.$phpEx method=post>";
    echo "<b>" .translate("Select a Category to Delete:")." </b>";
    echo "<select name=\"cat\">";
    while(list($catid, $title) = mysql_fetch_row($selcat)) {
        echo "<option name=\"cat\" value=\"$catid\">$title</option>";
    }
    echo "</select>";
    echo "<input type=hidden name=op value=DelCategory>";
    echo "<input class='button' type=submit value=\"" .translate("Delete")."\">";
    echo "</form>";
    } else {
    $result2 = mysql_query("select * from ".$mpnTables['stories']." where catid='$cat'");
    $numrows = mysql_num_rows($result2);
    if ($numrows == 0) {
        mysql_query("delete from ".$mpnTables['stories_cat']." where catid='$cat'");
        echo "<br><br>" .translate("Deletion Completed!")."";
    } else {
        echo "<br><br>" .translate("<b>WARNING:</b> The Category")." <b>$title</b> " .translate("has")." <b>$numrows</b> " .translate("stories</b> inside!")."<br>";
        echo "" .translate("You can Delete this Category and ALL its stories and comments")."<br>";
        echo "<br>" .translate("or you can Move ALL the stories to a New Category.")."<br><br>";
        echo "" .translate("What do you want to do?")."<br><br>";
        echo "<b>[ <a href=admin.$phpEx?op=YesDelCategory&catid=$cat>" .translate("Yes! Delete ALL!")."</a> | ";
        echo "<a href=admin.$phpEx?op=NoMoveCategory&catid=$cat>" .translate("No! Move my Stories")."</a> ]</b>";
    }
    }
    echo "</center>";
    CloseTable();
    include("footer.$phpEx");
}

function YesDelCategory($catid) {
    global $mpnTables,$phpEx;
    mysql_query("delete from ".$mpnTables['stories_cat']." where catid='$catid'");
    $result = mysql_query("select sid from ".$mpnTables['stories']." where catid='$catid'");
    while(list($sid) = mysql_fetch_row($result)) {
    mysql_query("delete from ".$mpnTables['stories']." where catid='$catid'");
    mysql_query("delete from ".$mpnTables['comments']." where sid='$sid'");
    }


    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php' );
}

function NoMoveCategory($catid, $newcat) {
    global $mpnTables,$phpEx;
    $result = mysql_query("select title from ".$mpnTables['stories_cat']." where catid='$catid'");
    list($title) = mysql_fetch_row($result);
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"3\"><b>" .translate("Move Stories to a New Category")."</b></font><br><br>";
    if (!$newcat) {
    echo "" .translate("ALL stories under")." <b>$title</b> " .translate("will be moved.")."<br><br>";
    $selcat = mysql_query("select catid, title from ".$mpnTables['stories_cat']);
    echo "<form action=admin.$phpEx method=post>";
    echo "<b>" .translate("Please Select the New Category:")."</b> ";
    echo "<select name=\"newcat\">";
        echo "<option name=\"newcat\" value=\"0\">Articles</option>";
    while(list($newcat, $title) = mysql_fetch_row($selcat)) {
            echo "<option name=\"newcat\" value=\"$newcat\">$title</option>";
    }
    echo "</select>";
    echo "<input type=hidden name=catid value=\"$catid\">";
    echo "<input type=hidden name=op value=NoMoveCategory>";
    echo "<input class='button' type=submit value=\"" .translate("Move!")."\">";
    echo "</form>";
    } else {
    $resultm = mysql_query("select sid from ".$mpnTables['stories']." where catid='$catid'");
    while(list($sid) = mysql_fetch_row($resultm)) {
        mysql_query("update ".$mpnTables['stories']." set catid='$newcat' where sid='$sid'");
    }
    mysql_query("delete from ".$mpnTables['stories_cat']." where catid='$catid'");
    echo translate("Congratulations! The Moved has been completed!");
    }
    CloseTable();
    include("footer.$phpEx");
}

function SaveEditCategory($catid, $title) {
    global $mpnTables, $phpEx, $hlpfile;
    $title = ereg_replace("\"","",$title);
    $check = mysql_query("select catid from ".$mpnTables['stories_cat']." where title=$title");
    if ($check) {
    $what1 = "This Category already exist!";
    $what2 = "[ <a href=javascript:history.go(-1)>" . translate("Go Back to Change the Name") . "</a> ]";
    } else {
    $what1 = translate("Category Saved!");
    $what2 = "[ <a href=\"admin.$phpEx\">" .translate("Go to Admin Section")."</a> ]";
    $result = mysql_query("update ".$mpnTables['stories_cat']." set title='$title' where catid='$catid'");
    if (!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }
    }
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"2\"><b>$what1</b></font><br><br>";
    echo "$what2</center>";
    CloseTable();
    include("footer.$phpEx");
}

function SaveCategory($title) {
    global $mpnTables, $phpEx, $hlpfile;
    $title = ereg_replace("\"","",$title);
    $check = mysql_query("select catid from ".$mpnTables['stories_cat']." where title=$title");
    if ($check) {
    $what1 = "This Category already exist!";
    $what2 = "[ <a href=javascript:history.go(-1)>" . translate("Go Back to Change the Name") . "</a> ]";
    } else {
    $what1 = translate("New Category Added!");
    $what2 = "[ <a href=\"admin.$phpEx\">" .translate("Go to Admin Section")."</a> ]";
    $result = mysql_query("insert into ".$mpnTables['stories_cat']." values (NULL, '$title', '0')");
    if (!$result) {
       echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }
    }
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<center><font size=\"2\"><b>$what1</b></font><br><br>";
    echo "$what2</center>";
    CloseTable();
    include("footer.$phpEx");
}

function displayStory ($qid) {
    global $user, $subject, $story, $tipath, $mpnTables,$phpEx;
    include ("header.$phpEx");
    if (!isset($hlpfile))
    {
        $hlpfile = null;
    }

    GraphicAdmin($hlpfile);
    $result = mysql_query("SELECT qid, uid, uname, subject, story, topic FROM ".$mpnTables['queue']." where qid=$qid");
    list($qid, $uid, $uname, $subject, $story, $topic) = mysql_fetch_row($result);
    mysql_free_result($result);

    // Kiril: Start New Sanitizer

    $sanity_ary = compact( 'subject', 'story' );
    $_SERVER['SAN'] -> slashIT( $sanity_ary );
    extract( $sanity_ary );

    $subject4edit  = $subject4show  = $subject;
    $story4edit = $story4show = $story;

    $_SERVER['SAN'] -> adjustFormat( $story4show, 3, 10 );

    unset( $sanity_ary );

    // End New Sanitizer

    if ($topic=="") {
        $topicimage="AllTopics.gif";
        $warning = "<center><blink><b>".translate("Please Select A Topic")."</b></blink></center>";
    } else {
        $warning = "";
        $result = mysql_query("select topicimage from ".$mpnTables['topics']." where topicid='$topic'");
        list($topicimage) = mysql_fetch_row($result);
    }


    echo "<center><br /><br />$warning<br /><br />\n";

    echo "<table border=1 width='100%' cellpadding=8 cellspacing=1><tr><td>\n";

    echo "<br /><br />\n";

    echo "<img src = \"".$tipath.$topicimage."\" border=0 align=\"right\" style=\"margin: 8px;\">";

    themepreview($subject4show, $story4show);

    echo "</td></tr></table>\n";

    echo "</center>\n";

    echo "<br /><br />\n";


    OpenTable();
    ?>
    <font size=3>
    <?php echo "<form action=\"admin.$phpEx\" method=\"post\" name=\"coolsus\">"; ?>
    <p><b><?php echo translate("Name"); ?></b><br>
    <input class=textbox type="text" NAME="author" SIZE=50 value="<?PHP echo "$uname"; ?>">
    <p><b><?php echo translate("Subject"); ?></b><br>
    <input class=textbox type=text name=subject size=50 value="<?PHP echo"$subject4edit"; ?>">
    <?PHP

    ?>
    <p><b>Topic</b> <select name=topic>
    <?PHP
    $toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
        echo "<OPTION VALUE=\"\">".translate("Select Topic")."</option>\n";
        while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
        $sel = ($topicid==$topic) ? ( "selected" ) : ( '' );
            echo "<option $sel value=\"$topicid\">$topics</option>\n";

        }
    echo "</select>";
    echo "<br><br>";
    if (!isset($cat))
    {
        $cat = null;
    }

    SelectCategory($cat);
    echo "<br /><br />";

    if (!isset($ihome))
    {
        $ihome = null;
    }

    puthome($ihome);
    ?>
    <p><b><?php echo translate("Intro Text"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=18 name=hometext><?PHP echo $story4edit; ?></textarea><br>
    <?php HTMLbuttons( 1 ); ?>
    <p><b><?php echo translate("Full Text"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=18 name=bodytext></textarea><BR>
    <?php HTMLbuttons( 2 ); ?>
    <br><FONT size=2><?php echo translate("(Did you check URLs?)"); ?></FONT><P>

    <p><b><?php echo translate("Notes"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=10 name=notes></textarea><br>
    <?php
    HTMLbuttons( 3 );

    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='hometext|bodytext|notes' />\n";
    echo "<input type='hidden' name='stripit_form' value='subject' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='1' />\n";

    ?>
    <br><input type="hidden" NAME="qid" SIZE=50 value="<?PHP echo "$qid"; ?>">
    <input type="hidden" NAME="uid" SIZE=50 value="<?PHP echo "$uid"; ?>">

    <select name="op">
        <option value="DeleteStory"><?php echo translate("DeleteStory"); ?></option>
        <option value="PreviewAgain" SELECTED><?php echo translate("PreviewAgain"); ?></option>
        <option value="PostStory"><?php echo translate("PostStory"); ?></option>
    </select>

    <INPUT class='button' type="submit" value="<?php echo translate("Go!"); ?>">
    </form></td></tr></table></td></tr></table>
    <?PHP
    include ("footer.$phpEx");
}

function previewStory($qid, $uid, $author, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome) {
    global $user, $boxstuff, $tipath, $mpnTables,$phpEx;
    include ("header.$phpEx");

    if (!isset($hlpfile))
    {
        $hlpfile = null;
    }

    GraphicAdmin($hlpfile);

    // Kiril: Start New Sanitizer

    $subject4form  = $subject4pre  = $subject;
    $hometext4form = $hometext4pre = $hometext;
    $bodytext4form = $bodytext4pre = $bodytext;
    $notes4form    = $notes4pre    = $notes;

    $sanity_ary    = compact( 'hometext4pre', 'bodytext4pre', 'notes4pre' );
    $_SERVER['SAN'] -> adjustFormat( $sanity_ary, 3, 10 );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer

    if ($topic=="") {
        $topicimage="AllTopics.gif";
        $warning = "<center><blink><b>".translate("Please Select A Topic")."</b></blink></center>";
    } else {
        $warning = "";
        $result = mysql_query("select topicimage from ".$mpnTables['topics']." where topicid='$topic'");
        list($topicimage) = mysql_fetch_row($result);
    }

    if(!$bodytext4pre) {
    $space = "<P>";
    } else {
    $space = "<P>---<P>";
    }

    echo "<center><br /><br />$warning<br /><br />\n";

    echo "<table border=0 width=100% cellpadding=8 cellspacing=1><tr><td>\n";

    echo "<br /><br />\n";

    echo "<img src = \"".$tipath.$topicimage."\" border=0 align=\"right\" style=\"margin: 8px;\">";

    // REFERENCE: themepreview( $title, $hometext, $bodytext = '', $notes = '', $space = '' )

    themepreview($subject4pre, $hometext4pre, $bodytext4pre, $notes4pre, $space);

    echo "</td></tr></table>\n";

    echo "</center>\n";

    echo "<br /><br />\n";



    OpenTable();
    ?>
    <font size=3>
    <?php echo "<form action=\"admin.$phpEx\" method=\"post\" name=\"coolsus\">"; ?>
    <p><b><?php echo translate("Name"); ?></b><br>
    <input class=textbox type="text" NAME="author" SIZE=50 value="<?PHP echo"$author"; ?>">
    <p><b><?php echo translate("Subject"); ?></b><br>
    <input class=textbox type=text name=subject size=50 value="<?PHP echo"$subject4form"; ?>">

    <?PHP
   /* echo "<br><br><center><table width=70% bgcolor=000000 cellpadding=0 cellspacing=1 border=0><tr><td>";
    echo "<table width=100% bgcolor=FFFFFF cellpadding=8 cellspacing=1 border=0><tr><td>";
    echo "<img src = \"".$tipath.$topicimage."\" border = 0 align = \"right\">";
    themepreview($subject4pre, $hometext4pre, $space, $bodytext4pre, $notes4pre);
    echo "</td></tr></table></td></tr></table></center>";*/
    ?>

    <p><b><?php echo translate("Topic"); ?></b> <select name=topic>
    <?PHP
    $toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
        echo "<OPTION VALUE=\"\">".translate("All Topics")."</option>\n";
        while(list($topicid, $topics) = mysql_fetch_row($toplist))
        {
            $sel = ($topicid==$topic) ? ( "selected" ) : ( '' );

            echo "<option $sel value=\"$topicid\">$topics</option>\n";

        }
    echo "</select>";
    echo "<br><br>";
    $cat = $catid;
    SelectCategory($cat);
    echo "<br>";

    if (!isset($ihome))
    {
        $ihome = null;
    }

    puthome($ihome);
    ?>
    <p><b><?php echo translate("Intro Text"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=18 name=hometext><?PHP echo $hometext4form; ?></textarea><br>
    <?php HTMLbuttons( 1 ); ?>
    <p><b><?php echo translate("Full Text"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=18 name=bodytext><?PHP echo $bodytext4form; ?></textarea><BR>
    <?php HTMLbuttons( 2 ); ?>
    <br><FONT size=2><?php echo translate("(Did you check URLs?)"); ?></FONT><P>
    <p><b><?php echo translate("Notes"); ?></b><br>
    <textarea class=textbox wrap=virtual cols=70 rows=10 name=notes><?PHP echo $notes4form; ?></textarea><br>

    <?php

    HTMLbuttons( 3 );

    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='hometext|bodytext|notes' />\n";
    echo "<input type='hidden' name='stripit_form' value='subject' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='1' />\n";

    ?>

    <br><input type="hidden" NAME="qid" SIZE=50 value="<?PHP echo"$qid"; ?>">
    <input type="hidden" NAME="uid" SIZE=50 value="<?PHP echo "$uid"; ?>">

    <select name="op">
        <option value="DeleteStory"><?php echo translate("DeleteStory"); ?></option>
        <option value="PreviewAgain" SELECTED><?php echo translate("PreviewAgain"); ?></option>
        <option value="PostStory"><?php echo translate("PostStory"); ?></option>
    </select>

    <INPUT class='button' type="submit" value="<?php echo translate("Go!"); ?>"></FORM>
    </td></tr></table></td></tr></table>
    <?PHP
    include ("footer.$phpEx");
}

function postStory($qid, $uid, $author, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome) {
    global $aid, $ultramode, $mpnTables;
    if ($uid == 1) $author = "";
    if ($hometext == $bodytext) $bodytext = "";

    // Kiril: Start New Sanitizer

    $sanity_ary = compact( 'subject', 'hometext', 'bodytext', 'notes' );
    $_SERVER['SAN'] -> slashIT( $sanity_ary, TRUE );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer

    $result = mysql_query("insert into ".$mpnTables['stories']." values (NULL, '$catid', '$aid', '$subject', now(), '$hometext', '$bodytext', '0', '0', '$topic','$author', '$notes', '$ihome')");
    if (!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        return;
    }
    if ($uid > 1)
    {
        mysql_query("update ".$mpnTables['users']." set counter=counter+1 where uid='$uid'");
    }
        mysql_query("update ".$mpnTables['authors']." set counter=counter+1 where aid='$aid'");
    if (isset($ultramode)) {
            ultramode();
    }
    deleteStory($qid);
}

function editStory ($sid) {
    global $user, $tipath, $mpnTables,$phpEx,$hlpfile;
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    $result = mysql_query("SELECT catid, title, hometext, bodytext, topic, notes, ihome, time FROM ".$mpnTables['stories']." where sid=$sid");
    list( $catid, $subject, $hometext, $bodytext, $topic, $notes, $ihome, $posttime ) = mysql_fetch_row($result);
    mysql_free_result($result);

    // Kiril: Start New Sanitizer

    $sanity_ary = compact( 'subject', 'hometext', 'bodytext', 'notes' );
    $_SERVER['SAN'] -> slashIT( $sanity_ary );
    extract( $sanity_ary );

    $subject4edit  = $subject4show  = $subject;
    $hometext4edit = $hometext4show = $hometext;
    $bodytext4edit = $bodytext4show = $bodytext;
    $notes4edit    = $notes4show    = $notes;

    $sanity_ary    = compact( 'hometext4show', 'bodytext4show', 'notes4show' );
    $_SERVER['SAN'] -> adjustFormat( $sanity_ary, 3, 10 );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer

    if(!$bodytext4show) {
    $space = "<P>";
    } else {
    $space = "<P>---<P>";
    }

    $result=mysql_query("select topicimage from ".$mpnTables['topics']." where topicid=$topic");
    list($topicimage) = mysql_fetch_row($result);

    echo "<br><br><center><font size=4><b>".translate("Edit Article")."</b></font><br /><br />\n";

    echo "<table border=0 width='100%' cellpadding=8 cellspacing=1><tr><td>\n";

    echo "<br /><br />\n";

    echo "<img src = \"".$tipath.$topicimage."\" border=0 align=\"right\" style=\"margin: 8px;\">";

    // REFERENCE: themepreview( $title, $hometext, $bodytext = '', $notes = '', $space = '' )

    themepreview($subject4show, $hometext4show, $bodytext4show, $notes4show, $space);

    echo "</td></tr></table>\n";

    echo "</center>\n";

    echo "<br /><br />\n";


    OpenTable();

    echo "<form action=admin.$phpEx method=post>";
	echo "<table border=1>\n";
    echo "<tr><td><B>".translate("Subject")."</B></td>";
    ?>
    <td><input class=textbox type=text name=subject size=50 value="<?php echo "$subject4edit"; ?>"></td></tr>
    <td><b><?php echo translate("Topic"); ?></b></td><td><select name=topic>
    <?PHP

    $sel = '';

    $toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
        echo "<OPTION VALUE=\"\">".translate("All Topics")."</option>\n";
        while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
        if ($topicid==$topic) { $sel = "selected "; }
            echo "<option $sel value=\"$topicid\">$topics</option>\n";
        $sel = "";
        }
    echo "</select>\n";
    echo "</td></tr>";
    $cat = $catid;
    SelectCategory($cat);
//    echo "<br>";

    if (!isset($ihome))
    {
        $ihome = null;
    }

    puthome($ihome);
    ?>
    <tr><td valign="top"><B><?php echo translate("Intro Text"); ?></B></td>
    <td><textarea class=textbox wrap=virtual cols=70 rows=18 name=hometext><?PHP echo $hometext4edit; ?></textarea></td></tr>
    <tr><td valign="top"><B><?php echo translate("Full Text"); ?></B><br>
	<FONT size=2><?php echo translate("(Did you check URLs?)"); ?></FONT></td>
    <td><textarea class=textbox wrap=virtual cols=70 rows=18 name=bodytext><?PHP echo $bodytext4edit; ?></textarea></td></tr>
    <tr><td valign="top"><B><?php echo translate("Notes"); ?></B></td>
    <td><textarea class=textbox wrap=virtual cols=70 rows=10 name=notes><?php echo $notes4edit; ?></textarea></td></tr>
	</table>

    <?php

    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='hometext|bodytext|notes' />\n";
    echo "<input type='hidden' name='stripit_form' value='subject' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='1' />\n";
    echo "<input type='hidden' name='posttime' value='$posttime' />\n";
    echo"<br /><br />";
    ?>

    <input type="hidden" NAME="sid" SIZE=50 value="<?PHP echo"$sid"; ?>">
    <INPUT type="hidden" name="op" value="ChangeStory">
    <INPUT class='button' type="submit" value="<?php echo translate("ChangeStory"); ?>">
    </form></td></tr></table></td></tr></table>
    <?PHP
    include ("footer.$phpEx");
}

function removeStory ($sid, $ok=0) {
    global $ultramode, $mpnTables,$phpEx;
    if($ok) {
        mysql_query("DELETE FROM ".$mpnTables['stories']." where sid=$sid");
        mysql_query("DELETE FROM ".$mpnTables['comments']." where sid=$sid");
        if (isset($ultramode)) {
            ultramode();
        }

        $_SERVER['F_LIB'] -> redirectRequest( 'admin.php' );

    } else {
        include("header.$phpEx");
        if (!isset($hlpfile))
        {
            $hlpfile = null;
        }

        GraphicAdmin($hlpfile);
        echo "<center>".translate("Are you sure you want to remove Story ID #")." $sid ".translate("and all it's comments?")."";
        echo "<br><br>[ <a href=\"admin.$phpEx\">".translate("No")."</a> | <a href=\"admin.$phpEx?op=RemoveStory&sid=$sid&ok=1\">".translate("Yes")."</a> ]</center>";
        include("footer.$phpEx");
    }
}


function changeStory($sid, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome) {
    global $aid, $ultramode, $mpnTables,$phpEx;

    // Kiril: Start New Sanitizer

    $sanity_ary = compact( 'subject', 'hometext', 'bodytext', 'notes' );
    $_SERVER['SAN'] -> slashIT( $sanity_ary, TRUE );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer

//    $time = ( $_SERVER['F_LIB'] -> checkPostDate( $_POST['posttime'] ) ) ? ( 'NOW()' ) : ( $_POST['posttime'] );

//var_dump( $_POST['posttime'], $time );exit;

    mysql_query("update ".$mpnTables['stories']." set catid='$catid', title='$subject', hometext='$hometext', bodytext='$bodytext', topic='$topic', notes='$notes', ihome='$ihome' where sid=$sid");

    if (isset($ultramode)) {
        ultramode();
    }

    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=adminMain' );

}

function adminStory() {
    global $hlpfile, $admin, $mpnTables,$phpEx;
    $hlpfile = getHelpFile("newarticle.html");

        $ihome = null;

    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<a name='new' ></a><center><font size=4><b>".translate("New Article")."</b></font></center><br><br>
    <form action=\"admin.$phpEx\" method=\"post\" name=\"coolsus\">"; ?>
	<table border=0>
	<tr>
    <td><b><?php echo translate("Title"); ?></b></td>
    <td><input class=textbox type=text name=subject size=50 value=""></td></tr>

    <td><b><?php echo translate("Topic"); ?></b></td><td>
        <?php

        $sel = '';

        $toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
        echo "<SELECT class=textbox NAME=\"topic\">";
        echo "<OPTION VALUE=\"\">".translate("Select Topic")."</option>\n";
        while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
            if ($topicid==$topic) { $sel = "selected "; }
            echo "<option $sel value=\"$topicid\">$topics</option>\n";
        $sel = "";
            }
        echo "</SELECT>\n";
    echo "</td></tr>\n";
    $cat = 0;
    SelectCategory($cat);
    puthome($ihome);
    ?>
    <td valign="top"><B><?php echo translate("The Story"); ?></B></td>
    <td><textarea class=textbox wrap=virtual cols=70 rows=18 name=hometext></textarea><br />
    <?php HTMLbuttons( 1 ); ?></td></tr>
    <td valign="top"><B><?php echo translate("Extended Text"); ?></B><br>
    <td><textarea class=textbox wrap=virtual cols=70 rows=18 name=bodytext></textarea><br />
    <?php

    HTMLbuttons( 2 );
	echo "</td></tr></table>";
    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='hometext|bodytext' />\n";
    echo "<input type='hidden' name='stripit_form' value='subject' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='1' />\n";

    ?>
    <br><FONT size=2><?php echo translate("(Are you sure you included a URL? Did ya test them for typos?)"); ?></FONT><P>

    <select class=textbox name="op">
    <option value="PreviewAdminStory" SELECTED><?php echo translate("PreviewAdminStory"); ?></option>
    <option value="PostAdminStory"><?php echo translate("PostAdminStory"); ?></option>
    </select>

        <INPUT class='button' type="submit" value="<?php echo translate("Go!"); ?>">
    </form></td></tr></table></td></tr></table>
    <?php
    include ("footer.$phpEx");
}

function previewAdminStory($subject, $hometext, $bodytext, $topic, $catid) {
    global $user, $tipath, $mpnTables,$phpEx, $ihome, $hlpfile;
    include ("header.$phpEx");
    if ($topic<1) {
        $topic = 1;
    }
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "
    <font size=4><b><center>".translate("Preview Story")."</b></font><br><br><br>
    <form action=\"admin.$phpEx\" method=\"post\" name=\"coolsus\">
    <input type='hidden' name='catid' value=$catid>
    </b></center>
    ";

    // Kiril: Start New Sanitizer

    $subject4form  = $subject4pre  = $subject;
    $hometext4form = $hometext4pre = $hometext;
    $bodytext4form = $bodytext4pre = $bodytext;

    $sanity_ary    = compact( 'hometext4pre', 'bodytext4pre' );
    $_SERVER['SAN'] -> adjustFormat( $sanity_ary, 3, 10 );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer

    if(!$bodytext4pre) {
    $space = "<P>";
    } else {
    $space = "<P>---<P>";
    }

    $result=mysql_query("select topicimage from ".$mpnTables['topics']." where topicid=$topic");
    list($topicimage) = mysql_fetch_row($result);

    echo "<br><br><center><font size=4><b>".translate("Previewing Article")."</b></font><br /><br />\n";

    echo "<table border=0 width=100% cellpadding=8 cellspacing=1><tr><td>\n";

    echo "<br /><br />\n";

    echo "<img src = \"".$tipath.$topicimage."\" border=0 align=\"right\" style=\"margin: 8px;\">";

    // REFERENCE: themepreview( $title, $hometext, $bodytext = '', $notes = '', $space = '' )

    themepreview($subject4pre, $hometext4pre, $bodytext4pre, '', $space);

    echo "</td></tr></table>\n";

    echo "</center>\n";

    echo "<br /><br />\n";



    /*echo "<center><br>
    <table border=0 width=75% cellpadding=0 cellspacing=1 bgcolor=000000><tr><td>
    <table border=0 width=100% cellpadding=8 cellspacing=1 bgcolor=FFFFFF><tr><td>";
    echo "";
    echo "<img src = \"".$tipath.$topicimage."\" border = 0 align = \"right\">";
    themepreview($subject4pre, $hometext4pre, $space, $bodytext4pre);
    echo "</td></tr></table></td></tr></table></center>";*/
    ?>
	<table border=0>
    <tr></tr><td><b><?php echo translate("Title"); ?></b></td>
    <td><input class='textbox' type='text' name='subject' size=50 value="<?PHP echo"$subject4form"; ?>"></td></tr>
    <tr><td><b><?php echo translate("Topic"); ?></b></td><td><select name='topic'>
    <?PHP
    $toplist = mysql_query("select topicid, topictext from ".$mpnTables['topics']." order by topictext");
        echo "<OPTION VALUE=\"\">".translate("All Topics")."</option>\n";

        $sel = null;

        while(list($topicid, $topics) = mysql_fetch_row($toplist)) {
        if ($topicid==$topic) { $sel = "selected "; }
            echo "<option $sel value=\"$topicid\">$topics</option>\n";
        $sel = "";
        }
    echo "</select></td></tr>\n";
//    echo "<br><br>";
    $cat = $catid;
    SelectCategory($cat);

    if (!isset($ihome))
    {
        $ihome = null;
    }

    puthome($ihome);
    ?>
    <tr><td valign="top"><b><?php echo translate("The Story"); ?></b></td>
    <td><textarea class='textbox' wrap=virtual cols=70 rows=18 name='hometext'><?PHP echo $hometext4form; ?></textarea><br />
    <?php HTMLbuttons( 1 ); ?></td></tr>
    <tr><td valign="top"><b><?php echo translate("Extended Text"); ?></b></td>
    <td><textarea class='textbox' wrap=virtual cols=70 rows=18 name='bodytext'><?PHP echo $bodytext4form; ?></textarea><br />
    <?php
    HTMLbuttons( 2 );
	echo "</td></tr></table>\n";
    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='hometext|bodytext' />\n";
    echo "<input type='hidden' name='stripit_form' value='subject' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='1' />\n";

    ?>


    <br><select class=textbox name="op">
    <option value="PreviewAdminStory" SELECTED><?php echo translate("PreviewAdminStory"); ?></option>
    <option value="PostAdminStory"><?php echo translate("PostAdminStory"); ?></option>
    </select>

    <INPUT class='button' type="submit" value="<?php echo translate("Go!"); ?>">
    </form></td></tr></table></td></tr></table>
    <?PHP
    include ("footer.$phpEx");

}

function postAdminStory($subject, $introstory, $fullstory, $topic, $catid, $ihome) {
    global $aid, $mpnTables,$phpEx, $ultramode;

    // Kiril: Start New Sanitizer

    $sanity_ary = compact( 'subject', 'introstory', 'fullstory' );
    $_SERVER['SAN'] -> slashIT( $sanity_ary, TRUE );

    extract( $sanity_ary );
    unset( $sanity_ary );

    // End New Sanitizer


    if (!isset($notes))
    {
        $notes = '';
    }

    $result = mysql_query("insert into ".$mpnTables['stories']." values (NULL, '$catid', '$aid', '$subject', now(), '$introstory', '$fullstory', '0', '0', '$topic', '$aid', '$notes', '$ihome')");
    if (!$result)
    {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        exit();
    }
    $result = mysql_query("update ".$mpnTables['authors']." set counter=counter+1 where aid='$aid'");
    if (isset($ultramode)) {
        ultramode();
    }
    $_SERVER['F_LIB'] -> redirectRequest( 'admin.php?op=adminMain' );

}

function submissions() {
    global $hlpfile, $admin, $mpnTables,$phpEx, $bgcolor2;
    global $system;
    $dummy = 0;
    include ("header.$phpEx");
    GraphicAdmin($hlpfile);
    OpenTable();
    $result = mysql_query("SELECT qid, subject, timestamp FROM ".$mpnTables['queue']." order by timestamp");
    if(mysql_num_rows($result) == 0) {
        echo "<table width=\"100%\"><tr><td align=\"center\"><b>".translate("No New Submissions")."</b></td></tr></table>\n";
    } else {
        echo "<center><font size=2><b>".translate("New Stories Submissions")."</b></font><form action=\"admin.$phpEx\" method=\"post\"><table width=\"100%\" border=1 bgcolor=$bgcolor2>\n";
        while (list($qid, $subject, $timestamp) = mysql_fetch_row($result))
        {
            $subject = stripslashes( $subject );

            $timestamp = formatTimestamp($timestamp);
            echo "<tr>\n
            <td align=middle nowrap><font SIZE=2>&nbsp;(<a href=admin.$phpEx?op=DeleteStory&qid=$qid>".translate("Delete")."</a>-<a href=admin.$phpEx?op=QautoStory&qid=$qid>Auto</a>)&nbsp;</td>\n
            <td width=100%><font SIZE=3>\n";
            if ($subject == "") {
                echo "&nbsp;<a href=\"admin.$phpEx?op=DisplayStory&qid=$qid\">".translate("No Subject")."</a></font>\n";
            } else {
                echo "&nbsp;<a href=\"admin.$phpEx?op=DisplayStory&qid=$qid\">$subject</a></font>\n";
            }
            echo "</td><td align=right nowrap><font SIZE=2>&nbsp;$timestamp&nbsp;</font></td></tr>\n";
            $dummy++;
        }
        if ($dummy < 1) {
            echo "<tr><td bgcolor=\"$bgcolor1\" align=\"center\"><b>".translate("No New Submissions")."</b></form></td></tr></table>\n";
        } else {
            echo "</table></form>\n";
        }
    }
    CloseTable();
    include ("footer.$phpEx");
}


switch($op) {

        case "EditCategory":
			if(!isset($catid)){ $catid = '';}
            EditCategory($catid);
            break;

        case "DelCategory":
            DelCategory($cat);
            break;

        case "YesDelCategory":
            YesDelCategory($catid);
            break;

        case "NoMoveCategory":
            NoMoveCategory($catid, $newcat);
            break;

        case "SaveEditCategory":
            SaveEditCategory($catid, $title);
            break;

        case "SelectCategory":
			if(!isset($cat)){ $cat = '';}
            SelectCategory($cat);
            break;

        case "AddCategory":
            AddCategory();
            break;

        case "SaveCategory":
            SaveCategory($title);
            break;

        case "DisplayStory":
            displayStory($qid);
            break;

        case "PreviewAgain":
            previewStory($qid, $uid, $author, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome);
            break;

        case "PostStory":
            postStory($qid, $uid, $author, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome);
            break;

        case "EditStory":
            editStory($sid);
            break;

        case "RemoveStory":
            if (!isset($ok))
            {
                $ok = null;
            }

            removeStory($sid, $ok);
            break;

        case "ChangeStory":
            changeStory($sid, $subject, $hometext, $bodytext, $topic, $notes, $catid, $ihome);
            break;

        case "DeleteStory":
            deleteStory($qid);
            break;

        case "adminStory":
            adminStory();
            break;

        case "PreviewAdminStory":
            previewAdminStory($subject, $hometext, $bodytext, $topic, $catid, $ihome);
            break;

        case "PostAdminStory":
            postAdminStory($subject, $hometext, $bodytext, $topic, $catid, $ihome);
            break;

        case 'submissions':
            submissions();
        break;

}

?>
