<?php

require_once ('../api/Simpla.php');
$simpla = new Simpla;

$titleFrom = $simpla->settings->site_name;
$emailFrom = $simpla->settings->notify_from_email;
$emailTo   = $simpla->settings->form_email;
$prefix    = $simpla->settings->form_prefix ? $simpla->settings->form_prefix . ' ' : '';
$messSent  = $simpla->settings->form_sent;
$messFail  = $simpla->settings->form_fail;

//echo '<div class="alert alert-success">' . $messSent . '</div>';die;
//$emailTo = 'proviruz@mail.ru';

$post = (object)$_POST;

foreach ($post->formData as $postItem) {
    if ($postItem['name'] == 'user' && $postItem['value']) { // antispam
        echo $messFail;
        die;
    }

    $posts[$postItem['name']] = $postItem['value'];
}

$pageCurrent = $simpla->pages->get_page(intval($posts['id']));
$pageCurrentName = $pageCurrent->name;

if ($pageCurrent->parent > 0) {
    $pageParent = $simpla->pages->get_page(intval($pageCurrent->parent));
    $pageParentName = $pageParent->name . ' - ';
}

$emailSubject = $prefix . $pageParentName . $pageCurrentName;

$count = 0;
foreach ($post->requiredData as $requiredItem) {
    if (array_key_exists($requiredItem['name'], $posts)) { // validation

        if ($requiredItem['validation'] == 'text') {
            $count++;
            if (strlen($posts[$requiredItem['name']]) < 2)
                $required[$requiredItem['validation']] = $requiredItem['name'];
        }

        if ($requiredItem['validation'] == 'email') {
            $count++;
            if (strpos($posts[$requiredItem['name']], '@') === false || strpos($posts[$requiredItem['name']], '.') === false)
                $required[$requiredItem['validation']] = $requiredItem['name'];
        }

        if ($requiredItem['validation'] == 'phone') {
            $count++;
            if (strlen($posts[$requiredItem['name']]) < 5)
                $required[$requiredItem['validation']] = $requiredItem['name'];
        }
    }
}

if(isset($post->formData[2]['value']) && $post->formData[2]['name'] == 'Телефон'){
	$tel = str_replace( array('+7',' ', '(', ')', '-'), '', $post->formData[2]['value'] );
	$deps = array('7921946798', '8892194679','8124071110','9219467988','7111078124',
									'2194679887','7812407111','7911921506','9119215060','1921506091','2194679889','8126034467','3446781260', '8126034467','4467781260','7812603446','9214443898','9043326208','8126422999');
	$deps2 = array();
	foreach($deps as $d) $deps2[] = '8' . $d;
	//if(in_array($tel, $deps) || in_array($tel, $deps2) ) die('denied - ' . $tel);
}

if (count($required) == 1 && $count >= 2 && ($required['email'] || $required['phone']))
    unset($required);   

if ($required) {
    echo json_encode($required);
    die;
}
$message = '<h2>' . $emailSubject . '</h2>';
$message .= '<table>' . "\n";
foreach ($post->formData as $postItem) {
    if ($postItem['name'] != 'user' && $postItem['name'] != 'id')
        $message .= '<tr><td>' . $postItem['name'] . '</td>' . '<td><b>' . $postItem['value'] . '</b></td></tr>' . "\n";
}
$message .= '</table>' . "\n";

require_once 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet = 'utf-8';
$mail->setFrom($emailFrom, $titleFrom);



$emails = explode(',', $emailTo);

foreach ($emails as $email) {
    $mail->addAddress(trim($email));
}

$mail->isHTML(true);

//$message_utf8 = mb_convert_encoding($message, "windows-1251", "utf-8");
$message_utf8 = $message;

$mail->Subject = $emailSubject . ' - ' . date("Y-m-d H:i:s");
$mail->Body = $message_utf8;

if ($mail->send()) {
    echo '<div class="alert alert-success">' . $messSent . '</div>';
} else {
    echo $messFail;
}
