#!/usr/bin/env php
# -*- Mode: php -*-

<?php
/*
 * Copyright 2005 - 2012  Zarafa B.V.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3, 
 * as published by the Free Software Foundation with the following additional 
 * term according to sec. 7:
 *  
 * According to sec. 7 of the GNU Affero General Public License, version
 * 3, the terms of the AGPL are supplemented with the following terms:
 * 
 * "Zarafa" is a registered trademark of Zarafa B.V. The licensing of
 * the Program under the AGPL does not imply a trademark license.
 * Therefore any rights, title and interest in our trademarks remain
 * entirely with us.
 * 
 * However, if you propagate an unmodified version of the Program you are
 * allowed to use the term "Zarafa" to indicate that you distribute the
 * Program. Furthermore you may use our trademarks where it is necessary
 * to indicate the intended purpose of a product or service provided you
 * use it in accordance with honest practices in industrial or commercial
 * matters.  If you want to propagate modified versions of the Program
 * under the name "Zarafa" or "Zarafa Server", you may only do so if you
 * have a written permission by Zarafa B.V. (to acquire a permission
 * please contact Zarafa at trademark@zarafa.com).
 * 
 * The interactive user interface of the software displays an attribution
 * notice containing the term "Zarafa" and/or the logo of Zarafa.
 * Interactive user interfaces of unmodified and modified versions must
 * display Appropriate Legal Notices according to sec. 5 of the GNU
 * Affero General Public License, version 3, when you propagate
 * unmodified or modified versions of the Program. In accordance with
 * sec. 7 b) of the GNU Affero General Public License, version 3, these
 * Appropriate Legal Notices must retain the logo of Zarafa or display
 * the words "Initial Development by Zarafa" if the display of the logo
 * is not reasonably feasible for technical reasons. The use of the logo
 * of Zarafa in Legal Notices is allowed for unmodified and modified
 * versions of the software.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *  
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

include('/usr/share/php/mapi/mapi.util.php');
include('/usr/share/php/mapi/mapidefs.php');
include('/usr/share/php/mapi/mapicode.php');
include('/usr/share/php/mapi/mapitags.php');
include('/usr/share/php/mapi/mapiguid.php');

include('/usr/share/php/mapi/class.meetingrequest.php');
include('/usr/share/php/mapi/class.recurrence.php');
include('/usr/share/php/mapi/class.freebusypublish.php');

define('POLICY_PROCESS_MEETING_REQUESTS',				0x0001);
define('POLICY_DECLINE_RECURRING_MEETING_REQUESTS',		0x0002);
define('POLICY_DECLINE_CONFLICTING_MEETING_REQUESTS', 	0x0004);

define('RECURRENCE_AVAILABILITY_RANGE', 60 * 60 * 24 * 180); // 180 days

$DEBUG = 1;

function parseConfig($configfile)
{
    $fp = fopen($configfile, "rt");
    if(!$fp)
        return false;
        
    $settings = array();
    
    while($line = fgets($fp)) {
        if($line[0] == '#')
            continue;
        
        $pos = strpos($line, "=");
        if($pos) {
            $key = trim(substr($line, 0, $pos));
            $value = trim(substr($line, $pos+1));
            
            $settings[$key] = $value;
        }
    }

    return $settings;
}

function u2w($s)
{
    return $s;
}

function hex2bin($data)
{
    return pack("H*", $data);
}

/**
* Sorts by timestamp, if equal, then end before start. Used by getOverlapDepth()
*/
function cmp($a, $b)
{
    if ($a["time"] == $b["time"]) {
        if($a["type"] < $b["type"])
            return 1;
        if($a["type"] > $b["type"])
            return -1;
        return 0;
    }
    return ($a["time"] > $b["time"] ? 1 : -1);
} 

/**
* Get the overlap depth of the passed items.
*
* This function calculates the maximum number of overlapping appointments at any one time
* for all the passed appointments, disregarding 'free' appointments. 
*/
function getOverlapDepth($items, $proptags, $goid2)
{
    $timestamps = Array();
    $cbusy = Array();
    $level = 0;
    $maxlevel = 0;

    foreach($items as $item)
    {
        // Disregard 'free' items and the item that we are updating
        if($item[$proptags['busystatus']] > 0 && $item[$proptags['goid2']] != $goid2) {
            $ts["type"] = 0;
            $ts["time"] = $item[$proptags["startdate"]];
            $timestamps[] = $ts;

            $ts["type"] = 1;
            $ts["time"] = $item[$proptags["duedate"]];
            $timestamps[] = $ts;
        }
    } 

    usort($timestamps, "cmp");

    foreach($timestamps as $ts)
    {
        switch ($ts["type"])
        {
            case 0: // Start
                $level++;
                $maxlevel = max($level, $maxlevel);
                break;
            case 1: // End
                $level--;
                break;
        } 
    } 

    return $maxlevel;
} 

/**
* Get unresponded items from the specified folder
*
* Looks for messages which have PR_RESPONSE_REQUESTED = TRUE, but no PR_PROCESSED = TRUE and have
* class IPM.Meeting.Req.*
*/
function getUnresponded($folder)
{
    $contents = mapi_folder_getcontentstable($folder);
    
    $restriction = Array(RES_OR, 
                            Array(
                                Array(RES_AND,
                                        Array(
                                            Array(RES_PROPERTY, Array(RELOP => RELOP_EQ, ULPROPTAG => PR_MESSAGE_CLASS, VALUE => 'IPM.Schedule.Meeting.Request') ),
                                            Array(RES_PROPERTY, Array(RELOP => RELOP_EQ, ULPROPTAG => PR_RESPONSE_REQUESTED, VALUE => true ) ),
                                            Array(RES_PROPERTY, Array(RELOP => RELOP_NE, ULPROPTAG => PR_PROCESSED, VALUE => true ) )
                                        )
                                ),
                                Array(RES_AND,
                                        Array(
                                            Array(RES_PROPERTY, Array(RELOP => RELOP_EQ, ULPROPTAG => PR_MESSAGE_CLASS, VALUE => 'IPM.Schedule.Meeting.Canceled') ),
                                            Array(RES_PROPERTY, Array(RELOP => RELOP_NE, ULPROPTAG => PR_PROCESSED, VALUE => true ) )
                                        )
                                )
                            )
                    );
                        
    $rows = mapi_table_queryallrows($contents, Array(PR_ENTRYID), $restriction);
    
    $entryids = array();
    
    foreach ($rows as $row) {
        $entryids[] = $row[PR_ENTRYID];
    }
    
    return $entryids;
}

/**
* Get the capacity of the resource store
*
* For rooms, the capacity is always 1. For equipment the capacity is 1 if
* PR_EMS_AB_ROOM_CAPACITY does not exist, otherwise it is equal to
* PR_EMS_AB_ROOM_CAPACITY. Other objects always have a capacity of 1.
*/
function getCapacity($session, $store)
{
    $storeprops = mapi_getprops($store, array(PR_MAILBOX_OWNER_ENTRYID));
    $ab = mapi_openaddressbook($session);
    
    $mailuser = mapi_ab_openentry($ab, $storeprops[PR_MAILBOX_OWNER_ENTRYID]);
    
    $props = mapi_getprops($mailuser, array(PR_EMS_AB_ROOM_CAPACITY, PR_DISPLAY_TYPE_EX));
    
    if(!isset($props[PR_EMS_AB_ROOM_CAPACITY]) || $props[PR_EMS_AB_ROOM_CAPACITY] <= 0 || !isset($props[PR_DISPLAY_TYPE_EX]) || $props[PR_DISPLAY_TYPE_EX] != DT_EQUIPMENT) {
        $capacity = 1;
    } else {
        $capacity = $props[PR_EMS_AB_ROOM_CAPACITY];
    }
    
    return $capacity;
}

/**
* Get local freebusy message for this store
*/
function getLocalFBMessage($store)
{
    $root = mapi_msgstore_openentry($store);
    
    $rootprops = mapi_getprops($root, array(PR_FREEBUSY_ENTRYIDS));
    
    if(!isset($rootprops[PR_FREEBUSY_ENTRYIDS]) || count($rootprops[PR_FREEBUSY_ENTRYIDS]) < 2)
        return false;
        
    $message = mapi_msgstore_openentry($store, $rootprops[PR_FREEBUSY_ENTRYIDS][1]);
    
    return $message;
}

/**
* Get auto-respond policy for a store
*
* Can return POLICY_PROCESS_MEETING_REQUESTS, POLICY_DECLINE_RECURRING_MEETING_REQUESTS and POLICY_DECLINE_CONFLICTING_MEETING_REQUESTS
* in any combination
*
*/
function getPolicy($store)
{
    $localfbmessage = getLocalFBMessage($store);
    
    if(!$localfbmessage) {
        return 0;
    }
    
    $props = mapi_getprops($localfbmessage, array(PR_PROCESS_MEETING_REQUESTS, PR_DECLINE_CONFLICTING_MEETING_REQUESTS, PR_DECLINE_RECURRING_MEETING_REQUESTS));
    
    $flags = 0;
    
    if(isset($props[PR_PROCESS_MEETING_REQUESTS]) && $props[PR_PROCESS_MEETING_REQUESTS])
        $flags |= POLICY_PROCESS_MEETING_REQUESTS;
    if(isset($props[PR_DECLINE_CONFLICTING_MEETING_REQUESTS]) && $props[PR_DECLINE_CONFLICTING_MEETING_REQUESTS])
        $flags |= POLICY_DECLINE_CONFLICTING_MEETING_REQUESTS;
    if(isset($props[PR_DECLINE_RECURRING_MEETING_REQUESTS]) && $props[PR_DECLINE_RECURRING_MEETING_REQUESTS])
        $flags |= POLICY_DECLINE_RECURRING_MEETING_REQUESTS;
        
    return $flags;
}

/**
* Get the main calendar
*/
function getCalendar($store)
{
    $inbox = mapi_msgstore_getreceivefolder($store);
    $inboxprops = mapi_getprops($inbox, array(PR_IPM_APPOINTMENT_ENTRYID));
    
    if(!isset($inboxprops[PR_IPM_APPOINTMENT_ENTRYID]))
        return false;
        
    $calendar = mapi_msgstore_openentry($store, $inboxprops[PR_IPM_APPOINTMENT_ENTRYID]);
    
    return $calendar;
}

/**
* Get the default store for this session
*/
function getDefaultStore($session)
{
    $msgstorestable = mapi_getmsgstorestable($session);

    $msgstores = mapi_table_queryallrows($msgstorestable, array(PR_DEFAULT_STORE, PR_ENTRYID));

    foreach ($msgstores as $row) {
        if($row[PR_DEFAULT_STORE]) {
            $storeentryid = $row[PR_ENTRYID];
        }
    }

    if(!$storeentryid) {
        print "Can't find default store\n";
        return false;
    }

    $store = mapi_openmsgstore($session, $storeentryid);
    
    return $store;
}

function debuglog($message)
{
    global $DEBUG;
    
    if($DEBUG) {
        print($message);
    }
}

/**
 * Return TRUE if two appointments overlap
 *
 */
function apptOverlap($appt1, $appt2, $proptags)
{
    // If appt1 starts after appt2 has ended, no overlap
    if($appt1[$proptags['startdate']] >= $appt2[$proptags['duedate']]) {
        return false;
    }
    
    // If appt2 starts after appt1 has ended, no overlap
    if($appt2[$proptags['startdate']] >= $appt1[$proptags['duedate']]) {
        return false;
    }

    return true;
}

/**
 * Return the intersection of a list of appointments with one appointment
 *
 * This means that only the appointments in $list that overlap with $appointment
 * are returned in a list
 */
function intersectAppointmentWithList($appointment, $list, $proptags)
{
    $intersect = array();
    
    foreach($list as $item) {
        if(apptOverlap($item, $appointment, $proptags))
            $intersect[] = $item;
    }
    
    return $intersect;
}

/**
* Auto-respond to a meeting request
*
* Looks at the incoming meeting request, checks availability for the resource, and responds accordingly
*/
function autoRespond($session, $store, $entryid, $capacity, $policy)
{
    debugLog("Processing item with entryid " . bin2hex($entryid) . "\n");
    $calendar = getCalendar($store);
    
    if(!$calendar) {
        debuglog("Unable to open calendar.\n");
        return false;
    }
    
    $proptags = getPropIdsFromStrings($store, array(
        'startdate' => "PT_SYSTIME:PSETID_Appointment:0x820d",
        'duedate' => "PT_SYSTIME:PSETID_Appointment:0x820e",
        'busystatus' => "PT_LONG:PSETID_Appointment:0x8205",
        'recurring' => "PT_BOOLEAN:PSETID_Appointment:0x8223",
        'goid2' => "PT_BINARY:PSETID_Meeting:0x23",
        'subject' => PR_SUBJECT
    ));
    
    $request = mapi_msgstore_openentry($store, $entryid);
    
    if(!$request) {
        debugLog("Unable to open item with entryid " . bin2hex($entryid) . "\n");
        return false;
    }
    
    $mr = new Meetingrequest($store, $request, $session);
    
    if($mr->isMeetingRequest()) {
        $props = mapi_getprops($request, $proptags);
        
        // Check general policy settings
        
        if(isset($props[$proptags['recurring']]) && $props[$proptags['recurring']] && ($policy & POLICY_DECLINE_RECURRING_MEETING_REQUESTS)) {
            $mr->doDecline(true, false, false, _("Recurring meetings are not allowed"));
            debuglog("Declined due to recurrence against non-recurring policy.\n");
            return true;
        }
        
        if($policy & POLICY_DECLINE_CONFLICTING_MEETING_REQUESTS) {
            if(isset($props[$proptags['recurring']]) && $props[$proptags['recurring']]) {
                $rec = new Recurrence($store, $request);
                
                // Only check for conflicts in the first X months, otherwise processing would become too
                // complicated.
                $reqitems = $rec->GetItems($props[$proptags['startdate']], $props[$proptags['startdate']] + RECURRENCE_AVAILABILITY_RANGE);
                
                // Get all the possible conflicts in the coming X months
                debugLog('Getting conflicts from ' . strftime('%x %X', $props[$proptags['startdate']]) . '\n');
                debugLog('Getting conflicts to ' . strftime('%x %X', $props[$proptags['startdate']] + RECURRENCE_AVAILABILITY_RANGE) . '\n');
                
                $possibleconflicts = getCalendarItems($store, $calendar, $props[$proptags['startdate']], $props[$proptags['startdate']] + RECURRENCE_AVAILABILITY_RANGE, $proptags);
            } else {
                $reqitems = array($props);
                
                // Only look at possible conflicts during the duration of the item
                $possibleconflicts = getCalendarItems($store, $calendar, $props[$proptags['startdate']], $props[$proptags['duedate']], $proptags);
            }
            
            $conflicts = array();
            
            foreach($reqitems as $reqitem) {
                // Check for conflicting appointments        
                $start = $reqitem[$proptags['startdate']];
                $end = $reqitem[$proptags['duedate']];
                
                debuglog("Checking availability from " . strftime("%x %X", $start) . " to " . strftime("%x %X", $end) . "\n");
                
                $items = intersectAppointmentWithList($reqitem, $possibleconflicts, $proptags);
                
                debuglog("Found " . count($items) . " overlapping records\n");
                
                $currentdepth = getOverlapDepth($items, $proptags, $props[$proptags['goid2']]);
                
                debuglog("Overlap depth is " . $currentdepth . "\n");
                
                if($currentdepth >= $capacity) {
                    $conflicts[] = $reqitem;
                }
            }
            
            if(count($conflicts) > 0) {
                // At least one conflict
                if(count($conflicts) == count($reqitems)) {
                    $body = _("The requested time slot is unavailable");
                } else {
                    $body = _("The requested time slots are unavailble on the following dates:") . "\n\n";
                    
                    foreach($conflicts as $conflict) {
                        $body .= strftime(_("%x %X"), $conflict[$proptags["startdate"]]) . " - " . strftime(_("%x %X"), $conflict[$proptags["duedate"]]) . "\n";
                    }
                }
                
                $mr->doDecline(true, false, false, $body);
                debuglog("Declined due to capacity reached.\n");
                return true;
            }
        }
        
        // Checks passed, book the meeting
        $ceid = $mr->doAccept(false, true, true);
		if ($ceid === false) {
			debuglog("Failed to accept: " . sprintf("0x%X", mapi_last_hresult()) . "\n");
			return false;
		}
        debuglog("Accepted.\n");
		// reopen entry to add self as BCC recipient for ZCP-9901
		$calitem = mapi_msgstore_openentry($store, $ceid);
		if ($calitem) {
			$storeprops = mapi_getprops($store, array(PR_MAILBOX_OWNER_ENTRYID));
			$ab = mapi_openaddressbook($session);
			$mailuser = mapi_ab_openentry($ab, $storeprops[PR_MAILBOX_OWNER_ENTRYID]);
			$recip = mapi_getprops($mailuser, array(PR_ACCOUNT, PR_ADDRTYPE, PR_DISPLAY_NAME, PR_DISPLAY_TYPE, PR_DISPLAY_TYPE_EX,
													PR_EMAIL_ADDRESS, PR_ENTRYID, PR_OBJECT_TYPE, PR_SEARCH_KEY, PR_SMTP_ADDRESS));
			$recip[PR_RECIPIENT_ENTRYID] = $recip[PR_ENTRYID];
			$recip[PR_RECIPIENT_FLAGS] = 256 | 1;
			$recip[PR_RECIPIENT_TRACKSTATUS] = 0;
			$recip[PR_RECIPIENT_TYPE] = MAPI_BCC;
			// not setting PidLidAllAttendees, not important
			mapi_message_modifyrecipients($calitem, MODRECIP_ADD, array($recip));
			mapi_message_savechanges($calitem);
			debuglog("Accept updated.\n");
		} else {
			debuglog("Unable to update accepted item.\n");
		}
        return true;
    } else if($mr->isMeetingCancellation()) {
        $mr->processMeetingCancellation();
        $mr->doRemoveFromCalendar();
        debuglog("Removed canceled meeting\n");
        return true;
    }
}

function forceUTF8($category)
{
    $old_locale = setlocale($category, "");
    if(!isset($old_locale) || !$old_locale) {
        print "Unable to initialize locale\n";
        exit(1);
    }
    $dot = strpos($old_locale, ".");
    if($dot) {
        if(strrchr($old_locale, ".") == ".UTF-8" || strrchr($old_locale, ".") == ".utf8")
            return true;
        $old_locale = substr($old_locale, 0, $dot);
    }
    $new_locale = $old_locale . ".UTF-8";
    $old_locale = setlocale($category, $new_locale);
    if(!$old_locale) {
        $new_locale = "en_US.UTF-8";
        $old_locale = setlocale($category, $new_locale);
    }
    if(!$old_locale) {
        print "Unable to set locale $new_locale\n";
        exit(1);
    }
    
    return true;
}

// Since the username we are getting from the commandline is always in utf8, we have
// to force LC_CTYPE to an UTF-8 language. This makes sure that opening the user's store
// will always open the correct user's store.

forceUTF8(LC_CTYPE);
forceUTF8(LC_MESSAGES);
textdomain("zarafa");

if(count($argv) != 3 && count($argv) != 4) {
    print "Usage: " . $argv[0] . " <username> <path/to/dagent.cfg> [<entryid>]\n";
    print
    print "If <entryid> is not specified, all unresponded MR's in the inbox are processed\n";
    exit(1);
}

$username = $argv[1];
$config = $argv[2];
if(isset($argv[3]))
    $entryid = $argv[3];

$settings = parseConfig($config);

if(!$settings || !isset($settings["server_socket"])) {
    $settings["server_socket"] = "file:///var/run/zarafa";
}

if(isset($settings["sslkey_file"]) && isset($settings["sslkey_pass"]))
    $session = mapi_logon_zarafa($username, "", $settings["server_socket"], $settings["sslkey_file"], $settings["sslkey_pass"]);
else
    $session = mapi_logon_zarafa($username, "", $settings["server_socket"]);
    
$store = GetDefaultStore($session);

$capacity = getCapacity($session, $store);
$policy = getPolicy($store);

debuglog("Policy is " . $policy . "\n");

if(($policy & POLICY_PROCESS_MEETING_REQUESTS) == 0) {
    debuglog("Policy auto-respond not set.\n");
}

debuglog("Resource capacity is $capacity\n");

$inbox = mapi_msgstore_getreceivefolder($store);

if(isset($entryid)) {
    $items = array (hex2bin($entryid));
} else {
    $items = getUnresponded($inbox);
}

debuglog("Found " . count($items) . " items to process\n");

foreach ($items as $item) {
    autoRespond($session, $store, $item, $capacity, $policy);
}

$storeprops = mapi_getprops($store, array(PR_MAILBOX_OWNER_ENTRYID));

$fb = new FreeBusyPublish($session, $store, getCalendar($store), $storeprops[PR_MAILBOX_OWNER_ENTRYID]);

$fb->PublishFB(time() - (7 * 24 * 60 * 60), 6 * 30 * 24 * 60 * 60); // publish from one week ago, 6 months ahead

exit(0);

?>
