-
Notifications
You must be signed in to change notification settings - Fork 0
/
HANNANStd_Session.php
235 lines (235 loc) · 7.55 KB
/
HANNANStd_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
<?php
if( !defined( 'HANNAN_SESSION_COOKIE' ) )
define( 'HANNAN_SESSION_COOKIE', '_HANNANStd_session' );
if ( !class_exists( 'Recursive_ArrayAccess' ) ) {
class Recursive_ArrayAccess implements ArrayAccess {
protected $container = array();
protected $dirty = false;
protected function __construct( $data = array() ) {
foreach ( $data as $key => $value ) {
$this[ $key ] = $value;
}
}
public function __clone() {
foreach ( $this->container as $key => $value ) {
if ( $value instanceof self ) {
$this[ $key ] = clone $value;
}
}
}
public function toArray() {
$data = $this->container;
foreach ( $data as $key => $value ) {
if ( $value instanceof self ) {
$data[ $key ] = $value->toArray();
}
}
return $data;
}
public function offsetExists( $offset ) {
return isset( $this->container[ $offset ]) ;
}
public function offsetGet( $offset ) {
return isset( $this->container[ $offset ] ) ? $this->container[ $offset ] : null;
}
public function offsetSet( $offset, $data ) {
if ( is_array( $data ) ) {
$data = new self( $data );
}
if ( $offset === null ) {
$this->container[] = $data;
}
else {
$this->container[ $offset ] = $data;
}
$this->dirty = true;
}
public function offsetUnset( $offset ) {
unset( $this->container[ $offset ] );
$this->dirty = true;
}
}
}
if ( !class_exists( 'HANNAN_Session' ) ) {
final class HANNAN_Session extends Recursive_ArrayAccess implements Iterator, Countable {
protected $session_id;
protected $expires;
protected $exp_variant;
private static $instance = false;
public static function get_instance() {
if ( !self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
protected function __construct() {
if ( isset( $_COOKIE[HANNAN_SESSION_COOKIE] ) ) {
$cookie = stripslashes( $_COOKIE[HANNAN_SESSION_COOKIE] );
$cookie_crumbs = explode( '||', $cookie );
$this->session_id = $cookie_crumbs[0];
$this->expires = $cookie_crumbs[1];
$this->exp_variant = $cookie_crumbs[2];
if ( time() > $this->exp_variant ) {
$this->set_expiration();
delete_option( "_HANNANStd_session_expires_{$this->session_id}" );
add_option( "_HANNANStd_session_expires_{$this->session_id}", $this->expires, '', 'no' );
}
}
else {
$this->session_id = $this->generate_id();
$this->set_expiration();
}
$this->read_data();
$this->set_cookie();
}
protected function set_expiration() {
$this->exp_variant = time() + (int) apply_filters( 'HANNANStd_session_expiration_variant', 24 * 60 );
$this->expires = time() + (int) apply_filters( 'HANNANStd_session_expiration', 30 * 60 );
}
protected function set_cookie(){
if( !headers_sent() )
setcookie(HANNAN_SESSION_COOKIE,$this->session_id.'||'.$this->expires.'||'.$this->exp_variant,$this->expires,COOKIEPATH,COOKIE_DOMAIN );
}
protected function generate_id() {
require_once( ABSPATH . 'wp-includes/class-phpass.php');
$hasher = new PasswordHash( 8, false );
return md5( $hasher->get_random_bytes( 32 ) );
}
protected function read_data() {
$this->container = get_option( "_HANNANStd_session_{$this->session_id}", array() );
return $this->container;
}
public function write_data() {
$option_key = "_HANNANStd_session_{$this->session_id}";
if ( $this->dirty ) {
if ( false === get_option( $option_key ) ) {
add_option( "_HANNANStd_session_{$this->session_id}", $this->container, '', 'no' );
add_option( "_HANNANStd_session_expires_{$this->session_id}", $this->expires, '', 'no' );
}
else {
delete_option( "_HANNANStd_session_{$this->session_id}" );
add_option( "_HANNANStd_session_{$this->session_id}", $this->container, '', 'no' );
}
}
}
public function json_out() {
return json_encode( $this->container );
}
public function json_in( $data ) {
$array = json_decode( $data );
if ( is_array( $array ) ) {
$this->container = $array;
return true;
}
return false;
}
public function regenerate_id( $delete_old = false ) {
if ( $delete_old ) {
delete_option( "_HANNANStd_session_{$this->session_id}" );
}
$this->session_id = $this->generate_id();
$this->set_cookie();
}
public function session_started() {
return !!self::$instance;
}
public function cache_expiration() {
return $this->expires;
}
public function reset() {
$this->container = array();
}
public function current() {
return current( $this->container );
}
public function key() {
return key( $this->container );
}
public function next() {
next( $this->container );
}
public function rewind() {
reset( $this->container );
}
public function valid() {
return $this->offsetExists( $this->key() );
}
public function count() {
return count( $this->container );
}
}
function HANNANStd_session_cache_expire() {
$HANNANStd_session = HANNAN_Session::get_instance();
return $HANNANStd_session->cache_expiration();
}
function HANNANStd_session_commit() {
HANNANStd_session_write_close();
}
function HANNANStd_session_decode( $data ) {
$HANNANStd_session = HANNAN_Session::get_instance();
return $HANNANStd_session->json_in( $data );
}
function HANNANStd_session_encode() {
$HANNANStd_session = HANNAN_Session::get_instance();
return $HANNANStd_session->json_out();
}
function HANNANStd_session_regenerate_id( $delete_old_session = false ) {
$HANNANStd_session = HANNAN_Session::get_instance();
$HANNANStd_session->regenerate_id( $delete_old_session );
return true;
}
function HANNANStd_session_start() {
$HANNANStd_session = HANNAN_Session::get_instance();
do_action( 'HANNANStd_session_start' );
return $HANNANStd_session->session_started();
}
add_action( 'plugins_loaded', 'HANNANStd_session_start' );
function HANNANStd_session_status() {
$HANNANStd_session = HANNAN_Session::get_instance();
if ( $HANNANStd_session->session_started() ) {
return PHP_SESSION_ACTIVE;
}
return PHP_SESSION_NONE;
}
function HANNANStd_session_unset() {
$HANNANStd_session = HANNAN_Session::get_instance();
$HANNANStd_session->reset();
}
function HANNANStd_session_write_close() {
$HANNANStd_session = HANNAN_Session::get_instance();
$HANNANStd_session->write_data();
do_action( 'HANNANStd_session_commit' );
}
add_action( 'shutdown', 'HANNANStd_session_write_close' );
function HANNANStd_session_cleanup() {
global $wpdb;
if ( defined( 'HANNAN_SETUP_CONFIG' ) ) {
return;
}
if ( ! defined( 'HANNAN_INSTALLING' ) ) {
$expiration_keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_HANNANStd_session_expires_%'" );
$now = time();
$expired_sessions = array();
foreach( $expiration_keys as $expiration ) {
if ( $now > intval( $expiration->option_value ) ) {
$session_id = substr( $expiration->option_name, 20 );
$expired_sessions[] = $expiration->option_name;
$expired_sessions[] = "_HANNANStd_session_$session_id";
}
}
if ( ! empty( $expired_sessions ) ) {
$option_names = implode( "','", $expired_sessions );
$wpdb->query( "DELETE FROM $wpdb->options WHERE option_name IN ('$option_names')" );
}
}
do_action( 'HANNANStd_session_cleanup' );
}
add_action( 'HANNANStd_session_garbage_collection', 'HANNANStd_session_cleanup' );
function HANNANStd_session_register_garbage_collection() {
if ( !wp_next_scheduled( 'HANNANStd_session_garbage_collection' ) ) {
wp_schedule_event( time(), 'hourly', 'HANNANStd_session_garbage_collection' );
}
}
add_action( 'wp', 'HANNANStd_session_register_garbage_collection' );
}
?>