<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2012 by Philippe Brunier                     */
/*                                                                      */
/* 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.       */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {
   include ("mainfile.php");
}

include ("publication.php");

if ($mod_admin_news>0) {
   if ($admin=="" and $user=="") {
      Header("Location: index.php");
      exit;
   }
   if ($mod_admin_news==1) {
      if ($user!="" and $admin=="") {
         global $cookie;
         $result = sql_query("select level from ".$NPDS_Prefix."users_status where uid='$cookie[0]'");
         if (sql_num_rows($result)==1) {
            list($userlevel) = sql_fetch_row($result);
            if ($userlevel==1) {
               Header("Location: index.php");
               exit;
            }
         }
      }
   }
}

function defaultDisplay() {
   global $NPDS_Prefix;

   include ('header.php');
   global $user, $anonymous;
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo translate("Submit News");
   echo "</td></tr></table>\n";
   echo "<br />";
   if (isset($user)) $userinfo=getusrinfo($user);
   echo "<form action=\"submit.php\" method=\"post\" name=\"adminForm\">"
        ."<b>".translate("Your Name")."</b> : ";
   if ($user) {
      echo "<a href=\"user.php\" class=\"noir\">".$userinfo['name']."</a> [ <a href=\"user.php?op=logout\" class=\"rouge\">".translate("Logout")."</a> ]";
      echo "<input type=\"hidden\" name=\"name\" value=\"".$userinfo['name']."\" />";
   } else {
      echo "$anonymous [ <a href=\"user.php\" class=\"noir\">".translate("New User")."</a> ]";
      echo "<input type=\"hidden\" name=\"name\" value=\"$anonymous\" />";
   }
   echo "<br /><br /><b>".translate("Title")." </b> (".translate ("Be Descriptive, Clear and Simple").") : ";
   echo "<input class=\"textbox_standard\" type=\"text\" name=\"subject\" size=\"55\" maxlength=\"255\" /> ".translate("bad titles='Check This Out!' or 'An Article'.");
   echo "<br /><br /><b>".translate("Topic")."</b> : <select class=\"textbox_standard\" name=\"topic\">";
   $toplist = sql_query("select topicid, topictext from ".$NPDS_Prefix."topics order by topictext");
   echo "<option value=\"\">".translate("Select Topic")."</option>\n";
   while (list($topicid, $topics) = sql_fetch_row($toplist)) {
      if ($topicid==$topic) { $sel = "selected=\"selected\" "; }
      echo "<option $sel value=\"$topicid\">".aff_langue($topics)."</option>\n";
      $sel = "";
   }
   echo "</select><br /><br />";
   echo "<b>".translate("Intro Text")."</b> ( ".translate("HTML is fine, but double check those URLs and HTML tags!")." ) :<br />";
   echo "<textarea class=\"textbox\" cols=\"70\" rows=\"25\" name=\"story\" style=\"width: 100%;\"></textarea>";
   echo aff_editeur("story", "true");
   echo "<br /><br /><b>".translate("Full Text")."</b> :<br />";
   echo "<textarea class=\"textbox\" cols=\"70\" rows=\"25\" name=\"bodytext\" style=\"width: 100%;\"></textarea>";
   echo aff_editeur("bodytext", "true");

   publication(0,0,0,0,0, 0,0,0,0,0, 0);

   echo "<hr noshade=\"noshade\" class=\"ongl\" />";
   echo "<input class=\"bouton_standard\" type=\"submit\" name=\"op\" value=\"".translate("Preview")."\" /> - ";
   echo translate("You must preview once before you can submit");
   echo "</form>";

   closetable();
   include ('footer.php');
}

function PreviewStory($name, $subject, $story, $bodytext,$topic, $deb_day,$deb_month,$deb_year,$deb_hour,$deb_min, $fin_day,$fin_month,$fin_year,$fin_hour,$fin_min, $epur) {
   global $tipath, $NPDS_Prefix;

   include ('header.php');
   $subject = stripslashes(str_replace("\"","&quot;",(strip_tags($subject))));
   $story = stripslashes($story);
   $bodytext = stripslashes($bodytext);
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo translate("Submit News");
   echo "</td></tr></table>\n";
   echo "<form action=\"submit.php\" method=\"post\" name=\"adminForm\">"
        ."<b>".translate("Your Name")."</b> : $name";
   echo "<input type=\"hidden\" name=\"name\" value=\"$name\" />";
   echo "<br /><p align=\"center\">";
   $rowcolor = tablos();
   echo "<table border=\"0\" width=\"85%\" cellpadding=\"0\" cellspacing=\"1\"><tr $rowcolor><td>";
   $rowcolor = tablos();
   echo "<table width=\"100%\" border=\"0\" cellpadding=\"8\" cellspacing=\"1\"><tr $rowcolor>";
   if ($topic=="") {
      $topicimage="all-topics.gif";
      $warning = "<br /><p align=\"center\" class=\"rouge\"><b>".translate("Select Topic")."</b></p>";
   } else {
      $warning = "";
      $result = sql_query("select topictext, topicimage from ".$NPDS_Prefix."topics where topicid='$topic'");
      list($topictext, $topicimage) = sql_fetch_row($result);
   }
   $no_img=false;
   if ((file_exists("$tipath$topicimage")) and ($topicimage!="")) {
      echo "<td><img src=\"".$tipath.$topicimage."\" border=\"0\" align=\"right\" alt=\"\" />";
   } else {
      echo "<td>";
      $no_img=true;
   }

   $storyX=aff_code($story);
   $bodytextX=aff_code($bodytext);
   themepreview($subject, $storyX, $bodytextX);

   if ($no_img) {
      echo "</td><td width=\"10%\" align=\"center\" valign=\"middle\"><b>".aff_langue($topictext)."</b>";
   }
   echo "$warning";
   echo "</td></tr></table></td></tr></table></p>";

   echo "<br /><b>".translate("Title")."</b>&nbsp;:&nbsp;&nbsp;";
   echo "<input class=\"textbox_standard\" type=\"text\" name=\"subject\" size=\"70\" maxlength=\"255\" value=\"$subject\" /><br /><br />";
   echo "<b>".translate("Topic")."</b> : <select class=\"textbox_standard\" name=\"topic\">";
   $toplist = sql_query("select topicid, topictext from ".$NPDS_Prefix."topics order by topictext");
   echo "<option value=\"\">".translate("Select Topic")."</option>\n";
   while (list($topicid, $topics) = sql_fetch_row($toplist)) {
      if ($topicid==$topic) { $sel = "selected=\"selected\" "; }
      echo "<option $sel value=\"$topicid\">".aff_langue($topics)."</option>\n";
      $sel = "";
   }
   echo "</select><br /><br />";
   echo "<b>".translate("Intro Text")."</b> ( ".translate("HTML is fine, but double check those URLs and HTML tags!")." ) :<br />";
   echo "<textarea class=\"textbox\" cols=\"70\" rows=\"25\" name=\"story\" style=\"width: 100%;\">$story</textarea>";
   echo aff_editeur("story", "true");
   echo "<br /><br /><b>".translate("Full Text")."</b> :<br />";
   echo "<textarea class=\"textbox\" cols=\"70\" rows=\"25\" name=\"bodytext\" style=\"width: 100%;\">$bodytext</textarea>";
   echo aff_editeur("bodytext", "true");

   publication($deb_day,$deb_month,$deb_year,$deb_hour,$deb_min, $fin_day,$fin_month,$fin_year,$fin_hour,$fin_min, $epur);

   echo "<hr noshade=\"noshade\" class=\"ongl\" /><input class=\"bouton_standard\" type=\"submit\" name=\"op\" value=\"".translate("Preview")."\" />";
   echo " - ".Q_spambot();
   echo "<input class=\"bouton_standard\" type=\"submit\" name=\"op\" value=\"Ok\" /></form>";
   closetable();
   include ('footer.php');
}

function submitStory($subject, $story, $bodytext, $topic, $date_debval,$date_finval,$epur, $asb_question, $asb_reponse) {
   global $user, $EditedMessage, $anonymous, $notify, $NPDS_Prefix;

   if ($user) {
      global $cookie;
      $uid = $cookie[0];
      $name = $cookie[1];
   } else {
      $uid = -1;
      $name = $anonymous;
      //anti_spambot
      if (!R_spambot($asb_question, $asb_reponse, "")) {
         Ecr_Log("security", "Submit Anti-Spam : name=".$yname." / mail=".$ymail, "");
         redirect_url("index.php");
         die();
      }
   }

   $subject=removeHack(stripslashes(FixQuotes(str_replace("\"","&quot;",(strip_tags($subject))))));
   $story=removeHack(stripslashes(FixQuotes($story)));
   $bodytext=removeHack(stripslashes(FixQuotes($bodytext)));

   $result = sql_query("insert into ".$NPDS_Prefix."queue values (NULL, '$uid', '$name', '$subject', '$story', '$bodytext', now(), '$topic','$date_debval','$date_finval','$epur')");
   if (sql_last_id()) {
      if ($notify) {
         global $notify_email, $notify_subject, $notify_message, $notify_from;
         send_email($notify_email, $notify_subject, $notify_message, $notify_from , false, "text");
      }
      include ('header.php');
      opentable();
      echo translate("Thanks for your submission.");
      closetable();
      include ('footer.php');
   } else {
      include ('header.php');
      opentable();
      echo sql_error();
      closetable();
      include ('footer.php');
   }
}

settype($op,'string');
switch ($op) {
   case "Preview":
   case translate("Preview"):
        PreviewStory($name, $subject, $story, $bodytext, $topic, $deb_day,$deb_month,$deb_year,$deb_hour,$deb_min, $fin_day,$fin_month,$fin_year,$fin_hour,$fin_min,  $epur);
        break;

   case "Ok":
        settype($date_debval,'string');
        if (!$date_debval) {
           if (strlen($deb_day)==1) {
              $deb_day = "0$deb_day";
           }
           if (strlen($deb_month)==1) {
              $deb_month = "0$deb_month";
           }
           $date_debval = "$deb_year-$deb_month-$deb_day $deb_hour:$deb_min:00";
        }
        settype($date_finval,'string');
        if (!$date_finval) {
           if (strlen($fin_day)==1) {
              $fin_day = "0$fin_day";
           }
           if (strlen($fin_month)==1) {
              $fin_month = "0$fin_month";
           }
           $date_finval = "$fin_year-$fin_month-$fin_day $fin_hour:$fin_min:00";
        }
        if ($date_finval<$date_debval) {
           $date_finval = $date_debval;
        }
        SubmitStory($subject, $story, $bodytext, $topic, $date_debval,$date_finval,$epur, $asb_question, $asb_reponse);
        break;

   default:
        defaultDisplay();
        break;
}
?>