function adrotate_ad_output($id, $group = 0, $name, $bannercode, $tracker, $image, $responsive)
{
    global $blog_id, $adrotate_debug, $adrotate_config;
    $banner_output = $bannercode;
    $banner_output = stripslashes(htmlspecialchars_decode($banner_output, ENT_QUOTES));
    if ($adrotate_config['stats'] > 0) {
        if (empty($blog_id) or $blog_id == '') {
            $blog_id = 0;
        }
        if ($adrotate_config['stats'] == 1 and $tracker == "Y") {
            // Internal tracker
            preg_match_all('/<a[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
            if (isset($matches[0])) {
                $banner_output = str_replace('<a ', '<a data-track="' . adrotate_hash($id, $group, $blog_id) . '" ', $banner_output);
                foreach ($matches[0] as $value) {
                    if (preg_match('/<a[^>]+class=\\"(.+?)\\"[^>]*>/i', $value, $regs)) {
                        $result = $regs[1] . " gofollow";
                        $banner_output = str_replace('class="' . $regs[1] . '"', 'class="' . $result . '"', $banner_output);
                    } else {
                        $banner_output = str_replace('<a ', '<a class="gofollow" ', $banner_output);
                    }
                    unset($value, $regs, $result);
                }
            }
            if ($adrotate_debug['timers'] == true) {
                $banner_output = str_replace('<a ', '<a data-debug="1" ', $banner_output);
            }
        }
    }
    // Add Responsive classes
    preg_match_all('/<img[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
    if (isset($matches[0])) {
        foreach ($matches[0] as $value) {
            if (preg_match('/<img[^>]+class=\\"(.+?)\\"[^>]*>/i', $value, $regs)) {
                $result = $regs[1];
                if ($responsive == 'Y') {
                    $result .= " responsive";
                }
                $result = trim($result);
                $banner_output = str_replace('class="' . $regs[1] . '"', 'class="' . $result . '"', $banner_output);
            } else {
                $result = '';
                if ($responsive == 'Y') {
                    $result .= " responsive";
                }
                $result = trim($result);
                if (strlen($result) > 0) {
                    $banner_output = str_replace('<img ', '<img class="' . $result . '" ', $banner_output);
                }
            }
            unset($value, $regs, $result);
        }
    }
    unset($matches);
    $banner_output = str_replace('%title%', $name, $banner_output);
    $banner_output = str_replace('%random%', rand(100000, 999999), $banner_output);
    $banner_output = str_replace('%image%', $image, $banner_output);
    $banner_output = str_replace('%id%', $id, $banner_output);
    $banner_output = do_shortcode($banner_output);
    return $banner_output;
}
function adrotate_ad_output($id, $group = 0, $name, $bannercode, $tracker, $image, $responsive)
{
    global $blog_id, $adrotate_debug, $adrotate_config;
    $banner_output = $bannercode;
    $banner_output = stripslashes(htmlspecialchars_decode($banner_output, ENT_QUOTES));
    if ($adrotate_config['stats'] > 0) {
        if (empty($blog_id) or $blog_id == '') {
            $blog_id = 0;
        }
        $tracking_pixel = "data:image/gif;base64,R0lGODlhAQABAIAAAP///////yH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==";
        if ($adrotate_config['stats'] == 1 and $tracker == "Y") {
            // Internal tracker
            preg_match_all('/<a[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
            if (isset($matches[0])) {
                $banner_output = str_replace('<a ', '<a data-track="' . adrotate_hash($id, $group, $blog_id) . '" ', $banner_output);
                foreach ($matches[0] as $value) {
                    if (preg_match('/<a[^>]+class=\\"(.+?)\\"[^>]*>/i', $value, $regs)) {
                        $result = $regs[1] . " gofollow";
                        $banner_output = str_replace('class="' . $regs[1] . '"', 'class="' . $result . '"', $banner_output);
                    } else {
                        $banner_output = str_replace('<a ', '<a class="gofollow" ', $banner_output);
                    }
                    unset($value, $regs, $result);
                }
            }
            if ($adrotate_debug['timers'] == true) {
                $banner_output = str_replace('<a ', '<a data-debug="1" ', $banner_output);
            }
        }
        if ($adrotate_config['stats'] == 2 and $tracker == "Y") {
            // Piwik Analytics
            if ($adrotate_config['enable_loggedin_clicks'] == 'Y' and is_user_logged_in() or !is_user_logged_in()) {
                preg_match_all('/<(?:a|img|object|embed|iframe)[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
                if (isset($matches[0])) {
                    $click_event = "data-track-content data-content-name=\"{$name}\" ";
                    $content_piece = !empty($image) ? basename($image) : $name;
                    $impression_event = "data-content-piece=\"{$content_piece}\" ";
                    // Image banner
                    if (stripos($banner_output, '<a') !== false and stripos($banner_output, '<img') !== false) {
                        if (!preg_match('/<a[^>]+data-track-content[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<a ', '<a ' . $click_event, $banner_output);
                        }
                        if (!preg_match('/<img[^>]+data-content-piece[^>]*>/i', $banner_output, $img)) {
                            $banner_output = str_replace('<img ', '<img ' . $impression_event, $banner_output);
                        }
                    }
                    // Text banner (With tagged tracking pixel for impressions)
                    if (stripos($banner_output, '<a') !== false and stripos($banner_output, '<img') === false) {
                        if (!preg_match('/<a[^>]+data-track-content[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<a ', '<a ' . $click_event, $banner_output);
                        }
                        $banner_output .= '<img width="0" height="0" src="' . $tracking_pixel . '" ' . $impression_event . '/>';
                    }
                    // HTML5/iFrame advert (Only supports impressions)
                    if (stripos($banner_output, '<iframe') !== false) {
                        if (preg_match('/<iframe[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<iframe ', '<iframe ' . $click_event, $banner_output);
                        }
                    }
                    // Flash advert
                    if (stripos($banner_output, '<object') !== false and stripos($banner_output, '<embed') !== false) {
                        if (!preg_match('/<object[^>]+data-track-content[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<object ', '<object ' . $click_event, $banner_output);
                        }
                        if (!preg_match('/<embed[^>]+data-content-piece[^>]*>/i', $banner_output, $img)) {
                            $banner_output = str_replace('<embed ', '<embed ' . $impression_event, $banner_output);
                        }
                    }
                    unset($content_piece, $url, $img);
                }
            }
        }
        if ($adrotate_config['stats'] == 3 and $tracker == "Y") {
            // Google Analytics
            if ($adrotate_config['enable_loggedin_clicks'] == 'Y' and is_user_logged_in() or !is_user_logged_in()) {
                preg_match_all('/<(?:a|img|object|iframe)[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
                if (isset($matches[0])) {
                    $click_event = "'send','event','banner','click','{$name}',1,{'nonInteraction':1}";
                    $impression_event = "'send','event','banner','impression','{$name}',2,{'nonInteraction':1}";
                    // Image banner
                    if (stripos($banner_output, '<a') !== false and stripos($banner_output, '<img') !== false) {
                        if (!preg_match('/<a[^>]+onClick[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<a ', '<a onClick="ga(' . $click_event . ');" ', $banner_output);
                        }
                        if (!preg_match('/<img[^>]+onload[^>]*>/i', $banner_output, $img)) {
                            $banner_output = str_replace('<img ', '<img onload="ga(' . $impression_event . ');" ', $banner_output);
                        }
                    }
                    // Text banner (With tagged tracking pixel for impressions)
                    if (stripos($banner_output, '<a') !== false and stripos($banner_output, '<img') === false) {
                        if (!preg_match('/<a[^>]+onClick[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<a ', '<a onClick="ga(' . $click_event . ');" ', $banner_output);
                        }
                        $banner_output .= '<img width="0" height="0" src="' . $tracking_pixel . '" onload="ga(' . $impression_event . ');" />';
                    }
                    // HTML5/iFrame advert (Only supports impressions)
                    if (stripos($banner_output, '<iframe') !== false) {
                        if (!preg_match('/<iframe[^>]+onload[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<iframe ', '<iframe onload="ga(' . $impression_event . ');" ', $banner_output);
                        }
                    }
                    // Flash advert (With tagged tracking pixel for impressions)
                    if (stripos($banner_output, '<object') !== false) {
                        if (!preg_match('/<object[^>]+onClick[^>]*>/i', $banner_output, $url)) {
                            $banner_output = str_replace('<object ', '<object onClick="ga(' . $click_event . ');" ', $banner_output);
                            $banner_output .= '<img width="0" height="0" src="' . $tracking_pixel . '" onload="ga(' . $impression_event . ');" />';
                        }
                    }
                    unset($url, $img, $click_event, $impression_event);
                }
            }
        }
        unset($matches);
    }
    // Add Adblock and Responsive classes
    preg_match_all('/<img[^>](?:.*?)>/i', $banner_output, $matches, PREG_SET_ORDER);
    if (isset($matches[0])) {
        foreach ($matches[0] as $value) {
            if (preg_match('/<img[^>]+class=\\"(.+?)\\"[^>]*>/i', $value, $regs)) {
                $result = $regs[1];
                if ($adrotate_config['adblock'] == 'Y') {
                    $result .= " adblock";
                }
                if ($responsive == 'Y') {
                    $result .= " responsive";
                }
                $result = trim($result);
                $banner_output = str_replace('class="' . $regs[1] . '"', 'class="' . $result . '"', $banner_output);
            } else {
                $result = '';
                if ($adrotate_config['adblock'] == 'Y') {
                    $result .= "adblock";
                }
                if ($responsive == 'Y') {
                    $result .= " responsive";
                }
                $result = trim($result);
                if (strlen($result) > 0) {
                    $banner_output = str_replace('<img ', '<img class="' . $result . '" ', $banner_output);
                }
            }
            unset($value, $regs, $result);
        }
        unset($matches);
    }
    $banner_output = str_replace('%title%', $name, $banner_output);
    $banner_output = str_replace('%random%', rand(100000, 999999), $banner_output);
    $banner_output = str_replace('%image%', $image, $banner_output);
    $banner_output = str_replace('%id%', $id, $banner_output);
    $banner_output = do_shortcode($banner_output);
    return $banner_output;
}