[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/components/com_contact/ -> controller.php (source)

   1  <?php
   2  /**
   3   * @version        $Id: controller.php 14974 2010-02-21 14:32:22Z ian $
   4   * @package        Joomla
   5   * @subpackage    Contact
   6   * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7   * @license        GNU/GPL, see LICENSE.php
   8   * Joomla! is free software. This version may have been modified pursuant to the
   9   * GNU General Public License, and as distributed it includes or is derivative
  10   * of works licensed under the GNU General Public License or other free or open
  11   * source software licenses. See COPYRIGHT.php for copyright notices and
  12   * details.
  13   */
  14  
  15  // Check to ensure this file is included in Joomla!
  16  defined('_JEXEC') or die( 'Restricted access' );
  17  
  18  jimport( 'joomla.application.component.controller' );
  19  
  20  /**
  21   * Contact Component Controller
  22   *
  23   * @static
  24   * @package        Joomla
  25   * @subpackage    Contact
  26   * @since 1.5
  27   */
  28  class ContactController extends JController
  29  {
  30      /**
  31       * Display the view
  32       */
  33  	function display()
  34      {
  35          $document =& JFactory::getDocument();
  36  
  37          $viewName    = JRequest::getVar('view', 'category', 'default', 'cmd');
  38          $viewType    = $document->getType();
  39  
  40          // interceptors to support legacy urls
  41          switch ($this->getTask())
  42          {
  43              //index.php?option=com_contact&task=category&id=0&Itemid=4
  44              case 'category':
  45                  $viewName    = 'category';
  46                  $layout        = 'default';
  47                  break;
  48              case 'view':
  49                  $viewName    = 'contact';
  50                  $layout        = 'default';
  51                  break;
  52          }
  53  
  54          // Set the default view name from the Request
  55          $view = &$this->getView($viewName, $viewType);
  56  
  57          // Push a model into the view
  58          $model    = &$this->getModel( $viewName );
  59          if (!JError::isError( $model )) {
  60              $view->setModel( $model, true );
  61          }
  62  
  63          // Workaround for the item view
  64          if ($viewName == 'contact')
  65          {
  66              $modelCat    = &$this->getModel( 'category' );
  67              $view->setModel( $modelCat );
  68          }
  69  
  70          // Display the view
  71          $view->assign('error', $this->getError());
  72          
  73          // View caching logic -- simple... are we logged in?
  74          $user = &JFactory::getUser();
  75          $viewnow = JRequest::getVar('view');
  76          $viewcache = JRequest::getVar('viewcache','1','POST','INT');
  77          
  78          if ($user->get('id') || ($viewnow == 'category' && $viewcache == 0)) {
  79              $view->display();
  80          } else {
  81  
  82              // Workaround for token caching
  83              if ($viewName == 'contact')
  84              {
  85                  ob_start();
  86              }
  87              
  88              $option = JRequest::getCmd('option');
  89              $cache =& JFactory::getCache($option, 'view');
  90              $cache->get($view, 'display');
  91              
  92              // Workaround for token caching
  93              if ($viewName == 'contact')
  94              {
  95                  $contents = ob_get_contents();
  96                  ob_end_clean();
  97                  
  98                  $token            = JUtility::getToken();
  99                  $search         = '#<input type="hidden" name="[0-9a-f]{32}" value="1" />#';
 100                  $replacement     = '<input type="hidden" name="'.$token.'" value="1" />';
 101  
 102                  echo preg_replace($search, $replacement, $contents);
 103              }
 104          }
 105      }
 106  
 107      /**
 108       * Method to send an email to a contact
 109       *
 110       * @static
 111       * @since 1.0
 112       */
 113  	function submit()
 114      {
 115          global $mainframe;
 116  
 117          // Check for request forgeries
 118          JRequest::checkToken() or jexit( 'Invalid Token' );
 119  
 120          // Initialize some variables
 121          $db            = & JFactory::getDBO();
 122          $SiteName    = $mainframe->getCfg('sitename');
 123  
 124          $default    = JText::sprintf( 'MAILENQUIRY', $SiteName );
 125          $contactId    = JRequest::getInt( 'id',            0,            'post' );
 126          $name        = JRequest::getVar( 'name',            '',            'post' );
 127          $email        = JRequest::getVar( 'email',        '',            'post' );
 128          $subject    = JRequest::getVar( 'subject',        $default,    'post' );
 129          $body        = JRequest::getVar( 'text',            '',            'post' );
 130          $emailCopy    = JRequest::getInt( 'email_copy',     0,            'post' );
 131  
 132           // load the contact details
 133          $model        = &$this->getModel('contact');
 134  
 135          // query options
 136          $qOptions['id']    = $contactId;
 137          $contact        = $model->getContact( $qOptions );
 138  
 139          if($contact->email_to == '' && $contact->user_id != 0)
 140          {
 141              $contact_user = JUser::getInstance($contact->user_id);
 142              $contact->email_to = $contact_user->get('email');
 143          }
 144  
 145          /*
 146           * If there is no valid email address or message body then we throw an
 147           * error and return false.
 148           */
 149          jimport('joomla.mail.helper');
 150          if (!$email || !$body || (JMailHelper::isEmailAddress($email) == false))
 151          {
 152              $this->setError(JText::_('CONTACT_FORM_NC'));
 153              $this->display();
 154              return false;
 155          }
 156  
 157          // Contact plugins
 158          JPluginHelper::importPlugin( 'contact' );
 159          $dispatcher    =& JDispatcher::getInstance();
 160  
 161          // Input validation
 162          if  (!$this->_validateInputs( $contact, $email, $subject, $body ) ) {
 163              JError::raiseWarning( 0, $this->getError() );
 164              return false;
 165          }
 166  
 167          // Custom handlers
 168          $post        = JRequest::get( 'post' );
 169          $results    = $dispatcher->trigger( 'onValidateContact', array( &$contact, &$post ) );
 170  
 171          foreach ($results as $result)
 172          {
 173              if (JError::isError( $result )) {
 174                  return false;
 175              }
 176          }
 177  
 178          // Passed Validation: Process the contact plugins to integrate with other applications
 179          $results    = $dispatcher->trigger( 'onSubmitContact', array( &$contact, &$post ) );
 180  
 181          $pparams = &$mainframe->getParams('com_contact');
 182          if (!$pparams->get( 'custom_reply' ))
 183          {
 184              $MailFrom     = $mainframe->getCfg('mailfrom');
 185              $FromName     = $mainframe->getCfg('fromname');
 186  
 187              // Prepare email body
 188              $prefix = JText::sprintf('ENQUIRY_TEXT', JURI::base());
 189              $body     = $prefix."\n".$name.' <'.$email.'>'."\r\n\r\n".stripslashes($body);
 190  
 191              $mail = JFactory::getMailer();
 192  
 193              $mail->addRecipient( $contact->email_to );
 194              $mail->setSender( array( $email, $name ) );
 195              $mail->setSubject( $FromName.': '.$subject );
 196              $mail->setBody( $body );
 197  
 198              $sent = $mail->Send();
 199  
 200              /*
 201               * If we are supposed to copy the admin, do so.
 202               */
 203              // parameter check
 204              $params = new JParameter( $contact->params );
 205              $emailcopyCheck = $params->get( 'show_email_copy', 0 );
 206  
 207              // check whether email copy function activated
 208              if ( $emailCopy && $emailcopyCheck )
 209              {
 210                  $copyText         = JText::sprintf('Copy of:', $contact->name, $SiteName);
 211                  $copyText         .= "\r\n\r\n".$body;
 212                  $copySubject     = JText::_('Copy of:')." ".$subject;
 213  
 214                  $mail = JFactory::getMailer();
 215  
 216                  $mail->addRecipient( $email );
 217                  $mail->setSender( array( $MailFrom, $FromName ) );
 218                  $mail->setSubject( $copySubject );
 219                  $mail->setBody( $copyText );
 220  
 221                  $sent = $mail->Send();
 222              }
 223          }
 224  
 225          $msg = JText::_( 'Thank you for your e-mail');
 226          $link = JRoute::_('index.php?option=com_contact&view=contact&id='.$contact->slug.'&catid='.$contact->catslug, false);
 227          $this->setRedirect($link, $msg);
 228      }
 229  
 230      /**
 231       * Method to output a vCard
 232       *
 233       * @static
 234       * @since 1.0
 235       */
 236  	function vcard()
 237      {
 238          global $mainframe;
 239  
 240          // Initialize some variables
 241          $db = & JFactory::getDBO();
 242  
 243          $SiteName = $mainframe->getCfg('sitename');
 244          $contactId = JRequest::getVar('contact_id', 0, '', 'int');
 245          // Get a Contact table object and load the selected contact details
 246          JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_contact'.DS.'tables');
 247          $contact =& JTable::getInstance('contact', 'Table');
 248          $contact->load($contactId);
 249          $user =& JFactory::getUser();
 250  
 251          // Get the contact detail parameters
 252          $params = new JParameter($contact->params);
 253  
 254          // Show the Vcard if contact parameter indicates (prevents direct access)
 255          if (($params->get('allow_vcard', 0)) && ($user->get('aid', 0) >= $contact->access))
 256          {
 257              // Parse the contact name field and build the nam information for the vcard.
 258              $firstname     = null;
 259              $middlename = null;
 260              $surname     = null;
 261  
 262              // How many parts do we have?
 263              $parts = explode(' ', $contact->name);
 264              $count = count($parts);
 265  
 266              switch ($count) {
 267                  case 1 :
 268                      // only a first name
 269                      $firstname = $parts[0];
 270                      break;
 271  
 272                  case 2 :
 273                      // first and last name
 274                      $firstname = $parts[0];
 275                      $surname = $parts[1];
 276                      break;
 277  
 278                  default :
 279                      // we have full name info
 280                      $firstname = $parts[0];
 281                      $surname = $parts[$count -1];
 282                      for ($i = 1; $i < $count -1; $i ++) {
 283                          $middlename .= $parts[$i].' ';
 284                      }
 285                      break;
 286              }
 287              // quick cleanup for the middlename value
 288              $middlename = trim($middlename);
 289  
 290              // Create a new vcard object and populate the fields
 291              require_once(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_contact'.DS.'helpers'.DS.'vcard.php');
 292              $v = new JvCard();
 293  
 294              $v->setPhoneNumber($contact->telephone, 'PREF;WORK;VOICE');
 295              $v->setPhoneNumber($contact->fax, 'WORK;FAX');
 296              $v->setName($surname, $firstname, $middlename, '');
 297              $v->setAddress('', '', $contact->address, $contact->suburb, $contact->state, $contact->postcode, $contact->country, 'WORK;POSTAL');
 298              $v->setEmail($contact->email_to);
 299              $v->setNote($contact->misc);
 300              $v->setURL( JURI::base(), 'WORK');
 301              $v->setTitle($contact->con_position);
 302              $v->setOrg(html_entity_decode($SiteName, ENT_COMPAT, 'UTF-8'));
 303  
 304              $filename = str_replace(' ', '_', $contact->name);
 305              $v->setFilename($filename);
 306  
 307              $output = $v->getVCard(html_entity_decode($SiteName, ENT_COMPAT, 'UTF-8'));
 308              $filename = $v->getFileName();
 309  
 310              // Send vCard file headers
 311              header('Content-Disposition: attachment; filename='.$filename);
 312              header('Content-Length: '.strlen($output));
 313              header('Connection: close');
 314              header('Content-Type: text/x-vCard; name='.$filename);
 315              header('Cache-Control: store, cache');
 316              header('Pragma: cache');
 317  
 318              print $output;
 319          } else {
 320              JError::raiseWarning('SOME_ERROR_CODE', 'ContactController::vCard: '.JText::_('ALERTNOTAUTH'));
 321              return false;
 322          }
 323      }
 324  
 325      /**
 326       * Validates some inputs based on component configuration
 327       *
 328       * @param Object    $contact    JTable Object
 329       * @param String    $email        Email address
 330       * @param String    $subject    Email subject
 331       * @param String    $body        Email body
 332       * @return Boolean
 333       * @access protected
 334       * @since 1.5
 335       */
 336  	function _validateInputs( $contact, $email, $subject, $body )
 337      {
 338          global $mainframe;
 339  
 340          $session =& JFactory::getSession();
 341  
 342          // Get params and component configurations
 343          $params        = new JParameter($contact->params);
 344          $pparams    = &$mainframe->getParams('com_contact');
 345  
 346          // check for session cookie
 347          $sessionCheck     = $pparams->get( 'validate_session', 1 );
 348          $sessionName    = $session->getName();
 349          if  ( $sessionCheck ) {
 350              if ( !isset($_COOKIE[$sessionName]) ) {
 351                  $this->setError( JText::_('ALERTNOTAUTH') );
 352                  return false;
 353              }
 354          }
 355  
 356          // Determine banned e-mails
 357          $configEmail    = $pparams->get( 'banned_email', '' );
 358          $paramsEmail    = $params->get( 'banned_mail', '' );
 359          $bannedEmail     = $configEmail . ($paramsEmail ? ';'.$paramsEmail : '');
 360  
 361          // Prevent form submission if one of the banned text is discovered in the email field
 362          if(false === $this->_checkText($email, $bannedEmail )) {
 363              $this->setError( JText::sprintf('MESGHASBANNEDTEXT', JText::_('Email')) );
 364              return false;
 365          }
 366  
 367          // Determine banned subjects
 368          $configSubject    = $pparams->get( 'banned_subject', '' );
 369          $paramsSubject    = $params->get( 'banned_subject', '' );
 370          $bannedSubject     = $configSubject . ( $paramsSubject ? ';'.$paramsSubject : '');
 371  
 372          // Prevent form submission if one of the banned text is discovered in the subject field
 373          if(false === $this->_checkText($subject, $bannedSubject)) {
 374              $this->setError( JText::sprintf('MESGHASBANNEDTEXT',JText::_('Subject')) );
 375              return false;
 376          }
 377  
 378          // Determine banned Text
 379          $configText        = $pparams->get( 'banned_text', '' );
 380          $paramsText        = $params->get( 'banned_text', '' );
 381          $bannedText     = $configText . ( $paramsText ? ';'.$paramsText : '' );
 382  
 383          // Prevent form submission if one of the banned text is discovered in the text field
 384          if(false === $this->_checkText( $body, $bannedText )) {
 385              $this->setError( JText::sprintf('MESGHASBANNEDTEXT', JText::_('Message')) );
 386              return false;
 387          }
 388  
 389          // test to ensure that only one email address is entered
 390          $check = explode( '@', $email );
 391          if ( strpos( $email, ';' ) || strpos( $email, ',' ) || strpos( $email, ' ' ) || count( $check ) > 2 ) {
 392              $this->setError( JText::_( 'You cannot enter more than one email address', true ) );
 393              return false;
 394          }
 395  
 396          return true;
 397      }
 398  
 399      /**
 400       * Checks $text for values contained in the array $array, and sets error message if true...
 401       *
 402       * @param String    $text        Text to search against
 403       * @param String    $list        semicolon (;) seperated list of banned values
 404       * @return Boolean
 405       * @access protected
 406       * @since 1.5.4
 407       */
 408  	function _checkText($text, $list) {
 409          if(empty($list) || empty($text)) return true;
 410          $array = explode(';', $list);
 411          foreach ($array as $value) {
 412              $value = trim($value);
 413              if(empty($value)) continue;
 414              if ( JString::stristr($text, $value) !== false ) {
 415                  return false;
 416              }
 417          }
 418          return true;
 419      }
 420  
 421  
 422  
 423  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1