<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* NPDS Copyright (c) 2002-2007 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 () {
  include("admin/die.php");
}
if (!eregi("admin.php", $PHP_SELF)) { Access_Error(); }

global $language;
$hlpfile = "manuels/$language/email_user.html";
$result = sql_query("select radminsuper, radminuser from ".$NPDS_Prefix."authors where aid='$aid'");
list($radminsuper, $radminuser) = sql_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 = sql_query("SELECT groupe_id, groupe_name FROM ".$NPDS_Prefix."groupes ORDER BY groupe_id ASC");
       while (list($groupe_id, $groupe_name)=sql_fetch_row($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) {
    global $NPDS_Prefix;

    if ($subject!="") {
        global $user;
        $userX = base64_decode($user);
        $userdata = explode(":", $userX);

        if ($all) {
           $result=sql_query("SELECT uid, user_langue FROM ".$NPDS_Prefix."users");
           while (list($to_userid, $user_langue)=sql_fetch_row($result)) {
              $tab_to_userid[]=$to_userid.":".$user_langue;
           }
        } else {
           if ($groupe) {
              $result = sql_query("select s.uid, s.groupe, u.user_langue from ".$NPDS_Prefix."users_status s, ".$NPDS_Prefix."users u where s.uid=u.uid and s.groupe!='' order by s.uid ASC");
              while(list($to_userid, $groupeX, $user_langue) = sql_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 = sql_query("SELECT uid, user_langue FROM ".$NPDS_Prefix."users WHERE uname='$username'");
              while (list($to_userid, $user_langue)=sql_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 ".$NPDS_Prefix."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 = sql_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;
}
?>