Skip to content

Commit

Permalink
Merge pull request #211 from RRZE-Webteam/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
rvdforst authored Jun 27, 2024
2 parents 0d07e33 + c9363e0 commit d5967d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
76 changes: 36 additions & 40 deletions includes/ICS/ICS.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

class ICS
{
// Date and time format
public const DT_FORMAT = 'Ymd\THis\Z';

// Carriage return and line feed
const CRLF = "\r\n";

// Length of line in bytes
const LINE_LENGTH = 70;

/**
* iCalendar data array.
*
Expand Down Expand Up @@ -86,7 +93,7 @@ public function build(): string
fn ($row) => $this->split($row),
$this->render()
);
return implode("\r\n", $rows);
return implode(self::CRLF, $rows);
}

/**
Expand Down Expand Up @@ -139,13 +146,12 @@ private function sanitizeValue(string $value, string $key = ''): string
case 'dtstart':
$value = $this->formatTimestamp($value);
break;
case 'summary':
case 'description':
case 'location':
$value = $this->escStr($value, true);
$value = $this->escStr($value);
break;
default:
$value = $this->escStr($value);
$value = $value;
}

return $value;
Expand All @@ -164,60 +170,50 @@ private function formatTimestamp(string $timestamp): string
}

/**
* Escape special characters.
* Escapes , and ; characters in text type fields.
*
* @param string $input
* @param boolean $specialChars
* @param string $value The string to escape
* @return string
*/
private function escStr(string $input, $specialChars = false): string
private function escStr(string $value): string
{
if ($specialChars) {
$input = preg_replace('/([\,;])/', '\\\$1', $input);
}
$input = str_replace("\n", "\\n", $input);
$input = str_replace("\r", "\\r", $input);
return $input;
$value = preg_replace('/((?<!\\\),|(?<!\\\);)/', '\\\$1', $value);
$value = preg_replace('/((?<!\\\)\\\(?!,|;|n|N|\\\))/', '\\\\$1', $value);
return $value;
}

/**
* Split content lines.
* Splits a string into new lines if necessary.
* RFC-5545 (3.1. Content Lines)
*
* @param string $input
* @param integer $lineLimit
* @param string $value
* @return string
*/
private function split(string $input, int $lineLimit = 70): string
private function split(string $value): string
{
// Newlines need to be converted to literal \n
$value = str_replace("\n", "\\n", str_replace("\r\n", "\n", $value));

// Get number of bytes
$length = strlen($value);

$output = '';
$line = '';
$pos = 0;

while ($pos < mb_strlen($input)) {
// Find newlines
$newLinepos = mb_strpos($input, "\n", $pos + 1);
if (!$newLinepos) {
$newLinepos = mb_strlen($input);
}
$line = mb_substr($input, $pos, $newLinepos - $pos);

if (mb_strlen($line) <= $lineLimit) {
$output .= $line;
} else {
// The line break limit of the first line is $lineLimit
$output .= mb_substr($line, 0, $lineLimit);
$line = mb_substr($line, $lineLimit);
if ($length > 75) {
$start = 0;

// Subsequent line break limit is $lineLimit - 1 due to leading whitespace
$output .= "\n " . mb_substr($line, 0, $lineLimit - 1);
while ($start < $length) {
$cut = mb_strcut($value, $start, self::LINE_LENGTH, 'UTF-8');
$output .= $cut;
$start = $start + strlen($cut);

while (mb_strlen($line) > $lineLimit - 1) {
$line = mb_substr($line, $lineLimit - 1);
$output .= "\n " . mb_substr($line, 0, $lineLimit - 1);
// Add space if not last line
if ($start < $length) {
$output .= self::CRLF . ' ';
}
}
$pos = $newLinepos;
} else {
$output = $value;
}

return $output;
Expand Down
4 changes: 4 additions & 0 deletions includes/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ public static function titleFilter($where, $wp_query)
*/
public static function validateDate($date)
{
if (empty($date)) {
return false;
}

if ($date instanceof \DateTime) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion rrze-calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin Name: RRZE Calendar
Plugin URI: https://github.com/RRZE-Webteam/rrze-calendar
Description: Import and output of FAU public events.
Version: 2.2.8
Version: 2.2.9
Author: RRZE Webteam
Author URI: https://blogs.fau.de/webworking/
License: GNU General Public License v3.0
Expand Down

0 comments on commit d5967d8

Please sign in to comment.