class-recursive-arrayaccess.php
3 years ago
class-wp-session-utils.php
3 years ago
class-wp-session.php
3 years ago
wp-session.php
3 years ago
class-wp-session-utils.php
298 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Utility class for session utilities |
| 5 | * |
| 6 | * THIS CLASS SHOULD NEVER BE INSTANTIATED |
| 7 | */ |
| 8 | class WP_PPress_Session_Utils |
| 9 | { |
| 10 | /** |
| 11 | * Count the total sessions in the database. |
| 12 | * |
| 13 | * @return int |
| 14 | * @global wpdb $wpdb |
| 15 | * |
| 16 | */ |
| 17 | public static function count_sessions() |
| 18 | { |
| 19 | global $wpdb; |
| 20 | |
| 21 | $query = "SELECT COUNT(*) FROM {$wpdb->prefix}ppress_sessions"; |
| 22 | |
| 23 | /** |
| 24 | * Filter the query in case tables are non-standard. |
| 25 | * |
| 26 | * @param string $query Database count query |
| 27 | */ |
| 28 | $query = apply_filters('wp_ppress_session_count_query', $query); |
| 29 | |
| 30 | $sessions = $wpdb->get_var($query); |
| 31 | |
| 32 | return absint($sessions); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Create a new, random session in the database. |
| 37 | * |
| 38 | * @param null|string $date |
| 39 | */ |
| 40 | public static function create_dummy_session($date = null) |
| 41 | { |
| 42 | // Generate our date |
| 43 | if (null !== $date) { |
| 44 | $time = strtotime($date); |
| 45 | |
| 46 | if (false === $time) { |
| 47 | $date = null; |
| 48 | } else { |
| 49 | $expires = date('U', strtotime($date)); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // If null was passed, or if the string parsing failed, fall back on a default |
| 54 | if (null === $date) { |
| 55 | /** |
| 56 | * Filter the expiration of the session in the database |
| 57 | * |
| 58 | * @param int |
| 59 | */ |
| 60 | $expires = time() + (int)apply_filters('wp_ppress_session_expiration', 30 * 60); |
| 61 | } |
| 62 | |
| 63 | $session_id = self::generate_id(); |
| 64 | |
| 65 | // Store the session |
| 66 | self::add_session(array( |
| 67 | 'session_key' => $session_id, |
| 68 | 'session_value' => array(), |
| 69 | 'session_expiry' => $expires |
| 70 | )); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Delete old sessions from the database. |
| 75 | * |
| 76 | * @param int $limit Maximum number of sessions to delete. |
| 77 | * |
| 78 | * @return int Sessions deleted. |
| 79 | * @global wpdb $wpdb |
| 80 | * |
| 81 | */ |
| 82 | public static function delete_old_sessions($limit = 1000) |
| 83 | { |
| 84 | global $wpdb; |
| 85 | |
| 86 | $limit = absint($limit); |
| 87 | $now = time(); |
| 88 | |
| 89 | $count = $wpdb->query( |
| 90 | $wpdb->prepare( |
| 91 | "DELETE FROM {$wpdb->prefix}ppress_sessions WHERE session_expiry < %s LIMIT %d", |
| 92 | $now, |
| 93 | $limit |
| 94 | ) |
| 95 | ); |
| 96 | |
| 97 | return $count; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Delete old sessions from the options table. |
| 102 | * |
| 103 | * @param int $limit Maximum number of sessions to delete. |
| 104 | * |
| 105 | * @return int Sessions deleted. |
| 106 | * @global wpdb $wpdb |
| 107 | * |
| 108 | */ |
| 109 | protected static function delete_old_sessions_from_options($limit = 1000) |
| 110 | { |
| 111 | global $wpdb; |
| 112 | |
| 113 | $limit = absint($limit); |
| 114 | |
| 115 | $keys = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_ppress_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}"); |
| 116 | |
| 117 | $now = time(); |
| 118 | $expired = array(); |
| 119 | $count = 0; |
| 120 | |
| 121 | foreach ($keys as $expiration) { |
| 122 | $key = $expiration->option_name; |
| 123 | $expires = $expiration->option_value; |
| 124 | |
| 125 | if ($now > $expires) { |
| 126 | $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr($key, 20)); |
| 127 | |
| 128 | $expired[] = $key; |
| 129 | $expired[] = "_wp_ppress_session_{$session_id}"; |
| 130 | $count += 1; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Delete expired sessions |
| 135 | if ( ! empty($expired)) { |
| 136 | $placeholders = array_fill(0, count($expired), '%s'); |
| 137 | $format = implode(', ', $placeholders); |
| 138 | $query = "DELETE FROM $wpdb->options WHERE option_name IN ($format)"; |
| 139 | |
| 140 | $prepared = $wpdb->prepare($query, $expired); |
| 141 | $wpdb->query($prepared); |
| 142 | } |
| 143 | |
| 144 | return $count; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Remove all sessions from the database, regardless of expiration. |
| 149 | * |
| 150 | * @return int Sessions deleted |
| 151 | * @global wpdb $wpdb |
| 152 | * |
| 153 | */ |
| 154 | public static function delete_all_sessions() |
| 155 | { |
| 156 | global $wpdb; |
| 157 | |
| 158 | return $wpdb->query("TRUNCATE TABLE {$wpdb->prefix}ppress_sessions"); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Remove all sessions from the options table, regardless of expiration. |
| 163 | * |
| 164 | * @return int Sessions deleted |
| 165 | * @global wpdb $wpdb |
| 166 | * |
| 167 | */ |
| 168 | public static function delete_all_sessions_from_options() |
| 169 | { |
| 170 | global $wpdb; |
| 171 | |
| 172 | $count = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_ppress_session_%'"); |
| 173 | |
| 174 | return (int)($count / 2); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Generate a new, random session ID. |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public static function generate_id() |
| 183 | { |
| 184 | require_once(ABSPATH . 'wp-includes/class-phpass.php'); |
| 185 | $hash = new PasswordHash(8, false); |
| 186 | |
| 187 | return md5($hash->get_random_bytes(32)); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get session from database. |
| 192 | * |
| 193 | * @param string $session_id The session ID to retrieve |
| 194 | * @param array $default The default value to return if the option does not exist. |
| 195 | * |
| 196 | * @return array Session data |
| 197 | */ |
| 198 | public static function get_session($session_id, $default = array()) |
| 199 | { |
| 200 | global $wpdb; |
| 201 | |
| 202 | $session = $wpdb->get_row( |
| 203 | $wpdb->prepare( |
| 204 | "SELECT * FROM {$wpdb->prefix}ppress_sessions WHERE session_key = %s", |
| 205 | esc_sql($session_id) |
| 206 | ), |
| 207 | ARRAY_A |
| 208 | ); |
| 209 | |
| 210 | if ($session === null) { |
| 211 | return $default; |
| 212 | } |
| 213 | |
| 214 | return unserialize($session['session_value']); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Test whether or not a session exists |
| 219 | * |
| 220 | * @param string $session_id The session ID to retrieve |
| 221 | * |
| 222 | * @return bool |
| 223 | */ |
| 224 | public static function session_exists($session_id) |
| 225 | { |
| 226 | global $wpdb; |
| 227 | |
| 228 | $exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$wpdb->prefix}ppress_sessions WHERE session_key = %s", $session_id)); |
| 229 | |
| 230 | return $exists > 0; |
| 231 | } |
| 232 | |
| 233 | |
| 234 | /** |
| 235 | * Add session in database. |
| 236 | * |
| 237 | * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
| 238 | * This means that if you are using GET or POST data you may need to use stripslashes() to avoid slashes ending up in the database. |
| 239 | * |
| 240 | * @return bool|int false if the row could not be inserted or the number of affected rows (which will always be 1). |
| 241 | */ |
| 242 | public static function add_session($data = array()) |
| 243 | { |
| 244 | global $wpdb; |
| 245 | |
| 246 | if (empty($data)) { |
| 247 | return false; |
| 248 | } |
| 249 | |
| 250 | $result = $wpdb->insert("{$wpdb->prefix}ppress_sessions", $data); |
| 251 | |
| 252 | return $result; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Delete session in database. |
| 257 | * |
| 258 | * @param int $session_id The session ID to update |
| 259 | * |
| 260 | * @return bool |
| 261 | */ |
| 262 | public static function delete_session($session_id = '') |
| 263 | { |
| 264 | global $wpdb; |
| 265 | |
| 266 | if ($session_id == '') { |
| 267 | return false; |
| 268 | } |
| 269 | |
| 270 | $wpdb->delete("{$wpdb->prefix}ppress_sessions", array('session_key' => $session_id)); |
| 271 | |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Update session in database. |
| 277 | * |
| 278 | * @param int $session_id The session ID to update |
| 279 | * @param array $data Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). |
| 280 | * This means that if you are using GET or POST data you may need to use stripslashes() to avoid slashes ending up in the database. |
| 281 | * |
| 282 | * @return bool|int the number of rows updated, or false if there is an error. |
| 283 | * Keep in mind that if the $data matches what is already in the database, no rows will be updated, so 0 will be returned. |
| 284 | * Because of this, you should probably check the return with false === $result |
| 285 | */ |
| 286 | public static function update_session($session_id = '', $data = array()) |
| 287 | { |
| 288 | global $wpdb; |
| 289 | |
| 290 | if ($session_id == '' || empty($data)) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | $result = $wpdb->update("{$wpdb->prefix}ppress_sessions", $data, array('session_key' => $session_id)); |
| 295 | |
| 296 | return $result; |
| 297 | } |
| 298 | } |