<?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-2005 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.       */
/************************************************************************/
function Access_Error() {
  if (file_exists("meta/meta.php"))
     include ("meta/meta.php");
  echo "<title>NPDS</title></head><body bgcolor=\"white\"><br /><p align=\"center\"><span style=\"font-size: 14px; font-family: Arial; font-weight: bold; color: red;\">";
  echo "Access Denied / Acc&egrave;s Refus&eacute;";
  if (cur_charset=="utf-8") echo" / &#x901A;&#x5165;&#x88AB;&#x5426;&#x8BA4;";
  echo "</span>";
  echo "<br /><br /><span style=\"font-size: 12px; font-family: Arial; font-weight: bold; color: black;\">NPDS - Portal System";
  echo "</span></p></body></html>";
  die();
}
if (!strstr($PHP_SELF,"admin.php")) { Access_Error(); }
global $pdst; $pdst="0";

global $language;
$hlpfile = "manuels/$language/forummaint.html";
$result = mysql_query("select radminforum, radminsuper from authors where aid='$aid'");
list($radminforum, $radminsuper) = mysql_fetch_row($result);
if (($radminforum!=1) and ($radminsuper!=1)) {
   Access_Error();
} else {
   include ("auth.php");
   include ("functions.php");
}

function ForumMaintMarkTopics() {
    global $hlpfile;
    include ("header.php");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
    echo adm_translate("Maintenance des Forums")." - ".adm_translate("Marquer tous les Topics comme lus")."";
    echo "</td></tr></table>\n";
    echo "<br />";
    echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\"><tr>\n<td width=\"25%\" align=\"center\"><b>ID</b></td><td width=\"50%\" align=\"center\"><b>Topics ID</b></td><td width=\"25%\" align=\"center\"><b>Status</b></td></tr>";
    if (!$r = mysql_query("DELETE FROM forum_read")) {
       forumerror('0001');
    } else {
       $resultF=mysql_query("select forum_id from forums order by forum_id ASC");
       $time_actu=time()+($gmt*3600);
       while (list($forum_id)=mysql_fetch_array($resultF)) {
          $rowcolor = tablos();
          echo "<tr $rowcolor><td align=\"center\">$forum_id</td><td align=\"left\">&nbsp;";
          $resultT=mysql_query("select topic_id from forumtopics where forum_id='$forum_id' order by topic_id ASC");
          while (list($topic_id)=mysql_fetch_array($resultT)) {
             $resultU=mysql_query("select uid from users order by uid DESC");
             while (list($uid)=mysql_fetch_array($resultU)) {
                if ($uid>1) {
                   $r=mysql_query("INSERT INTO forum_read (forum_id, topicid, uid, last_read, status) VALUES ('$forum_id', '$topic_id', '$uid', '$time_actu', '1')");
                }
             }
             echo $topic_id." ";
          }
          echo "</td><td align=\"center\">OK</td></tr>";
       }
    }
    echo "</table>";
    closetable();
    mysql_free_result;
    include("footer.php");
}

function ForumMaintTopics($before,$forum_name) {
    global $hlpfile;
    include ("header.php");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<form action=\"admin.php\" method=\"post\">";
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
    echo adm_translate("Maintenance des Forums")." - ".adm_translate("Supprimer massivement les Topics");
    if ($before!="") {
        echo "&nbsp;<span class=\"ROUGE\">< $before</span>";
       $add_sql="and topic_time<'$before'";
       $topic_check="checked";
    } else {
       $add_sql="";
       $topic_check="";
    }
    if ($forum_name!="") {
       $add_sql2="where forum_name='$forum_name'";
    } else {
       $add_sql2="";
    }
    echo "</td></tr></table>\n";
    echo "<br />";
    echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">\n";
    $resultF=mysql_query("select forum_id, forum_name from forums $add_sql2 order by forum_id ASC");
    while (list($forum_id, $forum_name)=mysql_fetch_array($resultF)) {
       $rowcolor = tablos();
       echo "<tr $rowcolor><td align=\"left\" width=\"25%\"><b>$forum_name</b></td>";
       $resultT=mysql_query("select topic_id, topic_title from forumtopics where forum_id='$forum_id' $add_sql order by topic_id ASC");
       $ibid=0;
       while (list($topic_id, $topic_title)=mysql_fetch_array($resultT)) {
          if ($parse==0) {
             $tt =  FixQuotes($topic_title);
          } else {
             $tt =  stripslashes($topic_title);
          }
          if ($ibid==20) {
             echo "</tr><tr><td>&nbsp;</td>";
             $ibid=0;
          }
          $ibid++;
          echo "<td align=\"center\" wrap><a href=\"admin.php?op=MaintForumTopicDetail&amp;topic=$topic_id&amp;topic_title=".urlencode($tt)."\" title=\"$tt\" class=\"NOIR\">$topic_id</a><br />
          <input class=\"texbox\" type=\"checkbox\" name=\"topics[$topic_id]\" $topic_check></td>";
       }
       if ($ibid<20)
          echo "<td colspan=\"".(20-$ibid)."\">&nbsp;</td>";
    }
    echo "</tr></table>";
    echo "<input type=\"hidden\" name=\"op\" value=\"ForumMaintTopicMassiveSup\">";
    echo "<p align=\"center\"><br /><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Topics_Del\" value=\"".adm_translate("Supprimer massivement les Topics")."\"></p>";
    echo "</form>";
    closetable();
    mysql_free_result;
    include("footer.php");
}

function ForumMaintTopicDetail($topic, $topic_title) {
    global $hlpfile;
    include ("header.php");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
    echo adm_translate("Maintenance des Forums")." - ".adm_translate("Supprimer massivement les Topics");
    echo "</td></tr></table>\n";
    echo "<form action=\"admin.php\" method=\"post\">";
    $resultTT=mysql_query("select post_text, post_time from posts where topic_id='$topic' order by post_time DESC limit 0,1");
    list($post_text, $post_time)=mysql_fetch_array($resultTT);
    OpenTable();
    echo "<input type=\"hidden\" name=\"op\" value=\"ForumMaintTopicSup\"><input type=\"hidden\" name=\"topic\" value=\"$topic\">";
    echo "<b>Topic : $topic | ".stripslashes($topic_title)."</b> | ";
    echo "<input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Topics_Del\" value=\"".adm_translate("Effacer")."\"><hr noshade class=\"ONGL\">";
    echo "[ ".convertdate($post_time)." ]<br /><br />";
    echo stripslashes($post_text);
    closetable();
    echo "</form>";
    closetable();
    include("footer.php");
}

function ForumMaintTopicMassiveSup($topics) {
    if ($topics) {
       while (list($topic_id,$value)=each($topics)) {
          if ($value=="on") {
             $sql = "DELETE FROM posts WHERE topic_id = '$topic_id'";
             if (!$result = mysql_query($sql))
                forumerror('0009');
             $sql = "DELETE FROM forumtopics WHERE topic_id = '$topic_id'";
             if (!$result = mysql_query($sql))
                forumerror('0010');
             $sql = "DELETE FROM forum_read where topicid = '$topic_id'";
             if (!$r = mysql_query($sql))
                forumerror('0001');
             control_efface_post("forum_npds","",$topic_id,"");
          }
       }
       mysql_free_result;
    }
    Q_Clean();
    header("location: admin.php?op=MaintForumAdmin");
}

function ForumMaintTopicSup($topic) {
    $sql = "DELETE FROM posts WHERE topic_id = '$topic'";
    if (!$result = mysql_query($sql))
       forumerror('0009');
    $sql = "DELETE FROM forumtopics WHERE topic_id = '$topic'";
    if (!$result = mysql_query($sql))
       forumerror('0010');
    $sql = "DELETE FROM forum_read where topicid = '$topic'";
    if (!$r = mysql_query($sql))
       forumerror('0001');
    mysql_free_result;
    control_efface_post("forum_npds","",$topic,"");
    Q_Clean();
    header("location: admin.php?op=MaintForumTopics");
}

function SynchroForum() {
    // affectation d'un topic à un forum
    if (!$result1 = mysql_query("select topic_id, forum_id FROM forumtopics order by topic_id ASC"))
       forumerror('0009');
    while (list($topi_cid, $foru_mid)=mysql_fetch_row($result1)) {
      mysql_query("UPDATE posts SET forum_id='$foru_mid' WHERE topic_id='$topi_cid'");
    }
    mysql_free_result;

    // table forum_read et contenu des topic
    if (!$result1 = mysql_query("select topicid, uid, rid FROM forum_read order by topicid ASC"))
       forumerror('0009');
    while (list($topicid, $uid, $rid)=mysql_fetch_row($result1)) {
       if (($topicid.$uid)==$tmp) {
          $resultD = mysql_query("DELETE FROM forum_read where topicid='$topicid' and uid='$uid' and rid='$rid'");
       }
       $tmp=$topicid.$uid;
       if ($result2 = mysql_query("select topic_id FROM forumtopics WHERE topic_id = '$topicid'")) {
          list($topic_id)=mysql_fetch_row($result2);
          if (!$topic_id) {
             $result3 = mysql_query("DELETE FROM forum_read where topicid='$topicid'");
          }
       }
    }
    mysql_free_result;
    header("location: admin.php?op=MaintForumAdmin");
}

function MergeForum() {
    global $hlpfile;
    include ("header.php");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
    echo adm_translate("Maintenance des Forums")." - ".adm_translate("Fusionner des forums");
    echo "</td></tr></table>\n";
    echo "<form action=\"admin.php\" method=\"POST\">
    <table border=\"0\" cellpadding=\"1\" cellspacing=\"1\" width=\"100%\">
    <tr>
    <td class=\"HEADER\">".adm_translate("Forum d'origine")." :</td>
    <td class=\"HEADER\"><select class=\"TEXTBOX_STANDARD\" name=\"oriforum\">";
    $sql = "SELECT forum_id, forum_name FROM forums ORDER BY forum_index,forum_id";
    if ($result = mysql_query($sql)) {
       if ($myrow = mysql_fetch_array($result)) {
          do {
             echo "<option value=\"$myrow[forum_id]\">$myrow[forum_name]</option>\n";
          } while($myrow = mysql_fetch_array($result));
       } else {
          echo "<option value=\"-1\">".translate("No More Forums")."</option>\n";
       }
    } else {
       echo "<option value=\"-1\">Database Error</option>\n";
    }
    echo "</select></td></tr>";

    echo "<tr>
    <td class=\"HEADER\">".adm_translate("Forum de destination")." :</td>
    <td class=\"HEADER\"><select class=\"TEXTBOX_STANDARD\" name=\"destforum\">";
    if ($result = mysql_query($sql)) {
       if ($myrow = mysql_fetch_array($result)) {
          do {
             echo "<option value=\"$myrow[forum_id]\">$myrow[forum_name]</option>\n";
          } while($myrow = mysql_fetch_array($result));
       } else {
          echo "<option value=\"-1\">".translate("No More Forums")."</option>\n";
       }
    } else {
       echo "<option value=\"-1\">Database Error</option>\n";
    }
    echo "</select></td></tr>";
    echo "</table>";
    echo "<input type=\"hidden\" name=\"op\" value=\"MergeForumAction\">";
    echo "<p align=\"center\"><br /><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Merge_Forum_Action\" value=\"".adm_translate("Envoyer")."\"></p>";
    echo "</form>";
    closetable();
    mysql_free_result;
    include("footer.php");
}
function MergeForumAction($oriforum,$destforum) {
    global $upload_table;
    $sql = "UPDATE forumtopics SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = mysql_query($sql))
       forumerro('0010');
    $sql = "UPDATE posts SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = mysql_query($sql))
       forumerror('0010');
    $sql = "UPDATE forum_read SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = mysql_query($sql))
       forumerror('0001');
    $sql = "UPDATE $upload_table SET forum_id='$destforum' WHERE apli='forum_npds' and forum_id='$oriforum'";
    mysql_query($sql);
    mysql_free_result;
    Q_Clean();
    header("location: admin.php?op=MaintForumAdmin");
}

function ForumMaintAdmin() {
    global $hlpfile;
    include ("header.php");
    GraphicAdmin($hlpfile);
    OpenTable();
    echo "<table width=\"100%\" cellspacing=\"2\" cellpadding=\"2\" border=\"0\"><tr><td class=\"HEADER\">\n";
    echo adm_translate("Maintenance des Forums");
    echo "</td></tr></table><br />\n";
    echo "<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" border=\"0\">";
    // Mark Topics
    echo "<form action=\"admin.php\" method=\"post\">";
    echo "<tr><td class=\"TITBOXC\">";
    echo "<input type=\"hidden\" name=\"op\" value=\"MaintForumMarkTopics\">";
    echo adm_translate("Marquer tous les Topics comme lus")." : </td><td class=\"TITBOXC\"><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Topics_Mark\" value=\"".adm_translate("Envoyer")."\"></td></tr>";
    echo "</form>";
    // Admin Topics
    echo "<form action=\"admin.php\" method=\"post\">";
    echo "<tr><td class=\"TITBOXC\">";
    echo "<input type=\"hidden\" name=\"op\" value=\"MaintForumTopics\">";
    echo adm_translate("Supprimer massivement les Topics")." : <br />";
    echo "&nbsp;&nbsp;AAAA-MM-JJ : <input type=\"text\" class=\"TEXTBOX_STANDARD\" name=\"before\" value=\"\" size=\"10\" maxlenght=\"11\">";
    echo "&nbsp;&nbsp;".adm_translate("Nom du forum")."&nbsp;<input type=\"text\" class=\"TEXTBOX_STANDARD\" name=\"forum_name\" value=\"\" size=\"20\" maxlenght=\"150\">";
    echo "</td><td class=\"TITBOXC\" valign=\"bottom\"><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Topics_Mark\" value=\"".adm_translate("Envoyer")."\"></td></tr>";
    echo "</form>";
    // Synchro Forum_read table
    echo "<form action=\"admin.php\" method=\"post\">";
    echo "<tr><td class=\"TITBOXC\">";
    echo "<input type=\"hidden\" name=\"op\" value=\"SynchroForum\">";
    echo adm_translate("Synchroniser les forums")." : </td><td class=\"TITBOXC\"><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Synchro_Forum\" value=\"".adm_translate("Envoyer")."\"></td></tr>";
    echo "</form>";
    // Merge Forums
    echo "<form action=\"admin.php\" method=\"post\">";
    echo "<tr><td class=\"TITBOXC\">";
    echo "<input type=\"hidden\" name=\"op\" value=\"MergeForum\">";
    echo adm_translate("Fusionner des forums")." : </td><td class=\"TITBOXC\"><input class=\"BOUTON_STANDARD\" type=\"submit\" name=\"Merge_Forum\" value=\"".adm_translate("Envoyer")."\"></td></tr>";
    echo "</form>";
    echo "</table>";
    closetable();
    include("footer.php");
}
?>