Including php functions in a UDT

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Locked
heatherfeuer

Including php functions in a UDT

Post by heatherfeuer »

CMSms has been hard on my brain!  My head hurts from all the new knowledge I've crammed into it in the last month!  :D

I need some clarification, please, to make sure I am understanding this.  If I want to use a php function in one of my content pages, I can't just stick it into a UDT.  I have to put the function code in a separate php file and use an include statement in the UDT to call that function.  Right so far?

So, I have some php code I want to use to process information on one of my content pages.  That code needs the function to work properly, so in my UDT I would have:

php code here

include(function file);

more php code here

If, in the config file, I've enabled the {php} smarty tag, I can also assign a php variable to content by enclosing it with {php}{/php}.

So... did I pass the test?  :-\
User avatar
chead
Forum Members
Forum Members
Posts: 63
Joined: Tue Feb 06, 2007 4:01 am

Re: Including php functions in a UDT

Post by chead »

In most cases, you should be able to simply paste the PHP code into the UDT, without the opening and closing tags.

If the code came from someplace else, it might have been built in a way that makes it necessary to have separate PHP files without some tweaking. That could be the case with the your form processor. Your form has to be completed and "submitted" to the server before the mailer PHP script can do anything with the data.

For most third-party mail scripts, on form submit the browser is redirected to the external script that quietly does the mailing and then back to a page on your CMSMS site.

For assigning variables, an easier method would be to use parameters in the UDT tags. Instead of using {php}{/php}, you could use something like the following in your content:

{mymailer subject="Information Request From Website" to="myemail@mywebsite.com"}

The parameters above would be available to the PHP in your {mymailer} UDT as variables:

$params['subject']
$params['to']

Have you got your custom mailer working? I'm remembering that you wanted to use FIELDSETs in your form, which the FormBuilder module currently doesn't offer. Is there more you are trying to do with the form or mailer? If it's just the form layout that you want more control over, I may be able to help you look at doing that with a custom FormBuilder template. I've considered doing this for one of my own forms too.
heatherfeuer

Re: Including php functions in a UDT

Post by heatherfeuer »

Thanks for the input, chead.  I actually did get everything working as I wanted, including the fieldset tags.  I am using a third party script.  The reason for my questions was because the script itself included one function to generate a random code.  I was able to remove the actual function and save it in a separate file that I could call from the script.  That way I was able to put the entire script (including the form itself) into a UDT.  No other parameters are needed.  Plus I can customize the script quite easily for different forms in different tags.

If you'd like to see the form itself in action, you can see a dev version that sends an email to me (instead of the actual recipient) here: http://www.ffgf.org/index.php?page=contact-2.  Feel free to play with it since it emails me.  It's not even in the menu -- the actual contact form IS in the menu and mails to the proper person, so don't use that to play with!  ;)

If you'd like to see any part of the code, let me know.  The script itself was free to use and modify and I am more than willing to share it.  In my estimation, it's superior to the form builder module -- even for a complete novice like me!  The form itself is table-free and completely styled using CSS and (very nearly) follows the recommended method for accessible forms.
User avatar
chead
Forum Members
Forum Members
Posts: 63
Joined: Tue Feb 06, 2007 4:01 am

Re: Including php functions in a UDT

Post by chead »

Yes; share code.  :)

I'm curious now how it's doing it's thing since the form's action parameter is blank, and the JScript form validation function doesn't seem to be called anywhere.
heatherfeuer

Re: Including php functions in a UDT

Post by heatherfeuer »

Ahhhhhh, but you see, there IS NO javascript!!

Here is the script itself:

Code: Select all

<?php
/*******************************************
My Form Processor
Adapted from the mailer script by
Gavin Bell, http://www.bellonline.co.uk
*******************************************/

extract($_POST);
$host = $_SERVER[HTTP_HOST ];
$path = pathinfo($_SERVER['PHP_SELF']);
$file_path = $path['dirname'];

/*******************************************
Set all variables
*******************************************/

//Email address form will be sent to
$sendto_email = "outreach@ffgf.org";

// Disable email addresses from the same domain as your email from being sent? 
// This will often reduce spam but will not allow anyone to send from anything@yourdomain. 
$checkdomain = "yes";

// Language variables
$lang_title = "Send an email";
$lang_notice = "Fill in the form to contact us by email. Required fields are shown in bold.";
$lang_name = "Your name:";
$lang_email = "Your email:";
$lang_phone = "Your phone:";
$lang_subject = "Subject:";
$lang_message = "Comments:";
$lang_checkbox = "Please contact me as soon as possible regarding this matter.";
$lang_confirmation = "Please enter validation code. (Note: it is case sensitive.):";
$lang_submit = "Send email";

// Error messages
$lang_error = "Your comments were not submitted. Please correct the following errors:";
$lang_noemail = "You did not enter your email address.";
$lang_nocode = "You did not enter the validation code.";
$lang_wrongcode = "You entered the validation code incorrectly. Please note that it is case sensitive";
$lang_invalidemail = "The email address that you entered appears to be invalid";

// Success
$lang_sent = "Thank you for your comments.<br />The following message was submitted:";

/*******************************************
Validate the form
In this case, just checking for email and
validation code.
*******************************************/

//check for empty email
if (empty ($senders_email))
	{
	$error = "1";
	$info_error .= $lang_noemail . "<br />";  
	}
//check for invalid email address
if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,6}$", $senders_email))
	{
	$error = "1";
	$info_error .= $lang_invalidemail . "<br />"; 
	}

//check to see if security code is entered
if (empty ($security_code))  
	{
	$error = "1";
	$info_error .= $lang_nocode . "<br />";  
	}

//check to make sure code entered correctly
elseif ($security_code != $randomness)  
	{
	$error = "1";
	$info_error .= $lang_wrongcode . "<br />";  
	}

//if errors were found, display them
if ($error == "1") 
	{
	$info_notice = "<span style=\"font-weight: bold;\">" . $lang_error . "</span><br />"; 
	
	if (empty ($submit)) 
		{
		$info_error = "";
		$info_notice = $lang_notice;
		}	

	require_once("random.php");

	$random_code = random();
	$mail_message = stripslashes($mail_message);

//display the form
	print "
	<form name=\"contact\" method=\"post\" action=\"\">

		<p>$info_notice<span class=\"error\">$info_error</span></p>

		<fieldset>
			<legend>Tell us how to get in touch with you:</legend>
				<label for=\"name\">$lang_name</label>
					<input type=\"text\" id=\"senders_name\" name=\"senders_name\" accesskey=\"n\" tabindex=\"1\" title=\"your name\" value=\"$senders_name\" /><br />
				<label for=\"email\" class=\"required\">$lang_email</label>
					<input type=\"text\" id=\"senders_email\" name=\"senders_email\" accesskey=\"e\" tabindex=\"2\" title=\"your email\" value=\"$senders_email\" /><br />
				<label for=\"phone\">$lang_phone</label>
					<input type=\"text\" id=\"phone\" name=\"phone\" accesskey=\"p\" tabindex=\"3\" title=\"your phone\" value=\"$senders_phone\" /><br />
		</fieldset>
		<p> </p>
		<fieldset>
			<legend>Enter your comments in the space provided:</legend>
			<label for=\"comments\">$lang_message</label>
				<textarea name=\"mail_message\" rows=\"5\" cols=\"23\" id=\"mail_message\" accesskey=\"c\" tabindex=\"4\" title=\"mail_message\">$mail_message</textarea><br />
			<input class=\"checkbox\" type=\"checkbox\" name=\"contact_me\" value=\"$contact_me\" id=\"contact_me\" accesskey=\"x\" tabindex=\"5\" title=\"Contact Me\" />  $lang_checkbox
		</fieldset>

		<p>$lang_confirmation</p>
		<input name=\"security_code\" type=\"text\" id=\"security_code\" size=\"5\" /> 
            <strong>$random_code</strong>     
		<input name=\"randomness\" type=\"hidden\" id=\"randomness\" value=\"$random_code\" />
		<input name=\"submit\" type=\"submit\" id=\"submit\" value=\"$lang_submit\" />
	</form>";
	}

else
	{
	
	if ($checkdomain == "yes") 
		{
		$sender_domain = substr($senders_email, (strpos($senders_email, '@')) +1);
		$recipient_domain = substr($sendto_email, (strpos($sendto_email, '@')) +1);
		if ($sender_domain == $recipient_domain)
			{
			print "Sorry, you cannot send messages from this domain ($sender_domain)";
			exit;
			}		
		}
if(isset($contact_me)){        // Check to see if the checkbox is checked
	$contact_me = "$lang_checkbox";
} 
else {
	$contact_me = " I do not want to be contacted. ";
}

	$info_notice = $lang_sent;
	$mail_message = stripslashes($mail_message);
	$senders_email = preg_replace("/[^a-zA-Z0-9s.@-]/", " ", $senders_email);
	$senders_name = preg_replace("/[^a-zA-Z0-9s]/", " ", $senders_name);
	$mail_subject = "Contact Form Submission";
	$headers = "From: $senders_name <$senders_email> \r\n";
	$headers .= "X-Mailer: BELLonline.co.uk PHP mailer \r\n";
	$mail_contents = "$lang_name $senders_name\n"
	. "\n"
	. "$lang_email $senders_email\n"
	. "\n"
	. "$lang_message\n"
	. "$mail_message\n"
	. "\n"
	. "$contact_me\n";
	mail($sendto_email, $mail_subject, $mail_contents, $headers);
	print "  
	<p>$info_notice</p>
	<p><em>$lang_name</em> $senders_name</p>
	<p><em>$lang_email</em> $senders_email</p>
	<p><em>$lang_subject</em> $mail_subject</p>
	<p><em>$lang_message</em><br /> $mail_message</p>
	<p>$contact_me</p>
	";
	}
?>
And here is the neat little function that does the random string:

Code: Select all

<?php
function Random()
{
	$chars = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjklmnopqrstuvwxyz";
	srand((double)microtime()*1000000);
	$i = 0;
	$pass = '' ;
	while ($i <= 4) 
		{
		$num = rand() % 32;
		$tmp = substr($chars, $num, 1);
		$pass = $pass . $tmp;
		$i++; 
		}
	return $pass; 
}
?>
Nifty, eh?  I was tearing out what few grey hairs I have left trying to find something that could do what I wanted and, better still, could actually understand... sort of.  :D  My head hurts now  :), but I've learned a lot with this little exercise!
User avatar
chead
Forum Members
Forum Members
Posts: 63
Joined: Tue Feb 06, 2007 4:01 am

Re: Including php functions in a UDT

Post by chead »

Interesting. I was guessing the external file either did the mailing or that all of the form handling was being done "inline" -- and inline is the answer.

There is some form validation JavaScript in your source--evidently it's just not used; maybe remnants from something else. You might be able to chop that out, though it's not really hurting anything.

Thanks.
heatherfeuer

Re: Including php functions in a UDT

Post by heatherfeuer »

Ohhhh... yeah.  I forgot all about that bit of javascript.  Thanks for pointing it out. Yeah, it's a holdover from another script I was trying, but I missed removing it because it's in the interior page template.  It's gone now.  The only javascript being used is to make png files transparent in IE and what I think is a more elegant solution to the min/max problem in IE.  (Stoopid IE!!!)

The rather elegant way this particular script handles input errors is just one of the reasons why I am using it!  Another is the random code -- since it's not an image, it's accessible-friendly.  It won't keep all the spambots at bay, but there is also other checks in the script to help ward off the spammers.
Locked

Return to “CMSMS Core”