/
listener.php
122 lines (107 loc) · 3.1 KB
/
listener.php
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
<?php
/**
*
* @package SEO Meta Description
* @copyright (c) 2014 Anvar [apwa.ru]
* @link http://bb3.mobi
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
*
*/
namespace bb3mobi\seodesc\event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class listener implements EventSubscriberInterface
{
/** @var \phpbb\template\template */
protected $template;
/** @var \phpbb\config\config */
protected $config;
const MAX_LENGHT = 250;
public function __construct(\phpbb\template\template $template, \phpbb\config\config $config)
{
$this->template = $template;
$this->config = $config;
}
static public function getSubscribedEvents()
{
return array(
'core.display_forums_modify_forum_rows' => 'forumlist_description',
'core.viewforum_get_topic_data' => 'viewforum_description',
'core.viewtopic_modify_post_data' => 'viewtopic_description',
);
}
public function forumlist_description($event)
{
if ($this->config['meta_description'])
{
$this->template->assign_var('DESCRIPTION', $this->config['meta_description']);
}
else
{
$forum_desc = '';
$row = $event['row'];
$forum_rows = $event['forum_rows'];
foreach ($forum_rows as $row)
{
/** No forum type category */
if ($row['forum_type'] != FORUM_CAT)
{
$forum_desc .= ($forum_desc) ? ', ' . $row['forum_name'] : $row['forum_name'];
}
}
if ($forum_desc)
{
if (mb_strlen($forum_desc) > self::MAX_LENGHT)
{
$forum_desc = mb_substr($forum_desc, 0, self::MAX_LENGHT) . '..';
}
$this->template->assign_var('DESCRIPTION', trim($forum_desc));
}
}
}
public function viewforum_description($event)
{
$forum_data = $event['forum_data'];
if (!empty($forum_data['forum_desc']))
{
$forum_desc = $this->strip_code($forum_data['forum_desc']);
if (mb_strlen($forum_desc) > self::MAX_LENGHT)
{
$forum_desc = mb_substr($forum_desc, 0, self::MAX_LENGHT) . '..';
}
$this->template->assign_var('DESCRIPTION', trim($forum_desc));
}
}
public function viewtopic_description($event)
{
$topic_desc = '';
$rowset = $event['rowset'];
$post_list = $event['post_list'];
for ($i = 0, $end = sizeof($post_list); $i < $end; ++$i)
{
// A non-existing rowset only happens if there was no user present for the entered poster_id
// This could be a broken posts table.
if (!isset($rowset[$post_list[$i]]))
{
continue;
}
$row = $rowset[$post_list[$i]];
$topic_desc = $this->strip_code($row['post_text']);
unset($rowset[$post_list[$i]]);
break;
}
// Limit chars
if (mb_strlen($topic_desc) >= self::MAX_LENGHT)
{
$topic_desc = mb_substr($topic_desc, 0, self::MAX_LENGHT) . '..';
}
$this->template->assign_var('DESCRIPTION', trim($topic_desc));
}
private function strip_code($text)
{
$text = censor_text($text);
strip_bbcode($text);
$text = str_replace(array(""", "/", "\n", "\t", "\r"), ' ', $text);
$text = preg_replace(array("|http(.*)jpg|isU", "@(http(s)?://)?(([a-z0-9.-]+)?[a-z0-9-]+(!?\.[a-z]{2,4}))@"), ' ', $text);
return preg_replace("/[^A-ZА-ЯЁ.,-–?]+/ui", " ", $text);
}
}