Test Setup Failed
Push — master ( fcc889...2029c0 )
by Tobias
02:00
created

listener::getSubscribedEvents()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 18
Bugs 5 Features 0
Metric Value
c 18
b 5
f 0
dl 0
loc 24
rs 8.9713
cc 1
eloc 21
nc 1
nop 0
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
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14
/**
15
 * Event listener
16
 */
17
class listener implements EventSubscriberInterface
18
{
19
	/** @var \tas2580\seourls\event\base */
20
	protected $base;
21
22
	/** @var \phpbb\template\template */
23
	protected $template;
24
25
	/** @var \phpbb\request\request */
26
	protected $request;
27
28
	/** @var \phpbb\path_helper */
29
	protected $path_helper;
30
31
	/** @var string phpbb_root_path */
32
	protected $phpbb_root_path;
33
34
	/** @var string php_ext */
35
	protected $php_ext;
36
37
	/**
38
	 * Constructor
39
	 *
40
	 * @param \tas2580\seourls\event\base	$base
41
	 * @param \phpbb\template\template		$template				Template object
42
	 * @param \phpbb\request\request		$request				Request object
43
	 * @param \phpbb\path_helper			$path_helper			Controller helper object
44
	 * @param string						$phpbb_root_path		phpbb_root_path
45
	 * @param string						$php_ext				php_ext
46
	 * @access public
47
	 */
48
	public function __construct(\tas2580\seourls\event\base $base, \phpbb\template\template $template, \phpbb\request\request $request, \phpbb\path_helper $path_helper, $phpbb_root_path, $php_ext)
49
	{
50
		$this->base = $base;
51
		$this->template = $template;
52
		$this->request = $request;
53
		$this->path_helper = $path_helper;
54
		$this->phpbb_root_path = $phpbb_root_path;
55
		$this->php_ext = $php_ext;
56
57
		$this->in_viewtopic = false;
58
	}
59
60
	/**
61
	 * Assign functions defined in this class to event listeners in the core
62
	 *
63
	 * @return array
64
	 * @static
65
	 * @access public
66
	 */
67
	public static function getSubscribedEvents()
68
	{
69
		return array(
70
			'core.append_sid'										=> 'append_sid',
71
			'core.display_forums_modify_sql'						=> 'display_forums_modify_sql',
72
			'core.display_forums_modify_template_vars'				=> 'display_forums_modify_template_vars',
73
			'core.display_forums_modify_forum_rows'					=> 'display_forums_modify_forum_rows',
74
			'core.display_forums_modify_category_template_vars'		=> 'display_forums_modify_category_template_vars',
75
			'core.generate_forum_nav'								=> 'generate_forum_nav',
76
			'core.make_jumpbox_modify_tpl_ary'						=> 'make_jumpbox_modify_tpl_ary',				// Not in phpBB
77
			'core.memberlist_view_profile'							=> 'memberlist_view_profile',
78
			'core.pagination_generate_page_link'					=> 'pagination_generate_page_link',
79
			'core.search_modify_tpl_ary'							=> 'search_modify_tpl_ary',
80
			'core.viewforum_modify_topicrow'						=> 'viewforum_modify_topicrow',
81
			'core.viewforum_get_topic_data'							=> 'viewforum_get_topic_data',
82
			'core.viewforum_get_shadowtopic_data'					=> 'viewforum_get_shadowtopic_data',
83
			'core.viewtopic_assign_template_vars_before'			=> 'viewtopic_assign_template_vars_before',
84
			'core.viewtopic_before_f_read_check'					=> 'viewtopic_before_f_read_check',
85
			'core.viewtopic_modify_page_title'						=> 'viewtopic_modify_page_title',
86
			'core.viewtopic_modify_post_row'						=> 'viewtopic_modify_post_row',
87
			'core.viewtopic_get_post_data'							=> 'viewtopic_get_post_data',
88
			'core.parse_attachments_modify_template_data'			=> 'parse_attachments_modify_template_data',
89
		);
90
	}
91
92
	/**
93
	 * Correct the path of $viewtopic_url
94
	 *
95
	 * @param	object	$event	The event object
96
	 * @return	null
97
	 * @access	public
98
	 */
99
	public function append_sid($event)
100
	{
101
		if ($this->in_viewtopic && preg_match('#./../viewtopic.' . $this->php_ext  . '#', $event['url']))
102
		{
103
			$url = '../viewtopic.' . $this->php_ext ;
104
			$event['url'] = $url;
105
		}
106
		if (isset($event['params']['redirect']))
107
		{
108
			$params = $event['params'];
109
			$params['redirect'] = str_replace('..', '.', $event['params']['redirect']);
110
			$event['params'] = $params;
111
		}
112
	}
113
114
	/**
115
	 * Get informations for the last post from Database
116
	 *
117
	 * @param	object	$event	The event object
118
	 * @return	null
119
	 * @access	public
120
	 */
121 View Code Duplication
	public function display_forums_modify_sql($event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
122
	{
123
		$sql_array = $event['sql_ary'];
124
		$sql_array['LEFT_JOIN'][] = array(
125
			'FROM' => array(TOPICS_TABLE => 't'),
126
			'ON' => "f.forum_last_post_id = t.topic_last_post_id"
127
		);
128
		$sql_array['SELECT'] .= ', t.topic_title, t.topic_id, t.topic_posts_approved, t.topic_posts_unapproved, t.topic_posts_softdeleted';
129
		$event['sql_ary'] = $sql_array;
130
	}
131
132
	/**
133
	 * Store informations for the last post in forum_rows array
134
	 *
135
	 * @param	object	$event	The event object
136
	 * @return	null
137
	 * @access	public
138
	 */
139
	public function display_forums_modify_forum_rows($event)
140
	{
141
		$forum_rows = $event['forum_rows'];
142
		if ($event['row']['forum_last_post_time'] == $forum_rows[$event['parent_id']]['forum_last_post_time'])
143
		{
144
			$forum_rows[$event['parent_id']]['forum_name_last_post'] =$event['row']['forum_name'];
145
			$forum_rows[$event['parent_id']]['topic_id_last_post'] =$event['row']['topic_id'];
146
			$forum_rows[$event['parent_id']]['topic_title_last_post'] =$event['row']['topic_title'];
147
			$event['forum_rows'] = $forum_rows;
148
		}
149
	}
150
151
	/**
152
	 * Rewrite links to forums and subforums in forum index
153
	 * also correct the path of the forum images if we are in a forum
154
	 *
155
	 * @param	object	$event	The event object
156
	 * @return	null
157
	 * @access	public
158
	 */
159
	public function display_forums_modify_template_vars($event)
160
	{
161
		$subforums_row = $event['subforums_row'];
162
		$forum_row = $event['forum_row'];
163
164
		// Rewrite URLs of sub forums
165
		foreach ($subforums_row as $i => $subforum)
166
		{
167
			// A little bit a dirty way, but there is no better solution
168
			$query = str_replace('&amp;', '&', parse_url($subforum['U_SUBFORUM'], PHP_URL_QUERY));
169
			parse_str($query, $id);
170
			$subforums_row[$i]['U_SUBFORUM'] = append_sid($this->base->generate_forum_link($id['f'], $subforum['SUBFORUM_NAME']));
171
		}
172
173
		// Update the image source in forums
174
		$img = $this->path_helper->update_web_root_path($forum_row['FORUM_IMAGE_SRC']);
175
		$forum_row['FORUM_IMAGE'] = preg_replace('#img src=\"(.*)\" alt#', 'img src="' . $img . '" alt', $forum_row['FORUM_IMAGE']);
176
177
		// Rewrite links to topics, posts and forums
178
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
179
		$url = $this->base->generate_topic_link($event['row']['forum_id_last_post'], $event['row']['forum_name_last_post'], $event['row']['topic_id_last_post'], $event['row']['topic_title_last_post']);
180
		$forum_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $url) . '#p' . $event['row']['forum_last_post_id']);
181
		$forum_row['U_VIEWFORUM'] = append_sid($this->base->generate_forum_link($forum_row['FORUM_ID'], $forum_row['FORUM_NAME']));
182
		$forum_row['U_NEWEST_POST'] = $url . '?view=unread#unread';
183
184
		$event['subforums_row'] = $subforums_row;
185
		$event['forum_row'] = $forum_row;
186
	}
187
188
	/**
189
	 * Rewrite the categorie links
190
	 *
191
	 * @param	object	$event	The event object
192
	 * @return	null
193
	 * @access	public
194
	 */
195
	public function display_forums_modify_category_template_vars($event)
196
	{
197
		$cat_row = $event['cat_row'];
198
		$row = $event['row'];
199
		$cat_row['U_VIEWFORUM'] = append_sid($this->base->generate_forum_link($row['forum_id'], $row['forum_name']));
200
		$event['cat_row'] = $cat_row;
201
	}
202
203
	/**
204
	 * Rewrite links in breadcrumbs
205
	 *
206
	 * @param	object	$event	The event object
207
	 * @return	null
208
	 * @access	public
209
	 */
210
	public function generate_forum_nav($event)
211
	{
212
		$forum_data = $event['forum_data'];
213
		$navlinks = $event['navlinks'];
214
		$navlinks_parents = $event['navlinks_parents'];
215
216
		foreach ($navlinks_parents as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
217
		{
218
			$navlinks_parents[$id]['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($data['FORUM_ID'] , $data['FORUM_NAME']));
219
		}
220
221
		$navlinks['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($forum_data['forum_id'], $forum_data['forum_name']));
222
		$event['navlinks'] = $navlinks;
223
		$event['navlinks_parents'] = $navlinks_parents;
224
	}
225
226
	// Not in phpBB
227
	public function make_jumpbox_modify_tpl_ary($event)
228
	{
229
		$tpl_ary = $event['tpl_ary'];
230
		$row = $event['row'];
231
		foreach ($tpl_ary as $id => $data)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
232
		{
233
			$tpl_ary[$id]['LINK']	 = append_sid($this->base->generate_forum_link($row['forum_id'], $row['forum_name']));
234
		}
235
236
		$event['tpl_ary'] = $tpl_ary;
237
	}
238
239
	/**
240
	 * Rewrite links to most active forum and topic on profile page
241
	 *
242
	 * @param	object	$event	The event object
243
	 * @return	null
244
	 * @access	public
245
	 */
246
	public function memberlist_view_profile($event)
247
	{
248
		$data = $event['member'];
249
		$this->template->assign_vars(array(
250
			'U_ACTIVE_FORUM' => $this->base->generate_forum_link($data['active_f_row']['forum_id'], $data['active_f_row']['forum_name'], 0, true),
251
			'U_ACTIVE_TOPIC' => $this->base->generate_topic_link($data['active_f_row']['forum_id'], $data['active_f_row']['forum_name'], $data['active_t_row']['topic_id'], $data['active_t_row']['topic_title'], 0, true),
252
		));
253
	}
254
255
	/**
256
	 * Rewrite pagination links
257
	 *
258
	 * @param	object	$event	The event object
259
	 * @return	null
260
	 * @access	public
261
	 */
262
	public function pagination_generate_page_link($event)
263
	{
264
		if(!is_string($event['base_url']))
265
		{
266
			return;
267
		}
268
		// If we have a sort key we do not rewrite the URL
269
		$query = str_replace('&amp;', '&', parse_url($event['base_url'], PHP_URL_QUERY));
270
		parse_str($query, $param);
271
		if (isset($param['sd']) || isset($param['sk']) || isset($param['st']))
272
		{
273
			return;
274
		}
275
276
		$start = (($event['on_page'] - 1) * $event['per_page']);
277
		if (!empty($this->topic_data) && isset($param['f']) && isset($param['t']))
278
		{
279
			$event['generate_page_link_override'] = append_sid($this->base->generate_topic_link($this->topic_data['forum_id'], $this->topic_data['forum_name'], $this->topic_data['topic_id'], $this->topic_data['topic_title'], $start));
280
		}
281
		else if (!empty($this->forum_data) && isset($param['f']))
282
		{
283
			$event['generate_page_link_override'] = append_sid($this->base->generate_forum_link($this->forum_data['forum_id'], $this->forum_data['forum_name'], $start));
284
		}
285
	}
286
287
	/**
288
	 * Rewrite links in the search result
289
	 *
290
	 * @param	object	$event	The event object
291
	 * @return	null
292
	 * @access	public
293
	 */
294
	public function search_modify_tpl_ary($event)
295
	{
296
		$replies = $this->base->get_count('topic_posts', $event['row'], $event['row']['forum_id']) - 1;
297
		$u_view_topic = $this->base->generate_topic_link($event['row']['forum_id'], $event['row']['forum_name'], $event['row']['topic_id'], $event['row']['topic_title']);
298
299
		$tpl_ary = $event['tpl_ary'];
300
		$tpl_ary['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($replies, $u_view_topic) . '#p' . $event['row']['topic_last_post_id']);
301
		$tpl_ary['U_VIEW_TOPIC'] = append_sid($u_view_topic);
302
		$tpl_ary['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($event['row']['forum_id'], $event['row']['forum_name']));
303
		$tpl_ary['U_NEWEST_POST'] = $u_view_topic . '?view=unread#unread';
304
305
		$event['tpl_ary'] = $tpl_ary;
306
	}
307
308
	/**
309
	 * Rewrite links to topics in forum view
310
	 *
311
	 * @param	object	$event	The event object
312
	 * @return	null
313
	 * @access	public
314
	 */
315
	public function viewforum_modify_topicrow($event)
316
	{
317
		$topic_row = $event['topic_row'];
318
		$row = $event['row'];
319
320
		// assign to be used in pagination_generate_page_link
321
		$this->topic_data = array(
322
			'forum_id' => $row['forum_id'],
323
			'forum_name' => $topic_row['FORUM_NAME'],
324
			'topic_id' => $row['topic_id'],
325
			'topic_title' => $row['topic_title']
326
		);
327
328
		$u_view_topic = $this->base->generate_topic_link($row['forum_id'], $topic_row['FORUM_NAME'], $row['topic_id'], $row['topic_title']);
329
		$topic_row['U_VIEW_TOPIC'] = append_sid($u_view_topic);
330
		$topic_row['U_VIEW_FORUM'] = append_sid($this->base->generate_forum_link($row['forum_id'], $topic_row['FORUM_NAME']));
331
		$topic_row['U_LAST_POST'] = append_sid($this->base->generate_lastpost_link($event['topic_row']['REPLIES'], $u_view_topic) . '#p' . $row['topic_last_post_id']);
332
		$topic_row['U_NEWEST_POST'] = $u_view_topic . '?view=unread#unread';
333
334
		$event['topic_row'] = $topic_row;
335
	}
336
337
	/**
338
	 * Get forum_name for shaddow topics
339
	 *
340
	 * @param	object	$event	The event object
341
	 * @return	null
342
	 * @access	public
343
	 */
344 View Code Duplication
	public function viewforum_get_shadowtopic_data($event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
345
	{
346
		$sql_array = $event['sql_array'];
347
		$sql_array['SELECT'] .= ', f.forum_name';
348
		$sql_array['LEFT_JOIN'][] = array('FROM' => array(FORUMS_TABLE => 'f'), 'ON' => 'f.forum_id = t.forum_id');
349
		$event['sql_array'] = $sql_array;
350
	}
351
352
	/**
353
	 * Rewrite the canonical URL on viewforum.php
354
	 *
355
	 * @param	object	$event	The event object
356
	 * @return	null
357
	 * @access	public
358
	 */
359
	public function viewforum_get_topic_data($event)
360
	{
361
		// assign to be used in pagination_generate_page_link
362
		$this->forum_data = array(
363
			'forum_id' => $event['forum_data']['forum_id'],
364
			'forum_name' => $event['forum_data']['forum_name']
365
		);
366
367
		$start = $this->request->variable('start', 0);
368
		$this->template->assign_vars(array(
369
			'U_VIEW_FORUM'		=> append_sid($this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start)),
370
			'U_CANONICAL'		=> $this->base->generate_forum_link($event['forum_data']['forum_id'], $event['forum_data']['forum_name'], $start, true),
371
		));
372
	}
373
374
	/**
375
	 * Rewrite the topic URL for the headline of the topic page and the link back to forum
376
	 *
377
	 * @param	object	$event	The event object
378
	 * @return	null
379
	 * @access	public
380
	 */
381
	public function viewtopic_get_post_data($event)
382
	{
383
		$data = $event['topic_data'];
384
		$this->template->assign_vars(array(
385
			'U_VIEW_TOPIC'		=> append_sid($this->base->generate_topic_link($event['forum_id'] , $data['forum_name'], $event['topic_id'], $data['topic_title'], $event['start'])),
386
			'U_VIEW_FORUM'		=> append_sid($this->base->generate_forum_link($event['forum_id'] , $data['forum_name'])),
387
			'S_POLL_ACTION'		=> append_sid($this->base->generate_topic_link($event['forum_id'] , $data['forum_name'], $event['topic_id'], $data['topic_title'], $event['start'])),
388
		));
389
	}
390
391
	/**
392
	 * Assign topic data to global variables for pagination
393
	 *
394
	 * @param	object	$event	The event object
395
	 * @return	null
396
	 * @access	public
397
	 */
398
	public function viewtopic_assign_template_vars_before($event)
399
	{
400
		// assign to be used in pagination_generate_page_link
401
		$this->topic_data = array(
402
			'forum_id' => $event['topic_data']['forum_id'],
403
			'forum_name' => $event['topic_data']['forum_name'],
404
			'topic_id' => $event['topic_data']['topic_id'],
405
			'topic_title' => $event['topic_data']['topic_title']
406
		);
407
	}
408
409
	public function viewtopic_before_f_read_check()
410
	{
411
		$this->in_viewtopic = true;
412
	}
413
414
	/**
415
	 * Rewrite the canonical URL on viewtopic.php
416
	 *
417
	 * @param	object	$event	The event object
418
	 * @return	null
419
	 * @access	public
420
	 */
421
	public function viewtopic_modify_page_title($event)
422
	{
423
		$start = $this->request->variable('start', 0);
424
		$data = $event['topic_data'];
425
		$this->template->assign_vars(array(
426
			'U_CANONICAL'		=> $this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start, true),
427
		));
428
	}
429
430
	/**
431
	 * Rewrite mini post img link
432
	 *
433
	 * @param	object	$event	The event object
434
	 * @return	null
435
	 * @access	public
436
	 */
437
	public function viewtopic_modify_post_row($event)
438
	{
439
		$row = $event['post_row'];
440
		$start = $this->request->variable('start', 0);
441
		$data = $event['topic_data'];
442
		$row['U_MINI_POST'] = append_sid($this->base->generate_topic_link($data['forum_id'], $data['forum_name'], $data['topic_id'], $data['topic_title'], $start) . '#p' . $event['row']['post_id']);
443
		$event['post_row'] = $row;
444
	}
445
446
	/**
447
	 * Correct path of upload icons
448
	 *
449
	 * @param	object	$event	The event object
450
	 * @return	null
451
	 * @access	public
452
	 */
453
	public function parse_attachments_modify_template_data($event)
454
	{
455
		if (isset($event['extensions'][$event['attachment']['extension']]))
456
		{
457
			if ($event['extensions'][$event['attachment']['extension']]['upload_icon'])
458
			{
459
				$block_array = $event['block_array'];
460
				$upload_icon = '<img src="' . generate_board_url() . '/' . $this->base->config['upload_icons_path'] . '/' . trim($event['extensions'][$event['attachment']['extension']]['upload_icon']) . '" alt="" />';
461
				$block_array['UPLOAD_ICON'] = $upload_icon;
462
				$event['block_array'] = $block_array;
463
			}
464
		}
465
	}
466
}
467