diff --git a/.env b/.env index 56f86a9..96624ef 100644 --- a/.env +++ b/.env @@ -19,7 +19,7 @@ APP_ENV=dev APP_SECRET=EDIT_ME ###< symfony/framework-bundle ### -APP_VERSION=1.0.3 +APP_VERSION=1.1.0 APP_NAME=KevaChat @@ -61,6 +61,9 @@ APP_KEVACOIN_ROOM_NAMESPACE_DEFAULT=EDIT_ME # Allow users to read external node rooms APP_KEVACOIN_ROOM_NAMESPACES_EXTERNAL= +# Online session expiration timeout +APP_SESSION_ONLINE_TIMEOUT=900 + # Allow remotes to create new rooms (namespaces) APP_ADD_ROOM_REMOTE_IP_REGEX=/.*/ diff --git a/config/services.yaml b/config/services.yaml index bc2d523..6b3cd97 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -23,6 +23,7 @@ parameters: app.kevacoin.explorer.url: '%env(APP_KEVACOIN_EXPLORER_URL)%' app.kevacoin.mine.pool.url: '%env(APP_KEVACOIN_MINE_POOL_URL)%' app.kevacoin.mine.solo.url: '%env(APP_KEVACOIN_MINE_SOLO_URL)%' + app.session.online.timeout: '%env(APP_SESSION_ONLINE_TIMEOUT)%' app.add.room.remote.ip.regex: '%env(APP_ADD_ROOM_REMOTE_IP_REGEX)%' app.add.post.remote.ip.regex: '%env(APP_ADD_POST_REMOTE_IP_REGEX)%' app.add.post.remote.ip.delay: '%env(APP_ADD_POST_REMOTE_IP_DELAY)%' diff --git a/src/Controller/ModuleController.php b/src/Controller/ModuleController.php index 9fc5b05..7bc5840 100644 --- a/src/Controller/ModuleController.php +++ b/src/Controller/ModuleController.php @@ -10,8 +10,11 @@ use Symfony\Component\HttpFoundation\Request; class ModuleController extends AbstractController { - public function info(): Response + public function info( + Request $request + ): Response { + // Connect kevacoin $client = new \Kevachat\Kevacoin\Client( $this->getParameter('app.kevacoin.protocol'), $this->getParameter('app.kevacoin.host'), @@ -20,9 +23,49 @@ class ModuleController extends AbstractController $this->getParameter('app.kevacoin.password') ); + // Connect memcached + $memcached = new \Memcached(); + $memcached->addServer( + $this->getParameter('app.memcached.host'), + $this->getParameter('app.memcached.port') + ); + + // Get sessions registry + $online = md5( + sprintf( + '%s.ModuleController::info:sessions', + __DIR__ + ) + ); + + // Drop offline sessions + $sessions = []; + + foreach ((array) $memcached->get($online) as $ip => $time) + { + if (time() - $time < $this->getParameter('app.session.online.timeout')) + { + $sessions[$ip] = $time; + } + } + + // Update current session time + $sessions[$request->getClientIp()] = time(); + + // Update session registry + $memcached->set( + $online, + $sessions, + $this->getParameter('app.session.online.timeout') + ); + + // Render the template return $this->render( 'default/module/info.html.twig', [ + 'online' => count( + $sessions + ), 'wallet' => [ 'balance' => (float) $client->getBalance(), diff --git a/templates/default/layout.html.twig b/templates/default/layout.html.twig index 2600933..c7d20e1 100644 --- a/templates/default/layout.html.twig +++ b/templates/default/layout.html.twig @@ -20,7 +20,10 @@ {{ render( controller( - 'App\\Controller\\ModuleController::info' + 'App\\Controller\\ModuleController::info', + { + request : request + } ) ) }} diff --git a/templates/default/module/info.html.twig b/templates/default/module/info.html.twig index 142e7e4..d5b640c 100644 --- a/templates/default/module/info.html.twig +++ b/templates/default/module/info.html.twig @@ -1,4 +1,6 @@
+ {{ 'online:' | trans }} {{ online }} + / {{ 'block:' | trans }} {{ wallet.block }} / {{ 'balance:' | trans }} {{ wallet.balance }}