PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.8.0
WP 2FA – Two-factor authentication for WordPress v2.8.0
4.0.0 1.7.1 2.0.0 2.0.1 2.1.0 2.2.0 2.2.1 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.8.0 2.9.0 2.9.1 2.9.2 2.9.3 3.0.0 3.0.1 3.1.0 3.1.1 3.1.1.2 trunk 1.2.0 1.3.0 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.7.0
wp-2fa / includes / classes / Utils / class-debugging.php
wp-2fa / includes / classes / Utils Last commit date
class-abstract-migration.php 1 year ago class-date-time-utils.php 1 year ago class-debugging.php 1 year ago class-generate-modal.php 1 year ago class-migration.php 1 year ago class-request-utils.php 1 year ago class-settings-utils.php 1 year ago class-user-utils.php 1 year ago class-white-label.php 1 year ago index.php 1 year ago
class-debugging.php
197 lines
1 <?php
2 /**
3 * Responsible for logging.
4 *
5 * @package wp2fa
6 * @subpackage utils
7 * @copyright 2024 Melapress
8 * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
9 * @link https://wordpress.org/plugins/wp-2fa/
10 * @since 1.4.2
11 */
12
13 declare(strict_types=1);
14
15 namespace WP2FA\Utils;
16
17 use PhpParser\Node\Stmt\Static_;
18
19 if ( ! class_exists( '\WP2FA\Utils\Debugging' ) ) {
20 /**
21 * Utility class for creating modal popup markup.
22 *
23 * @package WP2FA\Utils
24 *
25 * @since 1.4.2
26 */
27 class Debugging {
28
29 /**
30 * Local cache for the logging dir so that it doesn't need to be repopulated each time get_logging_dir_path is called.
31 *
32 * @var string
33 *
34 * @since 1.4.2
35 */
36 private static $logging_dir_path = '';
37
38 /**
39 * Retrieve the logging status
40 *
41 * @return boolean
42 *
43 * @since 1.4.2
44 */
45 private static function is_logging_enabled() {
46 /**
47 * Enables / Disables the logging for the plugin.
48 *
49 * @param bool $disabled - Default logging for the plugin.
50 */
51 return apply_filters( WP_2FA_PREFIX . 'logging_enabled', false );
52 }
53
54 /**
55 * Logs the given message
56 *
57 * @param string $message - The message to log.
58 *
59 * @return void
60 *
61 * @since 1.4.2
62 */
63 public static function log( $message ) {
64 if ( self::is_logging_enabled() ) {
65 self::write_to_log( self::get_log_timestamp() . "\n" . $message . "\n" . __( 'Current memory usage: ', 'wp-2fa' ) . memory_get_usage( true ) . "\n" );
66 }
67 }
68
69 /**
70 * Retrieves the path to the log file
71 *
72 * @return string
73 *
74 * @since 1.4.2
75 */
76 private static function get_logging_dir_path() {
77 if ( strlen( self::$logging_dir_path ) === 0 ) {
78 $uploads_dir = wp_upload_dir( null, false );
79 self::$logging_dir_path = trailingslashit( trailingslashit( $uploads_dir['basedir'] ) . WP_2FA_LOGS_DIR );
80 }
81
82 return self::$logging_dir_path;
83 }
84
85 /**
86 * Write data to log file.
87 *
88 * @param string $data - Data to write to file.
89 * @param bool $override - Set to true if overriding the file.
90 *
91 * @return bool
92 *
93 * @since 1.4.2
94 */
95 private static function write_to_log( $data, $override = false ) {
96 $logging_dir_path = self::get_logging_dir_path();
97 if ( ! is_dir( $logging_dir_path ) ) {
98 self::create_index_file();
99 self::create_htaccess_file();
100 }
101
102 $log_file_name = gmdate( 'Y-m-d' );
103
104 return self::write_to_file( 'wp-2fa-debug-' . $log_file_name . '-' . self::get_random_file_string_addon() . '.log', $data, $override );
105 }
106
107 /**
108 * Create an index.php file, if none exists, in order to
109 * avoid directory listing in the specified directory.
110 *
111 * @return bool
112 *
113 * @since 1.4.2
114 */
115 private static function create_index_file() {
116 return self::write_to_file( 'index.php', '<?php // Silence is golden' );
117 }
118
119 /**
120 * Create an .htaccess file, if none exists, in order to
121 * block access to directory listing in the specified directory.
122 *
123 * @return bool
124 *
125 * @since 1.4.2
126 */
127 private static function create_htaccess_file() {
128 return self::write_to_file( '.htaccess', 'Deny from all' );
129 }
130
131 /**
132 * Write data to log file in the uploads directory.
133 *
134 * @param string $filename - File name.
135 * @param string $content - Contents of the file.
136 * @param bool $override - (Optional) True if overriding file contents.
137 *
138 * @return bool
139 *
140 * @since 1.4.2
141 */
142 private static function write_to_file( $filename, $content, $override = false ) {
143 global $wp_filesystem;
144 require_once ABSPATH . 'wp-admin/includes/file.php';
145 WP_Filesystem();
146
147 $logging_dir = self::get_logging_dir_path();
148
149 $result = false;
150
151 if ( ! is_dir( $logging_dir ) ) {
152 if ( false === wp_mkdir_p( $logging_dir ) ) {
153 return false;
154 }
155 }
156
157 $filepath = $logging_dir . $filename;
158 if ( ! $wp_filesystem->exists( $filepath ) || $override ) {
159 $result = $wp_filesystem->put_contents( $filepath, $content );
160 } else {
161 $existing_content = $wp_filesystem->get_contents( $filepath );
162 $result = $wp_filesystem->put_contents( $filepath, $existing_content . $content );
163 }
164
165 return $result;
166 }
167
168 /**
169 * Returns the timestamp for log files.
170 *
171 * @return string
172 *
173 * @since 1.4.2
174 */
175 private static function get_log_timestamp() {
176 return '[' . gmdate( 'd-M-Y H:i:s' ) . ' UTC]';
177 }
178
179 /**
180 * Generates a short random string which is used to generate log file name.
181 *
182 * @return string
183 *
184 * @since 2.8.0
185 */
186 private static function get_random_file_string_addon(): string {
187 $rnd_string = Settings_Utils::get_option( 'debug_name', false );
188 if ( ! $rnd_string ) {
189 $rnd_string = (string) \wp_generate_password( 20, false, false );
190 Settings_Utils::update_option( 'debug_name', $rnd_string );
191 }
192
193 return $rnd_string;
194 }
195 }
196 }
197