Location: PHPKode > scripts > tiCalFile > tiCalFile-2.4/tiCalFile.php
<?php
/**
 * tiCalFile 2.4
 *
 * copyright (c) 2011 Kjell-Inge Gustafsson kigkonsult
 * www.kigkonsult.se/tiCalFile/index.php
 * hide@address.com
 * updated 20110422
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * create test iCal files with events. Configurable, start date, number of events etc.
 *
**/
            /** include iCalcreator, required */
require_once 'iCalcreator.class.php';
            /** include PEAR LOG or use attached file log class, eClog, or comment if no logging */
require_once 'Log.php';
            /** tiCalFile config */
$tifConfig = array();
$tifConfig['VERSION']      = 'tiCalFile 2.4';
            /** site unique id */
$tifConfig['UNIQUE']       = 'ical.net';
            /** folder for test files,  */
//$tifConfig['CALDIR']       = dirname(__FILE__).DIRECTORY_SEPARATOR.'calendars';
$tifConfig['CALDIR']       = '..'.DIRECTORY_SEPARATOR.'calendars';
            /** names of calendar files to check for and/or (re-)create */
$tifConfig['FILENAMES']    = array( 'testFile.ics' ); //, 'testFile2.ics' );
            /** Some (MS) calendar required properties */
$tifConfig['METHOD']       = 'PUBLISH';
$tifConfig['CALNAME']      = $tifConfig['VERSION'].' test calendar file';
$tifConfig['CALDESC']      = 'A test calendar file created by '.$tifConfig['VERSION'].' (Kjell-Inge Gustafsson, kigkonsult.se) and utilizing '.ICALCREATOR_VERSION;
$tifConfig['TIMEZONE']     = 'Europe/Stockholm'; // will also create one timezone and two standard/daylight components (not exact, but.. .)
            /** calendar startdate */
$tifConfig['THISDATE']     = date( 'YmdHis' ); // now
//$tifConfig['THISDATE']     = date( 'YmdHis', mktime( 0, 0, 0, 12, 27, 2008 ))); // new year test
            /** calendar age in hours, when to recreate calendar files, 0=never */
$tifConfig['CALAGE']       = 1;
            /** how many days to create events for */
$tifConfig['DAYCNT']       = 10;
            /** how many events every day */
$tifConfig['EVENTCNT']     = 5;
            /** event start time (hour) span, selected randomly within span */
$tifConfig['EVENTSTART']   = 7;
$tifConfig['EVENTEND']     = 18;
            /** duration in hours for events, selected randomly within span,
                BUT every 5th event is an 'alldayevent' (only start date) */
$tifConfig['EVENTDURMIN']  = 1;
$tifConfig['EVENTDURMAX']  = 36;
            /** Calendar optional recurrent properties, RRULE + EXRULE
               (in every 3rd event only, 50% with UNTIL (date), 50% with COUNT), remove from array to skip */
$tifConfig['OPTIONALS']    = array( 'RRULE' ); //, 'EXRULE' );
            /** when (hours before start) to create a display alarm, 0=never */
$tifConfig['DISPLAYALARM'] = 1;
            /** when (hours before start) to create an email alarm, 0=never */
$tifConfig['EMAILALARM']   = 36;
            /** set LANGUAGE parameter whenever (!) possible, FALSE=no language parameter */
$tifConfig['LANGUAGE']     = 'EN';
            /** set ALTREP parameter whenever (!) possible, FALSE=no altrep parameter */
$tifConfig['ALTREP']       = 'www.kigkonsult.se/docs/doc.txt';
            /** set X-param parameter whenever (!) possible, FALSE=no X-param parameter */
$tifConfig['X-PARAM']      = 'X-VALUE';
            /** set TZID parameter whenever (!) possible, FALSE=no TZID parameter */
$tifConfig['TZID']         = $tifConfig['TIMEZONE'];

            /** set FALSE (no log) or logfile (incl. path) and priority */
//$tifConfig['LOGFILE']      = FALSE; // no logging
$logpath                   = realpath( dirname(__FILE__).DIRECTORY_SEPARATOR.'..' );
$tifConfig['LOGFILE']      = $logpath.DIRECTORY_SEPARATOR.date('Ymd').'.log'; // daily log file name
//$tifConfig['LOGFILE']      = $logpath.DIRECTORY_SEPARATOR.date('YW').'.log'; // weekly log file name
//$tifConfig['LOGFILE']      = $logpath.DIRECTORY_SEPARATOR.'tiCalFile.log'; // fixed log file name
            /** strftime format, eClog default '%Y-%m-%d %T' */
$tifConfig['LOGTIMEFORMAT']=  '%Y-%m-%d %T';
            /** priority levels, explore priorities in PEAR LOG or eClog.class.php, ex. 7=debug, 6=info, 3=error and major */
$tifConfig['LOGLEVEL']     = 7;
if( FALSE === $tifConfig['LOGFILE'] ) {
            /** dummy log class */
  class eClog {
    function __construct( $type, $filename, $ident='', $conf=FALSE, $priority=7 ) {}
    function log( $message, $priority = 7 ) {}
    function flush() {}
  }
  $tiflog = new eClog( 'file', $tifConfig['LOGFILE'], $tifConfig['VERSION'], array( 'timeFormat' => $tifConfig['LOGTIMEFORMAT'] ), $tifConfig['LOGLEVEL'] );
}
elseif( class_exists( 'Log' )) {
            /** using PEAR log */
  if( !class_exists( 'iCalLog' )) {
    class iCalLog extends Log {
      function __destruct() {
        $this->flush();
      }
    }
  }
  $tiflog = &iCalLog::singleton( 'file', $tifConfig['LOGFILE'], $tifConfig['VERSION'], array( 'timeFormat' => $tifConfig['LOGTIMEFORMAT'] ), $tifConfig['LOGLEVEL'] );
}
else {
            /** using eClog class (NOTE path) */
  require_once 'eClog.class.php';
  $tiflog = new eClog( 'file', $tifConfig['LOGFILE'], $tifConfig['VERSION'], array( 'timeFormat' => $tifConfig['LOGTIMEFORMAT'] ), $tifConfig['LOGLEVEL'] );
}
            /** initiate log */
$msg = $tifConfig['VERSION'].' (Kjell-Inge Gustafsson, kigkonsult.se) START ';
if( isset( $_SERVER ) && !empty( $_SERVER['REMOTE_ADDR'] ))
  $msg .= 'from IP:'.$_SERVER['REMOTE_ADDR'].' HOST:'.gethostbyaddr( $_SERVER['REMOTE_ADDR'] );
$tiflog->log( $msg, 5 );
            /* create and save to disk, a calendar with $tifConfig['EVENTCNT'] events every day for $tifConfig['DAYCNT'] days from today */
function createTestFile( $filename, & $tiflog ) {
  global $tifConfig;
  $dirFile  = $tifConfig['CALDIR'].DIRECTORY_SEPARATOR.$filename;
  $calendar = new vcalendar();
  $calendar->setConfig(   'unique_id',     $tifConfig['UNIQUE'] );
  if( !$calendar->setConfig('directory',   $tifConfig['CALDIR'] )) {
    $tiflog->log( 'ERROR (11) when setting directory \''.$tifConfig['CALDIR'].'\', check directory/file permissions!!', 3 );
    $tiflog->flush( "createTestFile '$dirFile'", 7 );
    return FALSE;
  }
  if( !$calendar->setConfig('filename', $filename )) {
    $tiflog->log( 'ERROR (12) when setting directory/file '.$dirFile.', check directory/file permissions!!', 3 );
    $tiflog->flush( "createTestFile '$dirFile'", 7 );
    return FALSE;
  }
  $calendar->setProperty( 'METHOD',        $tifConfig['METHOD'] );
  $calendar->setProperty( 'X-WR-CALNAME',  $tifConfig['CALNAME'] );
  $calendar->setProperty( 'X-WR-CALDESC',  $tifConfig['CALDESC'] );
  $calendar->setProperty( 'X-WR-TIMEZONE', $tifConfig['TIMEZONE'] );
  try {
    $dtz =  new DateTimeZone( $tifConfig['TIMEZONE'] );
    $tz = & $calendar->newComponent( 'vtimezone' );
    $tz->setproperty( 'tzid', $tifConfig['TIMEZONE'] );
    $transitions = $dtz->getTransitions();
    $stdDTSTART = $stdTZOFFSETTO = $stdTZOFFSETFROM = $stdTZNAME = $dlghtDTSTART = $dlghtTZOFFSETTO = $dlghtTZOFFSETFROM = $dlghtTZNAME = FALSE;
    foreach( $transitions as $trans ) {
      if( substr( $trans['time'], 0, 4 ) > ( date( 'Y' ) - 1 ))
        break;
      if( $trans['isdst'] !== TRUE ) {
        $stdDTSTART    = $trans['time'];
        $stdTZOFFSETTO = $dlghtTZOFFSETFROM = transOffset( $trans['offset'] );
        $stdTZNAME     = $trans['abbr'];
      }
      else {
        $dlghtDTSTART    = $trans['time'];
        $dlghtTZOFFSETTO = $stdTZOFFSETFROM = transOffset( $trans['offset'] );
        $dlghtTZNAME     = $trans['abbr'];
      }
    }
    if( $stdDTSTART && $stdTZOFFSETTO && $stdTZOFFSETFROM && $stdTZNAME && $dlghtDTSTART && $dlghtTZOFFSETTO && $dlghtTZOFFSETFROM && $dlghtTZNAME ) {
      $std   = & $tz->newComponent( 'standard' );
      $std->setProperty( 'dtstart', $stdDTSTART );
      $std->setProperty( 'tzname', $stdTZNAME );
      $std->setProperty( 'tzoffsetto', $stdTZOFFSETTO );
      $std->setProperty( 'tzoffsetfrom', $stdTZOFFSETFROM );
      $std->setProperty( 'RRULE', array( 'FREQ' => 'YEARLY', 'BYDAY' => array( '-1', 'DAY' => 'SU' ), 'BYMONTH' => 10 ));
      $dlght = & $tz->newComponent( 'daylight' );
      $dlght->setProperty( 'dtstart', $dlghtDTSTART );
      $dlght->setProperty( 'tzname', $dlghtTZNAME );
      $dlght->setProperty( 'tzoffsetto', $dlghtTZOFFSETTO );
      $dlght->setProperty( 'tzoffsetfrom', $dlghtTZOFFSETFROM );
      $dlght->setProperty( 'RRULE', array( 'FREQ' => 'YEARLY', 'BYDAY' => array( '-1', 'DAY' => 'SU' ), 'BYMONTH' => 3 ));
    }
  }
  catch( Exception $e ) {}
  $date       = mktime( 0, 0, 0, (int) substr( $tifConfig['THISDATE'], 4, 2 ), (int) substr( $tifConfig['THISDATE'], 6, 2 ), (int) substr( $tifConfig['THISDATE'], 0, 4 ));
  $stopDate   = $date + ($tifConfig['DAYCNT'] * 24 * 3600);
  $eventCount = 1;
            // random priority, 1 to 9. HIGH (1-4), MEDIUM (5), LOW (6-9)
            // reversed prio; HIGH: weight 1, MEDIUM weight 4, LOW: weight 8
  $prioArr = array();
  for( $r=1; $r<=9; $r++) {
    $weight = ( 5 < $r ) ? 1 : ( 5 > $r ) ? 8 : 4;
    for( $r1=1; $r1<=$weight; $r1++)
      $prioArr[] = $r;
  }
  mt_srand();
  shuffle( $prioArr );
            // array to randomly select a summary from
  $summaries = array( 'Duis ac dui sit amet ante auctor euismod.'
                    , 'Suspendisse_pellentesque_velit_in_tortor.'
                    , 'Mauris vulputate.'
                    , 'Nulla sapien pede, dapibus sed.'
                    , 'Maecenas tristique, pede_id_sollicitudin_posuere, enim nibh mollis odio.'
                    , 'Lorem ipsum dolor sit amet, consectetuerAdipiscingElit.' );
  $descriptions = array( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus tempus erat vel risus. Aenean augue velit, bibendum ac, porttitor ut, egestas vel, massa.'
                    , 'Sed varius dignissim est. Integer venenatis, odio vel imperdiet tincidunt, felis mi consectetur quam, at consequat justo dui eu ante. Nam nec lectus.'
                    , 'Aenean vel lorem sed tortor venenatis scelerisque. Integer eget felis. Sed id justo. Sed ullamcorper mauris nec eros. Praesent mattis ligula non est.'
                    , 'Donec libero dolor, tincidunt vel, fermentum at, condimentum ut, ante. Nam mi. Aliquam lobortis enim at mi. Donec ornare varius turpis.'
                    , 'Phasellus tempor dui nec velit. Quisque viverra, nunc vel facilisis accumsan, lorem lacus consequat libero, sed lobortis justo nisl at odio.'
                    , 'Pellentesque bibendum, ipsum et rutrum volutpat, arcu libero accumsan turpis, et egestas nunc purus et erat.'  );
  $comments = array(  'Praesent eros nibh, posuere at, hendrerit non, aliquam a, sapien. Curabitur commodo orci eget neque. Vivamus tempor posuere sapien.'
                    , 'Fusce id nisl ut enim consectetur pulvinar. Ut auctor elit at lorem. Maecenas purus arcu, faucibus eu, iaculis quis, pretium quis, velit.'
                    , 'Fusce vel sem quis felis dictum venenatis. Sed ipsum. Fusce vel diam sit amet sapien pretium pretium.'
                    , 'Sed nunc ligula, consequat vel, facilisis eu, vulputate sed, libero. Maecenas vel augue mattis risus elementum mattis.'
                    , 'Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Curabitur tempor sodales ante.');
  $days      = array( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' );
  $bydays    = array();
  $bydays[2] = array( array( 'DAY' => 'MO' ), array( 'DAY' => 'TU' ), array( 'DAY' => 'WE' ), array( 'DAY' => 'TH' ), array( 'DAY' => 'FR' ));
  $bydays[3] = array( array( 'DAY' => 'TU' ), array( 'DAY' => 'TH' ));
  $bydays[4] = array( array( 'DAY' => 'MO' ), array( 'DAY' => 'WE' ), array( 'DAY' => 'FR' ));
  $bydays[5] = array( array( 'DAY' => 'SA' ), array( 'DAY' => 'SU' ));
  $status    = array( FALSE, 'TENTATIVE', 'CONFIRMED', 'CANCELLED' );
  while( $date < $stopDate ) {
    $dayCount  = 1;
    while( $dayCount <= $tifConfig['EVENTCNT'] ) {
      $event = & $calendar->newComponent( 'vevent' );
      if( 0 < ( $eventCount % 5 )) {
        $eventdate = $date + ( mt_rand( $tifConfig['EVENTSTART'], $tifConfig['EVENTEND'] ) * 3600 );           // random start hour, 7 to 18
        $event->setProperty( 'DTSTART',  array( 'timestamp' => $eventdate ), fixParameters( array( 'TZID' => $tifConfig['TZID'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        $event->setProperty( 'DURATION', array( 'sec' => mt_rand( $tifConfig['EVENTDURMIN'], $tifConfig['EVENTDURMAX'] ) * 3600 ));
      }
      else
        $event->setProperty( 'DTSTART',  array( 'timestamp' => $date), fixParameters( array( 'VALUE' => 'DATE', 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      $summary = "Event #$eventCount. ".$summaries[mt_rand( 0, 5 )];
      $event->setProperty( 'SUMMARY',    $summary, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      $description = $descriptions[mt_rand( 0, 5)];
      $event->setProperty( 'DESCRIPTION', $description, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));

      $pValue = "Category #$eventCount";
      if( 3 < mt_rand( 1, 10))
        $pValue .= ',Category #C'.mt_rand( 1, 10);
      $event->setProperty( 'CATEGORIES', $pValue, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      if( 3 >= mt_rand( 1, 10)) {
        $pValue = 'Category #A'.mt_rand( 1, 10);
        if( 3 < mt_rand( 1, 10))
          $pValue .= ',Category #XB'.mt_rand( 1, 10);
        $event->setProperty( 'CATEGORIES', $pValue, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      }

      $pValue = "Resource #$eventCount";
      if( 3 < mt_rand( 1, 10))
        $pValue .= ',Resource #R'.mt_rand( 1, 10);
      $event->setProperty( 'RESOURCES',  $pValue, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      if( 3 >= mt_rand( 1, 10)) {
        $pValue = 'Resource #P'.mt_rand( 1, 10);
        if( 3 < mt_rand( 1, 10))
          $pValue .= ',Resource #XP'.mt_rand( 1, 10);
        $event->setProperty( 'RESOURCES',  $pValue, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      }

      $event->setProperty( 'LOCATION',   "Location #$eventCount", fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      $event->setProperty( 'ORGANIZER',  "chair.$eventCount@".$tifConfig['UNIQUE'], fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      if( FALSE !== ( $pValue = $status[mt_rand( 1, 3)] ))
        $event->setProperty( 'STATUS',   $pValue, fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      $event->setProperty( 'CONTACT',    "contact.$eventCount@".$tifConfig['UNIQUE'], fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
            // random priority, 1 to 9. HIGH (1-4), MEDIUM (5), LOW (6-9)
      $event->setProperty( 'PRIORITY',   $prioArr[mt_rand( 0, ( count( $prioArr ) - 1 ))]);
            // two attendees every event
      $attendees = array( 'attendee.'.mt_rand( 1, 10).'@'.$tifConfig['UNIQUE'], 'attendee.'.mt_rand( 11,20).'@'.$tifConfig['UNIQUE'] );
      $event->setProperty( 'ATTENDEE',   $attendees[0] );
      $event->setProperty( 'ATTENDEE',   $attendees[1] );
            // two comments every event
      $event->setProperty( 'COMMENT',    $comments[mt_rand( 0, 4)], fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
      $event->setProperty( 'COMMENT',    $comments[mt_rand( 0, 4)], fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));

      $event->setProperty( 'CREATED',    array( 'timestamp' => ($eventdate - (2*24*3600))), fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount )); // fake two days before event startdate
      $event->setProperty( 'LAST-MODIFIED', array( 'timestamp' => ($eventdate - (23*3600))), fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount )); // fake 25 hours before event startdate

      if(( $eventCount % 3 ) == 0 ) {
        $recur = array( 'FREQ' => 'DAILY' );
        if( in_array( 'RRULE', $tifConfig['OPTIONALS'] )) {
          if(( $eventCount % 2 ) == 0 )
            $recur['COUNT'] = 4;
          else
            $recur['UNTIL'] = array( 'timestamp' => $date + ((24*3600) * mt_rand( 5, 7 )));
          $x = mt_rand( 0, 5 );
          if( 1 < $x )
            $recur['BYDAY'] = $bydays[$x];
          elseif( 0 < $x )
            $recur['BYDAY'] = array( 'DAY' => $days[date( 'w', $eventdate )] );
          $event->setProperty( 'RRULE',  $recur, fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        }
        if( in_array( 'EXRULE', $tifConfig['OPTIONALS'] )) {
          if(( $eventCount % 2 ) == 0 )
            $recur['COUNT'] = 2;
          else
            $recur['UNTIL'] = array( 'timestamp' => $date + ((24*3600) * mt_rand( 4, 6 )));
          $event->setProperty( 'EXRULE',  $recur, fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        }
      }

      $event->setProperty( 'URL', 'http://www.kigkonsult.se/index.php', fixParameters( array( 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));

      if( 0 < $tifConfig['DISPLAYALARM'] ) {
        if( 23 < $tifConfig['DISPLAYALARM'] )
          $trigger = array( 'day' => floor( $tifConfig['DISPLAYALARM'] / 24 ), 'hour' => $tifConfig['DISPLAYALARM'] % 24 );
        else
          $trigger = array( 'hour' => $tifConfig['DISPLAYALARM'] );
        $alarm = & $event->newComponent( 'valarm' );
        $alarm->setProperty( 'ACTION', 'DISPLAY' );
        $alarm->setProperty( 'DESCRIPTION', $description, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        $alarm->setProperty( 'trigger', $trigger );
      }
      if( 0 < $tifConfig['EMAILALARM'] ) {
        if( 23 < $tifConfig['EMAILALARM'] )
          $trigger = array( 'day' => floor( $tifConfig['EMAILALARM'] / 24 ), 'hour' => $tifConfig['EMAILALARM'] % 24 );
        else
          $trigger = array( 'hour' => $tifConfig['EMAILALARM'] );
        $alarm = & $event->newComponent( 'valarm' );
        $alarm->setProperty( 'ACTION', 'EMAIL' );
        $alarm->setProperty( 'DESCRIPTION', $description, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        $alarm->setProperty( 'SUMMARY',     $summary, fixParameters( array( 'LANGUAGE' => $tifConfig['LANGUAGE'], 'ALTREP' => $tifConfig['ALTREP'], 'X-PARAM' => $tifConfig['X-PARAM'] ), $eventCount ));
        $alarm->setProperty( 'attendee',    $attendees[0] );
        $alarm->setProperty( 'attendee',    $attendees[1] );
        $alarm->setProperty( 'trigger',     $trigger );
      }
      $eventCount++;
      $dayCount++;
    }
    $date += (24*3600);
  }
  $calendar->sort();
  if( FALSE === $calendar->saveCalendar()) {
    $tiflog->log( "ERROR (13) saving calendar file '$dirFile', check directory/fil-e permissions!!", 3 );
    $tiflog->log( "createTestFile unsuccessfull: '$dirFile'", 5 );
    $tiflog->flush();
    return FALSE;
  }
  $tiflog->log( "createTestFile successfull: '$dirFile'", 6 );
  $tiflog->flush();
  return TRUE;
}
            /* transform offset in seconds to [-]hhmm[ss] */
function transOffset( $seconds ) {
  if( '-' == substr( $seconds, 0, 1 )) {
    $prefix  = '-';
    $seconds = substr( $seconds, 1 );
  }
  elseif( '+' == substr( $seconds, 0, 1 )) {
    $prefix  = '+';
    $seconds = substr( $seconds, 1 );
  }
  else
    $prefix  = '+';
  $output  = '';
  $hour    = (int) floor( $seconds / 3600 );
  if( 10 > $hour )
    $hour  = '0'.$hour;
  $seconds = $seconds % 3600;
  $min     = (int) floor( $seconds / 60 );
  if( 10 > $min )
    $min   = '0'.$min;
  $output  = $hour.$min;
  $seconds = $seconds % 60;
  if( 0 < $seconds) {
    if( 9 < $seconds)
      $output .= $seconds;
    else
      $output .= '0'.$seconds;
  }
  return $prefix.$output;
}
            /* check and create (opt.) parameters */
function fixParameters( $params, $unique ) {
  $output = array();
  foreach( $params as $pkey => $pvalue ) {
    if( !$pvalue )
      continue;
    $output[$pkey] = ( 'X-' != substr( $pkey, 0, 2 )) ? $pvalue : $pvalue.$unique;
  }
  return ( empty( $output )) ? null : $output;
}
            /* check if testfile is to be recreated ( after $tifConfig['CALAGE'] hours ) */
function checkTestFileStatus( $filename, & $tifConfig, & $tiflog ) {
  $dirFile    = $tifConfig['CALDIR'].DIRECTORY_SEPARATOR.$filename;
  $return     = FALSE;
  $tiflog->log( "checking '$dirFile'", 7 );
  if( TRUE === ( $createFileStatus = !is_file( $dirFile )))
    $tiflog->log( "'$dirFile' is missing", 3 );
  elseif( 0 >= filesize( $dirFile ))
    $tiflog->log( "$dirFile is empty", 3 );
  elseif( !is_readable( $dirFile ))
    $tiflog->log( "'$dirFile' is not readable", 3 );
  elseif( 0 >= $tifConfig['CALAGE'] ) {
    $tiflog->log( "'$dirFile' exists and no recreate", 6 );
    $return = TRUE;
  }
  elseif( !is_writable( $dirFile ))
    $tiflog->log( "'$dirFile' is not writeable", 3 );
  elseif( FALSE === ( $content = file_get_contents(  $dirFile )))
    $tiflog->log( "Unable to read content in '$dirFile'", 4 );
  else {
    $pos = strpos( $content, 'DTSTAMP' ); // find first DTSTAMP
    if( FALSE === $pos )
      $tiflog->log( "Can't find 'DTSTAMP' in '$dirFile'", 4 );
    else {
      $pos += 8; // DTSTAMP:19971210T080000Z
      $fileDtstampDate = substr( $content, $pos, 8 ).substr( $content, $pos+9, 6 );
      $tiflog->log( "$fileDtstampDate = (first) getProperty(DTSTAMP)", 7 );
      $now = strtotime( $tifConfig['THISDATE'] );
      $fdd = strtotime( $fileDtstampDate ) + date( 'Z' ); // dtstamp always UTC
      $age = $tifConfig['THISDATE']." ($now), age=".($now - $fdd).' secs';
      if(( $now - $fdd) > ( $tifConfig['CALAGE'] * 3600 ))
        $tiflog->log( "Date in dtstamp:$fileDtstampDate(UTC) ($fdd) + ".$tifConfig['CALAGE'].' (h) older than now:'.$age.', not OK', 7 );
      else {
        $tiflog->log( "Date in dtstamp:$fileDtstampDate(UTC) ($fdd) not ".$tifConfig['CALAGE'].' (H) older than now:'.$age.', OK', 7 );
        $return = TRUE;
      }
    }
  }
  clearstatcache();
  $tiflog->log( "checkTestFileStatus '$dirFile'", 5 );
  $tiflog->flush();
  return $return;
}
foreach( $tifConfig['FILENAMES'] as $filename ) {
  if( FALSE === checkTestFileStatus( $filename, $tifConfig, $tiflog ))
    createTestFile( $filename, $tiflog );
}
?>
Return current item: tiCalFile