PluginProbe ʕ •ᴥ•ʔ
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress / trunk
Paid Membership Plugin, Ecommerce, User Registration Form, Login Form, User Profile & Restrict Content – ProfilePress vtrunk
4.16.18 4.16.17 4.16.16 trunk 1.0 1.0.1 1.0.2 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.5a 1.1.6 1.1.7 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4 1.4.1 1.4.2 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.10 1.8.2 1.8.3 1.8.4 1.8.5 1.8.6 1.8.7 1.8.8 1.8.9 1.9 1.9.1 1.9.10 1.9.11 1.9.12 1.9.13 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.1.9 2.2.10 2.2.11 2.2.12 2.2.13 2.2.14 2.2.15 2.2.16 2.2.2 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 3.0 3.1 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10.0 4.10.1 4.10.2 4.10.3 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.13.3 4.13.4 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.15.0 4.15.1 4.15.10 4.15.11 4.15.12 4.15.13 4.15.14 4.15.15 4.15.16 4.15.17 4.15.18 4.15.19 4.15.2 4.15.20 4.15.20.1 4.15.21 4.15.22 4.15.23 4.15.24 4.15.25 4.15.3 4.15.4 4.15.5 4.15.6 4.15.7 4.15.8 4.15.9 4.16.0 4.16.1 4.16.10 4.16.11 4.16.12 4.16.13 4.16.14 4.16.15 4.16.2 4.16.3 4.16.4 4.16.5 4.16.6 4.16.7 4.16.8 4.16.9 4.2.0 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.7.0 4.8.0 4.9.0
wp-user-avatar / src / lib / wp_session / class-wp-session-utils.php
wp-user-avatar / src / lib / wp_session Last commit date
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 }