function amr_process_icalspec($criteria, $l_start, $end, $no_events, $icalno = 0)
{
    /*  parameters - an array of urls, an array of limits (actually in amr_limits)  */
    global $amr_options, $amr_limits, $amr_listtype, $amrW, $amrtotalevents, $change_view_allowed, $amr_doing_icallist, $amr_one_page_events_cache;
    /* if widget and calendar doing same thing */
    $amr_doing_icallist = true;
    if (!empty($amrW)) {
        $w = 'w';
    } else {
        $w = '';
    }
    if (!empty($criteria['show_in_events_timezone'])) {
        // if we want event timezones, not website timezone
        add_filter('amr_show_in_events_timezone', 'amr_show_in_events_timezone', 10, 2);
    }
    if (!empty($criteria['exclude_in_progress'])) {
        add_filter('amr_events_after_sort_and_constrain', 'amr_events_exclude_in_progress');
    }
    if (!empty($criteria['sort_later_events_first'])) {
        add_filter('amr_events_after_sort_and_constrain', 'amr_events_sort_later_events_first');
    }
    $icals = false;
    if (isset($amr_limits['eventpoststoo']) and $amr_limits['eventpoststoo']) {
        if (function_exists('amr_ical_from_posts')) {
            $events = amr_ical_from_posts($criteria);
            if (!empty($events)) {
                $icals = amr_make_ical_from_posts($events, $criteria);
                if (ICAL_EVENTS_DEBUG) {
                    echo '<br>Got calendars from posts :' . count($icals) . ' events=' . count($events) . '<br>';
                }
            } else {
                if (ICAL_EVENTS_DEBUG) {
                    echo '<br>No events found with this criteria<br>';
                }
            }
        } else {
            if (ICAL_EVENTS_DEBUG) {
                echo '<br>Function amr_ical_from_posts not found<br>';
            }
            if (empty($criteria['urls'])) {
                suggest_other_icalplugin(__('No url entered - did you want events from posts ?', 'amr-ical-events-list'));
            }
            //else _e("huh?");
        }
    } else {
        if (ICAL_EVENTS_DEBUG) {
            echo '<br />Ignore events posts ';
        }
    }
    /* ------------------------------  check for urls and do those too, or only */
    $icals2 = array();
    if (!empty($criteria['urls'])) {
        foreach ($criteria['urls'] as $i => $url) {
            $icals2[$i] = process_icalurl($url);
            if (!is_array($icals2[$i])) {
                unset($icals2[$i]);
            } else {
                if (function_exists('amr_mimic_taxonomies')) {
                    $icals2[$i] = amr_mimic_taxonomies($icals2[$i]);
                }
            }
            // filter by category_name
        }
    }
    if (!empty($icals)) {
        // we got some events
        if (!empty($icals2)) {
            $icals = array_merge($icals, $icals2);
        }
    } else {
        if (!empty($icals2)) {
            $icals = $icals2;
            // we got no events from posts, just from ics
        }
    }
    /* ------------------------------now we have potentially  a bunch of calendars in the ical array, each with properties and items */
    /* Merge then constrain  by options */
    $components = array();
    /* all components actually, not just events */
    if (isset($icals) and is_array($icals)) {
        /* if we have some parsed data */
        foreach ($icals as $j => $ical) {
            /* for each  Ics file within an ICal spec*/
            if (!isset($amr_options['listtypes'][$amr_listtype]['component']) or count($amr_options['listtypes'][$amr_listtype]['component']) < 1) {
                _e('No ical components requested for display', 'amr-ical-events-list');
            } else {
                foreach ($amr_options['listtypes'][$amr_listtype]['component'] as $i => $c) {
                    /* for each component type requested */
                    if ($c) {
                        /* If this component was requested, merge the items from Ical items into events */
                        if (!empty($ical[$i]) and is_array($ical[$i])) {
                            /* Eg: if we have an array $ical['VEVENT'] etc*/
                            foreach ($ical[$i] as $k => $a) {
                                /*  save the component type so we can style accordingly */
                                $ical[$i][$k]['type'] = $i;
                                $ical[$i][$k]['name'] = 'cal' . $j;
                                /* save the name for styling */
                            }
                            if (!empty($components)) {
                                $components = array_merge($components, $ical[$i]);
                            } else {
                                $components = $ical[$i];
                            }
                        }
                    }
                }
            }
        }
        if (ICAL_EVENTS_DEBUG) {
            echo '<br />Got x events ' . count($components);
        }
        amrical_mem_debug('Before process');
        $components = amr_process_icalevents($components, $l_start, $end, $no_events);
        amrical_mem_debug('Before constrain');
        $amrtotalevents = count($components);
        $components = amr_constrain_components($components, $l_start, $end, $no_events);
        $amr_last_date_time = amr_save_last_event_date($components);
        if (ICAL_EVENTS_DEBUG) {
            echo '<br />After constrain No dates:' . count($components) . ' and last event date time is: ';
            if (is_object($amr_last_date_time)) {
                echo $amr_last_date_time->format('c');
            }
            //var_dump($amr_last_date_time);
        }
    }
    amrical_mem_debug('Before listing');
    if (isset($icals) and is_array($icals)) {
        /* amr here is the main html  code  *** */
        if (isset($amr_limits['calendar_properties'])) {
            $do_prop = $amr_limits['calendar_properties'];
        } else {
            $do_prop = true;
        }
        if ($do_prop) {
            $tid = $w . 'calprop' . $icalno;
            $class = 'vcalendar ' . $w . 'icalprop ';
            //20110222
            $thecal = amr_list_properties($icals, $tid, $class);
            /* list the calendar properties if requested */
        } else {
            $thecal = '';
        }
        if (!amr_doing_box_calendar() and empty($components)) {
            $thecal .= amr_handle_no_events();
        } else {
            $tid = '';
            $class = ' ical ';
            $thecal .= amr_list_events($components, $tid, $class, $show_views = true);
        }
    } else {
        $thecal = '';
    }
    /* the urls were not valid or some other error ocurred, for this spec, we have nothing to print */
    /* amr  end of core calling code --- */
    if (!empty($amr_options['do_grouping_js'])) {
        $thecal .= amr_ical_load_frontend_scripts();
    }
    amrical_mem_debug('After generating listing');
    return $thecal;
}
Example #2
0
function amr_cache_url($url, $cache = ICAL_EVENTS_CACHE_TTL)
{
    global $amr_lastcache;
    global $amr_globaltz;
    global $amr_options;
    $text = '';
    // if any args are sent then all must be sent - use wp defaults more or less
    // so better to use filters
    add_filter('http_request_timeout', 'amr_set_http_timeout');
    //add_filter( 'http_request_redirection_count', 'amr_' );
    //'httpversion' => apply_filters( 'http_request_version', '1.0' ),  //or 1.1
    /*
    	curl_setopt($c, CURLOPT_RETURNTRANSFER, true);  // just says to return rather than echo
    	curl_setopt($c, CURLOPT_USERAGENT, 'PHP/'.PHP_VERSION);
    	curl_setopt($c, CURLOPT_ENCODING, '');
    	if( strstr( $resource, 'https' ) !== FALSE ) {
    		curl_setopt($c, CURLOPT_SSLVERSION, 3);
    		curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    		curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
    	}
    	curl_setopt($c, CURLOPT_COOKIESESSION, true);
    	curl_setopt($c, CURLOPT_HEADER, true);
    	if( !ini_get('safe_mode') ){
    		curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
    	}
    */
    //		If (ICAL_EVENTS_DEBUG) echo '<hr />url before decode: '.$url.'<br />';
    $url = html_entity_decode($url);
    //		If (ICAL_EVENTS_DEBUG) echo '<br />url decoded: '.$url.'<hr />';
    $cachedfile = get_cache_file($url);
    if (file_exists($cachedfile)) {
        $c = filemtime($cachedfile);
        if ($c) {
            $amr_lastcache = amr_newDateTime(strftime('%c', $c));
        } else {
            $amr_lastcache = '';
        }
    } else {
        $c = false;
        $amr_lastcache = amr_newDateTime(strftime('%c', 0));
    }
    // must we refresh ?
    if (isset($_REQUEST['nocache']) or isset($_REQUEST['refresh']) or !file_exists($cachedfile) or time() - $c >= $cache * 60 * 60) {
        if (ICAL_EVENTS_DEBUG) {
            echo '<br>Get ical file remotely, it is time to refresh or it is not cached: <br />';
        }
        amrical_mem_debug('We are going to refresh next');
        //$url = urlencode($u);  - do NOT encode - that gives an invalid URL response
        $check = wp_remote_get($url);
        // if use args, must set all - rather use filters and let wp do its thing
        if (is_wp_error($check) or isset($check['response']['code']) and !($check['response']['code'] == 200) or isset($check[0]) and preg_match('#404#', $check[0])) {
            /* is this bit still meaningful or needed ? */
            if (ICAL_EVENTS_DEBUG) {
                echo '<hr /><b>Http request failed </b><br /> Dumping response: ';
                var_dump($check);
            }
            if (is_wp_error($check)) {
                $text = '<br />' . $check->get_error_message() . '</br>';
            } else {
                $text = '';
            }
            $data = false;
        } elseif (!stristr($check['headers']['content-type'], 'text/calendar')) {
            if (amr_check_start_of_file($data = $check['body'])) {
                // well wrong content type, but has the content!! - bad calendar provider
                $data = $check['body'];
                if (current_user_can('manage_options')) {
                    echo '<br />This message is only shown to the administrator, and only when we refresh the file.';
                    echo '<br />The ics url given is issuing an incorrect content type of text/html.' . ' It should be text/calendar. ' . ' Luckily we persevere and check if the content looks like an ics file.' . 'Please inform the provider of the url. ' . 'Their urls may not be recognised by browsers as ics files. <br />';
                }
            } else {
                if (ICAL_EVENTS_DEBUG) {
                    echo '<br />The url given is not returning a calendar file';
                    echo '<br />The response was ';
                    var_dump($check['response']);
                    echo '<br />The content type is ' . $check['headers']['content-type'];
                    echo '<br />The content type of an ics file should be text/calendar. <br />';
                }
                $data = false;
            }
        } else {
            $data = $check['body'];
        }
        // from the http request
        if (!amr_check_start_of_file($data)) {
            $text .= '&nbsp;' . sprintf(__('Error getting calendar file with htpp or curl %s', 'amr-ical-events-list'), $url);
            if (file_exists($cachedfile)) {
                // Try use cached file if it exists
                if (is_object($amr_lastcache)) {
                    $text .= '&nbsp;...' . sprintf(__('Using File last cached at %s', 'amr-ical-events-list'), $amr_lastcache->format('D c'));
                } else {
                    $text .= '&nbsp;...' . __('File last cached time not available', 'amr-ical-events-list');
                }
                echo '<a class="error" href="#" title="' . __('Warning: Events may be out of date. ', 'amr-ical-events-list') . $text . '">!</a>';
                return $cachedfile;
                //return file not data
            } else {
                echo '<a class="error" href="#" title="' . __('No cached ical file for events', 'amr-ical-events-list') . $text . '">!</a>';
                return false;
            }
            return false;
        } else {
            if (ICAL_EVENTS_DEBUG) {
                echo '<br />We have vcalendar in start of file';
            }
        }
        // somebody wanted to pre process ics files that were not well generated?
        // A filter could be added here, but I'm not keen - could add to support load?
        if ($data) {
            /* now save it as a cached file */
            $data = apply_filters('amr-ics-filter', $data, $url);
            if ($dest = fopen($cachedfile, 'w')) {
                if (!fwrite($dest, $data)) {
                    die('Error writing cache file' . $dest);
                }
                fclose($dest);
                $amr_lastcache = amr_newDateTime(date('Y-m-d H:i:s'));
            } else {
                echo '<br />Error opening or creating the cached file <br />' . $cachedfile;
                return false;
            }
        } else {
            echo '<br>Error opening remote file for refresh ' . $url;
            return false;
        }
        if (!isset($amr_lastcache)) {
            $amr_lastcache = amr_newDateTime(date('Y-m-d H:i:s'));
        }
    } else {
    }
    // no need to refresh, use the cached file
    return $cachedfile;
}