<?PHP

/**************************************
    Webutler V3.0 - www.webutler.de
    Copyright (c) 2008 - 2014
    Autor: Sven Zinke
    Free for any use
    Lizenz: GPL
**************************************/


if(preg_match('#/includes/forms/sendform.php#i', $_SERVER['REQUEST_URI']))
    exit('no access');


$forms_langpath = $this->config['server_path'].'/includes/forms/lang';
if(isset($_SESSION['language']) && file_exists($forms_langpath.'/'.$_SESSION['language'].'.php'))
    include $forms_langpath.'/'.$_SESSION['language'].'.php';
else
    include $forms_langpath.'/'.$webutlercouple->config['defaultlang'].'.php';

$webutlercouple->autoheaderdata[] = '<script type="text/javascript">
    /* <![CDATA[ */
        function forms_onsubmit(formname, fields) {
        	var fieldname = fields.split(",");
        	for(var i = 0; i < fieldname.length; i++) {
        		if (document.forms[formname].elements[fieldname[i]].value == \'\') {
        			alert(\''._FORMSLANG_FIELD_.' "\' + fieldname[i] + \'" '._FORMSLANG_MANDATORY_.'\');
        			return false;
        		}
        	}
        }
    /* ]]> */
    </script>';

if(count($_POST) > 0 && isset($_POST['sendform']))
{
    $forms_sendto = preg_replace('#[^0-9]#', '', $_POST['sendto']);
    if(!isset($forms_sendto) || $forms_sendto == "")
        $forms_sendto = "1";
	
	$forms_postmail = preg_replace('#[^a-z0-9-_.@]#', '', strtolower($_POST['eMail']));
	
	unset($_POST['sendform']);
	unset($_POST['sendto']);
	unset($_POST['eMail']);
    
    if(!preg_match('#^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$#', $forms_postmail)) {
        $forms_mailfalse = true;
        $forms_sendeinfo = _FORMSLANG_VALIDMAIL_;
    }
    
    if(!is_array($webutlercouple->mailaddresses) || !array_key_exists('sendto'.$forms_sendto, $webutlercouple->mailaddresses))
    {
        $forms_mailfalse = true;
        $forms_sendeinfo = _FORMSLANG_NOADDRESSE_;
    }
    
    if(!isset($forms_mailfalse))
    {
        $forms_docroot = $webutlercouple->config['server_path'].'/includes/forms';
		$forms_usertpl = $forms_docroot.'/tpls/usermail'.(file_exists($forms_docroot.'/tpls/usermail_'.$forms_sendto.'.tpl') ? '_'.$forms_sendto : '').'.tpl';
		$forms_admintpl = $forms_docroot.'/tpls/adminmail'.(file_exists($forms_docroot.'/tpls/adminmail_'.$forms_sendto.'.tpl') ? '_'.$forms_sendto : '').'.tpl';
        require_once $forms_docroot.'/config.php';
		
		$mailcontent = array();
        
    	$count = 0;
		$mailcontent['text'] = array();
    	
    	foreach($_POST as $name => $value) {
    		if($value != "") {
                $mailcontent['text'][$count]['name'] = htmlentities($name);
    			if(is_array($value)) {
					$mailvalue = array();
    				foreach($value as $val) {
                        $mailvalue[] = htmlentities($val);
    				}
					$mailcontent['text'][$count]['value'] = implode('<br>', $mailvalue);
    			}
    			else {
                    $mailcontent['text'][$count]['value'] = htmlentities($value);
    			}
				$count++;
    		}
    	}
		
		$fileusertpl = file_get_contents($forms_usertpl);
		$fileadmintpl = file_get_contents($forms_admintpl);
		foreach($forms_mailimages as $imgname => $imgvalue) {
			$forms_file = $forms_docroot.'/imgs/'.$imgvalue;
			if(file_exists($forms_file) && (substr_count($fileusertpl, '$mailcontent[\''.$imgname.'\']') > 0 || substr_count($fileadmintpl, '$mailcontent[\''.$imgname.'\']') > 0)) {
				if($fp = fopen($forms_file, 'rb', 0)) {
					$forms_img = fread($fp, filesize($forms_file));
					fclose($fp);
					$forms_size = getimagesize($forms_file);
					if($forms_size[2] == 1 || $forms_size[2] == 2 || $forms_size[2] == 3) {
						$forms_base64img = chunk_split(base64_encode($forms_img));
						$mailcontent[$imgname] = 'data:'.$forms_size['mime'].';base64,'.$forms_base64img;
					}
				}
			}
		}
		unset($fileusertpl);
		unset($fileadmintpl);
		
		$mailcontent['adminsubject'] = htmlentities($webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengerbetreff']);
        
        $forms_lang = (isset($_SESSION['language'])) ? $_SESSION['language'] : $webutler_config['defaultlang'];
        $forms_bestaetigungsbetreff = $webutlercouple->mailaddresses['sendto'.$forms_sendto]['bestaetigungsbetreff'];
        $formsbestaetigungsbetreff = (!is_array($forms_bestaetigungsbetreff)) ? $forms_bestaetigungsbetreff : $forms_bestaetigungsbetreff[$forms_lang];
        
		$mailcontent['usersubject'] = htmlentities($formsbestaetigungsbetreff);
		
		$mailcontent['footer'] = '';
        if(_FORMSLANG_MAILFOOTER_ != '') {
            $forms_conf_fusslines = explode('<br>', _FORMSLANG_MAILFOOTER_);
			$forms_mailfoot = array();
            foreach($forms_conf_fusslines as $forms_conf_fussline) {
                $forms_mailfoot[] = htmlentities($forms_conf_fussline);
            }
			$mailcontent['footer'] = implode('<br>', $forms_mailfoot);
        }
        
        require_once $webutlercouple->config['server_path'].'/includes/modexts/phpmailer/mailer.php';
		
		ob_start();
		include $forms_usertpl;
		$usermail = ob_get_contents();
		ob_end_clean();
        
        $forms_mailtohome = new PHPMailer();
        $forms_mailtohome->IsMail();
        $forms_mailtohome->SetFrom($forms_postmail);
        $forms_mailtohome->AddAddress($webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengermail'], $webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengername']);
        $forms_mailtohome->Subject = $webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengerbetreff'];
        $forms_mailtohome->AltBody = _FORMSLANG_HTMLCOMPATIBLE_;
        $forms_mailtohome->MsgHTML($usermail);
        $forms_mailtohome->IsHTML(true);
    
        if(!$forms_mailtohome->Send()) {
            $forms_sendeinfo = _FORMSLANG_MAILNOTSEND_;
    	}
        else {
			ob_start();
			include $forms_admintpl;
			$adminmail = ob_get_contents();
			ob_end_clean();
			
            $forms_sendeinfo = _FORMSLANG_SENDINGOK_;
            if($webutlercouple->mailaddresses['sendto'.$forms_sendto]['bestaetigung'] == "1") {
                $forms_mailtouser = new PHPMailer();
                $forms_mailtouser->IsMail();
                $forms_mailtouser->SetFrom($webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengermail'], $webutlercouple->mailaddresses['sendto'.$forms_sendto]['empfaengername']);
                $forms_mailtouser->AddAddress($forms_postmail, $forms_postmail);
                $forms_mailtouser->Subject = $formsbestaetigungsbetreff;
                $forms_mailtouser->AltBody = _FORMSLANG_HTMLCOMPATIBLE_;
                $forms_mailtouser->MsgHTML($adminmail);
                $forms_mailtouser->IsHTML(true);
				
                if(!$forms_mailtouser->Send()) {
            	   $forms_sendeinfo.= '\n'._FORMSLANG_CONFIRMNOTSEND_;
            	}
            }
        }
        unset($_POST);
    }
    
    if(isset($forms_sendeinfo))
    {
        $webutlercouple->autoheaderdata[] = '<script type="text/javascript">
                (function()
                {
                    function forms_sendeinfo() {
                        alert(\''.$forms_sendeinfo.'\');
                    }
                    if (window.addEventListener) {
                        window.addEventListener(\'load\', forms_sendeinfo, false);    
                    } else {
                        window.attachEvent(\'onload\', forms_sendeinfo);
                    }
                })();
            </script>';
    }
}

?>
