<?php
// +-------------------------------------------------------------------------+
// | Mobile Melody Plug-in for PHP Melody ( www.phpsugar.com )
// +-------------------------------------------------------------------------+
// | MOBILE MELODY IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +-------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2012 PhpSugar.com. All rights reserved.
// +-------------------------------------------------------------------------+
session_start();

@header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
@header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
@header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
@header( 'Pragma: no-cache' );

require_once('settings.php');

// Initialize some variables
$errors = array();
$nr_errors = 0;
$logged_in = 0;

$mode = '';
$redir = '';
$mode = ($_GET['do']) ? $_GET['do'] : '';

switch($mode){ 
	default:
	case 'login':
		//	check if user is already logged in
		//	if he already is, redirect him to index page
		if (is_user_logged_in()) 
		{
			header("Location: ". _URL_MOBI ."/index."._FEXT."");
			exit();
		}
		// get the last referer so that we can redirect the user to his last visited page after logging him in.
		$redir = $_POST['ref'];
		if( $redir === false){ 	
			$redir = "index."._FEXT;
		}
		$redir = ltrim($redir, '/ ');

		if( isset($_POST['Login'])) 
		{
			$reason = false;
			$errors = array();
			
			$email = $username = '';
			if (strpos($_POST['username'], '@') !== false && strlen($_POST['username']) > 5)
			{
				$email = trim($_POST['username']);
				$email = str_replace("\'", "''", $email);
				$email = secure_sql($email);
				
				if (is_real_email_address($email))
				{
					$sql = "SELECT username 
							FROM pm_users 
							WHERE email LIKE '$email'";
				
					if ($result = @mysql_query($sql))
					{
						$row = mysql_fetch_assoc($result);
						mysql_free_result($result);
						$username = $row['username'];
					}
				}
			}
			else
			{
				$username = trim($_POST['username']);
			}
			$pass = $_POST['pass'];

			if (empty($username) && empty($pass))
			{
				$reason = 1;
			}
			else
			{
				if (empty($username))
				{
					$reason = 1;
					
					if ($email != '')
					{
						$reason = 2;
					}
				}
				
				if (empty($pass))
				{
					$reason = 3;
				}
			}
			
			
			if ( ! confirm_login($username, $pass, false) && $username != '' && $pass != '')
			{
				$reason = 4;
			}
			
			if (count($errors) == 0)
			{
				$user_id = username_to_id($username);
				$ban = banlist($user_id);

				if ($ban['user_id'] == $user_id && $user_id != '')
				{				
					$reason = 5;
				}
			}

			if ($reason)
			{
				header('Location: '. _URL_MOBI .'/login.'. _FEXT .'?reason='. $reason);
				exit();
			}
			else 
			{
				$remember = $_POST['remember'];

				if( is_user_account_active('', $username) == 0 )
				{
					header('Location: '. _URL_MOBI .'/login.'. _FEXT .'?reason=6');
					exit();
				}
				else
				{
					log_user_in($username, $pass, $remember, false);
				}
				header('Location: '. _URL_MOBI .'/'. $redir);
				exit();
			}
		}
		else { 
			// show the form. 
			header("Location: ". _URL_MOBI ."/index."._FEXT."?login=1");
			exit();
		}
	break;
	
	case 'activate':
		
		if(is_user_logged_in()) {
			header("Location: ". _URL_MOBI. "/index."._FEXT);
			exit();
		}
		
		$user_id	= (int) $_GET['u'];
		$key		= trim($_GET['key']);
		$success	= 0;

		if($user_id && $key != '')
		{
			$sql = "SELECT * FROM pm_users WHERE id = '".secure_sql($user_id)."'";
			$result = mysql_query($sql);
			$user = mysql_fetch_assoc($result);
			mysql_free_result($result);
			
			if($user == '' || is_array($user) === FALSE)
			{
				$reason = 100;
			}
			elseif($user['power'] != U_INACTIVE)
			{
				$reason = 101;
			}
			elseif($user['activation_key'] == '' || (strcmp($user['activation_key'], $key) != 0))
			{
				$reason = 102;
			}
			else
			{
				$sql = "UPDATE pm_users SET power = '".U_ACTIVE."', activation_key = '' WHERE id = '".$user['id']."'";
				@mysql_query($sql);

				header("Location: ". _URL_MOBI ."/login."._FEXT."?msg=activated");
			}
		}
		
		if ($reason)
		{
			header('Location: '. _URL_MOBI .'/login.'. _FEXT .'?reason='. $reason);
		}
		
		exit();
	break;
	
	case 'register':
		header("Location: ". _URL_MOBI ."/register."._FEXT);
		exit();
	break;
	
	
	case 'logout':
		logout();
		header("Location: ". _URL_MOBI ."/index."._FEXT."?login=2");
	break;
}// end big Switch
exit();
?>