Diff #4 - trunk/share/HttpControllers/Webapp/EmailClicked.php
1,889 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: EmailClicked.php
--- EmailClicked.php (nonexistent) +++ EmailClicked.php (revision 4) @@ -0,0 +1,68 @@ +<?php +declare(strict_types = 1); + +namespace App\HttpControllers\Webapp; + +use Apex\Svc\View; +use App\Webapp\Models{EmailQueue, EmailAction, EmailLink}; +use Nyholm\Psr7\Response; +use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; +use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface}; + +/** + * Http Controller - ~alias~ + */ +class EmailClicked implements MiddlewareInterface +{ + + #[Inject(View::class)] + private View $view; + + /** + * Process request + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface + { + + // Init + list($link_hash, $mhash, $tracking_id) = explode(':', $app->pathParam('hash'), 3); + $ip = $app->getClient()->getIpAddress(); + + // Check for link + if (!$link = EmailLink::whereFirst('link_hash = %s', $link_hash)) { + $html = $this->view->render('404.html'); + return new Response(404, [], $html); + } + + // Check if already tracked + if ($row = EmailAction::whereFirst("mhash = %s AND link_hash = %s AND action = 'click' AND tracking_id = %s", $mhash, $link_hash, $tracking_id)) { + return new Response(200, ['Location' => $link->url], ''); + } + + // Get queue + if (!$queue = EmailQueue::whereFirst('mhash = %s', $mhash)) { + $html = $this->view->render('404.html'); + return new Response(404, [], $html); + } + + // Add to db + $log = EmailAction::insert([ + 'mhash' => $mhash, + 'link_hash' => $link_hash, + 'action' => 'click', + 'ip_address' => $ip, + 'tracking_id' => $tracking_id + ]); + + // Update clicks + $queue->clicks++; + $queue->save(); + + // Return response + return new Response(200, ['Location' => $link->url], ''); + } + +} + + +
Full Code
<?php declare(strict_types = 1);
namespace App\HttpControllers\Webapp;
use Apex\Svc\View; use App\Webapp\Models{EmailQueue, EmailAction, EmailLink}; use Nyholm\Psr7\Response; use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface};
/** * Http Controller - ~alias~ */ class EmailClicked implements MiddlewareInterface {
#[Inject(View::class)]
private View $view;
/**
* Process request
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface
{
// Init
list($link_hash, $mhash, $tracking_id) = explode(':', $app->pathParam('hash'), 3);
$ip = $app->getClient()->getIpAddress();
// Check for link
if (!$link = EmailLink::whereFirst('link_hash = %s', $link_hash)) {
$html = $this->view->render('404.html');
return new Response(404, [], $html);
}
// Check if already tracked
if ($row = EmailAction::whereFirst("mhash = %s AND link_hash = %s AND action = 'click' AND tracking_id = %s", $mhash, $link_hash, $tracking_id)) {
return new Response(200, ['Location' => $link->url], '');
}
// Get queue
if (!$queue = EmailQueue::whereFirst('mhash = %s', $mhash)) {
$html = $this->view->render('404.html');
return new Response(404, [], $html);
}
// Add to db
$log = EmailAction::insert([
'mhash' => $mhash,
'link_hash' => $link_hash,
'action' => 'click',
'ip_address' => $ip,
'tracking_id' => $tracking_id
]);
// Update clicks
$queue->clicks++;
$queue->save();
// Return response
return new Response(200, ['Location' => $link->url], '');
}
}