Completed
Push — master ( de1386...435892 )
by Matt
12s
created

admin_input::validate_ad_code()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2
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 */
21
	protected $user;
22
23
	/** @var \phpbb\user_loader */
24
	protected $user_loader;
25
26
	/** @var \phpbb\language\language */
27
	protected $language;
28
29
	/** @var \phpbb\request\request */
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 19
	 * @param \phpbb\language\language $language    Language object
44
	 * @param \phpbb\request\request   $request     Request object
45 19
	 * @param \phpbb\ads\banner\banner $banner      Banner upload object
46 19
	 */
47 19
	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 19
	{
49
		$this->user = $user;
50 19
		$this->user_loader = $user_loader;
51 19
		$this->language = $language;
52
		$this->request = $request;
53
		$this->banner = $banner;
54
55
		add_form_key('phpbb_ads');
56
	}
57
58 14
	/**
59
	 * Gets all errors
60 14
	 *
61
	 * @return	array	Errors
62
	 */
63
	public function get_errors()
64
	{
65
		return $this->errors;
66
	}
67
68 14
	/**
69
	 * Returns number of errors.
70 14
	 *
71
	 * @return	int	Number of errors
72
	 */
73
	public function has_errors()
74
	{
75
		return count($this->errors);
76
	}
77
78 12
	/**
79
	 * Get admin form data.
80
	 *
81 12
	 * @return	array	Form data
82 12
	 */
83 12
	public function get_form_data()
84 12
	{
85 12
		$data = array(
86 12
			'ad_name'         => $this->request->variable('ad_name', '', true),
87 12
			'ad_note'         => $this->request->variable('ad_note', '', true),
88 12
			'ad_code'         => $this->request->variable('ad_code', '', true),
89 12
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
90 12
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
91 12
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
92
			'ad_priority'     => $this->request->variable('ad_priority', ext::DEFAULT_PRIORITY),
93
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
94 12
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
95 12
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
96 2
		);
97 2
98
		// Validate form key
99
		if (!check_form_key('phpbb_ads'))
100 12
		{
101
			$this->errors[] = 'FORM_INVALID';
102 12
		}
103 12
104 12
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
105 12
		foreach ($data as $prop_name => $prop_val)
106 12
		{
107
			$method = 'validate_' . $prop_name;
108
			if (method_exists($this, $method))
109 12
			{
110 12
				$data[$prop_name] = $this->{$method}($prop_val);
111 12
			}
112 10
		}
113 10
114
		return $data;
115 12
	}
116
117
	/**
118
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
119
	 *
120
	 * @param	 string	 $ad_code	 Current ad code
121
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
122
	 */
123
	public function banner_upload($ad_code)
124 7
	{
125
		try
126
		{
127
			$this->banner->create_storage_dir();
128 7
			$realname = $this->banner->upload();
129 4
130
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
131 3
132
			if ($this->request->is_ajax())
133 3
			{
134 3
				$this->send_ajax_response(true, $banner_html);
135 1
			}
136
137
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
138 2
		}
139
		catch (\phpbb\exception\runtime_exception $e)
0 ignored issues
show
Bug introduced by
The class phpbb\exception\runtime_exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
140 7
		{
141
			$this->banner->remove();
142 4
143
			if ($this->request->is_ajax())
144 4
			{
145 4
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
146 1
			}
147
148
			$this->errors[] = $this->language->lang($e->getMessage());
149 3
		}
150
151
		return $ad_code;
152 5
	}
153
154
	/**
155
	 * Validate advertisement name
156
	 *
157
	 * Ad name is required and must not be empty. Ad name must
158
	 * also be less than 255 characters.
159
	 *
160 12
	 * @param string $ad_name Advertisement name
161
	 * @return string Advertisement name
162 12
	 */
163 12
	protected function validate_ad_name($ad_name)
164 2
	{
165 2
		if ($ad_name === '')
166 12
		{
167 12
			$this->errors[] = 'AD_NAME_REQUIRED';
168 1
		}
169 1
170 12
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
171
		{
172
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
173
		}
174
175
		return $ad_name;
176
	}
177 12
178
	/**
179 12
	 * Validate advertisement code
180 12
	 *
181 2
	 * Ad code should not contain 4-byte Emoji characters.
182
	 *
183 2
	 * @param string $ad_code Advertisement code
184 2
	 * @return string Advertisement code
185 1
	 */
186 1
	protected function validate_ad_code($ad_code)
187 2
	{
188 10
		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $ad_code, $matches))
189 10
		{
190 2
			$characters = implode(' ', $matches[0]);
191 2
			$this->errors[] = $this->language->lang('AD_CODE_ILLEGAL_CHARS', $characters);
192 12
		}
193
194
		return $ad_code;
195
	}
196
197
	/**
198
	 * Validate advertisement end date
199 12
	 *
200
	 * End date must use the expected format of YYYY-MM-DD.
201 12
	 * If the date is valid, convert it to a timestamp and then
202 12
	 * make sure the timestamp is less than the current time.
203 3
	 *
204 3
	 * @param string $end_date Advertisement end date
205 12
	 * @return int The end date converted to timestamp if valid, otherwise 0.
206
	 */
207
	protected function validate_ad_end_date($end_date)
208
	{
209
		$timestamp = 0;
210
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
211
		{
212 12
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $end_date);
213
214 12
			if ($timestamp < time())
215 12
			{
216 2
				$this->errors[] = 'AD_END_DATE_INVALID';
217 2
			}
218 12
		}
219
		else if ($end_date !== '')
220
		{
221
			$this->errors[] = 'AD_END_DATE_INVALID';
222
		}
223
224
		return $timestamp;
225 12
	}
226
227 12
	/**
228 12
	 * Validate advertisement priority
229 2
	 *
230 2
	 * Ad priority must be an integer between 1 and 10.
231 12
	 *
232
	 * @param int $ad_priority Advertisement priority
233
	 * @return int Advertisement priority
234
	 */
235
	protected function validate_ad_priority($ad_priority)
236
	{
237
		if ($ad_priority < 1 || $ad_priority > 10)
238 12
		{
239
			$this->errors[] = 'AD_PRIORITY_INVALID';
240
		}
241 12
242 12
		return $ad_priority;
243 2
	}
244 2
245 12
	/**
246
	 * Validate advertisement views limit
247
	 *
248
	 * Clicks must be a positive integer.
249
	 *
250
	 * @param int $ad_views_limit Advertisement views limit
251
	 * @return int Advertisement views limit
252
	 */
253 12
	protected function validate_ad_views_limit($ad_views_limit)
254
	{
255 12
		if ($ad_views_limit < 0)
256
		{
257
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
258
		}
259
260
		return $ad_views_limit;
261
	}
262
263
	/**
264 10
	 * Validate advertisement clicks limit
265
	 *
266 10
	 * Clicks must be a positive integer.
267 10
	 *
268 9
	 * @param int $ad_clicks_limit Advertisement clicks limit
269
	 * @return int Advertisement clicks limit
270
	 */
271 1
	protected function validate_ad_clicks_limit($ad_clicks_limit)
272 1
	{
273
		if ($ad_clicks_limit < 0)
274
		{
275
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
276
		}
277
278
		return $ad_clicks_limit;
279
	}
280
281 2
	/**
282
	 * Validate advertisement owner
283 2
	 *
284 2
	 * If ad owner name given, get their ID. If the ID returned is ANONYMOUS,
285 2
	 * set an error because the user name given doesn't exist.
286 2
	 *
287 2
	 * @param string $ad_owner User name
288 2
	 * @return int User id if user exists, otherwise 0.
289
	 */
290
	protected function validate_ad_owner($ad_owner)
291
	{
292
		$user_id = 0;
293
		if (!empty($ad_owner) && ANONYMOUS === ($user_id = (int) $this->user_loader->load_user_by_username($ad_owner)))
294
		{
295
			$this->errors[] = 'AD_OWNER_INVALID';
296
		}
297
298
		return ANONYMOUS !== $user_id ? $user_id : 0;
299
	}
300
301
	/**
302
	 * Send ajax response
303
	 *
304
	 * @param bool $success Is request successful?
305
	 * @param string $text Text to return
306
	 */
307
	protected function send_ajax_response($success, $text)
308
	{
309
		$json_response = new \phpbb\json_response;
310
		$json_response->send(array(
311
			'success'	=> $success,
312
			'title'		=> $this->language->lang('INFORMATION'),
313
			'text'		=> $text,
314
		));
315
	}
316
}
317