Completed
Pull Request — master (#54)
by Jakub
08:52
created

admin_input::validate_ad_views_limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 6
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\controller\admin_controller as controller;
14
15
/**
16
 * Admin input
17
 */
18
class admin_input
19
{
20
	const MAX_NAME_LENGTH = 255;
21
	const DEFAULT_PRIORITY = 5;
22
23
	/** @var \phpbb\user */
24
	protected $user;
25
26
	/** @var \phpbb\request\request */
27
	protected $request;
28
29
	/** @var \phpbb\files\upload */
30
	protected $files_upload;
31
32
	/** @var \phpbb\filesystem\filesystem_interface */
33
	protected $filesystem;
34
35
	/** @var string */
36
	protected $root_path;
37
38
	/** @var string */
39
	protected $php_ext;
40
41
	/** @var array Form validation errors */
42
	protected $errors = array();
43
44
	/**
45
	 * Constructor
46
	 *
47
	 * @param \phpbb\user								$user			User object
48
	 * @param \phpbb\request\request					$request		Request object
49
	 * @param \phpbb\files\upload						$files_upload	Files upload object
50
	 * @param \phpbb\filesystem\filesystem_interface	$filesystem		Filesystem object
51
	 * @param string									$root_path		Root path
52
	 * @param string									$php_ext		PHP extension
53
	 */
54
	public function __construct(\phpbb\user $user, \phpbb\request\request $request, \phpbb\files\upload $files_upload, \phpbb\filesystem\filesystem_interface $filesystem, $root_path, $php_ext)
55
	{
56
		$this->user = $user;
57
		$this->request = $request;
58
		$this->files_upload = $files_upload;
59
		$this->filesystem = $filesystem;
60
		$this->root_path = $root_path;
61
		$this->php_ext = $php_ext;
62
	}
63
64
	public function setup()
65
	{
66
		if (!function_exists('user_get_id_name'))
67
		{
68
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
69
		}
70
	}
71
72
	public function get_errors()
73
	{
74
		return $this->errors;
75
	}
76
77
	/**
78
	 * Get admin form data.
79
	 *
80
	 * @param	string	$form_name	The form name.
81
	 * @return	array	Form data
82
	 */
83
	public function get_form_data($form_name)
84
	{
85
		$data = array(
86
			'ad_name'         => $this->request->variable('ad_name', '', true),
87
			'ad_note'         => $this->request->variable('ad_note', '', true),
88
			'ad_code'         => $this->request->variable('ad_code', '', true),
89
			'ad_enabled'      => $this->request->variable('ad_enabled', 0),
90
			'ad_locations'    => $this->request->variable('ad_locations', array('')),
91
			'ad_end_date'     => $this->request->variable('ad_end_date', ''),
92
			'ad_priority'     => $this->request->variable('ad_priority', self::DEFAULT_PRIORITY),
93
			'ad_views_limit'  => $this->request->variable('ad_views_limit', 0),
94
			'ad_clicks_limit' => $this->request->variable('ad_clicks_limit', 0),
95
			'ad_owner'        => $this->request->variable('ad_owner', '', true),
96
		);
97
98
		// Validate form key
99
		if (!check_form_key($form_name))
100
		{
101
			$this->errors[] = $this->user->lang('FORM_INVALID');
102
		}
103
104
		// Validate each property. Every method adds errors directly to $this->errors.
105
		foreach ($data as $prop_name => $prop_val)
106
		{
107
			if (method_exists($this, 'validate_' . $prop_name))
108
			{
109
				$this->{'validate_' . $prop_name}($prop_val);
110
			}
111
		}
112
113
		// Replace end date and owner with IDs that will be stored in the DB
114
		$data['ad_end_date'] = $this->end_date_to_timestamp($data['ad_end_date']);
115
		$data['ad_owner'] = $this->owner_to_id($data['ad_owner']);
116
117
		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	 mixed	 \phpbb\json_response when request is ajax or updated ad code otherwise.
125
	 */
126
	public function process_banner_upload($ad_code)
127
	{
128
		// Set file restrictions
129
		$this->files_upload->reset_vars();
130
		$this->files_upload->set_allowed_extensions(array('gif', 'jpg', 'jpeg', 'png'));
131
132
		// Upload file
133
		$file = $this->files_upload->handle_upload('files.types.form', 'banner');
134
		$file->clean_filename('unique_ext');
135
136
		// First lets create phpbb_ads directory if needed
137
		if (!$this->filesystem->exists($this->root_path . 'images/phpbb_ads'))
138
		{
139
			try
140
			{
141
				$this->filesystem->mkdir($this->root_path . 'images/phpbb_ads');
142
			}
143
			catch (\phpbb\filesystem\exception\filesystem_exception $e)
0 ignored issues
show
Bug introduced by
The class phpbb\filesystem\exception\filesystem_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...
144
			{
145
				$file->set_error($this->user->lang($e->getMessage()));
146
			}
147
		}
148
149
		// Move file to proper location
150
		if (!$file->move_file('images/phpbb_ads'))
151
		{
152
			$file->set_error($this->user->lang('FILE_MOVE_UNSUCCESSFUL'));
153
		}
154
155
		// Problem with uploading
156
		if (count($file->error))
157
		{
158
			$file->remove();
159
			if ($this->request->is_ajax())
160
			{
161
				$json_response = new \phpbb\json_response;
162
				$json_response->send(array(
163
					'success'	=> false,
164
					'title'		=> $this->user->lang('INFORMATION'),
165
					'text'		=> implode('<br />', $file->error),
166
				));
167
			}
168
			else
169
			{
170
				$this->errors[] = implode('<br />', $file->error);
171
			}
172
		}
173
		else
174
		{
175
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
176
177
			if ($this->request->is_ajax())
178
			{
179
				$json_response = new \phpbb\json_response;
180
				$json_response->send(array(
181
					'success'	=> true,
182
					'text'		=> $banner_html,
183
				));
184
			}
185
186
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
187
		}
188
189
		return $ad_code;
190
	}
191
192
	protected function validate_ad_name($ad_name)
193
	{
194
		if ($ad_name === '')
195
		{
196
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
197
		}
198
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
199
		{
200
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
201
		}
202
	}
203
204
	protected function validate_ad_end_date($end_date)
205
	{
206
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
207
		{
208
			$end_date = (int) $this->end_date_to_timestamp($end_date);
209
210
			if ($end_date < time())
211
			{
212
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
213
			}
214
		}
215
		else if ($end_date !== '')
216
		{
217
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
218
		}
219
	}
220
221
	protected function validate_ad_priority($ad_priority)
222
	{
223
		if ($ad_priority < 1 || $ad_priority > 10)
224
		{
225
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
226
		}
227
	}
228
229
	protected function validate_ad_views_limit($ad_views_limit)
230
	{
231
		if ($ad_views_limit < 0)
232
		{
233
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
234
		}
235
	}
236
237
	protected function validate_ad_clicks_limit($ad_clicks_limit)
238
	{
239
		if ($ad_clicks_limit < 0)
240
		{
241
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
242
		}
243
	}
244
245
	protected function validate_ad_owner($ad_owner)
246
	{
247
		// user_get_id_name function returns false if everything is OK.
248
		if (!empty($ad_owner) && user_get_id_name($ad_owner_id, $ad_owner))
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
249
		{
250
			$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
251
		}
252
	}
253
254
	protected function end_date_to_timestamp($end_date)
255
	{
256
		return (int) $this->user->get_timestamp_from_format(controller::DATE_FORMAT, $end_date);
257
	}
258
259
	protected function owner_to_id($ad_owner)
260
	{
261
		if (empty($ad_owner))
262
		{
263
			return 0;
264
		}
265
		else
266
		{
267
			user_get_id_name($ad_owner_id, $ad_owner);
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
268
			return $ad_owner_id[0];
0 ignored issues
show
Bug introduced by
The variable $ad_owner_id does not exist. Did you mean $ad_owner?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
269
		}
270
	}
271
}