1552419dcfa21b74f2dfb7c8e5eb81681922913c
[catagits/Catalyst-Controller-MovableType.git] / lib / Catalyst / Controller / MovableType.pm
1 package Catalyst::Controller::MovableType;
2 use Moose;
3 BEGIN { extends 'Catalyst::Controller::WrapCGI'; }
4 use utf8;
5 use namespace::autoclean;
6
7 our $VERSION = 0.003;
8
9 has 'perl' => (is => 'rw', default => 'perl');
10
11 has 'mt_home' => (is => 'rw'); # /my/app/root/mt/
12
13 # Use chaining here, so that Catalyst::Controller::WrapCGI::wrap_cgi can properly
14 # populate $ENV{SCRIPT_NAME} with $c->uri_for($c->action, $c->req->captures)->path,
15 # so that MovableType can then extract the correct path of location.
16 sub capture_mt_script :Chained('/') :PathPart('mt') :CaptureArgs(1) { }
17  
18 sub run_mt_script :Chained('capture_mt_script') :PathPart('') :Args {
19     my ($self, $c) = @_;
20     my $captures = $c->req->captures;
21     my $cgi_script = $captures->[0];
22
23     my %mt_scripts
24         = map +($_ => 1),
25         qw( mt-add-notify.cgi
26             mt-atom.cgi
27             mt.cgi
28             mt-comments.cgi
29             mt-feed.cgi
30             mt-ftsearch.cgi
31             mt-search.cgi
32             mt-tb.cgi
33             mt-testbg.cgi
34             mt-upgrade.cgi
35             mt-wizard.cgi
36             mt-xmlrpc.cgi
37         ) # mt-config.cgi intentionally left out
38     ;
39
40     # http://www.movabletype.org/documentation/installation/install-movable-type.html#start-blogging states:
41     # Warning: because the mt-check.cgi script displays server details which could be useful to a hacker, it
42     # is recommended that this script be removed or renamed.
43     #
44     # Allow it only in debug mode.
45     $mt_scripts{'mt_check.cgi'} = 1 if ($c->debug());
46
47     $self->not_found($c) unless ($mt_scripts{$cgi_script});
48
49     $ENV{MT_HOME} = $self->mt_home;
50
51     $self->cgi_to_response($c, sub { 
52         system($self->perl, $self->mt_home.$cgi_script);
53     });
54 }
55
56 sub not_found :Private {
57     my ($self, $c) = @_;
58     $c->response->status(404);
59     $c->response->body('Not found!');
60     $c->detach();
61 }
62  
63 1;
64
65 __END__
66
67 =head1 NAME
68
69 Catalyst::Controller::MovableType - Run Movable Type through Catalyst
70
71 =head1 DESCRIPTION
72
73 Runs Movable Type 5 through Catalyst.
74 Download Movable Type 5 from http://www.movabletype.org/
75
76 =head1 SYNOPSIS
77
78  package MyApp::Controller::Mt;
79
80  use Moose;
81  BEGIN {extends 'Catalyst::Controller::MovableType'; }
82  use utf8;
83
84  1;
85
86 =head1 INSTALLATION
87
88 Install Movable Type by extracting the zip into your template root directory.
89 Move mt-static to root/static/mt. See Synopsis on how to inherit the Controller
90 in your app. Presuming you installed Movable Type into root/mt, in your App's
91 config add:
92
93 <Controller::Root>
94     cgi_root_path mt/
95     cgi_dir mt/
96 </Controller::Root>
97 <Controller::Mt>
98     mt_home = /full/path/to/MyApp/root/mt/
99     <actions>
100         <capture_script_name>
101             PathPart = mt
102         </capture_script_name>
103     </actions>
104 </Controller::Mt>
105
106 The cgi_* directives are always given for the Root controller, no matter what
107 the Root controller is.
108
109 You can modify the path where the script matches by configuring the PathPart as
110 shown above. This controller defaults to match on the path "/mt".
111
112 Finally, make sure that the Static::Simple doesn't affect the Movable Type's
113 installation directory. An example:
114
115 __PACKAGE__->config(
116     name => 'TerveinkansaFi',
117                      static => {
118                         # first ignore all extensions, then specify static directories!
119                         'ignore_extensions' => [ qr/.*/ ],
120                         'dirs' => [ qw/static/ ]
121                         }
122 );
123
124 =head1 METHODS
125
126 =head2 capture_mt_script
127
128 Captures the path of the Movable Type.
129
130 =head2 run_mt_script
131
132 Runs the requested Movable Type .cgi script transparently with cgi_to_response.
133
134 =head2 not_found
135
136 Sets the response to a simple 404 Not found page. You can override this method
137 with your own.
138
139 =head1 BUGS
140
141 None known.
142
143 =head1 SEE ALSO
144
145 L<Catalyst::Controller::WrapCGI>
146
147 =head1 AUTHOR
148
149 Oskari 'Okko' Ojala <perl@okko.net>
150
151 =head1 CONTRIBUTORS
152
153 Matt S. Trout <mst@shadowcatsystems.co.uk>
154
155 =head1 COPYRIGHT & LICENSE
156
157 Copyright 2010 the above author(s).
158
159 This sofware is free software, and is licensed under the same terms as Perl itself.
160
161 =cut
162