Completed
Pull Request — master (#54)
by Jakub
07:53
created

admin_input::validate_ad_priority()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

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