<?php
/**
 * @package Flatnux_module_contacts
 * @author Alessandro Vernassa <speleoalex@gmail.com>
 * @copyright Copyright (c) 2011
 * @license http://opensource.org/licenses/gpl-license.php GNU General Public License
 *
 */
defined('_FNEXEC') or die('Restricted access');
global $_FN;
//session_start();
if (!file_exists("{$_FN['datadir']}/{$_FN['database']}/contact_message.php"))
    INS_InitTable("contact_message");
INS_GestInsert("contact_message");
/**
 *
 * @global array $_FN
 * @param string $tablename
 * @param array $params
 */
function INS_GestInsert($tablename, $params=array())
{
    global $_FN;
    $captcha = FN_GetParam("captcha", $_POST, "flat");
    $config = FN_LoadConfig("modules/contacts/config.php");
    $savemessage = $config['savemessage'];
    $usermail = $config['usermail'];
    if ($usermail == "")
    {
        $usermail = $_FN['log_email_address'];
    }
    $Table = FN_XmlForm($tablename);
    $Table->setlayout("flat");
    $newvalues = false;
    $errors = array();
    $wrong_antispam = false;
    $captcha = FN_GetSessionValue("captcha");
    $captcha = FN_GetParam("security_code", $captcha);
    $security_code = FN_GetParam("security_code", $_POST);
    if (count($_POST) > 0)
    {
        $newvalues = $Table->GetByPost();
        $newvalues['username'] = $_FN['user'];
        $errors = $Table->Verify($newvalues);
        // checking the value of anti-spam code inserted
        if (empty($security_code) || $captcha != $security_code)
        {
            FN_SetSessionValue("captcha", array("security_code" => ""));
            $wrong_antispam = true;
        }
        if (count($errors) == 0 && !$wrong_antispam)
        {
            //nuovo record---->
            if ($newvalues)
            {
                if ($savemessage == 1)
                {
                    $newvalues['ip'] = FN_GetParam("REMOTE_ADDR", $_SERVER, "html");
                    $Table->InsertRecord($newvalues);
                }
                FN_SendMail($usermail, $newvalues['subject'], FN_Translate("From").": ".$newvalues['contact']."\n\n".$newvalues['message']);
                echo "<br />";
                echo FN_Translate("the message has been send correctly") . "<br />";
                echo "<br /><a href=\"" . FN_RewriteLink("index.php") . "\">" . FN_Translate("next") . " &gt;&gt;</a>";
                FN_SetSessionValue("captcha", array("security_code" => ""));
            }
            else
            {
                echo FN_Translate("error");
                //error
            }
            //nuovo record----<
            return;
        }
    }

    echo "<form enctype=\"multipart/form-data\" action=\"index.php?mod={$_FN['mod']}\" method=\"post\">";
    $Table->ShowInsertForm(false, $newvalues, $errors);
//antispam--------------------------------------------------------------------->
    echo "<div>";
    FN_SetSessionValue("captcha", array("security_code" => rand(1000, 9999)));

    echo "<b>" . FN_Translate("enter the code you see below") . "</b>";
    if ($wrong_antispam)
        echo "<span style=\"color:red\" >" . FN_Translate("wrong anti-spam code") . "</span>" . "*<br />";
    echo "<br /><img style=\"vertical-align:middle\" src=\"captcha.php?t=security_code\" alt=\"antispam code\" />";
    echo " <input type=\"text\" name=\"security_code\" value=\"\" />";
    echo "<br />";
    echo "</div>";
//antispam---------------------------------------------------------------------<

    echo "<button type=\"submit\">" . FN_i18n("sent message") . "</button>";
    echo "<button onclick=\"window.location='" . FN_RewriteLink("index.php?mod={$_FN['mod']}", "&") . "'\" type=\"button\">" . FN_i18n("cancel") . "</button>";
    echo "</form>";
}
function INS_InitTable($tablename)
{
    global $_FN;
    $xml = '<?xml version="1.0" encoding="UTF-8"?>
<?php exit(0);?>
<tables>
	<field>
		<name>id</name>
		<primarykey>1</primarykey>
		<frm_show>0</frm_show>
		<extra>autoincrement</extra>
	</field>
	<field>
		<name>name</name>
		<frm_i18n>name</frm_i18n>
		<frm_help_i18n>insert your name here</frm_help_i18n>
	</field>
	<field>
		<name>contact</name>
		<frm_i18n>email</frm_i18n>
		<frm_validator>FN_CheckMail</frm_validator>
		<frm_required>1</frm_required>
	</field>
	<field>
		<name>telephone</name>
		<frm_i18n>telephone</frm_i18n>
		<frm_required>0</frm_required>
	</field>
	<field>
		<name>subject</name>
		<frm_i18n>subject</frm_i18n>
		<frm_required>1</frm_required>
	</field>
	<field>
		<name>message</name>
		<frm_i18n>message</frm_i18n>
		<type>text</type>
		<frm_cols>60</frm_cols>
		<frm_rows>auto</frm_rows>
		<frm_required>1</frm_required>
	</field>
	<field>
		<name>ip</name>
		<frm_i18n>ip</frm_i18n>
		<frm_show>0</frm_show>
	</field>
</tables>
';
    FN_Write($xml, "{$_FN['datadir']}/{$_FN['database']}/$tablename.php");
}
/**
 *
 * @global array $_FN
 * @param type $values
 * @param type $sep
 * @return type 
 */
function INS_MakeLink($values, $sep="&amp;")
{
    global $_FN;
    if ($values)
        foreach ($values as $k => $v)
        {
            $link[$k] = urlencode($v);
        }
    $linkclean = "";
    foreach ($link as $k => $v)
    {
        if ($v != "")
            $linkclean .= "$sep$k=" . $v;
    }
    $linkclean = FN_RewriteLink("index.php?mod={$_FN['mod']}$linkclean");
    ;
    return $linkclean;
}
?>