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

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

# 定义事件
$events = array(
	'LOGIN_SUCCESSED',
	'LOGIN_FAILED',
	'LOGIN_OUT_LIM',
);
register_event( $events ) ;


class login extends detailPge
{
	/**
	 * 用户类型
	 * 
	 * 格式:
	 *   array(
	 *   	'teacher' => array(
	 * 			'dbtable' => 'teacher',
	 * 			'column_user_name' => 'teacher_name',
	 * 			'column_password_name' => 'teacher_password'
	 *   	)
	 *   	'student' => array(
	 * 			'dbtable' => 'student',
	 * 			'column_user_name' => 'student_user',
	 * 			'column_password_name' => 'student_password'
	 *   	)
	 *   )
	 *
	 * @var unknown_type
	 */
	var $user_type = array();
	
	var $show_user_type_input = false ;
	
	var $user_info ;
	
	var $max_failed_turn = 0 ;
	
	var $check_verifycode = true ;

	var $is_open_clm = '' ;
	var $log_time_clm	= 'last_login_time' ;
	var $log_ip_clm	= 'last_login_ip' ;
	
	function _init_fields()
	{
		$this->fields['user'] =& new field();
		$this->fields['user']->inputName		 = 'user' ;
		$this->fields['user']->inputType		 = 'text' ;
		$this->fields['user']->checkType		 = 'text' ;
		$this->fields['user']->maxValue			 = 40 ;
		$this->fields['user']->caption			 = '用户名' ;
		$this->fields['user']->alertName		 = '用户名' ;
		$this->fields['user']->allowEmpty		 = false ;		

		$this->fields['password'] =& new field();
		$this->fields['password']->inputName	 = 'password' ;
		$this->fields['password']->inputType	 = 'password' ;
		$this->fields['password']->checkType	 = 'text' ;
		$this->fields['password']->maxValue		 = 40 ;
		$this->fields['password']->caption		 = '密 码' ;
		$this->fields['password']->alertName	 = '密 码' ;
		$this->fields['password']->allowEmpty	 = false ;

		$this->fields['user_type'] =& new field();
		$this->fields['user_type']->inputName	 = 'user_type' ;
		$this->fields['user_type']->inputType	 = 'radio' ;
		$this->fields['user_type']->checkType	 = 'checkbox' ;
		$this->fields['user_type']->caption		 = '用户类型' ;
		$this->fields['user_type']->alertName	 = '用户类型' ;
		$this->fields['user_type']->allowEmpty	 = false ;

		if( $this->check_verifycode )
		{
			$this->fields['verifycode'] =& new field();
			$this->fields['verifycode']->inputName		 = 'verifycode' ;
			$this->fields['verifycode']->inputType		 = 'text' ;
			$this->fields['verifycode']->checkType		 = 'text' ;
			$this->fields['verifycode']->maxValue		 = 6 ;
			$this->fields['verifycode']->input_size_width= ' size="6"' ;
			$this->fields['verifycode']->caption		 = '验证码' ;
			$this->fields['verifycode']->alertName		 = '验证码' ;
			$this->fields['verifycode']->allowEmpty		 = false ;
		}

	}
	
	function _set_user_type_options( $options )
	{
		$this->fields['user_type']->options+= $options ;
	}
	
	function _set_default_user_type( $type )
	{
		$this->fields['user_type']->default = $type ;
	}

	function _act()
	{
		if( !$this->get_input_data() )
			return false ;

		// 检查验证码
		if( $this->check_verifycode )
		{
			if( !class_exists('verifycode') )
				include_once( dirname(__FILE__).'/verifycode.class.php' ) ;
			$vc =& new verifycode();
			
			$result = $vc->check_code( $this->fields['verifycode']->value ) ;
			$vc->destroy_code() ;
			
			if( ! $result )
			{
				$this->bad('验证码输入有误'.($vc->ignore_case? '。':',请注意字母的大小写区分。'));
				return false ;
			}
		}


		if( count($this->user_type)>1 )
			$usertype = @$this->user_type[ $this->fields['user_type']->value ] ;		
		else
		{
			$usertype = @current($this->user_type) ;			
			$this->user_type[ $this->fields['user_type']->value ] = $usertype['dbtable'] ;
		}	

		if( !$usertype )
		{
			$this->bad('参数错误,没有合法的用户类型。');
			return false ;
		}

		global $db;
		$sql = "select * from `{$usertype['dbtable']}` where `{$usertype['column_user']}`='{$this->fields['user']->value}'" ;
		if( !$this->user_info = $db->getrow( $sql ) )
		{
			$db->debug() ;
			$this->bad('输入了不存在的用户名。');
			return false ;
		}

		if( !empty($usertype['is_open_clm']) and ($this->user_info[$usertype['is_open_clm']]<1) )
		{
			$this->bad('账号已被限制,或尚未激活。');
			return false ;
		}

		if ( $this->user_info[ $usertype['column_password'] ] == $this->fields['password']->value ) 
		{
			$_SESSION['Identity'][ $usertype['dbtable'] ] = $this->user_info ;

			$this->ok('登陆成功。');
			$this->successed = true ;

			if( !empty($usertype['location']) )
				$this->assign('location',$usertype['location']) ;

			$this->table = $usertype['dbtable'] ;
			$this->id = $this->user_info['id'] ;

			return $this->user_info ;
		}
		else
		{
			$this->bad('密码错误。');
			return true ;
		}
	}

	function login()
	{
		$this->webPge();
		$this->_init_fields();

		if( !isset($_SESSION) )
			session_start() ;

		if( !isset($_SESSION['_Failed_turn']) )
		{
			session_register('_Failed_turn') ;
			$_SESSION['_Failed_turn'] = 0;
		}

		$this->act = 'modify' ;
	}

	function set_user_type( $dbtable, $column_user, $column_password, $caption='*', $location='', $is_open_clm='', $column_last_ip='', $column_last_time='', $if_default=0 )
	{
		if( $caption == '*' )
			$caption = $dbtable ;

		if( count($this->user_type) == 0 )
			$this->_set_default_user_type( $dbtable );

		$user_type = array(
			'dbtable' => $dbtable,
			'column_user' => $column_user,
			'column_password' => $column_password,
			'caption' => $caption,
			'location' => $location,
			'is_open_clm' => $is_open_clm,
			'column_last_ip' => $column_last_ip,
			'column_last_time' => $column_last_time
		);
		
		$this->user_type[ $dbtable ] = $user_type ;
		
		if( $if_default )
			$this->_set_default_user_type( $dbtable );

		$this->_set_user_type_options( array( $dbtable=>$caption ) ) ;
		$this->show_user_type_input = ( count($this->user_type) > 1 ) ;
	}
	
	function display_me( $template_filename, $content_filename='' )
	{
		if( $content_filename )
			$this->assign( 'content', $content_filename );
		$this->display_webPge($template_filename);
		
	}

	function act()
	{
		if( !isset($_REQUEST['submit']) )
			return ;

		if( $this->max_failed_turn and @$_SESSION['_Failed_turn'] >= $this->max_failed_turn )
		{
			$this->bad('失败次数已超过 5 次。');
			$this->_touch_user_event( LOGIN_OUT_LIM ) ;
			return ;
		}

		# 调用用户事件
		if( $this->_act() )
			$this->_touch_user_event( LOGIN_SUCCESSED ) ;
		else
		{
			$_SESSION['_Failed_turn'] ++ ;
			if( $this->max_failed_turn )
				$this->bad( "登陆尝试失败 {$_SESSION['_Failed_turn']} 次,超过 {$this->max_failed_turn} 次,账号会被系统临时禁用。"  );
			$this->_touch_user_event( LOGIN_FAILED ) ;
		}
	}

	function save_log( $time_clm='', $ip_clm='' )
	{
		if( !$this->successed )
			return ;

		if( empty($time_clm) )
			$time_clm = $this->log_time_clm ;
		
		if( empty($ip_clm) )
			$ip_clm = $this->log_ip_clm ;
		
		$now = time();
		$sql = "update `{$this->table}` set `{$time_clm}`={$now}, `{$ip_clm}`='{$_SERVER['REMOTE_ADDR']}'  where `id`={$this->id}";
		mysql_query( $sql ) ;
	}

}
?>