<? /*

News script for Intellect Board 2 Project

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

*/

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

function news_view() {
  if ($GLOBALS['topic']) view_topic();
  else view_list();
}

function view_list() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lread']) error(MSG_e_f_norightsread);
  $forum=$GLOBALS['forum'];
  $link=$GLOBALS['link'];
  $order = getvar("o");
  $direct=getvar("desc");
  $sql = "SELECT MIN(p__time) FROM ".$GLOBALS['DBprefix']."Post, ".$GLOBALS['DBprefix']."Topic WHERE p_id=t__startpostid AND t_fid=$forum ";
  $res = db_query($sql,$link);
  list($mindate)=db_fetch_row($res);

  if (!$vardate=getvar("vd")) $vardate=mktime(0,0,0,date("n",$GLOBALS['curtime']),date("j",$GLOBALS['curtime']),date("Y",$GLOBALS['curtime']));
  if ($mindate) format_calendar($vardate,$mindate,"index.php?f=".$GLOBALS['forum']);
  $timelimit=" AND p__time<=".($vardate+24*60*60);

  $start=getvar("st");
  if (!$start) $start="0";
  if (!$perpage) $perpage=$GLOBALS['inuser']['u_aperpage'];
  $tcount=$GLOBALS['inforum']['f__tcount'];
  $pages = build_pages($tcount,$start,$perpage,"index.php?f=$forum&o=$order&desc=$desc");
  if ($start!="all") $limit = " LIMIT $start,$perpage";
  if (!$order) $order=" p__time DESC";  

  $sql = "SELECT t.*, p.*, t__pcount-1 AS pcount, t__ratingsum/t__ratingcount AS trating, tv.tid AS visited FROM ".$GLOBALS['DBprefix']."Topic t, ".
     $GLOBALS['DBprefix']."User u, ".$GLOBALS['DBprefix']."Post p ".
     "LEFT JOIN ".$GLOBALS['DBprefix']."TopicView tv ON (tv.tid=t.t_id AND tv.uid=".$GLOBALS['inuserid'].") ".
     "WHERE t.t_fid=$forum AND u.u_id=p.p_uid AND t.t__startpostid=p.p_id AND p__premoderate=0 $timelimit ".
     "ORDER BY $order".$direct." $limit";
  $res = db_query($sql,$link);

  news_list_start($pages);
  while ($ndata=db_fetch_array($res)) {
    news_list_entry($ndata);
  }
  if (db_num_rows($res)==0) news_list_noentries();
  news_list_end();
}

function view_topic() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_lread']) error(MSG_e_f_norightsread);
  $forum=$GLOBALS['forum'];
  $topic=$GLOBALS['topic'];
  $link=$GLOBALS['link'];

  $rated=common_topic_view($topic);

  $tdata=$GLOBALS['intopic'];
  $sql = "SELECT p.*, file_type, u.u__name, u.u_id FROM ".$GLOBALS['DBprefix']."Post p, ".$GLOBALS['DBprefix']."User u ".
  "LEFT JOIN ".$GLOBALS['DBprefix']."File ON (file_id=p_attach) ".
  "WHERE p.p_tid=$topic AND u.u_id=p.p_uid AND p.p__premoderate=0 ORDER BY p_id";
  $res = db_query($sql,$link);
  $pdata=db_fetch_array($res);
  news_display($tdata,$pdata,$rated);
  $numrows=db_num_rows($res);

  if ($numrows>1) {
    news_discuss_start();
    while ($nentry=db_fetch_array($res)) {
      news_discuss_entry($nentry);
    }
    news_discuss_end();
  }

  $trash['p__bcode']=$GLOBALS['inforum']['f_bcode'];
  $trash['p__smiles']=$GLOBALS['inforum']['f_smiles'];
  if ($GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lpost']) news_discuss_form($trash,MSG_n_addcoment,"do_post");
}

function do_post() {
  process_post($GLOBALS['topic']);
  if (!$GLOBALS['inforum']['f_premoderate'] || $GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lmoderate']) {
    topic_message(MSG_n_commentadded,1);
  }
  else {
    message(MSG_t_premoderated,1);
  }
}

function add_news() {
  $pdata['p__bcode']=$GLOBALS['inforum']['f_bcode'];
  $pdata['p__smiles']=$GLOBALS['inforum']['f_smiles'];
  news_edit_form($tdata,$pdata,"do_topic",MSG_n_adding);
}

function do_topic() {
  if ($GLOBALS['inuserlevel']<$GLOBALS['inforum']['f_ltopic']) error(MSG_e_t_norights);
  if (!getvar("t_title")) error(MSG_e_n_emptytitle);
  if ($GLOBALS['inforum']['f_status']!=0) error(MSG_e_f_closed);
  $forum=$GLOBALS['forum'];
  $link=$GLOBALS['link'];

  $sqldata = build_sql("t_");
  $sqldata.= check_topic_params();

  $sql = "INSERT INTO ".$GLOBALS['DBprefix']."Topic SET t_fid=$forum, $sqldata";
  $res = db_query($sql,$link);
  $tid = db_insert_id($res);

  $pid=process_post($tid);

  topic_increment($forum,$tid,$pid);

  if (!$GLOBALS['inforum']['f_premoderate'] || $GLOBALS['inuserlevel']>=$GLOBALS['inforum']['f_lmoderate']) {
    $GLOBALS['topic']=$tid;
    topic_message(MSG_n_added,1);
  }
  else {
    $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
    message(MSG_t_premoderated,1);
  }
}

function edit_news() {
  $link=$GLOBALS['link'];
  $tid=$GLOBALS['topic'];
  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic WHERE t_id=$tid";
  $res = db_query($sql,$link);
  $tdata = db_fetch_array($res);
  db_free_result($res);

  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=$tid";
  $res = db_query($sql,$link);
  $pdata=db_fetch_array($res);
  db_free_result($res);

  news_edit_form($tdata,$pdata,"do_edit",MSG_n_edit);
}

function do_edit() {
  if ($inuserlevel<$inforum['f_lmoderate']) error(MSG_e_t_norights);
  if (!getvar("t_title")) error(MSG_e_n_emptytitle);
  if (!getvar("p_text")) error(MSG_e_n_emptytext);
  $forum=$GLOBALS['forum'];
  $link=$GLOBALS['link'];

  $sqldata = build_sql("t_");
  $sql = "UPDATE ".$GLOBALS['DBprefix']."Topic SET $sqldata WHERE t_id=".$GLOBALS['topic'];
  $res = db_query($sql,$link);
  $tid = db_insert_id($res);

  $sql = "SELECT MIN(p_id) FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=".$GLOBALS['topic'];
  $res = db_query($sql,$link);
  list($pid)=db_fetch_row($res);

  $sqldata = build_sql("p_");
  $sqldata.= check_post_params();
  $sql = "UPDATE ".$GLOBALS['DBprefix']."Post SET $sqldata WHERE p_id=\"$pid\" AND p_tid=".$GLOBALS['topic'];
  $res = db_query($sql,$link);

  if (getvar("delattach") && $pdata['p_attach']!=0) {
    $sqldata.=", p_attach=0";
    $sql = "DELETE FROM ".$GLOBALS['DBprefix']."File WHERE file_id=".$pdata['p_attach'];
    $res = db_query($sql,$link);
  }

  topic_message(MSG_n_saved,1);
}

function do_delete_news() {
  if ($inuserlevel<$inforum['f_lmoderate']) error(MSG_e_t_norights);
  $tid=$GLOBALS['topic'];
  delete_topic($tid);
  $GLOBALS['refpage']="index.php?f=".$GLOBALS['forum'];
  forum_resync($GLOBALS['forum']);
  message(MSG_t_deleted);
}

function do_print() {
  $link=$GLOBALS['link'];
  $tid=$GLOBALS['topic'];
  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Topic WHERE t_id=$tid";
  $res = db_query($sql,$link);
  $tdata = db_fetch_array($res);
  db_free_result($res);

  $sql = "SELECT * FROM ".$GLOBALS['DBprefix']."Post WHERE p_tid=$tid";
  $res = db_query($sql,$link);
  $pdata=db_fetch_array($res);
  db_free_result($res);

  news_print_form($tdata,$pdata);
}

function delete_confirm() {
  $params['t']=$GLOBALS['topic'];
  confirm("news","do_delete_news",$params,MSG_n_deleteconfirm." ".$GLOBALS['intopic']['t_title'],"index.php?t=".$GLOBALS['topic']);
}

function edit_comment() {
  edit_post("news_discuss_form",MSG_n_editcoment,"do_edit_post");
}

function news_locations($locations) {
  push_parents($locations,$GLOBALS['inforum']['f_parent']);
  if ($GLOBALS['action']!="add_news") {
    if ($GLOBALS['topic']) {
      array_push($locations,"<a href=\"index.php?f=".$GLOBALS['forum']."\">".$GLOBALS['inforum']['f_title']."</a>");
      if ($GLOBALS['action']=='news_view') array_push($locations,$GLOBALS['intopic']['t_title']);
      else array_push($locations,"<a href=\"index.php?t=".$GLOBALS['intopic']['t_title']."\">".$GLOBALS['intopic']['t_title']."</a>");
    }
    else {
      array_push($locations,$GLOBALS['inforum']['f_title']);
    }
  }
  if ($GLOBALS['action']=="edit_news") {
    array_push($locations,MSG_n_edit);
  }
  elseif ($GLOBALS['action']=="edit_comment") {
    array_push($locations,MSG_n_editcomment);
  }
  elseif ($GLOBALS['action']=="add_news") {
    array_push($locations,MSG_n_adding);
  }
  return $locations;
}