function dk_speakup_sendmail()
{
    // set WPML language
    global $sitepress;
    $lang = isset($_POST['lang']) ? $_POST['lang'] : '';
    if (isset($sitepress)) {
        $sitepress->switch_lang($lang, true);
    }
    include_once 'class.signature.php';
    include_once 'class.petition.php';
    include_once 'class.mail.php';
    include_once 'class.wpml.php';
    $the_signature = new dk_speakup_Signature();
    $the_petition = new dk_speakup_Petition();
    $wpml = new dk_speakup_WPML();
    $options = get_option('dk_speakup_options');
    // clean posted signature fields
    $the_signature->poppulate_from_post();
    // get petition data
    $the_petition->retrieve($the_signature->petitions_id);
    $wpml->translate_petition($the_petition);
    $options = $wpml->translate_options($options);
    // check if submitted email address is already in use for this petition
    if ($the_signature->has_unique_email($the_signature->email, $the_signature->petitions_id)) {
        // handle custom petition messages
        $original_message = str_replace("\r", '', $the_petition->petition_message);
        if ($the_petition->is_editable && $the_signature->submitted_message != $original_message) {
            $the_signature->custom_message = trim($the_signature->submitted_message);
        }
        // does petition require email confirmation?
        if ($the_petition->requires_confirmation) {
            $the_signature->is_confirmed = 0;
            $the_signature->create_confirmation_code();
            dk_speakup_Mail::send_confirmation($the_petition, $the_signature, $options);
        } else {
            if ($the_petition->sends_email) {
                dk_speakup_Mail::send_petition($the_petition, $the_signature);
            }
            if ($the_petition->user_send_email) {
                dk_speakup_Mail::send_subscriber($the_petition, $the_signature, $options);
            }
        }
        // add signature to database
        $the_signature->create($the_signature->petitions_id);
        // display success message
        $success_message = $options['success_message'];
        $success_message = str_replace('%first_name%', $the_signature->first_name, $success_message);
        $success_message = str_replace('%last_name%', $the_signature->last_name, $success_message);
        do_action('dk_speakup_petition_signed', $the_signature);
        $json_response = array('status' => 'success', 'message' => $success_message);
        $json_response = json_encode($json_response);
        echo $json_response;
    } else {
        $json_response = array('status' => 'error', 'message' => $options['already_signed_message']);
        $json_response = json_encode($json_response);
        echo $json_response;
    }
    // end AJAX processing
    die;
}
function dk_speakup_signatures_page()
{
    // check security: ensure user has authority
    if (!current_user_can('publish_posts')) {
        wp_die(__('Insufficient privileges: You need to be an editor to do that.', 'dk_speakup'));
    }
    include_once 'class.speakup.php';
    include_once 'class.signature.php';
    include_once 'class.petition.php';
    $the_signatures = new dk_speakup_Signature();
    $the_petitions = new dk_speakup_Petition();
    $options = get_option('dk_speakup_options');
    $action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
    $pid = isset($_REQUEST['pid']) ? $_REQUEST['pid'] : '';
    // petition id
    $sid = isset($_REQUEST['sid']) ? $_REQUEST['sid'] : '';
    // signature id
    // set variables for paged record display and for limit values in db query
    $paged = isset($_REQUEST['paged']) ? $_REQUEST['paged'] : '1';
    $total_pages = isset($_REQUEST['total_pages']) ? $_REQUEST['total_pages'] : '1';
    $current_page = dk_speakup_SpeakUp::current_paged($paged, $total_pages);
    $query_limit = $options['signatures_rows'];
    $query_start = $current_page * $query_limit - $query_limit;
    switch ($action) {
        case 'delete':
            // security: ensure user has intention
            check_admin_referer('dk_speakup-delete_signature' . $sid);
            // delete signature from the database
            $the_signatures->delete($sid);
            // count number of signatures in database
            $count = $the_signatures->count($pid);
            // get all signatures for display
            $signatures = $the_signatures->all($pid, $query_start, $query_limit);
            // set up display strings
            // set up values for the table label ie: All Signatures (36)
            if ($count == 0) {
                $petition = '';
            } elseif ($pid != '') {
                $petition = $signatures[0]->title;
            } else {
                $petition = __('All Signatures', 'dk_speakup');
            }
            $table_label = esc_html($petition) . ' <span class="count">(' . $count . ')</span>';
            $base_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures&action=petition&pid=' . $pid);
            $message_update = __('Signature deleted.', 'dk_speakup');
            break;
        case 'petition':
            // count number of signatures in database
            $count = $the_signatures->count($pid);
            // get all signatures for display
            $signatures = $the_signatures->all($pid, $query_start, $query_limit);
            // TODO: Make this always show petition title (maybe use join in query)
            // set up display strings
            // if signatures exist, show petition title, else show petition id number
            if (count($signatures) > 0) {
                $table_label = esc_html($signatures[0]->title) . ' <span class="count">(' . $count . ')</span>';
            } else {
                $table_label = __('Petition', 'dk_speakup') . ' ' . $pid . ' <span class="count">(0)</span>';
            }
            $base_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures&action=petition&pid=' . $pid);
            $message_update = '';
            break;
        case 'reconfirm':
            check_admin_referer('dk_speakup-resend_confirmations' . $pid);
            include_once 'class.mail.php';
            $petition_to_confirm = new dk_speakup_Petition();
            // get unconfirmed signatures
            $unconfirmed = $the_signatures->unconfirmed($pid);
            foreach ($unconfirmed as $signature) {
                $unconfirmed_signature = new dk_speakup_signature();
                $unconfirmed_signature->first_name = $signature->first_name;
                $unconfirmed_signature->last_name = $signature->last_name;
                $unconfirmed_signature->email = $signature->email;
                $unconfirmed_signature->confirmation_code = $signature->confirmation_code;
                dk_speakup_Mail::send_confirmation($petition_to_confirm, $unconfirmed_signature, $options);
                // destroy temporary object so we can re-use the variable
                unset($unconfirmed_signature);
            }
            // count number of signatures in database
            $count = $the_signatures->count($pid);
            // get all signatures for display
            $signatures = $the_signatures->all($pid, $query_start, $query_limit);
            // set up display strings
            if (count($signatures) > 0) {
                $table_label = esc_html($signatures[0]->title) . ' <span class="count">(' . $count . ')</span>';
            } else {
                $table_label = __('Petition', 'dk_speakup') . ' ' . $pid . ' <span class="count">(0)</span>';
            }
            $base_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures&action=petition&pid=' . $pid);
            $message_update = __('Confirmation emails sent.', 'dk_speakup');
            break;
        default:
            // count number of signatures in database
            $count = $the_signatures->count($pid);
            // get all signatures for display
            $signatures = $the_signatures->all($pid, $query_start, $query_limit);
            // set up display strings
            $table_label = __('All Signatures', 'dk_speakup') . ' <span class="count">(' . $count . ')</span>';
            $base_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures');
            $message_update = '';
    }
    // get list of petitions to populate select box navigation
    $petitions_list = $the_petitions->quicklist();
    // Set up URLs for 'Download as CSV' and 'Resend confirmations' buttons
    // Show buttons only when we are viewing signatures from a single petition
    if (count($petitions_list) == 1 || $pid != '') {
        // if $pid (petition id) wasn't sent through the URL, then create it from the query
        if ($pid == '') {
            $pid = $petitions_list[0]->id;
        }
        $csv_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures&action=petition&pid=' . $pid);
        $reconfirm_url = site_url('wp-admin/admin.php?page=dk_speakup_signatures&action=reconfirm&pid=' . $pid);
    }
    // display the Signatures table
    include_once dirname(__FILE__) . '/signatures.view.php';
}
/**
 * Displays the confirmation page
 */
function dk_speakup_confirm_email()
{
    // set WPML language
    global $sitepress;
    $lang = isset($_REQUEST['lang']) ? $_REQUEST['lang'] : '';
    if (isset($sitepress)) {
        $sitepress->switch_lang($lang, true);
    }
    include_once 'class.signature.php';
    include_once 'class.petition.php';
    include_once 'class.mail.php';
    include_once 'class.wpml.php';
    $the_signature = new dk_speakup_Signature();
    $the_petition = new dk_speakup_Petition();
    $wpml = new dk_speakup_WPML();
    // get the confirmation code from url
    $confirmation_code = $_REQUEST['dkspeakupconfirm'];
    // try to confirm the signature
    $try_confirm = $the_signature->confirm($confirmation_code);
    // if our attempt to confirm the signature was successful
    if ($try_confirm) {
        // retrieve the signature data
        $the_signature->retrieve_confirmed($confirmation_code);
        // retrieve the petition data
        $the_petition->retrieve($the_signature->petitions_id);
        $wpml->translate_petition($the_petition);
        // send the petition email
        if ($the_petition->sends_email) {
            dk_speakup_Mail::send_petition($the_petition, $the_signature);
        }
        // set up the status message
        $message = __('Thank you. Your signature has been added to the petition.', 'dk_speakup');
    } else {
        // has the signature already been confirmed?
        if ($the_signature->check_confirmation($confirmation_code)) {
            $message = __('Your signature has already been confirmed.', 'dk_speakup');
        } else {
            // the confirmation code is fubar or an admin has already deleted the signature
            $message = __('The confirmation code you provided is invalid.', 'dk_speakup');
        }
    }
    // display the confirmation page
    $confirmation_page = '
		<!doctype html>
		<html>
		<head>
			<meta http-equiv="Content-Type" content="text/html; charset=' . get_bloginfo("charset") . '" />
			<meta http-equiv="refresh" content="10;' . $the_petition->return_url . '"> 
			<title>' . get_bloginfo("name") . '</title>
			<style type="text/css">
				body {
					background: #666;
					font-family: arial, sans-serif;
					font-size: 14px;
				}
				#confirmation {
					background: #fff url(' . plugins_url("speakup-email-petitions/images/mail-stripes.png") . ') repeat top left;
					border: 1px solid #fff;
					width: 515px;
					margin: 200px auto 0 auto;
					box-shadow: 0px 3px 5px #333;
				}
				#confirmation-content {
					background: #fff url(' . plugins_url("speakup-email-petitions/images/postmark.png") . ') no-repeat top right;
					margin: 10px;
					padding: 40px 0 20px 100px;
				}
			</style>
		</head>
		<body>
			<div id="confirmation">
				<div id="confirmation-content">
					<h2>' . __("Email Confirmation", "dk_speakup") . '</h2>
					<p>' . $message . '</p>
					<p>' . __("You will be redirected momentarily.", "dk_speakup") . '</p>
					<p><a href="' . home_url() . '">' . get_bloginfo("name") . '  &raquo;</a></p>
				</div>
			</div>
		</body>
		</html>
	';
    echo $confirmation_page;
    // stop page rendering here
    // without this, the home page will display below the confirmation message
    die;
}