PluginProbe
DecaLog / 4.4.0
DecaLog v4.4.0
3.0.2 3.1.0 3.10.0 3.2.0 3.3.0 3.4.0 3.4.1 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.6.3 3.7.0 3.7.1 3.8.0 3.9.0 3.9.1 4.0.0 4.1.0 4.2.0 4.3.0 4.3.1 4.4.0 4.5.0 All 75 releases
decalog / includes / processors / class-wordpressprocessor.php

class-wordpressprocessor.php in DecaLog 4.4.0, at includes/processors/class-wordpressprocessor.php

126 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 DLMonolog\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 decalog_filter_string( $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']['sitedomain'] = Blog::get_current_blog_url();
102 $record['extra']['userid'] = User::get_current_user_id( 0 );
103 $record['extra']['username'] = User::get_current_user_name();
104 if ( 0 !== (int) $record['extra']['userid'] ) {
105 $record['extra']['usersession'] = Hash::simple_hash( wp_get_session_token(), false );
106 }
107 $record['extra']['ip'] = IP::get_current();
108 if ( $this->obfuscation ) {
109 if ( array_key_exists( 'ip', $record['extra'] ) ) {
110 $record['extra']['ip'] = Hash::simple_hash( $record['extra']['ip'] );
111 }
112 }
113 if ( $this->pseudonymize ) {
114 if ( array_key_exists( 'userid', $record['extra'] ) ) {
115 if ( $record['extra']['userid'] > 0 ) {
116 $record['extra']['userid'] = Hash::simple_hash( (string) $record['extra']['userid'] );
117 if ( array_key_exists( 'username', $record['extra'] ) ) {
118 $record['extra']['username'] = Hash::simple_hash( $record['extra']['username'] );
119 }
120 }
121 }
122 }
123 return $this->normalize_array( $record );
124 }
125 }
126