gusucode.com > 同城苏州黄页系统php源码程序 > lib/adAct.class.php

    <?
if( !class_exists('client') )
	include_once( dirname(__FILE__)."/client.class.php" );
if( !class_exists('webPge') )
	include_once( dirname(__FILE__)."/webPge.class.php" );


class adAct extends webPge
{

###################################
# 初始化
	function adAct( $just_test = false )
	{
		global $db ;
		$this->just_test = $just_test ;

		$this->client =& new client( !$just_test ) ;
		$this->db =& $db ;

		$this->date = mktime(0,0,0,date('m'),date('d'),date('Y')) / 100 ;

		$this->webPge() ;
	}

	function set_putin( $putin_id )
	{
		if( !$this->putin = $this->db->getrow( 'put_in', "`id`={$putin_id}" ) )
			return false ;

		if( !$this->set_ad( $this->putin['ad'] ) )
			return false ;

		/* -- 判断来路 -- */
		//pp($_SERVER);
		if( empty($_SERVER['HTTP_REFERER']) /*and $this->ad['ad_type'] != 'word'*/ )
		{
			$this->debug("来路判断未通过",0,"没有 http referer 字段") ;
			return $this->just_test = true ;
		}
		else
		{
			// 点击广告 来路来自 本系统
			if( $_REQUEST['act'] == 'click' )
				$putinSite = @parse_url("http://{$_SERVER['HTTP_HOST']}{$_SERVER['SCRIPT_NAME']}") ;
			else
				$putinSite = @parse_url($this->putin['site']) ;
			//pp($putinSite) ;
	
			$realSite = @parse_url($_SERVER['HTTP_REFERER']) ;//pp($realSite) ;

			if( !preg_match( "/{$putinSite['host']}$/", $realSite['host'] ) /*and $this->ad['ad_type'] != 'word'*/ )
			{
				$this->debug("来路判断未通过",0,"http referer 字段中的主机名({$putinSite['host']}) 未包含 投放渠道记录的site字段中的主机名({$realSite['host']})") ;
				return $this->just_test = true ;
			}
		}
		$this->debug("来路判断已通过({$_SERVER['HTTP_REFERER']})",1) ;
		/* -- 判断来路 -- */

		return true ;
	}

	function set_ad( $ad_id )
	{
		if( !$this->ad = $this->db->getrow( 'ad', "`id`={$ad_id}" ) )
		{
			return false ;
		}
		
		
		if( !$this->just_test )
		{
			$this->daylog_data['log_day_ad'] = array(
				'ad'				 => $this->ad['id'] ,
				'ad_name'			 => $this->ad['ad_name'] ,
				'ad_type'			 => $this->ad['ad_type'] ,
				'adcustomer'		 => $this->ad['parent'] ,
				'adcustomer_user'	 => $this->ad['user'] 
			) ;
			$this->daylog_data['log_day_putin'] = array(
				'putin'				 => $this->putin['id'] ,
				'ad'				 => $this->ad['id'] ,
				'ad_name'			 => $this->ad['ad_name'] ,
				'ad_type'			 => $this->ad['ad_type'] ,
				'adcustomer'		 => $this->putin['adcustomer'] ,
				'adcustomer_user'	 => $this->putin['adcustomer_user'] ,
				'webmaster'			 => $this->putin['webmaster'] ,
				'webmaster_user'	 => $this->putin['webmaster_user'] ,
				'site'				 => $this->putin['site'] ,
			) ;
			$this->daylog_data['log_day_webmaster'] = array(
				'webmaster'			 => $this->putin['webmaster'] ,
				'webmaster_user'	 => $this->putin['webmaster_user'] 
			) ;
		}

		return true ;
	}


###################################
# 操作

	// 显示广告操作
	function show( $out_at_once = true )
	{
		// 当 广告已被关闭
		if( !$this->ad['is_open'] and !$this->just_test )
		{
			$this->alert_closed_ad() ;
			return ;
		}

		// 当 剩余点击数 减至 0 时 或 到达终止日期, 不显示广告, 并关闭广告
		if( $this->ad['ad_residual'] <= 0 or $this->ad['endTime'] < time() )
		{
			$this->db->update( 'ad', array('is_open'=>'0'), "`id`={$this->ad['id']}" ) ;
			$this->alert_closed_ad() ;
			return ;
		}

		// 对于图片广告,判断是否为 flash		
		if( $this->ad['ad_type'] == 'pic' )
		{
			list($width, $height, $type, $attr) = getimagesize( $this->upload_path . $this->ad['ad_pic'] );
			if( $type == IMAGETYPE_SWF or $type == IMAGETYPE_SWC )
			{
				$this->ThisIsFlash = true ;
				$this->ThisFlashWidth = $width ;
				$this->ThisFlashHeight = $height ;
			}
		}

		// 显示广告
		$this->display_webPge( "show_ad_{$this->ad['ad_type']}.js", $out_at_once ) ;
		flush();									// 进行数据记录以前,先输出代码,
													// 使浏览器不必 等待 服务器 数据记录维护 所花费的时间
		if( !$this->just_test )
			$this->logshow() ;

		return true;
	}

	// 记录浏览显示量
	function logshow()
	{
		// 记录浏览者行为
		$this->client->log_show_ad( $this->putin ) ;

		$this->db->increase( 'put_in', 'show', "`id`={$this->putin['id']}" );
		$this->db->increase( 'ad', 'watch', "`id`={$this->ad['id']}" );
		if( @$this->putin['show_once'] )
			$this->db->increase( 'ad', 'watch_once', "`id`={$this->ad['id']}" );

		// 每日记录
		$this->_insert_daylog( 'log_day_ad', "`ad` = {$this->ad['id']}", 'show' ) ;
		$this->_insert_daylog( 'log_day_putin', "`putin` = {$this->putin['id']}", 'show' ) ;
		$this->_insert_daylog( 'log_day_webmaster', "`webmaster` = {$this->putin['webmaster']}", 'show' ) ;

	}
	
	// 点击广告操作
	function click()
	{
		ignore_user_abort( true ) ;
		// header( "Location: {$this->ad['ad_link']}" ) ;
		$code = "<body><a href=\"{$this->ad['ad_link']}\" id=\"theLink\"></a><script>document.getElementById('theLink').click() ;</script></body>" ;		

		if( $this->debug_adminClick_notGoto )
			print "<p style=\"font-size: 13px;\">开启了“管理员点击止步”调试功能,跳转代码被拦截:<br /><textarea cols=\"60\" rows=\"4\">".htmlentities($code)."</textarea><br />※注意:此功能对普通用户没有影响。可在“系统维护/系统设定/系统调试选项/管理员点击止步”关闭此选项。</p>" ;
		else
			print $code ;

		flush() ;
													// 以上三行 , 在 数据记录维护 前 现行输出 header Location 字段,
													// 浏览器 接收 Location 字段 后 立即断开 与服务器的连接,
													// 以下 代码在 客户端断开连接 后,仍然继续 执行,直至完成	

		$this->payoff() ;


	}

	// 计费
	function payoff()
	{
		if( $this->just_test )
		{
			$this->debug('点击计费已取消',0);
			return ;
		}
		$this->debug('点击计费开始……',0);

		// 扣量处理
		if( $deduct = getSystem('deduct')/100 )
			$ad_price_4_webmaster = ( $this->ad['ad_price_4_webmaster'] / 1000 )*$deduct ;

		// deduct 为0 则不扣量
		else		
			$ad_price_4_webmaster = ( $this->ad['ad_price_4_webmaster'] / 1000 ) ;


		// 记录浏览者行为
		$this->client->log_click_ad( $this->putin );

		// 记录 put_in , ad 表
		$this->db->increase( 'ad', 'click', "`id`={$this->ad['id']}" );


		if( @$this->putin['click_once'] )
		{

			$this->db->increase( 'ad', 'click_once', "`id`={$this->ad['id']}" );
			$this->db->increase( 'put_in', 'click', "`id`={$this->putin['id']}" );

			// 每日记录
			$this->_insert_daylog( 'log_day_ad', "`ad` = {$this->ad['id']}", 'click', $this->ad['ad_price'] / 1000 ) ;
			$this->_insert_daylog( 'log_day_putin', "`putin` = {$this->putin['id']}", 'click', $ad_price_4_webmaster ) ;
			$this->_insert_daylog( 'log_day_webmaster', "`webmaster` = {$this->putin['webmaster']}", 'click', $ad_price_4_webmaster ) ;


			// 从 广告中扣款
			$this->db->decrease( 'ad', 'ad_residual', "`id`={$this->ad['id']}" );
													// 剩余点击
			$this->db->decrease( 'ad', 'ad_sum', "`id`={$this->ad['id']}", $this->ad['ad_price'] / 1000 );
			// $this->db->debug();
													// 储金

			// 为 站长 增加应付佣金
			$this->db->increase( 'usr_webmaster', 'sum', "`id`={$this->putin['webmaster']}", $ad_price_4_webmaster );
													// 总佣金
			$this->db->increase( 'put_in', 'sum', "`id`={$this->putin['id']}", $ad_price_4_webmaster );
													// 记录 渠道佣金
			// $this->db->debug();
		}
		
		// 余额通知
		$this->_alert() ;
	}
	
	// 余额通知
	function _alert()
	{
		if( $this->ad['ad_alerted'] or $this->ad['ad_residual']>$this->ad['ad_alert_cnt'] )
			return ;

		//print "'广告点击剩余,已达到余额通知设定值。'\r\n" ;

		$email = $this->db->getOneField( 'usr_adcustomer', 'email', "`id`={$this->ad['parent']}" ) ;
		if( !check_email($email) )
		{
			//print "'广告主邮件不合法,通知邮件发送取消。'\r\n" ;
			return ;
		}

		include "smtp/class.phpmailer.php";
		$mail =& new PHPMailer() ;
		$mail->IsSMTP();
		$mail->CharSet	="gb2312";
		$mail->Mailer="smtp";
		$mail->From='cnbestad@126.com';
		$mail->FromName	= mb_convert_encoding( '中国贝斯特广告联盟', 'gb2312', 'utf-8' );

		$mail->SMTPAuth	= true;
		$mail->Host		= 'smtp.126.com' ;
		$mail->Username	= 'cnbestad' ;
		$mail->Password	= '123456' ;

		$mail->AddAddress( $email ) ;
		$sysName = getSystem('system_name') ;
		$mail->Subject	= mb_convert_encoding( "{$sysName} -- 余额不足通知", 'gb2312', 'utf-8' );
		$mail->Body		= mb_convert_encoding( "您在{$sysName}投放的广告:{$this->ad['ad_name']},点击(弹出)剩余已经不足 {$this->ad['ad_residual']} 次。如果您希望继续投放此广告,请登陆 {$sysName} 追加广告费用投入。", 'gb2312', 'utf-8' );

		if( $mail->Send() )
		{
			$this->db->update( 'ad', array('ad_alerted'=>1), "`id`={$this->ad['id']}") ;
			//print "'通知邮件已发出。'";
		}
		else
		{
			//print "'通知邮件发送失败。'";
		}
		

	}



	function alert_closed_ad()
	{
		pp('"已经关闭的广告"') ;
		// 回头增加 托管机制

		$this->db->update('ad',array('is_open'=>'0'),"`id`={$this->ad['id']}") ;

		flush() ;
	}
	
	function debug( $msg, $result=-1, $reason='' )
	{
		$msg = (empty($msg)) ? '': " msg=\"{$msg}\"" ;
		$reason = (empty($reason)) ? '': " reason=\"{$reason}\"" ;
		$result = ($result<0) ? '': " result=\"". ($result?'ok':'bad') ."\"" ;
		
		$code = "<Debug {$msg}{$reason}{$result} />\r\n" ;
		if( $this->debug_adminClick_notGoto )
			print '<br /><p style="font-size: 13px; color: #cc2222;">'.htmlspecialchars($code).'</p>' ;
		else
			print $code ;
	}


###################################
# 私有方法


	function _insert_daylog( $table, $identity, $fld, $money=0 )
	{

		$selectwhere = "`date`={$this->date} and " . $identity ;

		// 插入
		if( !$this->db->getrow( $table, $selectwhere ) )
			$this->db->insert( $table, $this->daylog_data[$table] + array('date'=>$this->date) ) ;
		
		// 修改
		$this->db->increase( $table, $fld, $selectwhere ) ;

		if( !empty($money) )
			$this->db->increase( $table, 'sum', $selectwhere, $money ) ;

	}



###################################
# 属性

	var $client ;
	var $db ;

	var $ad ;
	var $putin ;

	var $just_test = true;

	var $date ;
	
	var $debug_adminClick_notGoto = false ;

}

?><?

function debug_out( $msg, $return = false )
{
	if( empty($_REQUEST['debug']) )
		return $return;

	pp($msg);
	return $return ;
}
?>