Completed
Pull Request — master (#54)
by Jakub
09:47
created

admin_input::create_storage_dir()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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