
Originally Posted by
321web
Again that doesnt mean people are lazy, it just means that they didnt know that.
My code looks like this
PHP Code:
$msg = "Name:\t$name\n";
$msg .= "E-mail:\t$email\n";
$msg .= "ICQ:\t$icq\n";
$msg .= "URL:\t$url\n";
$msg .= "Description:\t$description\n";
$msg .= "IP:\t$REMOTE_ADDR\n";
$mailheaders = "From: $email\n";
$mailheaders .= "Reply-To: $email\n\n";
mail("webmaster@321graphics.com", "Feedback From 321Graphics.com", $msg, $mailheaders);
None of the suggestions seem to be working.
I'm going to be using these two functions throughout, so paste it in the page or include it in a file. Doesn't really matter.
PHP Code:
function valid($email, $debug = false)
{
// valid structure??
if (!preg_match('~^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$~', $email)) {
if ($debug) {
trigger_error('Email address not valid structure in valid()');
}
return false;
}
// get the username and the domain into seperate parts
list($username, $domain) = explode('@', $email);
// use getmxrr() to try to get the ip of the mail host
// you can remove the function_exists(...) if you know you won't be running on windows
if (function_exists('getmxrr') && getmxrr($domain, $mxhost)) {
$connectaddress = $mxhost[0];
} else {
if ($debug) {
trigger_error('Couldn\'t use getmxrr() in valid()');
}
// fall back on the domain
$connectaddress = $domain;
}
// connect to SMTP server of the connectaddress
$connect = fsockopen($connectaddress, 25, $no, $str, 15);
// validate connect
if ($connect) {
// match the 220 response code
if (preg_match('|^220|', $out = fgets($connect, 1024))) {
// hi! (tell the smtp server that)
fputs ($connect, 'HELO ' . $connectaddress . "\r\n");
$out = fgets($connect, 1024);
// see if its a valid user
fputs($connect, 'MAIL FROM: <' . $email . '>' . "\r\n");
$from = fgets($connect, 1024);
// ... twice ...
fputs ($connect, 'RCPT TO: <' . $email . '>' . "\r\n");
$to = fgets ($connect, 1024);
// quit and close
fputs ($connect, 'QUIT' . "\r\n");
fclose($connect);
// see if 250 was resonded
if (!preg_match('!^250!', $from) || !preg_match('!^250!', $to)) {
if ($debug) {
trigger_error('No 250 response code (e.g. not a valid user) in valid()');
}
return false;
}
} else {
if ($debug) {
trigger_error('No 220 response code in valid()');
}
// no 220 response code
return false;
}
} else {
if ($debug) {
trigger_error('Couldn\'t connect to ' . $connectaddress . ':25 in valid()');
}
// no connect
return false;
}
// what??? OMGOMGOMGOMGOMGOMG!!!1!!one!1!
return true;
}
// returns everything before any new lines (e.g. the user can't put in their own bcc or cc headers by putting in new lines)
function b4nl($var)
{
return preg_replace("!^(.+)(\r|\n).+$!isU", '$1', $var);
}
Here's the secured (to me) code:
PHP Code:
<?php
// holds all the errors
$errors = array();
if (isset($email)) {
// remove new lines
$email = b4nl($email);
// check if its valid
if (valid($email)) {
$clean['email'] = $email;
} else {
$errors[] = 'Invalid email: ' . $email;
}
} else {
$errors[] = 'Your email was not present or empty';
}
// this reduces the amount of code by a few lines ;)
// this strips all new lines away from the variables,
// so I didn't put description in here
foreach (array('name', 'icq', 'url') as $var) {
// $$var would translate into something like $name or $icq
$$var = b4nl($var);
// check that it exists and is not empty
if (!isset($$var) || empty(trim($$var))) {
$errors[] = 'Your' . $var . ' was not present or empty';
} else {
// it's clean
$clean[$var] = $$var;
}
}
if (!isset($description) || empty(trim($description))) {
$errors[] = 'Your' . $var . ' was not present or empty';
} else {
$clean['description'] = $description;
}
// more thorough, i guess?
if (isset($_SERVER['X_FORWARDED_FOR']) && !empty(trim($_SERVER['X_FORWARDED_FOR'])) {
$ip = $_SERVER['X_FORWARDED_FOR'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
// this detects if there are any errors.
// you should probably change the styling around a bit, but...
if (count($errors) > 0) {
echo 'A few errors occured:<ul>';
foreach ($errors) {
echo '<li>' . $errors . "</li>\r\n";
}
echo '</ul>';
}
// construct the message
$msg = "Name:\t" . $clean['name'] . "\nE-mail:\t" . $clean['email'] . "\nICQ:\t" . $clean['icq'];
$msg .= "\nURL:\t" . $clean['url'] . "\nDescription:\t" . $clean['description'] . "\nIP:\t" . $ip . "\n";
// construct the headers.
$headers = 'From: ' . $clean['email'] . "\r\nReply-To:" . $clean['email'] . "\r\n\r\n";
// you should probably change the error message here
mail('webmaster@321graphics.com', 'Feedback From 321Graphics.com', $msg, $headers) or echo 'Couldn\'t send mail...';
Bookmarks