/**
  * Add recipients to the email.
  *
  * @param   mixed   $recipient  Either a string or array of strings [email address(es)]
  * @param   mixed   $name       Either a string or array of strings [name(s)]
  * @param   string  $method     The parent method's name.
  *
  * @return  Mail  Returns this object for chaining.
  *
  * @since   11.1
  */
 protected function add($recipient, $name = '', $method = 'AddAddress')
 {
     // If the recipient is an array, add each recipient... otherwise just add the one
     if (is_array($recipient)) {
         if (is_array($name)) {
             $combined = array_combine($recipient, $name);
             if ($combined === false) {
                 throw new InvalidArgumentException("The number of elements for each array isn't equal.");
             }
             foreach ($combined as $recipientEmail => $recipientName) {
                 $recipientEmail = Helper::cleanLine($recipientEmail);
                 $recipientName = Helper::cleanLine($recipientName);
                 call_user_func('parent::' . $method, $recipientEmail, $recipientName);
             }
         } else {
             $name = Helper::cleanLine($name);
             foreach ($recipient as $to) {
                 $to = Helper::cleanLine($to);
                 call_user_func('parent::' . $method, $to, $name);
             }
         }
     } else {
         $recipient = Helper::cleanLine($recipient);
         call_user_func('parent::' . $method, $recipient, $name);
     }
     return $this;
 }
 /**
  * Validation and filtering
  *
  * @return  boolean  True if satisfactory
  *
  * @since   11.1
  */
 public function check()
 {
     // Set user id to null istead of 0, if needed
     if ($this->id === 0) {
         $this->id = null;
     }
     // Validate user information
     if (trim($this->name) == '') {
         $this->setError(Text::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_YOUR_NAME'));
         return false;
     }
     if (trim($this->username) == '') {
         $this->setError(Text::_('JLIB_DATABASE_ERROR_PLEASE_ENTER_A_USER_NAME'));
         return false;
     }
     if (preg_match("#[<>\"'%;()&]#i", $this->username) || strlen(utf8_decode($this->username)) < 2) {
         $this->setError(Text::sprintf('JLIB_DATABASE_ERROR_VALID_AZ09', 2));
         return false;
     }
     if (trim($this->email) == "" || !Helper::isEmailAddress($this->email)) {
         $this->setError(Text::_('JLIB_DATABASE_ERROR_VALID_MAIL'));
         return false;
     }
     // Set the registration timestamp
     if (empty($this->registerDate) || $this->registerDate == $this->_db->getNullDate()) {
         $this->registerDate = Factory::getDate()->toSql();
     }
     // Set the lastvisitDate timestamp
     if (empty($this->lastvisitDate)) {
         $this->lastvisitDate = $this->_db->getNullDate();
     }
     // Check for existing username
     $query = $this->_db->getQuery(true);
     $query->select($this->_db->quoteName('id'));
     $query->from($this->_db->quoteName('#__users'));
     $query->where($this->_db->quoteName('username') . ' = ' . $this->_db->quote($this->username));
     $query->where($this->_db->quoteName('id') . ' != ' . (int) $this->id);
     $this->_db->setQuery($query);
     $xid = (int) $this->_db->loadResult();
     if ($xid && $xid != (int) $this->id) {
         $this->setError(Text::_('JLIB_DATABASE_ERROR_USERNAME_INUSE'));
         return false;
     }
     // Check for existing email
     $query->clear();
     $query->select($this->_db->quoteName('id'));
     $query->from($this->_db->quoteName('#__users'));
     $query->where($this->_db->quoteName('email') . ' = ' . $this->_db->quote($this->email));
     $query->where($this->_db->quoteName('id') . ' != ' . (int) $this->id);
     $this->_db->setQuery($query);
     $xid = (int) $this->_db->loadResult();
     if ($xid && $xid != (int) $this->id) {
         $this->setError(Text::_('JLIB_DATABASE_ERROR_EMAIL_INUSE'));
         return false;
     }
     // Check for root_user != username
     $config = Factory::getConfig();
     $rootUser = $config->get('root_user');
     if (!is_numeric($rootUser)) {
         $query->clear();
         $query->select($this->_db->quoteName('id'));
         $query->from($this->_db->quoteName('#__users'));
         $query->where($this->_db->quoteName('username') . ' = ' . $this->_db->quote($rootUser));
         $this->_db->setQuery($query);
         $xid = (int) $this->_db->loadResult();
         if ($rootUser == $this->username && (!$xid || $xid && $xid != (int) $this->id) || $xid && $xid == (int) $this->id && $rootUser != $this->username) {
             $this->setError(Text::_('JLIB_DATABASE_ERROR_USERNAME_CANNOT_CHANGE'));
             return false;
         }
     }
     return true;
 }
 /**
  * Create a mailer object
  *
  * @return  Mail object
  *
  * @see     Mail
  * @since   11.1
  */
 protected static function createMailer()
 {
     $conf = self::getConfig();
     $smtpauth = $conf->get('smtpauth') == 0 ? null : 1;
     $smtpuser = $conf->get('smtpuser');
     $smtppass = $conf->get('smtppass');
     $smtphost = $conf->get('smtphost');
     $smtpsecure = $conf->get('smtpsecure');
     $smtpport = $conf->get('smtpport');
     $mailfrom = $conf->get('mailfrom');
     $fromname = $conf->get('fromname');
     $mailer = $conf->get('mailer');
     // Create a JMail object
     $mail = Mail::getInstance();
     // Set default sender without Reply-to
     $mail->SetFrom(MailHelper::cleanLine($mailfrom), MailHelper::cleanLine($fromname), 0);
     // Default mailer is to use PHP's mail function
     switch ($mailer) {
         case 'smtp':
             $mail->useSMTP($smtpauth, $smtphost, $smtpuser, $smtppass, $smtpsecure, $smtpport);
             break;
         case 'sendmail':
             $mail->IsSendmail();
             break;
         default:
             $mail->IsMail();
             break;
     }
     return $mail;
 }