<?php
  /**
   * Copyright 2011 Zazavi - http://www.zazavi.com
   * Zazavi is free software released under the terms of the GNU General Public License.
   * You should have received a copy of the license.  If not, see <http://www.gnu.org/licenses/>.
   */
  define("_VALID_PHP", true);
  require_once("../init.php");
?>
<?php
  //$msgs = array();
  $post = (!empty($_POST)) ? true : false;
  
  if ($post) {
      if ($_POST['name'] == "")
          $core->msgs['name'] = _CF_NAME_R;
      
      if ($_POST['code'] == "")
          $core->msgs['code'] = _CF_TOTAL_R;
      
      if ($_POST['code'] != "8")
          $core->msgs['code'] = _CF_TOTAL_ERR;
      
      if ($_POST['email'] == "")
          $core->msgs['email'] = _CF_EMAIL_R;
      
      if (!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $_POST['email']))
          $core->msgs['email'] = _CF_EMAIL_ERR;
      
      if ($_POST['message'] == "")
          $core->msgs['message'] = _CF_MSG_R;
      
      if (empty($core->msgs)) {
          
          $sender_email = sanitize($_POST['email']);
          $name = sanitize($_POST['name']);
          $message = strip_tags($_POST['message']);
		  $mailsubject = sanitize($_POST['subject']);
		  $ip = sanitize($_SERVER['REMOTE_ADDR']);

		  require_once(ZAZAVILITE . "lib/class_mailer.php");
		  $mailer = $mail->sendMail();	
					  
		  $row = $core->getRowById("email_templates", 10);
		  
		  $body = str_replace(array('[MESSAGE]', '[SENDER]', '[NAME]', '[MAILSUBJECT]', '[IP]', '[SITE_NAME]', '[URL]'), 
		  array($message, $sender_email, $name, $mailsubject, $ip, $core->site_name, $core->site_url), $row['body']);

		  $message = Swift_Message::newInstance()
					->setSubject($row['subject'])
					->setTo(array($core->site_email => $core->site_name))
					->setFrom(array($sender_email => $name))
					->setBody(cleanOut($body), 'text/html');
	
          if($mailer->send($message)) {
			  print 'OK';
			  $zazavisec->writeLog(_USER . ' ' . $user->username . ' ' . _LG_CONTACT_SENT, "", "no", "user");
		  }
		  
      } else 
          print $core->msgStatus();
  }
?>