-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPlugins.php
More file actions
272 lines (221 loc) · 9.48 KB
/
Plugins.php
File metadata and controls
272 lines (221 loc) · 9.48 KB
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
<?php
/**
* Copyright (C) 2015-2019 FeatherBB
* based on code by (C) 2008-2015 FluxBB
* and Rickard Andersson (C) 2002-2008 PunBB
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
*/
namespace FeatherBB\Model\Admin;
use FeatherBB\Core\AdminUtils;
use FeatherBB\Core\Database as DB;
use FeatherBB\Core\Error;
use FeatherBB\Core\Interfaces\ForumEnv;
use FeatherBB\Core\Interfaces\Hooks;
use FeatherBB\Core\Interfaces\Router;
use FeatherBB\Core\Plugin as PluginManager;
use ZipArchive;
class Plugins
{
protected $manager;
public function __construct()
{
$this->manager = new PluginManager();
}
public function activate($name)
{
$name = Hooks::fire('model.plugin.activate.name', $name);
// Check if plugin name is valid
if ($class = $this->manager->load($name)) {
$activePlugins = $this->manager->getActivePlugins();
// Check if plugin is not yet activated...
if (!in_array($name, $activePlugins)) {
// Find or create plugin in DB...
$plugin = DB::table('plugins')->where('name', $name)->findOne();
if (!$plugin) {
$plugin = DB::table('plugins')->create()->set('name', $name);
}
// ... Install it if needed ...
if ($plugin->installed != 1) {
if (method_exists($class, 'install')) {
$class->install();
}
$plugin->set('installed', 1);
}
// ... Save in DB ...
$plugin->set('active', 1);
$plugin = Hooks::fireDB('model.plugin.activate', $plugin);
$plugin->save();
// ... And regenerate cache.
$this->manager->setActivePlugins();
return $plugin;
}
return true;
}
return false;
}
/**
* Deactivate a plugin
*/
public function deactivate($name)
{
$name = Hooks::fire('model.plugin.deactivate.name', $name);
// Check if plugin name is valid
if ($class = $this->manager->load($name)) {
$activePlugins = $this->manager->getActivePlugins();
// Check if plugin is actually activated
if (($k = array_search($name, $activePlugins)) !== false) {
$plugin = DB::table('plugins')->where('name', $name)->findOne();
if (!$plugin) {
$plugin = DB::table('plugins')->create()->set('name', $name);
}
// Do we need to run extra code for deactivation ?
if (method_exists($class, 'pause')) {
$class->pause();
}
$plugin->set('active', 0);
$plugin = Hooks::fireDB('model.plugin.deactivate', $plugin);
$plugin->save();
// Regenerate cache
$this->manager->setActivePlugins();
return $plugin;
}
return true;
}
return false;
}
/**
* Uninstall a plugin after deactivated
*/
public function uninstall($name)
{
$name = Hooks::fire('model.plugin.uninstall.name', $name);
// Check if plugin name is valid
if ($class = $this->manager->load($name)) {
$activePlugins = $this->manager->getActivePlugins();
// Check if plugin is disabled, for security
if (!in_array($name, $activePlugins)) {
$plugin = DB::table('plugins')->where('name', $name)->findOne();
if ($plugin) {
$plugin->delete();
}
// Do we need to run extra code for uninstallation ?
if (method_exists($class, 'remove')) {
$class->remove();
}
if (file_exists(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$name)) {
AdminUtils::deleteFolder(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$name);
}
// Regenerate cache
$this->manager->setActivePlugins();
}
return true;
}
return false;
}
/**
* Download a plugin, unzip it and rename it
*/
public function download($args)
{
$zipFile = ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']."-".$args['version'].'.zip';
$zipResource = fopen($zipFile, "w");
// Get the zip file straight from GitHub
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://codeload.github.com/featherbb/' . $args['name'] . '/zip/'.$args['version']);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FILE, $zipResource);
$page = curl_exec($ch);
curl_close($ch);
fclose($zipResource);
if (!$page) {
unlink(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']."-".$args['version'].'.zip');
throw new Error(__('Bad request'), 400);
}
$zip = new ZipArchive;
if ($zip->open($zipFile) != true) {
throw new Error(__('Bad request'), 400);
}
$zip->extractTo(ForumEnv::get('FEATHER_ROOT').'plugins');
$zip->close();
if (file_exists(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name'])) {
AdminUtils::deleteFolder(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']);
}
rename(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']."-".$args['version'], ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']);
unlink(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$args['name']."-".$args['version'].'.zip');
return Router::redirect(Router::pathFor('adminPlugins'), __('Plugin downloaded'));
}
/**
* Upload a plugin manually
*/
public function uploadPlugin($filesData)
{
if (!isset($filesData['req_file'])) {
throw new Error(__('No file'));
}
$uploadedFile = $filesData['req_file'];
// Make sure the upload went smooth
if (isset($uploadedFile['error'])) {
switch ($uploadedFile['error']) {
case 1: // UPLOAD_ERR_INI_SIZE
case 2: // UPLOAD_ERR_FORM_SIZE
throw new Error(__('Too large ini'));
break;
case 3: // UPLOAD_ERR_PARTIAL
throw new Error(__('Partial upload'));
break;
case 4: // UPLOAD_ERR_NO_FILE
throw new Error(__('No file'));
break;
case 6: // UPLOAD_ERR_NO_TMP_DIR
throw new Error(__('No tmp directory'));
break;
default:
// No error occured, but was something actually uploaded?
if ($uploadedFile['size'] == 0) {
throw new Error(__('No file'));
}
break;
}
}
$name = $uploadedFile['name'];
if (is_uploaded_file($uploadedFile['tmp_name'])) {
// Preliminary file check, adequate in most cases
$allowedTypes = ['application/zip', 'application/x-compressed', 'application/x-zip-compressed', 'application/download'];
if (!in_array($uploadedFile['type'], $allowedTypes)) {
throw new Error(__('Bad type'));
}
// Move the file to the plugin directory
if (!@move_uploaded_file($uploadedFile['tmp_name'], ForumEnv::get('FEATHER_ROOT').'plugins'.'/'.$name)) {
throw new Error(__('Move failed'));
}
@chmod(ForumEnv::get('FEATHER_ROOT').'plugins'.'/'.$name, 0644);
} else {
throw new Error(__('Unknown failure'));
}
$zip = new ZipArchive;
if ($zip->open(ForumEnv::get('FEATHER_ROOT').'plugins'.'/'.$name) != true) {
throw new Error(__('Bad request'), 400);
}
$zip->extractTo(ForumEnv::get('FEATHER_ROOT').'plugins');
$zip->close();
$cleanedName = preg_replace('/[0-9]+/', '', $name);
$cleanedName = str_replace('.', '', $cleanedName);
$cleanedName = str_replace('-zip', '', $cleanedName);
$cleanedName = str_replace('zip', '', $cleanedName);
if (file_exists(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$cleanedName)) {
AdminUtils::deleteFolder(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$name);
}
$nameNozip = str_replace('-zip', '', $name);
$nameNozip = str_replace('zip', '', $nameNozip);
rename(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$nameNozip, ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$cleanedName);
unlink(ForumEnv::get('FEATHER_ROOT').'plugins'.DIRECTORY_SEPARATOR.$name);
return Router::redirect(Router::pathFor('adminPlugins'), __('Plugin downloaded'));
}
}