Second Pass

This commit is contained in:
2026-01-13 15:37:46 +01:00
parent 4f21d10383
commit 61606fd793
24 changed files with 1516 additions and 0 deletions

View File

@@ -0,0 +1,116 @@
<?php
declare(strict_types=1);
defined('ABSPATH') or die;
class Crypto {
private const VERSION = '1.1';
// --- Configuration ---
private const CIPHER = 'aes-256-gcm';
private const NONCE_LEN = 12;
private const TAG_LEN = 16;
private static function getKey(): string {
$material = (defined('CRYPTO_SALT_1') ? CRYPTO_SALT_1 : AUTH_SALT) .
(defined('CRYPTO_SALT_2') ? CRYPTO_SALT_2 : SECURE_AUTH_SALT) .
'crypto-v1';
return hash('sha256', $material, true); // 32 bytes for AES-256
}
public static function encryptSecret(?string $plaintext): ?string {
if (!is_string($plaintext) || $plaintext === '') {
return null;
}
$key = self::getKey();
$nonce = random_bytes(self::NONCE_LEN);
$ciphertext = openssl_encrypt(
$plaintext,
self::CIPHER,
$key,
OPENSSL_RAW_DATA,
$nonce,
$tag,
'',
self::TAG_LEN
);
if ($ciphertext === false) {
return null;
}
// Package as: [nonce | tag | ciphertext], then base64-encode for storage
return base64_encode($nonce . $tag . $ciphertext);
}
public static function decryptSecret(?string $encoded): ?string {
if (!is_string($encoded) || $encoded === '') {
return null;
}
$blob = base64_decode($encoded, true);
if ($blob === false) {
return null;
}
if (strlen($blob) < self::NONCE_LEN + self::TAG_LEN) {
return null;
}
$nonce = substr($blob, 0, self::NONCE_LEN);
$tag = substr($blob, self::NONCE_LEN, self::TAG_LEN);
$ciphertext = substr($blob, self::NONCE_LEN + self::TAG_LEN);
$key = self::getKey();
$plaintext = openssl_decrypt(
$ciphertext,
self::CIPHER,
$key,
OPENSSL_RAW_DATA,
$nonce,
$tag,
''
);
return ($plaintext === false) ? null : $plaintext;
}
public static function makeKey(bool $random = false, string $prefix = ''): string {
if ($random) {
return $prefix . bin2hex(random_bytes(16));
}
return $prefix . self::uuid7();
}
private static function uuid7(): string {
static $lastUnixMs = null;
static $sequence = 0;
$unixMs = (int) floor(microtime(true) * 1000);
if ($unixMs === $lastUnixMs) {
$sequence = ($sequence + 1) & 0x3FFF;
if ($sequence === 0) {
$unixMs++;
}
} else {
$sequence = random_int(0, 0x3FFF);
$lastUnixMs = $unixMs;
}
$time_high = ($unixMs >> 16) & 0xFFFFFFFF;
$time_low = $unixMs & 0xFFFF;
$time_hi_and_version = ($time_low & 0x0FFF) | (0x7 << 12);
$clock_seq_hi_and_reserved = ($sequence & 0x3FFF) | 0x8000;
$randHex = bin2hex(random_bytes(6));
return sprintf(
'%08x-%04x-%04x-%04x-%012s',
$time_high,
$time_low,
$time_hi_and_version,
$clock_seq_hi_and_reserved,
$randHex
);
}
}

View File

@@ -0,0 +1,97 @@
<?php
defined('ABSPATH') or die;
/**
* Centralized logging and admin notices for the MiniCRM integration.
*/
class Minicrm_Integration_Pb_Logger {
/**
* Log an error and optionally raise an admin notice.
*/
public static function log($message, array $context = [], $raise_notice = true) {
if ( $raise_notice ) {
update_option(
'minicrm_integration_last_error',
[
'message' => $message,
'context' => $context,
'time' => self::now_utc_plus_one()
],
false
);
}
if ( class_exists( '\WC_Logger' ) ) {
$logger = wc_get_logger();
$logger->error($message, array_merge(['source' => 'minicrm-integration'], $context));
return;
}
$context_str = '';
if ( ! empty($context) ) {
$context_str = ' | ' . wp_json_encode($context);
}
error_log('[minicrm-integration] ' . $message . $context_str);
}
/**
* Current time in UTC+1 (Y-m-d H:i:s) using wp_date().
*/
private static function now_utc_plus_one() {
return wp_date( 'Y-m-d H:i:s', time() + HOUR_IN_SECONDS, new DateTimeZone( 'UTC' ) );
}
/**
* Show latest MiniCRM sync error in the admin UI so someone can act on it.
*/
public static function render_admin_notice() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return;
}
$error = get_option( 'minicrm_integration_last_error' );
if ( ! $error || empty( $error['message'] ) ) {
return;
}
$message = esc_html( $error['message'] );
$time = ! empty( $error['time'] ) ? esc_html( $error['time'] ) : '';
$context = '';
if ( ! empty( $error['context'] ) ) {
$context = '<pre style="white-space:pre-wrap;word-break:break-word;margin-top:4px;">' . esc_html( wp_json_encode( $error['context'], JSON_PARTIAL_OUTPUT_ON_ERROR ) ) . '</pre>';
}
$dismiss_url = wp_nonce_url(
add_query_arg( 'minicrm_clear_error', '1' ),
'minicrm_clear_error',
'minicrm_nonce'
);
printf(
'<div class="notice notice-error"><p><strong>MiniCRM integráció hiba:</strong> %s%s%s</p><p><a href="%s" class="button">Megjegyezve, elrejtés</a></p></div>',
$message,
$time ? ' <em>(' . $time . ')</em>' : '',
$context,
esc_url( $dismiss_url )
);
}
/**
* Clear error notice when user dismisses via link.
*/
public static function handle_admin_notice_dismiss() {
if ( ! isset( $_GET['minicrm_clear_error'], $_GET['minicrm_nonce'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_GET['minicrm_nonce'], 'minicrm_clear_error' ) ) {
return;
}
delete_option( 'minicrm_integration_last_error' );
wp_safe_redirect( remove_query_arg( [ 'minicrm_clear_error', 'minicrm_nonce' ] ) );
exit;
}
}