1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Data helper |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2019 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Data helper |
15
|
|
|
* |
16
|
|
|
* @link https://github.com/wp-pay-gateways/ideal-basic/blob/2.0.0/src/DataHelper.php |
17
|
|
|
* @author Remco Tolsma |
18
|
|
|
* @version 2.1.9 |
19
|
|
|
* @since 2.0.2 |
20
|
|
|
*/ |
21
|
|
|
class DataHelper { |
22
|
|
|
/** |
23
|
|
|
* Strictly alphanumerical (letters and numbers only). |
24
|
|
|
* |
25
|
|
|
* The OmniKassa 2.0 document is not very clear about spaces, these are not allowd in AN (Strictly). |
26
|
|
|
* If a space is used in a AN (Strictly) field this will result in for examploe the following error: |
27
|
|
|
* `merchantOrderId should only contain alphanumeric characters`. |
28
|
|
|
* |
29
|
|
|
* @var array<string> |
30
|
|
|
*/ |
31
|
|
|
private static $characters_ans = array( 'A-Z', 'a-z', '0-9' ); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Validate AN..$max. |
35
|
|
|
* |
36
|
|
|
* @param string $value Value to validate. |
37
|
|
|
* @param int $max Max length of value. |
38
|
|
|
* @param string $field Field name. |
39
|
|
|
* @return true |
40
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception when string is longer then max length. |
41
|
|
|
*/ |
42
|
11 |
|
public static function validate_an( $value, $max, $field ) { |
43
|
11 |
|
if ( \mb_strlen( $value, 'UTF-8' ) > $max ) { |
44
|
1 |
|
throw new \InvalidArgumentException( |
45
|
1 |
|
\sprintf( |
46
|
1 |
|
'Field `%s` value "%s" can not be longer then `%d`.', |
47
|
1 |
|
$field, |
48
|
1 |
|
$value, |
49
|
1 |
|
$max |
50
|
|
|
) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* HTML tags are not allowed. |
56
|
|
|
* |
57
|
|
|
* @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string |
58
|
|
|
*/ |
59
|
|
|
|
60
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We don't want the `trim` in `wp_strip_all_tags`. |
61
|
10 |
|
if ( \strip_tags( $value ) !== $value ) { |
62
|
2 |
|
throw new \InvalidArgumentException( |
63
|
2 |
|
\sprintf( |
64
|
2 |
|
'Field `%s` cannot contain HTML tags: `%s`.', |
65
|
2 |
|
$field, |
66
|
2 |
|
$value |
67
|
|
|
) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
8 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Validate AN(Strictly)..Max nn. |
76
|
|
|
* |
77
|
|
|
* @param string $value Value to validate. |
78
|
|
|
* @param int $max Max length of value. |
79
|
|
|
* @param string $field Field name. |
80
|
|
|
* @return true |
81
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception when string is not alphanumeric characters. |
82
|
|
|
* @throws \Exception Throws exception when PCRE regex execution results in error. |
83
|
|
|
*/ |
84
|
5 |
|
public static function validate_ans( $value, $max, $field ) { |
85
|
5 |
|
$pattern = '#[^' . \implode( self::$characters_ans ) . ']#'; |
86
|
|
|
|
87
|
5 |
|
$result = \preg_match( $pattern, $value ); |
88
|
|
|
|
89
|
5 |
|
if ( false === $result ) { |
90
|
|
|
throw new \Exception( |
91
|
|
|
'PCRE regex execution error.', |
92
|
|
|
\preg_last_error() |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
if ( 1 === $result ) { |
97
|
1 |
|
throw new \InvalidArgumentException( |
98
|
1 |
|
\sprintf( |
99
|
1 |
|
'Field `%s` must consists strictly of alphanumeric characters: `%s`.', |
100
|
1 |
|
$field, |
101
|
1 |
|
$value |
102
|
|
|
) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
4 |
|
return self::validate_an( $value, $max, $field ); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Validate null or AN..$max. |
111
|
|
|
* |
112
|
|
|
* @param string|null $value Value to validate. |
113
|
|
|
* @param int $max Max length of value. |
114
|
|
|
* @param string $field Field name. |
115
|
|
|
* @return true |
116
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception when value is not null and longer then max length. |
117
|
|
|
*/ |
118
|
1 |
|
public static function validate_null_or_an( $value, $max, $field ) { |
119
|
1 |
|
if ( null === $value ) { |
120
|
|
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
1 |
|
return self::validate_an( $value, $max, $field ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Sanitize string to the specified length. |
128
|
|
|
* |
129
|
|
|
* @param string $string String. |
130
|
|
|
* @param int $length Length. |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
8 |
|
public static function sanitize_an( $string, $length ) { |
134
|
|
|
/** |
135
|
|
|
* HTML tags are not allowed. |
136
|
|
|
* |
137
|
|
|
* @link https://stackoverflow.com/questions/5732758/detect-html-tags-in-a-string |
138
|
|
|
*/ |
139
|
|
|
|
140
|
|
|
// phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags -- We don't want the `trim` in `wp_strip_all_tags`. |
141
|
8 |
|
$sanitized = \strip_tags( $string ); |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* In version `2.1.6` of this library we used the `mb_strimwidth` |
145
|
|
|
* function, unfortunately this function is not alwys available. |
146
|
|
|
* Therefor we now use the `mb_substr`, WordPress is shipped |
147
|
|
|
* with a compat function. |
148
|
|
|
* |
149
|
|
|
* @link https://github.com/WordPress/WordPress/blob/5.0/wp-includes/compat.php#L44-L217 |
150
|
|
|
*/ |
151
|
8 |
|
$sanitized = \mb_substr( $sanitized, 0, $length ); |
152
|
|
|
|
153
|
8 |
|
return $sanitized; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|