File: Bootstrap.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (314 lines) | stat: -rw-r--r-- 11,111 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

package MT::Bootstrap;

use strict;

sub BEGIN {
    my ( $dir, $orig_dir );
    require File::Spec;
    if ( !( $dir = $ENV{MT_HOME} ) ) {
        if ( ( $ENV{SCRIPT_FILENAME} || $0 ) =~ m!(.*([/\\]))! ) {
            $orig_dir = $dir = $1;
            my $slash = $2;
            $dir =~ s!(?:[/\\]|^)(?:plugins[/\\].*|tools[/\\])$!$slash!;
            $dir = '' if ( $dir =~ m!^\.?[\\/]$! );
        }
        elsif ( $] >= 5.006 ) {

         # MT_DIR/lib/MT/Bootstrap.pm -> MT_DIR/lib/MT -> MT_DIR/lib -> MT_DIR
            require File::Basename;
            $dir = File::Basename::dirname(
                File::Basename::dirname(
                    File::Basename::dirname( File::Spec->rel2abs(__FILE__) )
                )
            );
        }
        unless ($dir) {
            $orig_dir = $dir = $ENV{PWD} || '.';
            $dir =~ s!(?:[/\\]|^)(?:plugins[/\\].*|tools[/\\]?)$!!;
        }
        $ENV{MT_HOME} = $dir;
    }
    unshift @INC, File::Spec->catdir( $dir,      'extlib' );
    unshift @INC, File::Spec->catdir( $orig_dir, 'lib' )
        if $orig_dir && ( $orig_dir ne $dir );
}

my $fcgi_exit_requested   = 0;
my $fcgi_handling_request = 0;

sub fcgi_sig_handler {
    my $sig = shift;
    $fcgi_exit_requested = $sig;
    if ($fcgi_handling_request) {

       # With exit requested flag set, FastCGI loop will exit when it is done.
        print STDERR
            "Movable Type: SIG$sig caught. Exiting gracefully after current request.\n";
    }
    else {

        # Not currently handling a request, so just go ahead and exit.
        print STDERR "Movable Type: SIG$sig caught. Exiting gracefully.\n";
        exit(1);
    }
}

sub import {
    my ( $pkg, %param ) = @_;

    # use 'App' parameter, or MT_APP from the environment
    my $class = $param{App} || $ENV{MT_APP};

    if ($class) {

        # When running under FastCGI, the initial invocation of the
        # script has a bare environment. We can use this to test
        # for FastCGI.
        my $not_fast_cgi = 0;
        $not_fast_cgi ||= exists $ENV{$_}
            for qw(HTTP_HOST GATEWAY_INTERFACE SCRIPT_FILENAME SCRIPT_URL);
        my $fast_cgi
            = defined $param{FastCGI} ? $param{FastCGI} : ( !$not_fast_cgi );
        if ($fast_cgi) {
            eval 'require CGI::Fast;';
            $fast_cgi = 0 if $@;
        }

        # ready to run now... run inside an eval block so we can gracefully
        # die if something bad happens
        my $app;
        eval {

            # line __LINE__ __FILE__
            require MT;
            eval "# line " 
                . __LINE__ . " " 
                . __FILE__
                . "\nrequire $class; 1;"
                or die $@;
            if ($fast_cgi) {
                $ENV{FAST_CGI} = 1;

                # Signal handling needs FAIL_ACCEPT_ON_INTR set:
                # "If set, Accept will fail if interrupted. It not set, it
                # will just keep on waiting."
                require FCGI;
                $CGI::Fast::Ext_Request
                    = FCGI::Request( \*STDIN, \*STDOUT, \*STDERR, \%ENV, 0,
                    FCGI::FAIL_ACCEPT_ON_INTR() );
                my ( $max_requests, $max_time, $cfg );

                # catch SIGUSR1 and SIGTERM and allow request to finish before
                # exiting.
                # TODO: handle SIGPIPE more gracefully.
                $SIG{USR1} = \&fcgi_sig_handler;
                $SIG{TERM} = \&fcgi_sig_handler;
                $SIG{PIPE} = 'IGNORE';

           # we set the "handling request" flag so the signal handler can exit
           # immediately when requests aren't being handled.
                while ( $fcgi_handling_request = ( my $cgi = new CGI::Fast ) )
                {
                    $app = $class->new( %param, CGIObject => $cgi )
                        or die $class->errstr;

                    $ENV{FAST_CGI} = 1;
                    $app->{fcgi_startup_time} ||= time;
                    $app->{fcgi_request_count}
                        = ( $app->{fcgi_request_count} || 0 ) + 1;

                    unless ($cfg) {
                        $cfg          = $app->config;
                        $max_requests = $cfg->FastCGIMaxRequests;
                        $max_time     = $cfg->FastCGIMaxTime;
                    }

                    local $SIG{__WARN__} = sub { $app->trace( $_[0] ) };
                    MT->set_instance($app);
                    $app->config->read_config_db();
                    $app->init_request( CGIObject => $cgi );
                    $app->run;

                    # force closing of connection here
                    $CGI::Fast::Ext_Request->Finish();

                    $fcgi_handling_request = 0;

                    # Check for caught signal
                    if ($fcgi_exit_requested) {
                        print STDERR
                            "Movable Type: FastCGI request loop exiting. Caught signal SIG$fcgi_exit_requested.\n";
                        last;
                    }

                    # Check for timeout for this process
                    elsif ( $max_time
                        && ( time - $app->{fcgi_startup_time} >= $max_time ) )
                    {
                        last;
                    }

                    # Check for max executions for this process
                    elsif ( $max_requests
                        && ( $app->{fcgi_request_count} >= $max_requests ) )
                    {
                        last;
                    }
                    else {
                        require MT::Touch;
                        require MT::Util;
                        if ( my $touched
                            = MT::Touch->latest_touch( 0, 'config' ) )
                        {

                            # Should get UNIX epoch with no_offset flag,
                            # since MT::Touch uses gmtime always.
                            $touched
                                = MT::Util::ts2epoch( undef, $touched, 1 );
                            if ( $touched > $app->{fcgi_startup_time} ) {
                                last;
                            }
                        }
                    }
                }
            }
            else {
                $app = $class->new(%param) or die $class->errstr;
                local $SIG{__WARN__} = sub { $app->trace( $_[0] ) };
                $app->run;
            }
        };
        if ( my $err = $@ ) {
            if ( !$app && $err =~ m/Missing configuration file/ ) {
                my $host = $ENV{SERVER_NAME} || $ENV{HTTP_HOST};
                $host =~ s/:\d+//;
                my $port = $ENV{SERVER_PORT};
                my $uri = $ENV{REQUEST_URI} || $ENV{SCRIPT_NAME};
                if ( $uri =~ m/(\/mt\.(f?cgi|f?pl)(\?.*)?)$/ ) {
                    my $script = $1;
                    my $ext    = $2;

                    if (-f File::Spec->catfile(
                            $ENV{MT_HOME}, "mt-wizard.$ext"
                        )
                        )
                    {
                        $uri =~ s/\Q$script\E//;
                        $uri .= '/mt-wizard.' . $ext;

                        my $prot = $port == 443 ? 'https' : 'http';
                        my $cgipath = "$prot://$host";
                        $cgipath .= ":$port"
                            unless $port == 443
                                or $port == 80;
                        $cgipath .= $uri;
                        print "Status: 302 Moved\n";
                        print "Location: " . $cgipath . "\n\n";
                        exit;
                    }
                }
            }

            my $charset = 'utf-8';
            eval {

                # line __LINE__ __FILE__
                my $cfg = MT::ConfigMgr->instance;    #this is needed
                $app ||= MT->instance;
                my $c = $app->find_config;
                $app->{cfg}->read_config($c);
                $charset = $app->{cfg}->PublishCharset;
            };
            if (   $app
                && UNIVERSAL::isa( $app, 'MT::App' )
                && !UNIVERSAL::isa( $app, 'MT::App::Wizard' ) )
            {
                require MT::I18N;
                my $enc = MT::I18N::guess_encoding($err);
                $err = Encode::decode( $enc, $err )
                    unless Encode::is_utf8($err);
                eval {

                    # line __LINE__ __FILE__
                    if ( !$MT::DebugMode
                        && ( $err =~ m/^(.+?)( at .+? line \d+)(.*)$/s ) )
                    {
                        $err = $1;
                    }
                    my %param = ( error => $err );
                    if ( $err =~ m/Bad ObjectDriver/ ) {
                        $param{error_database_connection} = 1;
                    }
                    elsif ( $err =~ m/Bad CGIPath/ ) {
                        $param{error_cgi_path} = 1;
                    }
                    elsif ( $err =~ m/Missing configuration file/ ) {
                        $param{error_config_file} = 1;
                    }
                    my $page = $app->build_page( 'error.tmpl', \%param )
                        or die $app->errstr;
                    print "Content-Type: text/html; charset=$charset\n\n";
                    $app->print_encode($page);
                    exit;
                };
                $err = $@;
            }
            elsif ( $app && UNIVERSAL::isa( $app, 'MT::App::Wizard' ) ) {
                ## Because mt-config.cgi was not found in this time.
                $err = '';
            }

            if ($err) {
                if ( !$MT::DebugMode
                    && ( $err =~ m/^(.+?)( at .+? line \d+)(.*)$/s ) )
                {
                    $err = $1;
                }
                print "Content-Type: text/plain; charset=$charset\n\n";
                print $app
                    ? Encode::encode( $charset,
                    $app->translate( "Got an error: [_1]", $err ) )
                    : "Got an error: " . Encode::encode( $charset, $err );
            }
        }
    }
}

1;
__END__

=head1 NAME

MT::Bootstrap

=head1 DESCRIPTION

Startup module used to simplify MT application CGIs.

=head1 SYNOPSIS

Movable Type CGI scripts should utilize the C<MT::Bootstrap> module
to invoke the application code itself. When run, it is necessary
to add the MT "lib" directory to the Perl include path.

Example (for CGIs in the main MT directory itself):

    #!/usr/bin/perl -w
    use strict;
    use lib $ENV{MT_HOME} ? "$ENV{MT_HOME}/lib" : 'lib';
    use MT::Bootstrap App => 'MT::App::CMS';

Example (for CGIs in a plugin subdirectory, ie MT/plugins/plugin_x):

    #!/usr/bin/perl -w
    use strict;
    use lib "lib", ($ENV{MT_HOME} ? "$ENV{MT_HOME}/lib" : "../../lib");
    use MT::Bootstrap App => 'MyApp';

=cut