<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/* Based on Parts of phpBB                                              */
/*                                                                      */
/* NPDS Copyright (c) 2002-2010 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")) {
   include ("mainfile.php");
}

include('functions.php');
if ($SuperCache) {
   $cache_obj = new cacheManager();
} else {
   $cache_obj = new SuperCacheEmpty();
}
include('auth.php');
global $NPDS_Prefix;

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

if ($forum=="index")
   header("location: forum.php");
settype($forum, "integer");
$rowQ1=Q_Select ("SELECT forum_name, forum_moderator, forum_type, forum_pass, forum_access, arbre FROM ".$NPDS_Prefix."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 ".$NPDS_Prefix."forumtopics SET topic_status='2', topic_title='[".translate("Solved")."] - ".removehack($topic_title)."' WHERE topic_id='$topic_id'";
         if (!$r = sql_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'];} else {settype($Forum_passwd,'string');}

// 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); $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> &raquo; &raquo; ".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=\"textbox_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 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 ($user)
         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> &raquo; &raquo; ".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 (isset($closoled)) {
         $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>";
   settype($start,"integer");
   settype($topics_per_page,"integer");
   $sql = "SELECT * FROM ".$NPDS_Prefix."forumtopics WHERE forum_id='$forum' $closol ORDER BY topic_first,topic_time DESC LIMIT $start, $topics_per_page";
   if (!$result = sql_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 = sql_fetch_assoc($result)) {
      $linked = "onmouseover=\"this.className='lignb'; this.style.cursor='default';\" "
      ." 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 ".$NPDS_Prefix."posts WHERE topic_id='".$myrow['topic_id']."' and forum_id='$forum' limit 0,1", 86400);
               $image_subject=$rowQ1[0]['image'];
            }

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

            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=\"center\" 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 ".$NPDS_Prefix."users WHERE uid='".$myrow['topic_poster']."'", 3600);
               $uname=$rowQ1[0]['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 = sql_fetch_assoc($result));
      sql_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 ".$NPDS_Prefix."forumtopics WHERE forum_id='$forum' $closol";
   if (!$r = sql_query($sql))
      forumerror('0001');
   list($all_topics) = sql_fetch_row($r);
   sql_free_result($r);
   if (isset($closoled))
      $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=\"index\">...</option>";
      echo "<option value=\"index\">".translate("Forum Index")."</option>";
      $sub_sql = "SELECT forum_id, forum_name, forum_type, forum_pass FROM ".$NPDS_Prefix."forums ORDER BY cat_id,forum_index,forum_id";
      if ($res = sql_query($sub_sql)) {
         while (list($forum_id, $forum_name, $forum_type, $forum_pass)=sql_fetch_row($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");
?>