228 lines
9.2 KiB
PHP
228 lines
9.2 KiB
PHP
<?php
|
|
/**
|
|
* Datenschutz: Bilder von Drittanbietern lokal zwischenspeichern.
|
|
*
|
|
* Skin-Köpfe (mc-heads.net, visage.surgeplay.com, minotar.net) und
|
|
* YouTube-Vorschaubilder (img.youtube.com) wurden bisher direkt im Browser
|
|
* des Besuchers geladen. Dabei wäre dessen IP-Adresse ohne Einwilligung an
|
|
* diese Dienste übertragen worden. Jetzt holt der Server das Bild einmal ab,
|
|
* legt es unter wp-content/uploads/mm-proxy/ ab und liefert es von der
|
|
* eigenen Domain aus. Die Besucher sprechen nie mehr mit dem Drittanbieter.
|
|
*
|
|
* Ablauf: mm_proxy_url() gibt die Adresse der gespeicherten Datei zurück,
|
|
* wenn sie schon existiert. Sonst zeigt sie auf den Endpunkt
|
|
* admin-ajax.php?action=mm_proxy, der das Bild holt, speichert und ausliefert.
|
|
*
|
|
* Außerdem: Gravatar (WordPress-Standard für Autoren- und Kommentarbilder)
|
|
* wird abgeschaltet, solange es im Customizer nicht ausdrücklich erlaubt ist.
|
|
*
|
|
* @package Minecraft-Modern-Theme
|
|
*/
|
|
|
|
// Exit if accessed directly.
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/** Wie lange eine gespeicherte Datei als frisch gilt. */
|
|
define( 'MM_PROXY_TTL', 7 * DAY_IN_SECONDS );
|
|
|
|
/**
|
|
* Erlaubte Quellen. Nur diese Vorlagen werden jemals abgerufen.
|
|
*
|
|
* id: Muster für die Kennung (Spielername, UUID oder Video-ID)
|
|
* size: true, wenn die Vorlage eine Pixelgröße enthält
|
|
*/
|
|
function mm_proxy_kinds() {
|
|
$player = '/^(?:[A-Za-z0-9_]{1,16}|[0-9a-fA-F]{32}|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/';
|
|
return array(
|
|
'head' => array( 'url' => 'https://mc-heads.net/avatar/{id}/{size}', 'id' => $player, 'size' => true ),
|
|
'body' => array( 'url' => 'https://mc-heads.net/body/{id}/{size}', 'id' => $player, 'size' => true ),
|
|
'minotar' => array( 'url' => 'https://minotar.net/avatar/{id}/{size}', 'id' => $player, 'size' => true ),
|
|
'bust' => array( 'url' => 'https://visage.surgeplay.com/bust/{id}.png', 'id' => $player, 'size' => false ),
|
|
'full' => array( 'url' => 'https://visage.surgeplay.com/full/{id}.png', 'id' => $player, 'size' => false ),
|
|
'vhead' => array( 'url' => 'https://visage.surgeplay.com/head/{size}/{id}', 'id' => $player, 'size' => true ),
|
|
'ytthumb' => array( 'url' => 'https://img.youtube.com/vi/{id}/maxresdefault.jpg', 'id' => '/^[A-Za-z0-9_-]{11}$/', 'size' => false ),
|
|
);
|
|
}
|
|
|
|
/** Ordner für die gespeicherten Bilder. */
|
|
function mm_proxy_dir() {
|
|
$up = wp_upload_dir( null, false );
|
|
return array(
|
|
'path' => trailingslashit( $up['basedir'] ) . 'mm-proxy/',
|
|
'url' => trailingslashit( $up['baseurl'] ) . 'mm-proxy/',
|
|
);
|
|
}
|
|
|
|
/** Pixelgröße auf einen sinnvollen Bereich begrenzen. */
|
|
function mm_proxy_size( $size ) {
|
|
$size = (int) $size;
|
|
return max( 8, min( 512, $size ?: 64 ) );
|
|
}
|
|
|
|
/** Dateiname im Cache oder '' bei ungültiger Eingabe. */
|
|
function mm_proxy_filename( $kind, $id, $size ) {
|
|
$kinds = mm_proxy_kinds();
|
|
if ( ! isset( $kinds[ $kind ] ) || ! preg_match( $kinds[ $kind ]['id'], (string) $id ) ) {
|
|
return '';
|
|
}
|
|
// Spielernamen sind nicht groß-/kleinschreibungsabhängig, YouTube-IDs schon.
|
|
$id = $kind === 'ytthumb' ? (string) $id : strtolower( str_replace( '-', '', (string) $id ) );
|
|
$name = $kind . '-' . $id;
|
|
if ( $kinds[ $kind ]['size'] ) {
|
|
$name .= '-' . mm_proxy_size( $size );
|
|
}
|
|
// Namen enthalten nur [a-z0-9_-] - so ist ein Ausbruch aus dem Ordner ausgeschlossen.
|
|
return preg_replace( '/[^a-z0-9_-]/', '', $name ) . '.img';
|
|
}
|
|
|
|
/**
|
|
* Adresse des Bildes auf der eigenen Domain.
|
|
*
|
|
* @param string $kind Quelle, siehe mm_proxy_kinds().
|
|
* @param string $id Spielername, UUID oder YouTube-ID.
|
|
* @param int $size Pixelgröße (nur bei Quellen mit Größe).
|
|
* @return string Leer, wenn die Eingabe ungültig ist.
|
|
*/
|
|
function mm_proxy_url( $kind, $id, $size = 64 ) {
|
|
$id = trim( (string) $id );
|
|
$file = mm_proxy_filename( $kind, $id, $size );
|
|
if ( $file === '' ) {
|
|
return '';
|
|
}
|
|
|
|
$dir = mm_proxy_dir();
|
|
$path = $dir['path'] . $file;
|
|
// Frische Datei vorhanden: direkt ausliefern, ohne PHP.
|
|
if ( is_file( $path ) && ( time() - filemtime( $path ) ) < MM_PROXY_TTL ) {
|
|
return $dir['url'] . $file;
|
|
}
|
|
|
|
return add_query_arg(
|
|
array( 'action' => 'mm_proxy', 'k' => $kind, 'i' => $id, 's' => mm_proxy_size( $size ) ),
|
|
admin_url( 'admin-ajax.php' )
|
|
);
|
|
}
|
|
|
|
/** Basisadresse des Endpunkts für Skripte, die Namen erst im Browser zusammensetzen. */
|
|
function mm_proxy_endpoint() {
|
|
return admin_url( 'admin-ajax.php?action=mm_proxy' );
|
|
}
|
|
|
|
/** Bild holen, speichern, ausliefern. */
|
|
function mm_proxy_serve() {
|
|
$kind = isset( $_GET['k'] ) ? sanitize_key( wp_unslash( $_GET['k'] ) ) : '';
|
|
$id = isset( $_GET['i'] ) ? sanitize_text_field( wp_unslash( $_GET['i'] ) ) : '';
|
|
$size = isset( $_GET['s'] ) ? (int) $_GET['s'] : 64;
|
|
|
|
$file = mm_proxy_filename( $kind, $id, $size );
|
|
$kinds = mm_proxy_kinds();
|
|
if ( $file === '' ) {
|
|
status_header( 400 );
|
|
exit;
|
|
}
|
|
|
|
$dir = mm_proxy_dir();
|
|
$path = $dir['path'] . $file;
|
|
$fresh = is_file( $path ) && ( time() - filemtime( $path ) ) < MM_PROXY_TTL;
|
|
|
|
if ( ! $fresh ) {
|
|
$miss_key = 'mm_proxy_miss_' . md5( $file );
|
|
|
|
// Nach einem Fehlschlag 10 Minuten nicht erneut versuchen.
|
|
if ( get_transient( $miss_key ) ) {
|
|
$fresh = false;
|
|
} elseif ( ! mm_rate_limit( 'proxy', 40, MINUTE_IN_SECONDS ) ) {
|
|
status_header( 429 );
|
|
exit;
|
|
} else {
|
|
$templates = array( $kinds[ $kind ]['url'] );
|
|
if ( $kind === 'ytthumb' ) {
|
|
// Nicht jedes Video hat die höchste Auflösung.
|
|
$templates[] = 'https://img.youtube.com/vi/{id}/hqdefault.jpg';
|
|
}
|
|
$ok = false;
|
|
|
|
foreach ( $templates as $tpl ) {
|
|
$url = str_replace(
|
|
array( '{id}', '{size}' ),
|
|
array( rawurlencode( $id ), mm_proxy_size( $size ) ),
|
|
$tpl
|
|
);
|
|
$res = wp_remote_get( $url, array( 'timeout' => 5, 'redirection' => 2, 'limit_response_size' => 2 * MB_IN_BYTES ) );
|
|
|
|
if ( is_wp_error( $res ) || (int) wp_remote_retrieve_response_code( $res ) !== 200 ) {
|
|
continue;
|
|
}
|
|
$body = wp_remote_retrieve_body( $res );
|
|
// Nur echte Bilder annehmen.
|
|
if ( $body !== '' && strlen( $body ) <= 2 * MB_IN_BYTES && @getimagesizefromstring( $body ) ) { // phpcs:ignore WordPress.PHP.NoSilencedErrors
|
|
if ( wp_mkdir_p( $dir['path'] ) ) {
|
|
if ( ! is_file( $dir['path'] . 'index.html' ) ) {
|
|
file_put_contents( $dir['path'] . 'index.html', '' );
|
|
}
|
|
$ok = false !== file_put_contents( $path, $body, LOCK_EX );
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
if ( ! $ok ) {
|
|
set_transient( $miss_key, 1, 10 * MINUTE_IN_SECONDS );
|
|
}
|
|
}
|
|
}
|
|
|
|
// Auch eine veraltete Datei ist besser als gar kein Bild.
|
|
if ( ! is_file( $path ) ) {
|
|
status_header( 404 );
|
|
header( 'Cache-Control: public, max-age=300' );
|
|
exit;
|
|
}
|
|
|
|
$info = @getimagesize( $path ); // phpcs:ignore WordPress.PHP.NoSilencedErrors
|
|
header( 'Content-Type: ' . ( $info && ! empty( $info['mime'] ) ? $info['mime'] : 'image/png' ) );
|
|
header( 'Content-Length: ' . filesize( $path ) );
|
|
header( 'Cache-Control: public, max-age=86400' );
|
|
header( 'X-Content-Type-Options: nosniff' );
|
|
readfile( $path );
|
|
exit;
|
|
}
|
|
add_action( 'wp_ajax_mm_proxy', 'mm_proxy_serve' );
|
|
add_action( 'wp_ajax_nopriv_mm_proxy', 'mm_proxy_serve' );
|
|
|
|
|
|
// =========================================================================
|
|
// Gravatar abschalten
|
|
// =========================================================================
|
|
|
|
/** Customizer-Schalter. */
|
|
function mm_privacy_customizer( $wp_customize ) {
|
|
$wp_customize->add_setting( 'mm_gravatar_enabled', array(
|
|
'default' => false,
|
|
'sanitize_callback' => 'wp_validate_boolean',
|
|
) );
|
|
$wp_customize->add_control( 'mm_gravatar_enabled', array(
|
|
'label' => __( 'Gravatar erlauben', 'minecraft-modern-theme' ),
|
|
'description' => __( 'Aus: Autoren- und Kommentarbilder werden lokal erzeugt, es wird nichts an gravatar.com übertragen (empfohlen). An: Besucher-IP und E-Mail-Prüfsumme gehen an Gravatar - das muss in der Datenschutzerklärung stehen.', 'minecraft-modern-theme' ),
|
|
'section' => 'mm_cookie_banner_section',
|
|
'type' => 'checkbox',
|
|
) );
|
|
}
|
|
add_action( 'customize_register', 'mm_privacy_customizer', 20 );
|
|
|
|
/** Neutrales Platzhalterbild aus dem Theme. */
|
|
function mm_privacy_default_avatar() {
|
|
return get_template_directory_uri() . '/images/default-avatar.svg';
|
|
}
|
|
|
|
function mm_privacy_avatar_data( $args ) {
|
|
if ( get_theme_mod( 'mm_gravatar_enabled', false ) ) {
|
|
return $args;
|
|
}
|
|
$args['url'] = mm_privacy_default_avatar();
|
|
$args['found_avatar'] = true;
|
|
return $args;
|
|
}
|
|
add_filter( 'pre_get_avatar_data', 'mm_privacy_avatar_data', 20 );
|