<?php

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

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

  $pmpopupurl = '';

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

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

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

  $showpopups = $settings['短信通知'];

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

  if($forumname == 'Discuz5')
  {
    $sql = "SELECT COUNT(pmid) AS pmtotal,
            SUM(IF(new = 1, 1, 0)) AS pmunread
            FROM " . $tableprefix . "pms
            WHERE msgtoid='".$userinfo['userid']."'";

    $pmcount = $DB->query_first($sql);
    $pmtotal = intval($pmcount['pmtotal']);
    $pmunread = intval($pmcount['pmunread']);
    $pmurl = $forumpath . 'pm.php';

    if($showpopups > 0 && $pmunread > 0)
    {
      $sql = "SELECT pmid, subject, msgfrom
            FROM " . $tableprefix . "pms
            WHERE msgtoid  = ".$userinfo['userid']."
            AND new = 1
			AND folder = 'inbox'
            ORDER BY dateline DESC
            LIMIT 1";

      $newpm = $DB->query_first($sql);

      $pmtitle    = html_entity_decode($newpm['subject']);
      $pmfromuser = $newpm['msgfrom'];
      $pmpopupurl = $forumpath . 'pm.php?action=view&folder=inbox&mode=read&pmid=' . $newpm['pmid'];
    }

  }
  
  else if($forumname == 'vBulletin 2')
  {
    // VB lets the user choose whether to 彈出短信
    $pmpopup = $DB->query_first("SELECT pmpopup FROM " . $tableprefix . "user WHERE userid = ".$userinfo['userid']);

    // Only allow user to override if they are enabled globally
    if($showpopups > 0)
      $showpopups = $pmpopup[0];

    $pmcount = $DB->query_first("
            SELECT
            COUNT(privatemessageid) AS pmtotal,
            SUM(IF(messageread = 0 AND folderid = 0, 1, 0)) AS pmunread
            FROM " . $tableprefix . "privatemessage AS pm
            WHERE userid = ".$userinfo['userid']);

    $pmtotal = intval($pmcount['pmtotal']);
    $pmunread = intval($pmcount['pmunread']);
    $pmurl = $forumpath . 'private.php';

    if($pmunread > 0)
    {
      $newpm = $DB->query_first("
            SELECT pm.privatemessageid, title, u.username
            FROM " . $tableprefix . "privatemessage As pm
            LEFT JOIN " . $tableprefix . "user AS u ON u.userid = pm.fromuserid
            WHERE pm.userid = ".$userinfo['userid']."
            AND pm.messageread = 0
            ORDER BY dateline DESC
            LIMIT 1
        ");

      $pmtitle    = html_entity_decode($newpm['title']);
      $pmfromuser = $newpm['username'];
      $pmpopupurl = $forumpath . 'private.php?action=show&privatemessageid=' . $newpm['privatemessageid'];
    }

  }
  else if ($forumname == 'vBulletin 3')
  {
    // VB lets the user choose whether to 短信通知
    $pmpopup = $DB->query_first("SELECT pmpopup FROM " . $tableprefix . "user WHERE userid = ".$userinfo['userid']);

    // Only allow user to override if they are enabled globally
    if($showpopups > 0)
      $showpopups = $pmpopup[0];

    $pmcount = $DB->query_first("
            SELECT
            COUNT(pmid) AS pmtotal,
            SUM(IF(messageread = 0 AND folderid = 0, 1, 0)) AS pmunread
            FROM " . $tableprefix . "pm AS pm
            WHERE userid = ".$userinfo['userid']);

    $pmtotal = intval($pmcount['pmtotal']);
    $pmunread = intval($pmcount['pmunread']);
    $pmurl = $forumpath . 'private.php';

    if($pmunread > 0)
    {
      $newpm = $DB->query_first("
            SELECT pm.pmid, title, fromusername
            FROM " . $tableprefix . "pmtext AS pmtext
            LEFT JOIN " . $tableprefix . "pm AS pm USING(pmtextid)
            WHERE pm.userid = ".$userinfo['userid']."
            AND pm.messageread = 0
            ORDER BY dateline DESC
            LIMIT 1
        ");

      $pmtitle    = html_entity_decode($newpm['title']);
      $pmfromuser = $newpm['fromusername'];
      $pmpopupurl = $forumpath . 'private.php?do=showpm&pmid=' . $newpm['pmid'];
    }
  }
  else if($forumname == 'phpBB2')
  {
    $sql = "SELECT COUNT(privmsgs_id) AS pmtotal,
            SUM(IF(privmsgs_type = 5 OR privmsgs_type = 1, 1, 0)) AS pmunread
            FROM " . $tableprefix . "privmsgs
            WHERE privmsgs_to_userid='".$userinfo['userid']."' AND privmsgs_type <> 4 AND privmsgs_type <> 2";

    $pmcount = $DB->query_first($sql);
    $pmtotal = intval($pmcount['pmtotal']);
    $pmunread = intval($pmcount['pmunread']);
    $pmurl = $forumpath . 'privmsg.php';

    if($showpopups > 0 && $pmunread > 0)
    {
      $sql = "SELECT pm.privmsgs_id, pm.privmsgs_subject, u.username
            FROM " . $tableprefix . "privmsgs AS pm
            LEFT JOIN " . $tableprefix . "users As u ON u.user_id = pm.privmsgs_from_userid
            WHERE pm.privmsgs_to_userid  = ".$userinfo['userid']."
            AND (pm.privmsgs_type = 1 OR pm.privmsgs_type = 5)
            ORDER BY pm.privmsgs_date DESC
            LIMIT 1";

      $newpm = $DB->query_first($sql);

      $pmtitle    = html_entity_decode($newpm['privmsgs_subject']);
      $pmfromuser = $newpm['username'];
      $pmpopupurl = $forumpath . 'privmsg.php?folder=inbox&mode=read&p=' . $newpm['privmsgs_id'];
    }
  }
  else if($forumname == 'Invision Power Board 2')
  {
    $sql = "SELECT COUNT(mt_id) AS pmtotal,
            SUM(IF(mt_user_read = 0, 1, 0)) AS pmunread
            FROM " . $tableprefix . "message_topics
            WHERE mt_vid_folder = 'in' AND mt_to_id='".$userinfo['userid']."'";

    $pmcount = $DB->query_first($sql);
    $pmtotal = intval($pmcount['pmtotal']);
    $pmunread = intval($pmcount['pmunread']);
    $pmurl = $forumpath . 'index.php?act=Msg&CODE=01';

    if($showpopups > 0 && $pmunread > 0)
    {
      $sql = "
            SELECT pm.mt_id, pm.mt_title, m.name
            FROM " . $tableprefix . "message_text AS pmtext
            LEFT JOIN " . $tableprefix . "message_topics AS pm ON pm.mt_msg_id = pmtext.msg_id
            LEFT JOIN " . $tableprefix . "members As m ON m.id = pm.mt_from_id
            WHERE pm.mt_to_id = ".$userinfo['userid']."
            AND pm.mt_user_read = 0
                        AND mt_vid_folder = 'in'
            ORDER BY pm.mt_date DESC
            LIMIT 1
        ";

      $newpm = $DB->query_first($sql);

      $pmtitle    = html_entity_decode($newpm['mt_title']);
      $pmfromuser = $newpm['name'];
      $pmpopupurl = $forumpath . 'index.php?act=Msg&CODE=03&VID=in&MSID='.$newpm['mt_id'].'';
    }
  }
  else if($forumname == 'Simple Machines Forum 1')
  {
    // check which version of smf is the user running
    $smfversion = $DB->query_first("SELECT value FROM " . $tableprefix . "settings WHERE variable = 'smfVersion'");

    if(substr($smfversion['value'], 0, 3) == '1.1')
    {
      $sql = "SELECT COUNT(ID_PM) AS pmtotal,
            SUM(IF(is_read = 0, 1, 0)) AS pmunread
            FROM " . $tableprefix . "pm_recipients
            WHERE ID_MEMBER='".$userinfo['userid']."' AND deleted = 0"; /* AND bcc = 0 */

      $pmcount = $DB->query_first($sql);
      $pmtotal = intval($pmcount['pmtotal']);
      $pmunread = intval($pmcount['pmunread']);
      $pmurl = $forumpath . 'index.php?action=pm';

      if($showpopups > 0 && $pmunread > 0)
      {
        $sql = "SELECT pm.ID_PM, pmtext.subject, pmtext.fromName
              FROM " . $tableprefix . "personal_messages AS pmtext
              LEFT JOIN " . $tableprefix . "pm_recipients AS pm ON pm.ID_PM = pmtext.ID_PM
              WHERE pm.ID_MEMBER='".$userinfo['userid']."'
              AND pm.deleted = 0
                          AND pm.is_read = 0
              ORDER BY pmtext.msgtime DESC
              LIMIT 1";
        $newpm = $DB->query_first($sql);

        $pmtitle    = html_entity_decode($newpm['subject']);
        $pmfromuser = $newpm['fromName'];
        $pmpopupurl = $forumpath . 'index.php?action=pm;f=inbox#'.$newpm['ID_PM'];
      }
    }
    else
    {
      $sql = "SELECT COUNT(ID_PM) AS pmtotal,
            SUM(IF(is_read = 0, 1, 0)) AS pmunread
            FROM " . $tableprefix . "im_recipients
            WHERE ID_MEMBER='".$userinfo['userid']."' AND deleted = 0"; /* AND bcc = 0 */

      $pmcount = $DB->query_first($sql);
      $pmtotal = intval($pmcount['pmtotal']);
      $pmunread = intval($pmcount['pmunread']);
      $pmurl = $forumpath . 'index.php?action=pm';

      if($showpopups > 0 && $pmunread > 0)
      {
        $sql = "SELECT pm.ID_PM, pmtext.subject, pmtext.fromName
              FROM " . $tableprefix . "instant_messages AS pmtext
              LEFT JOIN " . $tableprefix . "im_recipients AS pm ON pm.ID_PM = pmtext.ID_PM
              WHERE pm.ID_MEMBER='".$userinfo['userid']."'
              AND pm.deleted = 0
                          AND pm.is_read = 0
              ORDER BY pmtext.msgtime DESC
              LIMIT 1";
        $newpm = $DB->query_first($sql);

        $pmtitle    = html_entity_decode($newpm['subject']);
        $pmfromuser = $newpm['fromName'];
        $pmpopupurl = $forumpath . 'index.php?action=pm;f=inbox#'.$newpm['ID_PM'];
      }
    }
  }

  echo '<a href="'.$pmurl.'">'.$language['private_messages'].'</a><br />'.iif($pmunread> 0, '<b>'.$pmunread.'</b>', $pmunread) . ' ' . $language['unread_total'] . ' ' . $pmtotal.'.';

  if(strlen($pmpopupurl) > 0)
  {
    echo '<script type="text/javascript">
<!--
  function NotifyMsg()
  {
    if (confirm("'.$language['new_priv_msg'].'\r\n\r\n'.$language['sender'].' ' . $pmfromuser .'\r\n'.$language['title'].' \''.$pmtitle.'\'\r\n\r\n'.$language['click_ok'].'"))
    {
        window.location = "'.$pmpopupurl.'";
    }
  }

  window.onload = NotifyMsg;
//-->
</script>';
  }

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

if($usersystem)
{
  if($usersystem['name'] != 'weenCompany')
  {
    m10_DisplayPMs($usersystem);
  }
}

?>
