Completed
Pull Request — master (#126)
by Jakub
11:59
created

admin_input   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 302
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 97.52%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 33
lcom 1
cbo 1
dl 0
loc 302
ccs 118
cts 121
cp 0.9752
rs 9.3999
c 4
b 1
f 0

13 Methods

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