source: trunk/library/iCalcreator/iCalUtilityFunctions.class.php @ 7655

Revision 7655, 55.6 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

Line 
1<?php
2/**
3 * iCalcreator class v2.8
4 * copyright (c) 2007-2011 Kjell-Inge Gustafsson kigkonsult
5 * www.kigkonsult.se/iCalcreator/index.php
6 * ical@kigkonsult.se
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 */
22/**
23 * moving all utility (static) functions to a utility class
24 *
25 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
26 * @since 2.6.22 - 2010-09-25
27 *
28 */
29class iCalUtilityFunctions {
30  // Store the single instance of iCalUtilityFunctions
31  private static $m_pInstance;
32
33  // Private constructor to limit object instantiation to within the class
34  private function __construct() {
35    $m_pInstance = FALSE;
36  }
37
38  // Getter method for creating/returning the single instance of this class
39  public static function getInstance() {
40    if (!self::$m_pInstance)
41      self::$m_pInstance = new iCalUtilityFunctions();
42
43    return self::$m_pInstance;
44  }
45/**
46 * check a date(-time) for an opt. timezone and if it is a DATE-TIME or DATE
47 *
48 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
49 * @since 2.4.16 - 2008-10-25
50 * @param array $date, date to check
51 * @param int $parno, no of date parts (i.e. year, month.. .)
52 * @return array $params, property parameters
53 */
54  public static function _chkdatecfg( $theDate, & $parno, & $params ) {
55    if( isset( $params['TZID'] ))
56      $parno = 6;
57    elseif( isset( $params['VALUE'] ) && ( 'DATE' == $params['VALUE'] ))
58      $parno = 3;
59    else {
60      if( isset( $params['VALUE'] ) && ( 'PERIOD' == $params['VALUE'] ))
61        $parno = 7;
62      if( is_array( $theDate )) {
63        if( isset( $theDate['timestamp'] ))
64          $tzid = ( isset( $theDate['tz'] )) ? $theDate['tz'] : null;
65        else
66          $tzid = ( isset( $theDate['tz'] )) ? $theDate['tz'] : ( 7 == count( $theDate )) ? end( $theDate ) : null;
67        if( !empty( $tzid )) {
68          $parno = 7;
69          if( !iCalUtilityFunctions::_isOffset( $tzid ))
70            $params['TZID'] = $tzid; // save only timezone
71        }
72        elseif( !$parno && ( 3 == count( $theDate )) &&
73          ( isset( $params['VALUE'] ) && ( 'DATE' == $params['VALUE'] )))
74          $parno = 3;
75        else
76          $parno = 6;
77      }
78      else { // string
79        $date = trim( $theDate );
80        if( 'Z' == substr( $date, -1 ))
81          $parno = 7; // UTC DATE-TIME
82        elseif((( 8 == strlen( $date ) && ctype_digit( $date )) || ( 11 >= strlen( $date ))) &&
83          ( !isset( $params['VALUE'] ) || !in_array( $params['VALUE'], array( 'DATE-TIME', 'PERIOD' ))))
84          $parno = 3; // DATE
85        $date = iCalUtilityFunctions::_date_time_string( $date, $parno );
86        if( !empty( $date['tz'] )) {
87          $parno = 7;
88          if( !iCalUtilityFunctions::_isOffset( $date['tz'] ))
89            $params['TZID'] = $date['tz']; // save only timezone
90        }
91        elseif( empty( $parno ))
92          $parno = 6;
93      }
94      if( isset( $params['TZID'] ))
95        $parno = 6;
96    }
97  }
98/**
99 * convert date/datetime to timestamp
100 *
101 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
102 * @since 2.4.8 - 2008-10-30
103 * @param array  $datetime  datetime/(date)
104 * @param string $tz        timezone
105 * @return timestamp
106 */
107  public static function _date2timestamp( $datetime, $tz=null ) {
108    $output = null;
109    if( !isset( $datetime['hour'] )) $datetime['hour'] = '0';
110    if( !isset( $datetime['min'] ))  $datetime['min']  = '0';
111    if( !isset( $datetime['sec'] ))  $datetime['sec']  = '0';
112    foreach( $datetime as $dkey => $dvalue ) {
113      if( 'tz' != $dkey )
114        $datetime[$dkey] = (integer) $dvalue;
115    }
116    if( $tz )
117      $datetime['tz'] = $tz;
118    $offset = ( isset( $datetime['tz'] ) && ( '' < trim ( $datetime['tz'] ))) ? iCalUtilityFunctions::_tz2offset( $datetime['tz'] ) : 0;
119    $output = mktime( $datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year'] );
120    return $output;
121  }
122/**
123 * ensures internal date-time/date format for input date-time/date in array format
124 *
125 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
126 * @since 0.3.0 - 2006-08-15
127 * @param array $datetime
128 * @param int $parno optional, default FALSE
129 * @return array
130 */
131  public static function _date_time_array( $datetime, $parno=FALSE ) {
132    $output = array();
133    foreach( $datetime as $dateKey => $datePart ) {
134      switch ( $dateKey ) {
135        case '0': case 'year':   $output['year']  = $datePart; break;
136        case '1': case 'month':  $output['month'] = $datePart; break;
137        case '2': case 'day':    $output['day']   = $datePart; break;
138      }
139      if( 3 != $parno ) {
140        switch ( $dateKey ) {
141          case '0':
142          case '1':
143          case '2': break;
144          case '3': case 'hour': $output['hour']  = $datePart; break;
145          case '4': case 'min' : $output['min']   = $datePart; break;
146          case '5': case 'sec' : $output['sec']   = $datePart; break;
147          case '6': case 'tz'  : $output['tz']    = $datePart; break;
148        }
149      }
150    }
151    if( 3 != $parno ) {
152      if( !isset( $output['hour'] ))
153        $output['hour'] = 0;
154      if( !isset( $output['min']  ))
155        $output['min'] = 0;
156      if( !isset( $output['sec']  ))
157        $output['sec'] = 0;
158    }
159    return $output;
160  }
161/**
162 * ensures internal date-time/date format for input date-time/date in string fromat
163 *
164 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
165 * @since 2.6.35 - 2010-12-03
166 * @param array $datetime
167 * @param int $parno optional, default FALSE
168 * @return array
169 */
170  public static function _date_time_string( $datetime, $parno=FALSE ) {
171    $datetime = (string) trim( $datetime );
172    $tz  = null;
173    $len = strlen( $datetime ) - 1;
174    if( 'Z' == substr( $datetime, -1 )) {
175      $tz = 'Z';
176      $datetime = trim( substr( $datetime, 0, $len ));
177    }
178    elseif( ( ctype_digit( substr( $datetime, -2, 2 ))) && // time or date
179                  ( '-' == substr( $datetime, -3, 1 )) ||
180                  ( ':' == substr( $datetime, -3, 1 )) ||
181                  ( '.' == substr( $datetime, -3, 1 ))) {
182      $continue = TRUE;
183    }
184    elseif( ( ctype_digit( substr( $datetime, -4, 4 ))) && // 4 pos offset
185            ( ' +' == substr( $datetime, -6, 2 )) ||
186            ( ' -' == substr( $datetime, -6, 2 ))) {
187      $tz = substr( $datetime, -5, 5 );
188      $datetime = substr( $datetime, 0, ($len - 5));
189    }
190    elseif( ( ctype_digit( substr( $datetime, -6, 6 ))) && // 6 pos offset
191            ( ' +' == substr( $datetime, -8, 2 )) ||
192            ( ' -' == substr( $datetime, -8, 2 ))) {
193      $tz = substr( $datetime, -7, 7 );
194      $datetime = substr( $datetime, 0, ($len - 7));
195    }
196    elseif( ( 6 < $len ) && ( ctype_digit( substr( $datetime, -6, 6 )))) {
197      $continue = TRUE;
198    }
199    elseif( 'T' ==  substr( $datetime, -7, 1 )) {
200      $continue = TRUE;
201    }
202    else {
203      $cx  = $tx = 0;    //  19970415T133000 US-Eastern
204      for( $cx = -1; $cx > ( 9 - $len ); $cx-- ) {
205        $char = substr( $datetime, $cx, 1 );
206        if(( ' ' == $char) || ctype_digit( $char))
207          break; // if exists, tz ends here.. . ?
208        else
209           $tx--; // tz length counter
210      }
211      if( 0 > $tx ) {
212        $tz = substr( $datetime, $tx );
213        $datetime = trim( substr( $datetime, 0, $len + $tx + 1 ));
214      }
215    }
216    if( 0 < substr_count( $datetime, '-' )) {
217      $datetime = str_replace( '-', '/', $datetime );
218    }
219    elseif( ctype_digit( substr( $datetime, 0, 8 )) &&
220           ( 'T' ==      substr( $datetime, 8, 1 )) &&
221            ctype_digit( substr( $datetime, 9, 6 ))) {
222      $datetime = substr( $datetime,  4, 2 )
223             .'/'.substr( $datetime,  6, 2 )
224             .'/'.substr( $datetime,  0, 4 )
225             .' '.substr( $datetime,  9, 2 )
226             .':'.substr( $datetime, 11, 2 )
227             .':'.substr( $datetime, 13);
228    }
229    $datestring = date( 'Y-m-d H:i:s', strtotime( $datetime ));
230    $tz                = trim( $tz );
231    $output            = array();
232    $output['year']    = substr( $datestring, 0, 4 );
233    $output['month']   = substr( $datestring, 5, 2 );
234    $output['day']     = substr( $datestring, 8, 2 );
235    if(( 6 == $parno ) || ( 7 == $parno ) || ( !$parno && ( 'Z' == $tz ))) {
236      $output['hour']  = substr( $datestring, 11, 2 );
237      $output['min']   = substr( $datestring, 14, 2 );
238      $output['sec']   = substr( $datestring, 17, 2 );
239      if( !empty( $tz ))
240        $output['tz']  = $tz;
241    }
242    elseif( 3 != $parno ) {
243      if(( '00' < substr( $datestring, 11, 2 )) ||
244         ( '00' < substr( $datestring, 14, 2 )) ||
245         ( '00' < substr( $datestring, 17, 2 ))) {
246        $output['hour']  = substr( $datestring, 11, 2 );
247        $output['min']   = substr( $datestring, 14, 2 );
248        $output['sec']   = substr( $datestring, 17, 2 );
249      }
250      if( !empty( $tz ))
251        $output['tz']  = $tz;
252    }
253    return $output;
254  }
255/**
256 * convert local startdate/enddate (Ymd[His]) to duration array
257 *
258 * uses this component dates if missing input dates
259 *
260 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
261 * @since 2.6.11 - 2010-10-21
262 * @param array $startdate
263 * @param array $duration
264 * @return array duration
265 */
266  function _date2duration( $startdate, $enddate ) {
267    $startWdate  = mktime( 0, 0, 0, $startdate['month'], $startdate['day'], $startdate['year'] );
268    $endWdate    = mktime( 0, 0, 0, $enddate['month'],   $enddate['day'],   $enddate['year'] );
269    $wduration   = $endWdate - $startWdate;
270    $dur         = array();
271    $dur['week'] = (int) floor( $wduration / ( 7 * 24 * 60 * 60 ));
272    $wduration   =              $wduration % ( 7 * 24 * 60 * 60 );
273    $dur['day']  = (int) floor( $wduration / ( 24 * 60 * 60 ));
274    $wduration   =              $wduration % ( 24 * 60 * 60 );
275    $dur['hour'] = (int) floor( $wduration / ( 60 * 60 ));
276    $wduration   =              $wduration % ( 60 * 60 );
277    $dur['min']  = (int) floor( $wduration / ( 60 ));
278    $dur['sec']  = (int)        $wduration % ( 60 );
279    return $dur;
280  }
281/**
282 * ensures internal duration format for input in array format
283 *
284 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
285 * @since 2.1.1 - 2007-06-24
286 * @param array $duration
287 * @return array
288 */
289  public static function _duration_array( $duration ) {
290    $output = array();
291    if(    is_array( $duration )        &&
292       ( 1 == count( $duration ))       &&
293              isset( $duration['sec'] ) &&
294              ( 60 < $duration['sec'] )) {
295      $durseconds  = $duration['sec'];
296      $output['week'] = floor( $durseconds / ( 60 * 60 * 24 * 7 ));
297      $durseconds  =           $durseconds % ( 60 * 60 * 24 * 7 );
298      $output['day']  = floor( $durseconds / ( 60 * 60 * 24 ));
299      $durseconds  =           $durseconds % ( 60 * 60 * 24 );
300      $output['hour'] = floor( $durseconds / ( 60 * 60 ));
301      $durseconds  =           $durseconds % ( 60 * 60 );
302      $output['min']  = floor( $durseconds / ( 60 ));
303      $output['sec']  =      ( $durseconds % ( 60 ));
304    }
305    else {
306      foreach( $duration as $durKey => $durValue ) {
307        if( empty( $durValue )) continue;
308        switch ( $durKey ) {
309          case '0': case 'week': $output['week']  = $durValue; break;
310          case '1': case 'day':  $output['day']   = $durValue; break;
311          case '2': case 'hour': $output['hour']  = $durValue; break;
312          case '3': case 'min':  $output['min']   = $durValue; break;
313          case '4': case 'sec':  $output['sec']   = $durValue; break;
314        }
315      }
316    }
317    if( isset( $output['week'] ) && ( 0 < $output['week'] )) {
318      unset( $output['day'], $output['hour'], $output['min'], $output['sec'] );
319      return $output;
320    }
321    unset( $output['week'] );
322    if( empty( $output['day'] ))
323      unset( $output['day'] );
324    if ( isset( $output['hour'] ) || isset( $output['min'] ) || isset( $output['sec'] )) {
325      if( !isset( $output['hour'] )) $output['hour'] = 0;
326      if( !isset( $output['min']  )) $output['min']  = 0;
327      if( !isset( $output['sec']  )) $output['sec']  = 0;
328      if(( 0 == $output['hour'] ) && ( 0 == $output['min'] ) && ( 0 == $output['sec'] ))
329        unset( $output['hour'], $output['min'], $output['sec'] );
330    }
331    return $output;
332  }
333/**
334 * ensures internal duration format for input in string format
335 *
336 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
337 * @since 2.0.5 - 2007-03-14
338 * @param string $duration
339 * @return array
340 */
341  public static function _duration_string( $duration ) {
342    $duration = (string) trim( $duration );
343    while( 'P' != strtoupper( substr( $duration, 0, 1 ))) {
344      if( 0 < strlen( $duration ))
345        $duration = substr( $duration, 1 );
346      else
347        return false; // no leading P !?!?
348    }
349    $duration = substr( $duration, 1 ); // skip P
350    $duration = str_replace ( 't', 'T', $duration );
351    $duration = str_replace ( 'T', '', $duration );
352    $output = array();
353    $val    = null;
354    for( $ix=0; $ix < strlen( $duration ); ++$ix ) {
355      switch( strtoupper( substr( $duration, $ix, 1 ))) {
356       case 'W':
357         $output['week'] = $val;
358         $val            = null;
359         break;
360       case 'D':
361         $output['day']  = $val;
362         $val            = null;
363         break;
364       case 'H':
365         $output['hour'] = $val;
366         $val            = null;
367         break;
368       case 'M':
369         $output['min']  = $val;
370         $val            = null;
371         break;
372       case 'S':
373         $output['sec']  = $val;
374         $val            = null;
375         break;
376       default:
377         if( !ctype_digit( substr( $duration, $ix, 1 )))
378           return false; // unknown duration controll character  !?!?
379         else
380           $val .= substr( $duration, $ix, 1 );
381      }
382    }
383    return iCalUtilityFunctions::_duration_array( $output );
384  }
385/**
386 * convert duration to date in array format
387 *
388 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
389 * @since 2.6.23 - 2010-10-23
390 * @param array $startdate
391 * @param array $duration
392 * @return array, date format
393 */
394  function _duration2date( $startdate=null, $duration=null ) {
395    if( empty( $startdate )) return FALSE;
396    if( empty( $duration ))  return FALSE;
397    $dateOnly          = ( isset( $startdate['hour'] ) || isset( $startdate['min'] ) || isset( $startdate['sec'] )) ? FALSE : TRUE;
398    $startdate['hour'] = ( isset( $startdate['hour'] )) ? $startdate['hour'] : 0;
399    $startdate['min']  = ( isset( $startdate['min'] ))  ? $startdate['min']  : 0;
400    $startdate['sec']  = ( isset( $startdate['sec'] ))  ? $startdate['sec']  : 0;
401    $dtend = 0;
402    if(    isset( $duration['week'] ))
403      $dtend += ( $duration['week'] * 7 * 24 * 60 * 60 );
404    if(    isset( $duration['day'] ))
405      $dtend += ( $duration['day'] * 24 * 60 * 60 );
406    if(    isset( $duration['hour'] ))
407      $dtend += ( $duration['hour'] * 60 *60 );
408    if(    isset( $duration['min'] ))
409      $dtend += ( $duration['min'] * 60 );
410    if(    isset( $duration['sec'] ))
411      $dtend +=   $duration['sec'];
412    if(( 24 * 60 * 60 ) < $dtend )
413      $dtend -= ( 24 * 60 * 60 ); // exclude start day
414    $dtend += mktime( $startdate['hour'], $startdate['min'], $startdate['sec'], $startdate['month'], $startdate['day'], $startdate['year'] );
415    $dtend2 = array();
416    $dtend2['year']   = date('Y', $dtend );
417    $dtend2['month']  = date('m', $dtend );
418    $dtend2['day']    = date('d', $dtend );
419    $dtend2['hour']   = date('H', $dtend );
420    $dtend2['min']    = date('i', $dtend );
421    $dtend2['sec']    = date('s', $dtend );
422    if( isset( $startdate['tz'] ))
423      $dtend2['tz']   = $startdate['tz'];
424    if( $dateOnly && (( 0 == $dtend2['hour'] ) && ( 0 == $dtend2['min'] ) && ( 0 == $dtend2['sec'] )))
425      unset( $dtend2['hour'], $dtend2['min'], $dtend2['sec'] );
426    return $dtend2;
427  }
428/**
429 * if not preSet, if exist, remove key with expected value from array and return hit value else return elseValue
430 *
431 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
432 * @since 2.4.16 - 2008-11-08
433 * @param array $array
434 * @param string $expkey, expected key
435 * @param string $expval, expected value
436 * @param int $hitVal optional, return value if found
437 * @param int $elseVal optional, return value if not found
438 * @param int $preSet optional, return value if already preset
439 * @return int
440 */
441  public static function _existRem( &$array, $expkey, $expval=FALSE, $hitVal=null, $elseVal=null, $preSet=null ) {
442    if( $preSet )
443      return $preSet;
444    if( !is_array( $array ) || ( 0 == count( $array )))
445      return $elseVal;
446    foreach( $array as $key => $value ) {
447      if( strtoupper( $expkey ) == strtoupper( $key )) {
448        if( !$expval || ( strtoupper( $expval ) == strtoupper( $array[$key] ))) {
449          unset( $array[$key] );
450          return $hitVal;
451        }
452      }
453    }
454    return $elseVal;
455  }
456/**
457 * creates formatted output for calendar component property data value type date/date-time
458 *
459 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
460 * @since 2.4.8 - 2008-10-30
461 * @param array   $datetime
462 * @param int     $parno, optional, default 6
463 * @return string
464 */
465  public static function _format_date_time( $datetime, $parno=6 ) {
466    if( !isset( $datetime['year'] )  &&
467        !isset( $datetime['month'] ) &&
468        !isset( $datetime['day'] )   &&
469        !isset( $datetime['hour'] )  &&
470        !isset( $datetime['min'] )   &&
471        !isset( $datetime['sec'] ))
472      return ;
473    $output = null;
474    // if( !isset( $datetime['day'] )) { $o=''; foreach($datetime as $k=>$v) {if(is_array($v)) $v=implode('-',$v);$o.=" $k=>$v";} echo " day SAKNAS : $o <br />\n"; }
475    foreach( $datetime as $dkey => & $dvalue )
476      if( 'tz' != $dkey ) $dvalue = (integer) $dvalue;
477    $output = date('Ymd', mktime( 0, 0, 0, $datetime['month'], $datetime['day'], $datetime['year']));
478    if( isset( $datetime['hour'] )  ||
479        isset( $datetime['min'] )   ||
480        isset( $datetime['sec'] )   ||
481        isset( $datetime['tz'] )) {
482      if( isset( $datetime['tz'] )  &&
483         !isset( $datetime['hour'] ))
484        $datetime['hour'] = 0;
485      if( isset( $datetime['hour'] )  &&
486         !isset( $datetime['min'] ))
487        $datetime['min'] = 0;
488      if( isset( $datetime['hour'] )  &&
489          isset( $datetime['min'] )   &&
490         !isset( $datetime['sec'] ))
491        $datetime['sec'] = 0;
492      $date = mktime( $datetime['hour'], $datetime['min'], $datetime['sec'], $datetime['month'], $datetime['day'], $datetime['year']);
493      $output .= date('\THis', $date );
494      if( isset( $datetime['tz'] ) && ( '' < trim ( $datetime['tz'] ))) {
495        $datetime['tz'] = trim( $datetime['tz'] );
496        if( 'Z' == $datetime['tz'] )
497          $output .= 'Z';
498        $offset = iCalUtilityFunctions::_tz2offset( $datetime['tz'] );
499        if( 0 != $offset ) {
500          $date = mktime( $datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
501          $output    = date( 'Ymd\THis\Z', $date );
502        }
503      }
504      elseif( 7 == $parno )
505        $output .= 'Z';
506    }
507    return $output;
508  }
509/**
510 * creates formatted output for calendar component property data value type duration
511 *
512 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
513 * @since 2.6.10 - 2010-11-28
514 * @param array $duration ( week, day, hour, min, sec )
515 * @return string
516 */
517  public static function _format_duration( $duration ) {
518    if( isset( $duration['week'] ) ||
519        isset( $duration['day'] )  ||
520        isset( $duration['hour'] ) ||
521        isset( $duration['min'] )  ||
522        isset( $duration['sec'] ))
523       $ok = TRUE;
524    else
525      return;
526    if( isset( $duration['week'] ) && ( 0 < $duration['week'] ))
527      return 'P'.$duration['week'].'W';
528    $output = 'P';
529    if( isset($duration['day'] ) && ( 0 < $duration['day'] ))
530      $output .= $duration['day'].'D';
531    if(( isset( $duration['hour']) && ( 0 < $duration['hour'] )) ||
532       ( isset( $duration['min'])  && ( 0 < $duration['min'] ))  ||
533       ( isset( $duration['sec'])  && ( 0 < $duration['sec'] )))
534      $output .= 'T';
535    $output .= ( isset( $duration['hour']) && ( 0 < $duration['hour'] )) ? $duration['hour'].'H' : '';
536    $output .= ( isset( $duration['min'])  && ( 0 < $duration['min'] ))  ? $duration['min']. 'M' : '';
537    $output .= ( isset( $duration['sec'])  && ( 0 < $duration['sec'] ))  ? $duration['sec']. 'S' : '';
538    return $output;
539  }
540/**
541 * checks if input array contains a date
542 *
543 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
544 * @since 2.4.16 - 2008-10-25
545 * @param array $input
546 * @return bool
547 */
548  public static function _isArrayDate( $input ) {
549    if( isset( $input['week'] ) || ( !in_array( count( $input ), array( 3, 6, 7 ))))
550      return FALSE;
551    if( 7 == count( $input ))
552      return TRUE;
553    if( isset( $input['year'] ) && isset( $input['month'] ) && isset( $input['day'] ))
554      return checkdate( (int) $input['month'], (int) $input['day'], (int) $input['year'] );
555    if( isset( $input['day'] ) || isset( $input['hour'] ) || isset( $input['min'] ) || isset( $input['sec'] ))
556      return FALSE;
557    if( in_array( 0, $input ))
558      return FALSE;
559    if(( 1970 > $input[0] ) || ( 12 < $input[1] ) || ( 31 < $input[2] ))
560      return FALSE;
561    if(( isset( $input[0] ) && isset( $input[1] ) && isset( $input[2] )) &&
562         checkdate( (int) $input[1], (int) $input[2], (int) $input[0] ))
563      return TRUE;
564    $input = iCalUtilityFunctions::_date_time_string( $input[1].'/'.$input[2].'/'.$input[0], 3 ); //  m - d - Y
565    if( isset( $input['year'] ) && isset( $input['month'] ) && isset( $input['day'] ))
566      return checkdate( (int) $input['month'], (int) $input['day'], (int) $input['year'] );
567    return FALSE;
568  }
569/**
570 * checks if input array contains a timestamp date
571 *
572 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
573 * @since 2.4.16 - 2008-10-18
574 * @param array $input
575 * @return bool
576 */
577  public static function _isArrayTimestampDate( $input ) {
578    return ( is_array( $input ) && isset( $input['timestamp'] )) ? TRUE : FALSE ;
579  }
580/**
581 * controll if input string contains trailing UTC offset
582 *
583 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
584 * @since 2.4.16 - 2008-10-19
585 * @param string $input
586 * @return bool
587 */
588  public static function _isOffset( $input ) {
589    $input         = trim( (string) $input );
590    if( 'Z' == substr( $input, -1 ))
591      return TRUE;
592    elseif((   5 <= strlen( $input )) &&
593       ( in_array( substr( $input, -5, 1 ), array( '+', '-' ))) &&
594       (   '0000'  < substr( $input, -4 )) && (   '9999' >= substr( $input, -4 )))
595      return TRUE;
596    elseif((    7 <= strlen( $input )) &&
597       ( in_array( substr( $input, -7, 1 ), array( '+', '-' ))) &&
598       ( '000000'  < substr( $input, -6 )) && ( '999999' >= substr( $input, -6 )))
599      return TRUE;
600    return FALSE;
601
602  }
603/**
604 * remakes a recur pattern to an array of dates
605 *
606 * if missing, UNTIL is set 1 year from startdate (emergency break)
607 *
608 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
609 * @since 2.4.23 - 2010-10-24
610 * @param array $result, array to update, array([timestamp] => timestamp)
611 * @param array $recur, pattern for recurrency (only value part, params ignored)
612 * @param array $wdate, component start date
613 * @param array $startdate, start date
614 * @param array $enddate, optional
615 * @return array of recurrence (start-)dates as index
616 * @todo BYHOUR, BYMINUTE, BYSECOND, ev. BYSETPOS due to ambiguity, WEEKLY at year end/start
617 */
618  public static function _recur2date( & $result, $recur, $wdate, $startdate, $enddate=FALSE ) {
619    foreach( $wdate as $k => $v ) if( ctype_digit( $v )) $wdate[$k] = (int) $v;
620    $wdatets     = iCalUtilityFunctions::_date2timestamp( $wdate );
621    $startdatets = iCalUtilityFunctions::_date2timestamp( $startdate );
622    if( !$enddate ) {
623      $enddate = $startdate;
624      $enddate['year'] += 1;
625// echo "recur __in_ ".implode('-',$startdate)." period start ".implode('-',$wdate)." period end ".implode('-',$enddate)."<br />\n";print_r($recur);echo "<br />\n";//test###
626    }
627    $endDatets = iCalUtilityFunctions::_date2timestamp( $enddate ); // fix break
628    if( !isset( $recur['COUNT'] ) && !isset( $recur['UNTIL'] ))
629      $recur['UNTIL'] = $enddate; // create break
630    if( isset( $recur['UNTIL'] )) {
631      $tdatets = iCalUtilityFunctions::_date2timestamp( $recur['UNTIL'] );
632      if( $endDatets > $tdatets ) {
633        $endDatets = $tdatets; // emergency break
634        $enddate   = iCalUtilityFunctions::_timestamp2date( $endDatets, 6 );
635      }
636      else
637        $recur['UNTIL'] = iCalUtilityFunctions::_timestamp2date( $endDatets, 6 );
638    }
639    if( $wdatets > $endDatets ) {
640     //echo "recur out of date ".implode('-',iCalUtilityFunctions::_date_time_string(date('Y-m-d H:i:s',$wdatets),6))."<br />\n";//test
641      return array(); // nothing to do.. .
642    }
643    if( !isset( $recur['FREQ'] )) // "MUST be specified.. ."
644      $recur['FREQ'] = 'DAILY'; // ??
645    $wkst = ( isset( $recur['WKST'] ) && ( 'SU' == $recur['WKST'] )) ? 24*60*60 : 0; // ??
646    if( !isset( $recur['INTERVAL'] ))
647      $recur['INTERVAL'] = 1;
648    $countcnt = ( !isset( $recur['BYSETPOS'] )) ? 1 : 0; // DTSTART counts as the first occurrence
649            /* find out how to step up dates and set index for interval count */
650    $step = array();
651    if( 'YEARLY' == $recur['FREQ'] )
652      $step['year']  = 1;
653    elseif( 'MONTHLY' == $recur['FREQ'] )
654      $step['month'] = 1;
655    elseif( 'WEEKLY' == $recur['FREQ'] )
656      $step['day']   = 7;
657    else
658      $step['day']   = 1;
659    if( isset( $step['year'] ) && isset( $recur['BYMONTH'] ))
660      $step = array( 'month' => 1 );
661    if( empty( $step ) && isset( $recur['BYWEEKNO'] )) // ??
662      $step = array( 'day' => 7 );
663    if( isset( $recur['BYYEARDAY'] ) || isset( $recur['BYMONTHDAY'] ) || isset( $recur['BYDAY'] ))
664      $step = array( 'day' => 1 );
665    $intervalarr = array();
666    if( 1 < $recur['INTERVAL'] ) {
667      $intervalix = iCalUtilityFunctions::_recurIntervalIx( $recur['FREQ'], $wdate, $wkst );
668      $intervalarr = array( $intervalix => 0 );
669    }
670    if( isset( $recur['BYSETPOS'] )) { // save start date + weekno
671      $bysetposymd1 = $bysetposymd2 = $bysetposw1 = $bysetposw2 = array();
672      $bysetposWold = (int) date( 'W', ( $wdatets + $wkst ));
673      $bysetposYold = $wdate['year'];
674      $bysetposMold = $wdate['month'];
675      $bysetposDold = $wdate['day'];
676      if( is_array( $recur['BYSETPOS'] )) {
677        foreach( $recur['BYSETPOS'] as $bix => $bval )
678          $recur['BYSETPOS'][$bix] = (int) $bval;
679      }
680      else
681        $recur['BYSETPOS'] = array( (int) $recur['BYSETPOS'] );
682      iCalUtilityFunctions::_stepdate( $enddate, $endDatets, $step); // make sure to count whole last period
683    }
684    iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
685    $year_old     = null;
686    $daynames     = array( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' );
687             /* MAIN LOOP */
688     // echo "recur start ".implode('-',$wdate)." end ".implode('-',$enddate)."<br />\n";//test
689    while( TRUE ) {
690      if( isset( $endDatets ) && ( $wdatets > $endDatets ))
691        break;
692      if( isset( $recur['COUNT'] ) && ( $countcnt >= $recur['COUNT'] ))
693        break;
694      if( $year_old != $wdate['year'] ) {
695        $year_old   = $wdate['year'];
696        $daycnts    = array();
697        $yeardays   = $weekno = 0;
698        $yeardaycnt = array();
699        for( $m = 1; $m <= 12; ++$m ) { // count up and update up-counters
700          $daycnts[$m] = array();
701          $weekdaycnt = array();
702          foreach( $daynames as $dn )
703            $yeardaycnt[$dn] = $weekdaycnt[$dn] = 0;
704          $mcnt     = date( 't', mktime( 0, 0, 0, $m, 1, $wdate['year'] ));
705          for( $d   = 1; $d <= $mcnt; ++$d ) {
706            $daycnts[$m][$d] = array();
707            if( isset( $recur['BYYEARDAY'] )) {
708              ++$yeardays;
709              $daycnts[$m][$d]['yearcnt_up'] = $yeardays;
710            }
711            if( isset( $recur['BYDAY'] )) {
712              $day    = date( 'w', mktime( 0, 0, 0, $m, $d, $wdate['year'] ));
713              $day    = $daynames[$day];
714              $daycnts[$m][$d]['DAY'] = $day;
715              $weekdaycnt[$day]++;
716              $daycnts[$m][$d]['monthdayno_up'] = $weekdaycnt[$day];
717              $yeardaycnt[$day]++;
718              $daycnts[$m][$d]['yeardayno_up'] = $yeardaycnt[$day];
719            }
720            if(  isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' ))
721              $daycnts[$m][$d]['weekno_up'] =(int)date('W',mktime(0,0,$wkst,$m,$d,$wdate['year']));
722          }
723        }
724        $daycnt = 0;
725        $yeardaycnt = array();
726        if(  isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' )) {
727          $weekno = null;
728          for( $d=31; $d > 25; $d-- ) { // get last weekno for year
729            if( !$weekno )
730              $weekno = $daycnts[12][$d]['weekno_up'];
731            elseif( $weekno < $daycnts[12][$d]['weekno_up'] ) {
732              $weekno = $daycnts[12][$d]['weekno_up'];
733              break;
734            }
735          }
736        }
737        for( $m = 12; $m > 0; $m-- ) { // count down and update down-counters
738          $weekdaycnt = array();
739          foreach( $daynames as $dn )
740            $yeardaycnt[$dn] = $weekdaycnt[$dn] = 0;
741          $monthcnt = 0;
742          $mcnt     = date( 't', mktime( 0, 0, 0, $m, 1, $wdate['year'] ));
743          for( $d   = $mcnt; $d > 0; $d-- ) {
744            if( isset( $recur['BYYEARDAY'] )) {
745              $daycnt -= 1;
746              $daycnts[$m][$d]['yearcnt_down'] = $daycnt;
747            }
748            if( isset( $recur['BYMONTHDAY'] )) {
749              $monthcnt -= 1;
750              $daycnts[$m][$d]['monthcnt_down'] = $monthcnt;
751            }
752            if( isset( $recur['BYDAY'] )) {
753              $day  = $daycnts[$m][$d]['DAY'];
754              $weekdaycnt[$day] -= 1;
755              $daycnts[$m][$d]['monthdayno_down'] = $weekdaycnt[$day];
756              $yeardaycnt[$day] -= 1;
757              $daycnts[$m][$d]['yeardayno_down'] = $yeardaycnt[$day];
758            }
759            if(  isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' ))
760              $daycnts[$m][$d]['weekno_down'] = ($daycnts[$m][$d]['weekno_up'] - $weekno - 1);
761          }
762        }
763      }
764            /* check interval */
765      if( 1 < $recur['INTERVAL'] ) {
766            /* create interval index */
767        $intervalix = iCalUtilityFunctions::_recurIntervalIx( $recur['FREQ'], $wdate, $wkst );
768            /* check interval */
769        $currentKey = array_keys( $intervalarr );
770        $currentKey = end( $currentKey ); // get last index
771        if( $currentKey != $intervalix )
772          $intervalarr = array( $intervalix => ( $intervalarr[$currentKey] + 1 ));
773        if(( $recur['INTERVAL'] != $intervalarr[$intervalix] ) &&
774           ( 0 != $intervalarr[$intervalix] )) {
775            /* step up date */
776    //echo "skip: ".implode('-',$wdate)." ix=$intervalix old=$currentKey interval=".$intervalarr[$intervalix]."<br />\n";//test
777          iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
778          continue;
779        }
780        else // continue within the selected interval
781          $intervalarr[$intervalix] = 0;
782   //echo "cont: ".implode('-',$wdate)." ix=$intervalix old=$currentKey interval=".$intervalarr[$intervalix]."<br />\n";//test
783      }
784      $updateOK = TRUE;
785      if( $updateOK && isset( $recur['BYMONTH'] ))
786        $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYMONTH']
787                                           , $wdate['month']
788                                           ,($wdate['month'] - 13));
789      if( $updateOK && isset( $recur['BYWEEKNO'] ))
790        $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYWEEKNO']
791                                           , $daycnts[$wdate['month']][$wdate['day']]['weekno_up']
792                                           , $daycnts[$wdate['month']][$wdate['day']]['weekno_down'] );
793      if( $updateOK && isset( $recur['BYYEARDAY'] ))
794        $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYYEARDAY']
795                                           , $daycnts[$wdate['month']][$wdate['day']]['yearcnt_up']
796                                           , $daycnts[$wdate['month']][$wdate['day']]['yearcnt_down'] );
797      if( $updateOK && isset( $recur['BYMONTHDAY'] ))
798        $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYMONTHDAY']
799                                           , $wdate['day']
800                                           , $daycnts[$wdate['month']][$wdate['day']]['monthcnt_down'] );
801    //echo "efter BYMONTHDAY: ".implode('-',$wdate).' status: '; echo ($updateOK) ? 'TRUE' : 'FALSE'; echo "<br />\n";//test###
802      if( $updateOK && isset( $recur['BYDAY'] )) {
803        $updateOK = FALSE;
804        $m = $wdate['month'];
805        $d = $wdate['day'];
806        if( isset( $recur['BYDAY']['DAY'] )) { // single day, opt with year/month day order no
807          $daynoexists = $daynosw = $daynamesw =  FALSE;
808          if( $recur['BYDAY']['DAY'] == $daycnts[$m][$d]['DAY'] )
809            $daynamesw = TRUE;
810          if( isset( $recur['BYDAY'][0] )) {
811            $daynoexists = TRUE;
812            if(( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'MONTHLY' )) || isset( $recur['BYMONTH'] ))
813              $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYDAY'][0]
814                                                , $daycnts[$m][$d]['monthdayno_up']
815                                                , $daycnts[$m][$d]['monthdayno_down'] );
816            elseif( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'YEARLY' ))
817              $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYDAY'][0]
818                                                , $daycnts[$m][$d]['yeardayno_up']
819                                                , $daycnts[$m][$d]['yeardayno_down'] );
820          }
821          if((  $daynoexists &&  $daynosw && $daynamesw ) ||
822             ( !$daynoexists && !$daynosw && $daynamesw )) {
823            $updateOK = TRUE;
824          }
825        //echo "daynoexists:$daynoexists daynosw:$daynosw daynamesw:$daynamesw<br />\n"; // test ###
826        }
827        else {
828          foreach( $recur['BYDAY'] as $bydayvalue ) {
829            $daynoexists = $daynosw = $daynamesw = FALSE;
830            if( isset( $bydayvalue['DAY'] ) &&
831                     ( $bydayvalue['DAY'] == $daycnts[$m][$d]['DAY'] ))
832              $daynamesw = TRUE;
833            if( isset( $bydayvalue[0] )) {
834              $daynoexists = TRUE;
835              if(( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'MONTHLY' )) ||
836                   isset( $recur['BYMONTH'] ))
837                $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $bydayvalue['0']
838                                                  , $daycnts[$m][$d]['monthdayno_up']
839                                                  , $daycnts[$m][$d]['monthdayno_down'] );
840              elseif( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'YEARLY' ))
841                $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $bydayvalue['0']
842                                                  , $daycnts[$m][$d]['yeardayno_up']
843                                                  , $daycnts[$m][$d]['yeardayno_down'] );
844            }
845        //echo "daynoexists:$daynoexists daynosw:$daynosw daynamesw:$daynamesw<br />\n"; // test ###
846            if((  $daynoexists &&  $daynosw && $daynamesw ) ||
847               ( !$daynoexists && !$daynosw && $daynamesw )) {
848              $updateOK = TRUE;
849              break;
850            }
851          }
852        }
853      }
854      //echo "efter BYDAY: ".implode('-',$wdate).' status: '; echo ($updateOK) ? 'TRUE' : 'FALSE'; echo "<br />\n"; // test ###
855            /* check BYSETPOS */
856      if( $updateOK ) {
857        if( isset( $recur['BYSETPOS'] ) &&
858          ( in_array( $recur['FREQ'], array( 'YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY' )))) {
859          if( isset( $recur['WEEKLY'] )) {
860            if( $bysetposWold == $daycnts[$wdate['month']][$wdate['day']]['weekno_up'] )
861              $bysetposw1[] = $wdatets;
862            else
863              $bysetposw2[] = $wdatets;
864          }
865          else {
866            if(( isset( $recur['FREQ'] ) && ( 'YEARLY'      == $recur['FREQ'] )  &&
867                                            ( $bysetposYold == $wdate['year'] ))   ||
868               ( isset( $recur['FREQ'] ) && ( 'MONTHLY'     == $recur['FREQ'] )  &&
869                                           (( $bysetposYold == $wdate['year'] )  &&
870                                            ( $bysetposMold == $wdate['month'] ))) ||
871               ( isset( $recur['FREQ'] ) && ( 'DAILY'       == $recur['FREQ'] )  &&
872                                           (( $bysetposYold == $wdate['year'] )  &&
873                                            ( $bysetposMold == $wdate['month'])  &&
874                                            ( $bysetposDold == $wdate['day'] ))))
875              $bysetposymd1[] = $wdatets;
876            else
877              $bysetposymd2[] = $wdatets;
878          }
879        }
880        else {
881            /* update result array if BYSETPOS is set */
882          ++$countcnt;
883          if( $startdatets <= $wdatets ) { // only output within period
884            $result[$wdatets] = TRUE;
885          //echo "recur ".implode('-',iCalUtilityFunctions::_date_time_string(date('Y-m-d H:i:s',$wdatets),6))."<br />\n";//test
886          }
887         //else echo "recur undate ".implode('-',iCalUtilityFunctions::_date_time_string(date('Y-m-d H:i:s',$wdatets),6))." okdatstart ".implode('-',iCalUtilityFunctions::_date_time_string(date('Y-m-d H:i:s',$startdatets),6))."<br />\n";//test
888          $updateOK = FALSE;
889        }
890      }
891            /* step up date */
892      iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
893            /* check if BYSETPOS is set for updating result array */
894      if( $updateOK && isset( $recur['BYSETPOS'] )) {
895        $bysetpos       = FALSE;
896        if( isset( $recur['FREQ'] ) && ( 'YEARLY'  == $recur['FREQ'] ) &&
897          ( $bysetposYold != $wdate['year'] )) {
898          $bysetpos     = TRUE;
899          $bysetposYold = $wdate['year'];
900        }
901        elseif( isset( $recur['FREQ'] ) && ( 'MONTHLY' == $recur['FREQ'] &&
902         (( $bysetposYold != $wdate['year'] ) || ( $bysetposMold != $wdate['month'] )))) {
903          $bysetpos     = TRUE;
904          $bysetposYold = $wdate['year'];
905          $bysetposMold = $wdate['month'];
906        }
907        elseif( isset( $recur['FREQ'] ) && ( 'WEEKLY'  == $recur['FREQ'] )) {
908          $weekno = (int) date( 'W', mktime( 0, 0, $wkst, $wdate['month'], $wdate['day'], $wdate['year']));
909          if( $bysetposWold != $weekno ) {
910            $bysetposWold = $weekno;
911            $bysetpos     = TRUE;
912          }
913        }
914        elseif( isset( $recur['FREQ'] ) && ( 'DAILY'   == $recur['FREQ'] ) &&
915         (( $bysetposYold != $wdate['year'] )  ||
916          ( $bysetposMold != $wdate['month'] ) ||
917          ( $bysetposDold != $wdate['day'] ))) {
918          $bysetpos     = TRUE;
919          $bysetposYold = $wdate['year'];
920          $bysetposMold = $wdate['month'];
921          $bysetposDold = $wdate['day'];
922        }
923        if( $bysetpos ) {
924          if( isset( $recur['BYWEEKNO'] )) {
925            $bysetposarr1 = & $bysetposw1;
926            $bysetposarr2 = & $bysetposw2;
927          }
928          else {
929            $bysetposarr1 = & $bysetposymd1;
930            $bysetposarr2 = & $bysetposymd2;
931          }
932          foreach( $recur['BYSETPOS'] as $ix ) {
933            if( 0 > $ix ) // both positive and negative BYSETPOS allowed
934              $ix = ( count( $bysetposarr1 ) + $ix + 1);
935            $ix--;
936            if( isset( $bysetposarr1[$ix] )) {
937              if( $startdatets <= $bysetposarr1[$ix] ) { // only output within period
938                $result[$bysetposarr1[$ix]] = TRUE;
939       //echo "recur ".implode('-',iCalUtilityFunctions::_date_time_string(date('Y-m-d H:i:s',$bysetposarr1[$ix]),6))."<br />\n";//test
940              }
941              ++$countcnt;
942            }
943            if( isset( $recur['COUNT'] ) && ( $countcnt >= $recur['COUNT'] ))
944              break;
945          }
946          $bysetposarr1 = $bysetposarr2;
947          $bysetposarr2 = array();
948        }
949      }
950    }
951  }
952  public static function _recurBYcntcheck( $BYvalue, $upValue, $downValue ) {
953    if( is_array( $BYvalue ) &&
954      ( in_array( $upValue, $BYvalue ) || in_array( $downValue, $BYvalue )))
955      return TRUE;
956    elseif(( $BYvalue == $upValue ) || ( $BYvalue == $downValue ))
957      return TRUE;
958    else
959      return FALSE;
960  }
961  public static function _recurIntervalIx( $freq, $date, $wkst ) {
962            /* create interval index */
963    switch( $freq ) {
964      case 'YEARLY':
965        $intervalix = $date['year'];
966        break;
967      case 'MONTHLY':
968        $intervalix = $date['year'].'-'.$date['month'];
969        break;
970      case 'WEEKLY':
971        $wdatets    = iCalUtilityFunctions::_date2timestamp( $date );
972        $intervalix = (int) date( 'W', ( $wdatets + $wkst ));
973       break;
974      case 'DAILY':
975           default:
976        $intervalix = $date['year'].'-'.$date['month'].'-'.$date['day'];
977        break;
978    }
979    return $intervalix;
980  }
981/**
982 * convert input format for exrule and rrule to internal format
983 *
984 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
985 * @since 2.6.23 - 2010-12-07
986 * @param array $rexrule
987 * @return array
988 */
989  public static function _setRexrule( $rexrule ) {
990    $input          = array();
991    if( empty( $rexrule ))
992      return $input;
993    foreach( $rexrule as $rexrulelabel => $rexrulevalue ) {
994      $rexrulelabel = strtoupper( $rexrulelabel );
995      if( 'UNTIL'  != $rexrulelabel )
996        $input[$rexrulelabel]   = $rexrulevalue;
997      else {
998        if( iCalUtilityFunctions::_isArrayTimestampDate( $rexrulevalue )) // timestamp date
999          $input[$rexrulelabel] = iCalUtilityFunctions::_timestamp2date( $rexrulevalue, 6 );
1000        elseif( iCalUtilityFunctions::_isArrayDate( $rexrulevalue )) // date-time
1001          $input[$rexrulelabel] = iCalUtilityFunctions::_date_time_array( $rexrulevalue, 6 );
1002        elseif( 8 <= strlen( trim( $rexrulevalue ))) // ex. 2006-08-03 10:12:18
1003          $input[$rexrulelabel] = iCalUtilityFunctions::_date_time_string( $rexrulevalue );
1004        if(( 3 < count( $input[$rexrulelabel] )) && !isset( $input[$rexrulelabel]['tz'] ))
1005          $input[$rexrulelabel]['tz'] = 'Z';
1006      }
1007    }
1008            /* set recurrence rule specification in rfc2445 order */
1009    $input2 = array();
1010    if( isset( $input['FREQ'] ))
1011      $input2['FREQ']       = $input['FREQ'];
1012    if( isset( $input['UNTIL'] ))
1013      $input2['UNTIL']      = $input['UNTIL'];
1014    elseif( isset( $input['COUNT'] ))
1015      $input2['COUNT']      = $input['COUNT'];
1016    if( isset( $input['INTERVAL'] ))
1017      $input2['INTERVAL']   = $input['INTERVAL'];
1018    if( isset( $input['BYSECOND'] ))
1019      $input2['BYSECOND']   = $input['BYSECOND'];
1020    if( isset( $input['BYMINUTE'] ))
1021      $input2['BYMINUTE']   = $input['BYMINUTE'];
1022    if( isset( $input['BYHOUR'] ))
1023      $input2['BYHOUR']     = $input['BYHOUR'];
1024    if( isset( $input['BYDAY'] ))
1025      $input2['BYDAY']      = $input['BYDAY'];
1026    if( isset( $input['BYMONTHDAY'] ))
1027      $input2['BYMONTHDAY'] = $input['BYMONTHDAY'];
1028    if( isset( $input['BYYEARDAY'] ))
1029      $input2['BYYEARDAY']  = $input['BYYEARDAY'];
1030    if( isset( $input['BYWEEKNO'] ))
1031      $input2['BYWEEKNO']   = $input['BYWEEKNO'];
1032    if( isset( $input['BYMONTH'] ))
1033      $input2['BYMONTH']    = $input['BYMONTH'];
1034    if( isset( $input['BYSETPOS'] ))
1035      $input2['BYSETPOS']   = $input['BYSETPOS'];
1036    if( isset( $input['WKST'] ))
1037      $input2['WKST']       = $input['WKST'];
1038    return $input2;
1039  }
1040/**
1041 * convert format for input date to internal date with parameters
1042 *
1043 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1044 * @since 2.6.22 - 2010-09-25
1045 * @param mixed $year
1046 * @param mixed $month optional
1047 * @param int $day optional
1048 * @param int $hour optional
1049 * @param int $min optional
1050 * @param int $sec optional
1051 * @param string $tz optional
1052 * @param array $params optional
1053 * @param string $caller optional
1054 * @param string $objName optional
1055 * @return array
1056 */
1057  public static function _setDate( $year, $month=FALSE, $day=FALSE, $hour=FALSE, $min=FALSE, $sec=FALSE, $tz=FALSE, $params=FALSE, $caller=null, $objName=null ) {
1058    $input = $parno = null;
1059    $localtime = (( 'dtstart' == $caller ) && in_array( $objName, array( 'vtimezone', 'standard', 'daylight' ))) ? TRUE : FALSE;
1060    if( iCalUtilityFunctions::_isArrayDate( $year )) {
1061      if( $localtime ) unset ( $month['VALUE'], $month['TZID'] );
1062      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ));
1063      if( isset( $input['params']['TZID'] )) {
1064        $input['params']['VALUE'] = 'DATE-TIME';
1065        unset( $year['tz'] );
1066      }
1067      $hitval          = (( !empty( $year['tz'] ) || !empty( $year[6] ))) ? 7 : 6;
1068      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE-TIME', $hitval );
1069      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE', 3, count( $year ), $parno );
1070      $input['value']  = iCalUtilityFunctions::_date_time_array( $year, $parno );
1071    }
1072    elseif( iCalUtilityFunctions::_isArrayTimestampDate( $year )) {
1073      if( $localtime ) unset ( $month['VALUE'], $month['TZID'] );
1074      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ));
1075      if( isset( $input['params']['TZID'] )) {
1076        $input['params']['VALUE'] = 'DATE-TIME';
1077        unset( $year['tz'] );
1078      }
1079      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE', 3 );
1080      $hitval          = ( isset( $year['tz'] )) ? 7 : 6;
1081      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE-TIME', $hitval, $parno );
1082      $input['value']  = iCalUtilityFunctions::_timestamp2date( $year, $parno );
1083    }
1084    elseif( 8 <= strlen( trim( $year ))) { // ex. 2006-08-03 10:12:18
1085      if( $localtime ) unset ( $month['VALUE'], $month['TZID'] );
1086      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ));
1087      if( isset( $input['params']['TZID'] )) {
1088        $input['params']['VALUE'] = 'DATE-TIME';
1089        $parno = 6;
1090      }
1091      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE-TIME', 7, $parno );
1092      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE', 3, $parno, $parno );
1093      $input['value']  = iCalUtilityFunctions::_date_time_string( $year, $parno );
1094    }
1095    else {
1096      if( is_array( $params )) {
1097        if( $localtime ) unset ( $params['VALUE'], $params['TZID'] );
1098        $input['params'] = iCalUtilityFunctions::_setParams( $params, array( 'VALUE' => 'DATE-TIME' ));
1099      }
1100      elseif( is_array( $tz )) {
1101        $input['params'] = iCalUtilityFunctions::_setParams( $tz,     array( 'VALUE' => 'DATE-TIME' ));
1102        $tz = FALSE;
1103      }
1104      elseif( is_array( $hour )) {
1105        $input['params'] = iCalUtilityFunctions::_setParams( $hour,   array( 'VALUE' => 'DATE-TIME' ));
1106        $hour = $min = $sec = $tz = FALSE;
1107      }
1108      if( isset( $input['params']['TZID'] )) {
1109        $tz            = null;
1110        $input['params']['VALUE'] = 'DATE-TIME';
1111      }
1112      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE', 3 );
1113      $hitval          = ( !empty( $tz )) ? 7 : 6;
1114      $parno           = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE-TIME', $hitval, $parno, $parno );
1115      $input['value']  = array( 'year'  => $year, 'month' => $month, 'day'   => $day );
1116      if( 3 != $parno ) {
1117        $input['value']['hour'] = ( $hour ) ? $hour : '0';
1118        $input['value']['min']  = ( $min )  ? $min  : '0';
1119        $input['value']['sec']  = ( $sec )  ? $sec  : '0';
1120        if( !empty( $tz ))
1121          $input['value']['tz'] = $tz;
1122      }
1123    }
1124    if( 3 == $parno ) {
1125      $input['params']['VALUE'] = 'DATE';
1126      unset( $input['value']['tz'] );
1127      unset( $input['params']['TZID'] );
1128    }
1129    elseif( isset( $input['params']['TZID'] ))
1130      unset( $input['value']['tz'] );
1131    if( $localtime ) unset( $input['value']['tz'], $input['params']['TZID'] );
1132    if( isset( $input['value']['tz'] ))
1133      $input['value']['tz'] = (string) $input['value']['tz'];
1134    if( !empty( $input['value']['tz'] ) && ( 'Z' != $input['value']['tz'] ) &&
1135      ( !iCalUtilityFunctions::_isOffset( $input['value']['tz'] )))
1136      $input['params']['TZID'] = $input['value']['tz'];
1137    return $input;
1138  }
1139/**
1140 * convert format for input date (UTC) to internal date with parameters
1141 *
1142 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1143 * @since 2.4.17 - 2008-10-31
1144 * @param mixed $year
1145 * @param mixed $month optional
1146 * @param int $day optional
1147 * @param int $hour optional
1148 * @param int $min optional
1149 * @param int $sec optional
1150 * @param array $params optional
1151 * @return array
1152 */
1153  public static function _setDate2( $year, $month=FALSE, $day=FALSE, $hour=FALSE, $min=FALSE, $sec=FALSE, $params=FALSE ) {
1154    $input = null;
1155    if( iCalUtilityFunctions::_isArrayDate( $year )) {
1156      $input['value']  = iCalUtilityFunctions::_date_time_array( $year, 7 );
1157      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ) );
1158    }
1159    elseif( iCalUtilityFunctions::_isArrayTimestampDate( $year )) {
1160      $input['value']  = iCalUtilityFunctions::_timestamp2date( $year, 7 );
1161      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ) );
1162    }
1163    elseif( 8 <= strlen( trim( $year ))) { // ex. 2006-08-03 10:12:18
1164      $input['value']  = iCalUtilityFunctions::_date_time_string( $year, 7 );
1165      $input['params'] = iCalUtilityFunctions::_setParams( $month, array( 'VALUE' => 'DATE-TIME' ) );
1166    }
1167    else {
1168      $input['value']  = array( 'year'  => $year
1169                              , 'month' => $month
1170                              , 'day'   => $day
1171                              , 'hour'  => $hour
1172                              , 'min'   => $min
1173                              , 'sec'   => $sec );
1174      $input['params'] = iCalUtilityFunctions::_setParams( $params, array( 'VALUE' => 'DATE-TIME' ));
1175    }
1176    $parno = iCalUtilityFunctions::_existRem( $input['params'], 'VALUE', 'DATE-TIME', 7 ); // remove default
1177    if( !isset( $input['value']['hour'] ))
1178      $input['value']['hour'] = 0;
1179    if( !isset( $input['value']['min'] ))
1180      $input['value']['min'] = 0;
1181    if( !isset( $input['value']['sec'] ))
1182      $input['value']['sec'] = 0;
1183    if( !isset( $input['value']['tz'] ) || !iCalUtilityFunctions::_isOffset( $input['value']['tz'] ))
1184      $input['value']['tz'] = 'Z';
1185    return $input;
1186  }
1187/**
1188 * check index and set (an indexed) content in multiple value array
1189 *
1190 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1191 * @since 2.6.12 - 2011-01-03
1192 * @param array $valArr
1193 * @param mixed $value
1194 * @param array $params
1195 * @param array $defaults
1196 * @param int $index
1197 * @return void
1198 */
1199  public static function _setMval( & $valArr, $value, $params=FALSE, $defaults=FALSE, $index=FALSE ) {
1200    if( !is_array( $valArr )) $valArr = array();
1201    if( $index )
1202      $index = $index - 1;
1203    elseif( 0 < count( $valArr )) {
1204      $keys  = array_keys( $valArr );
1205      $index = end( $keys ) + 1;
1206    }
1207    else
1208      $index = 0;
1209    $valArr[$index] = array( 'value' => $value, 'params' => iCalUtilityFunctions::_setParams( $params, $defaults ));
1210    ksort( $valArr );
1211  }
1212/**
1213 * set input (formatted) parameters- component property attributes
1214 *
1215 * default parameters can be set, if missing
1216 *
1217 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1218 * @since 1.x.x - 2007-05-01
1219 * @param array $params
1220 * @param array $defaults
1221 * @return array
1222 */
1223  public static function _setParams( $params, $defaults=FALSE ) {
1224    if( !is_array( $params))
1225      $params = array();
1226    $input = array();
1227    foreach( $params as $paramKey => $paramValue ) {
1228      if( is_array( $paramValue )) {
1229        foreach( $paramValue as $pkey => $pValue ) {
1230          if(( '"' == substr( $pValue, 0, 1 )) && ( '"' == substr( $pValue, -1 )))
1231            $paramValue[$pkey] = substr( $pValue, 1, ( strlen( $pValue ) - 2 ));
1232        }
1233      }
1234      elseif(( '"' == substr( $paramValue, 0, 1 )) && ( '"' == substr( $paramValue, -1 )))
1235        $paramValue = substr( $paramValue, 1, ( strlen( $paramValue ) - 2 ));
1236      if( 'VALUE' == strtoupper( $paramKey ))
1237        $input['VALUE']                 = strtoupper( $paramValue );
1238      else
1239        $input[strtoupper( $paramKey )] = $paramValue;
1240    }
1241    if( is_array( $defaults )) {
1242      foreach( $defaults as $paramKey => $paramValue ) {
1243        if( !isset( $input[$paramKey] ))
1244          $input[$paramKey] = $paramValue;
1245      }
1246    }
1247    return (0 < count( $input )) ? $input : null;
1248  }
1249/**
1250 * step date, return updated date, array and timpstamp
1251 *
1252 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1253 * @since 2.4.16 - 2008-10-18
1254 * @param array $date, date to step
1255 * @param int $timestamp
1256 * @param array $step, default array( 'day' => 1 )
1257 * @return void
1258 */
1259  public static function _stepdate( &$date, &$timestamp, $step=array( 'day' => 1 )) {
1260    foreach( $step as $stepix => $stepvalue )
1261      $date[$stepix] += $stepvalue;
1262    $timestamp  = iCalUtilityFunctions::_date2timestamp( $date );
1263    $date       = iCalUtilityFunctions::_timestamp2date( $timestamp, 6 );
1264    foreach( $date as $k => $v ) {
1265      if( ctype_digit( $v ))
1266        $date[$k] = (int) $v;
1267    }
1268  }
1269/**
1270 * convert timestamp to date array
1271 *
1272 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1273 * @since 2.4.16 - 2008-11-01
1274 * @param mixed $timestamp
1275 * @param int $parno
1276 * @return array
1277 */
1278  public static function _timestamp2date( $timestamp, $parno=6 ) {
1279    if( is_array( $timestamp )) {
1280      if(( 7 == $parno ) && !empty( $timestamp['tz'] ))
1281        $tz = $timestamp['tz'];
1282      $timestamp = $timestamp['timestamp'];
1283    }
1284    $output = array( 'year'  => date( 'Y', $timestamp )
1285                   , 'month' => date( 'm', $timestamp )
1286                   , 'day'   => date( 'd', $timestamp ));
1287    if( 3 != $parno ) {
1288             $output['hour'] =  date( 'H', $timestamp );
1289             $output['min']  =  date( 'i', $timestamp );
1290             $output['sec']  =  date( 's', $timestamp );
1291      if( isset( $tz ))
1292        $output['tz'] = $tz;
1293    }
1294    return $output;
1295  }
1296/**
1297 * convert timestamp to duration in array format
1298 *
1299 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1300 * @since 2.6.23 - 2010-10-23
1301 * @param int $timestamp
1302 * @return array, duration format
1303 */
1304  function _timestamp2duration( $timestamp ) {
1305    $dur         = array();
1306    $dur['week'] = (int) floor( $timestamp / ( 7 * 24 * 60 * 60 ));
1307    $timestamp   =              $timestamp % ( 7 * 24 * 60 * 60 );
1308    $dur['day']  = (int) floor( $timestamp / ( 24 * 60 * 60 ));
1309    $timestamp   =              $timestamp % ( 24 * 60 * 60 );
1310    $dur['hour'] = (int) floor( $timestamp / ( 60 * 60 ));
1311    $timestamp   =              $timestamp % ( 60 * 60 );
1312    $dur['min']  = (int) floor( $timestamp / ( 60 ));
1313    $dur['sec']  = (int)        $timestamp % ( 60 );
1314    return $dur;
1315  }
1316/**
1317 * convert (numeric) local time offset to seconds correcting localtime to GMT
1318 *
1319 * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
1320 * @since 2.4.16 - 2008-10-19
1321 * @param string $offset
1322 * @return integer
1323 */
1324  public static function _tz2offset( $tz ) {
1325    $tz           = trim( (string) $tz );
1326    $offset       = 0;
1327    if(((     5  != strlen( $tz )) && ( 7  != strlen( $tz ))) ||
1328       ((    '+' != substr( $tz, 0, 1 )) && ( '-' != substr( $tz, 0, 1 ))) ||
1329       (( '0000' >= substr( $tz, 1, 4 )) && ( '9999' < substr( $tz, 1, 4 ))) ||
1330           (( 7  == strlen( $tz )) && ( '00' > substr( $tz, 5, 2 )) && ( '99' < substr( $tz, 5, 2 ))))
1331      return $offset;
1332    $hours2sec    = (int) substr( $tz, 1, 2 ) * 3600;
1333    $min2sec      = (int) substr( $tz, 3, 2 ) *   60;
1334    $sec          = ( 7  == strlen( $tz )) ? (int) substr( $tz, -2 ) : '00';
1335    $offset       = $hours2sec + $min2sec + $sec;
1336    $offset       = ('-' == substr( $tz, 0, 1 )) ? $offset : -1 * $offset;
1337    return $offset;
1338  }
1339}
1340?>
Note: See TracBrowser for help on using the repository browser.