<?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");
}

function FriendSend($sid, $archive) {
   global $NPDS_Prefix;

   settype($sid,"integer");
   settype($archive, "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 "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n";
   echo "<tr><td>".translate("Your Name: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"yname\" value=\"$yn\" size=\"30\" maxlength=\"100\" /></td></tr>
   <tr><td>".translate("Your Email: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"ymail\" value=\"$ye\" size=\"30\" maxlength=\"100\" /></td></tr>
   <tr><td>".translate("Friend Name: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"fname\" size=\"25\" maxlength=\"100\" /></td></tr>
   <tr><td>".translate("Friend Email: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"fmail\" size=\"25\" maxlength=\"100\" /></td></tr>";
   echo "</table>".Q_spambot();
   echo "<input type=\"hidden\" name=\"archive\" value=\"$archive\" />";
   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, $archive, $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");
   settype($archive, "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")." : <a href=\"$nuke_url/article.php?sid=$sid&amp;archive=$archive\">$nuke_url/article.php?sid=$sid&amp;archive=$archive</a>\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=="") || (!preg_match('#^[_\.0-9a-z-]+@[0-9a-z-\.]+\.+[a-z]{2,4}$#i',$fmail))) $stop = true;
   if ((!$ymail) || ($ymail=="") || (!preg_match('#^[_\.0-9a-z-]+@[0-9a-z-\.]+\.+[a-z]{2,4}$#i',$ymail))) $stop = true;
   if (!$stop) {
      send_email($fmail, $subject, $message, $ymail, false,"html");
   } 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);
   } else {
      $yn="";
      $ye="";
   }
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">\n";
   echo "<tr><td>".translate("Your Name: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"yname\" value=\"$yn\" size=\"40\" maxlength=\"100\" /></tr>
   <tr><td>".translate("Your Email: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"ymail\" value=\"$ye\" size=\"40\" maxlength=\"100\" /></td></tr>
   <tr><td>".translate("Friend Name: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"fname\" size=\"40\" maxlength=\"100\" /></td></tr>
   <tr><td>".translate("Friend Email: ")."</td><td><input class=\"textbox\" type=\"text\" name=\"fmail\" size=\"40\" maxlength=\"100\" /></td></tr>";
   echo "</table>".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 : <a href=\"$nuke_url\">$nuke_url</a>\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=="") || (!preg_match('#^[_\.0-9a-z-]+@[0-9a-z-\.]+\.+[a-z]{2,4}$#i',$fmail))) $stop = true;
   if ((!$ymail) || ($ymail=="") || (!preg_match('#^[_\.0-9a-z-]+@[0-9a-z-\.]+\.+[a-z]{2,4}$#i',$ymail))) $stop = true;
   if (!$stop) {
      send_email($fmail, $subject, $message, $ymail, false,"html");
   } 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');
}

settype($op,'string');
switch ($op) {
   case "FriendSend":
        FriendSend($sid, $archive);
        break;

   case "SendStory":
        SendStory($sid, $yname, $ymail, $fname, $fmail, $archive, $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;
}
?>