Passed
Push — develop-3.3.x ( 5782d9...f10cd4 )
by Mario
03:04
created

post_data_helper::check_post_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 *
4
 * PayPal Donation extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2015-2024 Skouat
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace skouat\ppde\helpers;
12
13
use phpbb\language\language;
14
use phpbb\request\request;
15
16
class post_data_helper
17
{
18
	private const ASCII_RANGE = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
19
20
	protected $language;
21
	protected $compare_helper;
22
	protected $request;
23
24
	/**
25
	 * Constructor
26
	 *
27
	 * @param language       $language       Language object
28
	 * @param compare_helper $compare_helper Compare operator object
29
	 * @param request        $request        Request object
30
	 */
31
	public function __construct(language $language, compare_helper $compare_helper, request $request)
32
	{
33
		$this->language = $language;
34
		$this->compare_helper = $compare_helper;
35
		$this->request = $request;
36
	}
37
38
	/**
39
	 * Check requirements for data value.
40
	 *
41
	 * @param array $data_ary
42
	 * @return mixed
43
	 */
44
	public function set_func(array $data_ary)
45
	{
46
		$value = $data_ary['value'];
47
48
		foreach ($data_ary['force_settings'] as $control_point => $params)
49
		{
50
			// Calling the set_post_data_function
51
			$value = $this->{'set_post_data_' . $control_point}($data_ary['value'], $params);
52
		}
53
		unset($control_point);
54
55
		return $value;
56
	}
57
58
	/**
59
	 * Request predefined variable from super global
60
	 *
61
	 * @param array $data_ary List of data to request
62
	 * @return array
63
	 */
64
	public function get_post_data(array $data_ary): array
65
	{
66
		if (is_array($data_ary['default']))
67
		{
68
			$data_ary['value'] = $this->request->variable($data_ary['name'], (string) $data_ary['default'][0], (bool) $data_ary['default'][1]);
69
		}
70
		else
71
		{
72
			$data_ary['value'] = $this->request->variable($data_ary['name'], $data_ary['default']);
73
		}
74
75
		return $data_ary;
76
	}
77
78
	/**
79
	 * Check if some settings are valid.
80
	 *
81
	 * @param array $data_ary
82
	 * @return array
83
	 */
84
	public function check_post_data(array $data_ary): array
85
	{
86
		$data_ary['txn_errors'] = '';
87
88
		// Check all conditions declared for this post_data
89
		if (isset($data_ary['condition_check']))
90
		{
91
			$check = $this->call_func($data_ary);
92
			$data_ary['txn_errors'] .= $check['txn_errors'];
93
			unset($check['txn_errors']);
94
			$data_ary['condition_checked'] = (bool) array_product($check);
95
		}
96
97
		return $data_ary;
98
	}
99
100
	/**
101
	 * Check requirements for data value.
102
	 * If a check fails, error messages are stored in $this->error_message
103
	 *
104
	 * @param array $data_ary
105
	 * @return array
106
	 */
107
	public function call_func(array $data_ary): array
108
	{
109
		$check = [];
110
		$check['txn_errors'] = '';
111
112
		foreach ($data_ary['condition_check'] as $control_point => $params)
113
		{
114
			// Calling the check_post_data_function
115
			if ($this->{'check_post_data_' . $control_point}($data_ary['value'], $params))
116
			{
117
				$check[] = true;
118
				continue;
119
			}
120
121
			$check['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_' . strtoupper($control_point), $data_ary['name']);
122
			$check[] = false;
123
		}
124
		unset($control_point);
125
126
		return $check;
127
	}
128
129
	/**
130
	 * Check Post data length.
131
	 *
132
	 * @param string $value
133
	 * @param array  $statement
134
	 * @return bool
135
	 */
136
	public function check_post_data_length($value, $statement): bool
137
	{
138
		return $this->compare_helper->compare_value(strlen($value), $statement['value'], $statement['operator']);
139
	}
140
141
	/**
142
	 * Check if parsed value contains only ASCII chars.
143
	 * Return false if it contains non ASCII chars.
144
	 *
145
	 * @param string $value
146
	 * @return bool
147
	 */
148
	public function check_post_data_ascii($value): bool
149
	{
150
		return strlen($value) === strspn($value, self::ASCII_RANGE);
151
	}
152
153
	/**
154
	 * Check Post data content based on an array list.
155
	 *
156
	 * @param string $value
157
	 * @param array  $content_ary
158
	 * @return bool
159
	 */
160
	public function check_post_data_content($value, $content_ary): bool
161
	{
162
		return in_array($value, $content_ary);
163
	}
164
165
	/**
166
	 * Check if Post data is empty.
167
	 *
168
	 * @param string $value
169
	 * @return bool
170
	 */
171
	public function check_post_data_empty($value): bool
172
	{
173
		return !empty($value);
174
	}
175
176
	/**
177
	 * Set Post data length.
178
	 *
179
	 * @param string $value
180
	 * @param int    $length
181
	 * @return string
182
	 */
183
	public function set_post_data_length($value, $length): string
184
	{
185
		return substr($value, 0, (int) $length);
186
	}
187
188
	/**
189
	 * Set Post data to lowercase.
190
	 *
191
	 * @param string $value
192
	 * @param bool   $force
193
	 * @return string
194
	 */
195
	public function set_post_data_lowercase($value, $force = false): string
196
	{
197
		return $force ? strtolower($value) : $value;
198
	}
199
200
	/**
201
	 * Set Post data to date/time format.
202
	 *
203
	 * @param string $value
204
	 * @param bool   $force
205
	 * @return string
206
	 */
207
	public function set_post_data_strtotime($value, $force = false): string
208
	{
209
		return $force ? (string) strtotime($value) : $value;
210
	}
211
}
212