Sync Alfresco iCal Feeds nach OwnCloud

Dieses Howto beschreibt, wie man iCal-Kalender-Einträge aus Alfresco in den Kalender von OwnCloud importieren kann.

Links:

Mit OwnCloud besteht die Möglichkeit über ein PHP Script iCal-Kalender Einträge zu importieren:

PHP-Script

Dieses PHP-Script importiert iCals nach OwnCloud:

#!/usr/bin/php
<?php
/*
 * Standalone PHP (CLI) script to import a calendar into owncloud.
 *
 * Assumes you have the file already on the server (perhaps via
 * wget or curl), and want to bring it in replacing all events.
 * Or in fact, if your PHP is compiled with `URL_fopen_wrappers',
 * then you can specify a URL for the filename and PHP will fetch
 * it for you.
 *
 * Be sure to set the definition of OWNCLOUD_DIR appropriately
 * for your owncloud installation.
 *
 * Script by George Ferguson <code@phurg.com>
 * Based on code from owncloud/apps/calendar/ajax/import/import.php
 * and owncloud/apps/calendar/lib/import.php.
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 *
 * Tested with OwnCloud 5.0.10 (17?) using PHP 5.3.15 on OSX 10.8.3
 * - calendars table name got changed to "oc_clndr_calendars"
 * - added exception handler for debugging otherwise silent failures
 * Tested with OwnCloud 5.0.5 using PHP 5.3.15 on OSX 10.8.3 and Ubuntu 12.04.
 */

define('OWNCLOUD_DIR', '/var/www/owncloud');

require_once(OWNCLOUD_DIR . '/lib/base.php');
require_once(OWNCLOUD_DIR . '/apps/calendar/lib/import.php');
require_once(OWNCLOUD_DIR . '/apps/calendar/lib/object.php');
require_once(OWNCLOUD_DIR . '/apps/calendar/lib/calendar.php');
require_once(OWNCLOUD_DIR . '/apps/calendar/lib/app.php');

# Process command-line
$ARGV0 = $argv[0];
if ($argc != 4) {
    error_log("usage: " . $ARGV0 . " username calendarname filename");
    exit(1);
}
$userid = $argv[1];
$displayname = $argv[2];
$filename = $argv[3];

# Lookup calendar given username and calendarname
try {
    $stmt = OCP\DB::prepare( 'SELECT * FROM `*PREFIX*clndr_calendars` WHERE `userid` = ? AND `displayname` = ?' );
    $result = $stmt->execute(array($userid, $displayname));
    $row = $result->fetchRow();
    $calendar_id = $row['id'];
    if (!$calendar_id) {
        error_log("$ARGV0: no calendar for userid " . $username . " with displayname '" . $displayname . "'");
        exit(1);
    }
} catch (Exception $e) {
    error_log("DB exception: " . $e);
    exit(1);
}

# Get input file contents
$file = file_get_contents($filename);
if ($file === FALSE) {
    error_log("$ARGV0: couldn't read file: $filename");
    exit(1);
}

# Prepare to import
$import = new OC_Calendar_Import($file);

# Incantations done in real import code
$import->setUserID($userid);
$import->setTimeZone(OC_Calendar_App::$tz);
$import->setCalendarID($calendar_id);
$import->setOverwrite(true);

# This condition is tested in OC_Calendar_Object::add()
# in owncloud/apps/calendar/lib/object.php
OC_User::setUserId($userid);

# Try to do the import
try {
    $import->import();
} catch (Exception $e) {
    error_log("$ARGV0: import failed: " . $e);
    exit(1);
}

# Done!
$count = $import->getCount();
echo "$ARGV0: imported $count objects\n";

exit(0);

Cron-Job

Dieses Shell-Script holt sich von Alfresco den gewünschten iCal-Feed und führt den Import mit obigem PHP-Script durch. Das Shell-Script kann entsprechend als Cron-Job ausgeführt werden.

sudo vi /etc/cron.hourly/owncloud-importCalendar
#!/bin/bash
   echo "Download iCal:"
   wget --output-document=file.ics  --user=user --password=password --no-check-certificate "https://url/alfresco/service/calendar/site.ics?site=site&format=calendar"
   echo "Fuere Import aus"
   php -f owncloud-import-calendar.php user "site" file.ics
   rm file.ics
exit 0
sudo chmod +x /etc/cron.hourly/owncloud-importCalendar