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