| 1 |
<?php |
| 2 |
|
| 3 |
$log_viewer_action = isset( $_GET['wpas_tools_log_viewer_action'] ) ? sanitize_text_field( wp_unslash( $_GET['wpas_tools_log_viewer_action'] ) ) : ''; |
| 4 |
|
| 5 |
$log_viewer_current_file = isset( $_GET['wpas_tools_log_viewer_current_file'] ) ? sanitize_text_field( wp_unslash( $_GET['wpas_tools_log_viewer_current_file'] ) ) : ''; |
| 6 |
|
| 7 |
/** |
| 8 |
* Scan AS logs folder for files. |
| 9 |
* |
| 10 |
* @return array |
| 11 |
*/ |
| 12 |
function dirToArray() { |
| 13 |
|
| 14 |
$dir = get_logs_path(); |
| 15 |
|
| 16 |
$result = array(); |
| 17 |
|
| 18 |
$cdir = scandir( $dir ); |
| 19 |
foreach( $cdir as $key => $value ) { |
| 20 |
if( ! in_array( $value, array( ".", ".." ) ) ) { |
| 21 |
if( is_dir( $dir . DIRECTORY_SEPARATOR . $value ) ) { |
| 22 |
$result[ $value ] = dirToArray( $dir . DIRECTORY_SEPARATOR . $value ); |
| 23 |
} |
| 24 |
else { |
| 25 |
$result[] = $value; |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
return $result; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Enqueue Admin Scripts |
| 35 |
*/ |
| 36 |
function enqueue_scripts() { |
| 37 |
wp_enqueue_script( 'jquery-ui-core' ); |
| 38 |
wp_enqueue_script( 'jquery-ui-accordion' ); |
| 39 |
} |
| 40 |
add_action( 'admin_enqueue_scripts', 'enqueue_scripts' ); |
| 41 |
|
| 42 |
|
| 43 |
/* |
| 44 |
* The JavaScript for our AJAX call |
| 45 |
*/ |
| 46 |
function wpas_tools_log_viewer_ajax_script() { |
| 47 |
?> |
| 48 |
<style> |
| 49 |
#overlay { |
| 50 |
display: none; |
| 51 |
position: absolute; |
| 52 |
background: #fff; |
| 53 |
} |
| 54 |
|
| 55 |
#img-load { |
| 56 |
position: absolute; |
| 57 |
} |
| 58 |
|
| 59 |
.controls { |
| 60 |
display: none; |
| 61 |
} |
| 62 |
|
| 63 |
div.log-viewer-controls { |
| 64 |
width: 100%; |
| 65 |
box-sizing: border-box; |
| 66 |
clear: both; |
| 67 |
} |
| 68 |
#accordion .ui-accordion-content { |
| 69 |
height: auto !important; |
| 70 |
overflow: visible !important; |
| 71 |
box-sizing: border-box; |
| 72 |
padding: 10px 5px !important; |
| 73 |
} |
| 74 |
#accordion h3.log-viewer-filename { |
| 75 |
margin-top: 5px !important; |
| 76 |
margin-bottom: 2px !important; |
| 77 |
clear: both; |
| 78 |
} |
| 79 |
.log-viewer-controls table tr th, |
| 80 |
.log-viewer-controls table tr td { |
| 81 |
padding: 2px 10px; |
| 82 |
} |
| 83 |
|
| 84 |
textarea[disabled] { |
| 85 |
color: #000; |
| 86 |
background-color: #fff; |
| 87 |
} |
| 88 |
|
| 89 |
div:focus, span:focus, textarea:focus, a:focus, a:active { |
| 90 |
border-color: #5b9dd9; |
| 91 |
-webkit-box-shadow: 0 0 0 rgba(30,140,190,.8); |
| 92 |
box-shadow: 0 0 0 rgba(30,140,190,.8); |
| 93 |
outline: none; |
| 94 |
} |
| 95 |
|
| 96 |
*.fa { |
| 97 |
font-size: 16px; |
| 98 |
line-height: 26px; |
| 99 |
margin-right: 5px; |
| 100 |
} |
| 101 |
|
| 102 |
*.fa .disabled { |
| 103 |
color: lightgray; |
| 104 |
} |
| 105 |
|
| 106 |
</style> |
| 107 |
|
| 108 |
<!-- Load jQuery UI --> |
| 109 |
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script> |
| 110 |
<script type="text/javascript"> |
| 111 |
|
| 112 |
function makeSafeForCSS(name) { |
| 113 |
return name.replace(/[^a-z0-9]/g, function (s) { |
| 114 |
var c = s.charCodeAt(0); |
| 115 |
if (c == 32) return '-'; |
| 116 |
if (c >= 65 && c <= 90) return '_' + s.toLowerCase(); |
| 117 |
return '__' + ('000' + c.toString(16)).slice(-4); |
| 118 |
}); |
| 119 |
} |
| 120 |
|
| 121 |
function setWrap(wrap) { |
| 122 |
|
| 123 |
var area = jQuery('textarea#content'); |
| 124 |
//var wrap = jQuery('input#wrap').is(':checked') === true ? 'soft' : 'off'; |
| 125 |
|
| 126 |
if (area.wrap) { |
| 127 |
area.attr('wrap', wrap); |
| 128 |
area.wrap = wrap; |
| 129 |
} else { // wrap attribute not supported - try Mozilla workaround |
| 130 |
area.setAttribute("wrap", wrap); |
| 131 |
area.style.overflow = "hidden"; |
| 132 |
area.style.overflow = "auto"; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
wpas_log_viewer_current_file = ''; |
| 137 |
safeClassName = ''; |
| 138 |
|
| 139 |
jQuery(document).ready(function ($) { |
| 140 |
$('button.wpas-tools-log-delete, a.wpas-tools-log-view, button.wpas-tools-log-download').click(function () { |
| 141 |
|
| 142 |
$action = $(this).data("action"); |
| 143 |
$lines = $('#lines').val(); |
| 144 |
$nonce = $(this).data("nonce"); |
| 145 |
|
| 146 |
if ($action === 'wpas_tools_log_viewer_view') { |
| 147 |
wpas_log_viewer_current_file = $(this).data("filename"); |
| 148 |
$('button#delete').data('filename', wpas_log_viewer_current_file); |
| 149 |
$('button#download').data('filename', wpas_log_viewer_current_file); |
| 150 |
} |
| 151 |
|
| 152 |
disableInputs(false, wpas_log_viewer_current_file, 'Working ...'); |
| 153 |
|
| 154 |
// Confirm Delete |
| 155 |
if ($action === 'wpas_tools_log_viewer_delete' |
| 156 |
&& !confirm("Deleting server log file '" + wpas_log_viewer_current_file + "' is permanent.\r\nAre you sure?") |
| 157 |
) { |
| 158 |
return false; |
| 159 |
} |
| 160 |
// View |
| 161 |
else if ($action === 'wpas_tools_log_viewer_view' |
| 162 |
) { |
| 163 |
} |
| 164 |
// Download |
| 165 |
else if ($action === 'wpas_tools_log_viewer_download' |
| 166 |
) { |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
$.ajax({ |
| 171 |
type: "POST", |
| 172 |
url: ajaxurl, |
| 173 |
data: { |
| 174 |
action: $action, |
| 175 |
file: wpas_log_viewer_current_file, |
| 176 |
lines: $lines, |
| 177 |
nonce: $nonce |
| 178 |
}, |
| 179 |
success: function (data) { |
| 180 |
|
| 181 |
// Download |
| 182 |
if ($action === 'wpas_tools_log_viewer_download') { |
| 183 |
|
| 184 |
var url = data.data.url; |
| 185 |
var a = document.createElement('a'), ev = document.createEvent("MouseEvents"); |
| 186 |
|
| 187 |
a.href = url; |
| 188 |
a.download = url.slice(url.lastIndexOf('/') + 1); |
| 189 |
ev.initMouseEvent("click", true, false, self, 0, 0, 0, 0, 0, |
| 190 |
false, false, false, false, 0, null); |
| 191 |
a.dispatchEvent(ev); |
| 192 |
} |
| 193 |
|
| 194 |
if ($action === 'wpas_tools_log_viewer_delete') { |
| 195 |
|
| 196 |
// Alert user of successful deletion. |
| 197 |
disableInputs(true, '', wpas_log_viewer_current_file + ' successfully deleted.'); |
| 198 |
|
| 199 |
/* |
| 200 |
Delete the log viewer controls associated with this file. |
| 201 |
*/ |
| 202 |
var parent = $('div.log-viewer-controls.' + safeClassName); |
| 203 |
var head = parent.prev('h3'); |
| 204 |
parent.add(head).fadeOut('slow', function () { |
| 205 |
$(parent).remove(); |
| 206 |
}); |
| 207 |
|
| 208 |
} |
| 209 |
|
| 210 |
else if ($action === 'wpas_tools_log_viewer_download') { |
| 211 |
} |
| 212 |
|
| 213 |
// View |
| 214 |
else if ($action === 'wpas_tools_log_viewer_view') { |
| 215 |
|
| 216 |
disableInputs(false, wpas_log_viewer_current_file, data.data.status['message']); |
| 217 |
|
| 218 |
$('.' + safeClassName + ' .lastmodified').html(data.data.fileinfo.lastmodified); |
| 219 |
$('.' + safeClassName + ' .created').html(data.data.fileinfo.created); |
| 220 |
$('.' + safeClassName + ' .filesize').html(data.data.fileinfo.filesize); |
| 221 |
|
| 222 |
var $txt = $('textarea#content'); |
| 223 |
$txt.val(data.data.data.join("\n")); |
| 224 |
|
| 225 |
// Auto adjust textarea height to fit content |
| 226 |
$txt.css('height', 'auto'); |
| 227 |
var scrollH = $txt[0].scrollHeight; |
| 228 |
if (scrollH > 350) { |
| 229 |
$txt.css('height', Math.min(1500, scrollH + 30) + 'px'); |
| 230 |
} else { |
| 231 |
$txt.css('height', '350px'); |
| 232 |
} |
| 233 |
|
| 234 |
console.log(data); |
| 235 |
} |
| 236 |
} |
| 237 |
}) |
| 238 |
.done(function (data) { |
| 239 |
$('body').css('cursor', 'auto'); |
| 240 |
//disableInputs(false); |
| 241 |
//$("#overlay").fadeOut(); |
| 242 |
}) |
| 243 |
.fail(function (data) { |
| 244 |
alert('Failed.'); |
| 245 |
console.log('Failed AJAX Call :( /// Return Data: ' + data); |
| 246 |
}); |
| 247 |
}); |
| 248 |
|
| 249 |
|
| 250 |
/* |
| 251 |
* Number of lines to display. Triggers View action. |
| 252 |
*/ |
| 253 |
$('select#lines').change(function () { |
| 254 |
$('a.wpas-tools-log-view.' + safeClassName).trigger('click'); |
| 255 |
}); |
| 256 |
|
| 257 |
/* |
| 258 |
* Clear display. |
| 259 |
*/ |
| 260 |
$('button#clear_content').click(function () { |
| 261 |
disableInputs(true, '', 'Ready.'); |
| 262 |
}); |
| 263 |
|
| 264 |
/* |
| 265 |
* Initialize log files accordion with auto-expanding container height |
| 266 |
*/ |
| 267 |
$("#accordion").accordion({ |
| 268 |
active: false, |
| 269 |
collapsible: true, |
| 270 |
heightStyle: "content", |
| 271 |
activate: function(event, ui) { |
| 272 |
var container = $('#wpas-log-files-container'); |
| 273 |
if (ui.newHeader && ui.newHeader.length) { |
| 274 |
setTimeout(function() { |
| 275 |
var totalH = $('#accordion').outerHeight() + 40; |
| 276 |
container.css('max-height', Math.max(500, totalH) + 'px'); |
| 277 |
}, 100); |
| 278 |
} else { |
| 279 |
container.css('max-height', '500px'); |
| 280 |
} |
| 281 |
} |
| 282 |
}).show(); |
| 283 |
|
| 284 |
|
| 285 |
function accordion_expand_all() { |
| 286 |
var sections = $('#accordion').find("h3"); |
| 287 |
sections.each(function (index, section) { |
| 288 |
if ($(section).hasClass('ui-state-default') && !$(section).hasClass('accordion-header-active')) { |
| 289 |
$(section).click(); |
| 290 |
} |
| 291 |
}); |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
function accordion_collapse_all() { |
| 296 |
var sections = $('#accordion').find("h3"); |
| 297 |
sections.each(function (index, section) { |
| 298 |
if ($(section).hasClass('ui-state-active')) { |
| 299 |
$(section).click(); |
| 300 |
} |
| 301 |
}); |
| 302 |
} |
| 303 |
|
| 304 |
function statusMessage(message) { |
| 305 |
$('#log-viewer-status').html(message); |
| 306 |
} |
| 307 |
|
| 308 |
function disableInputs(disable, filename, statusmessage) { |
| 309 |
|
| 310 |
var download_color, delete_color, clear_color, wrap_on_color, wrap_off_color; |
| 311 |
|
| 312 |
if (!disable) { |
| 313 |
$('body').css('cursor', 'wait'); |
| 314 |
statusMessage(statusmessage); |
| 315 |
$('i.fa').removeClass('disabled'); |
| 316 |
|
| 317 |
download_color = "green"; |
| 318 |
delete_color = "red"; |
| 319 |
clear_color = "black"; |
| 320 |
wrap_off_color = "black"; |
| 321 |
wrap_on_color = "black"; |
| 322 |
} |
| 323 |
else { |
| 324 |
|
| 325 |
download_color = delete_color = clear_color = wrap_off_color = wrap_on_color = "lightgray"; |
| 326 |
|
| 327 |
statusMessage(statusmessage); |
| 328 |
$('body').css('cursor', 'auto'); |
| 329 |
$('i.fa').addClass('disabled'); |
| 330 |
$('textarea#content').val(''); |
| 331 |
} |
| 332 |
|
| 333 |
if (filename !== '') { |
| 334 |
safeClassName = wpas_log_viewer_current_file.replace(/[!\"#$%&'\(\)\*\+,\.\/:;<=>\?\@\[\\\]\^`\{\|\}~]/g, '-'); |
| 335 |
safeClassName = safeClassName.replace(/ /g, ''); |
| 336 |
safeClassName = safeClassName.replace(/-{2,}/g, '-'); |
| 337 |
safeClassName = safeClassName.toLowerCase(); |
| 338 |
} |
| 339 |
else { |
| 340 |
accordion_collapse_all(); |
| 341 |
$('button#delete').data('filename', ''); |
| 342 |
$('button#download').data('filename', ''); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
$('button#clear_content i.fa').css('color', clear_color); |
| 347 |
$('button#download i.fa').css('color', download_color); |
| 348 |
$('button#delete i.fa').css('color', delete_color); |
| 349 |
|
| 350 |
$('button#wrap-off i.fa').css('color', wrap_off_color); |
| 351 |
$('button#wrap-on i.fa').css('color', wrap_off_color); |
| 352 |
|
| 353 |
$('button#clear_content').attr('disabled', disable); |
| 354 |
$('button#download').attr('disabled', disable); |
| 355 |
$('button#delete').attr('disabled', disable); |
| 356 |
|
| 357 |
$('button#wrap-off').attr('disabled', disable); |
| 358 |
$('button#wrap-on').attr('disabled', disable); |
| 359 |
$('select#lines').attr('disabled', disable); |
| 360 |
$('textarea#content').attr('disabled', disable); |
| 361 |
|
| 362 |
var parent = $('div.log-viewer-controls.' + safeClassName); |
| 363 |
var head = parent.prev('h3'); |
| 364 |
parent.find('a').attr('disabled', disable); |
| 365 |
} |
| 366 |
|
| 367 |
}); |
| 368 |
</script> |
| 369 |
<?php |
| 370 |
} |
| 371 |
add_action( 'admin_footer', 'wpas_tools_log_viewer_ajax_script' ); |
| 372 |
?> |
| 373 |
|
| 374 |
<table class="widefat wpas-tools-log-viewer" style="background-color:#f1f1f1;"> |
| 375 |
|
| 376 |
<thead> |
| 377 |
<tr> |
| 378 |
<th data-override="key" class="row-title" width="289"> |
| 379 |
<strong><?php esc_html_e( 'Server Logs', 'awesome-support' ); ?></strong></th> |
| 380 |
<th data-override="value"> |
| 381 |
|
| 382 |
<div style="float: left;"> |
| 383 |
|
| 384 |
<button id="clear_content" |
| 385 |
data-action="" |
| 386 |
data-filename="" |
| 387 |
class="button-secondary wpas-tools-log-clear" |
| 388 |
disabled="disabled"><i |
| 389 |
class="fa fa-eraser fa-fw" |
| 390 |
style="color:lightgray;"></i><?php esc_html_e( 'Clear', 'awesome-support' ); ?></button> |
| 391 |
|
| 392 |
<button id="download" |
| 393 |
class="button-secondary wpas-tools-log-download" |
| 394 |
data-action="wpas_tools_log_viewer_download" |
| 395 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpas_tools_log_viewer_download' ) ); ?>" |
| 396 |
data-filename="" |
| 397 |
disabled="disabled"><i |
| 398 |
class="fa fa-arrow-circle-down fa-fw" |
| 399 |
style="color:lightgray;"></i><?php esc_html_e( 'Download', 'awesome-support' ); ?></button> |
| 400 |
|
| 401 |
<button id="delete" |
| 402 |
class="button-secondary wpas-tools-log-delete" |
| 403 |
data-action="wpas_tools_log_viewer_delete" |
| 404 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpas_tools_log_viewer_delete' ) ); ?>" |
| 405 |
data-filename="" |
| 406 |
disabled="disabled"><i |
| 407 |
class="fa fa-minus-circle fa-fw" |
| 408 |
style="color: lightgray;"></i><?php esc_html_e( 'Delete', 'awesome-support' ); ?></button> |
| 409 |
|
| 410 |
</div> |
| 411 |
|
| 412 |
<div id="log-viewer-status" |
| 413 |
style="float: left;height: 27px; min-width: 320px; line-height: 26px;border: 1px solid lightgray;margin-left: 20px;padding: 0 10px;"> |
| 414 |
Ready. |
| 415 |
</div> |
| 416 |
|
| 417 |
<div style="float: right;"> |
| 418 |
|
| 419 |
<label for="lines"><?php esc_html_e( 'Max Lines', 'awesome-support' ); ?></label> |
| 420 |
<select id="lines"> |
| 421 |
<option value="50">50</option> |
| 422 |
<option value="500">500</option> |
| 423 |
<option value="5000">5000</option> |
| 424 |
<option value="All">All</option> |
| 425 |
</select> |
| 426 |
|
| 427 |
<button id="wrap-on" |
| 428 |
data-wrap="soft" |
| 429 |
class="button-secondary" |
| 430 |
onclick="setWrap('soft');" |
| 431 |
disabled="disabled"><i |
| 432 |
class="fa fa-align-left fa-fw" |
| 433 |
style="color: lightgray;"></i></button> |
| 434 |
<button id="wrap-off" |
| 435 |
data-wrap="off" |
| 436 |
class="button-secondary" |
| 437 |
onclick="setWrap('off');" |
| 438 |
disabled="disabled"><i |
| 439 |
class="fa fa-align-justify fa-fw" |
| 440 |
style="color: lightgray;"></i></button> |
| 441 |
</div> |
| 442 |
|
| 443 |
</th> |
| 444 |
</tr> |
| 445 |
</thead> |
| 446 |
|
| 447 |
<tbody> |
| 448 |
|
| 449 |
<tr> |
| 450 |
<td class="row-title" style=""> |
| 451 |
|
| 452 |
<div id="wpas-log-files-container" style="max-height: 500px; overflow-y: auto; transition: max-height 0.3s ease;"> |
| 453 |
<div id="accordion" style="width: 100%; display: none;"> |
| 454 |
|
| 455 |
<?php |
| 456 |
|
| 457 |
$ar = dirToArray(); |
| 458 |
|
| 459 |
foreach( $ar as $file ) { |
| 460 |
|
| 461 |
$classfromfilename = sanitize_title( $file ); |
| 462 |
?> |
| 463 |
|
| 464 |
<h3 class="log-viewer-filename <?php echo esc_attr( $classfromfilename ); ?>" |
| 465 |
style="font-size: 14px;"><a href="#" |
| 466 |
data-filename="<?php echo esc_attr( $file ); ?>" |
| 467 |
data-action="wpas_tools_log_viewer_view" |
| 468 |
data-nonce="<?php echo esc_attr( wp_create_nonce( 'wpas_tools_log_viewer_view' ) ); ?>" |
| 469 |
class="wpas-tools-log-view <?php echo esc_attr( $classfromfilename ); ?>"><i |
| 470 |
class="fa fa-chevron-right fa-fw" |
| 471 |
style="color: dimgray; font-size: 12px;"></i><?php echo esc_attr( $file ); ?></a> |
| 472 |
</h3> |
| 473 |
|
| 474 |
<div class="log-viewer-controls <?php echo esc_attr( $classfromfilename ); ?>" style="width: 100%;"> |
| 475 |
|
| 476 |
<table width="100%"> |
| 477 |
<tr> |
| 478 |
<th>File Size:</th> |
| 479 |
<td><span class="filesize"></span></td> |
| 480 |
</tr> |
| 481 |
<tr> |
| 482 |
<th>Last Modified:</th> |
| 483 |
<td><span class="lastmodified"></span></td> |
| 484 |
</tr> |
| 485 |
<tr> |
| 486 |
<th>Created:</th> |
| 487 |
<td><span class="created"></span></td> |
| 488 |
</tr> |
| 489 |
</table> |
| 490 |
|
| 491 |
<br/> |
| 492 |
|
| 493 |
</div> |
| 494 |
|
| 495 |
<?php |
| 496 |
} |
| 497 |
?> |
| 498 |
</div> |
| 499 |
</div> |
| 500 |
|
| 501 |
</td> |
| 502 |
<td> |
| 503 |
|
| 504 |
<textarea id="content" cols="150" rows="25" style="width: 100%;" |
| 505 |
wrap="soft" |
| 506 |
readonly disabled="disabled"></textarea> |
| 507 |
<br/> |
| 508 |
</td> |
| 509 |
</tr> |
| 510 |
|
| 511 |
</tbody> |
| 512 |
|
| 513 |
</table> |
| 514 |
|