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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
<?php
/**
* Manages plugin packages under mod.
*
* @todo This should eventually be merged into ElggPlugin.
* Currently ElggPlugin objects are only used to get and save
* plugin settings and user settings, so not every plugin
* has an ElggPlugin object. It's not implemented in ElggPlugin
* right now because of conflicts with at least the constructor,
* enable(), disable(), and private settings.
*
* Around 1.9 or so we should each plugin over to using
* ElggPlugin and merge ElggPluginPackage and ElggPlugin.
*
* @package Elgg.Core
* @subpackage Plugins
* @since 1.8
*/
class ElggPluginPackage {
/**
* The required files in the package
*
* @var array
*/
private $requiredFiles = array(
'start.php', 'manifest.xml'
);
/**
* Valid types for provides.
*
* @var array
*/
private $providesSupportedTypes = array(
'plugin', 'php_extension'
);
/**
* The type of requires/conflicts supported
*
* @var array
*/
private $depsSupportedTypes = array(
'elgg_version', 'elgg_release', 'php_extension', 'php_ini', 'plugin'
);
/**
* An invalid plugin error.
*/
private $invalidPluginError = '';
/**
* Any dependencies messages
*/
private $depsMsgs = array();
/**
* The plugin's manifest object
*
* @var ElggPluginManifest
*/
protected $manifest;
/**
* The plugin's full path
*
* @var string
*/
protected $path;
/**
* Is the plugin valid?
*
* @var mixed Bool after validation check, null before.
*/
protected $valid = null;
/**
* The plugin ID (dir name)
*
* @var string
*/
protected $id;
/**
* Load a plugin package from mod/$id or by full path.
*
* @param string $plugin The ID (directory name) or full path of the plugin.
* @param bool $validate Automatically run isValid()?
*
* @return true
* @throws PluginException
*/
public function __construct($plugin, $validate = true) {
if (substr($plugin, 0, 1) == '/') {
// this is a path
$path = sanitise_filepath($plugin);
// the id is the last element of the array
$path_array = explode('/', trim($path, '/'));
$id = array_pop($path_array);
} else {
// this is a plugin id
// strict plugin names
if (preg_match('/[^a-z0-9\.\-_]/i', $id)) {
throw new PluginException(elgg_echo('PluginException:InvalidID', array($plugin)));
}
$path = get_config('pluginspath') . "$plugin/";
$id = $plugin;
}
if (!is_dir($path)) {
throw new PluginException(elgg_echo('PluginException:InvalidPath', array($path)));
}
$this->path = $path;
$this->id = $id;
if ($validate && !$this->isValid()) {
if ($this->invalidPluginError) {
throw new PluginException(elgg_echo('PluginException:InvalidPlugin:Details',
array($plugin, $this->invalidPluginError)));
} else {
throw new PluginException(elgg_echo('PluginException:InvalidPlugin', array($plugin)));
}
}
return true;
}
/********************************
* Validation and sanity checks *
********************************/
/**
* Checks if this is a valid Elgg plugin.
*
* Checks for requires files as defined at the start of this
* class. Will check require manifest fields via ElggPluginManifest
* for Elgg 1.8 plugins.
*
* @note This doesn't check dependencies or conflicts.
* Use {@link ElggPluginPackage::canActivate()} or
* {@link ElggPluginPackage::checkDependencies()} for that.
*
* @return bool
*/
public function isValid() {
if (isset($this->valid)) {
return $this->valid;
}
$valid = true;
// check required files.
$have_req_files = true;
foreach ($this->requiredFiles as $file) {
if (!is_readable($this->path . $file)) {
$have_req_files = false;
$this->invalidPluginError =
elgg_echo('ElggPluginPackage:InvalidPlugin:MissingFile', array($file));
break;
}
}
// check required files
if (!$have_req_files) {
$valid = false;
}
// check for valid manifest.
if (!$this->loadManifest()) {
$valid = false;
}
// can't require or conflict with yourself or something you provide.
// make sure provides are all valid.
if (!$this->isSaneDeps()) {
$valid = false;
}
$this->valid = $valid;
return $valid;
}
/**
* Check the plugin doesn't require or conflict with itself
* or something provides. Also check that it only list
* valid provides. Deps are checked in checkDependencies()
*
* @note Plugins always provide themselves.
*
* @todo Don't let them require and conflict the same thing
*
* @return bool
*/
private function isSaneDeps() {
$conflicts = $this->getManifest()->getConflicts();
$requires = $this->getManifest()->getRequires();
$provides = $this->getManifest()->getProvides();
foreach ($provides as $provide) {
// only valid provide types
if (!in_array($provide['type'], $this->providesSupportedTypes)) {
$this->invalidPluginError =
elgg_echo('ElggPluginPackage:InvalidPlugin:InvalidProvides', array($provide['type']));
return false;
}
// doesn't conflict or require any of its provides
$name = $provide['name'];
foreach (array('conflicts', 'requires') as $dep_type) {
foreach (${$dep_type} as $dep) {
if (!in_array($dep['type'], $this->depsSupportedTypes)) {
$this->invalidPluginError =
elgg_echo('ElggPluginPackage:InvalidPlugin:InvalidDependency', array($dep['type']));
return false;
}
// make sure nothing is providing something it conflicts or requires.
if ($dep['name'] == $name) {
$version_compare = version_compare($provide['version'], $dep['version'], $dep['comparison']);
if ($version_compare) {
$this->invalidPluginError =
elgg_echo('ElggPluginPackage:InvalidPlugin:CircularDep',
array($dep['type'], $dep['name'], $this->id));
return false;
}
}
}
}
}
return true;
}
/************
* Manifest *
************/
/**
* Returns a parsed manifest file.
*
* @return ElggPluginManifest
*/
public function getManifest() {
if (!$this->manifest) {
if (!$this->loadManifest()) {
return false;
}
}
return $this->manifest;
}
/**
* Loads the manifest into this->manifest as an
* ElggPluginManifest object.
*
* @return bool
*/
private function loadManifest() {
$file = $this->path . 'manifest.xml';
try {
$this->manifest = new ElggPluginManifest($file, $this->id);
} catch (Exception $e) {
return false;
}
if ($this->manifest instanceof ElggPluginManifest) {
return true;
}
return false;
}
/***********************
* Dependencies system *
***********************/
/**
* Returns if the Elgg system meets the plugin's dependency
* requirements. This includes both requires and conflicts.
*
* Full reports can be requested. The results are returned
* as an array of arrays in the form array(
* 'type' => requires|conflicts,
* 'dep' => array( dependency array ),
* 'status' => bool if depedency is met,
* 'comment' => optional comment to display to the user.
* )
*
* @param bool $full_report Return a full report.
* @return bool|array
*/
public function checkDependencies($full_report = false) {
$requires = $this->getManifest()->getRequires();
$conflicts = $this->getManifest()->getConflicts();
$enabled_plugins = elgg_get_plugins('active');
$report = array();
foreach (array('requires', 'conflicts') as $dep_type) {
$inverse = ($dep_type == 'conflicts') ? true : false;
foreach (${$dep_type} as $dep) {
switch ($dep['type']) {
case 'elgg_version':
$result = $this->checkDepElgg($dep, get_version());
break;
case 'elgg_release':
$result = $this->checkDepElgg($dep, get_version(true));
break;
case 'plugin':
$result = $this->checkDepPlugin($dep, $enabled_plugins, $inverse);
break;
case 'php_extension':
$result = $this->checkDepPhpExtension($dep);
break;
case 'php_ini':
$result = $this->checkDepPhpIni($dep);
break;
}
// unless we're doing a full report, break as soon as we fail.
if (!$full_report && !$result) {
return $result;
} else {
// build report element and comment
if ($dep_type == 'requires') {
$comment = '';
} elseif ($dep_type == 'conflicts') {
$comment = '';
}
$report[] = array(
'type' => $dep_type,
'dep' => $dep,
'status' => $result,
'comment' => $comment
);
}
}
}
if ($full_report) {
return $report;
}
return true;
}
/**
* Checks if $plugins meets the requirement by $dep.
*
* @param array $dep An Elgg manifest.xml deps array
* @param array $plugins A list of plugins as returned by get_installed_plugins();
* @param bool $inverse Inverse the results to use as a conflicts.
* @return bool
*/
private function checkDepPlugin(array $dep, array $plugins, $inverse = false) {
$r = elgg_check_plugins_provides('plugin', $dep['name'], $dep['version'], $dep['comparison']);
if ($inverse) {
$r = !$r;
}
return $r;
}
/**
* Checks if $elgg_version meets the requirement by $dep.
*
* @param array $dep An Elgg manifest.xml deps array
* @param array $elgg_version An Elgg version (either YYYYMMDDXX or X.Y.Z)
* @param bool $inverse Inverse the result to use as a conflicts.
* @return bool
*/
private function checkDepElgg(array $dep, $elgg_version, $inverse = false) {
$r = version_compare($elgg_version, $dep['version'], $dep['comparison']);
if ($inverse) {
$r = !$r;
}
return $r;
}
/**
* Checks if the PHP extension in $dep is loaded.
*
* @todo Can this be merged with the plugin checker?
*
* @param array $dep An Elgg manifest.xml deps array
* @return bool
*/
private function checkDepPhpExtension(array $dep) {
$name = $dep['name'];
$version = $dep['version'];
$comparison = $dep['comparison'];
// not enabled.
$r = extension_loaded($name);
// enabled. check version.
$ext_version = phpversion($name);
if ($version && !version_compare($ext_version, $version, $comparison)) {
$r = false;
}
// some php extensions can be emulated, so check provides.
if ($r == false) {
$r = elgg_check_plugins_provides('php_extension', $name, $version, $comparison);
}
return $r;
}
/**
* Check if the PHP ini setting satisfies $dep.
*
* @param array $dep An Elgg manifest.xml deps array
* @return bool
*/
private function checkDepPhpIni($dep) {
$name = $dep['name'];
$value = $dep['value'];
$comparison = $dep['comparison'];
// ini_get() normalizes truthy values to 1 but falsey values to 0 or ''.
// version_compare() considers '' < 0, so normalize '' to 0.
// ElggPluginManifest normalizes all bool values and '' to 1 or 0.
$setting = ini_get($name);
if ($setting === '') {
$setting = 0;
}
$r = version_compare($setting, $value, $comparison);
return $r;
}
/**
* Activate the plugin.
*
* @note This method is activate() to avoid clashing with ElggEntity::enable()
*
* @return bool
*/
public function activate() {
return enable_plugin($this->getID());
}
/**
* Deactivate the plugin.
*
* @note This method is deactivate() to avoid clashing with ElggEntity::disable()
*
* @return bool
*/
public function deactivate() {
return disable_plugin($this->getID());
}
/**
* Returns the Plugin ID
*
* @return string
*/
public function getID() {
return $this->id;
}
}
|