<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* Original Copyright (c) 2001 by Francisco Burzi (fburzi@ncc.org.ve)   */
/* http://phpnuke.org                                                   */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2005 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.       */
/************************************************************************/
function Access_Error () {
  if (file_exists("meta/meta.php"))
     include ("meta/meta.php");
  echo "<title>NPDS</title></head><body bgcolor=\"white\"><br /><p align=\"center\"><span style=\"font-size: 14px; font-family: Arial; font-weight: bold; color: red;\">";
  echo "Access Denied / Acc&egrave;s Refus&eacute;";
  if (cur_charset=="utf-8") echo" / &#x901A;&#x5165;&#x88AB;&#x5426;&#x8BA4;";
  echo "</span>";
  echo "<br /><br /><span style=\"font-size: 12px; font-family: Arial; font-weight: bold; color: black;\">NPDS - Portal System";
  echo "</span></p></body></html>";
  die();
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }
global $pdst; $pdst="0";

global $language;
$hlpfile = "manuels/$language/email_user.html";
$result = mysql_query("select radminsuper, radminuser from authors where aid='$aid'");
list($radminsuper, $radminuser) = mysql_fetch_row($result);
if (($radminsuper!=1) and ($radminuser!=1)) {
   Access_Error();
}

function email_user($uname="") {
    global $hlpfile, $NPDS_Prefix;
    include ('header.php');
       GraphicAdmin($hlpfile);
       OpenTable();
       echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
       echo adm_translate("Diffusion d'un Message Interne");
       echo "</td></tr></table>\n";
       echo "<br />\n";
       echo "<form action=\"admin.php\" method=\"post\">";
       echo "<input type=\"hidden\" name=\"op\" value=\"send_email_to_user\">";
       echo "<b>".adm_translate("Utilisateur")." :</b><br />\n";
       echo "<input class=\"TEXTBOX_STANDARD\" type=\"text\" size=\"28\" name=\"username\" value=\"$uname\">&nbsp;-&nbsp;";
       echo adm_translate("Groupe")." : <select class=\"TEXTBOX_STANDARD\" name=\"groupe\" size=\"1\">".
            "<option value=\"0\" selected>".adm_translate("Choisir un groupe");
       $resultID = mysql_query("SELECT groupe_id, groupe_name FROM ".$NPDS_Prefix."groupes ORDER BY groupe_id ASC");
       while (list($groupe_id, $groupe_name)=mysql_fetch_array($resultID)) {
          echo "<option value=\"".$groupe_id."\">".$groupe_id." - ".$groupe_name;
       }
       echo "</select>";
       echo "&nbsp;-&nbsp;<input type=\"checkbox\" name=\"all\" value=\"1\">&nbsp;".adm_translate("Envoyer à tous les membres")."<br /><hr noshade class=\"ONGL\">";

       echo "<b>".adm_translate("Sujet")." :</b><br />\n";
       echo "<input class=\"textbox\" type=\"text\" size=\"28\" name=\"subject\"><br /><br /><b>".adm_translate("Corps de message")." :</b><br />\n";
       echo "<textarea class=\"textbox\" wrap=\"virtual\" cols=\"45\" rows=\"25\" name=\"message\"></textarea><br /><br /><input class=\"BOUTON_STANDARD\" type=\"submit\" value=\"".adm_translate("Envoyer")."\"></form>";
       CloseTable();
    include("footer.php");
}

function send_email_to_user($username, $subject, $message, $all, $groupe) {
    if ($subject!="") {
        global $user;
        $userX = base64_decode($user);
        $userdata = explode(":", $userX);

        if ($all) {
           $result=mysql_query("SELECT uid, user_langue FROM users");
           while (list($to_userid, $user_langue)=mysql_fetch_row($result)) {
              $tab_to_userid[]=$to_userid.":".$user_langue;
           }
        } else {
           if ($groupe) {
              $result = mysql_query("select s.uid, s.groupe, u.user_langue from users_status s, users u where s.uid=u.uid and s.groupe!='' order by s.uid ASC");
              while(list($to_userid, $groupeX, $user_langue) = mysql_fetch_row($result)) {
                 $tab_groupe=explode(",",$groupeX);
                 if ($tab_groupe) {
                    foreach($tab_groupe as $groupevalue) {
                       if ($groupevalue==$groupe) {
                          $tab_to_userid[]=$to_userid.":".$user_langue;
                       }
                    }
                 }
              }
           } else {
              $result = mysql_query("SELECT uid, user_langue FROM users WHERE uname='$username'");
              while (list($to_userid, $user_langue)=mysql_fetch_row($result)) {
                 $tab_to_userid[]=$to_userid.":".$user_langue;
              }
           }
        }
        if (($subject=="") or ($message=="")) {
           header("location: admin.php");
        }
        $message = str_replace("\n","<br />", $message);
        global $gmt;
        $time = date(translate("dateinternal"),time()+($gmt*3600));
        $pasfin=false;
        $count=0;
        include_once("language/lang-multi.php");
        while ($count<sizeof($tab_to_userid)) {
           $to_tmp=explode(":",$tab_to_userid[$count]);
           $to_userid=$to_tmp[0];
           if (($to_userid != "") and ($to_userid != 1)) {
              $sql = "INSERT INTO priv_msgs (msg_image, subject, from_userid, to_userid, msg_time, msg_text) ";
              $sql .= "VALUES ('$image', '$subject', '$userdata[0]', '$to_userid', '$time', '$message')";
              if ($resultX = mysql_query($sql)) {
                 $pasfin=true;
              }
              // A copy in email if necessary
              global $nuke_url, $subscribe;
              if ($subscribe) {
                 $old_message=$message;
                 $sujet=translate_ml($to_tmp[1], "Vous avez un nouveau message.");
                 $message=translate_ml($to_tmp[1], "Bonjour").",<br /><br /><a href=\"$nuke_url/viewpmsg.php\" target=\"_blank\">".translate_ml($to_tmp[1], "Cliquez ici pour lire votre nouveau message.")."</a><br /><br />";
                 include("signat.php");
                 copy_to_email($to_userid,$sujet,nl2br($message));
                 $message=$old_message;
              }
           }
           $count++;
        }
    }
    global $hlpfile;
    include ('header.php');
    GraphicAdmin($hlpfile);
    OpenTable();
    if ($pasfin) {
       echo "<p align=\"center\" class=\"NOIR\"><b>\"".stripslashes($subject)."\"</b> ".adm_translate("a été envoyée").".</p>";
    } else {
       echo "<p align=\"center\" class=\"ROUGE\"><b>\"".stripslashes($subject)."\"</b> ".adm_translate("n'a pas été envoyée").".</p>";
    }
    CloseTable();
    include("footer.php");
}

switch ($op){
    case "send_email_to_user":
         send_email_to_user($username, $subject, $message, $all, $groupe);
          break;

    case "email_user":
         global $user;
         if (!$user) {
            global $hlpfile;
            include ('header.php');
            GraphicAdmin($hlpfile);
            OpenTable();
            echo "<p align=\"center\" class=\"ROUGE\">".adm_translate("Vous devez vous identifier aussi en tant que membre pour utiliser cette fonction.")."</p>";
            CloseTable();
            include("footer.php");
         } else {
            email_user($uname);
         }
         break;
}
?>
