/**
* Escape text elements for safe ICalendar use.
*
- * @param $text
+ * @param string $text
* Text to escape.
*
* @return string
- * Escaped text
*/
public 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(';', '\;', $text);
$text = str_replace(array("\r\n", "\n", "\r"), "\\n ", $text);
$text = implode("\n ", str_split($text, 50));
return $text;
}
+ /**
+ * Restore iCal formatted text to normal.
+ *
+ * @param string $text
+ * Text to unescape.
+ *
+ * @return string
+ */
+ public static function unformatText($text) {
+ $text = str_replace('\n ', "\n", $text);
+ $text = str_replace('\;', ';', $text);
+ $text = str_replace('\,', ',', $text);
+ $text = str_replace("\\\\", "\\", $text);
+ $text = str_replace("DQUOTE", "\"", $text);
+ return $text;
+ }
+
/**
* Escape date elements for safe ICalendar use.
*
--- /dev/null
+<?php
+/*
++--------------------------------------------------------------------+
+| CiviCRM version 4.7 |
++--------------------------------------------------------------------+
+| Copyright CiviCRM LLC (c) 2004-2016 |
++--------------------------------------------------------------------+
+| This file is a part of CiviCRM. |
+| |
+| CiviCRM is free software; you can copy, modify, and distribute it |
+| under the terms of the GNU Affero General Public License |
+| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
+| |
+| CiviCRM is distributed in the hope that it will be useful, but |
+| WITHOUT ANY WARRANTY; without even the implied warranty of |
+| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
+| See the GNU Affero General Public License for more details. |
+| |
+| You should have received a copy of the GNU Affero General Public |
+| License and the CiviCRM Licensing Exception along |
+| with this program; if not, contact CiviCRM LLC |
+| at info[AT]civicrm[DOT]org. If you have questions about the |
+| GNU Affero General Public License or the licensing of CiviCRM, |
+| see the CiviCRM license FAQ at http://civicrm.org/licensing |
++--------------------------------------------------------------------+
+ */
+
+/**
+ * Tests for linking to resource files
+ * @group headless
+ */
+class CRM_Utils_ICalendarTest extends CiviUnitTestCase {
+
+ /**
+ * @return array
+ */
+ public function escapeExamples() {
+ $cases = array();
+ $cases[] = array("Hello
+ this is, a test!");
+ $cases[] = array("Hello!!
+
+ this is, a \"test\"!");
+ return $cases;
+ }
+
+ /**
+ * @param string $testString
+ * @dataProvider escapeExamples
+ */
+ public function testParseStrings($testString) {
+ $this->assertEquals($testString, CRM_Utils_ICalendar::unformatText(CRM_Utils_ICalendar::formatText($testString)));
+ }
+
+}