<?php
/**
 * @package Flatnux_module_login
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2011
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 */
/**
 *
 * @global array $_FN
 * @return  
 */
function FNREG_ManageRecovery()
{
	global $_FN;

	$postuser = FN_GetParam("username",$_POST,"html");
	$getuser = FN_GetParam("user",$_GET,"html");
	$rnd = FN_GetParam("rnd",$_GET,"html");
	// step 1 generate rnd and send -------->
	if ($postuser != "")
	{
		$err = "";
		if (!FN_GetUser($postuser))
			$err .= $postuser." : ".fn_i18n("user not exists")."<br />";
		if ($err != "")
		{
			echo "$err<br />";
			$tit = FN_i18n("back");
			echo "<a accesskey=\"".FN_GetAccessKey($tit,"javascript:history.back()")."\" href=\"javascript:history.back()\">&lt;&lt;".$tit."</a>";
			return;
		}
		$uservalues = FN_GetUser($postuser);
		$newrnd = md5(rand(1,99999999));
		FN_UpdateUser($postuser,array("rnd"=>$newrnd));
		$server = $_FN['siteurl']."index.php?mod=login&op=recovery&user=$postuser&rnd=$newrnd&lang={$_FN['lang']}";
		$body = fn_i18n("password recovery")."\n"."\n".fn_i18n("to retrieve the password, please follow this link").":\n$server\n\n".fn_i18n("it will come generated one new password and it will come sended to your e-mail address");
		//send mail
		FN_SendMail($uservalues['email'],$_FN['sitename']." - ".FN_i18n("password recovery"),$body,false);
		FN_Log("User $postuser recover password step 1.");
		echo "<b>".fn_i18n("a new passowrd will be sent to your e-mail address.")."</b>";
		$home = "<br /><br /><br /><a href=\"".FN_RewriteLink("index.php")."\">".fn_i18n("next")." &gt;&gt;</a>";
		$ak = FN_GetAccessKey($home,"index.php");
		echo "<a accesskey=\"$ak\" href=\"index.php\">$home</a>";
	}
	// step 1 generate rnd and send ---------<
	// step 2 generate password and send ---->
	elseif ($getuser != "" && $rnd != "")
	{

		$uservalues = FN_GetUser($getuser);
		if (!$uservalues)
		{
			echo FN_i18n("user not exists")."<br />";
			return;
		}
		if ($uservalues['rnd'] != $rnd)
		{
			echo FN_i18n("authentication failure")."<br />";
			return;
		}
		$chrs = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
		$newpass = "";
		for ($i = 0; $i < 10; $i++)
		{
			$ch = rand(0,35);
			$newpass .= $chrs[$ch];
		}
		$uservalues['passwd'] = $newpass;
		$body = fn_i18n("password recovery")."\n"."\n".fn_i18n("this is the new password")." : $newpass\n\n";
		FN_UpdateUser($getuser,array("passwd"=>$newpass));
		//send mail
		FN_SendMail($uservalues['email'],$_FN['sitename']." - ".FN_i18n("password recovery"),$body,false);
		FN_Log("User $getuser recover password. step 2, password sended");
		echo FN_Translate("a new passowrd will be sent to your e-mail address").".<br />";
		return;
	}
	// step 2 generate password and send ----<
	//request recovery password ---->
	else
	{
		echo "<form action=\"".FN_RewriteLink("?mod=login&amp;op=recovery")."\" method=\"post\">";
		echo "<h2>".FN_Translate("recovery password")."</h2>";
		echo FN_Translate("it will come generated one new password and it will come sended to your e-mail address");
		if ($_FN['username_is_email'])
		{
			echo "<br /><br />".fn_Translate("email").":";
		}
		else
		{
			echo "<br /><br />".fn_Translate("username").":";
		}
		echo "&nbsp;<input size=\"20\" name=\"username\" type=\"text\" />";
		echo "&nbsp;<input type=\"submit\" class=\"submit\" value=\"".FN_Translate("send")."\" />";
		$tit = FN_Translate("cancel");
		echo "<br /><br /><input accesskey=\"".FN_GetAccessKey($tit,"javascript:history.back()")."\" onclick=\"javascript:history.back()\"  class=\"button\" type=\"button\" value=\"$tit\" />";
		echo "</form>";
	}
	//request recovery password ----<
}

/**
 *
 * @global array $_FN
 * @param string $user
 */
function FNREG_ManageEditRegister($user = "")
{
	global $_FN;
	$errors = array();
	$form = FN_XmlForm("fn_users");
	$form->SetLayout("table");
	$form->formvals['passwd']['frm_required'] = false;
	if ($user == "")
		$user = $_FN['user'];
	$newvalues = FN_GetUser($user);
	$uservalues = $newvalues;

	$oldvalues = $newvalues;
	$reg_ok = false;
	if ($oldvalues)
	{
		if (isset($_POST['updateuser']))
		{
			foreach ($form->formvals as $key=>$value)
			{
				if (isset($value['type']) && ($value['type'] == 'image' || $value['type'] == 'file'))
				{
					if (isset($_FILES[$key]['name']))
					{
						if ($_FILES[$key]['name'] != "")
						{
							$newvalues[$key] = FN_GetParam("name",$_FILES[$key]);
						}
					}
					if (isset($_POST["__isnull__$key"]))
					{
						$newvalues[$key] = "";
					}
				}
				else
				{
					if (isset($_POST[$key]))
						$newvalues[$key] = FN_GetParam($key,$_POST,"html");
				}
			}
			//dprint_r($_POST);
		}
		$newvalues['username'] = $oldvalues['username'];
		if (!FN_IsAdmin())
		{
			$newvalues['email'] = $oldvalues['email'];
			$newvalues['active'] = $oldvalues['active'];
			$newvalues['level'] = $oldvalues['level'];
			$newvalues['group'] = $oldvalues['group'];
		}
		else
		{
			$newvalues['active'] = FN_GetParam("active",$_POST,"html");
			$newvalues['level'] = FN_GetParam("level",$_POST,"html");
			$groups = FN_GetGroups();
			$insgroup = array();
			foreach ($groups as $g)
			{
				if (isset($_POST['group-'.$g]))
				{
					$insgroup[] = $g;
				}
			}
			$newvalues['group'] = implode(",",$insgroup);
		}
		if (isset($_POST['updateuser']))
		{
			$errors = $form->Verify($newvalues,true);
			if (count($errors) == 0)
			{
				$reg_ok = true;
			}
		}
		//dprint_r($errors);
		if (!$reg_ok)
		{
			echo FN_HtmlOpenTableTitle(FN_Translate("edit profile"));
			$us = "";
			if (FN_IsAdmin() && $user != $_FN['user'])
				$us = "&user=$user";
			echo "<form enctype=\"multipart/form-data\" action=\"?mod=".$_FN['mod']."&amp;op=editreg$us\" method=\"post\" name=\"register\" >\n<table style=\"margin:auto\">";
			$form->SetlayoutTemplate("
<!-- contents -->
<!-- group -->
	<tr><td colspan=\"2\" style=\"text-align:center\"><b>{groupname}</b></td></tr>
<!-- end_group -->
<!-- item -->
	<tr>
		<td valign=\"top\">{title}<!-- error --><span style=\"color:red\"><br />{error}</span><!-- end_error --></td>
		<td valign=\"top\">{input}</td>
	</tr>
<!-- end_item -->
<!-- endgroup -->
<!-- end_endgroup -->
<!-- end_contents -->
");
			$form->ShowUpdateForm($newvalues['username'],FN_IsAdmin(),false,$errors);
			if (FN_IsAdmin())
			{
				//level-->
				echo "\n<tr>\n\t<td>".FN_Translate("level")."</td>";
				echo "\n\t<td>";
				echo "<select name=\"level\">";
				for ($i = 0; $i < 11; $i++)
				{
					$s = ($oldvalues['level'] == $i) ? "selected=\"selected\"" : "";
					echo "<option $s value=\"$i\" >$i</option>";
				}
				echo "</select>";
				echo "</td>\n</tr>";
				//level--<
				//group-->
				echo "\n<tr>\n\t<td>".FN_Translate("group")."</td>";
				echo "\n\t<td>";
				$groups = FN_GetGroups();
				//dprint_r($newvalues);
				$group = $uservalues['group'];
				$grlist = explode(",",$group);
				foreach ($groups as $g)
				{
					$ck = in_array($g,$grlist) ? "checked=\"true\"" : "";
					echo "<input $ck name=\"group-$g\" value=\"users\" type=\"checkbox\" />$g<br />";
				}
				echo "</td>\n</tr>";
				//group--<
			}
			echo "\n<tr>\n\t<td colspan=\"2\">";
			echo "<input name=\"updateuser\" value=\"1\" type=\"hidden\" />";
			echo "<button type=\"submit\">".FN_Translate("save")."</button>";
			echo "<button onclick=\"window.location='".$_FN['siteurl']."index.php?mod=".$_FN['mod']."';return false;\">".FN_Translate("cancel")."</button>";
			echo "</td>\n</tr>\n";
			echo "</table>\n";
			echo "\n</form>";
			echo FN_HtmlCloseTableTitle();
		}
		else
		{

			//dprint_r($newvalues);
			//die();
			if (false != FN_UpdateUser($newvalues['username'],$newvalues))
			{
				//FN_Log("User updated:{$newvalues['username']}");
				echo FN_Translate("the data were successfully updated");
				FN_Login($newvalues['username']);
			}
			else
			{
				echo FN_Translate("error");
				echo "<br /><br /><a href=\"javascript:history.back()\">&lt;&lt; ".FN_Translate("back")."</a>";
			}
			echo "<br /><br /><a href=\"".FN_RewriteLink("index.php?mod={$_FN['mod']}")."\">".FN_Translate("next")." &gt;&gt;</a>";
		}
	}
}

/**
 *
 * @global array $_FN
 * @return bool
 */
function FNREG_ManageRegister($actionform = "")
{
	global $_FN;
	$config = FN_LoadConfig("modules/login/config.php");
	$form = FN_XmlForm("fn_users");
	$op = FN_GetParam("op",$_GET);
	$conditions = FN_XMLQuery("SELECT * FROM fn_conditions WHERE enabled LIKE '1' ORDER BY position");
	if ($op == "end_reg")
	{
		$user = trim(ltrim(FN_GetParam("user",$_GET,"html")));
		$id = trim(ltrim(FN_GetParam("id",$_GET,"html")));
		$uservalues = FN_GetUser($user);
		if ($user != "" && $id != "" && isset($uservalues['rnd']) && $uservalues['rnd'] == $id)
		{
			//complete registration-------------------------------------------->
			$uservalues['active'] = 1;
			unset($uservalues['passwd']);
			FN_UpdateUser($user,$uservalues);
			echo FN_Translate("registration has been completed");
			$mailbody = FNREG_GetWelcomeMessage();
			$mailbody = str_replace("!USERNAME!",$uservalues['username'],$mailbody);
			$mailbody = str_replace("!SITENAME!",$_FN['sitename'],$mailbody);
			$mailbody = str_replace("!SITEURL!",$_FN['siteurl'],$mailbody);
			$mailbody = FN_FixNewline($mailbody);
			$ishtml = false;
			FN_SendMail($uservalues['email'],FN_Translate("confirm registration site")." ".$_FN['sitename'],$mailbody,$ishtml);
			return true;
			//complete registration--------------------------------------------<
		}
		else
		{
			echo FN_Translate("error registration");
		}
		return false;
	}
	$newvalues = $form->getbypost();
	foreach ($form->formvals as $key=>$value)
	{
		if (isset($value['type']) && ($value['type'] == 'image' || $value['type'] == 'file') && isset($_FILES[$key]['name']))
		{
			if ($_FILES[$key]['name'] != "")
			{
				$newvalues[$key] = FN_GetParam("name",$_FILES[$key]);
			}
		}
		else
		{
			$newvalues[$key] = FN_GetParam($key,$_POST,"html");
		}
	}
	if (!empty($_FN['registration_by_email']))
	{
		$newvalues['active'] = 0;
	}
	else
	{
		$newvalues['active'] = 1;
	}
	$newvalues['level'] = 0;

	if (!empty($_FN['username_is_email']))
	{
		$form->formvals['username']['frm_show'] = 0;
		$form->formvals['username']['frm_required'] = 0;
		$form->formvals['email']['unique'] = 1;
		$form->formvals['username']['frm_validchars'].="01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-@.";
		if (!empty($newvalues['email']))
		{
			$newvalues['username'] = $newvalues['email'];
		}
	}
	$rnd = $newvalues['rnd'] = md5(rand(1000000000,9999999999)).md5(rand(1000000000,9999999999));
	$newvalues['ip'] = FN_GetParam("REMOTE_ADDR",$_SERVER,"html");
	$insgroup = array(0=>"users");
	if (FN_IsAdmin())
	{
		$newvalues['active'] = FN_GetParam("active",$_POST,"html");
		$newvalues['level'] = FN_GetParam("level",$_POST,"html");
		$groups = FN_GetGroups();
		foreach ($groups as $g)
		{
			if (isset($_POST['group-'.$g]))
			{
				$insgroup[] = $g;
			}
		}
	}
	else
	{
		$newvalues['active'] = ( $_FN['registration_by_email'] == 1 ) ? 0 : 1;
	}
	$newvalues['group'] = implode(",",$insgroup);
	$errors = array();
	if (isset($_POST['passwd']))
	{
		$errors = $form->Verify($newvalues);
		$conditions_ok = true;
		foreach ($conditions as $condition)
		{
			if (empty($condition['optional']) && empty($_POST['conditions_'.$condition['id']]))
			{
				$conditions_ok = false;
			}
		}

		//dprint_r($errors);
		//---check captcha----------------------------------------------------->

		$captcha_ok = true;
		if ($config['enable_captcha'])
		{
			$captcha = FN_GetSessionValue("captcha");
			$security_code = FN_GetParam("security_code",$_POST);
			if (empty($captcha['security_code']) || $captcha['security_code'] != $security_code)
			{
				$captcha_ok = false;
			}
		}
		//---check captcha-----------------------------------------------------<

		if (count($errors) == 0 && $conditions_ok == true && $captcha_ok == true)
		{
			$email = $newvalues['email'];
			$name = $newvalues['username'];
			if (!empty($_FN['registration_by_email']) && $newvalues['active'] != 1)
			{

				$link = FN_RewriteLink("index.php?mod=".$_FN['mod']."&op=end_reg&user=".urlencode($newvalues['username'])."&id=$rnd","&",true);
				$confirmation_message = FNREG_GetConfirmationMessage();
				$mailbody = str_replace("!CONFIRMREGISTRATIONADDRESS!",$link,$confirmation_message);
				$mailbody = str_replace("!USERNAME!",$name,$mailbody);
				$mailbody = str_replace("!SITENAME!",$_FN['sitename'],$mailbody);
				$mailbody = str_replace("!SITEURL!",$_FN['sitename'],$mailbody);

				if (!strstr($mailbody,$link))
					$mailbody .= "\n$link";
				$mailbody = FN_FixNewline($mailbody);
				$ishtml = false;

				if (FN_AddUser($newvalues) && FN_SendMail($email,$_FN['sitename']." ".FN_Translate("registration confirmation"),$mailbody,false))
				{
					echo "<br /><br />".FN_Translate("it has been sended one email to you to the address")." ".htmlentities($newvalues['email'])." ".FN_i18n("with the instructions to complete the registration")."<br />";
					echo FN_Translate("check your inbox");
					echo "<br /><br /><div ><a href=\"".FN_RewriteLink("index.php")."\" >".FN_Translate("next")." &gt;&gt;&gt;</a></div>";
					return false;
				}
				else
				{
					echo FN_Translate("error");
				}
			}
			else
			{
				echo "<br />".FN_Translate("user already confirmed")."<br />";
				FN_AddUser($newvalues);
				return true;
			}
		}
	}
//--------------------registration form---------------------------------------->
	if ($actionform == "")
	{
		$actionform = FN_RewriteLink("index.php?mod=".$_FN['mod']."&amp;op=register");
	}
	echo "<form enctype=\"multipart/form-data\" action=\"$actionform\" method=\"post\" name=\"register\" >\n";
	$form->ShowInsertForm(FN_IsAdmin(),$newvalues,$errors);
	//----------------captcha--------------->
	if ($config['enable_captcha'])
	{
		FN_SetSessionValue("captcha",array("security_code"=>rand(1000,9999)));
		echo "<b>".FN_Translate("enter this security code")." : </b><img style=\"vertical-align:middle\" src=\"{$_FN['siteurl']}captcha.php\" alt=\"\" title=\"\" /> <input size=\"4\" name=\"security_code\"  value = \"\" />";
		if (isset($_POST['security_code']) && $security_code != $captcha['security_code'])
			echo " <span style=\"background-color:#ffffff;color:red\">".FN_Translate("incorrect security code")."</span>";
		echo "<br />\n";
	}
	//----------------captcha---------------<
	//----------------conditions------------>
	if (is_array($conditions) && count($conditions) > 0)
	{
		$tcond = FN_XmlForm("fn_conditions");
		foreach ($conditions as $condition)
		{
			$condition = $tcond->GetRecordTranslatedByPrimarykey($condition['id']);
			if (!empty($condition['title']))
				echo "<b>".$condition['title']."</b><br />";
			echo "<div style=\"height:100px;overflow:auto;border:1px inset\" >";
			echo $condition['text'];
			echo "</div>";
			echo "<input name=\"conditions_{$condition['id']}\" type=\"checkbox\" />".FN_Translate("accept");
			if (empty($condition['optional']) && empty($_POST['conditions_'.$condition['id']]) && isset($_POST['passwd']))
				echo " <span style=\"background-color:#ffffff;color:red\">".FN_Translate("to register is required to accept")."</span>";
		}
	}
	//----------------conditions------------<
	echo "<table>";
	if (FN_IsAdmin())
	{
		//level-->
		echo "\n<tr>\n\t<td>".FN_Translate("level")."</td>";
		echo "\n\t<td>";
		echo "<select name=\"level\">";
		for ($i = 0; $i < 11; $i++)
		{
			echo "\n<option value=\"$i\" >$i</option>";
		}
		echo "</select>";
		echo "</td>\n</tr>";
		//level--<
		//group-->
		echo "\n<tr>\n\t<td>".FN_Translate("group")."</td>";
		echo "\n\t<td>";
		$groups = FN_GetGroups();
		$group = "users";
		$grlist = explode(",",$group);
		foreach ($groups as $g)
		{
			$ck = in_array($g,$grlist) ? "checked=\"true\"" : "";
			print("<input $ck name=\"group-$g\" value=\"$group\" type=\"checkbox\" />$g<br />");
		}
		echo "</td>\n</tr>";
		//group--<
	}
	echo "\n<tr>\n\t<td colspan=\"2\">";
	echo "<button type=\"submit\">".FN_i18n("save")."</button>";
	echo "<button onclick=\"window.location='".$_FN['siteurl']."index.php?mod=".$_FN['mod']."';return false;\">".FN_i18n("cancel")."</button>";
	echo "</td>\n</tr>\n";
	echo "</table>\n";
	echo "\n</form>";
//--------------------registration form----------------------------------------<
	return false;
}

/**
 *
 * @global array $_FN
 * @return string
 */
function FNREG_GetConfirmationMessage()
{
	global $_FN;
	if (!file_exists("{$_FN['datadir']}/messages/ConfirmationMessage.{$_FN['lang']}.txt"))
	{
		return FN_i18n("to complete the registration click on the following link").":\n!CONFIRMREGISTRATIONADDRESS!";
	}
	else
	{
		return file_get_contents("{$_FN['datadir']}/messages/ConfirmationMessage.{$_FN['lang']}.txt");
	}
}

/**
 *
 * @global array $_FN
 * @return string
 */
function FNREG_GetWelcomeMessage()
{
	global $_FN;
	if (!file_exists("{$_FN['datadir']}/messages/WelcomeMessage.{$_FN['lang']}.txt"))
	{
		return FN_i18n("welcome !USERNAME!, you are now registered to").": !SITENAME!\n\n!SITEURL!";
	}
	else
	{
		return file_get_contents("{$_FN['datadir']}/messages/WelcomeMessage.{$_FN['lang']}.txt");
	}
}

//---------ux_conditions--------------------------------------->
/**
 *
 * @author alessandro
 *
 */
class xmldbform_field_conditions_check
{
	function xmldbform_field_conditions_check()
	{
		
	}

	function show($params)
	{
		global $_FN;
		$this->starttagtitle = isset($params['frm_starttagtitle']) ? $params['frm_starttagtitle'] : "<tr><td  valign=\"top\" >";
		$this->endtagtitle = isset($params['frm_endtagtitle']) ? $params['frm_endtagtitle'] : "</td>";
		$this->starttagvalue = isset($params['frm_starttagvalue']) ? $params['frm_starttagvalue'] : "<td valign=\"top\" >";
		$this->endtagvalue = isset($params['frm_endtagvalue']) ? $params['frm_endtagvalue'] : "</td></tr>";
		$lang_user = $_FN['lang'];
		if (!file_exists($_FN['datadir']."/conditions"))
		{
			mkdir($_FN['datadir']."/conditions");
		}
		$file_conditions = $_FN['datadir']."/conditions/conditions.$lang_user.html";
		if (!file_exists($file_conditions))
		{
			if (file_exists("modules/login/conditions/conditions.$lang_user.html"))
			{
				FN_Copy("modules/login/conditions/conditions.$lang_user.html",$file_conditions);
			}
		}
		$toltips = "";
		$strhiddenfield = $params['strhiddenfield'];
		$oldval = $params['value'];
		echo $this->starttagtitle.$params['title'].$this->endtagtitle;
		echo $this->starttagvalue;
		$ch = "";
		if ($oldval != "")
			$ch = "checked=\"checked\"";
		if ($oldval != "yes")
		{
			$ch = "";
		}
		echo "\n$strhiddenfield ";
		echo "<input type=\"hidden\" value=\"".htmlspecialchars($oldval)."\" name=\"__check__".$params['name']."\"  />";
		echo "<div style=\"border:1px inset;overflow:auto;height:100px;width:100%;\" >";
		echo file_get_contents($file_conditions);
		echo "</div>";
		echo "<input name=\"".$params['name']."\" type=\"hidden\" value=\"no\" />";
		echo "<br />".FN_Translate("I accept")."&nbsp;<input style=\"vertical-align:middle\" $toltips $ch type=\"checkbox\" value=\"yes\" name=\"".$params['name']."\"  />";
		echo $this->endtagvalue;
	}

}
/**
 * contitions field validator
 * @param $value
 * @return misc true or error message
 */
function fn_accept_condition_validator($value)
{
	if ($value != "yes")
	{
		return FN_Translate("you must accept the terms of use");
	}
	else
		return true;
}

//---------ux_conditions---------------------------------------<
?>