<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP 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-2011 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('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
// define meta tags & common variables
$meta_title = $lang['login']." - "._SITENAME;
$meta_description = '';
// end

// Initialize some variables
$errors = array();
$nr_errors = 0;
$logged_in = 0;
// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$top_videos = top_videos($config['top_videos_sort'], _TOPVIDS);
$new_videos = list_videos('added', _NEWVIDS);
$smarty->assign('new_videos', $new_videos);
$smarty->assign('top_videos', $top_videos);
$smarty->register_function('list_categories', 'list_categories');

$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
				
$mode = '';
$success = 0;
$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. "/index."._FEXT);
		exit();
		}
		// get the last referer so that we can redirect the user to his last visited page after logging him in.
		$redir = get_last_referer();
		if( $redir === false){ 	
			$redir = _URL.'/index.'._FEXT;
		}

		//	check if the form has been submitted
		if( isset($_POST['Login'])) {
			$username = (get_magic_quotes_gpc()) ? stripslashes(trim($_POST['username'])) : trim($_POST['username']);
			$username = secure_sql($username);
			$pass = $_POST['pass'];

			if(empty($username) || $username == '') 
				$errors['username'] = $lang['login_msg1'];
			if(empty($pass) || $pass == '')
				$errors['pass'] = $lang['login_msg2'];
				
			if( !confirm_login($username, $pass, false) && $username != '' && $pass != ''){
				$errors[] = $lang['login_msg3'];
			}
			//	check banlist;
			$user_id = username_to_id($username);
			$ban = banlist($user_id);
			if($ban['user_id'] == $user_id && $user_id != '')
			{				
				$errors[] = sprintf($lang['login_msg16'], $ban['reason']);
			}
			$nr_errors = count($errors);
			if( $nr_errors > 0 ){
				// errors were encountered while processing form	
				$smarty->assign('errors', $errors);
				$smarty->display('login.tpl');
				exit();
			}
			else {
				// this means everything is ok!
				// log him in.
				$remember = $_POST['remember'];

				if( is_user_account_active('', $username) == 0 )
				{
					$errors[] = $lang['login_msg4'];
					$smarty->assign('success', 0);
					$smarty->assign('errors', $errors);
				}
				else
				{
					log_user_in($username, $pass, $remember, false);
					$smarty->assign('success', 1);
					$redir_msg = sprintf($lang['login_msg15'], $redir);
					$smarty->assign('redir', $redir);
					$smarty->assign('redir_msg', $redir_msg);
				}
				$smarty->display('login.tpl');
				exit();
			}
		}
		else { 
			// show the form. 
			$smarty->assign('ref', $redir);
			$smarty->display('login.tpl');
			exit();
		}
	break;
	
	
	case 'register':
		header("Location: " ._URL. "/register."._FEXT);
		exit();
	break;
	
	
	case 'logout':
		logout();
		header("Location: " ._URL. "/index."._FEXT);
	break;
	
	
	case 'forgot_pass':
		if(is_user_logged_in()) { 
			logout();
		}
//		if($_POST['Send'] == "Send") { 
		if(isset($_POST['Send'])){
			$username = trim($_POST['username']);
			$email = trim($_POST['email']);

			foreach($_POST as $k => $v)
			{
				$_POST[$k] = htmlspecialchars($v);
			}
			if( empty($email) || empty($username) ) {
				if( empty($email) ) 	$errors['email'] = $lang['login_msg5'];
				if( empty($username) )	$errors['username'] = $lang['login_msg6'];
				
				$smarty->assign('errors', $errors);
				$smarty->assign('success', 0);
				$smarty->display('forgot_pass.tpl');
				exit();
			}
			
			$var = validate_email($email);
			
			if($var == 1) {
				$errors['email'] = $lang['register_err_msg2'];
			}
			if($var === false) {
				$errors['email'] = $lang['login_msg7'];
			}
			if($var == 2) {
			//	this means that the email address was found in DB.
				$sql = "SELECT * FROM pm_users WHERE email LIKE '".$email."'";
				$result = @mysql_query($sql);
				$user = @mysql_fetch_assoc($result);
				@mysql_free_result($result);
				
				if( strcmp($user['username'], $username) != 0 )
				{
					$errors['username'] = $lang['login_msg8'];
				}
				elseif( $user['power'] == U_INACTIVE )
				{
					$errors[] = $lang['login_msg4'];
				}
				else
				{
					$new_pass = array();
					$new_pass = reset_password($email);
					
					if( ! $new_pass ) {
						$errors[] = $lang['login_msg9'];
						$smarty->assign('errors', $errors);
						$smarty->assign('success', 0);
						$smarty->display('forgot_pass.tpl');
						exit();
					}
					else	//	Send a email with the new password and the confirmation link;
					{
						$activation_link  =    _URL;
						$activation_link .=    "/login." . _FEXT;
						$activation_link .=    "?do=pwdreset&u=" . $user['id'] . "&key=" . $new_pass['key'];
						
						// ** SENDING EMAIL ** //
						$ip = addslashes($_SERVER['REMOTE_ADDR']);
						
						require_once("include/class.phpmailer.php");
							//*** DEFINING E-MAIL VARS
							$mailsubject = sprintf($lang['mailer_subj3'], _SITENAME);
							
							$array_content[]=array("mail_new_pass", $new_pass['pass']);  
							$array_content[]=array("mail_ip", $ip);
							$array_content[]=array("mail_sitename", _SITENAME);
							$array_content[]=array("mail_url", _URL);
							$array_content[]=array("mail_activation_link", $activation_link);
							//*** END DEFINING E-MAIL VARS
							if(file_exists('./email_template/'.$language.'/email_forgot_password.html'))
							{
								$mail = send_a_mail($array_content, $email, $mailsubject, 'email_template/'.$language.'/email_forgot_password.html');
							}
							elseif(file_exists('./email_template/english/email_forgot_password.html'))
							{
								$mail = send_a_mail($array_content, $email, $mailsubject, 'email_template/english/email_forgot_password.html');
							}
							elseif(file_exists('./email_template/email_forgot_password.html'))
							{
								$mail = send_a_mail($array_content, $email, $mailsubject, 'email_template/email_forgot_password.html');
							}
							else
							{
								@log_error(secure_sql('Error: Email template "email_forgot_password.html" not found!'), 'User Login Page', 1);
								$mail = TRUE;
							}
							if($mail !== TRUE)
							{
								@log_error($mail, 'User Login Page', 1);
							}
									
						// ** END SENDING EMAIL ** //
						$smarty->assign('success', 1);
						$smarty->display('forgot_pass.tpl');
						exit();
					}
				}
			}// end if($var == 2);
			$smarty->assign('errors', $errors);
			$smarty->assign('success', 0);
			$smarty->display('forgot_pass.tpl');
			exit();
		}// end if $_POST['send'] == 'send'
		else{ 
			// show the form;
			$smarty->assign('success', 0);
			$smarty->display('forgot_pass.tpl');
			exit();
		}
	break;
	case 'pwdreset':
	case 'activate':
		//	check if user is already logged in
		//	if he already is, redirect him to index page
		if(is_user_logged_in()) {
			header("Location: "._URL. "/index."._FEXT);
			exit();
		}
		
		$user_id	= (int) $_GET['u'];
		$key		= trim($_GET['key']);
		$success	= 0;

		if($user_id == '' || $key == '')
		{
			$errors[] = 'Invalid request.';
		}
		else
		{
			$sql = "SELECT * FROM pm_users WHERE id = '".secure_sql($user_id)."'";
			$result = mysql_query($sql) or die(mysql_error());
			if( ! $result )
			{
				echo $lang['login_msg11'].' <em>' . $config['contact_mail'] . '</em>';
				exit();
			}
			$user = mysql_fetch_assoc($result);
			mysql_free_result($result);
			
			if($mode == 'activate')
			{
				if($user == '' || is_array($user) === FALSE)
				{
					$errors[] = $lang['login_msg12'];
				}
				elseif($user['power'] != U_INACTIVE)
				{
					$errors[] = $lang['login_msg13'];
				}
				elseif($user['activation_key'] == '' || (strcmp($user['activation_key'], $key) != 0))
				{
					$errors[] = $lang['login_msg14'];
				}
				else
				{
					$sql = "UPDATE pm_users SET power = '".U_ACTIVE."', activation_key = '' WHERE id = '".$user['id']."'";
					$result = @mysql_query($sql);
					if( ! $result )
					{
						$errors[] = $lang['login_msg11'].' <em>' . $config['contact_mail'] . '</em>';
					}
					else $success = 1;
				}
			}
			elseif($mode == 'pwdreset')
			{
				if($user == '' || is_array($user) === FALSE)
				{
					$errors[] = $lang['login_msg12'];
				}
				elseif($user['activation_key'] == '' || (strcmp($user['activation_key'], $key) != 0))
				{
					$errors[] = $lang['login_msg14'];
				}
				else
				{
					$sql = "UPDATE pm_users SET password = '".$user['new_password']."', activation_key = '', new_password = '' WHERE id = '".$user['id']."'";
					$result = @mysql_query($sql);
					if( ! $result )
					{
						$errors[] = $lang['login_msg11'].' <em>' . $config['contact_mail'] . '</em>';
					}
					else $success = 1;
				}
			}
		}
		
		$smarty->assign('errors', $errors);
		$smarty->assign('success', $success);
		if($mode == 'activate')
		{
			$smarty->display('activate_account.tpl');
		}
		elseif($mode == 'pwdreset')
		{
			$smarty->display('activate_password.tpl');
		}
		exit();
	break;	

}// end big Switch
exit();
?>