<?php
/*
/------------------------------------\
| Modified by Koudanshi
| Count numbers: 1
| Date: 19-Sept-2003
| Time: 3:45
\------------------------------------/
*/

// $Id: checklogin.php,v 1.10 2003/04/11 15:19:59 okazu Exp $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
// Author: Kazumi Ono (AKA onokazu)                                          //
// URL: http://www.xoops.org/ http://jp.xoops.org/  http://www.myweb.ne.jp/  //
// Project: The XOOPS Project (http://www.xoops.org/)                        //
// ------------------------------------------------------------------------- //

if (!defined('XOOPS_ROOT_PATH')) {
	exit();
}
include_once XOOPS_ROOT_PATH.'/language/'.$xoopsConfig['language'].'/user.php';

$member_handler =& xoops_gethandler('member');
$myts =& MyTextsanitizer::getInstance();

//------------------------
// IPBM compatibility
//------------------------
if(isset($HTTP_POST_VARS['Privacy'])){
	$Privacy=$HTTP_POST_VARS['Privacy'];
} else{
	$Privacy=$HTTP_GET_VARS['Privacy'];
}
if (!empty($Privacy)){
   @setcookie('anonlogin' , 1, time()+(60*60*24*365), '/',  '', 0);
}

if (isset($HTTP_POST_VARS['uname']) && isset($HTTP_POST_VARS['pass'])) {
	$uname = trim($HTTP_POST_VARS['uname']);
	$pass  = trim($HTTP_POST_VARS['pass']);
	$user =& $member_handler->loginUser(addslashes($myts->stripSlashesGPC($uname)), addslashes(md5($myts->stripSlashesGPC($pass))));
} else {
	$uname = trim($HTTP_GET_VARS['uname']);
	$pass  = trim($HTTP_GET_VARS['pass']);
	$user =& $member_handler->loginUser(addslashes($myts->stripSlashesGPC($uname)), addslashes($myts->stripSlashesGPC($pass)));
}
//------------------------

if ($uname == '' || $pass == '') {
	redirect_header(XOOPS_URL.'/user.php', 1, _US_INCORRECTLOGIN);
	exit();
}
if (false != $user) {
	if (0 == $user->getVar('level')) {
		redirect_header(XOOPS_URL.'/index.php', 5, _US_NOACTTPADM);
		exit();
	}
	if ($xoopsConfig['closesite'] == 1) {
		$allowed = false;
		foreach ($user->getGroups() as $group) {
			if (in_array($group, $xoopsConfig['closesite_okgrp']) || XOOPS_GROUP_ADMIN == $group) {
				$allowed = true;
				break;
			}
		}
		if (!$allowed) {
			redirect_header(XOOPS_URL.'/index.php', 1, _NOPERM);
			exit();
		}
	}
	$user->setVar('last_login', time());
	if (!$member_handler->insertUser($user)) {
	}
	$HTTP_SESSION_VARS = array();
	$HTTP_SESSION_VARS['xoopsUserId'] = $user->getVar('uid');
	$HTTP_SESSION_VARS['xoopsUserGroups'] = $user->getGroups();
	if ($xoopsConfig['use_mysession'] && $xoopsConfig['session_name'] != '') {
		setcookie($xoopsConfig['session_name'], session_id(), time()+(60 * $xoopsConfig['session_expire']), '/',  '', 0);

    //-----------------------------------------------------------------------------------------------------
    // Cookies Set Code for AutoLogon by Joeabg (origin author) and Jan304 (modify author and distribute)//
    //-----------------------------------------------------------------------------------------------------
		if($_POST["AutoLogin"] == "On")
		{
      $passSQL  = "SELECT pass FROM ".$xoopsDB->prefix("users")." WHERE uid = ".$HTTP_SESSION_VARS['xoopsUserId'];
      @$passQry = $xoopsDB->query($passSQL);
      $passRst  = $xoopsDB->fetchArray($passQry);
      setcookie('member_id' , $HTTP_SESSION_VARS['xoopsUserId'], time()+(60*60*24*365), '/',  '', 0);
      setcookie('xoops_uid' , $HTTP_SESSION_VARS['xoopsUserId'], time()+(60*60*24*365), '/',  '', 0);
      setcookie('xoops_pass', ''.$passRst['pass'].''		   , time()+(60*60*24*365), '/',  '', 0);
		}

    //-----------------------------------------------------------------------------------------------------
	}
	$user_theme = $user->getVar('theme');

	if (in_array($user_theme, $xoopsConfig['theme_set_allowed'])) {
		$HTTP_SESSION_VARS['xoopsUserTheme'] = $user_theme;
	}
	if (!empty($HTTP_POST_VARS['xoops_redirect']) && !strpos($HTTP_POST_VARS['xoops_redirect'], 'register')) {
		$parsed = parse_url(XOOPS_URL);
		$url = isset($parsed['scheme']) ? $parsed['scheme'].'://' : 'http://';
		if (isset($parsed['host'])) {
			$url .= isset($parsed['port']) ?$parsed['host'].':'.$parsed['port'].trim($HTTP_POST_VARS['xoops_redirect']): $parsed['host'].trim($HTTP_POST_VARS['xoops_redirect']);
		} else {
			$url = xoops_getenv('HTTP_HOST').trim($HTTP_POST_VARS['xoops_redirect']);
		}
	}
	//------------------
	//IPBM AUTO REDIRECT
	//------------------

	if (preg_match("/ipboard/",$HTTP_SERVER_VARS['HTTP_REFERER'])) {
		$url = $HTTP_SERVER_VARS['HTTP_REFERER'];
		$url = str_replace ("act=Reg&CODE=00", "",$url);
		$url = str_replace ("act=Reg&CODE=10", "",$url);
		$url = str_replace ("act=Reg&CODE=reval", "",$url);
		$url = str_replace ("act=Login&CODE=00", "",$url);
	}
	//------------------
      setcookie('open_qr' , 1, time()+(60*60*24*365), '/',  '', 0);


	// RMV-NOTIFY
	// Perform some maintenance of notification records
	$notification_handler =& xoops_gethandler('notification');
	$notification_handler->doLoginMaintenance($user->getVar('uid'));

	redirect_header($url, 1, sprintf(_US_LOGGINGU, $user->getVar('uname')));
} else {
	redirect_header(XOOPS_URL.'/user.php',1,_US_INCORRECTLOGIN);
}
exit();
?>