-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubs-SimpleColorizer.php
More file actions
119 lines (101 loc) · 3.19 KB
/
Subs-SimpleColorizer.php
File metadata and controls
119 lines (101 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php
/**
* @package SimpleColorizer
* @version 1.4
* @author Diego Andrés <diegoandres_cortes@outlook.com>
* @copyright Copyright (c) 2022, SMF Tricks
* @license MIT
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
* Using integrate_buffer it will insert the color of the group for each user if a profile link is found.
*
* @param string $buffer The buffer content
* @return $buffer The modified buffer content
*/
function ob_colorizer($buffer)
{
global $scripturl;
// on xml we don't need to do anything
if (isset($_REQUEST['xml']))
return $buffer;
// Get all of the profile links in the content and obtain their ID's.
$user_ids = preg_match_all('~<a.+?href="' . preg_quote($scripturl) . '\?action=profile;u=(\d+)"~', $buffer, $matches) ? array_unique($matches[1]) : array();
// Do nothing if there are no ID's
if (empty($user_ids))
return $buffer;
// Get the users and their colors if there are any.
if (($user_colors = sc_loadColors($user_ids)) !== false)
{
// Loop through the users and insert their colors.
foreach ($user_colors as $user_id => $user_color)
{
// No color, no fun
if (empty($user_color))
continue;
// Replace the links that match the profile URL pattern
$buffer = preg_replace_callback(str_replace('{$user_id}', $user_id, '~<a[^>]*href="' . preg_quote($scripturl) . '\?action=profile\;u={$user_id}"[^>]*>~'),
function ($matches) use ($user_color)
{
$result = $matches[0];
// No styles
if (strpos($result, 'style="') === false)
$result = str_replace('>', ' style="color: ' . $user_color . ';">', $result);
// Add the color
else
$result = preg_replace('/style=(["\'])([^"\']*)\1/','style=$1$2;color:'. $user_color . ';$1', $result);
return $result;
},
$buffer);
}
}
// Return the colorized forum buffer.
return $buffer;
}
/**
* Loads the users with the found ID's and returns an array with the user ID's and the color.
*
* @param array $user_ids The user ID's
* @return array|bool The user ID's and the color or false if there are no users
*/
function sc_loadColors($user_ids = array())
{
global $smcFunc;
// No users? Sad.
if (empty($user_ids))
return false;
// Make sure it's an array or make it an array.
$user_ids = is_array($user_ids) ? $user_ids : array($user_ids);
$request = $smcFunc['db_query']('','
SELECT mem.id_member, mem.real_name, mg.online_color AS member_group_color, pg.online_color AS post_group_color
FROM {db_prefix}members AS mem
LEFT JOIN {db_prefix}membergroups AS pg ON (pg.id_group = mem.id_post_group)
LEFT JOIN {db_prefix}membergroups AS mg ON (mg.id_group = mem.id_group)
WHERE mem.id_member IN ({array_int:user_ids})',
array(
'user_ids' => $user_ids,
)
);
$user_colors = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$user_colors[$row['id_member']] = !empty($row['member_group_color']) ? $row['member_group_color'] : $row['post_group_color'];
unset($row['member_group_color'], $row['post_group_color']);
}
$smcFunc['db_free_result']($request);
return $user_colors;
}
/**
* Add some extra css to prevent inconveniences.
*
* @return void
*/
function sc_css()
{
addInlineCss('
a > strong {
color: inherit;
}
');
}