Diff #4 - trunk/src/Notifications/Broadcaster.php
3,590 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: Broadcaster.php
--- Broadcaster.php (nonexistent) +++ Broadcaster.php (revision 4) @@ -0,0 +1,119 @@ +<?php +declare(strict_types=1); + +namespace App\Webapp\Notifications; + +use Apex\Svc{App, Emailer}; +use App\Webapp\Models\EmailQueue; +use App\Webapp\AdminProfiles; +use App\Users\Exceptions\UserNotExistsException; +use Apex\Armor\Auth\Operations\RandomString; +use Apex\Mercury\Email\EmailMessage; + +/** + * Broadcaster + */ +class Broadcaster +{ + + #[Inject(App::class)] + private App $app; + + #[Inject(Emailer::class)] + private Emailer $emailer; + + #[Inject(AdminProfiles::class)] + private AdminProfiles $admin_profiles; + + /** + * Add queue + */ + public function queue(string $adapter, array $condition = [], array $post = []):?EmailQueue + { + + // CHeck post + if (count($post) == 0) { + $post = $this->app->getAllPost(); + } + + // Get attachments + list($attach_filename, $attach_contents) = ['', '']; + if (isset($_FILES['attachment']) && isset($_FILES['attachment']['tmp_name']) && is_uploaded_file($_FILES['attachment']['tmp_name'])) { + $attach_filename = $_FILES['attachment']['name']; + $attach_contents = base64_encode(file_Get_contents($_FILES['attachment']['tmp_name'])); + } + + // Get send date + $send_date = implode('-', [$post['date_year'], $post['date_month'], $post['date_day']]); + $send_time = implode(':', [$post['time_hour'], $post['time_min'], '00']); + + // Create mhash + $mhash = ''; + do { + $mhash = dechex(crc32(RandomSTring::get(48))); + $count = EmailQueue::count('mhash = %s', $mhash); + if ($count > 0) { + continue; + } + break; + } while (True); + + // Add queue + $queue = EmailQueue::insert([ + 'mhash' => $mhash, + 'sent_by' => $this->app->getUser()->getEmail(), + 'sender' => $post['sender'], + 'reply_to' => $post['reply_to'], + 'send_date' => $send_date . ' ' . $send_time, + 'adapter' => $adapter, + 'subject' => $post['subject'], + 'attach_filename' => $attach_filename, + 'attach_contents' => $attach_contents, + 'text_message' => $post['text_message'], + 'html_message' => $post['html_message'], + 'condition_vars' => base64_encode(json_encode($condition)) + ]); + + // Return + return $queue; + } + + /** + * Send test e-mail + */ + public function sendTestEmail(string $email, array $post = []):void + { + + // Check post + if (count($post) == 0) { + $post = $this->app->getAllPost(); + } + + // Get sender + if (!$admin = $this->admin_profiles->loadUuid($post['sender'])) { + throw new UserNotFoundException("No admin exists with the uuid, " . $ppost['sender']); + } + + // Get e-mail message + $email = new EmailMessage( + to_email: $email, + from_email: $admin->getEmail(), + from_name: $admin->getFullName(), + reply_to: $post['reply_to'] ?? '', + subject: $post['subject'], + text_message: $post['text_message'], + html_message: $post['html_message'] + ); + + // Add attachment, if needed + if (isset($_FILES['attachment']) && isset($_FILES['attachment']['tmp_name']) && is_uploaded_file($_FILES['attachment']['tmp_name'])) { + $email->addAttachment($_FILES['attachment']['name'], file_get_contents($_FILES['attachment']['tmp_name'])); + } + + // Send e-mail + $this->emailer->send($email); + } + +} + +
Full Code
<?php declare(strict_types=1);
namespace App\Webapp\Notifications;
use Apex\Svc{App, Emailer}; use App\Webapp\Models\EmailQueue; use App\Webapp\AdminProfiles; use App\Users\Exceptions\UserNotExistsException; use Apex\Armor\Auth\Operations\RandomString; use Apex\Mercury\Email\EmailMessage;
/** * Broadcaster */ class Broadcaster {
#[Inject(App::class)]
private App $app;
#[Inject(Emailer::class)]
private Emailer $emailer;
#[Inject(AdminProfiles::class)]
private AdminProfiles $admin_profiles;
/**
* Add queue
*/
public function queue(string $adapter, array $condition = [], array $post = []):?EmailQueue
{
// CHeck post
if (count($post) == 0) {
$post = $this->app->getAllPost();
}
// Get attachments
list($attach_filename, $attach_contents) = ['', ''];
if (isset($_FILES['attachment']) && isset($_FILES['attachment']['tmp_name']) && is_uploaded_file($_FILES['attachment']['tmp_name'])) {
$attach_filename = $_FILES['attachment']['name'];
$attach_contents = base64_encode(file_Get_contents($_FILES['attachment']['tmp_name']));
}
// Get send date
$send_date = implode('-', [$post['date_year'], $post['date_month'], $post['date_day']]);
$send_time = implode(':', [$post['time_hour'], $post['time_min'], '00']);
// Create mhash
$mhash = '';
do {
$mhash = dechex(crc32(RandomSTring::get(48)));
$count = EmailQueue::count('mhash = %s', $mhash);
if ($count > 0) {
continue;
}
break;
} while (True);
// Add queue
$queue = EmailQueue::insert([
'mhash' => $mhash,
'sent_by' => $this->app->getUser()->getEmail(),
'sender' => $post['sender'],
'reply_to' => $post['reply_to'],
'send_date' => $send_date . ' ' . $send_time,
'adapter' => $adapter,
'subject' => $post['subject'],
'attach_filename' => $attach_filename,
'attach_contents' => $attach_contents,
'text_message' => $post['text_message'],
'html_message' => $post['html_message'],
'condition_vars' => base64_encode(json_encode($condition))
]);
// Return
return $queue;
}
/**
* Send test e-mail
*/
public function sendTestEmail(string $email, array $post = []):void
{
// Check post
if (count($post) == 0) {
$post = $this->app->getAllPost();
}
// Get sender
if (!$admin = $this->admin_profiles->loadUuid($post['sender'])) {
throw new UserNotFoundException("No admin exists with the uuid, " . $ppost['sender']);
}
// Get e-mail message
$email = new EmailMessage(
to_email: $email,
from_email: $admin->getEmail(),
from_name: $admin->getFullName(),
reply_to: $post['reply_to'] ?? '',
subject: $post['subject'],
text_message: $post['text_message'],
html_message: $post['html_message']
);
// Add attachment, if needed
if (isset($_FILES['attachment']) && isset($_FILES['attachment']['tmp_name']) && is_uploaded_file($_FILES['attachment']['tmp_name'])) {
$email->addAttachment($_FILES['attachment']['name'], file_get_contents($_FILES['attachment']['tmp_name']));
}
// Send e-mail
$this->emailer->send($email);
}
}