';
$option = get_option( 'zcmail' );
if ( isset( $_POST['zcmail'] )) {
if ( !zcmail_floodprotect( $option, $_SERVER['REMOTE_ADDR'] ) ) {
$return .= '
';
$return .= __( 'Temporarily closed' );
$return .= '
';
return $return;
}
if ( zcmail_validates( $_POST['zcmail'], $option ) ) {
if ( $_POST['zcmail']['from'] ) {
$from = $_POST['zcmail']['from'];
}
else {
$from = 'Bug 01 - no from field defined';
}
if ( $_POST['zcmail']['subject'] ) {
$subject = $_POST['zcmail']['subject'];
}
else {
$subject = 'Bug 02 - no subject field defined';
}
if ( $_POST['zcmail']['name'] ) {
$message = $_POST['zcmail']['name'];
}
if ( $_POST['zcmail']['body'] ) {
$message .= "\n\n" . stripslashes( $_POST['zcmail']['body'] );
}
else {
$message .= "\n\n" . 'Bug 03 - no message field defined';
}
foreach($_POST['zcmail'] as $field => $values) {
// TODO I guess this should be safe, as a[] = foo => a[0] == foo
// Add custom fields to the message
if ( is_integer( $field ) ) {
$message .= "\n";
$message .= $option['fields'][$field]['label'] . ": $values";
}
}
// Add message footer
$message .= "\n\n---\n" . __( 'Sent with Zero Conf Mail: ', 'zero-conf-mail' );
$message .= __( 'http://www.nkuttler.de/wordpress/zero-conf-mail/', 'zero-conf-mail' );
$message .= "\n\n" . __( 'Blog: ', 'zero-conf-mail' );
$message .= get_bloginfo( 'home' );
$message .= "\n" . __( 'IP : ', 'zero-conf-mail' );
$message .= $_SERVER['REMOTE_ADDR'];
$message = zcmail_sanitize( $message );
$success = zcmail_sendmail( $option['config']['to'], $from, $subject, $message );
if ( $success ) {
$return .= '';
$return .= $option['config']['messages']['success'];
$return .= '
';
}
else {
$return .= ' ';
$return .= __( "No message could be sent. This means that this server wasn't configured to send mail. Please contact the site administrator.", 'zero-conf-mail' );
$return .= '
';
}
}
else {
$return .= '';
$return .= $option['config']['messages']['failure'];
$return .= '
';
$return .= zcmail_printform();
}
}
else {
$return .= zcmail_printform();
}
$return .= '';
return $return;
}
/**
* Send a mail
*
* @param string $to receiver
* @param string $from sender
* @param string $subject subject
* @param string $body email body
*
* @return was the message accepted for delivery
*/
function zcmail_sendmail( $to, $from, $subject, $body ) {
$result = mail(
$to,
'[' . get_bloginfo( 'name' ) . '] ' . $subject,
$body,
"From: $from"
);
return $result;
}
/**
* Perform (basic) sanitization of user input.
*
* @param $string string to sanitize
* @return string sanitized string
*/
function zcmail_sanitize( $string ) {
$string = htmlspecialchars( $string, ENT_NOQUOTES );;
return $string;
}
/**
* Validate all parameters
*
* @param array $input $_POST data
* @param array $option plugin options
*
* @return boolean do all fields validate
*
* @todo Useful error messages per field
*/
function zcmail_validates( $input, $option ) {
foreach( $option['fields'] as $field => $value ) {
// Catch empty or unchanged mandatory fields
if ( $value['type'] == 'text' || $value['type'] == 'textarea' ) {
if ( ( $value['must'] === true || $value['must'] === 'true' ) && $input[$field] == $value['label'] ) {
//echo "error: " . $input[$field] . " unchanged";
return false;
}
elseif ( ( $value['must'] === true || $value['must'] === 'true' ) && $input[$field] == '' ) {
//echo "error: " . $value['label'] . $input[$field] . " empty";
return false;
}
}
// Catch invalid email adresses
if ( $value['type'] == 'email' ) {
if ( ( $value['must'] === true || $value['must'] === 'true' ) && $value['type'] == 'email' && !is_email( $input[$field] ) ) {
//echo "error: " . $input[$field] . " no mail";
return false;
}
}
}
return true;
}
/**
* Basic flood protection
* Spam protection is the mail server's job. I don't need captcha.
*
* Ways to trick this flood protection (with default settings):
* 1. Limit oneself to ten mails per IP per hour. If that creates a problem
* for anybody they probably shouldn't be looking for a technical solution
* to their problem.
*
* @param $option plugin options
* @param $ip remote ip
*
* @return boolean not flood?
*
* @todo add dupe protection.. maybe. would require to save mail content + subject etc
*
* @since 0.4.0
*/
function zcmail_floodprotect( $option, $ip ) {
$maxmails = $option['config']['flood']['maxmails'];
$minhours = $option['config']['flood']['minhours'];
// get oldest existing or new timestamp
isset( $option['log'][$ip][0] ) ?
$oldest_timestamp = $option['log'][$ip][0] :
$oldest_timestamp = time();
$allowed_at = strtotime( "+$minhours hours", $oldest_timestamp );
if ( isset( $option['log'][$ip] ) ) {
// This IP has already sent a mail
if ( count( $option['log'][$ip] ) >= $maxmails ) {
// This IP has already sent more mail than allowed
if ( time() < $allowed_at ) {
// Inside forbidden timeframe, return
return false;
}
}
}
// We are allowed to send, update logs
$option['log'][$ip][] = time();
// limit log size per IP
if ( count( $option['log'][$ip] ) > $maxmails ) {
array_shift( $option['log'][$ip] );
}
// clean up log after updating it
$option = zcmail_purgelog( $option );
update_option( 'zcmail', $option );
return true;
}
/**
* Clean up log entries, forget IPs that haven't sent within the forbidden time
*
* @param $option plugin options
*
* @return array plugin options with cleaned up log
*
* @since 0.4.0
*/
function zcmail_purgelog( $option ) {
$minhours = $option['config']['flood']['minhours'];
foreach ( $option['log'] as $log_ip => $timestamps ) {
$allowed_at = strtotime( "+$minhours hours", $timestamps[0] );
if( time() > $allowed_at ) {
unset( $option['log'][$log_ip] );
}
}
return $option;
}
/**
* Return the mail form for visitors or on the options page
*
* @param boolean $disabled disable the submit button
*/
function zcmail_printform( $disabled = false ) {
$option = get_option( 'zcmail' );
$return = '';
if ( $option['config']['homelink'] == 'Below the form' ) {
$return .= get_zcmail_homelink();
}
elseif ( $option['config']['homelink'] == 'In the footer' ) {
add_action( 'wp_footer', 'zcmail_homelink' );
}
return $return;
}
/**
* Print the value the user submitted earlier, or the default.
*
* @param string $default Default value
* @param string $submitted The value that was submitted earlier
*
* @return: string Value to print
*/
function zcmail_get_value( $default, $submitted ) {
if ( isset( $submitted ) ) {
return $submitted;
}
else {
return $default;
}
}
/*
* Tell us if the value the user submitted earlier was the default value.
*
* @param string $default Default value
* @param string $submitted Submitted value
*
* @return: boolean Was the default used?
*/
function zcmail_get_value_bool( $default, $submitted ) {
if ( !isset( $submitted ) || $submitted == $default ) {
return true;
}
else {
return false;
}
}
/**
* Build an input tag
*
* @param string $name input field name
* @param string $label label for the field
* @param boolean $js show js, 'true' or 'false'
* @param string $size size of the input field
*
* @return string the input tag
*
* @since 0.1.12
*/
function get_zcmail_input( $name, $label, $js = true, $size = 40 ) {
$return = ' \n";
return $return;
}
/**
* Display a textarea
*
* @param string $name textarea name
* @param string $label label for the textarea
* @param boolean $js use JS effect
*/
function zcmail_textarea( $name, $label, $js ) {
echo get_zcmail_textarea( $name, $label, $js );
}
/**
* Print a submit putton
*
* @param string $label submit button value
* @param boolean $disabled disable the submit button
*
* @since 0.1.12
*/
function get_zcmail_submit( $label, $disabled ) {
$return = '
';
$return .= sprintf ( __( "Mail form powered by Zero Conf Mail ", 'zero-conf-mail'), $url, $url );
$return .= '';
return $return;
}
/**
* Print the link to the plugin site.
*/
function zcmail_homelink() {
echo get_zcmail_homelink();
}
/**
* Include standard CSS if shortcode exists
*
* @since 0.3.1
*/
function zcmail_include_css() {
// Check if shortcode exists in page or post content
global $post;
if ( strstr( $post->post_content, '[zcmail]' ) ) { ?>