Issues (97)

controller/admin_input.php (11 issues)

1
<?php
2
/**
3
 *
4
 * Advertisement management. An extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ads\controller;
12
13
use phpbb\ads\ext;
14
15
/**
16
 * Admin input
17
 */
18
class admin_input
19
{
20
	/** @var \phpbb\user */
0 ignored issues
show
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
	protected $user;
22
23
	/** @var \phpbb\user_loader */
0 ignored issues
show
The type phpbb\user_loader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
	protected $user_loader;
25
26
	/** @var \phpbb\language\language */
0 ignored issues
show
The type phpbb\language\language was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
	protected $language;
28
29
	/** @var \phpbb\request\request */
0 ignored issues
show
The type phpbb\request\request was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
	protected $request;
31
32
	/** @var \phpbb\ads\banner\banner */
33
	protected $banner;
34
35
	/** @var array Form validation errors */
36
	protected $errors = array();
37
38
	/**
39
	 * Constructor
40
	 *
41
	 * @param \phpbb\user              $user        User object
42
	 * @param \phpbb\user_loader       $user_loader User loader object
43
	 * @param \phpbb\language\language $language    Language object
44
	 * @param \phpbb\request\request   $request     Request object
45
	 * @param \phpbb\ads\banner\banner $banner      Banner upload object
46
	 */
47 23
	public function __construct(\phpbb\user $user, \phpbb\user_loader $user_loader, \phpbb\language\language $language, \phpbb\request\request $request, \phpbb\ads\banner\banner $banner)
48
	{
49 23
		$this->user = $user;
50 23
		$this->user_loader = $user_loader;
51 23
		$this->language = $language;
52 23
		$this->request = $request;
53 23
		$this->banner = $banner;
54
55 23
		add_form_key('phpbb_ads');
0 ignored issues
show
The function add_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
		/** @scrutinizer ignore-call */ 
56
  add_form_key('phpbb_ads');
Loading history...
56 23
	}
57
58
	/**
59
	 * Gets all errors
60
	 *
61
	 * @return	array	Errors
62
	 */
63 18
	public function get_errors()
64
	{
65 18
		return $this->errors;
66
	}
67
68
	/**
69
	 * Returns number of errors.
70
	 *
71
	 * @return	int	Number of errors
72
	 */
73 18
	public function has_errors()
74
	{
75 18
		return count($this->errors);
76
	}
77
78
	/**
79
	 * Get admin form data.
80
	 *
81
	 * @return	array	Form data
82
	 */
83 16
	public function get_form_data()
84
	{
85
		$data = array(
86 16
			'ad_name'         	=> $this->request->variable('ad_name', '', true),
87 16
			'ad_note'         	=> $this->request->variable('ad_note', '', true),
88 16
			'ad_code'         	=> $this->request->variable('ad_code', '', true),
89 16
			'ad_enabled'      	=> $this->request->variable('ad_enabled', 0),
90 16
			'ad_locations'    	=> $this->request->variable('ad_locations', array('')),
91 16
			'ad_start_date'     => $this->request->variable('ad_start_date', ''),
92 16
			'ad_end_date'     	=> $this->request->variable('ad_end_date', ''),
93 16
			'ad_priority'     	=> $this->request->variable('ad_priority', ext::DEFAULT_PRIORITY),
94 16
			'ad_content_only'	=> $this->request->variable('ad_content_only', 0),
95 16
			'ad_views_limit'  	=> $this->request->variable('ad_views_limit', 0),
96 16
			'ad_clicks_limit' 	=> $this->request->variable('ad_clicks_limit', 0),
97 16
			'ad_owner'        	=> $this->request->variable('ad_owner', '', true),
98 16
			'ad_groups'			=> $this->request->variable('ad_groups', array(0)),
99 16
			'ad_centering'		=> $this->request->variable('ad_centering', true),
100 16
		);
101
102
		// Validate form key
103 16
		if (!check_form_key('phpbb_ads'))
0 ignored issues
show
The function check_form_key was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

103
		if (!/** @scrutinizer ignore-call */ check_form_key('phpbb_ads'))
Loading history...
104 16
		{
105 2
			$this->errors[] = 'FORM_INVALID';
106 2
		}
107
108
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
109 16
		foreach ($data as $prop_name => $prop_val)
110
		{
111 16
			$method = 'validate_' . $prop_name;
112 16
			if (method_exists($this, $method))
113 16
			{
114 16
				$data[$prop_name] = $this->{$method}($prop_val);
115 16
			}
116 16
		}
117
118
		// Make sure start date is sooner than end date
119 16
		if ($data['ad_start_date'] != 0 && $data['ad_end_date'] != 0 && $data['ad_start_date'] > $data['ad_end_date'])
120 16
		{
121
			$this->errors[] = $this->language->lang('END_DATE_TOO_SOON');
122
		}
123
124 16
		return $data;
125
	}
126
127
	/**
128
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
129
	 *
130
	 * @param	 string	 $ad_code	 Current ad code
131
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
132
	 */
133 7
	public function banner_upload($ad_code)
134
	{
135
		try
136
		{
137 7
			$this->banner->create_storage_dir();
138 4
			$realname = $this->banner->upload();
139
140 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
0 ignored issues
show
The function generate_board_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
			$banner_html = '<img src="' . /** @scrutinizer ignore-call */ generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
Loading history...
141
142 3
			if ($this->request->is_ajax())
143 3
			{
144 1
				$this->send_ajax_response(true, $banner_html);
145
			}
146
147 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
148
		}
149 7
		catch (\phpbb\exception\runtime_exception $e)
0 ignored issues
show
The type phpbb\exception\runtime_exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
150
		{
151 4
			$this->banner->remove();
152
153 4
			if ($this->request->is_ajax())
154 4
			{
155 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
156
			}
157
158 3
			$this->errors[] = $this->language->lang($e->getMessage());
159
		}
160
161 5
		return $ad_code;
162
	}
163
164
	/**
165
	 * Validate advertisement name
166
	 *
167
	 * Ad name is required and must not be empty. Ad name must
168
	 * also be less than 255 characters.
169
	 *
170
	 * @param string $ad_name Advertisement name
171
	 * @return string Advertisement name
172
	 */
173 16
	protected function validate_ad_name($ad_name)
174
	{
175 16
		if ($ad_name === '')
176 16
		{
177 2
			$this->errors[] = 'AD_NAME_REQUIRED';
178 2
		}
179
180 16
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
0 ignored issues
show
The function truncate_string was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

180
		if (/** @scrutinizer ignore-call */ truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
Loading history...
181 16
		{
182 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
183 1
		}
184
185 16
		return $ad_name;
186
	}
187
188
	/**
189
	 * Validate advertisement code
190
	 *
191
	 * Ad code should not contain 4-byte Emoji characters.
192
	 *
193
	 * @param string $ad_code Advertisement code
194
	 * @return string Advertisement code
195
	 */
196 16
	protected function validate_ad_code($ad_code)
197
	{
198 16
		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $ad_code, $matches))
199 16
		{
200 1
			$characters = implode(' ', $matches[0]);
201 1
			$this->errors[] = $this->language->lang('AD_CODE_ILLEGAL_CHARS', $characters);
202 1
		}
203
204 16
		return $ad_code;
205
	}
206
207
	/**
208
	 * Validate advertisement start date
209
	 *
210
	 * @param string $start_date Advertisement start date
211
	 * @return int The start date converted to timestamp if valid, otherwise 0.
212
	 */
213 16
	protected function validate_ad_start_date($start_date)
214
	{
215 16
		return $this->validate_date($start_date, 'START');
216
	}
217
218
	/**
219
	 * Validate advertisement end date.
220
	 *
221
	 * @param string $end_date Advertisement end date
222
	 * @return int The end date converted to timestamp if valid, otherwise 0.
223
	 */
224 16
	protected function validate_ad_end_date($end_date)
225
	{
226 16
		return $this->validate_date($end_date, 'END');
227
	}
228
229
	/**
230
	 * Validate advertisement priority
231
	 *
232
	 * Ad priority must be an integer between 1 and 10.
233
	 *
234
	 * @param int $ad_priority Advertisement priority
235
	 * @return int Advertisement priority
236
	 */
237 16
	protected function validate_ad_priority($ad_priority)
238
	{
239 16
		if ($ad_priority < 1 || $ad_priority > 10)
240 16
		{
241 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
242 3
		}
243
244 16
		return $ad_priority;
245
	}
246
247
	/**
248
	 * Validate advertisement views limit
249
	 *
250
	 * Clicks must be a positive integer.
251
	 *
252
	 * @param int $ad_views_limit Advertisement views limit
253
	 * @return int Advertisement views limit
254
	 */
255 16
	protected function validate_ad_views_limit($ad_views_limit)
256
	{
257 16
		if ($ad_views_limit < 0)
258 16
		{
259 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
260 2
		}
261
262 16
		return $ad_views_limit;
263
	}
264
265
	/**
266
	 * Validate advertisement clicks limit
267
	 *
268
	 * Clicks must be a positive integer.
269
	 *
270
	 * @param int $ad_clicks_limit Advertisement clicks limit
271
	 * @return int Advertisement clicks limit
272
	 */
273 16
	protected function validate_ad_clicks_limit($ad_clicks_limit)
274
	{
275 16
		if ($ad_clicks_limit < 0)
276 16
		{
277 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
278 2
		}
279
280 16
		return $ad_clicks_limit;
281
	}
282
283
	/**
284
	 * Validate advertisement owner
285
	 *
286
	 * If ad owner name given, get their ID. If the ID returned is ANONYMOUS,
287
	 * set an error because the user name given doesn't exist.
288
	 *
289
	 * @param string $ad_owner User name
290
	 * @return int User id if user exists, otherwise 0.
291
	 */
292 16
	protected function validate_ad_owner($ad_owner)
293
	{
294 16
		$user_id = 0;
295 16
		if (!empty($ad_owner) && ANONYMOUS === ($user_id = (int) $this->user_loader->load_user_by_username($ad_owner)))
0 ignored issues
show
The constant phpbb\ads\controller\ANONYMOUS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
296 16
		{
297 3
			$this->errors[] = 'AD_OWNER_INVALID';
298 3
		}
299
300 16
		return ANONYMOUS !== $user_id ? $user_id : 0;
301
	}
302
303
	/**
304
	 * Send ajax response
305
	 *
306
	 * @param bool $success Is request successful?
307
	 * @param string $text Text to return
308
	 */
309 2
	protected function send_ajax_response($success, $text)
310
	{
311 2
		$json_response = new \phpbb\json_response;
0 ignored issues
show
The type phpbb\json_response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
312 2
		$json_response->send(array(
313 2
			'success'	=> $success,
314 2
			'title'		=> $this->language->lang('INFORMATION'),
315 2
			'text'		=> $text,
316 2
		));
317
	}
318
319
	/**
320
	 * Validate advertisement date
321
	 *
322
	 * The date must use the expected format of YYYY-MM-DD.
323
	 * If the date is valid, convert it to a timestamp and then
324
	 * make sure the timestamp is less than the current time.
325
	 *
326
	 * @param string $date Advertisement date
327
	 * @return int The date converted to timestamp if valid, otherwise 0.
328
	 */
329 16
	protected function validate_date($date, $type)
330
	{
331 16
		$timestamp = 0;
332 16
		if (preg_match('#^\d{4}-\d{2}-\d{2}$#', $date))
333 16
		{
334 3
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $date);
335
336 3
			if ($timestamp < time())
337 3
			{
338 3
				$this->errors[] = 'AD_' . $type . '_DATE_INVALID';
339 3
			}
340 3
		}
341 15
		else if ($date !== '')
342 15
		{
343 3
			$this->errors[] = 'AD_' . $type . '_DATE_INVALID';
344 3
		}
345
346 16
		return $timestamp;
347
	}
348
}
349