PluginProbe
WebTotem Security / 2.4.4
WebTotem Security v2.4.4
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.4, at lib/AgentManager.php

260 lines 6.4 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($files['amFilename']);
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);
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 ( ! isset( $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 foreach ( $list as $item ) {
140
141 $target_item = ABSPATH . $item['name'];
142
143 // Check whether the item is a directory.
144 $recursive = ( $item['type'] == 'd' ) ? true : false;
145
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})/';
147
148 if ( preg_match( $pattern, $target_item ) ) {
149 $wp_filesystem->delete( $target_item, $recursive, $item['type'] );
150 }
151 }
152 }
153
154 return TRUE;
155 }
156
157 /**
158 * Base WordPress Filesystem class which Filesystem implementations extend.
159 *
160 * @return object|bool
161 * Instance of Filesystem class.
162 */
163 private static function wpFileSystem() {
164 global $wp_filesystem;
165
166 if ( empty( $wp_filesystem ) ) {
167 require_once( ABSPATH . '/wp-admin/includes/file.php' );
168 WP_Filesystem();
169 }
170
171 if (empty($wp_filesystem)) {
172 WebTotemOption::setNotification('error', _('WP FileSystem path error'));
173 return FALSE;
174 }
175
176 return $wp_filesystem;
177 }
178
179 /**
180 * @param $url
181 * Link from where to download the file.
182 * @param $path
183 * Path where to save the file.
184 *
185 * @return bool
186 * If the file is saved successfully, it returns true.
187 */
188 private static function downloadFile($download_url, $path) {
189
190 $args = [
191 'timeout' => '30',
192 'sslverify' => FALSE,
193 ];
194
195 $response = wp_remote_get($download_url, $args);
196 $http_code = wp_remote_retrieve_response_code($response);
197
198 if ($http_code < 200) {
199 WebTotemOption::setNotification('error', __( 'Could not download file.', 'wtotem' ));
200 return FALSE;
201 }
202
203 $response_body = wp_remote_retrieve_body($response);
204
205 if($wp_filesystem = self::wpFileSystem()){
206 if(!empty($response_body)){
207 return $wp_filesystem->put_contents($path, $response_body, FS_CHMOD_FILE);
208 } else {
209 $message = __( 'API: Response body is empty.', 'wtotem' );
210 WebTotemOption::setNotification( 'error', $message );
211 }
212 }
213 return FALSE;
214
215 }
216
217 /**
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
222 */
223 public static function generateMarkerFile($am_filename) {
224
225 if($wp_filesystem = self::wpFileSystem()){
226 $content = '<?php exit(); ?>' . $am_filename;
227
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 );
231 }
232 }
233
234 }
235
236 /**
237 * Check if the plugin version has changed.
238 */
239 public static function checkVersion(){
240 // Get version of the plugin that was previously installed.
241 $version = WebTotemOption::getOption('plugin_version');
242
243 if ($version == WEBTOTEM_VERSION) {
244 return;
245 }
246
247 // Checking the old version of options.
248 WebTotemOption::checkOldOptions();
249
250 WebTotemOption::setOptions(['plugin_version' => WEBTOTEM_VERSION]);
251
252 // Generate the file that indicates that a WAF connection is being used through the plugin.
253 if($am_file = WebTotemOption::getOption('am_file')){
254 self::generateMarkerFile($am_file);
255 }
256
257 }
258
259 }
260