Ejemplo n.º 1
0
function export_recurrence_ical($id, $date)
{
    global $days_per_month, $ldays_per_month;
    $str_day = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
    $sql = "SELECT cal_date FROM webcal_entry_repeats_not WHERE cal_id = '{$id}'";
    $res = dbi_query($sql);
    if ($res) {
        $exdate = array();
        $i = 0;
        while ($row = dbi_fetch_row($res)) {
            $exdate[$i] = $row[0];
            $i++;
        }
        dbi_free_result($res);
    }
    $sql = "SELECT webcal_entry_repeats.cal_type, " . "webcal_entry_repeats.cal_end, webcal_entry_repeats.cal_frequency, " . "webcal_entry_repeats.cal_days, webcal_entry.cal_time " . "FROM webcal_entry, webcal_entry_repeats " . "WHERE webcal_entry_repeats.cal_id = '{$id}' " . "AND webcal_entry.cal_id = '{$id}'";
    $res = dbi_query($sql);
    if ($res) {
        if ($row = dbi_fetch_row($res)) {
            $type = $row[0];
            $end = $row[1];
            $freq = $row[2];
            $day = $row[3];
            $time = $row[4];
            $byday = "";
            echo "RRULE:";
            /* recurrence frequency */
            switch ($type) {
                case 'daily':
                    echo "FREQ=DAILY";
                    break;
                case 'weekly':
                    echo "FREQ=WEEKLY";
                    break;
                case 'monthlyByDayR':
                case 'monthlyByDay':
                case 'monthlyByDate':
                    echo "FREQ=MONTHLY";
                    break;
                case 'yearly':
                    echo "FREQ=YEARLY";
                    break;
            }
            echo ";INTERVAL={$freq}";
            if ($type == "weekly") {
                if ($day != "nnnnnnn") {
                    echo ";BYDAY=";
                    for ($i = 0; $i < strlen($day); $i++) {
                        if ($day[$i] == 'y') {
                            $byday .= $str_day[$i] . ",";
                        }
                    }
                    $byday = substr($byday, 0, strlen($byday) - 1);
                    // suppress last ','
                    echo $byday;
                }
            } elseif ($type == "monthlyByDate") {
                $day = (int) substr($date, -2, 2);
                echo ";BYMONTHDAY={$day}";
            } elseif ($type == "monthlyByDay") {
                $year = (int) substr($date, 0, -4);
                $month = (int) substr($date, -4, 2);
                $day = (int) substr($date, -2, 2);
                $stamp = mktime(0, 0, 0, $month, $day, $year);
                $dow = date("w", $stamp);
                $dow1 = date("w", mktime(3, 0, 0, $month, 1, $year));
                $partWeek = (7 - $dow1) % 7;
                $whichWeek = ceil(($day - $partWeek) / 7);
                if ($partWeek && $dow >= $dow1) {
                    $whichWeek++;
                }
                printf(";BYDAY=%d%s", $whichWeek, $str_day[$dow]);
            } elseif ($type == "monthlyByDayR") {
                $year = (int) substr($date, 0, -4);
                $month = (int) substr($date, -4, 2);
                $day = (int) substr($date, -2, 2);
                $stamp = mktime(0, 0, 0, $month, $day, $year);
                $dow = date("w", $stamp);
                // get number of days in this month
                $daysthismonth = $year % 4 == 0 ? $ldays_per_month[$month] : $days_per_month[$month];
                // how many weekdays like this one remain in the month?
                // 0=last one, 1=one more after this one, etc.
                $whichWeek = floor(($daysthismonth - $day) / 7);
                printf(";BYDAY=%d%s", -1 - $whichWeek, $str_day[$dow]);
            }
            if (!empty($end)) {
                echo ";UNTIL=";
                $utc = export_get_utc_date($end, $time);
                echo $utc;
            }
            echo "\r\n";
            if (count($exdate) > 0) {
                $string = "EXDATE:";
                $i = 0;
                while ($i < count($exdate)) {
                    $date = export_get_utc_date($exdate[$i], $time);
                    $string .= "{$date},";
                    $i++;
                }
                $string = substr($string, 0, strlen($string) - 1);
                // suppress last ','
                $string = export_fold_lines($string);
                while (list($key, $value) = each($string)) {
                    echo "{$value}\r\n";
                }
            }
        }
    }
}
Ejemplo n.º 2
0
$get_unapproved = false;
$datem = date('m');
$dateY = date('Y');
// Start date is beginning of this month.
$startdate = mktime(0, 0, 0, $datem, 0, $dateY);
// End date is one year from now.
// Seems kind of arbitrary, eh?
$enddate = mktime(0, 0, 0, $datem, 1, $dateY + 1);
/* Pre-Load the repeated events for quicker access. */
$repeated_events = read_repeated_events($user, $startdate, $enddate, '');
/* Pre-load the non-repeating events for quicker access. */
$events = read_events($user, $startdate, $enddate);
// Loop from start date until we reach end date...
$event_text = '';
for ($d = $startdate; $d <= $enddate; $d += 86400) {
    $dYmd = date('Ymd', $d);
    $ev = get_entries($dYmd, $get_unapproved);
    $evcnt = count($ev);
    for ($i = 0; $i < $evcnt; $i++) {
        $event_text .= fb_export_time($dYmd, $ev[$i]->getDuration(), $ev[$i]->getTime(), 'ical');
    }
    $revents = get_repeating_entries($user, $dYmd, $get_unapproved);
    $recnt = count($revents);
    for ($i = 0; $i < $recnt; $i++) {
        $event_text .= fb_export_time($dYmd, $revents[$i]->getDuration(), $revents[$i]->getTime(), 'ical');
    }
}
header('Content-Type: text/calendar');
header('Content-Disposition: attachment; filename="' . $login . '.ifb"');
echo 'BEGIN:VCALENDAR' . "\r\n" . 'X-WR-CALNAME;VALUE=TEXT:' . str_replace(',', '\\,', empty($publish_fullname) ? $user : translate($publish_fullname)) . "\r\n" . generate_prodid() . 'VERSION:2.0' . "\r\n" . 'METHOD:PUBLISH' . "\r\n" . 'BEGIN:VFREEBUSY' . "\r\n" . 'DTSTART:' . export_get_utc_date(date('Ymd', $startdate), 0) . "\r\n" . 'DTEND:' . export_get_utc_date(date('Ymd', $enddate), '235959') . "\r\n" . $event_text . 'URL:' . $GLOBALS['SERVER_URL'] . 'freebusy.php/' . $user . '.ifb' . "\r\n" . 'END:VFREEBUSY' . "\r\n" . 'END:VCALENDAR' . "\r\n";
exit;
Ejemplo n.º 3
0
function export_recurrence_ical($id, $date)
{
    $sql = "SELECT cal_date FROM webcal_entry_repeats_not WHERE cal_id = '{$id}'";
    $res = dbi_query($sql);
    if ($res) {
        $exdate = array();
        $i = 0;
        while ($row = dbi_fetch_row($res)) {
            $exdate[$i] = $row[0];
            $i++;
        }
    }
    dbi_free_result($res);
    $sql = "SELECT webcal_entry_repeats.cal_type, webcal_entry_repeats.cal_end, " . "webcal_entry_repeats.cal_frequency, webcal_entry_repeats.cal_days, webcal_entry.cal_time" . " FROM webcal_entry, webcal_entry_repeats WHERE webcal_entry_repeats.cal_id = '{$id}'" . "AND webcal_entry.cal_id = '{$id}'";
    $res = dbi_query($sql);
    if ($res) {
        $row = dbi_fetch_row($res);
    }
    if ($row) {
        $type = $row[0];
        $end = $row[1];
        $freq = $row[2];
        $day = $row[3];
        $time = $row[4];
        $str_day = array('SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA');
        $byday = "";
        echo "RRULE:";
        /* recurrence frequency */
        switch ($type) {
            case 'daily':
                echo "FREQ=DAILY";
                break;
            case 'weekly':
                echo "FREQ=WEEKLY";
                break;
            case 'monthlyByDay':
            case 'monthlyByDate':
                echo "FREQ=MONTHLY";
                break;
            case 'yearly':
                echo "FREQ=YEARLY";
                break;
        }
        echo ";INTERVAL={$freq}";
        if ($type == "weekly") {
            if ($day != "nnnnnnn") {
                echo ";BYDAY=";
                for ($i = 0; $i < strlen($day); $i++) {
                    if ($day[$i] == 'y') {
                        $byday .= $str_day[$i] . ",";
                    }
                }
                $byday = substr($byday, 0, strlen($byday) - 1);
                // suppress last ','
                echo $byday;
            }
        } elseif ($type == "monthlyByDate") {
            $day = (int) substr($date, -2, 2);
            echo ";BYMONTHDAY={$day}";
        } elseif ($type == "monthlyByDay") {
            echo ";BYDAY=";
            $year = (int) substr($date, 0, -4);
            $month = (int) substr($date, -4, 2);
            $day = (int) substr($date, -2, 2);
            $stamp = mktime(0, 0, 0, $month, $day, $year);
            $date_array = getdate($stamp);
            echo $str_day[$date_array['wday']];
            $next_stamp = $stamp + 7 * 24 * 60 * 60;
            $next_date_array = getdate($next_stamp);
            if ($date_array['mon'] != $next_date_array['mon']) {
                $pos = -1;
            } else {
                $pos = (int) ($day / 7);
                if ($day % 7 > 0) {
                    $pos++;
                }
            }
            echo ";BYSETPOS={$pos}";
        }
        if (!empty($end)) {
            echo ";UNTIL=";
            $utc = export_get_utc_date($end, $time);
            echo $utc;
        }
        echo "\r\n";
        if (count($exdate) > 0) {
            $string = "EXDATE:";
            $i = 0;
            while ($i < count($exdate)) {
                $date = export_get_utc_date($exdate[$i], $time);
                $string .= "{$date},";
                $i++;
            }
            $string = substr($string, 0, strlen($string) - 1);
            // suppress last ','
            $string = export_fold_lines($string);
            while (list($key, $value) = each($string)) {
                echo "{$value}\r\n";
            }
        }
    }
}
Ejemplo n.º 4
0
function fb_export_time($date, $duration, $time, $texport)
{
    $ret = '';
    $time = sprintf("%06d", $time);
    $allday = $time == -1 || $duration == 1440;
    $year = (int) substr($date, 0, -4);
    $month = (int) substr($date, -4, 2);
    $day = (int) substr($date, -2, 2);
    // No time, or an "All day" event"
    if ($allday) {
        // untimed event - consider this to not be busy
    } else {
        // normal/timed event (or all-day event)
        $hour = (int) substr($time, 0, -4);
        $min = (int) substr($time, -4, 2);
        $sec = (int) substr($time, -2, 2);
        $duration = $duration * 60;
        $start_tmstamp = mktime($hour, $min, $sec, $month, $day, $year);
        $utc_start = export_get_utc_date($date, $time);
        $end_tmstamp = $start_tmstamp + $duration;
        $utc_end = export_get_utc_date(date('Ymd', $end_tmstamp), date('His', $end_tmstamp));
        $ret .= "FREEBUSY:{$utc_start}/{$utc_end}\r\n";
    }
    return $ret;
}