<?
	//session_start();
	include ('/var/www/html/track/init.php');
	include ('fn_common.php');
	include ('/var/www/html/track/tools/email.php');
	loadLanguage($gsValues['LANGUAGE']);
	echo 'entra';

	

	if ($_REQUEST['cmd'] == 'recover_url_app')
	{

		echo "hola desde casa";
		
		
		$email = $_REQUEST['email'];
		// $seccode = $_POST['seccode'];
		
		if ($email != "")
		{
			// if ($seccode == $_SESSION["seccode"])
			// {
				$email = strtolower($email);
				
				$q = "SELECT * FROM `gs_users` WHERE `email`='".$email."' AND `privileges` NOT LIKE ('%subuser%') LIMIT 1";
				$r = mysqli_query($ms, $q);
				$num = mysqli_num_rows($r);
				
				if ($num > 0)
				{
					$row = mysqli_fetch_array($r);
					 
					$token = genAccountRecoverToken($email);
					
					$url_recover = $gsValues['URL_ROOT'].'/index.php?cmd=recover&token='.$token;

				   //$url_recover = $gsValues['URL_ROOT'].'/recoverpass.php?cmd=recover&token='.$token;

					
					$template = getDefaultTemplate('account_recover_url', $gsValues['LANGUAGE']);
					
					$subject = $template['subject'];
					$message = $template['message'];
					
					$subject = str_replace("%SERVER_NAME%", $gsValues['NAME'], $subject);
					$subject = str_replace("%URL_RECOVER%", $url_recover, $subject);
					
					$message = str_replace("%SERVER_NAME%", $gsValues['NAME'], $message);
					$message = str_replace("%URL_RECOVER%", $url_recover, $message);
					
				
						
					if (sendEmail($email, $subject, $message))
					{
						// inset token
						$q = "INSERT INTO `gs_user_account_recover` (`token`, `email`, `dt_recover`) VALUES ('".$token."','".$email."','".gmdate("Y-m-d H:i:s")."')";
						$r = mysqli_query($ms, $q);
				
						echo 'OK';
						
						//write log
						writeLog('user_access', 'User recover: URL sent. E-mail: '.$email);
					}
					else
					{
						echo 'ERROR_NOT_SENT';
					}
					

				}
				else
				{
					echo 'ERROR_EMAIL_NOT_FOUND';
					
					//write log
					writeLog('user_access', 'User recover: no such e-mail. E-mail: '.$email);
				}
			// }
			// else
			// {
			// 	echo 'ERROR_INCORRECT_SECURITY_CODE';
			// }
		}
		
		die;
	}
	
	
?>
