<?php
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is SOGo z-push backend.
 *
 * The Initial Developer of the Original Code is
 *   Philipp Kewisch <mozilla@kewis.ch>
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

include_once("debug.php");

function initstore() {
        $folder = new SyncFolder();

        $folder->serverid = "root";
        $folder->type = SYNC_FOLDER_TYPE_APPOINTMENT;
        $folder->displayname = "Root folder";
        $folder->parentid = "0";

    return $folder;
}

function icsDateToPHP($data) {
    if (!preg_match("/^([0-9]{4})([0-9]{2})([0-9]{2})" .
                    "([Tt]([0-9]{2})([0-9]{2})([0-9]{2})(\\.[0-9]+)?)?" .
                    "(([Zz]|([+-])([0-9]{2}):([0-9]{2})))?/", $data, $matches)) {
        return null;
    }

    if (!isset($matches[5])) $matches[5] = 0;
    if (!isset($matches[6])) $matches[6] = 0;
    if (!isset($matches[7])) $matches[7] = 0;

    return mktime($matches[5],$matches[6],$matches[7],$matches[2],$matches[3],$matches[1]);
}

function UTC() {
    return base64_encode(
        pack("la64vvvvvvvv" . "la64vvvvvvvv" . "l",
             0, "", 0, 0, 0, 0, 0, 0, 0, 0,
             0, "", 0, 0, 0, 0, 0, 0, 0, 0,
             0));
}

function convertOffset($data) {
    if (!preg_match("/(-?)(\d{2})(\d{2})/", $data, $matches)) {
        return 0;
    }
    return ($matches[1] == "-" ? -1 : 1) *
           (($matches[2] * 60) + $matches[3]);
}

function convertTimezone($vtimezone) {
    $dstdiff = convertOffset($daylight["TZOFFSETFROM"]["value"]) -
               convertOffset($daylight["TZOFFSETTO"]["value"]);
    return base64_encode(
        pack("la64vvvvvvvv" . "la64vvvvvvvv" . "l",
            convertOffset($standard["TZOFFSETTO"]["value"]),
            $standard["TZNAME"]["value"],
            0, 0, 0, 0, 0, 0, 0, 0,
            0,
            $daylight["TZNAME"]["value"],
            0, 0, 0, 0, 0, 0, 0, 0,
            $dstdiff));
}

function convertICSToSyncAppt($icsdata, $uniquename, $parentid) {
    $ics = iCalReader::parseString($icsdata);
    $icsevents = $ics->getEvents();
    $icsevent = $icsevents[0];
    $appt = new SyncAppointment();

    $appt->parentid = $parentid;
    $appt->uid = $icsevent["UID"]["value"];
    $appt->bodytruncated = 0;
    //$appt->reminder = 0;
    if (array_key_exists("ORGANIZER", $icsevent)) {
        if (array_key_exists("CN", $icsevent["ORGANIZER"]["params"])) {
            $appt->organizername = $icsevent["ORGANIZER"]["params"]["CN"];
        }
        $appt->organizeremail = preg_replace("/MAILTO:/i", "", $icsevent["ORGANIZER"]["value"]);
    }
    $appt->busystatus = 2;
    $appt->sensitivity = 0;
    if (array_key_exists("VALUE", $icsevent["DTSTART"]["params"]) &&
        $icsevent["DTSTART"]["params"]["VALUE"] == "DATE") {
        $appt->alldayevent = 1;
    } else {
        $appt->alldayevent = 0;
    }
    $appt->subject = $icsevent["SUMMARY"]["value"];
    $appt->starttime = icsDateToPHP($icsevent["DTSTART"]["value"]);
    $appt->endtime =  icsDateToPHP($icsevent["DTEND"]["value"]);
    $appt->dtstamp = icsDateToPHP($icsevent["DTSTAMP"]["value"]);

    // Description
    if (array_key_exists("DESCRIPTION", $icsevent)) {
        $appt->body = $icsevent["DESCRIPTION"]["value"];
    }

    // Location
    if (array_key_exists("LOCATION", $icsevent)) {
        $appt->location = $icsevent["LOCATION"]["value"];
    }

    // Transparency 
    $busymap = array("OPAQUE" => 2, "TRANSPARENT" => 0); 
    if (array_key_exists("TRANSP", $icsevent) &&
        array_key_exists($icsevent["TRANSP"]["value"], $busymap)) {
        $appt->busystatus = $busymap[$icsevent["TRANSP"]["value"]];
    }

    // Privacy
    $sensmap = array("PUBLIC" => 0, "PRIVATE" => 2, "CONFIDENTIAL" => 3);
    if (array_key_exists("CLASS", $icsevent) &&
        array_key_exists($icsevent["CLASS"]["value"], $sensmap)) {
        $appt->sensitivity = $sensmap[$icsevent["CLASS"]["value"]];
    }


    return $appt;
}
?>
