| 1 |
<?php |
| 2 |
|
| 3 |
namespace WP_DEFENDER_VENDOR\Safe; |
| 4 |
|
| 5 |
use WP_DEFENDER_VENDOR\Safe\Exceptions\LibeventException; |
| 6 |
|
| 7 |
/** |
| 8 |
* event_add schedules the execution of the event |
| 9 |
* when the event specified in event_set occurs or in at least the time |
| 10 |
* specified by the timeout argument. If |
| 11 |
* timeout was not specified, not timeout is set. The |
| 12 |
* event must be already initalized by event_set |
| 13 |
* and event_base_set functions. If the |
| 14 |
* event already has a timeout set, it is replaced by |
| 15 |
* the new one. |
| 16 |
* |
| 17 |
* @param resource $event Valid event resource. |
| 18 |
* @param int $timeout Optional timeout (in microseconds). |
| 19 |
* @throws LibeventException |
| 20 |
* |
| 21 |
*/ |
| 22 |
function event_add($event, int $timeout = -1): void |
| 23 |
{ |
| 24 |
error_clear_last(); |
| 25 |
$result = \event_add($event, $timeout); |
| 26 |
if ($result === false) { |
| 27 |
throw LibeventException::createFromPhpError(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Abort the active event loop immediately. The behaviour is similar to |
| 34 |
* break statement. |
| 35 |
* |
| 36 |
* @param resource $event_base Valid event base resource. |
| 37 |
* @throws LibeventException |
| 38 |
* |
| 39 |
*/ |
| 40 |
function event_base_loopbreak($event_base): void |
| 41 |
{ |
| 42 |
error_clear_last(); |
| 43 |
$result = \event_base_loopbreak($event_base); |
| 44 |
if ($result === false) { |
| 45 |
throw LibeventException::createFromPhpError(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
/** |
| 51 |
* The next event loop iteration after the given timer expires will complete |
| 52 |
* normally, then exit without blocking for events again. |
| 53 |
* |
| 54 |
* @param resource $event_base Valid event base resource. |
| 55 |
* @param int $timeout Optional timeout parameter (in microseconds). |
| 56 |
* @throws LibeventException |
| 57 |
* |
| 58 |
*/ |
| 59 |
function event_base_loopexit($event_base, int $timeout = -1): void |
| 60 |
{ |
| 61 |
error_clear_last(); |
| 62 |
$result = \event_base_loopexit($event_base, $timeout); |
| 63 |
if ($result === false) { |
| 64 |
throw LibeventException::createFromPhpError(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
/** |
| 70 |
* Returns new event base, which can be used later in event_base_set, |
| 71 |
* event_base_loop and other functions. |
| 72 |
* |
| 73 |
* @return resource event_base_new returns valid event base resource on |
| 74 |
* success. |
| 75 |
* @throws LibeventException |
| 76 |
* |
| 77 |
*/ |
| 78 |
function event_base_new() |
| 79 |
{ |
| 80 |
error_clear_last(); |
| 81 |
$result = \event_base_new(); |
| 82 |
if ($result === false) { |
| 83 |
throw LibeventException::createFromPhpError(); |
| 84 |
} |
| 85 |
return $result; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
/** |
| 90 |
* Sets the number of different event priority levels. |
| 91 |
* |
| 92 |
* By default all events are scheduled with the same priority |
| 93 |
* (npriorities/2). |
| 94 |
* Using event_base_priority_init you can change the number |
| 95 |
* of event priority levels and then set a desired priority for each event. |
| 96 |
* |
| 97 |
* @param resource $event_base Valid event base resource. |
| 98 |
* @param int $npriorities The number of event priority levels. |
| 99 |
* @throws LibeventException |
| 100 |
* |
| 101 |
*/ |
| 102 |
function event_base_priority_init($event_base, int $npriorities): void |
| 103 |
{ |
| 104 |
error_clear_last(); |
| 105 |
$result = \event_base_priority_init($event_base, $npriorities); |
| 106 |
if ($result === false) { |
| 107 |
throw LibeventException::createFromPhpError(); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Some event mechanisms do not survive across fork. The |
| 114 |
* event_base needs to be reinitialized with this |
| 115 |
* function. |
| 116 |
* |
| 117 |
* @param resource $event_base Valid event base resource that needs to be re-initialized. |
| 118 |
* @throws LibeventException |
| 119 |
* |
| 120 |
*/ |
| 121 |
function event_base_reinit($event_base): void |
| 122 |
{ |
| 123 |
error_clear_last(); |
| 124 |
$result = \event_base_reinit($event_base); |
| 125 |
if ($result === false) { |
| 126 |
throw LibeventException::createFromPhpError(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Associates the event_base with the |
| 133 |
* event. |
| 134 |
* |
| 135 |
* @param resource $event Valid event resource. |
| 136 |
* @param resource $event_base Valid event base resource. |
| 137 |
* @throws LibeventException |
| 138 |
* |
| 139 |
*/ |
| 140 |
function event_base_set($event, $event_base): void |
| 141 |
{ |
| 142 |
error_clear_last(); |
| 143 |
$result = \event_base_set($event, $event_base); |
| 144 |
if ($result === false) { |
| 145 |
throw LibeventException::createFromPhpError(); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
/** |
| 151 |
* Assign the specified bevent to the |
| 152 |
* event_base. |
| 153 |
* |
| 154 |
* @param resource $bevent Valid buffered event resource. |
| 155 |
* @param resource $event_base Valid event base resource. |
| 156 |
* @throws LibeventException |
| 157 |
* |
| 158 |
*/ |
| 159 |
function event_buffer_base_set($bevent, $event_base): void |
| 160 |
{ |
| 161 |
error_clear_last(); |
| 162 |
$result = \event_buffer_base_set($bevent, $event_base); |
| 163 |
if ($result === false) { |
| 164 |
throw LibeventException::createFromPhpError(); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* Disables the specified buffered event. |
| 171 |
* |
| 172 |
* @param resource $bevent Valid buffered event resource. |
| 173 |
* @param int $events Any combination of EV_READ and |
| 174 |
* EV_WRITE. |
| 175 |
* @throws LibeventException |
| 176 |
* |
| 177 |
*/ |
| 178 |
function event_buffer_disable($bevent, int $events): void |
| 179 |
{ |
| 180 |
error_clear_last(); |
| 181 |
$result = \event_buffer_disable($bevent, $events); |
| 182 |
if ($result === false) { |
| 183 |
throw LibeventException::createFromPhpError(); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
/** |
| 189 |
* Enables the specified buffered event. |
| 190 |
* |
| 191 |
* @param resource $bevent Valid buffered event resource. |
| 192 |
* @param int $events Any combination of EV_READ and |
| 193 |
* EV_WRITE. |
| 194 |
* @throws LibeventException |
| 195 |
* |
| 196 |
*/ |
| 197 |
function event_buffer_enable($bevent, int $events): void |
| 198 |
{ |
| 199 |
error_clear_last(); |
| 200 |
$result = \event_buffer_enable($bevent, $events); |
| 201 |
if ($result === false) { |
| 202 |
throw LibeventException::createFromPhpError(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Libevent provides an abstraction layer on top of the regular event API. |
| 209 |
* Using buffered event you don't need to deal with the I/O manually, instead |
| 210 |
* it provides input and output buffers that get filled and drained |
| 211 |
* automatically. |
| 212 |
* |
| 213 |
* @param resource $stream Valid PHP stream resource. Must be castable to file descriptor. |
| 214 |
* @param mixed $readcb Callback to invoke where there is data to read, or NULL if |
| 215 |
* no callback is desired. |
| 216 |
* @param mixed $writecb Callback to invoke where the descriptor is ready for writing, |
| 217 |
* or NULL if no callback is desired. |
| 218 |
* @param mixed $errorcb Callback to invoke where there is an error on the descriptor, cannot be |
| 219 |
* NULL. |
| 220 |
* @param mixed $arg An argument that will be passed to each of the callbacks (optional). |
| 221 |
* @return resource event_buffer_new returns new buffered event resource |
| 222 |
* on success. |
| 223 |
* @throws LibeventException |
| 224 |
* |
| 225 |
*/ |
| 226 |
function event_buffer_new($stream, $readcb, $writecb, $errorcb, $arg = null) |
| 227 |
{ |
| 228 |
error_clear_last(); |
| 229 |
if ($arg !== null) { |
| 230 |
$result = \event_buffer_new($stream, $readcb, $writecb, $errorcb, $arg); |
| 231 |
} else { |
| 232 |
$result = \event_buffer_new($stream, $readcb, $writecb, $errorcb); |
| 233 |
} |
| 234 |
if ($result === false) { |
| 235 |
throw LibeventException::createFromPhpError(); |
| 236 |
} |
| 237 |
return $result; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/** |
| 242 |
* Assign a priority to the bevent. |
| 243 |
* |
| 244 |
* @param resource $bevent Valid buffered event resource. |
| 245 |
* @param int $priority Priority level. Cannot be less than zero and cannot exceed maximum |
| 246 |
* priority level of the event base (see event_base_priority_init). |
| 247 |
* @throws LibeventException |
| 248 |
* |
| 249 |
*/ |
| 250 |
function event_buffer_priority_set($bevent, int $priority): void |
| 251 |
{ |
| 252 |
error_clear_last(); |
| 253 |
$result = \event_buffer_priority_set($bevent, $priority); |
| 254 |
if ($result === false) { |
| 255 |
throw LibeventException::createFromPhpError(); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/** |
| 261 |
* Sets or changes existing callbacks for the buffered event. |
| 262 |
* |
| 263 |
* @param resource $event Valid buffered event resource. |
| 264 |
* @param mixed $readcb Callback to invoke where there is data to read, or NULL if |
| 265 |
* no callback is desired. |
| 266 |
* @param mixed $writecb Callback to invoke where the descriptor is ready for writing, |
| 267 |
* or NULL if no callback is desired. |
| 268 |
* @param mixed $errorcb Callback to invoke where there is an error on the descriptor, cannot be |
| 269 |
* NULL. |
| 270 |
* @param mixed $arg An argument that will be passed to each of the callbacks (optional). |
| 271 |
* @throws LibeventException |
| 272 |
* |
| 273 |
*/ |
| 274 |
function event_buffer_set_callback($event, $readcb, $writecb, $errorcb, $arg = null): void |
| 275 |
{ |
| 276 |
error_clear_last(); |
| 277 |
if ($arg !== null) { |
| 278 |
$result = \event_buffer_set_callback($event, $readcb, $writecb, $errorcb, $arg); |
| 279 |
} else { |
| 280 |
$result = \event_buffer_set_callback($event, $readcb, $writecb, $errorcb); |
| 281 |
} |
| 282 |
if ($result === false) { |
| 283 |
throw LibeventException::createFromPhpError(); |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
/** |
| 289 |
* Writes data to the specified buffered event. The data is appended to the |
| 290 |
* output buffer and written to the descriptor when it becomes available for |
| 291 |
* writing. |
| 292 |
* |
| 293 |
* @param resource $bevent Valid buffered event resource. |
| 294 |
* @param string $data The data to be written. |
| 295 |
* @param int $data_size Optional size parameter. event_buffer_write writes |
| 296 |
* all the data by default. |
| 297 |
* @throws LibeventException |
| 298 |
* |
| 299 |
*/ |
| 300 |
function event_buffer_write($bevent, string $data, int $data_size = -1): void |
| 301 |
{ |
| 302 |
error_clear_last(); |
| 303 |
$result = \event_buffer_write($bevent, $data, $data_size); |
| 304 |
if ($result === false) { |
| 305 |
throw LibeventException::createFromPhpError(); |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
/** |
| 311 |
* Cancels the event. |
| 312 |
* |
| 313 |
* @param resource $event Valid event resource. |
| 314 |
* @throws LibeventException |
| 315 |
* |
| 316 |
*/ |
| 317 |
function event_del($event): void |
| 318 |
{ |
| 319 |
error_clear_last(); |
| 320 |
$result = \event_del($event); |
| 321 |
if ($result === false) { |
| 322 |
throw LibeventException::createFromPhpError(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
/** |
| 328 |
* Creates and returns a new event resource. |
| 329 |
* |
| 330 |
* @return resource event_new returns a new event resource on success. |
| 331 |
* @throws LibeventException |
| 332 |
* |
| 333 |
*/ |
| 334 |
function event_new() |
| 335 |
{ |
| 336 |
error_clear_last(); |
| 337 |
$result = \event_new(); |
| 338 |
if ($result === false) { |
| 339 |
throw LibeventException::createFromPhpError(); |
| 340 |
} |
| 341 |
return $result; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
/** |
| 346 |
* Assign a priority to the event. |
| 347 |
* |
| 348 |
* @param resource $event Valid event resource. |
| 349 |
* @param int $priority Priority level. Cannot be less than zero and cannot exceed maximum |
| 350 |
* priority level of the event base (see |
| 351 |
* event_base_priority_init). |
| 352 |
* @throws LibeventException |
| 353 |
* |
| 354 |
*/ |
| 355 |
function event_priority_set($event, int $priority): void |
| 356 |
{ |
| 357 |
error_clear_last(); |
| 358 |
$result = \event_priority_set($event, $priority); |
| 359 |
if ($result === false) { |
| 360 |
throw LibeventException::createFromPhpError(); |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
/** |
| 366 |
* Prepares the event to be used in event_add. The event |
| 367 |
* is prepared to call the function specified by the callback |
| 368 |
* on the events specified in parameter events, which |
| 369 |
* is a set of the following flags: EV_TIMEOUT, |
| 370 |
* EV_SIGNAL, EV_READ, |
| 371 |
* EV_WRITE and EV_PERSIST. |
| 372 |
* |
| 373 |
* If EV_SIGNAL bit is set in parameter events, |
| 374 |
* the fd is interpreted as signal number. |
| 375 |
* |
| 376 |
* After initializing the event, use event_base_set to |
| 377 |
* associate the event with its event base. |
| 378 |
* |
| 379 |
* In case of matching event, these three arguments are passed to the |
| 380 |
* callback function: |
| 381 |
* |
| 382 |
* |
| 383 |
* fd |
| 384 |
* |
| 385 |
* |
| 386 |
* Signal number or resource indicating the stream. |
| 387 |
* |
| 388 |
* |
| 389 |
* |
| 390 |
* |
| 391 |
* events |
| 392 |
* |
| 393 |
* |
| 394 |
* A flag indicating the event. Consists of the following flags: |
| 395 |
* EV_TIMEOUT, EV_SIGNAL, |
| 396 |
* EV_READ, EV_WRITE |
| 397 |
* and EV_PERSIST. |
| 398 |
* |
| 399 |
* |
| 400 |
* |
| 401 |
* |
| 402 |
* arg |
| 403 |
* |
| 404 |
* |
| 405 |
* Optional parameter, previously passed to event_set |
| 406 |
* as arg. |
| 407 |
* |
| 408 |
* |
| 409 |
* |
| 410 |
* |
| 411 |
* |
| 412 |
* @param resource $event Valid event resource. |
| 413 |
* @param mixed $fd Valid PHP stream resource. The stream must be castable to file |
| 414 |
* descriptor, so you most likely won't be able to use any of filtered |
| 415 |
* streams. |
| 416 |
* @param int $events A set of flags indicating the desired event, can be |
| 417 |
* EV_READ and/or EV_WRITE. |
| 418 |
* The additional flag EV_PERSIST makes the event |
| 419 |
* to persist until event_del is called, otherwise |
| 420 |
* the callback is invoked only once. |
| 421 |
* @param mixed $callback Callback function to be called when the matching event occurs. |
| 422 |
* @param mixed $arg Optional callback parameter. |
| 423 |
* @throws LibeventException |
| 424 |
* |
| 425 |
*/ |
| 426 |
function event_set($event, $fd, int $events, $callback, $arg = null): void |
| 427 |
{ |
| 428 |
error_clear_last(); |
| 429 |
if ($arg !== null) { |
| 430 |
$result = \event_set($event, $fd, $events, $callback, $arg); |
| 431 |
} else { |
| 432 |
$result = \event_set($event, $fd, $events, $callback); |
| 433 |
} |
| 434 |
if ($result === false) { |
| 435 |
throw LibeventException::createFromPhpError(); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
/** |
| 441 |
* Prepares the timer event to be used in event_add. The |
| 442 |
* event is prepared to call the function specified by the |
| 443 |
* callback when the event timeout elapses. |
| 444 |
* |
| 445 |
* After initializing the event, use event_base_set to |
| 446 |
* associate the event with its event base. |
| 447 |
* |
| 448 |
* In case of matching event, these three arguments are passed to the |
| 449 |
* callback function: |
| 450 |
* |
| 451 |
* |
| 452 |
* fd |
| 453 |
* |
| 454 |
* |
| 455 |
* Signal number or resource indicating the stream. |
| 456 |
* |
| 457 |
* |
| 458 |
* |
| 459 |
* |
| 460 |
* events |
| 461 |
* |
| 462 |
* |
| 463 |
* A flag indicating the event. This will always be |
| 464 |
* EV_TIMEOUT for timer events. |
| 465 |
* |
| 466 |
* |
| 467 |
* |
| 468 |
* |
| 469 |
* arg |
| 470 |
* |
| 471 |
* |
| 472 |
* Optional parameter, previously passed to |
| 473 |
* event_timer_set as arg. |
| 474 |
* |
| 475 |
* |
| 476 |
* |
| 477 |
* |
| 478 |
* |
| 479 |
* @param resource $event Valid event resource. |
| 480 |
* @param callable $callback Callback function to be called when the matching event occurs. |
| 481 |
* @param mixed $arg Optional callback parameter. |
| 482 |
* @throws LibeventException |
| 483 |
* |
| 484 |
*/ |
| 485 |
function event_timer_set($event, callable $callback, $arg = null): void |
| 486 |
{ |
| 487 |
error_clear_last(); |
| 488 |
if ($arg !== null) { |
| 489 |
$result = \event_timer_set($event, $callback, $arg); |
| 490 |
} else { |
| 491 |
$result = \event_timer_set($event, $callback); |
| 492 |
} |
| 493 |
if ($result === false) { |
| 494 |
throw LibeventException::createFromPhpError(); |
| 495 |
} |
| 496 |
} |
| 497 |
|