Skip to content

Commit

Permalink
feat: improved html check in serializiers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mustapayev committed Dec 24, 2024
1 parent 1f460d3 commit d04cd35
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 38 deletions.
12 changes: 2 additions & 10 deletions src/Serializer/KuveytPosSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

class KuveytPosSerializer implements SerializerInterface
{
use SerializerUtilTrait;

/** @var string[] */
private array $nonPaymentTransactions = [
PosInterface::TX_TYPE_REFUND,
Expand Down Expand Up @@ -88,16 +90,6 @@ public function decode(string $data, string $txType): array
}
}

/**
* @param string $str
*
* @return bool returns true if string is a HTML document.
*/
private function isHTML(string $str): bool
{
return $str !== \strip_tags($str);
}

/**
* Diger Gateway'lerden farkli olarak bu gateway HTML form olan bir response doner.
* Kutupahenin islem akisina uymasi icin bu HTML form verilerini array'e donusturup, kendimiz post ediyoruz.
Expand Down
15 changes: 2 additions & 13 deletions src/Serializer/PayFlexCPV4PosSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class PayFlexCPV4PosSerializer implements SerializerInterface
{
use SerializerUtilTrait;

private Serializer $serializer;

public function __construct()
Expand Down Expand Up @@ -78,17 +80,4 @@ public function decode(string $data, ?string $txType = null): array

throw $notEncodableValueException;
}

/**
* must be called after making sure that $str does not contain XML string.
* Because for XML strings it will also return true.
*
* @param string $str
*
* @return bool
*/
private function isHTML(string $str): bool
{
return $str !== \strip_tags($str);
}
}
7 changes: 2 additions & 5 deletions src/Serializer/PayFlexV4PosSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class PayFlexV4PosSerializer implements SerializerInterface
{
use SerializerUtilTrait;

private Serializer $serializer;

public function __construct()
Expand Down Expand Up @@ -75,9 +77,4 @@ public function decode(string $data, ?string $txType = null): array
throw $notEncodableValueException;
}
}

private function isHTML(string $str): bool
{
return $str !== \strip_tags($str);
}
}
42 changes: 42 additions & 0 deletions src/Serializer/SerializerUtilTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* @license MIT
*/

namespace Mews\Pos\Serializer;

/**
* Serializer utility methods.
*/
trait SerializerUtilTrait
{
/**
* @param string $string
*
* @return bool
*/
private function isHTML(string $string): bool
{
if ('' === \trim($string)) {
return false;
}

// Suppress errors for invalid HTML
$previousLibxmlState = \libxml_use_internal_errors(true);

// Create a new DOMDocument
$dom = new \DOMDocument();

// Attempt to load the string as HTML
$isValidHTML = $dom->loadHTML($string, LIBXML_NOERROR | LIBXML_NOWARNING);

// Clear any libxml errors
\libxml_clear_errors();

// Restore the previous libxml error handling state
\libxml_use_internal_errors($previousLibxmlState);

// Check if the string has recognizable HTML elements
return $isValidHTML && $dom->getElementsByTagName('html')->length > 0;
}
}
12 changes: 2 additions & 10 deletions src/Serializer/VakifKatilimPosSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

class VakifKatilimPosSerializer implements SerializerInterface
{
use SerializerUtilTrait;

private Serializer $serializer;

public function __construct()
Expand Down Expand Up @@ -66,16 +68,6 @@ public function decode(string $data, string $txType): array
}
}

/**
* @param string $str
*
* @return bool returns true if string is a HTML document.
*/
private function isHTML(string $str): bool
{
return $str !== \strip_tags($str);
}

/**
* Diger Gateway'lerden farkli olarak bu gateway HTML form olan bir response doner.
* Kutupahenin islem akisina uymasi icin bu HTML form verilerini array'e donusturup, kendimiz post ediyoruz.
Expand Down

0 comments on commit d04cd35

Please sign in to comment.