PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/nacridan/lib/swift/Swift/Plugin/AntiFlood.php

https://gitlab.com/nacridan/Nacridan
PHP | 132 lines | 37 code | 13 blank | 82 comment | 3 complexity | e79db4b86ba01200e3dddc041cdfd52b MD5 | raw file
  1. <?php
  2. /**
  3. * Anti-Flood plugin for Swift Mailer, a PHP Mailer class.
  4. *
  5. * @package Swift
  6. * @version >= 2.0.0
  7. * @author Chris Corbyn
  8. * @date 30th July 2006
  9. * @license http://www.gnu.org/licenses/lgpl.txt Lesser GNU Public License
  10. *
  11. * @copyright Copyright &copy; 2006 Chris Corbyn - All Rights Reserved.
  12. * @filesource
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2.1 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, write to
  26. *
  27. * The Free Software Foundation, Inc.,
  28. * 51 Franklin Street,
  29. * Fifth Floor,
  30. * Boston,
  31. * MA 02110-1301 USA
  32. *
  33. * "Chris Corbyn" <chris@w3style.co.uk>
  34. *
  35. */
  36. class Swift_Plugin_AntiFlood implements Swift_IPlugin
  37. {
  38. /**
  39. * Name of the plugin (identifier)
  40. *
  41. * @var string plugin id
  42. */
  43. public $pluginName = 'Anti_Flood';
  44. /**
  45. * The maximum number of messages to send
  46. * over a single connection
  47. *
  48. * @var int max messages
  49. */
  50. public $maxMessages;
  51. /**
  52. * The time to wait for before reconnecting
  53. *
  54. * @var int sleep seconds
  55. */
  56. public $sleep;
  57. /**
  58. * Current messages sent since last reconnect
  59. * or plugin loading.
  60. *
  61. * @var int current messages
  62. */
  63. protected $currMessages = 0;
  64. /**
  65. * Contains a reference to the main swift object.
  66. *
  67. * @var object swiftInstance
  68. */
  69. protected $swiftInstance;
  70. /**
  71. * Constructor.
  72. *
  73. * @param
  74. * int max messages, optional
  75. * @return void
  76. */
  77. public function __construct($max = 10, $sleep = 0)
  78. {
  79. $this->maxMessages = (int) $max;
  80. $this->sleep = (int) $sleep;
  81. }
  82. /**
  83. * Load in Swift
  84. *
  85. * @param
  86. * object SwiftInstance
  87. */
  88. public function loadBaseObject(&$object)
  89. {
  90. $this->swiftInstance = & $object;
  91. }
  92. /**
  93. * Event handler for onSend.
  94. */
  95. public function onSend()
  96. {
  97. $this->currMessages ++;
  98. if ($this->currMessages >= $this->maxMessages) {
  99. $this->reconnect();
  100. $this->currMessages = 0;
  101. }
  102. }
  103. /**
  104. * Reconnect to the server
  105. */
  106. protected function reconnect()
  107. {
  108. $this->swiftInstance->close();
  109. // Wait for N seconds if needed to give the server a rest
  110. if ($this->sleep)
  111. sleep($this->sleep);
  112. $this->swiftInstance->connect();
  113. // Re-authenticate if needs be
  114. if (! empty($this->swiftInstance->username)) {
  115. $this->swiftInstance->authenticate($this->swiftInstance->username, $this->swiftInstance->password);
  116. }
  117. }
  118. }
  119. ?>