<?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-2004 by Philippe Brunier   */
/*                                                                      */
/* =========================                                            */
/* Based on Parts of phpBB                                              */
/*                                                                      */
/* 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");}
include('functions.php');
if ($SuperCache) {
   $cache_obj = new cacheManager();
}
include('auth.php');
global $pdst; $pdst="0";

if (($op=="mark") and ($forum)) {
   if ($user) {
      $userX = base64_decode($user);
      $userR = explode(":", $userX);
      $resultT=mysql_query("select topic_id from forumtopics where forum_id='$forum' order by topic_id ASC");
      $time_actu = time()+($gmt*3600);
      while (list($topic_id)=mysql_fetch_array($resultT)) {
         $r=mysql_query("SELECT rid from forum_read where forum_id='$forum' and uid='$userR[0]' and topicid='$topic_id'");
         if ($r) {
            if (!list($rid)=mysql_fetch_array($r)) {
               $r=mysql_query("INSERT INTO forum_read (forum_id, topicid, uid, last_read, status) VALUES ('$forum', '$topic_id', '$userR[0]', $time_actu, '1')");
            } else {
               $r=mysql_query("UPDATE forum_read SET last_read='$time_actu', status='1' WHERE rid='$rid'");
            }
         }
      }
      header("location: forum.php");
   }
}

$rowQ1=Q_Select ("SELECT forum_name, forum_moderator, forum_type, forum_pass, forum_access, arbre FROM forums WHERE forum_id = '$forum'", 3600);
if (!$rowQ1)
   forumerror('0002');
list(,$myrow) = each($rowQ1);
$forum_name = stripslashes($myrow[forum_name]);
$moderator=get_moderator($myrow[forum_moderator]);
$forum_access=$myrow[forum_access];

if (($op=="solved") and ($topic_id) and ($forum) and ($sec_clef)) {
   if ($user) {
      $local_sec_clef=md5($forum.$topic_id.md5($NPDS_Key));
      if ($local_sec_clef==$sec_clef) {
         $sqlS = "UPDATE forumtopics SET topic_status='2', topic_title='[".translate("Solved")."] - ".removehack($topic_title)."' WHERE topic_id='$topic_id'";
         if (!$r = mysql_query($sqlS))
            forumerror('0011');
      }
      unset($local_sec_clef);
   }
   unset($sec_clef);
}

// Pour les forums de type Groupe, le Mot de Passe stock l'ID du groupe ...
// Pour les forums de type Extended Text, le Mot de Passe stock le nom du fichier de formulaire ...
if (($myrow[forum_type] == 5) or ($myrow[forum_type] == 7)) {
   $ok_affiche=false;
   $tab_groupe=valid_group($user);
   $ok_affiche=groupe_forum($myrow[forum_pass], $tab_groupe);
   if ($ok_affiche) {$Forum_passwd=$myrow[forum_pass];}
}
if ($myrow[forum_type] == 8) {$Forum_passwd=$myrow[forum_pass];}

// Forum ARBRE
if ($myrow[arbre])
   $hrefX="viewtopicH.php";
else
   $hrefX="viewtopic.php";

if ( ($myrow[forum_type] == 1) and ( ($myrow[forum_name] != $forum_name) or ($Forum_passwd != $myrow[forum_pass])) ) {
    include('header.php');
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\" colspan=\"2\">\n";
    echo translate("Moderated By: ");
    $moderator_data=explode(" ",$moderator);
    for ($i = 0; $i < count($moderator_data)+1; $i++) {
       echo "<a href=\"user.php?op=userinfo&amp;uname=$moderator_data[$i]\" class=\"BOX\">$moderator_data[$i]</a>&nbsp;";
    }
    echo "</td></tr><tr><td><a href=\"forum.php\" class=\"NOIR\">".translate("Forum Index")."</a> » » ".stripslashes($forum_name);
    echo "</td></tr></table><br />\n";

    echo "<form action=\"viewforum.php\" method=\"post\">";
    echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\" width=\"100%\" class=\"HEADER\">";
    echo "<tr>";
    echo "<td class=\"ONGL\" align=\"center\">".translate("This is a Private Forum. Please enter the password to gain access")."</td>";
    echo "</tr><tr>";
    echo "<td align=\"center\"><input class=\"BOUTON_STANDARD\" type=\"password\" name=\"Forum_passwd\" size=\"25\" maxlength=\"30\"></td>";
    echo "</tr><tr>";
    echo "<td align=\"center\">";
    echo "<input type=\"hidden\" name=\"forum\" value=\"$forum\">";
    echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"submit\" value=\"".translate("Submit")."\">&nbsp;&nbsp;";
    echo "<input type=\"hidden\" name=\"bypass\" value=\"1\">";
    echo "<input class=\"BOUTON_STANDARD\" type=\"reset\" value=\"".translate("Clear")."\">";
    echo "</td></tr></table></form>";
} elseif ( ($Forum_passwd == $myrow[forum_pass]) ) {
   if (($myrow[forum_type]== 9) and (!$user)) { header("location: forum.php"); }
   $title=$forum_name;
   include('header.php');

   if ($user) {
      $userX = base64_decode($user);
      $userR = explode(":", $userX);
   }
   echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\" colspan=\"2\">\n";
   echo translate("Moderated By: ");
   $Mmod=false;
   $moderator_data=explode(" ",$moderator);
   for ($i = 0; $i < count($moderator_data); $i++) {
      if (($userR[1]==$moderator_data[$i])) {$Mmod=true;}
      echo "<a href=\"user.php?op=userinfo&amp;uname=$moderator_data[$i]\" class=\"BOX\">$moderator_data[$i]</a>&nbsp;";
   }
   echo "</td></tr><tr><td><a href=\"forum.php\" class=\"NOIR\">".translate("Forum Index")."</a> » » ".stripslashes($forum_name);
   echo "</td><td align=\"right\">";
   if ($forum_access!=9) {
      $allow_to_post = true;
      if ($forum_access==2)
         if (!user_is_moderator($userR[0],$userR[2],$forum_access)) {$allow_to_post = false;}
      if ($allow_to_post) {
         echo "<a href=\"newtopic.php?forum=$forum\">";
         if ($ibid=theme_image("forum/icons/$language/new_topic.gif")) {$imgtmp=$ibid;} else {$imgtmp="images/forum/icons/$language/new_topic.gif";}
         echo "<img src=\"$imgtmp\" border=\"0\" alt=\"\" />";
         echo "</a>";
      }
   }
   echo "</td></tr></table>";
   echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"1\" width=\"100%\">";
   echo "<tr>";
   echo "<td class=\"HEADER\" width=\"2%\">&nbsp;</td>";
   echo "<td class=\"HEADER\" width=\"2%\">&nbsp;</td>";
   if ($solved) {
      if ($closoled=="on") {
         $closol="and topic_status='2'";
         $mess_closoled="[ <a href=\"viewforum.php?forum=$forum\" class=\"BOX\">".translate("Without")." ".translate("Solved")."</a> ]";
      } else {
         $closol="and topic_status!='2'";
         $mess_closoled="[ <a href=\"viewforum.php?forum=$forum&amp;closoled=on\" class=\"BOX\">".translate("Only")." ".translate("Solved")."</a> ]";
      }
   } else {
      $closol="";
      $mess_closoled="";
   }
   echo "<td class=\"HEADER\" width=\"55%\">&nbsp;".translate("Topic")."&nbsp;&nbsp;$mess_closoled</td>";
   echo "<td class=\"HEADER\" width=\"8%\" align=\"center\">".translate("Replies")."</td>";
   echo "<td class=\"HEADER\" width=\"8%\" align=\"center\">".translate("Poster")."</td>";
   echo "<td class=\"HEADER\" width=\"8%\" align=\"center\">".translate("Views")."</td>";
   echo "<td class=\"HEADER\" width=\"15%\" align=\"center\">".translate("Date")."</td>";
   echo "</tr>";
   if (!$start) $start = 0;
   settype($start,"integer");
   settype($topics_per_page,"integer");
   $sql = "SELECT * FROM forumtopics WHERE forum_id='$forum' $closol ORDER BY topic_first,topic_time DESC LIMIT $start, $topics_per_page";
   if (!$result = mysql_query($sql))
        forumerror('0004');

   if ($ibid=theme_image("forum/icons/hot_red_folder.gif")) {$imgtmpHR=$ibid;} else {$imgtmpHR="images/forum/icons/hot_red_folder.gif";}
   if ($ibid=theme_image("forum/icons/hot_folder.gif")) {$imgtmpH=$ibid;} else {$imgtmpH="images/forum/icons/hot_folder.gif";}
   if ($ibid=theme_image("forum/icons/red_folder.gif")) {$imgtmpR=$ibid;} else {$imgtmpR="images/forum/icons/red_folder.gif";}
   if ($ibid=theme_image("forum/icons/folder.gif")) {$imgtmpF=$ibid;} else {$imgtmpF="images/forum/icons/folder.gif";}
   if ($ibid=theme_image("forum/icons/lock.gif")) {$imgtmpL=$ibid;} else {$imgtmpL="images/forum/icons/lock.gif";}
   if ($ibid=theme_image("forum/icons/posticon.gif")) {$imgtmpP=$ibid;} else {$imgtmpP="images/forum/icons/posticon.gif";}
   if ($ibid=theme_image("box/right.gif")) {$imgtmpRi=$ibid;} else {$imgtmpRi="images/download/right.gif";}

   if ($myrow = mysql_fetch_array($result)) {
      $linked = "onMouseOver=\"this.className='LIGNB'; this.style.cursor='hand';\" "
      ." onMouseOut=\"this.className='LIGNA';\"";
      do {
         echo"<tr>";
         $replys = get_total_posts($forum, $myrow[topic_id], "topic", $Mmod);
         $replys--;
         if ($replys>=0) {
            global $smilies;
            if ($smilies) {
               $rowQ1=Q_Select ("SELECT image FROM posts WHERE topic_id = '$myrow[topic_id]'", 3600);
               list(,$imageT) = each($rowQ1);
               $image_subject=$imageT["image"];
            }

            $sqlR = "select rid from forum_read where forum_id='$forum' and uid='$userR[0]' and topicid='$myrow[topic_id]' and status!='0'";
            if ($replys >= $hot_threshold) {
               if (mysql_num_rows(mysql_query($sqlR))==0)
                 $image = $imgtmpHR;
               else
                 $image = $imgtmpH;
            } else {
                if (mysql_num_rows(mysql_query($sqlR))==0)
                   $image = $imgtmpR;
               else
                  $image = $imgtmpF;
            }

            if ($myrow[topic_status]!=0)
               $image = $imgtmpL;

            echo "<td class=\"LIGNB\"><img src=\"$image\" border=\"0\" alt=\"\" /></td>";

            if ($image_subject != "") {
               if ($ibid=theme_image("forum/subject/$image_subject")) {$imgtmp=$ibid;} else {$imgtmp="images/forum/subject/$image_subject";}
               echo "<td class=\"LIGNB\"><img src=\"$imgtmp\" border=\"0\" alt=\"\" /></td>";
            } else echo "<td class=\"LIGNB\"><img src=\"$imgtmpP\" border=\"0\" alt=\"\" /></td>\n";

            $topic_title = stripslashes($myrow[topic_title]);
            if (!stristr($topic_title,"<a href=")) {
               $last_post_url="$hrefX?topic=$myrow[topic_id]&amp;forum=$forum";
               echo "<td class=\"LIGNA\" $linked onclick=\"window.location.href='".$last_post_url."'\">&nbsp;<a href=\"".$last_post_url."\" class=\"NOIR\">$topic_title</a></td>";
               $Sredirection=false;
            } else {
               echo "<td class=\"LIGNA\" $linked>&nbsp;$topic_title</td>";
               $Sredirection=true;
            }

            if ($Sredirection) {
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNB\">&nbsp;</td>";
            } else {
               if (($replys+1) > $posts_per_page) {
                  $pages=0;
                  for ($x = 0; $x < ($replys+1); $x += $posts_per_page)
                     $pages++;
                  $last_post_url="$hrefX?topic=$myrow[topic_id]&amp;forum=$forum&amp;start=".(($pages-1)*$posts_per_page);
               }
               echo "<td align=\"right\" valign=\"middle\" class=\"LIGNB\" style=\"font-size: 10px;\">$replys&nbsp;<a href=\"".$last_post_url."#last-post\"><img src=\"$imgtmpRi\" border=\"0\" align=\"middle\" alt=\"".translate("Last Posts")."\" /></a>&nbsp;&nbsp;&nbsp;</td>";
            }
            if ($Sredirection) {
               if (!$Mmod) {
                  echo "<td align=\"center\" valign=\"middle\" class=\"LIGNA\">&nbsp;</td>";
               } else {
                  echo "<td align=\"center\" valign=\"middle\" class=\"LIGNA\" style=\"font-size: 10px;\">[ <a href=\"$hrefX?topic=$myrow[topic_id]&amp;forum=$forum\" class=\"NOIR\">".translate("Edit")."</a> ]</td>";
               }
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNB\">&nbsp;</td>";
            } else {
               $rowQ1=Q_Select ("SELECT uname FROM users WHERE uid='$myrow[topic_poster]'", 3600);
               list(,$unameT) = each($rowQ1);
               $uname=$unameT["uname"];
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNA\" style=\"font-size: 10px;\">$uname</td>";
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNB\" style=\"font-size: 10px;\">$myrow[topic_views]</td>";
            }
            if ($Sredirection) {
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNA\">&nbsp;</td></tr>";
            } else {
               echo "<td align=\"center\" valign=\"middle\" class=\"LIGNA\" style=\"font-size: 10px;\">".get_last_post($myrow[topic_id],"topic","infos",$Mmod)."</td></tr>";
            }
         }
      } while($myrow = mysql_fetch_array($result));
      mysql_free_result($result);
   } else {
      if ($forum_access!=9)
         echo "<td class=\"LIGNA\" colspan=\"7\" align=\"center\">".translate("There are no topics for this forum. ")."<br /><a href=\"newtopic.php?forum=$forum\" class=\"NOIR\">".translate("You can post one here.")."</a></td></tr>";
   }
   echo "</table>";

   echo "<table border=\"0\" width=\"100%\">";
   echo "<tr>";
   if ($user) {
      echo "<td nowrap=\"nowrap\" valign=\"top\" align=\"left\">";
      echo "&nbsp;".translate("Mark all Topics to Read")." [ <a href=\"viewforum.php?op=mark&amp;forum=$forum\" class=\"NOIR\">OK</a> ]</td>";
   }
   echo "<td nowrap=\"nowrap\" valign=\"top\" align=\"right\">";
   $sql = "SELECT count(*) AS total FROM forumtopics WHERE forum_id='$forum' $closol";
   if (!$r = mysql_query($sql))
      forumerror('0001');
   list($all_topics) = mysql_fetch_array($r);
   mysql_free_result($r);
   if ($closoled=="on")
      $closol="&amp;closoled=on";
    else
      $closol="";
   $count = 1;
   $next = $start + $topics_per_page;
   if ($all_topics > $topics_per_page) {
    if ($next >= $all_topics) {
       echo "<a href=\"viewforum.php?forum=$forum&amp;start=".($next-2*$topics_per_page).$closol."\" class=\"NOIR\">".translate("Previous Page")."</a> [ ";
    } else {
       echo "<a href=\"viewforum.php?forum=$forum&amp;start=".$next.$closol."\" class=\"NOIR\">".translate("Next Page")."</a> [ ";
    }
    for($x = 0; $x < $all_topics; $x++) {
      if (!($x % $topics_per_page)) {
         if ($x == $start)
            echo "<span class=\"ROUGE\">$count</span> ";
         else
            echo "<a href=\"viewforum.php?forum=$forum&amp;start=".$x.$closol."\" class=\"NOIR\">$count</a>&nbsp;";
         if (($all_topics-($count*$topics_per_page))>0) {
            if (!($count % 20)) echo "]&nbsp;<br />[&nbsp;";
         }
         $count++;
      }
    }
    echo " ]&nbsp;";
   }
   echo "</td></tr></table>";
   searchblock();
   echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
   echo "<tr>";
   echo "<td align=\"left\" valign=\"top\" style=\"font-size: 10px;\">";
   echo "<img src=\"$imgtmpR\" border=\"0\" alt=\"\" /> = ".translate("New Posts since your last visit.")."<br /><img src=\"$imgtmpHR\" border=\"0\" alt=\"\" /> = ".translate("More than")." $hot_threshold ".translate("Posts");
   echo "<br /><img src=\"$imgtmpF\" border=\"0\" alt=\"\" /> = ".translate("No New Posts since your last visit.")."<br /><img src=\"$imgtmpH\" border=\"0\" alt=\"\" /> = ".translate("More than")." $hot_threshold ".translate("Posts");
   echo "<br /><img src=\"$imgtmpL\" border=\"0\" alt=\"\" /> = ".translate("Topic is Locked - No new posts may be made in it");
   echo "</td>";
   echo "<td align=\"right\" valign=\"top\">";
   if ($SuperCache) {
      $cache_clef="forum-jump-to";
      $CACHE_TIMINGS[$cache_clef]=3600;
      $cache_obj->startCachingBlock($cache_clef);
   }
   if (($cache_obj->genereting_output==1) or ($cache_obj->genereting_output==-1) or (!$SuperCache)) {
      echo "<form action=\"viewforum.php\" method=\"post\">";
      echo translate("Jump To: ")." <select class=\"TEXTBOX_STANDARD\" name=\"forum\" onchange=\"submit();\"><option value=\"-1\">".translate("Select a Forum")."</option>";
      $sub_sql = "SELECT forum_id, forum_name, forum_type, forum_pass FROM forums ORDER BY cat_id,forum_index,forum_id";
      if ($res = mysql_query($sub_sql)) {
         while (list($forum_id, $forum_name, $forum_type, $forum_pass)=mysql_fetch_array($res)) {
            if (($forum_type != "9") or ($userdata)) {
               if (($forum_type=="7") or ($forum_type=="5")) {
                  $ok_affich=false;
               } else {
                  $ok_affich=true;
               }
               if ($ok_affich) echo "<option value=\"$forum_id\">&nbsp;&nbsp;".stripslashes($forum_name)."</option>\n";
            }
         }
      }
      echo "</select></form>";
   }
   if ($SuperCache) {
      $cache_obj->endCachingBlock($cache_clef);
   }
   echo "</td></tr></table>";
} else {
   header("location: forum.php");
}
include("footer.php");
?>