function adrotate_group($group_ids, $fallback = 0, $weight = 0, $site = 0)
{
    global $wpdb, $adrotate_config, $adrotate_debug;
    $output = $group_select = $weightoverride = $mobileoverride = '';
    if ($group_ids) {
        $license = get_site_option('adrotate_activate');
        if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
            $current_blog = $wpdb->blogid;
            switch_to_blog($site);
        }
        $now = adrotate_now();
        $group_array = !is_array($group_ids) ? explode(",", $group_ids) : $group_ids;
        foreach ($group_array as $key => $value) {
            $group_select .= " `{$wpdb->prefix}adrotate_linkmeta`.`group` = {$value} OR";
        }
        $group_select = rtrim($group_select, " OR");
        // Grab settings to use from first group
        $group = $wpdb->get_row($wpdb->prepare("SELECT * FROM `{$wpdb->prefix}adrotate_groups` WHERE `name` != '' AND `id` = %d;", $group_array[0]));
        if ($adrotate_debug['general'] == true) {
            echo "<p><strong>[DEBUG][adrotate_group] Group</strong><pre>";
            echo "Group ID: " . $group->id . ', ' . $group->name;
            echo "<br />Group mobile: ";
            echo $group->mobile == 1 ? "yes" : "no";
            echo "<br />User is mobile: ";
            echo adrotate_is_mobile() ? "yes" : "no";
            echo "<br />User is tablet: ";
            echo adrotate_is_tablet() ? "yes" : "no";
            echo "</pre></p>";
        }
        if ($group) {
            if ($group->mobile == 1) {
                if (!adrotate_is_mobile() and !adrotate_is_tablet()) {
                    // Desktop
                    $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`desktop` = 'Y'";
                } else {
                    if (adrotate_is_mobile()) {
                        // Phones
                        $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`mobile` = 'Y'";
                    } else {
                        if (adrotate_is_tablet()) {
                            // Tablets
                            $mobileoverride = "AND `{$wpdb->prefix}adrotate`.`tablet` = 'Y'";
                        }
                    }
                }
            }
            $weightoverride = $weight > 0 ? "AND `{$wpdb->prefix}adrotate`.`weight` >= {$weight} " : '';
            $fallback = $fallback == 0 ? $group->fallback : $fallback;
            // Get all ads in all selected groups
            $ads = $wpdb->get_results("SELECT \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`id`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`title`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`bannercode`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`image`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`responsive`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`tracker`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`weight`,\n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`crate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`irate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`budget`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`cities`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate`.`countries`,\n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta`.`group`\n\t\t\t\tFROM \n\t\t\t\t\t`{$wpdb->prefix}adrotate`, \n\t\t\t\t\t`{$wpdb->prefix}adrotate_linkmeta` \n\t\t\t\tWHERE \n\t\t\t\t\t({$group_select}) \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate_linkmeta`.`user` = 0 \n\t\t\t\t\tAND `{$wpdb->prefix}adrotate`.`id` = `{$wpdb->prefix}adrotate_linkmeta`.`ad` \n\t\t\t\t\t{$mobileoverride}\n\t\t\t\t\t{$weightoverride}\n\t\t\t\t\tAND (`{$wpdb->prefix}adrotate`.`type` = 'active' \n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '2days' \n\t\t\t\t\t\tOR `{$wpdb->prefix}adrotate`.`type` = '7days') \n\t\t\t\tGROUP BY `{$wpdb->prefix}adrotate`.`id` \n\t\t\t\tORDER BY `{$wpdb->prefix}adrotate`.`id`;");
            if ($ads) {
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group()] Selected ads</strong><pre>";
                    foreach ($ads as $ad) {
                        print_r($ad->id . ', ' . $ad->title . '<br>');
                    }
                    echo "</pre></p>";
                }
                foreach ($ads as $ad) {
                    $selected[$ad->id] = $ad;
                    $selected = adrotate_filter_schedule($selected, $ad);
                    if ($adrotate_config['enable_advertisers'] == 'Y' and ($ad->crate > 0 or $ad->irate > 0)) {
                        $selected = adrotate_filter_budget($selected, $ad);
                    }
                    if ($adrotate_config['enable_geo'] > 0 and $group->geo == 1) {
                        $selected = adrotate_filter_location($selected, $ad);
                    }
                }
                if ($adrotate_debug['general'] == true) {
                    echo "<p><strong>[DEBUG][adrotate_group] Reduced array based on settings</strong><pre>";
                    print_r($selected);
                    echo "</pre></p>";
                }
                $array_count = count($selected);
                if ($array_count > 0) {
                    $before = $after = '';
                    $before = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_before, ENT_QUOTES)));
                    $after = str_replace('%id%', $group_array[0], stripslashes(html_entity_decode($group->wrapper_after, ENT_QUOTES)));
                    $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . ' g' . $adrotate_config['adblock_disguise'] . '-' . $group->id . '">';
                    // Kill dynamic mode for mobile users
                    if ($adrotate_config['mobile_dynamic_mode'] == 'Y' and $group->modus == 1 and (adrotate_is_mobile() or adrotate_is_tablet())) {
                        $group->modus = 0;
                    }
                    if ($group->modus == 1) {
                        // Dynamic ads
                        $i = 1;
                        // Limit group to save resources
                        $amount = $group->adspeed >= 10000 ? 10 : 20;
                        // Randomize and trim output
                        $selected = adrotate_shuffle($selected);
                        foreach ($selected as $key => $banner) {
                            if ($i <= $amount) {
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $banner->image);
                                $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-dyn a' . $adrotate_config['adblock_disguise'] . '-' . $banner->id . ' c-' . $i . '">';
                                $output .= $before . adrotate_ad_output($banner->id, $group->id, $banner->title, $banner->bannercode, $banner->tracker, $image, $banner->responsive) . $after;
                                $output .= '</div>';
                                $i++;
                            }
                        }
                    } else {
                        if ($group->modus == 2) {
                            // Block of ads
                            $block_count = $group->gridcolumns * $group->gridrows;
                            if ($array_count < $block_count) {
                                $block_count = $array_count;
                            }
                            $columns = 1;
                            for ($i = 1; $i <= $block_count; $i++) {
                                $banner_id = adrotate_pick_weight($selected);
                                $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                                $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-col b' . $adrotate_config['adblock_disguise'] . '-' . $group->id . ' a' . $adrotate_config['adblock_disguise'] . '-' . $selected[$banner_id]->id . '">';
                                $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                                $output .= '</div>';
                                if ($columns == $group->gridcolumns and $i != $block_count) {
                                    $output .= '</div><div class="g' . $adrotate_config['adblock_disguise'] . ' g' . $adrotate_config['adblock_disguise'] . '-' . $group->id . '">';
                                    $columns = 1;
                                } else {
                                    $columns++;
                                }
                                if ($adrotate_config['stats'] == 1 and $selected[$banner_id]->tracker == "Y") {
                                    adrotate_count_impression($selected[$banner_id]->id, $group->id, $site, $adrotate_config['impression_timer']);
                                }
                                unset($selected[$banner_id]);
                            }
                        } else {
                            // Default (single ad)
                            $banner_id = adrotate_pick_weight($selected);
                            $image = str_replace('%folder%', $adrotate_config['banner_folder'], $selected[$banner_id]->image);
                            $output .= '<div class="g' . $adrotate_config['adblock_disguise'] . '-single a' . $adrotate_config['adblock_disguise'] . '-' . $selected[$banner_id]->id . '">';
                            $output .= $before . adrotate_ad_output($selected[$banner_id]->id, $group->id, $selected[$banner_id]->title, $selected[$banner_id]->bannercode, $selected[$banner_id]->tracker, $image, $selected[$banner_id]->responsive) . $after;
                            $output .= '</div>';
                            if ($adrotate_config['stats'] == 1 and $selected[$banner_id]->tracker == "Y") {
                                adrotate_count_impression($selected[$banner_id]->id, $group->id, $site, $adrotate_config['impression_timer']);
                            }
                        }
                    }
                    $output .= '</div>';
                    unset($selected);
                } else {
                    if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
                        switch_to_blog($current_blog);
                    }
                    $output .= adrotate_fallback($fallback, 'expired', $site);
                }
            } else {
                if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
                    switch_to_blog($current_blog);
                }
                $output .= adrotate_fallback($fallback, 'unqualified', $site);
            }
        } else {
            $output .= adrotate_error('group_not_found', array($group_array[0]));
        }
        if ($site > 0 and adrotate_is_networked() and ($license['type'] == 'Network' or $license['type'] == 'Developer')) {
            switch_to_blog($current_blog);
        }
    } else {
        $output .= adrotate_error('group_no_id');
    }
    return $output;
}
function adrotate_banner($group_ids, $banner_id = 0, $block = 0, $column = 0, $preview = false)
{
    global $wpdb;
    if ($group_ids) {
        $group_ids = explode(",", $group_ids);
        $x = array_rand($group_ids, 1);
        $now = current_time('timestamp');
        if ($banner_id > 0 and $block < 1) {
            $select_banner = " AND `id` = '{$banner_id}'";
        } else {
            $select_banner = "";
        }
        if ($preview == false) {
            $active_banner = " AND `active` = 'yes' AND '{$now}' >= `startshow` AND '{$now}' <= `endshow`";
        }
        $rawbanners = $wpdb->get_results("SELECT `id`, `tracker`, `clicks`, `maxclicks`, `shown`, `maxshown`, `group` FROM `" . $wpdb->prefix . "adrotate` WHERE `group` = '{$group_ids[$x]}' " . $select_banner . $active_banner);
        if ($rawbanners) {
            foreach ($rawbanners as $raw) {
                $selected[] = $raw->id;
                if ($raw->clicks >= $raw->maxclicks and $raw->maxclicks > 0 and $raw->tracker == "Y") {
                    $selected = array_diff($selected, array($raw->id));
                }
                if ($raw->shown >= $raw->maxshown and $raw->maxshown > 0 and in_array($raw->id, $selected)) {
                    $selected = array_diff($selected, array($raw->id));
                }
            }
            if ($block > 0 and $banner_id < 1) {
                shuffle($selected);
                $chosen = $selected;
                $limit = $block;
            } else {
                if ($banner_id > 0) {
                    $chosen = array($banner_id);
                    $limit = 1;
                } else {
                    $y = array_rand($selected, 1);
                    $chosen = array($selected[$y]);
                    $limit = count($chosen);
                }
            }
            if (count($selected) > 0) {
                $output = '';
                $cutoff = 1;
                for ($i = 0; $i < $limit; $i++) {
                    $banner = $wpdb->get_row("SELECT * FROM `" . $wpdb->prefix . "adrotate` WHERE `id` = '" . $chosen[$i] . "'");
                    $banner_output = $banner->bannercode;
                    if ($banner->tracker == "Y") {
                        $banner_output = str_replace('%link%', get_option('siteurl') . '/wp-content/plugins/adrotate/adrotate-out.php?trackerid=' . $banner->id, $banner_output);
                    } else {
                        $banner_output = str_replace('%link%', $banner->link, $banner_output);
                    }
                    $banner_output = str_replace('%image%', $banner->image, $banner_output);
                    $banner_output = str_replace('%id%', $banner->id, $banner_output);
                    $output .= $banner_output;
                    if ($column > 0 and $cutoff == $column) {
                        $output .= '<br style="height:none; width:none;" />';
                        $cutoff = 1;
                    } else {
                        $cutoff++;
                    }
                    if ($preview == false) {
                        $wpdb->query("UPDATE `" . $wpdb->prefix . "adrotate` SET `shown` = `shown` + 1 WHERE `id` = '{$banner->id}'");
                    }
                }
            } else {
                $output = adrotate_fallback($group_ids[$x], 'expired');
            }
        } else {
            $output = adrotate_fallback($group_ids[$x], 'unqualified');
        }
    } else {
        $output = '<span style="color: #F00; font-style: italic; font-weight: bold;">Error, no group_id specified! Check your syntax or contact an administrator!</span>';
    }
    $output = stripslashes(html_entity_decode($output, ENT_QUOTES));
    return $output;
}