此iCalendar事件代码中是否存在语法错误?

时间:2016-09-13 21:12:25

标签: syntax-error icalendar

我有一个'订阅的日历'在我的iPhone上,从URL获取iCalendar格式的日历事件。日历工作正常除了它没有显示以下事件,是否有原因?所有其他活动都很好。我认为这可能是事件格式化/语法化方式的问题,但我似乎无法找到可能导致事件发生的任何事情。

BEGIN:VEVENT SUMMARY:Meet with tenant DESCRIPTION:Notes: Meter readings\, SoC images\, post box key\, finalise Let Procedure.\nLocation: Apartment X X Woodland Road\, Bebington\, Wirral\, CHXX XXX\nEmployee: Michael Le Brocq\nStatus: Confirmed\nOriginally Arranged: 07/09/16 12:18:43 by Lucy Christian\nLast Updated: 12/09/16 15:57:05 by Michael Le Brocq\n UID:2432 STATUS:CONFIRMED DTSTART:20160914T160000 DTEND:20160914T151500 LAST-MODIFIED:20160912T155705 LOCATION:Apartment 5 20 Woodland Road\, Bebington\, Wirral\, CH42 4NT END:VEVENT

用于生成日历事件的代码;

<?php

require_once('../inc/app_top_cron.php');

if (!empty($_GET)) {

// define and escape each GET as a variable

foreach ($_GET as $key => $value) {

if (!empty($value)) {

${$key} = mysqli_real_escape_string($con, PrepareInput($value));

}
}
}

// company details
$company_details_query = mysqli_query($con, "SELECT company_id, company_trading_name FROM company WHERE company_token = '" . $company . "' LIMIT 1") or die(mysql_error());
$company_details = mysqli_fetch_array( $company_details_query );

// the iCal date format. Note the Z on the end indicates a UTC timestamp.
define('DATE_ICAL', 'Ymd\THis');

// max line length is 75 chars. New line is \\r\n

$output = "BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
PRODID:-//Property Software//Calendar//EN
CALSCALE:GREGORIAN
X-WR-CALNAME:" . $company_details['company_trading_name'] . " Calendar" . "
\r\n";

$sql = "SELECT ce.*, ces.calendar_event_status_name
FROM calendar_event ce
INNER JOIN calendar_event_status ces
on ce.calendar_event_status = ces.calendar_event_status_id
WHERE ce.calendar_event_company_id = '" . $company_details['company_id'] . "'";

$calendar_event_query = mysqli_query($con, $sql) or die(mysql_error());  

while($row = mysqli_fetch_array( $calendar_event_query )) {

$calendar_event_subject = str_replace(",","\,", $row['calendar_event_subject']);

$calendar_event_description = str_replace(",","\,", $row['calendar_event_description']);
$calendar_event_description = str_replace("\r\n","\\n", $calendar_event_description);

$calendar_event_location = str_replace(",","\,", $row['calendar_event_location']);

// loop through events
 $output .=
"BEGIN:VEVENT
SUMMARY:" . $calendar_event_subject . "
DESCRIPTION:" . $calendar_event_description . "
UID:" . $row["calendar_event_id"] . "
STATUS:" . $row["calendar_event_status_name"] . "
DTSTART:" . date(DATE_ICAL, strtotime($row["calendar_event_start"])) . "
DTEND:" . date(DATE_ICAL, strtotime($row["calendar_event_end"])) . "
LAST-MODIFIED:" . date(DATE_ICAL, strtotime($row["calendar_event_date_updated"])) . "
LOCATION:" . $calendar_event_location . "
END:VEVENT
";
}

// close calendar
$output .= "END:VCALENDAR";

echo $output;

mysqli_close($con);

?>

2 个答案:

答案 0 :(得分:1)

此:

$calendar_event_description = str_replace("\r\n","\\n", $calendar_event_description);

您正在使用\r\n(回车+换行符)并将其转换为文字\字符,然后是n。这不是一个新行(一个字节/字符),它是两个字节/字符,对任何东西都没有特殊含义。

根据我上面的评论,不要做多行字符串构建/连接。它使得难以阅读和难以跟踪的调试成为可能。改为使用heredoc

$output = <<<EOL
BEGIN:VEVENT
SUMMARY: {$calendar_event_subject}
DESCRIPTION: {$calendar_event_description}
UID: {$row["calendar_event_id"]}
etc...
EOL;

请注意,缺少任何". - 使得代码块更加紧凑且易于理解。如果您之后需要更改换行符,因为您的系统使用的内容与您的代码编辑器嵌入的内容不同,您可以在完成构建字符串后使用简单的str_replace()执行此操作。

答案 1 :(得分:0)

icalendar标准要求\ r \ n行之间的换行符。您可以使用http://icalendar.org/validator.html

处的验证程序验证icalendar输出