| 1 |
<?php |
| 2 |
/** |
| 3 |
* Background tasks handling |
| 4 |
* |
| 5 |
* Handles all background tasks. |
| 6 |
* |
| 7 |
* @package Features |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace POSessions\Plugin\Feature; |
| 13 |
|
| 14 |
use POSessions\System\Cache; |
| 15 |
|
| 16 |
use POSessions\System\Option; |
| 17 |
use POSessions\System\Session; |
| 18 |
use POSessions\Plugin\Feature\Schema; |
| 19 |
|
| 20 |
/** |
| 21 |
* Define the zookeeper functionality. |
| 22 |
* |
| 23 |
* Handles all background tasks. |
| 24 |
* |
| 25 |
* @package Features |
| 26 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 27 |
* @since 1.0.0 |
| 28 |
*/ |
| 29 |
class ZooKeeper { |
| 30 |
|
| 31 |
/** |
| 32 |
* Initialize the meta class and set its properties. |
| 33 |
* |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
public static function init() { |
| 37 |
add_action( 'shutdown', [ self::class, 'execute_tasks' ], 10 ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Execute the background tasks. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
*/ |
| 45 |
public static function execute_tasks() { |
| 46 |
$semaphore = Cache::get_global( 'zookeeper/semaphore' ); |
| 47 |
if ( isset( $semaphore ) && $semaphore && (int) $semaphore + (int) Option::network_get( 'zk_semaphore' ) < time() ) { |
| 48 |
return; |
| 49 |
} |
| 50 |
$lastexec = Cache::get_global( 'zookeeper/lastexec' ); |
| 51 |
if ( isset( $lastexec ) && $lastexec && (int) $lastexec + (int) Option::network_get( 'zk_cycle' ) > time() ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
if ( isset( $semaphore ) && $semaphore && (int) $semaphore + (int) Option::network_get( 'zk_semaphore' ) >= time() ) { |
| 55 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( '[ZooKeeper] Destroying staled semaphore.' ); |
| 56 |
} |
| 57 |
Cache::set_global( 'zookeeper/semaphore', time() ); |
| 58 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( '[ZooKeeper] Starting background tasks execution.' ); |
| 59 |
self::terminate_sessions(); |
| 60 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( '[ZooKeeper] Ending background tasks execution.' ); |
| 61 |
Cache::delete_global( 'zookeeper/semaphore' ); |
| 62 |
Cache::set_global( 'zookeeper/lastexec', time() ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Terminates staled sessions. |
| 67 |
* |
| 68 |
* @since 1.0.0 |
| 69 |
*/ |
| 70 |
private static function terminate_sessions() { |
| 71 |
global $wpdb; |
| 72 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( '[ZooKeeper] Starting "terminate_sessions" execution.' ); |
| 73 |
$index = Cache::get_global( 'zookeeper/userindex' ); |
| 74 |
if ( ! $index ) { |
| 75 |
$index = 0; |
| 76 |
} |
| 77 |
$limit = (int) Option::network_get( 'zk_tsize' ); |
| 78 |
$cpt = 0; |
| 79 |
$sql = 'SELECT user_id, meta_value FROM ' . $wpdb->usermeta . " WHERE meta_key='session_tokens' LIMIT " . $limit . " OFFSET " . $index . ";"; |
| 80 |
// phpcs:ignore |
| 81 |
$query = $wpdb->get_results( $sql, ARRAY_A ); |
| 82 |
if ( is_array( $query ) && 0 < count( $query ) ) { |
| 83 |
if ( $limit > count( $query ) ) { |
| 84 |
$index = 0; |
| 85 |
} else { |
| 86 |
$index += $limit; |
| 87 |
} |
| 88 |
foreach ( $query as $row ) { |
| 89 |
$sessions = $row['meta_value']; |
| 90 |
if ( ! is_array( $sessions ) && is_string( $sessions ) ) { |
| 91 |
$sessions = maybe_unserialize( $sessions ); |
| 92 |
} |
| 93 |
if ( is_array( $sessions ) ) { |
| 94 |
$cpt += Session::auto_terminate_session( $sessions, (int) $row['user_id'] ); |
| 95 |
} |
| 96 |
} |
| 97 |
switch ( $cpt ) { |
| 98 |
case 0: |
| 99 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( 'No session to auto-terminate.' ); |
| 100 |
break; |
| 101 |
case 1: |
| 102 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->notice( sprintf( '%d session auto-terminated.', $cpt ) ); |
| 103 |
break; |
| 104 |
default: |
| 105 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->notice( sprintf( '%d sessions auto-terminated.', $cpt ) ); |
| 106 |
break; |
| 107 |
} |
| 108 |
} else { |
| 109 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( 'No session to auto-terminate.' ); |
| 110 |
$index = 0; |
| 111 |
} |
| 112 |
Cache::set_global( 'zookeeper/userindex', $index, 'infinite' ); |
| 113 |
\DecaLog\Engine::eventsLogger( POSE_SLUG )->debug( '[ZooKeeper] Ending "terminate_sessions" execution.' ); |
| 114 |
} |
| 115 |
|
| 116 |
} |