<?PHP
/************************************************************************/
/* NPDS V : Net Portal Dynamic System .                                 */
/* ===========================                                          */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-2004 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) {
    global $pdst; $pdst="1";

    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 () {
   $result = mysql_query("SELECT username, guest, time FROM session where guest=0 order by username ASC");
   $i=0;
   $members_online[$i]=mysql_num_rows($result);
   while ($session = mysql_fetch_array($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) {
    $res = mysql_query("select uid, user_langue from users where uname='$to_userid'");
    list($to_useridx, $user_languex) = mysql_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 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 = mysql_query($sql)) {
          forumerror('0020');
       }
       if ($copie) {
          $sql = "INSERT INTO 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 = mysql_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")."\">&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() {
   $result = mysql_query("SELECT DISTINCT ip FROM chatbox WHERE date >= ".(time()-(60*3))."");
   $numofchatters = mysql_num_rows($result);
   return ($numofchatters);
}

#autodoc insertChat($username, $message, $dbname) : Ins&egrave;re un record dans la table Chat
function insertChat($username, $message, $dbname) {
   $username = removeHack(stripslashes(FixQuotes(strip_tags(trim($username)))));
   $message =  removeHack(stripslashes(FixQuotes(strip_tags(trim($message)))));
   $ip = getip();
   if ($dbname == 0) {
      $result = mysql_query("SELECT uname FROM users WHERE uname = \"".trim($username)."\"");
      $thing = mysql_fetch_array($result);
      if (isset($thing["username"])) {
         $username = "";
      }
   }
   if ($message!="") {
      $result = mysql_query("INSERT INTO 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;
   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=\"middle\" border=\"0\" alt=\"\" />&nbsp;";
          } else {
             if (!$imgtmp=theme_image("powerpack/user2.gif")) { $imgtmp="images/powerpack/user2.gif"; }
             $timex="<img src=\"$imgtmp\" align=\"middle\" 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=mysql_query("SELECT uid FROM users WHERE uname='".$ibid[$i][username]."' $and");
          list($userid)=mysql_fetch_row($result);
          if ($userid) {
             $resultR=mysql_query("SELECT rank FROM users_status WHERE uid='$userid'");
             list($rank)=mysql_fetch_row($resultR);
             $tmpR="";
             if ($rank) {
                if ($rank1=="") {
                   if ($result = mysql_query("SELECT rank1, rank2, rank3, rank4, rank5 FROM config")) {
                      if ($myrow = mysql_fetch_array($result)) {
                         $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 = mysql_num_rows(mysql_query("SELECT msg_id FROM 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 = mysql_num_rows(mysql_query("SELECT msg_id FROM 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;
   if (!$long_chain) {$long_chain=12;}

   if ($user) {
      global $cookie;
      $res=mysql_query("select uid from users where uname='$cookie[1]'");
   }
   $thing=""; $une_ligne=false;
   $counter=mysql_num_rows(mysql_query("SELECT message FROM chatbox"))-6;
   if ($counter<0) $counter=0;
   $result=mysql_query("SELECT username, message, dbname FROM chatbox ORDER BY date ASC LIMIT $counter,6");
   if ($result) {
      while (list($username, $message, $dbname, $date) = mysql_fetch_array($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=mysql_query("SELECT DISTINCT ip FROM chatbox WHERE date >= ".(time()-(60*2))."");
   $numofchatters = mysql_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);
   mysql_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;

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

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

    if (!$result) exit();

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

    while ($row = mysql_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);
          }

          $query = sprintf("Select * from forumtopics where forum_id = %s order by topic_time desc",$forumid);
          $res = mysql_query($query);
          $ibidx=mysql_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 = mysql_fetch_row($res))) {
              $topicid = $topicrow[0];
              $tt = $topictitle = $topicrow[1];
              $date = $topicrow[3];
              $replies = 0;
              $postquery = "Select count(*) as total from posts where topic_id = '$topicid'";
              if ($pres = mysql_query($postquery)) {
                 if ($myrow = mysql_fetch_array($pres))
                    $replies = $myrow[total];
              }
              if (strlen($topictitle) > $topicmaxchars) {
                 $topictitle = substr($topictitle,0,$topicmaxchars);
                 $topictitle .= "..";
              }

              if ($displayposter) {
                 $posterid = $topicrow[2];
                 $q = "select uname from users where uid = '$posterid'";
                 if ($pos = mysql_query($q)) {
                    if ($posrow = mysql_fetch_array($pos))
                       $postername = $posrow[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>
?>