| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Log status. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminLegacyCore\Inc; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class LogStatus. |
| 17 |
*/ |
| 18 |
class LogStatus { |
| 19 |
|
| 20 |
/** |
| 21 |
* Instance |
| 22 |
* |
| 23 |
* @access private |
| 24 |
* @var object Class object. |
| 25 |
* @since 1.0.0 |
| 26 |
*/ |
| 27 |
private static $instance; |
| 28 |
|
| 29 |
/** |
| 30 |
* Deleted |
| 31 |
* |
| 32 |
* @access private |
| 33 |
* @var object Class object. |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
private static $file_deleted = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* Initiator |
| 40 |
* |
| 41 |
* @since 1.0.0 |
| 42 |
* @return object initialized object of class. |
| 43 |
*/ |
| 44 |
public static function get_instance() { |
| 45 |
if ( ! isset( self::$instance ) ) { |
| 46 |
self::$instance = new self(); |
| 47 |
} |
| 48 |
return self::$instance; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Instance |
| 53 |
* |
| 54 |
* @access private |
| 55 |
* @var string Class object. |
| 56 |
* @since 1.0.0 |
| 57 |
*/ |
| 58 |
private $menu_slug; |
| 59 |
|
| 60 |
/** |
| 61 |
* Constructor |
| 62 |
* |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
public function __construct() { |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* User action like download or delete log files. |
| 70 |
*/ |
| 71 |
public function user_actions() { |
| 72 |
|
| 73 |
// Ignoring nonce verification as using SuperGlobal variables on WordPress hooks. |
| 74 |
if ( ! empty( $_REQUEST['handle'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 75 |
|
| 76 |
if ( ! current_user_can( 'cartflows_manage_settings' ) ) { |
| 77 |
|
| 78 |
wp_die( esc_html__( 'You don\'t have permission to view this page.', 'cartflows' ) ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( isset( $_REQUEST['btn_action'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 82 |
|
| 83 |
$button_action = sanitize_text_field( wp_unslash( $_REQUEST['btn_action'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 84 |
|
| 85 |
switch ( $button_action ) { |
| 86 |
case 'remove-log': |
| 87 |
$this->delete_log_file(); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'download-log': |
| 91 |
$this->download_log_file(); |
| 92 |
break; |
| 93 |
|
| 94 |
default: |
| 95 |
break; |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Show the log page contents for file log handler. |
| 103 |
*/ |
| 104 |
public function display_logs() { |
| 105 |
|
| 106 |
if ( self::$file_deleted ) { |
| 107 |
echo "<div class='wcf-notice updated inline wcf-delete-log--message'>" . esc_html__( 'Log deleted successfully!', 'cartflows' ) . ' </div>'; |
| 108 |
} |
| 109 |
|
| 110 |
$logs = $this->get_log_files(); |
| 111 |
|
| 112 |
$form_url = esc_url( |
| 113 |
add_query_arg( |
| 114 |
array( |
| 115 |
'page' => 'cartflows', |
| 116 |
'action' => 'wcf-log', |
| 117 |
), |
| 118 |
admin_url( '/admin.php' ) |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
$viewed_log = ''; |
| 123 |
$viewed_log_file = ''; |
| 124 |
|
| 125 |
// Calling this function on CartFlows action hook. Hence ignoring nonce. |
| 126 |
if ( ! empty( $_REQUEST['log_file'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 127 |
|
| 128 |
$filename = sanitize_text_field( wp_unslash( $_REQUEST['log_file'] ) ); //phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 129 |
|
| 130 |
if ( isset( $logs[ $filename ] ) ) { |
| 131 |
$viewed_log = $filename; |
| 132 |
$viewed_log_file = $viewed_log . '.log'; |
| 133 |
} |
| 134 |
} elseif ( ! empty( $logs ) ) { |
| 135 |
$viewed_log = current( $logs ) ? pathinfo( current( $logs ), PATHINFO_FILENAME ) : ''; |
| 136 |
$viewed_log_file = $viewed_log . '.log'; |
| 137 |
} |
| 138 |
|
| 139 |
include_once CARTFLOWS_ADMIN_CORE_DIR . 'views/debugger.php'; |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Get all log files in the log directory. |
| 144 |
* |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
public function get_log_files() { |
| 148 |
// Condition for multisite instances: |
| 149 |
// Return empty array if log directory doesn't exist yet or is not readable. |
| 150 |
if ( ! is_dir( CARTFLOWS_LOG_DIR ) || ! is_readable( CARTFLOWS_LOG_DIR ) ) { |
| 151 |
return array(); |
| 152 |
} |
| 153 |
|
| 154 |
$files = scandir( CARTFLOWS_LOG_DIR ); |
| 155 |
$result = array(); |
| 156 |
|
| 157 |
if ( ! empty( $files ) ) { |
| 158 |
foreach ( $files as $key => $file ) { |
| 159 |
if ( ! is_dir( $file ) && strstr( $file, '.log' ) ) { |
| 160 |
$result[ pathinfo( $file, PATHINFO_FILENAME ) ] = $file; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return $result; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Delete Provided log file |
| 170 |
*/ |
| 171 |
public function delete_log_file() { |
| 172 |
|
| 173 |
if ( empty( $_REQUEST['_wpnonce'] ) || |
| 174 |
! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'remove_log' ) |
| 175 |
) { |
| 176 |
wp_die( esc_html__( 'Nonce verification failed. Please refresh the page and retry.', 'cartflows' ) ); |
| 177 |
} |
| 178 |
|
| 179 |
if ( empty( $_REQUEST['handle'] ) ) { |
| 180 |
wp_die( esc_html__( 'Filename is empty. Please refresh the page and retry.', 'cartflows' ) ); |
| 181 |
} |
| 182 |
|
| 183 |
$file_name = trim( sanitize_text_field( wp_unslash( $_REQUEST['handle'] ) ) ); |
| 184 |
$file_path = CARTFLOWS_LOG_DIR . $file_name; |
| 185 |
|
| 186 |
if ( file_exists( $file_path ) ) { |
| 187 |
wp_delete_file( $file_path ); |
| 188 |
self::$file_deleted = true; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Download the selected log file. |
| 194 |
*/ |
| 195 |
public function download_log_file() { |
| 196 |
|
| 197 |
if ( empty( $_REQUEST['_wpnonce'] ) || |
| 198 |
! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'download_log' ) |
| 199 |
) { |
| 200 |
wp_die( esc_html__( 'Nonce verification failed. Please refresh the page and retry.', 'cartflows' ) ); |
| 201 |
} |
| 202 |
|
| 203 |
$file_name = isset( $_REQUEST['handle'] ) ? trim( sanitize_text_field( wp_unslash( $_REQUEST['handle'] ) ) ) : ''; |
| 204 |
$file_path = CARTFLOWS_LOG_DIR . $file_name; |
| 205 |
|
| 206 |
if ( ! file_exists( $file_path ) ) { |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
$file_extension = pathinfo( $file_name, PATHINFO_EXTENSION ); |
| 211 |
$allowed_files = array( 'log' ); |
| 212 |
|
| 213 |
// Return if the desired file is not found for download. |
| 214 |
if ( ! in_array( $file_extension, $allowed_files, true ) || strpos( $file_name, '.php' ) !== false ) { |
| 215 |
wp_die( esc_html__( 'Invalid file.', 'cartflows' ) ); |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
header( 'Content-Type: text/log; charset=utf-8' ); |
| 220 |
header( 'Content-Disposition: attachment; filename=' . $file_name ); |
| 221 |
header( 'Pragma: no-cache' ); |
| 222 |
header( 'Expires: 0' ); |
| 223 |
|
| 224 |
echo file_get_contents( $file_path ); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 225 |
exit; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
LogStatus::get_instance(); |
| 230 |
|