<?php
(!defined('IN_PowerBB')) ? die() : '';
include('common.php');
define('CLASS_NAME','PowerBBIndexMOD');
class PowerBBIndexMOD
{
	function run()
	{
		// Who can live without $PowerBB ? ;)
		global $PowerBB;

		//$cache = $PowerBB->section->UpdateSectionsCache(array('parent'=>1));

		/**
		 * Show header
		 */
		$PowerBB->functions->ShowHeader();

		/**
		 * Firstly we get sections list
		 */
		$this->_GetSections();

		/**
		 * Get who are online
		 */
		$this->_GetOnline();

		/**
		 * Now we get 'Who visit site today'
		 */
		$this->_GetToday();

		/**
		 * Show main template
		 */
		$this->_GetFastStatic();

		/**
		 * Show main template
		 */
		$this->_CallTemplate();

		/**
		 * Show footer
		 */
		$PowerBB->functions->GetFooter();
	}

	/**
	 * Get sections list from cache and show it.
	 */
	function _GetSections()
	{
		global $PowerBB;

		//////////

		$PowerBB->_CONF['template']['foreach']['forums_list'] = array();

		$PowerBB->functions->_GetSections($PowerBB->_CONF['template']['foreach']['forums_list']);

		//////////
	}

	function _GetOnline()
	{
		global $PowerBB;

		//////////

		$GuestNumberArr 						= 	array();
		$GuestNumberArr['where'] 				= 	array();

		$GuestNumberArr['where'][0] 			= 	array();
		$GuestNumberArr['where'][0]['name'] 	= 	'username';
		$GuestNumberArr['where'][0]['oper'] 	= 	'=';
		$GuestNumberArr['where'][0]['value'] 	= 	'Guest';
		$GuestNumberArr['where'][1] 			= 	array();
		$GuestNumberArr['where'][1]['con'] 	    = 	'AND';
		$GuestNumberArr['where'][1]['name'] 	= 	'is_bot<>1 AND hide_browse<>1 AND user_id';
		$GuestNumberArr['where'][1]['oper'] 	= 	'<>';
		$GuestNumberArr['where'][1]['value'] 	= 	'';

		$GuestNumberArr['order'] 					= 	array();
		$GuestNumberArr['order']['field'] 		= 	'username';
		$GuestNumberArr['order']['type'] 			= 	'ASC';

		$PowerBB->_CONF['template']['GuestNumber'] = $PowerBB->online->GetOnlineNumber($GuestNumberArr);

		//////////

		$MemberNumberArr 						= 	array();
		$MemberNumberArr['where'] 				= 	array();

		$MemberNumberArr['where'][0] 			= 	array();
		$MemberNumberArr['where'][0]['name'] 	= 	'username';
		$MemberNumberArr['where'][0]['oper'] 	= 	'<>';
		$MemberNumberArr['where'][0]['value'] 	= 	'Guest';
		$MemberNumberArr['where'][1] 			= 	array();
		$MemberNumberArr['where'][1]['con'] 	    = 	'AND';
		$MemberNumberArr['where'][1]['name'] 	= 	'hide_browse';
		$MemberNumberArr['where'][1]['oper'] 	= 	'<>';
		$MemberNumberArr['where'][1]['value'] 	= 	'1';


		$PowerBB->_CONF['template']['MemberNumber'] = $PowerBB->online->GetOnlineNumber($MemberNumberArr);


		// The largest number of users And Guests ever online in one moment
		$MemAndGstNumber = $PowerBB->_CONF['template']['MemberNumber']+$PowerBB->_CONF['template']['GuestNumber'];
		if ( $MemAndGstNumber > $PowerBB->_CONF['info_row']['max_online'] )
		{
        $date= date($PowerBB->_CONF['info_row']['datesystem']);
        $timestamp= time($PowerBB->_CONF['info_row']['timesystem']);
        $_time = $PowerBB->functions->time($timestamp);
        $_date = $PowerBB->functions->date($date);
        $time=$date.' , ' .$_time;

		$update = $PowerBB->info->UpdateInfo(array('value'=>$MemAndGstNumber,'var_name'=>'max_online'));
		$update = $PowerBB->info->UpdateInfo(array('value'=>$time,'var_name'=>'max_online_date'));
		}

		$PowerBB->template->assign('max_online_date',$PowerBB->_CONF['info_row']['max_online_date']);
		$PowerBB->template->assign('max_online',$PowerBB->_CONF['info_row']['max_online']);
		//////////

		$MemberNumberHideArr 						= 	array();
		$MemberNumberHideArr['where'] 				= 	array();

		$MemberNumberHideArr['where'][0] 			= 	array();
		$MemberNumberHideArr['where'][0]['name'] 	= 	'hide_browse';
		$MemberNumberHideArr['where'][0]['oper'] 	= 	'=';
		$MemberNumberHideArr['where'][0]['value'] 	= 	'1';

		$PowerBB->_CONF['template']['MemberNumberHide'] = $PowerBB->online->GetOnlineNumber($MemberNumberHideArr);

		//////////

		$SpidersNumberArr 						= 	array();
		$SpidersNumberArr['where'] 				= 	array();

		$SpidersNumberArr['where'][0] 			= 	array();
		$SpidersNumberArr['where'][0]['name'] 	= 	'is_bot';
		$SpidersNumberArr['where'][0]['oper'] 	= 	'=';
		$SpidersNumberArr['where'][0]['value'] 	= 	'1';

		$PowerBB->_CONF['template']['SpidersNumber'] = $PowerBB->online->GetOnlineNumber($SpidersNumberArr);

		$GroupArr 							= 	array();

		$GroupArr['where'] 					= 	array();
		$GroupArr['where'][0] 				= 	array();
		$GroupArr['where'][0]['name'] 		= 	'view_usernamestyle';
		$GroupArr['where'][0]['oper'] 		= 	'=';
		$GroupArr['where'][0]['value']		= 	1;

		$GroupArr['order'] 					= 	array();
		$GroupArr['order']['field'] 		= 	'group_order';
		$GroupArr['order']['type'] 			= 	'ASC';

		$GroupArr['proc']					=	array();
		$GroupArr['proc']['username_style']	=	array('method'=>'replace','search'=>'[username]','replace'=>'rows{title}','store'=>'h_title');


		$PowerBB->_CONF['template']['while']['GroupList'] = $PowerBB->group->GetGroupList($GroupArr);

		//////////
		$OnlineArr 						= 	array();
		$OnlineArr['order'] 			= 	array();
		$OnlineArr['order']['field'] 	= 	'user_id';
		$OnlineArr['order']['type'] 	= 	'DESC';


		$OnlineArr['proc'] 						= 	array();
		$OnlineArr['proc']['*'] 					= 	array('method'=>'clean','param'=>'html');
		$OnlineArr['proc']['logged'] 			= 	array('method'=>'time','store'=>'logged','type'=>$PowerBB->_CONF['info_row']['timesystem']);


		$OnlineArr['where'] = (!$PowerBB->_CONF['info_row']['show_onlineguest']
								or !$PowerBB->_CONF['rows']['group_info']['show_hidden']) ? array() : null;

		if (!$PowerBB->_CONF['info_row']['show_onlineguest'])
		{
			$OnlineArr['where'][0] 			= 	array();
			$OnlineArr['where'][0]['name'] 	= 	'username';
			$OnlineArr['where'][0]['oper'] 	= 	'<>';
			$OnlineArr['where'][0]['value'] = 	'Guest';
		}

		// This member can't see hidden member
		if (!$PowerBB->_CONF['group_info']['show_hidden'])
		{
			$OnlineArr['where'][1] 			= 	array();
			$OnlineArr['where'][1]['con'] 	= 	'AND';
			$OnlineArr['where'][1]['name'] 	= 	'hide_browse';
			$OnlineArr['where'][1]['oper'] 	= 	'<>';
			$OnlineArr['where'][1]['value'] = 	'1';
		}

		// Finally we get online list
		$PowerBB->_CONF['template']['while']['OnlineList'] = $PowerBB->online->GetOnlineList($OnlineArr);

		//////////

	}

  function _GetToday()
    {
       global $PowerBB;

       //////////
        if ($PowerBB->_CONF['info_row']['show_online_list_today'] == 1)
       {
       $TodayArr                    =    array();
       $TodayArr['where']              =    array();
       $TodayArr['where'][0]           =    array();

       $TodayArr['where'][0]['name']    =    'username';
       $TodayArr['where'][0]['oper']    =    '!=';
       $TodayArr['where'][0]['value']    =    'Guest';

       if (!$PowerBB->_CONF['group_info']['show_hidden'])
       {
          $TodayArr['where'][1]          =    array();
          $TodayArr['where'][1]['con']    =    'AND';
          $TodayArr['where'][1]['name']    =    'hide_browse';
          $TodayArr['where'][1]['oper']    =    '<>';
          $TodayArr['where'][1]['value']    = '1';
       }

       $TodayArr['order']             =    array();
       $TodayArr['order']['field']       =    'user_id';
       $TodayArr['order']['type']       =    'DESC';


       $PowerBB->_CONF['template']['while']['TodayList'] = $PowerBB->online->GetTodayList($TodayArr);

       //////////

     $TodayNumber = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT * FROM " . $PowerBB->table['today'] . " WHERE id"));
     $PowerBB->_CONF['template']['TodayNumber'] = $TodayNumber;
     }
       //////////
    }

   function _GetFastStatic()
    {
       global $PowerBB;

     // special topics
       $SpecialArr                        =    array();
       $SpecialArr['proc']                 =    array();
       $SpecialArr['proc']['*']              =    array('method'=>'clean','param'=>'html');

       $SpecialArr['where']                =    array();
       $SpecialArr['where'][0]             =    array();
       $SpecialArr['where'][0]['name']       =    'special';
       $SpecialArr['where'][0]['oper']       =    '=';
       $SpecialArr['where'][0]['value']       =    '1';

       $SpecialArr['order']                =    array();
       $SpecialArr['order']['field']          =    'id';
       $SpecialArr['order']['type']          =    'DESC';

       // Clean data
       $SpecialArr['proc']              =    array();
       $SpecialArr['proc']['*']           =    array('method'=>'clean','param'=>'html');
       $SpecialArr['proc']['native_write_time']    =    array('method'=>'date','store'=>'write_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);
       $SpecialArr['proc']['write_time']           =    array('method'=>'date','store'=>'reply_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);


       $PowerBB->_CONF['template']['while']['Special_subjectList'] = $PowerBB->subject->GetSubjectList($SpecialArr);

          if ($PowerBB->_CONF['template']['while']['Special_subjectList'] != false)
          {
             $PowerBB->template->assign('Special_Show',true);

          }
          else
          {
             $PowerBB->template->assign('Special_Show',false);
          }


       ////
      if ($PowerBB->_CONF['info_row']['active_static'] == 1)
       {
	      $lastmember = array();
	       $lastmember['order']        = array();
	       $lastmember['order']['field'] = 'id';
	       $lastmember['order']['type']  = 'DESC';
	       $lastmember['limit']        = '1';

	       $lm = $PowerBB->member->GetMemberInfo($lastmember);

	       $PowerBB->template->assign('lm',$lm);

	       $arr                = array();
	       $arr['get_from']      = 'db';

	       $mn = $PowerBB->member->GetMemberNumber($arr);

	       $PowerBB->template->assign('mn',$mn);

	       $arrr            = array();
	       $arrr['get_from']  = 'db';
	       $arrr['where']    = array('delete_topic',0);

	       $sn = $PowerBB->subject->GetSubjectNumber($arrr);

	       $PowerBB->template->assign('sn',$sn);

	       $arrrr            = array();
	       $arrrr['get_from']  = 'db';
	       $arrrr['where']    = array('delete_topic',0);

	       $rn = $PowerBB->reply->GetReplyNumber($arrrr);

	       $PowerBB->template->assign('rn',$rn);

      }
        ////////////// Get Statistics
     if ($PowerBB->_CONF['info_row']['activate_last_static_list'] == 1)
       {

        $limit = $PowerBB->_CONF['info_row']['last_static_num'];
        $limitLastPost = $PowerBB->_CONF['info_row']['last_posts_static_num'];
       // Latest registered Member list
       $StaticInfo = array();
       $StaticInfo['order']        = array();
       $StaticInfo['order']['field'] = 'id';
       $StaticInfo['order']['type']  = 'DESC';
       $StaticInfo['limit']        = $limit;


       $StaticInfo['proc']                    =    array();
       // Ok Mr.** go to hell !
       $StaticInfo['proc']['*']                 =    array('method'=>'clean','param'=>'html');
       $StaticInfo['proc']['register_date']           =    array('method'=>'date','store'=>'register_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);

       $PowerBB->_CONF['template']['while']['latest_registered'] = $PowerBB->member->GetMemberList($StaticInfo);

       /**
        * Get top ten list of subjects which have big visitors
        */

       $TopSubjectVisitorNum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query('SELECT * FROM '.$PowerBB->table['subject'].' WHERE review_subject<>1 AND sec_subject<>1 AND delete_topic <> 1 AND visitor != 0'));

       $TopSubjectVisitorArr                        =    array();
       // Order data
       $TopSubjectVisitorArr['order']              =    array();
       $TopSubjectVisitorArr['order']['field']    =    'visitor';
       $TopSubjectVisitorArr['order']['type']        =    'DESC';

       // Ten rows only
       $TopSubjectVisitorArr['limit']             =    $limit;

       $TopSubjectVisitorArr['where'][0]           =    array();
       $TopSubjectVisitorArr['where'][0]['name']    =    'review_subject<>1 AND sec_subject<>1 AND delete_topic';

       $TopSubjectVisitorArr['where'][0]['oper']    =    '<>';
       $TopSubjectVisitorArr['where'][0]['value']    =    '1';

       $TopSubjectVisitorArr['where'][1]['con']    =    ' AND';
       $TopSubjectVisitorArr['where'][1]['name']    =    ' visitor';
       $TopSubjectVisitorArr['where'][1]['oper']    =    '!=';
       $TopSubjectVisitorArr['where'][1]['value']    =    '0';

       // Clean data
       $TopSubjectVisitorArr['proc']              =    array();
       $TopSubjectVisitorArr['proc']['*']           =    array('method'=>'clean','param'=>'html');

       $PowerBB->_CONF['template']['while']['TopSubjectVisitor'] = $PowerBB->subject->GetSubjectList($TopSubjectVisitorArr);
       $PowerBB->template->assign('TopSubjectVisitorNum',$TopSubjectVisitorNum);

       /**
        * Get top ten list of member who have big posts
        */
       $TopMemberList                    =    array();

       // Order data
       $TopMemberList['order']           =    array();
       $TopMemberList['order']['field']    =    'posts';
       $TopMemberList['order']['type']    =    'DESC';

       // Ten rows only
       $TopMemberList['limit']             =    $limit;

       //remove who thier posts is 0
       $TopMemberList['where'][0]['name']       =    'posts';
       $TopMemberList['where'][0]['oper']       =    '!=';
       $TopMemberList['where'][0]['value']       =    '0';

       // Clean data
       $TopMemberList['proc']              =    array();
       $TopMemberList['proc']['*']        =    array('method'=>'clean','param'=>'html');

       $TopMemberListNum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query('SELECT * FROM '.$PowerBB->table['member'].' WHERE posts != 0'));

       $PowerBB->_CONF['template']['while']['TopMemberList'] = $PowerBB->member->GetMemberList($TopMemberList);

       $PowerBB->template->assign('TopMemberListNum',$TopMemberListNum);


       /**
        * Get top ten list of sections who have big posts
        */

       $TopSectionNum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query('SELECT * FROM '.$PowerBB->table['section'].' WHERE parent not in (0) AND sec_section<>1 AND hide_subject <> 1 AND reply_num != 0'));
       $TopSectionsArr                    =    array();
       // Order data
       $TopSectionsArr['order']           =    array();
       $TopSectionsArr['order']['field']    =    'reply_num';
       $TopSectionsArr['order']['type']    =    'DESC';

       // Ten rows only
       $TopSectionsArr['limit']             =    $limit;

       $TopSectionsArr['where'][1]           =    array();
       $TopSectionsArr['where'][1]['con']       =    'AND';
       $TopSectionsArr['where'][1]['name']    =    'sec_section<>1 AND hide_subject <> 1 AND reply_num';
       $TopSectionsArr['where'][1]['oper']    =    '!=';
       $TopSectionsArr['where'][1]['value']    =    '0';
       // Clean data
       $TopSectionsArr['proc']              =    array();
       $TopSectionsArr['proc']['*']        =    array('method'=>'clean','param'=>'html');

       $PowerBB->_CONF['template']['while']['TopSectionsList'] = $PowerBB->section->GetSectionsList($TopSectionsArr);
       $PowerBB->template->assign('TopSectionListNum',$TopSectionNum);
          /**
        * Get last posts
        */


       $LastSubjectArr                        =    array();
       // Order data
       $LastSubjectArr['order']              =    array();
       $LastSubjectArr['order']['field']    =    'write_time';
       $LastSubjectArr['order']['type']        =    'DESC';

       // Ten rows only
       $LastSubjectArr['limit']             =    $limitLastPost;

       $LastSubjectArr['where'][1]           =    array();
       $LastSubjectArr['where'][1]['con']       =    'AND';
       $LastSubjectArr['where'][1]['name']    =    'review_subject<>1 AND sec_subject<>1 AND delete_topic';

       $LastSubjectArr['where'][1]['oper']    =    '<>';
       $LastSubjectArr['where'][1]['value']    =    '1';

       // Clean data
       $LastSubjectArr['proc']              =    array();
       $LastSubjectArr['proc']['*']           =    array('method'=>'clean','param'=>'html');
       $LastSubjectArr['proc']['native_write_time']    =    array('method'=>'date','store'=>'write_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);
       $LastSubjectArr['proc']['write_time']           =    array('method'=>'date','store'=>'reply_date','type'=>$PowerBB->_CONF['info_row']['timesystem']);
       //$LastSubjectArr['proc']['write_time']           =    array('method'=>'time','store'=>'reply_dategg','type'=>$PowerBB->_CONF['info_row']['timesystem']);



       $PowerBB->_CONF['template']['while']['LastSubject'] = $PowerBB->subject->GetSubjectList($LastSubjectArr);

        /**
        * Get top member invite
        */
       $MemberInviteArr                        =    array();

       // Order data
       $MemberInviteArr['order']              =    array();
       $MemberInviteArr['order']['field']     =    'invite_num';
       $MemberInviteArr['order']['type']        =    'DESC';

       $MemberInviteArr['limit']             =    $limit;
       $MemberInviteArr['where'][0]['name']          =    'invite_num';
       $MemberInviteArr['where'][0]['oper']          =    '!=';
       $MemberInviteArr['where'][0]['value']          =    '0';
       // Clean data
       $MemberInviteArr['proc']              =    array();
       $MemberInviteArr['proc']['*']           =    array('method'=>'clean','param'=>'html');

       $PowerBB->_CONF['template']['while']['TopMemberInvite'] = $PowerBB->member->GetMemberList($MemberInviteArr);
       $TopMemberInviteNum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query("SELECT * FROM ".$PowerBB->table['member']." WHERE invite_num "));
       $PowerBB->template->assign('TopMemberInviteNum',$TopMemberInviteNum);
       ///////////////

        /**
        * Get top member Reputation
        */
       $MemberReputationArr                        =    array();

       // Order data
       $MemberReputationArr['order']              =    array();
       $MemberReputationArr['order']['field']     =    'reputation';
       $MemberReputationArr['order']['type']        =    'DESC';

       $MemberReputationArr['limit']             =    $limit;

       $MemberReputationArr['where'][0]['name']       =    'reputation';
       $MemberReputationArr['where'][0]['oper']       =    '!=';
       $MemberReputationArr['where'][0]['value']       =    '0';

       // Clean data
       $MemberReputationArr['proc']              =    array();
       $MemberReputationArr['proc']['*']           =    array('method'=>'clean','param'=>'html');

       $MemberReputationNum = $PowerBB->DB->sql_num_rows($PowerBB->DB->sql_query('SELECT * FROM '.$PowerBB->table['member'].' WHERE reputation !=0'));
       $PowerBB->_CONF['template']['while']['TopMemberReputation'] = $PowerBB->member->GetMemberList($MemberReputationArr);
       $PowerBB->template->assign('MemberReputationNum',$MemberReputationNum);
     }

    }


	function _CallTemplate()
	{
		global $PowerBB;
		$PowerBB->template->display('main');
	}
}

?>
