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
|
<?php
final class ITSEC_Away_Mode_Utilities {
/** * Check if the config file signaling away mode is active exists. * * @return bool */ public static function has_active_file() { if ( @file_exists( self::get_active_file_name() ) ) { return true; } else { return false; } }
/** * Create a config file specifying that away mode is active. * * @return bool */ public static function create_active_file() { if ( self::has_active_file() ) { return true; }
$file = self::get_active_file_name();
$result = @file_put_contents( $file, 'true' );
if ( false === $result ) { return false; } else { return true; } }
/** * Delete the config file specifying that away mode is active. * * @return bool */ public static function delete_active_file() { if ( ! self::has_active_file() ) { return true; }
$file = self::get_active_file_name();
return @unlink( $file ); }
/** * Get the file name for the config file specifying that away mode is active, * * @return string */ public static function get_active_file_name() { $file_name = apply_filters( 'itsec_filer_away_mode_active_file', ITSEC_Core::get_storage_dir() . '/itsec_away.confg' );
return $file_name; }
/** * Check if the current UTC time falls between the two specified times, inclusive. * * @param int $start The UTC timestamp signalling the beginning of the active window. * @param int $end The UTC timestamp signalling the end of the active window. * @param bool $include_details Whether to include additional details about the active window. * * @return array|bool */ public static function is_current_timestamp_active( $start, $end, $include_details = false ) { $now = ITSEC_Core::get_current_time_gmt();
$active = false;
if ( $start <= $now && $now <= $end ) { $active = true; }
if ( ! $include_details ) { return $active; }
if ( $start > $end ) { $remaining = false; $next = false; $length = false;
/* translators: 1: start timestamp, 2: end timestamp */ $error = new WP_Error( 'itsec-away-mode-is-current-timestamp-in-range-start-after-end', sprintf( __( 'The supplied data is invalid. The supplied start (%1$s) is after the supplied end (%2$s).', 'better-wp-security' ), $start, $end ) ); } else { $remaining = $end - $now; $next = $start - $now; $length = $end - $start; $error = false;
if ( $now < $start ) { $remaining = false; }
if ( $next < 0 ) { $next = false; } }
return compact( 'active', 'remaining', 'next', 'length', 'error' ); }
/** * Check if the current local time falls between the two specified times, inclusive. * * @param int $start The local timestamp signalling the beginning of the active window. * @param int $end The local timestamp signalling the end of the active window. * @param bool $include_details Whether to include additional details about the active window. * * @return array|bool */ public static function is_current_time_active( $start, $end, $include_details = false ) { $current_time = ITSEC_Core::get_current_time(); $now = $current_time - strtotime( date( 'Y-m-d', $current_time ) );
$active = false;
if ( $start <= $end ) { if ( $start <= $now && $now <= $end ) { $active = true; } } else { if ( $start <= $now || $now <= $end ) { $active = true; } }
if ( ! $include_details ) { return $active; }
$remaining = $end - $now; $next = $start - $now; $length = $end - $start;
if ( $active && $remaining < 0 ) { $remaining += DAY_IN_SECONDS; } else if ( ! $active && $remaining >= 0 ) { $remaining -= DAY_IN_SECONDS; }
if ( $next < 0 ) { $next += DAY_IN_SECONDS; }
if ( $length < 0 ) { $length += DAY_IN_SECONDS; }
return compact( 'active', 'remaining', 'next', 'length' ); } }
|