| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentMail\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentMail\App\Models\Logger; |
| 6 |
use FluentMail\Includes\Request\Request; |
| 7 |
|
| 8 |
class LoggerController extends Controller |
| 9 |
{ |
| 10 |
public function get(Request $request, Logger $logger) |
| 11 |
{ |
| 12 |
$this->verify(); |
| 13 |
|
| 14 |
return $this->send( |
| 15 |
$logger->get( |
| 16 |
$request->except(['nonce', 'action']) |
| 17 |
) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
public function show(Request $request, Logger $logger) |
| 22 |
{ |
| 23 |
$this->verify(); |
| 24 |
|
| 25 |
$result = $logger->navigate($request->all()); |
| 26 |
|
| 27 |
return $this->sendSuccess($result); |
| 28 |
} |
| 29 |
|
| 30 |
public function delete(Request $request, Logger $logger) |
| 31 |
{ |
| 32 |
$this->verify(); |
| 33 |
|
| 34 |
$id = (array) $request->get('id'); |
| 35 |
|
| 36 |
$logger->delete($id); |
| 37 |
|
| 38 |
if ($id && $id[0] == 'all') { |
| 39 |
$subject = 'All logs'; |
| 40 |
} else { |
| 41 |
$count = count($id); |
| 42 |
$subject = $count > 1 ? "{$count} Logs" : 'Log'; |
| 43 |
} |
| 44 |
|
| 45 |
return $this->sendSuccess([ |
| 46 |
'message' => sprintf(__('%s deleted successfully.', 'fluent-smtp'), $subject) |
| 47 |
]); |
| 48 |
} |
| 49 |
|
| 50 |
public function retry(Request $request, Logger $logger) |
| 51 |
{ |
| 52 |
$this->verify(); |
| 53 |
|
| 54 |
try { |
| 55 |
$this->app->addAction('wp_mail_failed', function($response) use ($logger, $request) { |
| 56 |
$log = $logger->find($id = $request->get('id')); |
| 57 |
$log['retries'] = $log['retries'] + 1; |
| 58 |
$logger->updateLog($log, ['id' => $id]); |
| 59 |
|
| 60 |
return $this->sendError([ |
| 61 |
'message' => $response->get_error_message(), |
| 62 |
'errors' => $response->get_error_data() |
| 63 |
], $response->get_error_code()); |
| 64 |
}); |
| 65 |
|
| 66 |
$recipients = $this->sanitizeRecipients($request->get('recipients')); |
| 67 |
|
| 68 |
$resentEmail = $logger->resendEmailFromLog( |
| 69 |
$request->get('id'), |
| 70 |
$request->get('type'), |
| 71 |
$recipients |
| 72 |
); |
| 73 |
|
| 74 |
if ($resentEmail) { |
| 75 |
$message = __('Email sent successfully.', 'fluent-smtp'); |
| 76 |
|
| 77 |
if (!empty($recipients)) { |
| 78 |
$message = sprintf( |
| 79 |
/* translators: %s: comma separated list of email addresses */ |
| 80 |
__('Email sent successfully to %s.', 'fluent-smtp'), |
| 81 |
implode(', ', $recipients) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
return $this->sendSuccess([ |
| 86 |
'email' => $resentEmail, |
| 87 |
'recipients' => $recipients, |
| 88 |
'message' => $message |
| 89 |
]); |
| 90 |
} |
| 91 |
|
| 92 |
throw new \Exception(esc_html__('Something went wrong.', 'fluent-smtp'), 400); |
| 93 |
|
| 94 |
} catch (\Exception $e) { |
| 95 |
return $this->sendError([ |
| 96 |
'message' => $e->getMessage() |
| 97 |
], $e->getCode()); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Sanitize and validate a list of recipient email addresses coming |
| 103 |
* from the resend dialog. Returns an array of valid email addresses |
| 104 |
* or an empty array when none are provided / valid. |
| 105 |
* |
| 106 |
* @param mixed $rawRecipients |
| 107 |
* @return array<int, string> |
| 108 |
* @throws \Exception When an invalid email address is supplied. |
| 109 |
*/ |
| 110 |
protected function sanitizeRecipients($rawRecipients) |
| 111 |
{ |
| 112 |
if (empty($rawRecipients)) { |
| 113 |
return []; |
| 114 |
} |
| 115 |
|
| 116 |
if (is_string($rawRecipients)) { |
| 117 |
$rawRecipients = preg_split('/[\s,;]+/', $rawRecipients); |
| 118 |
} |
| 119 |
|
| 120 |
/* |
| 121 |
* Refuse rather than fall through. Returning an empty list here would |
| 122 |
* mean a malformed request quietly resends to the ORIGINAL recipient |
| 123 |
* instead of the one that was asked for - the worst possible answer, |
| 124 |
* since the caller is told the send succeeded and never learns it went |
| 125 |
* somewhere else. |
| 126 |
*/ |
| 127 |
if (!is_array($rawRecipients)) { |
| 128 |
throw new \Exception( |
| 129 |
esc_html__('Could not read the recipient list.', 'fluent-smtp'), |
| 130 |
422 |
| 131 |
); |
| 132 |
} |
| 133 |
|
| 134 |
/* |
| 135 |
* A resend is a manual, one-at-a-time action. A list this long is a |
| 136 |
* mistake or a pasted address book, and either way it is better caught |
| 137 |
* than delivered. |
| 138 |
*/ |
| 139 |
$maxRecipients = apply_filters('fluentsmtp_max_resend_recipients', 25); |
| 140 |
|
| 141 |
if (count($rawRecipients) > $maxRecipients) { |
| 142 |
throw new \Exception( |
| 143 |
sprintf( |
| 144 |
/* translators: %d: maximum number of recipients allowed */ |
| 145 |
esc_html__('Please enter no more than %d email addresses.', 'fluent-smtp'), |
| 146 |
(int) $maxRecipients |
| 147 |
), |
| 148 |
422 |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
$recipients = []; |
| 153 |
|
| 154 |
foreach ($rawRecipients as $recipient) { |
| 155 |
$recipient = sanitize_email(trim((string) $recipient)); |
| 156 |
|
| 157 |
if ($recipient === '') { |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
if (!is_email($recipient)) { |
| 162 |
throw new \Exception( |
| 163 |
sprintf( |
| 164 |
/* translators: %s: email address */ |
| 165 |
esc_html__('Invalid email address: %s', 'fluent-smtp'), |
| 166 |
esc_html($recipient) |
| 167 |
), |
| 168 |
422 |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
$recipients[] = $recipient; |
| 173 |
} |
| 174 |
|
| 175 |
return array_values(array_unique($recipients)); |
| 176 |
} |
| 177 |
|
| 178 |
public function retryBulk(Request $request, Logger $logger) |
| 179 |
{ |
| 180 |
$this->verify(); |
| 181 |
$logIds = $request->get('log_ids', []); |
| 182 |
|
| 183 |
$failedCount = 0; |
| 184 |
$this->app->addAction('wp_mail_failed', function($response) use (&$failedCount) { |
| 185 |
$failedCount++; |
| 186 |
}); |
| 187 |
|
| 188 |
$failedInitiated = 0; |
| 189 |
$successCount = 0; |
| 190 |
foreach ($logIds as $logId) { |
| 191 |
try { |
| 192 |
$email = $logger->resendEmailFromLog($logId, 'check_realtime'); |
| 193 |
$successCount++; |
| 194 |
} catch (\Exception $exception) { |
| 195 |
$failedInitiated++; |
| 196 |
} |
| 197 |
} |
| 198 |
$message = __('The selected emails have been resent.', 'fluent-smtp'); |
| 199 |
|
| 200 |
if ($failedCount) { |
| 201 |
$message .= sprintf(__(' %d of them failed to send.', 'fluent-smtp'), $failedCount); |
| 202 |
} |
| 203 |
|
| 204 |
if ($failedInitiated) { |
| 205 |
$message .= sprintf(__(' %d could not be prepared for sending.', 'fluent-smtp'), $failedInitiated); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->sendSuccess([ |
| 209 |
'message' => $message |
| 210 |
]); |
| 211 |
|
| 212 |
} |
| 213 |
} |
| 214 |
|