true, 'am_file' => $files['amFilename'], 'waf_file' => $files['wafFilename'], 'av_file' => $files['avFilename'], ] ); self::generateMarkerFile($files['amFilename']); $message = __( 'Agent manager have been successfully installed', 'wtotem'); WebTotemOption::setNotification( 'success', $message ); } else { $message = sprintf(__( 'Check %s folder\'s write permission.', 'wtotem' ), ABSPATH) . sprintf(__(' Read more here.', 'wtotem' ), 'https://docs.wtotem.com/agent-setup#it-additional-recommendations'); WebTotemOption::setNotification( 'error', $message ); return FALSE; } } } catch ( \Exception $e ) { WebTotemOption::setNotification( 'error', $e->getMessage() ); return FALSE; } return TRUE; } /** * Get the AM file download link and agents (AV, WAF) files name. * * @param string $host_id * Host id on WebTotem. * * @return array * Agent Manager download link, names of agents */ public static function getAgentsFiles( $host_id ) { $result = WebTotemAPI::getAgentsFiles( $host_id ); if ( ! isset( $result ) ) { $file = [ 'amFilename' => NULL, 'downloadLink' => NULL ]; $message = __( 'Error generating the agent manager file.', 'wtotem' ); WebTotemOption::setNotification( 'error', $message ); } else { $file = $result; } return $file; } /** * Check the existence of the service file and the record in the DB. * * @param string $service * Service short name (am, av, waf). * * @return array * Service file data (installed status, file exist status, file name). */ public static function checkInstalledService( $service ) { $file = WebTotemOption::getOption( $service . "_file" ); return [ 'option_status' => WebTotemOption::getOption( $service . "_installed" ), 'file_status' => ( (bool) $file ) && is_file( ABSPATH . $file ), 'file_name' => $file, ]; } /** * Remove all agents files and folders. Clear agents options. * * @return bool * Returns TRUE if agent files * and folders have been successfully deleted. */ public static function removeAgents() { // Deleting all agent records from the database. WebTotemOption::clearOptions( [ 'am_installed', 'waf_installed', 'av_installed', 'am_file', 'waf_file', 'av_file', ] ); if($wp_filesystem = self::wpFileSystem()){ $list = $wp_filesystem->dirlist( ABSPATH ); foreach ( $list as $item ) { $target_item = ABSPATH . $item['name']; // Check whether the item is a directory. $recursive = ( $item['type'] == 'd' ) ? true : false; $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})/'; if ( preg_match( $pattern, $target_item ) ) { $wp_filesystem->delete( $target_item, $recursive, $item['type'] ); } } } return TRUE; } /** * Base WordPress Filesystem class which Filesystem implementations extend. * * @return object|bool * Instance of Filesystem class. */ private static function wpFileSystem() { global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once( ABSPATH . '/wp-admin/includes/file.php' ); WP_Filesystem(); } if (empty($wp_filesystem)) { WebTotemOption::setNotification('error', _('WP FileSystem path error')); return FALSE; } return $wp_filesystem; } /** * @param $url * Link from where to download the file. * @param $path * Path where to save the file. * * @return bool * If the file is saved successfully, it returns true. */ private static function downloadFile($download_url, $path) { $args = [ 'timeout' => '30', 'sslverify' => FALSE, ]; $response = wp_remote_get($download_url, $args); $http_code = wp_remote_retrieve_response_code($response); if ($http_code < 200) { WebTotemOption::setNotification('error', __( 'Could not download file.', 'wtotem' )); return FALSE; } $response_body = wp_remote_retrieve_body($response); if($wp_filesystem = self::wpFileSystem()){ if(!empty($response_body)){ return $wp_filesystem->put_contents($path, $response_body, FS_CHMOD_FILE); } else { $message = __( 'API: Response body is empty.', 'wtotem' ); WebTotemOption::setNotification( 'error', $message ); } } return FALSE; } /** * Generate the file that indicates that a WAF connection is being used through the plugin. */ public static function generateMarkerFile() { if($am_filename = WebTotemOption::getOption('am_file')) { if ( $wp_filesystem = self::wpFileSystem() ) { if ( ! file_exists(WEBTOTEM_PLUGIN_PATH . '/generate.php') ) { $content = '' . $am_filename; if ( ! $wp_filesystem->put_contents( WEBTOTEM_PLUGIN_PATH . '/generate.php', $content, FS_CHMOD_FILE ) ) { $message = sprintf( __( 'Check %s folder\'s write permission.', 'wtotem' ), WEBTOTEM_PLUGIN_PATH ) . sprintf( __( ' Read more here.', 'wtotem' ), 'https://docs.wtotem.com/agent-setup#it-additional-recommendations' ); WebTotemOption::setNotification( 'error', $message ); } } } } } /** * Check if the plugin version has changed. */ public static function checkVersion(){ // Get version of the plugin that was previously installed. $version = WebTotemOption::getOption('plugin_version'); if ($version == WEBTOTEM_VERSION) { return; } WebTotemOption::setOptions(['plugin_version' => WEBTOTEM_VERSION]); // TODO добавить проверку на главную или по крону // Generate the file that indicates that a WAF connection is being used through the plugin. self::generateMarkerFile(); } }