Report abuse

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
sub _init_plugins_core {
    my $mt = shift;
    my ($PluginSwitch, $use_plugins, $plugins) = @_;

    my $timer;
    if ($mt->config->PerformanceLogging) {
        $timer = $mt->get_timer();
    }
    foreach my $plugin (@$plugins) {
        my $load_plugin = sub {
            my ( $plugin, $sig ) = @_;
            die "Bad plugin filename '$plugin'"
                if ( $plugin !~ /^([-\\\/\@\:\w\.\s~]+)$/ );
            local $plugin_sig      = $sig;
            local $plugin_registry = {};
            $plugin = $1;
            if (
                !$use_plugins
                || ( exists $PluginSwitch->{$plugin_sig}
                     && !$PluginSwitch->{$plugin_sig} )
                )
            {
                $Plugins{$plugin_sig}{full_path} = $plugin_full_path;
                $Plugins{$plugin_sig}{enabled}   = 0;
                return 0;
            }
            return 0 if exists $Plugins{$plugin_sig};
            $Plugins{$plugin_sig}{full_path} = $plugin_full_path;
            $timer->pause_partial if $timer;
            eval "# line " . __LINE__ . " " . __FILE__ . "\nrequire '".
                File::Spec->catdir( $plugin_full_path, $sig ) . "';";
            $timer->mark("Loaded plugin " . $sig) if $timer;
            if ($@) {
                $Plugins{$plugin_sig}{error} = $@;
                # Issue MT log within another eval block in the
                # event that the plugin error is happening before
                # the database has been initialized...
                eval {
                    # line __LINE__ __FILE__
                    require MT::Log;
                    $mt->log(
                        {
                            message => $mt->translate(
                                "Plugin error: [_1] [_2]", $plugin,
                                $Plugins{$plugin_sig}{error}
                                ),
                            class => 'system',
                            level => MT::Log::ERROR()
                        }
                        );
                };
                return 0;
            }
            else {
                if ( my $obj = $Plugins{$plugin_sig}{object} ) {
                    $obj->init_callbacks();
                }
                else {
<<<<<<< HEAD
                    
                    # A plugin did not register itself, so
                    # create a dummy plugin object which will
                    # cause it to show up in the plugin listing
                    # by it's filename.
                    MT->add_plugin( {} );
=======
                    # open and scan the directory for plugin files,
                    # save them to load later. Report errors in
                    # the activity log
                    unless (opendir SUBDIR, $plugin_full_path) {
                        my $msg = $mt->translate(
                            "Bad directory found in plugin initialization: [_1]",
                            $plugin_full_path
                        );
                        $mt->log({
                            message => $msg,
                            class => 'system',
                            level => MT->model('log')->ERROR()
                        });
                        next;
                    }

                    my @plugin_files = readdir SUBDIR;
                    closedir SUBDIR;
                    for my $file (@plugin_files) {
                        if ($file eq 'lib' || $file eq 'extlib') {
                            my $plib = File::Spec->catdir( $plugin_full_path, $file );
                            unshift @INC, $plib if -d $plib;
                            next;
                        }
                        next if $file !~ /\.pl$/ && $file !~ /config\.yaml$/;
                        my $plugin_file =  File::Spec->catfile( $plugin_full_path, $file );
                        if ( -f $plugin_file ) {
                            # Plugin is a file, add it to list for processing
                            push @plugins, {
                                base => $plugin_full_path,
                                dir  => $plugin_dir,
                                file => $file,
                                # TODO: remove following comment if app is stable
                                # Changed from $plugin_file because load_tmpl was failing
                                path => $plugin_full_path,
                                envelope => "$plugin_lastdir/" . $plugin_dir,
                            };
                        }
                    }
                    # All plugins have been found, now load them
                    foreach my $plugin (@plugins) {
                        if ($plugin->{file} =~ /\.pl$/) {
                            # TODO in Melody 1.1: do NOT load plugin, .pl is deprecated
                            MT->log(
                                {
                                    message => MT->translate( "You are using a plugin ([_1]) that uses a deprecated plugin file format (.pl)", $plugin->{file} ),
                                    class   => 'system',
                                    category => 'deprecation',
                                    level    => MT::Log::INFO(),
                                }
                                );
                            $plugin_envelope = $plugin->{envelope};
                            $plugin_full_path = $plugin->{path};
                            $load_plugin->(
                                $plugin->{path}, $plugin->{file}
                                );
                        } else {
                            my $pclass =
                                $plugin->{dir} =~ m/\.pack$/ ? 'MT::Component' : 'MT::Plugin';
                                
                            # Don't process disabled plugin config.yaml files.
                            if (
                                $pclass eq 'MT::Plugin'
                                && (
                                    !$use_plugins
                                    || ( exists $PluginSwitch->{ $plugin->{dir} }
                                         && !$PluginSwitch->{ $plugin->{dir} } )
                                )
                                )
                            {
                                $Plugins{ $plugin->{dir} }{full_path} = $plugin->{path};
                                $Plugins{ $plugin->{dir} }{enabled}   = 0;
                                next;
                            }

                            # TODO - the plugin signature cannot simply be the base directory
                            #        in the event that there are multiple yaml files. Therefore
                            #        the signature must become a conjunction of the directory and
                            #        config.yaml file.
                            #        To address this, the ultimate normalizer should be the
                            #        id declared in the yaml.
                            # See http://bugs.movabletype.org/?79933
                            local $plugin_sig = $plugin->{dir} . '/' . $plugin->{file};
                            next if exists $Plugins{ $plugin_sig };
                            # TODO - the id needs to be yaml specific, not directory
                            # TODO - the id needs to be reformulated from contents of yaml
                            my $id = lc $plugin->{dir};
                            $id =~ s/\.\w+$//;

                            my $p = $pclass->new(
                                {
                                    id       => $id,
                                    path     => $plugin->{base},
                                    config   => $plugin->{file},
                                    envelope => $plugin->{envelope}
                                }
                                );
                            
                            # rebless? based on config?
                            $Plugins{$plugin_sig}{enabled} = 1;
                            $plugin_envelope = $plugin->{envelope};
                            $plugin_full_path = $plugin->{path};
                            MT->add_plugin($p);
                            $p->init_callbacks();
                            next;
                        }
                    }
>>>>>>> byrnereese/bug-452-loading-pl-plugins
                }
            }
            $Plugins{$plugin_sig}{enabled} = 1;
            return 1;
        }; # end load_plugin sub
        
        if ($plugin->{file} =~ /\.pl$/) {
            # TODO - do NOT load plugin, .pl is incompatible with Melody
            # TODO - issue warning
            $plugin_envelope = $plugin->{envelope};
            $load_plugin->(
                $plugin->{path}, $plugin->{file}
                );
        } else {
            my $pclass =
                $plugin->{type} eq 'pack' ? 'MT::Component' : 'MT::Plugin';
            
            # Don't process disabled plugin config.yaml files.
            if (
                $pclass eq 'MT::Plugin'
                && (
                    !$use_plugins
                    || ( exists $PluginSwitch->{ $plugin->{dir} }
                         && !$PluginSwitch->{ $plugin->{dir} } )
                )
                )
            {
                $Plugins{ $plugin->{dir} }{full_path} = $plugin->{path};
                $Plugins{ $plugin->{dir} }{enabled}   = 0;
                next;
            }
            
            # TODO - the plugin signature cannot simply be the base directory
            #        in the event that there are multiple yaml files. Therefore
            #        the signature must become a conjunction of the directory and
            #        config.yaml file.
            #        To address this, the ultimate normalizer should be the
            #        id declared in the yaml.
            # See http://bugs.movabletype.org/?79933
            local $plugin_sig = $plugin->{dir} . '/' . $plugin->{file};
            next if exists $Plugins{ $plugin_sig };
            # TODO - the id needs to be yaml specific, not directory
            # TODO - the id needs to be reformulated from contents of yaml
            my $id = lc $plugin->{dir};
            $id =~ s/\.\w+$//;
            
            my $p = $pclass->new(
                {
                    id       => $id,
                    path     => $plugin->{base},
                    config   => $plugin->{file},
                    envelope => $plugin->{envelope}
                }
                );
            
            # rebless? based on config?
            $Plugins{$plugin_sig}{enabled} = 1;
            $plugin_envelope = $plugin->{envelope};
            $plugin_full_path = $plugin->{path};
            MT->add_plugin($p);
            $p->init_callbacks();
            next;
        }
    }

    # Reset the Text_filters hash in case it was preloaded by plugins by
    # calling all_text_filters (Markdown in particular does this).
    # Upon calling all_text_filters again, it will be properly loaded by
    # querying the registry.
    %Text_filters = ();

    1;
}