Report IP option in contact_form!

Talk about new features for CMSMS and modules.
Post Reply
johnqpublic

Report IP option in contact_form!

Post by johnqpublic »

Hi,

It would be great if the contact form had an option to turn on IP address reporting when people fill out the contact form.  I know I need to add something like this in the form, but I keep breaking it no matter how hard I try!  My PHP skills are rather weak!



Thanks for listening.

John Q. Public
geoffb

Re: Report IP option in contact_form!

Post by geoffb »

This wasn't too difficult to implement. On a 0.11.1 system, make the following edits to the plugins/function.contact_form.php file:

At about line 37 (immediately after "if (!empty($_POST['message'])) $message = trim($_POST['message']);") add these two lines:

Code: Select all

   $env_report = '';
   if (!empty($_POST['env_report'])) $message .= "\n\nenv_report == " . trim($_POST['env_report']);


then, at about line 75 (immediately before ), add this line:


Code: Select all

   <input type="hidden" name="env_report" value="<?php echo $_SERVER["REMOTE_ADDR"] . ', ' . $_SERVER["HTTP_USER_AGENT"] ?>">



This will add a line to the bottom of the email message that says "env_report == , "

It doesn't give you the option to choose what things you want included, that would take a little bit more playing around. (I only solved this problem to avoid doing actual work of my own for a few minutes!!!) :D ;D :D

I hope this helps,

GB
geoffb

Re: Report IP option in contact_form!

Post by geoffb »

Actually, in hindsight, that was a pretty dumb solution. I guess I just fell in to using the approach 'cos that's what your question asked for. In fact, making those values something that the client submits makes them open to spoofing. Instead, do this...


At about line 37 (immediately after "if (!empty($_POST['message'])) $message = trim($_POST['message']);")" add these two lines:

Code: Select all

   $env_report = $_SERVER["REMOTE_ADDR"] . ', ' . $_SERVER["HTTP_USER_AGENT"];
   $message .= "\n\nenv_report == " . $env_report;


Add nothing to the
...
section. The other notes re not doing quite what you asked for still apply, this still applies to an 0.11.1 system, and I still haven't got back to doing my actual work!

Enjoy!

GB
johnqpublic

Re: Report IP option in contact_form!

Post by johnqpublic »

Thanks for not doing your work and taking the time to help me out!  ;D

I'm using 0.11.2 now, not 0.11.1 since it had the contact form injection problems, but that didn't stop me from trying out your code.  It seems to work just fine... test messages show the IP and other variables great... so I hope I didn't break anything I'm not aware of.

Again, thanks for the help.  It's appreciated.



#CMS - CMS Made Simple
#(c)2004 by Ted Kulp (wishy@users.sf.net)
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

function smarty_cms_function_contact_form($params, &$smarty) {

if (empty($params['email'])){
echo '
A email address to send to must be specified in order to use this plugin.
';
return;
}else{
$to = $params['email'];
}

$style = true;
if (!empty($params['style']))$style = $params['style'];

$name=$email=$subject=$message = '';
if($_SERVER['REQUEST_METHOD']=='POST'){
if (!empty($_POST['name'])) $name = cfSanitize($_POST['name']);
if (!empty($_POST['email'])) $email = cfSanitize($_POST['email']);
if (!empty($_POST['subject'])) $subject = cfSanitize($_POST['subject']);
if (!empty($_POST['message'])) $message = cfSanitize($_POST['message']);
$env_report = $_SERVER["REMOTE_ADDR"] . ', ' . $_SERVER["HTTP_USER_AGENT"];
$message .= "\n\nenv_report == " . $env_report;

$extra = "From: $name <$email>\r\nReply-To: $email\r\n";
echo '
echo ($style)?' style="font-weight: bold; color: red;"':'';
echo '>';

if (empty($name)) {
echo 'Please Enter Your Name.';
}elseif (empty($email)) {
echo 'Please Enter Your Email Address.';
}elseif (empty($subject)) {
echo 'Please Enter a Subject.';
}elseif (empty($message)) {
echo 'Please Enter a Message.';
}elseif (!validEmail($email)) {
echo 'Your Email Address is Not Valid.';
}elseif (@mail($to, $subject, $message, $extra)){
echo "Your message was successfully sent.
";
return;
}else{
echo 'Sorry, the message was not sent. The server may be down!
';
return;
}

echo '
';
}
?>

>
Your Name: />
Your Email Address: />
Subject: />
Message:



}

function cfsanitize($content){
return str_replace(array("\r", "\n"), "", trim($content));
}

function smarty_cms_help_function_contact_form() {
?>

What does this do?


Display's a contact form.  This can be used to allow others to send an email message to the address specified.


How do I use it?


Just insert the tag into your template/page like: {contact_form email="yourname@yourdomain.com"}



If you would like to send an email to multiple adresses, seperate each address with a comma.


What parameters does it take?



  • email - The email address that the message will be sent to.

  • (optional)style - true/false, use the predefined styles. Default is true.



}

function smarty_cms_about_function_contact_form() {
?>

Author: Brett Batie<brett-cms@classicwebdevelopment.com>


Version: 1.1



Change History:

None


}

function validEmail($email) {
if (!preg_match("/^([\w|\.|\-|_]+)@([\w||\-|_]+)\.([\w|\.|\-|_]+)$/i", $email)) {
return false;
exit;
}
return true;
}

?>