Completed
Push — master ( fc12da...5e2dd8 )
by Matt
07:52 queued 03:39
created

bbcodes_display   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 0
dl 0
loc 165
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A allow_custom_bbcodes() 0 12 3
A user_in_bbcode_group() 0 19 3
A __construct() 0 8 1
A display_custom_bbcodes() 0 11 3
A get_icons() 0 24 3
A load_memberships() 0 19 3
1
<?php
2
/**
3
 *
4
 * Advanced BBCode Box
5
 *
6
 * @copyright (c) 2013 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\abbc3\core;
12
13
use phpbb\config\config;
14
use phpbb\db\driver\driver_interface;
15
use phpbb\extension\manager;
16
use phpbb\textformatter\s9e\parser;
17
use phpbb\user;
18
19
/**
20
 * ABBC3 core BBCodes display class
21
 */
22
class bbcodes_display
23
{
24
	/** @var config */
25
	protected $config;
26
27
	/** @var driver_interface */
28
	protected $db;
29
30
	/** @var manager */
31
	protected $extension_manager;
32
33
	/** @var user */
34
	protected $user;
35
36
	/** @var string */
37
	protected $root_path;
38
39
	/** @var array */
40
	protected $memberships;
41
42
	/**
43
	 * Constructor
44
	 *
45
	 * @param config           $config            Config object
46
	 * @param driver_interface $db                Database connection
47 13
	 * @param manager          $extension_manager Extension manager object
48
	 * @param user             $user              User object
49 13
	 * @param string           $root_path         Path to phpBB root
50 13
	 * @access public
51 13
	 */
52 13
	public function __construct(config $config, driver_interface $db, manager $extension_manager, user $user, $root_path)
53 13
	{
54
		$this->config = $config;
55
		$this->db = $db;
56
		$this->extension_manager = $extension_manager;
57
		$this->user = $user;
58
		$this->root_path = $root_path;
59
	}
60
61
	/**
62
	 * Display allowed custom BBCodes with icons
63
	 *
64
	 * Uses GIF images named exactly the same as the bbcode_tag
65 1
	 *
66
	 * @param array $custom_tags Template data of the bbcode
67 1
	 * @param array $row         The data of the bbcode
68
	 * @return array Update template data of the bbcode
69 1
	 * @access public
70 1
	 */
71 1
	public function display_custom_bbcodes($custom_tags, $row)
72 1
	{
73
		$icons = $this->get_icons();
74 1
75 1
		$icon_tag = strtolower(rtrim($row['bbcode_tag'], '='));
76
77 1
		$custom_tags['BBCODE_IMG'] = isset($icons[$icon_tag]) ? $icons[$icon_tag] : '';
78 1
		$custom_tags['S_CUSTOM_BBCODE_ALLOWED'] = !empty($row['bbcode_group']) ? $this->user_in_bbcode_group($row['bbcode_group']) : true;
79
80 1
		return $custom_tags;
81
	}
82
83
	/**
84
	 * Disable BBCodes not allowed by a user's group(s).
85
	 *
86
	 * @param parser $service Object from the text_formatter.parser service
87
	 * @return void
88
	 * @access public
89
	 */
90 3
	public function allow_custom_bbcodes(parser $service)
91
	{
92 3
		$parser = $service->get_parser();
93 3
		foreach ($parser->registeredVars['abbc3.bbcode_groups'] as $bbcode_name => $groups)
94
		{
95 3
			if (!$this->user_in_bbcode_group($groups))
96 3
			{
97 1
				$bbcode_name = rtrim($bbcode_name, '=');
98 1
				$service->disable_bbcode($bbcode_name);
99 1
			}
100 3
		}
101 3
	}
102
103
	/**
104
	 * Determine if a user is in a group allowed to use a custom BBCode
105
	 *
106
	 * @param string|array $group_ids Allowed group IDs, comma separated string or array
107
	 * @return bool Return true if allowed to use BBCode
108
	 * @access public
109
	 */
110 13
	public function user_in_bbcode_group($group_ids = '')
111
	{
112
		if ($group_ids)
113 13
		{
114
			// Convert string to an array
115 9
			if (!is_array($group_ids))
116 9
			{
117 7
				$group_ids = explode(',', $group_ids);
118 7
			}
119
120
			// Load the user's group memberships
121 9
			$this->load_memberships();
122
123 9
			return (bool) count(array_intersect($this->memberships, $group_ids));
124
		}
125
126
		// If we get here, there were no group restrictions so everyone can use this BBCode
127 6
		return true;
128
	}
129
130
	/**
131
	 * Get paths/names to ABBC3's BBCode icons.
132
	 * Search in ABBC3's icons dir and also the core's images dir.
133
	 *
134
	 * @return array Array of icon paths: ['foo' => './ext/vse/abbc3/images/icons/foo.png']
135
	 * @access public
136 1
	 */
137
	public function get_icons()
138 1
	{
139
		static $icons = [];
140
141 1
		if (empty($icons))
142 1
		{
143 1
			$finder = $this->extension_manager->get_finder();
144
			$icons = $finder
145
				->set_extensions(['vse/abbc3'])
146
				->suffix(".{$this->config['abbc3_icons_type']}")
147
				->extension_directory('/images/icons')
148
				->core_path('images/abbc3/icons/')
149
				->get_files();
150
151 9
			// Rewrite the image array with img names as keys and paths as values
152
			foreach ($icons as $key => $path)
153 9
			{
154 9
				$icons[basename($path, ".{$this->config['abbc3_icons_type']}")] = $path;
155 3
				unset($icons[$key]);
156
			}
157
		}
158 9
159
		return $icons;
160 9
	}
161 9
162 9
	/**
163 9
	 * Load this user's group memberships if it's not cached already
164 9
	 *
165
	 * @access protected
166 9
	 */
167 9
	protected function load_memberships()
168 9
	{
169 9
		if ($this->memberships !== null)
170
		{
171
			return;
172
		}
173
174
		$this->memberships = [];
175
		$sql = 'SELECT group_id
176
			FROM ' . USER_GROUP_TABLE . '
177
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
178
			AND user_pending = 0';
179
		$result = $this->db->sql_query($sql);
180
		while ($row = $this->db->sql_fetchrow($result))
181
		{
182
			$this->memberships[] = $row['group_id'];
183
		}
184
		$this->db->sql_freeresult($result);
185
	}
186
}
187