/**
  * parse component unparsed data into properties
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.20.3 - 2015-03-05
  * @param mixed $unparsedtext   strict rfc2445 formatted, single property string or array of strings
  * @uses calendarComponent::getConfig()
  * @uses iCalUtilityFunctions::convEolChar()
  * @uses calendarComponent::$unparsed
  * @uses calendarComponent::$components
  * @uses calendarComponent::copy()
  * @uses iCalUtilityFunctions::_splitContent()
  * @uses calendarComponent::setProperty()
  * @uses iCalUtilityFunctions::_strunrep()
  * @uses calendarComponent::parse()
  * @return bool FALSE if error occurs during parsing
  */
 function parse($unparsedtext = null)
 {
     $nl = $this->getConfig('nl');
     if (!empty($unparsedtext)) {
         if (is_array($unparsedtext)) {
             $unparsedtext = implode('\\n' . $nl, $unparsedtext);
         }
         $unparsedtext = iCalUtilityFunctions::convEolChar($unparsedtext, $nl);
     } elseif (!isset($this->unparsed)) {
         $unparsedtext = array();
     } else {
         $unparsedtext = $this->unparsed;
     }
     /* skip leading (empty/invalid) lines */
     foreach ($unparsedtext as $lix => $line) {
         if (FALSE !== ($pos = stripos($line, 'BEGIN:'))) {
             $unparsedtext[$lix] = substr($unparsedtext[$lix], $pos);
             break;
         }
         $tst = trim($line);
         if ('\\n' == $tst || empty($tst)) {
             unset($unparsedtext[$lix]);
         }
     }
     $this->unparsed = array();
     $comp =& $this;
     $config = $this->getConfig();
     $compsync = $subsync = 0;
     foreach ($unparsedtext as $lix => $line) {
         if ('END:VALARM' == strtoupper(substr($line, 0, 10))) {
             if (1 != $subsync) {
                 return FALSE;
             }
             $this->components[] = $comp->copy();
             $subsync--;
         } elseif ('END:DAYLIGHT' == strtoupper(substr($line, 0, 12))) {
             if (1 != $subsync) {
                 return FALSE;
             }
             $this->components[] = $comp->copy();
             $subsync--;
         } elseif ('END:STANDARD' == strtoupper(substr($line, 0, 12))) {
             if (1 != $subsync) {
                 return FALSE;
             }
             array_unshift($this->components, $comp->copy());
             $subsync--;
         } elseif ('END:' == strtoupper(substr($line, 0, 4))) {
             // end:<component>
             if (1 != $compsync) {
                 return FALSE;
             }
             if (0 < $subsync) {
                 $this->components[] = $comp->copy();
             }
             $compsync--;
             break;
             /* skip trailing empty lines */
         } elseif ('BEGIN:VALARM' == strtoupper(substr($line, 0, 12))) {
             $comp = new valarm($config);
             $subsync++;
         } elseif ('BEGIN:STANDARD' == strtoupper(substr($line, 0, 14))) {
             $comp = new vtimezone('standard', $config);
             $subsync++;
         } elseif ('BEGIN:DAYLIGHT' == strtoupper(substr($line, 0, 14))) {
             $comp = new vtimezone('daylight', $config);
             $subsync++;
         } elseif ('BEGIN:' == strtoupper(substr($line, 0, 6))) {
             // begin:<component>
             $compsync++;
         } else {
             $comp->unparsed[] = $line;
         }
     }
     if (0 < $subsync) {
         $this->components[] = $comp->copy();
     }
     unset($config);
     /* concatenate property values spread over several lines */
     $lastix = -1;
     $propnames = array('action', 'attach', 'attendee', 'categories', 'comment', 'completed', 'contact', 'class', 'created', 'description', 'dtend', 'dtstart', 'dtstamp', 'due', 'duration', 'exdate', 'exrule', 'freebusy', 'geo', 'last-modified', 'location', 'organizer', 'percent-complete', 'priority', 'rdate', 'recurrence-id', 'related-to', 'repeat', 'request-status', 'resources', 'rrule', 'sequence', 'status', 'summary', 'transp', 'trigger', 'tzid', 'tzname', 'tzoffsetfrom', 'tzoffsetto', 'tzurl', 'uid', 'url', 'x-');
     $proprows = array();
     for ($i = 0; $i < count($this->unparsed); $i++) {
         // concatenate lines
         $line = rtrim($this->unparsed[$i], $nl);
         while (isset($this->unparsed[$i + 1]) && !empty($this->unparsed[$i + 1]) && ' ' == $this->unparsed[$i + 1][0]) {
             $line .= rtrim(substr($this->unparsed[++$i], 1), $nl);
         }
         $proprows[] = $line;
     }
     /* parse each property 'line' */
     foreach ($proprows as $line) {
         if ('\\n' == substr($line, -2)) {
             $line = substr($line, 0, -2);
         }
         /* get propname */
         $propname = null;
         $cix = 0;
         while (isset($line[$cix])) {
             if (in_array($line[$cix], array(':', ';'))) {
                 break;
             } else {
                 $propname .= $line[$cix];
             }
             $cix++;
         }
         if ('x-' == substr($propname, 0, 2) || 'X-' == substr($propname, 0, 2)) {
             $propname2 = $propname;
             $propname = 'X-';
         }
         if (!in_array(strtolower($propname), $propnames)) {
             // skip non standard property names
             continue;
         }
         /* rest of the line is opt.params and value */
         $line = substr($line, $cix);
         /* separate attributes from value */
         iCalUtilityFunctions::_splitContent($line, $propAttr);
         /* call setProperty( $propname.. . */
         switch (strtoupper($propname)) {
             case 'ATTENDEE':
                 foreach ($propAttr as $pix => $attr) {
                     if (!in_array(strtoupper($pix), array('MEMBER', 'DELEGATED-TO', 'DELEGATED-FROM'))) {
                         continue;
                     }
                     $attr2 = explode(',', $attr);
                     if (1 < count($attr2)) {
                         $propAttr[$pix] = $attr2;
                     }
                 }
                 $this->setProperty($propname, $line, $propAttr);
                 break;
             case 'CATEGORIES':
             case 'RESOURCES':
                 if (FALSE !== strpos($line, ',')) {
                     $content = array(0 => '');
                     $cix = $lix = 0;
                     while (FALSE !== substr($line, $lix, 1)) {
                         if (',' == $line[$lix] && "\\" != $line[$lix - 1]) {
                             $cix++;
                             $content[$cix] = '';
                         } else {
                             $content[$cix] .= $line[$lix];
                         }
                         $lix++;
                     }
                     if (1 < count($content)) {
                         $content = array_values($content);
                         foreach ($content as $cix => $contentPart) {
                             $content[$cix] = iCalUtilityFunctions::_strunrep($contentPart);
                         }
                         $this->setProperty($propname, $content, $propAttr);
                         break;
                     } else {
                         $line = reset($content);
                     }
                 }
             case 'COMMENT':
             case 'CONTACT':
             case 'DESCRIPTION':
             case 'LOCATION':
             case 'SUMMARY':
                 if (empty($line)) {
                     $propAttr = null;
                 }
                 $this->setProperty($propname, iCalUtilityFunctions::_strunrep($line), $propAttr);
                 break;
             case 'REQUEST-STATUS':
                 $values = explode(';', $line, 3);
                 $values[1] = !isset($values[1]) ? null : iCalUtilityFunctions::_strunrep($values[1]);
                 $values[2] = !isset($values[2]) ? null : iCalUtilityFunctions::_strunrep($values[2]);
                 $this->setProperty($propname, $values[0], $values[1], $values[2], $propAttr);
                 break;
             case 'FREEBUSY':
                 $fbtype = isset($propAttr['FBTYPE']) ? $propAttr['FBTYPE'] : '';
                 // force setting default, if missing
                 unset($propAttr['FBTYPE']);
                 $values = explode(',', $line);
                 foreach ($values as $vix => $value) {
                     $value2 = explode('/', $value);
                     if (1 < count($value2)) {
                         $values[$vix] = $value2;
                     }
                 }
                 $this->setProperty($propname, $fbtype, $values, $propAttr);
                 break;
             case 'GEO':
                 $value = explode(';', $line, 2);
                 if (2 > count($value)) {
                     $value[1] = null;
                 }
                 $this->setProperty($propname, $value[0], $value[1], $propAttr);
                 break;
             case 'EXDATE':
                 $values = !empty($line) ? explode(',', $line) : null;
                 $this->setProperty($propname, $values, $propAttr);
                 break;
             case 'RDATE':
                 if (empty($line)) {
                     $this->setProperty($propname, $line, $propAttr);
                     break;
                 }
                 $values = explode(',', $line);
                 foreach ($values as $vix => $value) {
                     $value2 = explode('/', $value);
                     if (1 < count($value2)) {
                         $values[$vix] = $value2;
                     }
                 }
                 $this->setProperty($propname, $values, $propAttr);
                 break;
             case 'EXRULE':
             case 'RRULE':
                 $values = explode(';', $line);
                 $recur = array();
                 foreach ($values as $value2) {
                     if (empty($value2)) {
                         continue;
                     }
                     // ;-char in ending position ???
                     $value3 = explode('=', $value2, 2);
                     $rulelabel = strtoupper($value3[0]);
                     switch ($rulelabel) {
                         case 'BYDAY':
                             $value4 = explode(',', $value3[1]);
                             if (1 < count($value4)) {
                                 foreach ($value4 as $v5ix => $value5) {
                                     $value6 = array();
                                     $dayno = $dayname = null;
                                     $value5 = trim((string) $value5);
                                     if (ctype_alpha(substr($value5, -1)) && ctype_alpha(substr($value5, -2, 1))) {
                                         $dayname = substr($value5, -2, 2);
                                         if (2 < strlen($value5)) {
                                             $dayno = substr($value5, 0, strlen($value5) - 2);
                                         }
                                     }
                                     if ($dayno) {
                                         $value6[] = $dayno;
                                     }
                                     if ($dayname) {
                                         $value6['DAY'] = $dayname;
                                     }
                                     $value4[$v5ix] = $value6;
                                 }
                             } else {
                                 $value4 = array();
                                 $dayno = $dayname = null;
                                 $value5 = trim((string) $value3[1]);
                                 if (ctype_alpha(substr($value5, -1)) && ctype_alpha(substr($value5, -2, 1))) {
                                     $dayname = substr($value5, -2, 2);
                                     if (2 < strlen($value5)) {
                                         $dayno = substr($value5, 0, strlen($value5) - 2);
                                     }
                                 }
                                 if ($dayno) {
                                     $value4[] = $dayno;
                                 }
                                 if ($dayname) {
                                     $value4['DAY'] = $dayname;
                                 }
                             }
                             $recur[$rulelabel] = $value4;
                             break;
                         default:
                             $value4 = explode(',', $value3[1]);
                             if (1 < count($value4)) {
                                 $value3[1] = $value4;
                             }
                             $recur[$rulelabel] = $value3[1];
                             break;
                     }
                     // end - switch $rulelabel
                 }
                 // end - foreach( $values.. .
                 $this->setProperty($propname, $recur, $propAttr);
                 break;
             case 'X-':
                 $propname = isset($propname2) ? $propname2 : $propname;
                 unset($propname2);
             case 'ACTION':
             case 'CLASSIFICATION':
             case 'STATUS':
             case 'TRANSP':
             case 'UID':
             case 'TZID':
             case 'RELATED-TO':
             case 'TZNAME':
                 $line = iCalUtilityFunctions::_strunrep($line);
             default:
                 $this->setProperty($propname, $line, $propAttr);
                 break;
         }
         // end  switch( $propname.. .
     }
     // end - foreach( $proprows.. .
     unset($unparsedtext, $this->unparsed, $proprows);
     if (isset($this->components) && is_array($this->components) && 0 < count($this->components)) {
         $ckeys = array_keys($this->components);
         foreach ($ckeys as $ckey) {
             if (!empty($this->components[$ckey]) && !empty($this->components[$ckey]->unparsed)) {
                 $this->components[$ckey]->parse();
             }
         }
     }
     return TRUE;
 }
 /**
  * parse iCal text/file into vcalendar, components, properties and parameters
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.21.12 - 2014-03-10
  * @param mixed    $unparsedtext  strict rfc2445 formatted, single property string or array of property strings
  * @param resource $context       PHP resource context
  * @uses iCalUtilityFunctions::convEolChar()
  * @uses vcalendar::getConfig()
  * @uses vcalendar::$components
  * @uses calendarComponent::copy()
  * @uses vevent::construct()
  * @uses vfreebusy::construct()
  * @uses vjournal::construct()
  * @uses vtodo::construct()
  * @uses vtimezone::construct()
  * @uses vcalendar::$unparsed
  * @uses iCalUtilityFunctions::_splitContent()
  * @uses iCalUtilityFunctions::_strunrep()
  * @uses vcalendar::setProperty()
  * @uses calendarComponent::$unparsed
  * @uses calendarComponent::parse()
  * @return bool FALSE if error occurs during parsing
  */
 function parse($unparsedtext = FALSE, $context = null)
 {
     $nl = $this->getConfig('nl');
     if (FALSE === $unparsedtext || empty($unparsedtext)) {
         /* directory+filename is set previously via setConfig url or directory+filename  */
         if (FALSE === ($file = $this->getConfig('url'))) {
             if (FALSE === ($file = $this->getConfig('dirfile'))) {
                 return FALSE;
             }
             /* err 1 */
             if (!is_file($file)) {
                 return FALSE;
             }
             /* err 2 */
             if (!is_readable($file)) {
                 return FALSE;
             }
             /* err 3 */
         }
         /* READ FILE */
         if (!empty($context) && filter_var($file, FILTER_VALIDATE_URL) && FALSE === ($rows = file_get_contents($file, FALSE, $context))) {
             return FALSE;
         } elseif (FALSE === ($rows = file_get_contents($file))) {
             return FALSE;
         }
         /* err 5 */
     } elseif (is_array($unparsedtext)) {
         $rows = implode('\\n' . $nl, $unparsedtext);
     } else {
         $rows =& $unparsedtext;
     }
     /* fix line folding */
     $rows = iCalUtilityFunctions::convEolChar($rows, $nl);
     /* skip leading (empty/invalid) lines (and remove leading BOM chars etc) */
     foreach ($rows as $lix => $line) {
         if (FALSE !== stripos($line, 'BEGIN:VCALENDAR')) {
             $rows[$lix] = 'BEGIN:VCALENDAR';
             break;
         }
         unset($rows[$lix]);
     }
     $rcnt = count($rows);
     if (3 > $rcnt) {
         /* err 10 */
         return FALSE;
     }
     /* skip trailing empty lines and ensure an end row */
     $lix = array_keys($rows);
     $lix = end($lix);
     while (3 < $lix) {
         $tst = trim($rows[$lix]);
         if ('\\n' == $tst || empty($tst)) {
             unset($rows[$lix]);
             $lix--;
             continue;
         }
         if (FALSE === stripos($rows[$lix], 'END:VCALENDAR')) {
             $rows[] = 'END:VCALENDAR';
         } else {
             $rows[$lix] = 'END:VCALENDAR';
         }
         break;
     }
     $comp =& $this;
     $calsync = $compsync = 0;
     /* identify components and update unparsed data within component */
     $config = $this->getConfig();
     $endtxt = array('END:VE', 'END:VF', 'END:VJ', 'END:VT');
     foreach ($rows as $lix => $line) {
         if ('BEGIN:VCALENDAR' == strtoupper(substr($line, 0, 15))) {
             $calsync++;
             continue;
         } elseif ('END:VCALENDAR' == strtoupper(substr($line, 0, 13))) {
             if (0 < $compsync) {
                 $this->components[] = $comp->copy();
             }
             $compsync--;
             $calsync--;
             break;
         } elseif (1 != $calsync) {
             return FALSE;
         } elseif (in_array(strtoupper(substr($line, 0, 6)), $endtxt)) {
             $this->components[] = $comp->copy();
             $compsync--;
             continue;
         }
         if ('BEGIN:VEVENT' == strtoupper(substr($line, 0, 12))) {
             $comp = new vevent($config);
             $compsync++;
         } elseif ('BEGIN:VFREEBUSY' == strtoupper(substr($line, 0, 15))) {
             $comp = new vfreebusy($config);
             $compsync++;
         } elseif ('BEGIN:VJOURNAL' == strtoupper(substr($line, 0, 14))) {
             $comp = new vjournal($config);
             $compsync++;
         } elseif ('BEGIN:VTODO' == strtoupper(substr($line, 0, 11))) {
             $comp = new vtodo($config);
             $compsync++;
         } elseif ('BEGIN:VTIMEZONE' == strtoupper(substr($line, 0, 15))) {
             $comp = new vtimezone($config);
             $compsync++;
         } else {
             /* update component with unparsed data */
             $comp->unparsed[] = $line;
         }
     }
     // end foreach( $rows as $line )
     unset($config, $endtxt);
     /* parse data for calendar (this) object */
     if (isset($this->unparsed) && is_array($this->unparsed) && 0 < count($this->unparsed)) {
         /* concatenate property values spread over several lines */
         $propnames = array('calscale', 'method', 'prodid', 'version', 'x-');
         $proprows = array();
         for ($i = 0; $i < count($this->unparsed); $i++) {
             // concatenate lines
             $line = rtrim($this->unparsed[$i], $nl);
             while (isset($this->unparsed[$i + 1]) && !empty($this->unparsed[$i + 1]) && ' ' == $this->unparsed[$i + 1][0]) {
                 $line .= rtrim(substr($this->unparsed[++$i], 1), $nl);
             }
             $proprows[] = $line;
         }
         foreach ($proprows as $line) {
             if ('\\n' == substr($line, -2)) {
                 $line = substr($line, 0, -2);
             }
             /* get property name */
             $propname = '';
             $cix = 0;
             while (FALSE !== ($char = substr($line, $cix, 1))) {
                 if (in_array($char, array(':', ';'))) {
                     break;
                 } else {
                     $propname .= $char;
                 }
                 $cix++;
             }
             /* skip non standard property names */
             if ('x-' != strtolower(substr($propname, 0, 2)) && !in_array(strtolower($propname), $propnames)) {
                 continue;
             }
             /* ignore version/prodid properties */
             if (in_array(strtolower($propname), array('version', 'prodid'))) {
                 continue;
             }
             /* rest of the line is opt.params and value */
             $line = substr($line, $cix);
             /* separate attributes from value */
             iCalUtilityFunctions::_splitContent($line, $propAttr);
             /* update Property */
             if (FALSE !== strpos($line, ',')) {
                 $content = array(0 => '');
                 $cix = $lix = 0;
                 while (FALSE !== substr($line, $lix, 1)) {
                     if (0 < $lix && ',' == $line[$lix] && "\\" != $line[$lix - 1]) {
                         $cix++;
                         $content[$cix] = '';
                     } else {
                         $content[$cix] .= $line[$lix];
                     }
                     $lix++;
                 }
                 if (1 < count($content)) {
                     foreach ($content as $cix => $contentPart) {
                         $content[$cix] = iCalUtilityFunctions::_strunrep($contentPart);
                     }
                     $this->setProperty($propname, $content, $propAttr);
                     continue;
                 } else {
                     $line = reset($content);
                 }
                 $line = iCalUtilityFunctions::_strunrep($line);
             }
             $this->setProperty($propname, rtrim($line, ".."), $propAttr);
         }
         // end - foreach( $this->unparsed.. .
     }
     // end - if( is_array( $this->unparsed.. .
     unset($unparsedtext, $rows, $this->unparsed, $proprows);
     /* parse Components */
     if (is_array($this->components) && 0 < count($this->components)) {
         $ckeys = array_keys($this->components);
         foreach ($ckeys as $ckey) {
             if (!empty($this->components[$ckey]) && !empty($this->components[$ckey]->unparsed)) {
                 $this->components[$ckey]->parse();
             }
         }
     } else {
         return FALSE;
     }
     /* err 91 or something.. . */
     return TRUE;
 }