<?php
/**************************************************/
/*
Released by AwesomePHP.com, under the GPL License, a
copy of it should be attached to the zip file, or
you can view it on http://AwesomePHP.com/gpl.txt
*/
/**************************************************/
/*
Form Process Class - This class will process any html forms.
User should provide list of required inputs, any emails for the
forum to be sent to and file storage place
*/
class processForum {
/* Variables */
private $mailTo;
private $emailFiles;
private $fileStorage;
private $allowedExt;
private $maxSize;
private $reqFields;
private $postList;
private $errorList;
private $fileList;
private $fileTypes;
private $siteEmail;
private $siteName;
/*
Function mail to
*/
function sendMail($doEmail)
{
$list = preg_split('/[ |:|;|,]/',$doEmail);
foreach($list as $curEmail){ $this->mailTo[] = $curEmail;}
}
/*
Function mail files?
*/
function mailFiles($option = true){ $this->emailFiles=$option;}
/*
Function set storage location
*/
function setStorage($fLocation){ $this->fileStorage = $fLocation;}
/*
Function to set site name/email
*/
function setSettings($name,$email){ $this->siteName = $name; $this->siteEmail = $email;}
/*
Function set extensions
*/
function allowedExtensions($doExtensions)
{
$list = preg_split('/[ |:|;|,]/',$doExtensions);
foreach($list as $curExt){ $this->allowedExt[] = $curExt;}
}
/*
Function set max file size
*/
function maxSize($option){ $this->maxSize=$option;}
/*
Load required fields
*/
function requireField($fList,$POST){
foreach($fList as $curItem){
list($curReq,$template) = $curItem;
if($POST[$curReq] == NULL){ return 'Field '.$curReq.' is required.<br />';}
if($template == 'username'
&& !preg_match('/^([a-zA-Z0-9._])+$/',$POST[$curReq])){ return 'Field '.$curReq.' is invalid.<br />';}
if($template == 'email'
&& !preg_match('/^[.\w-]+@([\w-]+\.)+[a-zA-Z]{2,6}$/',$POST[$curReq])){ return 'Field '.$curReq.' is invalid.<br />';}
}
return NULL;
}
/*
Process forum
*/
function processForm($POST)
{
foreach($POST as $item => $value){
$this->postList .= "$item -> $value<br />";
}
}
/*
Upload files
*/
function uploadFiles($FILES)
{
foreach($FILES as $thisFile){
if($thisFile['name'] != NULL){
$extension = $this->getExtension($thisFile['name']);
$name = $thisFile['name'];
if(!in_array($extension,$this->allowedExt)){
$errorList[] = '['.$name.'] Extension '.$extension.' not allowed.<br />';
} else {
if($thisFile['size'] > $this->maxSize){
$errorList[] = '['.$name.'] Filesize is over '.$this->maxSize.' bytes.<br />';
} else {
$this->processFile($thisFile,$this->fileStorage);
}
}
}
}
/*
Mail the form, and any files if any.
*/
$this->mailForm();
return $errorList;
}
/*
Function to mail the form and files - private
We mail the form first, then each file by itself
*/
private function mailForm(){
$mailHeaders = "Return-Path: ".$this->siteName." <".$this->siteEmail.">\r\n";
$mailHeaders .= "From: ".$this->siteName." <".$this->siteEmail.">\r\n";
$mailHeaders .= "Content-Type: text/html; charset=utf-8;\n\n\r\n";
if($this->emailFiles == true && count($this->fileList) > 0){
foreach($this->fileList as $sub => $curFile){
$fileatt_type = $this->fileTypes[$sub];
$file = fopen($this->fileStorage.$curFile,'rb');
while($dat = fread($file,1025657)){
$attachment_data .= $dat;
}
fclose($file);
$attachment_data = chunk_split(base64_encode($attachment_data));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$fileHeaders = "From: ".$this->siteName." <".$this->siteEmail.">";
$fileHeaders .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$mailMessage = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$this->postList . "\n\n";
// Add file attachment to the message
$mailMessage .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$curFile}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$curFile}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$attachment_data . "\n\n" .
"--{$mime_boundary}--\n";
unset($attachment_data);
foreach($this->mailTo as $curEmail){
mail($curEmail,'Form Email File',$mailMessage,$fileHeaders);
}
}
} else {
foreach($this->mailTo as $curEmail){
mail($curEmail,'Form Email',$this->postList,$mailHeaders);
}
}
}
/*
Actual file upload - private
*/
private function processFile($FILE,$dir)
{
if($FILE['name'] != NULL){
if(is_file($dir.$FILE['name'])){
$FILE['name'] = $this->generate(5).'_'.$FILE['name'];
}
$this->fileList[] = $FILE['name'];
$this->fileTypes[] = $FILE['type'];
move_uploaded_file($FILE['tmp_name'],$dir.$FILE['name']);
}
return;
}
/*
Generate random string - private
*/
private function generate($strlen)
{
return substr(md5(uniqid(rand(),1)),1,$strlen);
}
/*
Get file extension (even those containing periods) - private
*/
private function getExtension($name)
{
return strtolower(end(explode('.',$name)));
}
}
?>