Completed
Push — develop ( c77227...1859c0 )
by Daniel
14:00
created

feed::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 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\symfony_request */
24
	protected $symfony_request;
25
26
	/** @var \phpbb\template\template */
27
	protected $template;
28
29
	/** @var \phpbb\user */
30
	protected $user;
31
32
	/** @var string */
33
	protected $php_ext;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \phpbb\config\config			$config					Config object
39
	 * @param \phpbb\controller\helper		$controller_helper		Controller Helper object
40
	 * @param \phpbb\symfony_request		$symfony_request		Symfony request
41
	 * @param \phpbb\template\template		$template				Template object
42
	 * @param \phpbb\user					$user					User object
43
	 * @param string						$php_ext				php file extension
44
	*/
45
	public function __construct(\phpbb\config\config $config, \phpbb\controller\helper $controller_helper, \phpbb\symfony_request $symfony_request, \phpbb\template\template $template, \phpbb\user $user, $php_ext)
46
	{
47
		$this->config = $config;
48
		$this->controller_helper = $controller_helper;
49
		$this->symfony_request = $symfony_request;
50
		$this->template = $template;
51
		$this->user = $user;
52
		$this->php_ext = $php_ext;
53
	}
54
55
	/**
56
	 * @param int $max_update_time
57
	 * @return Response
58
	 */
59
	public function render($max_update_time)
60
	{
61
		$this->board_url = generate_board_url(true);
0 ignored issues
show
Bug introduced by
The property board_url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
63
		$this->template->assign_vars(array(
64
			'BOARD_URL'				=> $this->board_url,
65
			'SELF_LINK'				=> $this->controller_helper->route($this->symfony_request->attributes->get('_route'), $this->symfony_request->attributes->get('_route_params'), true, '', UrlGeneratorInterface::ABSOLUTE_URL),
66
			'FEED_LINK'				=> $this->board_url . $this->user->page['script_path'] . 'index.' . $this->php_ext,
67
			'FEED_TITLE'			=> $this->config['sitename'],
68
			'FEED_SUBTITLE'			=> $this->config['site_desc'],
69
			'FEED_UPDATED'			=> $max_update_time,
70
			'FEED_LANG'				=> $this->user->lang['USER_LANG'],
71
			'FEED_AUTHOR'			=> $this->config['sitename'],
72
		));
73
74
		$this->template->set_filenames(array(
75
			'body'	=> 'feed.xml.twig',
76
		));
77
78
		return $this->get_response($this->template->assign_display('body'), $max_update_time);
79
	}
80
81
	/**
82
	 * @param string $content
83
	 * @param int $max_update_time
84
	 * @return string
85
	 */
86
	protected function get_response($content, $max_update_time)
87
	{
88
		$response = new Response($this->prepare_content($content));
89
		$response->headers->set('Content-Type', 'application/atom+xml');
90
		$response->setCharset('UTF-8');
91
		$response->setLastModified(new \DateTime('@' . $max_update_time));
92
93
		if (!empty($this->user->data['is_bot']))
94
		{
95
			// Let reverse proxies know we detected a bot.
96
			$response->headers->set('X-PHPBB-IS-BOT', 'yes');
97
		}
98
99
		return $response;
100
	}
101
102
	/**
103
	 * @param string $content
104
	 * @return string
105
	 */
106
	protected function prepare_content($content)
107
	{
108
		// convert relative urls to absolute urls
109
		$script_path = trim($this->user->page['script_path'], '/');
110
		$full_path = $this->board_url . '/' . $script_path;
111
		$content = preg_replace('/(href|src)=("|\')((?:\.*\/)+(?:' . $script_path . '\/)?)(.*?)("|\')/i', '$1=$2' . $full_path . '/$4$5', $content);
112
113
		// remove hidden field labels
114
		$content = preg_replace('#<div class="field-label label-hidden">(.*?)</div>#', '', $content);
115
116
		// remove session id
117
		$content = preg_replace('/((?:\?|&)sid=[a-z0-9]+)/', '', $content);
118
119
		// Remove Comments from inline attachments [ia]
120
		$content = preg_replace('#<dd>(.*?)</dd>#','',$content);
121
122
		// Replace some entities with their unicode counterpart
123
		$entities = array(
124
			'&nbsp;'	=> "\xC2\xA0",
125
			'&bull;'	=> "\xE2\x80\xA2",
126
			'&middot;'	=> "\xC2\xB7",
127
			'&copy;'	=> "\xC2\xA9",
128
		);
129
130
		$content = str_replace(array_keys($entities), array_values($entities), $content);
131
132
		return $content;
133
	}
134
}
135