<?php
//
//Announcement Mod v1.0 by Manick (IBRESOURCE.COM)
//
//announce.php,v1.0

/*

Announcement Module 1.0.  I offer NO support, unless I feel like it. ;)

by Manick (IBRESOURCE.COM)


NOTE:  This is a *HACKED* to hell version of 'Topics.php'.

*/


$idx = new Announcements;

class Announcements {

    var $output    = "";
    var $base_url  = "";
    var $html      = "";
    var $forum     	= array();
    var $category  	= array();
    var $announcement 	= array();



    var $mem_titles = array();
    var $mod_action = array();
    var $poll_html  = "";
    var $colspan    = 0;
    var $parser     = "";
    var $mimetypes  = "";
    var $nav_extra  = "";
    var $read_array = array();
    var $sigs_shown = ",";
    
    /***********************************************************************************/
	//
	// Our constructor, load words, load skin, print the announcement listing
	//
	/***********************************************************************************/
    
    function Announcements() {
    
    
        global $ibforums, $DB, $std, $print, $skin_universal;
        
        require "./Skin/".$ibforums->skin_id."/skin_announce.php";

        //-------------------------------------
		// Compile the language file
		//-------------------------------------

        $ibforums->lang = $std->load_words($ibforums->lang, 'lang_announce', $ibforums->lang_id);
		

        $this->html     = new skin_announce();
        
        $this->base_url = "{$ibforums->vars['board_url']}/index.{$ibforums->vars['php_ext']}?s={$ibforums->session_id}";

        //-------------------------------------
        // Check the input
        //-------------------------------------
        
        $ibforums->input['a'] = $std->is_number($ibforums->input['a']);
        
	if ( ($ibforums->input['a'] < 0)  )
	{
			$std->Error( array( LEVEL => 1, MSG => 'missing_files') );
	}


	//-------------------------------------
 	// Get all the member groups and
 	// member title info
 	//-------------------------------------
        
        $DB->query("SELECT id, title, pips, posts from ibf_titles ORDER BY posts DESC");
        while ($i = $DB->fetch_row())
        {
         	$this->mem_titles[ $i['id'] ] = array(
												 'TITLE' => $i['title'],
												 'PIPS'  => $i['pips'],
												 'POSTS' => $i['posts'],
											   );
        }
        
        //-------------------------------------
        // Get the forum info based on the forum ID, get the category name, ID, and get the announcement details
        //-------------------------------------
        
	$time_now = time();


        $DB->query(	"SELECT a.*,".
			"m.id,m.name,m.mgroup,m.email,m.joined,m.avatar,m.avatar_size,m.posts,m.aim_name,m.icq_number,m.signature, m.website,m.yahoo,m.title,m.hide_email,m.msnname, ".
			"g.g_id, g.g_title, g.g_icon ".
			"FROM ibf_announcements a, ibf_members m, ibf_groups g ".
			"where a.a_id='".$ibforums->input[a]."' and a.a_mid=m.id AND g.g_id=m.mgroup AND ((a.a_start < '".$time_now."' AND a.a_end > '".$time_now."') OR a.a_expire = '0') ");


		if ( ! $DB->get_num_rows() )
		{
			$std->Error( array( LEVEL => 1, MSG => 'missing_files') );
		}



	$this->announcement = $DB->fetch_row();

	if ($this->announcement[a_forum])
	{
	$DB->query(	"SELECT f.name as forum_name, f.id as forum_id, f.read_perms, f.reply_perms, f.parent_id, f.password, c.name as cat_name, c.id as cat_id FROM ibf_forums f , ibf_categories c ".
			"WHERE f.id = '".$this->announcement[a_forum]."' and f.category=c.id");

		if ( ! $DB->get_num_rows() )
		{
			$std->Error( array( LEVEL => 1, MSG => 'missing_files') );
		}

	$stuff = $DB->fetch_row(); 


        $this->forum = array( 'id'           =>   $stuff[forum_id]          ,
        					  'name'         => $stuff[forum_name]	 ,
        					  'read_perms'   => $stuff[read_perms] 	 ,
        					  'parent_id'    => $stuff[parent_id]	 ,
        					  'password'     => $stuff[password]	 ,
        					);

	$this->forum['where']	=	"&act=SF&f=".$stuff['forum_id'];


 
        $this->category = array( 'name'   =>  $stuff[cat_name],
        						 'id'     =>  $stuff[cat_id]  ,
        				       );


	$nav_array	=	array( 
        					 	  					   "<a href='".$this->base_url."&act=SC&c={$this->category['id']}'>{$this->category['name']}</a>",
        					 	  					   $this->nav_extra,
        					 	  					   "<a href='".$this->base_url."&act=SF&f={$this->forum['id']}'>{$this->forum['name']}</a>",
												   $this->nav_extra,
												   "<a href='".$this->base_url."&act=SA&a={$this->announcement['a_id']}'>{$ibforums->lang['announce_bar']}</a>"

        					 	  					 );



	}
	else
	{

        $this->forum = array( 'id'           =>   '0'          ,
        					  'name'         => "ÂÛÌ³Ê×Ò³"	 ,
        					  'read_perms'   => "*"  	      	 ,
        					  'parent_id'    => "-1"        	 ,
        					  'password'     => ""			 ,
        					);
        					
	$this->forum['where']	=	"";


	$nav_array	=	array( 
        					 	  					   
        					 	  					   
        					 	  					   
												   $this->nav_extra,
												   "<a href='".$this->base_url."&act=SA&a={$this->announcement['a_id']}'>{$ibforums->lang['announce_bar']}</a>"

        					 	  					 );



        $this->category = array( 'name'   =>  "",
        						 'id'     =>  '0'  ,
        				       );



	}
      				       
        
        //-------------------------------------
        // Error out if we can not find the forum
        //-------------------------------------
        
        if ($this->forum['id'] < '0')
        {
        	$std->Error( array( LEVEL => 1, MSG => 'missing_files') );
        }
        
        //-------------------------------------
        // Error out if we can not find the announcement
        //-------------------------------------
        
        if (!$this->announcement['a_id'])
        {
        	$std->Error( array( LEVEL => 1, MSG => 'missing_files') );
        }

        if ( $read = $std->my_getcookie('topicsread') )
        {
        	$this->read_array = unserialize(stripslashes($read));
        	
        	if (! is_array($this->read_array) )
        	{
        		$this->read_array = array();
        	}
        }

        
        
		$this->forum['JUMP'] = $std->build_forum_jump();
		$this->forum['JUMP'] = preg_replace( "!#Forum Jump#!", $ibforums->lang['forum_jump'], $this->forum['JUMP']);
        
        //-------------------------------------
        // Check viewing permissions, private forums,
        // password forums, etc
        //-------------------------------------
        
       
        $bad_entry = $this->check_access();
        
        if ($bad_entry == 1)
        {
        	$std->Error( array( LEVEL => 1, MSG => 'no_view_announcement') );
        }      
        
        //-------------------------------------
        // Update the topic views counter
        //-------------------------------------
        
        $DB->query("UPDATE ibf_announcements SET a_views=a_views+1 WHERE a_id='".$this->announcement['a_id']."'");

        //-------------------------------------
        // Update the topic read cookie
        //-------------------------------------
        
        if ($ibforums->member['id'])
        {
			$this->read_array['ann_' . $this->announcement['a_id']] = time();
			
			$std->my_setcookie('topicsread', serialize($this->read_array), -1 );
        }
        
        
        //----------------------------------------
        // If this is a sub forum, we need to get
        // the cat details, and parent details
        //----------------------------------------
        
        if ($this->forum['parent_id'] > 0)
        {
        
        	$DB->query("SELECT f.id as forum_id, f.name as forum_name, c.id, c.name FROM ibf_forums f, ibf_categories c WHERE f.id='".$this->forum['parent_id']."' AND c.id=f.category");
        	
        	$row = $DB->fetch_row();
        	
        	$this->category['id']   = $row['id'];
        	$this->category['name'] = $row['name'];
        
        	$this->nav_extra = "<a href='".$this->base_url."&act=SF&f={$row['forum_id']}'>{$row['forum_name']}</a>";
        }
        
        

					   
		//-------------------------------------
		// Fix up some of the words
		//-------------------------------------
		

		$this->announcement['START_DATE'] = $std->get_date( $this->announcement['a_start'], 'LONG' );
		$this->announcement['END_DATE'] = $this->announcement['a_expire'] ? $std->get_date( $this->announcement['a_end'], 'LONG' ) : $ibforums->lang['no_expire'];
		
		$ibforums->lang['announcement_start'] = preg_replace( "/<#START#>/", $this->announcement['START_DATE'], $ibforums->lang['announcement_start']);
		$ibforums->lang['announcement_end'] = preg_replace( "/<#END#>/", $this->announcement['END_DATE'], $ibforums->lang['announcement_end']);
		$ibforums->lang['announcement_views'] = preg_replace( "/<#VIEWS#>/", $this->announcement['a_views']           , $ibforums->lang['announcement_views']);
		
		if ($this->announcement['a_desc']) {
			$this->announcement['a_desc'] = ', '.$this->announcement['a_desc'];
		}

		//-------------------------------------
		// Render the page top
		//-------------------------------------
		
		$this->output .= $this->html->PageTop( array( 'ANNOUNCEMENT' => $this->announcement, 'FORUM' => $this->forum ) );
		


				    
				
		$row	=	$this->announcement;

				    
		
			$poster = array();
		

			//
			//Insert:  Witty comment by Matt :-D ;)
			//

			
				$row['name_css'] = 'normalname';
				$poster = $this->parse_member( &$row );


			
			//--------------------------------------------------------------
			
			$row['post_css'] = 'post1';
			
			
			//--------------------------------------------------------------
			
			
			if (!$ibforums->member['view_img'])
			{
				$row['a_text'] = preg_replace( "/<img src=[\"'](.+?)[\"'].+?".">/", "(IMG:<a href='\\1' target='_blank'>\\1</a>)", $row['a_text'] );
			}
			
			//--------------------------------------------------------------
			
			
			$row['post_start']     = $ibforums->lang['announcement_start'];
			$row['post_end']     = $$ibforums->lang['announcement_end'];
			$row['post_icon']     = $row['a_icon']
							  ? "<img src='".$ibforums->vars['img_url']."/icon{$row['a_icon']}.gif' alt=''>&nbsp;&nbsp;"
							  : "";
			

			
			//--------------------------------------------------------------
			// Siggie stuff
			//--------------------------------------------------------------
			
			if (!$ibforums->vars[SIG_SEP]) $ibforums->vars[SIG_SEP] = "<br><br>--------------------<br>";
			
			if ($poster['signature'] and $ibforums->member['view_sigs'])
			{
			if ($row['a_use_sig'] == 1)
				{
				$row['signature'] = "<!--Signature-->{$ibforums->vars[SIG_SEP]}<span id='signature'>{$poster['signature']}</span><!--E-Signature-->";
				}
				else
				{
				$row['signature'] = "";
				}
								
			}
			else
			{
				$row['signature'] = "";
			}
			
			// Fix up the membername so it links to the members profile
			
			if ($poster['id'])
			{
				$poster['name'] = "<a href='{$this->base_url}&act=Profile&CODE=03&MID={$poster['id']}'>{$poster['name']}</a>";
			}
			
			$this->output .= $this->html->RenderRow( array( 'ANNOUNCEMENT' => $row, 'POSTER' => $poster ) );
			
		
		//-------------------------------------
		// Print the footer
		//-------------------------------------
		
		$this->output .= $this->html->TableFooter( array( 'ANNOUNCEMENT' => $this->announcement, 'FORUM' => $this->forum ) );
		

		//+----------------------------------------------------------------
		// Process users active in this forum
		//+----------------------------------------------------------------
		
		if ($ibforums->vars['no_au_topic'] != 1)
		{

			//+-----------------------------------------
			// Is this forum restricted, or global?
			//+-----------------------------------------
			
			if ($this->forum['read_perms'] != '*')
			{
				$q_extra = " AND s.member_group IN (0,".$this->forum['read_perms'].") ";
			}
			
			//+-----------------------------------------
			// Get the users
			//+-----------------------------------------
			
			$cut_off = ($ibforums->vars['au_cutoff'] != "") ? $ibforums->vars['au_cutoff'] * 60 : 900;
			 
			$time = time() - $cut_off;
			
			$DB->query("SELECT s.member_id, s.member_name, s.login_type, s.location, g.suffix, g.prefix
					    FROM ibf_sessions s
					     LEFT JOIN ibf_groups g ON (g.g_id=s.member_group)
					    WHERE s.in_topic='{$this->topic['tid']}'
					    AND s.running_time > '$time'"
					    .$q_extra.
					   "ORDER BY s.running_time DESC");
			
			//+-----------------------------------------
			// Cache all printed members so we don't double print them
			//+-----------------------------------------
			
			$cached = array();
			$active = array( 'guests' => 0, 'anon' => 0, 'members' => 0, 'names' => "");
			
			while ($result = $DB->fetch_row() )
			{
				if ($result['member_id'] == 0)
				{
					$active['guests']++;
				}
				else
				{
					if (empty( $cached[ $result['member_id'] ] ) )
					{
						$cached[ $result['member_id'] ] = 1;
						
						if ($result['login_type'] == 1)
						{
							if ( ($ibforums->member['mgroup'] == $ibforums->vars['admin_group']) and ($ibforums->vars['disable_admin_anon'] != 1) )
							{
								$active['names'] .= "</span><a href='{$ibforums->base_url}&act=Profile&MID={$result['member_id']}'>{$result['prefix']}{$result['member_name']}{$result['suffix']}</a>*, ";
								$active['anon']++;
							}
							else
							{
							$active['anon']++;
							}
						}
						else
						{
							$active['members']++;
							$active['names'] .= "</span><a href='{$ibforums->base_url}&act=Profile&MID={$result['member_id']}'>{$result['prefix']}{$result['member_name']}{$result['suffix']}</a>, ";
						}
					}
				}
			}
			
			$active['names'] = preg_replace( "/,\s+$/", "" , $active['names'] );
			
			$ibforums->lang['active_users_title']   = sprintf( $ibforums->lang['active_users_title']  , ($active['members'] + $active['guests'] + $active['anon'] ) );
			$ibforums->lang['active_users_detail']  = sprintf( $ibforums->lang['active_users_detail'] , $active['guests'],$active['anon'] );
			$ibforums->lang['active_users_members'] = sprintf( $ibforums->lang['active_users_members'], $active['members'] );
			
			
			$this->output = str_replace( "<!--IBF.TOPIC_ACTIVE-->", $this->html->topic_active_users($active), $this->output );
		
		}

		//-------------------------------------
		// Print all the output
		//-------------------------------------
		
        
		// Pass it to our print routine
		
		$print->add_output("$this->output");


        $print->do_output( array( 'TITLE'    => $ibforums->vars['board_name']." -> {$this->announcement['a_title']}",
        					 	  'JS'       => 1,
        					 	  'NAV'      => $nav_array,
        					  ) );
				        
	}
	
	/*********************************************************************/
	// Parse the member info
	/*********************************************************************/
	
	function parse_member($member=array()) {
		global $ibforums, $std, $DB;
		
		$member['avatar'] = $std->get_avatar( $member['avatar'], $ibforums->member['view_avs'], $member['avatar_size'] );
		
		$pips = 0;
		
		foreach($this->mem_titles as $k => $v)
		{
			if ($member['posts'] >= $v['POSTS'])
			{
				if (!$member['title'])
				{
					$member['title'] = $this->mem_titles[ $k ]['TITLE'];
				}
				$pips = $v['PIPS'];
				break;
			}
		}
		
		
		if ($member['g_icon'])
		{
			$member['member_rank_img'] = "<img src='{$ibforums->vars[TEAM_ICON_URL]}/{$member['g_icon']}' border='0'>";
		}
		else
		{
			if ($pips)
			{
				if ( preg_match( "/^\d+$/", $pips ) )
				{
					
					for ($i = 1; $i <= $pips; ++$i)
					{
						$member['member_rank_img'] .= "<{A_STAR}>";
					}
				}
				else
				{
					
					$member['member_rank_img'] = "<img src='{$ibforums->vars['TEAM_ICON_URL']}/$pips' border='0'>";
				}
			}
		}
							   
		$member['member_joined'] = $ibforums->lang['m_joined'].' '.$std->get_date( $member['joined'], 'JOINED' );
		
		$member['member_group'] = $ibforums->lang['m_group'].' '.$member['g_title'];
		
		$member['member_posts'] = $ibforums->lang['m_posts'].' '.$member['posts'];
		
		$member['member_number'] = $ibforums->lang['member_no'].' '.$member['id'];
		
		$member['profile_icon'] = "<a href='{$this->base_url}&act=Profile&CODE=03&MID={$member['id']}'><{P_PROFILE}></a>&nbsp;";
		
		$member['message_icon'] = "<a href='{$this->base_url}&act=Msg&CODE=04&MID={$member['id']}'><{P_MSG}></a>&nbsp;";
		
		if (!$member['hide_email'])
		{
			$member['email_icon'] = "<a href='{$this->base_url}&act=Mail&CODE=00&MID={$member['id']}'><{P_EMAIL}></a>&nbsp;";
		}
		
		if ( $member['website'] and preg_match( "/^http:\/\/\S+$/", $member['website'] ) )
		{
			$member['website_icon'] = "<a href='{$member['website']}' target='_blank'><{P_WEBSITE}></a>&nbsp;";
		}
		
		if ($member['icq_number'])
		{
			$member['icq_icon'] = "<a href=\"javascript:PopUp('{$this->base_url}&act=ICQ&MID={$member['id']}','Pager','450','330','0','1','1','1')\"><{P_ICQ}></a>&nbsp;";
		}
		
		if ($member['aim_name'])
		{
			$member['aol_icon'] = "<a href=\"javascript:PopUp('{$this->base_url}&act=AOL&MID={$member['id']}','Pager','450','330','0','1','1','1')\"><{P_AOL}></a>&nbsp;";
		}
		
		if ($member['yahoo'])
		{
			$member['yahoo_icon'] = "<a href=\"javascript:PopUp('{$this->base_url}&act=YAHOO&MID={$member['id']}','Pager','450','330','0','1','1','1')\"><{P_YIM}></a>&nbsp;";
		}
		
		if ($member['msnname'])
		{
			$member['msn_icon'] = "<a href=\"javascript:PopUp('{$this->base_url}&act=MSN&MID={$member['id']}','Pager','450','330','0','1','1','1')\"><{P_MSN}></a>&nbsp;";
		}
		
		//-----------------------------------------------------
		return $member;
	
	}
	

	

	function check_access() {
		global $ibforums, $std, $HTTP_COOKIE_VARS;
		
		$return = 1;
		
		$this->m_group = $ibforums->member['mgroup'];
		
		if ($this->forum['read_perms'] == '*')
		{
			$return = 0;
		}
		else if (preg_match( "/(^|,)$this->m_group(,|$)/", $this->forum['read_perms'] ) )
		{
			$return = 0;
		}
		
		if ($this->forum['password'] != "")
		{
		
			if ( ! $c_pass = $std->my_getcookie('iBForum'.$this->forum['id']) )
			{
				return 1;
			}
		
			if ( $c_pass == $this->forum['password'] )
			{
				return 0;
			}
			else
			{
			    return 1;
			}
		}
		
		return $return;
	
	}
	
	
}

?>