Completed
Pull Request — master (#54)
by Jakub
05:19
created

admin_input::create_storage_dir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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