Created
July 2, 2012 16:41
-
-
Save johnny5th/3034170 to your computer and use it in GitHub Desktop.
Simple Form Processor Class - uses php mail() function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* PHP FORM PROCESSOR | |
* A simple form processor with basic validation | |
* Sends with local mail() protocol | |
*/ | |
class FormProc{ | |
/* ATTRIBUTES */ | |
private $groups = array(); | |
private $fields = array(); | |
private $subject; | |
private $to; | |
private $from; | |
private $error = array(); | |
/* CONSTRUCTOR */ | |
public function __construct(){ | |
} | |
/* PRIVATE */ | |
private function genMessage(){ | |
$message = '<html><body>'; | |
// Loop Through Groups | |
foreach($this->groups as $group){ | |
$grouplist = array(); | |
$message .= "<h3>$group</h3>"; | |
for($i=0; $i < sizeof($this->fields); $i++){ | |
$field = $this->fields[$i]; | |
if($field->getGroup() == $group && $field->isSelected() === false) | |
$grouplist[] = $i; | |
} | |
for($i=0; $i < sizeof($grouplist); $i++){ | |
$field = $this->fields[$grouplist[$i]]; | |
if($i == 0) | |
$message .= "<p>"; | |
$message .= "<strong>" . $field->getFieldName() . ":</strong> "; | |
$message .= nl2br(htmlentities($field->getFieldVal())); | |
$field->select(); | |
if($i == sizeof($grouplist)-1) | |
$message .= "</p>"; | |
else | |
$message .= "<br>"; | |
} | |
} | |
// Loop Through Non Grouped Fields | |
$nongrouplist = array(); | |
for($i=0; $i < sizeof($this->fields); $i++){ | |
$field = $this->fields[$i]; | |
if($field->isSelected() === false) | |
$nongrouplist[] = $i; | |
} | |
for($i=0; $i < sizeof($nongrouplist); $i++){ | |
$field = $this->fields[$nongrouplist[$i]]; | |
if($i == 0) | |
$message .= "<br><p>"; | |
$message .= "<strong>" . $field->getFieldName() . ":</strong> "; | |
$message .= nl2br(htmlentities($field->getFieldVal())); | |
$field->select(); | |
if($i == sizeof($nongrouplist)-1) | |
$message .= "</p>"; | |
else | |
$message .= "<br>"; | |
} | |
$message .= '</body></html>'; | |
return $message; | |
} | |
/* STATIC */ | |
static function validator($val, $valtype){ // text, email, phone, zip | |
switch($valtype){ | |
case "phone" : | |
if($val == "") | |
throw new Exception("Empty Phone Number"); | |
if(!FormProc::cleanPhone($val)) | |
throw new Exception("Invalid Phone Number"); | |
break; | |
case "email" : | |
if($val == "") | |
throw new Exception("Empty Email Address"); | |
if(!preg_match('/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i', $val, $matches)) | |
throw new Exception("Invalid Email Address"); | |
break; | |
case "zip" : | |
if($val == "") | |
throw new Exception("Empty Zip Code"); | |
if(!preg_match('/^\d{5}(-\d{4})?$/i', $val, $matches)) | |
throw new Exception("Invalid Zip Code"); | |
break; | |
case "text" : | |
default : | |
if($val == "") | |
throw new Exception("Empty Field"); | |
} | |
} | |
static function cleanPhone($num){ | |
$num = preg_replace("/[^0-9]/", "", $num, -1); | |
$numarray = str_split($num); | |
$numlength = sizeof($numarray); | |
$cleannum = ""; | |
if($numlength == 11){ // Check for the +1 international number | |
for($i=0; $i<$numlength; $i++){ | |
$cleannum += $numarray[$i]; | |
if($i == 0 || $i == 3 || $i == 7) | |
$cleannum += "-"; | |
} | |
return $cleannum; | |
}else if($numlength == 10){ // US Number | |
for($i=0; $i<$numlength; $i++){ | |
$cleannum += $numarray[$i]; | |
if($i == 2 || $i == 6) | |
$cleannum += "-"; | |
} | |
return $cleannum; | |
}else | |
throw new Exception("10-11 characters needed. $numlength given."); | |
} | |
/* PUBLIC */ | |
public function setSubject($subject){ | |
$this->subject = $subject; | |
} | |
public function getSubject(){ | |
return $this->subject; | |
} | |
public function setTo($to){ | |
try{ | |
$this->validator($to, 'email'); // Validate inputted $to field | |
$this->to = $to; | |
} catch(Exception $e){ | |
throw new Exception("Recipient Error: " . $e->getMessage()); | |
} | |
} | |
public function getTo(){ | |
return $this->to; | |
} | |
public function setFrom($from){ | |
try{ | |
$this->validator($from, 'email'); // Validate inputted $from field | |
$this->from = $from; | |
} catch(Exception $e){ | |
throw new Exception("Sender Error: " . $e->getMessage()); | |
} | |
} | |
public function getFrom(){ | |
return $this->from; | |
} | |
public function addGroup($groupname){ | |
if($groupname == "") | |
throw new Exception("Empty Group Name"); | |
if(array_search($groupname, $this->groups) !== false) | |
throw new Exception("Duplicate Group Name"); | |
array_push($this->groups,$groupname); | |
} | |
public function removeGroup($groupname){ | |
if($key = array_search($groupname, $this->groups)){ | |
unset($this->groups['$groupname']); | |
}else{ | |
throw new Exception('Group Not Found'); | |
} | |
} | |
public function addField($fieldname, $fieldval, $validate, $valtype = 'text', $group = ''){ | |
try{ | |
array_push($this->fields, new FormField($fieldname, $fieldval, $validate, $valtype, $group)); | |
}catch(Exception $e){ | |
throw new Exception($fieldname . " Field Error: " . $e->getMessage()); | |
} | |
} | |
public function removeField($fieldname){ | |
$fieldfound = false; | |
for($i=0; $i < sizeof($this->fields); $i++){ // Loop through fields | |
if ($this->fields[$i]->getFieldName() == $fieldname){ // Check for matching fieldname | |
unset($this->fields[$i]); // Delete field | |
$fieldfound = true; | |
} | |
} | |
if($fieldfound == false) // No fields found | |
throw new Exception('Field Not Found'); | |
} | |
public function getField($fieldname){ | |
$fieldfound = false; | |
for($i=0; $i < sizeof($this->fields); $i++){ // Loop through fields | |
if ($this->fields[$i]->getFieldName() == $fieldname){ // Check for matching fieldname | |
return $this->fields[$i]; // Return field | |
$fieldfound = true; | |
} | |
} | |
if($fieldfound == false) // No fields found | |
throw new Exception('Field Not Found'); | |
} | |
public function addError($error){ | |
$this->error[] = $error; | |
} | |
public function getErrors(){ | |
return $this->error; | |
} | |
public function validateFields(){ | |
$this->error = array(); | |
foreach($this->fields as $field){ | |
if($field->getValidation() == true){ | |
try{ | |
$this->validator($field->getFieldVal(), $field->getValType()); | |
}catch(Exception $e){ | |
$this->addError($field->getFieldName() . " Field Error: " . $e->getMessage()); | |
} | |
} | |
} | |
if(empty($this->error)) | |
return true; | |
else | |
return false; | |
} | |
public function send(){ | |
if($this->validateFields()){ | |
// Set Headers | |
$headers = "From: " . $this->getFrom() . "\r\n"; | |
$headers .= "Reply-To: ". $this->getFrom() . "\r\n"; | |
$headers .= "MIME-Version: 1.0\r\n"; | |
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n"; | |
// Generate Message | |
$message = $this->genMessage(); | |
// Send Email | |
if(!mail($this->getTo(), $this->getSubject(), $message, $headers)){ | |
$this->addError('Error Sending Email'); | |
return false; | |
}else | |
return true; | |
}else | |
return false; | |
} | |
public function clear(){ | |
$fields = array(); | |
$groups = array(); | |
$to = ""; | |
$from = ""; | |
$subject = ""; | |
} | |
} | |
class FormField{ | |
/* ATTRIBUTES */ | |
private $fieldname; | |
private $fieldval; | |
private $validate; | |
private $valtype; // text, email, phone, zip | |
private $group; | |
private $selected; | |
/* CONSTRUCTOR */ | |
public function __construct($fieldname, $fieldval, $validate = true, $valtype = 'text', $group = ''){ | |
$this->setFieldName($fieldname); | |
$this->setFieldVal($fieldval); | |
if($validate == true) | |
$this->validationOn(); | |
else | |
$this->validationOff(); | |
$this->setValType($valtype); | |
$this->setGroup($group); | |
$this->selected = false; | |
} | |
/* PUBLIC */ | |
public function select(){ | |
$this->selected = true; | |
} | |
public function deselect(){ | |
$this->selected = false; | |
} | |
public function isSelected(){ | |
return $this->selected; | |
} | |
public function setFieldName($fieldname){ | |
if($fieldname != "") | |
$this->fieldname = $fieldname; | |
else | |
throw new Exception('Invalid Field Name'); | |
} | |
public function getFieldName(){ | |
return $this->fieldname; | |
} | |
public function setFieldVal($fieldval){ | |
$this->fieldval = $fieldval; | |
} | |
public function getFieldVal(){ | |
return $this->fieldval; | |
} | |
public function validationOn(){ | |
$this->validation = true; | |
} | |
public function validationOff(){ | |
$this->validation = false; | |
} | |
public function getValidation(){ | |
return $this->validation; | |
} | |
public function setValType($valtype){ | |
$this->valtype = $valtype; | |
} | |
public function getValType(){ | |
return $this->valtype; | |
} | |
public function setGroup($group){ | |
$this->group = $group; | |
} | |
public function getGroup(){ | |
return $this->group; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment