Movable Type 'encode_php' function

Note! This functionality is now part of Movable Type 2.0 and later (Thanks Ben!). So unless you have an ancient version of Movable Type, don't bother applying this hack.

I wrote an extension to Movable Type today that allows you to escape content for embedding in PHP quoted strings. It would work like this:

<?php
$str = '<$MTEntryTitle encode_php="1"$>';
echo "<div>Title = $str</div>";
?>

Anyway, I sent it to Ben for inclusion in MT (he responded to say it will be going into MT 2.0).

For the adventurous, I've attached the implementation details to this post (click the MORE link to see it).

First of all, these instructions are for Movable Type 1.4. I haven't tried any of this with any other version, so if you aren't using MT 1.4 this may not work for you. You will have to physically alter the Movable Type code in order to do this. Only two files will be affected: "lib/MT/Util.pm" and "lib/MT/Template/Context.pm" (relative to the Movable Type installation directory). If you're uncomfortable with changing the Movable Type files, this probably isn't for you-- this feature will appear in Movable Type 2.0. If you do follow these instructions, please make a backup copy of those two files before you proceed. I will not be held responsible for failure to follow these instructions. Also, you should familiarize yourself with the license agreement for Movable Type before you make these changes. You are allowed to modify the Movable Type files, but once altered, those files may not be distributed.

1. First, add these routines to Util.pm (you can place them below the encode_js subroutine if you'd like):

sub encode_php {
    my($str,$method) = @_;
    return '' unless defined $str;
    if ($method eq 'qq') {
        encode_phpqq($str);
    } elsif ($method eq 'heredoc' || $method eq 'here') {
        encode_phphere($str);
    } else {
        encode_phpq($str);
    }
}

sub encode_phpq {
    my($str) = @_;
    $str =~ s!\\!\\\\!g;       # replace character \ with string \\
    $str =~ s!'!\\'!g;         # replace character ' with string \'
    $str;
}

sub encode_phpqq {
    my($str) = @_;
    $str = encode_phphere($str);
    $str =~ s!"!\\"!g;         # replace character " with string \"
    $str;
}

sub encode_phphere {
    my($str) = @_;
    $str =~ s!\\!\\\\!g;       # replace character \ with string \\
    $str =~ s!\$!\\\$!g;       # replace character $ with string \$
    $str =~ s!\n!\\n!g;        # replace character \n with string \n
    $str =~ s!\r!\\r!g;        # replace character \r with string \r
    $str =~ s!\t!\\t!g;        # replace character \t with string \t
    $str;
}

2. Next, to make the 'php_encode' routine visible to external MT modules, you'll have to change this section (near the top of the file):

@EXPORT_OK = qw( start_end_day start_end_week start_end_month
                 html_text_transform encode_html munge_comment
                 offset_time offset_time_list first_n_words
                 archive_file_for format_ts dirify remove_html
                 days_in wday_from_ts encode_js get_entry );

To look like this:

@EXPORT_OK = qw( start_end_day start_end_week start_end_month
                 html_text_transform encode_html munge_comment
                 offset_time offset_time_list first_n_words
                 archive_file_for format_ts dirify remove_html
                 days_in wday_from_ts encode_js encode_php
                 get_entry );

3. Now, add 'encode_php' to the import list in Context.pm, add this to the post_process_handler:

            if ($args->{'encode_php'}) {
                $str = encode_php($str, $args->{'encode_php'});
            }

4. Finally, adding to the Context.pm's import list is easy. Just change this line:

use MT::Util qw( start_end_day start_end_week start_end_month
                 html_text_transform munge_comment archive_file_for
                 format_ts offset_time_list first_n_words dirify get_entry
                 encode_html encode_js remove_html wday_from_ts days_in );

to look like this:

use MT::Util qw( start_end_day start_end_week start_end_month
                 html_text_transform munge_comment archive_file_for
                 format_ts offset_time_list first_n_words dirify get_entry
                 encode_html encode_js encode_php remove_html
                 wday_from_ts days_in );

There-- now you can use these attributes on your MT variable tags (the ones that look like this: <$MTEntryBody$>):

 encode_php="1"       (encode for inclusion in single-quoted PHP strings)
 encode_php="q"       (ditto)
 encode_php="qq"      (encode for inclusion in double-quoted PHP strings)
 encode_php="here"    (encode for inclusion in PHP heredoc operator)
 encode_php="heredoc" (ditto)

Feel free to post questions, complaints and so forth as a comment to this entry.

1 Comments

Raffaele Colleo said:

I'm trying to replace the character Enter for a
.

What I mean is, if someone the enter button of a text area he script is going to replace it for a

About

This article was published on February 28, 2002 10:04 PM.

The article previously posted was Fever in, fever out.

The next article is Google "Lite".

Many more can be found on the home page or by looking through the archives.

Powered by Movable Type