<? /*

Moderation script for Intellect Board 2 Project

(C) 2004, XXXX Pro, United Open Project
Visit us online: http://intboard.ru

*/

if (!$IBOARD) die("Hack attempt!");

function mod_topic() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && !check_selfmod()) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $topic=$GLOBALS['topic'];
  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Poll WHERE pl_tid=$topic";
  $res = db_query($sql,$link);
  if ($vote=db_num_rows($res)) {
    $pldata = db_fetch_array($res);
    if ($pldata['pl_enddate']) $pldata['enddate']=ceil(($pldata['pl_enddate']-time())/(24*60*60));
    db_free_result($res);
    $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."PollVariant WHERE pv_plid=".$pldata['pl_id'];
    $res = db_query($sql,$link);
    $pv_text=array();
    while ($pvdata=db_fetch_array($res)) $pv_text[$pvdata['pv_id']]=$pvdata['pv_text'];
    for ($i=0; $i<$GLOBALS['opt_defvotecount']; $i++) $pv_text[-($i+1)]="";
    db_free_result($res);
  }
  $uid = $GLOBALS['inuserid'];
  $flist=build_forum_select("f_ltopic",$GLOBALS['inforum']['f_tpid'],"f_id!=".$GLOBALS['forum']);
  mod_topic_form($GLOBALS['intopic'],$pldata,$pv_text,$flist);
}

function do_mod_topic() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && !check_selfmod()) error(MSG_e_mod_norights);
  if (!getvar("t_title")) error(MSG_e_t_empty);
  $mode=getvar("mode");
  $link=$GLOBALS['link'];
  $forum=$GLOBALS['forum'];
  if ($mode==0 || $mode==1) {
    $topic=$GLOBALS['topic'];
    unset($_POST['t_id']);
    $sqldata=build_sql_all("t_");
    if ($mode==1) {
      $newforum=getvar("newforum");
      $sql = "SELECT f_id FROM ".$GLOBALS['DBprefix']."Forum f LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ".
      "ON (ua.uid=".$GLOBALS['inuserid']." AND ua.fid=f_id) WHERE  f_ltopic<=IFNULL(ua_level,".$GLOBALS['inuserbasic'].") AND f_id=\"$newforum\"";   
      $res = db_query($sql,$link);
      if (db_num_rows($res)==0) error(MSG_e_mod_notopicrights);
      $sqldata.=", t_fid=\"".$newforum."\"";
      $userdif = build_diff_list($topic);
      user_substr($userdif,$forum);
      user_summ($userdif,$newforum);

      $sql = "SELECT MAX(p__time) FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=\"$topic\"";
      $res=db_query($sql,$link);
      list($maxtime)=db_fetch_row($res);
      db_free_result($res);
      if ($maxtime) {
        $sql = "UPDATE ".$GLOBALS['DBprefix']."LastVisit SET lv_markcount=lv_markcount-1 WHERE lv_markall>$maxtime AND fid=".$forum;
        $res=db_query($sql,$link);
        $sql = "UPDATE ".$GLOBALS['DBprefix']."LastVisit SET lv_markcount=lv_markcount+1 WHERE lv_markall>$maxtime AND fid=".$newforum;
        $res=db_query($sql,$link);        
      }
    }
    $sql = "UPDATE ".$GLOBALS['DBprefix']."Topic SET $sqldata WHERE t_id=$topic";
    $res = db_query($sql,$link);
    if ($plid=getvar("pl_id")) {
      if (getvar("enddate")) $enddate=time()+getvar("enddate")*24*60*60;
      else $enddate=0;
      $sql = "UPDATE ".$GLOBALS['DBprefix']."Poll SET pl_title=\"".getvar("pl_title")."\", pl_enddate=\"$enddate\" WHERE pl_id=\"$plid\"";
      $res = db_query($sql,$link);
      $pv = $_POST['pv_text'];
      foreach ($pv as $pv_id=>$pv_text) {
        if ($pv_id>0 && $pv_text=="") {
          $sql = "DELETE FROM ".$GLOBALS['DBprefix']."Vote WHERE pvid=\"".db_slashes($pv_id)."\"";
          $res = db_query($sql,$link);
          $sql = "DELETE FROM ".$GLOBALS['DBprefix']."PollVariant WHERE pv_id=\"".db_slashes($pv_id)."\"";
          $res = db_query($sql,$link);
        }
        elseif ($pv_id>0 && $pv_text) {
          $sql = "UPDATE ".$GLOBALS['DBprefix']."PollVariant SET pv_text=\"".db_slashes($pv_text)."\" WHERE pv_id=\"".db_slashes($pv_id)."\"";
          $res = db_query($sql,$link);
        }
        elseif ($pv_id<0 && $pv_text) {
          $sql = "INSERT INTO ".$GLOBALS['DBprefix']."PollVariant (pv_id,pv_plid,pv_text) VALUES(0,\"$plid\",\"".db_slashes($pv_text)."\")";
          $res = db_query($sql,$link);
        }
      }
    }
    if ($mode==1) forum_resync($newforum);
    if ($mode!=0) forum_resync($forum);
    topic_message(MSG_mod_saved);
  }
  else {
    delete_topic($GLOBALS['topic']);
    $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
    forum_resync($forum);
    message(MSG_mod_topic_deleted);
  }
}

function clear_forum() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  clear_form();
}

function do_clear_forum() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link =$GLOBALS['link'];

  $forum = $GLOBALS['forum'];
  $sql = "SELECT t_id FROM ".$GLOBALS['DBprefix']."Topic t, ".$GLOBALS['DBprefix']."Post p1,  ".$GLOBALS['DBprefix']."Post p2 ".
          "WHERE t.t_fid=\"$forum\" AND p1.p_id=t.t_lastpostid AND p2.p_id=t_startpostid";
  if ($days=getvar("days")) {
    $days = $GLOBALS['curtime']-($days*24*60*60);
    $sql.=" AND p1.p__time<$days";
  }
  if ($count=getvar("count")) {
    $sql.=" AND t.t__pcount<=$count";
  }
  if ($title=getvar("title")) {
    $sql.=" AND t.t_title LIKE \"%$title%\"";
  }
  if ($user=getvar("user")) {
    $sql.=" AND p2.p_uname LIKE \"$user\"";
  }
  if (!$days && !$count && !$title && !$user) error(MSG_e_mod_noparams);
  $res = db_query($sql,$link);

  while ($tdata=db_fetch_array($res)) delete_topic($tdata['t_id']);
  forum_resync($forum);
  $msg = format_word($number,MSG_t1,MSG_t2,MSG_t3)." ".MSG_mod_deleted;
  message($msg,1);
}

function split_topic() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && !check_selfmod()) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $uid=$GLOBALS['inuserid'];
  $flist=build_forum_select("f_ltopic",$GLOBALS['inforum']['f_tpid'],$GLOBALS['forum']);
  
  $start=getvar("st");
  $pcount[0]=$GLOBALS['intopic']['t__pcount'];
  if (!isset($_GET['st']) && $pcount[0]>$GLOBALS['inuser']['u_mperpage']) {
    $start=$pcount[0]-$GLOBALS['inuser']['u_mperpage'];
  }
  if (!$start) $start="0";  
  if ($start!="all") $limit = " LIMIT $start,".$GLOBALS['inuser']['u_mperpage'];
  
  $pages=build_pages($pcount[0],$start,$GLOBALS['inuser']['u_mperpage'],"index.php?m=moderate&a=split_topic&t=".$GLOBALS['topic']."&o=$sort");  

  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic'].$limit;
  $res = db_query($sql,$link);
  mod_split_start($flist,$pages);
  while ($pdata=db_fetch_array($res)) {
    mod_split_entry($pdata);
  }
  mod_split_end();
}

function do_split_topic() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate'] && !check_selfmod()) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $tid=getvar('tid');
  if ($tid) {
    if ($tid==$GLOBALS['topic']) error(MSG_e_mod_sametopic);
    $sql = "SELECT f_id FROM ".$GLOBALS['DBprefix']."Forum, ".$GLOBALS['DBprefix']."Topic ".
    "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=".$GLOBALS['inuserid']." AND fid=f_id) ".    
    "WHERE t_id=\"$tid\" AND t_fid=f_id AND f_ltopic<=INFUNLL(ua_level,".$GLOBALS['inuserlevel'].")";
    $res=db_query($sql,$link);
    if (db_num_rows($res)==0) error(MSG_e_mod_noforumsplit);
    list($fid)=db_fetch_row($res);    
    db_free_result($res);
  }
  else $fid=getvar('newforum');
  $forum=$GLOBALS['forum'];
  if (isset($_POST['move'])) {
    $sql = "SELECT COUNT(*) FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic']." AND p_uid!=2";
    $res = db_query($sql,$link);
    list($pcount)=db_fetch_row($res);
    if ($pcount==1) error(MSG_e_mod_singlepost);
    
    if (!$tid) {
      if (!getvar("t_title")) error(MSG_e_t_empty);      
      check_topic($fid);
      $sql = "INSERT INTO ".$GLOBALS['DBprefix']."Topic SET t_fid=\"$fid\", t_title=\"".getvar("t_title")."\", t_descr=\"".getvar("t_descr")."\"";
      foreach ($GLOBALS['intopic'] as $key=>$value) {
        if (substr($key,2,0)=="t_" && $key!="t_title" && $key!="t_descr") $sql.=", $key=\"".db_slashes($value)."\"";
      }
      $res = db_query($sql,$link);
      $tid = db_insert_id($res);
    }
  }

  $posts=$_POST['pid'];
  foreach ($posts as $pid=>$value) {
    if ($_POST['move'][$pid]) {
      if ($sqldata1) $sqldata1.=" OR ";
      $sqldata1.="p_id=".db_slashes($pid);
    }
    if ($_POST['delete'][$pid]) {
      if ($sqldata2) $sqldata2.=" OR ";
      $sqldata2.="p_id=".db_slashes($pid);
    }
  }

  if ($sqldata1) {
    $sql = "SELECT p_uid,COUNT(p_id) AS ucount FROM ".$GLOBALS['DBprefix']."Post WHERE $sqldata1 GROUP BY p_uid";
    $res = db_query($sql,$link);
    while ($udata=db_fetch_row($res)) {
      $movedif[$udata[0]]=$udata[1];
    }

    if (getvar('putlink')) {
      if (getvar('tid')) {
        $sql = "SELECT t_title FROM ".$GLOBALS['DBprefix']."Topic WHERE t_id=\"$tid\"";
        $res=db_query($sql,$link);
        list($t_title)=db_fetch_row($res);
        db_free_result($res);
      }
      else $t_title=getvar("t_title");
      
      $text1 = MSG_t_msgsplitted1." \\\"<a href=\\\"index.php?t=$tid\\\">".$t_title."</a>\\\"";
      $text2 = MSG_t_msgsplitted2." \\\"<a href=\\\"index.php?t=".$GLOBALS['topic']."\\\">".db_slashes($GLOBALS['intopic']['t_title'])."</a>\\\"";
  
      $sql = "INSERT INTO ".$GLOBALS['DBprefix']."Post SET p_uid=2, p_uname=\"System\", p__time=".$GLOBALS['curtime'].", p_tid=".$GLOBALS['topic'].", p_text=\"$text1\", p__html=1";
      $res = db_query($sql,$link);
  
      $sql = "INSERT INTO ".$GLOBALS['DBprefix']."Post SET p_uid=2, p_uname=\"System\", p__time=".$GLOBALS['curtime'].", p_tid=".$tid.", p_text=\"$text2\", p__html=1";
      $res = db_query($sql,$link);
    }

    $sql = "UPDATE ".$GLOBALS['DBprefix']."Post SET p_tid=\"$tid\" WHERE $sqldata1";
    $res = db_query($sql,$link);
    $movecount = db_affected_rows($res);
  }

  if ($sqldata2) {
    $sql = "SELECT p_uid,COUNT(p_id) AS ucount FROM ".$GLOBALS['DBprefix']."Post WHERE $sqldata2 GROUP BY p_uid";
    $res = db_query($sql,$link);
    while ($udata=db_fetch_row($res)) {
      $deldif[$udata[0]]=$udata[1];
    }

    $sql = "SELECT p_attach FROM ".$GLOBALS['DBprefix']."Post WHERE $sqldata2";
    $res = db_query($sql,$link);
    if (db_num_rows($res)>0) {
      while ($pdata=db_fetch_row($res)) {
        if ($sqldata4) $sqldata4.=" OR ";
        $sqldata4.="file_id=".$pdata[0];
      }
      $sql = "DELETE FROM ".$GLOBALS['DBprefix']."File WHERE $sqldata4";
      $res = db_query($sql,$link);
    }
    $sql = "DELETE FROM ".$GLOBALS['DBprefix']."Post WHERE $sqldata2";
    $res = db_query($sql,$link);
    $delcount=db_affected_rows($res);
  }

  user_substr($movedif,$forum);
  user_summ($movedif,$fid);
  user_substr($deldif,$forum);

  topic_resync($GLOBALS['topic']);
  if ($tid) topic_resync($tid);
  forum_resync($forum);
  if ($sqldata1 && $fid!=$forum) forum_resync($fid);

  $msg=format_word($movecount,MSG_p1,MSG_p2,MSG_p3)." ".MSG_mod_moved.", ".format_word($delcount,MSG_p1,MSG_p2,MSG_p3)." ".MSG_mod_deleted;
  message($msg,1);
}

function mod_forum() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $forum=$GLOBALS['forum'];
  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic WHERE t_fid=\"$forum\" ORDER BY t__lastpostid DESC";
  $res = db_query($sql,$link);

  $flist = build_forum_select("f_ltopic",$GLOBALS['inforum']['tp_id'],"f_id!=$forum");
  mod_forum_start($flist);
  while ($tdata=db_fetch_array($res)) {
    mod_forum_entry($tdata);
  }
  mod_forum_end();
}

function do_mod_forum() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];

  if (count($_POST['move'])>0) check_topic(getvar("newforum"));
  $topics = $_POST['topics'];
  $forum=$GLOBALS['forum'];
  foreach ($topics as $tid=>$value) {
    if ($_POST['delete'][$tid]) {
      delete_topic($tid);
    }
    else {
      if ($_POST['sticky'][$tid]) {
        if ($sqldata1) $sqldata1.=" OR ";
        $sqldata1.="t_id=".db_slashes($tid);
      }
      else {
        if ($sqldata2) $sqldata2.=" OR ";
        $sqldata2.="t_id=".db_slashes($tid);
      }
      if ($_POST['stickypost'][$tid]) {
        if ($sqldata3) $sqldata3.=" OR ";
        $sqldata3.="t_id=".db_slashes($tid);
      }
      else {
        if ($sqldata4) $sqldata4.=" OR ";
        $sqldata4.="t_id=".db_slashes($tid);
      }
      if ($_POST['status'][$tid]) {
        if ($sqldata5) $sqldata5.=" OR ";
        $sqldata5.="t_id=".db_slashes($tid);
      }
      else {
        if ($sqldata6) $sqldata6.=" OR ";
        $sqldata6.="t_id=".db_slashes($tid);
      }
      if ($_POST['rate'][$tid]) {
        if ($sqldata7) $sqldata7.=" OR ";
        $sqldata7.="t_id=".db_slashes($tid);
      }
      else {
        if ($sqldata8) $sqldata8.=" OR ";
        $sqldata8.="t_id=".db_slashes($tid);
      }
      if ($_POST['move'][$tid]) {
        if ($sqldata9) $sqldata9.=" OR ";
        $sqldata9.="t_id=".db_slashes($tid);
        if ($sqldata10) $sqldata10.=" OR ";
        $sqldata10.="p_tid=".db_slashes($tid);
      }
    }
  }
  if ($sqldata1) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__sticky=1 WHERE t_fid=\"$forum\" AND ($sqldata1)";
    $res = db_query($sql,$link);
  }
  if ($sqldata2) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__sticky=0 WHERE t_fid=\"$forum\" AND ($sqldata2)";
    $res = db_query($sql,$link);
  }
  if ($sqldata3) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__stickypost=1 WHERE t_fid=\"$forum\" AND ($sqldata3)";
    $res = db_query($sql,$link);
  }
  if ($sqldata4) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__stickypost=0 WHERE t_fid=\"$forum\" AND ($sqldata4)";
    $res = db_query($sql,$link);
  }
  if ($sqldata5) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__status=1 WHERE t_fid=\"$forum\" AND ($sqldata5)";
    $res = db_query($sql,$link);
  }
  if ($sqldata6) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__status=0 WHERE t_fid=\"$forum\" AND ($sqldata6)";
    $res = db_query($sql,$link);
  }
  if ($sqldata7) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__rate=1 WHERE t_fid=\"$forum\" AND ($sqldata7)";
    $res = db_query($sql,$link);
  }
  if ($sqldata8) {
    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t__rate=0 WHERE t_fid=\"$forum\" AND ($sqldata8)";
    $res = db_query($sql,$link);
  }
  if ($sqldata9) {
    $sql = "SELECT p_uid,COUNT(p_id) AS ucount FROM ".$GLOBALS['DBprefix']."Post WHERE $sqldata10 GROUP BY p_uid";
    $res = db_query($sql,$link);
    while ($udata=db_fetch_row($res)) {
      $movedif[$udata[0]]=$udata[1];
    }
    $newforum=getvar('newforum');
    user_substr($movedif,$forum);
    user_summ($movedif,$newforum);

    $sql="UPDATE ".$GLOBALS['DBprefix']."Topic SET t_fid=\"".getvar("newforum")."\" WHERE t_fid=\"$forum\" AND ($sqldata9)";
    $res = db_query($sql,$link);
    forum_resync($forum);
    forum_resync($newforum);
  }
  $GLOBALS['refpage'] = "index.php?f=".$GLOBALS['forum'];
  message(MSG_mod_t_saved);
}

function check_topic($forum) {
  $link = $GLOBALS['link'];
  $sql = "SELECT f_id FROM ".$GLOBALS['DBprefix']."Forum f LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ".
  "ON (ua.uid=".$GLOBALS['inuserid']." AND ua.fid=f_id) WHERE IFNULL(ua_level,".$GLOBALS['inuserbasic'].")>=f_ltopic AND f_id=\"$forum\"";
  $res = db_query($sql,$link);
  if (db_num_rows($res)==0) error(MSG_e_mod_notopicrights);
  db_free_result($res);
}

function join_topic() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $sql = "SELECT t_id,t_title FROM ".$GLOBALS['DBprefix']."Topic WHERE t_fid=".$GLOBALS['forum']." AND t_id!=".$GLOBALS['topic'];
  $topiclist = build_select($sql);
  join_form($topiclist);
}

function do_join_topic() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $newtid=getvar("newtid");
  $text = MSG_t_msgjoin." \\\"".db_slashes($GLOBALS['intopic']['t_title'])."\\\"";
  $sql = "INSERT INTO ".$GLOBALS['DBprefix']."Post SET p_uid=2, p__time=".$GLOBALS['curtime'].", p_tid=\"$newtid\", p_text=\"$text\", p__html=1";
  $res = db_query($sql,$link);

  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic']." ORDER BY p_id";
  $res = db_query($sql,$link);
  while ($pdata=db_fetch_array($res)) {
    $sqldata="";
    foreach ($pdata as $curkey=>$curvalue) {
      if ($curkey!='p_id' && $curkey!='p_tid') {
        $sqldata.=", $curkey=\"".db_slashes($curvalue)."\"";
      }
    }
    $sql="INSERT INTO ".$GLOBALS['DBprefix']."Post SET p_tid=\"$newtid\" $sqldata";
    $res2 = db_query($sql,$link);
  }
  $sql="DELETE FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic'];
  $res = db_query($sql,$link);
  delete_topic($GLOBALS['topic']);
  forum_resync($GLOBALS['forum']);
  topic_resync($newtid);
  $GLOBALS['topic']=$newtid;
  topic_message(MSG_mod_joined);
}

function edit_rules() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  rules_form($GLOBALS['inforum']['f_rules']);
}

function do_edit_rules() {
  check_post();
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];
  $rules=getvar("rules_text");
  $sql = "UPDATE ".$GLOBALS['DBprefix']."Forum SET f_rules=\"$rules\" WHERE f_id=\"".$GLOBALS['forum']."\"";
  $res = db_query($sql,$link);
  $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
  message(MSG_f_rules_saved);
}

function premod() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MSG_e_mod_norights);
  $link=$GLOBALS['link'];

  $sql = "SELECT p.*,t_id,t_title FROM ".$GLOBALS['DBprefix']."Post p, ".$GLOBALS['DBprefix']."Topic ".
         "WHERE p_tid=t_id AND t_fid=".$GLOBALS['forum']." AND p__premoderate=1 ORDER BY p_tid,p__time";
  $res = db_query($sql,$link);
  premod_start();
  $oldtopic=0;
  while ($pdata=db_fetch_array($res)) {
    if ($pdata['t_id']!=$oldtopic) {
      premod_topic($pdata);
      $oldtopic=$pdata['t_id'];
    }
    premod_entry($pdata);
  }
  premod_end();
}

function do_premod() {
  check_post();
  $link=$GLOBALS['link'];
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MGE_e_mod_norights);

  $sql = "SELECT p_id FROM ".$GLOBALS['DBprefix']."Post, ".$GLOBALS['DBprefix']."Topic ".
  "WHERE p_tid=t_id AND t_fid=".$GLOBALS['forum']." AND p__premoderate=1";
  $res = db_query($sql,$link);
  while ($tmp=db_fetch_row($res)) {
    $curkey=$tmp[0];
    $curvalue=$_POST['pid'][$curkey];
    if ($curvalue==2) {
      if ($deldata) $deldata.=" OR ";
      $deldata.="p_id=$curkey";
    }
    elseif ($curvalue==1) {
      if ($pmdata) $pmdata.=" OR ";
      $pmdata.="p_id=$curkey";
    }
  }

  if ($pmdata) {
    $sql = "SELECT p_uid, COUNT(p_id) AS ucount FROM ".$GLOBALS['DBprefix']."Post WHERE $pmdata GROUP BY p_uid";
    $res = db_query($sql,$link);
    while ($udata=db_fetch_row($res)) {
      $movedif[$udata[0]]=$udata[1];
    }
    user_summ($movedif,$GLOBALS['inforum']);

    $sql = "UPDATE ".$GLOBALS['DBprefix']."Post SET p__premoderate=0 WHERE $pmdata";
    $res = db_query($sql,$link);

    $sql = "SELECT DISTINCT p_tid FROM ".$GLOBALS['DBprefix']."Post WHERE $pmdata";
    $res = db_query($sql,$link);
    while ($tdata=db_fetch_row($res)) {
      topic_resync($tdata[0]);
    }

    if (!$deldata) forum_resync($GLOBALS['forum']);

    $sql = "SELECT p.*,t_title,t_id,f_title,f_id FROM ".$GLOBALS['DBprefix']."Post p, ".$GLOBALS['DBprefix']."Topic, ".$GLOBALS['DBprefix']."Forum ".
          "WHERE ($pmdata) AND p_tid=t_id AND t_fid=f_id";
    $res = db_query($sql,$link);
    $buffer=load_mail("std_post.txt");
    while ($pdata=db_fetch_array($res)) {
      $sql = "SELECT u_id,u__name,u__email FROM ".$GLOBALS['DBprefix']."Post, ".$GLOBALS['DBprefix']."User ".
      "LEFT JOIN ".$GLOBALS['DBprefix']."Subscription sb ON (sb.uid=u_id AND p_tid=sb.tid) ".
      "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ON (ua.uid=u_id AND ua.fid=".$GLOBALS['forum'].") ".
      "WHERE p_uid=u_id AND u_id>4 AND ".
      "IFNULL(ua_level,u__level)>".$GLOBALS['inforum']['f_lmoderate'];
      $res = db_query($sql,$link);
      while ($email=db_fetch_row($res)) {
        $GLOBALS['username']=$email[0];
        $GLOBALS['postername']=$GLOBALS['inuser']['u__name'];
        $GLOBALS['flink']=$GLOBALS['opt_url']."/index.php?t=".$GLOBALS['topic'];
        $GLOBALS['unsublink']=$GLOBALS['opt_url']."/agent.php?a=unsub&u=".$GLOBALS['inuserid'].
         "&f=".$GLOBALS['forum']."&t=".$GLOBALS['topic']."&key=".md5($GLOBALS['topic'].$GLOBALS['inuser']['u__key']);
        replace_mail($buffer,$email[1],MSG_p_newmessage." ".$GLOBALS['ttitle']);
      }
    }
  }

  if ($deldata) {
    $sql = "SELECT p_attach FROM ".$GLOBALS['DBprefix']."Post WHERE $deldata";
    $res = db_query($sql,$link);
    while ($tmp=db_fetch_row($res)) {
      if ($tmp[0]!=0) {
        if ($attachdata) $attachdata.=", ";
        $attachdata.="file_id=".$tmp[0];
      }
    }

    if ($attachdata) {
      $sql = "DELETE FROM ".$GLOBALS['DBprefix']."File WHERE $attachdata";
      $res = db_query($sql,$link);
    }

    $sql = "DELETE FROM ".$GLOBALS['DBprefix']."Post WHERE $deldata";
    $res = db_query($sql,$link);

    $sql = "SELECT p_tid,COUNT(*) AS pcount FROM ".$GLOBALS['DBprefix']."Post, ".$GLOBALS['DBprefix']."Topic ".
    "WHERE p_tid=t_id AND t_fid=".$GLOBALS['forum']." GROUP BY p_tid HAVING pcount=0";
    $res = db_query($sql,$link);
    while ($tmp=db_fetch_row($res)) {
      delete_topic($tmp[0]);
    }
    forum_resync($GLOBALS['forum']);
  }
  $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
  message(MSG_f_premoderated);
}

function mod_banlist() {
  $link=$GLOBALS['link'];
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MGE_e_mod_norights);

  $sql = "SELECT u_id,u__name FROM ".$GLOBALS['DBprefix']."User, ".$GLOBALS['DBprefix']."UserAccess ".
  "WHERE u_id=uid AND fid=".$GLOBALS['forum']." AND ua_level=-1 ORDER BY u__name AND u_id>3";
  $res = db_query($sql,$link);
  mod_ban_start();
  while ($udata=db_fetch_array($res)) {
    mod_ban_entry($udata);
  }
  if (db_num_rows($res)==0) mod_ban_noentries();
  mod_ban_end();
}

function do_mod_clearban() {
  $key=getvar('key');
  if (md5($GLOBALS['inuser']['u__key'].$GLOBALS['forum'])!=$key) error(MSG_e_mod_badkey);
  $link=$GLOBALS['link'];
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MGE_e_mod_norights);

  $uid=getvar("u");
  $sql = "DELETE FROM ".$GLOBALS['DBprefix']."UserAccess WHERE uid=\"$uid\" AND fid=".$GLOBALS['forum'];
  $res = db_query($sql,$link);
  message(MSG_mod_bancleared,1);
}

function do_mod_addban() {
  check_post();
  $link=$GLOBALS['link'];
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MGE_e_mod_norights);

  $uname=getvar("uname");
  $sql = "SELECT u_id,u__level,ua_level FROM ".$GLOBALS['DBprefix']."User ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ON (uid=u_id AND fid=".$GLOBALS['forum'].") ".
  "WHERE u__name=\"$uname\"";
  $res = db_query($sql,$link);
  list($uid,$ulevel,$ualevel)=db_fetch_row($res);

  if ($ulevel>=$GLOBALS['inuserlevel'] || $ualevel>=$GLOBALS['inuserlevel']) error(MSG_e_mod_subordinate);

  $sql = "REPLACE INTO ".$GLOBALS['DBprefix']."UserAccess SET uid=$uid, fid=".$GLOBALS['forum'].", ua_level=-1";
  $res = db_query($sql,$link);
  message(MSG_mod_banned,1);
}

function complain() {
  complain_form();
}

function do_complain() {
  check_post();
  $link=$GLOBALS['link'];
  $text=getvar("text");
  if (!$text) error(MSG_e_p_emptycomplain);
  $subj=MSG_mod_complain;
  $text.="\n".MSG_topic." ".$GLOBALS['intopic']['t_title']." ".MSG_offorum." ".$GLOBALS['inforum']['f_title']."\n";
  $text.=$GLOBALS['opt_url']."/index.php?t=".$GLOBALS['topic']."&p=".getvar("p");

  $sql = "SELECT u_id FROM ".$GLOBALS['DBprefix']."User u ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."UserAccess ua ON (ua.uid=u.u_id AND ua.fid=".$GLOBALS['forum'].") ".
  "WHERE IFNULL(ua.ua_level,u.u__level)>=".$GLOBALS['inforum']['f_lmoderate']." AND u_id>3";
  $res = db_query($sql,$link);

  while ($uid=db_fetch_row($res)) {
    send_pm($uid[0],$GLOBALS['inuserid'],$text,$subj,"");
  }
  topic_message(MSG_mod_complain_send);
}

function view_vote() {
  $link=$GLOBALS['link'];
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lmoderate']) error(MGE_e_mod_norights);

  $topic=$GLOBALS['topic'];
  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Poll WHERE pl_tid=$topic";
  $res = db_query($sql,$link);
  $pldata = db_fetch_array($res);
  $plid=$pldata['pl_id'];
  $sql = "SELECT u__name,u_id, pv.* FROM ".$GLOBALS['DBprefix']."PollVariant pv, ".$GLOBALS['DBprefix']."Vote, ".$GLOBALS['DBprefix']."User ".
         "WHERE pv_plid=$plid AND pvid=pv_id AND u_id=uid";
  $res = db_query($sql,$link);
  vote_view_start($pldata);
  while ($pvdata=db_fetch_array($res)) {
    vote_view_entry($pvdata);
  }
  vote_view_end();
}

function locations($locations) {
  push_parents($locations,$GLOBALS['inforum']['f_parent']);
  if ($GLOBALS['topic']) {
    array_push($locations,"<a href=\"index.php?f=".$GLOBALS['forum']."\">".$GLOBALS['inforum']['f_title']."</a>");
    array_push($locations,"<a href=\"index.php?t=".$GLOBALS['topic']."\">".$GLOBALS['intopic']['t_title']."</a>");
  }
  else {
    array_push($locations,"<a href=\"index.php?f=".$GLOBALS['forum']."\">".$GLOBALS['inforum']['f_title']."</a>");
  }
  if ($GLOBALS['action']=="mod_topic") array_push($locations,MSG_mod_topic);
  elseif ($GLOBALS['action']=="split_topic") array_push($locations,MSG_mod_split);
  elseif ($GLOBALS['action']=="join_topic") array_push($locations,MSG_mod_join);
  elseif ($GLOBALS['action']=="premod") array_push($locations,MSG_mod_premod);
  elseif ($GLOBALS['action']=="mod_banlist") array_push($locations,MSG_mod_bannedlist);
  elseif ($GLOBALS['action']=="view_vote") array_push($locations,MSG_mod_voteview);
  elseif ($GLOBALS['action']=="mod_forum") array_push($locations,MSG_mod_forum);
  elseif ($GLOBALS['action']=="clear_forum") array_push($locations,MSG_mod_clear);
  return $locations;
}
