| @@ -1,165 +1,122 @@ | ||
| 1 | 1 | <?php defined('ABSPATH') or die("Protected By WT!"); |
| 2 | 2 | |
| 3 | -function wtsec_cmd($cmd, $service, $_service, $uid, $status, $domain = '') | |
| 4 | -{ | |
| 5 | - global $wp_filesystem; | |
| 3 | +function wtsec_cmd($wp_filesystem,$cmd, $service, $_service, $uid, $status,$domain = ''){ | |
| 6 | 4 | |
| 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 | - | |
| 19 | 5 | switch ($cmd) { |
| 20 | 6 | case $_service . "_install": |
| 21 | 7 | try { |
| 22 | 8 | $file = wtsec_generateFile($uid, $service); |
| 23 | - if (is_null($file['name'])) { | |
| 24 | - WTSEC_LIBRARY_Session::setNotification("error", wtsec_locale("failed_to_download_agent")); | |
| 25 | - return false; | |
| 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); | |
| 26 | 15 | } |
| 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 { | |
| 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{ | |
| 41 | 23 | $target_dir = $wp_filesystem->find_folder($wp_filesystem->abspath()); |
| 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']); | |
| 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']); | |
| 47 | 29 | } |
| 48 | 30 | } |
| 49 | - if ($service !== "AM") { | |
| 50 | - wtsec_serviceConnect($uid, $service, $domain . '/' . $file['name']); | |
| 51 | - } | |
| 31 | + wtsec_serviceConnect($uid, $service,$domain.'/'.$file['name']); | |
| 52 | 32 | } catch (Exception $e) { |
| 53 | - WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage()); | |
| 54 | - return false; | |
| 33 | + print_r($e->getMessage()); | |
| 34 | + die(); | |
| 55 | 35 | } |
| 56 | 36 | break; |
| 57 | 37 | case $_service . "_start": |
| 58 | - WTSEC_LIBRARY_WT::changeStatus($status['config_id'], $uid); | |
| 59 | - wtsec_app()->set($_service . '_status', "start"); | |
| 38 | + WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid); | |
| 39 | + wtsec_app()->set($_service.'_status',"start"); | |
| 60 | 40 | break; |
| 61 | 41 | case $_service . "_stop": |
| 62 | -// WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid); | |
| 63 | - wtsec_app()->set($_service . '_status', "stop"); | |
| 42 | + WTSEC_LIBRARY_WT::changeStatus($status['config_id'],$uid); | |
| 43 | + wtsec_app()->set($_service.'_status',"stop"); | |
| 64 | 44 | break; |
| 65 | 45 | case $_service . "_connect": |
| 66 | 46 | $file = wtsec_getInstalledFile($_service); |
| 67 | 47 | if ($file) { |
| 68 | 48 | wtsec_serviceConnect($uid, $service, $domain . '/' . $file); |
| 69 | - } else { | |
| 70 | - WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service])); | |
| 49 | + }else{ | |
| 50 | + WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('file_not_found', ['service' => $service])); | |
| 71 | 51 | } |
| 72 | 52 | break; |
| 73 | 53 | case $_service . "_uninstall": |
| 74 | 54 | try { |
| 75 | 55 | $file = wtsec_getInstalledFile($_service); |
| 76 | - if ($service === "WAF") { | |
| 56 | + if($service === "WAF"){ | |
| 77 | 57 | $plugin_path = str_replace(ABSPATH, $wp_filesystem->abspath(), WTSEC_INSTALLATION_DIR); |
| 78 | 58 | $mainDir = $plugin_path; |
| 79 | - } else { | |
| 59 | + }else{ | |
| 80 | 60 | $mainDir = $wp_filesystem->abspath(); |
| 81 | 61 | } |
| 82 | 62 | $target_dir = $wp_filesystem->find_folder($mainDir); |
| 83 | - $target_file = trailingslashit($mainDir) . $file; | |
| 63 | + $target_file = trailingslashit($mainDir).$file; | |
| 84 | 64 | if (!$wp_filesystem->delete($target_file)) { |
| 85 | - WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service, 'directory' => $target_dir])); | |
| 65 | + WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('could_not_uninstall_file', ['service' => $service,'directory' => $target_dir])); | |
| 86 | 66 | } |
| 87 | 67 | wtsec_app()->set($_service . '_status', "uninstalled"); |
| 88 | 68 | WTSEC_LIBRARY_App::deleteOption($_service . '_installed_file'); |
| 89 | 69 | } catch (Exception $e) { |
| 90 | - WTSEC_LIBRARY_Session::setNotification("error", $e->getMessage()); | |
| 91 | - return false; | |
| 70 | + print_r($e->getMessage()); | |
| 71 | + die(); | |
| 92 | 72 | } |
| 93 | 73 | break; |
| 94 | 74 | } |
| 95 | - return true; | |
| 96 | 75 | } |
| 97 | 76 | |
| 98 | 77 | function wtsec_checkStatus($id, $service) |
| 99 | 78 | { |
| 100 | - $service = ucfirst(strtolower($service)); | |
| 101 | - $is_active = null; | |
| 102 | - $config_id = null; | |
| 103 | - $result = WTSEC_LIBRARY_WT::checkStatus($id, $service); | |
| 104 | - $data = $result['data']['auth']['viewer']['sites']['one']['configs']; | |
| 105 | - foreach ($data as &$cfg) { | |
| 106 | - if (!empty($cfg)) { | |
| 107 | - $is_active = $cfg['isActive']; | |
| 108 | - $config_id = $cfg['id']; | |
| 109 | - } | |
| 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); | |
| 110 | 86 | } |
| 87 | + return ['active' => $is_active, 'status' => $status, 'config_id' => $config_id]; | |
| 88 | +} | |
| 111 | 89 | |
| 112 | - if ($is_active === false) { | |
| 113 | - wtsec_app()->set($service . '_connected_' . $id, true); | |
| 114 | - } | |
| 115 | - return ['active' => $is_active, 'config_id' => $config_id]; | |
| 116 | -} | |
| 117 | 90 | |
| 91 | + | |
| 118 | 92 | function wtsec_generateFile($id, $service) |
| 119 | 93 | { |
| 120 | - if ($service === "AM") { | |
| 121 | - $result = WTSEC_LIBRARY_WT::generateAmFile($id); | |
| 122 | - if (!isset($result['data']['auth']['am']['install'])) { | |
| 123 | - $file = ['filename' => null, "body" => null]; | |
| 124 | - } else { | |
| 125 | - $url = $result['data']['auth']['am']['install']; | |
| 126 | - $file = WTSEC_LIBRARY_WT::getFileByUrl($url); | |
| 127 | - if (empty($file)) { | |
| 128 | - $file = WTSEC_LIBRARY_WT::getFileByUrl($url); | |
| 129 | - } | |
| 130 | - } | |
| 131 | - | |
| 132 | - $name = $file["filename"]; | |
| 133 | - $file = $file["body"]; | |
| 134 | - } else { | |
| 135 | - $result = WTSEC_LIBRARY_WT::generateFile($id, $service); | |
| 136 | - $name = $result['data']['auth']['agents']['generate']['agentName']; | |
| 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)){ | |
| 137 | 98 | $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name); |
| 138 | - if (empty($file)) { | |
| 139 | - $file = WTSEC_LIBRARY_WT::requestURL(WTSEC_FILE_URL . '/' . $name); | |
| 140 | - } | |
| 141 | 99 | } |
| 142 | - | |
| 143 | 100 | return ['file' => $file, 'name' => $name]; |
| 144 | 101 | } |
| 145 | 102 | |
| 146 | -function wtsec_serviceConnect($id, $service, $domain = '') | |
| 103 | +function wtsec_serviceConnect($id, $service,$domain = '') | |
| 147 | 104 | { |
| 148 | - $result = WTSEC_LIBRARY_WT::serviceConnect($id, $service); | |
| 149 | - $status = isset($result['errors']) && !isset($result['data']['auth']['agents']['check']) ? false : true; | |
| 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; | |
| 150 | 107 | if (!$status) { |
| 151 | - WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain])); | |
| 108 | + WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); | |
| 152 | 109 | } |
| 153 | 110 | return $status; |
| 154 | 111 | } |
| 155 | 112 | |
| 156 | 113 | |
| 157 | -function wtsec_servicePing($service, $domain = '') | |
| 114 | +function wtsec_servicePing($service,$domain = '') | |
| 158 | 115 | { |
| 159 | - if ($domain == NULL) return false; | |
| 160 | - if (stripos($domain, "http") === false) { | |
| 161 | - $domain = "http://" . $domain; | |
| 116 | + if($domain == NULL) return false; | |
| 117 | + if(mb_stripos($domain,"http") === false){ | |
| 118 | + $domain = "http://".$domain; | |
| 162 | 119 | } |
| 163 | 120 | $args = [ |
| 164 | 121 | 'timeout' => '10', |
| 165 | 122 | 'sslverify' => false, |
| @@ -164,71 +121,78 @@ | ||
| 164 | 121 | 'timeout' => '10', |
| 165 | 122 | 'sslverify' => false, |
| 166 | 123 | 'redirection' => 3, |
| 167 | 124 | ]; |
| 168 | - $response = wp_remote_get($domain, $args); | |
| 125 | + $response = wp_remote_get($domain,$args); | |
| 169 | 126 | $httpcode = wp_remote_retrieve_response_code($response); |
| 170 | - if ($httpcode >= 200 && $httpcode < 400) { | |
| 127 | + if($httpcode>=200 && $httpcode<400){ | |
| 171 | 128 | return true; |
| 172 | 129 | } else { |
| 173 | - WTSEC_LIBRARY_Session::setNotification("warning", WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service, 'site' => $domain])); | |
| 130 | + WTSEC_LIBRARY_Session::setNotification("warning",WTSEC_LIBRARY_Localization::lmsg('unable_to_connect_to_service', ['service' => $service,'site' => $domain])); | |
| 174 | 131 | return false; |
| 175 | 132 | } |
| 176 | 133 | } |
| 177 | 134 | |
| 178 | -function wtsec_button($label = '', $class = '') | |
| 179 | -{ | |
| 180 | - return '<button class="ww-button ' . $class . '">' . $label . '</button>'; | |
| 135 | +function wtsec_button($label = '',$class = ''){ | |
| 136 | + return '<button class="ww-button '.$class.'">'.$label.'</button>'; | |
| 181 | 137 | } |
| 182 | 138 | |
| 183 | -function wtsec_main_button($label = '', $class = '') | |
| 184 | -{ | |
| 185 | - return '<button class="ww-button ' . $class . '">' . $label . '</button>'; | |
| 139 | +function wtsec_main_button($label = '',$class = ''){ | |
| 140 | + return '<button class="ww-button '.$class.'">'.$label.'</button>'; | |
| 186 | 141 | } |
| 187 | 142 | |
| 188 | -function wtsec_generateButtons($uid, $_service, $service, $status, $domain = '') | |
| 143 | +function wtsec_generateButtons($uid,$_service, $service, $status,$domain = '') | |
| 189 | 144 | { |
| 190 | 145 | $url = esc_url(admin_url('admin-post.php')); |
| 191 | 146 | $buttons = []; |
| 192 | 147 | |
| 193 | - $form = '<form action="' . $url . '" method="post" style="display:inline-block;"> | |
| 194 | - <input type="hidden" name="service" value="' . $_service . '"> | |
| 148 | + $form = '<form action="'.$url.'" method="post" style="display:inline-block;"> | |
| 149 | + <input type="hidden" name="service" value="'.$_service.'"> | |
| 195 | 150 | <input type="hidden" name="action" value="ajax_cmd"> |
| 196 | - <input type="hidden" name="uid" value="' . $uid . '"> | |
| 151 | + <input type="hidden" name="uid" value="'.$uid.'"> | |
| 197 | 152 | <input type="hidden" name="cmd" value="{{{cmd}}}">{{{button}}}</form>'; |
| 198 | 153 | $result = []; |
| 199 | 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 | + } | |
| 200 | 162 | $disable_uninstall = false; |
| 201 | - $first_class = "ww-button--block "; | |
| 202 | - | |
| 203 | - if ($service === "AM") { | |
| 204 | - if (!$installedFile['status']) { | |
| 205 | - $cmd = $_service . '_install'; | |
| 206 | - $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd]; | |
| 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]; | |
| 207 | 177 | } else { |
| 208 | -// $url = $domain.'/'.$installedFile['file']; | |
| 209 | -// $connected = wtsec_servicePing($service,$url); | |
| 210 | - if (!$disable_uninstall) { | |
| 211 | - $cmd = $_service . '_uninstall'; | |
| 212 | - $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('uninstall'), $first_class . "ww-button--attention"), 'cmd' => $cmd]; | |
| 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]; | |
| 213 | 184 | } |
| 214 | 185 | } |
| 215 | - } else { | |
| 216 | - if ($service === "WAF") { | |
| 217 | - $first_class = ""; | |
| 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]; | |
| 218 | 189 | } |
| 219 | - | |
| 220 | - if ($status === "not_installed") { | |
| 221 | - $cmd = $_service . '_install'; | |
| 222 | - $buttons[] = ['place' => 'first', 'button' => wtsec_button(WTSEC_LIBRARY_Localization::lmsg('install'), $first_class . "ww-button--success"), 'cmd' => $cmd]; | |
| 223 | - } | |
| 224 | 190 | } |
| 225 | - | |
| 226 | - | |
| 227 | - foreach ($buttons as $btn) { | |
| 191 | + foreach ($buttons as $btn){ | |
| 228 | 192 | $newform = $form; |
| 229 | - $newform = str_replace("{{{cmd}}}", $btn['cmd'], $newform); | |
| 230 | - $newform = str_replace("{{{button}}}", $btn['button'], $newform); | |
| 193 | + $newform = str_replace("{{{cmd}}}",$btn['cmd'],$newform); | |
| 194 | + $newform = str_replace("{{{button}}}",$btn['button'],$newform); | |
| 231 | 195 | $result[$btn['place']] = $newform; |
| 232 | 196 | } |
| 233 | 197 | return $result; |
| 234 | 198 | } |
| @@ -235,16 +199,10 @@ | ||
| 235 | 199 | |
| 236 | 200 | function wtsec_checkInstalledFile($service) |
| 237 | 201 | { |
| 238 | 202 | $file = wtsec_getInstalledFile($service); |
| 239 | - if ($service == "waf") { | |
| 240 | - $root = WTSEC_INSTALLATION_DIR; | |
| 241 | - } else { | |
| 242 | - $root = ABSPATH; | |
| 243 | - } | |
| 244 | - return ['status' => ((bool)$file) && is_file($root . $file), 'file' => $file]; | |
| 203 | + return ['status' => (bool) $file,'file' => $file]; | |
| 245 | 204 | } |
| 246 | 205 | |
| 247 | -function wtsec_getInstalledFile($service) | |
| 248 | -{ | |
| 249 | - return wtsec_app()->get($service . "_installed_file"); | |
| 206 | +function wtsec_getInstalledFile($service){ | |
| 207 | + return wtsec_app()->get($service."_installed_file"); | |
| 250 | 208 | } |