/var/www/html_it/wp-content/plugins/woocommerce-orders-tracking/includes/admin/log.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php

/**
 * Class VI_WOOCOMMERCE_ORDERS_TRACKING_ADMIN_LOG
 *
 */
if ( ! defined'ABSPATH' ) ) {
    exit;
}

class 
VI_WOOCOMMERCE_ORDERS_TRACKING_ADMIN_LOG {
    public function 
__construct() {
        
add_action'wp_ajax_vi_wot_view_log', array( $this'generate_log_ajax' ) );
        
add_action'admin_menu', array( $this'admin_menu' ), 39 );
        
add_action'admin_init', array( $this'admin_init' ) );
        
add_action'admin_init', array( $this'download_log_file' ) );
        
add_action'admin_enqueue_scripts', array( $this'wc_logs_enqueue_scripts' ) );
    }

    public static function 
wc_log$content$source 'debug'$level 'info' ) {
        
$content =  !in_array($source ,['shipstation-debug','dianxiaomi_debug''check']) ? strip_tags$content ) : $content;
        
$log     wc_get_logger();
        
$log->log$level,
            
$content,
            array(
                
'source' => 'woo-orders-tracking-' $source,
            )
        );
    }

    
/**
     * Use js to remove woo-orders-tracking - log files from WC/Status/Logs page because no PHP filters available
     */
    
public function wc_logs_enqueue_scripts() {
        global 
$pagenow;
        
$page = isset( $_GET['page'] ) ? sanitize_text_field$_GET['page'] ) : '';
        
$tab  = isset( $_GET['tab'] ) ? sanitize_text_field$_GET['tab'] ) : '';
        if ( 
$pagenow === 'admin.php' && $page === 'wc-status' && $tab === 'logs' ) {
            
wp_enqueue_script'woocommerce-orders-tracking-wc-logs'VI_WOOCOMMERCE_ORDERS_TRACKING_JS 'wc-logs.js', array( 'jquery' ), VI_WOOCOMMERCE_ORDERS_TRACKING_VERSION false);
        }
    }

    
/**
     * Download a log file to the user's computer
     */
    
public function download_log_file() {
        if ( ! 
current_user_canVI_WOOCOMMERCE_ORDERS_TRACKING_DATA::get_required_capability'access_logs' ) ) ) {
            return;
        }
        
$nonce = isset( $_POST['_wpnonce'] ) ? sanitize_text_field$_POST['_wpnonce'] ) : '';
        if ( 
$nonce && wp_verify_nonce$nonce'vi_wot_download_log' ) ) {
            
$file '';
            if ( isset( 
$_POST['vi_wot_download_log'] ) ) {
                
$log_file sanitize_text_field$_POST['vi_wot_download_log'] );
                if ( 
in_array$log_file$this->log_files() ) ) {
                    
$file VI_WOOCOMMERCE_ORDERS_TRACKING_CACHE $log_file '.txt';
                }
            } else {
                
$logs WC_Log_Handler_File::get_log_files();
                if ( ! empty( 
$_REQUEST['log_file'] ) && isset( $logssanitize_titlewp_unslash$_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok.
                    
$log_file $logssanitize_titlewp_unslash$_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok.
                    
$file     WC_LOG_DIR $log_file;
                }
            }
            if ( 
is_file$file ) ) {
                
$fh = @fopen'php://output''w' );
                
fprintf$fhchr0xEF ) . chr0xBB ) . chr0xBF ) );
                
header'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
                
header'Content-Description: File Transfer' );
                
header'Content-type: text/csv' );
                
header'Content-Disposition: attachment; filename=' $log_file '__' date'Y-m-d_H-i-s' ) . '.txt' );
                
header'Expires: 0' );
                
header'Pragma: public' );
                
fputs$fhfile_get_contents$file ) );
                
fclose$fh );
                die();
            }
        }
    }

    public function 
admin_init() {
        global 
$pagenow;
        if ( ! 
current_user_canVI_WOOCOMMERCE_ORDERS_TRACKING_DATA::get_required_capability'access_logs' ) ) ) {
            return;
        }
        
$page   = isset( $_GET['page'] ) ? sanitize_text_field$_GET['page'] ) : '';
        
$action = isset( $_GET['action'] ) ? sanitize_text_field$_GET['action'] ) : '';
        if ( 
$pagenow === 'admin.php' && $page === 'woocommerce-orders-tracking-logs' ) {
            
add_action'admin_enqueue_scripts', array( $this'admin_enqueue_scripts' ) );
            if ( 
$action === 'vi_wot_delete_log' ) {
                
$nonce    = isset( $_GET['_wpnonce'] ) ? sanitize_text_field$_GET['_wpnonce'] ) : '';
                
$log_file = isset( $_GET['vi_wot_file'] ) ? sanitize_text_field$_GET['vi_wot_file'] ) : '';
                if ( 
wp_verify_nonce$nonce'vi_wot_delete_log' ) && in_array$log_file$this->log_files() ) ) {
                    
$file VI_WOOCOMMERCE_ORDERS_TRACKING_CACHE $log_file '.txt';
                    if ( 
is_file$file ) ) {
                        
wp_delete_file$file );
                        
wp_safe_redirectadmin_url'admin.php?page=woocommerce-orders-tracking-logs' ) );
                        exit();
                    }
                }
            }
        }
    }

    public function 
admin_enqueue_scripts() {
        
wp_enqueue_style'woocommerce-orders-tracking-message'VI_WOOCOMMERCE_ORDERS_TRACKING_CSS 'message.min.css'''VI_WOOCOMMERCE_ORDERS_TRACKING_VERSION );
        
wp_enqueue_style'woocommerce-orders-tracking-logs'VI_WOOCOMMERCE_ORDERS_TRACKING_CSS 'logs.css'''VI_WOOCOMMERCE_ORDERS_TRACKING_VERSION );
        
wp_enqueue_script'woocommerce-orders-tracking-logs'VI_WOOCOMMERCE_ORDERS_TRACKING_JS 'logs.js', array( 'jquery' ), VI_WOOCOMMERCE_ORDERS_TRACKING_VERSIONfalse );
        
wp_localize_script'woocommerce-orders-tracking-logs''woo_orders_tracking_logs_params', array(
            
'i18n_confirm_delete'    => esc_html__'This cannot be undone, do you want to continue?''woocommerce-orders-tracking' ),
            
'i18n_select_file_alert' => esc_html__'Please select a log file.''woocommerce-orders-tracking' ),
        ) );
    }

    public function 
log_files() {
        return array( 
'import_tracking''debug''webhooks_logs' );
    }

    public function 
admin_menu() {
        
add_submenu_page'woocommerce-orders-tracking'esc_html__'Logs - WooCommerce Orders Tracking''woocommerce-orders-tracking' ), esc_html__'Logs''woocommerce-orders-tracking' ), VI_WOOCOMMERCE_ORDERS_TRACKING_DATA::get_required_capability'access_logs' ), 'woocommerce-orders-tracking-logs', array(
            
$this,
            
'page_callback'
        
) );
    }

    public function 
page_callback() {
        
?>
        <div class="wrap">
            <h2><?php esc_html_e'Your logs show here''woocommerce-orders-tracking' ?></h2>
            <form class="" action="" method="post">
                <?php
                wp_nonce_field
'vi_wot_download_log' );
                
$log_files $this->log_files();
                
$old_log   '';
                foreach ( 
$log_files as $log_file ) {
                    
$log_file self::build_log_file_name$log_file );
                    if ( 
is_file$log_file ) ) {
                        
ob_start();
                        
?>
                        <li>
                            <?php
                            self
::print_log_html( array( $log_file ) );
                            
?>
                        </li>
                        <?php
                        $old_log 
.= ob_get_clean();
                    }
                }
                if ( 
$old_log ) {
                    
?>
                    <div class="vi-ui warning message">
                        <div class="header"><?php esc_html_e'Log file(s) from versions before 1.1.0''woocommerce-orders-tracking' ?></div>
                        <ul class="list">
                            <?php
                            
echo wp_kses_post($old_log);
                            
?>
                        </ul>
                    </div>
                    <?php
                
}
                
?>
            </form>
            <div class="vi-ui positive message">
                <div class="header"><?php esc_html_e'Since version 1.1.0, all log files are stored in the same log folder of WooCommerce.''woocommerce-orders-tracking' ?></div>
                <ul class="list">
                    <li><?php echo wp_kses_postsprintf(esc_html__'Log folder: %s''woocommerce-orders-tracking' ), WC_LOG_DIR) )// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment ?></li>
                    <li><?php echo wp_kses_postsprintf(esc_html__'Log files older than %s days will be automatically deleted by WooCommerce''woocommerce-orders-tracking' ), apply_filters'woocommerce_logger_days_to_retain_logs'30 ) ))// phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment ?></li>
                </ul>
            </div>
            <?php
            
if ( class_exists'WC_Log_Handler_File' ) ) {
                
$logs WC_Log_Handler_File::get_log_files();
                if ( 
count$logs ) ) {
                    foreach ( 
$logs as $key => $value ) {
                        if ( 
strpos$key'woo-orders-tracking-' ) !== ) {
                            unset( 
$logs$key ] );
                        }
                    }
                }
                if ( ! empty( 
$_REQUEST['log_file'] ) && isset( $logssanitize_titlewp_unslash$_REQUEST['log_file'] ) ) ] ) ) { // WPCS: input var ok, CSRF ok.
                    
$viewed_log $logssanitize_titlewp_unslash$_REQUEST['log_file'] ) ) ]; // WPCS: input var ok, CSRF ok.
                
} elseif ( ! empty( $logs ) ) {
                    
$viewed_log current$logs );
                }

                if ( ! empty( 
$_REQUEST['handle'] ) ) { // WPCS: input var ok, CSRF ok.
                    
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_noncewp_unslash$_REQUEST['_wpnonce'] ), 'remove_log' ) ) { // WPCS: input var ok, sanitization ok.
                        
wp_dieesc_html__'Action failed. Please refresh the page and retry.''woocommerce-orders-tracking' ) );
                    }
                    if ( ! empty( 
$_REQUEST['handle'] ) ) {  // WPCS: input var ok.
                        
$log_handler = new WC_Log_Handler_File();
                        
$log_handler->removewp_unslash$_REQUEST['handle'] ) ); // WPCS: input var ok, sanitization ok.
                    
}
                    
wp_safe_redirectesc_url_rawadmin_url'admin.php?page=woocommerce-orders-tracking-logs' ) ) );
                    exit();
                }
                
$log_of = isset( $_REQUEST['log_of'] ) ? sanitize_text_field$_REQUEST['log_of'] ) : '';
                if ( 
$logs ) {
                    
?>
                    <div id="log-viewer-select">
                        <div class="alignleft">
                            <h2>
                                <?php
                                
echo esc_html$viewed_log );
                                if ( ! empty( 
$viewed_log ) ) { ?>
                                    <a class="page-title-action"
                                       href="<?php echo esc_urlwp_nonce_urladd_query_arg( array( 'handle' => sanitize_title$viewed_log ) ), admin_url'admin.php?page=woocommerce-orders-tracking-logs' ) ), 'remove_log' ) ); ?>"
                                       class="button"><?php esc_html_e'Delete log''woocommerce-orders-tracking' ); ?></a>
                                    <?php
                                
}
                                
?>
                            </h2>
                        </div>
                        <div class="alignright">
                            <form class="vi-wot-logs-form"
                                  action="<?php echo esc_urladmin_url'admin.php?page=woocommerce-orders-tracking-logs' ) ); ?>"
                                  method="post">
                                <select name="log_of">
                                    <option value=""><?php esc_html_e'All log files''woocommerce-orders-tracking' ?></option>
                                    <?php
                                    
foreach (
                                        array(
                                            
'import-tracking' => esc_html__'Import tracking''woocommerce-orders-tracking' ),
                                            
'webhooks'        => esc_html__'Webhooks''woocommerce-orders-tracking' ),
                                            
'webhooks-debug'  => esc_html__'Webhooks debug''woocommerce-orders-tracking' ),
                                            
'paypal-debug'    => esc_html__'PayPal debug''woocommerce-orders-tracking' ),
                                            
'dianxiaomi-debug'    => esc_html__'Dianxiaomi debug''woocommerce-orders-tracking' ),
                                            
'yakkyofy'    => esc_html__'Yakkyofy''woocommerce-orders-tracking' ),
                                        ) as 
$log_id => $log_name
                                    
) {
                                        
?>
                                        <option value="<?php echo esc_attr$log_id ?><?php selected$log_of$log_id ); ?>><?php echo esc_html$log_name ?></option>
                                        <?php
                                    
}
                                    
?>
                                </select>
                                <select name="log_file">
                                    <option value=""><?php esc_html_e'No files found''woocommerce-orders-tracking' ?></option>
                                    <?php
                                    
foreach ( $logs as $log_key => $log_file ) {
                                        
$timestamp filemtimeWC_LOG_DIR $log_file );
                                        
$date      sprintf(
                                        
/* translators: 1: last access date 2: last access time 3: last access timezone abbreviation */
                                            
__'%1$s at %2$s %3$s''woocommerce-orders-tracking' ),
                                            
wp_datewc_date_format(), $timestamp ),
                                            
wp_datewc_time_format(), $timestamp ),
                                            
wp_date'T'$timestamp )
                                        );
                                        
?>
                                        <option value="<?php echo esc_attr$log_key ); ?><?php selectedsanitize_title$viewed_log ), $log_key ); ?>><?php echo esc_html$log_file ); ?>
                                            (<?php echo esc_html$date ); ?>)
                                        </option>
                                        <?php
                                    
}
                                    
?>
                                </select>
                                <button type="submit" class="button"
                                        value="<?php esc_attr_e'View''woocommerce-orders-tracking' ); ?>"><?php esc_html_e'View''woocommerce-orders-tracking' ); ?></button>
                                <button type="submit" class="button" name="_wpnonce"
                                        title="<?php esc_attr_e'Download selected log file to your device''woocommerce-orders-tracking' ); ?>"
                                        value="<?php echo esc_attrwp_create_nonce'vi_wot_download_log' ) ); ?>"><?php esc_html_e'Download''woocommerce-orders-tracking' ); ?></button>
                            </form>
                        </div>
                        <div class="clear"></div>
                    </div>
                    <div id="log-viewer">
                        <pre><?php echo esc_htmlfile_get_contentsWC_LOG_DIR $viewed_log ) ); ?></pre>
                    </div>
                    <?php
                
} else {
                    
?>
                    <div class="updated woocommerce-message inline">
                        <p><?php esc_html_e'There are currently no logs to view.''woocommerce-orders-tracking' ); ?></p>
                    </div>
                    <?php
                
}
            }
            
?>
        </div>
        <?php
    
}

    
/**
     * View import log
     */
    
public function generate_log_ajax() {
        
/*Check the nonce*/
        
if ( ! current_user_canVI_WOOCOMMERCE_ORDERS_TRACKING_DATA::get_required_capability'access_logs' ) ) || empty( $_GET['action'] ) || ! check_admin_refererwp_unslash$_GET['action'] ) ) ) {
            
wp_dieesc_html__'You do not have sufficient permissions to access this page.''woocommerce-orders-tracking' ) );
        }
        if ( empty( 
$_GET['vi_wot_file'] ) ) {
            
wp_dieesc_html__'No log file selected.''woocommerce-orders-tracking' ) );
        }
        
$file urldecodewp_unslashwc_clean$_GET['vi_wot_file'] ) ) );
        if ( ! 
is_file$file ) ) {
            
wp_dieesc_html__'Log file not found.''woocommerce-orders-tracking' ) );
        }
        echo( 
wp_kses_postnl2brfile_get_contents$file ) ) ) );
        exit();
    }

    public static function 
print_log_html$logs ) {
        if ( 
is_array$logs ) && count$logs ) ) {
            
$page = isset( $_GET['page'] ) ? sanitize_text_field$_GET['page'] ) : '';
            foreach ( 
$logs as $log ) {
                
?>
                <p><?php echo esc_html$log ?>
                    <a target="_blank" href="<?php echo esc_urladd_query_arg( array(
                        
'action'      => 'vi_wot_view_log',
                        
'vi_wot_file' => urlencode$log ),
                        
'_wpnonce'    => wp_create_nonce'vi_wot_view_log' ),
                    ), 
admin_url'admin-ajax.php' ) ) ) ?>"><?php esc_html_e'View log''woocommerce-orders-tracking' ?>
                    </a>
                    <?php
                    
if ( $page === 'woocommerce-orders-tracking-logs' ) {
                        
$file_name explode'.'substr$logstrlenVI_WOOCOMMERCE_ORDERS_TRACKING_CACHE ) ) )[0];
                        
?>
                        ,
                        <a class="vi-wot-delete-log" href="<?php echo esc_urladd_query_arg( array(
                            
'action'      => 'vi_wot_delete_log',
                            
'vi_wot_file' => $file_name,
                            
'_wpnonce'    => wp_create_nonce'vi_wot_delete_log' ),
                        ) ) ) 
?>"><?php esc_html_e'Delete''woocommerce-orders-tracking' ?>
                        </a>
                        <?php esc_html_e' or ''woocommerce-orders-tracking' ); ?>
                        <button type="submit" name="vi_wot_download_log" value="<?php echo esc_attr$file_name ?>"
                                class="vi-wot-download-log"><?php esc_html_e'Download log file''woocommerce-orders-tracking' ?>
                        </button>
                        <?php
                    
}
                    
?>
                </p>
                <?php
            
}
        }
    }

    public static function 
build_log_file_name$log_file ) {
        
$ext      '';
        
$pathinfo pathinfo$log_file );
        if ( empty( 
$pathinfo['extension'] ) ) {
            
$ext '.txt';
        }

        return 
VI_WOOCOMMERCE_ORDERS_TRACKING_CACHE $log_file $ext;
    }
}