[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Administrator 5 * @subpackage com_users 6 * 7 * @copyright (C) 2009 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 namespace Joomla\Component\Users\Administrator\Model; 12 13 use Joomla\CMS\Access\Access; 14 use Joomla\CMS\Component\ComponentHelper; 15 use Joomla\CMS\Factory; 16 use Joomla\CMS\Filter\InputFilter; 17 use Joomla\CMS\Form\Form; 18 use Joomla\CMS\Language\Text; 19 use Joomla\CMS\Log\Log; 20 use Joomla\CMS\Mail\Exception\MailDisabledException; 21 use Joomla\CMS\Mail\MailTemplate; 22 use Joomla\CMS\MVC\Model\AdminModel; 23 use Joomla\Database\ParameterType; 24 use PHPMailer\PHPMailer\Exception as phpMailerException; 25 26 // phpcs:disable PSR1.Files.SideEffects 27 \defined('_JEXEC') or die; 28 // phpcs:enable PSR1.Files.SideEffects 29 30 /** 31 * Users mail model. 32 * 33 * @since 1.6 34 */ 35 class MailModel extends AdminModel 36 { 37 /** 38 * Method to get the row form. 39 * 40 * @param array $data An optional array of data for the form to interrogate. 41 * @param boolean $loadData True if the form is to load its own data (default case), false if not. 42 * 43 * @return Form A Form object on success, false on failure 44 * 45 * @since 1.6 46 */ 47 public function getForm($data = array(), $loadData = true) 48 { 49 // Get the form. 50 $form = $this->loadForm('com_users.mail', 'mail', array('control' => 'jform', 'load_data' => $loadData)); 51 52 if (empty($form)) { 53 return false; 54 } 55 56 return $form; 57 } 58 59 /** 60 * Method to get the data that should be injected in the form. 61 * 62 * @return mixed The data for the form. 63 * 64 * @since 1.6 65 * @throws \Exception 66 */ 67 protected function loadFormData() 68 { 69 // Check the session for previously entered form data. 70 $data = Factory::getApplication()->getUserState('com_users.display.mail.data', array()); 71 72 $this->preprocessData('com_users.mail', $data); 73 74 return $data; 75 } 76 77 /** 78 * Method to preprocess the form 79 * 80 * @param Form $form A form object. 81 * @param mixed $data The data expected for the form. 82 * @param string $group The name of the plugin group to import (defaults to "content"). 83 * 84 * @return void 85 * 86 * @since 1.6 87 * @throws \Exception if there is an error loading the form. 88 */ 89 protected function preprocessForm(Form $form, $data, $group = 'user') 90 { 91 parent::preprocessForm($form, $data, $group); 92 } 93 94 /** 95 * Send the email 96 * 97 * @return boolean 98 * 99 * @throws \Exception 100 */ 101 public function send() 102 { 103 $app = Factory::getApplication(); 104 $data = $app->input->post->get('jform', array(), 'array'); 105 $user = Factory::getUser(); 106 $access = new Access(); 107 $db = $this->getDatabase(); 108 $language = Factory::getLanguage(); 109 110 $mode = array_key_exists('mode', $data) ? (int) $data['mode'] : 0; 111 $subject = array_key_exists('subject', $data) ? $data['subject'] : ''; 112 $grp = array_key_exists('group', $data) ? (int) $data['group'] : 0; 113 $recurse = array_key_exists('recurse', $data) ? (int) $data['recurse'] : 0; 114 $bcc = array_key_exists('bcc', $data) ? (int) $data['bcc'] : 0; 115 $disabled = array_key_exists('disabled', $data) ? (int) $data['disabled'] : 0; 116 $message_body = array_key_exists('message', $data) ? $data['message'] : ''; 117 118 // Automatically removes html formatting 119 if (!$mode) { 120 $message_body = InputFilter::getInstance()->clean($message_body, 'string'); 121 } 122 123 // Check for a message body and subject 124 if (!$message_body || !$subject) { 125 $app->setUserState('com_users.display.mail.data', $data); 126 $this->setError(Text::_('COM_USERS_MAIL_PLEASE_FILL_IN_THE_FORM_CORRECTLY')); 127 128 return false; 129 } 130 131 // Get users in the group out of the ACL, if group is provided. 132 $to = $grp !== 0 ? $access->getUsersByGroup($grp, $recurse) : array(); 133 134 // When group is provided but no users are found in the group. 135 if ($grp !== 0 && !$to) { 136 $rows = array(); 137 } else { 138 // Get all users email and group except for senders 139 $uid = (int) $user->id; 140 $query = $db->getQuery(true) 141 ->select( 142 [ 143 $db->quoteName('email'), 144 $db->quoteName('name'), 145 ] 146 ) 147 ->from($db->quoteName('#__users')) 148 ->where($db->quoteName('id') . ' != :id') 149 ->bind(':id', $uid, ParameterType::INTEGER); 150 151 if ($grp !== 0) { 152 $query->whereIn($db->quoteName('id'), $to); 153 } 154 155 if ($disabled === 0) { 156 $query->where($db->quoteName('block') . ' = 0'); 157 } 158 159 $db->setQuery($query); 160 $rows = $db->loadObjectList(); 161 } 162 163 // Check to see if there are any users in this group before we continue 164 if (!$rows) { 165 $app->setUserState('com_users.display.mail.data', $data); 166 167 if (in_array($user->id, $to)) { 168 $this->setError(Text::_('COM_USERS_MAIL_ONLY_YOU_COULD_BE_FOUND_IN_THIS_GROUP')); 169 } else { 170 $this->setError(Text::_('COM_USERS_MAIL_NO_USERS_COULD_BE_FOUND_IN_THIS_GROUP')); 171 } 172 173 return false; 174 } 175 176 // Get the Mailer 177 $mailer = new MailTemplate('com_users.massmail.mail', $language->getTag()); 178 $params = ComponentHelper::getParams('com_users'); 179 180 try { 181 // Build email message format. 182 $data = [ 183 'subject' => stripslashes($subject), 184 'body' => $message_body, 185 'subjectprefix' => $params->get('mailSubjectPrefix', ''), 186 'bodysuffix' => $params->get('mailBodySuffix', '') 187 ]; 188 $mailer->addTemplateData($data); 189 190 $recipientType = $bcc ? 'bcc' : 'to'; 191 192 // Add recipients 193 foreach ($rows as $row) { 194 $mailer->addRecipient($row->email, $row->name, $recipientType); 195 } 196 197 if ($bcc) { 198 $mailer->addRecipient($app->get('mailfrom'), $app->get('fromname')); 199 } 200 201 // Send the Mail 202 $rs = $mailer->send(); 203 } catch (MailDisabledException | phpMailerException $exception) { 204 try { 205 Log::add(Text::_($exception->getMessage()), Log::WARNING, 'jerror'); 206 207 $rs = false; 208 } catch (\RuntimeException $exception) { 209 Factory::getApplication()->enqueueMessage(Text::_($exception->errorMessage()), 'warning'); 210 211 $rs = false; 212 } 213 } 214 215 // Check for an error 216 if ($rs !== true) { 217 $app->setUserState('com_users.display.mail.data', $data); 218 $this->setError($mailer->ErrorInfo); 219 220 return false; 221 } elseif (empty($rs)) { 222 $app->setUserState('com_users.display.mail.data', $data); 223 $this->setError(Text::_('COM_USERS_MAIL_THE_MAIL_COULD_NOT_BE_SENT')); 224 225 return false; 226 } else { 227 /** 228 * Fill the data (specially for the 'mode', 'group' and 'bcc': they could not exist in the array 229 * when the box is not checked and in this case, the default value would be used instead of the '0' 230 * one) 231 */ 232 $data['mode'] = $mode; 233 $data['subject'] = $subject; 234 $data['group'] = $grp; 235 $data['recurse'] = $recurse; 236 $data['bcc'] = $bcc; 237 $data['message'] = $message_body; 238 $app->setUserState('com_users.display.mail.data', array()); 239 $app->enqueueMessage(Text::plural('COM_USERS_MAIL_EMAIL_SENT_TO_N_USERS', count($rows)), 'message'); 240 241 return true; 242 } 243 } 244 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |