PageRenderTime 80ms CodeModel.GetById 17ms RepoModel.GetById 5ms app.codeStats 0ms

/wp-content/plugins/all-in-one-event-calendar/lib/iCalcreator-2.10.23/iCalUtilityFunctions.class.php

https://gitlab.com/endomorphosis/jeffersonsmithmayor
PHP | 1175 lines | 919 code | 4 blank | 252 comment | 377 complexity | c3695e00bab3ccde574604bb0ad299e4 MD5 | raw file
  1. <?php
  2. /**
  3. * iCalcreator v2.10.23
  4. * copyright (c) 2007-2011 Kjell-Inge Gustafsson kigkonsult
  5. * 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, kigkonsult <ical@kigkonsult.se>
  26. * @since 2.10.1 - 2011-07-16
  27. *
  28. */
  29. class iCalUtilityFunctions {
  30. // Store the single instance of iCalUtilityFunctions
  31. private static $m_pInstance;
  32. // Private constructor to limit object instantiation to within the class
  33. private function __construct() {
  34. $m_pInstance = FALSE;
  35. }
  36. // Getter method for creating/returning the single instance of this class
  37. public static function getInstance() {
  38. if (!self::$m_pInstance)
  39. self::$m_pInstance = new iCalUtilityFunctions();
  40. return self::$m_pInstance;
  41. }
  42. /**
  43. * check a date(-time) for an opt. timezone and if it is a DATE-TIME or DATE
  44. *
  45. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  46. * @since 2.4.16 - 2008-10-25
  47. * @param array $date, date to check
  48. * @param int $parno, no of date parts (i.e. year, month.. .)
  49. * @return array $params, property parameters
  50. */
  51. public static function _chkdatecfg( $theDate, & $parno, & $params ) {
  52. if( isset( $params['TZID'] ))
  53. $parno = 6;
  54. elseif( isset( $params['VALUE'] ) && ( 'DATE' == $params['VALUE'] ))
  55. $parno = 3;
  56. else {
  57. if( isset( $params['VALUE'] ) && ( 'PERIOD' == $params['VALUE'] ))
  58. $parno = 7;
  59. if( is_array( $theDate )) {
  60. if( isset( $theDate['timestamp'] ))
  61. $tzid = ( isset( $theDate['tz'] )) ? $theDate['tz'] : null;
  62. else
  63. $tzid = ( isset( $theDate['tz'] )) ? $theDate['tz'] : ( 7 == count( $theDate )) ? end( $theDate ) : null;
  64. if( !empty( $tzid )) {
  65. $parno = 7;
  66. if( !iCalUtilityFunctions::_isOffset( $tzid ))
  67. $params['TZID'] = $tzid; // save only timezone
  68. }
  69. elseif( !$parno && ( 3 == count( $theDate )) &&
  70. ( isset( $params['VALUE'] ) && ( 'DATE' == $params['VALUE'] )))
  71. $parno = 3;
  72. else
  73. $parno = 6;
  74. }
  75. else { // string
  76. $date = trim( $theDate );
  77. if( 'Z' == substr( $date, -1 ))
  78. $parno = 7; // UTC DATE-TIME
  79. elseif((( 8 == strlen( $date ) && ctype_digit( $date )) || ( 11 >= strlen( $date ))) &&
  80. ( !isset( $params['VALUE'] ) || !in_array( $params['VALUE'], array( 'DATE-TIME', 'PERIOD' ))))
  81. $parno = 3; // DATE
  82. $date = iCalUtilityFunctions::_date_time_string( $date, $parno );
  83. if( !empty( $date['tz'] )) {
  84. $parno = 7;
  85. if( !iCalUtilityFunctions::_isOffset( $date['tz'] ))
  86. $params['TZID'] = $date['tz']; // save only timezone
  87. }
  88. elseif( empty( $parno ))
  89. $parno = 6;
  90. }
  91. if( isset( $params['TZID'] ))
  92. $parno = 6;
  93. }
  94. }
  95. /**
  96. * create (very simple) timezone and standard/daylight components
  97. *
  98. * Result when 'Europe/Stockholm' is used as timezone:
  99. *
  100. * BEGIN:VTIMEZONE
  101. * TZID:Europe/Stockholm
  102. * BEGIN:STANDARD
  103. * DTSTART:20101031T020000
  104. * TZOFFSETFROM:+0200
  105. * TZOFFSETTO:+0100
  106. * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
  107. * TZNAME:CET
  108. * END:STANDARD
  109. * BEGIN:DAYLIGHT
  110. * DTSTART:20100328T030000
  111. * TZOFFSETFROM:+0100
  112. * TZOFFSETTO:+0200
  113. * RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
  114. * TZNAME:CEST
  115. * END:DAYLIGHT
  116. * END:VTIMEZONE
  117. *
  118. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  119. * @since 2.10.3 - 2011-08-01
  120. * @param object $calendar, reference to an iCalcreator calendar instance
  121. * @param string $timezone, a PHP5 (DateTimeZone) valid timezone
  122. * @param array $xProp, *[x-propName => x-propValue], optional
  123. * @return bool
  124. */
  125. public static function createTimezone( & $calendar, $timezone, $xProp=array() ) {
  126. if( !class_exists( 'DateTimeZone' ))
  127. return FALSE;
  128. if( empty( $timezone ))
  129. return FALSE;
  130. try {
  131. $dtz = new DateTimeZone( $timezone );
  132. }
  133. catch( Exception $e ) {
  134. return FALSE;
  135. }
  136. $stdDTSTART = $stdTZOFFSETTO = $stdTZOFFSETFROM = $stdTZNAME = $dlghtDTSTART = $dlghtTZOFFSETTO = $dlghtTZOFFSETFROM = $dlghtTZNAME = FALSE;
  137. $dateNow = new DateTime();
  138. $transitions = $dtz->getTransitions();
  139. foreach( $transitions as $trans ) {
  140. if( FALSE === ( $date = DateTime::createFromFormat( 'Y-m-d', substr( $trans['time'], 0, 10 ))))
  141. continue;
  142. if( $date > $dateNow )
  143. break;
  144. if( TRUE !== $trans['isdst'] ) {
  145. $stdDTSTART = $trans['time'];
  146. $stdTZOFFSETTO = $dlghtTZOFFSETFROM = iCalUtilityFunctions::offsetSec2His( $trans['offset'] );
  147. $stdTZNAME = $trans['abbr'];
  148. }
  149. else {
  150. $dlghtDTSTART = $trans['time'];
  151. $dlghtTZOFFSETTO = $stdTZOFFSETFROM = iCalUtilityFunctions::offsetSec2His( $trans['offset'] );
  152. $dlghtTZNAME = $trans['abbr'];
  153. }
  154. }
  155. if( !$stdDTSTART || !$stdTZOFFSETTO || !$stdTZOFFSETFROM )
  156. return FALSE;
  157. $tz = & $calendar->newComponent( 'vtimezone' );
  158. $tz->setproperty( 'tzid', $timezone );
  159. if( !empty( $xProp )) {
  160. foreach( $xProp as $xPropName => $xPropValue )
  161. if( 'x-' == strtolower( substr( $xPropName, 0, 2 )))
  162. $tz->setproperty( $xPropName, $xPropValue );
  163. }
  164. $std = & $tz->newComponent( 'standard' );
  165. $std->setProperty( 'dtstart', $stdDTSTART );
  166. if( $stdTZNAME )
  167. $std->setProperty( 'tzname', $stdTZNAME );
  168. $std->setProperty( 'tzoffsetto', $stdTZOFFSETTO );
  169. $std->setProperty( 'tzoffsetfrom', $stdTZOFFSETFROM );
  170. if(( $stdTZOFFSETTO != $stdTZOFFSETFROM ) && ( FALSE === iCalUtilityFunctions::_setTZrrule( $std )))
  171. $std->setProperty( 'RRULE', array( 'FREQ' => 'YEARLY', 'BYDAY' => array( '-1', 'DAY' => 'SU' ), 'BYMONTH' => 10 ));
  172. if(( !$dlghtDTSTART || !$dlghtTZOFFSETTO || !$dlghtTZOFFSETFROM ) || ( $dlghtTZOFFSETTO == $dlghtTZOFFSETFROM ))
  173. return TRUE;
  174. $dlght = & $tz->newComponent( 'daylight' );
  175. $dlght->setProperty( 'dtstart', $dlghtDTSTART );
  176. if( $dlghtTZNAME )
  177. $dlght->setProperty( 'tzname', $dlghtTZNAME );
  178. $dlght->setProperty( 'tzoffsetto', $dlghtTZOFFSETTO );
  179. $dlght->setProperty( 'tzoffsetfrom', $dlghtTZOFFSETFROM );
  180. if( FALSE === iCalUtilityFunctions::_setTZrrule( $dlght ))
  181. $dlght->setProperty( 'RRULE', array( 'FREQ' => 'YEARLY', 'BYDAY' => array( '-1', 'DAY' => 'SU' ), 'BYMONTH' => 3 ));
  182. return TRUE;
  183. }
  184. /**
  185. * convert date/datetime to timestamp
  186. *
  187. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  188. * @since 2.4.8 - 2008-10-30
  189. * @param array $datetime datetime/(date)
  190. * @param string $tz timezone
  191. * @return timestamp
  192. */
  193. public static function _date2timestamp( $datetime, $tz=null ) {
  194. $output = null;
  195. if( !isset( $datetime['hour'] )) $datetime['hour'] = '0';
  196. if( !isset( $datetime['min'] )) $datetime['min'] = '0';
  197. if( !isset( $datetime['sec'] )) $datetime['sec'] = '0';
  198. foreach( $datetime as $dkey => $dvalue ) {
  199. if( 'tz' != $dkey )
  200. $datetime[$dkey] = (integer) $dvalue;
  201. }
  202. if( $tz )
  203. $datetime['tz'] = $tz;
  204. $offset = ( isset( $datetime['tz'] ) && ( '' < trim ( $datetime['tz'] ))) ? iCalUtilityFunctions::_tz2offset( $datetime['tz'] ) : 0;
  205. $output = mktime( $datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year'] );
  206. return $output;
  207. }
  208. /**
  209. * ensures internal date-time/date format for input date-time/date in array format
  210. *
  211. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  212. * @since 0.3.0 - 2006-08-15
  213. * @param array $datetime
  214. * @param int $parno optional, default FALSE
  215. * @return array
  216. */
  217. public static function _date_time_array( $datetime, $parno=FALSE ) {
  218. $output = array();
  219. foreach( $datetime as $dateKey => $datePart ) {
  220. switch ( $dateKey ) {
  221. case '0': case 'year': $output['year'] = $datePart; break;
  222. case '1': case 'month': $output['month'] = $datePart; break;
  223. case '2': case 'day': $output['day'] = $datePart; break;
  224. }
  225. if( 3 != $parno ) {
  226. switch ( $dateKey ) {
  227. case '0':
  228. case '1':
  229. case '2': break;
  230. case '3': case 'hour': $output['hour'] = $datePart; break;
  231. case '4': case 'min' : $output['min'] = $datePart; break;
  232. case '5': case 'sec' : $output['sec'] = $datePart; break;
  233. case '6': case 'tz' : $output['tz'] = $datePart; break;
  234. }
  235. }
  236. }
  237. if( 3 != $parno ) {
  238. if( !isset( $output['hour'] ))
  239. $output['hour'] = 0;
  240. if( !isset( $output['min'] ))
  241. $output['min'] = 0;
  242. if( !isset( $output['sec'] ))
  243. $output['sec'] = 0;
  244. }
  245. return $output;
  246. }
  247. /**
  248. * ensures internal date-time/date format for input date-time/date in string fromat
  249. *
  250. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  251. * @since 2.6.35 - 2010-12-03
  252. * @param array $datetime
  253. * @param int $parno optional, default FALSE
  254. * @return array
  255. */
  256. public static function _date_time_string( $datetime, $parno=FALSE ) {
  257. $datetime = (string) trim( $datetime );
  258. $tz = null;
  259. $len = strlen( $datetime ) - 1;
  260. if( 'Z' == substr( $datetime, -1 )) {
  261. $tz = 'Z';
  262. $datetime = trim( substr( $datetime, 0, $len ));
  263. }
  264. elseif( ( ctype_digit( substr( $datetime, -2, 2 ))) && // time or date
  265. ( '-' == substr( $datetime, -3, 1 )) ||
  266. ( ':' == substr( $datetime, -3, 1 )) ||
  267. ( '.' == substr( $datetime, -3, 1 ))) {
  268. $continue = TRUE;
  269. }
  270. elseif( ( ctype_digit( substr( $datetime, -4, 4 ))) && // 4 pos offset
  271. ( ' +' == substr( $datetime, -6, 2 )) ||
  272. ( ' -' == substr( $datetime, -6, 2 ))) {
  273. $tz = substr( $datetime, -5, 5 );
  274. $datetime = substr( $datetime, 0, ($len - 5));
  275. }
  276. elseif( ( ctype_digit( substr( $datetime, -6, 6 ))) && // 6 pos offset
  277. ( ' +' == substr( $datetime, -8, 2 )) ||
  278. ( ' -' == substr( $datetime, -8, 2 ))) {
  279. $tz = substr( $datetime, -7, 7 );
  280. $datetime = substr( $datetime, 0, ($len - 7));
  281. }
  282. elseif( ( 6 < $len ) && ( ctype_digit( substr( $datetime, -6, 6 )))) {
  283. $continue = TRUE;
  284. }
  285. elseif( 'T' == substr( $datetime, -7, 1 )) {
  286. $continue = TRUE;
  287. }
  288. else {
  289. $cx = $tx = 0; // 19970415T133000 US-Eastern
  290. for( $cx = -1; $cx > ( 9 - $len ); $cx-- ) {
  291. $char = substr( $datetime, $cx, 1 );
  292. if(( ' ' == $char) || ctype_digit( $char))
  293. break; // if exists, tz ends here.. . ?
  294. else
  295. $tx--; // tz length counter
  296. }
  297. if( 0 > $tx ) {
  298. $tz = substr( $datetime, $tx );
  299. $datetime = trim( substr( $datetime, 0, $len + $tx + 1 ));
  300. }
  301. }
  302. if( 0 < substr_count( $datetime, '-' )) {
  303. $datetime = str_replace( '-', '/', $datetime );
  304. }
  305. elseif( ctype_digit( substr( $datetime, 0, 8 )) &&
  306. ( 'T' == substr( $datetime, 8, 1 )) &&
  307. ctype_digit( substr( $datetime, 9, 6 ))) {
  308. $datetime = substr( $datetime, 4, 2 )
  309. .'/'.substr( $datetime, 6, 2 )
  310. .'/'.substr( $datetime, 0, 4 )
  311. .' '.substr( $datetime, 9, 2 )
  312. .':'.substr( $datetime, 11, 2 )
  313. .':'.substr( $datetime, 13);
  314. }
  315. $datestring = date( 'Y-m-d H:i:s', strtotime( $datetime ));
  316. $tz = trim( $tz );
  317. $output = array();
  318. $output['year'] = substr( $datestring, 0, 4 );
  319. $output['month'] = substr( $datestring, 5, 2 );
  320. $output['day'] = substr( $datestring, 8, 2 );
  321. if(( 6 == $parno ) || ( 7 == $parno ) || ( !$parno && ( 'Z' == $tz ))) {
  322. $output['hour'] = substr( $datestring, 11, 2 );
  323. $output['min'] = substr( $datestring, 14, 2 );
  324. $output['sec'] = substr( $datestring, 17, 2 );
  325. if( !empty( $tz ))
  326. $output['tz'] = $tz;
  327. }
  328. elseif( 3 != $parno ) {
  329. if(( '00' < substr( $datestring, 11, 2 )) ||
  330. ( '00' < substr( $datestring, 14, 2 )) ||
  331. ( '00' < substr( $datestring, 17, 2 ))) {
  332. $output['hour'] = substr( $datestring, 11, 2 );
  333. $output['min'] = substr( $datestring, 14, 2 );
  334. $output['sec'] = substr( $datestring, 17, 2 );
  335. }
  336. if( !empty( $tz ))
  337. $output['tz'] = $tz;
  338. }
  339. return $output;
  340. }
  341. /**
  342. * convert local startdate/enddate (Ymd[His]) to duration array
  343. *
  344. * uses this component dates if missing input dates
  345. *
  346. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  347. * @since 2.6.11 - 2010-10-21
  348. * @param array $startdate
  349. * @param array $duration
  350. * @return array duration
  351. */
  352. public static function _date2duration( $startdate, $enddate ) {
  353. $startWdate = mktime( 0, 0, 0, $startdate['month'], $startdate['day'], $startdate['year'] );
  354. $endWdate = mktime( 0, 0, 0, $enddate['month'], $enddate['day'], $enddate['year'] );
  355. $wduration = $endWdate - $startWdate;
  356. $dur = array();
  357. $dur['week'] = (int) floor( $wduration / ( 7 * 24 * 60 * 60 ));
  358. $wduration = $wduration % ( 7 * 24 * 60 * 60 );
  359. $dur['day'] = (int) floor( $wduration / ( 24 * 60 * 60 ));
  360. $wduration = $wduration % ( 24 * 60 * 60 );
  361. $dur['hour'] = (int) floor( $wduration / ( 60 * 60 ));
  362. $wduration = $wduration % ( 60 * 60 );
  363. $dur['min'] = (int) floor( $wduration / ( 60 ));
  364. $dur['sec'] = (int) $wduration % ( 60 );
  365. return $dur;
  366. }
  367. /**
  368. * ensures internal duration format for input in array format
  369. *
  370. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  371. * @since 2.1.1 - 2007-06-24
  372. * @param array $duration
  373. * @return array
  374. */
  375. public static function _duration_array( $duration ) {
  376. $output = array();
  377. if( is_array( $duration ) &&
  378. ( 1 == count( $duration )) &&
  379. isset( $duration['sec'] ) &&
  380. ( 60 < $duration['sec'] )) {
  381. $durseconds = $duration['sec'];
  382. $output['week'] = floor( $durseconds / ( 60 * 60 * 24 * 7 ));
  383. $durseconds = $durseconds % ( 60 * 60 * 24 * 7 );
  384. $output['day'] = floor( $durseconds / ( 60 * 60 * 24 ));
  385. $durseconds = $durseconds % ( 60 * 60 * 24 );
  386. $output['hour'] = floor( $durseconds / ( 60 * 60 ));
  387. $durseconds = $durseconds % ( 60 * 60 );
  388. $output['min'] = floor( $durseconds / ( 60 ));
  389. $output['sec'] = ( $durseconds % ( 60 ));
  390. }
  391. else {
  392. foreach( $duration as $durKey => $durValue ) {
  393. if( empty( $durValue )) continue;
  394. switch ( $durKey ) {
  395. case '0': case 'week': $output['week'] = $durValue; break;
  396. case '1': case 'day': $output['day'] = $durValue; break;
  397. case '2': case 'hour': $output['hour'] = $durValue; break;
  398. case '3': case 'min': $output['min'] = $durValue; break;
  399. case '4': case 'sec': $output['sec'] = $durValue; break;
  400. }
  401. }
  402. }
  403. if( isset( $output['week'] ) && ( 0 < $output['week'] )) {
  404. unset( $output['day'], $output['hour'], $output['min'], $output['sec'] );
  405. return $output;
  406. }
  407. unset( $output['week'] );
  408. if( empty( $output['day'] ))
  409. unset( $output['day'] );
  410. if ( isset( $output['hour'] ) || isset( $output['min'] ) || isset( $output['sec'] )) {
  411. if( !isset( $output['hour'] )) $output['hour'] = 0;
  412. if( !isset( $output['min'] )) $output['min'] = 0;
  413. if( !isset( $output['sec'] )) $output['sec'] = 0;
  414. if(( 0 == $output['hour'] ) && ( 0 == $output['min'] ) && ( 0 == $output['sec'] ))
  415. unset( $output['hour'], $output['min'], $output['sec'] );
  416. }
  417. return $output;
  418. }
  419. /**
  420. * ensures internal duration format for input in string format
  421. *
  422. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  423. * @since 2.0.5 - 2007-03-14
  424. * @param string $duration
  425. * @return array
  426. */
  427. public static function _duration_string( $duration ) {
  428. $duration = (string) trim( $duration );
  429. while( 'P' != strtoupper( substr( $duration, 0, 1 ))) {
  430. if( 0 < strlen( $duration ))
  431. $duration = substr( $duration, 1 );
  432. else
  433. return false; // no leading P !?!?
  434. }
  435. $duration = substr( $duration, 1 ); // skip P
  436. $duration = str_replace ( 't', 'T', $duration );
  437. $duration = str_replace ( 'T', '', $duration );
  438. $output = array();
  439. $val = null;
  440. for( $ix=0; $ix < strlen( $duration ); $ix++ ) {
  441. switch( strtoupper( substr( $duration, $ix, 1 ))) {
  442. case 'W':
  443. $output['week'] = $val;
  444. $val = null;
  445. break;
  446. case 'D':
  447. $output['day'] = $val;
  448. $val = null;
  449. break;
  450. case 'H':
  451. $output['hour'] = $val;
  452. $val = null;
  453. break;
  454. case 'M':
  455. $output['min'] = $val;
  456. $val = null;
  457. break;
  458. case 'S':
  459. $output['sec'] = $val;
  460. $val = null;
  461. break;
  462. default:
  463. if( !ctype_digit( substr( $duration, $ix, 1 )))
  464. return false; // unknown duration control character !?!?
  465. else
  466. $val .= substr( $duration, $ix, 1 );
  467. }
  468. }
  469. return iCalUtilityFunctions::_duration_array( $output );
  470. }
  471. /**
  472. * convert duration to date in array format
  473. *
  474. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  475. * @since 2.8.7 - 2011-03-03
  476. * @param array $startdate
  477. * @param array $duration
  478. * @return array, date format
  479. */
  480. public static function _duration2date( $startdate=null, $duration=null ) {
  481. if( empty( $startdate )) return FALSE;
  482. if( empty( $duration )) return FALSE;
  483. $dateOnly = ( isset( $startdate['hour'] ) || isset( $startdate['min'] ) || isset( $startdate['sec'] )) ? FALSE : TRUE;
  484. $startdate['hour'] = ( isset( $startdate['hour'] )) ? $startdate['hour'] : 0;
  485. $startdate['min'] = ( isset( $startdate['min'] )) ? $startdate['min'] : 0;
  486. $startdate['sec'] = ( isset( $startdate['sec'] )) ? $startdate['sec'] : 0;
  487. $dtend = 0;
  488. if( isset( $duration['week'] ))
  489. $dtend += ( $duration['week'] * 7 * 24 * 60 * 60 );
  490. if( isset( $duration['day'] ))
  491. $dtend += ( $duration['day'] * 24 * 60 * 60 );
  492. if( isset( $duration['hour'] ))
  493. $dtend += ( $duration['hour'] * 60 *60 );
  494. if( isset( $duration['min'] ))
  495. $dtend += ( $duration['min'] * 60 );
  496. if( isset( $duration['sec'] ))
  497. $dtend += $duration['sec'];
  498. $dtend = mktime( $startdate['hour'], $startdate['min'], ( $startdate['sec'] + $dtend ), $startdate['month'], $startdate['day'], $startdate['year'] );
  499. $dtend2 = array();
  500. $dtend2['year'] = date('Y', $dtend );
  501. $dtend2['month'] = date('m', $dtend );
  502. $dtend2['day'] = date('d', $dtend );
  503. $dtend2['hour'] = date('H', $dtend );
  504. $dtend2['min'] = date('i', $dtend );
  505. $dtend2['sec'] = date('s', $dtend );
  506. if( isset( $startdate['tz'] ))
  507. $dtend2['tz'] = $startdate['tz'];
  508. if( $dateOnly && (( 0 == $dtend2['hour'] ) && ( 0 == $dtend2['min'] ) && ( 0 == $dtend2['sec'] )))
  509. unset( $dtend2['hour'], $dtend2['min'], $dtend2['sec'] );
  510. return $dtend2;
  511. }
  512. /**
  513. * if not preSet, if exist, remove key with expected value from array and return hit value else return elseValue
  514. *
  515. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  516. * @since 2.4.16 - 2008-11-08
  517. * @param array $array
  518. * @param string $expkey, expected key
  519. * @param string $expval, expected value
  520. * @param int $hitVal optional, return value if found
  521. * @param int $elseVal optional, return value if not found
  522. * @param int $preSet optional, return value if already preset
  523. * @return int
  524. */
  525. public static function _existRem( &$array, $expkey, $expval=FALSE, $hitVal=null, $elseVal=null, $preSet=null ) {
  526. if( $preSet )
  527. return $preSet;
  528. if( !is_array( $array ) || ( 0 == count( $array )))
  529. return $elseVal;
  530. foreach( $array as $key => $value ) {
  531. if( strtoupper( $expkey ) == strtoupper( $key )) {
  532. if( !$expval || ( strtoupper( $expval ) == strtoupper( $array[$key] ))) {
  533. unset( $array[$key] );
  534. return $hitVal;
  535. }
  536. }
  537. }
  538. return $elseVal;
  539. }
  540. /**
  541. * creates formatted output for calendar component property data value type date/date-time
  542. *
  543. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  544. * @since 2.4.8 - 2008-10-30
  545. * @param array $datetime
  546. * @param int $parno, optional, default 6
  547. * @return string
  548. */
  549. public static function _format_date_time( $datetime, $parno=6 ) {
  550. if( !isset( $datetime['year'] ) &&
  551. !isset( $datetime['month'] ) &&
  552. !isset( $datetime['day'] ) &&
  553. !isset( $datetime['hour'] ) &&
  554. !isset( $datetime['min'] ) &&
  555. !isset( $datetime['sec'] ))
  556. return ;
  557. $output = null;
  558. // 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"; }
  559. foreach( $datetime as $dkey => & $dvalue )
  560. if( 'tz' != $dkey ) $dvalue = (integer) $dvalue;
  561. $output = date('Ymd', mktime( 0, 0, 0, $datetime['month'], $datetime['day'], $datetime['year']));
  562. if( isset( $datetime['hour'] ) ||
  563. isset( $datetime['min'] ) ||
  564. isset( $datetime['sec'] ) ||
  565. isset( $datetime['tz'] )) {
  566. if( isset( $datetime['tz'] ) &&
  567. !isset( $datetime['hour'] ))
  568. $datetime['hour'] = 0;
  569. if( isset( $datetime['hour'] ) &&
  570. !isset( $datetime['min'] ))
  571. $datetime['min'] = 0;
  572. if( isset( $datetime['hour'] ) &&
  573. isset( $datetime['min'] ) &&
  574. !isset( $datetime['sec'] ))
  575. $datetime['sec'] = 0;
  576. $date = mktime( $datetime['hour'], $datetime['min'], $datetime['sec'], $datetime['month'], $datetime['day'], $datetime['year']);
  577. $output .= date('\THis', $date );
  578. if( isset( $datetime['tz'] ) && ( '' < trim ( $datetime['tz'] ))) {
  579. $datetime['tz'] = trim( $datetime['tz'] );
  580. if( 'Z' == $datetime['tz'] )
  581. $output .= 'Z';
  582. $offset = iCalUtilityFunctions::_tz2offset( $datetime['tz'] );
  583. if( 0 != $offset ) {
  584. $date = mktime( $datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
  585. $output = date( 'Ymd\THis\Z', $date );
  586. }
  587. }
  588. elseif( 7 == $parno )
  589. $output .= 'Z';
  590. }
  591. return $output;
  592. }
  593. /**
  594. * creates formatted output for calendar component property data value type duration
  595. *
  596. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  597. * @since 2.9.9 - 2011-06-17
  598. * @param array $duration ( week, day, hour, min, sec )
  599. * @return string
  600. */
  601. public static function _format_duration( $duration ) {
  602. if( isset( $duration['week'] ) ||
  603. isset( $duration['day'] ) ||
  604. isset( $duration['hour'] ) ||
  605. isset( $duration['min'] ) ||
  606. isset( $duration['sec'] ))
  607. $ok = TRUE;
  608. else
  609. return;
  610. if( isset( $duration['week'] ) && ( 0 < $duration['week'] ))
  611. return 'P'.$duration['week'].'W';
  612. $output = 'P';
  613. if( isset($duration['day'] ) && ( 0 < $duration['day'] ))
  614. $output .= $duration['day'].'D';
  615. if(( isset( $duration['hour']) && ( 0 < $duration['hour'] )) ||
  616. ( isset( $duration['min']) && ( 0 < $duration['min'] )) ||
  617. ( isset( $duration['sec']) && ( 0 < $duration['sec'] )))
  618. $output .= 'T';
  619. $output .= ( isset( $duration['hour']) && ( 0 < $duration['hour'] )) ? $duration['hour'].'H' : '';
  620. $output .= ( isset( $duration['min']) && ( 0 < $duration['min'] )) ? $duration['min']. 'M' : '';
  621. $output .= ( isset( $duration['sec']) && ( 0 < $duration['sec'] )) ? $duration['sec']. 'S' : '';
  622. if( 'P' == $output )
  623. $output = 'PT0S';
  624. return $output;
  625. }
  626. /**
  627. * checks if input array contains a date
  628. *
  629. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  630. * @since 2.4.16 - 2008-10-25
  631. * @param array $input
  632. * @return bool
  633. */
  634. public static function _isArrayDate( $input ) {
  635. if( isset( $input['week'] ) || ( !in_array( count( $input ), array( 3, 6, 7 ))))
  636. return FALSE;
  637. if( 7 == count( $input ))
  638. return TRUE;
  639. if( isset( $input['year'] ) && isset( $input['month'] ) && isset( $input['day'] ))
  640. return checkdate( (int) $input['month'], (int) $input['day'], (int) $input['year'] );
  641. if( isset( $input['day'] ) || isset( $input['hour'] ) || isset( $input['min'] ) || isset( $input['sec'] ))
  642. return FALSE;
  643. if( in_array( 0, $input ))
  644. return FALSE;
  645. if(( 1970 > $input[0] ) || ( 12 < $input[1] ) || ( 31 < $input[2] ))
  646. return FALSE;
  647. if(( isset( $input[0] ) && isset( $input[1] ) && isset( $input[2] )) &&
  648. checkdate( (int) $input[1], (int) $input[2], (int) $input[0] ))
  649. return TRUE;
  650. $input = iCalUtilityFunctions::_date_time_string( $input[1].'/'.$input[2].'/'.$input[0], 3 ); // m - d - Y
  651. if( isset( $input['year'] ) && isset( $input['month'] ) && isset( $input['day'] ))
  652. return checkdate( (int) $input['month'], (int) $input['day'], (int) $input['year'] );
  653. return FALSE;
  654. }
  655. /**
  656. * checks if input array contains a timestamp date
  657. *
  658. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  659. * @since 2.4.16 - 2008-10-18
  660. * @param array $input
  661. * @return bool
  662. */
  663. public static function _isArrayTimestampDate( $input ) {
  664. return ( is_array( $input ) && isset( $input['timestamp'] )) ? TRUE : FALSE ;
  665. }
  666. /**
  667. * controll if input string contains trailing UTC offset
  668. *
  669. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  670. * @since 2.4.16 - 2008-10-19
  671. * @param string $input
  672. * @return bool
  673. */
  674. public static function _isOffset( $input ) {
  675. $input = trim( (string) $input );
  676. if( 'Z' == substr( $input, -1 ))
  677. return TRUE;
  678. elseif(( 5 <= strlen( $input )) &&
  679. ( in_array( substr( $input, -5, 1 ), array( '+', '-' ))) &&
  680. ( '0000' < substr( $input, -4 )) && ( '9999' >= substr( $input, -4 )))
  681. return TRUE;
  682. elseif(( 7 <= strlen( $input )) &&
  683. ( in_array( substr( $input, -7, 1 ), array( '+', '-' ))) &&
  684. ( '000000' < substr( $input, -6 )) && ( '999999' >= substr( $input, -6 )))
  685. return TRUE;
  686. return FALSE;
  687. }
  688. /**
  689. * transform offset in seconds to [-/+]hhmm[ss]
  690. *
  691. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  692. * @since 2011-05-02
  693. * @param string $seconds
  694. * @return string
  695. */
  696. public static function offsetSec2His( $seconds ) {
  697. if( '-' == substr( $seconds, 0, 1 )) {
  698. $prefix = '-';
  699. $seconds = substr( $seconds, 1 );
  700. }
  701. elseif( '+' == substr( $seconds, 0, 1 )) {
  702. $prefix = '+';
  703. $seconds = substr( $seconds, 1 );
  704. }
  705. else
  706. $prefix = '+';
  707. $output = '';
  708. $hour = (int) floor( $seconds / 3600 );
  709. if( 10 > $hour )
  710. $hour = '0'.$hour;
  711. $seconds = $seconds % 3600;
  712. $min = (int) floor( $seconds / 60 );
  713. if( 10 > $min )
  714. $min = '0'.$min;
  715. $output = $hour.$min;
  716. $seconds = $seconds % 60;
  717. if( 0 < $seconds) {
  718. if( 9 < $seconds)
  719. $output .= $seconds;
  720. else
  721. $output .= '0'.$seconds;
  722. }
  723. return $prefix.$output;
  724. }
  725. /**
  726. * remakes a recur pattern to an array of dates
  727. *
  728. * if missing, UNTIL is set 1 year from startdate (emergency break)
  729. *
  730. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  731. * @since 2.10.19 - 2011-10-31
  732. * @param array $result, array to update, array([timestamp] => timestamp)
  733. * @param array $recur, pattern for recurrency (only value part, params ignored)
  734. * @param array $wdate, component start date
  735. * @param array $startdate, start date
  736. * @param array $enddate, optional
  737. * @return array of recurrence (start-)dates as index
  738. * @todo BYHOUR, BYMINUTE, BYSECOND, WEEKLY at year end/start
  739. */
  740. public static function _recur2date( & $result, $recur, $wdate, $startdate, $enddate=FALSE ) {
  741. foreach( $wdate as $k => $v ) if( ctype_digit( $v )) $wdate[$k] = (int) $v;
  742. $wdateStart = $wdate;
  743. $wdatets = iCalUtilityFunctions::_date2timestamp( $wdate );
  744. $startdatets = iCalUtilityFunctions::_date2timestamp( $startdate );
  745. if( !$enddate ) {
  746. $enddate = $startdate;
  747. $enddate['year'] += 1;
  748. }
  749. // echo "recur __in_ comp start ".implode('-',$wdate)." period start ".implode('-',$startdate)." period end ".implode('-',$enddate)."<br />\n";print_r($recur);echo "<br />\n";//test###
  750. $endDatets = iCalUtilityFunctions::_date2timestamp( $enddate ); // fix break
  751. if( !isset( $recur['COUNT'] ) && !isset( $recur['UNTIL'] ))
  752. $recur['UNTIL'] = $enddate; // create break
  753. if( isset( $recur['UNTIL'] )) {
  754. $tdatets = iCalUtilityFunctions::_date2timestamp( $recur['UNTIL'] );
  755. if( $endDatets > $tdatets ) {
  756. $endDatets = $tdatets; // emergency break
  757. $enddate = iCalUtilityFunctions::_timestamp2date( $endDatets, 6 );
  758. }
  759. else
  760. $recur['UNTIL'] = iCalUtilityFunctions::_timestamp2date( $endDatets, 6 );
  761. }
  762. if( $wdatets > $endDatets ) {
  763. // echo "recur out of date ".date('Y-m-d H:i:s',$wdatets)."<br />\n";//test
  764. return array(); // nothing to do.. .
  765. }
  766. if( !isset( $recur['FREQ'] )) // "MUST be specified.. ."
  767. $recur['FREQ'] = 'DAILY'; // ??
  768. $wkst = ( isset( $recur['WKST'] ) && ( 'SU' == $recur['WKST'] )) ? 24*60*60 : 0; // ??
  769. $weekStart = (int) date( 'W', ( $wdatets + $wkst ));
  770. if( !isset( $recur['INTERVAL'] ))
  771. $recur['INTERVAL'] = 1;
  772. $countcnt = ( !isset( $recur['BYSETPOS'] )) ? 1 : 0; // DTSTART counts as the first occurrence
  773. /* find out how to step up dates and set index for interval count */
  774. $step = array();
  775. if( 'YEARLY' == $recur['FREQ'] )
  776. $step['year'] = 1;
  777. elseif( 'MONTHLY' == $recur['FREQ'] )
  778. $step['month'] = 1;
  779. elseif( 'WEEKLY' == $recur['FREQ'] )
  780. $step['day'] = 7;
  781. else
  782. $step['day'] = 1;
  783. if( isset( $step['year'] ) && isset( $recur['BYMONTH'] ))
  784. $step = array( 'month' => 1 );
  785. if( empty( $step ) && isset( $recur['BYWEEKNO'] )) // ??
  786. $step = array( 'day' => 7 );
  787. if( isset( $recur['BYYEARDAY'] ) || isset( $recur['BYMONTHDAY'] ) || isset( $recur['BYDAY'] ))
  788. $step = array( 'day' => 1 );
  789. $intervalarr = array();
  790. if( 1 < $recur['INTERVAL'] ) {
  791. $intervalix = iCalUtilityFunctions::_recurIntervalIx( $recur['FREQ'], $wdate, $wkst );
  792. $intervalarr = array( $intervalix => 0 );
  793. }
  794. if( isset( $recur['BYSETPOS'] )) { // save start date + weekno
  795. $bysetposymd1 = $bysetposymd2 = $bysetposw1 = $bysetposw2 = array();
  796. // echo "bysetposXold_start=$bysetposYold $bysetposMold $bysetposDold<br />\n"; // test ###
  797. if( is_array( $recur['BYSETPOS'] )) {
  798. foreach( $recur['BYSETPOS'] as $bix => $bval )
  799. $recur['BYSETPOS'][$bix] = (int) $bval;
  800. }
  801. else
  802. $recur['BYSETPOS'] = array( (int) $recur['BYSETPOS'] );
  803. if( 'YEARLY' == $recur['FREQ'] ) {
  804. $wdate['month'] = $wdate['day'] = 1; // start from beginning of year
  805. $wdatets = iCalUtilityFunctions::_date2timestamp( $wdate );
  806. iCalUtilityFunctions::_stepdate( $enddate, $endDatets, array( 'year' => 1 )); // make sure to count whole last year
  807. }
  808. elseif( 'MONTHLY' == $recur['FREQ'] ) {
  809. $wdate['day'] = 1; // start from beginning of month
  810. $wdatets = iCalUtilityFunctions::_date2timestamp( $wdate );
  811. iCalUtilityFunctions::_stepdate( $enddate, $endDatets, array( 'month' => 1 )); // make sure to count whole last month
  812. }
  813. else
  814. iCalUtilityFunctions::_stepdate( $enddate, $endDatets, $step); // make sure to count whole last period
  815. // echo "BYSETPOS endDat++ =".implode('-',$enddate).' step='.var_export($step,TRUE)."<br />\n";//test###
  816. $bysetposWold = (int) date( 'W', ( $wdatets + $wkst ));
  817. $bysetposYold = $wdate['year'];
  818. $bysetposMold = $wdate['month'];
  819. $bysetposDold = $wdate['day'];
  820. }
  821. else
  822. iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
  823. $year_old = null;
  824. $daynames = array( 'SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA' );
  825. /* MAIN LOOP */
  826. // echo "recur start ".implode('-',$wdate)." end ".implode('-',$enddate)."<br />\n";//test
  827. while( TRUE ) {
  828. if( isset( $endDatets ) && ( $wdatets > $endDatets ))
  829. break;
  830. if( isset( $recur['COUNT'] ) && ( $countcnt >= $recur['COUNT'] ))
  831. break;
  832. if( $year_old != $wdate['year'] ) {
  833. $year_old = $wdate['year'];
  834. $daycnts = array();
  835. $yeardays = $weekno = 0;
  836. $yeardaycnt = array();
  837. foreach( $daynames as $dn )
  838. $yeardaycnt[$dn] = 0;
  839. for( $m = 1; $m <= 12; $m++ ) { // count up and update up-counters
  840. $daycnts[$m] = array();
  841. $weekdaycnt = array();
  842. foreach( $daynames as $dn )
  843. $weekdaycnt[$dn] = 0;
  844. $mcnt = date( 't', mktime( 0, 0, 0, $m, 1, $wdate['year'] ));
  845. for( $d = 1; $d <= $mcnt; $d++ ) {
  846. $daycnts[$m][$d] = array();
  847. if( isset( $recur['BYYEARDAY'] )) {
  848. $yeardays++;
  849. $daycnts[$m][$d]['yearcnt_up'] = $yeardays;
  850. }
  851. if( isset( $recur['BYDAY'] )) {
  852. $day = date( 'w', mktime( 0, 0, 0, $m, $d, $wdate['year'] ));
  853. $day = $daynames[$day];
  854. $daycnts[$m][$d]['DAY'] = $day;
  855. $weekdaycnt[$day]++;
  856. $daycnts[$m][$d]['monthdayno_up'] = $weekdaycnt[$day];
  857. $yeardaycnt[$day]++;
  858. $daycnts[$m][$d]['yeardayno_up'] = $yeardaycnt[$day];
  859. }
  860. if( isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' ))
  861. $daycnts[$m][$d]['weekno_up'] =(int)date('W',mktime(0,0,$wkst,$m,$d,$wdate['year']));
  862. }
  863. }
  864. $daycnt = 0;
  865. $yeardaycnt = array();
  866. if( isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' )) {
  867. $weekno = null;
  868. for( $d=31; $d > 25; $d-- ) { // get last weekno for year
  869. if( !$weekno )
  870. $weekno = $daycnts[12][$d]['weekno_up'];
  871. elseif( $weekno < $daycnts[12][$d]['weekno_up'] ) {
  872. $weekno = $daycnts[12][$d]['weekno_up'];
  873. break;
  874. }
  875. }
  876. }
  877. for( $m = 12; $m > 0; $m-- ) { // count down and update down-counters
  878. $weekdaycnt = array();
  879. foreach( $daynames as $dn )
  880. $yeardaycnt[$dn] = $weekdaycnt[$dn] = 0;
  881. $monthcnt = 0;
  882. $mcnt = date( 't', mktime( 0, 0, 0, $m, 1, $wdate['year'] ));
  883. for( $d = $mcnt; $d > 0; $d-- ) {
  884. if( isset( $recur['BYYEARDAY'] )) {
  885. $daycnt -= 1;
  886. $daycnts[$m][$d]['yearcnt_down'] = $daycnt;
  887. }
  888. if( isset( $recur['BYMONTHDAY'] )) {
  889. $monthcnt -= 1;
  890. $daycnts[$m][$d]['monthcnt_down'] = $monthcnt;
  891. }
  892. if( isset( $recur['BYDAY'] )) {
  893. $day = $daycnts[$m][$d]['DAY'];
  894. $weekdaycnt[$day] -= 1;
  895. $daycnts[$m][$d]['monthdayno_down'] = $weekdaycnt[$day];
  896. $yeardaycnt[$day] -= 1;
  897. $daycnts[$m][$d]['yeardayno_down'] = $yeardaycnt[$day];
  898. }
  899. if( isset( $recur['BYWEEKNO'] ) || ( $recur['FREQ'] == 'WEEKLY' ))
  900. $daycnts[$m][$d]['weekno_down'] = ($daycnts[$m][$d]['weekno_up'] - $weekno - 1);
  901. }
  902. }
  903. }
  904. /* check interval */
  905. if( 1 < $recur['INTERVAL'] ) {
  906. /* create interval index */
  907. $intervalix = iCalUtilityFunctions::_recurIntervalIx( $recur['FREQ'], $wdate, $wkst );
  908. /* check interval */
  909. $currentKey = array_keys( $intervalarr );
  910. $currentKey = end( $currentKey ); // get last index
  911. if( $currentKey != $intervalix )
  912. $intervalarr = array( $intervalix => ( $intervalarr[$currentKey] + 1 ));
  913. if(( $recur['INTERVAL'] != $intervalarr[$intervalix] ) &&
  914. ( 0 != $intervalarr[$intervalix] )) {
  915. /* step up date */
  916. // echo "skip: ".implode('-',$wdate)." ix=$intervalix old=$currentKey interval=".$intervalarr[$intervalix]."<br />\n";//test
  917. iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
  918. continue;
  919. }
  920. else // continue within the selected interval
  921. $intervalarr[$intervalix] = 0;
  922. // echo "cont: ".implode('-',$wdate)." ix=$intervalix old=$currentKey interval=".$intervalarr[$intervalix]."<br />\n";//test
  923. }
  924. $updateOK = TRUE;
  925. if( $updateOK && isset( $recur['BYMONTH'] ))
  926. $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYMONTH']
  927. , $wdate['month']
  928. ,($wdate['month'] - 13));
  929. if( $updateOK && isset( $recur['BYWEEKNO'] ))
  930. $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYWEEKNO']
  931. , $daycnts[$wdate['month']][$wdate['day']]['weekno_up']
  932. , $daycnts[$wdate['month']][$wdate['day']]['weekno_down'] );
  933. if( $updateOK && isset( $recur['BYYEARDAY'] ))
  934. $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYYEARDAY']
  935. , $daycnts[$wdate['month']][$wdate['day']]['yearcnt_up']
  936. , $daycnts[$wdate['month']][$wdate['day']]['yearcnt_down'] );
  937. if( $updateOK && isset( $recur['BYMONTHDAY'] ))
  938. $updateOK = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYMONTHDAY']
  939. , $wdate['day']
  940. , $daycnts[$wdate['month']][$wdate['day']]['monthcnt_down'] );
  941. // echo "efter BYMONTHDAY: ".implode('-',$wdate).' status: '; echo ($updateOK) ? 'TRUE' : 'FALSE'; echo "<br />\n";//test###
  942. if( $updateOK && isset( $recur['BYDAY'] )) {
  943. $updateOK = FALSE;
  944. $m = $wdate['month'];
  945. $d = $wdate['day'];
  946. if( isset( $recur['BYDAY']['DAY'] )) { // single day, opt with year/month day order no
  947. $daynoexists = $daynosw = $daynamesw = FALSE;
  948. if( $recur['BYDAY']['DAY'] == $daycnts[$m][$d]['DAY'] )
  949. $daynamesw = TRUE;
  950. if( isset( $recur['BYDAY'][0] )) {
  951. $daynoexists = TRUE;
  952. if(( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'MONTHLY' )) || isset( $recur['BYMONTH'] ))
  953. $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYDAY'][0]
  954. , $daycnts[$m][$d]['monthdayno_up']
  955. , $daycnts[$m][$d]['monthdayno_down'] );
  956. elseif( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'YEARLY' ))
  957. $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $recur['BYDAY'][0]
  958. , $daycnts[$m][$d]['yeardayno_up']
  959. , $daycnts[$m][$d]['yeardayno_down'] );
  960. }
  961. if(( $daynoexists && $daynosw && $daynamesw ) ||
  962. ( !$daynoexists && !$daynosw && $daynamesw )) {
  963. $updateOK = TRUE;
  964. // echo "m=$m d=$d day=".$daycnts[$m][$d]['DAY']." yeardayno_up=".$daycnts[$m][$d]['yeardayno_up']." daynoexists:$daynoexists daynosw:$daynosw daynamesw:$daynamesw updateOK:$updateOK<br />\n"; // test ###
  965. }
  966. //echo "m=$m d=$d day=".$daycnts[$m][$d]['DAY']." yeardayno_up=".$daycnts[$m][$d]['yeardayno_up']." daynoexists:$daynoexists daynosw:$daynosw daynamesw:$daynamesw updateOK:$updateOK<br />\n"; // test ###
  967. }
  968. else {
  969. foreach( $recur['BYDAY'] as $bydayvalue ) {
  970. $daynoexists = $daynosw = $daynamesw = FALSE;
  971. if( isset( $bydayvalue['DAY'] ) &&
  972. ( $bydayvalue['DAY'] == $daycnts[$m][$d]['DAY'] ))
  973. $daynamesw = TRUE;
  974. if( isset( $bydayvalue[0] )) {
  975. $daynoexists = TRUE;
  976. if(( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'MONTHLY' )) ||
  977. isset( $recur['BYMONTH'] ))
  978. $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $bydayvalue['0']
  979. , $daycnts[$m][$d]['monthdayno_up']
  980. , $daycnts[$m][$d]['monthdayno_down'] );
  981. elseif( isset( $recur['FREQ'] ) && ( $recur['FREQ'] == 'YEARLY' ))
  982. $daynosw = iCalUtilityFunctions::_recurBYcntcheck( $bydayvalue['0']
  983. , $daycnts[$m][$d]['yeardayno_up']
  984. , $daycnts[$m][$d]['yeardayno_down'] );
  985. }
  986. // echo "daynoexists:$daynoexists daynosw:$daynosw daynamesw:$daynamesw<br />\n"; // test ###
  987. if(( $daynoexists && $daynosw && $daynamesw ) ||
  988. ( !$daynoexists && !$daynosw && $daynamesw )) {
  989. $updateOK = TRUE;
  990. break;
  991. }
  992. }
  993. }
  994. }
  995. // echo "efter BYDAY: ".implode('-',$wdate).' status: '; echo ($updateOK) ? 'TRUE' : 'FALSE'; echo "<br />\n"; // test ###
  996. /* check BYSETPOS */
  997. if( $updateOK ) {
  998. if( isset( $recur['BYSETPOS'] ) &&
  999. ( in_array( $recur['FREQ'], array( 'YEARLY', 'MONTHLY', 'WEEKLY', 'DAILY' )))) {
  1000. if( isset( $recur['WEEKLY'] )) {
  1001. if( $bysetposWold == $daycnts[$wdate['month']][$wdate['day']]['weekno_up'] )
  1002. $bysetposw1[] = $wdatets;
  1003. else
  1004. $bysetposw2[] = $wdatets;
  1005. }
  1006. else {
  1007. if(( isset( $recur['FREQ'] ) && ( 'YEARLY' == $recur['FREQ'] ) &&
  1008. ( $bysetposYold == $wdate['year'] )) ||
  1009. ( isset( $recur['FREQ'] ) && ( 'MONTHLY' == $recur['FREQ'] ) &&
  1010. (( $bysetposYold == $wdate['year'] ) &&
  1011. ( $bysetposMold == $wdate['month'] ))) ||
  1012. ( isset( $recur['FREQ'] ) && ( 'DAILY' == $recur['FREQ'] ) &&
  1013. (( $bysetposYold == $wdate['year'] ) &&
  1014. ( $bysetposMold == $wdate['month']) &&
  1015. ( $bysetposDold == $wdate['day'] )))) {
  1016. // echo "bysetposymd1[]=".date('Y-m-d H:i:s',$wdatets)."<br />\n";//test
  1017. $bysetposymd1[] = $wdatets;
  1018. }
  1019. else {
  1020. // echo "bysetposymd2[]=".date('Y-m-d H:i:s',$wdatets)."<br />\n";//test
  1021. $bysetposymd2[] = $wdatets;
  1022. }
  1023. }
  1024. }
  1025. else {
  1026. /* update result array if BYSETPOS is set */
  1027. $countcnt++;
  1028. if( $startdatets <= $wdatets ) { // only output within period
  1029. $result[$wdatets] = TRUE;
  1030. // echo "recur ".date('Y-m-d H:i:s',$wdatets)."<br />\n";//test
  1031. }
  1032. // echo "recur undate ".date('Y-m-d H:i:s',$wdatets)." okdatstart ".date('Y-m-d H:i:s',$startdatets)."<br />\n";//test
  1033. $updateOK = FALSE;
  1034. }
  1035. }
  1036. /* step up date */
  1037. iCalUtilityFunctions::_stepdate( $wdate, $wdatets, $step);
  1038. /* check if BYSETPOS is set for updating result array */
  1039. if( $updateOK && isset( $recur['BYSETPOS'] )) {
  1040. $bysetpos = FALSE;
  1041. if( isset( $recur['FREQ'] ) && ( 'YEARLY' == $recur['FREQ'] ) &&
  1042. ( $bysetposYold != $wdate['year'] )) {
  1043. $bysetpos = TRUE;
  1044. $bysetposYold = $wdate['year'];
  1045. }
  1046. elseif( isset( $recur['FREQ'] ) && ( 'MONTHLY' == $recur['FREQ'] &&
  1047. (( $bysetposYold != $wdate['year'] ) || ( $bysetposMold != $wdate['month'] )))) {
  1048. $bysetpos = TRUE;
  1049. $bysetposYold = $wdate['year'];
  1050. $bysetposMold = $wdate['month'];
  1051. }
  1052. elseif( isset( $recur['FREQ'] ) && ( 'WEEKLY' == $recur['FREQ'] )) {
  1053. $weekno = (int) date( 'W', mktime( 0, 0, $wkst, $wdate['month'], $wdate['day'], $wdate['year']));
  1054. if( $bysetposWold != $weekno ) {
  1055. $bysetposWold = $weekno;
  1056. $bysetpos = TRUE;
  1057. }
  1058. }
  1059. elseif( isset( $recur['FREQ'] ) && ( 'DAILY' == $recur['FREQ'] ) &&
  1060. (( $bysetposYold != $wdate['year'] ) ||
  1061. ( $bysetposMold != $wdate['month'] ) ||
  1062. ( $bysetposDold != $wdate['day'] ))) {
  1063. $bysetpos = TRUE;
  1064. $bysetposYold = $wdate['year'];
  1065. $bysetposMold = $wdate['month'];
  1066. $bysetposDold = $wdate['day'];
  1067. }
  1068. if( $bysetpos ) {
  1069. if( isset( $recur['BYWEEKNO'] )) {
  1070. $bysetposarr1 = & $bysetposw1;
  1071. $bysetposarr2 = & $bysetposw2;
  1072. }
  1073. else {
  1074. $bysetposarr1 = & $bysetposymd1;
  1075. $bysetposarr2 = & $bysetposymd2;
  1076. }
  1077. // echo 'test före out startYMD (weekno)='.$wdateStart['year'].':'.$wdateStart['month'].':'.$wdateStart['day']." ($weekStart) "; // test ###
  1078. foreach( $recur['BYSETPOS'] as $ix ) {
  1079. if( 0 > $ix ) // both positive and negative BYSETPOS allowed
  1080. $ix = ( count( $bysetposarr1 ) + $ix + 1);
  1081. $ix--;
  1082. if( isset( $bysetposarr1[$ix] )) {
  1083. if( $startdatets <= $bysetposarr1[$ix] ) { // only output within period
  1084. // $testdate = iCalUtilityFunctions::_timestamp2date( $bysetposarr1[$ix], 6 ); // test ###
  1085. // $testweekno = (int) date( 'W', mktime( 0, 0, $wkst, $testdate['month'], $testdate['day'], $testdate['year'] )); // test ###
  1086. // echo " testYMD (weekno)=".$testdate['year'].':'.$testdate['month'].':'.$testdate['day']." ($testweekno)"; // test ###
  1087. $result[$bysetposarr1[$ix]] = TRUE;
  1088. // echo " recur ".date('Y-m-d H:i:s',$bysetposarr1[$ix]); // test ###
  1089. }
  1090. $countcnt++;
  1091. }
  1092. if( isset( $recur['COUNT'] ) && ( $countcnt >= $recur['COUNT'] ))
  1093. break;
  1094. }
  1095. // echo "<br />\n"; // test ###
  1096. $bysetposarr1 = $bysetposarr2;
  1097. $bysetposarr2 = array();
  1098. }
  1099. }
  1100. }
  1101. }
  1102. public static function _recurBYcntcheck( $BYvalue, $upValue, $downValue ) {
  1103. if( is_array( $BYvalue ) &&
  1104. ( in_array( $upValue, $BYvalue ) || in_array( $downValue, $BYvalue )))
  1105. return TRUE;
  1106. elseif(( $BYvalue == $upValue ) || ( $BYvalue == $downValue ))
  1107. return TRUE;
  1108. else
  1109. return FALSE;
  1110. }
  1111. public static function _recurIntervalIx( $freq, $date, $wkst ) {
  1112. /* create interval index */
  1113. switch( $freq ) {
  1114. case 'YEARLY':
  1115. $intervalix = $date['year'];
  1116. break;
  1117. case 'MONTHLY':
  1118. $intervalix = $date['year'].'-'.$date['month'];
  1119. break;
  1120. case 'WEEKLY':
  1121. $wdatets = iCalUtilityFunctions::_date2timestamp( $date );
  1122. $intervalix = (int) date( 'W', ( $wdatets + $wkst ));
  1123. break;
  1124. case 'DAILY':
  1125. default:
  1126. $intervalix = $date['year'].'-'.$date['month'].'-'.$date['day'];
  1127. break;
  1128. }
  1129. return $intervalix;
  1130. }
  1131. /**
  1132. * convert input format for exrule and rrule to internal format
  1133. *
  1134. * @author Kjell-Inge Gustafsson, kigkonsult <ical@kigkonsult.se>
  1135. * @since 2.9.10 - 2011-07-07
  1136. * @param array $rexrule
  1137. * @return array
  1138. */
  1139. public static function _setRexrule( $rexrule ) {
  1140. $input = array();
  1141. if( empty( $rexrule ))
  1142. return $input;
  1143. foreach( $rexrule as $rexrulelabel => $rexrulevalue ) {
  1144. $rexrulelabel = strtoupper( $rexrulelabel );
  1145. if( 'UNTIL' != $rexrulelabel )
  1146. $input[$rexrulelabel] = $rexrulevalue;
  1147. else {
  1148. if( iCalUtilityFunctions::_isArrayTimestampDate( $rexrulevalue )) // timestamp date
  1149. $input[$rexrulelabel] = iCalUtilityFunctions::_timestamp2date( $rexrulevalue, 6 );
  1150. elseif( iCalUtilityFunctions::_isArrayDate( $rexrulevalue )) // date-time
  1151. $input[$rexrulelabel] = iCalUtilityFunctions::_date_time_array( $rexrulevalue, 6 );
  1152. elseif( 8 <= strlen( trim( $rexrulevalue ))) // ex. 2006-08-03 10:12:18
  1153. $input[$rexrulelabel] = iCalUtilityFunctions::_date_time_string( $rexrulevalue );
  1154. if(( 3 < count( $input[$rexrulelabel] )) && !isset( $input[$rexrulelabel]['tz'] ))
  1155. $input[$rexrulelabel]['tz'] = 'Z';
  1156. }
  1157. }
  1158. /* set recurrence rule specification in rfc2445 order */
  1159. $input2 = array();
  1160. if( isset( $input['FREQ'] ))
  1161. $input2['FREQ'] = $input['FREQ'];
  1162. if( isset( $input['UNTIL'] ))
  1163. $input2['UNTIL'] = $input['UNTIL'];
  1164. elseif( isset( $input['COUNT'] ))
  1165. $input2['COUNT'] = $input['COUNT'];
  1166. if( isset( $input['INTERVAL'] ))
  1167. $input2['INTERVAL'] = $input['INTERVAL'];
  1168. if( isset( $input['BYSECOND'] ))
  1169. $input2['BYSECOND'] = $input['BYSECOND'];
  1170. if( isset( $input['BYMINUTE'] ))
  1171. $in