-
Notifications
You must be signed in to change notification settings - Fork 110
/
MT.pm
4033 lines (3335 loc) · 126 KB
/
MT.pm
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Movable Type (r) (C) Six Apart Ltd. All Rights Reserved.
# This code cannot be redistributed without permission from www.sixapart.com.
# For more information, consult your Movable Type license.
#
# $Id$
package MT;
use strict;
use warnings;
use base qw( MT::ErrorHandler );
use filetest 'access';
use File::Spec;
use File::Basename;
use MT::Util qw( weaken );
use MT::I18N qw( const );
use MT::Util::Encode;
our ( $VERSION, $SCHEMA_VERSION );
our (
$PRODUCT_NAME, $PRODUCT_CODE, $PRODUCT_VERSION, $VERSION_ID,
$RELEASE_NUMBER, $PORTAL_URL, $RELEASE_VERSION_ID
);
our ( $MT_DIR, $APP_DIR, $CFG_DIR, $CFG_FILE, $SCRIPT_SUFFIX );
our (
$plugin_sig, $plugin_envelope, $plugin_registry,
%Plugins, @Components, %Components,
$DebugMode, $mt_inst, %mt_inst,
$Builder, $Publisher,
);
my %Text_filters;
our $STATUS;
$SIG{USR2} = sub { $MT::STATUS ||= 'Got USR2 signal'; print STDERR "$$: $MT::STATUS\n"; return } unless $^O eq 'MSWin32';
# For state determination in MT::Object
our $plugins_installed;
BEGIN {
$plugins_installed = 0;
( $VERSION, $SCHEMA_VERSION ) = ( '8.000002', '8.0000' );
( $PRODUCT_NAME, $PRODUCT_CODE, $PRODUCT_VERSION,
$VERSION_ID, $RELEASE_NUMBER, $PORTAL_URL,
$RELEASE_VERSION_ID
)
= (
'__PRODUCT_NAME__', '__PRODUCT_CODE__',
'8.0.2', '__PRODUCT_VERSION_ID__',
'__RELEASE_NUMBER__', '__PORTAL_URL__',
'__RELEASE_VERSION_ID__',
);
# To allow MT to run straight from svn, if no build process (pre-processing)
# is run, then default to MTOS
if ( $PRODUCT_NAME eq '__PRODUCT' . '_NAME__' ) {
$PRODUCT_NAME = 'Movable Type';
}
if ( $PORTAL_URL eq '__PORTAL' . '_URL__' ) {
$PORTAL_URL = 'https://www.movabletype.org/';
}
if ( $VERSION_ID eq '__PRODUCT_VERSION' . '_ID__' ) {
$VERSION_ID = $PRODUCT_VERSION;
}
if ( $RELEASE_NUMBER eq '__RELEASE' . '_NUMBER__' ) {
$RELEASE_NUMBER = 0;
}
if ( $RELEASE_VERSION_ID eq '__RELEASE' . '_VERSION_ID__' ) {
$RELEASE_VERSION_ID = '';
}
$DebugMode = 0;
# Alias these; Components is the preferred array for MT 4
*Plugins = \@Components;
# Do not read ExifTool configFile.
$Image::ExifTool::configFile = '';
}
# On-demand loading of compatibility module, if a plugin asks for it, using
# use MT 3;
# or even specific to minor version (but this just loads MT::Compat::v3)
# use MT 3.3;
sub VERSION {
my $v = $_[1];
if ( defined $v && ( $v =~ m/^(\d+)/ ) ) {
my $compat = "MT::Compat::v" . $1;
if ( ( $1 > 2 ) && ( $1 < int($VERSION) ) ) {
no strict 'refs';
unless ( defined *{ $compat . '::' } ) {
eval "# line " . __LINE__ . " " . __FILE__
. "\nrequire $compat;";
}
}
}
return UNIVERSAL::VERSION(@_);
}
sub version_number {$VERSION}
sub version_id {$VERSION_ID}
sub product_code {$PRODUCT_CODE}
sub product_name {$PRODUCT_NAME}
sub product_version {$PRODUCT_VERSION}
sub schema_version {$SCHEMA_VERSION}
sub release_number {$RELEASE_NUMBER}
sub release_version_id {$RELEASE_VERSION_ID}
sub portal_url {
if ( my $url = const('PORTAL_URL') ) {
return $url;
}
return $PORTAL_URL;
}
# Default id method turns MT::App::CMS => cms; Foo::Bar => foo/bar
sub id {
my $pkg = shift;
my $id = ref($pkg) || $pkg;
# ignore the MT::App prefix as part of the identifier
$id =~ s/^MT::App:://;
$id =~ s!::!/!g;
return lc $id;
}
sub version_slug {
return MT->translate_templatized(<<"SLUG");
<__trans phrase="Powered by [_1]" params="$PRODUCT_NAME">
<__trans phrase="Version [_1]" params="$VERSION_ID">
<__trans phrase="https://www.movabletype.com/">
SLUG
}
sub build_id {
my $build_id = '__BUILD_ID__';
$build_id = '' if $build_id eq '__BUILD_' . 'ID__';
return $build_id;
}
sub instance {
my $class = shift;
$mt_inst ||= $mt_inst{$class} ||= $class->construct(@_);
}
*app = \&instance;
sub set_instance {
my $class = shift;
$mt_inst = shift;
}
sub new {
my $mt = &instance_of;
$mt_inst ||= $mt;
$mt;
}
sub instance_of {
my $class = shift;
$mt_inst{$class} ||= $class->construct(@_);
}
sub construct {
my $class = shift;
my $mt = bless {}, $class;
local $mt_inst = $mt;
$mt->init(@_)
or die $mt->errstr;
$mt;
}
{
my %object_types;
sub model {
my $pkg = shift;
my ($k) = @_;
return undef unless $k;
$object_types{$k} = $_[1] if scalar @_ > 1;
return $object_types{$k} if exists $object_types{$k};
if ( $k =~ m/^(.+):(meta|summary)$/ ) {
my $ppkg = $pkg->model($1);
my $mpkg = $ppkg->meta_pkg($2);
return $mpkg ? $object_types{$k} = $mpkg : undef;
}
if ( $k =~ m/^(.+):revision$/ ) {
my $ppkg = $pkg->model($1);
my $rpkg = $ppkg->revision_pkg;
return $rpkg ? $object_types{$k} = $rpkg : undef;
}
my $model = $pkg->registry( 'object_types', $k );
if ( ref($model) eq 'ARRAY' ) {
# First element of an array *should* be a scalar; in case it isn't,
# return undef.
$model = $model->[0];
return undef if ref $model;
}
elsif ( ref($model) eq 'HASH' ) {
# If all we have is a hash, this doesn't tell us the package for
# this object type, so it's undefined.
return undef;
}
return undef unless $model;
# Element in object type hash is scalar, so return it
no strict 'refs';
unless ( defined *{ $model . '::__properties' } ) {
use strict 'refs';
eval "# line " . __LINE__ . " " . __FILE__ . "\nrequire $model;";
if ( $@ && ( $k =~ m/^(.+)\./ ) ) {
# x.foo can't be found, so try loading x
if ( my $ppkg = $pkg->model($1) ) {
# well now see if $model is defined
no strict 'refs';
unless ( defined *{ $model . '::__properties' } ) {
# if not, use parent package instead
$model = $ppkg;
}
}
}
}
return $object_types{$k} = $model;
}
sub models {
my $pkg = shift;
my ($k) = @_;
my @matches;
my $model = $pkg->registry('object_types');
foreach my $m ( keys %$model ) {
if ( $m =~ m/^\Q$k\E\.?/ ) {
push @matches, $m;
}
}
return @matches;
}
sub loaded_models {
my $pkg = shift;
values %object_types;
}
sub clear_cache_of_loaded_models {
my $pkg = shift;
for my $model (values %object_types) {
# avoid loading a new driver here
my $props = $model->properties or next;
my $driver = $props->{driver} or next;
$driver->clear_cache if $driver->can('clear_cache');
}
}
}
sub all_models {
my $pkg = shift;
map { $pkg->model($_) } keys %{ $pkg->registry('object_types') };
}
sub registry {
my $pkg = shift;
require MT::Component;
my $regs = MT::Component->registry(@_);
my $r;
if ($regs) {
foreach my $cr (@$regs) {
# in the event that our registry request returns something
# other than an array of hashes, return it as is instead of
# merging it together.
return $regs unless ref($cr) eq 'HASH';
delete $cr->{plugin} if exists $cr->{plugin};
__merge_hash( $r ||= {}, $cr );
}
}
return $r;
}
# merges contents of two hashes, giving preference to the right side
# if $replace is true; otherwise it will always append to the left side.
sub __merge_hash {
my ( $h1, $h2, $replace ) = @_;
for my $k ( keys(%$h2) ) {
if ( exists( $h1->{$k} ) && ( !$replace ) ) {
if ( ref $h1->{$k} eq 'HASH' ) {
__merge_hash( $h1->{$k}, $h2->{$k}, ( $replace || 0 ) + 1 );
}
elsif ( ref $h1->{$k} eq 'ARRAY' ) {
if ( ref $h2->{$k} eq 'ARRAY' ) {
push @{ $h1->{$k} }, @{ $h2->{$k} };
}
else {
push @{ $h1->{$k} }, $h2->{$k};
}
}
else {
$h1->{$k} = [ $h1->{$k}, $h2->{$k} ];
}
}
else {
$h1->{$k} = $h2->{$k};
}
}
}
# The above functions can all be used to make MT objects (and subobjects).
# The difference between them is characterized by these assertions:
#
# $mt = MT::App::Search->new();
# assert($mt->isa('MT::App::Search'))
#
# $mt1 = MT->instance
# $mt2 = MT->instance
# assert($mt1 == $mt2);
#
# $mt1 = MT::App::CMS->construct()
# $mt2 = MT::App::CMS->construct()
# assert($mt1 != $mt2);
#
# TBD: make a test script for these.
# obsolete; do not use
sub unplug {
}
sub config {
my $mt = shift;
ref $mt or $mt = MT->instance;
unless ( $mt->{cfg} ) {
require MT::ConfigMgr;
weaken( $mt->{cfg} = MT::ConfigMgr->instance );
}
if (@_) {
my $setting = shift;
@_ ? $mt->{cfg}->set( $setting, @_ ) : $mt->{cfg}->get($setting);
}
else {
$mt->{cfg};
}
}
sub request {
my $pkg = shift;
my $inst = ref($pkg) ? $pkg : $pkg->instance;
unless ( $inst->{request} ) {
require MT::Request;
$inst->{request} = MT::Request->instance;
}
if (@_) {
$inst->{request}->stash(@_);
}
else {
$inst->{request};
}
}
sub log {
my ( $mt, $msg );
if ( @_ == 1 ) {
# single parameter to log, so it must be the message
$msg = shift;
$mt = MT->instance;
}
else {
# multiple parameters to log; second one is message
( $mt, $msg ) = @_;
}
unless ($plugins_installed) {
# finish init_schema here since we have to log something
# to the database.
$mt->init_schema();
}
my $log_class = $mt->model('log');
my $log = $log_class->new();
if ( ref $msg eq 'HASH' ) {
$log->set_values($msg);
}
elsif ( ( ref $msg ) && ( UNIVERSAL::isa( $msg, 'MT::Log' ) ) ) {
$log = $msg;
}
else {
$log->message($msg);
}
$log->level( MT::Log::INFO() )
unless defined $log->level;
$log->class('system')
unless defined $log->class;
# log to a file/handle before saving to the database
require MT::Util::Log;
MT::Util::Log::init();
my $method
= $log->level == MT::Log::DEBUG() ? 'debug'
: $log->level == MT::Log::INFO() ? 'info'
: $log->level == MT::Log::NOTICE() ? 'notice'
: $log->level == MT::Log::WARNING() ? 'warn'
: $log->level == MT::Log::ERROR() ? 'error'
: $log->level == MT::Log::SECURITY() ? 'error'
: 'none';
my $message = $log->message;
if ($message =~ /Can't locate/) {
$message =~ s!\(you may need to install [^)]+\)\s+\(\@INC [^)]+\)!!s;
$log->message($message);
}
my $metadata = $log->metadata;
if ($metadata and $metadata =~ /Can't locate/) {
$metadata =~ s!\(you may need to install [^)]+\)\s+\(\@INC [^)]+\)!!s;
$log->metadata($metadata);
}
$message .= " ($metadata)" if defined $metadata && $metadata ne '';
MT::Util::Log->$method($message);
$log->save();
}
sub run_tasks {
my $mt = shift;
require MT::TaskMgr;
MT::TaskMgr->run_tasks(@_);
}
our %CallbackAlias;
our $CallbacksEnabled = 1;
my %CallbacksEnabled;
my @Callbacks;
sub add_callback {
my $class = shift;
my ( $meth, $priority, $plugin, $code ) = @_;
if ( $meth =~ m/^(.+::)?([^\.]+)(\..+)?$/ ) {
# Remap (whatever)::(name).(something)
if ( exists $CallbackAlias{$2} ) {
$meth = $CallbackAlias{$2};
$meth = $1 . $meth if $1;
$meth = $meth . $3 if $3;
}
}
$meth = $CallbackAlias{$meth} if exists $CallbackAlias{$meth};
my $internal = 0;
if ( ref $plugin ) {
if ( ( defined $mt_inst ) && ( $plugin == $mt_inst ) ) {
$plugin = undef;
$internal = 1;
}
elsif ( !UNIVERSAL::isa( $plugin, "MT::Component" ) ) {
return $class->trans_error(
"If it is present, the third argument to add_callback must be an object of type MT::Component or MT::Plugin"
);
}
}
if ( ( ref $code ) ne 'CODE' ) {
if ( ref $code ) {
return $class->trans_error(
'Fourth argument to add_callback must be a CODE reference.');
}
else {
# Defer until callback is used
# if ($plugin) {
# $code = MT->handler_to_coderef($code);
# }
}
}
# 0 and 11 are exclusive.
if ( $priority == 0 || $priority == 11 ) {
if ( $Callbacks[$priority]->{$meth} ) {
return $class->trans_error("Two plugins are in conflict");
}
}
return $class->trans_error( "Invalid priority level [_1] at add_callback",
$priority )
if ( ( $priority < 0 ) || ( $priority > 11 ) );
require MT::Callback;
$CallbacksEnabled{$meth} = 1;
## push @{$Plugins{$plugin_sig}{callbacks}}, "$meth Callback" if $plugin_sig;
my $cb = MT::Callback->new(
plugin => $plugin,
code => $code,
priority => $priority,
internal => $internal,
method => $meth
);
push @{ $Callbacks[$priority]->{$meth} }, $cb;
$cb;
}
sub remove_callback {
my $class = shift;
my ($cb) = @_;
my $priority = $cb->{priority};
my $method = $cb->{method};
my $list = $Callbacks[$priority];
return unless $list;
my $cbarr = $list->{$method};
return unless $cbarr;
@$cbarr = grep { $_ != $cb } @$cbarr;
}
sub is_callback_registered {
my $class = shift;
my ($meth) = @_;
foreach my $list (@Callbacks) {
return 1 if exists $list->{$meth};
}
return 0;
}
# For use by MT internal code
sub _register_core_callbacks {
my $class = shift;
my ($callback_table) = @_;
foreach my $name ( keys %$callback_table ) {
$class->add_callback( $name, 5, $mt_inst, $callback_table->{$name} )
|| return;
}
1;
}
sub register_callbacks {
my $class = shift;
my ($callback_list) = @_;
foreach my $cb (@$callback_list) {
$class->add_callback( $cb->{name}, $cb->{priority}, $cb->{plugin},
$cb->{code} )
|| return;
}
1;
}
{
my $CB_ERR;
sub callback_error { $CB_ERR = $_[0]; }
sub callback_errstr {$CB_ERR}
}
sub run_callback {
my $class = shift;
my ( $cb, @args ) = @_;
$cb->error(); # reset the error string
my $result = eval { $cb->invoke(@args); };
if ( my $err = $@ ) {
$cb->error($err);
my $plugin = $cb->{plugin};
my $name;
if ( $cb->{internal} ) {
$name = MT->translate("Internal callback");
}
elsif ( UNIVERSAL::isa( $plugin, 'MT::Plugin' ) ) {
$name = $plugin->name() || MT->translate("Unnamed plugin");
}
else {
$name = MT->translate("Unnamed plugin");
}
require MT::Log;
MT->log(
{ message =>
MT->translate( "[_1] died with: [_2]", $name, $err ),
class => 'system',
category => 'callback',
level => MT::Log::ERROR(),
}
);
return 0;
}
if ( $cb->errstr() ) {
return 0;
}
return $result;
}
# A callback should return a true/false value. The result of
# run_callbacks is the logical AND of all the callback's return
# values. Some hookpoints will ignore the return value: e.g. object
# callbacks don't use it. By convention, those that use it have Filter
# at the end of their names (CommentPostFilter, CommentThrottleFilter,
# etc.)
# Note: this composition is not short-circuiting. All callbacks are
# executed even if one has already returned false.
# ALSO NOTE: failure (dying or setting $cb->errstr) does not force a
# "false" return.
# THINK: are there cases where a true value should override all false values?
# that is, where logical OR is the right way to compose multiple callbacks?
sub run_callbacks {
my $class = shift;
my ( $meth, @args ) = @_;
return 1 unless $CallbacksEnabled && %CallbacksEnabled;
$meth = $CallbackAlias{$meth} if exists $CallbackAlias{$meth};
my @methods;
# execution:
# Full::Name.<variant>
# *::Name.<variant> OR Name.<variant>
# Full::Name
# *::Name OR Name
push @methods, $meth if $CallbacksEnabled{$meth}; # bleh::blah variant
if ( $meth =~ /::/ ) { # presence of :: implies it's an obj. cb
my $name = $meth;
$name =~ s/^.*::([^:]*)$/$1/;
$name = $CallbackAlias{ '*::' . $name }
if exists $CallbackAlias{ '*::' . $name };
push @methods, '*::' . $name
if $CallbacksEnabled{ '*::' . $name }; # *::blah variant
push @methods, $name if $CallbacksEnabled{$name}; # blah variant
}
if ( $meth =~ /\./ ) { # presence of ' ' implies it is a variant callback
my ($name) = split /\./, $meth, 2;
$name = $CallbackAlias{$name} if exists $CallbackAlias{$name};
push @methods, $name if $CallbacksEnabled{$name}; # bleh::blah
if ( $name =~ m/::/ ) {
my $name2 = $name;
$name2 =~ s/^.*::([^:]*)$/$1/;
$name2 = $CallbackAlias{ '*::' . $name2 }
if exists $CallbackAlias{ '*::' . $name2 };
push @methods, '*::' . $name2
if $CallbacksEnabled{ '*::' . $name2 }; # *::blah
push @methods, $name2 if $CallbacksEnabled{$name2}; # blah
}
}
return 1 unless @methods;
$CallbacksEnabled{$_} = 0 for @methods;
my @errors;
my $filter_value = 1;
my $first_error;
foreach my $callback_sheaf (@Callbacks) {
for my $meth (@methods) {
if ( my $set = $callback_sheaf->{$meth} ) {
for my $cb (@$set) {
my $result = $class->run_callback( $cb, @args );
$filter_value &&= $result;
if ( !$result ) {
if ( $cb->errstr() ) {
push @errors, $cb->errstr();
}
if ( $class->errstr() ) {
push @errors, $class->errstr();
}
if ( !defined($first_error) ) {
$first_error = $cb->errstr() || $class->errstr();
}
}
}
}
}
}
callback_error( join( '', @errors ) );
$CallbacksEnabled{$_} = 1 for @methods;
if ( !$filter_value ) {
return $class->error($first_error);
}
else {
return $filter_value;
}
}
sub user_class {
shift->{user_class};
}
sub find_config {
my $mt = shift;
my ($param) = @_;
$param->{Config} ||= $ENV{MT_CONFIG};
$param->{Directory} ||= $ENV{MT_HOME};
if ( !$param->{Directory} ) {
if ( $param->{Config} ) {
$param->{Directory} = dirname( $param->{Config} );
}
else {
$param->{Directory} = dirname($0) || $ENV{PWD} || '.';
}
}
# the directory is the more important parameter between it and
# the config parameter. if config is unreadable, then scan for
# a config file using the directory as a base. we support
# either mt.cfg or mt-config.cgi for the config file name. the
# latter being a more secure choice since it is unreadable from
# a browser.
for my $cfg_file ( $param->{Config},
File::Spec->catfile( $param->{Directory}, 'mt-config.cgi' ),
'mt-config.cgi' )
{
return $cfg_file if $cfg_file && -r $cfg_file && -f $cfg_file;
}
return undef;
}
sub init_schema {
require MT::Object;
MT::Object->install_pre_init_properties();
}
sub init_permissions {
my $app = shift;
require MT::Permission;
MT::Permission->init_permissions;
my $ct_permissions = eval { $app->model('content_type')->all_permissions };
if ($@) {
warn "An error occurred when loading the content_type class: $@" if $MT::DebugMode;
return;
}
$app->component('core')->registry( 'permissions', $ct_permissions );
}
sub init_config {
my $mt = shift;
my ($param) = @_;
unless ( $mt->{cfg_file} ) {
my $cfg_file = $mt->find_config($param);
return $mt->error(
"Missing configuration file. Did you forgot to move mt-config.cgi-original to mt-config.cgi?"
) unless $cfg_file;
$cfg_file = File::Spec->rel2abs($cfg_file);
$mt->{cfg_file} = $cfg_file;
}
# translate the config file's location to an absolute path, so we
# can use that directory as a basis for calculating other relative
# paths found in the config file.
my $config_dir = $mt->{config_dir} = dirname( $mt->{cfg_file} );
# store the mt_dir (home) as an absolute path; fallback to the config
# directory if it isn't set.
$mt->{mt_dir}
= $param->{Directory}
? File::Spec->rel2abs( $param->{Directory} )
: $mt->{config_dir};
$mt->{mt_dir} ||= dirname($0);
# also make note of the active application path; this is derived by
# checking the PWD environment variable, the dirname of $0,
# the directory of SCRIPT_FILENAME and lastly, falls back to mt_dir
unless ( $mt->{app_dir} ) {
$mt->{app_dir} = $ENV{PWD} || "";
$mt->{app_dir} = dirname($0)
if !$mt->{app_dir}
|| !File::Spec->file_name_is_absolute( $mt->{app_dir} );
$mt->{app_dir} = dirname( $ENV{SCRIPT_FILENAME} )
if $ENV{SCRIPT_FILENAME}
&& ( !$mt->{app_dir}
|| ( !File::Spec->file_name_is_absolute( $mt->{app_dir} ) ) );
$mt->{app_dir} ||= $mt->{mt_dir};
$mt->{app_dir} = File::Spec->rel2abs( $mt->{app_dir} );
}
my $cfg = $mt->config;
$cfg->define( $mt->registry('config_settings') );
$cfg->read_config( $mt->{cfg_file} ) or return $mt->error( $cfg->errstr );
my @mt_paths = $cfg->paths;
for my $meth (@mt_paths) {
my $path = $cfg->get( $meth, undef );
my $type = $cfg->type($meth);
if ( defined $path ) {
if ( $type eq 'ARRAY' ) {
my @paths = $cfg->get($meth);
foreach my $path (@paths) {
next if File::Spec->file_name_is_absolute($path);
my $abs_path = File::Spec->catfile( $config_dir, $path );
$abs_path = File::Spec->catfile( $mt->{mt_dir}, $path )
unless -d $abs_path;
$path = $abs_path;
}
$cfg->$meth( \@paths );
}
else {
next if ref($path); # unexpected referene, ignore
if ( !File::Spec->file_name_is_absolute($path) ) {
my $abs_path = File::Spec->catfile( $config_dir, $path );
$abs_path = File::Spec->catfile( $mt->{mt_dir}, $path )
unless -d $abs_path;
$cfg->$meth($abs_path);
}
}
}
else {
next if $type eq 'ARRAY';
my $path = $cfg->default($meth);
if ( defined $path ) {
my $abs_path = File::Spec->catfile( $config_dir, $path );
$abs_path = File::Spec->catfile( $mt->{mt_dir}, $path )
unless -d $abs_path;
$cfg->$meth($abs_path);
}
}
}
if ( my $local_lib = $cfg->LocalLib ) {
$local_lib = [$local_lib] if !ref $local_lib;
eval "use local::lib qw( @{$local_lib} )";
return $mt->trans_error( 'Bad LocalLib config ([_1]): [_2]',
join( ', ', @$local_lib ), $@, )
if $@;
}
return $mt->trans_error("Bad ObjectDriver config")
unless $cfg->ObjectDriver;
if ( $cfg->PerformanceLogging && $cfg->ProcessMemoryCommand ) {
$mt->log_times();
}
$mt->set_language( $cfg->DefaultLanguage );
my $cgi_path = $cfg->CGIPath;
if ( !$cgi_path || $cgi_path =~ m!http://www\.example\.com/! ) {
return $mt->trans_error("Bad CGIPath config");
}
$mt->{cfg} = $cfg;
1;
}
{
my ($memory_start);
sub log_times {
my $pkg = shift;
my $timer = $pkg->get_timer;
return unless $timer;
my $memory;
my $cmd = $pkg->config->ProcessMemoryCommand;
if ($cmd) {
my $re;
if ( ref($cmd) eq 'HASH' ) {
$re = $cmd->{regex};
$cmd = $cmd->{command};
}
$cmd =~ s/\$\$/$$/g;
$memory = `$cmd`;
if ($re) {
if ( $memory =~ m/$re/ ) {
$memory = $1;
$memory =~ s/\D//g;
}
}
else {
$memory =~ s/\s+//gs;
}
}
# Called at the start of the process; so we're only recording
# the memory usage at the start of the app right now.
unless ( $timer->{elapsed} ) {
$memory_start = $memory;
return;
}
require File::Spec;
my $dir = MT->config('PerformanceLoggingPath') or return;
my @time = localtime(time);
my $file = sprintf(
"pl-%04d%02d%02d.log",
$time[5] + 1900,
$time[4] + 1,
$time[3]
);
my $log_file = File::Spec->catfile( $dir, $file );
my $first_write = !-f $log_file;
open my $PERFLOG, ">>", $log_file
or ( warn("Failed to open preflog $log_file"), return );
require Fcntl;
flock( $PERFLOG, Fcntl::LOCK_EX() );
if ($first_write) {
require Config;
my ( $osname, $osvers )
= ( $Config::Config{osname}, $Config::Config{osvers} );
print $PERFLOG "# Operating System: $osname/$osvers\n";
print $PERFLOG "# Platform: $^O\n";
my $ver
= ref($^V) eq 'version'
? $^V->normal
: ( $^V ? join( '.', unpack 'C*', $^V ) : $] );
print $PERFLOG "# Perl Version: $ver\n";
print $PERFLOG "# Web Server: $ENV{SERVER_SOFTWARE}\n";
require MT::Object;
my $driver = MT::Object->driver;
if ($driver) {
my $dbh = $driver->r_handle;
if ($dbh) {
my $dbname = $dbh->get_info(17); # SQL_DBMS_NAME
my $dbver = $dbh->get_info(18); # SQL_DBMS_VER
if ( $dbname && $dbver ) {
print $PERFLOG "# Database: $dbname/$dbver\n";
}
}
}
my ( $drname, $drh ) = each %DBI::installed_drh;
print $PERFLOG "# Database Library: DBI/"
. $DBI::VERSION
. "; DBD/"
. $drh->{Version} . "\n";
if ( MT::Util::is_mod_perl1() ) {
print $PERFLOG "# App Mode: mod_perl\n";
}
elsif ( $ENV{FAST_CGI} ) {
print $PERFLOG "# App Mode: FastCGI\n";
}
elsif ( $ENV{'psgi.input'} ) {
print $PERFLOG "# App Mode: PSGI\n";
}
else {
print $PERFLOG "# App Mode: CGI\n";
}
}
if ($memory) {
print $PERFLOG $timer->dump_line( "mem_start=$memory_start",
"mem_end=$memory" );
}
else {
print $PERFLOG $timer->dump_line();
}
close $PERFLOG;
}
}
sub get_timer {
my $mt = shift;
$mt = MT->instance unless ref $mt;
my $timer = $mt->request('timer');
unless ( defined $timer ) {
if ( MT->config('PerformanceLogging') ) {
my $uri;
if ( $mt->isa('MT::App') ) {
local @$mt{qw(__path __mt_path)};
delete @$mt{qw(__path __mt_path)};
local $mt->{is_admin}
= exists( $mt->{is_admin} )
? $mt->{is_admin}
: $mt->isa('MT::App::CMS');
$uri = $mt->uri( args => { $mt->param_hash } );
}
require MT::Util::ReqTimer;
$timer = MT::Util::ReqTimer->new($uri);
}
else {
$timer = 0;
}
$mt->request( 'timer', $timer );
}
return $timer;
}
sub time_this {
my $mt = shift;
my ( $str, $code ) = @_;
my $timer = $mt->get_timer();
my $ret;
if ($timer) {
$timer->pause_partial();
$ret = $code->();
$timer->mark($str);
}
else {
$ret = $code->();
}
return $ret;
}