| @@ -33,38 +33,32 @@ | ||
| 33 | 33 | $host = WebTotemAPI::siteInfo(); |
| 34 | 34 | |
| 35 | 35 | $files = self::getAgentsFiles( $host['id'] ); |
| 36 | 36 | |
| 37 | - if ( isset($files['am_filename']) and $files['am_filename'] ) { | |
| 37 | + if ( $files['amFilename'] ) { | |
| 38 | 38 | |
| 39 | - if (!is_writable(ABSPATH)) { | |
| 40 | - WebTotemOption::setNotification('error', __('There are no permissions to write to the root directory', 'wtotem')); | |
| 41 | - return FALSE; | |
| 42 | - } | |
| 43 | - | |
| 44 | 39 | // Download file. |
| 45 | 40 | $result = self::downloadFile( |
| 46 | - $files['download_link'], | |
| 47 | - ABSPATH . $files['am_filename'] | |
| 41 | + $files['downloadLink'], | |
| 42 | + ABSPATH . $files['amFilename'] | |
| 48 | 43 | ); |
| 49 | 44 | |
| 50 | 45 | // If the file is downloaded, then we write the data to the DB. |
| 51 | 46 | if ( $result ) { |
| 52 | 47 | WebTotemOption::setOptions( [ |
| 53 | - 'am_installed' => true, | |
| 54 | - 'am_file' => $files['am_filename'], | |
| 55 | - 'waf_file' => $files['waf_filename'], | |
| 56 | - 'av_file' => $files['av_filename'], | |
| 57 | - 'am_created_at' => $files['created_at'], | |
| 48 | + 'am_installed' => true, | |
| 49 | + 'am_file' => $files['amFilename'], | |
| 50 | + 'waf_file' => $files['wafFilename'], | |
| 51 | + 'av_file' => $files['avFilename'], | |
| 58 | 52 | ] ); |
| 59 | 53 | |
| 60 | - self::generateMarkerFile(); | |
| 54 | + self::generateMarkerFile($files['amFilename']); | |
| 61 | 55 | |
| 62 | 56 | $message = __( 'Agent manager have been successfully installed', 'wtotem'); |
| 63 | 57 | WebTotemOption::setNotification( 'success', $message ); |
| 64 | 58 | } |
| 65 | 59 | else { |
| 66 | - $message = sprintf(__( 'Check %s folder\'s write permission.', 'wtotem' ), ABSPATH) . sprintf(__(' Read more <a href="%s" target="_blank">here</a>.', 'wtotem' ), 'https://docs.wtotem.com/agent-setup#it-additional-recommendations'); | |
| 60 | + $message = sprintf(__( 'Check %s folder\'s write permission.', 'wtotem' ), ABSPATH); | |
| 67 | 61 | WebTotemOption::setNotification( 'error', $message ); |
| 68 | 62 | |
| 69 | 63 | return FALSE; |
| 70 | 64 | } |
| @@ -69,11 +63,8 @@ | ||
| 69 | 63 | return FALSE; |
| 70 | 64 | } |
| 71 | 65 | } |
| 72 | 66 | |
| 73 | - // Update config_id. | |
| 74 | - WebTotemAPI::getHostID($host['name']); | |
| 75 | - | |
| 76 | 67 | } catch ( \Exception $e ) { |
| 77 | 68 | WebTotemOption::setNotification( 'error', $e->getMessage() ); |
| 78 | 69 | |
| 79 | 70 | return FALSE; |
| @@ -92,10 +83,10 @@ | ||
| 92 | 83 | * Agent Manager download link, names of agents |
| 93 | 84 | */ |
| 94 | 85 | public static function getAgentsFiles( $host_id ) { |
| 95 | 86 | $result = WebTotemAPI::getAgentsFiles( $host_id ); |
| 96 | - if ( ! $result ) { | |
| 97 | - $file = [ 'download_link' => NULL ]; | |
| 87 | + if ( ! isset( $result ) ) { | |
| 88 | + $file = [ 'amFilename' => NULL, 'downloadLink' => NULL ]; | |
| 98 | 89 | |
| 99 | 90 | $message = __( 'Error generating the agent manager file.', 'wtotem' ); |
| 100 | 91 | WebTotemOption::setNotification( 'error', $message ); |
| 101 | 92 | } else { |
| @@ -141,20 +132,11 @@ | ||
| 141 | 132 | 'waf_file', |
| 142 | 133 | 'av_file', |
| 143 | 134 | ] ); |
| 144 | 135 | |
| 145 | - self::postdelete(); | |
| 146 | - | |
| 147 | 136 | if($wp_filesystem = self::wpFileSystem()){ |
| 148 | 137 | $list = $wp_filesystem->dirlist( ABSPATH ); |
| 149 | 138 | |
| 150 | - $uploads_list = $wp_filesystem->dirlist( ABSPATH .'wp-content/uploads' ) ?: []; | |
| 151 | - foreach ($uploads_list as $key => $item){ | |
| 152 | - $uploads_list[$key]['name'] = 'wp-content/uploads/' . $item['name']; | |
| 153 | - } | |
| 154 | - | |
| 155 | - $list = array_merge($list, $uploads_list); | |
| 156 | - | |
| 157 | 139 | foreach ( $list as $item ) { |
| 158 | 140 | |
| 159 | 141 | $target_item = ABSPATH . $item['name']; |
| 160 | 142 | |
| @@ -160,9 +142,9 @@ | ||
| 160 | 142 | |
| 161 | 143 | // Check whether the item is a directory. |
| 162 | 144 | $recursive = ( $item['type'] == 'd' ) ? true : false; |
| 163 | 145 | |
| 164 | - $pattern = '/([a-zA-Z0-9_]{64}.av.php)|([a-zA-Z0-9_]{64}.am.php)|([a-zA-Z0-9_]{64}.waf.php)|(\.wtotem_[a-zA-Z0-9_]{12,16})/'; | |
| 146 | + $pattern = '/([a-zA-Z0-9_]{64}.av.php)|([a-zA-Z0-9_]{64}.am.php)|([a-zA-Z0-9_]{64}.waf.php)|(wtotem_[a-zA-Z0-9_]{12,16})/'; | |
| 165 | 147 | |
| 166 | 148 | if ( preg_match( $pattern, $target_item ) ) { |
| 167 | 149 | $wp_filesystem->delete( $target_item, $recursive, $item['type'] ); |
| 168 | 150 | } |
| @@ -171,69 +153,8 @@ | ||
| 171 | 153 | |
| 172 | 154 | return TRUE; |
| 173 | 155 | } |
| 174 | 156 | |
| 175 | - /** | |
| 176 | - * This method clears the system file from the WAF connection strings. | |
| 177 | - * | |
| 178 | - * @return bool | |
| 179 | - */ | |
| 180 | - public static function postdelete(): bool | |
| 181 | - { | |
| 182 | - $base_path = ABSPATH; | |
| 183 | - $targets = [ | |
| 184 | - 'default' => $base_path . 'index.php', | |
| 185 | - 'wp' => $base_path . 'wp-load.php', | |
| 186 | - ]; | |
| 187 | - | |
| 188 | - foreach ($targets as $target_path) { | |
| 189 | - self::cut_inc($target_path); | |
| 190 | - } | |
| 191 | - | |
| 192 | - return true; | |
| 193 | - } | |
| 194 | - | |
| 195 | - /** | |
| 196 | - * This method clears the system file from the WAF connection strings. | |
| 197 | - * | |
| 198 | - * @return string | |
| 199 | - */ | |
| 200 | - private static function cut_inc(string $target_path) | |
| 201 | - { | |
| 202 | - if (file_exists($target_path)) { | |
| 203 | - $reg = '/^([\r\n\t])*((<\?php\s)?if\s?\(function_exists\(\'current_user_can\'\)\)\s?{\s?if\s?\(\s?!current_user_can\(\'publish_posts\'\)\s?\)\s?{\s)?(<\?php\s?)?\$wtwaf\s?=\s?dirname\(__FILE__\).{76,77}\.waf\.php(\'|\")?;\s?if\s?\(file_exists\(\$wtwaf\)(\s&&\sis_readable\(\$wtwaf\))?\)\s?{(\s?if\s?\(function_exists\("is_admin"\)\)\s?{\s?if\s?\(!is_admin\(\)\)\s?{)?\s?@include_once\(\$wtwaf\);\s?}(\s?}\s?else\s?{\s?@include_once\(\$wtwaf\);\s?}\s?})?\s?unset\(\$wtwaf\);\s?(\?>|}\s})?([\r\n\t])*/im'; | |
| 204 | - $reg2 = '/(\?>)?(\s*(<\?php)?\s+if\s?\(\s*PHP_VERSION_ID\s*>\s*70000\s*\)\s*{\s*\$wtwaf\s*=\s*__DIR__\s*\.\s*\'(\/\.\.\/\.\.)?\/_include_\w{64}\.waf\.php\'\s*;\s*if\s*\(\s*file_exists\s*\(\s*\$wtwaf\s*\)\s*\)\s*{\s*@\s*include_once\s*\(\s*\$wtwaf\s*\)\s*;\s*}\s*unset\s*\(\s*\$wtwaf\s*\)\s*;\s*}\s*(\?>)?\s*)(<\?php)?/im'; | |
| 205 | - $target_content = file_get_contents($target_path); | |
| 206 | - $pos_inc = stripos($target_content, '@include_once($wtwaf);'); | |
| 207 | - if ($pos_inc !== false) { | |
| 208 | - $cutted = preg_replace($reg, '', $target_content); | |
| 209 | - if (is_string($cutted) && $cutted !== '') { | |
| 210 | - if (preg_match($reg2, $cutted, $reg2_matches)) { | |
| 211 | - if (!empty($reg2_matches[1]) && !empty($reg2_matches[5])) { | |
| 212 | - $cutted = str_replace($reg2_matches[0], '', $cutted); | |
| 213 | - } else { | |
| 214 | - $cutted = str_replace($reg2_matches[2], '', $cutted); | |
| 215 | - } | |
| 216 | - } | |
| 217 | - | |
| 218 | - if (is_string($cutted) && $cutted !== '') { | |
| 219 | - $wp_filesystem = self::wpFileSystem(); | |
| 220 | - $res = $wp_filesystem->put_contents($target_path, $cutted, FS_CHMOD_FILE); | |
| 221 | - } else { | |
| 222 | - $res = 'preg_replace error 2'; | |
| 223 | - } | |
| 224 | - } else { | |
| 225 | - $res = 'preg_replace error 1'; | |
| 226 | - } | |
| 227 | - } else { | |
| 228 | - $res = 'inc not found'; | |
| 229 | - } | |
| 230 | - } else { | |
| 231 | - $res = 'not found'; | |
| 232 | - } | |
| 233 | - return $res; | |
| 234 | - } | |
| 235 | - | |
| 236 | 157 | /** |
| 237 | 158 | * Base WordPress Filesystem class which Filesystem implementations extend. |
| 238 | 159 | * |
| 239 | 160 | * @return object|bool |
| @@ -242,9 +163,9 @@ | ||
| 242 | 163 | private static function wpFileSystem() { |
| 243 | 164 | global $wp_filesystem; |
| 244 | 165 | |
| 245 | 166 | if ( empty( $wp_filesystem ) ) { |
| 246 | - require_once( ABSPATH . 'wp-admin/includes/file.php' ); | |
| 167 | + require_once( ABSPATH . '/wp-admin/includes/file.php' ); | |
| 247 | 168 | WP_Filesystem(); |
| 248 | 169 | } |
| 249 | 170 | |
| 250 | 171 | if (empty($wp_filesystem)) { |
| @@ -255,9 +176,9 @@ | ||
| 255 | 176 | return $wp_filesystem; |
| 256 | 177 | } |
| 257 | 178 | |
| 258 | 179 | /** |
| 259 | - * @param $download_url | |
| 180 | + * @param $url | |
| 260 | 181 | * Link from where to download the file. |
| 261 | 182 | * @param $path |
| 262 | 183 | * Path where to save the file. |
| 263 | 184 | * |
| @@ -270,12 +191,8 @@ | ||
| 270 | 191 | 'timeout' => '30', |
| 271 | 192 | 'sslverify' => FALSE, |
| 272 | 193 | ]; |
| 273 | 194 | |
| 274 | -// if(WebTotemOption::getOption('api_environment') == 'M'){ | |
| 275 | -// $download_url = str_replace("wtotem.com", "wtotem.net", $download_url); | |
| 276 | -// } | |
| 277 | - | |
| 278 | 195 | $response = wp_remote_get($download_url, $args); |
| 279 | 196 | $http_code = wp_remote_retrieve_response_code($response); |
| 280 | 197 | |
| 281 | 198 | if ($http_code < 200) { |
| @@ -298,61 +215,41 @@ | ||
| 298 | 215 | } |
| 299 | 216 | |
| 300 | 217 | /** |
| 301 | 218 | * Generate the file that indicates that a WAF connection is being used through the plugin. |
| 219 | + * | |
| 220 | + * @param $am_filename | |
| 221 | + * AM file name | |
| 302 | 222 | */ |
| 303 | - public static function generateMarkerFile() { | |
| 304 | - if($am_filename = WebTotemOption::getOption('am_file')) { | |
| 305 | - if ( $wp_filesystem = self::wpFileSystem() ) { | |
| 306 | - $content = '<?php exit(); ?>' . $am_filename; | |
| 307 | - $file_path = WEBTOTEM_PLUGIN_PATH . '/generate.php'; | |
| 308 | - if ( ! file_exists($file_path) or $wp_filesystem->get_contents($file_path) != $content) { | |
| 223 | + public static function generateMarkerFile($am_filename) { | |
| 309 | 224 | |
| 310 | - if ( ! $wp_filesystem->put_contents( $file_path, $content, FS_CHMOD_FILE ) ) { | |
| 311 | - $message = sprintf( __( 'Check %s folder\'s write permission.', 'wtotem' ), WEBTOTEM_PLUGIN_PATH ) . sprintf( __( ' Read more <a href="%s" target="_blank">here</a>.', 'wtotem' ), 'https://docs.wtotem.com/agent-setup#it-additional-recommendations' ); | |
| 312 | - WebTotemOption::setNotification( 'warning', $message ); | |
| 313 | - } | |
| 225 | + if($wp_filesystem = self::wpFileSystem()){ | |
| 226 | + $content = '<?php exit(); ?>' . $am_filename; | |
| 314 | 227 | |
| 315 | - } | |
| 228 | + if ( !$wp_filesystem->put_contents(WEBTOTEM_PLUGIN_PATH . '/generate.php', $content, FS_CHMOD_FILE) ) { | |
| 229 | + $message = sprintf(__( 'Check %s folder\'s write permission.', 'wtotem' ), WEBTOTEM_PLUGIN_PATH); | |
| 230 | + WebTotemOption::setNotification( 'error', $message ); | |
| 316 | 231 | } |
| 317 | 232 | } |
| 233 | + | |
| 318 | 234 | } |
| 319 | 235 | |
| 320 | - /** | |
| 321 | - * WAF Include. | |
| 322 | - */ | |
| 323 | - public static function wafInclude(){ | |
| 324 | - if(WebTotemOption::isActivated()){ | |
| 325 | - $sapi = @php_sapi_name(); | |
| 326 | - if( $sapi != "cli" ) { | |
| 327 | - if ($waf = WebTotemOption::getOption("waf_file")) { | |
| 328 | - $include_waf_file = ABSPATH . '_include_' . $waf; | |
| 329 | - | |
| 330 | - if (is_file($include_waf_file) && is_readable($include_waf_file)) { | |
| 331 | - include_once $include_waf_file; | |
| 332 | - } | |
| 333 | - } | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - } | |
| 338 | - | |
| 339 | 236 | /** |
| 340 | 237 | * Check if the plugin version has changed. |
| 341 | 238 | */ |
| 342 | 239 | public static function checkVersion(){ |
| 343 | - if(WebTotemOption::isActivated()){ | |
| 344 | - // Get version of the plugin that was previously installed. | |
| 345 | - $version = WebTotemOption::getOption('plugin_version'); | |
| 240 | + // Get version of the plugin that was previously installed. | |
| 241 | + $version = WebTotemOption::getOption('plugin_version'); | |
| 346 | 242 | |
| 347 | - if ($version == WEBTOTEM_VERSION) { | |
| 348 | - return; | |
| 349 | - } | |
| 243 | + if ($version == WEBTOTEM_VERSION) { | |
| 244 | + return; | |
| 245 | + } | |
| 350 | 246 | |
| 351 | - WebTotemOption::setOptions(['plugin_version' => WEBTOTEM_VERSION]); | |
| 247 | + WebTotemOption::setOptions(['plugin_version' => WEBTOTEM_VERSION]); | |
| 352 | 248 | |
| 353 | - // Generate the file that indicates that a WAF connection is being used through the plugin. | |
| 354 | - self::generateMarkerFile(); | |
| 249 | + // Generate the file that indicates that a WAF connection is being used through the plugin. | |
| 250 | + if($am_file = WebTotemOption::getOption('am_file')){ | |
| 251 | + self::generateMarkerFile($am_file); | |
| 355 | 252 | } |
| 356 | 253 | |
| 357 | 254 | } |
| 358 | 255 | |