@@ -14,6 +14,13 @@ public static function processEvent($incomingWebhook, array $payload) {
1414 }
1515 }
1616
17+ if (isset ($ conditions ['header_cond ' ]) && $ conditions ['header_cond ' ] != "" ) {
18+ // HTTP request headers are matched with the standard condition matcher
19+ if (!self ::isValidCondition ('header_cond ' , array ('header_cond ' => self ::normalizeHeaderCondition ($ conditions ['header_cond ' ])), self ::getRequestHeadersPayload ())) {
20+ throw new Exception ('Header conditions does not met! ' );
21+ }
22+ }
23+
1724 $ messages = isset ($ conditions ['messages ' ]) && $ conditions ['messages ' ] != '' ? $ payload [$ conditions ['messages ' ]] : [$ payload ];
1825
1926 if (isset ($ conditions ['messages ' ]) && $ conditions ['messages ' ] != '' && isset ($ conditions ['message_direct ' ]) && $ conditions ['message_direct ' ] == true ) {
@@ -337,6 +344,82 @@ public static function isValidCondition($field, $conditions, $payloadMessage)
337344 return $ validConditions ;
338345 }
339346
347+ /**
348+ * Current request headers as a flat payload so webhook "Header conditions" can be
349+ * matched with the standard condition matcher (isValidCondition()).
350+ *
351+ * E.g. "X-Webhook-Secret=s3cr3t||Content-Type=application/json" checks two headers.
352+ * Keys are lower cased and normalizeHeaderCondition() lower cases attribute names,
353+ * so header names are matched case insensitively.
354+ */
355+ public static function getRequestHeadersPayload ()
356+ {
357+ $ headers = array ();
358+
359+ foreach ($ _SERVER as $ key => $ value ) {
360+ if (strpos ($ key , 'HTTP_ ' ) === 0 ) {
361+ $ headers [strtolower (str_replace ('_ ' , '- ' , substr ($ key , 5 )))] = $ value ;
362+ } elseif (in_array ($ key , array ('CONTENT_TYPE ' , 'CONTENT_LENGTH ' ))) {
363+ $ headers [strtolower (str_replace ('_ ' , '- ' , $ key ))] = $ value ;
364+ }
365+ }
366+
367+ if (function_exists ('getallheaders ' )) {
368+ $ requestHeaders = getallheaders ();
369+ if (is_array ($ requestHeaders )) {
370+ foreach ($ requestHeaders as $ name => $ value ) {
371+ $ headers [strtolower ($ name )] = $ value ;
372+ }
373+ }
374+ }
375+
376+ return $ headers ;
377+ }
378+
379+ /**
380+ * Lower cases header names in "Header conditions" so they match lower cased keys
381+ * returned by getRequestHeadersPayload(). Values are untouched.
382+ */
383+ public static function normalizeHeaderCondition ($ condition )
384+ {
385+ $ conditionsPairs = array ();
386+
387+ foreach (explode ('|| ' , $ condition ) as $ conditionsPair ) {
388+ $ equalsPosition = strpos ($ conditionsPair , '= ' );
389+
390+ if ($ equalsPosition === false ) {
391+ $ conditionsPairs [] = strtolower (trim ($ conditionsPair ));
392+ } else {
393+ $ conditionsPairs [] = strtolower (trim (substr ($ conditionsPair , 0 , $ equalsPosition ))) . substr ($ conditionsPair , $ equalsPosition );
394+ }
395+ }
396+
397+ return implode ('|| ' , $ conditionsPairs );
398+ }
399+
400+ /**
401+ * Removes [html]...[/html] BBCode wrapper from every value of the incoming payload.
402+ *
403+ * That BBCode is rendered as raw HTML in the back office, so an external system must
404+ * never be able to deliver HTML/JavaScript into an operator session.
405+ */
406+ public static function stripHtmlBBCode ($ payload )
407+ {
408+ if (is_array ($ payload )) {
409+ foreach ($ payload as $ key => $ value ) {
410+ $ payload [$ key ] = self ::stripHtmlBBCode ($ value );
411+ }
412+
413+ return $ payload ;
414+ }
415+
416+ if (is_string ($ payload )) {
417+ return str_ireplace (array ('[html] ' , '[/html] ' ), '' , $ payload );
418+ }
419+
420+ return $ payload ;
421+ }
422+
340423 public static function processMessage ($ incomingWebhook , $ payloadMessage , $ payloadAll , $ returnDeterminedData = false ) {
341424
342425 $ conditions = $ incomingWebhook ->conditions_array ;
0 commit comments