<?php
// ics_attachment.php
// RJM Programming
// November, 2016
// Based on PHPtoICS.php at https://gist.github.com/vtedesco/5671155 ... in the last if ... thanks

$start_end_offsets = array(0.0, 0.0);

function ourtimezonelist($ididea) { // thanks to http://php.net/manual/en/function.timezone-identifiers-list.php
  $idi=1;
  if (strpos($ididea, "end") !== false) $idi=2;
  $selstuff='<select id="s' . $ididea . '" onchange=" changetz(' . $idi . ',this.value,this); "><option title="" value="" selected>Local</option><option title="" value="0">GMT</option></select>';
  for ($xoff=-24.0; $xoff<=24.0; $xoff+=0.5) {
    $selstuff=str_replace("</select>", "<option title='' value='" . str_replace("+-","-",("+" . (0.0 - $xoff))) . "'>" . str_replace("+-","-",("+" . $xoff . " hours")) . "</option></select>", $selstuff);
  }
  $zones = timezone_identifiers_list();
  foreach ($zones as $zone) {
    $origzone=$zone;
    //$ourtz=new DateTimeZone($zone);
    $zone = explode('/', $zone); // 0 => Continent, 1 => City
    // Only use "friendly" continent names
    if ($zone[0] == 'Africa' || $zone[0] == 'America' || $zone[0] == 'Antarctica' || $zone[0] == 'Arctic' || $zone[0] == 'Asia' || $zone[0] == 'Atlantic' || $zone[0] == 'Australia' || $zone[0] == 'Europe' || $zone[0] == 'Indian' || $zone[0] == 'Pacific') {       
        if (isset($zone[1]) != '') {
            //$locations[$zone[0]][$zone[0]. '/' . $zone[1]] = str_replace('_', ' ', $zone[1]); // Creates array(DateTimeZone => 'Friendly name')
            //$ourtz=new DateTimeZone(str_replace('_', ' ', $zone[0]. '/' . $zone[1]));
            //$selstuff=str_replace("</select>", "<option title='" . $ourtz->getLocation()[latitude] . "," . $ourtz->getLocation()[longitude] . ",~" . urlencode(str_replace('_', ' ', $zone[1])) . "~' value='" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "'>" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "</option></select>", $selstuff);
            $selstuff=str_replace("</select>", "<option title='" . $origzone . "' value='" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "'>" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "</option></select>", $selstuff);
            //$selstuff=str_replace("</select>", "<option value='" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "'>" . str_replace('_', ' ', $zone[0]. '/' . $zone[1]) . "</option></select>", $selstuff);
        }
    }
  }
  return $selstuff;
}

function dateselcombo($parid, $defstr) {
  $mths=["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
  $yyyystr="<input style='width:50px;' onchange=\"fixthis('" . $parid . "',0,4,this.value);\" type=number step=1 id='yyyy" . $parid . "' value=" . substr($defstr,0,4) . "></input>";
  $mmstr="<select onchange=\"fixthis('" . $parid . "',4,6,this.value);\" id='mm" . $parid . "'><option value='" . substr($defstr,4,2) . "'>" . substr($defstr,4,2) . "</option>";
  $prefix="0";
  for ($i=1; $i<=9; $i++) $mmstr.="<option value='" . $prefix . $i . "'>" . $mths[$i] . "</option>";
  $prefix="";
  for ($i=10; $i<=12; $i++) $mmstr.="<option value='" . $prefix . $i . "'>" . $mths[$i] . "</option>";
  $mmstr.="</select>";
  $ddstr="<select onchange=\"fixthis('" . $parid . "',6,8,this.value);\" id='dd" . $parid . "'><option value='" . substr($defstr,6,2) . "'>" . substr($defstr,6,2) . "</option>";
  $prefix="0";
  for ($i=1; $i<=9; $i++) $ddstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $prefix="";
  for ($i=10; $i<=31; $i++) $ddstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $ddstr.="</select>";
  $hhstr="<select onchange=\"fixthis('" . $parid . "',9,11,this.value);\" id='hh" . $parid . "'><option value='" . substr($defstr,9,2) . "'>" . substr($defstr,9,2) . "</option>";
  $prefix="0";
  for ($i=0; $i<=9; $i++) $hhstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $prefix="";
  for ($i=10; $i<=23; $i++) $hhstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $hhstr.="</select>";
  $mistr="<select onchange=\"fixthis('" . $parid . "',11,13,this.value);\" id='mi" . $parid . "'><option value='" . substr($defstr,11,2) . "'>" . substr($defstr,11,2) . "</option>";
  $prefix="0";
  for ($i=0; $i<=9; $i++) $mistr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $prefix="";
  for ($i=10; $i<=59; $i++) $mistr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $mistr.="</select>";
  $ssstr="<select onchange=\"fixthis('" . $parid . "',13,15,this.value);\" id='ss" . $parid . "'><option value='" . substr($defstr,13,2) . "'>" . substr($defstr,13,2) . "</option>";
  $prefix="0";
  for ($i=0; $i<=9; $i++) $ssstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $prefix="";
  for ($i=10; $i<=59; $i++) $ssstr.="<option value='" . $prefix . $i . "'>" . $prefix . $i . "</option>";
  $ssstr.="</select>";
  return $yyyystr . $mmstr . $ddstr . ":" . $hhstr . $mistr . $ssstr;
}

if (isset($_GET['msg'])) {
$dhuh=urldecode($_GET['msg']) . ' ';
//exit;
if (strcmp(substr($dhuh,0,1),"0") >= 0 && strcmp(substr($dhuh,0,1),"9") <= 0) {
$pcont=@file_get_contents("ics_attachment.php");
$pbits=explode("/" . "/ " . $_GET['msg'] . " ", explode('?' . '>', $pcont)[0]);
if (sizeof($pbits) > 1) {
  $postpbits=explode("/" . "/ ", explode("/" . "/ " . (1 + $_GET['msg']) . " ", $pbits[-1 + sizeof($pbits)])[0]);
  $msg="";
  $okay=true;
  for ($i=0; $i<sizeof($postpbits); $i++) {
    if (substr($postpbits[$i],0,9) == "999999999") {
      $okay=false;
    } else if ($okay) {
      $msg.=$postpbits[$i];
    }
  }
  echo "<!doctype html>
<html>
<head>
<title>Calendar *.ics file creation - RJM Programming - November, 2016</title>
</head>
<body style='background-color:yellow;'><p>" . $msg . "</p></body></html>";
} else {
  echo "";
}
} else {
$ourtz=new DateTimeZone(urldecode($_GET['msg']));
$ztz=$ourtz->getLocation()[latitude] . "," . $ourtz->getLocation()[longitude] . ",~" . urlencode(str_replace('_', ' ', explode('/', urldecode($_GET['msg']))[1])) . "~";
  echo "<!doctype html>
<html>
<head>
<title>Calendar *.ics file creation - RJM Programming - November, 2016</title>
<scr" . "ipt type='text/javascript'>
function domap(withthis) {
var mapurl=\"http://www.rjmprogramming.com.au/PHP/Map/map.php?title=Places&onclick=y&label=['Lat',&value='Lon','Name']&data=,[0.0000,0.0000,~Timbuktu~]\";
var maphuh='0.0000,0.0000,~Timbuktu~';
    if (withthis != '') {
      if (parent.document.getElementById('myplaceiframe')) {
        //mapurl=parent.document.getElementById('divmap').innerHTML.replace(']','],[' + withthis + ']');
        parent.document.getElementById('myplaceiframe').src+=',[' + withthis + ']';
        mapurl=\"http://www.rjmprogramming.com.au/PHP/Map/map.php?title=Places&onclick=y&label=['Lat',&value='Lon','Name']&data=,[0.0000,0.0000,~Timbuktu~]\";
      } else if (mapurl.indexOf(maphuh) != -1) {
        mapurl=mapurl.replace(maphuh,withthis);
        if (parent.document.getElementById('divmap').innerHTML == '') {
         parent.document.getElementById('divmap').innerHTML='<iframe id=myplaceiframe width=450 height=420 src=\"' + mapurl + '\"></iframe>';
        } else {
         parent.document.getElementById('myplaceiframe').src=mapurl;
        }
      } else {
        mapurl=mapurl.replace(']','],[' + withthis + ']');
        parent.document.getElementById('myplaceiframe').src=mapurl;
        mapurl=\"http://www.rjmprogramming.com.au/PHP/Map/map.php?title=Places&onclick=y&label=['Lat',&value='Lon','Name']&data=,[0.0000,0.0000,~Timbuktu~]\";
      }
    }
}
</scr" . "ipt>
</head>
<body onload=\"domap('" . $ztz . "');\"></body></html>";
}
} else if (!isset($_POST['id']) && !isset($_GET['id'])) {
date_default_timezone_set('Australia/Perth');	
$qbits=@file_get_contents("ics_attachment.php");
$msgno=1;
while (strpos($qbits, "/" . "/ " . $msgno . " ") !== false) {
  $msgno++;
}
echo "<!doctype html>
<html>
<head>
<title>Calendar *.ics file creation - RJM Programming - November, 2016</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<style>
input { width:30%; }
</style>
<script type='text/javascript'>
var mapurl=\"http://www.rjmprogramming.com.au/PHP/Map/map.php?title=Places&onclick=y&label=['Lat',&value='Lon','Name']&data=,[0.0000,0.0000,~Timbuktu~]\";
var maphuh='0.0000,0.0000,~Timbuktu~';
function changetz(itz,tzwith,tdd) {
 if (tzwith != '') {
  var sofars=document.getElementById('tz').value.split(',');
  if (tzwith.substring(0,1) == '-' || tzwith.substring(0,1) == '+' || (tzwith.substring(0,1) >= '0' && tzwith.substring(0,1) <= '9')) {
    if (itz == 2) {
      document.getElementById('tz').value=sofars[0] + ',' + tzwith;
    } else if (sofars.length == 1) {
      //document.getElementById('tz').value=tzwith;
      if (itz == 1) {
        document.getElementById('tz').value=tzwith + ',' + sofars[0];
      } else {
        document.getElementById('tz').value=sofars[0] + ',' + tzwith;
      }
    } else {
      document.getElementById('tz').value=tzwith + ',' + sofars[1];
    }
    //alert(document.getElementById('tz').value);
  } else {
    if (tdd.options[tdd.selectedIndex].title.indexOf(',') != -1) {
      //alert(tdd.options[tdd.selectedIndex].title);
      if (mapurl.indexOf(maphuh) != -1) {
        mapurl=mapurl.replace(maphuh,tdd.options[tdd.selectedIndex].title);
        if (document.getElementById('divmap').innerHTML == '') {
         document.getElementById('divmap').innerHTML='<iframe id=myplaceiframe width=450 height=420 src=\"' + mapurl + '\"></iframe>';
        } else {
         document.getElementById('myplaceiframe').src=mapurl;
        }
      } else {
        mapurl=mapurl.replace(']','],[' + tdd.options[tdd.selectedIndex].title + ']');
        document.getElementById('myplaceiframe').src=mapurl;
        mapurl=\"http://www.rjmprogramming.com.au/PHP/Map/map.php?title=Places&onclick=y&label=['Lat',&value='Lon','Name']&data=,[0.0000,0.0000,~Timbuktu~]\";
      }
    } else {
      //alert(document.URL.split('?')[0].split('#')[0] + '?msg=' + encodeURIComponent(tdd.options[tdd.selectedIndex].title));
      //document.getElementById('mycaltwo').src=document.URL.split('?')[0].split('#')[0] + '?msg=' + encodeURIComponent(tdd.options[tdd.selectedIndex].title);
      //alert('<iframe id=mycaltwo src=' + document.URL.split('?')[0].split('#')[0] + '?msg=' + encodeURIComponent(tdd.options[tdd.selectedIndex].title) + ' style=display:none;></iframe>');
      document.getElementById('divcaltwo').innerHTML='<iframe id=mycaltwo src=' + document.URL.split('?')[0].split('#')[0] + '?msg=' + encodeURIComponent(tdd.options[tdd.selectedIndex].title) + ' style=display:none;></iframe>';
    }
    if (itz == 2) {
      document.getElementById('tz').value=sofars[0] + ',' + tzwith;
    } else if (sofars.length == 1) {
      if (itz == 1) {
        document.getElementById('tz').value=tzwith + ',' + sofars[0];
      } else {
        document.getElementById('tz').value=sofars[0] + ',' + tzwith;
      }
    } else {
      document.getElementById('tz').value=tzwith + ',' + sofars[1];
    }
  }
 }
}
function prefixthis(which,withthis) {
 //alert(withthis);
 fixthis('yyyy' + which, 0, 0, withthis.substring(0,4));
 fixthis('mm' + which, 0, 0, withthis.substring(4,6));
 fixthis('dd' + which, 0, 0, withthis.substring(6,8));
 fixthis('hh' + which, 0, 0, withthis.substring(9,11));
 fixthis('mi' + which, 0, 0, withthis.substring(11,13));
 fixthis('ss' + which, 0, 0, withthis.substring(13,15));
}
function fixthis(which,at1,at2,withthis) {
 if (withthis.length > 0) {
  if (which == 'datestart' || which == 'dateend') {
   var wasstr=document.getElementById(which).value;
   if (at1 == 0) {
    //alert('huh1=' + withthis + wasstr.substring(withthis.length));
    document.getElementById(which).value=withthis + wasstr.substring(withthis.length);
   } else {
    //alert('huh2=' + wasstr.substring(0,at1) + withthis + wasstr.substring(at2));
    document.getElementById(which).value=wasstr.substring(0,at1) + withthis + wasstr.substring(at2);
   }
  } else {
   //alert('huh3=' + withthis);
   document.getElementById(which).value=withthis;
  }
 }
}
function dodates() {
  var isv1='', isv2='';
  var dd=new Date();
  //alert(eval((0.0 + eval(dd.toTimeString().replace('-',' ').replace('+',' ').split(' ')[2] % 100)) / 60.0));
  var qw=eval(eval((eval(dd.toTimeString().replace('-',' ').replace('+',' ').split(' ')[2]) - eval(dd.toTimeString().replace('-',' ').replace('+',' ').split(' ')[2] % 100)) / 100) + eval((0.0 + eval(dd.toTimeString().replace('-',' ').replace('+',' ').split(' ')[2] % 100)) / 60.0));
  if (dd.toTimeString().indexOf('+') != -1) qw=-qw;
  document.getElementById('tz').value=qw;
  isv1='' + dd.getFullYear() + ('0' + eval(1 + dd.getMonth())).slice(-2) + ('0' + dd.getDate()).slice(-2) + ':' + dd.toTimeString().split(' ')[0].replace(/:/g,'');
  //alert(isv1); //dd.getFullYear() + ' + ' + ('0' + eval(1 + dd.getMonth())).slice(-2) + ' + ' + ('0' + dd.getDate()).slice(-2) + ' + ' + dd.toTimeString().split(' ')[0].replace(/:/g,''));
  //document.getElementById('datestart').value=isv1;
  prefixthis('datestart',isv1);
  isv2='' + dd.getFullYear() + ('0' + eval(1 + dd.getMonth())).slice(-2) + ('0' + dd.getDate()).slice(-2) + ':' + dd.toTimeString().split(' ')[0].replace(/:/g,'');
  //document.getElementById('dateend').value=isv2;
  prefixthis('dateend',isv2);
  document.getElementById('datestart').value=isv1;
  document.getElementById('dateend').value=isv2;
  //alert(isv1);
  document.getElementById('title').focus();
}
</script>
</head>
<body style='background-color:yellow;' onload=' dodates(); '>
<table><tr><td>
<h1>Create Calendar iCal File</h1>
<h3>RJM Programming</h3>
<h3>November, 2016</h3>
<h4>Thanks to <a target=_blank title='https://gist.github.com/vtedesco/5671155' href='https://gist.github.com/vtedesco/5671155'>https://gist.github.com/vtedesco/5671155</a></h4>
<form target='mycal' action='./ics_attachment.php' method='POST'>
<input type='hidden' id='tz' name='tz' value='-10'></input>
<input type='hidden' id='id' name='id' value='" . $msgno . "'></input><br><br>
Title: <input type='text' id='title' name='title' value=''></input><br><br>
Address: <input type='text' id='address' name='address' value=''></input><br><br>
Description: <input type='text' id='description' name='description' value=''></input><br><br>
Start Date (YYYYmmdd:HH24MISS) <br>" . ourtimezonelist('datestart') . " Time: <input style='width:100px !important;' type='text' onblur=\" prefixthis('datestart',this.value);\" id='datestart' name='datestart' value='" . date("Ymd:His") . "'></input> or " . dateselcombo("datestart",date("Ymd:His")) . "<br><br>
End Date (YYYYmmdd:HH24MISS) <br>" . ourtimezonelist('dateend') . " Time: <input style='width:100px !important;' type='text' onblur=\" prefixthis('dateend',this.value);\" id='dateend' name='dateend' value='" . date("Ymd:His") . "'></input> or " . dateselcombo("dateend",date("Ymd:His")) . "<br><br>
Subtitle: <input type='text' id='stage' name='stage' value=''></input><br><br>
Event<br>Recall<br>Text: <textarea rows=6 cols=80 name='eventwords' id='eventwords' value=''></textarea><br><br>
<input type='submit' value='Create iCal Calendar Entry' style='background-color:pink;'></input>
<input type='text' id='junk' value='' style='margin-left:-98765px;width:1px;height:1px;'></input>
</form>
</td><td><div id='divmap'></div></td></tr></table>
<iframe id='mycal' src='about:blank' style='display:none;' name='mycal'></iframe>
<div id='divcaltwo'></div>
</body>
</html>
";
} else {
if ((isset($_POST['eventwords']) && isset($_POST['id']) ) || (isset($_GET['eventwords']) && isset($_GET['id']) )) {
if (isset($_GET['eventwords']) && isset($_GET['id'])) {
$ptmsg=explode(" ", "/" . "/ " . $_GET['id'] . " " . urldecode($_GET['eventwords']));
} else {
$ptmsg=explode(" ", "/" . "/ " . $_POST['id'] . " " . urldecode($_POST['eventwords']));
}
//echo sizeof($ptmsg);
if (sizeof($ptmsg) > 3) {
if ($ptmsg[2] != "") {
//echo "here";
//exit;
if (isset($_GET['eventwords']) && isset($_GET['id'])) {
file_put_contents("ics_attachment.php", explode('?' . '>', file_get_contents("ics_attachment.php"))[0] . "\n/" . "/ " . $_GET['id'] . " " . str_replace("\n", "\n/" . "/ ", str_replace('?' . '>', '', urldecode($_GET['eventwords']))) . "\n" . "?" . ">");
} else {
file_put_contents("ics_attachment.php", explode('?' . '>', file_get_contents("ics_attachment.php"))[0] . "\n/" . "/ " . $_POST['id'] . " " . str_replace("\n", "\n/" . "/ ", str_replace('?' . '>', '', urldecode($_POST['eventwords']))) . "\n" . "?" . ">");
}
} else {
$x="";
}
} else {
$x="";
}
}
// Fetch vars
if (isset($_GET['id'])) {
$event = array(
	'id' => $_GET['id'],
	'title' => urldecode($_GET['title']),
    'address' => urldecode($_GET['address']),
	'description' => urldecode($_GET['description']),
	'datestart' => $_GET['datestart'],
	'dateend' => $_GET['dateend'],
	'address' => urldecode($_GET['stage'])
);
} else {
$event = array(
	'id' => $_POST['id'],
	'title' => urldecode($_POST['title']),
    'address' => urldecode($_POST['address']),
	'description' => urldecode($_POST['description']),
	'datestart' => $_POST['datestart'],
	'dateend' => $_POST['dateend'],
	'address' => urldecode($_POST['stage'])
);
}
// iCal date format: yyyymmddThhiissZ
// PHP equiv format: Ymd\This
// The Function
function dateToCal($time) {
  date_default_timezone_set('GMT');	
  //echo "\n" . date('Ymd\This', $time) . 'Z';
  return date('Ymd\This', $time) . 'Z';
}
function tzbit($ts,$ofw) {
  global $start_end_offsets;
  $thisi=0;
  $outts=$ts;
  if (isset($_POST['tz']) || isset($_GET['tz'])) {
    if ((isset($_POST['tz']) && $_POST['tz'] != "") || (isset($_GET['tz']) && $_GET['tz'] != "")) {
      date_default_timezone_set('Europe/London');	
          //echo urldecode($_POST['tz']) . ' ... ' . $ofw . ' ... ';
      if (isset($_GET['tz'])) {
        $posttz=$_GET['tz'];
      } else {
        $posttz=$_POST['tz'];
      }
      if (strpos(urldecode($posttz), ",") !== false) {
        $ourtzs=explode(",", urldecode($posttz));
        if (strpos($ofw, "end") !== false) {
          $thisi=1;
        }
        if (strpos($ourtzs[$thisi], "/") !== false) {
          $r_dtz = new DateTimeZone($ourtzs[$thisi]);
          date_default_timezone_set('Europe/London');	
          $dateTimeGMT = new DateTime("now");
          $start_end_offsets[$thisi] = (0.0 - ($r_dtz->getOffset($dateTimeGMT) / (60.0 * 60.0)));
          //echo $ourtzs[$thisi] . " offset is " . (0.0 - ($r_dtz->getOffset($dateTimeGMT) / (60.0 * 60.0)));
          //exit;
        } else {
        //if (strpos($ofw, "end") !== false) {
        //  echo ' +++ ' . $ourtzs[$thisi] . ' --- ';
        //}
          $start_end_offsets[$thisi] = floatval(str_replace('+','',$ourtzs[$thisi]));
        }
      } else if (strpos(urldecode($posttz), "/") === false) {
        $start_end_offsets[0] = floatval(str_replace('+','',urldecode($posttz)));
      } else {
        $r_dtz = new DateTimeZone($ourtzs[$thisi]);
        $dateTimeGMT = new DateTime("now");
        $start_end_offsets[$thisi] = (0.0 - ($r_dtz->getOffset($dateTimeGMT) / (60.0 * 60.0)));
          //echo $ourtzs[$thisi] . " offset Is " . (0.0 - ($r_dtz->getOffset($dateTimeGMT) / (60.0 * 60.0)));
          //exit;
      }
      $di="PT" . str_replace("-","",("" . $start_end_offsets[$thisi])) . "H";
      $parsed_date = DateTime::createFromFormat('Ymd:His', $ts);
      if (strpos(("" . $start_end_offsets[$thisi]), "-") !== false) {
              //if (strpos($ofw, "end") !== false) {
              //   echo "sub" . $di;
			  //}
	  if (strpos($di, ".") !== false) {
      $parsed_date->sub(new DateInterval(explode(".",$di)[0] . "H"));
      $parsed_date->sub(new DateInterval("PT30M"));
	  } else {
      $parsed_date->sub(new DateInterval($di));
      }
      } else {
              //if (strpos($ofw, "end") !== false) {
              //   echo "add" . $di . " " . urldecode($_POST['tz']) . " start_end_offsets[0] = " . $start_end_offsets[0] . " and start_end_offsets[1] = " . $start_end_offsets[1] . " ";
			  //}
	  if (strpos($di, ".") !== false) {
      $parsed_date->add(new DateInterval(explode(".",$di)[0] . "H"));
      $parsed_date->add(new DateInterval("PT30M"));
	  } else {
      $parsed_date->add(new DateInterval($di));
      }
      }
      $outts = $parsed_date->format('Ymd:His');
              //if (strpos($ofw, "end") !== false) {
               //  echo " " . $outts . " ";
			  //}
      //echo $outts . " " . $di . " ";
      date_default_timezone_set('Australia/Perth');	
    }
  }
  return $outts;
}  
// Build the ics file
$ical = 'BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTEND:' . str_replace(':','T',tzbit($event['dateend'],'dateend')) . 'Z
UID:' . md5($event['title']) . '
DTSTAMP:' . time() . '
LOCATION:' . addslashes($event['address']) . '
DESCRIPTION:' . addslashes($event['description']) . '
URL;VALUE=URI:http://www.rjmprogramming.com.au/PHP/ics_attachment.php?msg=' . $event['id'] . '
SUMMARY:' . addslashes($event['title']) . '
DTSTART:' . str_replace(':','T',tzbit($event['datestart'],'datestart')) . 'Z
END:VEVENT
END:VCALENDAR';
//echo $ical;
//exit;
//set correct content-type-header
if ($event['id']) {
	header('Content-type: text/calendar; charset=utf-8');
	header('Content-Disposition: attachment; filename=rjmprogramming-event.ics');
	echo $ical;
} else {
	// If $id isn't set, then kick the user back to home. Do not pass go, and do not collect $200.
	header('Location: /');
}
}
// -1 No message
// 999999999 No message
?>