-
Notifications
You must be signed in to change notification settings - Fork 102
/
class-session.php
255 lines (219 loc) · 5.04 KB
/
class-session.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/**
* AnsPress session handler for managing custom session for users.
*
* @link https://anspress.net/anspress
* @since 4.1.5
* @author Rahul Aryan <rah12@live.com>
* @package AnsPress
* @subpackage Session
*/
namespace AnsPress;
/**
* AnsPress session handler for managing custom session
* for logged in and non logged in users.
*
* @package AnsPress
* @since 4.1.5
* @since 4.2.0 Fixed: CS bugs.
*/
class Session {
/**
* Instance.
*
* @var Instance
*/
protected static $instance = null;
/**
* Cookie and session name.
*
* @var string
*/
private $name = 'anspress_session';
/**
* The cookie path.
*
* @var string
*/
private $cookie_path = COOKIEPATH;
/**
* The cookie domain.
*
* @var string|null
*/
private $cookie_domain = null;
/**
* When will cookie and session will expire.
*
* @var string
*/
private $expires = DAY_IN_SECONDS;
/**
* The session ID.
*
* @var string
*/
private $id;
/**
* Get current instance.
*
* @return AnsPress\Session
*/
public static function init() {
// Create an object.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance; // Return the object.
}
/**
* Initialize the class.
*/
public function __construct() {
if ( isset( $_COOKIE[ $this->name ] ) ) {
$this->id = stripslashes( ap_sanitize_unslash( $_COOKIE[ $this->name ] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
} else {
$this->id = $this->generate_id();
$this->set_cookie();
}
}
/**
* Set the session cookie.
*/
protected function set_cookie() {
if ( ! headers_sent() ) {
setcookie( $this->name, $this->id, time() + $this->expires, $this->cookie_path, $this->cookie_domain );
}
}
/**
* Delete session cookie.
*
* @return void
*/
protected function delete_cookie() {
if ( ! headers_sent() ) {
setcookie( $this->name, '', time() - 42000, $this->cookie_path, $this->cookie_domain );
}
}
/**
* Generate a cryptographically strong unique ID for the session token.
*
* @return string
*/
protected function generate_id() {
if ( ! class_exists( 'PasswordHash' ) ) {
require_once ABSPATH . 'wp-includes/class-phpass.php';
}
$hasher = new \PasswordHash( 8, false );
return md5( $hasher->get_random_bytes( 32 ) );
}
/**
* Get an offset by from AnsPress session.
*
* @param string $key Offset key.
* @return mixed
* @since 4.1.5
*/
public function get( $key ) {
$cache = get_transient( 'anspress_session_' . $this->id );
if ( false === $cache ) {
return;
}
if ( isset( $cache[ $key ] ) ) {
return $cache[ $key ];
}
}
/**
* Set offset in AnsPress session.
*
* @param string $key Offset key.
* @param mixed|null $val Offset value. Delete key value pair if this is `null`.
* @return void
* @since 4.1.5
*/
public function set( $key, $val = null ) {
$cache = get_transient( 'anspress_session_' . $this->id );
if ( false === $cache ) {
$cache = array( $key => $val );
}
$cache[ $key ] = $val;
set_transient( 'anspress_session_' . $this->id, $cache, $this->expires );
}
/**
* Set a question id in session's questions offset.
*
* @param integer $id Question id.
* @return void
* @since 4.1.5
*/
public function set_question( $id ) {
$questions = $this->get( 'questions' );
if ( ! $questions ) {
$questions = array();
}
$questions[] = $id;
$this->set( 'questions', $questions );
}
/**
* Set an answer id in session's answers offset.
*
* @param integer $id Answer id.
* @return void
* @since 4.1.5
* @since 4.1.7 Fixed: new answers are not being set in session.
*/
public function set_answer( $id ) {
$answers = $this->get( 'answers' );
if ( ! $answers ) {
$answers = array();
}
$answers[] = $id;
$this->set( 'answers', $answers );
}
/**
* Delete all session data or just a key=>value pair.
*
* @param null|string $key Name of key. On `null` all session data is deleted.
* @return void
* @since 4.1.5
*/
public function delete( $key = null ) {
// Delete all session data if no key set.
if ( null === $key ) {
delete_transient( 'anspress_session_' . $this->id );
return;
}
$this->set( $key );
}
/**
* Check if post is in user's session.
*
* @param mixed $_post WordPress post object or ID.
* @return boolean
* @since 4.1.5
*/
public function post_in_session( $_post ) {
$_post = ap_get_post( $_post );
$session_type = 'answer' === $_post->post_type ? 'answers' : 'questions';
$session_posts = anspress()->session->get( $session_type );
if ( ! empty( $session_posts ) && ! is_user_logged_in() && '0' === $_post->post_author && in_array( (int) $_post->ID, $session_posts, true ) ) {
return true;
}
return false;
}
/**
* Set a file in user's session.
*
* @param string $filename File name.
* @return void
* @since 4.1.8
*/
public function set_file( $filename ) {
$files = $this->get( 'files' );
if ( ! $files ) {
$files = array();
}
$files[] = $filename;
$this->set( 'files', $files );
}
}