<?PHP
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* 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.       */
/************************************************************************/
if (!function_exists("Mysql_Connexion")) {
   header("location: index.php");
}

global $language;
include("language/pp-lang-$language.php");

#autodoc Form_instant_message($to_userid) : Ouvre la page d'envoi d'un MI (Message Interne)
function Form_instant_message($to_userid) {
    include ("header.php");
    opentable();
    write_short_private_message(removeHack($to_userid));
    closetable();
    include ("footer.php");
}

#autodoc online_members () : liste des membres connect&eacute;s <br /> Retourne un tableau dont la position 0 est le nombre, puis la liste des username | time <br />Appel : $xx=online_members(); puis $xx[x][username] $xx[x][time] ...
function online_members () {
   global $NPDS_Prefix;

   $result = sql_query("SELECT username, guest, time FROM ".$NPDS_Prefix."session where guest=0 order by username ASC");
   $i=0;
   $members_online[$i]=sql_num_rows($result);
   while ($session = sql_fetch_assoc($result)) {
      if (isset($session["guest"]) and $session["guest"] == 0) {
         $i++;
         $members_online[$i][username] = $session[username];
         $members_online[$i][time] = $session[time];
      }
   }
   return $members_online;
}

#autodoc writeDB_private_message($to_userid,$image,$subject,$from_userid,$message, $copie) : Ins&egrave;re un MI dans la base et le cas éch&eacute;ant envoi un mail
function writeDB_private_message($to_userid,$image,$subject,$from_userid,$message, $copie) {
    global $NPDS_Prefix;

    $res = sql_query("select uid, user_langue from ".$NPDS_Prefix."users where uname='$to_userid'");
    list($to_useridx, $user_languex) = sql_fetch_row($res);

    if ($to_useridx == "") {
       forumerror('0016');
    } else {
       global $gmt;
       $time = date(translate("dateinternal"),time()+($gmt*3600));
       include_once("language/lang-multi.php");
       $subject=removeHack($subject);
       $message=str_replace("\n","<br />", $message);
       $message=addslashes(removeHack($message));
       $sql = "INSERT INTO ".$NPDS_Prefix."priv_msgs (msg_image, subject, from_userid, to_userid, msg_time, msg_text) ";
       $sql .= "VALUES ('$image', '$subject', '$from_userid', '$to_useridx', '$time', '$message')";
       if (!$result = sql_query($sql)) {
          forumerror('0020');
       }
       if ($copie) {
          $sql = "INSERT INTO ".$NPDS_Prefix."priv_msgs (msg_image, subject, from_userid, to_userid, msg_time, msg_text, type_msg, read_msg) ";
          $sql .= "VALUES ('$image', '$subject', '$from_userid', '$to_useridx', '$time', '$message', '1', '1')";
          if (!$result = sql_query($sql)) {
             forumerror('0020');
          }
       }
       global $subscribe, $nuke_url;
       if ($subscribe) {
          $sujet=translate_ml($user_languex, "Vous avez un nouveau message.");
          $message=translate_ml($user_languex, "Bonjour").",<br /><br /><a href=\"$nuke_url/viewpmsg.php\" target=\"_blank\">".translate_ml($user_languex, "Cliquez ici pour lire votre nouveau message.")."</a><br /><br />";
          include("signat.php");
          copy_to_email($to_useridx,$sujet,nl2br($message));
       }
    }
}

#autodoc write_short_private_message($to_userid) : Formulaire d'&eacute;criture d'un MI
function write_short_private_message($to_userid) {
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"header\">\n";
   echo pp_translate("Message to Member");
   echo "</td></tr></table>\n";
   echo "<br />";
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\">";
   echo "<form action=\"powerpack.php\" method=\"post\">";
   $rowcolor=tablos();
   $rowcolor=tablos();
   echo "<tr $rowcolor>";
   echo "<td><b>".translate("To: ")."<b></td>";
   echo "<td>$to_userid";
   echo " - <input type=\"checkbox\" name=\"copie\">".translate("Send a copy to me")."</td>";
   echo "<input type=\"hidden\" name=\"to_userid\" value=\"$to_userid\">";
   echo "</tr>";

   echo "<tr $rowcolor>";
   echo "<td><b>".translate("Subject: ")."<b></td>";
   echo "<td><input class=\"textbox\" type=\"text\" name=\"subject\" value=\"\" size=\"45\" maxlength=\"100\"></td>";
   echo "</tr>";

   echo "<tr $rowcolor>";
   echo "<td><b>".translate("Message: ")."</b></td>";
   echo "<td><textarea class=\"textbox\" name=\"message\" rows=\"10\" cols=\"70\" wrap=\"virtual\"></textarea></td>";
   echo "</tr>";

   echo "<tr><td></td>";
   echo "<input type=\"hidden\" name=\"op\" value=\"write_instant_message\">";
   echo "<td><input class=\"bouton_standard\" type=\"submit\" name=\"submit\" value=\"".translate("Submit")."\" accesskey=\"s\">&nbsp;<input class=\"bouton_standard\" type=\"reset\" value=\"".translate("Clear")."\"></td>";
   echo "</tr>";
   echo "</form>";
   echo "</table>";
}

#autodoc if_chat() : Retourne le nombre de connect&eacute; au Chat
function if_chat() {
   global $NPDS_Prefix;

   $result = sql_query("SELECT DISTINCT ip ".$NPDS_Prefix."FROM chatbox WHERE date >= ".(time()-(60*3))."");
   $numofchatters = sql_num_rows($result);
   return ($numofchatters);
}

#autodoc insertChat($username, $message, $dbname) : Ins&egrave;re un record dans la table Chat
function insertChat($username, $message, $dbname) {
   global $NPDS_Prefix;

   $username = removeHack(stripslashes(FixQuotes(strip_tags(trim($username)))));
   $message =  removeHack(stripslashes(FixQuotes(strip_tags(trim($message)))));
   $ip = getip();
   if ($dbname == 0) {
      $rowQ1 = Q_select("SELECT uname FROM ".$NPDS_Prefix."users WHERE uname = \"".trim($username)."\"", 3600);
      list(,$thing) = each($rowQ1);
      if (isset($thing["username"])) {
         $username = "";
      }
   }
   if ($message!="") {
      $result = sql_query("INSERT INTO ".$NPDS_Prefix."chatbox VALUES ('".$username."', '".$ip."', '".$message."', '".time()."', '', ".$dbname.")");
   }
}


#autodoc JavaPopUp($F,$T,$W,$H) : Personnalise une ouverture de fen&egrave;tre (popup)
function JavaPopUp($F,$T,$W,$H) {
   // 01.feb.2002 by GaWax
   if ($T=="") $T="@ ".time()." ";
   $PopUp = "'$F','$T','menubar=no,location=no,directories=no,status=no,copyhistory=no,height=$H,width=$W,toolbar=no,scrollbars=yes,resizable=yes'";
   return $PopUp;
}

#autodoc:<Powerpack_f.php>
#autodoc:
#autodoc <font color=green>BLOCS NPDS</font>:
#autodoc instant_members_message() : Bloc MI (Message Interne) <br />=> syntaxe : function#instant_members_message
function instant_members_message() {
   global $user, $admin, $long_chain;
   global $NPDS_Prefix;

   if (!$long_chain) {$long_chain=13;}

   global $block_title;
   if ($block_title=="")
      $block_title=pp_translate("M2M box");

   if ($user) {
      global $cookie;
      $boxstuff="<table cellspacing=\"1\" cellpadding=\"0\" border=\"0\">";
      $ibid=online_members();
      $rank1="";
      for ($i = 1; $i <= $ibid[0]; $i++) {
          $timex=time()-$ibid[$i][time];
          if ($timex>=60) {
             if (!$imgtmp=theme_image("powerpack/user1.gif")) { $imgtmp="images/powerpack/user1.gif"; }
             $timex="<img src=\"$imgtmp\" align=\"center\" border=\"0\" alt=\"\" />&nbsp;";
          } else {
             if (!$imgtmp=theme_image("powerpack/user2.gif")) { $imgtmp="images/powerpack/user2.gif"; }
             $timex="<img src=\"$imgtmp\" align=\"center\" border=\"0\" alt=\"\" />&nbsp;";
          }
          global $member_invisible;
          if ($member_invisible) {
             if ($admin)
                $and="";
             else {
                if ($ibid[$i][username]==$cookie[1])
                   $and="";
                else
                   $and="and is_visible=1";
             }
          } else {
             $and="";
          }
          $result=sql_query("SELECT uid FROM ".$NPDS_Prefix."users WHERE uname='".$ibid[$i][username]."' $and");
          list($userid)=sql_fetch_row($result);
          if ($userid) {
             $rowQ1=Q_Select("SELECT rank FROM ".$NPDS_Prefix."users_status WHERE uid='$userid'", 3600);
             list(,$myrow) = each($rowQ1);
             $rank=$myrow["rank"];
             $tmpR="";
             if ($rank) {
                if ($rank1=="") {
                   if ($rowQ2 = Q_Select("SELECT rank1, rank2, rank3, rank4, rank5 FROM ".$NPDS_Prefix."config",86400)) {
                      list(,$myrow) = each($rowQ2);
                      $rank1 = $myrow["rank1"];
                      $rank2 = $myrow["rank2"];
                      $rank3 = $myrow["rank3"];
                      $rank4 = $myrow["rank4"];
                      $rank5 = $myrow["rank5"];
                   }
                }
                if ($ibidR=theme_image("forum/rank/".$rank.".gif")) {$imgtmpA=$ibidR;} else {$imgtmpA="images/forum/rank/".$rank.".gif";}
                $messR="rank".$rank;
                $tmpR="<img src=\"".$imgtmpA."\" border=\"0\" alt=\"".aff_langue($$messR)."\" title=\"".aff_langue($$messR)."\" />";
             } else {
                $tmpR="&nbsp;";
             }
             $new_messages = sql_num_rows(sql_query("SELECT msg_id FROM ".$NPDS_Prefix."priv_msgs WHERE to_userid = '$userid' AND read_msg='0' AND type_msg='0'"));
             if ($new_messages>0) {
                $PopUp=JavaPopUp("readpmsg_imm.php?op=new_msg","IMM",600,500);
                $PopUp="<a href=\"javascript:void(0);\" onClick=\"window.open($PopUp);\">";
                if ($ibid[$i][username]==$cookie[1]) {$icon=$PopUp;} else {$icon="";}
                if (!$imgtmp=theme_image("powerpack/newmail.gif")) { $imgtmp="images/powerpack/newmail.gif"; }
                $icon.="<img src=\"$imgtmp\" border=\"0\" alt=\"\" />";
                if ($ibid[$i][username]==$cookie[1]) {$icon.="</a>";}
             } else {
                $messages = sql_num_rows(sql_query("SELECT msg_id FROM ".$NPDS_Prefix."priv_msgs WHERE to_userid = '$userid' AND type_msg='0' and dossier='...'"));
                if ($messages>0) {
                   $PopUp=JavaPopUp("readpmsg_imm.php?op=msg","IMM",600,500);
                   $PopUp="<a href=\"javascript:void(0);\" onClick=\"window.open($PopUp);\">";
                   if ($ibid[$i][username]==$cookie[1]) {$icon=$PopUp;} else {$icon="";}
                   if (!$imgtmp=theme_image("powerpack/oldmail.gif")) { $imgtmp="images/powerpack/oldmail.gif"; }
                   $icon.="<img src=\"$imgtmp\" border=\"0\" alt=\"\" /></a>";
                } else {
                   $icon="&nbsp;";
                }
             }
             $N=$ibid[$i][username];
             if (strlen($N)>$long_chain)
                $M=substr($N,0,$long_chain).".";
             else
                $M=$N;
             $boxstuff .="<tr width=\"100%\"><td valign=\"top\">$timex&nbsp;<a href=\"powerpack.php?op=instant_message&amp;to_userid=$N\">$M</a></td><td width=\"5%\" nowrap=\"nowrap\" align=\"right\">$icon</td><td width=\"5%\" nowrap=\"nowrap\" align=\"right\">$tmpR</td></tr>";
          }
      }
      $boxstuff .="</table>";
      themesidebox($block_title, $boxstuff);
   } else {
      if ($admin) {
         $boxstuff="<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\">";
         $ibid=online_members();
         if ($ibid[0]) {
            for ($i = 1; $i <= $ibid[0]; $i++) {
               $N=$ibid[$i][username];
               if (strlen($N)>$long_chain)
                  $M=substr($N,0,$long_chain).".";
               else
                  $M=$N;
               $boxstuff .="<tr width=\"100%\"><td valign=\"top\" class=\"titboxcont\">$M</td></tr>";
            }
            $boxstuff .="</table>";
            themesidebox("<i>".$block_title."</i>", $boxstuff);
         }
      }
   }
}

#autodoc makeChatBox() : Bloc ChatBox <br />=> syntaxe : function#makeChatBox
function makeChatBox() {
   global $user, $admin, $member_list, $long_chain;
   global $NPDS_Prefix;

   if (!$long_chain) {$long_chain=12;}

   $thing=""; $une_ligne=false;
   $counter=sql_num_rows(sql_query("SELECT message FROM ".$NPDS_Prefix."chatbox"))-6;
   if ($counter<0) $counter=0;
   $result=sql_query("SELECT username, message, dbname FROM ".$NPDS_Prefix."chatbox ORDER BY date ASC LIMIT $counter,6");
   if ($result) {
      while (list($username, $message, $dbname, $date) = sql_fetch_row($result)) {
         if (isset($username)) {
            if ($dbname==1) {
               if ((!$user) and ($member_list==1) and (!$admin)) {
                  $thing.="<span class=\"titrec\" style=\"font-size: 10px;\">".substr($username,0,8).".</span>";
               } else {
                  $thing.="<a href=\"user.php?op=userinfo&amp;uname=$username\">".substr($username,0,8).".</a>";
               }
            } else {
              $thing.="<span class=\"titrec\" style=\"font-size: 10px;\">".substr($username,0,8).".</span>";
            }
         }
         $une_ligne=true;
         if (strlen($message)>$long_chain) {
            $thing.="&gt;<span style=\"font-size: 10px;\">".stripslashes(substr($message,0,$long_chain))."</span><br />\n";
         } else {
           $thing.="&gt;<span style=\"font-size: 10px;\">".stripslashes($message)."</span><br />\n";
         }
      }
   }
   $PopUp = JavaPopUp("chat.php","chat",380,480);
   if ($une_ligne) {$thing.="<hr size=\"2\" width=\"90%\" class=\"boxmenu\">";} else {$thing.="<br />";}
   $thing.="<p align=\"center\"><a href=\"javascript:void(0);\" onClick=\"window.open($PopUp);\">".pp_translate("click here to open the chat window...")."</a></p>";

   $result=sql_query("SELECT DISTINCT ip FROM ".$NPDS_Prefix."chatbox WHERE date >= ".(time()-(60*2))."");
   $numofchatters = sql_num_rows($result);

   $thing .= "<br />";
   if ($numofchatters==1) {
      $thing.="<span style=\"font-size: 10px;\">".pp_translate("There is")." <span class=\"titrec\">1</span> ".pp_translate("person chatting right now.")."</span>";
   } else {
      $thing.="<span style=\"font-size: 10px;\">".pp_translate("There are")." <span class=\"titrec\">".$numofchatters."</span> ".pp_translate("people chatting right now.")."</span>";
   }
   global $block_title;
   if ($block_title=="")
      $block_title=pp_translate("Chat box");
   themesidebox($block_title, $thing);
   sql_free_result($result);
}

#autodoc RecentForumPosts($title, $maxforums, $maxtopics, $dposter, $topicmaxchars,$hr, $deco) : Bloc Forums <br />=> syntaxe :
#autodoc : function#RecentForumPosts<br />params#titre, nb_max_forum (O=tous), nb_max_topic, affiche_l'emetteur(true / false), topic_nb_max_char, affiche_HR(true / false), decoration_de_la_liste
function RecentForumPosts($title, $maxforums, $maxtopics, $displayposter=false, $topicmaxchars=15,$hr=false, $decoration="") {
    $boxstuff=RecentForumPosts_fab($title, $maxforums, $maxtopics, $displayposter, $topicmaxchars, $hr, $decoration,true);
    global $block_title;
    if ($title=="") {
       if ($block_title=="")
          $title=pp_translate("Forums infos");
       else
          $title=$block_title;
    }
    themesidebox($title, $boxstuff);
}
function RecentForumPosts_fab($title, $maxforums, $maxtopics, $displayposter, $topicmaxchars, $hr, $decoration, $tableau) {
    global $parse, $user;
    global $NPDS_Prefix;

    $topics = 0;
    settype($maxforums,"integer");
    if ($maxforums==0)
       $lim="";
    else
       $lim=" limit $maxforums";

    if ($user) {
       $query = "Select * from ".$NPDS_Prefix."forums ORDER BY cat_id,forum_index,forum_id".$lim;
    } else {
       $query = "Select * from ".$NPDS_Prefix."forums where forum_type!='9' and forum_type!='7' and forum_type!='5' ORDER BY cat_id,forum_index,forum_id".$lim;
    }
    $result = sql_query($query);

    if (!$result) exit();

    $premier=false;
    if ($tableau)
       $boxstuff = "<table>";
    else
       $boxstuff = "";

    while ($row = sql_fetch_row($result)) {
       if (($row[6] == "5") or ($row[6] == "7")) {
          $ok_affich=false;
          $tab_groupe=valid_group($user);
          $ok_affich=groupe_forum($row[7], $tab_groupe);
       } else {
          $ok_affich=true;
       }
       if ($ok_affich) {
          $forumid = $row[0];
          $forumname = $row[1];
          $forum_desc =$row[2];
          if ($tableau)
             $boxstuff .= "<tr><td colspan=\"2\" class=\"titrec\">";
          if ($premier) {
             if ($hr) {
                if ($tableau)
                   $boxstuff .= "<hr class=\"titboxcont\">";
             }
          } else {
             $premier=true;
          }
          if ($parse==0) {
             $forumname =  FixQuotes($forumname);
             $forum_desc =  FixQuotes($forum_desc);
          } else {
             $forumname =  stripslashes($forumname);
             $forum_desc =  stripslashes($forum_desc);
          }

          $res = sql_query("Select * from ".$NPDS_Prefix."forumtopics where forum_id = '$forumid' order by topic_time desc");
          $ibidx=sql_num_rows($res);
          $boxstuff .= $decoration."<a href=\"viewforum.php?forum=$forumid\" alt=\"".strip_tags($forum_desc)."\" title=\"".strip_tags($forum_desc)."\">$forumname</a> ($ibidx)";
          if ($tableau)
             $boxstuff .="</td></tr>";
          $topics = 0;
          while(($topics < $maxtopics) && ($topicrow = sql_fetch_row($res))) {
              $topicid = $topicrow[0];
              $tt = $topictitle = $topicrow[1];
              $date = $topicrow[3];
              $replies = 0;
              $postquery = "Select count(*) as total from ".$NPDS_Prefix."posts where topic_id = '$topicid'";
              if ($pres = sql_query($postquery)) {
                 if ($myrow = sql_fetch_assoc($pres))
                    $replies = $myrow[total];
              }
              if (strlen($topictitle) > $topicmaxchars) {
                 $topictitle = substr($topictitle,0,$topicmaxchars);
                 $topictitle .= "..";
              }

              if ($displayposter) {
                 $posterid = $topicrow[2];
                 $RowQ1=Q_Select ("select uname from ".$NPDS_Prefix."users where uid = '$posterid'",3600);
                 list(,$myrow) = each($rowQ1);
                 $postername = $myrow[uname];
              }
              if ($parse==0) {
                 $tt =  strip_tags(FixQuotes($tt));
                 $topictitle= FixQuotes($topictitle);
              } else {
                 $tt =  strip_tags(stripslashes($tt));
                 $topictitle= stripslashes($topictitle);
              }
              if ($tableau)
                 $boxstuff .= "<tr><td>&nbsp;</td><td class=\"titboxcont\">";
              $boxstuff .= "<a href=\"viewtopic.php?topic=$topicid&amp;forum=$forumid\" alt=\"$tt\" title=\"$tt\">\"$topictitle\"</a> ($replies)";
              if ($decoration=="<li>")
                 $boxstuff .= "</li>";
              if ($displayposter) $boxstuff .="<br />&nbsp;&nbsp;- $postername";
              if ($tableau)
                 $boxstuff .= "</td></tr>";
              $topics++;
          }
       }
    }
    if ($tableau)
       $boxstuff .= "</table>";
    return ($boxstuff);
}
#autodoc:</Powerpack_f.php>
?>