<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x and PhpBB integration source code               */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2013 by Philippe Brunier   */
/* Great mods by snipe                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
settype($cancel, "string");
if ($cancel) {
   header("Location: viewforum.php?forum=$forum");
}

if (!function_exists("Mysql_Connexion")) {
   include ("mainfile.php");
}

include('functions.php');
if ($SuperCache) {
   $cache_obj = new cacheManager();
} else {
   $cache_obj = new SuperCacheEmpty();
}
include('auth.php');
global $NPDS_Prefix;

$rowQ1=Q_Select ("SELECT forum_name, forum_moderator, forum_type, forum_pass, forum_access, arbre FROM ".$NPDS_Prefix."forums WHERE forum_id = '$forum'", 3600);
if (!$rowQ1)
   forumerror('0001');

list(,$myrow) = each($rowQ1);
$forum_name = $myrow['forum_name'];
$forum_access = $myrow['forum_access'];
$moderator = get_moderator($myrow['forum_moderator']);
$moderator=explode(" ",$moderator);

if (isset($user)) {
   $userX = base64_decode($user);
   $userdata = explode(":", $userX);
   $Mmod=false;
   for ($i = 0; $i < count($moderator); $i++) {
       if (($userdata[1]==$moderator[$i])) { $Mmod=true; break;}
   }
   $userdata = get_userdata($userdata[1]);
}

if ( ($myrow['forum_type'] == 1) and ($Forum_passwd != $myrow['forum_pass']) ) {
   header("Location: forum.php");
}
if ($forum_access== 9) {
   header("Location: forum.php");
}
if (!does_exists($forum, "forum")) {
   forumerror(0030);
}
// Forum ARBRE
if ($myrow['arbre'])
   $hrefX="viewtopicH.php";
else
   $hrefX="viewtopic.php";

settype($submitS,'string');
settype($stop,'integer');
if ($submitS) {
   if ($message == '')  {
      $stop=1;
   }
   if ($subject == '') {
      $stop=1;
   }
   if (!isset($user)) {
      if ($forum_access == 0) {
         $userdata = array("uid" => 1);
         $modo="";
         include('header.php');
      } else {
         if (($username=="") or ($password=="")) {
            forumerror('0027');
         } else {
            $result = sql_query("select pass FROM ".$NPDS_Prefix."users WHERE uname='$username'");
            list($pass) = sql_fetch_row($result);
            if (!$system) {
               $passwd=crypt($password,$pass);
            } else {
              $passwd=$password;
            }
            if ((strcmp($passwd,$pass)==0) and ($pass != "")) {
               $userdata = get_userdata($username);
               include('header.php');
            } else {
               forumerror('0028');
            }
         }
      }
   } else {
      $modo=user_is_moderator($userdata['uid'],$userdata['uname'],$forum_access);
      include('header.php');
   }
   // Either valid user/pass, or valid session. continue with post.
   if ($stop != 1) {
      $poster_ip =  getip();
      if ($dns_verif)
         $hostname=@gethostbyaddr($poster_ip);
      else
         $hostname="";

     // anti flood
      anti_flood ($modo, $anti_flood, $poster_ip, $userdata, $gmt);

      //anti_spambot
      if (!R_spambot($asb_question, $asb_reponse, $message)) {
         Ecr_Log("security", "Forum Anti-Spam : forum=".$forum." / topic_title=".$subject, "");
         redirect_url("index.php");
         die();
      }

      if ($myrow['forum_type']==8) {
         $formulaire=$myrow['forum_pass'];
         include ("modules/sform/forum/forum_extender.php");
      }
      if ($allow_html == 0 || isset($html))
         $message = htmlspecialchars($message,ENT_COMPAT|ENT_HTML401,cur_charset);
      if (isset($sig) && $userdata['uid'] != 1 && $myrow['forum_type']!=6 && $myrow['forum_type']!=5) {
         $message .= " [addsig]";
      }
      if (($myrow['forum_type']!=6) and ($myrow['forum_type']!=5)) {
         $message = aff_code($message);
         $message = str_replace("\n", "<br />", $message);
      }
      if (($allow_bbcode) and ($myrow['forum_type']!=6) and ($myrow['forum_type']!=5)) {
         $message = smile($message);
      }
      if (($myrow['forum_type']!=6) and ($myrow['forum_type']!=5)) {
         $message = make_clickable($message);
         $message = removeHack($message);
      }
      $message = addslashes($message);
      if (!isset($Mmod)) {
         $subject = removeHack(strip_tags($subject));
      }

      $Msubject = $subject;
      $time = date("Y-m-d H:i",time()+($gmt*3600));
      $sql = "INSERT INTO ".$NPDS_Prefix."forumtopics (topic_title, topic_poster, current_poster, forum_id, topic_time, topic_notify) VALUES ('$subject', '".$userdata['uid']."', '".$userdata['uid']."', '$forum', '$time'";
      if (isset($notify2) && $userdata['uid'] != 1) {
         $sql .= ", '1'";
      } else {
         $sql .= ", '0'";
      }
      $sql .= ")";
      if(!$result = sql_query($sql)) {
         forumerror(0020);
      }
      $topic_id = sql_last_id();
      $sql = "INSERT INTO ".$NPDS_Prefix."posts (topic_id, image, forum_id, poster_id, post_text, post_time, poster_ip, poster_dns) VALUES ('$topic_id', '$image_subject', '$forum', '".$userdata['uid']."', '$message', '$time', '$poster_ip', '$hostname')";
      if (!$result = sql_query($sql)) {
         forumerror(0020);
      } else {
         $IdPost=sql_last_id();
      }
      $sql = "UPDATE ".$NPDS_Prefix."users_status SET posts=posts+1 WHERE (uid='".$userdata['uid']."')";
      $result = sql_query($sql);
      if (!$result) {
         forumerror(0029);
      }
      $topic = $topic_id;
      global $subscribe;
      if ($subscribe) {
         subscribe_mail("forum",$topic,stripslashes($forum),stripslashes($Msubject),$userdata['uid']);
      }
      if (isset($upload)) {
         include("modules/upload/upload_forum.php");
         win_upload("forum_npds",$IdPost,$forum,$topic,"win");
      }
      redirect_url($hrefX."?forum=$forum&topic=$topic");
   } else {
      opentable();
      echo "<p align=\"center\">".translate("You must provide subject and message to post your topic.")."</p>";
      closetable();
   }

} else {
   include('header.php');
   if ($allow_bbcode) {
      include("lib/formhelp.java.php");
   }

   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo "<b>".translate("Moderated By: ")."</b>";
   for ($i = 0; $i < count($moderator); $i++) {
      echo "<a href=\"user.php?op=userinfo&amp;uname=$moderator[$i]\" class=\"box\">$moderator[$i]</a> ";
   }
   echo "</td></tr></table><br />";
   echo "<b>".translate("Post New Topic in:")."</b>";
   echo "&nbsp;<a href=\"viewforum.php?forum=$forum\" class=\"noir\">$forum_name</a>&nbsp;&nbsp;|&nbsp;&nbsp;";
   echo "<a href=\"forum.php\" class=\"noir\">".translate("Forum Index")."</a>\n";
   echo "<br />";
   echo "<form action=\"newtopic.php\" method=\"post\" name=\"coolsus\">";
   echo "<table border=\"0\" cellspacing=\"1\" cellpadding=\"2\" width=\"100%\">";
   echo "<tr><td class=\"header\" colspan=\"2\">".translate("About Posting:")."</td></tr>";
   echo "<tr>";
   if ($forum_access == 0) {
      echo "<td colspan=\"2\">".translate("Anonymous users can post new topics and replies in this forum.")."</td>";
   } else if($forum_access == 1) {
      echo "<td colspan=\"2\">".translate("All registered users can post new topics and replies to this forum.")."</td>";
   } else if($forum_access == 2) {
      echo "<td colspan=\"2\">".translate("Only Moderators can post new topics and replies in this forum.")."</td>";
   }
   echo "</tr>";
   $userX = base64_decode($user);
   $userdata = explode(":", $userX);
   if ($forum_access == 1) {
      if (!isset($user)) {
         echo "<tr align=\"left\">";
         echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Nickname: ")."<b></td>";
         echo "<td class=\"lignb\">";
         echo "<input class=\"textbox_standard\" type=\"text\" name=\"username\" size=\"25\" maxlength=\"40\" value=\"$username\" /></td>";
         echo "</tr><tr align=\"left\">";
         echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Password: ")."</b></td>";
         echo "<td class=\"lignb\"><input class=\"textbox_standard\" type=\"password\" name=\"password\" size=\"25\" maxlength=\"25\" value=\"$password\" /></td>";
         echo "</tr>";
         $allow_to_post = 1;
      } else {
         echo "<tr align=\"left\">";
         echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Author")." :<b></td>";
         echo "<td class=\"lignb\">";
         echo $userdata[1];
         echo "</td></tr>";
         $allow_to_post = 1;
      }
   } elseif ($forum_access==2) {
         if (user_is_moderator($userdata[0],$userdata[2],$forum_access)) {
            echo "<tr align=\"left\">";
            echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Author")." :<b></td>";
            echo "<td class=\"lignb\">";
            echo $userdata[1];
            echo "</td></tr>";
            $allow_to_post = 1;
         }
   } elseif ($forum_access == 0) {
      $allow_to_post = 1;
   }

   settype($submitP,'string');
   if ($allow_to_post) {
      if ($submitP) {
         $acc = "newtopic";
         $subject=stripslashes($subject);
         $message=stripslashes($message);
         if (isset($username))
            $username=stripslashes($username);
         else
            $username="";
         if (isset($password))
            $password=stripslashes($password);
         else
            $password="";
         echo "<tr><td colspan=\"2\">";
         include ("preview.php");
         echo "</td></tr>";
      } else {
        $username="";
        $password="";
        $subject="";
        $message="";
      }
      echo "<tr align=\"left\">";
      if ($myrow['forum_type']==8) {
         echo "<td colspan=\"2\">";
         $formulaire=$myrow['forum_pass'];
         include ("modules/sform/forum/forum_extender.php");
      } else {
         echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Subject: ")."</b></td>";
         echo "<td class=\"lignb\"><input class=\"textbox_standard\" type=\"text\" name=\"subject\" size=\"45\" maxlength=\"100\" value=\"$subject\" /></td>";
         echo "</tr>";
         if ($smilies) {
            echo "<tr align=\"left\" valign=\"top\">
            <td class=\"lignb\" width=\"25%\"><b>".translate("Message Icon: ")."<b></td>
            <td class=\"lignb\">";
            settype($image_subject,'string');
            echo emotion_add($image_subject);
            echo "</td></tr>";
         }
         echo "<tr align=\"left\">";
         echo "<td class=\"lignb\" width=\"25%\" valign=\"top\"><b>".translate("Message: ")."</b><br /><br />";
         echo "<span style=\"font-size: 10px;\">";
         echo "HTML : ";
         if ($allow_html==1) {
            echo translate("On")."<br />";
            echo HTML_Add();
         } else
            echo translate("Off")."<br />";
         echo "</span></td>";
         echo "<td class=\"lignb\">";
         if ($allow_bbcode)
            $xJava = 'name="message" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);" onfocus="storeForm(this)"';
         echo "<textarea class=\"textbox_standard\" $xJava name=\"message\" rows=\"10\" cols=\"60\">$message</textarea><br />";
         if ($allow_bbcode)
            putitems();
         echo "</td></tr><tr align=\"left\">";
         echo "<td class=\"lignb\" width=\"25%\"><b>".translate("Options: ")."</b></td>";
         echo "<td class=\"lignb\">";
         if (($allow_html==1) and ($myrow['forum_type']!=6) and ($myrow['forum_type']!=5)) {
         if (isset($html)) {
            $sethtml = "checked=\"checked\"";
         } else {
            $sethtml = "";
         }
         echo "<input type=\"checkbox\" name=\"html\" $sethtml />".translate("Disable HTML on this Post")."<br />";
         }
         if ($user) {
            if ($allow_sig == 1||$sig == "on") {
               $asig = sql_query("select attachsig from ".$NPDS_Prefix."users_status where uid='$cookie[0]'");
               list($attachsig) = sql_fetch_row($asig);
               if ($attachsig == 1) {
                  $s = "checked=\"checked\"";
               } else {
                  $s = "";
               }
               if (($myrow['forum_type']!=6) and ($myrow['forum_type']!=5)) {
                  echo "<input type=\"checkbox\" name=\"sig\" $s />".translate("Show signature")." <span style=\"font-size: 10px;\">(".translate("This can be altered or added in your profile").")</span><br />";
               }
            }
            if ($allow_upload_forum) {
               if ($upload == "on") {
                  $up = "checked=\"checked\"";
               }
               echo "<input type=\"checkbox\" name=\"upload\" $up />".translate("Upload file after send accepted")."<br />";
            }
            if (isset($notify2)) {
               $selnot="checked=\"checked\"";
            } else {
               $selnot="";
            }
            echo "<input type=\"checkbox\" name=\"notify2\" $selnot />".translate("Notify by email when replies are posted")."<br />";
         }
         echo "</td></tr><tr>";
         echo "<td class=\"ligna\" align=\"center\">".Q_spambot()."</td>";
         echo "<td class=\"ligna\" align=\"center\">";
         echo "<input type=\"hidden\" name=\"forum\" value=\"$forum\" />";
         echo "<br /><input class=\"bouton_standard\" type=\"submit\" name=\"submitS\" value=\"".translate("Submit")."\" accesskey=\"s\" />&nbsp;";
         echo "&nbsp;<input class=\"bouton_standard\" type=\"submit\" name=\"submitP\" value=\"".translate("Preview")."\" />&nbsp;";
         echo "&nbsp;<input class=\"bouton_standard\" type=\"reset\" value=\"".translate("Clear")."\" />&nbsp;";
         echo "&nbsp;<input class=\"bouton_standard\" type=\"submit\" name=\"cancel\" value=\"".translate("Cancel Post")."\" /><br /><br />";
      }
      echo "</td></tr>";
   }
   echo "</table></form>";
}
include('footer.php');
?>