Completed
Push — #12 ( cef8fd...5132ea )
by Erwan
02:09
created

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 *
4
 * @package Quick Title Edition Extension
5
 * @copyright (c) 2015 ABDev
6
 * @copyright (c) 2015 PastisD
7
 * @copyright (c) 2015 Geolim4 <http://geolim4.com>
8
 * @copyright (c) 2015 Zoddo <[email protected]>
9
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
10
 *
11
 */
12
13
namespace ernadoo\qte;
14
15
class qte
16
{
17
	const KEEP = -2;
18
	const REMOVE = -1;
19
20
	/** @var \phpbb\request\request */
21
	protected $request;
22
23
	/** @var \phpbb\cache\driver\driver_interface */
24
	protected $cache;
25
26
	/** @var \phpbb\config\config */
27
	protected $config;
28
29
	/** @var \phpbb\db\driver\driver_interface */
30
	protected $db;
31
32
	/** @var \phpbb\template\template */
33
	protected $template;
34
35
	/** @var \phpbb\user */
36
	protected $user;
37
38
	/** @var \phpbb\log\log */
39
	protected $log;
40
41
	/** @var string */
42
	protected $root_path;
43
44
	/** @var string */
45
	protected $php_ext;
46
47
	/** @var string */
48
	protected $table_prefix;
49
50
	/** @var array */
51
	private $_attr = array();
52
53
	/** @var array */
54
	private $_name = array();
55
56
	/**
57
	* Constructor
58
	*
59
	* @param \phpbb\request\request					$request			Request object
60
	* @param \phpbb\cache\driver\driver_interface	$cache				Cache object
61
	* @param \phpbb\config\config					$config				Config object
62
	* @param \phpbb\db\driver\driver_interface 		$db					Database object
63
	* @param \phpbb\template\template				$template			Template object
64
	* @param \phpbb\user							$user				User object
65
	* @param \phpbb\log\log							$log				Log object
66
	* @param string									$root_path			phpBB root path
67
	* @param string									$php_ext   			phpEx
68
	* @param string									$table_prefix   	Prefix tables
69
	*/
70
	public function __construct(\phpbb\request\request $request, \phpbb\cache\driver\driver_interface $cache, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\log\log $log, $root_path, $php_ext, $table_prefix)
71
	{
72
		$this->request		= $request;
73
		$this->cache		= $cache;
74
		$this->config		= $config;
75
		$this->db			= $db;
76
		$this->template		= $template;
77
		$this->user			= $user;
78
		$this->log			= $log;
79
80
		$this->root_path	= $root_path;
81
		$this->php_ext		= $php_ext;
82
		$this->table_prefix = $table_prefix;
83
84
		$this->_get_attributes();
85
		$this->user->add_lang_ext('ernadoo/qte', 'attributes');
86
	}
87
88
	/**
89
	* Get topic attributes username
90
	*
91
	* @param	array	$topic_list	Topic ids
92
	*
93
	* @return	null
94
	*/
95
	public function get_users_by_topic_id($topic_list)
96
	{
97
		if (!empty($topic_list))
98
		{
99
			$sql = 'SELECT u.user_id, u.username, u.user_colour
100
				FROM ' . USERS_TABLE . ' u
101
				LEFT JOIN ' . TOPICS_TABLE . ' t ON (u.user_id = t.topic_attr_user)
102
				WHERE ' . $this->db->sql_in_set('t.topic_id', array_map('intval', $topic_list)) . '
103
					AND t.topic_attr_user <> ' . ANONYMOUS;
104
			$result = $this->db->sql_query($sql);
105
106 View Code Duplication
			while ($row = $this->db->sql_fetchrow($result))
107
			{
108
				$this->_name[$row['user_id']] = array(
109
					'user_id' => (int) $row['user_id'],
110
					'username' => $row['username'],
111
					'user_colour' => $row['user_colour'],
112
				);
113
			}
114
			$this->db->sql_freeresult();
115
		}
116
	}
117
118
	/**
119
	* Get attribute name
120
	*
121
	* @param	int		$attr_id	The attribute id
122
	*
123
	* @return	string
124
	*/
125
	public function get_attr_name_by_id($attr_id)
126
	{
127
		return $this->_attr[$attr_id]['attr_name'];
128
	}
129
130
	/**
131
	* Get attribute author
132
	*
133
	* @param	int		$user_id	User id
134
	*
135
	* @return	string
136
	*/
137
	public function get_users_by_user_id($user_id)
138
	{
139
		if (!isset($this->_name[$user_id]))
140
		{
141
			$sql = 'SELECT user_id, username, user_colour
142
			FROM ' . USERS_TABLE . '
143
			WHERE user_id = ' . (int) $user_id;
144
			$result = $this->db->sql_query($sql);
145
146
			$this->_name = array();
147 View Code Duplication
			while ( $row = $this->db->sql_fetchrow($result) )
148
			{
149
				$this->_name[$row['user_id']] = array(
150
					'user_id'		=> (int) $row['user_id'],
151
					'username'		=> $row['username'],
152
					'user_colour'	=> $row['user_colour'],
153
				);
154
			}
155
			$this->db->sql_freeresult();
156
		}
157
	}
158
159
	/**
160
	* Generate a list of attributes based on permissions
161
	*
162
	* @param	int		$forum_id		Forum id
163
	* @param	int		$author_id		Topic author id
164
	* @param	int		$attribute_id	Current attribute id
165
	* @param	array	$hide_attr		Groups which can't delete attribute in this forum
166
	* @param	string	$viewtopic_url	Topic's url
167
	*
168
	* @return	null
169
	*/
170
	public function attr_select($forum_id = 0, $author_id = 0, $attribute_id = 0, $hide_attr = array(), $viewtopic_url = '')
171
	{
172
		// get current time once !
173
		$current_time = time();
174
175
		$show_select = false;
176
		$user_groups = array();
177
		$show_remove = $this->_check_auth_remove_attr($user_groups, $hide_attr);
178
179
		foreach ($this->_attr as $attr)
180
		{
181
			if (empty($attr['attr_auths']))
182
			{
183
				$attr_auths = array(array(
184
					'forums_ids'	=> array(),
185
					'groups_ids'	=> array(),
186
					'author'		=> false,
187
				));
188
			}
189
			else
190
			{
191
				$attr_auths = json_decode($attr['attr_auths'], true);
192
			}
193
194
			foreach ($attr_auths as $attr_auth)
195
			{
196
				if (!$this->_check_auth_attribute($attr_auth, $forum_id, $user_groups, $author_id))
197
				{
198
					continue;
199
				}
200
201
				// show the selector !
202
				$show_select = true;
203
204
				// parse the attribute name
205
				$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->data['username'], $this->user->format_date($current_time, $attr['attr_date'])), $this->user->lang($attr['attr_name']));
206
207
				$this->template->assign_block_vars('attributes', array(
208
					'QTE_ID'		=> $attr['attr_id'],
209
					'QTE_TYPE'		=> $attr['attr_type'],
210
					'QTE_NAME'		=> $attribute_name,
211
					'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
212
					'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
213
214
					'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
215
216
					'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
217
					'U_QTE_URL'		=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => $attr['attr_id'])) : false,
218
				));
219
			}
220
		}
221
222
		if ($show_select)
223
		{
224
			$this->template->assign_vars(array(
225
				'S_QTE_SELECT'		=> true,
226
				'S_QTE_REMOVE'		=> $show_remove,
227
				'S_QTE_EMPTY'		=> (empty($attribute_id)),
228
				'S_QTE_SELECTED'	=> ($show_remove && ($attribute_id == -1)),
229
				'S_QTE_KEEP'		=> !empty($attribute_id) && ($attribute_id == self::KEEP),
230
231
				'L_QTE_SELECT'		=> $this->user->lang['QTE_ATTRIBUTE_' . (!empty($attribute_id) ? ($show_remove ? 'REMOVE' : 'RESTRICT') : 'ADD')],
232
				'U_QTE_URL'			=> !empty($viewtopic_url) ? append_sid($viewtopic_url, array('attr_id' => -1)) : false,
233
			));
234
		}
235
	}
236
237
	/**
238
	* Generate a list of all attributes for search page
239
	*
240
	* @return	null
241
	*/
242
	public function attr_search()
243
	{
244
		$show_select = false;
245
246
		foreach ($this->_attr as $attr)
247
		{
248
			// show the selector !
249
			$show_select = true;
250
251
			// parse the attribute name
252
			$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
253
254
			$this->template->assign_block_vars('attributes', array(
255
				'QTE_ID'		=> $attr['attr_id'],
256
				'QTE_TYPE'		=> $attr['attr_type'],
257
				'QTE_NAME'		=> $attribute_name,
258
				'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
259
				'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
260
261
				'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
262
			));
263
		}
264
265
		if ($show_select)
266
		{
267
			$this->template->assign_var('S_QTE_SELECT', true);
268
		}
269
	}
270
271
	/**
272
	* Generate a list of attributes for viewforum page
273
	*
274
	* @param	int		$forum_id		Forum id
275
	* @param	int		$attribute_id	Current attribute id
276
	*
277
	* @return	null
278
	*/
279 View Code Duplication
	public function attr_sort($forum_id = 0, $attribute_id = 0)
280
	{
281
		$show_select = false;
282
283
		foreach ($this->_attr as $attr)
284
		{
285
			if (empty($attr['attr_auths']))
286
			{
287
				$attr_auths = array(array(
288
					'forums_ids'	=> array(),
289
					'groups_ids'	=> array(),
290
					'author'		=> false,
291
				));
292
			}
293
			else
294
			{
295
				$attr_auths = json_decode($attr['attr_auths'], true);
296
			}
297
298
			foreach ($attr_auths as $attr_auth)
299
			{
300
				$forum_ids = $attr_auth['forums_ids'];
301
302
				if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
303
				{
304
					// show the selector !
305
					$show_select = true;
306
307
					// parse the attribute name
308
					$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
309
310
					$this->template->assign_block_vars('attributes', array(
311
						'QTE_ID'		=> $attr['attr_id'],
312
						'QTE_TYPE'		=> $attr['attr_type'],
313
						'QTE_NAME'		=> $attribute_name,
314
						'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
315
						'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
316
317
						'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)) ? true : false,
318
319
						'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
320
					));
321
				}
322
			}
323
		}
324
325
		if ($show_select)
326
		{
327
			$this->template->assign_var('S_QTE_SELECT', true);
328
		}
329
	}
330
331
	/**
332
	* Generate a default attribute list for a forum
333
	*
334
	* @param	int		$forum_id		Forum id
335
	* @param	int		$attribute_id	Current attribute id
336
	*
337
	* @return	null
338
	*/
339 View Code Duplication
	public function attr_default($forum_id = 0, $attribute_id = 0)
340
	{
341
		$show_select = false;
342
343
		foreach ($this->_attr as $attr)
344
		{
345
			if (empty($attr['attr_auths']))
346
			{
347
				$attr_auths = array(array(
348
					'forums_ids'	=> array(),
349
					'groups_ids'	=> array(),
350
					'author'		=> false,
351
				));
352
			}
353
			else
354
			{
355
				$attr_auths = json_decode($attr['attr_auths'], true);
356
			}
357
358
			foreach ($attr_auths as $attr_auth)
359
			{
360
				$forum_ids = $attr_auth['forums_ids'];
361
362
				if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
363
				{
364
					// show the selector !
365
					$show_select = true;
366
367
					// parse the attribute name
368
					$attribute_name = str_replace(array('%mod%', '%date%'), array($this->user->lang['QTE_KEY_USERNAME'], $this->user->lang['QTE_KEY_DATE']), $this->user->lang($attr['attr_name']));
369
370
					$this->template->assign_block_vars('attributes', array(
371
						'QTE_ID'		=> $attr['attr_id'],
372
						'QTE_TYPE'		=> $attr['attr_type'],
373
						'QTE_NAME'		=> $attribute_name,
374
						'QTE_DESC'		=> $this->user->lang($attr['attr_desc']),
375
						'QTE_COLOUR'	=> $this->attr_colour($attr['attr_name'], $attr['attr_colour']),
376
377
						'IS_SELECTED'	=> (!empty($attribute_id) && ($attr['attr_id'] == $attribute_id)),
378
379
						'S_QTE_DESC'	=> !empty($attr['attr_desc']) ? true : false,
380
					));
381
				}
382
			}
383
		}
384
385
		if ($show_select)
386
		{
387
			$this->template->assign_var('S_QTE_SELECT', true);
388
		}
389
	}
390
391
	/**
392
	* Generate attribute for topic title
393
	*
394
	* @param	int		$attribute_id	Current attribute id
395
	* @param	int		$user_id		Current attribute user id
396
	* @param	int		$timestamp		Attribute timestamp
397
	*
398
	* @return	string					Attribute html code
399
	*/
400
	public function attr_display($attribute_id = 0, $user_id = 0, $timestamp = 0)
401
	{
402
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
403
		{
404
			return false;
405
		}
406
407
		if (isset($this->_attr[$attribute_id]))
408
		{
409
			$attribute_colour = $this->attr_colour($this->_attr[$attribute_id]['attr_name'], $this->_attr[$attribute_id]['attr_colour']);
410
411
			if (isset($this->_name[$user_id]['user_id']))
412
			{
413
				$attribute_username = get_username_string(($this->_attr[$attribute_id]['attr_user_colour'] ? 'no_profile' : 'username'), $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
414
			}
415
			else
416
			{
417
				$attribute_username = $this->user->lang['GUEST'];
418
			}
419
420
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
421
422
			$attribute_name = str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
423
424
			return !$this->_attr[$attribute_id]['attr_type'] ? '<span' . $attribute_colour . '>' . $attribute_name . '</span>' : $this->attr_img_key($this->_attr[$attribute_id]['attr_img'], $attribute_name);
425
		}
426
	}
427
428
	/**
429
	* Generate attribute for page title
430
	*
431
	* @param	int		$attribute_id	Current attribute id
432
	* @param	int		$user_id		Current attribute user id
433
	* @param	int		$timestamp		Attribute timestamp
434
	*
435
	* @return	string					attribute html code
436
	*/
437
	public function attr_title($attribute_id = 0, $user_id = 0, $timestamp = 0)
438
	{
439
		if (empty($attribute_id) || empty($user_id) || empty($timestamp))
440
		{
441
			return false;
442
		}
443
444
		if (isset($this->_attr[$attribute_id]))
445
		{
446
			if (isset($this->_name[$user_id]['user_id']))
447
			{
448
				$attribute_username = get_username_string('username', $this->_name[$user_id]['user_id'], $this->_name[$user_id]['username'], $this->_name[$user_id]['user_colour']);
449
			}
450
			else
451
			{
452
				$attribute_username = $this->user->lang['GUEST'];
453
			}
454
455
			$attribute_date = $this->user->format_date($timestamp, $this->_attr[$attribute_id]['attr_date']);
456
457
			return str_replace(array('%mod%', '%date%'), array($attribute_username, $attribute_date), $this->user->lang($this->_attr[$attribute_id]['attr_name']));
458
		}
459
	}
460
461
462
	/**
463
	* Change topic attribute
464
	*
465
	* @param	int		$attribute_id		New attribute id
466
	* @param	int		$topic_id			The id of the topic
467
	* @param	int		$forum_id			The id of the forum
468
	* @param	int		$topic_attribute	Current attribute id
469
	* @param	array	$hide_attr			Groups which can't delete attribute in this forum
470
	*
471
	* @return	null
472
	*/
473
	public function attr_apply($attribute_id = 0, $topic_id = 0, $forum_id = 0, $topic_attribute = 0, $hide_attr = array())
474
	{
475
		if (empty($topic_id) || empty($forum_id) || empty($attribute_id))
476
		{
477
			return;
478
		}
479
480
		// time !
481
		$current_time = time();
482
		$user_groups = array();
483
484
		if ($attribute_id == self::REMOVE && !$this->_check_auth_remove_attr($user_groups, $hide_attr))
485
		{
486
			return;
487
		}
488 View Code Duplication
		else if ($attribute_id == self::REMOVE)
489
		{
490
			$fields = array(
491
				'topic_attr_id'		=> 0,
492
				'topic_attr_user'	=> 0,
493
				'topic_attr_time'	=> 0,
494
			);
495
		}
496
		else
497
		{
498
			$fields = array(
499
				'topic_attr_id'		=> $attribute_id,
500
				'topic_attr_user'	=> $this->user->data['user_id'],
501
				'topic_attr_time'	=> $current_time,
502
			);
503
		}
504
505
		$sql = 'UPDATE ' . TOPICS_TABLE . '
506
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
507
			WHERE topic_id = ' . (int) $topic_id;
508
		$this->db->sql_query($sql);
509
510
		$sql = 'SELECT topic_id
511
			FROM ' . TOPICS_TABLE . '
512
			WHERE topic_moved_id = ' . (int) $topic_id;
513
		$result = $this->db->sql_query($sql);
514
		$shadow_topic_id = (int) $this->db->sql_fetchfield('topic_id');
515
		$this->db->sql_freeresult($result);
516
517
		if (!empty($shadow_topic_id))
518
		{
519
			$sql = 'UPDATE ' . TOPICS_TABLE . '
520
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
521
				WHERE topic_id = ' . $shadow_topic_id;
522
			$this->db->sql_query($sql);
523
		}
524
525
		$meta_url = append_sid($this->root_path . 'viewtopic.' . $this->php_ext, array('f' => $forum_id, 't' => $topic_id));
526
		meta_refresh(3, $meta_url);
527
528
		// load language
529
		$this->user->add_lang('posting');
530
531
		$message = $this->user->lang['QTE_ATTRIBUTE_' . ($attribute_id == -1 ? 'REMOVED' : (empty($topic_attribute) ? 'ADDED' : 'UPDATED'))] . '<br /><br />' . sprintf($this->user->lang['VIEW_MESSAGE'], '<a href="' . $meta_url . '">', '</a>');
532
		$message .= '<br /><br />' . sprintf($this->user->lang['RETURN_FORUM'], '<a href="' . append_sid($this->root_path . 'viewforum.' . $this->php_ext, array('f' => $forum_id)) . '">', '</a>');
533
534
		if ($this->request->is_ajax())
535
		{
536
			$json_response = new \phpbb\json_response;
537
			$json_response->send(array(
538
				'success' => true,
539
540
				'MESSAGE_TITLE'	=> $this->user->lang['INFORMATION'],
541
				'MESSAGE_TEXT'	=> $message,
542
				'NEW_ATTRIBUTE'	=> $this->attr_display($attribute_id, $this->user->data['user_id'], $current_time),
543
			));
544
		}
545
546
		trigger_error($message);
547
	}
548
549
	/**
550
	* Change topic attribute in mcp
551
	*
552
	* @param	int		$attribute_id		New attribute id
553
	* @param	array	$topic_ids			Topics ids
554
	*
555
	* @return	null
556
	*/
557
	public function mcp_attr_apply($attribute_id = 0, $topic_ids = array())
558
	{
559
		if (!sizeof($topic_ids))
560
		{
561
			trigger_error('NO_TOPIC_SELECTED');
562
		}
563
564
		if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id'))
565
		{
566
			return;
567
		}
568
569
		// time !
570
		$current_time = time();
571
572
		$sql = 'SELECT topic_id, forum_id, topic_title, topic_attr_id
573
			FROM ' . TOPICS_TABLE . '
574
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
575
		$result = $this->db->sql_query($sql);
576
577
		// log this action
578
		while ($row = $this->db->sql_fetchrow($result))
579
		{
580
			$message = ($attribute_id == -1) ? 'REMOVED' : (empty($row['topic_attr_id']) ? 'ADDED' : 'UPDATED');
581
			$additional_data = array(
582
				'forum_id'	=> $row['forum_id'],
583
				'topic_id'	=> $row['topic_id'],
584
				$row['topic_title'],
585
			);
586
			$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, 'MCP_ATTRIBUTE_' . $message, $current_time, $additional_data);
587
		}
588
		$this->db->sql_freeresult($result);
589
590 View Code Duplication
		if ($attribute_id == -1)
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
591
		{
592
			$fields = array(
593
				'topic_attr_id'		=> 0,
594
				'topic_attr_user'	=> 0,
595
				'topic_attr_time'	=> 0,
596
			);
597
		}
598
		else
599
		{
600
			$fields = array(
601
				'topic_attr_id'		=> $attribute_id,
602
				'topic_attr_user'	=> $this->user->data['user_id'],
603
				'topic_attr_time'	=> $current_time,
604
			);
605
		}
606
607
		$sql = 'UPDATE ' . TOPICS_TABLE . '
608
			SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
609
			WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $topic_ids));
610
		$this->db->sql_query($sql);
611
612
		$sql = 'SELECT topic_id
613
			FROM ' . TOPICS_TABLE . '
614
			WHERE ' . $this->db->sql_in_set('topic_moved_id', array_map('intval', $topic_ids));
615
		$result = $this->db->sql_query($sql);
616
617
		$shadow_topic_ids = array();
618
		while ($row = $this->db->sql_fetchrow($result))
619
		{
620
			$shadow_topic_ids[] = (int) $row['topic_id'];
621
		}
622
		$this->db->sql_freeresult($result);
623
624
		if (sizeof($shadow_topic_ids))
625
		{
626
			$sql = 'UPDATE ' . TOPICS_TABLE . '
627
				SET ' . $this->db->sql_build_array('UPDATE', $fields) . '
628
				WHERE ' . $this->db->sql_in_set('topic_id', array_map('intval', $shadow_topic_ids));
629
			$this->db->sql_query($sql);
630
		}
631
632
		$redirect = $this->request->variable('redirect', $this->user->data['session_page']);
633
634
		meta_refresh(3, $redirect);
635
		trigger_error($this->user->lang['QTE_TOPIC' . (sizeof($topic_ids) == 1 ? '' : 'S') . '_ATTRIBUTE_' . (isset($message) ? $message : 'ADDED')] . '<br /><br />' . sprintf($this->user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
636
637
		return;
638
	}
639
640
	/**
641
	* Getter...
642
	*
643
	* @return	array
644
	*/
645
	public function getAttr()
646
	{
647
		return $this->_attr;
648
	}
649
650
	/**
651
	* Generate list of groups
652
	*
653
	* @param int		$group_ids		The default groups id to mark as selected
654
	* @param array|bool	$exclude_ids	The group ids to exclude from the list, false (default) if you whish to exclude no id
655
	* @param bool		$manage_founder If set to false (default) all groups are returned, if 0 only those groups returned not being managed by founders only, if 1 only those groups returned managed by founders only.
656
	*
657
	* @return string The list of options.
658
	*/
659
	public function qte_group_select($group_ids, $exclude_ids = array(), $manage_founder = false)
660
	{
661
		$exclude_sql = ($exclude_ids !== false && sizeof($exclude_ids)) ? 'WHERE ' . $this->db->sql_in_set('group_id', array_map('intval', $exclude_ids), true) : '';
662
		$sql_and = !$this->config['coppa_enable'] ? ($exclude_sql ? ' AND ' : ' WHERE ') . "group_name <> 'REGISTERED_COPPA'" : '';
663
		$sql_founder = ($manage_founder !== false) ? (($exclude_sql || $sql_and) ? ' AND ' : ' WHERE ') . 'group_founder_manage = ' . (int) $manage_founder : '';
664
665
		$sql = 'SELECT group_id, group_name, group_type
666
			FROM ' . GROUPS_TABLE . "
667
			$exclude_sql
668
			$sql_and
669
			$sql_founder
670
			ORDER BY group_type DESC, group_name ASC";
671
		$result = $this->db->sql_query($sql);
672
673
		$s_group_options = '';
674
		while ($row = $this->db->sql_fetchrow($result))
675
		{
676
			$selected = in_array($row['group_id'], $group_ids) ? ' selected="selected"' : '';
677
			$s_group_options .= '<option' . (($row['group_type'] == GROUP_SPECIAL) ? ' class="sep"' : '') . ' value="' . $row['group_id'] . '"' . $selected . '>' . (($row['group_type'] == GROUP_SPECIAL) ? $this->user->lang['G_' . $row['group_name']] : $row['group_name']) . '</option>';
678
		}
679
		$this->db->sql_freeresult($result);
680
681
		return $s_group_options;
682
	}
683
684
	// borrowed from "Categories Hierarchy" : used to check if a image key exists
685
	public function attr_img_key($key, $alt)
686
	{
687
		return empty($key) ? '' : (preg_match('#^[a-z0-9_-]+$#i', $key) ? $this->user->img($key, $alt) : '<img src="' . (preg_match('#^(ht|f)tp[s]?\://#i', $key) ? $key : $this->root_path . $key) . '" alt="' . $alt . '" title="' . $alt . '" />');
688
	}
689
690
	/**
691
	* Build class and style attribute
692
	*
693
	* @param	string	$a_name			Attribute name
694
	* @param	string	$a_colour		Attribute color
695
	* @return	string					html code
696
	*/
697
	public function attr_colour($a_name, $a_colour)
698
	{
699
		if ($a_name != $this->user->lang($a_name))
700
		{
701
			$a_class_name = preg_replace('#[^a-z0-9 _-]#', '', strtolower($a_name));
702
		}
703
704
		return ' class="qte-attr ' . (isset($a_class_name) ?  $a_class_name : '') . '"' . (!empty($a_colour) ? ' style="color:#' . $a_colour . '; font-weight:bold;"' : '');
705
	}
706
707
	/**
708
	* Check if user can apply an attribute
709
	*
710
	* @param	array	$attr_auth		Forum auth
711
	* @param	int		$forum_id		Forum id
712
	* @param	array	$user_groups	User's groups
713
	* @param	int		$author_id		Topic author id
714
	* @return	bool
715
	*/
716
	private function _check_auth_attribute($attr_auth, $forum_id, $user_groups, $author_id)
717
	{
718
		$forum_ids = $attr_auth['forums_ids'];
719
		$group_ids = $attr_auth['groups_ids'];
720
721
		if (is_array($forum_ids) && in_array($forum_id, $forum_ids))
722
		{
723
			if (is_array($group_ids) && array_intersect($group_ids, $user_groups) || ($attr_auth['author'] && ($author_id == $this->user->data['user_id']) && ($this->user->data['user_id'] != ANONYMOUS)))
724
			{
725
				return true;
726
			}
727
		}
728
729
		return false;
730
	}
731
732
	/**
733
	* Check if user can delete an attribute
734
	*
735
	* @param	array	$user_groups	User's groups
736
	* @param	array	$hide_attr		Groups which can't delete attribute in a forum
737
	* @return	bool
738
	*/
739
	private function _check_auth_remove_attr(&$user_groups, $hide_attr)
740
	{
741
		// include that file !
742
		if (!function_exists('group_memberships'))
743
		{
744
			include $this->root_path . 'includes/functions_user.' . $this->php_ext;
745
		}
746
747
		// get groups membership !
748
		$user_membership = group_memberships(false, $this->user->data['user_id']);
749
750
		$user_groups = array();
751
		if (!empty($user_membership))
752
		{
753
			foreach ($user_membership as $row)
754
			{
755
				$user_groups[$row['group_id']] = (int) $row['group_id'];
756
			}
757
		}
758
759
		$groups_removed = array_intersect($user_groups, $hide_attr);
760
		return (empty($hide_attr) || (count($groups_removed) < count($user_groups)));
761
	}
762
763
	/**
764
	* Get attributes from database
765
	*
766
	* @return	null
767
	*/
768
	private function _get_attributes()
769
	{
770
		if (($this->_attr = $this->cache->get('_attr')) === false)
771
		{
772
			$sql = 'SELECT *
773
				FROM ' . $this->table_prefix . 'topics_attr
774
				ORDER BY left_id ASC';
775
			$result = $this->db->sql_query($sql);
776
777
			$this->_attr = array();
778
			while ($row = $this->db->sql_fetchrow($result))
779
			{
780
				$this->_attr[$row['attr_id']] = array(
781
					'attr_id'			=> (int) $row['attr_id'],
782
					'attr_type'			=> (bool) $row['attr_type'],
783
					'attr_name'			=> $row['attr_name'],
784
					'attr_desc'			=> $row['attr_desc'],
785
					'attr_img'			=> $row['attr_img'],
786
					'attr_colour'		=> $row['attr_colour'],
787
					'attr_date'			=> $row['attr_date'],
788
					'attr_user_colour'	=> (bool) $row['attr_user_colour'],
789
					'attr_auths'		=> $row['attr_auths'],
790
				);
791
			}
792
			$this->db->sql_freeresult();
793
794
			$this->cache->put('_attr', $this->_attr);
795
		}
796
	}
797
}
798