<?php
// +---------------------------------------------+
// |     Copyright  2003 - 2005 weenCompany      |
// |     http://www.weentech.com                 |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
{
  die('File not found!');
}



function m8_LatestPosts($usersystem)
{
  global $DB, $weenurl, $dbname;

  $forumdbname = $usersystem['dbname'];
  $forumname   = $usersystem['name'];
  $forumpath   = $usersystem['folderpath'];
  $tableprefix = $usersystem['tblprefix'];

  // get language
  $language = GetLanguage(8);

  // get settings
  $getsettings = $DB->query("SELECT title, value FROM " . TABLE_PREFIX . "modulesettings WHERE moduleid = 8");
  while($setting = $DB->fetch_array($getsettings))
  {
    $settings[$setting['title']] = $setting['value'];
  }

  $postlimit = $settings['最新话题条目数'];
  $wordwrap  = $settings['换行字符数'];
  $forumids  = $settings['排除论坛'];

  if($forumname == 'Discuz5')
  {
    $columnid = 'fid';
  }
  else if( ($forumname == 'vBulletin 2') || ($forumname == 'vBulletin 3') )
  {
    $columnid = 'forumid';
  }
  else if($forumname == 'phpBB2')
  {
    $columnid = 'forum_id';
  }
  else if($forumname == 'Invision Power Board 2')
  {
    $columnid = 'forum_id';
  }
  else if($forumname == 'Simple Machines Forum 1')
  {
    $columnid = 'ID_BOARD';
  }

  if(strlen($forumids) > 0)
  {
    // get rid of commas (maybe user entered commas)
    // get rid of extra spaces
    // then trim
    $forumids = str_replace(',', ' ', $forumids);
    $forumids = preg_replace("/[[:space:]]+/", " ", $forumids);
    $forumids = trim($forumids);

    // obtain privateids
    $privateids = explode(" ", $forumids);

    for($i = 0; $i < count($privateids); $i++)
    {
      if($i == 0)
      {
        $extraquery = 'WHERE ' . $columnid . ' != ' . $privateids[$i];
      }
      else
      {
        $extraquery .= ' AND ' . $columnid . ' != ' . $privateids[$i];
      }
    }
  }



  // switch to forum database
  if($dbname != $forumdbname)
  {
    $DB->select_db($forumdbname);
  }

  echo '<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
        <tr>
          <td>';

  if($forumname == 'Discuz5')
  {
    $gettopics = $DB->query("SELECT tid, author, subject FROM ".$tableprefix."threads ".$extraquery." ORDER BY tid DESC LIMIT $postlimit");

    while($topic = $DB->fetch_array($gettopics))
    {
      $thread['topic_title'] = cws_wordwrap($topic['subject'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'viewthread.php?tid=' . $topic['tid'] . '">' . $thread['topic_title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $topic['author'] . '<br /><br />';
    }

  }
  else if($forumname == 'vBulletin 2')
  {
    $getthreads = $DB->query("SELECT title, threadid, lastposter FROM ".$tableprefix."thread ".$extraquery." ORDER BY lastpost DESC LIMIT $postlimit");

    while($thread = $DB->fetch_array($getthreads))
    {
      $thread['title']      = cws_wordwrap($thread['title'], $wordwrap, "<br />", 1);
      $thread['lastposter'] = cws_wordwrap($thread['lastposter'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'showthread.php?threadid=' . $thread['threadid'] . '&goto=lastpost">' . $thread['title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $thread['lastposter'] . '<br /><br />';
    }
  }
  
  else if($forumname == 'vBulletin 3')
  {
    $extraquery = strlen($extraquery) ? $extraquery . ' AND visible = 1 ' : ' WHERE visible = 1 ';

    $getthreads = $DB->query("SELECT title, threadid, lastposter FROM ".$tableprefix."thread ".$extraquery." ORDER BY lastpost DESC LIMIT $postlimit");

    while($thread = $DB->fetch_array($getthreads))
    {
      $thread['title']      = cws_wordwrap($thread['title'], $wordwrap, "<br />", 1);
      $thread['lastposter'] = cws_wordwrap($thread['lastposter'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'showthread.php?threadid=' . $thread['threadid'] . '&goto=lastpost">' . $thread['title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $thread['lastposter'] . '<br /><br />';
    }
  }
  else if($forumname == 'phpBB2')
  {
    $gettopics = $DB->query("SELECT topic_id, topic_title, topic_poster FROM ".$tableprefix."topics ".$extraquery." ORDER BY topic_id DESC LIMIT $postlimit");

    while($topic = $DB->fetch_array($gettopics))
    {
      $lastposter = $DB->query_first("SELECT username FROM ".$tableprefix."users WHERE user_id = '".$topic['topic_poster']."' ");
      $post['post_username'] = $lastposter['username'];

      $thread['topic_title'] = cws_wordwrap($topic['topic_title'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'viewtopic.php?t=' . $topic['topic_id'] . '">' . $thread['topic_title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $post['post_username'] . '<br /><br />';
    }
  }
  else if($forumname == 'Invision Power Board 2')
  {
    $getthreads = $DB->query("SELECT title, tid, last_poster_name FROM " . $tableprefix . "topics " . $extraquery . " ORDER BY last_post DESC LIMIT $postlimit");

    while($thread = $DB->fetch_array($getthreads))
    {
      $thread['title']            = cws_wordwrap($thread['title'], $wordwrap, "<br />", 1);
      $thread['last_poster_name'] = cws_wordwrap($thread['last_poster_name'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'index.php?showtopic=' . $thread['tid'] . '">' . $thread['title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $thread['last_poster_name'] . '<br /><br />';
    }
  }
  else if($forumname == 'Simple Machines Forum 1')
  {
    $getthreads = $DB->query("SELECT subject, ID_MSG, ID_TOPIC, posterName FROM " . $tableprefix . "messages " . $extraquery . " ORDER BY posterTime DESC LIMIT $postlimit");

    while($thread = $DB->fetch_array($getthreads))
    {
      $thread['title']            = cws_wordwrap($thread['subject'], $wordwrap, "<br />", 1);
      $thread['last_poster_name'] = cws_wordwrap($thread['posterName'], $wordwrap, "<br />", 1);

      echo '<a href="' . $weenurl . $forumpath . 'index.php?topic=' . $thread['ID_TOPIC'] . '.msg' . $thread['ID_MSG'] . '#msg' . $thread['ID_MSG'] . '">' . $thread['title'] . '</a>
            <br />' . $language['posted_by'] . ' ' . $thread['last_poster_name'] . '<br /><br />';
    }
  }


  echo '  </td>
        </tr>
        </table>';

  // switch back to weencompany database
  if($dbname != $forumdbname)
  {
    $DB->select_db($dbname);
  }

}


if($usersystem)
{
  if($usersystem['name'] != 'weenCompany')
  {
    m8_LatestPosts($usersystem);
  }
  else
  {
    echo '此模块是基于论坛而设置的, 需要安装论坛同时实现网站与论坛接口后方能使用!';
  }
}
else
{
  echo '网站用户系统已关闭, 此模块无法正常使用!';
}



?>