<?php
/**
 * This is a quick contact page.
 *
 * This only uses a single messenger to get to the party.
 *
 */
unset($gCfg);
unset(
$gCurrent);
unset(
$gVars);

/* Initial Requirements */
require_once('config.php');
//require_once('libraries/gacl/gacl.class.php');
//require_once('libraries/gacl/gacl_api.class.php');
require_once('libraries/SipTemplate.class.php');
#require_once('libraries/SipModule.class.php');
#require_once('libraries/SipCode.class.php');
#require_once('libraries/SipProgram.class.php');
#require_once('libraries/ExternalCode.class.php');

/**
 * For those times when mail() does not work.
 */
function send_mail($in_to$in_subject$in_message$in_headers=null)
{
    global 
$default;
    
    
$serv $GLOBALS['gCfg']['contact']['server'];
    
$host $_SERVER['SERVER_ADDR'];
    
$sh fsockopen($serv['host'], $serv['port'], $errno$errstr80);
    if (!
$sh) {
        return 
false;
    }
    
$smtpResponse fgets($sh515);
    
fputs($sh"HELO $host\r\n");
    
$smtpResponse .= fgets($sh515);
    if (isset(
$serv['auth'])) {
        
fputs($sh"AUTH LOGIN\r\n");
        
$smtpResponse .= fgets($sh515);
        
fputs($shbase64_encode($serv['auth']['username']) . "\r\n");
        
$smtpResponse .= fgets($sh515);
        
fputs($shbase64_encode($serv['auth']['password']) . "\r\n");
        
$smtpResponse .= fgets($sh515);
    }
    
fputs($sh"MAIL FROM: <webmaster@syberisle.net>\r\n");
    
$smtpResponse .= fgets($sh515);
    if (
is_array($in_to)) {
        foreach(
$in_to as $to) {
            
fputs($sh"RCPT TO: $to\r\n");
            
$smtpResponse .= fgets($sh515);
        }
    }
    else {
        
fputs($sh"RCPT TO: $in_to\r\n");
        
$smtpResponse .= fgets($sh515);
    }
    
fputs($sh"DATA\r\n");
    
$smtpResponse .= 'data > '.fgets($sh515);
    if (
$in_headers) {
        
fputs($sh$in_headers "\r\n");
    }
    
$in_message preg_replace('/\n/'"\r\n"$in_message);
    
fputs($sh"Subject: $in_subject\r\n\r\n$in_message\r\n");
    
fputs($sh".\r\n");
    
$smtpResponse .= fgets($sh515);
    
fputs($sh"QUIT\r\n");
    
$smtpResponse .= fgets($sh515);

    return 
true;
}



/* Define the base theme, and the base template */
$gCurrent['name_display'] = (false == $_SESSION['user']['name_display']) ? $gCfg['name_display'][($gCfg['defaults']['name_display'])]['style'] : $gCfg['name_display'][($_SESSION['user']['name_display'])]['style'];
$gCurrent['date_display'] = (false == $_SESSION['user']['date_display']) ? $gCfg['date_display'][($gCfg['defaults']['date_display'])]['style'] : $gCfg['date_display'][($_SESSION['user']['date_display'])]['style'];
$gCurrent['theme'] = $gCfg['themes'][($gCfg['defaults']['theme'])];
$gCurrent['theme_path'] = $gCfg['paths']['base_dir'] . '/themes/' $gCurrent['theme']['path'];
$gCurrent['tpl_path'] = $gCfg['paths']['base_dir'] . '/themes/' $gCurrent['theme']['path'] . '/templates/';
$gCfg['current'] = $gCurrent;

/* global configuration into the vars */
$gVars $gCfg['urls'];
$gVars['css'] = $gCfg['urls']['theme'] . $gCurrent['theme']['path'] . '/css/';
$gVars['theme_path'] = $gCfg['urls']['theme'] . $gCurrent['theme']['path'] . '/';
$gVars['theme'] = $gCurrent['theme']['path'];

$o_tpl = new SipTemplate($gCfg);
$s_path = &$gCfg['current']['tpl_path'];
$s_index implode("\n"SipTemplate::get_template("{$s_path}/index.tpl"true));
$s_contactForm implode("\n"SipTemplate::get_template("{$s_path}/contact_form.tpl"true));

$a_contactErrors = array();

// detect if the form is being submitted!
$bError false;
if (isset(
$_POST['submitbutton'])) {
    
// XXX: need to integrate a CAPTCHA

    // strip data from malicious users.
    
$s_msgName strip_tags($_POST['name']);
    
$s_msgEmail strip_tags($_POST['email']);
    
$s_msgPhone strip_tags($_POST['phone']);
    
$s_msgData strip_tags($_POST['comment']);

    
// verify that all data is here
    
if (!$s_msgName || !$s_msgEmail || !$s_msgPhone || !$s_msgData) {
        
$a_contactErrors = array('error_name'  => !$s_msgName  ' error' '',
                        
'error_email' => !$s_msgEmail ' error' '',
                        
'error_phone' => !$s_msgName  ' error' '',
                        
'error_data'  => !$s_msgData  ' error' '');
        
$bError true;
        
        if (!
$s_msgName) {
            
$a_contactErrors['errors'] .= '<li>Missing Your Name</li>';
        }
        if (!
$s_msgPhone) {
            
$a_contactErrors['errors'] .= '<li>Missing Phone</li>';
        }
        if (!
$s_msgEmail) {
            
$a_contactErrors['errors'] .= '<li>Missing Email</li>';
        }
        if (!
$s_msgData) {
            
$a_contactErrors['errors'] .= '<li>Missing Details</li>';
        }
    }

    if (! 
$bError) {
        
// generate the email message
        
$s_subject 'JSD Truck Repair contact request';
        
$s_message "
This is an automated email sent from jsdtrucking.com Contact form
-------------------------------------------------------------------------------
Name: 
{$s_msgName}
Email: 
{$s_msgEmail}
Phone: 
{$s_msgPhone}
Comment:
{$s_msgData}";
        
$s_headers "From: automated-reply@jsdtrucking.com\r\n" 
                     
"Reply-To: {$s_msgFrom}\r\n" .
                     
'X-Mailer: PHP/' phpversion();

        if (isset(
$gCfg['contact']['use_php']) && $gCfg['contact']['use_php']) {
            
mail($gCfg['contact']['email'], $gCfg['contact']['subject'], $s_message$s_headers);
        }
        else {
            if (!
send_mail($gCfg['contact']['email'], $gCfg['contact']['subject'], $s_message$s_headers)) {
                
$bError true;
            }
        }
        
// XXX: send message to recipient after CAPTCHA is working
    
}
    else {
    
    }
}

$o_tpl->replace_variables(&$s_contactForm$a_contactErrorstruetrue);
$o_tpl->replace_variables(&$s_index, array('pagetitle'   => 'Contact Us',
                                           
'theme_path'  => $gVars['theme_path'],
                                           
'loggedout'   => true,
                                           
'has_content' => true,
                                           
'has_error'   => $bError,
                                           
'content'     => $s_contactForm,
                                           
'self'        => '/index.php'), truetrue);
echo 
$s_index;

?>