a few more base modules
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\maillog\Controller\MaillogController
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Controller;
|
||||
|
||||
use Drupal\Component\Utility\SafeMarkup;
|
||||
use Drupal\Core\Controller\ControllerBase;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class MaillogController extends ControllerBase {
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection;
|
||||
*/
|
||||
protected $database;
|
||||
|
||||
/**
|
||||
* Constructs a \Drupal\maillog\Controller\MaillogController object.
|
||||
*
|
||||
* @param \Drupal\Core\Database\Connection $database
|
||||
* The database connection.
|
||||
*/
|
||||
public function __construct(Connection $database) {
|
||||
$this->database = $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static($container->get('database'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Page callback - Get the Maillog Entry.
|
||||
*
|
||||
* @param int $maillog_id
|
||||
* The Maillog ID
|
||||
*
|
||||
* @return array
|
||||
* The output fields
|
||||
*/
|
||||
public function details($maillog_id) {
|
||||
$maillog_entry = $this->getMaillogEntry(intval($maillog_id));
|
||||
|
||||
if (!$maillog_entry) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$output = array();
|
||||
|
||||
$output['#title'] = $maillog_entry['subject'];
|
||||
|
||||
$output['header_from'] = array(
|
||||
'#title' => t('From'),
|
||||
'#type' => 'item',
|
||||
'#markup' => SafeMarkup::checkPlain($maillog_entry['header_from']),
|
||||
);
|
||||
$output['header_to'] = array(
|
||||
'#title' => t('To'),
|
||||
'#type' => 'item',
|
||||
'#markup' => SafeMarkup::checkPlain($maillog_entry['header_to']),
|
||||
);
|
||||
$output['header_reply_to'] = array(
|
||||
'#title' => t('Reply to'),
|
||||
'#type' => 'item',
|
||||
'#markup' => SafeMarkup::checkPlain($maillog_entry['header_reply_to']),
|
||||
);
|
||||
$output['header_all'] = array(
|
||||
'#title' => t('All'),
|
||||
'#type' => 'item',
|
||||
'#markup' => '<pre>',
|
||||
);
|
||||
|
||||
foreach ($maillog_entry['header_all'] as $header_all_name => $header_all_value) {
|
||||
$output['header_all']['#markup'] .= SafeMarkup::checkPlain($header_all_name) . ': ' . SafeMarkup::checkPlain($header_all_value) . '<br/>';
|
||||
}
|
||||
|
||||
$output['header_all']['#markup'] .= '</pre>';
|
||||
|
||||
$output['body'] = array(
|
||||
'#title' => t('Body'),
|
||||
'#type' => 'item',
|
||||
'#markup' => '<pre>' . SafeMarkup::checkPlain($maillog_entry['body']) . '</pre>',
|
||||
);
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Page Callback - Delete a specific maillog entry.
|
||||
*
|
||||
* @param int $maillog_id
|
||||
* The maillog ID.
|
||||
*/
|
||||
public function delete($maillog_id) {
|
||||
$idmaillog = intval($maillog_id);
|
||||
$this->database->query("DELETE FROM {maillog} WHERE idmaillog = :id", array(':id' => $idmaillog));
|
||||
drupal_set_message(t('Mail with ID @idmaillog has been deleted!', array('@idmaillog' => $idmaillog)));
|
||||
|
||||
return $this->redirect('view.maillog_overview.page_1');
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the Maillog Entry.
|
||||
*
|
||||
* @param int $maillog_id
|
||||
* The maillog ID.
|
||||
*
|
||||
* @return array
|
||||
* Maillog entry as Array
|
||||
*/
|
||||
protected function getMaillogEntry($maillog_id) {
|
||||
$result = $this->database->query("SELECT idmaillog, header_from, header_to, header_reply_to, header_all, subject, body FROM {maillog} WHERE idmaillog=:id", array(
|
||||
':id' => $maillog_id,
|
||||
));
|
||||
|
||||
if ($maillog = $result->fetchAssoc()) {
|
||||
// Unserialize values.
|
||||
$maillog['header_all'] = unserialize($maillog['header_all']);
|
||||
}
|
||||
return $maillog;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\maillog\Form\MaillogClearConfirmForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Form;
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\Core\Form\ConfirmFormBase;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Provides a form for clearing all the maillog entries.
|
||||
*/
|
||||
class MaillogClearConfirmForm extends ConfirmFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'maillog_clear_log';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDescription() {
|
||||
return $this->t('All maillog database entries will be deleted. This action cannot be undone.');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getQuestion() {
|
||||
return $this->t('Are you sure you want to clear all the maillog entries?');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getCancelUrl() {
|
||||
return new Url('maillog.settings');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConfirmText() {
|
||||
return $this->t('Clear');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
Database::getConnection('default')->truncate('maillog')->execute();
|
||||
drupal_set_message($this->t("All maillog entries have been deleted."));
|
||||
$form_state->setRedirect('maillog.settings');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\maillog\Form\MaillogSettingsForm.
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Form;
|
||||
|
||||
use Drupal\Core\Form\ConfigFormBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Configure file system settings for this site.
|
||||
*/
|
||||
class MaillogSettingsForm extends ConfigFormBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFormId() {
|
||||
return 'maillog_settings';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getEditableConfigNames() {
|
||||
return ['maillog.settings'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$config = $this->config('maillog.settings');
|
||||
|
||||
$form = array();
|
||||
|
||||
$form['clear_maillog'] = array(
|
||||
'#type' => 'fieldset',
|
||||
'#title' => $this->t('Clear Maillog'),
|
||||
);
|
||||
|
||||
$form['clear_maillog']['clear'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => $this->t('Clear all maillog entries'),
|
||||
'#submit' => ['::clearLog'],
|
||||
);
|
||||
|
||||
$form['maillog_send'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t("Allow the e-mails to be sent."),
|
||||
'#default_value' => $config->get('send'),
|
||||
);
|
||||
|
||||
$form['maillog_log'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t("Create table entries in maillog table for each e-mail."),
|
||||
'#default_value' => $config->get('log'),
|
||||
);
|
||||
|
||||
$form['maillog_verbose'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t("Display the e-mails on page."),
|
||||
'#default_value' => $config->get('verbose'),
|
||||
'#description' => $this->t('If enabled, anonymous users with permissions will see any verbose output mail.'),
|
||||
);
|
||||
|
||||
/*if (\Drupal::moduleHandler()->moduleExists('mimemail')) {
|
||||
$engines = mimemail_get_engines();
|
||||
// maillog will be unset, because ist would cause an recursion
|
||||
unset($engines['maillog']);
|
||||
$form['maillog_engine'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t("Select the mailengine which should be used."),
|
||||
'#default_value' => $config->get('maillog_engine'),
|
||||
'#options' => $engines,
|
||||
);
|
||||
}*/
|
||||
|
||||
return parent::buildForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->config('maillog.settings')
|
||||
->set('send', $form_state->getValue('maillog_send'))
|
||||
->set('log', $form_state->getValue('maillog_log'))
|
||||
->set('verbose', $form_state->getValue('maillog_verbose'))->save();
|
||||
|
||||
parent::submitForm($form, $form_state);
|
||||
|
||||
if ($this->config('maillog.settings')->get('verbose') == TRUE) {
|
||||
drupal_set_message(t('Any user having the permission "view maillog" will see output of any mail that is sent.'), 'warning');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all the maillog entries.
|
||||
*/
|
||||
public function clearLog(array $form, FormStateInterface $form_state) {
|
||||
$form_state->setRedirect('maillog.clear_log');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\mailsystem\Plugin\mailsystem\Dummy.
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Plugin\Mail;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Database\Database;
|
||||
use Drupal\Core\Mail\MailInterface;
|
||||
use Drupal\Core\Mail\Plugin\Mail\PhpMail;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Provides a 'Dummy' plugin to send emails.
|
||||
*
|
||||
* @Mail(
|
||||
* id = "maillog",
|
||||
* label = @Translation("Maillog Mail-Plugin"),
|
||||
* description = @Translation("Maillog Mail-Plugin for sending and formating complete mails.")
|
||||
* )
|
||||
*/
|
||||
class Maillog implements MailInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function format(array $message) {
|
||||
$default = new PhpMail();
|
||||
return $default->format($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function mail(array $message) {
|
||||
$config = \Drupal::configFactory()->get('maillog.settings');
|
||||
// Log the e-mail
|
||||
if ($config->get('log')) {
|
||||
$record = new \stdClass;
|
||||
|
||||
// In case the subject/from/to is already encoded, decode with
|
||||
// Unicode::mimeHeaderDecode().
|
||||
$record->header_message_id = isset($message['headers']['Message-ID']) ? $message['headers']['Message-ID'] : NULL;
|
||||
$record->subject = $message['subject'];
|
||||
$record->subject = Unicode::substr(Unicode::mimeHeaderDecode($record->subject), 0, 255);
|
||||
$record->body = $message['body'];
|
||||
$record->header_from = isset($message['from']) ? $message['from'] : NULL;
|
||||
$record->header_from = Unicode::mimeHeaderDecode($record->header_from);
|
||||
|
||||
$header_to = array();
|
||||
if (isset($message['to'])) {
|
||||
if (is_array($message['to'])) {
|
||||
foreach ($message['to'] as $value) {
|
||||
$header_to[] = Unicode::mimeHeaderDecode($value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$header_to[] = Unicode::mimeHeaderDecode($message['to']);
|
||||
}
|
||||
}
|
||||
$record->header_to = implode(', ', $header_to);
|
||||
|
||||
$record->header_reply_to = isset($message['headers']['Reply-To']) ? $message['headers']['Reply-To'] : '';
|
||||
$record->header_all = serialize($message['headers']);
|
||||
$record->sent_date = REQUEST_TIME;
|
||||
|
||||
Database::getConnection()->insert('maillog')
|
||||
->fields((array) $record)
|
||||
->execute();
|
||||
}
|
||||
|
||||
// Display the e-mail if the verbose is enabled.
|
||||
if ($config->get('verbose') && \Drupal::currentUser()->hasPermission('view maillog')) {
|
||||
|
||||
// Print the message.
|
||||
$header_output = print_r($message['headers'], TRUE);
|
||||
$output = t('A mail has been sent: <br/> [Subject] => @subject <br/> [From] => @from <br/> [To] => @to <br/> [Reply-To] => @reply <br/> <pre> [Header] => @header <br/> [Body] => @body </pre>', [
|
||||
'@subject' => $message['subject'],
|
||||
'@from' => $message['from'],
|
||||
'@to' => $message['to'],
|
||||
'@reply' => isset($message['reply_to']) ? $message['reply_to'] : NULL,
|
||||
'@header' => $header_output,
|
||||
'@body' => $message['body']
|
||||
]);
|
||||
drupal_set_message($output, 'status', TRUE);
|
||||
}
|
||||
|
||||
if ($config->get('send')) {
|
||||
$default = new PhpMail();
|
||||
$result = $default->mail($message);
|
||||
}
|
||||
elseif (\Drupal::currentUser()->hasPermission('administer maillog')) {
|
||||
$message = t('Sending of e-mail messages is disabled by Maillog module. Go @here to enable.', ['@here' => \Drupal::l('here', Url::fromRoute('maillog.settings'))]);
|
||||
|
||||
drupal_set_message($message, 'warning', TRUE);
|
||||
}
|
||||
else {
|
||||
\Drupal::logger('maillog')->notice('Attempted to send an email, but sending emails is disabled.');
|
||||
}
|
||||
return isset($result) ? $result : TRUE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains Drupal\maillog\Plugin\views\field\MaillogFieldDelete.
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Plugin\views\field;
|
||||
|
||||
use Drupal\views\Plugin\views\field\FieldPluginBase;
|
||||
use Drupal\views\ResultRow;
|
||||
|
||||
/**
|
||||
* Default implementation of the base field plugin.
|
||||
*
|
||||
* @ingroup views_field_handlers
|
||||
*
|
||||
* @PluginID("maillog_field_delete")
|
||||
*/
|
||||
class MaillogFieldDelete extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function render(ResultRow $values) {
|
||||
// Ensure user has permission to delete.
|
||||
if (!\Drupal::currentUser()->hasPermission('delete maillog')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$id = $this->getValue($values);
|
||||
|
||||
$text = !empty($this->options['text']) ? $this->options['text'] : t('delete');
|
||||
|
||||
return \Drupal::l($text, 'maillog.delete', array('maillog_id' => $id), array('query' => drupal_get_destination()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\maillog\Tests\MailUiTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\maillog\Tests\Mail;
|
||||
|
||||
use Drupal\maillog\Plugin\Mail\Maillog;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests the maillog plugin user interface.
|
||||
*
|
||||
* @group maillog
|
||||
*/
|
||||
class MailUiTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['maillog', 'user', 'system', 'views', 'contact'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
// Use the maillog mail plugin.
|
||||
$this->config('system.mail')->set('interface.default', 'maillog')->save();
|
||||
// The system.site.mail setting goes into the From header of outgoing mails.
|
||||
$this->config('system.site')->set('mail', 'simpletest@example.com')->save();
|
||||
|
||||
// Disable e-mail sending.
|
||||
$this->config('maillog.settings')
|
||||
->set('send', FALSE)
|
||||
->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests logging mail with maillog module.
|
||||
*/
|
||||
public function testLogging() {
|
||||
$mail = \Drupal::service('plugin.manager.mail')->mail('maillog', 'ui_test', 'test@example.com', \Drupal::languageManager()->getCurrentLanguage(), [], 'me@example.com', FALSE);
|
||||
$mail['subject'] = 'This is a test subject.';
|
||||
$mail['body'] = 'This message is a test email body.';
|
||||
|
||||
// Send the prepared email.
|
||||
$sender = new Maillog();
|
||||
$sender->mail($mail);
|
||||
|
||||
// Create a user with valid permissions and go to the maillog overview page.
|
||||
$this->drupalLogin($this->drupalCreateUser(['view maillog', 'administer maillog']));
|
||||
$this->drupalGet('admin/reports/maillog');
|
||||
|
||||
// Assert some values and click the subject link.
|
||||
$this->assertText('simpletest@example.com');
|
||||
$this->assertText('test@example.com');
|
||||
$this->clickLink('This is a test subject.');
|
||||
$this->assertText('This message is a test email body.');
|
||||
|
||||
// Test clear log.
|
||||
$this->drupalPostForm('admin/config/development/maillog', [], 'Clear all maillog entries');
|
||||
$this->drupalPostForm(NULL, [], 'Clear');
|
||||
$this->drupalGet('admin/reports/maillog');
|
||||
$this->assertNoText('simpletest@example.com');
|
||||
$this->assertText(t('There are no mail logs in the database.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the drupal_set_message() for disabled mail sending.
|
||||
*/
|
||||
protected function testNotice() {
|
||||
|
||||
// Create a user with valid permissions and a recipient for a message.
|
||||
$recipient = $this->drupalCreateUser();
|
||||
$this->drupalLogin($this->drupalCreateUser(['access user contact forms', 'access user profiles', 'administer maillog']));
|
||||
|
||||
// Send the recipient a message and check the expected notice.
|
||||
$this->drupalPostForm('user/' . $recipient->id() . '/contact', [
|
||||
'subject[0][value]' => 'Test Message',
|
||||
'message[0][value]' => 'This is a test.',
|
||||
], t('Send message'));
|
||||
$this->clickLink('here');
|
||||
$this->assertResponse(200);
|
||||
$this->assertTitle('Maillog Settings | Drupal');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests verbose output after send an email.
|
||||
*/
|
||||
public function testVerboseOutput() {
|
||||
|
||||
// Create a user with valid permissions.
|
||||
$user = $this->drupalCreateUser();
|
||||
$this->drupalLogin($this->drupalCreateUser([
|
||||
'access user contact forms',
|
||||
'access user profiles',
|
||||
'view maillog'
|
||||
]));
|
||||
|
||||
// Send the message.
|
||||
$this->drupalPostForm('user/' . $user->id() . '/contact', [
|
||||
'subject[0][value]' => 'Test Message',
|
||||
'message[0][value]' => 'This is a test.',
|
||||
], t('Send message'));
|
||||
|
||||
// Assert the verbose output.
|
||||
$this->assertText('A mail has been sent:');
|
||||
$this->assertRaw('[To] => ' . $user->getUsername() . '@example.com');
|
||||
$this->assertRaw('[Header] => Array');
|
||||
$this->assertRaw('[X-Mailer] => Drupal');
|
||||
$this->assertRaw('[Content-Type] => text/plain; charset=UTF-8; format=flowed; delsp=yes');
|
||||
$this->assertRaw('[Body] => Hello ' . $user->getUsername());
|
||||
|
||||
// Set verbose to false.
|
||||
$this->config('maillog.settings')->set('verbose', FALSE)->save();
|
||||
$this->drupalPostForm('user/' . $user->id() . '/contact', [
|
||||
'subject[0][value]' => 'Test Message',
|
||||
'message[0][value]' => 'This is a test.',
|
||||
], t('Send message'));
|
||||
|
||||
// Assert there is no output.
|
||||
$this->assertNoText('A mail has been sent:');
|
||||
$this->assertNoRaw('[To] => ' . $user->getUsername() . '@example.com');
|
||||
$this->assertNoRaw('[Header] => Array');
|
||||
|
||||
// Tests that users without permission cannot see verbose output.
|
||||
$this->config('maillog.settings')->set('verbose', TRUE)->save();
|
||||
$this->drupalLogin($this->drupalCreateUser([
|
||||
'access user contact forms',
|
||||
'access user profiles',
|
||||
]));
|
||||
|
||||
$this->drupalPostForm('user/' . $user->id() . '/contact', [
|
||||
'subject[0][value]' => 'Test Message',
|
||||
'message[0][value]' => 'This is a test.',
|
||||
], t('Send message'));
|
||||
|
||||
// Assert there is no output.
|
||||
$this->assertNoText('A mail has been sent:');
|
||||
$this->assertNoRaw('[To] => ' . $user->getUsername() . '@example.com');
|
||||
$this->assertNoRaw('[Header] => Array');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user