<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: LOGIN.INC.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$data = $errors = array();

// logout
if ( isset($_POST['logout']) && $_POST['logout'] = 'true' )
{
	$clearSession = array('id', 'username', 'password', 'type', 'type_id', 'abilities', 'account');
	foreach($clearSession as $key)
	{
		unset($_SESSION[$key]);
	}
	$data['unlogged'] = true;
}
// remind password
else if ( isset($_POST['remind']) && !empty($_POST['remind']) )
{
	$email = $rlValid -> xSql($_POST['remind']);

	if ( !$rlValid -> isEmail($email) )
	{
		array_push($errors, $iPhone -> pValid($lang['incorrect_email']));
	}

	if ( empty($errors) )
	{
		$account_id = $rlDb -> getOne('ID', "`Mail` = '{$email}'", 'accounts');

		if ( empty($account_id) )
		{
			array_push($errors, $iPhone -> pValid($lang['email_account_not_found']));
		}
	}

	if ( empty($errors) )
	{
		$reefless -> loadClass('Account');
		$profile_info = $rlAccount -> getProfile((int)$account_id);
		$remind_page = $rlDb -> getOne('Path', "`Key` = 'remind'", 'pages');

		$reefless -> loadClass('Mail');
		$mail_tpl = $rlMail -> getEmailTemplate('remind_password_request');

		$hash_key = $reefless -> generateHash();
		$hash = md5($hash_key) . md5($config['security_key']);

		$sql = "UPDATE `" . RL_DBPREFIX . "accounts` SET `Password_hash` = '{$hash_key}' WHERE `ID` = '{$account_id}' LIMIT 1";
		$rlDb -> query( $sql );

		$link = RL_URL_HOME;
		$link .= $config['mod_rewrite'] ? $remind_page .'.html?hash='. $hash : '?page='. $remind_page .'&hash='. $hash;
		$link = '<a href="'. $link .'">'. $link .'</a>';

		$mail_tpl['body'] = str_replace(array('{link}', '{username}'), array($link, $profile_info['Full_name']), $mail_tpl['body']);
		$rlMail -> send($mail_tpl, $email);

		$data['remind'] = true;
		$data['message'] = $iPhone -> pValid($lang['remind_password_request_sent']);
	}
	else
	{
		$data['remind'] = false;
		$data['message'] = implode("\n", $errors);
	}
}
// login
else
{
	if ( !$rlAccount -> isLogin() )
	{
		$username = $rlValid -> xSql($_POST['username']);
		$password = $rlValid -> xSql($_POST['password']);
		$data = $iPhone -> login($username, $password);
	}
	else
	{
		$data['logged'] = true;
	}
}

$iPhone -> printAsXml($data);