PluginProbe
WebTotem Security / 2.3.35
WebTotem Security v2.3.35
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / library / App.php

App.php in WebTotem Security 2.3.35, at library/App.php

207 lines 7.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php defined('ABSPATH') or die("Protected By WT!");
2
3
4 class WTSEC_LIBRARY_App
5 {
6
7 public function __construct()
8 {
9 $this->auth();
10 }
11
12 public function auth(){
13 if(self::getOption('authorized') === false){
14 wp_safe_redirect(admin_url('admin.php?page=' . WTSEC_PAGE_PREFIX . 'login'));
15 exit;
16 }
17 }
18
19 public static function authorized(){
20 return (boolean) self::getOption('authorized');
21 }
22
23 public static function login($token){
24 self::_set('authorized', true);
25 self::_set('authToken', $token);
26 }
27
28 public static function logout(){
29 $options = [
30 'api_key', 'api_key_safe', 'api_key_activated', 'authorized', 'authToken',
31 'waf_installed_file','am_installed_file','am_installed',
32 'av_installed', 'waf_installed', 'agents_installed',
33 'color_scheme','check_login_attempt','time_zone',
34 'token_expired','deactivated', 'antivirus_event', 'antivirus_permissions_changed',
35 'antivirus_endCursor', 'antivirus_hasNextPage',
36 'firewall_endCursor', 'firewall_hasNextPage',
37 'reports_endCursor', 'reports_hasNextPage'];
38
39 self::_delete($options);
40 self::_set('logout', true);
41 //self::_set('am_installed', false);
42 }
43
44 public function getLocalDomains(){
45 return [];
46 }
47
48 public static function _set($name,$value){
49 return update_option(WTSEC_PLUGIN_PREFIX.$name,$value);
50 }
51
52 public static function _get($name){
53 return get_option(WTSEC_PLUGIN_PREFIX.$name);
54 }
55
56 public static function _delete($options){
57 if(is_array($options)){
58 foreach ($options as $option) {
59 delete_option(WTSEC_PLUGIN_PREFIX.$option);
60 }
61 } else{
62 delete_option(WTSEC_PLUGIN_PREFIX.$options);
63 }
64 return true;
65 }
66
67 public function set($name,$value){
68 return update_option(WTSEC_PLUGIN_PREFIX.$name,$value);
69 }
70
71 public function get($name){
72 return get_option(WTSEC_PLUGIN_PREFIX.$name);
73 }
74
75 public static function getOption($name){
76 return get_option(WTSEC_PLUGIN_PREFIX.$name);
77 }
78
79 public static function deleteOption($name){
80 return delete_option(WTSEC_PLUGIN_PREFIX.$name);
81 }
82
83 public static function getToken(){
84 return self::getOption('authToken');
85 }
86
87 public static function getName(){
88 return self::getOption('username');
89 }
90
91
92 // Limit login attempt
93 public function wtsec_login_check($user, $username, $password){
94
95 if( ! $this->wtsec_is_need_check() ){
96 return $user;
97 }
98
99 $massage = __( 'The maximum number of login attempts has been reached. Please try again in %d minutes.', 'wtotem' );
100 global $msg;
101
102 $options = (object) [
103 // время снятия блокировки в секуда�
104
105 'lock_time' => 1800, // 1800
106 // Количество попыток авторизоваться
107 'lock_num' => 5,
108 // Сообщение о блокировке
109 'massage' => $massage,
110 // время полной очистки файла - 4 дня
111 'expire_time' => 3600 * 24 * 4,
112 // добавить в названия файлов запрос REQUEST_URI - может пригодится для дебага
113 'add_uri' => false,
114 // использовать ли мякгий метод получения IP?
115 'soft_get_ip' => false,
116 ];
117
118 // чтобы установить имя файла
119 $action = @ $_GET['action'];
120 if( ! in_array( $action, array( 'postpass', 'logout', 'lostpassword', 'retrievepassword', 'resetpass', 'rp', 'register', 'login' ), true ) )
121 $action = 'login';
122
123 // создаем директорию кэша если надо
124 $cache_dir = plugin_dir_path(__FILE__) . 'cache';
125 if( ! file_exists($cache_dir) )
126 mkdir( $cache_dir, 0777 );
127
128 // оперделим название файла
129 $REQUEST_URI = $options->add_uri ? ( '___'. preg_replace('/[^a-zA-Z0-9:=?_-]/', '-', $_SERVER['REQUEST_URI'] ) /*все на -*/ ) : '';
130 $domen = str_replace('www.', '', $_SERVER['HTTP_HOST'] );
131 $file_path = $cache_dir . "/{$domen}___{$action}{$REQUEST_URI}.ini";
132
133 // оперделим ip
134 if( ! $ip = $this->wtsec_get_ip( $options->soft_get_ip ) )
135 die( 'No IP...' );
136
137 // парсим данные
138 $data = @ parse_ini_file( $file_path );
139 $data = (!$data) ? [] : $data;
140 $attempts = array_key_exists($ip,$data) ? count($data[$ip]) + 1 : 1;
141 $last_access_time = array_key_exists($ip,$data) ? max( $data[$ip] ) : 0;
142 $blocked_time = $last_access_time + ($options->lock_time * floor( $attempts / $options->lock_num) );
143
144 if ( $data['status'][$ip] == 'blocked' && $blocked_time > time() ){
145 $blocked_time = ($blocked_time - time()) / 60;
146
147 // Вывод ошибки и блокировка
148 $msg = sprintf($options->massage, $options->lock_num, $blocked_time);
149 $error = new WP_Error();
150 $error->add('wp_to_many_try', $msg);
151 return $error;
152 } else{
153 file_put_contents( $file_path, sprintf("{$ip}[] = %d\n", time()), FILE_APPEND ); // добавляем ip и время
154
155 if ($data['status'][$ip] == 'blocked'){
156 $new_content = preg_replace("~status\[" . $ip . "\] = [a-z]+\n~", '', file_get_contents($file_path) );
157 file_put_contents( $file_path, $new_content ); // снимает блокировку
158 }
159
160 if($attempts >= $options->lock_num && ( $attempts % $options->lock_num ) == 0)
161 file_put_contents( $file_path, "status[{$ip}] = blocked\n", FILE_APPEND); // добавляем блокировку
162 }
163
164 // полная очистка файла
165 if( empty($data['expire']) ){
166 file_put_contents( $file_path, 'expire = ' . time() . "\n", FILE_APPEND );
167 }
168 elseif( $data['expire'] + $options->expire_time < time() ){
169 $content = 'expire = '. time() . "\n";
170 file_put_contents( $file_path, $content ); // очищаем файл полностью
171 }
172 return $user;
173 }
174
175 protected function wtsec_is_need_check(){
176
177 // работает только для POST запросов.
178 if( empty($_POST) )
179 return false;
180
181 $need_check = false;
182 $settings_value = self::_get('check_login_attempt');
183
184 // для страницы '/wp-login.php'
185 if( false !== strpos($_SERVER['REQUEST_URI'], '/wp-login.php') && $settings_value )
186 $need_check = true;
187
188
189 return $need_check;
190 }
191
192 protected function wtsec_get_ip( $soft = false ){
193
194 if( $soft ){
195 $ip = isset($_SERVER['HTTP_CF_CONNECTING_IP']) ? $_SERVER['HTTP_CF_CONNECTING_IP'] : ''; // cloudflare IP support
196 if( ! filter_var($ip, FILTER_VALIDATE_IP) ) $ip = isset($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : '';
197 if( ! filter_var($ip, FILTER_VALIDATE_IP) ) $ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : '';
198 if( ! filter_var($ip, FILTER_VALIDATE_IP) ) $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
199 }
200 else {
201 $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
202 }
203
204 return $ip;
205 }
206
207 }