function widget($args, $instance)
 {
     if (adinj_ads_completely_disabled_from_page('widget')) {
         return;
     }
     if (is_front_page() && adinj_ticked('widget_exclude_front') || is_home() && adinj_ticked('widget_exclude_home') || is_page() && adinj_ticked('widget_exclude_page') || is_single() && adinj_ticked('widget_exclude_single') || is_archive() && adinj_ticked('widget_exclude_archive') || is_search() && adinj_ticked('widget_exclude_search') || is_404() && adinj_ticked('widget_exclude_404')) {
         return;
     }
     if (is_front_page() && adinj_ticked('exclude_front', $instance) || is_home() && adinj_ticked('exclude_home', $instance) || is_page() && adinj_ticked('exclude_page', $instance) || is_single() && adinj_ticked('exclude_single', $instance) || is_archive() && adinj_ticked('exclude_archive', $instance) || is_search() && adinj_ticked('exclude_search', $instance) || is_404() && adinj_ticked('exclude_404', $instance)) {
         return;
     }
     if (!adinj_allowed_in_category('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_tag('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_author('widget', $instance)) {
         return;
     }
     if (!adinj_allowed_in_id('widget', $instance)) {
         return;
     }
     extract($args);
     $ops = adinj_options();
     $include = "";
     if (adinj_mfunc_mode()) {
         $include = adinj_ad_code_include();
     }
     $title = apply_filters('widget_title', $instance['title']);
     // The old 'non upgraded' db will be passed here if the widget hasn't
     // been resaved. We can't upgrade as doing so would mean we'd have to
     // re-save the widget files - which we can't do as we can't re-write
     // the settings back to the db (at least not without a bit more work)
     $adcode = adinj_get_ad_code('widget_' . $this->get_id(), $instance);
     echo $before_widget;
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     if (!empty($adcode)) {
         echo $include;
         echo $adcode;
     }
     echo $after_widget;
 }
 function widget($args, $instance)
 {
     if (adinj_ads_completely_disabled_from_page()) {
         return;
     }
     $options = adinj_options();
     if (is_home() && adinj_ticked('widget_exclude_home') || is_page() && adinj_ticked('widget_exclude_page') || is_single() && adinj_ticked('widget_exclude_single') || is_archive() && adinj_ticked('widget_exclude_archive')) {
         return;
     }
     if ($options['ad_insertion_mode'] == 'direct_dynamic') {
         if (adinj_show_adverts() !== true) {
             return;
         }
     }
     extract($args);
     $title = apply_filters('widget_title', $instance['title']);
     $advert = $instance['advert'];
     echo $before_widget;
     if ($title) {
         echo $before_title . $title . $after_title;
     }
     $adcode = "";
     $include = "";
     if ($options['ad_insertion_mode'] == 'mfunc') {
         $include = adinj_ad_code_include();
         $adcode = adinj_get_mfunc_code($this->get_ad_file_name());
     } else {
         $adcode = $advert;
     }
     $adcode = adinj_ad_code_eval($adcode);
     if ($advert) {
         echo $include;
         echo $adcode;
     }
     echo $after_widget;
 }
function adinj_inject_hook($content)
{
    if (is_feed()) {
        return $content;
    }
    $reason = adinj_ads_completely_disabled_from_page($content);
    if ($reason !== false) {
        return adinj($content, $reason);
    }
    $ops = adinj_options();
    if ($ops['ad_insertion_mode'] == 'direct_dynamic') {
        $showads = adinj_show_adverts();
        if ($showads !== true) {
            return adinj($content, "NOADS: ad blocked at run time reason={$showads}");
        }
    }
    $ad_include = adinj_ad_code_include();
    # Ad sandwich mode
    if (is_page() || is_single()) {
        if (stripos($content, "<!--adsandwich-->") !== false) {
            return adinj($ad_include . adinj_ad_code_top() . $content . adinj_ad_code_bottom(), "Ads=adsandwich");
        }
        if (stripos($content, "<!--adfooter-->") !== false) {
            return adinj($content . $ad_include . adinj_ad_code_bottom(), "Ads=adfooter");
        }
    }
    # Insert top and bottom ads if necesary
    $length = strlen($content);
    if (is_page() || is_single()) {
        if (adinj_do_rule_if($ops['top_ad_if_longer_than'], '<', $length)) {
            $content = $ad_include . adinj_ad_code_top() . $content;
            $ad_include = false;
        }
        if (adinj_do_rule_if($ops['bottom_ad_if_longer_than'], '<', $length)) {
            $content = $content . adinj_ad_code_bottom();
        }
    }
    $num_rand_ads_to_insert = adinj_num_rand_ads_to_insert($length);
    if ($num_rand_ads_to_insert <= 0) {
        return adinj($content, "all ads used up");
    }
    $ad = adinj_ad_code_random();
    if (empty($ad)) {
        return adinj($content, "no random ad defined");
    }
    if ($ad_include !== false) {
        $content = $ad_include . $content;
    }
    $debug_on = $ops['debug_mode'];
    if (!$debug_on) {
        $debugtags = false;
    }
    $content_adfree_header = "";
    $content_adfree_footer = "";
    // TODO add docs explaining the significance of leaving blank lines
    // before or after these tags
    # 'Adsense Injection' tag compatibility
    $split = adinj_split_by_tag($content, "<!--adsensestart-->", $debugtags);
    if (count($split) == 2) {
        $content_adfree_header = $split[0];
        $content = $split[1];
    }
    # Use the same naming convention for the end tag
    $split = adinj_split_by_tag($content, "<!--adsenseend-->", $debugtags);
    if (count($split) == 2) {
        $content = $split[0];
        $content_adfree_footer = $split[1];
    }
    $split = adinj_split_by_tag($content, "<!--adstart-->", $debugtags);
    if (count($split) == 2) {
        $content_adfree_header = $split[0];
        $content = $split[1];
    }
    $split = adinj_split_by_tag($content, "<!--adend-->", $debugtags);
    if (count($split) == 2) {
        $content = $split[0];
        $content_adfree_footer = $split[1];
    }
    // TODO add note explaining that start tags are processed before the 'first
    // paragraph ad
    // Move onto random ad insertions
    $paragraphmarker = "</p>";
    if (stripos($content, $paragraphmarker) === false) {
        return adinj($content, "no &lt;/p&gt; tags");
    }
    if ($debug_on) {
        $debug = "\nTags=" . htmlentities($debugtags);
    }
    // Generate a list of all potential injection points
    if ($debug_on) {
        $debug .= "\nPotential positions: ";
    }
    $potential_inj_positions = array();
    $prevpos = -1;
    while (($prevpos = stripos($content, $paragraphmarker, $prevpos + 1)) !== false) {
        $potential_inj_positions[] = $prevpos + strlen($paragraphmarker);
        if ($debug_on) {
            $debug .= $prevpos . ", ";
        }
    }
    if ($debug_on) {
        $debug .= "\npotential_inj_positions:" . sizeof($potential_inj_positions);
    }
    if (sizeof($potential_inj_positions) == 0) {
        return adinj($content, "Error: no potential inj positions");
    }
    $inj_positions = array();
    if (adinj_ticked('first_paragraph_ad')) {
        $inj_positions[] = array_shift($potential_inj_positions);
        --$num_rand_ads_to_insert;
    }
    // Pick the correct number of random injection points
    if (sizeof($potential_inj_positions) > 0 && $num_rand_ads_to_insert > 0) {
        if (!adinj_ticked('multiple_ads_at_same_position')) {
            // Each ad is inserted into a unique position
            if (sizeof($potential_inj_positions) < $num_rand_ads_to_insert) {
                $debug .= "\nnum_rand_ads_to_insert requested={$num_rand_ads_to_insert}. But restricted to " . sizeof($potential_inj_positions) . " due to limited injection points.";
                $num_rand_ads_to_insert = sizeof($potential_inj_positions);
            }
            $rand_positions = array_rand(array_flip($potential_inj_positions), $num_rand_ads_to_insert);
            if ($num_rand_ads_to_insert == 1) {
                // Convert it back into an array
                $inj_positions[] = $rand_positions;
            } else {
                $inj_positions = array_merge($inj_positions, $rand_positions);
            }
            foreach ($inj_positions as $pos) {
                if ($debug_on) {
                    $debug = $pos . ", " . $debug;
                }
            }
        } else {
            // Multiple ads may be inserted at the same position
            $injections = 0;
            while ($injections++ < $num_rand_ads_to_insert) {
                $rnd = array_rand($potential_inj_positions);
                if ($debug_on) {
                    $debug = $potential_inj_positions[$rnd] . ", " . $debug;
                }
                $inj_positions[] = $potential_inj_positions[$rnd];
            }
        }
    }
    if (sizeof($inj_positions) == 0) {
        return adinj($content_adfree_header . $content . $content_adfree_footer, "Error: No ad injection positions: " . $debug);
    }
    // Sort positions
    sort($inj_positions);
    // Insert ads in reverse order
    global $adinj_total_rand_ads_used, $adinj_total_all_ads_used;
    for ($adnum = sizeof($inj_positions) - 1; $adnum >= 0; $adnum--) {
        $content = substr_replace($content, $ad, $inj_positions[$adnum], 0);
        ++$adinj_total_rand_ads_used;
        ++$adinj_total_all_ads_used;
    }
    return adinj($content_adfree_header . $content . $content_adfree_footer, "Ads injected: " . $debug);
}
function adinj_print_ad($adname = '')
{
    $reason = adinj_ads_completely_disabled_from_page('template', "");
    if ($reason !== false) {
        return;
    }
    if (adinj_excluded_by_tick_box('template_')) {
        return;
    }
    $dummydebug = '';
    if (adinj_ads_filtered_out('template', $dummydebug)) {
        return;
    }
    if ($adname == 'random') {
        echo adinj_ad_code_random();
    } else {
        if ($adname == 'top') {
            echo adinj_ad_code_top();
        } else {
            if ($adname == 'bottom') {
                echo adinj_ad_code_bottom();
            } else {
                if ($adname == 'footer') {
                    echo adinj_ad_code_footer();
                } else {
                    if (preg_match("/.+\\.txt/i", $adname)) {
                        adshow_display_ad_file_v2($adname);
                    }
                }
            }
        }
    }
}