<?php
/************************************************************************/
/* DUNE by NPDS                                                         */
/* ===========================                                          */
/*                                                                      */
/* Based on PhpNuke 4.x source code                                     */
/*                                                                      */
/* This version name NPDS Copyright (c) 2001-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.       */
/************************************************************************/
function Access_Error() {
  include("admin/die.php");
}
if (!stristr($_SERVER['PHP_SELF'],"admin.php")) { Access_Error(); }

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

function ForumMaintMarkTopics() {
    global $hlpfile;
    global $NPDS_Prefix;
    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 = sql_query("DELETE FROM ".$NPDS_Prefix."forum_read")) {
       forumerror('0001');
    } else {
       $resultF=sql_query("select forum_id from ".$NPDS_Prefix."forums order by forum_id ASC");
       $time_actu=time()+($gmt*3600);
       while (list($forum_id)=sql_fetch_row($resultF)) {
          $rowcolor = tablos();
          echo "<tr $rowcolor><td align=\"center\">$forum_id</td><td align=\"left\">&nbsp;";
          $resultT=sql_query("select topic_id from ".$NPDS_Prefix."forumtopics where forum_id='$forum_id' order by topic_id ASC");
          while (list($topic_id)=sql_fetch_row($resultT)) {
             $resultU=sql_query("select uid from ".$NPDS_Prefix."users order by uid DESC");
             while (list($uid)=sql_fetch_row($resultU)) {
                if ($uid>1) {
                   $r=sql_query("INSERT INTO ".$NPDS_Prefix."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\">".translate("Ok")."</td></tr>";
       }
    }
    echo "</table>";
    closetable();
    sql_free_result;
    include("footer.php");
}

function ForumMaintTopics($before,$forum_name) {
    global $hlpfile;
    global $NPDS_Prefix;
    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=\"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=sql_query("select forum_id, forum_name from ".$NPDS_Prefix."forums $add_sql2 order by forum_id ASC");
    while (list($forum_id, $forum_name)=sql_fetch_row($resultF)) {
       $rowcolor = tablos();
       echo "<tr $rowcolor><td align=\"left\" width=\"25%\"><b>$forum_name</b></td>";
       $resultT=sql_query("select topic_id, topic_title from ".$NPDS_Prefix."forumtopics where forum_id='$forum_id' $add_sql order by topic_id ASC");
       $ibid=0;
       while (list($topic_id, $topic_title)=sql_fetch_row($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\"><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();
    sql_free_result;
    include("footer.php");
}

function ForumMaintTopicDetail($topic, $topic_title) {
    global $hlpfile;
    global $NPDS_Prefix;
    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=sql_query("select post_text, post_time from ".$NPDS_Prefix."posts where topic_id='$topic' order by post_time DESC limit 0,1");
    list($post_text, $post_time)=sql_fetch_row($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=\"noshade\" class=\"ongl\" />";
    echo "[ ".convertdate($post_time)." ]<br /><br />";
    echo stripslashes($post_text);
    closetable();
    echo "</form>";
    closetable();
    include("footer.php");
}

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

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

function SynchroForum() {
    global $NPDS_Prefix;
    // affectation d'un topic à un forum
    if (!$result1 = sql_query("select topic_id, forum_id FROM ".$NPDS_Prefix."forumtopics order by topic_id ASC"))
       forumerror('0009');
    while (list($topi_cid, $foru_mid)=sql_fetch_row($result1)) {
      sql_query("UPDATE ".$NPDS_Prefix."posts SET forum_id='$foru_mid' WHERE topic_id='$topi_cid' and forum_id>0");
    }
    sql_free_result;

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

function MergeForum() {
    global $hlpfile;
    global $NPDS_Prefix;
    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 ".$NPDS_Prefix."forums ORDER BY forum_index,forum_id";
    if ($result = sql_query($sql)) {
       if ($myrow = sql_fetch_assoc($result)) {
          do {
             echo "<option value=\"".$myrow['forum_id']."\">".$myrow['forum_name']."</option>\n";
          } while($myrow = sql_fetch_assoc($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 = sql_query($sql)) {
       if ($myrow = sql_fetch_assoc($result)) {
          do {
             echo "<option value=\"".$myrow['forum_id']."\">".$myrow['forum_name']."</option>\n";
          } while($myrow = sql_fetch_assoc($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();
    sql_free_result;
    include("footer.php");
}
function MergeForumAction($oriforum,$destforum) {
    global $upload_table;
    global $NPDS_Prefix;
    $sql = "UPDATE ".$NPDS_Prefix."forumtopics SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = sql_query($sql))
       forumerro('0010');
    $sql = "UPDATE ".$NPDS_Prefix."posts SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = sql_query($sql))
       forumerror('0010');
    $sql = "UPDATE ".$NPDS_Prefix."forum_read SET forum_id='$destforum' WHERE forum_id='$oriforum'";
    if (!$r = sql_query($sql))
       forumerror('0001');
    $sql = "UPDATE $upload_table SET forum_id='$destforum' WHERE apli='forum_npds' and forum_id='$oriforum'";
    sql_query($sql);
    sql_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\" size=\"20\" maxlenght=\"150\" value=\"\" />";
    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");
}
?>