PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.5.0
WP 2FA – Two-factor authentication for WordPress v2.5.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 2 years ago class-date-time-utils.php 2 years ago class-debugging.php 2 years ago class-generate-modal.php 2 years ago class-migration.php 2 years ago class-request-utils.php 2 years ago class-settings-utils.php 2 years ago class-user-utils.php 2 years ago index.php 5 years ago
class-debugging.php
154 lines
1 <?php
2 /**
3 * Responsible for logging.
4 *
5 * @package wp2fa
6 * @subpackage utils
7 * @copyright %%YEAR%% 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 namespace WP2FA\Utils;
14
15 if ( ! class_exists( '\WP2FA\Utils\Debugging' ) ) {
16 /**
17 * Utility class for creating modal popup markup.
18 *
19 * @package WP2FA\Utils
20 * @since 1.4.2
21 */
22 class Debugging {
23
24 /**
25 * Local cache for the logging dir so that it doesn't need to be repopulated each time get_logging_dir_path is called.
26 *
27 * @var string
28 */
29 private static $logging_dir_path = '';
30
31 /**
32 * Retrieve the logging status
33 *
34 * @return boolean
35 */
36 private static function is_logging_enabled() {
37 /**
38 * Enables / Disables the logging for the plugin.
39 *
40 * @param bool $disabled - Default logging for the plugin.
41 */
42 return apply_filters( WP_2FA_PREFIX . 'logging_enabled', false );
43 }
44
45 /**
46 * Logs the given message
47 *
48 * @param string $message - The message to log.
49 *
50 * @return void
51 */
52 public static function log( $message ) {
53 if ( self::is_logging_enabled() ) {
54 self::write_to_log( self::get_log_timestamp() . "\n" . $message . "\n" . __( 'Current memory usage: ', 'wp-2fa' ) . memory_get_usage( true ) . "\n" );
55 }
56 }
57
58 /**
59 * Retrieves the path to the log file
60 *
61 * @return string
62 */
63 private static function get_logging_dir_path() {
64 if ( strlen( self::$logging_dir_path ) === 0 ) {
65 $uploads_dir = wp_upload_dir( null, false );
66 self::$logging_dir_path = trailingslashit( trailingslashit( $uploads_dir['basedir'] ) . WP_2FA_LOGS_DIR );
67 }
68
69 return self::$logging_dir_path;
70 }
71
72 /**
73 * Write data to log file.
74 *
75 * @param string $data - Data to write to file.
76 * @param bool $override - Set to true if overriding the file.
77 * @return bool
78 */
79 private static function write_to_log( $data, $override = false ) {
80 $logging_dir_path = self::get_logging_dir_path();
81 if ( ! is_dir( $logging_dir_path ) ) {
82 self::create_index_file();
83 self::create_htaccess_file();
84 }
85
86 $log_file_name = gmdate( 'Y-m-d' );
87 return self::write_to_file( 'wp-2fa-debug-' . $log_file_name . '.log', $data, $override );
88 }
89
90 /**
91 * Create an index.php file, if none exists, in order to
92 * avoid directory listing in the specified directory.
93 *
94 * @return bool
95 */
96 private static function create_index_file() {
97 return self::write_to_file( 'index.php', '<?php // Silence is golden' );
98 }
99
100 /**
101 * Create an .htaccess file, if none exists, in order to
102 * block access to directory listing in the specified directory.
103 *
104 * @return bool
105 */
106 private static function create_htaccess_file() {
107 return self::write_to_file( '.htaccess', 'Deny from all' );
108 }
109
110 /**
111 * Write data to log file in the uploads directory.
112 *
113 * @param string $filename - File name.
114 * @param string $content - Contents of the file.
115 * @param bool $override - (Optional) True if overriding file contents.
116 * @return bool
117 */
118 private static function write_to_file( $filename, $content, $override = false ) {
119 global $wp_filesystem;
120 require_once ABSPATH . 'wp-admin/includes/file.php';
121 WP_Filesystem();
122
123 $logging_dir = self::get_logging_dir_path();
124
125 $result = false;
126
127 if ( ! is_dir( $logging_dir ) ) {
128 if ( false === wp_mkdir_p( $logging_dir ) ) {
129 return false;
130 }
131 }
132
133 $filepath = $logging_dir . $filename;
134 if ( ! $wp_filesystem->exists( $filepath ) || $override ) {
135 $result = $wp_filesystem->put_contents( $filepath, $content );
136 } else {
137 $existing_content = $wp_filesystem->get_contents( $filepath );
138 $result = $wp_filesystem->put_contents( $filepath, $existing_content . $content );
139 }
140
141 return $result;
142 }
143
144 /**
145 * Returns the timestamp for log files.
146 *
147 * @return string
148 */
149 private static function get_log_timestamp() {
150 return '[' . gmdate( 'd-M-Y H:i:s' ) . ' UTC]';
151 }
152 }
153 }
154