plugins.php 8.31 KB
Newer Older
Penny Leach's avatar
Penny Leach committed
1
2
3
4
5
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
6
 * @author     Catalyst IT Ltd
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
Penny Leach's avatar
Penny Leach committed
9
10
11
12
13
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'configextensions/pluginadmin');
Penny Leach's avatar
Penny Leach committed
15
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Martyn Smith's avatar
Martyn Smith committed
16
define('TITLE', get_string('pluginadmin', 'admin'));
Penny Leach's avatar
Penny Leach committed
17
18
19
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'plugins');
20
require('upgrade.php');
Penny Leach's avatar
Penny Leach committed
21

22
23
24
// @TODO when artefact plugins get installed, move the not installed blocktypes
// that get installed into the list of installed blocktype plugins

Penny Leach's avatar
Penny Leach committed
25
$plugins = array();
26
$plugins['blocktype'] = array();
Penny Leach's avatar
Penny Leach committed
27
28

foreach (plugin_types()  as $plugin) {
29
    // this has to happen first because of broken artefact/blocktype ordering
Penny Leach's avatar
Penny Leach committed
30
31
32
    $plugins[$plugin] = array();
    $plugins[$plugin]['installed'] = array();
    $plugins[$plugin]['notinstalled'] = array();
33
}
34
foreach (array_keys($plugins) as $plugin) {
35
    if (table_exists(new XMLDBTable($plugin . '_installed'))) {
36
        if ($installed = plugins_installed($plugin, true)) {
37
38
39
40
41
            foreach ($installed as $i) {
                $key = $i->name;
                if ($plugin == 'blocktype') {
                    $key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
                }
42
43
44
45
46
                if (!safe_require_plugin($plugin, $key)) {
                    if ($i->active) {
                        $SESSION->add_error_msg(get_string('missingplugindisabled', 'admin', hsc("$plugin:$key")));
                    }
                    continue;
47
                }
48
49
50
51
                $plugins[$plugin]['installed'][$key] = array(
                    'active' => $i->active,
                    'disableable' => call_static_method(generate_class_name($plugin, $key), 'can_be_disabled'),
                );
52
                if ($plugins[$plugin]['installed'][$key]['disableable'] || !$i->active) {
53
54
                    $plugins[$plugin]['installed'][$key]['activateform'] = activate_plugin_form($plugin, $i);
                }
55
56
57
58
59
60
61
62
63
64
                if ($plugin == 'artefact') {
                    $plugins[$plugin]['installed'][$key]['types'] = array();
                    safe_require('artefact', $key);
                    if ($types = call_static_method(generate_class_name('artefact', $i->name), 'get_artefact_types')) {
                        foreach ($types as $t) {
                            $classname = generate_artefact_class_name($t);
                            if ($collapseto = call_static_method($classname, 'collapse_config')) {
                                $plugins[$plugin]['installed'][$key]['types'][$collapseto] = true;
                            }
                            else {
Aaron Wells's avatar
Aaron Wells committed
65
                                $plugins[$plugin]['installed'][$key]['types'][$t] =
66
67
                                    call_static_method($classname, 'has_config');
                            }
68
                        }
Penny Leach's avatar
Penny Leach committed
69
                    }
Aaron Wells's avatar
Aaron Wells committed
70
                }
71
72
                else {
                    $classname = generate_class_name($plugin, $i->name);
73
                    safe_require($plugin, $key);
74
75
76
                    if (call_static_method($classname, 'has_config')) {
                        $plugins[$plugin]['installed'][$key]['config'] = true;
                    }
Penny Leach's avatar
Penny Leach committed
77
78
79
                }
            }
        }
Aaron Wells's avatar
Aaron Wells committed
80

81
82
83
84
85
        $dirhandle = opendir(get_config('docroot') . $plugin);
        while (false !== ($dir = readdir($dirhandle))) {
            $installed = false; // reinitialise
            if (strpos($dir, '.') === 0) {
                continue;
86
            }
87
88
            if (!is_dir(get_config('docroot') . $plugin . '/' . $dir)) {
                continue;
89
            }
90
91
92
93
94
95
            if (array_key_exists($dir, $plugins[$plugin]['installed'])) {
                $installed = true;
            }
            // if we're already installed keep going
            // if we're an artefact plugin, we have to check for blocktypes.
            if ($plugin != 'artefact' && !empty($installed)) {
96
97
                continue;
            }
98
99
100
101
            if (empty($installed)) {
                $plugins[$plugin]['notinstalled'][$dir] = array();
                try {
                    validate_plugin($plugin, $dir);
102
103
                    $classname = generate_class_name($plugin, $dir);
                    $classname::sanity_check();
104
105
106
                }
                catch (InstallationException $e) {
                    $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $e->GetMessage();
107
                }
108
109
110
111
            }
            if ($plugin == 'artefact' && table_exists(new XMLDBTable('blocktype_installed'))) { // go check it for blocks as well
                $btlocation = get_config('docroot') . $plugin . '/' . $dir . '/blocktype';
                if (!is_dir($btlocation)) {
112
113
                    continue;
                }
114
115
116
117
118
119
120
121

                $btdirhandle = opendir($btlocation);
                while (false !== ($btdir = readdir($btdirhandle))) {
                    if (strpos($btdir, '.') === 0) {
                        continue;
                    }
                    if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
                        continue;
122
                    }
123
124
125
126
127
                    if (!array_key_exists($dir . '/' . $btdir, $plugins['blocktype']['installed'])) {
                        try {
                            if (!array_key_exists($dir, $plugins['artefact']['installed'])) {
                                throw new InstallationException(get_string('blocktypeprovidedbyartefactnotinstallable', 'error', $dir));
                            }
Aaron Wells's avatar
Aaron Wells committed
128
                            validate_plugin('blocktype', $dir . '/' . $btdir,
129
130
131
132
133
134
                                get_config('docroot') . 'artefact/' . $dir . '/blocktype/' . $btdir);
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir] = array();
                        }
                        catch (InstallationException $_e) {
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir]['notinstallable'] = $_e->getMessage();
                        }
135
136
137
138
                    }
                }
            }
        }
Penny Leach's avatar
Penny Leach committed
139
140
141
    }
}

142
143
global $THEME;
$loadingicon = $THEME->get_url('images/loading.gif');
144
145
$successicon = $THEME->get_url('images/success.png');
$failureicon = $THEME->get_url('images/failure.png');
146

147
148
149
$loadingstring = json_encode(get_string('upgradeloading', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
150
151
152
153

$javascript = <<<JAVASCRIPT

function installplugin(name) {
154
    $(name + '.message').innerHTML = '<img src="{$loadingicon}" alt=' + {$loadingstring} + '" />';
155

156
    sendjsonrequest('../upgrade.json.php', { 'name': name }, 'GET', function (data) {
Penny Leach's avatar
Penny Leach committed
157
        if (!data.error) {
158
            var message = {$successstring} + data.newversion;
Penny Leach's avatar
Penny Leach committed
159
            $(name + '.message').innerHTML = '<img src="{$successicon}" alt=":)" />  ' + message;
160
            $(name + '.install').innerHTML = '';
Aaron Wells's avatar
Aaron Wells committed
161
            // move the whole thing into the list of installed plugins
Penny Leach's avatar
Penny Leach committed
162
163
164
            // new parent node
            var bits = name.split('\.');
            var newparent = $(bits[0] + '.installed');
165
166
167
168
169
170
            var oldparent = $(name).parentNode;
            insertSiblingNodesBefore(newparent, $(name));
            // If there are no more plugins left for this type to be installed
            if (oldparent.children.length == 0) {
                oldparent.parentNode.style.display = 'none';
            }
171
        }
Penny Leach's avatar
Penny Leach committed
172
        else {
173
            var message = '';
174
175
            if (data.errormessage) {
                message = data.errormessage;
Aaron Wells's avatar
Aaron Wells committed
176
            }
177
            else {
178
                message = {$failurestring};
179
180
181
182
            }
            $(name).innerHTML = '<img src="{$failureicon}" alt=":(" /> ' + message;
        }
    },
183
    function () {
184
        message = {$failurestring};
185
186
187
        $(name).innerHTML = message;
    },
    true);
188
189
190
191
}
JAVASCRIPT;


Penny Leach's avatar
Penny Leach committed
192
$smarty = smarty();
193
$smarty->assign('INLINEJAVASCRIPT', $javascript);
Penny Leach's avatar
Penny Leach committed
194
$smarty->assign('plugins', $plugins);
195
$smarty->assign('installlink', 'installplugin');
196
$smarty->assign('PAGEHEADING', TITLE);
197
$smarty->display('admin/extensions/plugins.tpl');