PluginProbe ʕ •ᴥ•ʔ
WP 2FA – Two-factor authentication for WordPress / 2.1.0
WP 2FA – Two-factor authentication for WordPress v2.1.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 / Debugging.php
wp-2fa / includes / classes / Utils Last commit date
AbstractMigration.php 4 years ago DateTimeUtils.php 4 years ago Debugging.php 5 years ago GenerateModal.php 4 years ago Migration.php 4 years ago RequestUtils.php 4 years ago SettingsUtils.php 4 years ago UserUtils.php 4 years ago index.php 5 years ago
Debugging.php
119 lines
1 <?php
2 namespace WP2FA\Utils;
3
4 /**
5 * Utility class for creating modal popup markup.
6 *
7 * @package WP2FA\Utils
8 * @since 1.4.2
9 */
10 class Debugging {
11
12 /**
13 * @var string Local cache for the logging dir so that it doesn't need to be repopulated each time get_logging_dir_path is called.
14 */
15 private static $logging_dir_path = '';
16
17 private static function is_logging_enabled() {
18 return apply_filters( 'wp_2fa_logging_enabled', false );
19 }
20
21 public static function log( $message ) {
22 if ( self::is_logging_enabled() ) {
23 self::write_to_log( self::get_log_timestamp() . "\n" . $message . "\n" . __( 'Current memory usage: ', 'wp-2fa' ) . memory_get_usage( true ) . "\n" );
24 }
25 }
26
27 private static function get_logging_dir_path() {
28 if ( strlen( self::$logging_dir_path ) === 0 ) {
29 $uploads_dir = wp_upload_dir( null, false );
30 self::$logging_dir_path = trailingslashit( trailingslashit( $uploads_dir['basedir'] ) . WP_2FA_LOGS_DIR );
31 }
32
33 return self::$logging_dir_path;
34 }
35
36 /**
37 * Write data to log file.
38 *
39 * @param string $data - Data to write to file.
40 * @param bool $override - Set to true if overriding the file.
41 * @return bool
42 */
43 private static function write_to_log( $data, $override = false ) {
44 $logging_dir_path = self::get_logging_dir_path();
45 if ( ! is_dir( $logging_dir_path ) ) {
46 self::create_index_file( $logging_dir_path );
47 self::create_htaccess_file( $logging_dir_path );
48 }
49
50 $log_file_name = date( 'Y-m-d' );
51 return self::write_to_file( 'wp-2fa-debug-' . $log_file_name . '.log', $data, $override );
52 }
53
54 /**
55 * Create an index.php file, if none exists, in order to
56 * avoid directory listing in the specified directory.
57 *
58 * @param string $dir_path - Directory Path.
59 * @return bool
60 */
61 private static function create_index_file( $dir_path ) {
62 return self::write_to_file( 'index.php', '<?php // Silence is golden' );
63 }
64
65 /**
66 * Create an .htaccess file, if none exists, in order to
67 * block access to directory listing in the specified directory.
68 *
69 * @param string $dir_path - Directory Path.
70 * @return bool
71 */
72 private static function create_htaccess_file( $dir_path ) {
73 return self::write_to_file( '.htaccess', 'Deny from all' );
74 }
75
76 /**
77 * Write data to log file in the uploads directory.
78 *
79 * @param string $filename - File name.
80 * @param string $content - Contents of the file.
81 * @param bool $override - (Optional) True if overriding file contents.
82 * @return bool
83 */
84 private static function write_to_file( $filename, $content, $override = false ) {
85 global $wp_filesystem;
86 require_once ABSPATH . 'wp-admin/includes/file.php';
87 WP_Filesystem();
88
89 $logging_dir = self::get_logging_dir_path();
90
91 $result = false;
92
93 if ( ! is_dir( $logging_dir ) ) {
94 if ( false === wp_mkdir_p( $logging_dir ) ) {
95 return false;
96 }
97 }
98
99 $filepath = $logging_dir . $filename;
100 if ( ! $wp_filesystem->exists( $filepath ) || $override ) {
101 $result = $wp_filesystem->put_contents( $filepath, $content );
102 } else {
103 $existing_content = $wp_filesystem->get_contents( $filepath );
104 $result = $wp_filesystem->put_contents( $filepath, $existing_content . $content );
105 }
106
107 return $result;
108 }
109
110 /**
111 * Returns the timestamp for log files.
112 *
113 * @return string
114 */
115 private static function get_log_timestamp() {
116 return '[' . date( 'd-M-Y H:i:s' ) . ' UTC]';
117 }
118 }
119