function my_calendar_grab_events($from, $to, $category = null, $ltype = '', $lvalue = '', $source = 'calendar', $author = null, $host = null, $holidays = null, $search = '')
{
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    if ($holidays === null) {
        if (isset($_GET['mcat'])) {
            $ccategory = $_GET['mcat'];
        } else {
            $ccategory = $category;
        }
    } else {
        $ccategory = $category;
    }
    if (isset($_GET['ltype'])) {
        $cltype = $_GET['ltype'];
    } else {
        $cltype = $ltype;
    }
    if (isset($_GET['loc'])) {
        $clvalue = $_GET['loc'];
    } else {
        $clvalue = $lvalue;
    }
    if (isset($_GET['mc_auth'])) {
        $clauth = $_GET['mc_auth'];
    } else {
        $clauth = $author;
    }
    if (isset($_GET['mc_host'])) {
        $clhost = $_GET['mc_host'];
    } else {
        $clhost = $host;
    }
    if ($ccategory == '') {
        $ccategory = 'all';
    }
    if ($clvalue == '') {
        $clvalue = 'all';
    }
    if ($cltype == '') {
        $cltype = 'all';
    }
    if ($clvalue == 'all') {
        $cltype = 'all';
    }
    if ($clauth == '') {
        $clauth = 'all';
    }
    if ($clhost == '') {
        $clhost = 'all';
    }
    if (!mc_checkdate($from) || !mc_checkdate($to)) {
        return array();
    }
    // not valid dates
    $caching = apply_filters('mc_caching_enabled', false, $ccategory, $ltype, $lvalue, $author, $host);
    $hash = md5($from . $to . $ccategory . $cltype . $clvalue . $clauth . $clhost);
    if ($source != 'upcoming') {
        // no caching on upcoming events by days widgets or lists
        if ($caching) {
            $output = mc_check_cache($ccategory, $cltype, $clvalue, $clauth, $clhost, $hash);
            if ($output && $output != 'empty') {
                return $output;
            }
            if ($output == 'empty') {
                return array();
            }
        }
    }
    $select_category = $ccategory != 'all' ? mc_select_category($ccategory) : '';
    $select_author = $clauth != 'all' ? mc_select_author($clauth) : '';
    $select_host = $clhost != 'all' ? mc_select_host($clhost) : '';
    $select_location = mc_limit_string('grab', $cltype, $clvalue);
    if ($caching && $source != 'upcoming') {
        $select_category = '';
        $select_location = '';
        $select_author = '';
        $select_host = '';
    }
    // if caching, then need all categories/locations in cache. UNLESS this is an upcoming events list
    $arr_events = array();
    $limit_string = "event_flagged <> 1 AND event_approved = 1";
    $search = mc_prepare_search_query($search);
    $event_query = "SELECT *, UNIX_TIMESTAMP(occur_begin) AS ts_occur_begin, UNIX_TIMESTAMP(occur_end) AS ts_occur_end\n\t\t\t\t\tFROM " . MY_CALENDAR_EVENTS_TABLE . " \n\t\t\t\t\tJOIN " . MY_CALENDAR_TABLE . "\n\t\t\t\t\tON (event_id=occur_event_id) \t\t\t\t\t\n\t\t\t\t\tJOIN " . MY_CALENDAR_CATEGORIES_TABLE . " \n\t\t\t\t\tON (event_category=category_id) \n\t\t\t\t\tWHERE {$select_category} {$select_location} {$select_author} {$select_host} {$limit_string} {$search} \n\t\t\t\t\tAND ( DATE(occur_begin) BETWEEN '{$from} 00:00:00' AND '{$to} 23:59:59' \n\t\t\t\t\t\tOR DATE(occur_end) BETWEEN '{$from} 00:00:00' AND '{$to} 23:59:59' \n\t\t\t\t\t\tOR ( DATE('{$from}') BETWEEN DATE(occur_begin) AND DATE(occur_end) ) \n\t\t\t\t\t\tOR ( DATE('{$to}') BETWEEN DATE(occur_begin) AND DATE(occur_end) ) ) \n\t\t\t\t\tORDER BY " . apply_filters('mc_primary_sort', 'occur_begin') . ", " . apply_filters('mc_secondary_sort', 'event_title ASC');
    $events = $mcdb->get_results($event_query);
    if (!empty($events)) {
        foreach (array_keys($events) as $key) {
            $event =& $events[$key];
            $arr_events[] = $event;
        }
    }
    if ($source != 'upcoming' && $caching) {
        $new_cache = mc_create_cache($arr_events, $hash, $category, $ltype, $lvalue, $author, $host);
        if ($new_cache) {
            $output = mc_check_cache($ccategory, $cltype, $clvalue, $clauth, $clhost, $hash);
            return $output;
        } else {
            // need to clean cache if the cache is maxed.
            return mc_clean_cache($arr_events, $ccategory, $cltype, $clvalue, $clauth, $clhost);
        }
    } else {
        return $arr_events;
    }
}
function mc_check_data($action, $post, $i)
{
    $post = apply_filters('mc_pre_checkdata', $post, $action, $i);
    global $wpdb, $current_user, $users_entries;
    $mcdb = $wpdb;
    $submit = array();
    $errors = '';
    $every = $recur = $events_access = $begin = $end = $short = $time = $endtime = $event_label = $event_street = $event_street2 = $event_city = $event_state = $event_postcode = $event_region = $event_country = $event_url = $event_image = $event_phone = $event_phone2 = $event_access = $event_tickets = $event_registration = $event_author = $category = $expires = $event_zoom = $event_open = $event_group = $approved = $host = $event_fifth_week = $event_holiday = $event_group_id = $event_span = $event_hide_end = $event_longitude = $event_latitude = '';
    if (get_magic_quotes_gpc()) {
        $post = array_map('stripslashes_deep', $post);
    }
    if (!wp_verify_nonce($post['event_nonce_name'], 'event_nonce')) {
        return array();
    }
    if ($action == 'add' || $action == 'edit' || $action == 'copy') {
        $title = !empty($post['event_title']) ? trim($post['event_title']) : '';
        $desc = !empty($post['content']) ? trim($post['content']) : '';
        $short = !empty($post['event_short']) ? trim($post['event_short']) : '';
        $recur = !empty($post['event_recur']) ? trim($post['event_recur']) : '';
        $every = !empty($post['event_every']) ? (int) $post['event_every'] : 1;
        // if this is an all weekdays event, and it's been scheduled to start on a weekend, the math gets nasty.
        // ...AND there's no reason to allow it, since weekday events will NEVER happen on the weekend.
        $begin = trim($post['event_begin'][$i]);
        $end = !empty($post['event_end']) ? trim($post['event_end'][$i]) : $post['event_begin'][$i];
        if ($recur == 'E' && (date('w', strtotime($begin)) == 0 || date('w', strtotime($begin)) == 6)) {
            if (date('w', strtotime($begin)) == 0) {
                $newbegin = my_calendar_add_date($begin, 1);
                if (!empty($post['event_end'][$i])) {
                    $newend = my_calendar_add_date($end, 1);
                } else {
                    $newend = $newbegin;
                }
            } else {
                if (date('w', strtotime($begin)) == 6) {
                    $newbegin = my_calendar_add_date($begin, 2);
                    if (!empty($post['event_end'][$i])) {
                        $newend = my_calendar_add_date($end, 2);
                    } else {
                        $newend = $newbegin;
                    }
                }
            }
            $begin = $newbegin;
            $end = $newend;
        } else {
            $begin = !empty($post['event_begin'][$i]) ? trim($post['event_begin'][$i]) : '';
            $end = !empty($post['event_end'][$i]) ? trim($post['event_end'][$i]) : $begin;
        }
        $begin = date('Y-m-d', strtotime($begin));
        // regardless of entry format, convert.
        $time = !empty($post['event_time'][$i]) ? trim($post['event_time'][$i]) : '';
        if ($time != '') {
            $endtime = !empty($post['event_endtime'][$i]) ? trim($post['event_endtime'][$i]) : date('H:i:s', strtotime($time . ' +1 hour'));
        } else {
            $endtime = !empty($post['event_endtime'][$i]) ? trim($post['event_endtime'][$i]) : '';
        }
        $time = $time == '' || $time == '00:00:00' ? '00:00:00' : $time;
        // set at midnight if not provided
        $endtime = $endtime == '' && $time == '00:00:00' ? '23:59:59' : $endtime;
        // set at end of night if np
        // prevent setting enddate to incorrect value on copy.
        if (strtotime($end) < strtotime($begin) && $action == 'copy') {
            $end = date('Y-m-d', strtotime($begin) + (strtotime($post['prev_event_end']) - strtotime($post['prev_event_begin'])));
        }
        if (isset($post['event_allday']) && (int) $post['event_allday'] !== 0) {
            $time = '00:00:00';
            $endtime = '23:59:59';
        }
        // verify formats
        $time = date('H:i:s', strtotime($time));
        $endtime = date('H:i:s', strtotime($endtime));
        $end = date('Y-m-d', strtotime($end));
        // regardless of entry format, convert.
        $repeats = isset($post['event_repeats']) ? trim($post['event_repeats']) : 0;
        $host = !empty($post['event_host']) ? $post['event_host'] : $current_user->ID;
        $category = isset($post['event_category']) && is_numeric($post['event_category']) ? $post['event_category'] : 1;
        $event_author = isset($post['event_author']) && is_numeric($post['event_author']) ? $post['event_author'] : 0;
        $event_link = !empty($post['event_link']) ? trim($post['event_link']) : '';
        $expires = !empty($post['event_link_expires']) ? $post['event_link_expires'] : '0';
        $approved = !empty($post['event_approved']) ? $post['event_approved'] : '0';
        $location_preset = !empty($post['location_preset']) ? $post['location_preset'] : '';
        $event_open = isset($post['event_open']) && $post['event_open'] !== 0 ? $post['event_open'] : '2';
        $event_tickets = isset($post['event_tickets']) ? trim($post['event_tickets']) : '';
        $event_registration = isset($post['event_registration']) ? trim($post['event_registration']) : '';
        $event_group = !empty($post['event_group']) ? 1 : 0;
        $event_image = isset($post['event_image']) ? esc_url_raw($post['event_image']) : '';
        $event_fifth_week = !empty($post['event_fifth_week']) ? 1 : 0;
        $event_holiday = !empty($post['event_holiday']) ? 1 : 0;
        // get group id: if multiple events submitted, auto group OR if event being submitted is already part of a group; otherwise zero.
        $group_id_submitted = (int) $post['event_group_id'];
        $event_group_id = is_array($post['event_begin']) && count($post['event_begin']) > 1 || mc_event_is_grouped($group_id_submitted) ? $group_id_submitted : 0;
        $event_span = !empty($post['event_span']) && $event_group_id != 0 ? 1 : 0;
        $event_hide_end = !empty($post['event_hide_end']) ? (int) $post['event_hide_end'] : 0;
        $event_hide_end = $time == '' || $time == '23:59:59' ? 1 : $event_hide_end;
        // hide end time automatically on all day events
        // set location
        if ($location_preset != 'none' && is_numeric($location_preset)) {
            $sql = "SELECT * FROM " . my_calendar_locations_table() . " WHERE location_id = {$location_preset}";
            $location = $mcdb->get_row($sql);
            $event_label = $location->location_label;
            $event_street = $location->location_street;
            $event_street2 = $location->location_street2;
            $event_city = $location->location_city;
            $event_state = $location->location_state;
            $event_postcode = $location->location_postcode;
            $event_region = $location->location_region;
            $event_country = $location->location_country;
            $event_url = $location->location_url;
            $event_longitude = $location->location_longitude;
            $event_latitude = $location->location_latitude;
            $event_zoom = $location->location_zoom;
            $event_phone = $location->location_phone;
            $event_phone2 = $location->location_phone2;
            $event_access = $location->location_access;
        } else {
            $event_label = !empty($post['event_label']) ? $post['event_label'] : '';
            $event_street = !empty($post['event_street']) ? $post['event_street'] : '';
            $event_street2 = !empty($post['event_street2']) ? $post['event_street2'] : '';
            $event_city = !empty($post['event_city']) ? $post['event_city'] : '';
            $event_state = !empty($post['event_state']) ? $post['event_state'] : '';
            $event_postcode = !empty($post['event_postcode']) ? $post['event_postcode'] : '';
            $event_region = !empty($post['event_region']) ? $post['event_region'] : '';
            $event_country = !empty($post['event_country']) ? $post['event_country'] : '';
            $event_url = !empty($post['event_url']) ? $post['event_url'] : '';
            $event_longitude = !empty($post['event_longitude']) ? $post['event_longitude'] : '';
            $event_latitude = !empty($post['event_latitude']) ? $post['event_latitude'] : '';
            $event_zoom = !empty($post['event_zoom']) ? $post['event_zoom'] : '';
            $event_phone = !empty($post['event_phone']) ? $post['event_phone'] : '';
            $event_phone2 = !empty($post['event_phone2']) ? $post['event_phone2'] : '';
            $event_access = !empty($post['event_access']) ? $post['event_access'] : '';
            $event_access = !empty($post['event_access_hidden']) ? unserialize($post['event_access_hidden']) : $event_access;
            if (isset($post['mc_copy_location']) && $post['mc_copy_location'] == 'on' && $i == 0) {
                // only the first event, if adding multiples.
                $add_loc = array('location_label' => $event_label, 'location_street' => $event_street, 'location_street2' => $event_street2, 'location_city' => $event_city, 'location_state' => $event_state, 'location_postcode' => $event_postcode, 'location_region' => $event_region, 'location_country' => $event_country, 'location_url' => $event_url, 'location_longitude' => $event_longitude, 'location_latitude' => $event_latitude, 'location_zoom' => $event_zoom, 'location_phone' => $event_phone, 'location_phone2' => $event_phone2, 'location_access' => is_array($event_access) ? serialize($event_access) : '');
                $add_loc = array_map('mc_kses_post', $add_loc);
                $loc_formats = array('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%f', '%f', '%d', '%s', '%s', '%s');
                $mcdb->insert(my_calendar_locations_table(), $add_loc, $loc_formats);
            }
        }
        // Perform validation on the submitted dates - checks for valid years and months
        if (mc_checkdate($begin) && mc_checkdate($end)) {
            // Make sure dates are equal or end date is later than start date
            if (strtotime("{$end} {$endtime}") < strtotime("{$begin} {$time}")) {
                $errors .= "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('Your event end date must be either after or the same as your event begin date', 'my-calendar') . "</p></div>";
            }
        } else {
            $errors .= "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('Your date formatting is correct but one or more of your dates is invalid. Check for number of days in month and leap year related errors.', 'my-calendar') . "</p></div>";
        }
        // We check for a valid time, or an empty one
        $time = $time == '' ? '23:59:59' : date('H:i:00', strtotime($time));
        $time_format_one = '/^([0-1][0-9]):([0-5][0-9]):([0-5][0-9])$/';
        $time_format_two = '/^([2][0-3]):([0-5][0-9]):([0-5][0-9])$/';
        if (preg_match($time_format_one, $time) || preg_match($time_format_two, $time) || $time == '') {
        } else {
            $errors .= "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('The time field must either be blank or be entered in the format hh:mm am/pm', 'my-calendar') . "</p></div>";
        }
        // We check for a valid end time, or an empty one
        if (preg_match($time_format_one, $endtime) || preg_match($time_format_two, $endtime) || $endtime == '') {
        } else {
            $errors .= "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('The end time field must either be blank or be entered in the format hh:mm am/pm', 'my-calendar') . "</p></div>";
        }
        // We check to make sure the URL is acceptable (blank or starting with http://)
        if (!($event_link == '' || preg_match('/^(http)(s?)(:)\\/\\//', $event_link))) {
            $event_link = "http://" . $event_link;
        }
    }
    // A title is required, and can't be more than 255 characters.
    $title_length = strlen($title);
    if (!($title_length >= 1 && $title_length <= 255)) {
        $title = __('Untitled Event', 'my-calendar');
    }
    // Run checks on recurrence profile
    if ($repeats == 0 && $recur == 'S' || $repeats >= 0 && ($recur == 'W' || $recur == 'B' || $recur == 'M' || $recur == 'U' || $recur == 'Y' || $recur == 'D' || $recur == 'E')) {
        $recur = $recur . $every;
    } else {
        // if it's not valid, assign a default value.
        $repeats = 0;
        $recur = 'S1';
    }
    if (function_exists('mcs_submissions') && isset($post['mcs_check_conflicts'])) {
        $conflicts = mcs_check_conflicts($begin, $time, $end, $endtime, $event_label);
        if ($conflicts) {
            $errors .= "<div class='error'><p><strong>" . __('Error', 'my-calendar') . ":</strong> " . __('That event conflicts with a previously scheduled event.', 'my-calendar') . "</p></div>";
        }
    }
    $spam = mc_spam($event_link, $desc, $post);
    // the likelihood that something will be both flagged as spam and have a zero start time
    // and yet be legitimate is crazy minimal. Just kill it.
    if ($spam == 1 && $begin == '1970-01-01') {
        die;
    }
    if ($errors == '') {
        $current_user = wp_get_current_user();
        $event_author = $event_author == $current_user->ID || current_user_can('mc_manage_events') ? $event_author : $current_user->ID;
        $event_category = !$category ? 1 : $category;
        $ok = true;
        $submit = array('event_begin' => $begin, 'event_end' => $end, 'event_title' => $title, 'event_desc' => force_balance_tags($desc), 'event_short' => force_balance_tags($short), 'event_time' => $time, 'event_endtime' => $endtime, 'event_link' => $event_link, 'event_label' => $event_label, 'event_street' => $event_street, 'event_street2' => $event_street2, 'event_city' => $event_city, 'event_state' => $event_state, 'event_postcode' => $event_postcode, 'event_region' => $event_region, 'event_country' => $event_country, 'event_url' => $event_url, 'event_recur' => $recur, 'event_image' => $event_image, 'event_phone' => $event_phone, 'event_phone2' => $event_phone2, 'event_access' => is_array($event_access) ? serialize($event_access) : '', 'event_tickets' => $event_tickets, 'event_registration' => $event_registration, 'event_repeats' => $repeats, 'event_author' => $event_author, 'event_category' => $event_category, 'event_link_expires' => $expires, 'event_zoom' => $event_zoom, 'event_open' => $event_open, 'event_group' => $event_group, 'event_approved' => $approved, 'event_host' => $host, 'event_flagged' => $spam, 'event_fifth_week' => $event_fifth_week, 'event_holiday' => $event_holiday, 'event_group_id' => $event_group_id, 'event_span' => $event_span, 'event_hide_end' => $event_hide_end, 'event_longitude' => $event_longitude, 'event_latitude' => $event_latitude);
        $submit = array_map('mc_kses_post', $submit);
    } else {
        $ok = false;
        $event_access = is_array($event_access) ? serialize($event_access) : '';
        // The form is going to be rejected due to field validation issues, so we preserve the users entries here
        // all submitted data should be in this object, regardless of data destination.
        $users_entries = !is_object($users_entries) ? new stdClass() : $users_entries;
        $users_entries->event_id = isset($_GET['event_id']) && is_numeric($_GET['event_id']) ? $_GET['event_id'] : false;
        $users_entries->event_title = $title;
        $users_entries->event_desc = $desc;
        $users_entries->event_begin = $begin;
        $users_entries->event_end = $end;
        $users_entries->event_time = $time;
        $users_entries->event_endtime = $endtime;
        $users_entries->event_recur = $recur;
        $users_entries->event_repeats = $repeats;
        $users_entries->event_host = $host;
        $users_entries->event_category = $category;
        $users_entries->event_link = $event_link;
        $users_entries->event_link_expires = $expires;
        $users_entries->event_label = $event_label;
        $users_entries->event_street = $event_street;
        $users_entries->event_street2 = $event_street2;
        $users_entries->event_city = $event_city;
        $users_entries->event_state = $event_state;
        $users_entries->event_postcode = $event_postcode;
        $users_entries->event_country = $event_country;
        $users_entries->event_region = $event_region;
        $users_entries->event_url = $event_url;
        $users_entries->event_longitude = $event_longitude;
        $users_entries->event_latitude = $event_latitude;
        $users_entries->event_zoom = $event_zoom;
        $users_entries->event_phone = $event_phone;
        $users_entries->event_phone2 = $event_phone2;
        $users_entries->event_author = $event_author;
        $users_entries->event_open = $event_open;
        $users_entries->event_short = $short;
        $users_entries->event_group = $event_group;
        $users_entries->event_approved = $approved;
        $users_entries->event_image = $event_image;
        $users_entries->event_fifth_week = $event_fifth_week;
        $users_entries->event_holiday = $event_holiday;
        $users_entries->event_flagged = 0;
        $users_entries->event_group_id = $event_group_id;
        $users_entries->event_span = $event_span;
        $users_entries->event_hide_end = $event_hide_end;
        $users_entries->event_access = $event_access;
        $users_entries->events_access = serialize($events_access);
        $users_entries->event_tickets = $event_tickets;
        $users_entries->event_registration = $event_registration;
    }
    $data = array($ok, $users_entries, $submit, $errors);
    return $data;
}