Passed
Pull Request — 3.3.x (#94)
by Mario
04:52
created

post_data::set_func()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
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 12
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
	 * 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 = [])
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
		return $data_ary;
85
	}
86
87
	/**
88
	 * Check if some settings are valid.
89
	 *
90
	 * @param array $data_ary
91
	 *
92
	 * @return array
93
	 * @access public
94
	 */
95
	public function check_post_data($data_ary = [])
96
	{
97
		$data_ary['txn_errors'] = '';
98
		// Check all conditions declared for this post_data
99
		if (isset($data_ary['condition_check']))
100
		{
101
			$check = $this->call_func($data_ary);
102
			$data_ary['txn_errors'] .= $check['txn_errors'];
103
			unset($check['txn_errors']);
104
			$data_ary['condition_checked'] = (bool) array_product($check);
105
		}
106
107
		return $data_ary;
108
	}
109
110
	/**
111
	 * Check requirements for data value.
112
	 * If a check fails, error message are stored in $this->error_message
113
	 *
114
	 * @param array $data_ary
115
	 *
116
	 * @return array
117
	 * @access public
118
	 */
119
120
	public function call_func($data_ary)
121
	{
122
		$check = [];
123
		$check['txn_errors'] = '';
124
125
		foreach ($data_ary['condition_check'] as $control_point => $params)
126
		{
127
			// Calling the check_post_data_function
128
			if (call_user_func_array([$this, 'check_post_data_' . $control_point], [$data_ary['value'], $params]))
129
			{
130
				$check[] = true;
131
				continue;
132
			}
133
134
			$check['txn_errors'] .= '<br>' . $this->language->lang('INVALID_TXN_' . strtoupper($control_point), $data_ary['name']);
135
			$check[] = false;
136
		}
137
		unset($data_ary, $control_point, $params);
138
139
		return $check;
140
	}
141
142
	/**
143
	 * Check Post data length.
144
	 * Called by $this->check_post_data() method
145
	 *
146
	 * @param string $value
147
	 * @param array  $statement
148
	 *
149
	 * @return bool
150
	 * @access public
151
	 */
152
	public function check_post_data_length($value, $statement)
153
	{
154
		return $this->ppde_operator_compare->compare_value(strlen($value), $statement['value'], $statement['operator']);
155
	}
156
157
	/**
158
	 * Check if parsed value contains only ASCII chars.
159
	 * Return false if it contains non ASCII chars.
160
	 *
161
	 * @param string $value
162
	 *
163
	 * @return bool
164
	 * @access public
165
	 */
166
	public function check_post_data_ascii($value)
167
	{
168
		return strlen($value) == strspn($value, self::ASCII_RANGE);
169
	}
170
171
	/**
172
	 * Check Post data content based on an array list.
173
	 * Called by $this->check_post_data() method
174
	 *
175
	 * @param string $value
176
	 * @param array  $content_ary
177
	 *
178
	 * @return bool
179
	 * @access public
180
	 */
181
	public function check_post_data_content($value, $content_ary)
182
	{
183
		return in_array($value, $content_ary);
184
	}
185
186
	/**
187
	 * Check if Post data is empty.
188
	 * Called by $this->check_post_data() method
189
	 *
190
	 * @param string $value
191
	 *
192
	 * @return bool
193
	 * @access public
194
	 */
195
	public function check_post_data_empty($value)
196
	{
197
		return empty($value) ? false : true;
198
	}
199
200
	/**
201
	 * Set Post data length.
202
	 * Called by $this->set_post_data() method
203
	 *
204
	 * @param string  $value
205
	 * @param integer $length
206
	 *
207
	 * @return string
208
	 * @access public
209
	 */
210
	public function set_post_data_length($value, $length)
211
	{
212
		return substr($value, 0, (int) $length);
213
	}
214
215
	/**
216
	 * Set Post data to lowercase.
217
	 * Called by $this->set_post_data() method
218
	 *
219
	 * @param string $value
220
	 * @param bool   $force
221
	 *
222
	 * @return string
223
	 * @access public
224
	 */
225
	public function set_post_data_lowercase($value, $force = false)
226
	{
227
		return $force ? strtolower($value) : $value;
228
	}
229
230
	/**
231
	 * Set Post data to date/time format.
232
	 * Called by $this->set_post_data() method
233
	 *
234
	 * @param string $value
235
	 * @param bool   $force
236
	 *
237
	 * @return string
238
	 * @access public
239
	 */
240
	public function set_post_data_strtotime($value, $force = false)
241
	{
242
		return $force ? strtotime($value) : $value;
243
	}
244
}
245