/**
 * Shortcode.
 * Usage: [email-downloads file="http://path/to/file.ext"].
 * 
 * @param  array $atts  Attributes that passed through shortcode.
 * @return string       Formatted form.
 * ------------------------------------------------------------------------------
 */
function nanodesigns_email_downloads_shortcode($atts)
{
    global $maximum_link_duration;
    $atts = shortcode_atts(array('file' => ''), $atts);
    $file_path = $atts['file'];
    //Error storage
    $submission_error = array();
    if (isset($_POST['download_submit'])) {
        $email = $_POST['download_email'];
        if (!empty($email)) {
            if (is_email($email)) {
                $ip_address = nanodesigns_get_the_ip();
                //grab the user's IP
                $unique_string = $email . $ip_address . $file_path;
                //more complex unique string
                $hash = hash_hmac('md5', $unique_string, $ip_address);
                //IP address is the key
                //db storage - for 12 hours only
                set_transient($hash, $file_path, $maximum_link_duration * HOUR_IN_SECONDS);
                /**
                 * Making the download link with parameter
                 * 'download_token' is important.
                 * @var string
                 */
                $download_link = esc_url(add_query_arg('download_token', $hash, site_url()));
                //email the download link
                $success = nanodesigns_email_downloads($email, $download_link);
                //store the email into our database
                nanodesigns_ed_store_emails($email);
            } else {
                $submission_error[] = __('Please enter a valid email address', 'email-downloads');
            }
        } else {
            $submission_error[] = __('Email Address cannot be empty', 'email-downloads');
        }
    }
    ob_start();
    ?>
    <hr>
    <div class="email-downloads">
    	<form action="" enctype="multipart/form-data" method="post">
            <p><label for="download-email"><?php 
    _e('Enter your email address to download the file. An email will be sent to your email address with the download link.', 'email-downloads');
    ?>
</label></p>
            <?php 
    //Show errors, if any
    if (!empty($submission_error)) {
        foreach ($submission_error as $error) {
            echo '<p style="color: red;">' . __('<strong>Error: </strong>', 'email-downloads') . $error . '</p>';
        }
    }
    ?>
            <p><input type="email" name="download_email" id="download-email" placeholder="type your email address here" value="<?php 
    echo isset($_POST['download_email']) ? $_POST['download_email'] : '';
    ?>
" autocomplete="off" size="50"></p>
            <button type="submit" name="download_submit"><?php 
    _e('Send me the File', 'email-downloads');
    ?>
</button>
        </form>
    </div>
    <?php 
    return ob_get_clean();
}
/**
 * Storing email addresses into our table.
 * 
 * @param  string $email The user submitted email address.
 * ------------------------------------------------------------------------------
 */
function nanodesigns_store_emails($email)
{
    if ($email && is_email($email)) {
        if (nano_email_exists($email)) {
            //don't duplicate email addresses
            return;
        } else {
            $currenttimestring = strtotime(date('Y-m-d H:i:s', current_time('timestamp')));
            $ip_address = nanodesigns_get_the_ip();
            $hashed_string = md5($currenttimestring . $ip_address);
            update_option("nanoedmail_{$hashed_string}", $email);
        }
    }
}