<?php 
// 绑定页面
include "../core.php";
saxue_getconfigs( 'oauth', 'member' );
if ( empty( $_GET['site'] ) ) {
		$_GET['site'] = $_SESSION['saxueUserOauth'];
} 
if ( empty( $_GET['site'] ) || !isset( $saxueOauth[$_GET['site']] ) || !$saxueOauth[$_GET['site']]['open'] ) {
		saxue_printfail( '登录接口 ' . $saxueOauth[$_GET['site']]['name'] . ' 不存在或者已关闭' );
} 
include SAXUE_ROOT_PATH . '/oauth/' . $_GET['site'] . '.php';
if ( isset( $_GET['op'] ) && $_GET['op'] == 'bind' ) {
		// 执行登录或注册返回绑定
		$oauth_info = get_oauthinfo();
		del_token();
		if ( empty( $oauth_info['openid'] ) ) {
				saxue_jumppage( '/member/', '授权信息已失效，请重新绑定' );
		} 
		if ( empty( $_SESSION['saxueUserId'] ) || $_SESSION['saxueUserId'] <= 0 ) {
				saxue_jumppage( '/member/login.php', '登录失败，不能绑定' );
		} 
		// 导入oauth接口类
		include SAXUE_ROOT_PATH . "/model/member_oauth.php";
		$oauth_handler = &saxuememberoauthhandler :: getinstance( "saxuememberoauthhandler" );

		if ( $oauth_handler -> getoauth( $_SESSION['saxueUserId'], $_GET['site'] ) != false ) {
				saxue_jumppage( '/member/', '您已绑定其他的 ' . $saxueOauth[$_GET['site']]['name'] . ' 帐号，请先解除绑定' );
		} 

		if ( $oauth_handler -> bind( $_SESSION['saxueUserId'], $_GET['site'], $oauth_info ) ) {
				saxue_jumppage( '/member/', $saxueOauth[$_GET['site']]['name'] . '帐号绑定成功' );
		} else {
				saxue_jumppage( '/member/', $saxueOauth[$_GET['site']]['name'] . '帐号绑定失败' );
		} 
} else {
		// 已经登陆
		if ( !empty( $_SESSION['saxueUserId'] ) && $_SESSION['saxueUserId'] > 0 ) {
				del_token();
				saxue_printfail( '请不要重复登录' );
		} 
		$oauth_info = get_oauthinfo();
		if ( empty( $oauth_info['openid'] ) ) {
				del_token();
				saxue_jumppage( '/member/login.php', '授权信息已失效，请重新登录' );
		} 
		saxue_getconfigs( "configs", "member" );
		include SAXUE_WEB_PATH . "/header.php";
		$saxueTpl -> assign_by_ref( "oauth_cfg", $saxueOauth[$_GET['site']] );
		$saxueTpl -> assign_by_ref( "oauth_info", $oauth_info );
		$saxueTpl -> assign_by_ref( "config", $saxueConfigs['member'] );
		$saxueTpl -> setcaching( 0 );
		$saxueTset['saxue_page_template'] = SAXUE_THEME_PATH . '/member/oauthbind.html';
		include SAXUE_WEB_PATH . "/footer.php";
} 
