Completed
Push — master ( 4ea723...0a266a )
by Matt
10s
created

admin_input::has_errors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 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
		);
99
100
		// Validate form key
101 14
		if (!check_form_key('phpbb_ads'))
102 14
		{
103 2
			$this->errors[] = 'FORM_INVALID';
104 2
		}
105
106
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
107 14
		foreach ($data as $prop_name => $prop_val)
108
		{
109 14
			$method = 'validate_' . $prop_name;
110 14
			if (method_exists($this, $method))
111 14
			{
112 14
				$data[$prop_name] = $this->{$method}($prop_val);
113 14
			}
114 14
		}
115
116 14
		return $data;
117
	}
118
119
	/**
120
	 * Upload image and return updated ad code or <img> of new banner when using ajax.
121
	 *
122
	 * @param	 string	 $ad_code	 Current ad code
123
	 * @return	 string	 \phpbb\json_response when request is ajax or updated ad code otherwise.
124
	 */
125 7
	public function banner_upload($ad_code)
126
	{
127
		try
128
		{
129 7
			$this->banner->create_storage_dir();
130 4
			$realname = $this->banner->upload();
131
132 3
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $realname . '" />';
133
134 3
			if ($this->request->is_ajax())
135 3
			{
136 1
				$this->send_ajax_response(true, $banner_html);
137
			}
138
139 2
			$ad_code = ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
140
		}
141 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...
142
		{
143 4
			$this->banner->remove();
144
145 4
			if ($this->request->is_ajax())
146 4
			{
147 1
				$this->send_ajax_response(false, $this->language->lang($e->getMessage()));
148
			}
149
150 3
			$this->errors[] = $this->language->lang($e->getMessage());
151
		}
152
153 5
		return $ad_code;
154
	}
155
156
	/**
157
	 * Validate advertisement name
158
	 *
159
	 * Ad name is required and must not be empty. Ad name must
160
	 * also be less than 255 characters.
161
	 *
162
	 * @param string $ad_name Advertisement name
163
	 * @return string Advertisement name
164
	 */
165 14
	protected function validate_ad_name($ad_name)
166
	{
167 14
		if ($ad_name === '')
168 14
		{
169 2
			$this->errors[] = 'AD_NAME_REQUIRED';
170 2
		}
171
172 14
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
173 14
		{
174 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
175 1
		}
176
177 14
		return $ad_name;
178
	}
179
180
	/**
181
	 * Validate advertisement code
182
	 *
183
	 * Ad code should not contain 4-byte Emoji characters.
184
	 *
185
	 * @param string $ad_code Advertisement code
186
	 * @return string Advertisement code
187
	 */
188 14
	protected function validate_ad_code($ad_code)
189
	{
190 14
		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $ad_code, $matches))
191 14
		{
192 1
			$characters = implode(' ', $matches[0]);
193 1
			$this->errors[] = $this->language->lang('AD_CODE_ILLEGAL_CHARS', $characters);
194 1
		}
195
196 14
		return $ad_code;
197
	}
198
199
	/**
200
	 * Validate advertisement end date
201
	 *
202
	 * End date must use the expected format of YYYY-MM-DD.
203
	 * If the date is valid, convert it to a timestamp and then
204
	 * make sure the timestamp is less than the current time.
205
	 *
206
	 * @param string $end_date Advertisement end date
207
	 * @return int The end date converted to timestamp if valid, otherwise 0.
208
	 */
209 14
	protected function validate_ad_end_date($end_date)
210
	{
211 14
		$timestamp = 0;
212 14
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
213 14
		{
214 2
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $end_date);
215
216 2
			if ($timestamp < time())
217 2
			{
218 1
				$this->errors[] = 'AD_END_DATE_INVALID';
219 1
			}
220 2
		}
221 12
		else if ($end_date !== '')
222 12
		{
223 2
			$this->errors[] = 'AD_END_DATE_INVALID';
224 2
		}
225
226 14
		return $timestamp;
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 14
	protected function validate_ad_priority($ad_priority)
238
	{
239 14
		if ($ad_priority < 1 || $ad_priority > 10)
240 14
		{
241 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
242 3
		}
243
244 14
		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 14
	protected function validate_ad_views_limit($ad_views_limit)
256
	{
257 14
		if ($ad_views_limit < 0)
258 14
		{
259 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
260 2
		}
261
262 14
		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 14
	protected function validate_ad_clicks_limit($ad_clicks_limit)
274
	{
275 14
		if ($ad_clicks_limit < 0)
276 14
		{
277 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
278 2
		}
279
280 14
		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 14
	protected function validate_ad_owner($ad_owner)
293
	{
294 14
		$user_id = 0;
295 14
		if (!empty($ad_owner) && ANONYMOUS === ($user_id = (int) $this->user_loader->load_user_by_username($ad_owner)))
296 14
		{
297 3
			$this->errors[] = 'AD_OWNER_INVALID';
298 3
		}
299
300 14
		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;
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