<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ERROR);
// Login
$basepath=''; 
require_once 'core/init_core.inc.php';
$plugin->run_hook('login_begin');
$lang->setlang($session->userdata['lang']);
if($_POST['username']!='' && $_POST['password']!=''){
	$login=$session->check_login($_POST['username'],$_POST['password']);
	if($login=='BANNED'){
		$core->message($lang->_('BANNED'),$lang->_('YOUHAVEBEENBANNED'));
	}elseif($login){
		$pdata=array('remember'=>$_POST['remember'],'uid'=>$login,'domain'=>$config['domain']);
		$plugin->run_hook('login_end',array('pdata'=>$pdata));
		// Login done.
		if(isset($_POST['url']) && $_POST['url']!==''){
			$core->message($lang->_('LOGGEDIN'),$lang->_('YOUHAVEBEENLOGGEDIN'),TRUE,$_POST['url'],3);
		}else{
			$core->message($lang->_('LOGGEDIN'),$lang->_('YOUHAVEBEENLOGGEDIN'),TRUE,$config['path'].'/index.php',3);
		}
	}else{
		$core->message($lang->_('ERROR'),$lang->_('WRONGUSERORPASS'),TRUE,$config['path'].'/index.php',3);
	}
}elseif($config['use_openid']==1 && $_POST['openid_identifier']!=''){
	$openid = new SimpleOpenID;
	$openid->SetIdentity($_POST['openid_identifier']);
	$openid->SetApprovedURL('https://'.$config['domain'].$config['path'].'/login.php'); // Script which handles a response from OpenID Server
	$openid->SetTrustRoot('https://'.$config['domain']);
	$openid->SetRequiredFields(array('email','nickname','fullname','dob','gender'));
	$open_id_server=$openid->GetOpenIDServer(); // Returns false if server is not found
	if($open_id_server){
		$openid->Redirect();
	}else{
		$core->message($lang->_('ERROR'),$lang->_('AUTHENTIFICATIONFAILURE'),TRUE,$config['path'].'/index.php',3);
	}
}elseif($config['use_openid']==1 && isset($_GET['openid_identity']) && $_GET['openid_identity']!=''){
	$openid = new SimpleOpenID;
	$identity = $_GET['openid_identity'];
	$openid->SetIdentity($identity);
	$ok = $openid->ValidateWithServer();
	if ($ok) {
		$login=$session->check_login('','',$identity);
		if($login){
			$pdata=array('remember'=>$_POST['remember'],'uid'=>$login,'domain'=>$config['domain']);
			$plugin->run_hook('login_end',array('pdata'=>$pdata));
			// Login done.
			if(isset($_POST['url']) && $_POST['url']!==''){
				$core->message($lang->_('LOGGEDIN'),$lang->_('YOUHAVEBEENLOGGEDIN'),TRUE,$_POST['url'],3);
			}else{
				$core->message($lang->_('LOGGEDIN'),$lang->_('YOUHAVEBEENLOGGEDIN'),TRUE,$config['path'].'/index.php',3);
			}
		}else{
			$data['username']=$_GET['openid_sreg_nickname'];
			$birth=explode('-',$_GET['openid_sreg_dob']);
			$data['birthday']=$birth[2].".".$birth[1].".".$birth[0];
			$gender[$_GET['openid_sreg_gender']]='selected="selected"';
			$data['realname']=$_GET['openid_sreg_fullname'];
			$data['email']=$_GET['openid_sreg_email'];
			$tpl->assign('openid',1);
			$tpl->assign('data',$data);
			$tpl->assign('gender',$gender);
			$tpl->assign('identity',$_GET['openid_identity']);
			$tpl->assign('captcha',$config['captcha']);
			$tpl->assign('config',$config);
			$content=$tpl->fetch('register.tpl');
			$core->make_page($content);
		}
	}else{
		$core->message($lang->_('ERROR'),$lang->_('AUTHENTIFICATIONFAILURE'),TRUE,$config['path'].'/index.php',3);
	}
}else{
	$tpl->assign('config',$config);
	$content=$tpl->fetch('login.tpl');
	$core->make_page($content, FALSE);
}
?>
