Skip to content

Instantly share code, notes, and snippets.

@tradesouthwest
Created August 21, 2020 17:41
Show Gist options
  • Save tradesouthwest/f26085ad5326c1aef6dd8dedafca9fd4 to your computer and use it in GitHub Desktop.
Save tradesouthwest/f26085ad5326c1aef6dd8dedafca9fd4 to your computer and use it in GitHub Desktop.
Get all raw session meta from all wordpress users.
<?php
/**
* Get all raw session meta from all users
*
* @return array
*/
function readlimit_get_all_sessions_raw() {
global $wpdb;
$results = array();
$sessions = $wpdb->get_results( "SELECT meta_value FROM $wpdb->usermeta
WHERE meta_key = 'session_tokens' LIMIT 0, 9999" );
$sessions = wp_list_pluck( $sessions, 'meta_value' );
$sessions = array_map( 'maybe_unserialize', $sessions );
foreach ( $sessions as $session ) {
$results = array_merge( $results, $session );
}
return (array) $results;
}
/**
* Debug routine for exposing user sessions in admin
*
*/
function readlimit_get_users_with_sessions(){
$args = array(
'number' => 9999,
'blog_id' => is_network_admin() ? 0 : get_current_blog_id(),
'meta_query' => array(
array(
'key' => 'session_tokens',
'compare' => 'EXISTS',
),
),
);
$users = new WP_User_Query( $args );
return $users;
}
/**
* Get all sessions from all users
*
* @return array
*/
function readlimit_get_all_sessions() {
$results = array();
$users = readlimit_get_users_with_sessions()->get_results();
$sessions = readlimit_get_all_sessions_raw();
//$results = array();
$test = $_COOKIE['readlimit-readlimits'];
?>
<table class="widefat fixed" cellspacing="0">
<thead><tr>
<th>user_id</th>
<th>_session->login</th>
<th>_session->expiration</th>
<th>token_hash</th></tr></thead><tbody>
<?php
echo '<tr><td colspan="4">rls: ' . $test . '</td></tr>';
if ( ! empty( $users ) ) {
foreach ( $users as $user ) {
$user_sessions = get_user_meta( $user->ID, 'session_tokens', true );
foreach ( $sessions as $session ) {
foreach ( $user_sessions as $token_hash => $user_session ) {
// Loose comparison needed
if ( $user_session != $session ) {
continue;
}
echo '<tr><td>' . $user->ID . '</td>
<td>' . $user_session['login'] . ' created '
. human_time_diff( $user_session['login'] ) . ' ago</td>
<td>' . $user_session['expiration'] . ' exprs in '
. human_time_diff( $user_session['expiration'] ) . '</td>
<td>' . $token_hash . '</td></tr>';
}
}
}
} else {
echo '<tr><td colspan="4">No sessions found.</td></tr>';
} ?>
</tbody></table>
<?php
}
//add_action('readlimit_session_handler', 'readlimit_session_handler_onlogin' );
// debug shortcode
function readlimit_show_cookie_form() {
//$readlimit_login_time = get_user_meta( $user->ID, 'last_login', true );
$readlimit_readlimit_val = ''; //readlimit_get_cookie();
ob_start();
readlimit_get_all_sessions();
return ob_get_clean();
}
// Shortcode to display table layout of sessions
function readlimit_register_shortcodes() {
add_shortcode( 'rrcookie_form', 'readlimit_show_cookie_form' );
}
add_action( 'init', 'readlimit_register_shortcodes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment