PluginProbe
WebTotem Security / 2.3.14
WebTotem Security v2.3.14
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
← All changes | services.php +236 -98 2.1.22.3.14 View file →
@@ -1,89 +1,137 @@
1 1 <?php defined('ABSPATH') or die("Protected By WT!");
2 2
3 -function wtsec_cmd($wp_filesystem,$cmd, $service, $_service, $uid, $status,$domain = ''){
3 +function wtsec_cmd($cmd, $service, $_service, $uid, $status, $domain = '')
4 +{
5 + global $wp_filesystem;
4 6
7 + //bug fix with wp file system, which return null
8 + if (empty($wp_filesystem)) {
9 + require_once(ABSPATH . '/wp-admin/includes/file.php');
10 + WP_Filesystem();
11 + }
12 +
13 + //retry checking
14 + if (empty($wp_filesystem)) {
15 + WTSEC_LIBRARY_Session::setNotification("error", "WP FileSystem path error");
16 + return false;
17 + }
18 +
5 19 switch ($cmd) {
6 20 case $_service . "_install":
7 21 try {
8 22 $file = wtsec_generateFile($uid, $service);
9 - if($service === "WAF"){
10 - $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
11 - $path = $plugin_path;
12 - $target_dir = $wp_filesystem->find_folder($path);
13 - if(!$wp_filesystem->is_dir($path)) {
14 - $wp_filesystem->mkdir($target_dir);
23 + if (is_null($file['name'])) {
24 + WTSEC_LIBRARY_Session::setNotification("error", wtsec_locale("failed_to_download_agent"));
25 + return false;
15 26 }
16 - $target_file = trailingslashit($target_dir).$file['name'];
17 - if (!$wp_filesystem->put_contents($target_file, $file['file'], FS_CHMOD_FILE)){
18 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service,'directory' => $target_dir]));
19 - }else{
20 - wtsec_app()->set($_service . '_installed_file',$file['name']);
21 - }
22 - }else{
27 + if ($service === "WAF") {
28 + $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
29 + $path = $plugin_path;
30 + $target_dir = $wp_filesystem->find_folder($path);
31 + if (!$wp_filesystem->is_dir($path)) {
32 + $wp_filesystem->mkdir($target_dir);
33 + }
34 + $target_file = trailingslashit($target_dir) . $file['name'];
35 + if (!$wp_filesystem->put_contents($target_file, $file['file'], FS_CHMOD_FILE)) {
36 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service, 'directory' => $target_dir]));
37 + } else {
38 + wtsec_app()->set($_service . '_installed_file', $file['name']);
39 + }
40 + } else {
23 41 $target_dir = $wp_filesystem->find_folder($wp_filesystem->abspath());
24 - $target_file = trailingslashit($target_dir).$file['name'];
25 - if(!$wp_filesystem->put_contents($target_file, $file['file'])) {
26 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service,'directory' => $target_dir]));
27 - }else{
28 - wtsec_app()->set($_service . '_installed_file',$file['name']);
42 + $target_file = trailingslashit($target_dir) . $file['name'];
43 + if (!$wp_filesystem->put_contents($target_file, $file['file'])) {
44 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service, 'directory' => $target_dir]));
45 + } else {
46 + wtsec_app()->set($_service . '_installed_file', $file['name']);
47 + if ($service === "AM") {
48 + wtsec_addCheckFile($file['name']);
49 + }
29 50 }
30 51 }
31 - wtsec_serviceConnect($uid, $service,$domain.'/'.$file['name']);
52 + if ($service !== "AM") {
53 + //wtsec_serviceConnect($uid, $service, $domain . '/' . $file['name']);
54 + }
32 55 } catch (Exception $e) {
33 - print_r($e->getMessage());
34 - die();
56 + WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
57 + return false;
35 58 }
36 59 break;
37 60 case $_service . "_start":
38 - WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
39 - wtsec_app()->set($_service.'_status',"start");
61 + WTSEC_LIBRARY_WT::changeStatus($status['config_id'], $uid);
62 + wtsec_app()->set($_service . '_status', "start");
40 63 break;
41 64 case $_service . "_stop":
42 65 // WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
43 - wtsec_app()->set($_service.'_status',"stop");
66 + wtsec_app()->set($_service . '_status', "stop");
44 67 break;
45 68 case $_service . "_connect":
46 69 $file = wtsec_getInstalledFile($_service);
47 70 if ($file) {
48 - wtsec_serviceConnect($uid, $service, $domain . '/' . $file);
49 - }else{
50 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service]));
71 + //wtsec_serviceConnect($uid, $service, $domain . '/' . $file);
72 + } else {
73 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service]));
51 74 }
52 75 break;
53 76 case $_service . "_uninstall":
54 77 try {
55 78 $file = wtsec_getInstalledFile($_service);
56 - if($service === "WAF"){
79 + if ($service === "WAF") {
57 80 $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
58 81 $mainDir = $plugin_path;
59 - }else{
82 + } else {
60 83 $mainDir = $wp_filesystem->abspath();
61 84 }
62 85 $target_dir = $wp_filesystem->find_folder($mainDir);
63 - $target_file = trailingslashit($mainDir).$file;
86 + $target_file = trailingslashit($mainDir) . $file;
64 87 if (!$wp_filesystem->delete($target_file)) {
65 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service,'directory' => $target_dir]));
88 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service, 'directory' => $target_dir]));
66 89 }
67 90 wtsec_app()->set($_service . '_status', "uninstalled");
68 91 WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file');
69 92 } catch (Exception $e) {
70 - print_r($e->getMessage());
71 - die();
93 + WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage());
94 + return false;
72 95 }
73 96 break;
74 97 }
98 + return true;
75 99 }
76 100
101 +// Add check file if plugin is activated
102 +function wtsec_addCheckFile($name){
103 + global $wp_filesystem;
104 +
105 + if (empty($wp_filesystem)) {
106 + require_once(ABSPATH . '/wp-admin/includes/file.php');
107 + WP_Filesystem();
108 + }
109 +
110 + //retry checking
111 + if (empty($wp_filesystem)) {
112 + WTSEC_LIBRARY_Session::setNotification("error", "WP FileSystem path error");
113 + return false;
114 + }
115 +
116 + $target_dir = $wp_filesystem->find_folder(WTSEC_PLUGIN_PATH);
117 + $target_file = trailingslashit($target_dir) . 'generate.php';
118 +
119 + if ($wp_filesystem->put_contents($target_file, '<?php exit(); ?>'.$name, FS_CHMOD_FILE)) {
120 + return true;
121 + }
122 + return false;
123 +}
124 +
77 125 function wtsec_checkStatus($id, $service)
78 126 {
79 127 $service = ucfirst(strtolower($service));
80 128 $is_active = null;
81 129 $config_id = null;
82 - $result = WTSEC_LIBRARY_WT::checkStatus($id,$service);
130 + $result = WTSEC_LIBRARY_WT::checkStatus($id, $service);
83 131 $data = $result['data']['auth']['viewer']['sites']['one']['configs'];
84 - foreach ($data as &$cfg){
85 - if(!empty($cfg)){
132 + foreach ($data as &$cfg) {
133 + if (!empty($cfg)) {
86 134 $is_active = $cfg['isActive'];
87 135 $config_id = $cfg['id'];
88 136 }
89 137 }
@@ -93,37 +141,61 @@
93 141 }
94 142 return ['active' => $is_active, 'config_id' => $config_id];
95 143 }
96 144
145 +function wtsec_generateFile($id, $service)
146 +{
147 + if ($service === "AM") {
148 + $result = WTSEC_LIBRARY_WT::generateAmFile($id);
149 + if (!isset($result['data']['auth']['am']['install'])) {
150 + $file = ['filename' => null, "body" => null];
151 + } else {
152 + $result = $result['data']['auth']['am']['install'];
97 153
154 + $url = $result['downloadLink'];
155 + $amFilename = $result['amFilename'];
156 + $wafFilename = $result['wafFilename'];
98 157
99 -function wtsec_generateFile($id, $service)
100 -{
101 - $result = WTSEC_LIBRARY_WT::generateFile($id,$service);
102 - $name = $result['data']['auth']['agents']['generate']['agentName'];
103 - $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
104 - if(empty($file)){
158 + if($wafFilename){
159 + wtsec_app()->set('waf_installed_file', $wafFilename);
160 + }
161 +
162 + $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
163 + if (empty($file)) {
164 + $file = WTSEC_LIBRARY_WT::getFileByUrl($url, $amFilename);
165 + }
166 + }
167 +
168 + $name = $file["filename"];
169 + $file = $file["body"];
170 + } else {
171 + $result = WTSEC_LIBRARY_WT::generateFile($id, $service);
172 + $name = $result['data']['auth']['agents']['generate']['agentName'];
105 173 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
174 + if (empty($file)) {
175 + $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
176 + }
106 177 }
178 +
107 179 return ['file' => $file, 'name' => $name];
108 180 }
109 181
110 -function wtsec_serviceConnect($id, $service,$domain = '')
182 +function wtsec_serviceConnect($id, $service, $domain = '')
111 183 {
112 - $result = WTSEC_LIBRARY_WT::serviceConnect($id,$service);
184 + $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service);
113 185 $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true;
114 186 if (!$status) {
115 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain]));
187 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain]));
116 188 }
117 189 return $status;
118 190 }
119 191
120 192
121 -function wtsec_servicePing($service,$domain = '')
193 +function wtsec_servicePing($service, $domain = '')
122 194 {
123 - if($domain == NULL) return false;
124 - if(stripos($domain,"http") === false){
125 - $domain = "http://".$domain;
195 + if ($domain == NULL) return false;
196 + if (stripos($domain, "http") === false) {
197 + $domain = "http://" . $domain;
126 198 }
127 199 $args = [
128 200 'timeout' => '10',
129 201 'sslverify' => false,
@@ -128,78 +200,131 @@
128 200 'timeout' => '10',
129 201 'sslverify' => false,
130 202 'redirection' => 3,
131 203 ];
132 - $response = wp_remote_get($domain,$args);
204 + $response = wp_remote_get($domain, $args);
133 205 $httpcode = wp_remote_retrieve_response_code($response);
134 - if($httpcode>=200 && $httpcode<400){
206 + if ($httpcode >= 200 && $httpcode < 400) {
135 207 return true;
136 208 } else {
137 - WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain]));
209 + WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain]));
138 210 return false;
139 211 }
140 212 }
141 213
142 -function wtsec_button($label = '',$class = ''){
143 - return '<button class="ww-button '.$class.'">'.$label.'</button>';
214 +
215 +function wtsec_AVLogsData($logs){
216 + foreach ($logs as $key => $log){
217 + $log = $log['node'];
218 + $log['filePath'] = urldecode($log['filePath']);
219 + $log['text'] = (strlen($log['filePath']) > 40) ? substr($log['filePath'],0,40).'...' : $log['filePath'];
220 + $log['time'] = convertToCurrentTime($log['time']);
221 + $log['class_path'] = '';
222 + $log['class_info'] = '';
223 +
224 + if ($log['event'] === 'modified') {
225 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
226 + $log['class_path'] = "wtotem_file-table__td_changed";
227 + $log['class_info'] = "wtotem_file-table__td_normal";
228 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.changed");
229 + } elseif ($log['event'] === 'quarantine') {
230 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.backdoor_virus");
231 + $log['class_path'] = "wtotem_file-table__td_changed";
232 + $log['class_info'] = "wtotem_file-table__td_normal";
233 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.quarantine");
234 + } elseif ($log['event'] === 'infected') {
235 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.backdoor_virus");
236 + $log['class_path'] = "wtotem_file-table__td_critical";
237 + $log['class_info'] = "wtotem_file-table__td_critical";
238 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.infected");
239 + } elseif ($log['event'] === 'deleted') {
240 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.deleted");
241 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.deleted");
242 + }elseif ($log['event'] === 'new'){
243 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
244 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.new");
245 + }elseif ($log['event'] === 'scanned'){
246 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.info.normal");
247 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.scanned");
248 + }else{
249 + $log['info'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.error");
250 + $log['description'] = WTSEC_LIBRARY_Localization::lmsg("antivirus.status.error");
251 + }
252 +
253 + $logs[$key]['node'] = $log;
254 + }
255 + return $logs;
144 256 }
145 257
146 -function wtsec_main_button($label = '',$class = ''){
147 - return '<button class="ww-button '.$class.'">'.$label.'</button>';
258 +function wtsec_getConfigs($host_id)
259 +{
260 + $config = WTSEC_LIBRARY_WT::getConfigs($host_id);
261 + return wtsec_arrayIndex($config['data']['auth']['viewer']['sites']['one']['configs'], 'service');
148 262 }
149 263
150 -function wtsec_generateButtons($uid,$_service, $service, $status,$domain = '')
264 +function wtsec_arrayIndex($array, $key){
265 + $newArray = [];
266 + foreach ($array as $item){
267 + if(array_key_exists($key,$item)){
268 + $newArray[$item[$key]] = $item;
269 + }
270 + }
271 + return $newArray;
272 +}
273 +
274 +function wtsec_button($label = '', $class = '')
151 275 {
276 + return '<button class="ww-button ' . $class . '">' . $label . '</button>';
277 +}
278 +
279 +function wtsec_main_button($label = '', $class = '')
280 +{
281 + return '<button class="ww-button ' . $class . '">' . $label . '</button>';
282 +}
283 +
284 +function wtsec_generateButtons($uid, $_service, $service, $status, $domain = '')
285 +{
152 286 $url = esc_url(admin_url('admin-post.php'));
153 287 $buttons = [];
154 288
155 - $form = '<form action="'.$url.'" method="post" style="display:inline-block;">
156 - <input type="hidden" name="service" value="'.$_service.'">
289 + $form = '<form action="' . $url . '" method="post" style="display:inline-block;">
290 + <input type="hidden" name="service" value="' . $_service . '">
157 291 <input type="hidden" name="action" value="ajax_cmd">
158 - <input type="hidden" name="uid" value="'.$uid.'">
292 + <input type="hidden" name="uid" value="' . $uid . '">
159 293 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
160 294 $result = [];
161 295 $installedFile = wtsec_checkInstalledFile($_service);
162 - $runned = $status['active'];
296 + $disable_uninstall = false;
297 + $first_class = "ww-button--block ";
163 298
164 - if($service === "WAF"){
165 - $first_class = "";
166 - }else{
167 - $first_class = "ww-button--block ";
168 - }
169 - $disable_uninstall = false;
170 - if (!$installedFile['status']) {
171 - $cmd = $_service . '_install';
172 - $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'),$first_class."ww-button--success"),'cmd' => $cmd];
173 - } else {
174 - if($service === "WAF"){
175 - $url = $domain.'/?ping='.$installedFile['file'];
176 - }else{
177 - $url = $domain.'/'.$installedFile['file'];
178 - }
179 - $connected = wtsec_servicePing($service,$url);
180 - if (!$connected) {
181 - $disable_uninstall = false;
182 - $cmd = $_service . '_connect';
183 - $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('connect'),$first_class."ww-button--success"),'cmd' => $cmd];
299 + if ($service === "AM") {
300 + if (!$installedFile['status']) {
301 + $cmd = $_service . '_install';
302 + $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd];
184 303 } else {
185 - if ($runned) {
186 - $cmd = $_service . '_stop';
187 -// $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('stop'),"ww-button--primary"),'cmd' => $cmd];
188 - } else {
189 - $cmd = $_service . '_start';
190 -// $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('run'),"ww-button--primary"),'cmd' => $cmd];
304 +// $url = $domain.'/'.$installedFile['file'];
305 +// $connected = wtsec_servicePing($service,$url);
306 + if (!$disable_uninstall) {
307 + $cmd = $_service . '_uninstall';
308 + $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'), $first_class . "ww-button--attention"), 'cmd' => $cmd];
191 309 }
192 310 }
193 - if(!$disable_uninstall){
194 - $cmd = $_service . '_uninstall';
195 - $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'),$first_class."ww-button--attention"),'cmd' => $cmd];
311 + } else {
312 + if ($service === "WAF") {
313 + $first_class = "";
196 314 }
315 +
316 + if ($status === "not_installed") {
317 + $cmd = $_service . '_install';
318 + $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd];
319 + }
197 320 }
198 - foreach ($buttons as $btn){
321 +
322 +
323 + foreach ($buttons as $btn) {
199 324 $newform = $form;
200 - $newform = str_replace("{{{cmd}}}",$btn['cmd'],$newform);
201 - $newform = str_replace("{{{button}}}",$btn['button'],$newform);
325 + $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform);
326 + $newform = str_replace("{{{button}}}", $btn['button'], $newform);
202 327 $result[$btn['place']] = $newform;
203 328 }
204 329 return $result;
205 330 }
@@ -206,10 +331,23 @@
206 331
207 332 function wtsec_checkInstalledFile($service)
208 333 {
209 334 $file = wtsec_getInstalledFile($service);
210 - return ['status' => (bool) $file,'file' => $file];
335 + if ($service == "waf") {
336 + $root = WTSEC_INSTALLATION_DIR;
337 + } else {
338 + $root = ABSPATH;
339 + }
340 + return ['status' => ((bool)$file) && is_file($root . $file), 'file' => $file];
211 341 }
212 342
213 -function wtsec_getInstalledFile($service){
214 - return wtsec_app()->get($service."_installed_file");
215 -}
343 +function wtsec_getInstalledFile($service)
344 +{
345 + return wtsec_app()->get($service . "_installed_file");
346 +}
347 +
348 +function wtsec_DeleteAm(){
349 + $host = WTSEC_LIBRARY_WT::getOwnSite();
350 + if(!empty($host)){
351 + WTSEC_LIBRARY_WT::deleteAm($host['id']);
352 + }
353 +}