| 1 |
<?php declare(strict_types=1); |
| 2 |
/** |
| 3 |
* WordPress records processing |
| 4 |
* |
| 5 |
* Adds WordPress specific records with respect to privacy settings. |
| 6 |
* |
| 7 |
* @package Processors |
| 8 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Decalog\Processor; |
| 13 |
|
| 14 |
use Decalog\System\Environment; |
| 15 |
use Monolog\Processor\ProcessorInterface; |
| 16 |
use Decalog\System\Blog; |
| 17 |
use Decalog\System\Hash; |
| 18 |
use Decalog\System\User; |
| 19 |
use Decalog\System\IP; |
| 20 |
|
| 21 |
/** |
| 22 |
* Define the WordPress processor functionality. |
| 23 |
* |
| 24 |
* Adds WordPress specific records with respect to privacy settings. |
| 25 |
* |
| 26 |
* @package Processors |
| 27 |
* @author Pierre Lannoy <https://pierre.lannoy.fr/>. |
| 28 |
* @since 1.0.0 |
| 29 |
*/ |
| 30 |
class WordpressProcessor implements ProcessorInterface { |
| 31 |
|
| 32 |
/** |
| 33 |
* Pseudonymization switch. |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* @var boolean $pseudonymize Is pseudonymization activated? |
| 37 |
*/ |
| 38 |
private $pseudonymize = false; |
| 39 |
|
| 40 |
/** |
| 41 |
* Obfuscation switch. |
| 42 |
* |
| 43 |
* @since 1.0.0 |
| 44 |
* @var boolean $obfuscation Is obfuscation activated? |
| 45 |
*/ |
| 46 |
private $obfuscation = false; |
| 47 |
|
| 48 |
/** |
| 49 |
* Initializes the class and set its properties. |
| 50 |
* |
| 51 |
* @since 1.0.0 |
| 52 |
* @param boolean $pseudonymize Optional. Is pseudonymization activated? |
| 53 |
* @param boolean $obfuscation Optional. Is obfuscation activated? |
| 54 |
*/ |
| 55 |
public function __construct( $pseudonymize = true, $obfuscation = true ) { |
| 56 |
$this->pseudonymize = $pseudonymize; |
| 57 |
$this->obfuscation = $obfuscation; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Normalize a string. |
| 62 |
* |
| 63 |
* @param string $string The string. |
| 64 |
* @return string The normalized string. |
| 65 |
* @since 1.10.0+ |
| 66 |
*/ |
| 67 |
private function normalize_string( $string ) { |
| 68 |
$string = str_replace( '"', '“', $string ); |
| 69 |
$string = str_replace( '\'', '`', $string ); |
| 70 |
return filter_var( $string, FILTER_SANITIZE_STRING ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Normalize an array. |
| 75 |
* |
| 76 |
* @param mixed $array The array. |
| 77 |
* @return mixed The normalized array. |
| 78 |
* @since 1.10.0+ |
| 79 |
*/ |
| 80 |
private function normalize_array( $array ) { |
| 81 |
array_walk_recursive( |
| 82 |
$array, |
| 83 |
function ( &$item, $key ) { |
| 84 |
if ( is_string( $item ) ) { |
| 85 |
$item = $this->normalize_string( $item ); |
| 86 |
} } |
| 87 |
); |
| 88 |
return $array; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Invocation of the processor. |
| 93 |
* |
| 94 |
* @since 1.0.0 |
| 95 |
* @param array $record Array or added records. |
| 96 |
* @@return array The modified records. |
| 97 |
*/ |
| 98 |
public function __invoke( array $record ): array { |
| 99 |
$record['extra']['siteid'] = Blog::get_current_blog_id( 0 ); |
| 100 |
$record['extra']['sitename'] = Blog::get_current_blog_name(); |
| 101 |
$record['extra']['userid'] = User::get_current_user_id( 0 ); |
| 102 |
$record['extra']['username'] = User::get_current_user_name(); |
| 103 |
if ( 0 !== (int) $record['extra']['userid'] ) { |
| 104 |
$record['extra']['usersession'] = Hash::simple_hash( wp_get_session_token(), false ); |
| 105 |
} |
| 106 |
$record['extra']['ip'] = IP::get_current(); |
| 107 |
if ( $this->obfuscation ) { |
| 108 |
if ( array_key_exists( 'ip', $record['extra'] ) ) { |
| 109 |
$record['extra']['ip'] = Hash::simple_hash( $record['extra']['ip'] ); |
| 110 |
} |
| 111 |
} |
| 112 |
if ( $this->pseudonymize ) { |
| 113 |
if ( array_key_exists( 'userid', $record['extra'] ) ) { |
| 114 |
if ( $record['extra']['userid'] > 0 ) { |
| 115 |
$record['extra']['userid'] = Hash::simple_hash( (string) $record['extra']['userid'] ); |
| 116 |
if ( array_key_exists( 'username', $record['extra'] ) ) { |
| 117 |
$record['extra']['username'] = Hash::simple_hash( $record['extra']['username'] ); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
return $this->normalize_array( $record ); |
| 123 |
} |
| 124 |
} |
| 125 |
|