PluginProbe
DecaLog / 3.0.2
DecaLog v3.0.2
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-wwwprocessor.php

class-wwwprocessor.php in DecaLog 3.0.2, at includes/processors/class-wwwprocessor.php

108 lines 3.2 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 * Web records processing
4 *
5 * Extends Decalog\Processor\WebProcessor to respect 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\Hash;
15 use Monolog\Processor\WebProcessor;
16 use Decalog\System\Environment;
17 use Decalog\System\IP;
18
19 /**
20 * Define the WWW processor functionality.
21 *
22 * Extends Decalog\Processor\WebProcessor to respect privacy settings.
23 *
24 * @package Processors
25 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
26 * @since 1.0.0
27 */
28 class WWWProcessor extends WebProcessor {
29
30 /**
31 * Obfuscation switch.
32 *
33 * @since 1.0.0
34 * @var boolean $obfuscation Is obfuscation activated?
35 */
36 private $obfuscation = false;
37
38 /**
39 * Initializes the class and set its properties.
40 *
41 * @since 1.0.0
42 * @param array|ArrayAccess|null $serverData Array or object w/ ArrayAccess that provides access to the $_SERVER data.
43 * @param array|null $extraFields Field names and the related key inside $serverData to be added. If not provided it defaults to: url, ip, http_method, server, referrer.
44 * @param boolean $obfuscation Optional. Is obfuscation activated?
45 */
46 public function __construct( $serverData = null, array $extraFields = null, $obfuscation = false ) {
47 parent::__construct( $serverData, $extraFields );
48 $this->obfuscation = $obfuscation;
49 }
50
51 /**
52 * Normalize a string.
53 *
54 * @param string $string The string.
55 * @return string The normalized string.
56 * @since 1.10.0+
57 */
58 private function normalize_string( $string ) {
59 $string = str_replace( '"', '', $string );
60 $string = str_replace( '\'', '`', $string );
61 return filter_var( $string, FILTER_SANITIZE_STRING );
62 }
63
64 /**
65 * Normalize an array.
66 *
67 * @param mixed $array The array.
68 * @return mixed The normalized array.
69 * @since 1.10.0+
70 */
71 private function normalize_array( $array ) {
72 array_walk_recursive( $array, function ( &$item, $key ) { if ( is_string( $item ) ) { $item = $this->normalize_string( $item ); } } );
73 return $array;
74 }
75
76 /**
77 * Invocation of the processor.
78 *
79 * @since 1.0.0
80 * @param array $record Array or added records.
81 * @@return array The modified records.
82 */
83 public function __invoke( array $record ): array {
84 $record['extra']['ip'] = IP::get_current();
85 if ( array_key_exists( 'HTTP_USER_AGENT', $_SERVER ) ) {
86 $record['extra']['ua'] = filter_input( INPUT_SERVER, 'HTTP_USER_AGENT' );
87 }
88 if ( array_key_exists( 'REQUEST_URI', $_SERVER ) ) {
89 $record['extra']['url'] = filter_input( INPUT_SERVER, 'REQUEST_URI' );
90 }
91 if ( array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
92 $record['extra']['http_method'] = filter_input( INPUT_SERVER, 'REQUEST_METHOD' );
93 }
94 if ( array_key_exists( 'SERVER_NAME', $_SERVER ) ) {
95 $record['extra']['server'] = filter_input( INPUT_SERVER, 'SERVER_NAME' );
96 }
97 if ( array_key_exists( 'HTTP_REFERER', $_SERVER ) ) {
98 $record['extra']['referrer'] = filter_input( INPUT_SERVER, 'HTTP_REFERER' );
99 }
100 if ( $this->obfuscation ) {
101 if ( array_key_exists( 'ip', $record['extra'] ) ) {
102 $record['extra']['ip'] = Hash::simple_hash( $record['extra']['ip'] );
103 }
104 }
105 return $this->normalize_array( $record );
106 }
107 }
108