| 1 |
<?php |
| 2 |
/** |
| 3 |
* UpStream_Debug Class |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* UpStream_Debug Class |
| 16 |
* |
| 17 |
* @since 1.0.0 |
| 18 |
*/ |
| 19 |
class UpStream_Debug { |
| 20 |
|
| 21 |
const FILE = 'debug-upstream.log'; |
| 22 |
|
| 23 |
const PAGE_SLUG = 'upstream_debug_log'; |
| 24 |
|
| 25 |
const ACTION_DELETE_LOG = 'delete_log'; |
| 26 |
|
| 27 |
const ADMIN_PAGE_URL_FRAGMENT = 'admin.php?page='; |
| 28 |
|
| 29 |
/** |
| 30 |
* Log file path. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
protected static $path; |
| 35 |
|
| 36 |
/** |
| 37 |
* Is initialized. |
| 38 |
* |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
protected static $initialized = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* Log messages. |
| 45 |
* |
| 46 |
* @var array |
| 47 |
*/ |
| 48 |
protected static $messages = array(); |
| 49 |
|
| 50 |
/** |
| 51 |
* Write the given message into the log file. |
| 52 |
* |
| 53 |
* @param string $message Error message. |
| 54 |
* @param mixed $trace Error message. |
| 55 |
*/ |
| 56 |
public static function write( $message, $trace = null ) { |
| 57 |
if ( ! static::is_enabled() ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! static::$initialized ) { |
| 62 |
static::init(); |
| 63 |
} |
| 64 |
|
| 65 |
// Make sure we have a string to write. |
| 66 |
if ( ! is_string( $message ) ) { |
| 67 |
$message = print_r( $message, true ); |
| 68 |
} |
| 69 |
|
| 70 |
// Add the timestamp to the message. |
| 71 |
$message = sprintf( '[%s] %s', gmdate( 'Y-m-d H:i:s T O' ), $message ) . "\n"; |
| 72 |
|
| 73 |
if ( $trace ) { |
| 74 |
$tt = print_r( $trace, true ); |
| 75 |
$message .= $tt . "\n"; |
| 76 |
} |
| 77 |
|
| 78 |
error_log( $message, 3, static::$path ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns true if debug is enabled in the UpStream settings. |
| 83 |
* |
| 84 |
* @return bool |
| 85 |
*/ |
| 86 |
public static function is_enabled() { |
| 87 |
$option = get_option( 'upstream_general' ); |
| 88 |
|
| 89 |
if ( ! $option ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
$key = 'debug'; |
| 94 |
|
| 95 |
return array_key_exists( $key, $option ) |
| 96 |
&& ! empty( $option[ $key ] ) |
| 97 |
&& 1 === (int) $option[ $key ][0]; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get things going |
| 102 |
* |
| 103 |
* @since 1.0.0 |
| 104 |
*/ |
| 105 |
public static function init() { |
| 106 |
if ( ! static::is_enabled() ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
static::$path = str_replace( '//', '/', WP_CONTENT_DIR . '/' . static::FILE ); |
| 111 |
|
| 112 |
// Admin bar. |
| 113 |
add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 99 ); |
| 114 |
|
| 115 |
// Admin menu. |
| 116 |
add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ) ); |
| 117 |
|
| 118 |
static::$initialized = true; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add menu to admin bar |
| 123 |
* |
| 124 |
* @since 1.0.0 |
| 125 |
*/ |
| 126 |
public static function admin_bar_menu() { |
| 127 |
global $wp_admin_bar; |
| 128 |
|
| 129 |
$args = array( |
| 130 |
'id' => 'upstream_debug', |
| 131 |
'title' => __( 'UpStream Debug Log', 'upstream' ), |
| 132 |
'href' => admin_url( static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG ), |
| 133 |
); |
| 134 |
|
| 135 |
$wp_admin_bar->add_menu( $args ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Admin menu hook |
| 140 |
* |
| 141 |
* @since 1.0.0 |
| 142 |
*/ |
| 143 |
public static function admin_menu() { |
| 144 |
// Admin menu. |
| 145 |
add_submenu_page( |
| 146 |
admin_url( static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG ), |
| 147 |
__( 'Debug Log' ), |
| 148 |
__( 'Debug Log' ), |
| 149 |
'activate_plugins', |
| 150 |
'upstream_debug_log', |
| 151 |
array( __CLASS__, 'view_log_page' ) |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* View log page |
| 157 |
* |
| 158 |
* @since 1.0.0 |
| 159 |
*/ |
| 160 |
public static function view_log_page() { |
| 161 |
static::handle_actions(); |
| 162 |
|
| 163 |
global $wp_version; |
| 164 |
|
| 165 |
$is_log_found = file_exists( static::$path ); |
| 166 |
|
| 167 |
// Get all the plugins and versions. |
| 168 |
$plugins = get_plugins(); |
| 169 |
$plugins_data = array(); |
| 170 |
$server_data = wp_unslash( $_SERVER ); |
| 171 |
|
| 172 |
foreach ( $plugins as $plugin => $data ) { |
| 173 |
$plugins_data[ $plugin ] = ( is_plugin_active( $plugin ) ? 'ACTIVATED' : 'deactivated' ) . ' [' . $data['Version'] . ']'; |
| 174 |
} |
| 175 |
|
| 176 |
$user_info = get_userdata( wp_get_current_user()->ID ); |
| 177 |
|
| 178 |
$debug_data = array( |
| 179 |
'php' => array( |
| 180 |
'version' => PHP_VERSION, |
| 181 |
'os' => PHP_OS, |
| 182 |
'date_default_timezone_get' => date_default_timezone_get(), |
| 183 |
'date(e)' => gmdate( 'e' ), |
| 184 |
'date(T)' => gmdate( 'T' ), |
| 185 |
'browser_str' => sanitize_text_field( $server_data['HTTP_USER_AGENT'] ), |
| 186 |
), |
| 187 |
'wordpress' => array( |
| 188 |
'version' => $wp_version, |
| 189 |
'date_format' => get_option( 'date_format' ), |
| 190 |
'time_format' => get_option( 'time_format' ), |
| 191 |
'timezone_string' => get_option( 'timezone_string' ), |
| 192 |
'gmt_offset' => get_option( 'gmt_offset' ), |
| 193 |
'plugins' => $plugins_data, |
| 194 |
), |
| 195 |
'user' => array( |
| 196 |
'uid' => $user_info->ID, |
| 197 |
'roles' => $user_info->roles, |
| 198 |
'username' => $user_info->user_email, |
| 199 |
'capabilities' => $user_info->allcaps, |
| 200 |
), |
| 201 |
'theme' => array( |
| 202 |
'template' => get_template(), |
| 203 |
'template_dir' => get_template_directory(), |
| 204 |
), |
| 205 |
); |
| 206 |
|
| 207 |
$context = array( |
| 208 |
'label' => array( |
| 209 |
'title' => __( 'UpStream Debug Log', 'upstream' ), |
| 210 |
'file_info' => __( 'File info', 'upstream' ), |
| 211 |
'path' => __( 'Path', 'upstream' ), |
| 212 |
'log_content' => __( 'Log content', 'upstream' ), |
| 213 |
'size' => __( 'Size', 'upstream' ), |
| 214 |
'creation_time' => __( 'Created on', 'upstream' ), |
| 215 |
'modification_time' => __( 'Modified on', 'upstream' ), |
| 216 |
'delete_file' => __( 'Delete file', 'upstream' ), |
| 217 |
'debug_data' => __( 'Debug data', 'upstream' ), |
| 218 |
'log_file' => __( 'Log File', 'upstream' ), |
| 219 |
), |
| 220 |
'message' => array( |
| 221 |
'log_not_found' => __( 'Log file not found.', 'upstream' ), |
| 222 |
'contact_support_tip' => __( |
| 223 |
'If you see any error or look for information regarding UpStream, please don\'t hesitate to contact the support team. E-mail us:', |
| 224 |
'upstream' |
| 225 |
), |
| 226 |
'click_to_delete' => __( |
| 227 |
'Click to delete the log file. Be careful, this operation can not be undone. ', |
| 228 |
'upstream' |
| 229 |
), |
| 230 |
), |
| 231 |
'contact_email' => 'help@upstreamplugin.com', |
| 232 |
'link_delete' => admin_url( |
| 233 |
sprintf( |
| 234 |
'admin.php?page=%s&action=%s&_wpnonce=%s', |
| 235 |
static::PAGE_SLUG, |
| 236 |
static::ACTION_DELETE_LOG, |
| 237 |
wp_create_nonce( static::ACTION_DELETE_LOG ) |
| 238 |
) |
| 239 |
), |
| 240 |
'is_log_found' => $is_log_found, |
| 241 |
'file' => array( |
| 242 |
'path' => static::$path, |
| 243 |
'size' => $is_log_found ? round( filesize( static::$path ) / 1024, 2 ) : 0, |
| 244 |
'modification_time' => $is_log_found ? gmdate( 'Y-m-d H:i:s T O', filemtime( static::$path ) ) : '', |
| 245 |
'content' => $is_log_found ? file_get_contents( static::$path ) : '', |
| 246 |
), |
| 247 |
'debug_data' => print_r( $debug_data, true ), |
| 248 |
'messages' => static::$messages, |
| 249 |
); |
| 250 |
|
| 251 |
?> |
| 252 |
|
| 253 |
|
| 254 |
<h1><?php echo esc_html( $context['label']['title'] ); ?></h1> |
| 255 |
|
| 256 |
<div id="upstream-debug-data"> |
| 257 |
<h2><?php echo esc_html( $context['label']['debug_data'] ); ?></h2> |
| 258 |
<textarea style="width:100%; height:400px"><?php echo esc_textarea( $context['debug_data'] ); ?></textarea> |
| 259 |
</div> |
| 260 |
|
| 261 |
<hr> |
| 262 |
|
| 263 |
<div id="upstream-debug-log"> |
| 264 |
<h2><?php echo esc_html( $context['label']['log_file'] ); ?></h2> |
| 265 |
|
| 266 |
<p><?php echo esc_html( $context['message']['click_to_delete'] ); ?></p> |
| 267 |
<a class="button button-danger" href="<?php echo esc_attr( $context['link_delete'] ); ?>"><?php echo esc_html( $context['label']['delete_file'] ); ?></a> |
| 268 |
|
| 269 |
<h3><?php echo esc_html( $context['label']['log_content'] ); ?></h3> |
| 270 |
<textarea style="width:100%; height:400px" id="upstream-debug-log"><?php echo esc_textarea( $context['file']['content'] ); ?></textarea> |
| 271 |
</div> |
| 272 |
|
| 273 |
|
| 274 |
<?php |
| 275 |
|
| 276 |
} |
| 277 |
|
| 278 |
/** |
| 279 |
* Handle admin page actions. |
| 280 |
* |
| 281 |
* @since 1.0.0 |
| 282 |
*/ |
| 283 |
protected static function handle_actions() { |
| 284 |
$get_data = wp_unslash( $_GET ); |
| 285 |
|
| 286 |
// Are we on the correct page? |
| 287 |
$page_param = 'page'; |
| 288 |
if ( ! array_key_exists( $page_param, $get_data ) || sanitize_text_field( $get_data[ $page_param ] ) !== static::PAGE_SLUG ) { |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
// Do we have an action? |
| 293 |
$action_param = 'action'; |
| 294 |
if ( ! array_key_exists( $action_param, $get_data ) || empty( $get_data[ $action_param ] ) ) { |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
$action = preg_replace( '/[^a-z0-9_\-]/i', '', sanitize_text_field( $get_data[ $action_param ] ) ); |
| 299 |
|
| 300 |
// Do we have a nonce? |
| 301 |
$wp_once_param = '_wpnonce'; |
| 302 |
if ( ! array_key_exists( $wp_once_param, $get_data ) || empty( $get_data[ $wp_once_param ] ) ) { |
| 303 |
static::$messages[] = __( 'Action nonce not found.', 'upstream' ); |
| 304 |
|
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
// Check the nonce. |
| 309 |
if ( ! wp_verify_nonce( sanitize_text_field( $get_data[ $wp_once_param ] ), $action ) ) { |
| 310 |
static::$messages[] = __( 'Invalid action nonce.', 'upstream' ); |
| 311 |
|
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
if ( static::ACTION_DELETE_LOG && file_exists( static::$path ) === $action ) { |
| 316 |
unlink( static::$path ); |
| 317 |
} |
| 318 |
|
| 319 |
wp_save_redirect( admin_url( static::ADMIN_PAGE_URL_FRAGMENT . static::PAGE_SLUG ) ); |
| 320 |
exit(); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Handle debugging message. |
| 326 |
* |
| 327 |
* @param mixed $s Debuging data. |
| 328 |
*/ |
| 329 |
function up_debug( $s = null ) { |
| 330 |
if ( ! $s ) { |
| 331 |
$fname = ''; |
| 332 |
$bt = debug_backtrace(); |
| 333 |
$bt_count = count( $bt ); |
| 334 |
|
| 335 |
for ( $i = 1; $i < 2 && $i < $bt_count; $i++ ) { |
| 336 |
$fname = $bt[ $i ]['function']; |
| 337 |
} |
| 338 |
|
| 339 |
UpStream_Debug::write( 'Entering function: ' . $fname ); |
| 340 |
} elseif ( is_string( $s ) ) { |
| 341 |
UpStream_Debug::write( $s, debug_backtrace() ); |
| 342 |
} elseif ( is_object( $s ) ) { |
| 343 |
if ( $s instanceof \Exception ) { |
| 344 |
UpStream_Debug::write( $s->getMessage(), $s->getTrace() ); |
| 345 |
} else { |
| 346 |
UpStream_Debug::write( print_r( $s, true ), debug_backtrace() ); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|