Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
13.04% |
6 / 46 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
helper | |
13.04% |
6 / 46 |
|
20.00% |
1 / 5 |
62.26 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
get_board_url | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
append_sid | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
format_date | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
generate_content | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\feed; |
15 | |
16 | use phpbb\config\config; |
17 | use phpbb\path_helper; |
18 | use phpbb\textformatter\s9e\renderer; |
19 | use phpbb\user; |
20 | use phpbb\auth\auth; |
21 | use Symfony\Component\DependencyInjection\ContainerInterface; |
22 | |
23 | /** |
24 | * Class with some helpful functions used in feeds |
25 | */ |
26 | class helper |
27 | { |
28 | /** @var config */ |
29 | protected $config; |
30 | |
31 | /** @var ContainerInterface */ |
32 | protected $container; |
33 | |
34 | /** @var path_helper */ |
35 | protected $path_helper; |
36 | |
37 | /** @var renderer */ |
38 | protected $renderer; |
39 | |
40 | /** @var user */ |
41 | protected $user; |
42 | |
43 | /** @var auth */ |
44 | protected $auth; |
45 | |
46 | /** |
47 | * Constructor |
48 | * |
49 | * @param auth $auth Auth object |
50 | * @param config $config Config object |
51 | * @param ContainerInterface $container Service container object |
52 | * @param path_helper $path_helper Path helper object |
53 | * @param renderer $renderer TextFormatter renderer object |
54 | * @param user $user User object |
55 | */ |
56 | public function __construct(auth $auth, config $config, ContainerInterface $container, path_helper $path_helper, renderer $renderer, user $user) |
57 | { |
58 | $this->auth = $auth; |
59 | $this->config = $config; |
60 | $this->container = $container; |
61 | $this->path_helper = $path_helper; |
62 | $this->renderer = $renderer; |
63 | $this->user = $user; |
64 | } |
65 | |
66 | /** |
67 | * Returns the board url (and caches it in the function) |
68 | */ |
69 | public function get_board_url() |
70 | { |
71 | static $board_url; |
72 | |
73 | if (empty($board_url)) |
74 | { |
75 | $board_url = generate_board_url(); |
76 | } |
77 | |
78 | return $board_url; |
79 | } |
80 | |
81 | /** |
82 | * Run links through append_sid(), prepend generate_board_url() and remove session id |
83 | */ |
84 | public function append_sid($url, $params) |
85 | { |
86 | return append_sid($this->get_board_url() . '/' . $url, $params, true, ''); |
87 | } |
88 | |
89 | /** |
90 | * Generate ISO 8601 date string (RFC 3339) |
91 | */ |
92 | public function format_date($time) |
93 | { |
94 | static $zone_offset; |
95 | static $offset_string; |
96 | |
97 | if (empty($offset_string)) |
98 | { |
99 | $zone_offset = $this->user->create_datetime()->getOffset(); |
100 | $offset_string = phpbb_format_timezone_offset($zone_offset, true); |
101 | } |
102 | |
103 | return gmdate("Y-m-d\TH:i:s", $time + $zone_offset) . $offset_string; |
104 | } |
105 | |
106 | /** |
107 | * Generate text content |
108 | * |
109 | * @param string $content is feed text content |
110 | * @param string $uid is bbcode_uid |
111 | * @param string $bitfield is bbcode bitfield |
112 | * @param int $options bbcode flag options |
113 | * @param int $forum_id is the forum id |
114 | * @param array $post_attachments is an array containing the attachments and their respective info |
115 | * @return string the html content to be printed for the feed |
116 | */ |
117 | public function generate_content($content, $uid, $bitfield, $options, $forum_id, $post_attachments) |
118 | { |
119 | if (empty($content)) |
120 | { |
121 | return ''; |
122 | } |
123 | |
124 | // Setup our own quote_helper to remove all attributes from quotes |
125 | $this->renderer->configure_quote_helper($this->container->get('feed.quote_helper')); |
126 | |
127 | $this->renderer->set_smilies_path($this->get_board_url() . '/' . $this->config['smilies_path']); |
128 | |
129 | $this->renderer->configure_user($this->user, $this->config, $this->auth); |
130 | |
131 | $content = generate_text_for_display($content, $uid, $bitfield, $options); |
132 | |
133 | // Remove "Select all" link and mouse events |
134 | $content = str_replace('<a href="#" onclick="selectCode(this); return false;">' . $this->user->lang['SELECT_ALL_CODE'] . '</a>', '', $content); |
135 | $content = preg_replace('#(onkeypress|onclick)="(.*?)"#si', '', $content); |
136 | |
137 | // Firefox does not support CSS for feeds, though |
138 | |
139 | // Remove font sizes |
140 | // $content = preg_replace('#<span style="font-size: [0-9]+%; line-height: [0-9]+%;">([^>]+)</span>#iU', '\1', $content); |
141 | |
142 | // Make text strong :P |
143 | // $content = preg_replace('#<span style="font-weight: bold?">(.*?)</span>#iU', '<strong>\1</strong>', $content); |
144 | |
145 | // Italic |
146 | // $content = preg_replace('#<span style="font-style: italic?">([^<]+)</span>#iU', '<em>\1</em>', $content); |
147 | |
148 | // Underline |
149 | // $content = preg_replace('#<span style="text-decoration: underline?">([^<]+)</span>#iU', '<u>\1</u>', $content); |
150 | |
151 | // Remove embed Windows Media Streams |
152 | $content = preg_replace( '#<\!--\[if \!IE\]>-->([^[]+)<\!--<!\[endif\]-->#si', '', $content); |
153 | |
154 | // Do not use < and >, because we want to retain code contained in [code][/code] |
155 | |
156 | // Remove embed and objects |
157 | $content = preg_replace( '#<(object|embed)(.*?) (value|src)=(.*?) ([^[]+)(object|embed)>#si',' <a href=$4 target="_blank"><strong>$1</strong></a> ',$content); |
158 | |
159 | // Remove some specials html tag, because somewhere there are a mod to allow html tags ;) |
160 | $content = preg_replace( '#<(script|iframe)([^[]+)\1>#siU', ' <strong>$1</strong> ', $content); |
161 | |
162 | // Parse inline images to display with the feed |
163 | if (!empty($post_attachments)) |
164 | { |
165 | $update_count = array(); |
166 | parse_attachments($forum_id, $content, $post_attachments, $update_count); |
167 | $content .= implode('<br />', $post_attachments); |
168 | |
169 | // Convert attachments' relative path to absolute path |
170 | $pattern = '#(/app.php)?/download/attachment/#'; |
171 | $replacement = $this->get_board_url() . '\1/download/attachment/'; |
172 | $content = preg_replace($pattern, $replacement, $content); |
173 | } |
174 | |
175 | // Remove Comments from inline attachments [ia] |
176 | $content = preg_replace('#<dd>(.*?)</dd>#','',$content); |
177 | |
178 | // Replace some entities with their unicode counterpart |
179 | $entities = array( |
180 | ' ' => "\xC2\xA0", |
181 | '•' => "\xE2\x80\xA2", |
182 | '·' => "\xC2\xB7", |
183 | '©' => "\xC2\xA9", |
184 | ); |
185 | |
186 | $content = str_replace(array_keys($entities), array_values($entities), $content); |
187 | |
188 | // Remove CDATA blocks. ;) |
189 | $content = preg_replace('#\<\!\[CDATA\[(.*?)\]\]\>#s', '', $content); |
190 | |
191 | // Other control characters |
192 | $content = preg_replace('#(?:[\x00-\x1F\x7F]+|(?:\xC2[\x80-\x9F])+)#', '', $content); |
193 | |
194 | return $content; |
195 | } |
196 | } |