root/branches/release-30/lib/MT/CMS/Blog.pm @ 1418

Revision 1418, 72.1 kB (checked in by auno, 21 months ago)

Fixed to work 'Publishing Method' change on Publishing Settings.

  • Property svn:keywords set to Id Revision
Line 
1package MT::CMS::Blog;
2
3use strict;
4
5sub edit {
6    my $cb = shift;
7    my ($app, $id, $obj, $param) = @_;
8
9    my $q = $app->param;
10    my $cfg = $app->config;
11    my $blog = $app->blog;
12    my $blog_id = $id;
13
14    if ($id) {
15        require MT::IPBanList;
16        my $output = $param->{output} || '';
17        $param->{need_full_rebuild}  = 1 if $q->param('need_full_rebuild');
18        $param->{need_index_rebuild} = 1 if $q->param('need_index_rebuild');
19        $param->{show_ip_info} = $cfg->ShowIPInformation;
20        $param->{use_plugins} = $cfg->UsePlugins;
21
22        my $entries_on_index = ( $obj->entries_on_index || 0 );
23        if ($entries_on_index) {
24            $param->{'list_on_index'} = $entries_on_index;
25            $param->{'posts'}         = 1;
26        }
27        else {
28            $param->{'list_on_index'} = ( $obj->days_on_index || 0 );
29            $param->{'days'} = 1;
30        }
31        my $lang = $obj->language || 'en';
32        $lang = 'en' if lc($lang) eq 'en-us' || lc($lang) eq 'en_us';
33        $lang = 'ja' if lc($lang) eq 'jp';
34        $param->{ 'language_' . $lang } = 1;
35
36        $param->{system_allow_comments} = $cfg->AllowComments;
37        $param->{system_allow_pings}    = $cfg->AllowPings;
38        $param->{tk_available}          = eval { require MIME::Base64; 1; }
39          && eval { require LWP::UserAgent; 1 };
40        $param->{'auto_approve_commenters'} =
41          !$obj->manual_approve_commenters;
42        $param->{identity_system}     = $app->config('IdentitySystem');
43        $param->{handshake_return}    = $app->base . $app->mt_uri;
44        $param->{"moderate_comments"} = $obj->moderate_unreg_comments;
45        $param->{ "moderate_comments_"
46              . ( $obj->moderate_unreg_comments || 0 ) } = 1;
47        $param->{ "moderate_pings_" . ( $obj->moderate_pings || 0 ) } = 1;
48
49        my $cmtauth_reg = $app->registry('commenter_authenticators');
50        foreach my $auth ( keys %$cmtauth_reg ) {
51            $cmtauth_reg->{$auth}->{disabled} = 1
52              if exists( $cmtauth_reg->{$auth}->{condition} )
53              && !( $cmtauth_reg->{$auth}->{condition}->() );
54        }
55        if ( my $auths = $blog->commenter_authenticators ) {
56            foreach ( split ',', $auths ) {
57                if ( 'MovableType' eq $_ ) {
58                    $param->{enabled_MovableType} = 1;
59                }
60                else {
61                    $cmtauth_reg->{$_}->{enabled} = 1;
62                }
63            }
64        }
65        my @cmtauth_loop;
66        foreach ( keys %$cmtauth_reg ) {
67            $cmtauth_reg->{$_}->{key} = $_;
68            if (
69                UNIVERSAL::isa(
70                    $cmtauth_reg->{$_}->{plugin}, 'MT::Plugin'
71                )
72              )
73            {
74                push @cmtauth_loop, $cmtauth_reg->{$_};
75            }
76        }
77        unshift @cmtauth_loop, $cmtauth_reg->{'TypeKey'}
78          if exists( $cmtauth_reg->{'TypeKey'} )
79          && $blog->remote_auth_token;
80        unshift @cmtauth_loop, $cmtauth_reg->{'Vox'}
81          if exists $cmtauth_reg->{'Vox'};
82        unshift @cmtauth_loop, $cmtauth_reg->{'LiveJournal'}
83          if exists $cmtauth_reg->{'LiveJournal'};
84        unshift @cmtauth_loop, $cmtauth_reg->{'OpenID'}
85          if exists $cmtauth_reg->{'OpenID'};
86
87        $param->{cmtauth_loop} = \@cmtauth_loop;
88
89        if ( $output eq 'cfg_prefs.tmpl' ) {
90            $app->add_breadcrumb( $app->translate('General Settings') );
91
92            my $lang = $obj->language || 'en';
93            $lang = 'en' if lc($lang) eq 'en-us' || lc($lang) eq 'en_us';
94            $lang = 'ja' if lc($lang) eq 'jp';
95            $param->{ 'language_' . $lang } = 1;
96
97            if ( $obj->cc_license ) {
98                $param->{cc_license_name} =
99                  MT::Util::cc_name( $obj->cc_license );
100                $param->{cc_license_image_url} =
101                  MT::Util::cc_image( $obj->cc_license );
102                $param->{cc_license_url} =
103                  MT::Util::cc_url( $obj->cc_license );
104            }
105        }
106        elsif ( $output eq 'cfg_entry.tmpl' ) {
107            ## load entry preferences for new/edit entry page of the blog
108            my $pref_param = $app->load_entry_prefs;
109            %$param = ( %$param, %$pref_param );
110            $param->{ 'sort_order_posts_'
111                  . ( $obj->sort_order_posts || 0 ) } = 1;
112            $param->{ 'status_default_' . $obj->status_default } = 1
113              if $obj->status_default;
114            $param->{ 'allow_comments_default_'
115                  . ( $obj->allow_comments_default || 0 ) } = 1;
116            $param->{system_allow_pings} =
117              $cfg->AllowPings && $blog->allow_pings;
118            $param->{system_allow_comments} = $cfg->AllowComments
119              && ( $blog->allow_reg_comments
120                || $blog->allow_unreg_comments );
121            my $replace_fields = $blog->smart_replace_fields || '';
122            my @replace_fields = split( /,/, $replace_fields );
123            foreach my $fld (@replace_fields) {
124                $param->{ 'nwc_' . $fld } = 1;
125            }
126            $param->{ 'nwc_smart_replace_' . ( $blog->smart_replace || 0 ) } = 1;
127            $param->{ 'nwc_replace_none' } = ( $blog->smart_replace || 0 ) == 2;
128        }
129        elsif ( $output eq 'cfg_web_services.tmpl' ) {
130            $param->{system_disabled_notify_pings} =
131              $cfg->DisableNotificationPings;
132            $param->{system_allow_outbound_pings} =
133              $cfg->OutboundTrackbackLimit eq 'any';
134            my %selected_pings = map { $_ => 1 }
135              split ',', ($obj->update_pings || '');
136            my $pings = $app->registry('ping_servers');
137            my @pings;
138            push @pings,
139              {
140                key   => $_,
141                label => $pings->{$_}->{label},
142                exists( $selected_pings{$_} ) ? ( selected => 1 ) : (),
143              } foreach keys %$pings;
144            $param->{pings_loop} = \@pings;
145        }
146        elsif ( $output eq 'cfg_comments.tmpl' ) {
147            $param->{email_new_comments_1} =
148              ( $obj->email_new_comments || 0 ) == 1;
149            $param->{email_new_comments_2} =
150              ( $obj->email_new_comments || 0 ) == 2;
151            $param->{nofollow_urls}     = $obj->nofollow_urls;
152            $param->{follow_auth_links} = $obj->follow_auth_links;
153            $param->{ 'sort_order_comments_'
154                  . ( $obj->sort_order_comments || 0 ) } = 1;
155            $param->{global_sanitize_spec} = $cfg->GlobalSanitizeSpec;
156            $param->{ 'sanitize_spec_' . ( $obj->sanitize_spec ? 1 : 0 ) } =
157              1;
158            $param->{sanitize_spec_manual} = $obj->sanitize_spec
159              if $obj->sanitize_spec;
160            $param->{allow_comments} = $blog->allow_reg_comments
161              || $blog->allow_unreg_comments;
162            $param->{use_comment_confirmation} =
163              defined $blog->use_comment_confirmation
164              ? $blog->use_comment_confirmation
165              : 0;
166            $param->{system_allow_comments} = $cfg->AllowComments
167              && ( $blog->allow_reg_comments
168                || $blog->allow_unreg_comments );
169            my @cps = MT->captcha_providers;
170
171            foreach my $cp (@cps) {
172                if ( ( $blog->captcha_provider || '' ) eq $cp->{key} ) {
173                    $cp->{selected} = 1;
174                }
175            }
176            $param->{captcha_loop} = \@cps;
177        }
178        elsif ( $output eq 'cfg_trackbacks.tmpl' ) {
179            $param->{email_new_pings_1} = ( $obj->email_new_pings || 0 ) == 1;
180            $param->{email_new_pings_2} = ( $obj->email_new_pings || 0 ) == 2;
181            $param->{nofollow_urls}     = $obj->nofollow_urls;
182            $param->{system_allow_selected_pings} =
183              $cfg->OutboundTrackbackLimit eq 'selected';
184            $param->{system_allow_outbound_pings} =
185              $cfg->OutboundTrackbackLimit eq 'any';
186            $param->{system_allow_local_pings} =
187                 ( $cfg->OutboundTrackbackLimit eq 'local' )
188              || ( $cfg->OutboundTrackbackLimit eq 'any' );
189        }
190        elsif ( $output eq 'cfg_registration.tmpl' ) {
191            $param->{commenter_authenticators} =
192              $obj->commenter_authenticators;
193            my $registration = $cfg->CommenterRegistration;
194            if ( $registration->{Allow} ) {
195                $param->{registration} =
196                  $blog->allow_commenter_regist ? 1 : 0;
197            }
198            else {
199                $param->{system_disallow_registration} = 1;
200            }
201            $param->{allow_reg_comments} = $blog->allow_reg_comments;
202            $param->{allow_unreg_comments} = $blog->allow_unreg_comments;
203            $param->{require_typekey_emails} = $obj->require_typekey_emails;
204        }
205        elsif ( $output eq 'cfg_spam.tmpl' ) {
206            my $threshold = $obj->junk_score_threshold || 0;
207            $threshold = '+' . $threshold if $threshold > 0;
208            $param->{junk_score_threshold} = $threshold;
209            $param->{junk_folder_expiry}   = $obj->junk_folder_expiry || 60;
210            $param->{auto_delete_junk}     = $obj->junk_folder_expiry;
211        }
212        elsif ( $output eq 'cfg_archives.tmpl' ) {
213            $app->add_breadcrumb( $app->translate('Publishing Settings') );
214            if (   $obj->column('archive_path')
215                || $obj->column('archive_url') )
216            {
217                $param->{enable_archive_paths} = 1;
218                $param->{archive_path}         = $obj->column('archive_path');
219                $param->{archive_url}          = $obj->column('archive_url');
220            }
221            else {
222                $param->{archive_path} = '';
223                $param->{archive_url}  = '';
224            }
225            $param->{ 'archive_type_preferred_'
226                  . $blog->archive_type_preferred } = 1
227              if $blog->archive_type_preferred;
228            my $at = $blog->archive_type;
229            if ( $at && $at ne 'None' ) {
230                my @at = split /,/, $at;
231                for my $at (@at) {
232                    $param->{ 'archive_type_' . $at } = 1;
233                }
234            }
235            if ( $blog->publish_queue ) {
236                $param->{publish_queue} = 1;
237            }
238        }
239        elsif ( $output eq 'cfg_plugin.tmpl' ) {
240            $app->add_breadcrumb( $app->translate('Plugin Settings') );
241            $param->{blog_view} = 1;
242            require MT::CMS::Plugin;
243            MT::CMS::Plugin::build_plugin_table( $app,
244                param => $param,
245                scope => 'blog:' . $blog_id
246            );
247            $param->{can_config} = 1;
248        }
249        else {
250            $app->add_breadcrumb( $app->translate('Settings') );
251        }
252        ( my $offset = $obj->server_offset ) =~ s![-\.]!_!g;
253        $offset =~ s!_0+$!!; # fix syntax highlight ->!
254        $param->{ 'server_offset_' . $offset } = 1;
255        if ( $output eq 'cfg_comments.tmpl' ) {
256            ## Load text filters.
257            $param->{text_filters_comments} =
258              $app->load_text_filters( $obj->convert_paras_comments,
259                'comment' );
260        }
261        elsif ( $output eq 'cfg_entry.tmpl' ) {
262            ## Load text filters.
263            $param->{text_filters} =
264              $app->load_text_filters( $obj->convert_paras, 'entry' );
265        }
266        $param->{nav_config} = 1;
267        $param->{error} = $app->errstr if $app->errstr;
268    } else {
269        $app->add_breadcrumb( $app->translate('New Blog') );
270        ( my $tz = $cfg->DefaultTimezone ) =~ s![-\.]!_!g;
271        $tz =~ s!_00$!!; # fix syntax highlight ->!
272        $param->{ 'server_offset_' . $tz } = 1;
273        $param->{'can_edit_config'}        = $app->user->can_create_blog;
274        $param->{'can_set_publish_paths'}  = $app->user->can_create_blog;
275
276        my $sets = $app->registry("template_sets");
277        $sets->{$_}{key} = $_ for keys %$sets;
278        $sets->{'mt_blog'}{selected} = 1;
279        $sets = $app->filter_conditional_list([ values %$sets ]);
280        no warnings;
281        @$sets = sort { $a->{order} <=> $b->{order} } @$sets;
282        $param->{'template_set_loop'} = $sets;
283        $param->{'template_set_index'} = $#$sets;
284    }
285
286    if (   !$param->{site_path}
287        && !( $param->{site_path} = $app->config('DefaultSiteRoot') ) )
288    {
289        my $cwd = $app->document_root;
290        $cwd = File::Spec->catdir($cwd, 'BLOG-NAME'); # for including the end of directory separator
291        $cwd =~ s!BLOG-NAME\z!!;                      # canonpath() remove it
292        $cwd =~ s!([\\/])cgi(?:-bin)?([\\/].*)?$!$1!;
293        $cwd =~ s!([\\/])mt[\\/]?$!$1!i;
294        $param->{suggested_site_path} = $cwd;
295    }
296    if ( !$param->{id} ) {
297        if ( $param->{site_path} ) {
298            $param->{site_path} =
299              File::Spec->catdir( $param->{site_path}, 'BLOG-NAME' );
300        }
301        else {
302            $param->{suggested_site_path} =
303              File::Spec->catdir( $param->{suggested_site_path},
304                'BLOG-NAME' );
305        }
306    }
307
308    # If not yet defined, set the site_url to the config default, if one exists.
309    $param->{site_url} ||= $app->config('DefaultSiteURL');
310    if ( !$param->{site_url} ) {
311        $param->{suggested_site_url} = $app->base . '/';
312        $param->{suggested_site_url} =~ s!/cgi(?:-bin)?(/.*)?$!/!;
313        $param->{suggested_site_url} =~ s!/mt/?$!/!i;
314    }
315    if ( !$param->{id} ) {
316        if ( $param->{site_url} ) {
317            $param->{site_url} .= '/'
318              unless $param->{site_url} =~ /\/$/;
319            $param->{site_url} .= 'BLOG-NAME/';
320        }
321        else {
322            $param->{suggested_site_url} .= '/'
323              unless $param->{suggested_site_url} =~ /\/$/;
324            $param->{suggested_site_url} .= 'BLOG-NAME/';
325        }
326    }
327    1;
328}
329
330sub list {
331    my $app = shift;
332
333    $app->return_to_dashboard( redirect => 1 ) if $app->param('blog_id');
334
335    my $author    = $app->user;
336    my $list_pref = $app->list_pref('blog');
337
338    my $limit  = $list_pref->{rows};
339    my $offset = $app->param('offset') || 0;
340    my $args   = { offset => $offset, sort => 'name' };
341    $args->{limit} = $limit + 1;
342    unless ( $author->is_superuser ) {
343        $args->{join} = MT::Permission->join_on(
344            'blog_id',
345            { author_id => $author->id },
346            { unique    => 1 }
347        );
348    }
349    my $blog_class       = $app->model('blog');
350    my %param            = %$list_pref;
351    my @blogs            = $blog_class->load( undef, $args );
352    my $can_edit_authors = $author->is_superuser;
353    my $blog_loop        = make_blog_list( $app, \@blogs );
354
355    if ($blog_loop) {
356        ## We tried to load $limit + 1 entries above; if we actually got
357        ## $limit + 1 back, we know we have another page of entries.
358        my $have_next = @$blog_loop > $limit;
359        pop @$blog_loop while @$blog_loop > $limit;
360        if ($offset) {
361            $param{prev_offset}     = 1;
362            $param{prev_offset_val} = $offset - $limit;
363            $param{prev_offset_val} = 0 if $param{prev_offset_val} < 0;
364        }
365        if ($have_next) {
366            $param{next_offset}     = 1;
367            $param{next_offset_val} = $offset + $limit;
368        }
369    }
370    $param{offset}      = $offset;
371    $param{object_type} = 'blog';
372    $param{list_start}  = $offset + 1;
373    delete $args->{limit};
374    delete $args->{offset};
375    $param{list_total} = $blog_class->count( undef, $args );
376    $param{list_end}        = $offset + ( $blog_loop ? scalar @$blog_loop : 0 );
377    $param{next_max}        = $param{list_total} - $limit;
378    $param{next_max}        = 0 if ( $param{next_max} || 0 ) < $offset + 1;
379    $param{can_create_blog} = $author->can_create_blog;
380    $param{saved_deleted}   = $app->param('saved_deleted');
381    $param{refreshed}       = $app->param('refreshed');
382    $param{nav_blogs}       = 1;
383    $param{list_noncron}    = 1;
384    $param{search_label}    = $app->translate('Blogs');
385
386    if ($blog_loop) {
387        $param{object_loop} = $param{blog_table}[0]{object_loop} = $blog_loop;
388        $app->load_list_actions( 'blog', \%param );
389    }
390
391    $param{page_actions} = $app->page_actions('list_blog');
392    $param{feed_name}    = $app->translate("Blog Activity Feed");
393    $param{feed_url}     = $app->make_feed_link('blog');
394    $app->add_breadcrumb( $app->translate("Blogs") );
395    $param{nav_weblogs} = 1;
396    $param{object_label} = $blog_class->class_label;
397    $param{object_label_plural} = $blog_class->class_label_plural;
398    $param{screen_class} = "list-blog";
399    $param{screen_id} = "list-blog";
400    $param{listing_screen} = 1;
401    return $app->load_tmpl( 'list_blog.tmpl', \%param );
402}
403
404sub cfg_archives {
405    my $app = shift;
406    my %param;
407    %param = %{ $_[0] } if $_[0];
408    my $q = $app->param;
409
410    my $blog_id = $q->param('blog_id');
411
412    return $app->return_to_dashboard( redirect => 1 ) unless $blog_id;
413
414    my $blog = $app->model('blog')->load($blog_id);
415    my @data;
416    for my $at ( split /\s*,\s*/, $blog->archive_type ) {
417        my $archiver = $app->publisher->archiver($at);
418        next unless $archiver;
419        next if 'entry' ne $archiver->entry_class;
420        my $archive_label = $archiver->archive_label;
421        $archive_label = $at unless $archive_label;
422        $archive_label = $archive_label->() if ( ref $archive_label ) eq 'CODE';
423        push @data,
424          {
425            archive_type_translated => $archive_label,
426            archive_type            => $at,
427            archive_type_is_preferred =>
428              ( $blog->archive_type_preferred eq $at ? 1 : 0 ),
429          };
430    }
431    @data = sort { MT::App::CMS::archive_type_sorter( $a, $b ) } @data;
432    $param{entry_archive_types} = \@data;
433    $param{saved_deleted}       = 1 if $q->param('saved_deleted');
434    $param{saved_added}         = 1 if $q->param('saved_added');
435    $param{archives_changed}    = 1 if $q->param('archives_changed');
436    $param{no_writedir}         = $q->param('no_writedir');
437    $param{no_cachedir}         = $q->param('no_cachedir');
438    $param{no_writecache}       = $q->param('no_writecache');
439    $param{dynamic_none}        = $blog->custom_dynamic_templates eq 'none';
440    $param{dynamic_archives}    = $blog->custom_dynamic_templates eq 'archives';
441    $param{dynamic_custom}      = $blog->custom_dynamic_templates eq 'custom';
442    $param{dynamic_all}         = $blog->custom_dynamic_templates eq 'all';
443
444    if ( $app->config->ObjectDriver =~ qr/(db[id]::)?sqlite/i ) {
445        $param{hide_build_option} = 1
446          unless $app->config->UseSQLite2;
447    }
448    my $mtview_path = File::Spec->catfile( $blog->site_path(), "mtview.php" );
449
450    if ( -f $mtview_path ) {
451        open my ($fh), $mtview_path;
452        while ( my $line = <$fh> ) {
453            $param{dynamic_caching} = 1
454              if $line =~ m/^\s*\$mt->caching\s*=\s*true;/i;
455            $param{dynamic_conditional} = 1
456              if $line =~ /^\s*\$mt->conditional\s*=\s*true;/i;
457        }
458        close $fh;
459    }
460    $param{output} = 'cfg_archives.tmpl';
461    $q->param( '_type', 'blog' );
462    $q->param( 'id',    $blog_id );
463    $param{screen_class} = "settings-screen archive-settings";
464    $param{object_type}  = 'author';
465    $param{search_label} = $app->translate('Users');
466    $app->forward( "view", \%param );
467}
468
469sub cfg_prefs {
470    my $app     = shift;
471    my $q       = $app->param;
472    my $blog_id = scalar $q->param('blog_id');
473    return $app->return_to_dashboard( redirect => 1 )
474      unless $blog_id;
475    $q->param( '_type', 'blog' );
476    $q->param( 'id',    $blog_id );
477    my $blog_prefs = $app->user_blog_prefs;
478    my $perms      = $app->permissions;
479    return $app->error( $app->translate('Permission denied.') )
480      unless $app->user->is_superuser()
481      || (
482        $perms
483        && (   $perms->can_edit_config
484            || $perms->can_administer_blog
485            || $perms->can_set_publish_paths )
486      );
487    my $output = 'cfg_prefs.tmpl';
488    $app->forward("view",
489        {
490            output       => $output,
491            screen_class => 'settings-screen general-screen'
492        }
493    );
494}
495
496sub cfg_web_services {
497    my $app     = shift;
498    my $q       = $app->param;
499    my $blog_id = scalar $q->param('blog_id');
500    return $app->return_to_dashboard( redirect => 1 )
501      unless $blog_id;
502    $q->param( '_type', 'blog' );
503    $q->param( 'id',    scalar $q->param('blog_id') );
504    $app->forward( "view",
505        {
506            output       => 'cfg_web_services.tmpl',
507            screen_class => 'settings-screen web-services-settings'
508        }
509    );
510}
511
512sub rebuild_phase {
513    my $app  = shift;
514    my $type = $app->param('_type') || 'entry';
515    my @ids  = $app->param('id');
516    $app->{goback} = "window.location='" . $app->return_uri . "'";
517    $app->{value} ||= $app->translate('Go Back');
518    if ( $type eq 'entry' ) {
519        my %ids = map { $_ => 1 } @ids;
520        return $app->rebuild_these( \%ids );
521    }
522    elsif ( $type eq 'template' ) {
523        require MT::Template;
524        foreach (@ids) {
525            my $template = MT::Template->load($_);
526            $app->rebuild_indexes(
527                Template => $template,
528                Force    => 1
529            ) or return;
530        }
531    }
532    $app->call_return;
533}
534
535sub rebuild_pages {
536    my $app   = shift;
537    my $perms = $app->permissions
538      or return $app->error( $app->translate("No permissions") );
539    require MT::Entry;
540    require MT::Blog;
541    my $q             = $app->param;
542    my $blog_id       = $q->param('blog_id');
543    my $blog          = MT::Blog->load($blog_id);
544    my $order         = $q->param('type');
545    my @order         = split /,/, $order;
546    my $next          = $q->param('next');
547    my $done          = 0;
548    my $type          = $order[$next];
549    my $archiver      = $app->publisher->archiver($type);
550    my $archive_label = $archiver ? $archiver->archive_label : '';
551    $archive_label = $app->translate($type) unless $archive_label;
552    $archive_label = $archive_label->() if ( ref $archive_label ) eq 'CODE';
553    $next++;
554    $done++ if $next >= @order;
555    my $offset = 0;
556    my ($total) = $q->param('total');
557
558    ## Tells MT::_rebuild_entry_archive_type to cache loaded templates so
559    ## that each template is only loaded once.
560    $app->{cache_templates} = 1;
561
562    my ($tmpl_saved);
563
564    # Make sure errors go to a sensible place when in fs mode
565    # TODO: create contin. earlier, pass it thru
566    if ( $app->param('fs') ) {
567        my ( $type, $obj_id ) = $app->param('type') =~ m/(entry|index)-(\d+)/;
568        if ( $type && $obj_id ) {
569            my $edit_type = $type;
570            $edit_type = 'template' if $type eq 'index';
571            if ($type eq 'entry') {
572                require MT::Entry;
573                my $entry = MT::Entry->load($obj_id);
574                $edit_type = $entry->class;
575            }
576            $app->{goback} =
577              "window.location='"
578              . $app->object_edit_uri( $edit_type, $obj_id ) . "'";
579            $app->{value} ||= $app->translate('Go Back');
580        }
581    }
582
583    # FIXME: Wrap the entire rebuild operation with begin/end callbacks
584    if ( $type eq 'all' ) {
585        return $app->error( $app->translate("Permission denied.") )
586          unless $perms->can_rebuild;
587
588        # FIXME: Rebuild the entire blog????
589        $app->rebuild( BlogID => $blog_id )
590          or return $app->publish_error();
591    }
592    elsif ( $type eq 'index' ) {
593        return $app->error( $app->translate("Permission denied.") )
594          unless $perms->can_rebuild;
595        $app->rebuild_indexes( BlogID => $blog_id )
596            or return $app->publish_error();
597    }
598    elsif ( $type =~ /^index-(\d+)$/ ) {
599        return $app->error( $app->translate("Permission denied.") )
600          unless $perms->can_rebuild;
601        my $tmpl_id = $1;
602        require MT::Template;
603        $tmpl_saved = MT::Template->load($tmpl_id);
604        $app->rebuild_indexes(
605            BlogID   => $blog_id,
606            Template => $tmpl_saved,
607            Force    => 1
608        ) or return $app->publish_error();
609        $order = $app->translate( "index template '[_1]'", $tmpl_saved->name );
610    }
611    elsif ( $type =~ /^entry-(\d+)$/ ) {
612        my $entry_id = $1;
613        require MT::Entry;
614        my $entry = MT::Entry->load($entry_id);
615        return $app->error( $app->translate("Permission denied.") )
616          unless $perms->can_edit_entry( $entry, $app->user );
617        $app->rebuild_entry(
618            Entry             => $entry,
619            BuildDependencies => 1,
620            OldPrevious       => $q->param('old_previous'),
621            OldNext           => $q->param('old_next')
622        ) or return $app->publish_error();
623        $order = "entry '" . $entry->title . "'";
624    }
625    elsif ( $archiver->category_based ) {
626        return $app->error( $app->translate("Permission denied.") )
627          unless $perms->can_rebuild;
628        $offset = $q->param('offset') || 0;
629        my $start = time;
630        my $count = 0;
631        my $cb    = sub {
632            return 0 if time - $start > 20;    # 10 seconds
633            $count++;
634            return 1;
635        };
636        if ( $offset < $total ) {
637            $app->rebuild(
638                BlogID         => $blog_id,
639                ArchiveType    => $type,
640                NoIndexes      => 1,
641                Offset         => $offset,
642                FilterCallback => $cb,
643            ) or return $app->publish_error();
644            $offset += $count;
645        }
646        if ( $offset < $total ) {
647            $done-- if $done;
648            $next--;
649        }
650        else {
651            $offset = 0;
652        }
653    }
654    elsif ($type) {
655        my $special = 0;
656        my @options = $app->{rebuild_options} ||= {};
657        $app->run_callbacks( 'rebuild_options', $app, \@options );
658        for my $optn (@options) {
659            if ( ( $optn->{key} || '' ) eq $type ) {
660                $optn->{code}->();
661                $special = 1;
662            }
663        }
664        if ( !$special ) {
665            return $app->error( $app->translate("Permission denied.") )
666              unless $perms->can_rebuild;
667            $offset = $q->param('offset') || 0;
668            if ( $offset < $total ) {
669                my $start = time;
670                my $count = 0;
671                my $cb    = sub {
672                    return 0 if time - $start > 20;    # 10 seconds
673                    $count++;
674                    return 1;
675                };
676                $app->rebuild(
677                    BlogID         => $blog_id,
678                    ArchiveType    => $type,
679                    NoIndexes      => 1,
680                    Offset         => $offset,
681                    FilterCallback => $cb,
682                ) or return $app->publish_error();
683                $offset += $count;
684            }
685            if ( $offset < $total ) {
686                $done-- if $done;
687                $next--;
688            }
689            else {
690                $offset = 0;
691            }
692        }
693    }
694
695    # Rebuild done--now form the continuation.
696    unless ($done) {
697        my $dynamic   = 0;
698        my $type_name = $order[$next];
699
700        ## If we're moving on to the next rebuild step, recalculate the
701        ## limit.
702        my $static_count;
703        if ( $type_name !~ m/^index/ ) {
704            $static_count = $blog->count_static_templates($type_name) || 0;
705        }
706        else {
707            $static_count = 1;
708        }
709        if ( !$static_count ) {
710            $dynamic = 1;
711        }
712        elsif ( defined($offset) && $offset == 0 ) {
713            $dynamic = 0;
714        }
715        if ( $offset == 0 ) {
716
717            # determine total
718            if ( my $archiver = $app->publisher->archiver($type_name) ) {
719                if ( $archiver->entry_based || $archiver->date_based ) {
720                    my $entry_class = $archiver->entry_class || 'entry';
721                    require MT::Entry;
722                    my $terms = {
723                        class   => $entry_class,
724                        status  => MT::Entry::RELEASE(),
725                        blog_id => $blog_id,
726                    };
727                    $total = MT::Entry->count($terms);
728                }
729                elsif ( $archiver->category_based ) {
730                    require MT::Category;
731                    my $terms = { blog_id => $blog_id, };
732                    $total = MT::Category->count($terms);
733                }
734                elsif ( $archiver->author_based ) {
735                    require MT::Author;
736                    require MT::Entry;
737                    my $terms = {
738                        blog_id => $blog_id,
739                        status  => MT::Entry::RELEASE(),
740                        class   => 'entry',
741                    };
742                    $total = MT::Author->count(
743                        undef,
744                        {
745                            join   => MT::Entry->join_on( 'author_id', $terms, { unique => 1 } ),
746                            unique => 1,
747                        }
748                    );
749                }
750            }
751        }
752
753        my $type = $order[$next];
754        if ($type) {
755            $archiver      = $app->publisher->archiver($type);
756            $archive_label = $archiver ? $archiver->archive_label : '';
757            $archive_label = $app->translate($type) unless $archive_label;
758            $archive_label = $archive_label->()
759              if ( ref $archive_label ) eq 'CODE';
760        }
761
762        my $complete =
763          $total
764          ? ( $total == $offset ? 100 : int( ( $offset / $total ) * 100 ) )
765          : 0;
766
767        my %param = (
768            build_type      => $order,
769            build_next      => $next,
770            build_type_name => $archive_label,
771            archives        => $archiver ? 1 : 0,
772            total           => $total,
773            offset          => $offset,
774            complete        => $complete,
775            incomplete      => 100 - $complete,
776            entry_id        => scalar $q->param('entry_id'),
777            dynamic         => $dynamic,
778            is_new          => scalar $q->param('is_new'),
779            old_status      => scalar $q->param('old_status')
780        );
781        $app->load_tmpl( 'rebuilding.tmpl', \%param );
782    }
783    else {
784        if ( $q->param('entry_id') ) {
785            require MT::Entry;
786            my $entry = MT::Entry->load( scalar $q->param('entry_id') );
787            require MT::Blog;
788            my $blog = MT::Blog->load( $entry->blog_id );
789            require MT::CMS::Entry;
790            MT::CMS::Entry::ping_continuation( $app,
791                $entry, $blog,
792                OldStatus => scalar $q->param('old_status'),
793                IsNew     => scalar $q->param('is_new'),
794            );
795        }
796        else {
797            my $all          = $order =~ /,/;
798            my $type         = $order;
799            my $is_one_index = $order =~ /index template/;
800            my $is_entry     = $order =~ /entry/;
801            my $built_type;
802            if ( $is_entry || $is_one_index ) {
803                ( $built_type = $type ) =~
804                  s/^(entry|index template)/$app->translate($1)/e;
805            }
806            else {
807                $built_type = $app->translate($type);
808            }
809            my %param = (
810                all          => $all,
811                type         => $archive_label,
812                is_one_index => $is_one_index,
813                is_entry     => $is_entry,
814                archives     => $type ne 'index',
815            );
816            if ($is_one_index) {
817                $param{tmpl_url} = $blog->site_url;
818                $param{tmpl_url} .= '/' if $param{tmpl_url} !~ m!/$!;
819                $param{tmpl_url} .= $tmpl_saved->outfile;
820            }
821            if ( $q->param('fs') ) {    # full screen--go to a useful app page
822                my $type = $q->param('type');
823                $type =~ /index-(\d+)/;
824                my $tmpl_id = $1;
825                $app->run_callbacks( 'rebuild', $blog );
826                return $app->redirect(
827                    $app->uri(
828                        'mode' => 'view',
829                        args   => {
830                            '_type'       => 'template',
831                            id            => $tmpl_id,
832                            blog_id       => $blog->id,
833                            saved_rebuild => 1
834                        }
835                    )
836                );
837            }
838            else {    # popup--just go to cnfrmn. page
839                $app->run_callbacks( 'rebuild', $blog );
840                return $app->load_tmpl( 'popup/rebuilt.tmpl', \%param );
841            }
842        }
843    }
844}
845
846sub rebuild_new_phase {
847    my ($app) = @_;
848    my %reb_set = map { $_ => 1 } $app->param('id');
849    $app->rebuild_these( \%reb_set, how => MT::App::CMS::NEW_PHASE() );
850}
851
852sub start_rebuild_pages {
853    my $app           = shift;
854    my $q             = $app->param;
855    my $type          = $q->param('type');
856    my $next          = $q->param('next') || 0;
857    my @order         = split /,/, $type;
858    my $total         = $q->param('total') || 0;
859    my $type_name     = $order[$next];
860    my $archiver      = $app->publisher->archiver($type_name);
861    my $archive_label = $archiver ? $archiver->archive_label : '';
862    $archive_label = $app->translate($type_name) unless $archive_label;
863    $archive_label = $archive_label->() if ( ref $archive_label ) eq 'CODE';
864    my $blog_id = $q->param('blog_id');
865
866    if ($archiver) {
867        if ( $archiver->entry_based || $archiver->date_based ) {
868            my $entry_class = $archiver->entry_class || 'entry';
869            require MT::Entry;
870            my $terms = {
871                class   => $entry_class,
872                status  => MT::Entry::RELEASE(),
873                blog_id => $blog_id,
874            };
875            $total = MT::Entry->count($terms);
876        }
877        elsif ( $archiver->category_based ) {
878            require MT::Category;
879            my $terms = {
880                blog_id => $blog_id,
881                class   => $archiver->category_class,
882            };
883            $total = MT::Category->count($terms);
884        }
885        elsif ( $archiver->author_based ) {
886            require MT::Author;
887            require MT::Entry;
888            my $terms = {
889                blog_id => $blog_id,
890                status  => MT::Entry::RELEASE(),
891                class => 'entry',
892            };
893            $total = MT::Author->count(
894                undef,
895                {
896                    join   => MT::Entry->join_on( 'author_id', $terms, { unique => 1 } ),
897                    unique => 1,
898                }
899            );
900        }
901    }
902
903    my %param = (
904        build_type      => $type,
905        build_next      => $next,
906        total           => $total,
907        complete        => 0,
908        incomplete      => 100,
909        build_type_name => $archive_label
910    );
911
912    if ( $type_name =~ /^index-(\d+)$/ ) {
913        my $tmpl_id = $1;
914        require MT::Template;
915        my $tmpl = MT::Template->load($tmpl_id);
916        $param{build_type_name} =
917          $app->translate( "index template '[_1]'", $tmpl->name );
918        $param{is_one_index} = 1;
919    }
920    elsif ( $type_name =~ /^entry-(\d+)$/ ) {
921        my $entry_id = $1;
922        require MT::Entry;
923        my $entry = MT::Entry->load($entry_id);
924        $param{build_type_name} =
925          $app->translate( "[_1] '[_2]'", $entry->class_label, $entry->title );
926        $param{is_entry} = 1;
927        $param{entry_id} = $entry_id;
928        for my $col (qw( is_new old_status old_next old_previous )) {
929            $param{$col} = $q->param($col);
930        }
931    }
932    $param{is_full_screen} = ( $param{is_entry} )
933      || $q->param('single_template');
934    $param{page_titles} = [ { bc_name => 'Rebuilding' } ];
935    $app->load_tmpl( 'rebuilding.tmpl', \%param );
936}
937
938sub rebuild_confirm {
939    my $app     = shift;
940    my $blog_id = $app->param('blog_id');
941    require MT::Blog;
942    my $blog = MT::Blog->load($blog_id);
943    my $at = $blog->archive_type || '';
944    my ( @blog_at, @at, @data );
945    my $archiver;
946    my $archive_label;
947
948    if ( $at && $at ne 'None' ) {
949        @blog_at = split /,/, $at;
950        foreach my $t (@blog_at) {
951            $archiver = $app->publisher->archiver($t);
952            next unless $archiver;    # ignore unknown archive types
953            push @at, $t;
954            $archive_label = $archiver->archive_label;
955            $archive_label = $at unless $archive_label;
956            $archive_label = $archive_label->()
957              if ( ref $archive_label ) eq 'CODE';
958            push(
959                @data,
960                {
961                    archive_type       => $t,
962                    archive_type_label => $archive_label
963                }
964            );
965        }
966    }
967    my $order     = join ',', @at, 'index';
968    my $entry_pkg = $app->model('entry');
969    my %param     = (
970        archive_type_loop => \@data,
971        build_order       => $order,
972        build_next        => 0,
973    );
974    $param{index_selected} = ( $app->param('prompt') || "" ) eq 'index';
975
976    if ( my $tmpl_id = $app->param('tmpl_id') ) {
977        require MT::Template;
978        my $tmpl = MT::Template->load($tmpl_id);
979        $param{index_tmpl_id}   = $tmpl->id;
980        $param{index_tmpl_name} = $tmpl->name;
981    }
982    my $options = $app->registry("rebuild_options") || {};
983    my @options;
984    if ($options) {
985        foreach my $opt ( keys %$options ) {
986            $options->{$opt}{key} ||= $opt;
987            push @options, $options->{$opt};
988        }
989    }
990    $app->run_callbacks( 'rebuild_options', $app, \@options );
991    my $rebuild_options = $app->filter_conditional_list( \@options );
992    $param{rebuild_option_loop} = $rebuild_options;
993    $param{refocus}             = 1;
994    $app->add_breadcrumb( $app->translate('Publish Site') );
995    $app->load_tmpl( 'popup/rebuild_confirm.tmpl', \%param );
996}
997
998sub save_favorite_blogs {
999    my $app = shift;
1000    $app->validate_magic() or return;
1001    my $fav = $app->param('id');
1002    return unless int($fav) > 0;
1003    $app->add_to_favorite_blogs($fav);
1004    $app->send_http_header("text/javascript+json");
1005    return 'true';
1006}
1007
1008sub cc_return {
1009    my $app   = shift;
1010    my $code  = $app->param('license_code');
1011    my $url   = $app->param('license_url');
1012    my $image = $app->param('license_button');
1013    my %param = ( license_name => MT::Util::cc_name($code) );
1014    if ($url) {
1015        $param{license_code} = "$code $url $image";
1016    }
1017    else {
1018        $param{license_code} = $code;
1019    }
1020    $app->load_tmpl( 'cc_return.tmpl', \%param );
1021}
1022
1023sub handshake {
1024    my $app               = shift;
1025    my $blog_id           = $app->param('blog_id');
1026    my $remote_auth_token = $app->param('remote_auth_token');
1027
1028    my %param = ();
1029    $param{remote_auth_token} = $remote_auth_token;
1030    $app->load_tmpl( 'handshake_return.tmpl', \%param );
1031}
1032
1033sub update_welcome_message {
1034    my $app = shift;
1035    $app->validate_magic or return;
1036
1037    my $perms = $app->permissions;
1038    return $app->errtrans("Permission denied.")
1039      unless $perms && $perms->can_edit_config;
1040
1041    my $blog_id    = $app->param('blog_id');
1042    my $message    = $app->param('welcome-message-text');
1043    my $blog_class = $app->model('blog');
1044    my $blog       = $blog_class->load($blog_id)
1045      or return $app->error( $app->translate("Invalid blog") );
1046    $blog->welcome_msg($message);
1047    $blog->save;
1048    $app->redirect(
1049        $app->uri( mode => 'menu', args => { blog_id => $blog_id } ) );
1050}
1051
1052sub dialog_select_weblog {
1053    my $app = shift;
1054
1055    #return $app->errtrans("Permission denied.")
1056    #    unless $app->user->is_superuser;
1057
1058    my $favorites = $app->param('select_favorites');
1059    my %favorite;
1060    my $confirm_js;
1061    my $terms = {};
1062    my $args  = {};
1063    if ($favorites) {
1064        my $auth = $app->user or return;
1065        if ( my @favs = @{ $auth->favorite_blogs || [] } ) {
1066            $terms->{id} = \@favs;
1067            $args->{not}{id} = 1;
1068        }
1069        $confirm_js = 'saveFavorite';
1070    }
1071
1072    my $hasher = sub {
1073        my ( $obj, $row ) = @_;
1074        $row->{label} = $row->{name};
1075        $row->{'link'} = $row->{site_url};
1076    };
1077
1078    $app->listing(
1079        {
1080            type     => 'blog',
1081            code     => $hasher,
1082            template => 'dialog/select_weblog.tmpl',
1083            terms    => $terms,
1084            args     => $args,
1085            params   => {
1086                dialog_title  => $app->translate("Select Blog"),
1087                items_prompt  => $app->translate("Selected Blog"),
1088                search_prompt => $app->translate(
1089                    "Type a blog name to filter the choices below."),
1090                panel_label       => $app->translate("Blog Name"),
1091                panel_description => $app->translate("Description"),
1092                panel_type        => 'blog',
1093                panel_multi       => defined $app->param('multi')
1094                ? $app->param('multi')
1095                : 0,
1096                panel_searchable => 1,
1097                panel_first      => 1,
1098                panel_last       => 1,
1099                list_noncron     => 1,
1100                return_url       => $app->uri . '?'
1101                  . ( $app->param('return_args') || '' ),
1102                confirm_js => $confirm_js,
1103                idfield    => ( $app->param('idfield') || '' ),
1104                namefield  => ( $app->param('namefield') || '' ),
1105            },
1106        }
1107    );
1108}
1109
1110sub can_view {
1111    my ( $eh, $app, $id ) = @_;
1112    my $perms = $app->permissions;
1113    if ( $id
1114        && ( $perms->can_set_publish_paths && !$perms->can_administer_blog ) )
1115    {
1116        return 1 if 'view' eq $app->mode;
1117    }
1118    if (
1119        (
1120            $id && !(
1121                   $perms->can_edit_config
1122                || $perms->can_set_publish_paths
1123                || $perms->can_manage_feedback
1124            )
1125        )
1126        || ( !$id && !$app->user->can_create_blog )
1127      )
1128    {
1129        return 0;
1130    }
1131    1;
1132}
1133
1134sub can_save {
1135    my ( $eh, $app, $id ) = @_;
1136    my $perms = $app->permissions;
1137    return ( $id
1138          && ( $perms->can_edit_config || $perms->can_set_publish_paths ) )
1139      || ( !$id && $app->user->can_create_blog );
1140}
1141
1142sub can_delete {
1143    my ( $eh, $app, $obj ) = @_;
1144    my $author = $app->user;
1145    return 1 if $author->is_superuser();
1146    require MT::Permission;
1147    my $perms = $author->permissions( $obj->id );
1148    return $perms && $perms->can_administer_blog;
1149}
1150
1151sub pre_save {
1152    my $eh = shift;
1153    my ( $app, $obj ) = @_;
1154    if ( !$app->param('overlay')
1155        && $app->param('cfg_screen') )
1156    {
1157
1158        # Checkbox options have to be blanked if they aren't passed.
1159        my $screen = $app->param('cfg_screen');
1160        my @fields;
1161        if ( $screen eq 'cfg_web_services' ) {
1162        }
1163        elsif ( $screen eq 'cfg_archives' ) {
1164            @fields = qw(file_extension);
1165        }
1166        elsif ( $screen eq 'cfg_templatemaps' ) {
1167        }
1168        elsif ( $screen eq 'cfg_comments' ) {
1169            @fields = qw( allow_comment_html autolink_urls
1170              use_comment_confirmation );
1171        }
1172        elsif ( $screen eq 'cfg_registration' ) {
1173            @fields = qw( allow_commenter_regist
1174              require_comment_emails allow_unreg_comments
1175              require_typekey_emails );
1176        }
1177        elsif ( $screen eq 'cfg_entry' ) {
1178            @fields = qw( allow_comments_default
1179              allow_pings_default );
1180        }
1181        elsif ( $screen eq 'cfg_trackbacks' ) {
1182            @fields = qw( allow_pings moderate_pings
1183              autodiscover_links internal_autodiscovery );
1184        }
1185        elsif ( $screen eq 'cfg_plugins' ) {
1186        }
1187        for my $cb (@fields) {
1188            unless ( defined $app->param($cb) ) {
1189
1190      # two possibilities: user unchecked the option, or user was not allowed to
1191      # set the value (and therefore there was no field to submit).
1192                my $perms = $app->permissions;
1193                if (
1194                    $app->user->is_superuser
1195                    || (
1196                        $perms
1197                        && (   $perms->can_administer_blog
1198                            || $perms->can_edit_config )
1199                    )
1200                  )
1201                {
1202                    $obj->$cb(0);
1203                }
1204                else {
1205                    delete $obj->{column_values}->{$cb};
1206                    delete $obj->{changed_cols}->{$cb};
1207                }
1208            }
1209        }
1210        if ( $screen eq 'cfg_comments' ) {
1211
1212            # value for comments:  1 == Accept from anyone
1213            #                      2 == Accept authenticated only
1214            #                      0 == No comments
1215            if ( $app->param('allow_comments') ) {
1216                $obj->allow_reg_comments(1);
1217            }
1218            else {
1219                $obj->allow_unreg_comments(0);
1220                $obj->allow_reg_comments(0);
1221            }
1222            $obj->moderate_unreg_comments( $app->param('moderate_comments') );
1223            $obj->nofollow_urls( $app->param('nofollow_urls')         ? 1 : 0 );
1224            $obj->follow_auth_links( $app->param('follow_auth_links') ? 1 : 0 );
1225            my $cp_old = $obj->captcha_provider;
1226            $obj->captcha_provider( $app->param('captcha_provider') );
1227            my $rebuild = $cp_old ne $obj->captcha_provider ? 1 : 0;
1228            $app->add_return_arg( need_full_rebuild => 1 ) if $rebuild;
1229        }
1230        if ( $screen eq 'cfg_web_services' ) {
1231            my $tok = '';
1232            ( $tok = $obj->remote_auth_token ) =~ s/\s//g;
1233            $obj->remote_auth_token($tok);
1234
1235            my $ping_servers = $app->registry('ping_servers');
1236            my @pings_list;
1237            push @pings_list, $_ foreach grep {
1238                defined( $app->param( 'ping_' . $_ ) )
1239                  && $app->param( 'ping_' . $_ )
1240              }
1241              keys %$ping_servers;
1242            $obj->update_pings( join( ',', @pings_list ) );
1243        }
1244        if ( $screen eq 'cfg_entry' ) {
1245            my %param = $_[0] ? %{ $_[0] } : ();
1246            my $pref_param = $app->load_entry_prefs;
1247            %param = ( %param, %$pref_param );
1248        }
1249        if ( $screen eq 'cfg_trackbacks' ) {
1250            if ( my $pings = $app->param('allow_pings') ) {
1251                if ($pings) {
1252                    $obj->moderate_pings( $app->param('moderate_pings') );
1253                    $obj->nofollow_urls( $app->param('nofollow_urls') ? 1 : 0 );
1254                }
1255                else {
1256                    $obj->moderate_pings(1);
1257                    $obj->email_new_pings(1);
1258                }
1259            }
1260        }
1261        if ( $screen eq 'cfg_registration' ) {
1262            $obj->allow_commenter_regist(
1263                $app->param('allow_commenter_regist') );
1264            $obj->allow_unreg_comments( $app->param('allow_unreg_comments') );
1265            if ( $app->param('allow_unreg_comments') ) {
1266                $obj->require_comment_emails(
1267                    $app->param('require_comment_emails') );
1268            }
1269            else {
1270                $obj->require_comment_emails(0);
1271            }
1272            my @authenticators;
1273
1274            my $c = $app->registry('commenter_authenticators');
1275            foreach ( keys %$c ) {
1276                if ( $app->param( 'enabled_' . $_ ) ) {
1277                    push @authenticators, $_;
1278                }
1279            }
1280            push @authenticators, 'MovableType'
1281              if $app->param('enabled_MovableType');
1282            my $c_old = $obj->commenter_authenticators;
1283            $obj->commenter_authenticators( join( ',', @authenticators ) );
1284            my $rebuild = $obj->commenter_authenticators ne $c_old ? 1 : 0;
1285            if ( $app->param('enabled_TypeKey') ) {
1286                $rebuild = $obj->require_typekey_emails ? 0 : 1;
1287                $obj->require_typekey_emails(
1288                    $app->param('require_typekey_emails') );
1289            }
1290            else {
1291                $obj->require_typekey_emails(0);
1292            }
1293            my $tok = '';
1294            ( $tok = $obj->remote_auth_token ) =~ s/\s//g;
1295            $obj->remote_auth_token($tok);
1296
1297            $app->add_return_arg( need_full_rebuild => 1 ) if $rebuild;
1298        }
1299        if ( $screen eq 'cfg_spam' ) {
1300            my $threshold = $app->param('junk_score_threshold');
1301            $threshold =~ s/\+//;
1302            $threshold ||= 0;
1303            $obj->junk_score_threshold($threshold);
1304            if ( my $expiry = $app->param('junk_folder_expiry') ) {
1305                $obj->junk_folder_expiry($expiry);
1306            }
1307            my $perms = $app->permissions;
1308            unless ( defined $app->param('auto_delete_junk') ) {
1309                if (
1310                    $app->user->is_superuser
1311                    || (
1312                        $perms
1313                        && (   $perms->can_administer_blog
1314                            || $perms->can_edit_config )
1315                    )
1316                  )
1317                {
1318                    $obj->junk_folder_expiry(0);
1319                }
1320                else {
1321                    delete $obj->{column_values}->{junk_folder_expiry};
1322                    delete $obj->{changed_cols}->{junk_folder_expiry};
1323                }
1324            }
1325        }
1326        if ( $screen eq 'cfg_entry' ) {
1327            my %param = $_[0] ? %{ $_[0] } : ();
1328            my $pref_param = $app->load_entry_prefs;
1329            %param = ( %param, %$pref_param );
1330            $param{ 'sort_order_posts_' . ( $obj->sort_order_posts || 0 ) } = 1;
1331            $param{words_in_excerpt} = 40
1332              unless defined $param{words_in_excerpt}
1333              && $param{words_in_excerpt} ne '';
1334            if ( $app->param('days_or_posts') eq 'days' ) {
1335                $obj->days_on_index( $app->param('list_on_index') );
1336                $obj->entries_on_index(0);
1337            }
1338            else {
1339                $obj->entries_on_index( $app->param('list_on_index') );
1340                $obj->days_on_index(0);
1341            }
1342            $obj->basename_limit(15)
1343              if $obj->basename_limit < 15;    # 15 is the *minimum*
1344            $obj->basename_limit(250)
1345              if $obj->basename_limit > 250;    # 15 is the *maximum*
1346        }
1347        if ( $screen eq 'cfg_archives' ) {
1348            if ( my $dcty = $app->param('dynamicity') ) {
1349                $obj->custom_dynamic_templates($dcty);
1350            }
1351            if ( !$app->param('enable_archive_paths') ) {
1352                $obj->archive_url('');
1353                $obj->archive_path('');
1354            }
1355        }
1356    }
1357    else {
1358
1359        #$obj->is_dynamic(0) unless defined $app->{query}->param('is_dynamic');
1360    }
1361
1362    if ( ( $obj->sanitize_spec || '' ) eq '1' ) {
1363        $obj->sanitize_spec( scalar $app->param('sanitize_spec_manual') );
1364    }
1365
1366    1;
1367}
1368
1369sub post_save {
1370    my $eh = shift;
1371    my ( $app, $obj, $original ) = @_;
1372
1373    my $perms = $app->permissions;
1374    return 1
1375      unless $app->user->is_superuser
1376      || $app->user->can_create_blog
1377      || ( $perms && $perms->can_edit_config );
1378
1379    my $screen = $app->param('cfg_screen') || '';
1380    if ( $screen eq 'cfg_archives' ) {
1381        if ( my $dcty = $app->param('dynamicity') ) {
1382            my $dcty_changed = $dcty ne $original->custom_dynamic_templates ? 1 : 0;
1383
1384            if ($dcty_changed) {
1385                if ( $dcty eq 'none' ) {
1386                    require MT::Template;
1387                    my @tmpls = MT::Template->load({
1388                        blog_id       => $obj->id,
1389                        build_dynamic => 1,
1390                    });
1391                    for my $tmpl (@tmpls) {
1392                        $tmpl->build_dynamic(0);
1393                        $tmpl->save;
1394                    }
1395                }
1396                update_dynamicity(
1397                    $app,
1398                    $obj,
1399                    $app->param('dynamic_cache')       ? 1 : 0,
1400                    $app->param('dynamic_conditional') ? 1 : 0
1401                );
1402            }
1403
1404            # If either of the publishing paths changed, rebuild the fileinfos.
1405            my $path_changed = 0;
1406            for my $path_field (qw( site_path archive_path site_url archive_url )) {
1407                $path_changed = 1 && last if $app->param($path_field)
1408                    && $app->param($path_field) ne $original->$path_field();
1409            }
1410            if ($dcty ne 'none' && ($dcty_changed || $path_changed)) {
1411                $app->rebuild( BlogID => $obj->id, NoStatic => 1 )
1412                    or return $app->publish_error();
1413            }
1414        }
1415
1416        cfg_archives_save($app, $obj) or return;
1417    }
1418    if ( $screen eq 'cfg_prefs' ) {
1419        my $blog_id = $obj->id;
1420
1421        # FIXME: Needs to exclude MT::Permission records for groups
1422        $app->model('permission')
1423          ->load( { blog_id => $blog_id, author_id => 0 } );
1424        if ( !$perms ) {
1425            $perms = $app->model('permission')->new;
1426            $perms->blog_id($blog_id);
1427            $perms->author_id(0);
1428        }
1429    }
1430    if ( $screen eq 'cfg_entry' ) {
1431        my $blog_id = $obj->id;
1432
1433        # FIXME: Needs to exclude MT::Permission records for groups
1434        my $perms =
1435          $app->model('permission')
1436          ->load( { blog_id => $blog_id, author_id => 0 } );
1437        if ( !$perms ) {
1438            $perms = $app->model('permission')->new;
1439            $perms->blog_id($blog_id);
1440            $perms->author_id(0);
1441        }
1442        my $prefs = $app->_entry_prefs_from_params;
1443        if ($prefs) {
1444            $perms->entry_prefs($prefs);
1445            $perms->save
1446              or return $app->errtrans( "Saving permissions failed: [_1]",
1447                $perms->errstr );
1448        }
1449    }
1450
1451    if ( !$original->id ) {    # If the object is new, the "orignal" was blank
1452        ## If this is a new blog, we need to set up a permissions
1453        ## record for the existing user.
1454        $obj->create_default_templates( $obj->template_set );
1455
1456        # Add this blog to the user's "favorite blogs", pushing any 10th
1457        # blog off the list
1458        my $auth = $app->user;
1459
1460        # FIXME: Should we still be doing this?
1461        my $perms = $app->model('permission')->new;
1462        $perms->author_id( $auth->id );
1463        $perms->blog_id( $obj->id );
1464        $perms->set_full_permissions;
1465        $perms->save;
1466
1467        require MT::Log;
1468        $app->log(
1469            {
1470                message => $app->translate(
1471                    "Blog '[_1]' (ID:[_2]) created by '[_3]'",
1472                    $obj->name, $obj->id, $auth->name
1473                ),
1474                level    => MT::Log::INFO(),
1475                class    => 'blog',
1476                category => 'new',
1477            }
1478        );
1479
1480        $app->run_callbacks( 'blog_template_set_change', { blog => $obj } );
1481    }
1482    else {
1483
1484        # if you've changed the comment configuration
1485        if (
1486            (
1487                grep { $original->column($_) ne $obj->column($_) }
1488                qw(allow_unreg_comments allow_reg_comments remote_auth_token)
1489            )
1490          )
1491        {
1492            if ( RegistrationAffectsArchives( $obj->id, 'Individual' ) ) {
1493                $app->add_return_arg( need_full_rebuild => 1 );
1494            }
1495            else {
1496                $app->add_return_arg( need_index_rebuild => 1 );
1497            }
1498        }
1499
1500        # if other settings were changed that would affect published pages:
1501        if ( grep { $original->column($_) ne $obj->column($_) }
1502            qw(allow_pings allow_comment_html) )
1503        {
1504            $app->add_return_arg( need_full_rebuild => 1 );
1505        }
1506
1507        if ( ($original->template_set || '') ne ($obj->template_set || '') ) {
1508            $app->run_callbacks( 'blog_template_set_change', { blog => $obj } );
1509            $app->add_return_arg( need_full_rebuild => 1 );
1510        }
1511    }
1512    1;
1513}
1514
1515sub save_filter {
1516    my $eh    = shift;
1517    my ($app) = @_;
1518    my $name  = $app->param('name');
1519    if ( defined $name ) {
1520        $name =~ s/(^\s+|\s+$)//g;
1521        $app->param( 'name', $name );
1522    }
1523    my $perms = $app->permissions;
1524    my $screen = $app->param('cfg_screen') || '';
1525    return $eh->error( MT->translate("You did not specify a blog name.") )
1526      if ( !( $screen && $perms->can_edit_config )
1527        && ( defined $app->param('name') && ( $app->param('name') eq '' ) ) );
1528    return $eh->error( MT->translate("Site URL must be an absolute URL.") )
1529      if ( $screen eq 'cfg_archives' )
1530      && $perms->can_set_publish_paths
1531      && $app->param('site_url') !~ m.^https?://.;
1532    return $eh->error( MT->translate("Archive URL must be an absolute URL.") )
1533      if ( $screen eq 'cfg_archives' )
1534      && $perms->can_set_publish_paths
1535      && $app->param('archive_url') !~ m.^https?://.
1536      && $app->param('enable_archive_paths');
1537    return $eh->error( MT->translate("You did not specify an Archive Root.") )
1538      if ( $screen eq 'cfg_archives' )
1539      && $app->param('archive_path') =~ m/^\s*$/
1540      && $app->param('enable_archive_paths');
1541    return 1;
1542}
1543
1544sub post_delete {
1545    my ( $eh, $app, $obj ) = @_;
1546
1547    $app->log(
1548        {
1549            message => $app->translate(
1550                "Blog '[_1]' (ID:[_2]) deleted by '[_3]'",
1551                $obj->name, $obj->id, $app->user->name
1552            ),
1553            level    => MT::Log::INFO(),
1554            class    => 'blog',
1555            category => 'delete'
1556        }
1557    );
1558    $app->_delete_pseudo_association(undef, $obj->id);
1559}
1560
1561sub make_blog_list {
1562    my $app = shift;
1563    my ($blogs) = @_;
1564
1565    my $tbp_class     = $app->model('ping');
1566    my $entry_class   = $app->model('entry');
1567    my $comment_class = $app->model('comment');
1568    my $author        = $app->user;
1569    my $data;
1570    my $i;
1571    my @blog_ids;
1572    push @blog_ids, $_->id for @$blogs;
1573    my ( $entry_count, $ping_count, $comment_count );
1574    my $can_edit_authors = 1 if $author->is_superuser;
1575
1576    for my $blog (@$blogs) {
1577        my $blog_id = $blog->id;
1578        my $perms   = $author->permissions($blog_id);
1579        my $row     = {
1580            id          => $blog->id,
1581            name        => $blog->name,
1582            description => $blog->description,
1583            site_url    => $blog->site_url
1584        };
1585
1586        # we should use count by group here...
1587        $row->{num_entries} =
1588          ( $entry_count ? $entry_count->{$blog_id} : $entry_count->{$blog_id} =
1589              MT::Entry->count( { blog_id => $blog_id } ) )
1590          || 0;
1591        $row->{num_comments} = (
1592              $comment_count
1593            ? $comment_count->{$blog_id}
1594            : $comment_count->{$blog_id} = MT::Comment->count(
1595                { blog_id => $blog_id, junk_status => [ 0, 1 ] }
1596            )
1597          )
1598          || 0;
1599        $row->{num_pings} = (
1600            $ping_count ? $ping_count->{$blog_id} : $ping_count->{$blog_id} =
1601              MT::TBPing->count(
1602                { blog_id => $blog_id, junk_status => [ 0, 1 ] }
1603              )
1604        ) || 0;
1605        $row->{num_authors} = 0;
1606
1607        # FIXME: this isn't efficient
1608        my $iter = MT::Permission->load_iter(
1609            {
1610                blog_id => [ 0, $blog_id ],
1611
1612                #    role_mask => [ 2, undef ]
1613                #}, {
1614                #    range_incl => { 'role_mask' => 1 }
1615            }
1616        );
1617        my %a;
1618        while ( my $p = $iter->() ) {
1619            next if exists $a{ $p->author_id };
1620            $a{ $p->author_id } = 1;
1621            $row->{num_authors}++ if $p->can_create_post;
1622        }
1623        $row->{can_create_post}  = $perms->can_create_post;
1624        $row->{can_edit_entries} = $perms->can_create_post
1625          || $perms->can_edit_all_posts
1626          || $perms->can_publish_post;
1627        $row->{can_edit_templates} = $perms->can_edit_templates;
1628        $row->{can_edit_config}    = $perms->can_edit_config
1629          || $perms->can_administer_blog;
1630        $row->{can_set_publish_paths} = $perms->can_set_publish_paths
1631          || $perms->can_administer_blog;
1632        $row->{can_manage_feedback} = $perms->can_manage_feedback;
1633        $row->{can_edit_assets}     = $perms->can_edit_assets;
1634        $row->{can_administer_blog} = $perms->can_administer_blog;
1635        push @$data, $row;
1636    }
1637    $data;
1638}
1639
1640sub build_blog_table {
1641    my $app = shift;
1642    my (%args) = @_;
1643
1644    my $blog_class    = $app->model('blog');
1645    my $tbp_class     = $app->model('ping');
1646    my $entry_class   = $app->model('entry');
1647    my $comment_class = $app->model('comment');
1648
1649    my $iter;
1650    if ( $args{load_args} ) {
1651        my $class = $app->model('blog');
1652        $iter = $class->load_iter( @{ $args{load_args} } );
1653    }
1654    elsif ( $args{iter} ) {
1655        $iter = $args{iter};
1656    }
1657    elsif ( $args{items} ) {
1658        $iter = sub { pop @{ $args{items} } };
1659    }
1660    return [] unless $iter;
1661    my $param = $args{param};
1662
1663    my $author           = $app->user;
1664    my $can_edit_authors = $author->is_superuser;
1665    my @data;
1666    my $i;
1667    my ( $entry_count, $ping_count, $comment_count );
1668    while ( my $blog = $iter->() ) {
1669        my $blog_id = $blog->id;
1670        my $row     = {
1671            id          => $blog->id,
1672            name        => $blog->name,
1673            description => $blog->description,
1674            site_url    => $blog->site_url
1675        };
1676
1677        # we should use count by group here...
1678        $row->{num_entries} =
1679          ( $entry_count ? $entry_count->{$blog_id} : $entry_count->{$blog_id} =
1680              MT::Entry->count( { blog_id => $blog_id } ) )
1681          || 0;
1682        $row->{num_comments} = (
1683              $comment_count
1684            ? $comment_count->{$blog_id}
1685            : $comment_count->{$blog_id} = MT::Comment->count(
1686                { blog_id => $blog_id, junk_status => [ 0, 1 ] }
1687            )
1688          )
1689          || 0;
1690        $row->{num_pings} = (
1691            $ping_count ? $ping_count->{$blog_id} : $ping_count->{$blog_id} =
1692              MT::TBPing->count(
1693                { blog_id => $blog_id, junk_status => [ 0, 1 ] }
1694              )
1695        ) || 0;
1696        $row->{num_authors} = 0;
1697
1698        # FIXME: This isn't efficient
1699        my $iter = MT::Permission->load_iter(
1700            {
1701                blog_id => [ 0, $blog_id ],
1702
1703                #    role_mask => [ 2, undef ]
1704                #}, {
1705                #    range_incl => { 'role_mask' => 1 }
1706            }
1707        );
1708        my %a;
1709        while ( my $p = $iter->() ) {
1710            next if exists $a{ $p->author_id };
1711            $a{ $p->author_id } = 1;
1712            $row->{num_authors}++ if $p->can_create_post;
1713        }
1714        if ( $author->is_superuser ) {
1715            $row->{can_create_post}       = 1;
1716            $row->{can_edit_entries}      = 1;
1717            $row->{can_edit_templates}    = 1;
1718            $row->{can_edit_config}       = 1;
1719            $row->{can_set_publish_paths} = 1;
1720            $row->{can_administer_blog}   = 1;
1721        }
1722        else {
1723            my $perms = $author->permissions($blog_id);
1724            $row->{can_create_post}  = $perms->can_create_post;
1725            $row->{can_edit_entries} = $perms->can_create_post
1726              || $perms->can_edit_all_posts
1727              || $perms->can_publish_post;
1728            $row->{can_edit_templates} = $perms->can_edit_templates;
1729            $row->{can_edit_config}    = $perms->can_edit_config
1730              || $perms->can_administer_blog;
1731            $row->{can_set_publish_paths} = $perms->can_set_publish_paths
1732              || $perms->can_administer_blog;
1733            $row->{can_administer_blog} = $perms->can_administer_blog;
1734        }
1735        $row->{object} = $blog;
1736        push @data, $row;
1737    }
1738
1739    if (@data) {
1740        $param->{blog_table}[0]{object_loop} = \@data;
1741        $app->load_list_actions( 'blog', \%$param );
1742        $param->{object_loop} = $param->{blog_table}[0]{object_loop};
1743    }
1744
1745    \@data;
1746}
1747
1748sub cfg_blog {
1749    my $q = $_[0]->{query};
1750    $q->param( '_type', 'blog' );
1751    $q->param( 'id',    scalar $q->param('blog_id') );
1752    $_[0]->forward( "view", { output => 'cfg_prefs.tmpl' } );
1753}
1754
1755sub cfg_archives_save {
1756    my $app = shift;
1757    my ($blog) = @_;
1758
1759    my $at = $app->param('preferred_archive_type');
1760    $blog->archive_type_preferred($at);
1761    my $pq = $app->param('publish_queue');
1762    $blog->publish_queue( $pq ? 1 : 0 );
1763    $blog->save
1764      or return $app->error(
1765        $app->translate( "Saving blog failed: [_1]", $blog->errstr ) );
1766}
1767
1768sub RegistrationAffectsArchives {
1769    my ( $blog_id, $archive_type ) = @_;
1770    require MT::TemplateMap;
1771    require MT::Template;
1772    my @tms = MT::TemplateMap->load(
1773        {
1774            archive_type => $archive_type,
1775            blog_id      => $blog_id
1776        }
1777    );
1778    grep { $_->text =~ /<MT:?IfRegistration/i }
1779      map { MT::Template->load( $_->template_id ) } @tms;
1780}
1781
1782sub update_dynamicity {
1783    my $app = shift;
1784    my ( $blog, $cache, $conditional ) = @_;
1785    my $dcty = $blog->custom_dynamic_templates;
1786    if ( $dcty eq 'none' ) {
1787        require MT::Template;
1788        my @templates = MT::Template->load( { blog_id => $blog->id } );
1789        for my $tmpl (@templates) {
1790            $tmpl->build_dynamic(0);
1791            $tmpl->save();
1792        }
1793    }
1794    elsif ( $dcty eq 'archives' ) {
1795        require MT::Template;
1796        my @templates = MT::Template->load( { blog_id => $blog->id } );
1797        for my $tmpl (@templates) {
1798            $tmpl->build_dynamic( $tmpl->type ne 'index' || 0 );
1799            $tmpl->save();
1800        }
1801    }
1802    elsif ( $dcty eq 'custom' ) {
1803    }
1804    elsif ( $dcty eq 'all' ) {
1805        require MT::Template;
1806        my @templates = MT::Template->load(
1807            {
1808                blog_id => $blog->id,
1809
1810                # FIXME: enumeration of types
1811                type =>
1812                  [ 'index', 'archive', 'individual', 'page', 'category' ],
1813            }
1814        );
1815        for my $tmpl (@templates) {
1816            $tmpl->build_dynamic(1);
1817            $tmpl->save();
1818        }
1819    }
1820
1821    if ( $dcty ne 'none' ) {
1822        prepare_dynamic_publishing($app, @_, $blog->site_path, $blog->site_url);
1823        if ( $blog->archive_path ) {
1824            prepare_dynamic_publishing($app, @_, $blog->archive_path, $blog->archive_url);
1825        }
1826        my $compiled_template_path =
1827          File::Spec->catfile( $blog->site_path(), 'templates_c' );
1828        if ( -d $compiled_template_path ) {
1829            $app->add_return_arg( 'no_writecache' => 1 )
1830              unless ( -w $compiled_template_path );
1831        }
1832        else {
1833            $app->add_return_arg( 'no_cachedir' => 1 )
1834              unless ( -d $compiled_template_path );
1835        }
1836
1837        # FIXME: use FileMgr
1838        if ($cache) {
1839            my $cache_path = File::Spec->catfile( $blog->site_path(), 'cache' );
1840            if ( -d $cache_path ) {
1841                $app->add_return_arg( 'no_write_cache_path' => 1 )
1842                  unless ( -w $cache_path );
1843            }
1844            else {
1845                $app->add_return_arg( 'no_cache_path' => 1 )
1846                  unless ( -d $cache_path );
1847            }
1848        }
1849    }
1850    $app->add_return_arg( dynamic_set => 1 );
1851}
1852
1853sub prepare_dynamic_publishing {
1854    my ( $cb, $blog, $cache, $conditional, $site_path, $site_url ) = @_;
1855
1856    my $htaccess_path = File::Spec->catfile( $site_path, ".htaccess" );
1857    my $mtview_path   = File::Spec->catfile( $site_path, "mtview.php" );
1858
1859    ## Don't re-create when files are there in callback.
1860    return 1
1861      if !defined($cache)
1862      && !defined($conditional)
1863      && ( 'MT::Callback' eq ref($cb) )
1864      && ( -f $htaccess_path )
1865      && ( -f $mtview_path );
1866    return 1 if ( 'none' eq $blog->custom_dynamic_templates );
1867
1868    require File::Spec;
1869
1870    # IIS itself does not handle .htaccess,
1871    # but IISPassword (3rd party) does and dies with this.
1872    if ( $ENV{SERVER_SOFTWARE} !~ /Microsoft-IIS/ ) {
1873        eval {
1874            require URI;
1875            my $mtview_server_url = new URI( $site_url );
1876            $mtview_server_url = $mtview_server_url->path();
1877            $mtview_server_url .=
1878              ( $mtview_server_url =~ m|/$| ? "" : "/" ) . "mtview.php";
1879
1880            my $contents = "";
1881            if ( open( HT, $htaccess_path ) ) {
1882                local $/ = undef;
1883                $contents = <HT>;
1884                close HT;
1885            }
1886            if ( $contents !~ /^\s*Rewrite(Cond|Engine|Rule)\b/m ) {
1887                my $htaccess = <<HTACCESS;
1888
1889## %%%%%%% Movable Type generated this part; don't remove this line! %%%%%%%
1890# Disable fancy indexes, so mtview.php gets a chance...
1891Options -Indexes +SymLinksIfOwnerMatch
1892  <IfModule mod_rewrite.c>
1893  # The mod_rewrite solution is the preferred way to invoke
1894  # dynamic pages, because of its flexibility.
1895
1896  # Add mtview.php to the list of DirectoryIndex options, listing it last,
1897  # so it is invoked only if the common choices aren't present...
1898  <IfModule mod_dir.c>
1899    DirectoryIndex index.php index.html index.htm default.htm default.html default.asp $mtview_server_url
1900  </IfModule>
1901
1902  RewriteEngine on
1903
1904  # don't serve mtview.php if the request is for a real directory
1905  # (allows the DirectoryIndex lookup to function)
1906  RewriteCond %{REQUEST_FILENAME} !-d
1907
1908  # don't serve mtview.php if the request is for a real file
1909  # (allows the actual file to be served)
1910  RewriteCond %{REQUEST_FILENAME} !-f
1911  # anything else is handed to mtview.php for resolution
1912  RewriteRule ^(.*)\$ $mtview_server_url [L,QSA]
1913</IfModule>
1914
1915<IfModule !mod_rewrite.c>
1916  # if mod_rewrite is unavailable, we forward any missing page
1917  # or unresolved directory index requests to mtview
1918  # if mtview.php can resolve the request, it returns a 200
1919  # result code which prevents any 4xx error code from going
1920  # to the server's access logs. However, an error will be
1921  # reported in the error log file. If this is your only choice,
1922  # and you want to suppress these messages, adding a "LogLevel crit"
1923  # directive within your VirtualHost or root configuration for
1924  # Apache will turn them off.
1925  ErrorDocument 404 $mtview_server_url
1926  ErrorDocument 403 $mtview_server_url
1927</IfModule>
1928## ******* Movable Type generated this part; don't remove this line! *******
1929
1930HTACCESS
1931
1932                $blog->file_mgr->mkpath( $site_path );
1933
1934                open( HT, ">>$htaccess_path" )
1935                  || die "Couldn't open $htaccess_path for appending";
1936                print HT $htaccess || die "Couldn't write to $htaccess_path";
1937                close HT;
1938            }
1939        };
1940        if ($@) { print STDERR $@; }
1941    }
1942
1943    eval {
1944        my $mv_contents = '';
1945        if ( -f $mtview_path ) {
1946            open( my $mv, "<$mtview_path" );
1947            while ( my $line = <$mv> ) {
1948                $mv_contents .= $line if ( $line !~ m!^//|<\?(?:php)?|\?>! );
1949            }
1950            close $mv;
1951        }
1952        my $cgi_path = MT->instance->server_path() || "";
1953        $cgi_path =~ s!/*$!!;
1954        my $mtphp_path = File::Spec->canonpath("$cgi_path/php/mt.php");
1955        my $blog_id    = $blog->id;
1956        my $config     = MT->instance->{cfg_file};
1957        my $cache_code = $cache ? "\n    \$mt->caching = true;" : '';
1958        my $conditional_code =
1959          $conditional ? "\n    \$mt->conditional = true;" : '';
1960        my $new_mtview = <<NEW_MTVIEW;
1961
1962    include('$mtphp_path');
1963    \$mt = new MT($blog_id, '$config');$cache_code$conditional_code
1964    \$mt->view();
1965NEW_MTVIEW
1966
1967        if ( $new_mtview ne substr( $mv_contents, 0, length($new_mtview) ) ) {
1968            $mv_contents =~ s!\n!\n//!gs;
1969            my $mtview = <<MTVIEW;
1970<?php
1971$new_mtview
1972$mv_contents
1973?>
1974MTVIEW
1975
1976            $blog->file_mgr->mkpath( $site_path );
1977            open( my $mv, ">$mtview_path" )
1978              || die "Couldn't open $mtview_path for appending";
1979            print $mv $mtview || die "Couldn't write to $mtview_path";
1980            close $mv;
1981        }
1982    };
1983    if ($@) { print STDERR $@; }
1984
1985    my $compiled_template_path =
1986      File::Spec->catfile( $site_path, 'templates_c' );
1987    my $fmgr        = $blog->file_mgr;
1988    my $cfg         = MT->config;
1989    my $saved_umask = $cfg->DirUmask;
1990    $cfg->DirUmask('0000');
1991    $fmgr->mkpath($compiled_template_path);
1992    $cfg->DirUmask($saved_umask);
1993    my $message = q();
1994
1995    if ( -d $compiled_template_path ) {
1996        $message = MT->translate(
1997'Error: Movable Type cannot write to the template cache directory. Please check the permissions for the directory called <code>[_1]</code> underneath your blog directory.',
1998            'templates_c'
1999        ) unless ( -w $compiled_template_path );
2000    }
2001    else {
2002        $message = MT->translate(
2003'Error: Movable Type was not able to create a directory to cache your dynamic templates. You should create a directory called <code>[_1]</code> underneath your blog directory.',
2004            'templates_c'
2005        ) unless ( -d $compiled_template_path );
2006    }
2007
2008    # FIXME: use FileMgr
2009    if ($cache) {
2010        my $cache_path = File::Spec->catfile( $blog->site_path(), 'cache' );
2011        $cfg->DirUmask('0000');
2012        $fmgr->mkpath($cache_path);
2013        $cfg->DirUmask($saved_umask);
2014        if ( -d $cache_path ) {
2015            $message = MT->translate(
2016'Error: Movable Type cannot write to the template cache directory. Please check the permissions for the directory called <code>[_1]</code> underneath your blog directory.',
2017                'cache'
2018            ) unless ( -w $cache_path );
2019        }
2020        else {
2021            $message = MT->translate(
2022'Error: Movable Type was not able to create a directory to cache your dynamic templates. You should create a directory called <code>[_1]</code> underneath your blog directory.',
2023                'cache'
2024            ) unless ( -d $cache_path );
2025        }
2026    }
2027    MT->log(
2028        {
2029            message => $message,
2030            level   => MT::Log::ERROR(),
2031            class   => 'system',
2032        }
2033    );
2034}
2035
20361;
Note: See TracBrowser for help on using the browser.