<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># Copyright 2001, 2002 Benjamin Trott. This code cannot be redistributed without
# permission from www.movabletype.org.
#
# $Id: App.pm,v 1.84 2002/10/31 00:39:56 btrott Exp $

package MT::App;
use strict;

use File::Spec;

use MT::Log;
use MT::Request;
use MT::Util qw( encode_html offset_time_list decode_html );
use MT;
@MT::App::ISA = qw( MT );

use vars qw( %Global_actions );
sub add_methods {
    my $this = shift;
    my %meths = @_;
    if (ref($this)) {
        for my $meth (keys %meths) {
            $this-&gt;{vtbl}{$meth} = $meths{$meth};
        }
    } else {
        for my $meth (keys %meths) {
            $Global_actions{$this}{$meth} = $meths{$meth};
        }
    }
}

sub handler ($$) {
    my $class = shift;
    my($r) = @_;
    require Apache::Constants;
    my $config_file = $r-&gt;dir_config('MTConfig');
    my $app = $class-&gt;new( Config =&gt; $config_file )
        or die $class-&gt;errstr;
    $app-&gt;run;
    return Apache::Constants::OK();
}

sub send_http_header {
    my $app = shift;
    my($type) = @_;
    $type ||= 'text/html';
    if (my $charset = $app-&gt;{charset}) {
        $type .= "; charset=$charset"
            if $type =~ m!^text/! &amp;&amp; $type !~ /\bcharset\b/;
    }
    if ($ENV{MOD_PERL}) {
        $app-&gt;{apache}-&gt;send_http_header($type);
    } else {
        $app-&gt;{cgi_headers}{-type} = $type;
        print $app-&gt;{query}-&gt;header(%{ $app-&gt;{cgi_headers} });
    }
}

sub print {
    my $app = shift;
    if ($ENV{MOD_PERL}) {
        $app-&gt;{apache}-&gt;print(@_);
    } else {
        CORE::print(@_);
    }
}

sub init {
    my $app = shift;
    $app-&gt;SUPER::init(@_) or return;
    $app-&gt;{vtbl} = { };
    $app-&gt;{requires_login} = 0;
    $app-&gt;{is_admin} = 0;
    $app-&gt;{template_dir} = '';
    $app-&gt;{cgi_headers} = { };
    if ($ENV{MOD_PERL}) {
        require Apache::Request;
        $app-&gt;{apache} = Apache-&gt;request;
        $app-&gt;{query} = Apache::Request-&gt;new($app-&gt;{apache},
            POST_MAX =&gt; $app-&gt;{cfg}-&gt;CGIMaxUpload);
    } else {
        require CGI;
        $CGI::POST_MAX = $app-&gt;{cfg}-&gt;CGIMaxUpload;
        $app-&gt;{query} = CGI-&gt;new;
    }
    $app-&gt;{cookies} = $app-&gt;cookies;
    ## Initialize the MT::Request singleton for this particular request.
    MT::Request-&gt;instance;
    ## Load up the object's initial vtbl with any global methods.
    if (my $meths = $Global_actions{ref($app)}) {
        for my $meth (keys %$meths) {
            $app-&gt;{vtbl}{$meth} = $meths-&gt;{$meth};
        }
    }
    $app;
}

sub is_authorized { 1 }

sub login {
    my $app = shift;
    my $q = $app-&gt;{query};
    my $cookies = $app-&gt;{cookies};
    my($user, $pass, $remember, $crypted);
    my $first_time = 0;
    if ($cookies-&gt;{user}) {
        ($user, $pass, $remember) = split /::/, $cookies-&gt;{user}-&gt;value;
        $crypted = 1;
    } else {
        $first_time = 1;
        $user = $q-&gt;param('username');
        $pass = $q-&gt;param('password');
    }
    return unless $user &amp;&amp; $pass;
    my $user_class = $app-&gt;{user_class};
    eval "use $user_class;";
    if (my $author = $user_class-&gt;load({ name =&gt; $user })) {
        if ($author-&gt;is_valid_password($pass, $crypted)) {
            if ($first_time) {
                $app-&gt;log("User '" . $author-&gt;name . "' logged in " .
                          "successfully");
            }
            return($author, $first_time);
        }
    }
    ## Login invalid, so get rid of cookie (if it exists) and let the
    ## user know.
    $app-&gt;log("Invalid login attempt from user '$user'");
    $app-&gt;bake_cookie(-name =&gt; 'user', -value =&gt; '', -expires =&gt; '-1y')
        unless $first_time;
    return $app-&gt;error("Invalid login.");
}

sub set_header {
    my $app = shift;
    my($key, $val) = @_;
    if ($ENV{MOD_PERL}) {
        $app-&gt;{apache}-&gt;header_out($key, $val);
    } else {
        unless ($key =~ /^-/) {
            ($key = lc($key)) =~ tr/-/_/;
            $key = '-' . $key;
        }
        $app-&gt;{cgi_headers}{$key} = $val;
    }
}

sub bake_cookie {
    my $app = shift;
    my %param = @_;
    unless ($param{-path}) {
        $param{-path} = $app-&gt;path;
    }
    if ($ENV{MOD_PERL}) {
        require Apache::Cookie;
        my $cookie = Apache::Cookie-&gt;new($app-&gt;{apache}, %param);
        $cookie-&gt;bake;
    } else {
        require CGI::Cookie;
        my $cookie = CGI::Cookie-&gt;new(%param);
        $app-&gt;set_header('-cookie', $cookie);
    }
}

sub cookies {
    my $app = shift;
    my $class = $ENV{MOD_PERL} ? 'Apache::Cookie' : 'CGI::Cookie';
    eval "use $class;";
    $class-&gt;fetch;
}

sub show_error {
    my $app = shift;
    my($error) = @_;
    my $tmpl;
    $error = encode_html($error);
    $error =~ s!(http://\S+)!&lt;a href="$1" target="_blank"&gt;$1&lt;/a&gt;!g;
    $tmpl = $app-&gt;load_tmpl('error.tmpl') or
        return "Can't load error template; got error '" . $app-&gt;errstr .
               "'. Giving up. Original error was &lt;pre&gt;$error&lt;/pre&gt;";
    $tmpl-&gt;param(ERROR =&gt; $error);
    $tmpl-&gt;output;
}

sub pre_run { 1 }
sub post_run { 1 }

sub run {
    my $app = shift;
    my $q = $app-&gt;{query};
    my($body);
    eval {
        if ($ENV{MOD_PERL}) {
            my $status = $q-&gt;parse;
            unless ($status == Apache::Constants::OK()) {
                die $app-&gt;translate('The file you uploaded is too large.') .
                    "\n";
            }
        } else {
            my $err;
            eval { $err = $q-&gt;cgi_error };
            unless ($@) {
                if ($err &amp;&amp; $err =~ /^413/) {
                    die $app-&gt;translate('The file you uploaded is too large.') .
                        "\n";
                }
            }
        }

        REQUEST:
        {
            if ($app-&gt;{requires_login}) {
            LOGIN:
            {
                my($author, $first_time) = $app-&gt;login;
                if ($author) {
                    $app-&gt;{author} = $app-&gt;{user} = $author;
                    if ($first_time) {
                        my $remember = $q-&gt;param('remember') ? 1 : 0;
                        my %arg = (
                            -name =&gt; 'user',
                            -value =&gt; join('::', $author-&gt;name, $author-&gt;password,
                                                 $remember),
                        );
                        $arg{-expires} = '+10y' if $remember;
                        $app-&gt;bake_cookie(%arg);
                    }
                    last LOGIN if $app-&gt;is_authorized;
                }
                $body = $app-&gt;build_page('login.tmpl', {error =&gt; $app-&gt;errstr})
                    or $body = $app-&gt;show_error( $app-&gt;errstr ), last REQUEST;
                last REQUEST;
            }  ## end LOGIN block
            }

            $app-&gt;pre_run;
            my $mode = $q-&gt;param('__mode') || $app-&gt;{default_mode};
            my $code = $app-&gt;{vtbl}{$mode} or
                $app-&gt;error($app-&gt;translate('Unknown action [_1]', $mode));
            if ($code) {
                $body = $code-&gt;($app);
            }
            $app-&gt;post_run;
            unless (defined $body || $app-&gt;{redirect}) {
                if ($app-&gt;{no_print_body}) {
                    $app-&gt;print($app-&gt;errstr);
                } else {
                    $body = $app-&gt;show_error( $app-&gt;errstr );
                }
            }
        }  ## end REQUEST block
    };
    if ($@) {
        $body = $app-&gt;show_error($@);
    }

    ## Add the Pragma: no-cache header.
    ## WEIRD: for CGI::cache, any true argument to cache means NO cache
    if ($ENV{MOD_PERL}) {
        $app-&gt;{apache}-&gt;no_cache(1);
    } else {
        $q-&gt;cache(1);
    }

    if (my $url = $app-&gt;{redirect}) {
        if ($ENV{MOD_PERL}) {
            $app-&gt;{apache}-&gt;header_out(Location =&gt; $url);
            $app-&gt;{apache}-&gt;status(Apache::Constants::REDIRECT());
            $app-&gt;send_http_header;
        } else {
            print $q-&gt;redirect(-uri =&gt; $url, %{ $app-&gt;{cgi_headers} });
        }
    } else {
        unless ($app-&gt;{no_print_body}) {
            $app-&gt;send_http_header;
            $app-&gt;print($body);
            $app-&gt;print("&lt;pre&gt;$app-&gt;{trace}&lt;/pre&gt;") if $app-&gt;{trace};
        }
    }
}

sub l10n_filter {
    my $app = shift;
    my($text) = @_;
    $text =~ s!&lt;MT_TRANS ([^&gt;]+)&gt;!
        my($msg, %args) = ($1);
        while ($msg =~ /(\w+)\s*=\s*(["'])(.*?)\2/g) {
            $args{$1} = $3;
        }
        $args{params} = '' unless defined $args{params};
        my @p = map decode_html($_), split /\s*%%\s*/, $args{params};
        $app-&gt;translate($args{phrase}, @p);
    !ge;
    $text;
}

sub load_tmpl {
    my $app = shift;
    my($file, @p) = @_;
    my $path = $app-&gt;{cfg}-&gt;TemplatePath;
    require HTML::Template;
    my $tmpl;
    eval {
        $tmpl = HTML::Template-&gt;new_file(
            File::Spec-&gt;catfile($path, $app-&gt;{template_dir}, $file),
            path =&gt; [ File::Spec-&gt;catdir($path, $app-&gt;{template_dir}) ],
            die_on_bad_params =&gt; 0, global_vars =&gt; 1, @p);
    };
    my $err = $@;
    return $app-&gt;error(
        $app-&gt;translate("Loading template '[_1]' failed: [_2]", $file, $err))
        if $@;

    ## We do this in load_tmpl because show_error and login don't call
    ## build_page; so we need to set these variables here.
    my $spath = $app-&gt;{cfg}-&gt;StaticWebPath || $app-&gt;path;
    $spath .= '/' unless $spath =~ m!/$!;
    $tmpl-&gt;param(static_uri =&gt; $spath);
    $tmpl-&gt;param(script_url =&gt; $app-&gt;uri);
    $tmpl-&gt;param(script_path =&gt; $app-&gt;path);
    $tmpl-&gt;param(script_full_url =&gt; $app-&gt;base . $app-&gt;uri);
    $tmpl-&gt;param(mt_version =&gt; MT-&gt;VERSION);

    $tmpl-&gt;param(language_tag =&gt; $app-&gt;current_language);
    my $enc = $app-&gt;{cfg}-&gt;PublishCharset ||
              $app-&gt;language_handle-&gt;encoding;
    $tmpl-&gt;param(language_encoding =&gt; $enc);
    $app-&gt;{charset} = $enc;

    $tmpl;
}

sub build_page {
    my $app = shift;
    my($file, $param) = @_;
    my $tmpl = $app-&gt;load_tmpl($file) or return;
    for my $key (keys %$param) {
        $tmpl-&gt;param($key, $param-&gt;{$key});
    }
    $app-&gt;l10n_filter($tmpl-&gt;output);
}

sub delete_param {
    my $app = shift;
    my($key) = @_;
    my $q = $app-&gt;{query};
    if ($ENV{MOD_PERL}) {
        my $tab = $q-&gt;parms;
        $tab-&gt;unset($key);
    } else {
        $q-&gt;delete($key);
    }
}

## Path/server/script-name determination methods

sub base {
    my $app = shift;
    return $app-&gt;{__host} if exists $app-&gt;{__host};
    my $path = $app-&gt;{is_admin} ?
        ($app-&gt;{cfg}-&gt;AdminCGIPath || $app-&gt;{cfg}-&gt;CGIPath) :
        $app-&gt;{cfg}-&gt;CGIPath;
    if ($path =~ m!^(https?://[^/]+)!i) {
        (my $host = $1) =~ s!/$!!;
        return $app-&gt;{__host} = $host;
    }
    '';
}

sub path {
    my $app = shift;
    return $app-&gt;{__path} if exists $app-&gt;{__path};
    my $path = $app-&gt;{is_admin} ?
        ($app-&gt;{cfg}-&gt;AdminCGIPath || $app-&gt;{cfg}-&gt;CGIPath) :
        $app-&gt;{cfg}-&gt;CGIPath;
    if ($path =~ m!^https?://[^/]+(/.*)$!i) {
        $path = $1;
        $path .= '/' unless substr($path, -1, 1) eq '/';
    } else {
        $path = '/';
    }
    $app-&gt;{__path} = $path;
}

sub script {
    my $app = shift;
    return $app-&gt;{__script} if exists $app-&gt;{__script};
    my $script = $ENV{MOD_PERL} ? $app-&gt;{apache}-&gt;uri : $ENV{SCRIPT_NAME};
    $script =~ s!/$!!;
    $script = (split /\//, $script)[-1];
    $app-&gt;{__script} = $script;
}

sub uri { $_[0]-&gt;path . $_[0]-&gt;script }

sub path_info {
    my $app = shift;
    return $app-&gt;{__path_info} if exists $app-&gt;{__path_info};
    my $path_info;
    if ($ENV{MOD_PERL}) {
        ## mod_perl often leaves part of the script name (Location)
        ## in the path info, for some reason. This should remove it.
        $path_info = $app-&gt;{apache}-&gt;path_info;
        if ($path_info) {
            my($script_last) = $app-&gt;{apache}-&gt;location =~ m!/([^/]+)$!;
            $path_info =~ s!^/$script_last!!;
        }
    } else {
        $path_info = $app-&gt;{query}-&gt;path_info;
    }
    $app-&gt;{__path_info} = $path_info;
}

sub redirect {
    my $app = shift;
    my($url) = @_;
    unless ($url =~ m!^https?://!i) {
        $url = $app-&gt;base . $url;
    }
    $app-&gt;{redirect} = $url;
    return;
}

## Logging/tracing

sub log {
    my $app = shift;
    my($msg) = @_;
    my $log = MT::Log-&gt;new;
    $log-&gt;message($msg);
    $log-&gt;ip($app-&gt;remote_ip);
    $log-&gt;save;
}

sub trace { $_[0]-&gt;{trace} .= "@_" }

sub remote_ip {
    my $app = shift;
    $ENV{MOD_PERL} ? $app-&gt;{apache}-&gt;connection-&gt;remote_ip : $ENV{REMOTE_ADDR};
}

sub DESTROY {
    ## Destroy the Request object, which is used for caching
    ## per-request data. We have to do this manually, because in
    ## a persistent environment, the object will not go out of scope.
    ## Same with the ConfigMgr object and ObjectDriver.
    undef $MT::Request::r;
    undef $MT::Object::DRIVER;
    undef $MT::ConfigMgr::cfg;
}

1;
__END__

=head1 NAME

MT::App - Movable Type base web application class

=head1 SYNOPSIS

    package MT::App::Foo;
    use MT::App;
    @MT::App::Foo::ISA = qw( MT::App );

    package main;
    my $app = MT::App::Foo-&gt;new;
    $app-&gt;run;

=head1 DESCRIPTION

I&lt;MT::App&gt; is the base class for Movable Type web applications. It provides
support for an application running using standard CGI, or under
I&lt;Apache::Registry&gt;, or as a I&lt;mod_perl&gt; handler. I&lt;MT::App&gt; is not meant to
be used directly, but rather as a base class for other web applications using
the Movable Type framework (for example, I&lt;MT::App::CMS&gt;).

=head1 USAGE

I&lt;MT::App&gt; subclasses the I&lt;MT&gt; class, which provides it access to the
publishing methods in that class.

Following are the list of methods specific to I&lt;MT::App&gt;:

=head2 MT::App-&gt;new

Constructs and returns a new I&lt;MT::App&gt; object.

=head2 $app-&gt;run

Runs the application. This gathers the input, chooses the method to execute,
executes it, and prints the output to the client.

If an error occurs during the execution of the application, I&lt;run&gt; handles all
of the errors thrown either through the I&lt;MT::ErrorHandler&gt; or through I&lt;die&gt;.

=head2 $app-&gt;login

Checks the user's credentials, first by looking for a login cookie, then by
looking for the C&lt;username&gt; and C&lt;password&gt; CGI parameters. In both cases,
the username and password are verified for validity. This method does not set
the user's login cookie, however--that should be done by the caller (in most
cases, the caller is the I&lt;run&gt; method).

On success, returns the I&lt;MT::Author&gt; object representing the author who logged
in, and a boolean flag; if the boolean flag is true, it indicates the the login
credentials were obtained from the CGI parameters, and thus that a cookie
should be set by the caller. If the flag is false, the credentials came from
an existing cookie.

On an authentication error, I&lt;login&gt; removes any authentication cookies that
the user might have on his or her browser, then returns C&lt;undef&gt;, and the
error message can be obtained from C&lt;$app-E&lt;gt&gt;errstr&gt;.

=head2 $app-&gt;send_http_header([ $content_type ])

Sends the HTTP header to the client; if I&lt;$content_type&gt; is specified, the
I&lt;Content-Type&gt; header is set to I&lt;$content_type&gt;. Otherwise, C&lt;text/html&gt; is
used as the default.

In a I&lt;mod_perl&gt; context, this calls the I&lt;Apache::send_http_header&gt; method;
in a CGI context, the I&lt;CGI::header&gt; method is called.

=head2 $app-&gt;print(@data)

Sends data I&lt;@data&gt; to the client.

In a I&lt;mod_perl&gt; context, this calls the I&lt;Apache::print&gt; method; in a CGI
context, data is printed directly to STDOUT.

=head2 $app-&gt;bake_cookie(%arg)

Bakes a cookie to be sent to the client.

I&lt;%arg&gt; can contain any valid parameters to the I&lt;new&gt; methods of
I&lt;CGI::Cookie&gt; (or I&lt;Apache::Cookie&gt;--both take the same parameters). These
include C&lt;-name&gt;, C&lt;-value&gt;, C&lt;-path&gt;, and C&lt;-expires&gt;.

If you do not include the C&lt;-path&gt; parameter in I&lt;%arg&gt;, it will be set
automatically to C&lt;$app-E&lt;gt&gt;path&gt; (below).

In a I&lt;mod_perl&gt; context, this method uses I&lt;Apache::Cookie&gt;; in a CGI context,
it uses I&lt;CGI::Cookie&gt;.

=head2 $app-&gt;cookies

Returns a reference to a hash containing cookie objects, where the objects are
either of class I&lt;Apache::Cookie&gt; (in a I&lt;mod_perl&gt; context) or I&lt;CGI::Cookie&gt;
(in a CGI context).

=head2 $app-&gt;build_page($tmpl_name, \%param)

Builds an application page to be sent to the client; the page name is specified
in I&lt;$tmpl_name&gt;, which should be the name of a template containing valid
I&lt;HTML::Template&gt; markup. I&lt;\%param&gt; is a hash ref whose keys and values will
be passed to I&lt;HTML::Template::param&gt; for use in the template.

On success, returns a scalar containing the page to be sent to the client. On
failure, returns C&lt;undef&gt;, and the error message can be obtained from
C&lt;$app-E&lt;gt&gt;errstr&gt;.

=head2 $app-&gt;redirect($url)

Issues a redirect to the client to the URL I&lt;$url&gt;. If I&lt;$url&gt; is not an
absolute URL, it is prepended with the value of I&lt;$app-E&lt;gt&gt;base&gt;.

=head2 $app-&gt;base

The protocol and domain of the application. For example, with the full URI
F&lt;http://www.foo.com/mt/mt.cgi&gt;, this method will return F&lt;http://www.foo.com&gt;.

=head2 $app-&gt;path

The path to the application directory. For example, with the full URI
F&lt;http://www.foo.com/mt/mt.cgi&gt;, this method will return F&lt;/mt/&gt;.

=head2 $app-&gt;script

The name of the application. For example, with the full URI
F&lt;http://www.foo.com/mt/mt.cgi&gt;, this method will return F&lt;mt.cgi&gt;.

=head2 $app-&gt;uri

The concatenation of C&lt;$app-E&lt;gt&gt;path&gt; and C&lt;$app-E&lt;gt&gt;script&gt;. For example,
with the full URI F&lt;http://www.foo.com/mt/mt.cgi&gt;, this method will return
F&lt;/mt/mt.cgi&gt;.

=head2 $app-&gt;path_info

The path_info for the request (that is, whatever is left in the URI after the
URI to filename translation).

=head2 $app-&gt;log($msg)

Adds the message I&lt;$msg&gt; to the activity log. The log entry will be tagged
with the IP address of the client running the application (that is, of the
browser that made the HTTP request), using C&lt;$app-E&lt;gt&gt;remote_ip&gt;.

=head2 $app-&gt;trace(@msg)

Adds a trace message "I&lt;@msg&gt;" to the internal tracing mechanism; trace
messages are then displayed at the top of the output page sent to the client.
This is useful for debugging.

=head2 $app-&gt;remote_ip

The IP address of the client.

In a I&lt;mod_perl&gt; context, this calls I&lt;Apache::Connection::remote_ip&gt;; in a
CGI context, this uses I&lt;$ENV{REMOTE_ADDR}&gt;.

=head1 AUTHOR &amp; COPYRIGHTS

Please see the I&lt;MT&gt; manpage for author, copyright, and license information.

=cut
</pre></body></html>