class-recursive-arrayaccess.php
9 years ago
class-wp-session-utils.php
9 years ago
class-wp-session.php
9 years ago
wp-session.php
9 years ago
class-wp-session-utils.php
139 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Utility class for sesion utilities |
| 5 | * |
| 6 | * THIS CLASS SHOULD NEVER BE INSTANTIATED |
| 7 | */ |
| 8 | class WP_Session_Utils { |
| 9 | /** |
| 10 | * Count the total sessions in the database. |
| 11 | * |
| 12 | * @global wpdb $wpdb |
| 13 | * |
| 14 | * @return int |
| 15 | */ |
| 16 | public static function count_sessions() { |
| 17 | global $wpdb; |
| 18 | |
| 19 | $query = "SELECT COUNT(*) FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'"; |
| 20 | |
| 21 | /** |
| 22 | * Filter the query in case tables are non-standard. |
| 23 | * |
| 24 | * @param string $query Database count query |
| 25 | */ |
| 26 | $query = apply_filters( 'wp_session_count_query', $query ); |
| 27 | |
| 28 | $sessions = $wpdb->get_var( $query ); |
| 29 | |
| 30 | return absint( $sessions ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Create a new, random session in the database. |
| 35 | * |
| 36 | * @param null|string $date |
| 37 | */ |
| 38 | public static function create_dummy_session( $date = null ) { |
| 39 | // Generate our date |
| 40 | if ( null !== $date ) { |
| 41 | $time = strtotime( $date ); |
| 42 | |
| 43 | if ( false === $time ) { |
| 44 | $date = null; |
| 45 | } else { |
| 46 | $expires = date( 'U', strtotime( $date ) ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // If null was passed, or if the string parsing failed, fall back on a default |
| 51 | if ( null === $date ) { |
| 52 | /** |
| 53 | * Filter the expiration of the session in the database |
| 54 | * |
| 55 | * @param int |
| 56 | */ |
| 57 | $expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 ); |
| 58 | } |
| 59 | |
| 60 | $session_id = self::generate_id(); |
| 61 | |
| 62 | // Store the session |
| 63 | add_option( "_wp_session_{$session_id}", array(), '', 'no' ); |
| 64 | add_option( "_wp_session_expires_{$session_id}", $expires, '', 'no' ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Delete old sessions from the database. |
| 69 | * |
| 70 | * @param int $limit Maximum number of sessions to delete. |
| 71 | * |
| 72 | * @global wpdb $wpdb |
| 73 | * |
| 74 | * @return int Sessions deleted. |
| 75 | */ |
| 76 | public static function delete_old_sessions( $limit = 1000 ) { |
| 77 | global $wpdb; |
| 78 | |
| 79 | $limit = absint( $limit ); |
| 80 | $keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}" ); |
| 81 | |
| 82 | $now = time(); |
| 83 | $expired = array(); |
| 84 | $count = 0; |
| 85 | |
| 86 | foreach( $keys as $expiration ) { |
| 87 | $key = $expiration->option_name; |
| 88 | $expires = $expiration->option_value; |
| 89 | |
| 90 | if ( $now > $expires ) { |
| 91 | $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr( $key, 20 ) ); |
| 92 | |
| 93 | $expired[] = $key; |
| 94 | $expired[] = "_wp_session_{$session_id}"; |
| 95 | |
| 96 | $count += 1; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // Delete expired sessions |
| 101 | if ( ! empty( $expired ) ) { |
| 102 | $placeholders = array_fill( 0, count( $expired ), '%s' ); |
| 103 | $format = implode( ', ', $placeholders ); |
| 104 | $query = "DELETE FROM $wpdb->options WHERE option_name IN ($format)"; |
| 105 | |
| 106 | $prepared = $wpdb->prepare( $query, $expired ); |
| 107 | $wpdb->query( $prepared ); |
| 108 | } |
| 109 | |
| 110 | return $count; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Remove all sessions from the database, regardless of expiration. |
| 115 | * |
| 116 | * @global wpdb $wpdb |
| 117 | * |
| 118 | * @return int Sessions deleted |
| 119 | */ |
| 120 | public static function delete_all_sessions() { |
| 121 | global $wpdb; |
| 122 | |
| 123 | $count = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" ); |
| 124 | |
| 125 | return (int) ( $count / 2 ); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Generate a new, random session ID. |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | public static function generate_id() { |
| 134 | require_once( ABSPATH . 'wp-includes/class-phpass.php' ); |
| 135 | $hash = new PasswordHash( 8, false ); |
| 136 | |
| 137 | return md5( $hash->get_random_bytes( 32 ) ); |
| 138 | } |
| 139 | } |