<?php
include "../core.php";
saxue_checklogin();
include SAXUE_ROOT_PATH . "/model/member_users.php";
$users_handler = &saxuememberusershandler :: getinstance( "saxuememberusershandler" );
$saxueUsers = $users_handler -> get( $_SESSION['saxueUserId'] );
if ( !$saxueUsers ) {
		saxue_printfail( LANG_NO_USER );
} 
if ( !isset( $_REQUEST['action'] ) ) {
		$GLOBALS['_REQUEST']['action'] = "edit";
} 
saxue_getconfigs( 'configs', 'member' );
switch ( $_REQUEST['action'] ) {
		case "update" :
				$_POST['oldpass'] = trim( $_POST['oldpass'] );
				$_POST['newpass'] = trim( $_POST['newpass'] );
				$_POST['repass'] = trim( $_POST['repass'] );
				// 密码检查
				if ( strlen( $_POST['newpass'] ) == 0 || strlen( $_POST['repass'] ) == 0 ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '密码不能为空' ) ) );
				} else if ( $_POST['newpass'] != $_POST['repass'] ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '密码和重复密码不一致' ) ) );
				} else if ( !empty( $saxueConfigs['member']['regminpwd'] ) && strlen( $_POST['newpass'] ) < intval( $saxueConfigs['member']['regminpwd'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '密码长度不能少于' . $saxueConfigs['member']['regminpwd'] . '个字符' ) ) );
				} else if ( !empty( $saxueConfigs['member']['regmaxpwd'] ) && strlen( $_POST['newpass'] ) > intval( $saxueConfigs['member']['regmaxpwd'] ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '密码长度不能大于' . $saxueConfigs['member']['regmaxpwd'] . '个字符' ) ) );
				} else if ( $saxueConfigs['member']['regnpdifferf'] == 1 && $_POST['newpass'] == $_SESSION['saxueUserName'] ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '密码和用户名不能相同' ) ) );
				} elseif ( $saxueUsers -> getvar( "password" ) != '' && $users_handler -> encryptpass( $_POST['oldpass'] ) != $saxueUsers -> getvar( "password" ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '旧密码错误' ) ) );
				} 
				// 密码复杂度检查
				if ( !empty( $saxueConfigs['member']['regchrpwd'] ) ) {
						$pwdrule = explode( ',', $saxueConfigs['member']['regchrpwd'] );
						foreach ( $pwdrule as $r ) {
								switch ( intval( $r ) ) {
										case 1:
												if ( !preg_match( "/[a-z]/", $_POST['newpass'] ) ) {
														exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含小写字母' ) ) );
												} 
												break;
										case 2:
												if ( !preg_match( "/[A-Z]/", $_POST['newpass'] ) ) {
														exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含大写字母' ) ) );
												} 
												break;
										case 3:
												if ( !preg_match( "/[0-9]/", $_POST['newpass'] ) ) {
														exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含数字' ) ) );
												} 
												break;
										case 4:
												if ( !preg_match( "/[^a-zA-Z0-9]/", $_POST['newpass'] ) ) {
														exit( json_encode( array( 'flag' => 0, 'msg' => '密码中必须包含符号' ) ) );
												} 
												break;
										default:
												break;
								} 
						} 
				} 
				$saxueUsers -> setvar( "password", $users_handler -> encryptpass( $_POST['newpass'] ) );
				if ( !$users_handler -> insert( $saxueUsers ) ) {
						exit( json_encode( array( 'flag' => 0, 'msg' => '修改密码失败' ) ) );
				}
				exit( json_encode( array( 'flag' => 1 ) ) );
				break;
		case "edit" :
				include_once( SAXUE_WEB_PATH . "/member/header.php" );
				$saxueTpl -> assign( "config", $saxueConfigs['member'] );
				$saxueTpl -> setcaching( 0 );
				$saxueTset['saxue_contents_template'] = SAXUE_THEME_PATH . '/member/passedit.html';
				include_once( SAXUE_WEB_PATH . "/member/footer.php" );
} 
