@@ -32,8 +32,8 @@ public static function logEvent(
3232
3333 $ successful = false ;
3434
35- try {
36-
35+ try
36+ {
3737 $ stmt = Common::$ database ->prepare ('
3838 INSERT INTO `event_log` (
3939 `event_type_id`, `event_datetime`, `user_id`, `ip_address`,
@@ -57,19 +57,23 @@ public static function logEvent(
5757 $ successful = $ stmt ->execute ();
5858 $ stmt ->closeCursor ();
5959
60- if ($ successful ) {
60+ if ($ successful )
61+ {
6162 self ::dispatchDiscordWebhook ((int ) Common::$ database ->lastInsertId ());
6263 }
63-
64- } catch (PDOException $ e ) {
64+ }
65+ catch (PDOException $ e )
66+ {
6567 throw new QueryException ('Cannot log event ' , $ e );
66-
67- } finally {
68+ }
69+ finally
70+ {
6871 return $ successful ;
6972 }
7073 }
7174
72- protected static function dispatchDiscordWebhook ($ event_id ) {
75+ protected static function dispatchDiscordWebhook ($ event_id )
76+ {
7377 $ c = Common::$ config ->discord ->forward_event_log ;
7478 if (!$ c ->enabled ) return ;
7579
@@ -87,63 +91,79 @@ protected static function dispatchDiscordWebhook($event_id) {
8791 $ embed ->setTimestamp ($ event ->getEventDateTime ());
8892
8993 $ user = $ event ->getUser ();
90- if (!is_null ($ user )) {
94+ if (!is_null ($ user ))
95+ {
9196 $ author = new DiscordEmbedAuthor (
9297 $ user ->getName (), $ user ->getURI (), $ user ->getAvatarURI (null )
9398 );
9499 $ embed ->setAuthor ($ author );
95100 }
96101
97- if (!$ c ->exclude_meta_data ) {
102+ if (!$ c ->exclude_meta_data )
103+ {
98104 $ 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- }
108105
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 ;
115109
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+ }
121121
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+ }
129126
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+ }
131133
132- } else {
134+ if (!$ field )
135+ {
136+ $ field = new DiscordEmbedField ($ key , gettype ($ value ), true );
137+ }
133138
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+ }
135149
136- }
137- $ embed -> addField ( $ field ) ;
150+ array_push ( $ depth , '' );
151+ if ( count ( $ depth ) > 2 ) return ;
138152
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 );
140157 }
141158
142- }
159+ array_pop ($ depth );
160+ };
161+
162+ $ depth = [];
163+ $ flatten_fx ($ data , $ flatten_fx , $ parse_fx , $ depth , $ embed );
143164 }
144165
145166 $ webhook ->addEmbed ($ embed );
146167 $ webhook ->send ();
147168 }
148-
149169}
0 commit comments