Example #1
0
function encodePage($input)
{
    # Look for script blocks
    #	if ( preg_match_all('#<(?:script|style).*?</(?:script|style)>#is', $input, $scripts, PREG_OFFSET_CAPTURE) ) { # not working
    if (preg_match_all('#<script.*?</script>#is', $input, $scripts, PREG_OFFSET_CAPTURE)) {
        # Create starting offset - only start encoding after the <head>
        # as this seems to help browsers cope!
        $offset = preg_match('#<body[^>]*>(.)#is', $input, $tmp, PREG_OFFSET_CAPTURE) ? $tmp[1][1] : 0;
        $new = $offset ? substr($input, 0, $offset) : '';
        # Go through all the matches
        foreach ($scripts[0] as $id => $match) {
            # Determine position of the preceeding non-script block
            $end = $match[1] ? $match[1] - 1 : 0;
            $start = $offset;
            $length = $end - $start;
            # Add encoded block to page if there is one
            if ($length && $length > 0) {
                $new .= "\n\n\n<!--start encode block-->\n";
                $new .= encodeBlock(substr($input, $start, $length));
                $new .= "\n<!--end encode block-->\n\n\n";
            }
            # Add unencoded script to page
            $new .= "\n\n\n<!--start unencoded block-->\n";
            $new .= $match[0];
            $new .= "\n<!--end unencoded block-->\n\n\n";
            # Move offset up
            $offset = $match[1] + strlen($match[0]);
        }
        # Add final block
        if ($remainder = substr($input, $offset)) {
            $new .= encodeBlock($remainder);
        }
        # Update input with new
        $input = $new;
    } else {
        # No scripts is easy - just encode the lot
        $input = encodeBlock($input);
    }
    # Return the encoded page
    return $input;
}
Example #2
0
function encodePage($input)
{
    // Look for script blocks
    if (preg_match_all('#<script.*?</script>#is', $input, $scripts, PREG_OFFSET_CAPTURE)) {
        // Create starting offset - only start encoding after the <head>
        // as this seems to help browsers cope!
        $offset = preg_match('#<head[^>]*>(.)#is', $input, $tmp, PREG_OFFSET_CAPTURE) ? $tmp[1][1] : 0;
        $new = $offset ? substr($input, 0, $offset) : '';
        // Go through all the matches
        foreach ($scripts[0] as $id => $match) {
            // Determine position of the preceeding non-script block
            $end = $match[1] ? $match[1] - 1 : 0;
            $start = $offset;
            $length = $end - $start;
            // Add encoded block to page if there is one
            if ($length) {
                $new .= encodeBlock(substr($input, $start, $length));
            }
            // Add unencoded script to page
            $new .= $match[0];
            // Move offset up
            $offset = $match[1] + strlen($match[0]);
        }
        // Add final block
        if ($remainder = substr($input, $offset)) {
            $new .= encodeBlock($remainder);
        }
        // Update input with new
        $input = $new;
    } else {
        // No scripts is easy - just encode the lot
        $input = encodeBlock($input);
    }
    // Return the encoded page
    return $input;
}