<?php if ( !defined( 'ABSPATH' ) ) exit;//PHPの宣言と直接アクセスの拒否

/***** ダッシュボードに重要ファイルの変更がないかをチェックして表示する *****/
/* 現在時刻を確認済時刻とするがクリックされた後に変更されたファイルをチェックする */
function haup_detect_wp_core_file_changes_widget_init(){
	wp_add_dashboard_widget(
		'hima_art_integrated_monitor',
		__('File integrity monitoring widget','hima-art-utility'),
		'haup_detect_wp_core_file_changes_callback'
	);
}
add_action('wp_dashboard_setup','haup_detect_wp_core_file_changes_widget_init');

function haup_detect_wp_core_file_changes_callback(){
	// 最終更新日時をoptionsテーブルから取得
	$option_key = 'hautility_last_file_security_check_date';
	$last_check_point = get_option($option_key, false);
        
	// 2. WordPress本体の更新日 (version.php) を取得
	$version_file = ABSPATH . 'wp-includes/version.php';
	$core_update_no_date_text = __('unknown','hima-art-utility');
	$core_update_date = file_exists($version_file) ? wp_date(get_option('date_format') . ' ' . get_option('time_format'), filemtime($version_file)) : esc_html( $core_update_no_date_text );

	// 最終確認日時の更新ボタンが押された時の処理
	if (isset($_POST['hima_verify_files']) && check_admin_referer('hima_verify_action')) {
		$last_check_update_success_text = __('The last check date and time for the file integrity monitoring widget has been updated.','hima-art-utility');
		$last_check_point = time();
		update_option($option_key, $last_check_point);
		echo '<div class="updated"><p>'.esc_html( $last_check_update_success_text ).'</p></div>';
	}

	$files = [
		'wp-config.php' => ABSPATH . 'wp-config.php',
		'.htaccess'     => ABSPATH . '.htaccess',
		'index.php'     => ABSPATH . 'index.php',
		'wp-settings.php' => ABSPATH . 'wp-settings.php',
		//'functions.php' => get_template_directory() . '/functions.php',
	];

	// 最終更新日時の表示
	$no_last_check_text = __('Not set','hima-art-utility');
	$display_last_check = $last_check_point ? wp_date(get_option('date_format') . ' ' . get_option('time_format'), $last_check_point) : esc_html( $no_last_check_text );

	//ウィジェットエリアの文字列
	$widget_wp_core_update_date_text = __('Last updated date and time for the WordPress core:','hima-art-utility');
	$widget_last_modified_time_text = __('Last checked:','hima-art-utility'); 
	$widget_file_name_header_text = __('File Name','hima-art-utility'); 
	$widget_file_modified_header_text = __('Last modified date','hima-art-utility'); 
	$widget_file_status_header_text = __('Condition','hima-art-utility'); 
	$widget_file_status_unset_text = __('Unconfirmed','hima-art-utility'); 
	$widget_file_status_unchanged_text = __('No changes','hima-art-utility'); 
	$widget_file_status_changed_text = __('Change Detect','hima-art-utility'); 

	// ヘッダー情報
	echo '<div style="margin-bottom:15px; padding:10px; background:#f0f6fb; border-left:4px solid #11a0d2;">';
		echo esc_html( $widget_wp_core_update_date_text ) . esc_html($core_update_date) . '</b><br>';
		echo esc_html( $widget_last_modified_time_text ) . esc_html($display_last_check) . '</b>';
	echo '</div>';

	echo '<table class="widefat" style="border-collapse: collapse;">';
	echo '<thead><tr><th>'.esc_html( $widget_file_name_header_text ).'</th><th>'.esc_html( $widget_file_modified_header_text ).'</th><th>'.esc_html( $widget_file_status_header_text ).'</th></tr></thead><tbody>';

	foreach ($files as $name => $path) {
		if (file_exists($path)) {
			$mtime = filemtime($path);
			// 各ファイルの更新日時もロケールに合わせて表示
			$display_mtime = wp_date(get_option('date_format') . ' ' . get_option('time_format'), $mtime);
                
			$status = '';
			$row_style = '';

			if ($last_check_point === false) {
				$status = '<span style="color:#999;">'.esc_html( $widget_file_status_unset_text ).'</span>';
			} elseif ($mtime > $last_check_point) {
				$status = '<span style="color:red; font-weight:bold;">'.esc_html( $widget_file_status_changed_text ).'</span>';
				$row_style = 'background-color: #fff0f0;';
			} else {
				$status = '<span style="color:green;">'.esc_html( $widget_file_status_unchanged_text ).'</span>';
			}
                
			echo '<tr style="';
			echo esc_html($row_style);
			echo '">';
			echo '<td><code>';
			echo esc_html($name);
			echo '</code></td>';
			echo '<td>';
			echo esc_html($display_mtime);
			echo '</td>';
			echo '<td>';
			echo wp_kses_post($status);
			echo '</td>';
			echo '</tr>';
		}
	}
	echo '</tbody></table>';

	$widget_button_text = __('Update the last check date and time.','hima-art-utility');
	$widget_description_title = __('About this widget','hima-art-utility');
	$widget_description_text_a = __('This widget retrieves the modification time of the files listed and checks if any changes have been made to the files since the last check date and time, then displays the result.','hima-art-utility');
	$widget_description_text_b = __('If any changes have been made, "Change Detect" will be displayed in the status column.','hima-art-utility');
	$widget_description_text_c = __('The files listed are generally not modified except when WordPress core is updated, when plugins are updated, when code changes are saved, or when files are replaced. Therefore, if an update is not intended, there is a possibility of tampering.','hima-art-utility');
	$widget_description_text_d = __('We recommend that you review the file and check for any suspicious code or other elements.','hima-art-utility');
	$widget_description_text_e = __('If it shows "No changes" for a while, you can narrow down the period during which changes may have occurred by clicking "Update last checked date and time" to update the comparison date and time.','hima-art-utility');
?>
<form method="post" style="margin-top:15px;">
<?php wp_nonce_field('hima_verify_action'); ?>
<input type="submit" name="hima_verify_files" class="haup-dashboard-widget-button" value="<?php echo esc_html( $widget_button_text ); ?>">
</form>
<details class="accordion">
	<summary class="accordion__title" style="text-align:right;cursor: pointer;"><?php echo esc_html( $widget_description_title ); ?></summary>
	<div class="accordion__content">
		<ul>
			<li><?php echo esc_html( $widget_description_text_a ); ?></li>
			<li><?php echo esc_html( $widget_description_text_b ); ?></li>
			<li><?php echo esc_html( $widget_description_text_c ); ?></li>
			<li><?php echo esc_html( $widget_description_text_d ); ?></li>
			<li><?php echo esc_html( $widget_description_text_e ); ?></li>
		</ul>
	</div>
</details>
<?php
}