PluginProbe
WebTotem Security / 1.0
WebTotem Security v1.0
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 / services.php

services.php in WebTotem Security 1.0, at services.php

208 lines 8.8 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 function wtsec_cmd($wp_filesystem,$cmd, $service, $_service, $uid, $status,$domain = ''){
4
5 switch ($cmd) {
6 case $_service . "_install":
7 try {
8 $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);
15 }
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{
23 $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']);
29 }
30 }
31 wtsec_serviceConnect($uid, $service,$domain.'/'.$file['name']);
32 } catch (Exception $e) {
33 print_r($e->getMessage());
34 die();
35 }
36 break;
37 case $_service . "_start":
38 WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
39 wtsec_app()->set($_service.'_status',"start");
40 break;
41 case $_service . "_stop":
42 WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid);
43 wtsec_app()->set($_service.'_status',"stop");
44 break;
45 case $_service . "_connect":
46 $file = wtsec_getInstalledFile($_service);
47 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]));
51 }
52 break;
53 case $_service . "_uninstall":
54 try {
55 $file = wtsec_getInstalledFile($_service);
56 if($service === "WAF"){
57 $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR);
58 $mainDir = $plugin_path;
59 }else{
60 $mainDir = $wp_filesystem->abspath();
61 }
62 $target_dir = $wp_filesystem->find_folder($mainDir);
63 $target_file = trailingslashit($mainDir).$file;
64 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]));
66 }
67 wtsec_app()->set($_service . '_status', "uninstalled");
68 WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file');
69 } catch (Exception $e) {
70 print_r($e->getMessage());
71 die();
72 }
73 break;
74 }
75 }
76
77 function wtsec_checkStatus($id, $service)
78 {
79 $result = WTSEC_LIBRARY_WT::checkStatus($id,$service);
80 $data = $result['data'][strtolower($service) . 'ServiceChecks'][0];
81 $is_active = (boolean)$data['config']['isActive'];
82 $config_id = $data['config']['id'];
83 $status = $data['status'];
84 if ($status == -300 && $is_active) {
85 wtsec_app()->set($service . '_connected_' . $id, false);
86 }
87 return ['active' => $is_active, 'status' => $status, 'config_id' => $config_id];
88 }
89
90
91
92 function wtsec_generateFile($id, $service)
93 {
94 $result = WTSEC_LIBRARY_WT::generateFile($id,$service);
95 $name = $result['data']['generateAgent']['agentName'] . '.' . strtolower($service) . '.php';
96 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
97 if(empty($file)){
98 $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name);
99 }
100 return ['file' => $file, 'name' => $name];
101 }
102
103 function wtsec_serviceConnect($id, $service,$domain = '')
104 {
105 $result = WTSEC_LIBRARY_WT::serviceConnect($id,$service);
106 $status = (isset($result['errors']) || isset($result['data']['checkAgent']['lockFor']) || (isset($result['data']['checkAgent']['installed']) && $result['data']['checkAgent']['installed'] === false)) ? false : true;
107 if (!$status) {
108 WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain]));
109 }
110 return $status;
111 }
112
113
114 function wtsec_servicePing($service,$domain = '')
115 {
116 if($domain == NULL) return false;
117 if(mb_stripos($domain,"http") === false){
118 $domain = "http://".$domain;
119 }
120 $args = [
121 'timeout' => '10',
122 'sslverify' => false,
123 'redirection' => 3,
124 ];
125 $response = wp_remote_get($domain,$args);
126 $httpcode = wp_remote_retrieve_response_code($response);
127 if($httpcode>=200 && $httpcode<400){
128 return true;
129 } else {
130 WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain]));
131 return false;
132 }
133 }
134
135 function wtsec_button($label = '',$class = ''){
136 return '<button class="ww-button '.$class.'">'.$label.'</button>';
137 }
138
139 function wtsec_main_button($label = '',$class = ''){
140 return '<button class="ww-button '.$class.'">'.$label.'</button>';
141 }
142
143 function wtsec_generateButtons($uid,$_service, $service, $status,$domain = '')
144 {
145 $url = esc_url(admin_url('admin-post.php'));
146 $buttons = [];
147
148 $form = '<form action="'.$url.'" method="post" style="display:inline-block;">
149 <input type="hidden" name="service" value="'.$_service.'">
150 <input type="hidden" name="action" value="ajax_cmd">
151 <input type="hidden" name="uid" value="'.$uid.'">
152 <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>';
153 $result = [];
154 $installedFile = wtsec_checkInstalledFile($_service);
155 $runned = $status['active'];
156
157 if($service === "WAF"){
158 $first_class = "";
159 }else{
160 $first_class = "ww-button--block ";
161 }
162 $disable_uninstall = false;
163 if (!$installedFile['status']) {
164 $cmd = $_service . '_install';
165 $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'),$first_class."ww-button--success"),'cmd' => $cmd];
166 } else {
167 if($service === "WAF"){
168 $url = $domain.'/?ping='.$installedFile['file'];
169 }else{
170 $url = $domain.'/'.$installedFile['file'];
171 }
172 $connected = wtsec_servicePing($service,$url);
173 if (!$connected) {
174 $disable_uninstall = true;
175 $cmd = $_service . '_connect';
176 $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('connect'),$first_class."ww-button--success"),'cmd' => $cmd];
177 } else {
178 if ($runned) {
179 $cmd = $_service . '_stop';
180 $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('stop'),"ww-button--primary"),'cmd' => $cmd];
181 } else {
182 $cmd = $_service . '_start';
183 $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('run'),"ww-button--primary"),'cmd' => $cmd];
184 }
185 }
186 if(!$disable_uninstall && !$runned){
187 $cmd = $_service . '_uninstall';
188 $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'),$first_class."ww-button--attention"),'cmd' => $cmd];
189 }
190 }
191 foreach ($buttons as $btn){
192 $newform = $form;
193 $newform = str_replace("{{{cmd}}}",$btn['cmd'],$newform);
194 $newform = str_replace("{{{button}}}",$btn['button'],$newform);
195 $result[$btn['place']] = $newform;
196 }
197 return $result;
198 }
199
200 function wtsec_checkInstalledFile($service)
201 {
202 $file = wtsec_getInstalledFile($service);
203 return ['status' => (bool) $file,'file' => $file];
204 }
205
206 function wtsec_getInstalledFile($service){
207 return wtsec_app()->get($service."_installed_file");
208 }