示例#1
0
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);
                    }
                }
            }
        }
    }
}