Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/classes/admin/class-logs.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace WP_Rocket\Admin;
3
+
4
+
use WP_Rocket\Logger\Logger;
5
+
use WP_Rocket\Event_Management\Subscriber_Interface;
6
+
7
+
defined( 'ABSPATH' ) || exit;
8
+
9
+
/**
10
+
* Class that handles few things about the logs.
11
+
*
12
+
* @since 3.1.4
13
+
* @author Grégory Viguier
14
+
*/
15
+
class Logs implements Subscriber_Interface {
16
+
17
+
/**
18
+
* Returns an array of events that this subscriber wants to listen to.
19
+
*
20
+
* @since 3.1.4
21
+
* @access public
22
+
* @author Grégory Viguier
23
+
*
24
+
* @return array
25
+
*/
26
+
public static function get_subscribed_events() {
27
+
return [
28
+
'pre_update_option_' . WP_ROCKET_SLUG => [ 'enable_debug', 10, 2 ],
29
+
'admin_post_rocket_download_debug_file' => 'download_debug_file',
30
+
'admin_post_rocket_delete_debug_file' => 'delete_debug_file',
31
+
];
32
+
}
33
+
34
+
/** ----------------------------------------------------------------------------------------- */
35
+
/** DEBUG ACTIVATION ======================================================================== */
36
+
/** ----------------------------------------------------------------------------------------- */
37
+
38
+
/**
39
+
* Enable or disable the debug mode when settings are saved.
40
+
*
41
+
* @since 3.1.4
42
+
* @access public
43
+
* @author Grégory Viguier
44
+
*
45
+
* @param array $newvalue An array of submitted options values.
46
+
* @param array $oldvalue An array of previous options values.
47
+
* @return array Updated submitted options values.
48
+
*/
49
+
public function enable_debug( $newvalue, $oldvalue ) {
50
+
if ( empty( $_POST ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
51
+
return $newvalue;
52
+
}
53
+
54
+
if ( ! empty( $newvalue['debug_enabled'] ) ) {
55
+
Logger::enable_debug();
56
+
} else {
57
+
Logger::disable_debug();
58
+
}
59
+
60
+
unset( $newvalue['debug_enabled'] );
61
+
62
+
return $newvalue;
63
+
}
64
+
65
+
/** ----------------------------------------------------------------------------------------- */
66
+
/** ADMIN POST CALLBACKS ==================================================================== */
67
+
/** ----------------------------------------------------------------------------------------- */
68
+
69
+
/**
70
+
* Download the log file.
71
+
*
72
+
* @since 3.1.4
73
+
* @access public
74
+
* @author Grégory Viguier
75
+
*/
76
+
public function download_debug_file() {
77
+
if ( ! $this->verify_nonce( 'download_debug_file' ) ) {
78
+
wp_nonce_ays( '' );
79
+
}
80
+
81
+
if ( ! $this->current_user_can() ) {
82
+
$this->redirect();
83
+
}
84
+
85
+
$contents = Logger::get_log_file_contents();
86
+
87
+
if ( is_wp_error( $contents ) ) {
88
+
add_settings_error( 'general', $contents->get_error_code(), $contents->get_error_message(), 'error' );
89
+
set_transient( 'settings_errors', get_settings_errors(), 30 );
90
+
91
+
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
92
+
}
93
+
94
+
$file_name = Logger::get_log_file_path();
95
+
$file_name = basename( $file_name, '.log' ) . Logger::get_log_file_extension();
96
+
97
+
nocache_headers();
98
+
@header( 'Content-Type: text/x-log' );
99
+
@header( 'Content-Disposition: attachment; filename="' . $file_name . '"' );
100
+
@header( 'Content-Transfer-Encoding: binary' );
101
+
@header( 'Content-Length: ' . strlen( $contents ) );
102
+
@header( 'Connection: close' );
103
+
echo $contents; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
104
+
exit();
105
+
}
106
+
107
+
/**
108
+
* Delete the log file.
109
+
*
110
+
* @since 3.1.4
111
+
* @access public
112
+
* @author Grégory Viguier
113
+
*/
114
+
public function delete_debug_file() {
115
+
if ( ! $this->verify_nonce( 'delete_debug_file' ) ) {
116
+
wp_nonce_ays( '' );
117
+
}
118
+
119
+
if ( ! $this->current_user_can() ) {
120
+
$this->redirect();
121
+
}
122
+
123
+
if ( ! Logger::delete_log_file() ) {
124
+
add_settings_error( 'general', 'debug_file_not_deleted', __( 'The debug file could not be deleted.', 'rocket' ), 'error' );
125
+
set_transient( 'settings_errors', get_settings_errors(), 30 );
126
+
127
+
$this->redirect( add_query_arg( 'settings-updated', 1, wp_get_referer() ) );
128
+
}
129
+
130
+
// Done.
131
+
$this->redirect();
132
+
}
133
+
134
+
135
+
/** ----------------------------------------------------------------------------------------- */
136
+
/** TOOLS =================================================================================== */
137
+
/** ----------------------------------------------------------------------------------------- */
138
+
139
+
/**
140
+
* Verify the nonce sent in $_GET['_wpnonce'].
141
+
*
142
+
* @since 3.1.4
143
+
* @access protected
144
+
* @author Grégory Viguier
145
+
*
146
+
* @param string $nonce_name The nonce name.
147
+
* @return bool
148
+
*/
149
+
protected function verify_nonce( $nonce_name ) {
150
+
return isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], $nonce_name ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
151
+
}
152
+
153
+
/**
154
+
* Tell if the current user can operate.
155
+
*
156
+
* @since 3.1.4
157
+
* @access protected
158
+
* @author Grégory Viguier
159
+
*
160
+
* @return bool
161
+
*/
162
+
protected function current_user_can() {
163
+
return current_user_can( 'rocket_manage_options' );
164
+
}
165
+
166
+
/**
167
+
* Redirect the user.
168
+
*
169
+
* @since 3.1.4
170
+
* @access protected
171
+
* @author Grégory Viguier
172
+
*
173
+
* @param string $redirect URL to redirect the user to.
174
+
*/
175
+
protected function redirect( $redirect = null ) {
176
+
if ( empty( $redirect ) ) {
177
+
$redirect = wp_get_referer();
178
+
}
179
+
180
+
wp_safe_redirect( esc_url_raw( $redirect ) );
181
+
die();
182
+
}
183
+
}
184
+