Completed
Pull Request — master (#114)
by Jakub
26:35 queued 24:38
created

admin_input::validate_ad_clicks_limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
c 1
b 0
f 0
cc 2
eloc 4
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
	 * @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 21
	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 21
		$this->user = $user;
50 21
		$this->user_loader = $user_loader;
51 21
		$this->language = $language;
52 21
		$this->request = $request;
53 21
		$this->banner = $banner;
54
55 21
		add_form_key('phpbb_ads');
56 21
	}
57
58
	/**
59
	 * Gets all errors
60
	 *
61
	 * @return	array	Errors
62
	 */
63 16
	public function get_errors()
64
	{
65 16
		return $this->errors;
66
	}
67
68
	/**
69
	 * Returns number of errors.
70
	 *
71
	 * @return	int	Number of errors
72
	 */
73 16
	public function has_errors()
74
	{
75 16
		return count($this->errors);
76
	}
77
78
	/**
79
	 * Get admin form data.
80
	 *
81
	 * @return	array	Form data
82
	 */
83 14
	public function get_form_data()
84
	{
85
		$data = array(
86 14
			'ad_name'         	=> $this->request->variable('ad_name', '', true),
87 14
			'ad_note'         	=> $this->request->variable('ad_note', '', true),
88 14
			'ad_code'         	=> $this->request->variable('ad_code', '', true),
89 14
			'ad_enabled'      	=> $this->request->variable('ad_enabled', 0),
90 14
			'ad_locations'    	=> $this->request->variable('ad_locations', array('')),
91 14
			'ad_end_date'     	=> $this->request->variable('ad_end_date', ''),
92 14
			'ad_priority'     	=> $this->request->variable('ad_priority', ext::DEFAULT_PRIORITY),
93 14
			'ad_content_only'	=> $this->request->variable('ad_content_only', 0),
94 14
			'ad_views_limit'  	=> $this->request->variable('ad_views_limit', 0),
95 14
			'ad_clicks_limit' 	=> $this->request->variable('ad_clicks_limit', 0),
96 14
			'ad_owner'        	=> $this->request->variable('ad_owner', '', true),
97 14
		);
98
99
		// Validate form key
100 14
		if (!check_form_key('phpbb_ads'))
101 14
		{
102 2
			$this->errors[] = 'FORM_INVALID';
103 2
		}
104
105
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
106 14
		foreach ($data as $prop_name => $prop_val)
107
		{
108 14
			$method = 'validate_' . $prop_name;
109 14
			if (method_exists($this, $method))
110 14
			{
111 14
				$data[$prop_name] = $this->{$method}($prop_val);
112 14
			}
113 14
		}
114
115 14
		return $data;
116
	}
117
118
	/**
119
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
120
	 *
121
	 * @param	 string	 $ad_code	 Current ad code
122
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
123
	 */
124 7
	public function banner_upload($ad_code)
125
	{
126
		try
127
		{
128 7
			$this->banner->create_storage_dir();
129 4
			$realname = $this->banner->upload();
130
131 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
132
133 3
			if ($this->request->is_ajax())
134 3
			{
135 1
				$this->send_ajax_response(true, $banner_html);
136
			}
137
138 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
139
		}
140 7
		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...
141
		{
142 4
			$this->banner->remove();
143
144 4
			if ($this->request->is_ajax())
145 4
			{
146 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
147
			}
148
149 3
			$this->errors[] = $this->language->lang($e->getMessage());
150
		}
151
152 5
		return $ad_code;
153
	}
154
155
	/**
156
	 * Validate advertisement name
157
	 *
158
	 * Ad name is required and must not be empty. Ad name must
159
	 * also be less than 255 characters.
160
	 *
161
	 * @param string $ad_name Advertisement name
162
	 * @return string Advertisement name
163
	 */
164 14
	protected function validate_ad_name($ad_name)
165
	{
166 14
		if ($ad_name === '')
167 14
		{
168 2
			$this->errors[] = 'AD_NAME_REQUIRED';
169 2
		}
170
171 14
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
172 14
		{
173 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
174 1
		}
175
176 14
		return $ad_name;
177
	}
178
179
	/**
180
	 * Validate advertisement code
181
	 *
182
	 * Ad code should not contain 4-byte Emoji characters.
183
	 *
184
	 * @param string $ad_code Advertisement code
185
	 * @return string Advertisement code
186
	 */
187 14
	protected function validate_ad_code($ad_code)
188
	{
189 14
		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $ad_code, $matches))
190 14
		{
191 1
			$characters = implode(' ', $matches[0]);
192 1
			$this->errors[] = $this->language->lang('AD_CODE_ILLEGAL_CHARS', $characters);
193 1
		}
194
195 14
		return $ad_code;
196
	}
197
198
	/**
199
	 * Validate advertisement end date
200
	 *
201
	 * End date must use the expected format of YYYY-MM-DD.
202
	 * If the date is valid, convert it to a timestamp and then
203
	 * make sure the timestamp is less than the current time.
204
	 *
205
	 * @param string $end_date Advertisement end date
206
	 * @return int The end date converted to timestamp if valid, otherwise 0.
207
	 */
208 14
	protected function validate_ad_end_date($end_date)
209
	{
210 14
		$timestamp = 0;
211 14
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
212 14
		{
213 2
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $end_date);
214
215 2
			if ($timestamp < time())
216 2
			{
217 1
				$this->errors[] = 'AD_END_DATE_INVALID';
218 1
			}
219 2
		}
220 12
		else if ($end_date !== '')
221 12
		{
222 2
			$this->errors[] = 'AD_END_DATE_INVALID';
223 2
		}
224
225 14
		return $timestamp;
226
	}
227
228
	/**
229
	 * Validate advertisement priority
230
	 *
231
	 * Ad priority must be an integer between 1 and 10.
232
	 *
233
	 * @param int $ad_priority Advertisement priority
234
	 * @return int Advertisement priority
235
	 */
236 14
	protected function validate_ad_priority($ad_priority)
237
	{
238 14
		if ($ad_priority < 1 || $ad_priority > 10)
239 14
		{
240 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
241 3
		}
242
243 14
		return $ad_priority;
244
	}
245
246
	/**
247
	 * Validate advertisement views limit
248
	 *
249
	 * Clicks must be a positive integer.
250
	 *
251
	 * @param int $ad_views_limit Advertisement views limit
252
	 * @return int Advertisement views limit
253
	 */
254 14
	protected function validate_ad_views_limit($ad_views_limit)
255
	{
256 14
		if ($ad_views_limit < 0)
257 14
		{
258 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
259 2
		}
260
261 14
		return $ad_views_limit;
262
	}
263
264
	/**
265
	 * Validate advertisement clicks limit
266
	 *
267
	 * Clicks must be a positive integer.
268
	 *
269
	 * @param int $ad_clicks_limit Advertisement clicks limit
270
	 * @return int Advertisement clicks limit
271
	 */
272 14
	protected function validate_ad_clicks_limit($ad_clicks_limit)
273
	{
274 14
		if ($ad_clicks_limit < 0)
275 14
		{
276 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
277 2
		}
278
279 14
		return $ad_clicks_limit;
280
	}
281
282
	/**
283
	 * Validate advertisement owner
284
	 *
285
	 * If ad owner name given, get their ID. If the ID returned is ANONYMOUS,
286
	 * set an error because the user name given doesn't exist.
287
	 *
288
	 * @param string $ad_owner User name
289
	 * @return int User id if user exists, otherwise 0.
290
	 */
291 14
	protected function validate_ad_owner($ad_owner)
292
	{
293 14
		$user_id = 0;
294 14
		if (!empty($ad_owner) && ANONYMOUS === ($user_id = (int) $this->user_loader->load_user_by_username($ad_owner)))
295 14
		{
296 3
			$this->errors[] = 'AD_OWNER_INVALID';
297 3
		}
298
299 14
		return ANONYMOUS !== $user_id ? $user_id : 0;
300
	}
301
302
	/**
303
	 * Send ajax response
304
	 *
305
	 * @param bool $success Is request successful?
306
	 * @param string $text Text to return
307
	 */
308 2
	protected function send_ajax_response($success, $text)
309
	{
310 2
		$json_response = new \phpbb\json_response;
311 2
		$json_response->send(array(
312 2
			'success'	=> $success,
313 2
			'title'		=> $this->language->lang('INFORMATION'),
314 2
			'text'		=> $text,
315 2
		));
316
	}
317
}
318