base::generate_topic_link()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.2
cc 4
eloc 4
nc 4
nop 6
1
<?php
2
/**
3
 *
4
 * @package phpBB Extension - tas2580 SEO URLs
5
 * @copyright (c) 2016 tas2580 (https://tas2580.net)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace tas2580\seourls\event;
11
12
/**
13
 * Event listener
14
 */
15
class base
16
{
17
18
	/** @var \phpbb\auth\auth */
19
	protected $auth;
20
21
	/** @var \phpbb\config\config */
22
	public $config;
23
24
	/** @var string phpbb_root_path */
25
	protected $phpbb_root_path;
26
27
	/**
28
	 * Constructor
29
	 *
30
	 * @param \phpbb\auth\auth				auth					Authentication object
31
	 * @param \phpbb\config\config			$config					Config Object
32
	 * @param string						$phpbb_root_path		phpbb_root_path
33
	 * @access public
34
	 */
35
	public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, $phpbb_root_path)
36
	{
37
		$this->auth = $auth;
38
		$this->config = $config;
39
		$this->phpbb_root_path = $phpbb_root_path;
40
	}
41
42
	/**
43
	 * Generate the SEO link for a topic
44
	 *
45
	 * @param	int		$forum_id		The ID of the forum
46
	 * @param	string	$forum_name		The title of the forum
47
	 * @param	int		$topic_id		The ID if the topic
48
	 * @param	string	$topic_title	The title of the topic
49
	 * @param	int		$start			Optional start parameter
50
	 * @param	bool	$full			Return the full URL
51
	 * @return	string	The SEO URL
52
	 * @access private
53
	 */
54
	public function generate_topic_link($forum_id, $forum_name, $topic_id, $topic_title, $start = 0, $full = false)
55
	{
56
		if ($full)
57
		{
58
			return generate_board_url() . '/' . $this->title_to_url($forum_name) . '-f' . $forum_id . '/' . $this->title_to_url($topic_title) . '-t' . $topic_id . ($start ? '-s' . $start : '') . '.html';
59
		}
60
		return $this->phpbb_root_path . $this->title_to_url($forum_name) . '-f' . $forum_id . '/' . $this->title_to_url($topic_title) . '-t' . $topic_id . ($start ? '-s' . $start : '') . '.html';
61
	}
62
63
	/**
64
	 * Generate the SEO link for a forum
65
	 *
66
	 * @param	int		$forum_id		The ID of the forum
67
	 * @param	string	$forum_name		The title of the forum
68
	 * @param	int		$start			Optional start parameter
69
	 * @param	bool	$full			Return the full URL
70
	 * @return	string	The SEO URL
71
	 * @access private
72
	 */
73
	public function generate_forum_link($forum_id, $forum_name, $start = 0, $full = false)
74
	{
75
		if ($full)
76
		{
77
			return generate_board_url() . '/' . $this->title_to_url($forum_name) . '-f' . $forum_id . '/' . ($start ? 'index-s' . $start . '.html' : '');
78
		}
79
		return $this->phpbb_root_path . $this->title_to_url($forum_name) . '-f' . $forum_id . '/' . ($start ? 'index-s' . $start . '.html' : '');
80
	}
81
82
	/**
83
	 *
84
	 * @global	type	$_SID
85
	 * @param	int		$replies	Replays in the topic
86
	 * @param	string	$url		URL oft the topic
87
	 * @return	string				The URL with start included
88
	 */
89
	public function generate_lastpost_link($replies, $url)
90
	{
91
		$url = str_replace('.html', '', $url);
92
		$per_page = ($this->config['posts_per_page'] <= 0) ? 1 : $this->config['posts_per_page'];
93
		$last_post_link = '';
94
		if (($replies + 1) > $per_page)
95
		{
96
			for ($j = 0; $j < $replies + 1; $j += $per_page)
97
			{
98
				$last_post_link = $url . '-s' . $j . '.html';
99
			}
100
		}
101
		else
102
		{
103
			$last_post_link = $url . '.html';
104
		}
105
		return $last_post_link;
106
	}
107
108
	/**
109
	 * Replace letters to use title in URL
110
	 *
111
	 * @param	string	$title	The title to use in the URL
112
	 * @return	string	Title to use in URLs
113
	 */
114
	public static function title_to_url($title)
115
	{
116
		$url = strtolower(censor_text(utf8_normalize_nfc(html_entity_decode(strip_tags($title)))));
117
118
		// Let's replace
119
		$url_search = array(
120
		' ', 'í', 'ý', 'ß', 'ö', 'ô', 'ó', 'ò', 'ä', 'â', 'à', 'á', 'é', 'è', 'ü', 'ú', 'ù', 'ñ', 'ß', '²', '³', '@', '€', '$',
121
		'ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ż', 'ź', // polish letters
122
		'ç', 'ê', 'ë', 'ê', 'î', 'ï', 'œ', 'û', // french letters
123
		'ř', 'š', 'ž', 'ť', 'č', 'ý', 'ů', 'ě', 'ď', 'ň' //czech letters 
124
		);
125
		$url_replace = array(
126
		'-', 'i', 'y', 's', 'oe', 'o', 'o', 'o', 'ae', 'a', 'a', 'a', 'e', 'e', 'ue', 'u', 'u', 'n', 'ss', '2', '3', 'at', 'eur', 'usd',
127
		'a', 'c', 'e', 'l', 'n', 'o', 's', 'z', 'z', // polish letters
128
		'c', 'e', 'e', 'e', 'i', 'i', 'oe', 'u', // french letters
129
		'r', 's', 'z', 't', 'c', 'y', 'u', 'e', 'd', 'n' //czech letters
130
		);
131
		$url = str_replace($url_search, $url_replace, $url);
132
133
		$url = preg_replace('/[^\w\d]/', '-', $url);
134
		$url = preg_replace('/[-]{2,}/', '-', $url);
135
		$url = trim($url, '-');
136
137
		$url = substr($url, 0, 50); // Max length for a title in URL
138
		return urlencode($url);
139
	}
140
141
	/**
142
	 * Get the topics post count or the forums post/topic count based on permissions
143
	 *
144
	 * @param $mode            string    One of topic_posts, forum_posts or forum_topics
145
	 * @param $data            array    Array with the topic/forum data to calculate from
146
	 * @param $forum_id        int        The forum id is used for permission checks
147
	 * @return int    Number of posts/topics the user can see in the topic/forum
148
	 */
149
	public function get_count($mode, $data, $forum_id)
150
	{
151
		if (!$this->auth->acl_get('m_approve', $forum_id))
152
		{
153
			return (int) $data[$mode . '_approved'];
154
		}
155
156
		return (int) $data[$mode . '_approved'] + (int) $data[$mode . '_unapproved'] + (int) $data[$mode . '_softdeleted'];
157
	}
158
}
159