captaincore/lib/remote-scripts/fetch-log-files
2024-05-08 13:49:08 -04:00

144 lines
No EOL
4.3 KiB
Bash

#!/usr/bin/env bash
#
# Fetches list of server logs
#
read -r -d '' php_code << 'heredoc'
<?php
$debugger = empty( WP_DEBUG ) ? false : WP_DEBUG;
$files = [];
$home_path = get_home_path();
$parent_path = dirname( $home_path );
$wp_content = empty( WP_CONTENT_DIR ) ? "${home_path}wp-content" : WP_CONTENT_DIR;
$wp_content_base = basename( $wp_content );
if (! function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle): bool
{
$needle_len = strlen($needle);
return ($needle_len === 0 || 0 === substr_compare($haystack, $needle, - $needle_len));
}
}
if ( is_file( "${home_path}error_log" ) ) {
$files[] = [
"name" => "error_log",
"path" => "${home_path}error_log",
"created_at" => filectime( "${home_path}error_log" ),
"modified_at" => filemtime( "${home_path}error_log" ),
"size" => filesize( "${home_path}error_log" )
];
}
foreach (glob("${parent_path}/logs/*error.log*") as $file) {
if ( filesize( $file ) == 0 || str_ends_with( $file, '.gz' ) ) {
continue;
}
$files[] = [
"name" => "~/logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
if ( is_file( "${home_path}wp-content/debug.log" ) ) {
$files[] = [
"name" => "${wp_content_base}/debug.log",
"path" => "${wp_content}/debug.log",
"created_at" => filectime( "${wp_content}/debug.log" ),
"modified_at" => filemtime( "${wp_content}/debug.log" ),
"size" => filesize( "${wp_content}/debug.log" )
];
}
foreach (glob("${parent_path}/logs/*access.log*") as $file) {
if ( filesize( $file ) == 0 || str_ends_with( $file, '.gz' ) ) {
continue;
}
$files[] = [
"name" => "~/logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
foreach (glob("${parent_path}/access-logs/*_log") as $file) {
if ( filesize( $file ) == 0 ) {
continue;
}
$files[] = [
"name" => "~/access-logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
foreach (glob("${parent_path}/logs/*mail.log*") as $file) {
if ( filesize( $file ) == 0 || str_ends_with( $file, '.gz' ) ) {
continue;
}
$files[] = [
"name" => "~/logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
foreach (glob("${parent_path}/logs/*cache*.log*") as $file) {
if ( filesize( $file ) == 0 || str_ends_with( $file, '.gz' ) ) {
continue;
}
$files[] = [
"name" => "~/logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
foreach (glob("${wp_content}/uploads/wc-logs/*.log") as $file) {
if ( filesize( $file ) == 0 ) {
continue;
}
$files[] = [
"name" => "$wp_content_base/uploads/wc-logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
foreach (glob("${wp_content}/uploads/gravity_forms/logs/*.txt") as $file) {
if ( filesize( $file ) == 0 ) {
continue;
}
$files[] = [
"name" => "$wp_content_base/uploads/gravity_forms/logs/" . basename( $file ),
"path" => $file,
"created_at" => filectime( $file ),
"modified_at" => filemtime( $file ),
"size" => filesize( $file )
];
}
$response = [
"debugger" => $debugger,
"files" => $files
];
echo json_encode( $response, JSON_PRETTY_PRINT );
heredoc
echo "$php_code" | wp eval-file - --skip-themes --skip-plugins 2>/dev/null