Passed
Branch master (e751cb)
by Matt
02:48
created

bbcodes_display   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 67
dl 0
loc 197
ccs 55
cts 55
cp 1
rs 10
c 2
b 0
f 0
wmc 20

7 Methods

Rating   Name   Duplication   Size   Complexity  
A bbcode_statuses() 0 21 4
A get_icons() 0 23 3
A user_in_bbcode_group() 0 18 3
A load_memberships() 0 18 3
A display_custom_bbcodes() 0 10 3
A __construct() 0 8 1
A allow_custom_bbcodes() 0 9 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\auth\auth;
0 ignored issues
show
Bug introduced by
The type phpbb\auth\auth was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use phpbb\config\config;
0 ignored issues
show
Bug introduced by
The type phpbb\config\config was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use phpbb\db\driver\driver_interface;
0 ignored issues
show
Bug introduced by
The type phpbb\db\driver\driver_interface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use phpbb\extension\manager;
0 ignored issues
show
Bug introduced by
The type phpbb\extension\manager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use phpbb\textformatter\s9e\parser;
0 ignored issues
show
Bug introduced by
The type phpbb\textformatter\s9e\parser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use phpbb\user;
0 ignored issues
show
Bug introduced by
The type phpbb\user was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
20
/**
21
 * ABBC3 core BBCodes display class
22
 */
23
class bbcodes_display
24
{
25
	/** @var auth */
26
	protected $auth;
27
28
	/** @var config */
29
	protected $config;
30
31
	/** @var driver_interface */
32
	protected $db;
33
34
	/** @var manager */
35
	protected $extension_manager;
36
37
	/** @var user */
38
	protected $user;
39
40
	/** @var string */
41
	protected $root_path;
42
43
	/** @var array */
44
	protected $memberships;
45
46
	/**
47 13
	 * Constructor
48
	 *
49 13
	 * @param auth             $auth              Auth object
50 13
	 * @param config           $config            Config object
51 13
	 * @param driver_interface $db                Database connection
52 13
	 * @param manager          $extension_manager Extension manager object
53 13
	 * @param user             $user              User object
54
	 * @param string           $root_path         Path to phpBB root
55
	 * @access public
56
	 */
57
	public function __construct(auth $auth, config $config, driver_interface $db, manager $extension_manager, user $user, $root_path)
58
	{
59
		$this->auth = $auth;
60
		$this->config = $config;
61
		$this->db = $db;
62
		$this->extension_manager = $extension_manager;
63
		$this->user = $user;
64
		$this->root_path = $root_path;
65 1
	}
66
67 1
	/**
68
	 * Display allowed custom BBCodes with icons
69 1
	 *
70 1
	 * Uses GIF images named exactly the same as the bbcode_tag
71 1
	 *
72 1
	 * @param array $custom_tags Template data of the bbcode
73
	 * @param array $row         The data of the bbcode
74 1
	 * @return array Update template data of the bbcode
75 1
	 * @access public
76
	 */
77 1
	public function display_custom_bbcodes($custom_tags, $row)
78 1
	{
79
		$icons = $this->get_icons();
80 1
81
		$icon_tag = strtolower(rtrim($row['bbcode_tag'], '='));
82
83
		$custom_tags['BBCODE_IMG'] = isset($icons[$icon_tag]) ? $icons[$icon_tag] : '';
84
		$custom_tags['S_CUSTOM_BBCODE_ALLOWED'] = empty($row['bbcode_group']) || $this->user_in_bbcode_group($row['bbcode_group']);
85
86
		return $custom_tags;
87
	}
88
89
	/**
90 3
	 * Disable BBCodes not allowed by a user's group(s).
91
	 *
92 3
	 * @param parser $service Object from the text_formatter.parser service
93 3
	 * @return void
94
	 * @access public
95 3
	 */
96 3
	public function allow_custom_bbcodes(parser $service)
97 1
	{
98 1
		$parser = $service->get_parser();
99 1
		foreach ($parser->registeredVars['abbc3.bbcode_groups'] as $bbcode_name => $groups)
100 3
		{
101 3
			if (!$this->user_in_bbcode_group($groups))
102
			{
103
				$bbcode_name = rtrim($bbcode_name, '=');
104
				$service->disable_bbcode($bbcode_name);
105
			}
106
		}
107
	}
108
109
	/**
110 13
	 * Determine if a user is in a group allowed to use a custom BBCode
111
	 *
112
	 * @param string|array $group_ids Allowed group IDs, comma separated string or array
113 13
	 * @return bool Return true if allowed to use BBCode
114
	 * @access public
115 9
	 */
116 9
	public function user_in_bbcode_group($group_ids = '')
117 7
	{
118 7
		if ($group_ids)
119
		{
120
			// Convert string to an array
121 9
			if (!is_array($group_ids))
122
			{
123 9
				$group_ids = explode(',', $group_ids);
124
			}
125
126
			// Load the user's group memberships
127 6
			$this->load_memberships();
128
129
			return (bool) count(array_intersect($this->memberships, $group_ids));
130
		}
131
132
		// If we get here, there were no group restrictions so everyone can use this BBCode
133
		return true;
134
	}
135
136 1
	/**
137
	 * Get paths/names to ABBC3's BBCode icons.
138 1
	 * Search in ABBC3's icons dir and also the core's images dir.
139
	 *
140
	 * @return array Array of icon paths: ['foo' => './ext/vse/abbc3/images/icons/foo.png']
141 1
	 * @access public
142 1
	 */
143 1
	public function get_icons()
144
	{
145
		static $icons = [];
146
147
		if (empty($icons))
148
		{
149
			$finder = $this->extension_manager->get_finder();
150
			$icons = $finder
151 9
				->set_extensions(['vse/abbc3'])
152
				->suffix(".{$this->config['abbc3_icons_type']}")
153 9
				->extension_directory('/images/icons')
154 9
				->core_path('images/abbc3/icons/')
155 3
				->find();
156
157
			// Rewrite the image array with img names as keys and paths as values
158 9
			foreach ($icons as $path => $ext)
159
			{
160 9
				$icons[basename($path, ".{$this->config['abbc3_icons_type']}")] = $path;
161 9
				unset($icons[$path]);
162 9
			}
163 9
		}
164 9
165
		return $icons;
166 9
	}
167 9
168 9
	/**
169 9
	 * Load this user's group memberships if it's not cached already
170
	 *
171
	 * @access protected
172
	 */
173
	protected function load_memberships()
174
	{
175
		if ($this->memberships !== null)
176
		{
177
			return;
178
		}
179
180
		$this->memberships = [];
181
		$sql = 'SELECT group_id
182
			FROM ' . USER_GROUP_TABLE . '
0 ignored issues
show
Bug introduced by
The constant vse\abbc3\core\USER_GROUP_TABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
183
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
184
			AND user_pending = 0';
185
		$result = $this->db->sql_query($sql);
186
		while ($row = $this->db->sql_fetchrow($result))
187
		{
188
			$this->memberships[] = $row['group_id'];
189
		}
190
		$this->db->sql_freeresult($result);
191
	}
192
193
	/**
194
	 * Set BBCode statuses for posting IMG, URL, FLASH and QUOTE in a forum.
195
	 *
196
	 * @param int $forum_id The forum identifier
197
	 * @return array An array containing booleans for each BBCode status
198
	 */
199
	public function bbcode_statuses($forum_id)
200
	{
201
		$bbcode_status = $this->config['allow_bbcode'] && $this->auth->acl_get('f_bbcode', $forum_id);
202
		$url_status = $this->config['allow_post_links'];
203
		$img_status = $flash_status = false;
204
		$quote_status = true;
205
206
		if ($bbcode_status)
207
		{
208
			$img_status = $this->auth->acl_get('f_img', $forum_id);
209
			$flash_status = $this->auth->acl_get('f_flash', $forum_id) && $this->config['allow_post_flash'];
210
211
			display_custom_bbcodes();
0 ignored issues
show
Bug introduced by
The function display_custom_bbcodes was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
			/** @scrutinizer ignore-call */ 
212
   display_custom_bbcodes();
Loading history...
212
		}
213
214
		return [
215
			'S_BBCODE_ALLOWED' => $bbcode_status,
216
			'S_BBCODE_IMG'     => $img_status,
217
			'S_BBCODE_FLASH'   => $flash_status,
218
			'S_BBCODE_QUOTE'   => $quote_status,
219
			'S_LINKS_ALLOWED'  => $url_status,
220
		];
221
	}
222
}
223