<?php
    require_once('include/functions/send_email.php');

    $user_id = get_user_id($_user);
    if (!$user_id)
    {
        show_error_page('Sorry, we do not have any members by that name.');
        die;
    }
    
    $_body = isset($_POST['body']) ? trim($_POST['body']) : null;
    $_security_code = isset($_POST['security_code']) ? trim($_POST['security_code']) : null;

    $err = '';

    $messages = mysqli_query_logged("SELECT * FROM messages WHERE sender_id = '" . $GLOBALS['auth']['id'] . "' ORDER BY id DESC LIMIT 1");
    if ($messages_row = mysqli_fetch_array($messages))
    {
        if ($messages_row['body'] == rq($_body))
        {
            $err = 'double_post';
        }
        elseif ($messages_row['posted_on'] > date('Y-m-d H:i:s', mktime(date('H'), date('i'), date('s') - 10, date('m'), date('d'), date('Y'))))
        {
            $err = 'rapid';
        }
    }

    $security_code_last = mysqli_query_logged("SELECT * FROM security_code_last WHERE user_id = '" . $GLOBALS['auth']['id'] . "'");
    if ($security_code_last_row = mysqli_fetch_assoc($security_code_last))
    {
        if (strtoupper($_security_code) != strtoupper($security_code_last_row['code']))
        {
            $err = 'security';
        }
        mysqli_query_logged("DELETE FROM security_code_last WHERE user_id = '" . $GLOBALS['auth']['id'] . "'");
    }
    elseif ($user_country != 'CA' && $user_country != 'US')
    {
        $err = 'security';
    }

    if ($err == '' && $_body == '')
    {
        $err = 'body_missing';
    }

    if ($err != '')
    {
        header('Location: ./?s=messages&u=' . $_user . '&e=' . $err);
        die;
    }
        
    mysqli_query_logged("INSERT INTO messages SET sender_id = '" . $GLOBALS['auth']['id'] . "', recipiant_id = '" . $user_id . "', body = '" . $_body . "', posted_on = '" . script_time() . "'");
    
    $mysql_insert_id = mysqli_insert_id($GLOBALS['mysqli']);
    
    $messages_index = mysqli_query_logged("SELECT * FROM messages_index WHERE sender_id = '" . $GLOBALS['auth']['id'] . "' AND receiver_id = '" . $user_id . "'");
    if ($messages_index_row = mysqli_fetch_assoc($messages_index))
    {
        mysqli_query_logged("UPDATE messages_index SET last_received = '" . script_time() . "', message_id_received = '" . $mysql_insert_id . "' WHERE sender_id = '" . $GLOBALS['auth']['id'] . "' AND receiver_id = '" . $user_id . "'");
    }
    else
    {
        mysqli_query_logged("INSERT INTO messages_index SET sender_id = '" . $GLOBALS['auth']['id'] . "', receiver_id = '" . $user_id . "', last_received = '" . script_time() . "', message_id_received = '" . $mysql_insert_id . "'");
    }
    
    $messages_index = mysqli_query_logged("SELECT * FROM messages_index WHERE receiver_id = '" . $GLOBALS['auth']['id'] . "' AND sender_id = '" . $user_id . "'");
    if ($messages_index_row = mysqli_fetch_assoc($messages_index))
    {
        mysqli_query_logged("UPDATE messages_index SET last_sent = '" . script_time() . "', message_id_sent = '" . $mysql_insert_id . "' WHERE receiver_id = '" . $GLOBALS['auth']['id'] . "' AND sender_id = '" . $user_id . "'");
    }
    else
    {
        mysqli_query_logged("INSERT INTO messages_index SET receiver_id = '" . $GLOBALS['auth']['id'] . "', sender_id = '" . $user_id . "', last_sent = '" . script_time() . "', message_id_sent = '" . $mysql_insert_id . "'");
    }
    
    $members_extras = mysqli_query_logged("SELECT * FROM members_extras WHERE user_id = '" . $user_id . "' AND notification = '1'");
    if ($members_extras_row = mysqli_fetch_assoc($members_extras))
    {
        $messages_email = mysqli_query_logged("SELECT * FROM messages_email WHERE user_id = '" . $user_id . "' AND link_id = '" . $GLOBALS['auth']['id'] . "'");
        if (!$messages_email_row = mysqli_fetch_assoc($messages_email))
        {
            $members = mysqli_fetch_assoc(mysqli_query_logged("SELECT * FROM members WHERE id = '" . $user_id . "'"));
            $body = 'You have a new message from ' . get_username($GLOBALS['auth']['id']) . '.';
            send_email($members['email'], 'Quilts Community - New Message', $body);
            mysqli_query_logged("INSERT INTO messages_email SET user_id = '" . $user_id . "', link_id = '" . $GLOBALS['auth']['id'] . "', posted_on = '" . script_time() . "'");
        }
    }

    /*
    $sms = mysqli_query_logged("SELECT * FROM sms WHERE user_id = '" . $user_id . "'");
    if ($sms_row = mysqli_fetch_assoc($sms))
    {
        if ($sms_row['carrier'] == 'telus') $ending = '@msg.telus.com';
        if ($sms_row['carrier'] == 'rogers') $ending = '@pcs.rogers.com';
        if ($sms_row['carrier'] == 'fido') $ending = '@fido.ca';
        if ($sms_row['carrier'] == 'bell') $ending = '@txt.bellmobility.ca';
        if ($ending)
        {
            send_email($sms_row['phone'] . $ending, 'Quiltsco.com', 'New Message From: ' . get_username($GLOBALS['auth']['id']) . "\r\n" . 'Time: ' . date('Y-m-d H:i:s'));
        }
    }
    */

    header('Location: ./?s=messages&u=' . $_user);
    die;
?>