<?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: mymsg2.php,v 1.12.2.5 2004/08/15 12:58:19 kirilt Exp $



/************************************************************************/
/*  myMessage II v1.2.1 (MPN Addon)                                     */
/*  Kazumi Ono                                                          */
/*  http://www.myweb.ne.jp                                              */
/*                                                                      */
/*                                                                      */
/* This addon will let you add your own messages at the top of your     */
/* home page and each category page. There is also a function to        */
/* add your own message to each topic page, but this feature            */
/* will not be used in MPN1.8.6. Only the admin can add/edit/delete     */
/* messages.                                                            */
/*                                                                      */
/************************************************************************/


function list_mymsg2(){
    global $phpEx, $mpnTables;
    OpenTable();
    echo "<center><h3>".translate("My Message Configuration")."</h3></center>\n";
    echo "<font size=\"+1\"><b>".translate("Home Message")."</b></font><br>\n";
    echo "<table width=\"100%\">\n";
    echo "<tr><td width=\"20%\"><b>Type</b></td><td width=\"30%\"><b>".translate("Message Title")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Display")."</b></td><td width=\"30%\" align=\"center\"><b>".translate("Last Update")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Action")."</b></td></tr>\n";

    $message_name="Home";

    $result = mysql_query("SELECT message_id,title,time,display from ".$mpnTables['mymsg2']." WHERE catid=0 and topicid=0 ORDER BY time DESC");
    $numrows=mysql_num_rows($result);
    if($numrows==0){
        echo "<tr><td><a href=\"" . _MPN_ROOT_URI . "index.$phpEx\">$message_name</a></td>";
        echo "<td></td><td></td><td></td><td align=\"center\">";
        echo "<a href=\"admin.$phpEx?fct=mymsg2&op=add&catid=0&topicid=0\">".translate("Add")."</a>";
    }else{
        while(list($message_id,$title,$time,$display)=mysql_fetch_row($result)){
            echo "<tr><td><a href=\"" . _MPN_ROOT_URI . "index.$phpEx\">$message_name</a></td>";
            $title=StripSlashes($title);
            echo "<td>$title</td>";
            if($display==1)echo "<td align=\"center\">".translate("Yes")."</td>";
            else echo "<td align=\"center\">".translate("No")."</td>";
            echo "<td align=\"center\">$time</td>";
            echo "<td align=\"center\"><a href=\"admin.$phpEx?fct=mymsg2&op=edit&message_id=$message_id\">".translate("Edit")."</a>&nbsp;/&nbsp;<a href=\"admin.$phpEx?fct=mymsg2&op=delete&message_id=$message_id\">".translate("Delete")."</a></td></tr>\n";
        }
        echo "<tr><td><a href=\"" . _MPN_ROOT_URI . "index.$phpEx\">$message_name</a></td>";
        echo "<td></td><td></td><td></td><td align=\"center\">";
        echo "<a href=\"admin.$phpEx?fct=mymsg2&op=add&catid=0&topicid=0\">Add</a>";
    }
    echo "</td></tr>\n";
    echo "</table>\n<br><br>";

    echo "<font size=\"+1\"><b>".translate("Category Message")."</b></font>&nbsp;[<a href=\"" . _MPN_ROOT_URI . "admin.$phpEx?op=AddCategory\">".translate("Create category")."</a>]<br>\n";
    echo "<table width=\"100%\">\n";
    echo "<tr><td width=\"20%\"><b>".translate("Category")."</b></td><td width=\"30%\"><b>".translate("Message Title")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Display")."</b></td><td width=\"30%\" align=\"center\"><b>".translate("Last Update")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Action")."</b></td></tr>\n";

    $result = mysql_query("SELECT catid,title from ".$mpnTables['stories_cat']."");
    if(mysql_num_rows($result)>0)
    {
      while(list($catid,$message_name)=mysql_fetch_row($result))
      {
        echo "<tr><td><a href=\"" . _MPN_ROOT_URI . "categories.$phpEx?op=newindex&catid=$catid\">$message_name</a></td>";

        //$message_name=urlencode($message_name);

        $result2=mysql_query("SELECT message_id,title,display,time from " . $mpnTables['mymsg2'] . " WHERE catid=$catid");

        $numrows=mysql_num_rows($result2);
        if($numrows==0){
          echo "<td></td><td></td><td></td><td align=\"center\">\n";
          echo "<a href=\"admin.$phpEx?fct=mymsg2&op=add&catid=$catid&topicid=0\">Add</a>\n";
          echo"</td></tr>";
        }else{
            list($message_id,$title,$display,$time)=mysql_fetch_row($result2);
            $title=StripSlashes($title);
            echo "<td>$title</td>";
            if($display==1)echo "<td align=\"center\">".translate("Yes")."</td>";
            else echo "<td align=\"center\">".translate("No")."</td>";
            echo "<td align=\"center\">$time</td>";
            echo "<td align=\"center\"><a href=\"admin.$phpEx?fct=mymsg2&op=edit&message_id=$message_id\">".translate("Edit")."</a>&nbsp;/&nbsp;<a href=\"admin.$phpEx?fct=mymsg2&op=delete&message_id=$message_id\">".translate("Delete")."</a>";
            echo "</td></tr>\n";
        }
      }
    }else{echo "<tr><td colspan=\"5\">".translate("You don't have any category").".</td></tr>";
    }
    echo "</table>\n<br><br>";
/*
    echo "<hr>";
    echo "<font size=\"2\">".translate("This section is reserved for future use").".</font><br>";
    echo "<font size=\"+1\"><b>".translate("Topic Message")."</b></font>&nbsp;[<a href=\"$nuke_url" . "admin.$phpEx?op=topicsmanager\">".translate("Create topic")."</a>]<br>\n";
    echo "<table width=\"100%\">\n";
    echo "<tr><td width=\"20%\"><b>Topic</b></td><td width=\"30%\"><b>".translate("Message Title")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Display")."</b></td><td width=\"30%\" align=\"center\"><b>".translate("Last Update")."</b></td><td width=\"10%\" align=\"center\"><b>".translate("Action")."</b></td></tr>\n";
    $result = mysql_query("SELECT topicid,topictext from ".$mpnTables['topics']."");
    if(mysql_num_rows($result)>0){
      while(list($topicid,$message_name)=mysql_fetch_row($result)){
        echo "<tr><td>$message_name</td>";
        $message_name=urlencode($message_name);
        $result2 = mysql_query("SELECT message_id,title,display,time from " . $mpnTables['mymsg2'] . " WHERE topicid=$topicid");
        $numrows=mysql_num_rows($result2);
        if($numrows==0){
            echo "<td></td><td></td><td></td><td align=\"center\">\n";
            echo "<a href=\"admin.$phpEx?fct=mymsg2&op=add&catid=0&topicid=$topicid&message_name=$message_name\">Add</a>\n";
            echo"</td></tr>";
        }else{
            list($message_id,$title,$display,$time)=mysql_fetch_row($result2);
            $title=StripSlashes($title);
            echo "<td>$title</td>";
            if($display==1)echo "<td align=\"center\">".translate("Yes")."</td>";
            else echo "<td align=\"center\">".translate("No")."</td>";
            echo "<td align=\"center\">$time</td>";
            echo "<td align=\"center\"><a href=\"admin.$phpEx?fct=mymsg2&op=edit&message_id=$message_id&message_name=$message_name\">".translate("Edit")."</a>&nbsp;/&nbsp;<a href=\"admin.$phpEx?fct=mymsg2&op=delete&message_id=$message_id&message_name=$message_name\">".translate("Delete")."</a>";
            echo "</td></tr>\n";
        }
      }
    }else{echo "<tr><td colspan=\"5\">".translate("You don't have any topic").".</td></tr>";
    }
    echo "</table>\n";
*/
    CloseTable();
/*
    echo "<br>";
    OpenTable();
    echo "<center>".translate("Script from")." Kazumi Ono, myWeb JAPAN.<br>\n";
    echo translate("For the latest version of this script, please go to")." <a href=\"http://www.myweb.ne.jp/\">www.myweb.ne.jp</a>.</center>";
    CloseTable();
*/
}

function edit_mymsg2($message_id,$message_name = ''){
    global $phpEx, $mpnTables;

    $result=mysql_query("SELECT message_id,title,text,display,catid from " . $mpnTables['mymsg2'] . " WHERE message_id='$message_id'");
    if(!$result) {
        echo mysql_errno(). ": ".mysql_error(). "<br>";
        exit();
    }


    OpenTable();
    echo  "<table>\n";
    echo "<form action=\"admin.$phpEx?fct=mymsg2\" method=\"post\">\n";

    list( $message_id, $title, $text, $display, $catid ) = mysql_fetch_row($result);


        $tmp = mysql_query("SELECT title from ".$mpnTables['stories_cat']." WHERE catid = '$catid'");

        list( $message_name ) = mysql_fetch_row( $tmp );

        mysql_free_result( $tmp );


        // Kiril: New Sanitizer

        $sanity_ary = compact( 'title', 'text' );
        $_SERVER['SAN'] -> slashIT( $sanity_ary );

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

        // End New Sanitizer


        echo "<tr><td>".translate("Message for")." <b>".$message_name."</b></td></tr>\n";
        echo "<tr><td>".translate("Message title").":&nbsp;&nbsp;<input name=\"title\" type=\"textbox\" value=\"$title\"></td></tr>\n";
        echo "<tr><td>".translate("Message text").":<br><textarea name=\"text\" cols=\"70\" rows=\"20\">$text</textarea></td></tr>\n";
        echo "<tr><td>".translate("Display this message")."&nbsp;<input type=\"checkbox\" name=\"display\" value=\"1\"";
        if($display==1)echo " ".translate("checked");
        echo "></td></tr>\n";
    echo "<tr><td>\n";

    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='text' />\n";
    echo "<input type='hidden' name='stripit_form' value='title' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='2' />\n";

    echo "<input type=\"hidden\" name=\"message_id\" value=\"$message_id\">\n";

    echo "<input class=\"button\" type=\"submit\" value=\"".translate("Update")."\">\n";
    echo "<input type=\"button\" onClick=\"history.go(-1)\" value =\"".translate("Cancel")."\"></td></tr>\n";
    echo "<input type=\"hidden\" name=\"op\" value=\"update\">\n";
    echo "</form></table>\n";

    CloseTable();
}

function update_mymsg2($message_id,$title,$text,$display){
    global $phpEx, $mpnTables;

    $error = 0;
    OpenTable();

    if ($title == "")
    {
        $error = 1;
        echo translate("Please enter message title").".<br>";
    }
    if ($text == "")
    {
        $error = 1;
        echo translate("Please enter message text").".<br>";
    }
    if ($error == 1){
        echo "<br><input type=button onClick=history.go(-1) value=\"".translate("Back")."\">";
    }
    else
    {

        $query = "UPDATE " . $mpnTables['mymsg2'] . " set title='$title', text='$text', time=now(), display='$display'  WHERE message_id=$message_id";

        mysql_query("$query");
        echo translate("Successfully Updated")."!";
        echo "<br><br><a href=\"admin.$phpEx?fct=mymsg2\">".translate("Back to message list")."</a>\n";
    }
    CloseTable();

}

function add_mymsg2($catid,$topicid,$message_name = '')
{
    global $mpnTables, $phpEx;

    $result = mysql_query("SELECT title from ".$mpnTables['stories_cat']." WHERE catid = '$catid'");

    list( $message_name ) = mysql_fetch_row( $result );


    OpenTable();
    echo  "<table>\n";
    echo "<form action=\"admin.$phpEx?fct=mymsg2\" method=\"post\">\n";
        echo "<tr><td>Message for <b>".$message_name."</b></td></tr>\n";
        echo "<tr><td>Message title:&nbsp;&nbsp;<input name=\"title\" type=\"textbox\"></td></tr>\n";
        echo "<tr><td>Message text:<br><textarea name=\"text\" cols=\"70\" rows=\"20\"></textarea></td></tr>\n";
        echo "<tr><td>Display this message&nbsp;<input type=\"checkbox\" name=\"display\" value=\"1\" checked></td></tr>\n";

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

    // Kiril: New Sanitizer

    addFormatOption();

    echo "<input type='hidden' name='clickit_form' value='text' />\n";
    echo "<input type='hidden' name='stripit_form' value='title' />\n";
    echo "<input type='hidden' name='mpn_sectype' value='2' />\n";

    // End New Saintizer

    echo "<input type=\"hidden\" name=\"catid\" value=\"$catid\">\n";
    echo "<input type=\"hidden\" name=\"topicid\" value=\"$topicid\">\n";
    echo "<input class=\"button\" type=\"submit\" value=\"".translate("Add")."\">&nbsp;\n";
    echo "<input class=\"button\" type=\"button\" onClick=\"history.go(-1)\" value =\"".translate("Cancel")."\"></td></tr>\n";
    echo "<input class=\"button\" type=\"hidden\" name=\"op\" value=\"insert\">\n";
    echo "</form></table>\n";

    CloseTable();
}

function insert_mymsg2($title,$text,$catid,$topicid,$display){
    global $phpEx, $mpnTables;
    OpenTable();
    if ($title == "")
    {
        $error = 1;
        echo translate("Please enter message title").".<br>";
    }
    if ($text == "")
    {
        $error = 1;
        echo translate("Please enter message text").".<br>";
    }
    if (isset($error) && $error == 1){
        echo "<br><input type=button onClick=history.go(-1) value=\"".translate("Back")."\">";
    }
    else
    {

        mysql_query("INSERT INTO " . $mpnTables['mymsg2'] . " VALUES (NULL, '$title', '$text', '$catid', '$topicid', now(), '$display' )");
        echo translate("New Message Added")."!";
        echo "<br><br><a href=\"admin.$phpEx?fct=mymsg2\">".translate("Back to message list")."</a>\n";
    }
    CloseTable();

}

function delete_mymsg2($message_id,$message_name = '', $ok=0){
    global $phpEx, $mpnTables;
    if($ok==1){
        mysql_query("DELETE FROM " . $mpnTables['mymsg2'] . " WHERE message_id='$message_id'");
        echo translate("Message deleted").".";
        echo "<br><br><a href=\"admin.$phpEx?fct=mymsg2\">".translate("Back to message list")."</a>\n";
    }else{
        $result=mysql_query("SELECT title,display, catid FROM " . $mpnTables['mymsg2'] . " WHERE message_id='$message_id'");

        list($title,$display, $catid)=mysql_fetch_row($result);

        $result = mysql_query("SELECT title from ".$mpnTables['stories_cat']." WHERE catid = '$catid'");

        list( $message_name ) = mysql_fetch_row( $result );

        $title=StripSlashes($title);
        //$text=StripSlashes($text);
        OpenTable();
        echo "<b>".translate("Message title").":</b> ".$title."<br>";
        //echo "<b>".translate("Message text").":</b><br>";
        //echo "$text<br>";
        echo "<b>".translate("Message status").":</b> ";
        if($display)echo translate("displayed")."<br><br>";
        else echo translate("not displayed")."<br><br>";
        echo translate("Are you sure you want to delete this message for")." <b>".$message_name."</b>?<br><br>";
        echo "[<a href=\"javascript:history.go(-1)\">".translate("No")."</a>&nbsp;|&nbsp;<a href=\"admin.$phpEx?fct=mymsg2&op=delete&message_id=$message_id&ok=1\">".translate("Yes")."</a>]";
        CloseTable();
    }
}


?>
