PluginProbe
WebTotem Security / 2.4.21
WebTotem Security v2.4.21
3.0.1 3.0.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 2.0 2.1 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.1 2.2.2 2.2.3 2.2.4 All 109 releases
wt-security / lib / AgentManager.php

AgentManager.php in WebTotem Security 2.4.21, at lib/AgentManager.php

265 lines 7.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('WEBTOTEM_INIT') || WEBTOTEM_INIT !== true) {
4 if (!headers_sent()) {
5 header('HTTP/1.1 403 Forbidden');
6 }
7 die("Protected By WebTotem!");
8 }
9
10 /**
11 * Agent Manager library.
12 *
13 * Agent Manager is needed to create AM file,
14 * and to check whether am and other agents are installed.
15 * The AM file creates WAV and AV files,
16 * and transmits information to the Web Totem platform and back.
17 */
18 class WebTotemAgentManager extends WebTotem {
19
20 /**
21 * AM file install.
22 *
23 * Create AM file in the root of the site
24 * and save the data about it in the DB.
25 *
26 * @return bool
27 * TRUE if the AM file is successfully added.
28 */
29 public static function amInstall() {
30 try {
31 self::removeAgents();
32
33 $host = WebTotemAPI::siteInfo();
34
35 $files = self::getAgentsFiles( $host['id'] );
36
37 if ( $files['amFilename'] ) {
38
39 // Download file.
40 $result = self::downloadFile(
41 $files['downloadLink'],
42 ABSPATH . $files['amFilename']
43 );
44
45 // If the file is downloaded, then we write the data to the DB.
46 if ( $result ) {
47 WebTotemOption::setOptions( [
48 'am_installed' => true,
49 'am_file' => $files['amFilename'],
50 'waf_file' => $files['wafFilename'],
51 'av_file' => $files['avFilename'],
52 ] );
53
54 self::generateMarkerFile();
55
56 $message = __( 'Agent manager have been successfully installed', 'wtotem');
57 WebTotemOption::setNotification( 'success', $message );
58 }
59 else {
60 $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');
61 WebTotemOption::setNotification( 'error', $message );
62
63 return FALSE;
64 }
65 }
66
67 } catch ( \Exception $e ) {
68 WebTotemOption::setNotification( 'error', $e->getMessage() );
69
70 return FALSE;
71 }
72
73 return TRUE;
74 }
75
76 /**
77 * Get the AM file download link and agents (AV, WAF) files name.
78 *
79 * @param string $host_id
80 * Host id on WebTotem.
81 *
82 * @return array
83 * Agent Manager download link, names of agents
84 */
85 public static function getAgentsFiles( $host_id ) {
86 $result = WebTotemAPI::getAgentsFiles( $host_id );
87 if ( ! $result ) {
88 $file = [ 'amFilename' => NULL, 'downloadLink' => NULL ];
89
90 $message = __( 'Error generating the agent manager file.', 'wtotem' );
91 WebTotemOption::setNotification( 'error', $message );
92 } else {
93 $file = $result;
94 }
95
96 return $file;
97 }
98
99 /**
100 * Check the existence of the service file and the record in the DB.
101 *
102 * @param string $service
103 * Service short name (am, av, waf).
104 *
105 * @return array
106 * Service file data (installed status, file exist status, file name).
107 */
108 public static function checkInstalledService( $service ) {
109 $file = WebTotemOption::getOption( $service . "_file" );
110
111 return [
112 'option_status' => WebTotemOption::getOption( $service . "_installed" ),
113 'file_status' => ( (bool) $file ) && is_file( ABSPATH . $file ),
114 'file_name' => $file,
115 ];
116 }
117
118 /**
119 * Remove all agents files and folders. Clear agents options.
120 *
121 * @return bool
122 * Returns TRUE if agent files
123 * and folders have been successfully deleted.
124 */
125 public static function removeAgents() {
126 // Deleting all agent records from the database.
127 WebTotemOption::clearOptions( [
128 'am_installed',
129 'waf_installed',
130 'av_installed',
131 'am_file',
132 'waf_file',
133 'av_file',
134 ] );
135
136 if($wp_filesystem = self::wpFileSystem()){
137 $list = $wp_filesystem->dirlist( ABSPATH );
138
139 $uploads_list = $wp_filesystem->dirlist( ABSPATH .'wp-content/uploads' ) ?: [];
140 foreach ($uploads_list as $key => $item){
141 $uploads_list[$key]['name'] = 'wp-content/uploads/' . $item['name'];
142 }
143
144 $list = array_merge($list, $uploads_list);
145
146 foreach ( $list as $item ) {
147
148 $target_item = ABSPATH . $item['name'];
149
150 // Check whether the item is a directory.
151 $recursive = ( $item['type'] == 'd' ) ? true : false;
152
153 $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})/';
154
155 if ( preg_match( $pattern, $target_item ) ) {
156 $wp_filesystem->delete( $target_item, $recursive, $item['type'] );
157 }
158 }
159 }
160
161 return TRUE;
162 }
163
164 /**
165 * Base WordPress Filesystem class which Filesystem implementations extend.
166 *
167 * @return object|bool
168 * Instance of Filesystem class.
169 */
170 private static function wpFileSystem() {
171 global $wp_filesystem;
172
173 if ( empty( $wp_filesystem ) ) {
174 require_once( ABSPATH . '/wp-admin/includes/file.php' );
175 WP_Filesystem();
176 }
177
178 if (empty($wp_filesystem)) {
179 WebTotemOption::setNotification('error', _('WP FileSystem path error'));
180 return FALSE;
181 }
182
183 return $wp_filesystem;
184 }
185
186 /**
187 * @param $url
188 * Link from where to download the file.
189 * @param $path
190 * Path where to save the file.
191 *
192 * @return bool
193 * If the file is saved successfully, it returns true.
194 */
195 private static function downloadFile($download_url, $path) {
196
197 $args = [
198 'timeout' => '30',
199 'sslverify' => FALSE,
200 ];
201
202 $response = wp_remote_get($download_url, $args);
203 $http_code = wp_remote_retrieve_response_code($response);
204
205 if ($http_code < 200) {
206 WebTotemOption::setNotification('error', __( 'Could not download file.', 'wtotem' ));
207 return FALSE;
208 }
209
210 $response_body = wp_remote_retrieve_body($response);
211
212 if($wp_filesystem = self::wpFileSystem()){
213 if(!empty($response_body)){
214 return $wp_filesystem->put_contents($path, $response_body, FS_CHMOD_FILE);
215 } else {
216 $message = __( 'API: Response body is empty.', 'wtotem' );
217 WebTotemOption::setNotification( 'error', $message );
218 }
219 }
220 return FALSE;
221
222 }
223
224 /**
225 * Generate the file that indicates that a WAF connection is being used through the plugin.
226 */
227 public static function generateMarkerFile() {
228 if($am_filename = WebTotemOption::getOption('am_file')) {
229 if ( $wp_filesystem = self::wpFileSystem() ) {
230 $content = '<?php exit(); ?>' . $am_filename;
231 $file_path = WEBTOTEM_PLUGIN_PATH . '/generate.php';
232 if ( ! file_exists($file_path) or $wp_filesystem->get_contents($file_path) != $content) {
233
234 if ( ! $wp_filesystem->put_contents( $file_path, $content, FS_CHMOD_FILE ) ) {
235 $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' );
236 WebTotemOption::setNotification( 'error', $message );
237 }
238
239 }
240 }
241 }
242 }
243
244 /**
245 * Check if the plugin version has changed.
246 */
247 public static function checkVersion(){
248 if(WebTotemOption::isActivated()){
249 // Get version of the plugin that was previously installed.
250 $version = WebTotemOption::getOption('plugin_version');
251
252 if ($version == WEBTOTEM_VERSION) {
253 return;
254 }
255
256 WebTotemOption::setOptions(['plugin_version' => WEBTOTEM_VERSION]);
257
258 // Generate the file that indicates that a WAF connection is being used through the plugin.
259 self::generateMarkerFile();
260 }
261
262 }
263
264 }
265