2019-07-17 20:08:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @copyright Copyright (C) 2005-2009 Keyboard Monkeys Ltd. http://www.kb-m.com
|
|
|
|
* @license http://creativecommons.org/licenses/BSD/ BSD License
|
|
|
|
* @author Keyboard Monkey Ltd
|
|
|
|
* @since CommunityID 0.9
|
|
|
|
* @package CommunityID
|
|
|
|
* @packager Keyboard Monkeys
|
|
|
|
*/
|
|
|
|
|
2019-07-17 20:16:19 +00:00
|
|
|
class MessageusersController extends CommunityID_Controller_Action
|
2019-07-17 20:08:50 +00:00
|
|
|
{
|
|
|
|
public function indexAction()
|
|
|
|
{
|
|
|
|
$appSession = Zend_Registry::get('appSession');
|
|
|
|
if (isset($appSession->messageUsersForm)) {
|
|
|
|
$this->view->messageUsersForm = $appSession->messageUsersForm;
|
|
|
|
unset($appSession->messageUsersForm);
|
|
|
|
} else {
|
2019-07-17 20:16:19 +00:00
|
|
|
$this->view->messageUsersForm = new Form_MessageUsers();
|
2019-07-17 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_helper->actionStack('index', 'login', 'users');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendAction()
|
|
|
|
{
|
2019-07-17 20:16:19 +00:00
|
|
|
$form = new Form_MessageUsers();
|
2019-07-17 20:08:50 +00:00
|
|
|
$formData = $this->_request->getPost();
|
|
|
|
|
|
|
|
$form->populate($formData);
|
|
|
|
if (!$form->isValid($formData)) {
|
|
|
|
return $this->_redirectFaultyForm($form);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cc = $form->getValue('cc');
|
2019-07-17 20:16:19 +00:00
|
|
|
$bccArr = array();
|
2019-07-17 20:08:50 +00:00
|
|
|
if (trim($cc) != '') {
|
|
|
|
$validator = new Zend_Validate_EmailAddress();
|
2019-07-17 20:16:19 +00:00
|
|
|
$bccArr = explode(',', $cc);
|
|
|
|
for ($i = 0; $i < count($bccArr); $i++) {
|
|
|
|
$bccArr[$i] = trim($bccArr[$i]);
|
|
|
|
if (!$validator->isValid($bccArr[$i])) {
|
2019-07-17 20:08:50 +00:00
|
|
|
foreach ($validator->getMessages() as $messageId => $message) {
|
|
|
|
$form->cc->addError($this->view->translate('CC field must be a comma-separated list of valid E-mails'));
|
|
|
|
return $this->_redirectFaultyForm($form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail = self::getMail(
|
|
|
|
$form->getValue('subject'),
|
|
|
|
$this->_getParam('messageType'),
|
|
|
|
$this->_getParam('messageType') == 'plain'?
|
|
|
|
$form->getValue('bodyPlain')
|
|
|
|
: $form->getValue('bodyHTML')
|
|
|
|
);
|
|
|
|
|
|
|
|
$mail->setSubject($form->getValue('subject'));
|
|
|
|
if ($this->_getParam('messageType') == 'plain') {
|
|
|
|
$mail->setBodyText($form->getValue('bodyPlain'));
|
|
|
|
} else {
|
|
|
|
$mail->setBodyHtml($form->getValue('bodyHTML'));
|
|
|
|
}
|
|
|
|
|
2019-07-17 20:16:19 +00:00
|
|
|
$users = new Users_Model_Users();
|
2019-07-17 20:08:50 +00:00
|
|
|
foreach ($users->getUsers() as $user) {
|
2019-07-17 20:16:19 +00:00
|
|
|
if ($user->role == ROLE_ADMIN) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail->addBcc($user->email);
|
2019-07-17 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-17 20:16:19 +00:00
|
|
|
foreach ($bccArr as $bcc) {
|
|
|
|
$mail->addBcc($bcc);
|
2019-07-17 20:08:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$mail->send();
|
|
|
|
$this->_helper->FlashMessenger->addMessage('Message has been sent');
|
|
|
|
} catch (Zend_Mail_Protocol_Exception $e) {
|
|
|
|
$this->_helper->FlashMessenger->addMessage('There was an error trying to send the message');
|
|
|
|
if ($this->_config->logging->level == Zend_Log::DEBUG) {
|
|
|
|
$this->_helper->FlashMessenger->addMessage($e->getMessage());
|
|
|
|
|
|
|
|
return $this->_redirectFaultyForm($form);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_redirect('');
|
|
|
|
}
|
|
|
|
|
|
|
|
private function _redirectFaultyForm(Zend_Form $form)
|
|
|
|
{
|
|
|
|
$appSession = Zend_Registry::get('appSession');
|
|
|
|
$appSession->messageUsersForm = $form;
|
|
|
|
|
|
|
|
return $this->_forward('index');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Zend_Mail
|
|
|
|
* @throws Zend_Mail_Protocol_Exception
|
|
|
|
*/
|
|
|
|
public static function getMail()
|
|
|
|
{
|
|
|
|
// can't use $this->_config 'cause we're in a static function
|
|
|
|
$configEmail = Zend_Registry::get('config')->email;
|
|
|
|
switch (strtolower($configEmail->transport)) {
|
|
|
|
case 'smtp':
|
|
|
|
Zend_Mail::setDefaultTransport(
|
|
|
|
new Zend_Mail_Transport_Smtp(
|
|
|
|
$configEmail->host,
|
|
|
|
$configEmail->toArray()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
case 'mock':
|
|
|
|
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Mock());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Zend_Mail::setDefaultTransport(new Zend_Mail_Transport_Sendmail());
|
|
|
|
}
|
|
|
|
|
|
|
|
$mail = new Zend_Mail('UTF-8');
|
2019-07-17 20:16:19 +00:00
|
|
|
$mail->setFrom($configEmail->supportemail);
|
|
|
|
|
|
|
|
// all recipients will be in BCC, but I need at least one in the To header
|
|
|
|
$mail->addTo($configEmail->supportemail);
|
2019-07-17 20:08:50 +00:00
|
|
|
|
|
|
|
return $mail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|