Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @version $Id: contact.php 13031 2009-10-02 21:54:22Z louis $
- * @package Joomla.Site
- * @subpackage Contact
- * @copyright Copyright (C) 2005 - 2009 Open Source Matters, Inc. All rights reserved.
- * @license GNU General Public License version 2 or later; see LICENSE.txt
- */
- // No direct access
- defined('_JEXEC') or die;
- jimport('joomla.application.component.modelitem');
- jimport('joomla.database.query');
- /**
- * @package Joomla.Site
- * @subpackage Contact
- */
- class ContactModelContact extends JModelItem
- {
- /**
- * Model context string.
- *
- * @var string
- */
- protected $_context = 'com_contact.contact';
- /**
- * Method to auto-populate the model state.
- *
- * @return void
- */
- protected function _populateState()
- {
- $app = &JFactory::getApplication('site');
- // Load state from the request.
- $pk = JRequest::getInt('id');
- $this->setState('contact.id', $pk);
- $offset = JRequest::getInt('limitstart');
- $this->setState('list.offset', $offset);
- // Load the parameters.
- ## $params = $app->getParams();
- ## $this->setState('params', $params);
- // TODO: Tune these values based on other permissions.
- $this->setState('filter.published', 1);
- $this->setState('filter.archived', -1);
- $this->setState('filter.access', true);
- }
- /**
- * Method to get contact data.
- *
- * @param integer The id of the article.
- *
- * @return mixed Menu item data object on success, false on failure.
- */
- public function &getItem($pk = null)
- {
- // Initialise variables.
- $pk = (!empty($pk)) ? $pk : (int) $this->getState('contact.id');
- if ($this->_item === null) {
- $this->_item = array();
- }
- if (!isset($this->_item[$pk]))
- {
- try
- {
- $query = new JQuery;
- $query->select($this->getState('item.select', 'a.*'));
- $query->from('#__contact_details AS a');
- // Join on category table.
- $query->select('c.title AS category_title, c.alias AS category_alias, c.access AS category_access');
- $query->join('LEFT', '#__categories AS c on c.id = a.catid');
- $query->where('a.id = '.(int) $pk);
- // Filter by published state.
- $published = $this->getState('filter.published');
- $archived = $this->getState('filter.archived');
- if (is_numeric($published)) {
- $query->where('(a.published = '.(int) $published.' OR a.published ='.(int) $archived.')');
- }
- // Filter by access level.
- if ($access = $this->getState('filter.access'))
- {
- $user = &JFactory::getUser();
- $groups = implode(',', $user->authorisedLevels());
- $query->where('a.access IN ('.$groups.')');
- $query->where('(c.access IS NULL OR c.access IN ('.$groups.'))');
- }
- $this->_db->setQuery($query);
- $data = $this->_db->loadObject();
- if ($error = $this->_db->getErrorMsg()) {
- throw new Exception($error);
- }
- if (empty($data)) {
- throw new Exception(JText::_('Contact_Error_Contact_not_found'), 404);
- }
- // Check for published state if filter set.
- if (((is_numeric($published))||(is_numeric($archived))) &&
- (($data->published != $published ) && ( $data->state != $archived )))
- {
- throw new Exception(JText::_('Contact_Error_Contact_not_found'), 404);
- }
- // Convert parameter fields to objects.
- $registry = new JRegistry;
- ## $registry->loadJSON($data->params);
- ## $data->params = clone $this->getState('params');
- ## $data->params->merge($registry);
- ##
- // Compute access permissions.
- if ($access)
- {
- // If the access filter has been set, we already know this user can view.
- $data->params->set('access-view', true);
- }
- else
- {
- // If no access filter is set, the layout takes some responsibility for display of limited information.
- $user = &JFactory::getUser();
- $groups = $user->authorisedLevels();
- if ($data->catid == 0 || $data->category_access === null) {
- $data->params->set('access-view', in_array($data->access, $groups));
- }
- else {
- $data->params->set('access-view', in_array($data->access, $groups) && in_array($data->category_access, $groups));
- }
- }
- // TODO: Type 2 permission checks?
- $this->_item[$pk] = $data;
- }
- catch (Exception $e)
- {
- $this->setError($e);
- $this->_item[$pk] = false;
- }
- }
- return $this->_item[$pk];
- }
- /**
- * Method to send an email to a contact
- *
- * @static
- * @since 1.0
- */
- function submit()
- {
- // Check for request forgeries
- JRequest::checkToken() or jexit(JText::_('JInvalid_Token'));
- // Initialise some variables
- $app = &JFactory::getApplication();
- $db = & JFactory::getDbo();
- $SiteName = $app->getCfg('sitename');
- $default = JText::sprintf('MAILENQUIRY', $SiteName);
- $contactId = JRequest::getInt('id', 0, 'post');
- $name = JRequest::getVar('name', '', 'post');
- $email = JRequest::getVar('email', '', 'post');
- $subject = JRequest::getVar('subject', $default, 'post');
- $body = JRequest::getVar('text', '', 'post');
- $emailCopy = JRequest::getInt('email_copy', 0, 'post');
- // load the contact details
- $model = &$this->getModel('contact');
- // query options
- $qOptions['id'] = $contactId;
- $contact = $model->getContact($qOptions);
- if ($contact->email_to == '' && $contact->user_id != 0)
- {
- $contact_user = JUser::getInstance($contact->user_id);
- $contact->email_to = $contact_user->get('email');
- }
- /*
- * If there is no valid email address or message body then we throw an
- * error and return false.
- */
- jimport('joomla.mail.helper');
- if (!$email || !$body || (JMailHelper::isEmailAddress($email) == false))
- {
- $this->setError(JText::_('CONTACT_FORM_NC'));
- $this->display();
- return false;
- }
- // Contact plugins
- JPluginHelper::importPlugin('contact');
- $dispatcher = &JDispatcher::getInstance();
- // Input validation
- if (!$this->_validateInputs($contact, $email, $subject, $body)) {
- JError::raiseWarning(0, $this->getError());
- return false;
- }
- // Custom handlers
- $post = JRequest::get('post');
- $results = $dispatcher->trigger('onValidateContact', array(&$contact, &$post));
- foreach ($results as $result)
- {
- if (JError::isError($result)) {
- return false;
- }
- }
- // Passed Validation: Process the contact plugins to integrate with other applications
- $results = $dispatcher->trigger('onSubmitContact', array(&$contact, &$post));
- ## $pparams = &$app->getParams('com_contact');
- if (!$pparams->get('custom_reply'))
- {
- $MailFrom = $app->getCfg('mailfrom');
- $FromName = $app->getCfg('fromname');
- // Prepare email body
- $prefix = JText::sprintf('ENQUIRY_TEXT', JURI::base());
- $body = $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);
- $mail = JFactory::getMailer();
- $mail->addRecipient($contact->email_to);
- $mail->setSender(array($email, $name));
- $mail->setSubject($FromName.': '.$subject);
- $mail->setBody($body);
- $sent = $mail->Send();
- /*
- * If we are supposed to copy the admin, do so.
- */
- // parameter check
- $params = new JParameter($contact->params);
- $emailcopyCheck = $params->get('show_email_copy', 0);
- // check whether email copy function activated
- if ($emailCopy && $emailcopyCheck)
- {
- $copyText = JText::sprintf('Copy of:', $contact->name, $SiteName);
- $copyText .= "\r\n\r\n".$body;
- $copySubject = JText::_('Copy of:')." ".$subject;
- $mail = JFactory::getMailer();
- $mail->addRecipient($email);
- $mail->setSender(array($MailFrom, $FromName));
- $mail->setSubject($copySubject);
- $mail->setBody($copyText);
- $sent = $mail->Send();
- }
- }
- $msg = JText::_('Com_Contact_Contact_Email_Thanks');
- //redirect if it is set
- if ($this->contact->params->$link)
- {
- $link=$contact->redirect;
- }
- else
- {
- // stay on the same contact page
- $link = JRoute::_('index.php?option=com_contact&view=contact&id='.(int) $contact->id, false);
- }
- $this->setRedirect($link, $msg);
- }
- /**
- * Checks $text for values contained in the array $array, and sets error message if true...
- *
- * @param String $text Text to search against
- * @param String $list semicolon (;) seperated list of banned values
- * @return Boolean
- * @access protected
- * @since 1.5.4
- */
- function _checkText($text, $list) {
- if (empty($list) || empty($text)) return true;
- $array = explode(';', $list);
- foreach ($array as $value) {
- $value = trim($value);
- if (empty($value)) continue;
- if (JString::stristr($text, $value) !== false) {
- return false;
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment