php-calendar
php-calendar copied to clipboard
vCalendar support
integrate script from Christian Toepp.
<?php
/*
* Copyright 2014 Christian Toepp <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
$dbUser = "...";
$dbPass = "...";
$dbHost = "...";
$dbName = "...";
header('Content-type: text/calendar');
?>
BEGIN:VCALENDAR
METHOD:PUBLISH
VERSION:2.0
CALSCALE:GREGORIAN
<?php
$sql = "select e.eid, e.subject,e.description, date_format(e.ctime,'%Y%m%dT%k%i%sZ') as ctime".
",date_format(o.start_ts,'%Y%m%dT%k%i%sZ') as start_ts ,date_format(o.end_ts,'%Y%m%dT%k%i%sZ') ".
"as end_ts from phpc_events as e,phpc_occurrences as o where e.eid=o.eid";
$db = mysql_pconnect($dbHost,$dbUser,$dbPass);
mysql_select_db($dbName,$db);
$res = mysql_query($sql, $db);
while($row = mysql_fetch_assoc($res)){
error_log('START:'.$row['start_ts']);
if(empty($row['start_ts'])){
$res2 = mysql_query("select date_format(start_date,'%Y%m%d') as start, ".
"date_format(end_date,'%Y%m%d') as end from ".
"phpc_occurrences where eid=".$row['eid'], $db);
if($row2 = mysql_fetch_assoc($res2)){
$row['start_ts'] = 'DTSTART;VALUE=DATE:'.$row2['start'];
$row['end_ts'] = 'DTEND;VALUE=DATE:'.$row2['end'];
}
}else{
$row['start_ts'] = 'DTSTART:'.$row['start_ts'];
$row['end_ts'] = 'DTEND:'.$row['end_ts'];
}
echo "BEGIN:VEVENT\r\n";
echo 'CREATED:' . $row['ctime'] ."\r\n";
echo 'SUMMARY:' . $row['subject'] . "\r\n";
echo 'DESCRIPTION:'.$row['description']."\r\n";
echo $row['start_ts'] . "\r\n";
echo $row['end_ts'] . "\r\n";
echo "END:VEVENT\r\n";
}
?>
END:VCALENDAR
If anyone would like to implement the above as a quick fix, here's my modification:
$phpc_root_path = dirname(__FILE__);
$phpc_config_file = "$phpc_root_path/config.php";
require $phpc_config_file;
// constants defined in config
$dbUser = SQL_USER;
$dbPass = SQL_PASSWD;
$dbHost = SQL_HOST;
$dbName = SQL_DATABASE;
(Replacing the existing $dbXXX
variables)
I saved mine as export.php
and added an Apache rewrite rule to allow the vCalendar to be downloaded as if it were a .ics file:
(A simple Alias directive would probably have made more sense, but this was installed in a shared hosting environment.)
.htaccess
RewriteEngine on
RewriteBase /
RewriteRule main.ics /<path to calendar>/export.php [QSA,L]
Works like a charm. I was able to import it to Google Calendar.
Thanks to @sproctor and Christian Toepp!