Skip to content

Commit 40e3968

Browse files
committed
refactored submit-log comment_format evaluation, and
fixed submitting OCPL html format logs
1 parent b7e8a5b commit 40e3968

File tree

2 files changed

+70
-74
lines changed

2 files changed

+70
-74
lines changed

okapi/services/logs/submit.php

Lines changed: 67 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -258,41 +258,57 @@ private static function _call(OkapiRequest $request)
258258
# Prepare our comment to be inserted into the database. This may require
259259
# some reformatting which depends on the current OC installation.
260260

261-
if (Settings::get('OC_BRANCH') == 'oc.de')
261+
# OC sites store all comments in HTML format, while the 'text_html' field
262+
# indicates their *original* format as delivered by the user. This
263+
# allows processing the 'text' field contents without caring about the
264+
# original format, while still being able to re-create the comment in
265+
# its original form.
266+
267+
if ($comment_format == 'plaintext')
262268
{
263-
# OCDE stores all comments in HTML format, while the 'text_html' field
264-
# indicates their *original* format as delivered by the user. This
265-
# allows processing the 'text' field contents without caring about the
266-
# original format, while still being able to re-create the comment in
267-
# its original form. It requires us to HTML-encode plaintext comments
268-
# and to indicate this by setting 'html_text' to FALSE.
269-
#
270-
# For user-supplied HTML comments, OCDE requires us to do additional
271-
# HTML purification prior to the insertion into the database.
272-
273-
if ($comment_format == 'plaintext')
274-
{
275-
# This code is identical to the plaintext processing in OCDE code,
276-
# including a space handling bug: Multiple consecutive spaces will
277-
# get semantically lost in the generated HTML.
269+
# This code is identical to the plaintext processing in OC code,
270+
# including a space handling bug: Multiple consecutive spaces will
271+
# get semantically lost in the generated HTML.
278272

279-
$formatted_comment = htmlspecialchars($comment, ENT_COMPAT);
280-
$formatted_comment = nl2br($formatted_comment);
273+
$formatted_comment = htmlspecialchars($comment, ENT_COMPAT);
274+
$formatted_comment = nl2br($formatted_comment);
275+
276+
if (Settings::get('OC_BRANCH') == 'oc.de')
277+
{
281278
$value_for_text_html_field = 0;
282279
}
283280
else
284281
{
285-
if ($comment_format == 'auto')
286-
{
287-
# 'Auto' is for backward compatibility. Before the "comment_format"
288-
# was introduced, OKAPI used a weird format in between (it allowed
289-
# HTML, but applied nl2br too).
282+
# 'text_html' = 0 (false) is broken in OCPL code and has been
283+
# deprecated; OCPL code was changed to always set it to 1 (true).
284+
# For OKAPI, the value has been changed from 0 to 1 with commit
285+
# cb7d222, after an email discussion with Harrie Klomp. This is
286+
# an ID of the appropriate email thread:
287+
#
288+
# Message-ID: <22b643093838b151b300f969f699aa04@harrieklomp.be>
290289

291-
$formatted_comment = nl2br($comment);
292-
}
293-
else
294-
$formatted_comment = $comment;
290+
$value_for_text_html_field = 1;
291+
}
292+
}
293+
elseif ($comment_format == 'auto')
294+
{
295+
# 'Auto' is for backward compatibility. Before the "comment_format"
296+
# was introduced, OKAPI used a weird format in between (it allowed
297+
# HTML, but applied nl2br too).
298+
299+
$formatted_comment = nl2br($comment);
300+
$value_for_text_html_field = 1;
301+
}
302+
else
303+
{
304+
$formatted_comment = $comment;
305+
306+
# For user-supplied HTML comments, OC sites require us to do
307+
# additional HTML purification prior to the insertion into the
308+
# database.
295309

310+
if (Settings::get('OC_BRANCH') == 'oc.de')
311+
{
296312
# NOTICE: We are including EXTERNAL OCDE library here! This
297313
# code does not belong to OKAPI!
298314

@@ -302,48 +318,35 @@ private static function _call(OkapiRequest $request)
302318

303319
$purifier = new \OcHTMLPurifier($opt);
304320
$formatted_comment = $purifier->purify($formatted_comment);
305-
$value_for_text_html_field = 1;
306-
}
307-
}
308-
else
309-
{
310-
# OCPL is even weirder. It also stores HTML-lized comments in the database
311-
# (it doesn't really matter if 'text_html' field is set to FALSE). OKAPI must
312-
# save it in HTML either way. However, escaping plain-text doesn't work!
313-
# If we put "&lt;b&gt;" in, it still gets converted to "<b>" before display!
314-
# NONE of this process is documented within OCPL code. OKAPI uses a dirty
315-
# "hack" to save PLAINTEXT comments (let us hope the hack will remain valid).
316-
#
317-
# OCPL doesn't require HTML purification prior to the database insertion.
318-
# HTML seems to be purified dynamically, before it is displayed.
319-
320-
if ($comment_format == 'plaintext')
321-
{
322-
$formatted_comment = htmlspecialchars($comment, ENT_QUOTES);
323-
$formatted_comment = nl2br($formatted_comment);
324-
$formatted_comment = str_replace("&amp;", "&amp;#38;", $formatted_comment);
325-
$formatted_comment = str_replace("&lt;", "&amp;#60;", $formatted_comment);
326-
$formatted_comment = str_replace("&gt;", "&amp;#62;", $formatted_comment);
327-
#
328-
# The following was changed from 0 to 1 by Harrie Klomp. The discussion
329-
# regarding this change was exchanged via emails. This is an ID of the
330-
# appropriate email thread:
331-
#
332-
# Message-ID: <22b643093838b151b300f969f699aa04@harrieklomp.be>
333-
#
334-
$value_for_text_html_field = 1;
335-
}
336-
elseif ($comment_format == 'auto')
337-
{
338-
$formatted_comment = nl2br($comment);
339-
$value_for_text_html_field = 1;
340321
}
341322
else
342323
{
343-
$formatted_comment = $comment;
344-
$value_for_text_html_field = 1;
324+
# TODO: Add OCPL HTML filtering. The OCPL code for this is:
325+
326+
# require_once($rootpath . 'lib/class.inputfilter.php');
327+
# $myFilter = new InputFilter($allowedtags, $allowedattr, 0, 0, 1);
328+
# $log_text = $myFilter->process($log_text);
345329
}
330+
331+
$value_for_text_html_field = 1;
332+
}
333+
334+
if (Settings::get('OC_BRANCH') == 'oc.pl')
335+
{
336+
# The HTML processing in OCPL code is broken. Effectively, it
337+
# will decode &lt; &gt; and &amp; (and maybe other things?)
338+
# before display so that text contents may be interpreted as HTML.
339+
# We work around this by applying a double-encoding for & < >:
340+
341+
$formatted_comment = str_replace("&amp;", "&amp;#38;", $formatted_comment);
342+
$formatted_comment = str_replace("&lt;", "&amp;#60;", $formatted_comment);
343+
$formatted_comment = str_replace("&gt;", "&amp;#62;", $formatted_comment);
344+
345+
# Note: This problem also exists when submitting logs on OCPL websites.
346+
# If you e.g. enter "<text>" in the editor, it will get lost.
347+
# See https://github.com/opencaching/opencaching-pl/issues/469.
346348
}
349+
347350
unset($comment);
348351

349352
# Prevent bug #367. Start the transaction and lock all the rows of this

okapi/services/logs/submit.xml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,10 @@
2525
</opt>
2626
<opt name='comment_format' default='auto'>
2727
<p>Indicates the format of your <b>comment</b>. Three values allowed:
28-
<b>auto</b>, <b>html</b> or <b>plaintext</b>. Usually, you should <b>not</b>
28+
<b>auto</b>, <b>html</b> or <b>plaintext</b>. You should <b>not</b>
2929
use the <b>auto</b> option, because its exact behavior is unspecified
30-
and may depend on the installation
31-
(<a href='https://github.com/opencaching/okapi/issues/124'>more info</a>).</p>
32-
33-
<p><b>Important note:</b> Unfortunatelly, some OC nodes don't support this
34-
parameter properly - regardless of what you choose, you may end up with
35-
<a href='https://github.com/opencaching/okapi/issues/324'>unexpected
36-
results</a>. Currently, there is nothing OKAPI developers can do
37-
to fix this, but you should use this parameter either way (to indicate how
38-
you <b>expect</b> it to behave) - we hope this will be fixed, eventually.</p>
30+
(<a href='https://github.com/opencaching/okapi/issues/124'>more info</a>).
31+
It is only included for backward compatibility.</p>
3932

4033
<p><b>Important note:</b> The subset of allowed HTML elements is left undefined
4134
and may change in the future. For future-compatibility, you should use only

0 commit comments

Comments
 (0)