feed   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 49
c 2
b 0
f 0
dl 0
loc 126
ccs 0
cts 56
cp 0
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A render() 0 20 1
A __construct() 0 9 1
A get_response() 0 14 2
A prepare_content() 0 27 1
1
<?php
2
/**
3
 *
4
 * @package sitemaker
5
 * @copyright (c) 2017 Daniel A. (blitze)
6
 * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
7
 *
8
 */
9
10
namespace blitze\content\services;
11
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\HttpFoundation\Response;
14
15
class feed
16
{
17
	/** @var \phpbb\config\config */
18
	protected $config;
19
20
	/** @var \phpbb\controller\helper */
21
	protected $controller_helper;
22
23
	/** @var \phpbb\language\language */
24
	protected $language;
25
26
	/** @var \phpbb\symfony_request */
27
	protected $symfony_request;
28
29
	/** @var \phpbb\template\template */
30
	protected $template;
31
32
	/** @var \phpbb\user */
33
	protected $user;
34
35
	/** @var string */
36
	protected $php_ext;
37
38
	/** @var string */
39
	protected $board_url;
40
41
	/**
42
	 * Constructor
43
	 *
44
	 * @param \phpbb\config\config			$config					Config object
45
	 * @param \phpbb\controller\helper		$controller_helper		Controller Helper object
46
	 * @param \phpbb\language\language		$language				Language object
47
	 * @param \phpbb\symfony_request		$symfony_request		Symfony request
48
	 * @param \phpbb\template\template		$template				Template object
49
	 * @param \phpbb\user					$user					User object
50
	 * @param string						$php_ext				php file extension
51
	*/
52
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, \phpbb\language\language $language, \phpbb\symfony_request $symfony_request, \phpbb\template\template $template, \phpbb\user $user, $php_ext)
53
	{
54
		$this->config = $config;
55
		$this->controller_helper = $controller_helper;
56
		$this->language = $language;
57
		$this->symfony_request = $symfony_request;
58
		$this->template = $template;
59
		$this->user = $user;
60
		$this->php_ext = $php_ext;
61
	}
62
63
	/**
64
	 * @param int $max_update_time
65
	 * @return Response
66
	 */
67
	public function render($max_update_time)
68
	{
69
		$this->board_url = generate_board_url(true);
70
71
		$this->template->assign_vars(array(
72
			'BOARD_URL'				=> $this->board_url,
73
			'SELF_LINK'				=> $this->controller_helper->route($this->symfony_request->attributes->get('_route'), $this->symfony_request->attributes->get('_route_params'), true, '', UrlGeneratorInterface::ABSOLUTE_URL),
74
			'FEED_LINK'				=> $this->board_url . $this->user->page['script_path'] . 'index.' . $this->php_ext,
75
			'FEED_TITLE'			=> $this->config['sitename'],
76
			'FEED_SUBTITLE'			=> $this->config['site_desc'],
77
			'FEED_UPDATED'			=> $max_update_time,
78
			'FEED_LANG'				=> $this->language->lang('USER_LANG'),
79
			'FEED_AUTHOR'			=> $this->config['sitename'],
80
		));
81
82
		$this->template->set_filenames(array(
83
			'body'	=> 'feed.xml.twig',
84
		));
85
86
		return $this->get_response((string) $this->template->assign_display('body'), $max_update_time);
87
	}
88
89
	/**
90
	 * @param string $content
91
	 * @param int $max_update_time
92
	 * @return Response
93
	 */
94
	protected function get_response($content, $max_update_time)
95
	{
96
		$response = new Response($this->prepare_content($content));
97
		$response->headers->set('Content-Type', 'application/atom+xml');
98
		$response->setCharset('UTF-8');
99
		$response->setLastModified(new \DateTime('@' . $max_update_time));
100
101
		if (!empty($this->user->data['is_bot']))
102
		{
103
			// Let reverse proxies know we detected a bot.
104
			$response->headers->set('X-PHPBB-IS-BOT', 'yes');
105
		}
106
107
		return $response;
108
	}
109
110
	/**
111
	 * @param string $content
112
	 * @return string
113
	 */
114
	protected function prepare_content($content)
115
	{
116
		// convert relative urls to absolute urls
117
		$script_path = trim($this->user->page['script_path'], '/');
118
		$full_path = $this->board_url . '/' . $script_path;
119
		$content = preg_replace('/(href|src)=("|\')((?:\.*\/)+(?:' . $script_path . '\/)?)(.*?)("|\')/i', '$1=$2' . $full_path . '/$4$5', $content);
120
121
		// remove hidden field labels
122
		$content = preg_replace('#<div class="field-label label-hidden">(.*?)</div>#', '', $content);
123
124
		// remove session id
125
		$content = preg_replace('/((?:\?|&)sid=[a-z0-9]+)/', '', $content);
126
127
		// Remove Comments from inline attachments [ia]
128
		$content = preg_replace('#<dd>(.*?)</dd>#','',$content);
129
130
		// Replace some entities with their unicode counterpart
131
		$entities = array(
132
			'&nbsp;'	=> "\xC2\xA0",
133
			'&bull;'	=> "\xE2\x80\xA2",
134
			'&middot;'	=> "\xC2\xB7",
135
			'&copy;'	=> "\xC2\xA9",
136
		);
137
138
		$content = str_replace(array_keys($entities), array_values($entities), $content);
139
140
		return $content;
141
	}
142
}
143