Skip to content
Snippets Groups Projects
iCalcreator.class.php 314 KiB
Newer Older
  • Learn to ignore specific revisions
  • 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336
         * @return bool
         */
        function setUid($value, $params = FALSE)
        {
            if (empty($value)) return FALSE; // no allowEmpty check here !!!!
            $this->uid = array('value' => $value, 'params' => $this->_setParams($params));
            return TRUE;
        }
        /*********************************************************************************/
        /**
         * Property Name: URL
         */
        /**
         * creates formatted output for calendar component property url
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-21
         * @return string
         */
        function createUrl()
        {
            if (empty($this->url)) return FALSE;
            if (empty($this->url['value']))
                return ($this->getConfig('allowEmpty')) ? $this->_createElement('URL') : FALSE;
            $attributes = $this->_createParams($this->url['params']);
            return $this->_createElement('URL', $attributes, $this->url['value']);
        }
    
        /**
         * set calendar component property url
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-11-04
         * @param string $value
         * @param string $params optional
         * @return bool
         */
        function setUrl($value, $params = FALSE)
        {
            if (empty($value)) if ($this->getConfig('allowEmpty')) $value = null; else return FALSE;
            $this->url = array('value' => $value, 'params' => $this->_setParams($params));
            return TRUE;
        }
        /*********************************************************************************/
        /**
         * Property Name: x-prop
         */
        /**
         * creates formatted output for calendar component property x-prop
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.11 - 2008-10-22
         * @return string
         */
        function createXprop()
        {
            if (empty($this->xprop)) return FALSE;
            $output = null;
            foreach ($this->xprop as $label => $xpropPart) {
                if (empty($xpropPart['value'])) {
                    if ($this->getConfig('allowEmpty')) $output .= $this->_createElement($label);
                    continue;
                }
                $attributes = $this->_createParams($xpropPart['params'], array('LANGUAGE'));
                if (is_array($xpropPart['value'])) {
                    foreach ($xpropPart['value'] as $pix => $theXpart)
                        $xpropPart['value'][$pix] = $this->_strrep($theXpart);
                    $xpropPart['value'] = implode(',', $xpropPart['value']);
                } else
                    $xpropPart['value'] = $this->_strrep($xpropPart['value']);
                $output .= $this->_createElement($label, $attributes, $xpropPart['value']);
            }
            return $output;
        }
    
        /**
         * set calendar component property x-prop
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.11 - 2008-11-04
         * @param string $label
         * @param mixed $value
         * @param array $params optional
         * @return bool
         */
        function setXprop($label, $value, $params = FALSE)
        {
            if (empty($label)) return;
            if (empty($value)) if ($this->getConfig('allowEmpty')) $value = null; else return FALSE;
            $xprop = array('value' => $value);
            $toolbox = new calendarComponent();
            $xprop['params'] = $toolbox->_setParams($params);
            if (!is_array($this->xprop)) $this->xprop = array();
            $this->xprop[strtoupper($label)] = $xprop;
            return TRUE;
        }
        /*********************************************************************************/
        /*********************************************************************************/
        /**
         * create element format parts
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.0.6 - 2006-06-20
         * @return string
         */
        function _createFormat()
        {
            $objectname = null;
            switch ($this->format) {
                case 'xcal':
                    $objectname = (isset($this->timezonetype)) ?
                        strtolower($this->timezonetype) : strtolower($this->objName);
                    $this->componentStart1 = $this->elementStart1 = '<';
                    $this->componentStart2 = $this->elementStart2 = '>';
                    $this->componentEnd1 = $this->elementEnd1 = '</';
                    $this->componentEnd2 = $this->elementEnd2 = '>' . $this->nl;
                    $this->intAttrDelimiter = '<!-- -->';
                    $this->attributeDelimiter = $this->nl;
                    $this->valueInit = null;
                    break;
                default:
                    $objectname = (isset($this->timezonetype)) ?
                        strtoupper($this->timezonetype) : strtoupper($this->objName);
                    $this->componentStart1 = 'BEGIN:';
                    $this->componentStart2 = null;
                    $this->componentEnd1 = 'END:';
                    $this->componentEnd2 = $this->nl;
                    $this->elementStart1 = null;
                    $this->elementStart2 = null;
                    $this->elementEnd1 = null;
                    $this->elementEnd2 = $this->nl;
                    $this->intAttrDelimiter = '<!-- -->';
                    $this->attributeDelimiter = ';';
                    $this->valueInit = ':';
                    break;
            }
            return $objectname;
        }
    
        /**
         * creates formatted output for calendar component property
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-23
         * @param string $label property name
         * @param string $attributes property attributes
         * @param string $content property content (optional)
         * @return string
         */
        function _createElement($label, $attributes = null, $content = FALSE)
        {
            $label = $this->_formatPropertyName($label);
            $output = $this->elementStart1 . $label;
            $categoriesAttrLang = null;
            $attachInlineBinary = FALSE;
            $attachfmttype = null;
            if (!empty($attributes)) {
                $attributes = trim($attributes);
                if ('xcal' == $this->format) {
                    $attributes2 = explode($this->intAttrDelimiter, $attributes);
                    $attributes = null;
                    foreach ($attributes2 as $attribute) {
                        $attrKVarr = explode('=', $attribute);
                        if (empty($attrKVarr[0]))
                            continue;
                        if (!isset($attrKVarr[1])) {
                            $attrValue = $attrKVarr[0];
                            $attrKey = null;
                        } elseif (2 == count($attrKVarr)) {
                            $attrKey = strtolower($attrKVarr[0]);
                            $attrValue = $attrKVarr[1];
                        } else {
                            $attrKey = strtolower($attrKVarr[0]);
                            unset($attrKVarr[0]);
                            $attrValue = implode('=', $attrKVarr);
                        }
                        if (('attach' == $label) && (in_array($attrKey, array('fmttype', 'encoding', 'value')))) {
                            $attachInlineBinary = TRUE;
                            if ('fmttype' == $attrKey)
                                $attachfmttype = $attrKey . '=' . $attrValue;
                            continue;
                        } elseif (('categories' == $label) && ('language' == $attrKey))
                            $categoriesAttrLang = $attrKey . '=' . $attrValue;
                        else {
                            $attributes .= (empty($attributes)) ? ' ' : $this->attributeDelimiter . ' ';
                            $attributes .= (!empty($attrKey)) ? $attrKey . '=' : null;
                            if (('"' == substr($attrValue, 0, 1)) && ('"' == substr($attrValue, -1))) {
                                $attrValue = substr($attrValue, 1, (strlen($attrValue) - 2));
                                $attrValue = str_replace('"', '', $attrValue);
                            }
                            $attributes .= '"' . htmlspecialchars($attrValue) . '"';
                        }
                    }
                } else {
                    $attributes = str_replace($this->intAttrDelimiter, $this->attributeDelimiter, $attributes);
                }
            }
            if (((('attach' == $label) && !$attachInlineBinary) ||
                    (in_array($label, array('tzurl', 'url')))) && ('xcal' == $this->format)
            ) {
                $pos = strrpos($content, "/");
                $docname = ($pos !== false) ? substr($content, (1 - strlen($content) + $pos)) : $content;
                $this->xcaldecl[] = array('xmldecl' => 'ENTITY'
                , 'uri' => $docname
                , 'ref' => 'SYSTEM'
                , 'external' => $content
                , 'type' => 'NDATA'
                , 'type2' => 'BINERY');
                $attributes .= (empty($attributes)) ? ' ' : $this->attributeDelimiter . ' ';
                $attributes .= 'uri="' . $docname . '"';
                $content = null;
                if ('attach' == $label) {
                    $attributes = str_replace($this->attributeDelimiter, $this->intAttrDelimiter, $attributes);
                    $content = $this->_createElement('extref', $attributes, null);
                    $attributes = null;
                }
            } elseif (('attach' == $label) && $attachInlineBinary && ('xcal' == $this->format)) {
                $content = $this->nl . $this->_createElement('b64bin', $attachfmttype, $content); // max one attribute
            }
            $output .= $attributes;
            if (!$content) {
                switch ($this->format) {
                    case 'xcal':
                        $output .= ' /';
                        $output .= $this->elementStart2;
                        return $output;
                        break;
                    default:
                        $output .= $this->elementStart2 . $this->valueInit;
                        return $this->_size75($output);
                        break;
                }
            }
            $output .= $this->elementStart2;
            $output .= $this->valueInit . $content;
            switch ($this->format) {
                case 'xcal':
                    return $output . $this->elementEnd1 . $label . $this->elementEnd2;
                    break;
                default:
                    return $this->_size75($output);
                    break;
            }
        }
    
        /**
         * creates formatted output for calendar component property parameters
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 0.9.22 - 2007-04-10
         * @param array $params optional
         * @param array $ctrKeys optional
         * @return string
         */
        function _createParams($params = array(), $ctrKeys = array())
        {
            $attrLANG = $attr1 = $attr2 = null;
            $CNattrKey = (in_array('CN', $ctrKeys)) ? TRUE : FALSE;
            $LANGattrKey = (in_array('LANGUAGE', $ctrKeys)) ? TRUE : FALSE;
            $CNattrExist = $LANGattrExist = FALSE;
            if (is_array($params)) {
                foreach ($params as $paramKey => $paramValue) {
                    if (is_int($paramKey))
                        $attr2 .= $this->intAttrDelimiter . $paramValue;
                    elseif (('LANGUAGE' == $paramKey) && $LANGattrKey) {
                        $attrLANG .= $this->intAttrDelimiter . "LANGUAGE=$paramValue";
                        $LANGattrExist = TRUE;
                    } elseif (('CN' == $paramKey) && $CNattrKey) {
                        $attr1 = $this->intAttrDelimiter . 'CN="' . $paramValue . '"';
                        $CNattrExist = TRUE;
                    } elseif (('ALTREP' == $paramKey) && in_array($paramKey, $ctrKeys))
                        $attr2 .= $this->intAttrDelimiter . 'ALTREP="' . $paramValue . '"';
                    elseif (('DIR' == $paramKey) && in_array($paramKey, $ctrKeys))
                        $attr2 .= $this->intAttrDelimiter . 'DIR="' . $paramValue . '"';
                    elseif (('SENT-BY' == $paramKey) && in_array($paramKey, $ctrKeys))
                        $attr2 .= $this->intAttrDelimiter . 'SENT-BY="MAILTO:' . $paramValue . '"';
                    else
                        $attr2 .= $this->intAttrDelimiter . "$paramKey=$paramValue";
                }
            }
            if (!$LANGattrExist) {
                $lang = $this->getConfig('language');
                if (($CNattrExist || $LANGattrKey) && $lang)
                    $attrLANG .= $this->intAttrDelimiter . 'LANGUAGE=' . $lang;
            }
            return $attrLANG . $attr1 . $attr2;
        }
    
        /**
         * check a date(-time) for an opt. timezone and if it is a DATE-TIME or DATE
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.16 - 2008-10-25
         * @param array $date , date to check
         * @param int $parno , no of date parts (i.e. year, month.. .)
         * @return array $params, property parameters
         */
        function _chkdatecfg($theDate, & $parno, & $params)
        {
            if (isset($params['TZID']))
                $parno = 6;
            elseif (isset($params['VALUE']) && ('DATE' == $params['VALUE']))
                $parno = 3;
            else {
                if (isset($params['VALUE']) && ('PERIOD' == $params['VALUE']))
                    $parno = 7;
                if (is_array($theDate)) {
                    if (isset($theDate['timestamp']))
                        $tzid = (isset($theDate['tz'])) ? $theDate['tz'] : null;
                    else
                        $tzid = (isset($theDate['tz'])) ? $theDate['tz'] : (7 == count($theDate)) ? end($theDate) : null;
                    if (!empty($tzid)) {
                        $parno = 7;
                        if (!$this->_isOffset($tzid))
                            $params['TZID'] = $tzid; // save only timezone
                    } elseif (!$parno && (3 == count($theDate)) &&
                        (isset($params['VALUE']) && ('DATE' == $params['VALUE']))
                    )
                        $parno = 3;
                    else
                        $parno = 6;
                } else { // string
                    $date = trim($theDate);
                    if ('Z' == substr($date, -1))
                        $parno = 7; // UTC DATE-TIME
                    elseif (((8 == strlen($date) && ctype_digit($date)) || (11 >= strlen($date))) &&
                        (!isset($params['VALUE']) || !in_array($params['VALUE'], array('DATE-TIME', 'PERIOD')))
                    )
                        $parno = 3; // DATE
                    $date = $this->_date_time_string($date, $parno);
                    if (!empty($date['tz'])) {
                        $parno = 7;
                        if (!$this->_isOffset($date['tz']))
                            $params['TZID'] = $date['tz']; // save only timezone
                    } elseif (empty($parno))
                        $parno = 6;
    
                if (isset($params['TZID']))
                    $parno = 6;
            }
    
    
        /**
         * convert local startdate/enddate (Ymd[His]) to duration
         *
         * uses this component dates if missing input dates
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.2.11 - 2007-11-03
         * @param array $startdate , optional
         * @param array $duration , optional
         * @return array duration
         */
        function _date2duration($startdate = FALSE, $enddate = FALSE)
        {
            if (!$startdate || !$enddate) {
                if (FALSE === ($startdate = $this->getProperty('dtstart')))
                    return null;
                if (FALSE === ($enddate = $this->getProperty('dtend')))    // vevent/vfreebusy
                    if (FALSE === ($enddate = $this->getProperty('due')))      // vtodo
                        return null;
            }
            if (!$startdate || !$enddate)
                return null;
            $startWdate = mktime(0, 0, 0, $startdate['month'], $startdate['day'], $startdate['year']);
            $endWdate = mktime(0, 0, 0, $enddate['month'], $enddate['day'], $enddate['year']);
            $wduration = $endWdate - $startWdate;
            $dur = array();
            $dur['week'] = (int)floor($wduration / (7 * 24 * 60 * 60));
            $wduration = $wduration % (7 * 24 * 60 * 60);
            $dur['day'] = (int)floor($wduration / (24 * 60 * 60));
            $wduration = $wduration % (24 * 60 * 60);
            $dur['hour'] = (int)floor($wduration / (60 * 60));
            $wduration = $wduration % (60 * 60);
            $dur['min'] = (int)floor($wduration / (60));
            $dur['sec'] = (int)$wduration % (60);
            return $dur;
    
    
        /**
         * convert date/datetime to timestamp
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-30
         * @param array $datetime datetime/(date)
         * @param string $tz timezone
         * @return timestamp
         */
        function _date2timestamp($datetime, $tz = null)
        {
            $output = null;
            if (!isset($datetime['hour'])) $datetime['hour'] = '0';
            if (!isset($datetime['min'])) $datetime['min'] = '0';
            if (!isset($datetime['sec'])) $datetime['sec'] = '0';
            foreach ($datetime as $dkey => $dvalue) {
                if ('tz' != $dkey)
                    $datetime[$dkey] = (integer)$dvalue;
            }
            if ($tz)
                $datetime['tz'] = $tz;
            $offset = (isset($datetime['tz']) && ('' < trim($datetime['tz']))) ? $this->_tz2offset($datetime['tz']) : 0;
            $output = mktime($datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
            return $output;
    
    
        /**
         * ensures internal date-time/date format for input date-time/date in array format
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 0.3.0 - 2006-08-15
         * @param array $datetime
         * @param int $parno optional, default FALSE
         * @return array
         */
        function _date_time_array($datetime, $parno = FALSE)
        {
            $output = array();
            foreach ($datetime as $dateKey => $datePart) {
                switch ($dateKey) {
                    case '0':
                    case 'year':
                        $output['year'] = $datePart;
                        break;
                    case '1':
                    case 'month':
                        $output['month'] = $datePart;
                        break;
                    case '2':
                    case 'day':
                        $output['day'] = $datePart;
                        break;
                }
                if (3 != $parno) {
                    switch ($dateKey) {
                        case '0':
                        case '1':
                        case '2':
                            break;
                        case '3':
                        case 'hour':
                            $output['hour'] = $datePart;
                            break;
                        case '4':
                        case 'min' :
                            $output['min'] = $datePart;
                            break;
                        case '5':
                        case 'sec' :
                            $output['sec'] = $datePart;
                            break;
                        case '6':
                        case 'tz'  :
                            $output['tz'] = $datePart;
                            break;
                    }
                }
            }
            if (3 != $parno) {
                if (!isset($output['hour']))
                    $output['hour'] = 0;
                if (!isset($output['min']))
                    $output['min'] = 0;
                if (!isset($output['sec']))
                    $output['sec'] = 0;
            }
            return $output;
    
    
        /**
         * ensures internal date-time/date format for input date-time/date in string fromat
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.2.10 - 2007-10-19
         * @param array $datetime
         * @param int $parno optional, default FALSE
         * @return array
         */
        function _date_time_string($datetime, $parno = FALSE)
        {
            $datetime = (string)trim($datetime);
            $tz = null;
            $len = strlen($datetime) - 1;
            if ('Z' == substr($datetime, -1)) {
                $tz = 'Z';
                $datetime = trim(substr($datetime, 0, $len));
            } elseif ((ctype_digit(substr($datetime, -2, 2))) && // time or date
                ('-' == substr($datetime, -3, 1)) ||
                (':' == substr($datetime, -3, 1)) ||
                ('.' == substr($datetime, -3, 1))
            ) {
                $continue = TRUE;
            } elseif ((ctype_digit(substr($datetime, -4, 4))) && // 4 pos offset
                (' +' == substr($datetime, -6, 2)) ||
                (' -' == substr($datetime, -6, 2))
            ) {
                $tz = substr($datetime, -5, 5);
                $datetime = substr($datetime, 0, ($len - 5));
            } elseif ((ctype_digit(substr($datetime, -6, 6))) && // 6 pos offset
                (' +' == substr($datetime, -8, 2)) ||
                (' -' == substr($datetime, -8, 2))
            ) {
                $tz = substr($datetime, -7, 7);
                $datetime = substr($datetime, 0, ($len - 7));
            } elseif ((6 < $len) && (ctype_digit(substr($datetime, -6, 6)))) {
                $continue = TRUE;
            } elseif ('T' == substr($datetime, -7, 1)) {
                $continue = TRUE;
            } else {
                $cx = $tx = 0;    //  19970415T133000 US-Eastern
                for ($cx = -1; $cx > (9 - $len); $cx--) {
                    if ((' ' == substr($datetime, $cx, 1)) || ctype_digit(substr($datetime, $cx, 1)))
                        break; // if exists, tz ends here.. . ?
                    elseif (ctype_alpha(substr($datetime, $cx, 1)) ||
                        (in_array(substr($datetime, $cx, 1), array('-', '/')))
                    )
                        $tx--; // tz length counter
    
                if (0 > $tx) {
                    $tz = substr($datetime, $tx);
                    $datetime = trim(substr($datetime, 0, $len + $tx + 1));
                }
            }
            if (0 < substr_count($datetime, '-')) {
                $datetime = str_replace('-', '/', $datetime);
            } elseif (ctype_digit(substr($datetime, 0, 8)) &&
                ('T' == substr($datetime, 8, 1)) &&
                ctype_digit(substr($datetime, 9, 6))
            ) {
                $datetime = substr($datetime, 4, 2)
                    . '/' . substr($datetime, 6, 2)
                    . '/' . substr($datetime, 0, 4)
                    . ' ' . substr($datetime, 9, 2)
                    . ':' . substr($datetime, 11, 2)
                    . ':' . substr($datetime, 13);
            }
            $datestring = date('Y-m-d H:i:s', strtotime($datetime));
            $tz = trim($tz);
            $output = array();
            $output['year'] = substr($datestring, 0, 4);
            $output['month'] = substr($datestring, 5, 2);
            $output['day'] = substr($datestring, 8, 2);
            if ((6 == $parno) || (7 == $parno)) {
                $output['hour'] = substr($datestring, 11, 2);
                $output['min'] = substr($datestring, 14, 2);
                $output['sec'] = substr($datestring, 17, 2);
                if (!empty($tz))
                    $output['tz'] = $tz;
            } elseif (3 != $parno) {
                if (('00' < substr($datestring, 11, 2)) ||
                    ('00' < substr($datestring, 14, 2)) ||
                    ('00' < substr($datestring, 17, 2))
                ) {
                    $output['hour'] = substr($datestring, 11, 2);
                    $output['min'] = substr($datestring, 14, 2);
                    $output['sec'] = substr($datestring, 17, 2);
                }
                if (!empty($tz))
                    $output['tz'] = $tz;
            }
            return $output;
        }
    
        /**
         * ensures internal duration format for input in array format
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.1.1 - 2007-06-24
         * @param array $duration
         * @return array
         */
        function _duration_array($duration)
        {
            $output = array();
            if (is_array($duration) &&
                (1 == count($duration)) &&
                isset($duration['sec']) &&
                (60 < $duration['sec'])
            ) {
                $durseconds = $duration['sec'];
                $output['week'] = floor($durseconds / (60 * 60 * 24 * 7));
                $durseconds = $durseconds % (60 * 60 * 24 * 7);
                $output['day'] = floor($durseconds / (60 * 60 * 24));
                $durseconds = $durseconds % (60 * 60 * 24);
                $output['hour'] = floor($durseconds / (60 * 60));
                $durseconds = $durseconds % (60 * 60);
                $output['min'] = floor($durseconds / (60));
                $output['sec'] = ($durseconds % (60));
            } else {
                foreach ($duration as $durKey => $durValue) {
                    if (empty($durValue)) continue;
                    switch ($durKey) {
                        case '0':
                        case 'week':
                            $output['week'] = $durValue;
                            break;
                        case '1':
                        case 'day':
                            $output['day'] = $durValue;
                            break;
                        case '2':
                        case 'hour':
                            $output['hour'] = $durValue;
                            break;
                        case '3':
                        case 'min':
                            $output['min'] = $durValue;
                            break;
                        case '4':
                        case 'sec':
                            $output['sec'] = $durValue;
                            break;
    
                }
            }
            if (isset($output['week']) && (0 < $output['week'])) {
                unset($output['day'], $output['hour'], $output['min'], $output['sec']);
                return $output;
            }
            unset($output['week']);
            if (empty($output['day']))
                unset($output['day']);
            if (isset($output['hour']) || isset($output['min']) || isset($output['sec'])) {
                if (!isset($output['hour'])) $output['hour'] = 0;
                if (!isset($output['min'])) $output['min'] = 0;
                if (!isset($output['sec'])) $output['sec'] = 0;
                if ((0 == $output['hour']) && (0 == $output['min']) && (0 == $output['sec']))
                    unset($output['hour'], $output['min'], $output['sec']);
            }
            return $output;
        }
    
        /**
         * convert duration to date in array format based on input or dtstart value
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-30
         * @param array $startdate , optional
         * @param array $duration , optional
         * @return array, date format
         */
        function duration2date($startdate = FALSE, $duration = FALSE)
        {
            if ($startdate && $duration) {
                $d1 = $startdate;
                $dur = $duration;
            } elseif (isset($this->dtstart['value']) && isset($this->duration['value'])) {
                $d1 = $this->dtstart['value'];
                $dur = $this->duration['value'];
            } else
                return null;
            $dateOnly = (isset($d1['hour']) || isset($d1['min']) || isset($d1['sec'])) ? FALSE : TRUE;
            $d1['hour'] = (isset($d1['hour'])) ? $d1['hour'] : 0;
            $d1['min'] = (isset($d1['min'])) ? $d1['min'] : 0;
            $d1['sec'] = (isset($d1['sec'])) ? $d1['sec'] : 0;
            $dtend = mktime($d1['hour'], $d1['min'], $d1['sec'], $d1['month'], $d1['day'], $d1['year']);
            if (isset($dur['week']))
                $dtend += ($dur['week'] * 7 * 24 * 60 * 60);
            if (isset($dur['day']))
                $dtend += ($dur['day'] * 24 * 60 * 60);
            if (isset($dur['hour']))
                $dtend += ($dur['hour'] * 60 * 60);
            if (isset($dur['min']))
                $dtend += ($dur['min'] * 60);
            if (isset($dur['sec']))
                $dtend += $dur['sec'];
            $dtend2 = array();
            $dtend2['year'] = date('Y', $dtend);
            $dtend2['month'] = date('m', $dtend);
            $dtend2['day'] = date('d', $dtend);
            $dtend2['hour'] = date('H', $dtend);
            $dtend2['min'] = date('i', $dtend);
            $dtend2['sec'] = date('s', $dtend);
            if (isset($d1['tz']))
                $dtend2['tz'] = $d1['tz'];
            if ($dateOnly && ((0 == $dtend2['hour']) && (0 == $dtend2['min']) && (0 == $dtend2['sec'])))
                unset($dtend2['hour'], $dtend2['min'], $dtend2['sec']);
            return $dtend2;
        }
    
        /**
         * ensures internal duration format for input in string format
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.0.5 - 2007-03-14
         * @param string $duration
         * @return array
         */
        function _duration_string($duration)
        {
            $duration = (string)trim($duration);
            while ('P' != strtoupper(substr($duration, 0, 1))) {
                if (0 < strlen($duration))
                    $duration = substr($duration, 1);
                else
                    return false; // no leading P !?!?
            }
            $duration = substr($duration, 1); // skip P
            $duration = str_replace('t', 'T', $duration);
            $duration = str_replace('T', '', $duration);
            $output = array();
            $val = null;
            for ($ix = 0; $ix < strlen($duration); $ix++) {
                switch (strtoupper($duration{$ix})) {
                    case 'W':
                        $output['week'] = $val;
                        $val = null;
                        break;
                    case 'D':
                        $output['day'] = $val;
                        $val = null;
                        break;
                    case 'H':
                        $output['hour'] = $val;
                        $val = null;
                        break;
                    case 'M':
                        $output['min'] = $val;
                        $val = null;
                        break;
                    case 'S':
                        $output['sec'] = $val;
                        $val = null;
                        break;
                    default:
                        if (!ctype_digit($duration{$ix}))
                            return false; // unknown duration controll character  !?!?
                        else
                            $val .= $duration{$ix};
                }
            }
            return $this->_duration_array($output);
        }
    
        /**
         * if not preSet, if exist, remove key with expected value from array and return hit value else return elseValue
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.16 - 2008-11-08
         * @param array $array
         * @param string $expkey , expected key
         * @param string $expval , expected value
         * @param int $hitVal optional, return value if found
         * @param int $elseVal optional, return value if not found
         * @param int $preSet optional, return value if already preset
         * @return int
         */
        function _existRem(&$array, $expkey, $expval = FALSE, $hitVal = null, $elseVal = null, $preSet = null)
        {
            if ($preSet)
                return $preSet;
            if (!is_array($array) || (0 == count($array)))
                return $elseVal;
            foreach ($array as $key => $value) {
                if (strtoupper($expkey) == strtoupper($key)) {
                    if (!$expval || (strtoupper($expval) == strtoupper($array[$key]))) {
                        unset($array[$key]);
                        return $hitVal;
    
    
        /**
         * creates formatted output for calendar component property data value type date/date-time
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-30
         * @param array $datetime
         * @param int $parno , optional, default 6
         * @return string
         */
        function _format_date_time($datetime, $parno = 6)
        {
            if (!isset($datetime['year']) &&
                !isset($datetime['month']) &&
                !isset($datetime['day']) &&
                !isset($datetime['hour']) &&
                !isset($datetime['min']) &&
                !isset($datetime['sec'])
            )
                return;
            $output = null;
            // 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"; }
            foreach ($datetime as $dkey => $dvalue) {
                if ('tz' != $dkey)
                    $datetime[$dkey] = (integer)$dvalue;
            }
            $output = date('Ymd', mktime(0, 0, 0, $datetime['month'], $datetime['day'], $datetime['year']));
            if (isset($datetime['hour']) ||
                isset($datetime['min']) ||
                isset($datetime['sec']) ||
                isset($datetime['tz'])
            ) {
                if (isset($datetime['tz']) &&
                    !isset($datetime['hour'])
                )
                    $datetime['hour'] = 0;
                if (isset($datetime['hour']) &&
                    !isset($datetime['min'])
                )
                    $datetime['min'] = 0;
                if (isset($datetime['hour']) &&
                    isset($datetime['min']) &&
                    !isset($datetime['sec'])
                )
                    $datetime['sec'] = 0;
                $date = mktime($datetime['hour'], $datetime['min'], $datetime['sec'], $datetime['month'], $datetime['day'], $datetime['year']);
                $output .= date('\THis', $date);
                if (isset($datetime['tz']) && ('' < trim($datetime['tz']))) {
                    $datetime['tz'] = trim($datetime['tz']);
                    if ('Z' == $datetime['tz'])
                        $output .= 'Z';
                    $offset = $this->_tz2offset($datetime['tz']);
                    if (0 != $offset) {
                        $date = mktime($datetime['hour'], $datetime['min'], ($datetime['sec'] + $offset), $datetime['month'], $datetime['day'], $datetime['year']);
                        $output = date('Ymd\THis\Z', $date);
                    }
                } elseif (7 == $parno)
                    $output .= 'Z';
            }
            return $output;
    
        /**
         * creates formatted output for calendar component property data value type duration
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.16 - 2008-10-10
         * @param array $duration ( week, day, hour, min, sec )
         * @return string
         */
        function _format_duration($duration)
        {
            if (!isset($duration['week']) &&
                !isset($duration['day']) &&
                !isset($duration['hour']) &&
                !isset($duration['min']) &&
                !isset($duration['sec'])
            )
                return;
            $output = 'P';
            if (isset($duration['week']) && (0 < $duration['week']))
                $output .= $duration['week'] . 'W';
            else {
                if (isset($duration['day']) && (0 < $duration['day']))
                    $output .= $duration['day'] . 'D';
                if ((isset($duration['hour']) && (0 < $duration['hour'])) ||
                    (isset($duration['min']) && (0 < $duration['min'])) ||
                    (isset($duration['sec']) && (0 < $duration['sec']))
                ) {
                    $output .= 'T';
                    $output .= (isset($duration['hour']) && (0 < $duration['hour'])) ? $duration['hour'] . 'H' : '0H';
                    $output .= (isset($duration['min']) && (0 < $duration['min'])) ? $duration['min'] . 'M' : '0M';
                    $output .= (isset($duration['sec']) && (0 < $duration['sec'])) ? $duration['sec'] . 'S' : '0S';
                }
            }
            return $output;
    
    
        /**
         * creates formatted output for calendar component property data value type recur
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.8 - 2008-10-22
         * @param array $recurlabel
         * @param array $recurdata
         * @return string
         */
        function _format_recur($recurlabel, $recurdata)
        {
            $output = null;
            foreach ($recurdata as $therule) {
                if (empty($therule['value'])) {
                    if ($this->getConfig('allowEmpty')) $output .= $this->_createElement($recurlabel);
                    continue;
                }
                $attributes = (isset($therule['params'])) ? $this->_createParams($therule['params']) : null;
                $content1 = $content2 = null;
                foreach ($therule['value'] as $rulelabel => $rulevalue) {
                    switch ($rulelabel) {
                        case 'FREQ': {
                            $content1 .= "FREQ=$rulevalue";
                            break;
                        }
                        case 'UNTIL': {
                            $content2 .= ";UNTIL=";
                            $content2 .= $this->_format_date_time($rulevalue);
                            break;
                        }
                        case 'COUNT':
                        case 'INTERVAL':
                        case 'WKST': {
                            $content2 .= ";$rulelabel=$rulevalue";
                            break;
                        }
                        case 'BYSECOND':
                        case 'BYMINUTE':
                        case 'BYHOUR':
                        case 'BYMONTHDAY':
                        case 'BYYEARDAY':
                        case 'BYWEEKNO':
                        case 'BYMONTH':
                        case 'BYSETPOS': {
                            $content2 .= ";$rulelabel=";
                            if (is_array($rulevalue)) {
                                foreach ($rulevalue as $vix => $valuePart) {
                                    $content2 .= ($vix) ? ',' : null;
                                    $content2 .= $valuePart;
                                }
                            } else
                                $content2 .= $rulevalue;
                            break;
                        }
                        case 'BYDAY': {
                            $content2 .= ";$rulelabel=";
                            $bydaycnt = 0;
                            foreach ($rulevalue as $vix => $valuePart) {
                                $content21 = $content22 = null;
                                if (is_array($valuePart)) {
                                    $content2 .= ($bydaycnt) ? ',' : null;
                                    foreach ($valuePart as $vix2 => $valuePart2) {
                                        if ('DAY' != strtoupper($vix2))
                                            $content21 .= $valuePart2;
                                        else
                                            $content22 .= $valuePart2;
                                    }
                                    $content2 .= $content21 . $content22;
                                    $bydaycnt++;
                                } else {
                                    $content2 .= ($bydaycnt) ? ',' : null;
                                    if ('DAY' != strtoupper($vix))
                                        $content21 .= $valuePart;
                                    else {
                                        $content22 .= $valuePart;
                                        $bydaycnt++;
                                    }
                                    $content2 .= $content21 . $content22;
                                }
                            }
                            break;
                        }
                        default: {
                            $content2 .= ";$rulelabel=$rulevalue";
                            break;
                        }
                    }
    
                $output .= $this->_createElement($recurlabel, $attributes, $content1 . $content2);
            }
            return $output;
        }
    
        /**
         * create property name case - lower/upper
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 0.9.7 - 2006-11-20
         * @param string $propertyName
         * @return string
         */
        function _formatPropertyName($propertyName)
        {
            switch ($this->format) {
                case 'xcal':
                    return strtolower($propertyName);
                    break;
                default:
                    return strtoupper($propertyName);
                    break;
            }
        }
    
        /**
         * checks if input array contains a date
         *
         * @author Kjell-Inge Gustafsson <ical@kigkonsult.se>
         * @since 2.4.16 - 2008-10-25
         * @param array $input
         * @return bool
         */
        function _isArrayDate($input)
        {
            if (isset($input['week']) || (!in_array(count($input), array(3, 6, 7))))
                return FALSE;
            if (7 == count($input))
                return TRUE;
            if (isset($input['year']) && isset($input['month']) && isset($input['day']))
                return checkdate((int)$input['month'], (int)$input['day'], (int)$input['year']);
            if (isset($input['day']) || isset($input['hour']) || isset($input['min']) || isset($input['sec']))
                return FALSE;
            if (in_array(0, $input))
                return FALSE;
            if ((1970 > $input[0]) || (12 < $input[1]) || (31 < $input[2]))
                return FALSE;
            if ((isset($input[0]) && isset($input[1]) && isset($input[2])) &&
                checkdate((int)$input[1], (int)$input[2], (int)$input[0])
            )
                return TRUE;
            $input = $this->_date_time_string($input[1] . '/' . $input[2] . '/' . $input[0], 3); //  m - d - Y