<?php
//define this module's name and path
$mn = "odyssey_contact_form";
include("modules/modules.php");

/*
Available variables:
All system classes
All request variables
$p (the template path)
$mp (the module path)
$sufixID (id sufix for this module's divs)
*/

$marginTop = ($moduleParameters['marginTop'] == "__EMPTY__" || empty($moduleParameters['marginTop'])) ? 0 : $moduleParameters['marginTop'];
$marginRight = ($moduleParameters['marginRight'] == "__EMPTY__" || empty($moduleParameters['marginRight'])) ? 0 : $moduleParameters['marginRight'];
$marginBottom = ($moduleParameters['marginBottom'] == "__EMPTY__" || empty($moduleParameters['marginBottom'])) ? 0 : $moduleParameters['marginBottom'];
$marginLeft = ($moduleParameters['marginLeft'] == "__EMPTY__" || empty($moduleParameters['marginLeft'])) ? 0 : $moduleParameters['marginLeft'];
$paddingTop = ($moduleParameters['paddingTop'] == "__EMPTY__" || empty($moduleParameters['paddingTop'])) ? 0 : $moduleParameters['paddingTop'];
$paddingRight = ($moduleParameters['paddingRight'] == "__EMPTY__" || empty($moduleParameters['paddingRight'])) ? 0 : $moduleParameters['paddingRight'];
$paddingBottom = ($moduleParameters['paddingBottom'] == "__EMPTY__" || empty($moduleParameters['paddingBottom'])) ? 0 : $moduleParameters['paddingBottom'];
$paddingLeft = ($moduleParameters['paddingLeft'] == "__EMPTY__" || empty($moduleParameters['paddingLeft'])) ? 0 : $moduleParameters['paddingLeft'];
$useModuleContainer = ($moduleParameters['useModuleContainer'] == "__EMPTY__" || empty($moduleParameters['useModuleContainer'])) ? 0 : (int)$moduleParameters['useModuleContainer'];
$widthInPixels = ($moduleParameters['widthInPixels'] == "__EMPTY__" || empty($moduleParameters['widthInPixels'])) ? null : (int)$moduleParameters['widthInPixels'];

if (empty($marginTop) && empty($marginRight) && empty($marginBottom) && empty($marginLeft)) {
    $Mmargin = "0";
} else {
    $Mmargin = $marginTop . " " . $marginRight . " " . $marginBottom . " " . $marginLeft . " ";
}
if (empty($paddingTop) && empty($paddingRight) && empty($paddingBottom) && empty($paddingLeft)) {
    $Mpadding = "0";
} else {
    $Mpadding = $paddingTop . " " . $paddingRight . " " . $paddingBottom . " " . $paddingLeft . " ";
}


$fieldName = $moduleParameters['fieldName'];
$fieldEmail = $moduleParameters['fieldEmail'];
$fieldPhone = $moduleParameters['fieldPhone'];
$fieldAddress = $moduleParameters['fieldAddress'];
$fieldCity = $moduleParameters['fieldCity'];
$fieldZipCode = $moduleParameters['fieldZipCode'];
$useCaptcha = $moduleParameters['useCaptcha'];
$email_address = $moduleParameters['emailAddress'];

global $domain_name; // get value from config ini


if ($useModuleContainer){
?>
<div id="<?php echo $mn . $sufixID; ?>" data-editable="editable" class="responsive_float">
    <?php
    }
    if (!empty($_POST['info'])) { // check for spam
        header("location: index.php");
        exit;
    }

    if ($_POST['submitContact']) {
        ?>
        <script type="text/javascript">
            jQuery().ready(function () {
                goto('<?php echo $mn . $sufixID;?>');
            });
        </script>
        <?php
        if ($useCaptcha == "1") { // Odyssey Captcha
            $_POST['security_code'] = strtoupper($_POST['security_code']); // μονο κεφαλαια στο capcha
            if ($_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'])) {
                unset($_SESSION['security_code']); // ολα ok με το captcha image
            } else {
                $messages->addError(t("Ο κωδικός ταυτοποίησης που πληκτρολογήσατε δεν είναι σωστός"));
            }
        }
        if ($useCaptcha == "2") { // Google reCaptcha
            if (isset($_POST['g-recaptcha-response'])) $captcha = $_POST['g-recaptcha-response'];
            if (!$captcha) {
                $messages->addError(t("Πρέπει να αποδείξετε πως δεν είστε ρομπότ"));
            } else {
                $secretKey = "6LesLCATAAAAADWqbi8Uro_z_OLzekDuxdAkdzX4";
                $ip = $_SERVER['REMOTE_ADDR'];
                $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $secretKey . "&response=" . $captcha . "&remoteip=" . $ip);
                $responseKeys = json_decode($response, true);
                $hostname = str_replace("www.", NULL, $responseKeys["hostname"]);
                $domainValidated = (strpos($domain_name, $hostname) !== false);
                if (!$domainValidated || intval($responseKeys["success"]) !== 1) {
                    $messages->addError(t("Πρέπει να αποδείξετε πως δεν είστε ρομπότ"));
                }
            }
        }
        if ((int)$fieldName == 1) {
            if (!$fullname) $messages->addError(t("Το ονοματεπώνυμο πρέπει να συμπληρωθεί"));
        }
        if ((int)$fieldEmail == 1) {
            if (!validEmail($email) && !empty($email)) $messages->addError(t("Η διεύθυνση e-mail που πληκτρολογήσατε δεν είναι υπαρκτή"));
        }
        if ((int)$fieldPhone == 1) {
            if (!$phone) $messages->addError(t("Το τηλέφωνο πρέπει να συμπληρωθεί"));
        }
        if ((int)$fieldAddress == 1) {
            if (!$address) $messages->addError(t("Η διεύθυνση πρέπει να συμπληρωθεί"));
        }
        if ((int)$fieldCity == 1) {
            if (!$city) $messages->addError(t("Η πόλη πρέπει να συμπληρωθεί"));
        }
        if ((int)$fieldZipCode == 1) {
            if (!$tk) $messages->addError(t("Ο Τ.Κ. πρέπει να συμπληρωθεί"));
        }
        if (!$message) $messages->addError(t("Πρέπει να πληκτρολογήσετε κάποιο μήνυμα"));

    }

    $hasErrors = $messages->hasErrors;

    if (!$submitContact || $hasErrors) { // display form
        ?>
        <div class="contact_form_description"><?php echo t("Για να επικοινωνήσετε μαζί μας, παρακαλούμε συμπληρώστε την ακόλουθη φόρμα"); ?>.</div>
    <?php
    if ($useCaptcha == "2"){ // Google reCaptcha
    global $langCode;
    ?>
        <script src='https://www.google.com/recaptcha/api.js?hl=<?php echo $langCode; ?>'></script>
    <?php
    }
    ?>
        <form action="index.php" method="post" id="contactForm" class="contactForm" autocomplete="off">
            <?php
            if ($hasErrors) $messages->printSystemMessages(30000, 0);
            ?>
            <div class="formTable">
                <div style="text-align:right; color: grey"><span class="req">*</span><em><?php echo t("Απαραίτητα πεδία"); ?></em></div>
                <?php
                if ((int)$fieldName > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="fullname" class="formLabel left">
                            <?php
                            if ((int)$fieldName == 1) {
                                echo '<span class="req">*</span>';
                            }
                            echo t('Ονοματεπώνυμο'); ?>
                        </label>
                        <div>
                            <input id="fullname" name="fullname" type="text" class="textbox" value="<?php echo str_replace('"', "'", stripslashes($fullname)); ?>">
                            <label for="info" class="required">info</label><input type="text" id="info" name="info" class="required">
                        </div>
                    </div>
                    <?php
                }
                if ((int)$fieldPhone > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="phone" class="formLabel left">
                            <?php
                            if ((int)$fieldPhone == 1) {
                                echo '<span class="req">*</span>';
                            }
                            echo t('Τηλέφωνο'); ?>
                        </label>
                        <div>
                            <input id="phone" name="phone" type="text" class="textbox" value="<?php echo stripslashes($phone); ?>">
                        </div>
                    </div>
                    <?php
                }
                if ((int)$fieldEmail > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="email" class="formLabel left">
                            <?php
                            if ((int)$fieldEmail == 1) {
                                echo '<span class="req">*</span>';
                            }
                            ?>
                            e-mail
                        </label>
                        <div>
                            <input id="email" name="email" type="text" class="textbox" value="<?php echo stripslashes($email); ?>">
                        </div>
                    </div>
                    <?php
                }
                if ((int)$fieldAddress > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="address" class="formLabel left">
                            <?php
                            if ((int)$fieldAddress == 1) {
                                echo '<span class="req">*</span>';
                            }
                            echo t('Διεύθυνση');
                            ?>
                        </label>
                        <div>
                            <input id="address" name="address" type="text" class="textbox" value="<?php echo stripslashes($address); ?>">
                        </div>
                    </div>
                    <?php
                }
                if ((int)$fieldZipCode > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="tk" class="formLabel left">
                            <?php
                            if ((int)$fieldZipCode == 1) {
                                echo '<span class="req">*</span>';
                            }
                            echo t('Τ.Κ.');
                            ?>
                        </label>
                        <div>
                            <input id="tk" name="tk" type="text" class="textbox" style="width:100px" value="<?php echo stripslashes($tk); ?>">
                        </div>
                    </div>
                    <?php
                }
                if ((int)$fieldCity > 0) {
                    ?>
                    <div style="margin: 0 auto;" class="form_container">
                        <label for="city" class="formLabel left">
                            <?php
                            if ((int)$fieldCity == 1) {
                                echo '<span class="req">*</span>';
                            }
                            echo t('Πόλη');
                            ?>
                        </label>
                        <div>
                            <input id="city" name="city" type="text" class="textbox" value="<?php echo stripslashes($city); ?>">
                        </div>
                    </div>
                    <?php
                }
                ?>
                <div style="margin: 0 auto;" class="form_container">
                    <label for="message" class="formLabel left"><span class="req">*</span><?php echo t('Μήνυμα'); ?></label>
                    <div>
                        <textarea id="message" name="message" rows="4" class="multiline_textbox" style="min-width: 95%; font-size:inherit !important"><?php echo stripslashes($message); ?></textarea>
                    </div>
                </div>
                <?php
                if ($useCaptcha == "1") { // Odyssey Captcha
                    ?>
                    <div style="margin: 10px auto 2px auto;" class="form_container">
                        <label for="security_code" class="formLabel left"><span class="req">*</span><?php echo t('Κωδικός ταυτοποίησης'); ?></label>
                        <div><img src="CaptchaSecurityImages.php?width=100&amp;height=40&amp;characters=5" style="float:left;min-width:100px;" alt="captcha"/>
                            &nbsp;<input name="security_code" type="text" id="security_code" class="textbox" style="width:80px"></div>
                        <div class="formLabel left" style="margin-top: 15px;"><?php echo t('Συμπληρώστε τον κωδικό ταυτοποίησης'); ?></div>
                    </div>
                    <?php
                }
                if ($useCaptcha == "2") { // Google reCaptcha
                    ?>
                    <div style="margin: 0px auto 30px;" class="form_container">
                        <div class="g-recaptcha" data-sitekey="6LesLCATAAAAAMb1V-SibnemWbLB995dZ_jCZgXa"></div>
                    </div>
                    <?php
                }
                ?>
                <input name="section" type="hidden" value="<?php echo $section; ?>">
                <input name="view" type="hidden" value="<?php echo $view; ?>">
                <input name="id" type="hidden" value="<?php echo $id; ?>">
                <input name="langID" type="hidden" value="<?php echo $langID; ?>">
                <div style="margin: 10px auto; display:table">
                    <input type="submit" class="formButton" value="<?php echo t('Αποστολή'); ?>" onclick="$(this).attr('disabled', true)">
                    <input type="hidden" name="submitContact" value="1">
                </div>
            </div>
        </form>

    <?php
    }else{
    if ($_POST['submitContact']){ // successfull submit

    // mail headers
    //$mail_header = "Reply-To: $email <".$email.">\n";
    $mail_header = "Return-Path: $domain_name <" . $email_address . ">\n";
    /*petros code start*/
    if (trim($email) == "")
        $mail_header .= "From: " . $email_address . " <" . $email_address . ">\n";
    else
        $mail_header .= "From: " . $email . " <" . $email . ">\n";
    /*petros code end*/
    $mail_header .= "Organization: $domain_name\n";
    $mail_header .= "Content-Type: text; charset=utf-8\n";

    $msg = NULL;
    $subject = "Contact from " . $domain_name;
    $msg = t("Καταχωρήθηκαν τα ακόλουθα στοιχεία στη φόρμα επικοινωνίας") . ":\n\n";
    if ((int)$fieldName > 0) $msg .= t("Ονοματεπώνυμο") . ": " . $fullname . "\n\n";
    if ((int)$fieldAddress > 0) $msg .= t("Διεύθυνση") . ": " . $address . "\n\n";
    if ((int)$fieldCity > 0) $msg .= t("Πόλη") . ": " . $city . "\n\n";
    if ((int)$fieldZipCode > 0) $msg .= t("Τ.Κ.") . ": " . $tk . "\n\n";
    if ((int)$fieldPhone > 0) $msg .= t("Τηλέφωνο") . ": " . $phone . "\n\n";
    if ((int)$fieldEmail > 0) $msg .= "e-mail: " . $email . "\n\n";
    $msg .= t("Μήνυμα") . ": \n" . stripslashes($message) . "\n\n";
    if (mail($email_address, $subject, $msg, $mail_header)){
    $template_ok_image = $document->getTemplatePath() . "images/ok.png";
    $ok_image = (is_file($template_ok_image)) ? $template_ok_image : "images/ok.png";
    ?>
        <div style="text-align:center; margin:40px">
            <img src="<?php echo $ok_image;?>" alt="success"><br><br>
            <?php
            echo(t("Το μήνυμά σας παραδόθηκε επιτυχώς"));
            ?>
        </div>
        <?php
    }else {
        ?>
        <div style="text-align:center; margin:40px;">
            <img src="images/warning.png" width="40" height="40" alt="error"><br><br>
            <?php
            echo(t("Παρουσιάστηκε σφάλμα κατά την αποστολή του μηνύματος<br>Παρακαλούμε δοκιμάστε ξανά αργότερα"));
            ?>
        </div>
        <?php
    }
    }
    }
    if ($useModuleContainer){
    ?>
</div>
<?php
}
?>

