Advertisement
ArcaniSGK507

Untitled

Mar 27th, 2025
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services\Email;
  4.  
  5. use Exception;
  6. use App\Services\Session;
  7. use App\Services\Logger;
  8. use App\Services\SecurityService;
  9.  
  10. /**
  11. * Email Rate Limiter
  12. *
  13. * Handles rate limiting for email sending to prevent abuse.
  14. *
  15. * @package App\Services\Email
  16. */
  17. class EmailRateLimiter
  18. {
  19. /**
  20. * @var Logger Logger service
  21. */
  22. private Logger $logger;
  23.  
  24. /**
  25. * @var SecurityService Security service
  26. */
  27. private SecurityService $securityService;
  28.  
  29. /**
  30. * @var int Maximum emails per hour
  31. */
  32. private int $maxEmailsPerHour;
  33.  
  34. /**
  35. * @var string Email log key in session
  36. */
  37. private string $emailLogKey = 'email_sent_log';
  38.  
  39. /**
  40. * EmailRateLimiter constructor
  41. *
  42. * @param int $maxEmailsPerHour Maximum emails per hour
  43. */
  44. public function __construct(int $maxEmailsPerHour = 10)
  45. {
  46. $this->logger = new Logger();
  47. $this->securityService = new SecurityService();
  48. $this->maxEmailsPerHour = $maxEmailsPerHour;
  49. }
  50.  
  51. /**
  52. * Check rate limit for email sending
  53. *
  54. * @return bool True if under the rate limit
  55. */
  56. public function checkRateLimit(): bool
  57. {
  58. try {
  59. $session = new Session();
  60. $session->start();
  61.  
  62. // Get email log from session
  63. $emailLog = $session->get($this->emailLogKey, []);
  64.  
  65. // Clean up old entries (older than 1 hour)
  66. $now = time();
  67. $emailLog = array_filter($emailLog, function ($timestamp) use ($now) {
  68. return ($now - $timestamp) < 3600; // Keep emails from the last hour
  69. });
  70.  
  71. // Count emails sent in the last hour
  72. $emailCount = count($emailLog);
  73.  
  74. // Check if we're over the limit
  75. if ($emailCount >= $this->maxEmailsPerHour) {
  76. return false;
  77. }
  78.  
  79. return true;
  80. } catch (Exception $e) {
  81. // Log the error
  82. $this->logger->error('Rate limit check failed', [
  83. 'error' => $e->getMessage()
  84. ]);
  85.  
  86. // If there's an error checking rate limit, be cautious and allow the email
  87. return true;
  88. }
  89. }
  90.  
  91. /**
  92. * Log an email sending attempt
  93. *
  94. * @param string|array $to Recipient(s)
  95. * @param string $subject Email subject
  96. * @return void
  97. */
  98. public function logEmailAttempt(string|array $to, string $subject): void
  99. {
  100. try {
  101. $session = new Session();
  102. $session->start();
  103.  
  104. // Get email log from session
  105. $emailLog = $session->get($this->emailLogKey, []);
  106.  
  107. // Add current timestamp
  108. $emailLog[] = time();
  109.  
  110. // Update session
  111. $session->set($this->emailLogKey, $emailLog);
  112.  
  113. // Log for security monitoring
  114. $recipient = is_array($to) ? implode(', ', array_keys($to)) : $to;
  115. $this->securityService->logSecurityEvent(
  116. 'email_sent',
  117. [
  118. 'to' => $recipient,
  119. 'subject' => $subject
  120. ],
  121. 'info'
  122. );
  123. } catch (Exception $e) {
  124. // Log the error
  125. $this->logger->error('Failed to log email attempt', [
  126. 'error' => $e->getMessage()
  127. ]);
  128. }
  129. }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement