generated from aljawaid/KanboardSkeletonPlugin
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
PluginManagerController.php
263 lines (227 loc) · 7.31 KB
/
PluginManagerController.php
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
<?php
namespace Kanboard\Plugin\PluginManager\Controller;
use Kanboard\Controller\BaseController;
use Kanboard\Core\Plugin\Directory;
use Kanboard\Core\Plugin\PluginInstallerException;
use Kanboard\Plugin\PluginManager\Controller\Installer;
use ZipArchive;
/**
* Plugin PluginManager
*
* Class PluginManagerController
* @package Kanboard\Controller
* @author Frederic Guillot
* @author aljawaid
* @author alfredbuehler Alfred Bühler
*/
class PluginManagerController extends \Kanboard\Controller\PluginController
{
/**
* Display the Plugin Problems Page
*
* @access public
* @author aljawaid
*/
public function show()
{
$this->response->html($this->helper->layout->plugin('pluginManager:info/plugin-problems', array(
'title' => t('Plugin Manager') . ' ⥂ ' . t('Plugin Problems'),
)));
}
/**
* Display the Plugin Info Page
*
* @access public
* @author aljawaid
*/
public function showPluginInfo()
{
$this->response->html($this->helper->layout->plugin('pluginManager:info/plugin-info', array(
'title' => t('Plugin Manager') . ' ⥂ ' . t('Plugin Info'),
'available_plugins' => Directory::getInstance($this->container)->getAvailablePlugins()
)));
}
/**
* Display the Manual Plugins Page
*
* @access public
* @author aljawaid
*/
public function showManualPlugins()
{
$this->response->html($this->helper->layout->plugin('pluginManager:plugin/manual-plugins', array(
'title' => t('Plugin Manager') . ' ⥂ ' . t('Manual Plugins'),
)));
}
/**
* Install a Plugin
*
* @author alfredbuehler Alfred Bühler
*/
public function installPlugin()
{
$rc = false;
if (strlen($archiveURL = urldecode($this->request->getValue('plugin_url'))) > 0) {
$rc = $this->installByURL($archiveURL);
}
if (strlen($archiveFile = $this->request->getFilePath('plugin_file')) > 0) {
$rc = $this->installByFile($archiveFile);
}
$this->response->redirect($rc
? $this->helper->url->to('PluginController', 'show')
: $this->helper->url->to('PluginManagerController', 'showManualPlugins', array('plugin' => 'PluginManager')));
}
/**
* Install a Plugin from URL
*
* @param string $archiveUrl
* @return bool
* @author alfredbuehler Alfred Bühler
*/
private function installByURL(string $archiveUrl): bool
{
try {
$installer = new Installer($this->container);
$archiveFile = $installer->downloadPluginArchive($archiveUrl);
$this->installByFile($archiveFile);
} catch (PluginInstallerException $e) {
$this->flash->failure($e->getMessage());
return false;
}
return true;
}
/**
* Install a Plugin from File
*
* @param string $archiveFile
* @return bool
* @author alfredbuehler Alfred Bühler
*/
private function installByFile(string $archiveFile): bool
{
if (file_exists($archiveFile)) {
$zip = new ZipArchive();
try {
if ($zip->open($archiveFile) !== true) {
throw new PluginInstallerException(t('Unable to open the plugin archive'));
}
$dirname = getPluginDir($zip);
$plugin = getPluginFile($zip, $dirname);
$namespace = getPluginNamespace($plugin);
if ($dirname != "$namespace/") {
throw new PluginInstallerException(t('The directory name (%s) does not match with the namespace (%s)', $dirname, $namespace));
}
$pluginName = getPluginName($plugin);
if ($pluginName != $namespace) {
throw new PluginInstallerException(t('The plugin name (%s) does not match with the namespace (%s)', $pluginName, $namespace));
}
if (!$zip->extractTo(PLUGINS_DIR)) {
$zip->close();
throw new PluginInstallerException(t('Unable to extract the plugin archive'));
}
// Success
$zip->close();
unlink($archiveFile);
$this->flash->success(t('Plugin installed successfully'));
} catch (PluginInstallerException $e) {
unlink($archiveFile);
$this->flash->failure($e->getMessage());
return false;
}
} else {
$this->flash->failure(t('Plugin archive file not found'));
return false;
}
return true;
}
}
// phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
/**
* Different class in file
* Added by @author
*
* @author alfredbuehler Alfred Bühler
*/
class Installer extends \Kanboard\Core\Plugin\Installer
{
/**
* Download Archive
*
* @param string $archiveUrl
* @return string Downloaded $archiveFile
* @author alfredbuehler Alfred Bühler
*/
public function downloadPluginArchive($archiveUrl): string
{
$zip = parent::downloadPluginArchive($archiveUrl); // zip is open!
$archiveFile = $zip->filename;
$zip->close();
return $archiveFile;
}
}
//phpcs enable
/**
* Get the Directory Name
*
* @param ZipArchive open zip
* @return string directory name in archive
* @author alfredbuehler Alfred Bühler
*/
function getPluginDir(ZipArchive $zip): string
{
$dirname = $zip->getNameIndex(0);
if ($dirname === false) {
throw new PluginInstallerException(t('Plugin directory name was not found'));
}
return $dirname;
}
/**
* Get the Plugin.php
*
* @param ZipArchive Open zip
* @param string Directory name in archive
* @return string Content of Plugin.php
* @author alfredbuehler Alfred Bühler
*/
function getPluginFile(ZipArchive $zip, string $dirname): string
{
$plugin = $zip->getFromName($dirname . 'Plugin.php');
if ($plugin === false) {
throw new PluginInstallerException(t('File \'Plugin.php\' could not get extracted'));
}
return $plugin;
}
/**
* Get the Namespace
*
* @param string Content of Plugin.php
* @return string Namespace of plugin
* @author alfredbuehler Alfred Bühler
*/
function getPluginNamespace(string $plugin): string
{
$match = [];
$rc = preg_match("/^namespace Kanboard\\\\Plugin\\\\(\w{1,});/m", $plugin, $match);
if ($rc == false || $rc != 1) {
throw new PluginInstallerException('The namespace was not found');
}
$namespace = $match[1];
return $namespace;
}
/**
* Get the Plugin's Name
*
* @param string Content of Plugin.php
* @return string Name of plugin
* @author alfredbuehler Alfred Bühler
*/
function getPluginName(string $plugin): string
{
$match = [];
$rc = preg_match('/(public function getPluginName\(\))(.|\n)*return ["\'](.*)["\']/U', $plugin, $match);
if ($rc == false || $rc != 1) {
throw new PluginInstallerException(t('The plugin name was not found'));
}
$pluginName = $match[3];
return $pluginName;
}