Completed
Push — develop-3.2.x ( 477e50...b33cd9 )
by Matt
9s
created

bbcodes_display::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
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\db\driver\driver_interface;
14
use phpbb\extension\manager;
15
use phpbb\user;
16
17
/**
18
 * ABBC3 core BBCodes display class
19
 */
20
class bbcodes_display
21
{
22
	/** @var driver_interface */
23
	protected $db;
24
25
	/** @var manager */
26
	protected $extension_manager;
27
28
	/** @var user */
29
	protected $user;
30
31
	/** @var string */
32
	protected $ext_root_path;
33
34
	/** @var array */
35
	protected $memberships;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param driver_interface $db                Database connection
41
	 * @param manager          $extension_manager Extension manager object
42
	 * @param user             $user              User object
43
	 * @param string           $ext_root_path     Path to abbc3 extension root
44
	 * @access public
45
	 */
46 11
	public function __construct(driver_interface $db, manager $extension_manager, user $user, $ext_root_path)
47
	{
48 11
		$this->db = $db;
49 11
		$this->extension_manager = $extension_manager;
50 11
		$this->user = $user;
51 11
		$this->ext_root_path = $ext_root_path;
52 11
	}
53
54
	/**
55
	 * Display allowed custom BBCodes with icons
56
	 *
57
	 * Uses GIF images named exactly the same as the bbcode_tag
58
	 *
59
	 * @param array $custom_tags Template data of the bbcode
60
	 * @param array $row         The data of the bbcode
61
	 * @return array Update template data of the bbcode
62
	 * @access public
63
	 */
64 1
	public function display_custom_bbcodes($custom_tags, $row)
65
	{
66 1
		static $images = array();
67
68 1
		if (empty($images))
69 1
		{
70 1
			$images = $this->get_images();
71 1
		}
72
73 1
		$bbcode_img = 'abbc3/images/icons/' . strtolower(rtrim($row['bbcode_tag'], '=')) . '.gif';
74
		$images_key = 'ext/' . $bbcode_img;
75 1
76 1
		$custom_tags['BBCODE_IMG'] = isset($images[$images_key]) ? 'ext/vse/' . $bbcode_img : '';
77
		$custom_tags['S_CUSTOM_BBCODE_ALLOWED'] = (!empty($row['bbcode_group'])) ? $this->user_in_bbcode_group($row['bbcode_group']) : true;
78 1
79
		return $custom_tags;
80
	}
81
82
	/**
83
	 * Set custom BBCodes to 'disabled' if they are not allowed to be used
84
	 *
85
	 * @param array $bbcodes Array of bbcode data for use in parsing
86
	 * @param array $rowset  Array of bbcode data from the database
87
	 * @return array The bbcodes data array
88
	 * @access public
89
	 *
90
	 * @deprecated 3.2.0. Provides bc for phpBB 3.1.x.
91
	 */
92
	public function allow_custom_bbcodes($bbcodes, $rowset)
93
	{
94
		foreach ($rowset as $row)
95
		{
96
			if (!$this->user_in_bbcode_group($row['bbcode_group']))
97
			{
98
				$bbcodes[$row['bbcode_tag']]['disabled'] = true;
99
			}
100
		}
101
102
		return $bbcodes;
103
	}
104
105
	/**
106
	 * Determine if a user is in a group allowed to use a custom BBCode
107
	 *
108
	 * @param string|array $group_ids Allowed group IDs, comma separated string or array
109
	 * @return bool Return true if allowed to use BBCode
110
	 * @access public
111 11
	 */
112
	public function user_in_bbcode_group($group_ids = '')
113
	{
114 11
		if ($group_ids)
115
		{
116 7
			// Convert string to an array
117 7
			if (!is_array($group_ids))
118 5
			{
119 5
				$group_ids = explode(',', $group_ids);
120
			}
121
122 7
			// Load the user's group memberships
123
			$this->load_memberships();
124 7
125
			return (bool) count(array_intersect($this->memberships, $group_ids));
126
		}
127
128 4
		// If we get here, there were no group restrictions so everyone can use this BBCode
129
		return true;
130
	}
131
132
	/**
133
	 * Get image paths/names from ABBC3's icons folder
134
	 *
135
	 * @return array File data from ./ext/vse/abbc3/images/icons
136
	 * @access protected
137 1
	 */
138
	protected function get_images()
139 1
	{
140
		$finder = $this->extension_manager->get_finder();
141
142 1
		return $finder
143 1
			->extension_suffix('.gif')
144 1
			->extension_directory('/images/icons')
145
			->find_from_extension('abbc3', $this->ext_root_path);
146
	}
147
148
	/**
149
	 * Load this user's group memberships if it's not cached already
150
	 *
151
	 * @return null
152
	 * @access protected
153 7
	 */
154
	protected function load_memberships()
155 7
	{
156 7
		if (isset($this->memberships))
157 1
		{
158
			return;
159
		}
160 7
161
		$this->memberships = array();
162 7
		$sql = 'SELECT group_id
163 7
			FROM ' . USER_GROUP_TABLE . '
164 7
			WHERE user_id = ' . (int) $this->user->data['user_id'] . '
165 7
			AND user_pending = 0';
166 7
		$result = $this->db->sql_query($sql);
167
		while ($row = $this->db->sql_fetchrow($result))
168 7
		{
169 7
			$this->memberships[] = $row['group_id'];
170 7
		}
171 7
		$this->db->sql_freeresult($result);
172
	}
173
}
174