Completed
Pull Request — master (#89)
by Matt
12:19
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
	 * @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 20
	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 20
		$this->user = $user;
50 20
		$this->user_loader = $user_loader;
51 20
		$this->language = $language;
52 20
		$this->request = $request;
53 20
		$this->banner = $banner;
54
55 20
		add_form_key('phpbb_ads');
56 20
	}
57
58
	/**
59
	 * Gets all errors
60
	 *
61
	 * @return	array	Errors
62
	 */
63 15
	public function get_errors()
64
	{
65 15
		return $this->errors;
66
	}
67
68
	/**
69
	 * Returns number of errors.
70
	 *
71
	 * @return	int	Number of errors
72
	 */
73 15
	public function has_errors()
74
	{
75 15
		return count($this->errors);
76
	}
77
78
	/**
79
	 * Get admin form data.
80
	 *
81
	 * @return	array	Form data
82
	 */
83 13
	public function get_form_data()
84
	{
85
		$data = array(
86 13
			'ad_name'         => $this->request->variable('ad_name', '', true),
87 13
			'ad_note'         => $this->request->variable('ad_note', '', true),
88 13
			'ad_code'         => $this->request->variable('ad_code', '', true),
89 13
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
90 13
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
91 13
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
92 13
			'ad_priority'     => $this->request->variable('ad_priority', ext::DEFAULT_PRIORITY),
93 13
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
94 13
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
95 13
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
96 13
		);
97
98
		// Validate form key
99 13
		if (!check_form_key('phpbb_ads'))
100 13
		{
101 2
			$this->errors[] = 'FORM_INVALID';
102 2
		}
103
104
		// Validate each property. Some validators update the property value. Errors are added to $this->errors.
105 13
		foreach ($data as $prop_name => &$prop_val)
106
		{
107 13
			$method = 'validate_' . $prop_name;
108 13
			if (method_exists($this, $method))
109 13
			{
110 13
				$prop_val = $this->{$method}($prop_val);
111 13
			}
112 13
		}
113
114 13
		unset($prop_val);
115
116 13
		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
	 * @param string $ad_name Advertisement name
160
	 * @return string Advertisement name
161
	 */
162 13
	protected function validate_ad_name($ad_name)
163
	{
164 13
		if ($ad_name === '')
165 13
		{
166 2
			$this->errors[] = 'AD_NAME_REQUIRED';
167 2
		}
168
169 13
		if (truncate_string($ad_name, ext::MAX_NAME_LENGTH) !== $ad_name)
170 13
		{
171 1
			$this->errors[] = $this->language->lang('AD_NAME_TOO_LONG', ext::MAX_NAME_LENGTH);
172 1
		}
173
174 13
		return $ad_name;
175
	}
176
177
	/**
178
	 * Validate advertisement code
179
	 *
180
	 * @param string $ad_code Advertisement code
181
	 * @return string Advertisement code
182
	 */
183 13
	protected function validate_ad_code($ad_code)
184
	{
185 13
		if (preg_match_all('/[\x{10000}-\x{10FFFF}]/u', $ad_code, $matches))
186 13
		{
187 1
			$characters = implode(' ', $matches[0]);
188 1
			$this->errors[] = $this->language->lang('AD_CODE_ILLEGAL_CHARS', $characters);
189 1
		}
190
191 13
		return $ad_code;
192
	}
193
194
	/**
195
	 * Validate advertisement end date
196
	 *
197
	 * @param string $end_date Advertisement end date
198
	 * @return int The end date converted to timestamp if valid, otherwise 0.
199
	 */
200 13
	protected function validate_ad_end_date($end_date)
201
	{
202 13
		$timestamp = 0;
203 13
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
204 13
		{
205 2
			$timestamp = (int) $this->user->get_timestamp_from_format(ext::DATE_FORMAT, $end_date);
206
207 2
			if ($timestamp < time())
208 2
			{
209 1
				$this->errors[] = 'AD_END_DATE_INVALID';
210 1
			}
211 2
		}
212 11
		else if ($end_date !== '')
213 11
		{
214 2
			$this->errors[] = 'AD_END_DATE_INVALID';
215 2
		}
216
217 13
		return $timestamp;
218
	}
219
220
	/**
221
	 * Validate advertisement priority
222
	 *
223
	 * @param int $ad_priority Advertisement priority
224
	 * @return int Advertisement priority
225
	 */
226 13
	protected function validate_ad_priority($ad_priority)
227
	{
228 13
		if ($ad_priority < 1 || $ad_priority > 10)
229 13
		{
230 3
			$this->errors[] = 'AD_PRIORITY_INVALID';
231 3
		}
232
233 13
		return $ad_priority;
234
	}
235
236
	/**
237
	 * Validate advertisement views limit
238
	 *
239
	 * @param int $ad_views_limit Advertisement views limit
240
	 * @return int Advertisement views limit
241
	 */
242 13
	protected function validate_ad_views_limit($ad_views_limit)
243
	{
244 13
		if ($ad_views_limit < 0)
245 13
		{
246 2
			$this->errors[] = 'AD_VIEWS_LIMIT_INVALID';
247 2
		}
248
249 13
		return $ad_views_limit;
250
	}
251
252
	/**
253
	 * Validate advertisement clicks limit
254
	 *
255
	 * @param int $ad_clicks_limit Advertisement clicks limit
256
	 * @return int Advertisement clicks limit
257
	 */
258 13
	protected function validate_ad_clicks_limit($ad_clicks_limit)
259
	{
260 13
		if ($ad_clicks_limit < 0)
261 13
		{
262 2
			$this->errors[] = 'AD_CLICKS_LIMIT_INVALID';
263 2
		}
264
265 13
		return $ad_clicks_limit;
266
	}
267
268
	/**
269
	 * Validate advertisement owner
270
	 *
271
	 * @param string $ad_owner User name
272
	 * @return int User id if user exists, otherwise 0.
273
	 */
274 13
	protected function validate_ad_owner($ad_owner)
275
	{
276 13
		$user_id = 0;
277 13
		if (!empty($ad_owner) && ANONYMOUS === ($user_id = (int) $this->user_loader->load_user_by_username($ad_owner)))
278 13
		{
279 2
			$this->errors[] = 'AD_OWNER_INVALID';
280 2
		}
281
282 13
		return ANONYMOUS !== $user_id ? $user_id : 0;
283
	}
284
285
	/**
286
	 * Send ajax response
287
	 *
288
	 * @param bool $success Is request successful?
289
	 * @param string $text Text to return
290
	 */
291 2
	protected function send_ajax_response($success, $text)
292
	{
293 2
		$json_response = new \phpbb\json_response;
294 2
		$json_response->send(array(
295 2
			'success'	=> $success,
296 2
			'title'		=> $this->language->lang('INFORMATION'),
297 2
			'text'		=> $text,
298 2
		));
299
	}
300
}
301