<?php
	/**************************************************************************\
	* phpGroupWare - iCal readonly exporter                                    *
	* http://www.phpgroupware.org                                              *
	* Copyright (c) 2003 Free Software Foundation Inc			   *
	* Written by Dave Hall - dave.hall at mbox.com.au			   *
	* Based on anon_wrapper written by Dan Kuykendall <seek3r@phpgroupware.org>*
	* --------------------------------------------                             *
	*  This program is free software; you can redistribute it and/or modify it *
	*  under the terms of the GNU General Public License as published by the   *
	*  Free Software Foundation; either version 2 of the License, or (at your  *
	*  option) any later version.                                              *
	\**************************************************************************/

	/* $Id: ical.php,v 1.1.1.1 2004/04/06 21:13:44 kazuyan Exp $ */

	/**
	* NOTES:
	*
	* This code is experimental - use it at your own risk
	*
	* Configuration
	* Change only the following 3 lines
	* login 
	* - the login you want to use for the iCal script to establish a session
	* - make sure the user exists and has access to NO apps! this user just establishes a session
	*
	* passwd 
	* - change this password to match what you have setup for this account
	*
	* domain
	* - change this if you want to only allow access to this script from a single domain
	*   for single domains and hostname based domain detection leave as is.
	*
	* prev & adv
	* - change these at your own risk
	*
	* exit; comment this line below (so it reads //exit;) to enable this script
	*
	* If you don't understand these instructions, you should not use this script
	*
	* Usage
	* Call this script using a url similar to https://phpgw_user:password@server.com/phpgroupware/calendar/ical.php
	* As the password is sent in the url, it should only be run using HTTPS
	*/
	
	$login  = 'user';
	$passwd = 'changeme';
	$domain = $_SERVER['SERVER_NAME']; //'default';

	$prev = 1; // number of previous months
	$adv = 3; // number of months in advance
	exit;

	// ** DO NOT EDIT BELOW THIS LINE ** //
	if(!$_SERVER['PHP_AUTH_USER'])
	{
		header('WWW-Authenticate: Basic realm="phpGW-iCal"');
		header('HTTP/1.0 401 Unauthorized');
		echo '<p>You must call this script using ';
		list($ignore, $url) = explode('//', $_SERVER['PHP_SELF']);
		echo  '"webcal://phpgw_user:password@' . $url . '"</p>';
		exit;
	}
	@set_time_limit(0);
	
	//multiple domain support hack
	$_GET['domain'] = $domain;
	
	$GLOBALS['phpgw_info']['flags'] = array(
		'disable_Template_class' => True,
		'login' => True,
		'currentapp' => 'login',
		'noheader'  => True
	);
	include('../header.inc.php');
	header('Content-type: text/calendar');

	$login  = $_SERVER['PHP_AUTH_USER']; 
	$passwd = $_SERVER['PHP_AUTH_PW'];
	$uid = $GLOBALS['phpgw']->accounts->name2id($login);

	$sessionid = $GLOBALS['phpgw']->session->create($login,$passwd,'text');

	$params = array('owner' => $GLOBALS['phpgw']->accounts->name2id($login));

	$so = createObject('calendar.socalendar', array('owner' => $uid) );
	$export = createObject('calendar.boicalendar');
	
	list($start['d'], $start['m'], $start['y']) = explode('-', date('d-n-Y', strtotime("-$prev months")));
	list($end['d'], $end['m'], $end['y']) = explode('-', date('d-n-Y', strtotime("+$adv months")));
	$ids = $so->list_events($start['y'], $start['m'], $start['d'], $end['y'], $end['m'], $end['d']);
	
	//this is done so there is streaming output
	foreach($ids as $id)
	{
		echo $export->export( array('l_event_id' => $id) );
	}
?>
