Location: PHPKode > scripts > eventCreator > eventCreator-2.0/eventCreator.php
<?php
/**
 * eventCreator
 * ver 2.0
 *
 * The iCal file event php-HTML form editor
 *
 * copyright (c) 2007 Kjell-Inge Gustafsson kigkonsult
 * www.kigkonsult.se/iCalcreator/eventCreator/index.php
 * hide@address.com
 * updated 20070528
 *
 * 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.
 *
 * 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * This script contains main logic
 *
**/
            /** ********************************************************/
            /**           Set flag that this is a parent file         **/
            /** ********************************************************/
define( '_VALID_KIG', 1 );
            /** ********************************************************/
            /**            grab the language, if changed              **/
            /** ********************************************************/
$lang                  = isset($_REQUEST['v']['lang']) ? $_REQUEST['v']['lang'] : null;

require_once 'eventCreator.config.php';  // setup
            /** ********************************************************/
            /**            check input                                **/
            /** ********************************************************/
if( $res = eccheckParams( $_REQUEST )) {
  // echo test( $_REQUEST, 'Illegal parameters : cnt='.$res);
  exit( 'Illegal Access to this location is not allowed.' );
}
//  echo test( $_REQUEST, 'Parameters cnt='.$res );
$ecmsg                 = array();        // collects system messages to user
            /** ********************************************************/
            /**            grab the data                              **/
            /** ********************************************************/
$v                     = isset($_REQUEST['v'])  ? $_REQUEST['v'] : null;
// echo "start task={$v['task']} ordno={$v['ordno']} eventordno={$v['eventordno']} uid={$v['UID']} lang={$v['lang']}<br />\n";
if( !isset( $v['lang'] ))
  $v['lang']           = isset( $lang ) ? $lang : ECLANG;
            /** ********************************************************/
            /**          check files timeout removal                  **/
            /** ********************************************************/
if( TRUE !== @ECSINGLEFILE ) {
  ecremFiles( ECCACHE, ECTIMEOUT );
  if((  TRUE === @ECFILEUPLOAD )    &&
     (      '' < @ECFILEUPLOADDIR ) &&
     ( ECCACHE != ECFILEUPLOADDIR )) {
    $timeout           = ( 0 < @ECFILEUPLOADDIRTIMEOUT ) ? ECFILEUPLOADDIRTIMEOUT : ECTIMEOUT;
    ecremFiles( ECFILEUPLOADDIR, $timeout, @ECFILEUPLOADEXCLUSIVEDIR );
  }
  if((  TRUE !== @ECFILESELECT )    &&
     (      '' < @ECFILESELECTDIR ) &&
     ( ECCACHE != ECFILESELECTDIR )) {
    $timeout           = ( 0 < @ECFILESELECTDIRTIMEOUT ) ? ECFILESELECTDIRTIMEOUT : ECTIMEOUT;
    ecremFiles( ECFILESELECTDIR, $timeout );
  }
}
            /** ********************************************************/
            /**          alarm removed files                          **/
            /** ********************************************************/
if(  isset( $v['directory'] ) &&
     isset( $v['filename'] )  &&
  ( !isset( $v['task'] ) || ( 'selectFile'  != $v['task'] )) &&
  !is_file( $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] )) {
  $ecmsg[]             = $EClang['fileNotFoundOrRemoved'];
  clearstatcache();
  $v = array( 'lang' => $v['lang'] );
  $v['oldtask']        = 'notask';
  $v['task']           = 'newEvent'; // just show a new event for editing
}

// echo test($v, 'start'); // test ###

$calendar              = null;
            /** ********************************************************/
            /**          check if single file update                  **/
            /** ********************************************************/
if( empty( $v ) || ( isset( $v['lang']) && ( 1 == count( $v )))) {
  if( TRUE === @ECSINGLEFILE ) {
            // if called with filename (req.), dir (opt.) and uid/ordno (opt.)
    $v = array();
    $v['directory']    = isset($_REQUEST['d'])  ? $_REQUEST['d'] : ECCACHE; // 1st call
    $v['filename']     = isset($_REQUEST['f'])  ? $_REQUEST['f'] : null;
    if (( !is_file(     $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] )) ||
        ( !is_readable( $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] ))) {
      $ecmsg[]         = $EClang['fileNotFoundOrRemoved'].' : '.$v['filename'];
      unset( $v['filename'] );
      $v['oldtask']    = 'notask';
      $v['task']       = 'newEvent'; // just show a new event for editing
    }
    else {
      clearstatcache();
      if (( TRUE === @ECEVENTUPDATE ) && ( ! @is_writable( $v['directory'] ))) {
        $ecmsg[]       = $EClang['directoryNotWriteable'].$v['directory'];
      }
      else {
        clearstatcache();
        $v['eventordno'] = null;
        $v['UID']      = isset($_REQUEST['u'])  ? $_REQUEST['u'] : null;
        if( empty( $v['UID'] ))
          $v['ordno']  = isset($_REQUEST['o'])  ? $_REQUEST['o'] : null;
        $v['lang']     = isset($_REQUEST['l'])  ? $_REQUEST['l'] : ECLANG;
        $v['oldtask']  = 'single';
        $v['task']     = 'loadEvent';
        if( empty( $v['ordno'] )) {
          ecFileSort( $v );
          if( empty( $v['UID'] ))
            $v['eventordno'] = 1;
        }
      }
    }
  }
  else {
            /** ********* or no task (???) or first call, no params   **/
    $v['oldtask']      = 'notask';
    $v['task']         = 'newEvent'; // just show a new event for editing
  }
                   // echo test( $v, 'singleFile' ); // test ###
}
            /** ********************************************************/
            /**          check if the task is to upload file          **/
            /** ********************************************************/
elseif( 'uploadFile'  == $v['task'] ) {
  if(( TRUE !== @ECFILEUPLOAD ) || ( TRUE === @ECSINGLEFILE )) {
           /** ********* no permission to upload ********* **/
    $v['oldtask']      = $v['task'].'2';
    $v['task']         = 'newEvent';
    $ecmsg[]           = $EClang['noFileUploadPermission'];
  }
  else {
    $directory         = ( '' < @ECFILEUPLOADDIR ) ? ECFILEUPLOADDIR : ECCACHE;
    $res               = TRUE;
    if( TRUE === @ECFILEUPLOADEXCLUSIVEDIR ) {
      $directory      .= DIRECTORY_SEPARATOR.date('YmdHis').mt_rand( 1, 9 );
      $res = mkdir( $directory, 0700 );
      clearstatcache();
    }
    if( !$res || ( ! @is_writable( $directory ))) {
      $ecmsg[]         = $EClang['directoryNotWriteable'].$v['directory'];
    }
    else {
      clearstatcache();
      $res = ecimpfile( $directory, 'uploadFile', 'ics' );
           /** ********* upload NOT ok ********* **/
      if( $str = @$EClang['fileerror'][$res] ) {
        $ecmsg[]         = $str;
        $v['oldtask']    = $v['task'].'3';
        $v['task']       = 'newEvent';
      }
           /** ********* upload OK ********* **/
      else {
        $ecmsg[]         = $EClang['uploadSuccessfull'].$res;
        $v = array( 'lang' => $v['lang'] );
        $v['directory']  = $directory;
        $v['filename']   = $res;
        $v['oldtask']    = 'uploadFile';
        $v['task']       = 'loadEvent';
        $v['eventordno'] = 1;
                       // echo "1 >$res< ".test( $v, 'uploadFile' ); // test ###
        ecFileSort( $v );
      }
    }
  }
}
            /** ********************************************************/
            /**          check if to select another file              **/
            /** ********************************************************/
elseif( 'selectFile'  == $v['task'] ) {
  if(( TRUE !== @ECFILESELECT ) || ( TRUE === @ECSINGLEFILE )) {
            /** ********* no permission to select ********* **/
    $v['oldtask']      = $v['task'].'2';
    $v['task']         = 'loadEvent';
    $ecmsg[]           = $EClang['fileNotFoundOrRemoved'];
  }
  else {
    $filename          = ( !empty( $v['selectFile1'] )) ? $v['selectFile1'] : null;
    if( !$filename )
      $filename        = ( !empty( $v['selectFile2'] )) ? $v['selectFile2'] : null;
    $filename          = trim( $filename );
    $directory         = ( '' < @ECFILESELECTDIR ) ? ECFILESELECTDIR : ECCACHE;
                     // echo test($v, "selectFile 1 filename=$filename"); // test ###
    if(( TRUE === @ECEVENTUPDATE ) && ( ! @is_writable( $directory ))) {
      $ecmsg[]         = $EClang['directoryNotWriteable'].$directory;
    }
    elseif( empty( $filename )) {
            /** ********* no file selected ?? ********* **/
      $v = array( 'lang' => $v['lang'] );
      $v['directory']  = $directory;
      $v['oldtask']    = $v['task'].'3';
      $v['task']       = 'newEvent';
      $ecmsg[]         = $EClang['noFileSelected'];
    }
    else {
      clearstatcache();
            /** ********* file selected !! ********* **/
                     // echo test($v, "selectFile 1"); // test ###
      $ecmsg[]         = $EClang['fileSelected'].$filename;
      $v = array( 'lang' => $v['lang'] );
      $v['directory']  = $directory;
      $v['filename']   = $filename;
      $v['oldtask']    = 'selectFile';
      $v['task']       = 'loadEvent';
      $v['eventordno'] = 1;
                  // echo test($v, "selectFile 4"); // test ###
      ecFileSort( $v );
    }
  }
}
            /** ********************************************************/
            /**          create new file                              **/
            /** ********************************************************/
elseif( 'saveEvent'   == $v['task'] && empty( $v['filename'] )) {
  if(( TRUE !== @ECFILECREATE ) || ( TRUE === @ECSINGLEFILE )) {
           /** ********* no permission to create new file ********* **/
    $v['task']         = 'viewEvent';
    $ecmsg[]           = $EClang['noCreateFilePermission'];
  }
  else {
    $v['directory']    = ( '' < @ECFILECREATEDIR ) ? ECFILECREATEDIR : ECCACHE;
    if( @is_writable( $v['directory'] )) {
      $v['filename']   = ( '' < @ECFILECREATEFIXFILENAME )
                               ? ECFILECREATEFIXFILENAME.'ics' : date(ECFILECREATEFILENAMETMPL).'.ics';
      $ecmsg[]         = $EClang['fileCreationSuccessfull'].$v['filename'];
    }
    else {
      $ecmsg[]         = $EClang['directoryNotWriteable'].$v['directory'];
    }
    clearstatcache();
  }
}
            /** ********************************************************/
            /**          user activated file remove                   **/
            /** ********************************************************/
elseif( 'deleteFile'  == $v['task'] ) {
  if( TRUE !== @ECFILEDELETE ) {
            /** ********* no permission to delete file ********* **/
    $ecmsg[]           = $EClang['noDeleteFilePermission'];
    $v['oldtask']      = $v['task'].'2';
    $v['task']         = 'newEvent';
  }
  elseif( ! @is_writable( $v['directory'] )) {
    $ecmsg[]           = $EClang['directoryNotWriteable'].$v['directory'];
  }
  else {
    clearstatcache();
    if( !empty( $v['directory'] ) && !empty( $v['filename'] )) {
      if( is_file( $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] ))
        @unlink( $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] );
      clearstatcache();
      if( is_file( $v['directory'].DIRECTORY_SEPARATOR.$v['filename'] ))
        $ecmsg[]       = $EClang['unableToRemoveFile'].$v['filename'];
      else {
        $ecmsg[]       = $EClang['fileRemoved'].$v['filename'];
        $v             = array( 'lang'      => $v['lang']
                              , 'directory' => $v['directory']
                              , 'task'      => 'newEvent'
                              , 'oldtask'   => 'deleteFile3' );
      }
      clearstatcache();
    }
  }
}
                      // echo test($v, "före delete event"); // test ###
            /** ********************************************************/
            /**          event delete                                 **/
            /** ********************************************************/
if( 'deleteEvent'     == $v['task'] ) {
  if( TRUE !== @ECEVENTDELETE ) {
           /** ********* no permission to delete event ********* **/
    $ecmsg[]           = $EClang['noDeleteEventPermission'];
    $v['task']         = 'viewEvent';
  }
  elseif( empty( $v['UID'] )) {
            /** ********* delete of unsaved event ********* **/
    $ecmsg[]   = $EClang['eventRemovalSuccessfull'].$v['eventordno'];
    $v = array( 'lang'       => $v['lang']
              , 'filename'   => $v['filename']
              , 'directory'  => $v['directory']
              , 'ordno'      => --$v['ordno']
              , 'eventordno' => --$v['eventordno']
              , 'oldtask'    => $v['task'].'3' );
                           // echo test($v,'delUnSaved'); // test ###
  }
  else {
            /** ********* delete of saved, existing event ********* **/
    $calendar          = & ecloadFile( $v );
    $compsinfo         = $calendar->getConfig( 'compsinfo' );
                   // echo test($v,'delete before'); // test ###
            /** ********* find event to delete ********* **/
    if( $calendar->deleteComponent( (int) $v['ordno'] )) {
      $calendar->saveCalendar();
      if( TRUE === @ECSINGLEEVENT ) {
        $v['closeWindow'] = TRUE;
      }
      else {
        $ecmsg[]       = $EClang['eventRemovalSuccessfull'].$v['eventordno'];
        ecinitNewEvent( $v );
        $calendar      = null;
        $v['oldtask']  = $v['task'].'3';
        $v['eventordno'] = ( $v['eventordno'] < $v['eventcnt'] ) ? $v['eventordno'] : --$v['eventordno'];
                    // echo test($v,'after delete'); // test ###
      }
    }
    else {  // event to delete not found.. .
      $ecmsg[]         = $EClang['eventNotFound'].' (4)';   // todo new text!!!
      $v['oldtask']    = $v['task'].'4';
      unset( $v['ordno'] );
    }
  }
  $v['task']           = ( !empty( $v['eventordno'] )) ? 'loadEvent' : 'newEvent';
                    // echo test($v,'delete after'); // test ###
}
            /** ********************************************************/
            /**          load selected event                          **/
            /** ********************************************************/
$v['loadStatusOk']     = FALSE;
if( 'loadEvent'       == $v['task'] ) {
                  // echo test($v, 'före loadEvent 1'); // test ###
  if( !empty( $v['eventordno'] ))
    $v['UID'] = $v['ordno'] = null;
  elseif( !empty( $v['UID'] ))
    $v['eventordno']   = $v['ordno'] = null;
  elseif( !empty( $v['ordno'] ))
    $v['eventordno']   = $v['UID'] = null;
  else {
    $v['UID'] = $v['ordno'] = null;
    $v['eventordno']   = 1;
  }
  if( !$calendar )
    $calendar          = & ecloadFile( $v );
  $compsinfo = $calendar->getConfig( 'compsinfo' );
                  // echo test($v, 'före loadEvent 2'); // test ###
  $loadFirst = ( isset( $v['oldtask'] ) &&
              in_array( $v['oldtask'], array('selectFile', 'uploadFile' ))) ? TRUE : FALSE;
  $v['loadStatusOk']   =
    ecFetchEvent( $v, $compsinfo, $v['ordno'], $v['eventordno'], $v['UID'], $loadFirst );
  if( $v['loadStatusOk'] ) {
                   // echo test($v, 'före loadEvent 3'); // test ###
    $event             = $calendar->getComponent( 'vevent', $v['eventordno'] ); // $v['UID'] );
    ecloadEvent( $v, $event );
    $text              = $EClang['eventLoaded'].$v['eventordno'];
    $text             .= ( !empty( $v['eventcnt'] )) ? " ({$v['eventcnt']})" : '';
    $ecmsg[]           = $text;
                    // echo test($v, 'efter loadEvent 3'); // test ###
  }
  else {
    if( isset( $v['oldtask'] ) && ( 'single' == $v['oldtask'] )) {
      if( @ECEVENTCREATE )
        $v['task']     = 'newEvent';
      else {
        $s      = '<html><head><title>eventCreator</title></head><body>';
        $s     .= '<script type="text/javascript">alert('.$EClang['eventNotFound'].');';
        if( @ECCLOSEWINDOW )
          $s   .= ' window.close();';
        elseif( @ECREDIRECTONCLOSE ) {
            /** ********* if ECREDIRECTONCLOSE set, fix redirect url ********* **/
          if( TRUE === @ECREDIRECTONCLOSE )
            $s .= ' location.href="'.ecgetRedirectUrl( $v ).'";';
          else
            $s .= ' window.close();';
        }
        $s     .= '</script></body></html>';
        echo $s;
        exit();
      }
    }
    else {
      $ecmsg[]         = $EClang['eventNotFound'].' : '.$v['eventordno'];
      $v['oldtask']    = 'uploadFile4';
      $v['task']       = 'newEvent';
    }
  }
                     // echo test($v, 'efter loadEvent 4'); // test ###
}
// echo "efter loadEvent filename={$v['filename']} <br />\n"; // test ###
            /** ********************************************************/
            /**          initiate new event                           **/
            /** ********************************************************/
if( 'newEvent'        == $v['task'] ) {
  if(( TRUE !== @ECEVENTCREATE ) &&
     (  !isset( $v['oldtask'] )   ||
      ( !in_array( $v['oldtask'],
            array( 'notask'
                 , 'uploadFile2',  'uploadFile3',  'uploadFile4'
                 , 'selectFile2',  'selectFile3'
                 , 'deleteFile2',  'deleteFile3'
                 , 'deleteEvent3', 'deleteEvent4' ))))) {
           /** ********* no permission to create event ********* **/
    $ecmsg[]           = $EClang['noCreateEventPermission'];
    $v['task']         = 'viewEvent';
  }
  else {
    $event             = & eccreateDefaultEvent();
    ecloadEvent( $v, $event );
                   // echo test($v,'efter ecloadEvent'); // test ###
    if( !isset( $v['oldtask'] ) || ( 'notask' != $v['oldtask'] )) {
      if( !$calendar )
        $calendar      = & ecloadFile( $v );
      $calendar->setComponent( $event );
      $compsinfo       = $calendar->getConfig( 'compsinfo' );
      $compinfo        = end($compsinfo);
      ecFetchEvent( $v, $compsinfo, $compinfo['ordno'] );
      if( !isset( $v['oldtask'] )   ||
        ( !in_array( $v['oldtask'],
              array( 'notask'
                   , 'uploadFile2',  'uploadFile3',  'uploadFile4'
                   , 'selectFile2',  'selectFile3'
                   , 'deleteFile2',  'deleteFile3'
                   , 'deleteEvent3', 'deleteEvent4' ))))
        $ecmsg[]       = $EClang['newEventCreated'].$v['eventordno'];
    }
                   // echo test($v,'newEvent'); // test ###
    $v['UID']          = '';  // NEW EVENT !!
  }
}
            /** ********************************************************/
            /**          save event                                   **/
            /** ********************************************************/
elseif( 'saveEvent'   == $v['task'] ) {
                   // echo test( $p='', 'saveEvent'); // test ###
  if( TRUE !== @ECEVENTUPDATE ) {
           /** ********* no permission to update event ********* **/
    $ecmsg[]           = $EClang['noUpdateEventPermission'];
    $v['task']         = 'viewEvent';
  }
  else {
    $calendar          = & ecsaveEvent( $v );
    $ecmsg[]           = $EClang['eventSaved'].' : '.$v['eventordno'];
  }
}
/*
 echo "1 prev=";  if(isset($v['previous']))   echo $v['previous'];
 echo " ordno=";  if(isset($v['ordno']))      echo $v['ordno'];
 echo " (eg=";    if(isset($v['eventordno'])) echo $v['eventordno'];
 echo ") av ";    if(isset($v['compscnt']))   echo $v['compscnt'];
 echo " events="; if(isset($v['eventcnt']))   echo $v['eventcnt'];
 echo " next=";   if(isset($v['next']))       echo $v['next'];
 echo "<br />\n"; /// test
*/
            /** ********************************************************/
            /**          transform array start/end-date to timestamp **/
            /** ********************************************************/
if(  isset( $v['task'] ) && ('viewEvent' == $v['task'] ) && isset( $v['DTSTART'] )) {
  $v['DTSTART'] = ecdate2timestamp ( $v['DTSTART'] );
  if( !empty( $v['DTEND'] ) && empty( $v['chkb'] )) {
    $v['DTEND'] = ecdate2timestamp ( $v['DTEND'] );
            /** ********* check from/to date order ********* **/
    if( $v['DTEND']['timestamp'] < $v['DTSTART']['timestamp'] ) {
      $tmp          = $v['DTEND'];
      $v['DTEND']   = $v['DTSTART'];
      $v['DTSTART'] = $tmp;
    }
  }
  else
    unset( $v['DTEND'] );
}
unset( $calendar, $v['task'] );
            /** ********************************************************/
            /**          get the filelist from ECFILESELECTDIR        **/
            /** ********************************************************/
if( TRUE === @ECFILESELECT ) {
  $directory           = ( '' < @ECFILESELECTDIR ) ? ECFILESELECTDIR : ECCACHE;
  $v['filelist']       = ecgetFilelist( $directory );
}
            /** ********************************************************/
            /**          fix defaults, empty array members et.al.     **/
            /** ********************************************************/
if( isset( $v['eventcnt'] ) && ( 1 < $v['eventcnt'] )) {
  $v['eventSelect']    = array();
  for( $x=1; $x <= $v['eventcnt']; $x++ ) {
    $y = ( strlen( $x ) < 2 ) ? '&nbsp;'.$x : $x;
    $v['eventSelect'][$x] = $y;
  }
}
if( !isset( $v['COMMENT'] ) || !is_array( $v['COMMENT'] ) || ( 0 >= count( $v['COMMENT'] )))
  $v['COMMENT']        = array( '' );
if( !isset( $v['ATTENDEE'] ) || !is_array( $v['ATTENDEE'] ) || ( 0 >= count( $v['ATTENDEE'] )))
  $v['ATTENDEE']       = array( '' );

$v['ecmsg'] = $ecmsg;
if( TRUE === @ECREDIRECTAFTERSAVE )
  $EClang['ECREDIRECTAFTERSAVETEXT'] = $ECREDIRECTAFTERSAVETEXT;
                   // echo test($v,'före visning'); // test ###
// echo "före visning filename={$v['filename']} <br />\n"; // test ###
            /** ********* get language flags ********* **/
if( TRUE === @ECLANGCHANGE )
  $v['flags']          = ecGetFlags();
            /** ********* if ECREDIRECTONCLOSE set, fix redirect url ********* **/
if( TRUE === @ECREDIRECTONCLOSE )
  $v['redirecturl']    = ecgetRedirectUrl( $v );
//$c=get_defined_constants(TRUE);ksort($c['user']);foreach($c['user'] as $k=>$c) echo "$k=>$c<br />\n"; //test##
    // echo test($a=array($v['DTSTART'],$v['DTEND']),'eventCreator.php start+end'); // test ###
            /** ********************************************************/
            /**          display the event                            **/
            /** ********************************************************/
if( defined( 'ECPHPHTMLDIR' ) && ( '' < ECPHPHTMLDIR )) {
            /** ********* edit event with PHP+HTML ********* **/
  foreach( $EClang as $langkey => $langtext )
    $v['t'][$langkey] = $langtext;
  include_once ECPHPHTMLDIR.DIRECTORY_SEPARATOR.'eventCreator.HTML.php';
  EC::ecEditEvent( $v );
}
else {
            /** ********* edit event with Smarty ********* **/
  require_once ECSMARTYDIR.DIRECTORY_SEPARATOR.'Smarty.class.php';
  $smarty = new Smarty;
  $smarty->template_dir = ECSMARTYTMPLDIR;
  $smarty->compile_dir  = ECSMARTYCOMPILEDIR;
  $smarty->cache_dir    = ECSMARTYCACHEDIR;
                 $smarty->force_compile = TRUE; // testing only
  $smarty->assign( $EClang );
  $smarty->assign( $v );
  $smarty->display( 'eventCreator.tpl' );
}
?>
Return current item: eventCreator