Exemple #1
0
 /**
  * parse iCal file into vcalendar, components, properties and parameters
  *
  * @author Kjell-Inge Gustafsson <*****@*****.**>
  * @since 2.4.10 - 2008-08-06
  * @param string $filename optional filname (incl. opt. directory/path) or URL
  * @return bool FALSE if error occurs during parsing
  *
  */
 function parse($filename = FALSE)
 {
     if (!$filename) {
         /* directory/filename previous set via setConfig directory+filename / url */
         if (FALSE === ($filename = $this->getConfig('url'))) {
             $filename = $this->getConfig('dirfile');
         }
     } elseif ('http://' == strtolower(substr($filename, 0, 7)) || 'webcal://' == strtolower(substr($filename, 0, 9))) {
         /* remote file - URL */
         $this->setConfig('URL', $filename);
         if (!($filename = $this->getConfig('url'))) {
             return FALSE;
         }
         /* err 2 */
     } else {
         /* local directory/filename */
         $parts = pathinfo($filename);
         if (!empty($parts['dirname']) && '.' != $parts['dirname']) {
             if (!$this->setConfig('directory', $parts['dirname'])) {
                 return FALSE;
             }
             /* err 3 */
         }
         if (!$this->setConfig('filename', $parts['basename'])) {
             return FALSE;
         }
         /* err 4 */
     }
     if ('http://' != substr($filename, 0, 7) && 'https://' != substr($filename, 0, 8)) {
         /* local file error tests */
         if (!is_file($filename)) {
             /* err 5 */
             return FALSE;
         }
         if (!is_readable($filename)) {
             return FALSE;
         }
         /* err 6 */
         if (!filesize($filename)) {
             return FALSE;
         }
         /* err 7 */
         clearstatcache();
     }
     /* READ FILE */
     if (FALSE === ($rows = file($filename))) {
         return FALSE;
     }
     /* err 1 */
     /* identify BEGIN:VCALENDAR, MUST be first row */
     if ('BEGIN:VCALENDAR' != strtoupper(trim($rows[0]))) {
         return FALSE;
     }
     /* err 8 */
     /* remove empty trailing lines */
     while ('' == trim($rows[count($rows) - 1])) {
         unset($rows[count($rows) - 1]);
         $rows = array_values($rows);
     }
     /* identify ending END:VCALENDAR row */
     if ('END:VCALENDAR' != strtoupper(trim($rows[count($rows) - 1]))) {
         return FALSE;
         /* err 9 */
     }
     if (3 > count($rows)) {
         return FALSE;
     }
     /* err 10 */
     $comp = $subcomp = null;
     $actcomp =& $this;
     $nl = $this->getConfig('nl');
     $calsync = 0;
     /* identify components and update unparsed data within component */
     foreach ($rows as $line) {
         if ('' == trim($line)) {
             continue;
         }
         if ($nl == substr($line, 0 - strlen($nl))) {
             $line = substr($line, 0, strlen($line) - strlen($nl)) . '\\n';
         }
         if ('BEGIN:VCALENDAR' == strtoupper(substr($line, 0, 15))) {
             $calsync++;
             continue;
         } elseif ('END:VCALENDAR' == strtoupper(substr($line, 0, 13))) {
             $calsync--;
             continue;
         } elseif (1 != $calsync) {
             return FALSE;
         }
         /* err 20 */
         if ('END:' == strtoupper(substr($line, 0, 4))) {
             if (null != $subcomp) {
                 $comp->setComponent($subcomp);
                 $subcomp = null;
             } else {
                 $this->setComponent($comp);
                 $comp = null;
             }
             $actcomp = null;
             continue;
         } elseif ('BEGIN:' == strtoupper(substr($line, 0, 6))) {
             $line = str_replace('\\n', '', $line);
             $compname = trim(strtoupper(substr($line, 6)));
             if (null != $comp) {
                 if ('VALARM' == $compname) {
                     $subcomp = new valarm();
                 } elseif ('STANDARD' == $compname) {
                     $subcomp = new vtimezone('STANDARD');
                 } elseif ('DAYLIGHT' == $compname) {
                     $subcomp = new vtimezone('DAYLIGHT');
                 } else {
                     return FALSE;
                 }
                 /* err 6 */
                 $actcomp =& $subcomp;
             } else {
                 switch ($compname) {
                     case 'VALARM':
                         $comp = new valarm();
                         break;
                     case 'VEVENT':
                         $comp = new vevent();
                         break;
                     case 'VFREEBUSY':
                         $comp = new vfreebusy();
                         break;
                     case 'VJOURNAL':
                         $comp = new vjournal();
                         break;
                     case 'VTODO':
                         $comp = new vtodo();
                         break;
                     case 'VTIMEZONE':
                         $comp = new vtimezone();
                         break;
                     default:
                         return FALSE;
                         // err 7
                         break;
                 }
                 // end - switch
                 $actcomp =& $comp;
             }
             continue;
         }
         // end - elsif ( 'BEGIN:'.. .
         /* update selected component with unparsed data */
         $actcomp->unparsed[] = $line;
     }
     // end - foreach( rows.. .
     /* parse data for calendar (this) object */
     if (is_array($this->unparsed) && 0 < count($this->unparsed)) {
         /* concatenate property values spread over several lines */
         $lastix = -1;
         $propnames = array('calscale', 'method', 'prodid', 'version', 'x-');
         $proprows = array();
         foreach ($this->unparsed as $line) {
             $newProp = FALSE;
             foreach ($propnames as $propname) {
                 if ($propname == strtolower(substr($line, 0, strlen($propname)))) {
                     $newProp = TRUE;
                     break;
                 }
             }
             if ($newProp) {
                 $newProp = FALSE;
                 $lastix++;
                 $proprows[$lastix] = $line;
             } else {
                 /* remove line breaks */
                 if ('\\n' == substr($proprows[$lastix], -2) && ' ' == substr($line, 0, 1)) {
                     $proprows[$lastix] = substr($proprows[$lastix], 0, strlen($proprows[$lastix]) - 2);
                     $line = substr($line, 1);
                 }
                 $proprows[$lastix] .= $line;
             }
         }
         $toolbox = new calendarComponent();
         foreach ($proprows as $line) {
             if ('\\n' == substr($line, -2)) {
                 $line = substr($line, 0, strlen($line) - 2);
             }
             /* get propname */
             $cix = $propname = null;
             for ($cix = 0; $cix < strlen($line); $cix++) {
                 if (in_array($line[$cix], array(':', ';'))) {
                     break;
                 } else {
                     $propname .= $line[$cix];
                 }
             }
             /* ignore version/prodid properties */
             if (in_array(strtoupper($propname), array('VERSION', 'PRODID'))) {
                 continue;
             }
             $line = substr($line, $cix);
             /* separate attributes from value */
             $attr = array();
             $attrix = -1;
             $strlen = strlen($line);
             for ($cix = 0; $cix < $strlen; $cix++) {
                 if (':' == $line[$cix] && '://' != substr($line, $cix, 3) && 'mailto:' != strtolower(substr($line, $cix - 6, 7))) {
                     $attrEnd = TRUE;
                     if ($cix < $strlen - 4 && ctype_digit(substr($line, $cix + 1, 4))) {
                         // an URI with a (4pos) portnr??
                         for ($c2ix = $cix; 3 < $c2ix; $c2ix--) {
                             if ('://' == substr($line, $c2ix - 2, 3)) {
                                 $attrEnd = FALSE;
                                 break;
                                 // an URI with a portnr!!
                             }
                         }
                     }
                     if ($attrEnd) {
                         $line = substr($line, $cix + 1);
                         break;
                     }
                 }
                 if (';' == $line[$cix]) {
                     $attr[++$attrix] = null;
                 } else {
                     $attr[$attrix] .= $line[$cix];
                 }
             }
             /* make attributes in array format */
             $propattr = array();
             foreach ($attr as $attribute) {
                 $attrsplit = explode('=', $attribute, 2);
                 if (1 < count($attrsplit)) {
                     $propattr[$attrsplit[0]] = $attrsplit[1];
                 } else {
                     $propattr[] = $attribute;
                 }
             }
             /* update Property */
             if (FALSE !== strpos($line, ',')) {
                 $content = explode(',', $line);
                 $clen = count($content);
                 for ($cix = 0; $cix < $clen; $cix++) {
                     if ("\\" == substr($content[$cix], -1)) {
                         $content[$cix] .= ',' . $content[$cix + 1];
                         unset($content[$cix + 1]);
                         $cix++;
                     }
                 }
                 if (1 < count($content)) {
                     foreach ($content as $cix => $contentPart) {
                         $content[$cix] = $toolbox->_strunrep($contentPart);
                     }
                     $this->setProperty($propname, $content, $propattr);
                     continue;
                 } else {
                     $line = reset($content);
                 }
                 $line = $toolbox->_strunrep($line);
             }
             $this->setProperty($propname, trim($line), $propattr);
         }
         // end - foreach( $this->unparsed.. .
     }
     // end - if( is_array( $this->unparsed.. .
     /* parse Components */
     if (is_array($this->components) && 0 < count($this->components)) {
         for ($six = 0; $six < count($this->components); $six++) {
             if (!empty($this->components[$six])) {
                 $this->components[$six]->parse();
             }
         }
     } else {
         return FALSE;
     }
     /* err 91 or something.. . */
     return TRUE;
 }
 /**
  * parse component unparsed data into properties
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.8.2 - 2011-05-21
  * @param mixed $unparsedtext, optional, strict rfc2445 formatted, single property string or array of strings
  * @return bool FALSE if error occurs during parsing
  *
  */
 function parse($unparsedtext = null)
 {
     if (!empty($unparsedtext)) {
         $nl = $this->getConfig('nl');
         if (is_array($unparsedtext)) {
             $unparsedtext = implode('\\n' . $nl, $unparsedtext);
         }
         /* fix line folding */
         $eolchars = array("\r\n", "\n\r", "\n", "\r");
         // check all line endings
         $EOLmark = FALSE;
         foreach ($eolchars as $eolchar) {
             if (!$EOLmark && FALSE !== strpos($unparsedtext, $eolchar)) {
                 $unparsedtext = str_replace($eolchar . " ", '', $unparsedtext);
                 $unparsedtext = str_replace($eolchar . "\t", '', $unparsedtext);
                 if ($eolchar != $nl) {
                     $unparsedtext = str_replace($eolchar, $nl, $unparsedtext);
                 }
                 $EOLmark = TRUE;
             }
         }
         $tmp = explode($nl, $unparsedtext);
         $unparsedtext = array();
         foreach ($tmp as $tmpr) {
             if (!empty($tmpr)) {
                 $unparsedtext[] = $tmpr;
             }
         }
     } elseif (!isset($this->unparsed)) {
         $unparsedtext = array();
     } else {
         $unparsedtext = $this->unparsed;
     }
     $this->unparsed = array();
     $comp =& $this;
     $config = $this->getConfig();
     foreach ($unparsedtext as $line) {
         // echo $comp->objName.": $line<br />"; // test ###
         if (in_array(strtoupper(substr($line, 0, 6)), array('END:VA', 'END:ST', 'END:DA'))) {
             $this->components[] = $comp->copy();
         } elseif ('END:' == strtoupper(substr($line, 0, 4))) {
             break;
         } elseif ('BEGIN:VALARM' == strtoupper(substr($line, 0, 12))) {
             $comp = new valarm($config);
         } elseif ('BEGIN:STANDARD' == strtoupper(substr($line, 0, 14))) {
             $comp = new vtimezone('standard', $config);
         } elseif ('BEGIN:DAYLIGHT' == strtoupper(substr($line, 0, 14))) {
             $comp = new vtimezone('daylight', $config);
         } elseif ('BEGIN:' == strtoupper(substr($line, 0, 6))) {
             continue;
         } else {
             $comp->unparsed[] = $line;
             // echo $comp->objName.": $line<br />\n"; // test ###
         }
     }
     unset($config);
     // echo $this->objName.'<br />'.var_export( $this->unparsed, TRUE )."<br />\n"; // test ###
     /* 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();
     foreach ($this->unparsed as $line) {
         $newProp = FALSE;
         foreach ($propnames as $propname) {
             if ($propname == strtolower(substr($line, 0, strlen($propname)))) {
                 $newProp = TRUE;
                 break;
             }
         }
         if ($newProp) {
             if (-1 < $lastix) {
                 $proprows[$lastix] = $proprows[$lastix];
             }
             $newProp = FALSE;
             $lastix++;
             $proprows[$lastix] = $line;
         } else {
             $proprows[$lastix] .= '!"#¤%&/()=?' . $line;
         }
     }
     /* parse each property 'line' */
     foreach ($proprows as $line) {
         $line = str_replace('!"#¤%&/()=? ', '', $line);
         $line = str_replace('!"#¤%&/()=?', '', $line);
         if ('\\n' == substr($line, -2)) {
             $line = substr($line, 0, strlen($line) - 2);
         }
         /* get propname, (problem with x-properties, otherwise in previous loop) */
         $cix = $propname = null;
         for ($cix = 0, $clen = strlen($line); $cix < $clen; $cix++) {
             if (in_array($line[$cix], array(':', ';'))) {
                 break;
             } else {
                 $propname .= $line[$cix];
             }
         }
         if ('x-' == substr($propname, 0, 2) || 'X-' == substr($propname, 0, 2)) {
             $propname2 = $propname;
             $propname = 'X-';
         }
         /* rest of the line is opt.params and value */
         $line = substr($line, $cix);
         /* separate attributes from value */
         $attr = array();
         $attrix = -1;
         $clen = strlen($line);
         for ($cix = 0; $cix < $clen; $cix++) {
             if (':' == $line[$cix] && '://' != substr($line, $cix, 3) && !in_array(strtolower(substr($line, $cix - 3, 4)), array('fax:', 'cid:', 'sms:', 'tel:', 'urn:')) && !in_array(strtolower(substr($line, $cix - 4, 5)), array('crid:', 'news:', 'pres:')) && 'mailto:' != strtolower(substr($line, $cix - 6, 7))) {
                 $attrEnd = TRUE;
                 if ($cix < $clen - 4 && ctype_digit(substr($line, $cix + 1, 4))) {
                     // an URI with a (4pos) portnr??
                     for ($c2ix = $cix; 3 < $c2ix; $c2ix--) {
                         if ('://' == substr($line, $c2ix - 2, 3)) {
                             $attrEnd = FALSE;
                             break;
                             // an URI with a portnr!!
                         }
                     }
                 }
                 if ($attrEnd) {
                     $line = substr($line, $cix + 1);
                     break;
                 }
             }
             if (';' == $line[$cix]) {
                 $attr[++$attrix] = null;
             } else {
                 $attr[$attrix] .= $line[$cix];
             }
         }
         /* make attributes in array format */
         $propattr = array();
         foreach ($attr as $attribute) {
             $attrsplit = explode('=', $attribute, 2);
             if (1 < count($attrsplit)) {
                 $propattr[$attrsplit[0]] = $attrsplit[1];
             } else {
                 $propattr[] = $attribute;
             }
         }
         /* call setProperty( $propname.. . */
         switch (strtoupper($propname)) {
             case 'ATTENDEE':
                 foreach ($propattr as $pix => $attr) {
                     $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 = explode(',', $line);
                     $clen = count($content);
                     for ($cix = 0; $cix < $clen; $cix++) {
                         if ("\\" == substr($content[$cix], -1)) {
                             $content[$cix] .= ',' . $content[$cix + 1];
                             unset($content[$cix + 1]);
                             $cix++;
                         }
                     }
                     if (1 < count($content)) {
                         $content = array_values($content);
                         foreach ($content as $cix => $contentPart) {
                             $content[$cix] = calendarComponent::_strunrep($contentPart);
                         }
                         $this->setProperty($propname, $content, $propattr);
                         break;
                     } else {
                         $line = reset($content);
                     }
                 }
             case 'X-':
                 $propname = isset($propname2) ? $propname2 : $propname;
             case 'COMMENT':
             case 'CONTACT':
             case 'DESCRIPTION':
             case 'LOCATION':
             case 'SUMMARY':
                 if (empty($line)) {
                     $propattr = null;
                 }
                 $this->setProperty($propname, calendarComponent::_strunrep($line), $propattr);
                 unset($propname2);
                 break;
             case 'REQUEST-STATUS':
                 $values = explode(';', $line, 3);
                 $values[1] = !isset($values[1]) ? null : calendarComponent::_strunrep($values[1]);
                 $values[2] = !isset($values[2]) ? null : calendarComponent::_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 'ACTION':
             case 'CLASSIFICATION':
             case 'STATUS':
             case 'TRANSP':
             case 'UID':
             case 'TZID':
             case 'RELATED-TO':
             case 'TZNAME':
                 $line = calendarComponent::_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 component unparsed data into properties
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.15.10 - 2012-10-28
  * @param mixed $unparsedtext, optional, strict rfc2445 formatted, single property string or array of strings
  * @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 = explode($nl, iCalUtilityFunctions::convEolChar($unparsedtext, $nl));
     } elseif (!isset($this->unparsed)) {
         $unparsedtext = array();
     } else {
         $unparsedtext = $this->unparsed;
     }
     /* skip leading (empty/invalid) lines */
     foreach ($unparsedtext as $lix => $line) {
         $tst = trim($line);
         if ('\\n' == $tst || empty($tst)) {
             unset($unparsedtext[$lix]);
         } else {
             break;
         }
     }
     $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' */
     $paramMStz = array('utc-', 'utc+', 'gmt-', 'gmt+');
     $paramProto3 = array('fax:', 'cid:', 'sms:', 'tel:', 'urn:');
     $paramProto4 = array('crid:', 'news:', 'pres:');
     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 */
         $attr = array();
         $attrix = -1;
         $clen = strlen($line);
         $WithinQuotes = FALSE;
         $cix = 0;
         while (FALSE !== substr($line, $cix, 1)) {
             if (':' == $line[$cix] && substr($line, $cix, 3) != '://' && !in_array(strtolower(substr($line, $cix - 6, 4)), $paramMStz) && !in_array(strtolower(substr($line, $cix - 3, 4)), $paramProto3) && !in_array(strtolower(substr($line, $cix - 4, 5)), $paramProto4) && strtolower(substr($line, $cix - 6, 7)) != 'mailto:' && !$WithinQuotes) {
                 $attrEnd = TRUE;
                 if ($cix < $clen - 4 && ctype_digit(substr($line, $cix + 1, 4))) {
                     // an URI with a (4pos) portnr??
                     for ($c2ix = $cix; 3 < $c2ix; $c2ix--) {
                         if ('://' == substr($line, $c2ix - 2, 3)) {
                             $attrEnd = FALSE;
                             break;
                             // an URI with a portnr!!
                         }
                     }
                 }
                 if ($attrEnd) {
                     $line = substr($line, $cix + 1);
                     break;
                 }
                 ++$cix;
             }
             if ('"' == $line[$cix]) {
                 $WithinQuotes = FALSE === $WithinQuotes ? TRUE : FALSE;
             }
             if (';' == $line[$cix]) {
                 $attr[++$attrix] = null;
             } else {
                 $attr[$attrix] .= $line[$cix];
             }
             ++$cix;
         }
         /* make attributes in array format */
         $propattr = array();
         foreach ($attr as $attribute) {
             $attrsplit = explode('=', $attribute, 2);
             if (1 < count($attrsplit)) {
                 $propattr[$attrsplit[0]] = $attrsplit[1];
             } else {
                 $propattr[] = $attribute;
             }
         }
         /* 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 'X-':
                 $propname = isset($propname2) ? $propname2 : $propname;
                 unset($propname2);
             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] = calendarComponent::_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, calendarComponent::_strunrep($line), $propattr);
                 break;
             case 'REQUEST-STATUS':
                 $values = explode(';', $line, 3);
                 $values[1] = !isset($values[1]) ? null : calendarComponent::_strunrep($values[1]);
                 $values[2] = !isset($values[2]) ? null : calendarComponent::_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 'ACTION':
             case 'CLASSIFICATION':
             case 'STATUS':
             case 'TRANSP':
             case 'UID':
             case 'TZID':
             case 'RELATED-TO':
             case 'TZNAME':
                 $line = calendarComponent::_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;
 }
Exemple #4
0
 /**
  * parse iCal text/file into iCal_Vcalendar, components, properties and parameters
  *
  * @author Kjell-Inge Gustafsson, kigkonsult <*****@*****.**>
  * @since 2.8.2 - 2011-05-21
  * @param mixed $unparsedtext, optional, strict rfc2445 formatted, single property string or array of property strings
  * @return bool FALSE if error occurs during parsing
  *
  */
 function parse($unparsedtext = FALSE)
 {
     $nl = $this->getConfig('nl');
     if (FALSE === $unparsedtext || empty($unparsedtext)) {
         /* directory+filename is set previously via setConfig directory+filename or url */
         if (FALSE === ($filename = $this->getConfig('url'))) {
             $filename = $this->getConfig('dirfile');
         }
         /* READ FILE */
         if (FALSE === ($rows = file_get_contents($filename))) {
             return FALSE;
         }
         /* err 1 */
     } elseif (is_array($unparsedtext)) {
         $rows = implode('\\n' . $nl, $unparsedtext);
     } else {
         $rows =& $unparsedtext;
     }
     /* identify BEGIN:VCALENDAR, MUST be first row */
     if ('BEGIN:VCALENDAR' != strtoupper(substr($rows, 0, 15))) {
         return FALSE;
     }
     /* err 8 */
     /* fix line folding */
     $eolchars = array("\r\n", "\n\r", "\n", "\r");
     // check all line endings
     $EOLmark = FALSE;
     foreach ($eolchars as $eolchar) {
         if (!$EOLmark && FALSE !== strpos($rows, $eolchar)) {
             $rows = str_replace($eolchar . " ", '', $rows);
             $rows = str_replace($eolchar . "\t", '', $rows);
             if ($eolchar != $nl) {
                 $rows = str_replace($eolchar, $nl, $rows);
             }
             $EOLmark = TRUE;
         }
     }
     $tmp = explode($nl, $rows);
     $rows = array();
     foreach ($tmp as $tmpr) {
         if (!empty($tmpr)) {
             $rows[] = $tmpr;
         }
     }
     /* skip trailing empty lines */
     $lix = count($rows) - 1;
     while (empty($rows[$lix]) && 0 < $lix) {
         $lix -= 1;
     }
     /* identify ending END:VCALENDAR row, MUST  be last row */
     if ('END:VCALENDAR' != strtoupper(substr($rows[$lix], 0, 13))) {
         return FALSE;
     }
     /* err 9 */
     if (3 > count($rows)) {
         return FALSE;
     }
     /* err 10 */
     $comp =& $this;
     $calsync = 0;
     /* identify components and update unparsed data within component */
     $config = $this->getConfig();
     foreach ($rows as $line) {
         if ('' == trim($line)) {
             continue;
         }
         if ('BEGIN:VCALENDAR' == strtoupper(substr($line, 0, 15))) {
             $calsync++;
             continue;
         } elseif ('END:VCALENDAR' == strtoupper(substr($line, 0, 13))) {
             $calsync--;
             break;
         } elseif (1 != $calsync) {
             return FALSE;
         } elseif (in_array(strtoupper(substr($line, 0, 6)), array('END:VE', 'END:VF', 'END:VJ', 'END:VT'))) {
             $this->components[] = $comp->copy();
             continue;
         }
         if ('BEGIN:VEVENT' == strtoupper(substr($line, 0, 12))) {
             $comp = new vevent($config);
         } elseif ('BEGIN:VFREEBUSY' == strtoupper(substr($line, 0, 15))) {
             $comp = new vfreebusy($config);
         } elseif ('BEGIN:VJOURNAL' == strtoupper(substr($line, 0, 14))) {
             $comp = new vjournal($config);
         } elseif ('BEGIN:VTODO' == strtoupper(substr($line, 0, 11))) {
             $comp = new vtodo($config);
         } elseif ('BEGIN:VTIMEZONE' == strtoupper(substr($line, 0, 15))) {
             $comp = new vtimezone($config);
         } else {
             /* update component with unparsed data */
             $comp->unparsed[] = $line;
         }
     }
     // end - foreach( rows.. .
     unset($config);
     /* 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 */
         $lastix = -1;
         $propnames = array('calscale', 'method', 'prodid', 'version', 'x-');
         $proprows = array();
         foreach ($this->unparsed as $line) {
             if ('' == trim($line)) {
                 continue;
             }
             $newProp = FALSE;
             foreach ($propnames as $propname) {
                 if ($propname == strtolower(substr($line, 0, strlen($propname)))) {
                     $newProp = TRUE;
                     break;
                 }
             }
             if ($newProp) {
                 $newProp = FALSE;
                 $lastix++;
                 $proprows[$lastix] = $line;
             } else {
                 $proprows[$lastix] .= '!"#¤%&/()=?' . $line;
             }
         }
         foreach ($proprows as $line) {
             $line = str_replace('!"#¤%&/()=? ', '', $line);
             $line = str_replace('!"#¤%&/()=?', '', $line);
             if ('\\n' == substr($line, -2)) {
                 $line = substr($line, 0, strlen($line) - 2);
             }
             /* get property name */
             $cix = $propname = null;
             for ($cix = 0, $clen = strlen($line); $cix < $clen; $cix++) {
                 if (in_array($line[$cix], array(':', ';'))) {
                     break;
                 } else {
                     $propname .= $line[$cix];
                 }
             }
             /* ignore version/prodid properties */
             if (in_array(strtoupper($propname), array('VERSION', 'PRODID'))) {
                 continue;
             }
             $line = substr($line, $cix);
             /* separate attributes from value */
             $attr = array();
             $attrix = -1;
             $strlen = strlen($line);
             for ($cix = 0; $cix < $strlen; $cix++) {
                 if (':' == $line[$cix] && '://' != substr($line, $cix, 3) && !in_array(strtolower(substr($line, $cix - 3, 4)), array('fax:', 'cid:', 'sms:', 'tel:', 'urn:')) && !in_array(strtolower(substr($line, $cix - 4, 5)), array('crid:', 'news:', 'pres:')) && 'mailto:' != strtolower(substr($line, $cix - 6, 7))) {
                     $attrEnd = TRUE;
                     if ($cix < $strlen - 4 && ctype_digit(substr($line, $cix + 1, 4))) {
                         // an URI with a (4pos) portnr??
                         for ($c2ix = $cix; 3 < $c2ix; $c2ix--) {
                             if ('://' == substr($line, $c2ix - 2, 3)) {
                                 $attrEnd = FALSE;
                                 break;
                                 // an URI with a portnr!!
                             }
                         }
                     }
                     if ($attrEnd) {
                         $line = substr($line, $cix + 1);
                         break;
                     }
                 }
                 if (';' == $line[$cix]) {
                     $attr[++$attrix] = null;
                 } else {
                     $attr[$attrix] .= $line[$cix];
                 }
             }
             /* make attributes in array format */
             $propattr = array();
             foreach ($attr as $attribute) {
                 $attrsplit = explode('=', $attribute, 2);
                 if (1 < count($attrsplit)) {
                     $propattr[$attrsplit[0]] = $attrsplit[1];
                 } else {
                     $propattr[] = $attribute;
                 }
             }
             /* update Property */
             if (FALSE !== strpos($line, ',')) {
                 $content = explode(',', $line);
                 $clen = count($content);
                 for ($cix = 0; $cix < $clen; $cix++) {
                     if ("\\" == substr($content[$cix], -1)) {
                         $content[$cix] .= ',' . $content[$cix + 1];
                         unset($content[$cix + 1]);
                         $cix++;
                     }
                 }
                 if (1 < count($content)) {
                     foreach ($content as $cix => $contentPart) {
                         $content[$cix] = calendarComponent::_strunrep($contentPart);
                     }
                     $this->setProperty($propname, $content, $propattr);
                     continue;
                 } else {
                     $line = reset($content);
                 }
                 $line = calendarComponent::_strunrep($line);
             }
             $this->setProperty($propname, trim($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;
 }