Diff #4 - trunk/share/HttpControllers/Webapp/EmailOpened.php
1,571 bytes
|
|
January 20, 2025 at 08:20
|
Diff
Index: EmailOpened.php
--- EmailOpened.php (nonexistent) +++ EmailOpened.php (revision 4) @@ -0,0 +1,59 @@ +<?php +declare(strict_types = 1); + +namespace App\HttpControllers\Webapp; + +use App\Webapp\Models{EmailQueue, EmailAction}; +use Nyholm\Psr7\Response; +use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; +use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface}; + +/** + * Http Controller - ~alias~ + */ +class EmailOpened implements MiddlewareInterface +{ + + /** + * Process request + */ + public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface + { + + // Init + $hash = preg_replace("/..+$/", "", $app->pathParam('mhash')); + list($mhash, $tracking_id) = explode(':', $hash, 2); + $ip = $app->getClient()->getIpAddress(); + $res = new Response(200, ['Content-type' => 'image/png'], base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wAAAgAB/7Lr3QAAAABJRU5ErkJggg==')); + + // Check if already tracked + if ($row = EmailAction::whereFirst("mhash = %s AND action = 'open' AND tracking_id = %s", $mhash, $tracking_id)) { + return $res; + } + + // Get queue + if (!$queue = EmailQueue::whereFirst('mhash = %s', $mhash)) { + return $res; + } + + // Add to db + $log = EmailAction::insert([ + 'mhash' => $mhash, + 'action' => 'open', + 'ip_address' => $ip, + 'tracking_id' => $tracking_id + ]); + + // Update opened + $queue->opens++; + $queue->save(); + + // Return response + return $res; + } + +} + + + +
Full Code
<?php declare(strict_types = 1);
namespace App\HttpControllers\Webapp;
use App\Webapp\Models{EmailQueue, EmailAction}; use Nyholm\Psr7\Response; use Psr\Http\Message{ServerRequestInterface, ResponseInterface}; use Psr\Http\Server{MiddlewareInterface, RequestHandlerInterface};
/** * Http Controller - ~alias~ */ class EmailOpened implements MiddlewareInterface {
/**
* Process request
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $app): ResponseInterface
{
// Init
$hash = preg_replace("/\..+$/", "", $app->pathParam('mhash'));
list($mhash, $tracking_id) = explode(':', $hash, 2);
$ip = $app->getClient()->getIpAddress();
$res = new Response(200, ['Content-type' => 'image/png'], base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/wAAAgAB/7Lr3QAAAABJRU5ErkJggg=='));
// Check if already tracked
if ($row = EmailAction::whereFirst("mhash = %s AND action = 'open' AND tracking_id = %s", $mhash, $tracking_id)) {
return $res;
}
// Get queue
if (!$queue = EmailQueue::whereFirst('mhash = %s', $mhash)) {
return $res;
}
// Add to db
$log = EmailAction::insert([
'mhash' => $mhash,
'action' => 'open',
'ip_address' => $ip,
'tracking_id' => $tracking_id
]);
// Update opened
$queue->opens++;
$queue->save();
// Return response
return $res;
}
}