File: /var/www/html/wp-content/plugins/adnkronos/admin/log-panel.php
<?php
/*
Admin Panel Option
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
if (!class_exists('adnk_importer_Logs')) {
class adnk_importer_Logs
{
/**
* Start things up
*
* @since 1.0.0
*/
public function __construct()
{
// We only need to register the admin panel on the back-end
if (is_admin()) {
add_action('admin_menu', array('adnk_importer_Logs', 'add_settings_menu_page'), 20); // Priorità più alta
}
}
/**
* Add sub menu page
*
* @since 1.0.0
*/
public static function add_settings_menu_page()
{
// Verifica che il menu parent esista
if (!menu_page_url('adnk-plugin-settings', false)) {
return; // Se il parent non esiste, non aggiungere la sottopagina
}
add_submenu_page(
'adnk-plugin-settings',
'Log importazioni',
'Log importazioni',
'manage_options',
'adnk-plugin-log',
array('adnk_importer_Logs', 'adnk_importer_logs_page')
);
}
// Funzione helper per formattare le dimensioni dei file
private static function adnk_format_file_size($bytes)
{
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, 2) . ' ' . $units[$pow];
}
// Funzione helper per l'attributo selected
private static function adnk_selected($value1, $value2)
{
return "{$value1}" === "{$value2}" ? ' selected="selected"' : '';
}
private static function adnk_sanitize_text_field($text)
{
// Rimuove caratteri nulli
$text = str_replace(chr(0), '', $text);
// Rimuove spazi bianchi all'inizio e alla fine
$text = trim($text);
// Normalizza whitespace
$text = preg_replace('/\s+/', ' ', $text);
// Rimuove caratteri di controllo
$text = preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/u', '', $text);
return $text;
}
public static function adnk_importer_logs_page()
{
// IMPORTANTE: Verifica delle autorizzazioni
if (!current_user_can('manage_options')) {
wp_die(__('Non hai sufficienti permessi per accedere a questa pagina.'));
}
?>
<div class="wrap">
<h1>ADNK Importer - Log Viewer</h1>
<p></p>
<nav class="nav-tab-wrapper">
<a href="?page=adnk-plugin-settings" class="nav-tab"><?php esc_html_e('Impostazioni di Importazione', 'adnk_importer'); ?></a>
<a href="?page=adnk-plugin-account" class="nav-tab"><?php esc_html_e('Account Settings', 'adnk_importer'); ?></a>
<a href="?page=adnk-plugin-log" class="nav-tab nav-tab-active"><?php esc_html_e('Log importazioni', 'adnkronos'); ?></a>
</nav>
<?php
// Ottieni la lista dei file di log disponibili
$logs_dir = plugin_dir_path(dirname(__FILE__)) . 'logs/';
$log_files = [];
// Verifica che la directory esista e sia leggibile
if (!is_dir($logs_dir) || !is_readable($logs_dir)) {
echo '<div class="notice notice-error"><p>Directory dei log non accessibile: ' . esc_html($logs_dir) . '</p></div>';
return;
}
foreach (glob($logs_dir . "*_import.log") as $log_file) {
$filename = basename($log_file);
if (preg_match('/^(\d{8})_import\.log$/', $filename, $matches)) {
$date = $matches[1];
$formatted_date = date('d/m/Y', strtotime(substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2)));
// Cerca il file CSV corrispondente
$csv_pattern = $logs_dir . $date . '_*_import.csv';
$csv_files = glob($csv_pattern);
$csv_file = !empty($csv_files) ? $csv_files[0] : null;
$log_files[$date] = [
'date' => $formatted_date,
'log_file' => $log_file,
'log_size' => filesize($log_file),
'csv_file' => $csv_file,
'csv_size' => $csv_file ? filesize($csv_file) : 0,
'hostname' => $csv_file ? preg_replace('/^' . $date . '_(.+)_import\.csv$/', '$1', basename($csv_file)) : '',
'raw_date' => $date
];
}
}
// Ordina per data decrescente
krsort($log_files);
if (empty($log_files)) {
echo '<div class="notice notice-info"><p>Nessun file di log trovato.</p></div>';
return;
}
// Determina quale data visualizzare
$selected_date = isset($_GET['date']) ? self::adnk_sanitize_text_field($_GET['date']) : key($log_files);
$selected_log = isset($log_files[$selected_date]) ? $log_files[$selected_date] : null;
if (!$selected_log) {
echo '<div class="notice notice-error"><p>Log non disponibile.</p></div>';
return;
}
// Analizza i 7 CSV più recenti per il riepilogo delle categorie
$category_summary = [];
$recent_logs = array_slice($log_files, 0, 7, true);
foreach ($recent_logs as $date_key => $log_info) {
if ($log_info['csv_file'] && file_exists($log_info['csv_file'])) {
$csv_handle = fopen($log_info['csv_file'], 'r');
if ($csv_handle === false) {
continue; // Salta se non riesci ad aprire il file
}
$header = fgetcsv($csv_handle, 1000, ';');
// Verifica che l'header sia valido
if ($header === false) {
fclose($csv_handle);
continue;
}
// Trova gli indici delle colonne necessarie
$cat_index = array_search('CATEGORIA', $header);
$import_date_index = array_search('IMPORTATAZIONE', $header);
if ($cat_index !== false && $import_date_index !== false) {
while (($data = fgetcsv($csv_handle, 1000, ';')) !== false) {
if (isset($data[$cat_index]) && !empty($data[$cat_index])) {
$category = $data[$cat_index];
$import_date = isset($data[$import_date_index]) ? $data[$import_date_index] : '';
if (!isset($category_summary[$category])) {
$category_summary[$category] = [
'count' => 0,
'last_import' => '',
'raw_date' => 0
];
}
$category_summary[$category]['count']++;
// Aggiorna la data di ultima importazione se più recente
if (!empty($import_date)) {
$import_timestamp = strtotime($import_date);
if ($import_timestamp &&
($category_summary[$category]['raw_date'] == 0 ||
$import_timestamp > $category_summary[$category]['raw_date'])) {
$category_summary[$category]['last_import'] = $import_date;
$category_summary[$category]['raw_date'] = $import_timestamp;
}
}
}
}
}
fclose($csv_handle);
}
}
// Visualizza il riepilogo delle categorie
if (!empty($category_summary)) {
?>
<h2>Riepilogo Importazioni per Categoria (ultimi 7 giorni)</h2>
<div class="category-summary" style="margin-bottom: 30px;">
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>Categoria</th>
<th>Numero Articoli</th>
<th>Ultima Importazione</th>
</tr>
</thead>
<tbody>
<?php foreach ($category_summary as $category => $data): ?>
<tr>
<td><?php echo esc_html($category); ?></td>
<td><?php echo esc_html($data['count']); ?></td>
<td><?php echo esc_html($data['last_import']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<?php
}
// Dropdown per selezionare la data
?>
<div class="log-selector">
<form method="get" action="">
<input type="hidden" name="page" value="adnk-plugin-log">
<select name="date" id="log-date-selector" onchange="this.form.submit()">
<?php foreach ($log_files as $date_key => $log_info): ?>
<option value="<?php echo esc_attr($date_key); ?>"<?php echo self::adnk_selected($date_key, $selected_date); ?>>
<?php echo esc_html($log_info['date']); ?> -
CSV: <?php echo esc_html(self::adnk_format_file_size($log_info['csv_size'])); ?>,
LOG: <?php echo esc_html(self::adnk_format_file_size($log_info['log_size'])); ?>
</option>
<?php endforeach; ?>
</select>
</form>
</div>
<?php
// Visualizza il contenuto del file CSV in formato tabellare con intestazione fissa
if ($selected_log['csv_file'] && file_exists($selected_log['csv_file'])) {
echo '<div class="csv-header-section">';
echo '<h2 style="display: inline-block;">CSV Import Data</h2>';
echo '<a href="/wp-content/plugins/adnkronos/logs/' . esc_attr($selected_date) . '_' . $_SERVER['SERVER_NAME'] .'_import.csv" class="button button-secondary" style="margin-left: 15px;">Scarica CSV</a>';
echo '</div>';
echo '<div class="csv-viewer">';
echo '<table class="wp-list-table widefat fixed striped csv-data-table">';
$csv_handle = fopen($selected_log['csv_file'], 'r');
if ($csv_handle !== false) {
// Leggi tutti i dati del CSV
$all_rows = [];
while (($data = fgetcsv($csv_handle, 1000, ';')) !== false) {
$all_rows[] = $data;
}
fclose($csv_handle);
if (!empty($all_rows)) {
// Estrai l'header (prima riga)
$header = array_shift($all_rows);
// Inverti l'ordine delle righe rimanenti
$all_rows = array_reverse($all_rows);
// Visualizza l'header
echo '<thead>';
echo '<tr>';
foreach ($header as $header_cell) {
echo '<th>' . esc_html($header_cell) . '</th>';
}
echo '</tr>';
echo '</thead>';
// Visualizza le righe in ordine inverso
echo '<tbody>';
foreach ($all_rows as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . esc_html($cell) . '</td>';
}
echo '</tr>';
}
echo '</tbody>';
}
echo '</table>';
} else {
echo '<p>Impossibile aprire il file CSV.</p>';
}
echo '</div>';
} else {
echo '<p>File CSV non trovato.</p>';
}
// Visualizza il contenuto del file di log
if ($selected_log['log_file'] && file_exists($selected_log['log_file'])) {
echo '<div class="csv-header-section">';
echo '<h2>Log File</h2>';
echo '<a target="_blank" href="/wp-content/plugins/adnkronos/logs/' . esc_attr($selected_date) . '_import.log" class="button button-secondary" style="margin-left: 15px;">Scarica Log</a>';
echo '</div>';
echo '<div class="log-viewer" style="max-height: 400px; overflow-y: auto; background: #f1f1f1; padding: 10px; font-family: monospace;">';
// Leggi e mostra il contenuto del file di log con gestione errori
$log_content = file_get_contents($selected_log['log_file']);
if ($log_content !== false) {
echo '<pre>' . esc_html($log_content) . '</pre>';
} else {
echo '<p>Impossibile leggere il file di log.</p>';
}
echo '</div>';
} else {
echo '<p>File di log non trovato.</p>';
}
?>
</div>
<style>
.log-selector {
margin: 20px 0;
}
#log-date-selector {
min-width: 300px;
}
/* Stile per l'intestazione fissa della tabella CSV */
.csv-viewer {
max-height: 400px;
overflow-y: auto;
margin-bottom: 20px;
position: relative;
}
.csv-data-table {
width: 100%;
}
.csv-data-table thead {
position: sticky;
top: 0;
z-index: 1;
background-color: #f1f1f1;
}
.csv-data-table th {
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}
.csv-header-section {
margin: 20px 0 10px 0;
display: flex;
align-items: center;
}
.csv-header-section h2 {
margin: 0;
}
</style>
<?php
}
}
}
new adnk_importer_Logs();