Пример #1
0
function parse_vcal($cal_file)
{
    global $tz, $errormsg;
    $vcal_data = array();
    //echo "Parsing vcal file... <br />\n";
    if (!($fd = @fopen($cal_file, "r"))) {
        $errormsg .= "Can't read temporary file: {$cal_file}\n";
        exit;
    } else {
        // reflect the section where we are in the file:
        // VCALENDAR, TZ/DAYLIGHT, VEVENT, ALARM
        $state = "NONE";
        $substate = "none";
        // reflect the sub section
        $subsubstate = "";
        // reflect the sub-sub section
        $error = false;
        $line = 0;
        $event = '';
        while (!feof($fd) && !$error) {
            $line++;
            $buff = fgets($fd, 4096);
            $buff = chop($buff);
            // parser debugging code...
            //echo "line = $line <br />";
            //echo "state = $state <br />";
            //echo "substate = $substate <br />";
            //echo "subsubstate = $subsubstate <br />";
            //echo "buff = " . htmlspecialchars ( $buff ) . "<br /><br />\n";
            if ($state == "VEVENT") {
                if (!empty($subsubstate)) {
                    if (preg_match("/^END:(.+)\$/i", $buff, $match)) {
                        if ($match[1] == $subsubstate) {
                            $subsubstate = '';
                        }
                    } else {
                        if ($subsubstate == "VALARM" && preg_match("/TRIGGER:(.+)\$/i", $buff, $match)) {
                            //echo "Set reminder to $match[1]<br />";
                            //reminder time is $match[1]
                        }
                    }
                } else {
                    if (preg_match("/^BEGIN:(.+)\$/i", $buff, $match)) {
                        $subsubstate = $match[1];
                    } else {
                        if (preg_match("/^SUMMARY.*:(.+)\$/i", $buff, $match)) {
                            $substate = "summary";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^DESCRIPTION:(.+)\$/i", $buff, $match)) {
                            $substate = "description";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^DESCRIPTION\\S*:(.+)\$/i", $buff, $match)) {
                            $substate = "description";
                            $event[$substate] = $match[1];
                            if (preg_match("/encoding=quoted-printable/i", $buff)) {
                                $event[$substate] = quoted_printable_decode($match[1]);
                            }
                        } elseif (preg_match("/^CLASS.*:(.+)\$/i", $buff, $match)) {
                            $substate = "class";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^PRIORITY.*:(.+)\$/i", $buff, $match)) {
                            $substate = "priority";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^DTSTART.*:(.+)\$/i", $buff, $match)) {
                            $substate = "dtstart";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^DTEND.*:(.+)\$/i", $buff, $match)) {
                            $substate = "dtend";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^RRULE.*:(.+)\$/i", $buff, $match)) {
                            $substate = "rrule";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^EXDATE.*:(.+)\$/i", $buff, $match)) {
                            $substate = "exdate";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^DALARM.*:(.+)\$/i", $buff, $match)) {
                            $substate = "dalarm";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^CATEGORIES.*:(.+)\$/i", $buff, $match)) {
                            $substate = "categories";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^UID.*:(.+)\$/i", $buff, $match)) {
                            $substate = "uid";
                            $event[$substate] = $match[1];
                        } elseif (preg_match("/^END:VEVENT\$/i", $buff, $match)) {
                            $state = "VCALENDAR";
                            $substate = "none";
                            $subsubstate = '';
                            $vcal_data[] = format_vcal($event);
                            // clear out data for new event
                            $event = '';
                            // TODO: QUOTED-PRINTABLE descriptions
                            // folded lines
                        } elseif (preg_match("/^[ ]{1}(.+)\$/", $buff, $match)) {
                            if ($substate != "none") {
                                $event[$substate] .= $match[1];
                            } else {
                                $errormsg .= "Error in file {$cal_file} line {$line}:<br />{$buff}\n";
                                $error = true;
                            }
                            // For unsupported properties
                        } else {
                            $substate = "none";
                        }
                    }
                }
            } elseif ($state == "VCALENDAR") {
                if (preg_match("/^TZ.*:(.+)\$/i", $buff, $match)) {
                    $event['tz'] = $match[1];
                } elseif (preg_match("/^DAYLIGHT.*:(.+)\$/i", $buff, $match)) {
                    $event['daylight'] = $match[1];
                } elseif (preg_match("/^BEGIN:VEVENT\$/i", $buff)) {
                    $state = "VEVENT";
                } elseif (preg_match("/^END:VCALENDAR\$/i", $buff)) {
                    $state = "NONE";
                }
            } elseif ($state == "NONE") {
                if (preg_match("/^BEGIN:VCALENDAR\$/i", $buff)) {
                    $state = "VCALENDAR";
                } else {
                    if (preg_match("/^BEGIN:ALARM\$/i", $buff)) {
                        $state = "ALARM";
                    }
                }
            }
        }
        //End while
        fclose($fd);
    }
    return $vcal_data;
}
Пример #2
0
function parse_vcal($cal_file)
{
    global $tz, $errormsg;
    $vcal_data = array();
    // echo "Parsing vcal file...<br />\n";
    if (!($fd = @fopen($cal_file, 'r'))) {
        $errormsg .= "Can't read temporary file: {$cal_file}\n";
        exit;
    } else {
        // reflect the section where we are in the file:
        // VCALENDAR, TZ/DAYLIGHT, VEVENT, ALARM
        $state = 'NONE';
        $substate = 'none';
        // reflect the sub section
        $subsubstate = '';
        // reflect the sub-sub section
        $error = false;
        $line = 0;
        $event = '';
        while (!feof($fd) && !$error) {
            $line++;
            if ($line > 5 && $line < 10 && $state == 'NONE') {
                // we are probably not reading a vcs file
                return false;
            }
            $buff = fgets($fd, 4096);
            $buff = chop($buff);
            if ($state == 'VEVENT') {
                if (!empty($subsubstate)) {
                    if (preg_match('/^END:(.+)$/i', $buff, $match)) {
                        if ($match[1] == $subsubstate) {
                            $subsubstate = '';
                        }
                    } else {
                        if ($subsubstate == 'VALARM' && preg_match("/TRIGGER:(.+)\$/i", $buff, $match)) {
                        }
                    }
                } else {
                    if (preg_match('/^BEGIN:(.+)$/i', $buff, $match)) {
                        $subsubstate = $match[1];
                    } else {
                        if (preg_match('/^SUMMARY.*:(.+)$/iU', $buff, $match)) {
                            $substate = 'summary';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^DESCRIPTION:(.+)$/iU', $buff, $match)) {
                            $substate = 'description';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^DESCRIPTION;ENCODING=QUOTED-PRINTABLE:(.+)$/i', $buff, $match)) {
                            $substate = 'descriptionqp';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^CLASS.*:(.+)$/i', $buff, $match)) {
                            $substate = 'class';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^PRIORITY.*:(.+)$/i', $buff, $match)) {
                            $substate = 'priority';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^DTSTART.*:(.+)$/i', $buff, $match)) {
                            $substate = 'dtstart';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^DTEND.*:(.+)$/i', $buff, $match)) {
                            $substate = 'dtend';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^RRULE.*:(.+)$/i', $buff, $match)) {
                            $substate = 'rrule';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^EXDATE.*:(.+)$/i', $buff, $match)) {
                            $substate = 'exdate';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^DALARM.*:(.+)$/i', $buff, $match)) {
                            $substate = 'dalarm';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^CATEGORIES.*:(.+)$/i', $buff, $match)) {
                            $substate = 'categories';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^UID.*:(.+)$/i', $buff, $match)) {
                            $substate = 'uid';
                            $event[$substate] = $match[1];
                        } elseif (preg_match('/^END:VEVENT$/i', $buff, $match)) {
                            $state = 'VCALENDAR';
                            $substate = 'none';
                            $subsubstate = '';
                            if ($tmp_data = format_vcal($event)) {
                                $vcal_data[] = $tmp_data;
                            }
                            // clear out data for new event
                            $event = '';
                            // TODO: QUOTED-PRINTABLE descriptions
                            // folded lines
                        } elseif (preg_match('/^[ ]{1}(.+)$/', $buff, $match)) {
                            if ($substate != 'none') {
                                $event[$substate] .= $match[1];
                            } else {
                                $errormsg .= "Error in file {$cal_file} line {$line}:<br />{$buff}\n";
                                $error = true;
                            }
                            // For unsupported properties
                        } else {
                            $substate = 'none';
                        }
                    }
                }
            } elseif ($state == 'VCALENDAR') {
                if (preg_match('/^TZ.*:(.+)$/i', $buff, $match)) {
                    $event['tz'] = $match[1];
                } elseif (preg_match('/^DAYLIGHT.*:(.+)$/i', $buff, $match)) {
                    $event['daylight'] = $match[1];
                } elseif (preg_match('/^BEGIN:VEVENT$/i', $buff)) {
                    $state = 'VEVENT';
                } elseif (preg_match('/^END:VCALENDAR$/i', $buff)) {
                    $state = 'NONE';
                }
            } elseif ($state == 'NONE') {
                if (preg_match('/^BEGIN:VCALENDAR$/i', $buff)) {
                    $state = 'VCALENDAR';
                } else {
                    if (preg_match('/^BEGIN:ALARM$/i', $buff)) {
                        $state = 'ALARM';
                    }
                }
            }
            $event['state'] = $state;
        }
        //End while
        fclose($fd);
    }
    return $vcal_data;
}