PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.12.9
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.12.9
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / includes / core / logger / class-wpfnl-logger.php

class-wpfnl-logger.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.12.9, at includes/core/logger/class-wpfnl-logger.php

244 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Logger
4 *
5 * @package
6 */
7 use WPFunnels\Traits\SingletonTrait;
8
9 class Wpfnl_Logger {
10
11 use SingletonTrait;
12
13
14
15 /**
16 * Log location, URL path.
17 *
18 * @since 2.5.9
19 * @var string
20 */
21 protected $log_location_url = '';
22
23 /**
24 * Log location, server path.
25 *
26 * @since 2.5.9
27 * @var string
28 */
29 protected $log_location_file = '';
30
31 /**
32 * Log directory location, server path.
33 *
34 * @since 2.5.9
35 * @var string
36 */
37 protected $log_location_dir = '';
38
39 /**
40 * The location of the log folder's index file.
41 *
42 * @since 2.5.9
43 * @var string
44 */
45 protected $log_index_file = '';
46
47 /**
48 * The logging directory name.
49 *
50 * @since 2.5.9
51 * @var string
52 */
53 protected $log_file_dir = 'wpfunnels/wpfunnels-logs';
54
55
56 /**
57 * Constructor
58 *
59 * @return void
60 * @since 2.5.9
61 */
62 public function __construct() {
63
64 $uploads_dir = wp_upload_dir();
65 $log_file_name = 'wpfunnels';
66 $this->log_location_url = "{$uploads_dir['baseurl']}/{$this->log_file_dir}/{$log_file_name}";
67 $this->log_location_dir = "{$uploads_dir['basedir']}/{$this->log_file_dir}";
68 $this->log_location_file = "{$this->log_location_dir}/{$log_file_name}";
69 $this->log_index_file = "{$this->log_location_dir}/index.php";
70
71 define('WPFNL_LOG_FILE_DIR', $this->log_location_dir );
72 define('WPFNL_LOG_FILE', $this->log_location_file);
73 }
74
75
76 /**
77 * Create the log folder.
78 *
79 * @since 2.5.9
80 */
81 public function create_log_folder() {
82 wp_mkdir_p( $this->log_location_dir );
83 $this->protect_log_folder();
84 }
85
86
87 /**
88 * Harden the log directory: deny direct web access and prevent directory listing.
89 *
90 * @since 3.12.8
91 */
92 protected function protect_log_folder() {
93 if ( ! is_dir( $this->log_location_dir ) || ! is_writable( $this->log_location_dir ) ) {
94 return;
95 }
96
97 $htaccess = trailingslashit( $this->log_location_dir ) . '.htaccess';
98 if ( ! file_exists( $htaccess ) ) {
99 file_put_contents( $htaccess, "Order deny,allow\nDeny from all\n" );
100 }
101
102 $index = trailingslashit( $this->log_location_dir ) . 'index.php';
103 if ( ! file_exists( $index ) ) {
104 file_put_contents( $index, "<?php\n// Silence is golden.\n" );
105 }
106 }
107
108
109 /**
110 * Create log file.
111 *
112 * @param String $log_location_dir
113 * @param String $file_name
114 *
115 * @since 2.5.9
116 * @return void
117 */
118 public static function create_log_file( $log_location_dir, $file_name ) {
119 if ( ! is_writable( $log_location_dir ) ) {
120 return;
121 }
122
123 if ( file_exists( $file_name ) ) {
124 return;
125 }
126
127 touch( $file_name );
128 }
129
130
131 /**
132 * Retrieve logging file location.
133 *
134 * @return string Logging file location.
135 *
136 * @since 2.5.9
137 */
138 public function get_logging_location() {
139 return $this->log_location_file;
140 }
141
142
143
144
145 /**
146 * Initialize Logging directory
147 *
148 * @since 2.5.9
149 */
150 public function initialize_logging() {
151 $this->create_log_folder();
152 }
153
154
155 /**
156 * Update log file
157 *
158 * @param String $log_type
159 * @param Mix $content
160 * @param String $header_text
161 *
162 * @since 2.5.9
163 */
164 public static function modify_log_file( $log_type, $content, $header_text = '' ){
165
166 if( $log_type && $content && defined( 'WPFNL_LOG_FILE' ) && defined( 'WPFNL_LOG_FILE_DIR' ) ){
167
168 $file_name = WPFNL_LOG_FILE.'-'.trim(strtolower($log_type)).'-log-'.date('Y-m-d').'.log';
169 $time = new \DateTimeImmutable('now', wp_timezone());
170 $current_time = $time->format("h:i A");
171 $header_content = "\nWPFunnels - ".$current_time." - ".$header_text."\n";
172
173 if ( !file_exists( $file_name ) ) {
174 self::create_log_file( WPFNL_LOG_FILE_DIR, $file_name );
175 }
176
177 if ( ! is_writable( $file_name ) ) {
178 return;
179 }
180
181 // Neutralise any PHP open tags in attacker-controlled content so the
182 // log file can never be executed if it is ever included by PHP.
183 $header_content = self::sanitize_log_content( $header_content );
184 $content = is_scalar( $content ) ? self::sanitize_log_content( (string) $content ) : '';
185
186 file_put_contents ($file_name, $header_content, FILE_APPEND );
187 file_put_contents ($file_name, $content, FILE_APPEND );
188
189 }
190 }
191
192
193 /**
194 * Strip PHP open tags from log content to prevent code execution.
195 *
196 * @param String $content
197 *
198 * @return String
199 * @since 3.12.8
200 */
201 protected static function sanitize_log_content( $content ) {
202 return str_ireplace( array( '<?php', '<?=', '<?' ), '', $content );
203 }
204
205
206 /**
207 * Delete existing log files.
208 *
209 * @since 2.5.9
210 *
211 * @return Bool
212 */
213 public static function delete_log_file( $file_name ) {
214 if ( file_exists( $file_name ) ) {
215 unlink( $file_name );
216 return true;
217 }
218 return false;
219 }
220
221
222 /**
223 * Get all log files in the log directory.
224 *
225 * @since 2.5.9
226 * @return array
227 */
228 public static function get_log_files() {
229 $files = @scandir( WPFNL_LOG_FILE_DIR ); // @codingStandardsIgnoreLine.
230 $result = array();
231 if ( ! empty( $files ) ) {
232 foreach ( $files as $key => $value ) {
233 if ( ! in_array( $value, array( '.', '..' ), true ) ) {
234 if ( ! is_dir( $value ) && strstr( $value, '.log' ) ) {
235 $result[ sanitize_title( $value ) ] = $value;
236 }
237 }
238 }
239 }
240
241 return $result;
242 }
243 }
244