<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2008 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");
}

function FriendSend($sid) {
   global $NPDS_Prefix;

   settype($sid,"integer");
   $result=sql_query("select title, aid  from ".$NPDS_Prefix."stories where sid='$sid'");
   list($title, $aid) = sql_fetch_row($result);
   if (!$aid) {
       header ("Location: index.php");
   }
   include ("header.php");
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo translate("Send Story to a Friend");
   echo "</td></tr></table>\n";
   echo "<br />";
   echo translate("You will send the story")." <b>".aff_langue($title)."</b> ".translate("to a specified friend:")."<br /><br />
   <form action=\"friend.php\" method=\"post\"><input type=\"hidden\" name=\"sid\" value=\"$sid\">";
   global $user;
   if ($user) {
      global $cookie;
      $result=sql_query("select name, email from ".$NPDS_Prefix."users where uname='$cookie[1]'");
      list($yn, $ye) = sql_fetch_row($result);
   }
   echo translate("Your Name: ")."<br /><input class=\"textbox\" type=\"text\" name=\"yname\" value=\"$yn\" size=\"30\" max=\"100\"><br />
   ".translate("Your Email: ")."<br /><input class=\"textbox\" type=\"text\" name=\"ymail\" value=\"$ye\" size=\"30\" max=\"100\"><br /><br />
   ".translate("Friend Name: ")."<br /><input class=\"textbox\" type=\"text\" name=\"fname\" size=\"25\" max=\"100\"><br />
   ".translate("Friend Email: ")."<br /><input class=\"textbox\" type=\"text\" name=\"fmail\" size=\"25\" max=\"100\"><br />";
   echo "<br />".Q_spambot();
   echo "<input type=\"hidden\" name=\"op\" value=\"SendStory\">
   <input class=\"bouton_standard\" type=\"submit\" value=\"".translate("Send")."\">
   </form>";
   closetable();
   include ('footer.php');
}

function SendStory($sid, $yname, $ymail, $fname, $fmail, $asb_question, $asb_reponse) {
   global $user;
   if (!$user) {
      //anti_spambot
      if (!R_spambot($asb_question, $asb_reponse, "")) {
         Ecr_Log("security", "Send-Story Anti-Spam : name=".$yname." / mail=".$ymail, "");
         redirect_url("index.php");
         die();
      }
   }

   global $sitename, $nuke_url;
   global $NPDS_Prefix;
   settype($sid,"integer");
   $result2=sql_query("select title, time, topic from ".$NPDS_Prefix."stories where sid='$sid'");
   list($title, $time, $topic) = sql_fetch_row($result2);
   $result3=sql_query("select topictext from ".$NPDS_Prefix."topics where topicid='$topic'");
   list($topictext) = sql_fetch_row($result3);
   $subject = translate("Interesting Article at")." $sitename";
   $fname=removeHack($fname);
   $message = "".translate("Hello")." $fname :\n\n".translate("Your Friend")." $yname ".translate("considered the following article interesting and wanted to send it to you.")."\n\n".aff_langue($title)."\n".translate("Date:")." $time\n".translate("Topic:")." ".aff_langue($topictext)."\n\n".translate("The Article")." : $nuke_url/article.php?sid=$sid\n\n";
   include("signat.php");
   $fmail=removeHack($fmail);
   $subject=removeHack($subject);
   $message=removeHack($message);
   $yname=removeHack($yname);
   $ymail=removeHack($ymail);
   $stop=false;
   if ((!$fmail) || ($fmail=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$fmail))) $stop = true;
   if ((!$ymail) || ($ymail=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$ymail))) $stop = true;
   if (!$stop) {
      send_email($fmail, $subject, $message, $ymail, false,"text");
   } else {
     $title="";
     $fname="";
   }
   $title = urlencode(aff_langue($title));
   $fname = urlencode($fname);
   Header("Location: friend.php?op=StorySent&title=$title&fname=$fname");
}

function StorySent($title, $fname) {
   include ("header.php");
   opentable();
   $title = urldecode($title);
   $fname = urldecode($fname);
   echo "<p align=\"center\">";
   if ($fname=="") {
      echo translate("ERROR: Invalid email");
   } else {
      echo translate("Story")." <b>".stripslashes($title)."</b> ".translate("has been sent to")." $fname / ".translate("Thanks!");
   }
   echo "</p>";
   closetable();
   include ("footer.php");
}

function RecommendSite() {    
   include ("header.php");
   opentable();
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo translate("Recommend this Site to a Friend");
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<form action=\"friend.php\" method=\"post\"><input type=\"hidden\" name=\"op\" value=\"SendSite\">";
   global $user;
   if ($user) {
      global $cookie;
      global $NPDS_Prefix;
      $result=sql_query("select name, email from ".$NPDS_Prefix."users where uname='$cookie[1]'");
      list($yn, $ye) = sql_fetch_row($result);
   }
   echo translate("Your Name: ")."<br /><input class=\"textbox\" type=\"text\" name=\"yname\" value=\"$yn\" size=\"30\" max=\"100\"><br />
   ".translate("Your Email: ")."<br /><input class=\"textbox\" type=\"text\" name=\"ymail\" value=\"$ye\" size=\"30\" max=\"100\"><br /><br />
   ".translate("Friend Name: ")."<br /><input class=\"textbox\" type=\"text\" name=\"fname\" size=\"25\" max=\"100\"><br />
   ".translate("Friend Email: ")."<br /><input class=\"textbox\" type=\"text\" name=\"fmail\" size=\"25\" max=\"100\"><br />";
   echo "<br />".Q_spambot();
   echo "<input class=\"bouton_standard\" type=\"submit\" value=\"".translate("Send")."\">
   </form>";
   closetable();
   include ('footer.php');
}

function SendSite($yname, $ymail, $fname, $fmail, $asb_question, $asb_reponse) {
   global $user;
   if (!$user) {
      //anti_spambot
      if (!R_spambot($asb_question, $asb_reponse, "")) {
         Ecr_Log("security", "Friend Anti-Spam : name=".$yname." / mail=".$ymail, "");
         redirect_url("index.php");
         die();
      }
   }

   global $sitename, $nuke_url;
   $subject = "".translate("Interesting Site:")." $sitename";
   $fname=removeHack($fname);
   $message = "".translate("Hello")." $fname :\n\n".translate("Your Friend")." $yname ".translate("considered our site")." $sitename ".translate("interesting and wanted to send it to you.")."\n\n$sitename : $nuke_url\n\n";
   include("signat.php");
   $fmail=removeHack($fmail);
   $subject=removeHack($subject);
   $message=removeHack($message);
   $yname=removeHack($yname);
   $ymail=removeHack($ymail);
   $stop=false;
   if ((!$fmail) || ($fmail=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$fmail))) $stop = true;
   if ((!$ymail) || ($ymail=="") || (!eregi("^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,4}$",$ymail))) $stop = true;
   if (!$stop) {
      send_email($fmail, $subject, $message, $ymail, false,"text");
   } else {
     $fname="";
   }
   Header("Location: friend.php?op=SiteSent&fname=$fname");
}

function SiteSent($fname) {
   include ('header.php');
   opentable();
   echo "<p align=\"center\">";
   if ($fname=="") {
      echo translate("ERROR: Invalid email");
   } else {
      echo translate("The reference to our site has been sent to")." $fname";
      echo "<br /><br />".translate("Thanks for recommend us!");
   }
   echo "</p>";
   closetable();
   include ('footer.php');
}

switch($op) {
   case "FriendSend":
        FriendSend($sid);
        break;

   case "SendStory":
        SendStory($sid, $yname, $ymail, $fname, $fmail, $asb_question, $asb_reponse);
        break;

   case "StorySent":
        StorySent($title, $fname);
        break;

   case "SendSite":
        SendSite($yname, $ymail, $fname, $fmail, $asb_question, $asb_reponse);
        break;

   case "SiteSent":
        SiteSent($fname);
        break;

   default:
        RecommendSite();
        break;
}
?>