Repository navigation
Expand file tree
/
Copy pathclass-alert-type-webhook.php
More file actions
444 lines (390 loc) · 12.3 KB
/
Copy pathclass-alert-type-webhook.php
File metadata and controls
444 lines (390 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* Outgoing Webhook Alerts.
*
* @package WP_Stream
*/
namespace WP_Stream;
/**
* Class Alert_Type_Webhook
*
* @package WP_Stream
*/
class Alert_Type_Webhook extends Alert_Type {
/**
* Record keys that may appear as {{field}} placeholders in the body template.
*/
const PLACEHOLDER_FIELDS = array(
'summary',
'connector',
'context',
'action',
'user_id',
'user_role',
'ip',
'object_id',
'blog_id',
'created',
);
/**
* Allowed Content-Type header values.
*/
const ALLOWED_CONTENT_TYPES = array(
'application/json',
'application/json; charset=utf-8',
'text/plain',
'application/x-www-form-urlencoded',
);
/**
* HTTP header names that must not be overridden by configuration.
*/
const BLOCKED_HEADER_NAMES = array(
'host',
'content-length',
'transfer-encoding',
);
/**
* Alert type name
*
* @var string
*/
public string $name = 'Outgoing Webhook';
/**
* Alert type slug
*
* @var string
*/
public string $slug = 'webhook';
/**
* Class Constructor
*
* @param Plugin $plugin Plugin object.
*/
public function __construct( Plugin $plugin ) {
parent::__construct( $plugin );
$this->register_hooks();
}
/**
* Register hooks for the alert type.
*
* @return void
*/
private function register_hooks() {
if ( ! is_admin() ) {
return;
}
add_filter(
'wp_stream_alerts_save_meta',
array( $this, 'add_alert_meta' ),
10,
2
);
}
/**
* Send an HTTP request for the matching record.
*
* @param int $record_id Record that triggered notification.
* @param array $recordarr Record details.
* @param Alert $alert Alert options.
* @return void
*/
public function alert( $record_id, $recordarr, $alert ) {
unset( $record_id );
$options = wp_parse_args(
is_object( $alert ) ? $alert->alert_meta : array(),
array(
'url' => '',
'method' => 'POST',
'content_type' => 'application/json',
'headers' => array(),
'body_template' => '',
)
);
$url = is_string( $options['url'] ) ? trim( $options['url'] ) : '';
if ( '' === $url || false === wp_http_validate_url( $url ) ) {
return;
}
$method = strtoupper( (string) $options['method'] );
if ( ! in_array( $method, array( 'POST', 'PUT' ), true ) ) {
$method = 'POST';
}
$template = is_string( $options['body_template'] ) ? $options['body_template'] : '';
if ( '' === trim( $template ) ) {
$template = self::default_body_template();
}
$body = self::substitute_placeholders( $template, (array) $recordarr );
$headers = self::build_request_headers( $options['content_type'], $options['headers'] );
$response = wp_safe_remote_request(
$url,
array(
'method' => $method,
'headers' => $headers,
'body' => $body,
'timeout' => 15,
)
);
if ( is_wp_error( $response ) && defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( sprintf( 'Stream webhook alert request failed: %s', $response->get_error_message() ) ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- reason: opt-in WP_DEBUG logging of a failed outbound request.
}
}
/**
* Compact JSON object with placeholders for every supported record field.
*
* @return string
*/
public static function default_body_template() {
$parts = array();
foreach ( self::PLACEHOLDER_FIELDS as $field ) {
$parts[] = '"' . $field . '":{{' . $field . '}}';
}
return '{' . implode( ',', $parts ) . '}';
}
/**
* Replace {{field}} tokens with JSON-encoded record values.
*
* @param string $template JSON body template.
* @param array $recordarr Record details.
* @return string
*/
public static function substitute_placeholders( $template, $recordarr ) {
$replacements = array();
foreach ( self::PLACEHOLDER_FIELDS as $field ) {
$value = array_key_exists( $field, $recordarr ) ? $recordarr[ $field ] : '';
$replacements[ '{{' . $field . '}}' ] = wp_json_encode( $value );
}
return strtr( (string) $template, $replacements );
}
/**
* Merge Content-Type with extra header rows; drop blocked names.
*
* @param string $content_type Requested Content-Type.
* @param array $header_rows Stored header rows.
* @return array
*/
public static function build_request_headers( $content_type, $header_rows ) {
$headers = array(
'Content-Type' => self::sanitize_content_type( $content_type ),
);
foreach ( self::normalize_header_rows( $header_rows ) as $row ) {
$name = $row['name'];
$value = $row['value'];
$key = strtolower( $name );
if ( in_array( $key, self::BLOCKED_HEADER_NAMES, true ) ) {
continue;
}
if ( 'content-type' === $key ) {
$headers['Content-Type'] = self::sanitize_content_type( $value );
continue;
}
$headers[ $name ] = $value;
}
return $headers;
}
/**
* Restrict Content-Type to the allowlist.
*
* @param string $content_type Raw Content-Type.
* @return string
*/
public static function sanitize_content_type( $content_type ) {
$normalized = strtolower( trim( (string) $content_type ) );
foreach ( self::ALLOWED_CONTENT_TYPES as $allowed ) {
if ( strtolower( $allowed ) === $normalized ) {
return $allowed;
}
}
return 'application/json';
}
/**
* Normalize stored or posted headers into name/value rows.
*
* @param mixed $headers Header rows, associative map, or textarea string.
* @return array<int, array{name: string, value: string}>
*/
public static function normalize_header_rows( $headers ) {
if ( is_string( $headers ) ) {
return self::parse_header_textarea( $headers );
}
if ( ! is_array( $headers ) ) {
return array();
}
$rows = array();
foreach ( $headers as $key => $entry ) {
if ( is_array( $entry ) && isset( $entry['name'] ) ) {
$name = (string) $entry['name'];
$value = isset( $entry['value'] ) ? (string) $entry['value'] : '';
} elseif ( is_string( $key ) && ! is_numeric( $key ) ) {
$name = $key;
$value = is_scalar( $entry ) ? (string) $entry : '';
} else {
continue;
}
$sanitized = self::sanitize_header_pair( $name, $value );
if ( null !== $sanitized ) {
$rows[] = $sanitized;
}
}
return $rows;
}
/**
* Parse "Name: value" lines from the additional-headers field.
*
* @param string $text Textarea contents.
* @return array<int, array{name: string, value: string}>
*/
public static function parse_header_textarea( $text ) {
$rows = array();
$lines = preg_split( '/\r\n|\r|\n/', (string) $text );
if ( ! is_array( $lines ) ) {
return array();
}
foreach ( $lines as $line ) {
$line = trim( $line );
if ( '' === $line || false === strpos( $line, ':' ) ) {
continue;
}
$parts = explode( ':', $line, 2 );
$sanitized = self::sanitize_header_pair( $parts[0], $parts[1] );
if ( null !== $sanitized ) {
$rows[] = $sanitized;
}
}
return $rows;
}
/**
* Sanitize one HTTP header name/value pair.
*
* @param string $name Header name.
* @param string $value Header value.
* @return array{name: string, value: string}|null
*/
public static function sanitize_header_pair( $name, $value ) {
$name = trim( (string) $name );
$value = trim( str_replace( array( "\r", "\n" ), '', (string) $value ) );
if ( '' === $name || ! preg_match( '/^[A-Za-z0-9-]+$/', $name ) ) {
return null;
}
if ( in_array( strtolower( $name ), self::BLOCKED_HEADER_NAMES, true ) ) {
return null;
}
return array(
'name' => $name,
'value' => $value,
);
}
/**
* Display settings form for this alert type.
*
* @param Alert $alert Alert currently being worked on.
* @return void
*/
public function display_fields( $alert ) {
$alert_meta = array();
if ( is_object( $alert ) ) {
$alert_meta = $alert->alert_meta;
}
$options = wp_parse_args(
$alert_meta,
array(
'url' => '',
'method' => 'POST',
'content_type' => 'application/json',
'headers' => array(),
'body_template' => self::default_body_template(),
)
);
$header_text = '';
foreach ( self::normalize_header_rows( $options['headers'] ) as $row ) {
$header_text .= $row['name'] . ': ' . $row['value'] . "\n";
}
$content_type_options = array();
foreach ( self::ALLOWED_CONTENT_TYPES as $type ) {
$content_type_options[ $type ] = $type;
}
$form = new Form_Generator();
echo '<span class="wp_stream_alert_type_description">' . esc_html__( 'Send an HTTP POST or PUT request to a URL with a JSON body.', 'stream' ) . '</span>';
echo '<label for="wp_stream_webhook_url"><span class="title">' . esc_html__( 'URL', 'stream' ) . '</span>';
echo '<span class="input-text-wrap">';
$form->render_field(
'text',
array(
'name' => 'wp_stream_webhook_url',
'title' => esc_attr( __( 'URL', 'stream' ) ),
'value' => $options['url'],
)
);
echo '</span>';
echo '<span class="input-text-wrap">' . esc_html__( 'Must be a valid HTTP or HTTPS URL.', 'stream' ) . '</span>';
echo '</label>';
echo '<label for="wp_stream_webhook_method"><span class="title">' . esc_html__( 'Method', 'stream' ) . '</span>';
echo '<span class="input-text-wrap">';
$form->render_field(
'select',
array(
'name' => 'wp_stream_webhook_method',
'value' => in_array( $options['method'], array( 'POST', 'PUT' ), true ) ? $options['method'] : 'POST',
'options' => array(
'POST' => 'POST',
'PUT' => 'PUT',
),
)
);
echo '</span></label>';
echo '<label for="wp_stream_webhook_content_type"><span class="title">' . esc_html__( 'Content-Type', 'stream' ) . '</span>';
echo '<span class="input-text-wrap">';
$form->render_field(
'select',
array(
'name' => 'wp_stream_webhook_content_type',
'value' => self::sanitize_content_type( $options['content_type'] ),
'options' => $content_type_options,
)
);
echo '</span></label>';
echo '<label for="wp_stream_webhook_headers"><span class="title">' . esc_html__( 'Headers', 'stream' ) . '</span>';
echo '<span class="input-text-wrap">';
printf(
'<textarea name="wp_stream_webhook_headers" id="wp_stream_webhook_headers" rows="3" class="large-text">%s</textarea>',
esc_textarea( $header_text )
);
echo '</span>';
echo '<span class="input-text-wrap">' . esc_html__( 'Optional extra headers, one per line as Name: value. Host cannot be overridden.', 'stream' ) . '</span>';
echo '</label>';
echo '<label for="wp_stream_webhook_body_template"><span class="title">' . esc_html__( 'Body template', 'stream' ) . '</span>';
echo '<span class="input-text-wrap">';
printf(
'<textarea name="wp_stream_webhook_body_template" id="wp_stream_webhook_body_template" rows="5" class="large-text code">%s</textarea>',
esc_textarea( $options['body_template'] )
);
echo '</span>';
echo '<span class="input-text-wrap">' . esc_html__( 'JSON with {{summary}}, {{connector}}, {{context}}, {{action}}, {{user_id}}, {{user_role}}, {{ip}}, {{object_id}}, {{blog_id}}, {{created}}. Values are JSON-encoded.', 'stream' ) . '</span>';
echo '</label>';
}
/**
* Add webhook fields when this alert type is saved.
*
* @param array $alert_meta The metadata to be inserted for this alert.
* @param string $alert_type The type of alert being added or updated.
*
* @return array
*/
public function add_alert_meta( $alert_meta, $alert_type ) {
if ( 'webhook' !== $alert_type ) {
return $alert_meta;
}
$url = wp_stream_filter_input( INPUT_POST, 'wp_stream_webhook_url' );
if ( ! empty( $url ) ) {
$alert_meta['url'] = esc_url_raw( $url );
}
$method = wp_stream_filter_input( INPUT_POST, 'wp_stream_webhook_method' );
$method = is_string( $method ) ? strtoupper( $method ) : 'POST';
$alert_meta['method'] = in_array( $method, array( 'POST', 'PUT' ), true ) ? $method : 'POST';
$content_type = wp_stream_filter_input( INPUT_POST, 'wp_stream_webhook_content_type' );
$alert_meta['content_type'] = self::sanitize_content_type( (string) $content_type );
$headers_raw = wp_stream_filter_input( INPUT_POST, 'wp_stream_webhook_headers' );
$alert_meta['headers'] = self::normalize_header_rows( is_string( $headers_raw ) ? $headers_raw : '' );
$body_template = wp_stream_filter_input( INPUT_POST, 'wp_stream_webhook_body_template' );
$alert_meta['body_template'] = is_string( $body_template ) ? $body_template : '';
return $alert_meta;
}
}