Example #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_get_random_paragraphs($content, $length, $original_paragraph_positions, &$debug)
{
    $ops = adinj_options();
    $debug_on = adinj_debug_on();
    $random_ad_paragraphs = array();
    $random_start_paragraph = 0;
    $random_end_paragraph = 0;
    adinj_get_random_ad_start_end_paragraph($content, $original_paragraph_positions, $random_start_paragraph, $random_end_paragraph, $debug);
    if ($debug_on) {
        $debug .= "\n1st Injected random ads range starts at: {$random_start_paragraph}, and ends at: {$random_end_paragraph}";
    }
    $paracount = count($original_paragraph_positions);
    $random_end_paragraph = min($random_end_paragraph, $paracount);
    if (!adinj_ticked('rnd_allow_ads_on_last_paragraph')) {
        if ($random_end_paragraph == $paracount) {
            --$random_end_paragraph;
        }
        if ($random_end_paragraph == 0) {
            if ($debug_on) {
                $debug .= "\nNo random ads: no potential inj positions after removing last paragraph position";
            }
            return;
        }
    }
    if ($random_start_paragraph <= 0 || $random_end_paragraph <= 0 || $random_end_paragraph - $random_start_paragraph < 0) {
        if ($debug_on) {
            $debug .= "\nWarning: No paragraphs: random_start_paragraph:{$random_start_paragraph} random_end_paragraph:{$random_end_paragraph}";
        }
        return array();
    }
    if ($debug_on) {
        $debug .= "\n2nd Injected random ads range starts at: {$random_start_paragraph}, and ends at: {$random_end_paragraph}";
    }
    $potential_random_ad_paragraphs = range($random_start_paragraph, $random_end_paragraph);
    if ($debug_on) {
        $debug .= "\npotential_random_ad_paragraphs:" . sizeof($potential_random_ad_paragraphs);
    }
    if (sizeof($potential_random_ad_paragraphs) == 0) {
        if ($debug_on) {
            $debug .= "\nNo random ads: no potential inj positions found in content";
        }
        return array();
    }
    # Checks to see if we can inject random ads
    $requested_num_rand_ads_to_insert = adinj_num_rand_ads_to_insert($length, $debug);
    if ($debug_on) {
        $debug .= "\nrequested_num_rand_ads_to_insert:{$requested_num_rand_ads_to_insert}";
    }
    if ($requested_num_rand_ads_to_insert <= 0) {
        if ($debug_on) {
            $debug .= "\nNo random ads enabled on this post";
        }
        return array();
    }
    $num_rand_ads_to_insert = $requested_num_rand_ads_to_insert;
    # We have to put the first ad at the first position we already selected unless the first ad can start 'anywhere'
    if ($ops['random_ads_start_mode'] != 'anywhere' && $ops['random_ads_start_mode'] != 'after') {
        $random_ad_paragraphs[] = array_shift($potential_random_ad_paragraphs);
        --$num_rand_ads_to_insert;
    }
    # Pick the correct number of random injection points
    if (sizeof($potential_random_ad_paragraphs) > 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_random_ad_paragraphs) < $num_rand_ads_to_insert) {
                $debug .= "\nNum random ads requested={$requested_num_rand_ads_to_insert}. But restricted to " . (sizeof($potential_random_ad_paragraphs) + sizeof($random_ad_paragraphs)) . " due to limited injection points.";
                $num_rand_ads_to_insert = sizeof($potential_random_ad_paragraphs);
            }
            $rand_positions = array_rand(array_flip($potential_random_ad_paragraphs), $num_rand_ads_to_insert);
            if ($num_rand_ads_to_insert == 1) {
                // Add the single value to the array
                $random_ad_paragraphs[] = $rand_positions;
            } else {
                // Merge the values with the array
                $random_ad_paragraphs = array_merge($random_ad_paragraphs, $rand_positions);
            }
        } else {
            // Multiple ads may be inserted at the same position
            $injections = 0;
            while ($injections++ < $num_rand_ads_to_insert) {
                $rnd = array_rand($potential_random_ad_paragraphs);
                $random_ad_paragraphs[] = $potential_random_ad_paragraphs[$rnd];
            }
        }
    }
    # Sort positions
    sort($random_ad_paragraphs);
    if ($debug_on) {
        $injected_list = '';
        foreach ($random_ad_paragraphs as $pos) {
            $injected_list .= "{$pos} ";
        }
        $debug .= "\nInjected ads at: {$injected_list}";
    }
    return $random_ad_paragraphs;
}