CaptchaQueue is a rough implementation for pre-generating captcha using a timer and SYS V message queue.
1.1 Message Queue
Native SYS V message queue is fast, but its functionalities are limited. Consider using message queue implementations which are based on stomp instead(I will work on it in the far future).
There are several libraries in PHP which implements async stomp client, such as reactphp/stomp, and amphp/stomp. Luckily, both are available in Workerman(not yet in Acast, though). See Workerman-Amp for Workerman Amp support.
- This library don't actually require Workerman or Acast to run. With a few modifications, you can run it in any async environment.
1.2 When to use this library
As it is mentioned before, this library is used for pre-generation of captcha. That means the captcha is generated and stored on the server before a client request for it. Thus, this library may be helpful if the generation process is rather slow. However, limitations should be added to prevent malicious users from exhausting the captcha queue.
static function Captcha::register($name, CaptchaInterface $captcha) void
$name: The name of captcha generator(used for disable and enable).
$captcha: Instance of class which extends
Register a captcha generator. It will be automatically marked as enabled.
static function Captcha::init() void
Initialize all registered captcha generators and start generating captcha.
static function Captcha::enable() void
static function Captcha::disable() void
Enable or disable a specified captcha generator, but will not destroy it.
static function Captcha::destroy() void
Stop generating captcha, and destroy all captcha generators.
static function Captcha::getQueue() resource
Get resource of message queue. The queue can be shared among all captcha generators.
2.2 Captcha Generator
The timer function does not process anything returned by the captcha generator. The following things should be done within the
generate() function of the captcha generator.
- Writes an image to the disk.
- Send a message to the message queue. The message should contain image ID and captcha result.
CAPTCHA_TIMER_INTERVAL : Interval of the timer which calls the
generate() function of captcha generator.
CAPTCHA_CACHE_MAX : When the messages in the queue reach this value, the
generate() function will not be called.