abspath(), WTSEC_INSTALLATION_DIR); $path = $plugin_path; $target_dir = $wp_filesystem->find_folder($path); if(!$wp_filesystem->is_dir($path)) { $wp_filesystem->mkdir($target_dir); } $target_file = trailingslashit($target_dir).$file['name']; if (!$wp_filesystem->put_contents($target_file, $file['file'], FS_CHMOD_FILE)){ WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service,'directory' => $target_dir])); }else{ wtsec_app()->set($_service . '_installed_file',$file['name']); } }else{ $target_dir = $wp_filesystem->find_folder($wp_filesystem->abspath()); $target_file = trailingslashit($target_dir).$file['name']; if(!$wp_filesystem->put_contents($target_file, $file['file'])) { WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_install_file', ['service' => $service,'directory' => $target_dir])); }else{ wtsec_app()->set($_service . '_installed_file',$file['name']); } } wtsec_serviceConnect($uid, $service,$domain.'/'.$file['name']); } catch (Exception $e) { print_r($e->getMessage()); die(); } break; case $_service . "_start": WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid); wtsec_app()->set($_service.'_status',"start"); break; case $_service . "_stop": // WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid); wtsec_app()->set($_service.'_status',"stop"); break; case $_service . "_connect": $file = wtsec_getInstalledFile($_service); if ($file) { wtsec_serviceConnect($uid, $service, $domain . '/' . $file); }else{ WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service])); } break; case $_service . "_uninstall": try { $file = wtsec_getInstalledFile($_service); if($service === "WAF"){ $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR); $mainDir = $plugin_path; }else{ $mainDir = $wp_filesystem->abspath(); } $target_dir = $wp_filesystem->find_folder($mainDir); $target_file = trailingslashit($mainDir).$file; if (!$wp_filesystem->delete($target_file)) { WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service,'directory' => $target_dir])); } wtsec_app()->set($_service . '_status', "uninstalled"); WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file'); } catch (Exception $e) { print_r($e->getMessage()); die(); } break; } } function wtsec_checkStatus($id, $service) { $service = ucfirst(strtolower($service)); $is_active = null; $config_id = null; $result = WTSEC_LIBRARY_WT::checkStatus($id,$service); $data = $result['data']['auth']['viewer']['sites']['one']['configs']; foreach ($data as &$cfg){ if(!empty($cfg)){ $is_active = $cfg['isActive']; $config_id = $cfg['id']; } } if ($is_active === false) { wtsec_app()->set($service . '_connected_' . $id, true); } return ['active' => $is_active, 'config_id' => $config_id]; } function wtsec_generateFile($id, $service) { $result = WTSEC_LIBRARY_WT::generateFile($id,$service); $name = $result['data']['auth']['agents']['generate']['agentName']; $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name); if(empty($file)){ $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name); } return ['file' => $file, 'name' => $name]; } function wtsec_serviceConnect($id, $service,$domain = '') { $result = WTSEC_LIBRARY_WT::serviceConnect($id,$service); $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true; if (!$status) { WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); } return $status; } function wtsec_servicePing($service,$domain = '') { if($domain == NULL) return false; if(mb_stripos($domain,"http") === false){ $domain = "http://".$domain; } $args = [ 'timeout' => '10', 'sslverify' => false, 'redirection' => 3, ]; $response = wp_remote_get($domain,$args); $httpcode = wp_remote_retrieve_response_code($response); if($httpcode>=200 && $httpcode<400){ return true; } else { WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); return false; } } function wtsec_button($label = '',$class = ''){ return ''; } function wtsec_main_button($label = '',$class = ''){ return ''; } function wtsec_generateButtons($uid,$_service, $service, $status,$domain = '') { $url = esc_url(admin_url('admin-post.php')); $buttons = []; $form = '
'; $result = []; $installedFile = wtsec_checkInstalledFile($_service); $runned = $status['active']; if($service === "WAF"){ $first_class = ""; }else{ $first_class = "ww-button--block "; } $disable_uninstall = false; if (!$installedFile['status']) { $cmd = $_service . '_install'; $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'),$first_class."ww-button--success"),'cmd' => $cmd]; } else { if($service === "WAF"){ $url = $domain.'/?ping='.$installedFile['file']; }else{ $url = $domain.'/'.$installedFile['file']; } $connected = wtsec_servicePing($service,$url); if (!$connected) { $disable_uninstall = false; $cmd = $_service . '_connect'; $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('connect'),$first_class."ww-button--success"),'cmd' => $cmd]; } else { if ($runned) { $cmd = $_service . '_stop'; // $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('stop'),"ww-button--primary"),'cmd' => $cmd]; } else { $cmd = $_service . '_start'; // $buttons[] = ['place' => 'second','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('run'),"ww-button--primary"),'cmd' => $cmd]; } } if(!$disable_uninstall){ $cmd = $_service . '_uninstall'; $buttons[] = ['place' => 'first','button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'),$first_class."ww-button--attention"),'cmd' => $cmd]; } } foreach ($buttons as $btn){ $newform = $form; $newform = str_replace("{{{cmd}}}",$btn['cmd'],$newform); $newform = str_replace("{{{button}}}",$btn['button'],$newform); $result[$btn['place']] = $newform; } return $result; } function wtsec_checkInstalledFile($service) { $file = wtsec_getInstalledFile($service); return ['status' => (bool) $file,'file' => $file]; } function wtsec_getInstalledFile($service){ return wtsec_app()->get($service."_installed_file"); }