| 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 |
$service = ucfirst(strtolower($service)); |
| 80 |
$is_active = null; |
| 81 |
$config_id = null; |
| 82 |
$result = WTSEC_LIBRARY_WT::checkStatus($id,$service); |
| 83 |
$data = $result['data']['auth']['viewer']['sites']['one']['configs']; |
| 84 |
foreach ($data as &$cfg){ |
| 85 |
if(!empty($cfg)){ |
| 86 |
$is_active = $cfg['isActive']; |
| 87 |
$config_id = $cfg['id']; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
if ($is_active === false) { |
| 92 |
wtsec_app()->set($service . '_connected_' . $id, true); |
| 93 |
} |
| 94 |
return ['active' => $is_active, 'config_id' => $config_id]; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 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)){ |
| 105 |
$file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name); |
| 106 |
} |
| 107 |
return ['file' => $file, 'name' => $name]; |
| 108 |
} |
| 109 |
|
| 110 |
function wtsec_serviceConnect($id, $service,$domain = '') |
| 111 |
{ |
| 112 |
$result = WTSEC_LIBRARY_WT::serviceConnect($id,$service); |
| 113 |
$status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true; |
| 114 |
if (!$status) { |
| 115 |
WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); |
| 116 |
} |
| 117 |
return $status; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
function wtsec_servicePing($service,$domain = '') |
| 122 |
{ |
| 123 |
if($domain == NULL) return false; |
| 124 |
if(stripos($domain,"http") === false){ |
| 125 |
$domain = "http://".$domain; |
| 126 |
} |
| 127 |
$args = [ |
| 128 |
'timeout' => '10', |
| 129 |
'sslverify' => false, |
| 130 |
'redirection' => 3, |
| 131 |
]; |
| 132 |
$response = wp_remote_get($domain,$args); |
| 133 |
$httpcode = wp_remote_retrieve_response_code($response); |
| 134 |
if($httpcode>=200 && $httpcode<400){ |
| 135 |
return true; |
| 136 |
} else { |
| 137 |
WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
function wtsec_button($label = '',$class = ''){ |
| 143 |
return '<button class="ww-button '.$class.'">'.$label.'</button>'; |
| 144 |
} |
| 145 |
|
| 146 |
function wtsec_main_button($label = '',$class = ''){ |
| 147 |
return '<button class="ww-button '.$class.'">'.$label.'</button>'; |
| 148 |
} |
| 149 |
|
| 150 |
function wtsec_generateButtons($uid,$_service, $service, $status,$domain = '') |
| 151 |
{ |
| 152 |
$url = esc_url(admin_url('admin-post.php')); |
| 153 |
$buttons = []; |
| 154 |
|
| 155 |
$form = '<form action="'.$url.'" method="post" style="display:inline-block;"> |
| 156 |
<input type="hidden" name="service" value="'.$_service.'"> |
| 157 |
<input type="hidden" name="action" value="ajax_cmd"> |
| 158 |
<input type="hidden" name="uid" value="'.$uid.'"> |
| 159 |
<input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>'; |
| 160 |
$result = []; |
| 161 |
$installedFile = wtsec_checkInstalledFile($_service); |
| 162 |
$runned = $status['active']; |
| 163 |
|
| 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]; |
| 184 |
} 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]; |
| 191 |
} |
| 192 |
} |
| 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]; |
| 196 |
} |
| 197 |
} |
| 198 |
foreach ($buttons as $btn){ |
| 199 |
$newform = $form; |
| 200 |
$newform = str_replace("{{{cmd}}}",$btn['cmd'],$newform); |
| 201 |
$newform = str_replace("{{{button}}}",$btn['button'],$newform); |
| 202 |
$result[$btn['place']] = $newform; |
| 203 |
} |
| 204 |
return $result; |
| 205 |
} |
| 206 |
|
| 207 |
function wtsec_checkInstalledFile($service) |
| 208 |
{ |
| 209 |
$file = wtsec_getInstalledFile($service); |
| 210 |
return ['status' => (bool) $file,'file' => $file]; |
| 211 |
} |
| 212 |
|
| 213 |
function wtsec_getInstalledFile($service){ |
| 214 |
return wtsec_app()->get($service."_installed_file"); |
| 215 |
} |