plugins.php 8.18 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
                if (!safe_require_plugin($plugin, $key)) {
                    continue;
44
                }
45
46
47
48
                $plugins[$plugin]['installed'][$key] = array(
                    'active' => $i->active,
                    'disableable' => call_static_method(generate_class_name($plugin, $key), 'can_be_disabled'),
                );
49
                if ($plugins[$plugin]['installed'][$key]['disableable'] || !$i->active) {
50
51
                    $plugins[$plugin]['installed'][$key]['activateform'] = activate_plugin_form($plugin, $i);
                }
52
53
54
55
56
57
58
59
60
61
                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
62
                                $plugins[$plugin]['installed'][$key]['types'][$t] =
63
64
                                    call_static_method($classname, 'has_config');
                            }
65
                        }
Penny Leach's avatar
Penny Leach committed
66
                    }
Aaron Wells's avatar
Aaron Wells committed
67
                }
68
69
                else {
                    $classname = generate_class_name($plugin, $i->name);
70
                    safe_require($plugin, $key);
71
72
73
                    if (call_static_method($classname, 'has_config')) {
                        $plugins[$plugin]['installed'][$key]['config'] = true;
                    }
Penny Leach's avatar
Penny Leach committed
74
75
76
                }
            }
        }
Aaron Wells's avatar
Aaron Wells committed
77

78
79
80
81
82
        $dirhandle = opendir(get_config('docroot') . $plugin);
        while (false !== ($dir = readdir($dirhandle))) {
            $installed = false; // reinitialise
            if (strpos($dir, '.') === 0) {
                continue;
83
            }
84
85
            if (!is_dir(get_config('docroot') . $plugin . '/' . $dir)) {
                continue;
86
            }
87
88
89
90
91
92
            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)) {
93
94
                continue;
            }
95
96
97
98
            if (empty($installed)) {
                $plugins[$plugin]['notinstalled'][$dir] = array();
                try {
                    validate_plugin($plugin, $dir);
99
100
                    $classname = generate_class_name($plugin, $dir);
                    $classname::sanity_check();
101
102
103
                }
                catch (InstallationException $e) {
                    $plugins[$plugin]['notinstalled'][$dir]['notinstallable'] = $e->GetMessage();
104
                }
105
106
107
108
            }
            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)) {
109
110
                    continue;
                }
111
112
113
114
115
116
117
118

                $btdirhandle = opendir($btlocation);
                while (false !== ($btdir = readdir($btdirhandle))) {
                    if (strpos($btdir, '.') === 0) {
                        continue;
                    }
                    if (!is_dir(get_config('docroot') . $plugin . '/' . $dir . '/blocktype/' . $btdir)) {
                        continue;
119
                    }
120
121
122
123
124
                    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
125
                            validate_plugin('blocktype', $dir . '/' . $btdir,
126
127
128
129
130
131
                                get_config('docroot') . 'artefact/' . $dir . '/blocktype/' . $btdir);
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir] = array();
                        }
                        catch (InstallationException $_e) {
                            $plugins['blocktype']['notinstalled'][$dir . '/' . $btdir]['notinstallable'] = $_e->getMessage();
                        }
132
133
134
135
                    }
                }
            }
        }
Penny Leach's avatar
Penny Leach committed
136
137
138
    }
}

139
global $THEME;
140
141
142
143

$loadingicon = 'fa fa-spinner fa-pulse';
$successicon = 'fa fa-check text-success';
$failureicon = 'fa fa-exclaimation-triangle';
144

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

$javascript = <<<JAVASCRIPT

function installplugin(name) {
152
    $(name + '.message').innerHTML = '<span class="{$loadingicon}" title=' + {$loadingstring} + '"></span>';
153

154
    sendjsonrequest('../upgrade.json.php', { 'name': name }, 'GET', function (data) {
Penny Leach's avatar
Penny Leach committed
155
        if (!data.error) {
156
            var message = {$successstring} + data.newversion;
157
            $(name + '.message').innerHTML = '<span class="{$successicon}" title=":)"></span>' + message;
158
            $(name + '.install').innerHTML = '';
Aaron Wells's avatar
Aaron Wells committed
159
            // move the whole thing into the list of installed plugins
Penny Leach's avatar
Penny Leach committed
160
161
162
            // new parent node
            var bits = name.split('\.');
            var newparent = $(bits[0] + '.installed');
163
164
165
166
167
168
            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';
            }
169
        }
Penny Leach's avatar
Penny Leach committed
170
        else {
171
            var message = '';
172
173
            if (data.errormessage) {
                message = data.errormessage;
Aaron Wells's avatar
Aaron Wells committed
174
            }
175
            else {
176
                message = {$failurestring};
177
            }
178
            $(name).innerHTML = '<span class="{$failureicon}" title=":("></span>' + message;
179
180
        }
    },
181
    function () {
182
        message = {$failurestring};
183
184
185
        $(name).innerHTML = message;
    },
    true);
186
187
188
189
}
JAVASCRIPT;


Penny Leach's avatar
Penny Leach committed
190
$smarty = smarty();
191
$smarty->assign('INLINEJAVASCRIPT', $javascript);
Penny Leach's avatar
Penny Leach committed
192
$smarty->assign('plugins', $plugins);
193
$smarty->assign('installlink', 'installplugin');
194
$smarty->assign('PAGEHEADING', TITLE);
Naomi Guyer's avatar
Naomi Guyer committed
195
$smarty->assign('PAGEICON', 'fa fa-puzzle-piece');
196
$smarty->display('admin/extensions/plugins.tpl');