Passed
Push — develop-3.3.x ( c925a0...2e3aed )
by Mario
02:35
created

post_data::check_post_data_content()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
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
	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($data_ary, $control_point, $params);
61
62
		return $value;
63
	}
64
65
	/**
66
	 * Check requirements for data value.
67
	 * If a check fails, error message are stored in $this->error_message
68
	 *
69
	 * @param array $data_ary
70
	 *
71
	 * @return array
72
	 * @access public
73
	 */
74
75
	public function call_func($data_ary)
76
	{
77
		$check['txn_errors'] = '';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$check was never initialized. Although not strictly required by PHP, it is generally a good practice to add $check = array(); before regardless.
Loading history...
78
79
		foreach ($data_ary['condition_check'] as $control_point => $params)
80
		{
81
			// Calling the check_post_data_function
82
			if (call_user_func_array([$this, 'check_post_data_' . $control_point], [$data_ary['value'], $params]))
83
			{
84
				$check[] = true;
85
				continue;
86
			}
87
88
			$check['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_' . strtoupper($control_point), $data_ary['name']);
89
			$check[] = false;
90
		}
91
		unset($data_ary, $control_point, $params);
92
93
		return $check;
94
	}
95
96
	/**
97
	 * Request predefined variable from super global
98
	 *
99
	 * @param array $data_ary List of data to request
100
	 *
101
	 * @return array
102
	 * @access public
103
	 */
104
	public function get_post_data($data_ary = [])
105
	{
106
		// Request variables
107
		if (is_array($data_ary['default']))
108
		{
109
			$data_ary['value'] = $this->request->variable($data_ary['name'], (string) $data_ary['default'][0], (bool) $data_ary['default'][1]);
110
		}
111
		else
112
		{
113
			$data_ary['value'] = $this->request->variable($data_ary['name'], $data_ary['default']);
114
		}
115
		return $data_ary;
116
	}
117
118
	/**
119
	 * Check if some settings are valid.
120
	 *
121
	 * @param array $data_ary
122
	 *
123
	 * @return array
124
	 * @access public
125
	 */
126
	public function check_post_data($data_ary = [])
127
	{
128
		$data_ary['txn_errors'] = '';
129
		// Check all conditions declared for this post_data
130
		if (isset($data_ary['condition_check']))
131
		{
132
			$check = $this->call_func($data_ary);
133
			$data_ary['txn_errors'] .= $check['txn_errors'];
134
			unset($check['txn_errors']);
135
			$data_ary['condition_checked'] = (bool) array_product($check);
136
		}
137
138
		return $data_ary;
139
	}
140
141
	/**
142
	 * Check Post data length.
143
	 * Called by $this->check_post_data() method
144
	 *
145
	 * @param string $value
146
	 * @param array  $statement
147
	 *
148
	 * @return bool
149
	 * @access public
150
	 */
151
	public function check_post_data_length($value, $statement)
152
	{
153
		return $this->ppde_operator_compare->compare_value(strlen($value), $statement['value'], $statement['operator']);
154
	}
155
156
	/**
157
	 * Check if parsed value contains only ASCII chars.
158
	 * Return false if it contains non ASCII chars.
159
	 *
160
	 * @param string $value
161
	 *
162
	 * @return bool
163
	 * @access public
164
	 */
165
	public function check_post_data_ascii($value)
166
	{
167
		return strlen($value) == strspn($value, self::ASCII_RANGE);
168
	}
169
170
	/**
171
	 * Check Post data content based on an array list.
172
	 * Called by $this->check_post_data() method
173
	 *
174
	 * @param string $value
175
	 * @param array  $content_ary
176
	 *
177
	 * @return bool
178
	 * @access public
179
	 */
180
	public function check_post_data_content($value, $content_ary)
181
	{
182
		return in_array($value, $content_ary);
183
	}
184
185
	/**
186
	 * Check if Post data is empty.
187
	 * Called by $this->check_post_data() method
188
	 *
189
	 * @param string $value
190
	 *
191
	 * @return bool
192
	 * @access public
193
	 */
194
	public function check_post_data_empty($value)
195
	{
196
		return empty($value) ? false : true;
197
	}
198
199
	/**
200
	 * Set Post data length.
201
	 * Called by $this->set_post_data() method
202
	 *
203
	 * @param string  $value
204
	 * @param integer $length
205
	 *
206
	 * @return string
207
	 * @access public
208
	 */
209
	public function set_post_data_length($value, $length)
210
	{
211
		return substr($value, 0, (int) $length);
212
	}
213
214
	/**
215
	 * Set Post data to lowercase.
216
	 * Called by $this->set_post_data() method
217
	 *
218
	 * @param string $value
219
	 * @param bool   $force
220
	 *
221
	 * @return string
222
	 * @access public
223
	 */
224
	public function set_post_data_lowercase($value, $force = false)
225
	{
226
		return $force ? strtolower($value) : $value;
227
	}
228
229
	/**
230
	 * Set Post data to date/time format.
231
	 * Called by $this->set_post_data() method
232
	 *
233
	 * @param string $value
234
	 * @param bool   $force
235
	 *
236
	 * @return string
237
	 * @access public
238
	 */
239
	public function set_post_data_strtotime($value, $force = false)
240
	{
241
		return $force ? strtotime($value) : $value;
242
	}
243
}
244