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: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236:
<?php
class Mentions
{
protected static $char = '@';
public static function getMentionsByContent($content_type, $content_id, array $members = array())
{
global $smcFunc;
$request = $smcFunc['db_query']('', '
SELECT mem.id_member, mem.real_name, mem.email_address, mem.id_group, mem.id_post_group, mem.additional_groups,
mem.lngfile, ment.id_member AS id_mentioned_by, ment.real_name AS mentioned_by_name
FROM {db_prefix}mentions AS m
INNER JOIN {db_prefix}members AS mem ON (mem.id_member = m.id_mentioned)
INNER JOIN {db_prefix}members AS ment ON (ment.id_member = m.id_member)
WHERE content_type = {string:type}
AND content_id = {int:id}' . (!empty($members) ? '
AND mem.id_member IN ({array_int:members})' : ''),
array(
'type' => $content_type,
'id' => $content_id,
'members' => (array) $members,
)
);
$members = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
$members[$row['id_member']] = array(
'id' => $row['id_member'],
'real_name' => $row['real_name'],
'email_address' => $row['email_address'],
'groups' => array_unique(array_merge(array($row['id_group'], $row['id_post_group']), explode(',', $row['additional_groups']))),
'mentioned_by' => array(
'id' => $row['id_mentioned_by'],
'name' => $row['mentioned_by_name'],
),
'lngfile' => $row['lngfile'],
);
$smcFunc['db_free_result']($request);
return $members;
}
public static function insertMentions($content_type, $content_id, array $members, $id_member)
{
global $smcFunc;
call_integration_hook('mention_insert_' . $content_type, array($content_id, &$members));
foreach ($members as $member)
$smcFunc['db_insert']('ignore',
'{db_prefix}mentions',
array('content_id' => 'int', 'content_type' => 'string', 'id_member' => 'int', 'id_mentioned' => 'int', 'time' => 'int'),
array((int) $content_id, $content_type, $id_member, $member['id'], time()),
array('content_id', 'content_type', 'id_mentioned')
);
}
public static function getBody($body, array $members)
{
foreach ($members as $member)
$body = str_ireplace(static::$char . $member['real_name'], '[member=' . $member['id'] . ']' . $member['real_name'] . '[/member]', $body);
return $body;
}
public static function getMentionedMembers($body)
{
global $smcFunc;
$possible_names = self::getPossibleMentions($body);
if (empty($possible_names) || !allowedTo('mention'))
return array();
$request = $smcFunc['db_query']('', '
SELECT id_member, real_name
FROM {db_prefix}members
WHERE real_name IN ({array_string:names})
ORDER BY LENGTH(real_name) DESC
LIMIT {int:count}',
array(
'names' => $possible_names,
'count' => count($possible_names),
)
);
$members = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
if (stripos($body, static::$char . $row['real_name']) === false)
continue;
$members[$row['id_member']] = array(
'id' => $row['id_member'],
'real_name' => $row['real_name'],
);
}
$smcFunc['db_free_result']($request);
return $members;
}
protected static function getPossibleMentions($body)
{
global $smcFunc;
$body = htmlspecialchars_decode(preg_replace('~<br\s*/?\>~', "\n", str_replace(' ', ' ', $body)), ENT_QUOTES);
while (preg_match('~\[quote[^\]]*\](.+?)\[\/quote\]~s', $body))
$body = preg_replace('~\[quote[^\]]*\](.+?)\[\/quote\]~s', '', $body);
$matches = array();
$string = str_split($body);
$depth = 0;
foreach ($string as $k => $char)
{
if ($char == static::$char && ($k == 0 || trim($string[$k - 1]) == ''))
{
$depth++;
$matches[] = array();
}
elseif ($char == "\n")
$depth = 0;
for ($i = $depth; $i > 0; $i--)
{
if (count($matches[count($matches) - $i]) > 60)
{
$depth--;
continue;
}
$matches[count($matches) - $i][] = $char;
}
}
foreach ($matches as $k => $match)
$matches[$k] = substr(implode('', $match), 1);
$names = array();
foreach ($matches as $match)
{
$match = preg_split('/([^\w])/', $match, -1, PREG_SPLIT_DELIM_CAPTURE);
$count = count($match);
for ($i = 1; $i <= $count; $i++)
$names[] = $smcFunc['htmlspecialchars']($smcFunc['htmltrim'](implode('', array_slice($match, 0, $i))));
}
$names = array_unique($names);
return $names;
}
}
?>