Source for file contact.php

Documentation is available at contact.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Site
  4.  * @subpackage  com_contact
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * @package     Joomla.Site
  14.  * @subpackage  com_contact
  15.  */
  16. {
  17.     public function getModel($name ''$prefix ''$config array('ignore_request' => true))
  18.     {
  19.         return parent::getModel($name$prefixarray('ignore_request' => false));
  20.     }
  21.  
  22.     public function submit()
  23.     {
  24.         // Check for request forgeries.
  25.         JSession::checkToken(or jexit(JText::_('JINVALID_TOKEN'));
  26.  
  27.         $app    JFactory::getApplication();
  28.         $model  $this->getModel('contact');
  29.         $params JComponentHelper::getParams('com_contact');
  30.         $stub   $this->input->getString('id');
  31.         $id     = (int) $stub;
  32.  
  33.         // Get the data from POST
  34.         $data  $this->input->post->get('jform'array()'array');
  35.  
  36.         $contact $model->getItem($id);
  37.  
  38.         $params->merge($contact->params);
  39.  
  40.         // Check for a valid session cookie
  41.         if ($params->get('validate_session'0))
  42.         {
  43.             if (JFactory::getSession()->getState(!= 'active'){
  44.                 JError::raiseWarning(403JText::_('COM_CONTACT_SESSION_INVALID'));
  45.  
  46.                 // Save the data in the session.
  47.                 $app->setUserState('com_contact.contact.data'$data);
  48.  
  49.                 // Redirect back to the contact form.
  50.                 $this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id='.$stubfalse));
  51.                 return false;
  52.             }
  53.         }
  54.  
  55.         // Contact plugins
  56.         JPluginHelper::importPlugin('contact');
  57.         $dispatcher    JEventDispatcher::getInstance();
  58.  
  59.         // Validate the posted data.
  60.         $form $model->getForm();
  61.         if (!$form)
  62.         {
  63.             JError::raiseError(500$model->getError());
  64.             return false;
  65.         }
  66.  
  67.         $validate $model->validate($form$data);
  68.  
  69.         if ($validate === false)
  70.         {
  71.             // Get the validation messages.
  72.             $errors    $model->getErrors();
  73.             // Push up to three validation messages out to the user.
  74.             for ($i 0$n count($errors)$i $n && $i 3$i++)
  75.             {
  76.                 if ($errors[$iinstanceof Exception)
  77.                 {
  78.                     $app->enqueueMessage($errors[$i]->getMessage()'warning');
  79.                 else {
  80.                     $app->enqueueMessage($errors[$i]'warning');
  81.                 }
  82.             }
  83.  
  84.             // Save the data in the session.
  85.             $app->setUserState('com_contact.contact.data'$data);
  86.  
  87.             // Redirect back to the contact form.
  88.             $this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id='.$stubfalse));
  89.             return false;
  90.         }
  91.  
  92.         // Validation succeeded, continue with custom handlers
  93.         $results    $dispatcher->trigger('onValidateContact'array(&$contact&$data));
  94.  
  95.         foreach ($results as $result)
  96.         {
  97.             if ($result instanceof Exception)
  98.             {
  99.                 return false;
  100.             }
  101.         }
  102.  
  103.         // Passed Validation: Process the contact plugins to integrate with other applications
  104.         $dispatcher->trigger('onSubmitContact'array(&$contact&$data));
  105.  
  106.         // Send the email
  107.         $sent false;
  108.         if (!$params->get('custom_reply'))
  109.         {
  110.             $sent $this->_sendEmail($data$contact);
  111.         }
  112.  
  113.         // Set the success message if it was a success
  114.         if (!($sent instanceof Exception))
  115.         {
  116.             $msg JText::_('COM_CONTACT_EMAIL_THANKS');
  117.         }
  118.         else
  119.         {
  120.             $msg '';
  121.         }
  122.  
  123.         // Flush the data from the session
  124.         $app->setUserState('com_contact.contact.data'null);
  125.  
  126.         // Redirect if it is set in the parameters, otherwise redirect back to where we came from
  127.         if ($contact->params->get('redirect'))
  128.         {
  129.             $this->setRedirect($contact->params->get('redirect')$msg);
  130.         }
  131.         else
  132.         {
  133.             $this->setRedirect(JRoute::_('index.php?option=com_contact&view=contact&id='.$stubfalse)$msg);
  134.         }
  135.  
  136.         return true;
  137.     }
  138.  
  139.     private function _sendEmail($data$contact)
  140.     {
  141.             $app        JFactory::getApplication();
  142.             if ($contact->email_to == '' && $contact->user_id != 0)
  143.             {
  144.                 $contact_user JUser::getInstance($contact->user_id);
  145.                 $contact->email_to $contact_user->get('email');
  146.             }
  147.             $mailfrom    $app->getCfg('mailfrom');
  148.             $fromname    $app->getCfg('fromname');
  149.             $sitename    $app->getCfg('sitename');
  150.  
  151.             $name        $data['contact_name'];
  152.             $email        JstringPunycode::emailToPunycode($data['contact_email']);
  153.             $subject    $data['contact_subject'];
  154.             $body        $data['contact_message'];
  155.  
  156.             // Prepare email body
  157.             $prefix JText::sprintf('COM_CONTACT_ENQUIRY_TEXT'JUri::base());
  158.             $body    $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);
  159.  
  160.             $mail JFactory::getMailer();
  161.             $mail->addRecipient($contact->email_to);
  162.             $mail->addReplyTo(array($email$name));
  163.             $mail->setSender(array($mailfrom$fromname));
  164.             $mail->setSubject($sitename.': '.$subject);
  165.             $mail->setBody($body);
  166.             $sent $mail->Send();
  167.  
  168.             //If we are supposed to copy the sender, do so.
  169.  
  170.             // check whether email copy function activated
  171.             if array_key_exists('contact_email_copy'$data)  )
  172.             {
  173.                 $copytext        JText::sprintf('COM_CONTACT_COPYTEXT_OF'$contact->name$sitename);
  174.                 $copytext        .= "\r\n\r\n".$body;
  175.                 $copysubject    JText::sprintf('COM_CONTACT_COPYSUBJECT_OF'$subject);
  176.  
  177.                 $mail JFactory::getMailer();
  178.                 $mail->addRecipient($email);
  179.                 $mail->addReplyTo(array($email$name));
  180.                 $mail->setSender(array($mailfrom$fromname));
  181.                 $mail->setSubject($copysubject);
  182.                 $mail->setBody($copytext);
  183.                 $sent $mail->Send();
  184.             }
  185.  
  186.             return $sent;
  187.     }
  188. }

Documentation generated on Tue, 19 Nov 2013 14:56:36 +0100 by phpDocumentor 1.4.3