validation   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 414
Duplicated Lines 8.45 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 3
dl 35
loc 414
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 18 18 1
A display_confirm() 0 9 1
C display_websites() 8 101 8
A exec_action() 0 20 3
A notifiy_submiters() 0 41 4
A set_page_url() 0 4 1
A _action_approved() 0 26 3
A _action_disapproved() 9 18 5
A _get_infos_links() 0 32 3
A _notify_suscribers() 0 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
*
4
* phpBB Directory extension for the phpBB Forum Software package.
5
*
6
* @copyright (c) 2014 ErnadoO <http://www.phpbb-services.com>
7
* @license GNU General Public License, version 2 (GPL-2.0)
8
*
9
*/
10
11
namespace ernadoo\phpbbdirectory\controller\acp;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class validation extends helper
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\db\driver\driver_interface */
21
	protected $db;
22
23
	/** @var \phpbb\pagination */
24
	protected $pagination;
25
26
	/** @var \phpbb\log\log */
27
	protected $phpbb_log;
28
29
	/** @var \phpbb\notification\manager */
30
	protected $notification;
31
32
	/** @var \phpbb\request\request */
33
	protected $request;
34
35
	/** @var \phpbb\template\template */
36
	protected $template;
37
38
	/** @var \phpbb\user */
39
	protected $user;
40
41
	/** @var \ernadoo\phpbbdirectory\core\categorie */
42
	protected $categorie;
43
44
	/** @var \ernadoo\phpbbdirectory\core\link */
45
	protected $link;
46
47
	/** @var string phpBB root path */
48
	protected $root_path;
49
50
	/** @var string phpEx */
51
	protected $php_ext;
52
53
	/** @var string Custom form action */
54
	protected $u_action;
55
56
	/** @var string */
57
	private $action;
58
59
	/** @var array */
60
	private $affected_link_name = array();
61
62
	/** @var array */
63
	private $cat_data = array();
64
65
	/** @var array */
66
	private $links_data;
67
68
	/**
69
	* Constructor
70
	*
71
	* @param \phpbb\config\config								$config				Config object
72
	* @param \phpbb\db\driver\driver_interface 					$db					Database object
73
	* @param \phpbb\pagination									$pagination			Pagination object
74
	* @param \phpbb\language\language							$language			Language object
75
	* @param \phpbb\log\log										$log				Log object
76
	* @param \phpbb\notification\manager						$notification		Notification object
77
	* @param \phpbb\request\request								$request			Request object
78
	* @param \phpbb\template\template							$template			Template object
79
	* @param \phpbb\user										$user				User object
80
	* @param \ernadoo\phpbbdirectory\core\categorie				$categorie			PhpBB Directory extension categorie object
81
	* @param \ernadoo\phpbbdirectory\core\link					$link				PhpBB Directory extension link object
82
	* @param string												$root_path			phpBB root path
83
	* @param string												$php_ext   			phpEx
84
	*/
85 View Code Duplication
	public function __construct(\phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\pagination $pagination, \phpbb\language\language $language, \phpbb\log\log $log, \phpbb\notification\manager $notification, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\link $link, $root_path, $php_ext)
86
	{
87
		$this->config		= $config;
88
		$this->db			= $db;
89
		$this->pagination	= $pagination;
90
		$this->language		= $language;
91
		$this->phpbb_log	= $log;
92
		$this->notification	= $notification;
93
		$this->request		= $request;
94
		$this->template		= $template;
95
		$this->user			= $user;
96
		$this->categorie	= $categorie;
97
		$this->link			= $link;
98
		$this->root_path	= $root_path;
99
		$this->php_ext		= $php_ext;
100
101
		$this->action		= $this->request->variable('action', '');
102
	}
103
104
	/**
105
	* Display confirm box
106
	*
107
	* @param	array $mark Website selected for (dis)approval
108
	* @return	null
109
	*/
110
	public function display_confirm($mark)
111
	{
112
		$s_hidden_fields = array(
113
			'action'		=> $this->action,
114
			'link_id'		=> $mark,
115
			'start'			=> $this->request->variable('start', 0),
116
		);
117
		confirm_box(false, $this->language->lang('CONFIRM_OPERATION'), build_hidden_fields($s_hidden_fields));
118
	}
119
120
	/**
121
	* Display website list for (dis)approval
122
	*
123
	* @return null
124
	*/
125
	public function display_websites()
126
	{
127
		global $phpbb_admin_path;
128
129
		// Sort keys
130
		$sort_days	= $this->request->variable('st', 0);
131
		$sort_key	= $this->request->variable('sk', 't');
132
		$sort_dir	= $this->request->variable('sd', 'd');
133
134
		// Number of entries to display
135
		$per_page = $this->request->variable('links_per_page', (int) $this->config['dir_show']);
136
137
		$start	= $this->request->variable('start', 0);
138
139
		// Categorie ordering options
140
		$limit_days		= array(0 => $this->language->lang('SEE_ALL'), 1 => $this->language->lang('1_DAY'), 7 => $this->language->lang('7_DAYS'), 14 => $this->language->lang('2_WEEKS'), 30 => $this->language->lang('1_MONTH'), 90 => $this->language->lang('3_MONTHS'), 180 => $this->language->lang('6_MONTHS'), 365 => $this->language->lang('1_YEAR'));
141
		$sort_by_text	= array('a' => $this->language->lang('AUTHOR'), 't' => $this->language->lang('POST_TIME'));
142
		$sort_by_sql	= array('a' => 'u.username_clean', 't' => array('l.link_time', 'l.link_id'));
143
144
		$s_limit_days = $s_sort_key = $s_sort_dir = $u_sort_param = '';
145
		gen_sort_selects($limit_days, $sort_by_text, $sort_days, $sort_key, $sort_dir, $s_limit_days, $s_sort_key, $s_sort_dir, $u_sort_param);
146
147
		// Define where and sort sql for use in displaying logs
148
		$sql_where = ($sort_days) ? (time() - ($sort_days * 86400)) : 0;
149
		$direction = (($sort_dir == 'd') ? 'DESC' : 'ASC');
150
151 View Code Duplication
		if (is_array($sort_by_sql[$sort_key]))
152
		{
153
			$sql_sort_order = implode(' ' . $direction . ', ', $sort_by_sql[$sort_key]) . ' ' . $direction;
154
		}
155
		else
156
		{
157
			$sql_sort_order = $sort_by_sql[$sort_key] . ' ' . $direction;
158
		}
159
160
		$sql = 'SELECT COUNT(1) AS total_links
161
			FROM ' . $this->links_table . '
162
			WHERE link_active = 0' .
163
				(($sql_where) ? " AND link_time >= $sql_where" : '');
164
		$result = $this->db->sql_query($sql);
165
		$total_links = (int) $this->db->sql_fetchfield('total_links');
166
		$this->db->sql_freeresult($result);
167
168
		// Make sure $start is set to the last page if it exceeds the amount
169
		$start = $this->pagination->validate_start($start, $per_page, $total_links);
170
171
		$sql_array = array(
172
			'SELECT'	=> 'l.link_id, l.link_name, l.link_url, l.link_description, l.link_cat, l.link_user_id, l.link_guest_email, l.link_uid, l.link_bitfield, l.link_flags, l.link_banner, l.link_time, c.cat_name, u.user_id, u.username, u.user_colour',
173
			'FROM'		=> array(
174
				$this->links_table	=> 'l'),
175
			'LEFT_JOIN'	=> array(
176
				array(
177
					'FROM'	=> array($this->categories_table => 'c'),
178
					'ON'	=> 'c.cat_id = l.link_cat'
179
				),
180
				array(
181
					'FROM'	=> array(USERS_TABLE => 'u'),
182
					'ON'	=> 'u.user_id = l.link_user_id'
183
				)
184
			),
185
			'WHERE'		=> 'l.link_active = 0' . (($sql_where) ? " AND l.link_time >= $sql_where" : ''),
186
			'ORDER_BY'	=> $sql_sort_order);
187
188
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
189
		$result = $this->db->sql_query_limit($sql, $per_page, $start);
190
191
		while ($row = $this->db->sql_fetchrow($result))
192
		{
193
			$s_banner = $this->link->display_bann($row);
194
195
			$username = ($row['link_user_id'] == ANONYMOUS) ? $row['link_guest_email'] : $row['username'];
196
197
			$link_row = array(
198
				'LINK_URL'			=> $row['link_url'],
199
				'LINK_NAME'			=> $row['link_name'],
200
				'LINK_DESC'			=> generate_text_for_display($row['link_description'], $row['link_uid'], $row['link_bitfield'], $row['link_flags']),
201
				'L_DIR_USER_PROP'	=> $this->language->lang('DIR_USER_PROP', get_username_string('full', $row['link_user_id'], $username, $row['user_colour'], false, append_sid("{$phpbb_admin_path}index.$this->php_ext", 'i=users&amp;mode=overview')), '<select name=c'.$row['link_id'].'>'.$this->categorie->make_cat_select((int) $row['link_cat']).'</select>', $this->user->format_date($row['link_time'])),
202
				'BANNER'			=> $s_banner,
203
				'LINK_ID'			=> $row['link_id'],
204
205
			);
206
			$this->template->assign_block_vars('linkrow', $link_row);
207
		}
208
		$this->db->sql_freeresult($result);
209
210
		$option_ary = array('approved' => 'DIR_LINK_ACTIVATE', 'disapproved' => 'DIR_LINK_DELETE');
211
212
		$base_url = $this->u_action . "&amp;$u_sort_param&amp;links_per_page=$per_page";
213
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'start', $total_links, $per_page, $start);
214
215
		$this->template->assign_vars(array(
216
			'S_LINKS_OPTIONS'	=> build_select($option_ary),
217
218
			'S_LIMIT_DAYS'		=> $s_limit_days,
219
			'S_SORT_KEY'		=> $s_sort_key,
220
			'S_SORT_DIR'		=> $s_sort_dir,
221
			'LINKS_PER_PAGE'	=> $per_page,
222
223
			'U_ACTION'			=> $this->u_action . "&amp;$u_sort_param&amp;links_per_page=$per_page&amp;start=$start",
224
		));
225
	}
226
227
	/**
228
	* Get link's information and call appropriate action
229
	*
230
	* @param	array $mark Website selected for (dis)approval
231
	* @return	null
232
	*/
233
	public function exec_action($mark)
234
	{
235
		$this->_get_infos_links($mark);
236
237
		switch ($this->action)
238
		{
239
			case 'approved':
240
				$this->_action_approved();
241
				break;
242
243
			case 'disapproved':
244
				$this->_action_disapproved();
245
				break;
246
247
			default:
248
				return;
249
		}
250
251
		$this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_LINK_' . strtoupper($this->action), time(), array(implode(', ', $this->affected_link_name)));
252
	}
253
254
	/**
255
	* Notify users which had submitted their websites
256
	*
257
	* @return null
258
	*/
259
	public function notifiy_submiters()
260
	{
261
		if (!class_exists('messenger'))
262
		{
263
			include($this->root_path . 'includes/functions_messenger.' . $this->php_ext);
264
		}
265
		$messenger = new \messenger(false);
266
267
		foreach ($this->links_data as $row)
268
		{
269
			$this->notification->mark_notifications('ernadoo.phpbbdirectory.notification.type.directory_website_in_queue', (int) $row['link_id'], false);
270
271
			// New notification system can't send mail to an anonymous user with an email adress storage in another table than phpbb_users
272
			if ($row['link_user_id'] == ANONYMOUS)
273
			{
274
				$username = $email = $row['link_guest_email'];
275
276
				$messenger->template('@ernadoo_phpbbdirectory/directory_website_'.$this->action, $row['user_lang']);
277
				$messenger->to($email, $username);
278
279
				$messenger->assign_vars(array(
280
					'USERNAME'	=> htmlspecialchars_decode($username),
281
					'LINK_NAME'	=> $row['link_name'],
282
				));
283
284
				$messenger->send(NOTIFY_EMAIL);
285
			}
286
			else
287
			{
288
				$notification_data = array(
289
					'user_from'			=> (int) $row['link_user_id'],
290
					'link_id'			=> (int) $row['link_id'],
291
					'link_name'			=> $row['link_name'],
292
					'cat_name'			=> \ernadoo\phpbbdirectory\core\categorie::getname((int) $row['link_cat']),
293
					'cat_id'			=> (int) $row['link_cat'],
294
				);
295
296
				$this->notification->add_notifications('ernadoo.phpbbdirectory.notification.type.directory_website_'.$this->action, $notification_data);
297
			}
298
		}
299
	}
300
301
	/**
302
	* Set page url
303
	*
304
	* @param	string $u_action Custom form action
305
	* @return	null
306
	* @access	public
307
	*/
308
	public function set_page_url($u_action)
309
	{
310
		$this->u_action = $u_action;
311
	}
312
313
	/**
314
	* Approve action
315
	*
316
	* @return null
317
	*/
318
	private function _action_approved()
319
	{
320
		foreach ($this->links_data as $row)
321
		{
322
			$this->_notify_suscribers($row);
323
324
			$sql_ary = array(
325
				'link_active'	=> 1,
326
				'link_time'		=> time(),
327
				'link_cat'		=> (int) $row['link_cat'],
328
			);
329
330
			$sql = 'UPDATE ' . $this->links_table . '
331
							SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . '
332
							WHERE link_id = ' . (int) $row['link_id'];
333
			$this->db->sql_query($sql);
334
		}
335
336
		foreach ($this->cat_data as $cat_id => $count)
337
		{
338
			$sql = 'UPDATE ' . $this->categories_table . '
339
							SET cat_links = cat_links + '.$count.'
340
							WHERE cat_id = ' . (int) $cat_id;
341
			$this->db->sql_query($sql);
342
		}
343
	}
344
345
	/**
346
	* Disapprove action
347
	*
348
	* @return null
349
	*/
350
	private function _action_disapproved()
351
	{
352
		foreach ($this->links_data as $row)
353
		{
354 View Code Duplication
			if ($row['link_banner'] && !preg_match('/^(http:\/\/|https:\/\/|ftp:\/\/|ftps:\/\/|www\.).+/si', $row['link_banner']))
355
			{
356
				$banner_img = $this->get_banner_path(basename($row['link_banner']));
357
358
				if (file_exists($banner_img))
359
				{
360
					@unlink($banner_img);
361
				}
362
			}
363
364
			$sql = 'DELETE FROM ' . $this->links_table . ' WHERE link_id = ' . (int) $row['link_id'];
365
			$this->db->sql_query($sql);
366
		}
367
	}
368
369
	/**
370
	* Get informations about links selected
371
	*
372
	* @param	$mark Website selected for (dis)approval
373
	* @return	null
374
	*/
375
	private function _get_infos_links($mark)
376
	{
377
		$sql_array = array(
378
			'SELECT'	=> 'a.link_id, a.link_name, a.link_url, a.link_description, a.link_banner, a.link_user_id, a.link_guest_email, u.username, u.user_email, u.user_lang, u.user_notify_type, c.cat_id, c.cat_name',
379
			'FROM'		=> array(
380
				$this->links_table	=> 'a'),
381
			'LEFT_JOIN'	=> array(
382
				array(
383
					'FROM'	=> array(USERS_TABLE => 'u'),
384
					'ON'	=> 'u.user_id = a.link_user_id'
385
				),
386
				array(
387
					'FROM'	=> array($this->categories_table => 'c'),
388
					'ON'	=> 'a.link_cat = c.cat_id'
389
				)
390
			),
391
			'WHERE'		=> $this->db->sql_in_set('a.link_id', $mark));
392
393
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
394
		$result = $this->db->sql_query($sql);
395
396
		while ($row = $this->db->sql_fetchrow($result))
397
		{
398
			$row['link_cat'] = $this->request->variable('c'.$row['link_id'], (int) $row['cat_id']);
399
400
			$this->links_data[] = $row;
401
402
			$this->affected_link_name[] = $row['link_name'];
403
404
			$this->cat_data[$row['link_cat']] = isset($this->cat_data[$row['link_cat']]) ? $this->cat_data[$row['link_cat']] + 1 : 1;
405
		}
406
	}
407
408
	/**
409
	* Notify users which watch categories
410
	*
411
	* @param	$row Informations about website
412
	* @return	null
413
	*/
414
	private function _notify_suscribers($row)
415
	{
416
		$notification_data = array(
417
			'user_from'			=> (int) $row['link_user_id'],
418
			'link_id'			=> (int) $row['link_id'],
419
			'link_name'			=> $row['link_name'],
420
			'link_url'			=> $row['link_url'],
421
			'link_description'	=> preg_replace('/(\[.*?\])(.*?)(\[\/.*?\])/si', '\\1', $row['link_description']),
422
			'cat_name'			=> \ernadoo\phpbbdirectory\core\categorie::getname((int) $row['link_cat']),
423
			'cat_id'			=> (int) $row['link_cat'],
424
		);
425
426
		$this->notification->add_notifications('ernadoo.phpbbdirectory.notification.type.directory_website', $notification_data);
427
	}
428
}
429