CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • CRM/Utils/ICalendar.php confuses encoding of newlines with folding
Pages: [1]

Author Topic: CRM/Utils/ICalendar.php confuses encoding of newlines with folding  (Read 3296 times)

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
CRM/Utils/ICalendar.php confuses encoding of newlines with folding
January 30, 2011, 03:11:08 am
CiviCRM doesn't treat newlines correctly in iCal files. Rather than encoding newlines in text, it mistakenly strips them out, and tries to 'fold' the line instead. I have included an analysis and a patch for this problem.

Example

For example, the LOCATION ...

Quote
CQ Function Centre\n113 Queen St\nMelbourne, VIC 3000\n

... contains newline characters (\n) and is encoded by CiviCRM as...

Quote
LOCATION:CQ Function Centre
 113 Queen St
 Melbourne\, VIC 3000
 

... which an iCal reader will interpret as ...

Quote
LOCATION:CQ Function Centre113 Queen StMelbourne, VIC 3000

Note that the newlines are missing.

Analysis

RTF 2445 http://www.ietf.org/rfc/rfc2445.txt says that newline characters in text should be encoded as "\\n" (a backslash followed by an 'n'). It also specifies that lines longer than 75 bytes should be 'folded' to keep lines from getting too long. A line is 'folded' by inserting the character sequence "\n " (a newline character followed by a whitespace character). The reader of an iCal file must 'unfold' lines by removing these sequences before any processing is done.

CiviCRM confuses the encoding of newlines with folding of lines. Here is a patch to fix the code on lines 55-65 of CRM/Utils/ICalendar.php ...

Code: [Select]
     static function formatText( $text )
     {
         $text = strip_tags($text);
         $text = str_replace("\"", "DQUOTE", $text);
         $text = str_replace("\\", "\\\\", $text);
         $text = str_replace(",", "\,", $text);
         $text = str_replace(":", "\":\"", $text);
         $text = str_replace(";", "\;", $text);
-        $text = str_replace("\n", "\n ", $text);
+        $text = str_replace("\n", "\\n", $text);
+        $text = implode("\n ", str_split($text, 50));
         return $text;
     }

The line to be deleted does not encode the newline, but replaces it with a 'fold' sequence. Not only are newlines stripped out, but the folding is not done correctly: the 75 byte limit is not respected.

The lines to be added fix this by encoding the newlines, and then inserting the fold sequences. Note that '50' is suggested rather than '75' ...
  • The first line may already contain text when formatText is called. In iCal.tpl the longest parameter name is "ORGANIZER:MAILTO:" which is 17 characters. A value of '50' means such a line will respect the RTF
  • The RFC says "75 octets" rather than "75 characters". As the encoding may be UTF-8 '50' allows some contingency.
  • A programmer would use a constant rather than a hard-wired number

Ken
« Last Edit: January 30, 2011, 03:15:13 am by ken »

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: CRM/Utils/ICalendar.php confuses encoding of newlines with folding
February 04, 2011, 03:22:04 am
Filed http://issues.civicrm.org/jira/browse/CRM-7496

Thanks for catching this!
Yashodha
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

ken

  • I live on this forum
  • *****
  • Posts: 916
  • Karma: 53
    • City Bible Forum
  • CiviCRM version: 4.6.3
  • CMS version: Drupal 7.36
  • MySQL version: 5.5.41
  • PHP version: 5.3.10
Re: CRM/Utils/ICalendar.php confuses encoding of newlines with folding
February 04, 2011, 04:25:04 am
Yashodha,

I've found another problem with that function. Colons are being quoted unnecessarily. The following patch should be applied to lines 55- 66 CRM/Utils/ICalendar.php ...

Code: [Select]
     static function formatText( $text )
     {
         $text = strip_tags($text);
         $text = str_replace("\"", "DQUOTE", $text);
         $text = str_replace("\\", "\\\\", $text);
         $text = str_replace(",", "\,", $text);
-        $text = str_replace(":", "\":\"", $text);
         $text = str_replace(";", "\;", $text);
         $text = str_replace("\n", "\\n", $text);
         $text = implode("\n ", str_split($text, 50));
         return $text;
     }

This is what http://www.ietf.org/rfc/rfc2445.txt says about escaping characters...

Quote
     text       = *(TSAFE-CHAR / ":" / DQUOTE / ESCAPED-CHAR)
     ; Folded according to description above

     ESCAPED-CHAR = "\\" / "\;" / "\," / "\N" / "\n")
        ; \\ encodes \, \N or \n encodes newline
        ; \; encodes ;, \, encodes ,

     TSAFE-CHAR = %x20-21 / %x23-2B / %x2D-39 / %x3C-5B
                  %x5D-7E / NON-US-ASCII
        ; Any character except CTLs not needed by the current
        ; character set, DQUOTE, ";", ":", "\", ","

Note that the colon character is not escaped.

Ken

Yashodha Chaku

  • Forum Godess / God
  • Ask me questions
  • *****
  • Posts: 755
  • Karma: 57
    • CiviCRM
Re: CRM/Utils/ICalendar.php confuses encoding of newlines with folding
February 04, 2011, 05:58:42 am
fixed  :)
Found this reply helpful? Contribute NOW and help us improve CiviCRM with the Make it Happen! initiative.

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using CiviEvent (Moderator: Yashodha Chaku) »
  • CRM/Utils/ICalendar.php confuses encoding of newlines with folding

This forum was archived on 2017-11-26.