comments::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 32

Duplication

Lines 5
Ratio 15.63 %

Importance

Changes 0
Metric Value
dl 5
loc 32
rs 9.408
c 0
b 0
f 0
cc 3
nc 2
nop 14

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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;
12
13
use \ernadoo\phpbbdirectory\core\helper;
14
15
class comments extends helper
16
{
17
	private $captcha;
18
	private $s_comment;
19
	private $s_hidden_fields = array();
20
21
	/** @var \phpbb\db\driver\driver_interface */
22
	protected $db;
23
24
	/** @var \phpbb\config\config */
25
	protected $config;
26
27
	/** @var \phpbb\language\language */
28
	protected $language;
29
30
	/** @var \phpbb\template\template */
31
	protected $template;
32
33
	/** @var \phpbb\user */
34
	protected $user;
35
36
	/** @var \phpbb\controller\helper */
37
	protected $helper;
38
39
	/** @var \phpbb\request\request */
40
	protected $request;
41
42
	/** @var \phpbb\auth\auth */
43
	protected $auth;
44
45
	/** @var \phpbb\pagination */
46
	protected $pagination;
47
48
	/** @var \phpbb\captcha\factory */
49
	protected $captcha_factory;
50
51
	/** @var \ernadoo\phpbbdirectory\core\categorie */
52
	protected $categorie;
53
54
	/** @var \ernadoo\phpbbdirectory\core\comment */
55
	protected $comment;
56
57
	/** @var string phpBB root path */
58
	protected $root_path;
59
60
	/** @var string phpEx */
61
	protected $php_ext;
62
63
	/**
64
	* Constructor
65
	*
66
	* @param \phpbb\db\driver\driver_interface					$db					Database object
67
	* @param \phpbb\config\config								$config				Config object
68
	* @param \phpbb\language\language							$language			Language object
69
	* @param \phpbb\template\template							$template			Template object
70
	* @param \phpbb\user										$user				User object
71
	* @param \phpbb\controller\helper							$helper				Controller helper object
72
	* @param \phpbb\request\request								$request			Request object
73
	* @param \phpbb\auth\auth									$auth				Auth object
74
	* @param \phpbb\pagination									$pagination			Pagination object
75
	* @param \phpbb\captcha\factory								$captcha_factory	Captcha object
76
	* @param \ernadoo\phpbbdirectory\core\categorie				$categorie			PhpBB Directory extension categorie object
77
	* @param \ernadoo\phpbbdirectory\core\comment				$comment			PhpBB Directory extension comment object
78
	* @param string												$root_path			phpBB root path
79
	* @param string												$php_ext			phpEx
80
	*/
81
	public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\config\config $config, \phpbb\language\language $language, \phpbb\template\template $template, \phpbb\user $user, \phpbb\controller\helper $helper, \phpbb\request\request $request, \phpbb\auth\auth $auth, \phpbb\pagination $pagination, \phpbb\captcha\factory $captcha_factory, \ernadoo\phpbbdirectory\core\categorie $categorie, \ernadoo\phpbbdirectory\core\comment $comment, $root_path, $php_ext)
82
	{
83
		$this->db				= $db;
84
		$this->config			= $config;
85
		$this->language			= $language;
86
		$this->template			= $template;
87
		$this->user				= $user;
88
		$this->helper			= $helper;
89
		$this->request			= $request;
90
		$this->auth				= $auth;
91
		$this->pagination		= $pagination;
92
		$this->captcha_factory 	= $captcha_factory;
93
		$this->categorie		= $categorie;
94
		$this->comment			= $comment;
95
		$this->root_path		= $root_path;
96
		$this->php_ext			= $php_ext;
97
98
		$language->add_lang('directory', 'ernadoo/phpbbdirectory');
99
		$user->add_lang(array('ucp', 'posting'));
100
101
		$template->assign_vars(array(
102
			'S_PHPBB_DIRECTORY'	=> true,
103
			'S_SIMPLE_MESSAGE' 	=> true,
104
		));
105
106
		// The CAPTCHA kicks in here. We can't help that the information gets lost on language change.
107 View Code Duplication
		if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm'])
108
		{
109
			$this->captcha = $this->captcha_factory->get_instance($this->config['captcha_plugin']);
110
			$this->captcha->init(CONFIRM_POST);
111
		}
112
	}
113
114
	/**
115
	* Populate form when an error occurred
116
	*
117
	* @param	int		$link_id		The link ID
118
	* @param	int		$comment_id		The comment ID
119
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
120
	* @throws	\phpbb\exception\http_exception
121
	*/
122
	public function delete_comment($link_id, $comment_id)
123
	{
124
		$this->_check_comments_enable($link_id);
125
126
		if ($this->request->is_set_post('cancel'))
127
		{
128
			$redirect = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $link_id));
129
			redirect($redirect);
130
		}
131
132
		$sql = 'SELECT *
133
			FROM ' . $this->comments_table . '
134
			WHERE comment_id = ' . (int) $comment_id;
135
		$result = $this->db->sql_query($sql);
136
		$value = $this->db->sql_fetchrow($result);
137
138 View Code Duplication
		if (!$this->user->data['is_registered'] || !$this->auth->acl_get('m_delete_comment_dir') && (!$this->auth->acl_get('u_delete_comment_dir') || $this->user->data['user_id'] != $value['comment_user_id']))
139
		{
140
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
141
		}
142
143
		if (confirm_box(true))
144
		{
145
			$this->comment->del($link_id, $comment_id);
146
147
			$meta_info = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $link_id));
148
			meta_refresh(3, $meta_info);
149
			$message = $this->language->lang('DIR_COMMENT_DELETE_OK');
150
			$message = $message . '<br /><br />' . $this->language->lang('DIR_CLICK_RETURN_COMMENT', '<a href="' . $meta_info . '">', '</a>');
151
			return $this->helper->message($message);
152
		}
153
		else
154
		{
155
			confirm_box(false, 'DIR_COMMENT_DELETE');
156
		}
157
	}
158
159
	/**
160
	* Edit a comment
161
	*
162
	* @param	int		$link_id		The category ID
163
	* @param	int		$comment_id		The comment ID
164
	* @return	null|\Symfony\Component\HttpFoundation\Response	A Symfony Response object
165
	* @throws	\phpbb\exception\http_exception
166
	*/
167
	public function edit_comment($link_id, $comment_id)
168
	{
169
		$this->_check_comments_enable($link_id);
170
171
		$sql = 'SELECT *
172
			FROM ' . $this->comments_table . '
173
			WHERE comment_id = ' . (int) $comment_id;
174
		$result = $this->db->sql_query($sql);
175
		$value = $this->db->sql_fetchrow($result);
176
177 View Code Duplication
		if (!$this->user->data['is_registered'] || !$this->auth->acl_get('m_edit_comment_dir') && (!$this->auth->acl_get('u_edit_comment_dir') || $this->user->data['user_id'] != $value['comment_user_id']))
178
		{
179
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
180
		}
181
182
		$comment_text = generate_text_for_edit($value['comment_text'], $value['comment_uid'], $value['comment_flags']);
183
		$this->s_comment = $comment_text['text'];
184
185
		$submit	= $this->request->is_set_post('update_comment') ? true : false;
186
187
		// If form is done
188
		if ($submit)
189
		{
190
			return $this->_data_processing($link_id, $comment_id, 'edit');
191
		}
192
193
		return $this->view($link_id, 1, 'edit');
194
	}
195
196
	/**
197
	* Post a new comment
198
	*
199
	* @param	int	$link_id		The category ID
200
	* @return	null
201
	* @throws	\phpbb\exception\http_exception
202
	*/
203
	public function new_comment($link_id)
204
	{
205
		$this->_check_comments_enable($link_id);
206
207
		if (!$this->auth->acl_get('u_comment_dir'))
208
		{
209
			throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
210
		}
211
212
		$submit		= $this->request->is_set_post('submit_comment') ? true : false;
213
		$refresh	= $this->request->is_set_post('refresh_vc') ? true : false;
214
215
		// If form is done
216
		if ($submit || $refresh)
217
		{
218
			return $this->_data_processing($link_id);
219
		}
220
		else
221
		{
222
			$redirect = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $link_id));
223
			redirect($redirect);
224
		}
225
	}
226
227
	/**
228
	* Display popup comment
229
	*
230
	* @param	int		$link_id		The category ID
231
	* @param	int		$page			Page number taken from the URL
232
	* @param	string	$mode			add|edit
233
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
234
	* @throws	\phpbb\exception\http_exception
235
	*/
236
	public function view($link_id, $page, $mode = 'new')
237
	{
238
		$this->_check_comments_enable($link_id);
239
240
		$comment_id = $this->request->variable('c', 0);
241
		$view 		= $this->request->variable('view', '');
242
		$start 		= ($page - 1) * $this->config['dir_comments_per_page'];
243
244
		$this->s_hidden_fields = array_merge($this->s_hidden_fields, array('page' => $page));
245
246
		$this->_populate_form($link_id, $mode);
247
248
		$sql = 'SELECT COUNT(comment_id) AS nb_comments
249
			FROM ' . $this->comments_table . '
250
			WHERE comment_link_id = ' . (int) $link_id;
251
		$result = $this->db->sql_query($sql);
252
		$nb_comments = (int) $this->db->sql_fetchfield('nb_comments');
253
		$this->db->sql_freeresult($result);
254
255
		// Make sure $start is set to the last page if it exceeds the amount
256
		$start = $this->pagination->validate_start($start, $this->config['dir_comments_per_page'], $nb_comments);
257
258
		$sql_array = array(
259
			'SELECT'	=> 'a.comment_id, a.comment_user_id, a. comment_user_ip, a.comment_date, a.comment_text, a.comment_uid, a.comment_bitfield, a.comment_flags, u.username, u.user_id, u.user_colour, z.foe',
260
			'FROM'		=> array(
261
					$this->comments_table	=> 'a'),
262
			'LEFT_JOIN'	=> array(
263
					array(
264
						'FROM'	=> array(USERS_TABLE => 'u'),
265
						'ON'	=> 'a.comment_user_id = u.user_id'
266
					),
267
					array(
268
						'FROM'	=> array(ZEBRA_TABLE => 'z'),
269
						'ON'	=> 'z.user_id = ' . $this->user->data['user_id'] . ' AND z.zebra_id = a.comment_user_id'
270
					)
271
			),
272
			'WHERE'		=> 'a.comment_link_id = ' . (int) $link_id,
273
			'ORDER_BY'	=> 'a.comment_date DESC');
274
		$sql = $this->db->sql_build_query('SELECT', $sql_array);
275
		$result = $this->db->sql_query_limit($sql, $this->config['dir_comments_per_page'], $start);
276
277
		$have_result = false;
278
279
		while ($comments = $this->db->sql_fetchrow($result))
280
		{
281
			$have_result = true;
282
283
			$edit_allowed = ($this->user->data['is_registered'] && ($this->auth->acl_get('m_edit_comment_dir') || (
284
				$this->user->data['user_id'] == $comments['comment_user_id'] &&
285
				$this->auth->acl_get('u_edit_comment_dir')
286
			)));
287
288
			$delete_allowed = ($this->user->data['is_registered'] && ($this->auth->acl_get('m_delete_comment_dir') || (
289
				$this->user->data['user_id'] == $comments['comment_user_id'] &&
290
				$this->auth->acl_get('u_delete_comment_dir')
291
			)));
292
293
			$this->template->assign_block_vars('comment', array(
294
				'MINI_POST_IMG'		=> $this->user->img('icon_post_target', 'POST'),
295
				'S_USER'			=> get_username_string('full', $comments['comment_user_id'], $comments['username'], $comments['user_colour']),
296
				'S_USER_IP'			=> $comments['comment_user_ip'],
297
				'S_DATE'			=> $this->user->format_date($comments['comment_date']),
298
				'S_COMMENT'			=> generate_text_for_display($comments['comment_text'], $comments['comment_uid'], $comments['comment_bitfield'], $comments['comment_flags']),
299
				'S_ID'				=> $comments['comment_id'],
300
301
				'U_EDIT'			=> ($edit_allowed) 		? $this->helper->route('ernadoo_phpbbdirectory_comment_edit_controller', array('link_id' => (int) $link_id, 'comment_id' => (int) $comments['comment_id'])) : '',
302
				'U_DELETE'			=> ($delete_allowed) 	? $this->helper->route('ernadoo_phpbbdirectory_comment_delete_controller', array('link_id' => (int) $link_id, 'comment_id' => (int) $comments['comment_id'], '_referer' => $this->helper->get_current_url())) : '',
303
304
				'S_IGNORE_POST'		=> ($comments['foe'] && ($view != 'show' || $comment_id != $comments['comment_id'])) ? true : false,
305
				'L_IGNORE_POST'		=> ($comments['foe']) ? $this->language->lang('POST_BY_FOE', get_username_string('full', $comments['comment_user_id'], $comments['username'], $comments['user_colour']), '<a href="'.$this->helper->url('directory/link/'.$link_id.'/comment'.(($page > 1) ? '/'.$page : '').'?view=show#c'.(int) $comments['comment_id']).'">', '</a>') : '',
306
				'L_POST_DISPLAY'	=> ($comments['foe']) ? $this->language->lang('POST_DISPLAY', '<a class="display_post" data-post-id="' . $comments['comment_id'] . '" href="' . $this->helper->url('directory/link/'.$link_id.'/comment'.(($page > 1) ? '/'.$page : '').'?c='.(int) $comments['comment_id'] . '&view=show#c'.(int) $comments['comment_id']).'">', '</a>') : '',
307
308
				'S_INFO'			=> $this->auth->acl_get('m_info'),
309
			));
310
		}
311
312
		$base_url = array(
313
			'routes'	=> 'ernadoo_phpbbdirectory_comment_view_controller',
314
			'params'	=> array('link_id' => (int) $link_id),
315
		);
316
317
		$this->pagination->generate_template_pagination($base_url, 'pagination', 'page', $nb_comments, $this->config['dir_comments_per_page'], $start);
318
319
		$this->template->assign_vars(array(
320
			'TOTAL_COMMENTS'	=> $this->language->lang('DIR_NB_COMMS', (int) $nb_comments),
321
			'S_HAVE_RESULT'		=> $have_result ? true : false,
322
		));
323
324
		return $this->helper->render('comments.html', $this->language->lang('DIR_COMMENT_TITLE'));
325
	}
326
327
	/**
328
	* Routine
329
	*
330
	* @param	int		$link_id		The link ID
331
	* @param	int		$comment_id		The comment ID
332
	* @param	string	$mode			new|edit
333
	* @return	\Symfony\Component\HttpFoundation\Response	A Symfony Response object
334
	*/
335
	private function _data_processing($link_id, $comment_id = 0, $mode = 'new')
336
	{
337
		if (!check_form_key('dir_form_comment'))
338
		{
339
			return $this->helper->message('FORM_INVALID');
340
		}
341
342
		$this->s_comment = $this->request->variable('message', '', true);
343
344
		if (!function_exists('validate_data'))
345
		{
346
			include($this->root_path . 'includes/functions_user.' . $this->php_ext);
347
		}
348
349
		$error = validate_data(
350
			array(
351
				'reply' => $this->s_comment),
352
			array(
353
				'reply' => array(
354
					array('string', false, 1, $this->config['dir_length_comments'])
355
				)
356
			)
357
		);
358
359
		$error = array_map(array($this->language, 'lang'), $error);
360
361 View Code Duplication
		if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm'])
362
		{
363
			$vc_response = $this->captcha->validate();
364
			if ($vc_response !== false)
365
			{
366
				$error[] = $vc_response;
367
			}
368
369
			if ($this->config['dir_visual_confirm_max_attempts'] && $this->captcha->get_attempt_count() > $this->config['dir_visual_confirm_max_attempts'])
370
			{
371
				$error[] = $this->language->lang('TOO_MANY_ADDS');
372
			}
373
		}
374
375
		if (!$error)
376
		{
377
			$uid = $bitfield = $flags = '';
378
			generate_text_for_storage($this->s_comment, $uid, $bitfield, $flags, (bool) $this->config['dir_allow_bbcode'], (bool) $this->config['dir_allow_links'], (bool) $this->config['dir_allow_smilies'], (bool) $this->config['dir_allow_bbcode'], ($this->config['dir_allow_bbcode'] && $this->config['dir_allow_flash']), true, (bool) $this->config['dir_allow_links']);
379
380
			$data_edit = array(
381
				'comment_text'		=> $this->s_comment,
382
				'comment_uid'		=> $uid,
383
				'comment_flags'		=> $flags,
384
				'comment_bitfield'	=> $bitfield,
385
			);
386
387
			if ($mode == 'edit')
388
			{
389
				$this->comment->edit($data_edit, $comment_id);
390
			}
391
			else
392
			{
393
				$data_add = array(
394
					'comment_link_id'	=> (int) $link_id,
395
					'comment_date'		=> time(),
396
					'comment_user_id'	=> $this->user->data['user_id'],
397
					'comment_user_ip'	=> $this->user->ip,
398
				);
399
400
				$data_add = array_merge($data_edit, $data_add);
401
402
				$this->comment->add($data_add);
403
			}
404
405
			$meta_info = $this->helper->route('ernadoo_phpbbdirectory_comment_view_controller', array('link_id' => (int) $link_id));
406
			meta_refresh(3, $meta_info);
407
			$message = $this->language->lang('DIR_'.strtoupper($mode).'_COMMENT_OK');
408
			$message = $message . '<br /><br />' . $this->language->lang('DIR_CLICK_RETURN_COMMENT', '<a href="' . $meta_info . '">', '</a>');
409
			return $this->helper->message($message);
410
		}
411
		else
412
		{
413
			$this->template->assign_vars(array(
414
				'ERROR'	=> (count($error)) ? implode('<br />', $error) : ''
415
			));
416
417
			return $this->view($link_id, $this->request->variable('page', 1), $mode);
418
		}
419
	}
420
421
	/**
422
	* Check if comments are enable in a category
423
	*
424
	* @param	int		$link_id		The link ID
425
	* @return	null					Retun null if comments are allowed, http_exception if not
426
	* @throws	\phpbb\exception\http_exception
427
	*/
428
	private function _check_comments_enable($link_id)
429
	{
430
		$sql = 'SELECT link_cat
431
			FROM ' . $this->links_table . '
432
			WHERE link_id = ' . (int) $link_id;
433
		$result = $this->db->sql_query($sql);
434
		$cat_id = (int) $this->db->sql_fetchfield('link_cat');
435
		$this->db->sql_freeresult($result);
436
437
		if ($cat_id)
438
		{
439
			$this->categorie->get($cat_id);
440
441
			if (!$this->categorie->data['cat_allow_comments'])
442
			{
443
				throw new \phpbb\exception\http_exception(403, 'DIR_ERROR_NOT_AUTH');
444
			}
445
		}
446
		else
447
		{
448
			throw new \phpbb\exception\http_exception(404, 'DIR_ERROR_NO_LINKS');
449
		}
450
	}
451
452
	/**
453
	* Populate form when an error occurred
454
	*
455
	* @param	int		$link_id		The link ID
456
	* @param	string	$mode			add|edit
457
	* @return	null
458
	*/
459
	private function _populate_form($link_id, $mode)
460
	{
461 View Code Duplication
		if (!$this->user->data['is_registered'] && $this->config['dir_visual_confirm'] && $mode != 'edit')
462
		{
463
			$this->s_hidden_fields = array_merge($this->s_hidden_fields, $this->captcha->get_hidden_fields());
464
465
			$this->template->assign_vars(array(
466
				'S_CONFIRM_CODE'		=> true,
467
				'CAPTCHA_TEMPLATE'		=> $this->captcha->get_template(),
468
			));
469
		}
470
471
		if (!function_exists('generate_smilies'))
472
		{
473
			include($this->root_path . 'includes/functions_posting.' . $this->php_ext);
474
		}
475
		if (!function_exists('display_custom_bbcodes'))
476
		{
477
			include($this->root_path . 'includes/functions_display.' . $this->php_ext);
478
		}
479
480
		generate_smilies('inline', 0);
481
		display_custom_bbcodes();
482
		add_form_key('dir_form_comment');
483
484
		$this->template->assign_vars(array(
485
			'S_AUTH_COMM' 		=> $this->auth->acl_get('u_comment_dir'),
486
487
			'BBCODE_STATUS'		=> ($this->config['dir_allow_bbcode']) 	? $this->language->lang('BBCODE_IS_ON', '<a href="' . append_sid($this->root_path."faq.$this->php_ext", 'mode=bbcode') . '">', '</a>') : $this->language->lang('BBCODE_IS_OFF', '<a href="' . append_sid($this->root_path."faq.$this->php_ext", 'mode=bbcode') . '">', '</a>'),
488
			'IMG_STATUS'		=> ($this->config['dir_allow_bbcode']) 	? $this->language->lang('IMAGES_ARE_ON') : $this->language->lang('IMAGES_ARE_OFF'),
489
			'SMILIES_STATUS'	=> ($this->config['dir_allow_smilies'])	? $this->language->lang('SMILIES_ARE_ON') : $this->language->lang('SMILIES_ARE_OFF'),
490
			'URL_STATUS'		=> ($this->config['dir_allow_links'])	? $this->language->lang('URL_IS_ON') : $this->language->lang('URL_IS_OFF'),
491
			'FLASH_STATUS'		=> ($this->config['dir_allow_bbcode'] && $this->config['dir_allow_flash'])	? $this->language->lang('FLASH_IS_ON') : $this->language->lang('FLASH_IS_OFF'),
492
493
			'L_DIR_REPLY_EXP'	=> $this->language->lang('DIR_REPLY_EXP', $this->config['dir_length_comments']),
494
495
			'S_COMMENT' 		=> isset($this->s_comment) ? $this->s_comment : '',
496
497
			'S_BBCODE_ALLOWED' 	=> (bool) $this->config['dir_allow_bbcode'],
498
			'S_BBCODE_IMG'		=> (bool) $this->config['dir_allow_bbcode'],
499
			'S_BBCODE_FLASH'	=> ($this->config['dir_allow_bbcode'] && $this->config['dir_allow_flash']) ? true : false,
500
			'S_BBCODE_QUOTE'	=> true,
501
			'S_LINKS_ALLOWED'	=> (bool) $this->config['dir_allow_links'],
502
			'S_SMILIES_ALLOWED' => (bool) $this->config['dir_allow_smilies'],
503
504
			'S_HIDDEN_FIELDS'	=> build_hidden_fields($this->s_hidden_fields),
505
			'S_BUTTON_NAME'		=> ($mode == 'edit') ? 'update_comment' : 'submit_comment',
506
			'S_POST_ACTION' 	=> ($mode == 'edit') ? '' : $this->helper->route('ernadoo_phpbbdirectory_comment_new_controller', array('link_id' => (int) $link_id)),
507
		));
508
	}
509
}
510