function mc_event_date_span($group_id, $event_span, $dates = array())
{
    global $wpdb;
    $mcdb = $wpdb;
    // cache as transient to save db queries.
    if (get_transient('mc_event_date_span_' . $group_id . '_' . $event_span)) {
        return get_transient('mc_event_date_span_' . $group_id . '_' . $event_span);
    }
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $group_id = (int) $group_id;
    if ($group_id == 0 && $event_span != 1) {
        return $dates;
    } else {
        $sql = "SELECT occur_begin, occur_end FROM " . my_calendar_event_table() . " WHERE occur_group_id = {$group_id} ORDER BY occur_begin ASC";
        $dates = $mcdb->get_results($sql);
        set_transient('mc_event_date_span_' . $group_id . '_' . $event_span, $dates, HOUR_IN_SECONDS);
        return $dates;
    }
}
function mc_location_data($field, $id)
{
    if ($id) {
        global $wpdb;
        $mcdb = $wpdb;
        if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
            $mcdb = mc_remote_db();
        }
        $field = esc_sql($field);
        $sql = $wpdb->prepare("SELECT {$field} FROM " . my_calendar_locations_table() . " WHERE location_id = %d", $id);
        $result = $mcdb->get_var($sql);
        return $result;
    }
}
function my_calendar_locations_list($show = 'list', $type = 'saved', $datatype = 'name', $group = 'single')
{
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $output = '';
    if (isset($_GET['mc_id'])) {
        return;
    }
    if ($type == 'saved') {
        switch ($datatype) {
            case "name":
                $data = "location_label";
                break;
            case "city":
                $data = "location_city";
                break;
            case "state":
                $data = "location_state";
                break;
            case "zip":
                $data = "location_postcode";
                break;
            case "country":
                $data = "location_country";
                break;
            case "region":
                $data = "location_region";
                break;
            default:
                $data = "location_label";
        }
    } else {
        $data = $datatype;
    }
    $current_url = mc_get_current_url();
    if ($type == 'saved') {
        $locations = $mcdb->get_results("SELECT DISTINCT {$data} FROM " . MY_CALENDAR_LOCATIONS_TABLE . " ORDER BY {$data} ASC", ARRAY_A);
    } else {
        $data = get_option('mc_user_settings');
        $locations = $data['my_calendar_location_default']['values'];
        $datatype = str_replace('event_', '', get_option('mc_location_type'));
        $datatype = $datatype == 'label' ? 'name' : $datatype;
        $datatype = $datatype == 'postcode' ? 'zip' : $datatype;
    }
    if (count($locations) > 1) {
        if ($show == 'list') {
            $url = mc_build_url(array('loc' => 'all', 'ltype' => 'all'), array());
            $output .= "<ul id='mc-locations-list'>\n\t\t\t<li><a href='{$url}'>" . __('Show all', 'my-calendar') . "</a></li>\n";
        } else {
            $ltype = !isset($_GET['ltype']) ? $datatype : $_GET['ltype'];
            $output .= "<div id='mc_locations'>";
            $output .= $group == 'single' ? "\n\t\t<form action='" . $current_url . "' method='get'>\n\t\t<div>" : '';
            $output .= "<input type='hidden' name='ltype' value='{$ltype}' />";
            if ($group == 'single') {
                $qsa = array();
                parse_str($_SERVER['QUERY_STRING'], $qsa);
                if (!isset($_GET['cid'])) {
                    $output .= '<input type="hidden" name="cid" value="all" />';
                }
                foreach ($qsa as $name => $argument) {
                    $name = esc_attr(strip_tags($name));
                    $argument = esc_attr(strip_tags($argument));
                    if ($name != 'loc' && $name != 'ltype') {
                        $output .= "\n\t\t" . '<input type="hidden" name="' . $name . '" value="' . $argument . '" />';
                    }
                }
            }
            $output .= "\n\t\t\t<label for='mc-locations-list'>" . __('Location', 'my-calendar') . "</label>\n\t\t\t<select name='loc' id='mc-locations-list'>\n\t\t\t<option value='all'>" . __('Show all', 'my-calendar') . "</option>\n";
        }
        foreach ($locations as $key => $location) {
            if ($type == 'saved') {
                foreach ($location as $key => $value) {
                    $vt = urlencode(trim($value));
                    $value = stripcslashes($value);
                    if ($value == '') {
                        continue;
                    }
                    if (empty($_GET['loc'])) {
                        $loc = '';
                    } else {
                        $loc = $_GET['loc'];
                    }
                    if ($show == 'list') {
                        $selected = $vt == $loc ? " class='selected'" : '';
                        $this_url = mc_build_url(array('loc' => $vt, 'ltype' => $datatype), array());
                        $output .= "\t\t\t<li{$selected}><a rel='nofollow' href='{$this_url}'>{$value}</a></li>\n";
                    } else {
                        $selected = $vt == $loc ? " selected='selected'" : '';
                        if ($value != '') {
                            $output .= "\t\t\t<option value='{$vt}'{$selected}>{$value}</option>\n";
                        }
                    }
                }
            } else {
                $vk = urlencode(trim($key));
                $location = trim($location);
                if ($location == '') {
                    continue;
                }
                if ($show == 'list') {
                    $selected = $vk == $_GET['loc'] ? " class='selected'" : '';
                    $this_url = mc_build_url(array('loc' => $vk, 'ltype' => $datatype), array());
                    $output .= "\t\t\t<li{$selected}><a rel='nofollow' href='{$this_url}'>{$location}</a></li>\n";
                } else {
                    $selected = $vk == $_GET['loc'] ? " selected='selected'" : '';
                    $output .= "\t\t\t<option value='{$vk}'{$selected}>{$location}</option>\n";
                }
            }
        }
        if ($show == 'list') {
            $output .= "</ul>";
        } else {
            $output .= "</select>";
            $output .= $group == 'single' ? "<input type='submit' value=" . __('Submit', 'my-calendar') . " />\n\t\t\t\t\t</div>\n\t\t\t\t</form>" : '';
            $output .= "\n\t\t\t</div>";
        }
        $output = apply_filters('mc_location_selector', $output, $locations);
        return $output;
    } else {
        return;
    }
}
function mc_location_select($location = false)
{
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    // Grab all locations and list them
    $list = '';
    $sql = "SELECT * FROM " . my_calendar_locations_table() . " ORDER BY location_label ASC";
    $locs = $mcdb->get_results($sql);
    foreach ($locs as $loc) {
        $l = '<option value="' . $loc->location_id . '"';
        if ($location) {
            if ($location == $loc->location_id) {
                $l .= ' selected="selected"';
            }
        }
        $l .= '>' . stripslashes($loc->location_label) . '</option>';
        $list .= $l;
    }
    return $list;
}
function my_calendar_grab_events($from, $to, $category = null, $ltype = '', $lvalue = '', $source = 'calendar', $author = null, $host = null, $holidays = null)
{
    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;
    }
    // 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;
            }
        }
    }
    $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";
    $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}  \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\t\tORDER BY 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_get_db_type()
{
    global $wpdb;
    $mcdb = $wpdb;
    $db_type = 'MyISAM';
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $dbs = $mcdb->get_results('SHOW TABLE STATUS');
    foreach ($dbs as $db) {
        if ($db->Name == MY_CALENDAR_TABLE) {
            $db_type = $db->Engine;
        }
    }
    return $db_type;
}
function mc_span_time($group_id)
{
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $group_id = (int) $group_id;
    $sql = "SELECT event_begin, event_time, event_end, event_endtime FROM " . my_calendar_table() . " WHERE event_group_id = {$group_id} ORDER BY event_begin ASC";
    $dates = $mcdb->get_results($sql);
    $count = count($dates);
    $last = $count - 1;
    $begin = $dates[0]->event_begin . ' ' . $dates[0]->event_time;
    $end = $dates[$last]->event_end . ' ' . $dates[$last]->event_endtime;
    return array($begin, $end);
}
function mc_select_category($category, $type = 'event', $group = 'events')
{
    $category = urldecode($category);
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $select_category = '';
    $data = $group == 'category' ? 'category_id' : 'event_category';
    if (preg_match('/^all$|^all,|,all$|,all,/i', $category) > 0) {
        return '';
    } else {
        if (strpos($category, "|") || strpos($category, ",")) {
            if (strpos($category, "|")) {
                $categories = explode("|", $category);
            } else {
                $categories = explode(",", $category);
            }
            $numcat = count($categories);
            $i = 1;
            foreach ($categories as $key) {
                if (is_numeric($key)) {
                    $key = (int) $key;
                    if ($i == 1) {
                        $select_category .= $type == 'all' ? " WHERE (" : ' (';
                    }
                    $select_category .= " {$data} = {$key}";
                    if ($i < $numcat) {
                        $select_category .= " OR ";
                    } else {
                        if ($i == $numcat) {
                            $select_category .= $type == 'all' ? ") " : ' ) AND';
                        }
                    }
                    $i++;
                } else {
                    $key = esc_sql(trim($key));
                    $cat = $mcdb->get_row("SELECT category_id FROM " . my_calendar_categories_table() . " WHERE category_name = '{$key}'");
                    if ($cat) {
                        $category_id = $cat->category_id;
                        if ($i == 1) {
                            $select_category .= $type == 'all' ? " WHERE (" : ' (';
                        }
                        $select_category .= " {$data} = {$category_id}";
                        if ($i < $numcat) {
                            $select_category .= " OR ";
                        } else {
                            if ($i == $numcat) {
                                $select_category .= $type == 'all' ? ") " : ' ) AND';
                            }
                        }
                        $i++;
                    } else {
                        return '';
                    }
                }
            }
        } else {
            if (is_numeric($category)) {
                $select_category = $type == 'all' ? " WHERE {$data} = {$category}" : " event_category = {$category} AND";
            } else {
                $cat = $mcdb->get_row("SELECT category_id FROM " . my_calendar_categories_table() . " WHERE category_name = '{$category}'");
                if (is_object($cat)) {
                    $category_id = $cat->category_id;
                    $select_category = $type == 'all' ? " WHERE {$data} = {$category_id}" : " {$data} = {$category_id} AND";
                } else {
                    $select_category = '';
                }
            }
        }
        return $select_category;
    }
}
function mc_get_db_type()
{
    global $wpdb;
    $mcdb = $wpdb;
    $db_type = 'MyISAM';
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $my_calendar = my_calendar_table();
    $dbs = $mcdb->get_results("SHOW TABLE STATUS WHERE name='{$my_calendar}'");
    foreach ($dbs as $db) {
        if ($db->Name == my_calendar_table()) {
            $db_type = $db->Engine;
        }
    }
    return $db_type;
}
function my_calendar_todays_events($category = 'default', $template = 'default', $substitute = '', $author = 'all', $host = 'all')
{
    $caching = apply_filters('mc_cache_enabled', false);
    $todays_cache = $caching ? get_transient('mc_todays_cache') : '';
    if ($caching && is_array($todays_cache) && @$todays_cache[$category]) {
        return @$todays_cache[$category];
    }
    global $wpdb, $default_template;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $output = '';
    $defaults = get_option('mc_widget_defaults');
    $template = $template == 'default' ? $defaults['today']['template'] : $template;
    if ($template == '') {
        $template = "{$default_template}";
    }
    $category = $category == 'default' ? $defaults['today']['category'] : $category;
    $no_event_text = $substitute == '' ? $defaults['today']['text'] : $substitute;
    $from = $to = date('Y-m-d', current_time('timestamp'));
    $events = my_calendar_events($from, $to, $category, '', '', 'upcoming', $author, $host);
    $today = isset($events[$from]) ? $events[$from] : false;
    $header = "<ul id='todays-events'>";
    $footer = "</ul>";
    $groups = $todays_events = array();
    // quick loop through all events today to check for holidays
    if (is_array($today)) {
        foreach ($today as $e) {
            if ($e->category_private == 1 && !is_user_logged_in()) {
            } else {
                if (!in_array($e->event_group_id, $groups)) {
                    $event_details = mc_create_tags($e);
                    $ts = $e->ts_occur_begin;
                    $date = date_i18n(apply_filters('mc_date_format', get_option('mc_date_format'), 'todays_events'), current_time('timestamp'));
                    if (get_option('mc_event_approve') == 'true') {
                        if ($e->event_approved != 0) {
                            $todays_events[$ts][] = "<li>" . jd_draw_template($event_details, $template) . "</li>";
                        }
                    } else {
                        $todays_events[$ts][] = "<li>" . jd_draw_template($event_details, $template) . "</li>";
                    }
                }
            }
        }
        $todays_events = apply_filters('mc_event_today', $todays_events, $events);
        foreach ($todays_events as $k => $t) {
            foreach ($t as $now) {
                $output .= $now;
            }
        }
        if (count($events) != 0) {
            $return = $header . $output . $footer;
        } else {
            $return = stripcslashes($no_event_text);
        }
        $time = strtotime(date('Y-m-d H:m:s', current_time('timestamp'))) - strtotime(date('Y-m-d', current_time('timestamp')));
        $time_remaining = 24 * 60 * 60 - $time;
        $todays_cache[$category] = $caching ? $return : '';
        if ($caching) {
            set_transient('mc_todays_cache', $todays_cache, $time_remaining);
        }
    } else {
        $return = stripcslashes($no_event_text);
    }
    return $return;
}
function mc_select_host($host, $type = 'event')
{
    $host = urldecode($host);
    $key = '';
    if ($host == '' || $host == 'all' || $host == 'default' || $host == null) {
        return;
    }
    global $wpdb;
    $mcdb = $wpdb;
    if (get_option('mc_remote') == 'true' && function_exists('mc_remote_db')) {
        $mcdb = mc_remote_db();
    }
    $select_author = '';
    $data = 'event_host';
    if (isset($_GET['mc_auth'])) {
        $host = $_GET['mc_host'];
    }
    if (preg_match('/^all$|^all,|,all$|,all,/i', $host) > 0) {
        return '';
    } else {
        if (strpos($host, "|") || strpos($host, ",")) {
            if (strpos($host, "|")) {
                $hosts = explode("|", $host);
            } else {
                $hosts = explode(",", $host);
            }
            $numhost = count($hosts);
            $i = 1;
            foreach ($hosts as $key) {
                if (is_numeric($key)) {
                    $key = (int) $key;
                    if ($i == 1) {
                        $select_host .= $type == 'all' ? " WHERE (" : ' (';
                    }
                    $select_host .= " {$data} = {$key}";
                    if ($i < $numhost) {
                        $select_host .= " OR ";
                    } else {
                        if ($i == $numhost) {
                            $select_host .= $type == 'all' ? ") " : ' ) AND';
                        }
                    }
                    $i++;
                } else {
                    $key = esc_sql(trim($key));
                    $host = get_user_by('login', $key);
                    // get host by username
                    $host_id = $host->ID;
                    if ($i == 1) {
                        $select_host .= $type == 'all' ? " WHERE (" : ' (';
                    }
                    $select_host .= " {$data} = {$host_id}";
                    if ($i < $numhost) {
                        $select_host .= " OR ";
                    } else {
                        if ($i == $numhost) {
                            $select_host .= $type == 'all' ? ") " : ' ) AND';
                        }
                    }
                    $i++;
                }
            }
        } else {
            if (is_numeric($host)) {
                $select_host = $type == 'all' ? " WHERE {$data} = {$host}" : " event_host = {$host} AND";
            } else {
                $host = esc_sql(trim($host));
                $host = get_user_by('login', $host);
                // get author by username
                if (is_object($host)) {
                    $host_id = $host->ID;
                    $select_host = $type == 'all' ? " WHERE {$data} = {$host_id}" : " {$data} = {$host_id} AND";
                } else {
                    $select_host = '';
                }
            }
        }
        return $select_host;
    }
}