PluginProbe
WebTotem Security / 2.4.25
WebTotem Security v2.4.25
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.25, at lib/AgentManager.php

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