Completed
Pull Request — master (#54)
by Jakub
10:32
created

admin_input::get_form_data()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 36
ccs 0
cts 29
cp 0
rs 8.5806
cc 4
eloc 20
nc 6
nop 1
crap 20
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
		try
130
		{
131
			$this->create_storage_dir();
132
		}
133
		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...
134
		{
135
			$file->set_error($this->user->lang($e->getMessage()));
136
		}
137
138
		// Move file to proper location
139
		if (!$file->move_file('images/phpbb_ads'))
140
		{
141
			$file->set_error($this->user->lang('FILE_MOVE_UNSUCCESSFUL'));
142
		}
143
144
		// Problem with uploading
145
		if (count($file->error))
146
		{
147
			$file->remove();
148
			if ($this->request->is_ajax())
149
			{
150
				$this->send_ajax_response(false, implode('<br />', $file->error));
151
			}
152
153
			$this->errors[] = implode('<br />', $file->error);
154
		}
155
		else
156
		{
157
			$banner_html = '<img src="' . generate_board_url() . '/images/phpbb_ads/' . $file->get('realname') . '" />';
158
159
			if ($this->request->is_ajax())
160
			{
161
				$this->send_ajax_response(true, $banner_html);
162
			}
163
164
			return ($ad_code ? $ad_code . "\n\n" : '') . $banner_html;
165
		}
166
167
		return $ad_code;
168
	}
169
170
	protected function validate_ad_name($ad_name)
171
	{
172
		if ($ad_name === '')
173
		{
174
			$this->errors[] = $this->user->lang('AD_NAME_REQUIRED');
175
		}
176
		if (truncate_string($ad_name, self::MAX_NAME_LENGTH) !== $ad_name)
177
		{
178
			$this->errors[] = $this->user->lang('AD_NAME_TOO_LONG', self::MAX_NAME_LENGTH);
179
		}
180
	}
181
182
	protected function validate_ad_end_date($end_date)
183
	{
184
		if (preg_match('#^\d{4}\-\d{2}\-\d{2}$#', $end_date))
185
		{
186
			$end_date = (int) $this->end_date_to_timestamp($end_date);
187
188
			if ($end_date < time())
189
			{
190
				$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
191
			}
192
		}
193
		else if ($end_date !== '')
194
		{
195
			$this->errors[] = $this->user->lang('AD_END_DATE_INVALID');
196
		}
197
	}
198
199
	protected function validate_ad_priority($ad_priority)
200
	{
201
		if ($ad_priority < 1 || $ad_priority > 10)
202
		{
203
			$this->errors[] = $this->user->lang('AD_PRIORITY_INVALID');
204
		}
205
	}
206
207
	protected function validate_ad_views_limit($ad_views_limit)
208
	{
209
		if ($ad_views_limit < 0)
210
		{
211
			$this->errors[] = $this->user->lang('AD_VIEWS_LIMIT_INVALID');
212
		}
213
	}
214
215
	protected function validate_ad_clicks_limit($ad_clicks_limit)
216
	{
217
		if ($ad_clicks_limit < 0)
218
		{
219
			$this->errors[] = $this->user->lang('AD_CLICKS_LIMIT_INVALID');
220
		}
221
	}
222
223
	protected function validate_ad_owner($ad_owner)
224
	{
225
		// user_get_id_name function returns false if everything is OK.
226
		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...
227
		{
228
			$this->errors[] = $this->user->lang('AD_OWNER_INVALID');
229
		}
230
	}
231
232
	protected function end_date_to_timestamp($end_date)
233
	{
234
		return (int) $this->user->get_timestamp_from_format(controller::DATE_FORMAT, $end_date);
235
	}
236
237
	protected function owner_to_id($ad_owner)
238
	{
239
		if (empty($ad_owner))
240
		{
241
			return 0;
242
		}
243
244
		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...
245
		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...
246
	}
247
248
	protected function create_storage_dir()
249
	{
250
		if (!$this->filesystem->exists($this->root_path . 'images/phpbb_ads'))
251
		{
252
			$this->filesystem->mkdir($this->root_path . 'images/phpbb_ads');
253
		}
254
	}
255
256
	protected function send_ajax_response($success, $text)
257
	{
258
		$json_response = new \phpbb\json_response;
259
		$json_response->send(array(
260
			'success'	=> $success,
261
			'title'		=> $this->user->lang('INFORMATION'),
262
			'text'		=> $text,
263
		));
264
	}
265
}
266