Passed
Pull Request — 3.3.x (#95)
by Mario
04:11
created

post_data::get_post_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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