Skip to content

Commit 1bc1acd

Browse files
committed
feat: handle a new case in fix tool
1 parent aacccfd commit 1bc1acd

File tree

1 file changed

+80
-10
lines changed

1 file changed

+80
-10
lines changed

inc/command/cleanticketscommand.class.php

Lines changed: 80 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {
5353

5454
$this->fixBadForm_1($input, $output);
5555
$this->fixBadForm_2($input, $output);
56+
$this->fixBadForm_3($input, $output);
5657

5758
$output->writeln('<info>Done.</info>');
5859
return 0;
@@ -62,8 +63,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
6263
* fix HTML tags double encoded
6364
* <p> => &lt;p&gt; => &#38;lt;p&#38;gt;
6465
*
65-
* @param InputInterface $input
66-
* @param OutputInterface $output
6766
* @return void
6867
*/
6968
protected function fixBadForm_1(InputInterface $input, OutputInterface $output) {
@@ -102,7 +101,7 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
102101
return 0;
103102
}
104103

105-
$output->write("<info>-> Found $count tickets to clean</info>");
104+
$output->write("<info>-> Found $count tickets to clean (double encoded < and > signs)</info>");
106105
$output->writeln("");
107106
$output->write("<info>-> Cleaning tickets...</info>");
108107
$output->writeln("");
@@ -130,10 +129,8 @@ protected function fixBadForm_1(InputInterface $input, OutputInterface $output)
130129
}
131130

132131
/**
133-
* remove HTML tag <br />
132+
* replace litteral HTML tag <br /> with &lt;br /&gt;
134133
*
135-
* @param InputInterface $input
136-
* @param OutputInterface $output
137134
* @return void
138135
*/
139136
protected function fixBadForm_2(InputInterface $input, OutputInterface $output) {
@@ -142,7 +139,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
142139
// Search tickets having HTML tags <br />
143140
$itemTicketTable = Item_Ticket::getTable();
144141
$ticketTable = Ticket::getTable();
145-
$pattern = 'br /';
142+
$pattern = '<br />';
146143
$tickets = $DB->request([
147144
'SELECT' => [$ticketTable => [Ticket::getIndexName(), 'content']],
148145
'FROM' => $ticketTable,
@@ -158,7 +155,7 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
158155
],
159156
],
160157
'WHERE' => [
161-
"$ticketTable.content" => ['LIKE', '%' . $pattern . '%'], // Matches bad encoding for '<'
158+
"$ticketTable.content" => ['LIKE', '%' . $pattern . '%'], // Matches bad encoding for 'br /'
162159
],
163160
]);
164161

@@ -169,16 +166,89 @@ protected function fixBadForm_2(InputInterface $input, OutputInterface $output)
169166
return 0;
170167
}
171168

172-
$output->write("<info>-> Found $count tickets to clean</info>");
169+
$output->write("<info>-> Found $count tickets to clean (literal BR tag)</info>");
173170
$output->writeln("");
174171
$output->write("<info>-> Cleaning tickets...</info>");
175172
$output->writeln("");
176173
foreach ($tickets as $row) {
177174
$pattern = [
178175
'<br />',
179176
];
177+
// Determine if we mlust use legacy or new encoding
178+
// @see Sanitizer::sanitize()
179+
if (strpos($row['content'], '&lt;') !== false && strpos($row['content'], '#60;') === false) {
180+
$replace = [
181+
'&lt;br /&gt;',
182+
];
183+
} else if (strpos($row['content'], '#60') !== false && strpos($row['content'], '&lt;') === false) {
184+
$replace = [
185+
'&#60;br /&#62;',
186+
];
187+
}
188+
$row['content'] = str_replace($pattern, $replace, $row['content']);
189+
// Direct write to the table to avoid alteration of other fields
190+
$DB->update(
191+
$ticketTable,
192+
[
193+
'content' => $DB->escape($row['content'])
194+
],
195+
[
196+
'id' => $row['id'],
197+
]
198+
);
199+
}
200+
}
201+
202+
/**
203+
* replace litteral HTML tag > with #38;
204+
* This may happen when a question gives the path to an item of a CommonTreeObject
205+
* entities, locations, ...
206+
*
207+
* @return void
208+
*/
209+
protected function fixBadForm_3(InputInterface $input, OutputInterface $output) {
210+
global $DB;
211+
212+
// Search tickets having HTML tags <br />
213+
$itemTicketTable = Item_Ticket::getTable();
214+
$ticketTable = Ticket::getTable();
215+
$pattern = ' > '; // greater than sign with a space before and after
216+
$tickets = $DB->request([
217+
'SELECT' => [$ticketTable => [Ticket::getIndexName(), 'content']],
218+
'FROM' => $ticketTable,
219+
'INNER JOIN' => [
220+
$itemTicketTable => [
221+
'FKEY' => [
222+
$ticketTable => Ticket::getIndexName(),
223+
$itemTicketTable => Ticket::getForeignKeyField(),
224+
],
225+
'AND' => [
226+
"$itemTicketTable.itemtype" => PluginFormcreatorFormAnswer::getType(),
227+
]
228+
],
229+
],
230+
'WHERE' => [
231+
"$ticketTable.content" => ['LIKE', '%' . $pattern . '%'],
232+
],
233+
]);
234+
235+
$count = $tickets->count();
236+
if ($count < 1) {
237+
$output->writeln('<info>-> No ticket to fix.</info>');
238+
$output->writeln("");
239+
return 0;
240+
}
241+
242+
$output->write("<info>-> Found $count tickets to clean (litteral > sign)</info>");
243+
$output->writeln("");
244+
$output->write("<info>-> Cleaning tickets...</info>");
245+
$output->writeln("");
246+
foreach ($tickets as $row) {
247+
$pattern = [
248+
' > ',
249+
];
180250
$replace = [
181-
'&lt;br /&gt;',
251+
' #38; ',
182252
];
183253
$row['content'] = str_replace($pattern, $replace, $row['content']);
184254
// Direct write to the table to avoid alteration of other fields

0 commit comments

Comments
 (0)