@@ -32,8 +32,8 @@ public static function logEvent(
32
32
33
33
$ successful = false ;
34
34
35
- try {
36
-
35
+ try
36
+ {
37
37
$ stmt = Common::$ database ->prepare ('
38
38
INSERT INTO `event_log` (
39
39
`event_type_id`, `event_datetime`, `user_id`, `ip_address`,
@@ -57,19 +57,23 @@ public static function logEvent(
57
57
$ successful = $ stmt ->execute ();
58
58
$ stmt ->closeCursor ();
59
59
60
- if ($ successful ) {
60
+ if ($ successful )
61
+ {
61
62
self ::dispatchDiscordWebhook ((int ) Common::$ database ->lastInsertId ());
62
63
}
63
-
64
- } catch (PDOException $ e ) {
64
+ }
65
+ catch (PDOException $ e )
66
+ {
65
67
throw new QueryException ('Cannot log event ' , $ e );
66
-
67
- } finally {
68
+ }
69
+ finally
70
+ {
68
71
return $ successful ;
69
72
}
70
73
}
71
74
72
- protected static function dispatchDiscordWebhook ($ event_id ) {
75
+ protected static function dispatchDiscordWebhook ($ event_id )
76
+ {
73
77
$ c = Common::$ config ->discord ->forward_event_log ;
74
78
if (!$ c ->enabled ) return ;
75
79
@@ -87,63 +91,79 @@ protected static function dispatchDiscordWebhook($event_id) {
87
91
$ embed ->setTimestamp ($ event ->getEventDateTime ());
88
92
89
93
$ user = $ event ->getUser ();
90
- if (!is_null ($ user )) {
94
+ if (!is_null ($ user ))
95
+ {
91
96
$ author = new DiscordEmbedAuthor (
92
97
$ user ->getName (), $ user ->getURI (), $ user ->getAvatarURI (null )
93
98
);
94
99
$ embed ->setAuthor ($ author );
95
100
}
96
101
97
- if (!$ c ->exclude_meta_data ) {
102
+ if (!$ c ->exclude_meta_data )
103
+ {
98
104
$ data = json_decode ($ event ->getMetadata (), true );
99
- if (is_scalar ($ data )) {
100
-
101
- if (is_string ($ data )) {
102
- $ f_value = substr ($ data , 0 , DiscordEmbedField::MAX_VALUE - 3 );
103
- if (strlen ($ data ) > DiscordEmbedField::MAX_VALUE - 3 )
104
- $ f_value .= '... ' ;
105
- } else {
106
- $ f_value = $ data ;
107
- }
108
105
109
- $ field = new DiscordEmbedField ('Meta Data ' , $ f_value , true );
110
- $ embed ->addField ($ field );
111
-
112
- } else {
113
-
114
- foreach ($ data as $ key => $ value ) {
106
+ $ parse_fx = function ($ value , $ key , $ embed )
107
+ {
108
+ $ field = null ;
115
109
116
- $ f_key = substr ($ key , 0 , DiscordEmbedField::MAX_NAME - 3 );
117
- if (strlen ($ key ) > DiscordEmbedField::MAX_NAME - 3 )
118
- $ f_key .= '... ' ;
119
-
120
- if (is_scalar ($ value )) {
110
+ if (!$ field && is_string ($ value ))
111
+ {
112
+ $ v = substr ($ value , 0 , DiscordEmbedField::MAX_VALUE - 3 );
113
+ if (strlen ($ value ) > DiscordEmbedField::MAX_VALUE - 3 )
114
+ {
115
+ $ v .= '... ' ;
116
+ }
117
+ $ field = new DiscordEmbedField (
118
+ $ key , $ v , (strlen ($ v ) < DiscordEmbedField::MAX_VALUE / 4 )
119
+ );
120
+ }
121
121
122
- if (is_string ($ value )) {
123
- $ f_value = substr ($ value , 0 , DiscordEmbedField::MAX_VALUE - 3 );
124
- if (strlen ($ value ) > DiscordEmbedField::MAX_VALUE - 3 )
125
- $ f_value .= '... ' ;
126
- } else {
127
- $ f_value = $ value ;
128
- }
122
+ if (!$ field && is_numeric ($ value ))
123
+ {
124
+ $ field = new DiscordEmbedField ($ key , $ value , true );
125
+ }
129
126
130
- $ field = new DiscordEmbedField ($ f_key , $ f_value , true );
127
+ if (!$ field && is_bool ($ value ))
128
+ {
129
+ $ field = new DiscordEmbedField (
130
+ $ key , ($ value ? 'true ' : 'false ' ), true
131
+ );
132
+ }
131
133
132
- } else {
134
+ if (!$ field )
135
+ {
136
+ $ field = new DiscordEmbedField ($ key , gettype ($ value ), true );
137
+ }
133
138
134
- $ field = new DiscordEmbedField ($ f_key , gettype ($ value ), true );
139
+ $ embed ->addField ($ field );
140
+ };
141
+
142
+ $ flatten_fx = function (&$ tree , &$ flatten_fx , &$ parse_fx , &$ depth , &$ embed )
143
+ {
144
+ if (!is_array ($ tree ))
145
+ {
146
+ $ parse_fx ($ tree , implode ('_ ' , $ depth ), $ embed );
147
+ return ;
148
+ }
135
149
136
- }
137
- $ embed -> addField ( $ field ) ;
150
+ array_push ( $ depth , '' );
151
+ if ( count ( $ depth ) > 2 ) return ;
138
152
139
- if ($ embed ->fieldCount () >= DiscordEmbed::MAX_FIELDS ) break ;
153
+ foreach ($ tree as $ key => $ value )
154
+ {
155
+ $ depth [count ($ depth )-1 ] = $ key ;
156
+ $ flatten_fx ($ value , $ flatten_fx , $ parse_fx , $ depth , $ embed );
140
157
}
141
158
142
- }
159
+ array_pop ($ depth );
160
+ };
161
+
162
+ $ depth = [];
163
+ $ flatten_fx ($ data , $ flatten_fx , $ parse_fx , $ depth , $ embed );
143
164
}
144
165
145
166
$ webhook ->addEmbed ($ embed );
146
167
$ webhook ->send ();
147
168
}
148
-
149
169
}
0 commit comments