/
plugin_api.inc.php
1871 lines (1615 loc) · 69.2 KB
/
plugin_api.inc.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
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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
# Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
# All rights reserved. See LICENSE file for licensing details
if (IN_serendipity !== true) {
die ('Don\'t hack!');
}
// List of bundled core plugins
define('BUNDLED_PLUGINS',
array(
'serendipity_event_bbcode',
'serendipity_event_creativecommons',
'serendipity_event_emoticate',
'serendipity_event_entryproperties',
'serendipity_event_mailer',
'serendipity_event_nl2br',
'serendipity_event_responsiveimages',
'serendipity_event_s9ymarkup',
'serendipity_event_spamblock',
'serendipity_event_spartacus',
'serendipity_event_templatechooser',
'serendipity_event_textile',
'serendipity_event_xhtmlcleanup',
'serendipity_plugin_archives',
'serendipity_plugin_calendar',
'serendipity_plugin_categories',
'serendipity_plugin_comments',
'serendipity_plugin_creativecommons',
'serendipity_plugin_entrylinks',
'serendipity_plugin_eventwrapper',
'serendipity_plugin_history',
'serendipity_plugin_html_nugget',
'serendipity_plugin_plug',
'serendipity_plugin_recententries',
'serendipity_plugin_remoterss',
'serendipity_plugin_superuser',
'serendipity_plugin_syndication',
'serendipity_plugin_templatedropdown'
)
);
include_once S9Y_INCLUDE_PATH . 'include/functions.inc.php';
/* Core API function mappings
* This allows the s9y Core to also execute internal core actions on plugin API hooks
* Future use: Global variable can be customized/overriden by your own plugin on the frontend_configure event
* or during runtime. The capabilities are theme or plugin based only.
*/
$serendipity['capabilities']['jquery'] = true;
$serendipity['capabilities']['jquery_backend'] = true;
$serendipity['capabilities']['jquery-noconflict'] = true; //set as being deprecated, while we should not need it anymore
$serendipity['core_events']['frontend_header']['jquery'] = 'serendipity_plugin_api_frontend_header';
$serendipity['core_events']['backend_header']['jquery'] = 'serendipity_plugin_api_backend_header';
// Add jquery to all frontend templates (in noConflict mode)
function serendipity_plugin_api_frontend_header($event_name, &$bag, &$eventData, $addData) {
global $serendipity;
// Only execute if current template (only) does not have its own jquery.js file
// jquery can be disabled if a template's config.inc.php or a plugin sets
// $serendipity['capabilities']['jquery'] = false
$check = file_exists($serendipity['serendipityPath'] . $serendipity['templatePath'] . $serendipity['template'] . '/jquery.js');
if (!$check && $serendipity['capabilities']['jquery']) {
?>
<script src="<?php echo $serendipity['serendipityHTTPPath']; ?>templates/jquery.js"></script>
<?php
if ($serendipity['capabilities']['jquery-noconflict']) {
?>
<script>jQuery.noConflict();</script>
<?php
}
}
}
// Add jquery to all backend templates
function serendipity_plugin_api_backend_header($event_name, &$bag, &$eventData, $addData) {
global $serendipity;
// Only execute if current template does not have its own backend_jquery.js file
// jquery can be disabled if a template's config.inc.php or a plugin sets
// $serendipity['capabilities']['jquery'] = false
$check = serendipity_getTemplateFile('jquery_backend.js', 'serendipityPath', true);
if (!$check && $serendipity['capabilities']['jquery_backend']) {
?>
<script src="<?php echo $serendipity['serendipityHTTPPath']; ?>templates/jquery.js"></script>
<?php
}
}
// Add backend core (pre) hooks
function serendipity_plugin_api_core_event_hook($event, &$bag, &$eventData, &$addData) {
global $serendipity;
switch($event) {
case 'js_backend':
case 'js':
// Add a global available (index.tpl; admin/index.tpl; preview_iframe.tpl) redirect error string function used by errorToExceptionHandler()
// hardened by admin only - better have that here, to be reachable everywhere
if( $serendipity['production'] === true && $serendipity['serendipityUserlevel'] >= USERLEVEL_ADMIN ) {
echo "
function errorHandlerCreateDOM(htmlStr) {
var frag = document.createDocumentFragment(),
temp = document.createElement('div');
temp.innerHTML = htmlStr;
while (temp.firstChild) {
frag.appendChild(temp.firstChild);
}
return frag;
} \n";
}
break;
case 'external_plugin':
if ($eventData == 'admin/serendipity_editor.js') {
if (! headers_sent()) {
header('Content-Type: application/javascript');
}
echo serendipity_smarty_show('admin/serendipity_editor.js.tpl', null, 'JS', 'include/plugin_api.inc.php:external_plugin');
}
break;
case 'backend_save':
case 'backend_publish':
// this is preview_iframe.tpl updertHooks [ NOT ONLY!! See freetags ]
if ((($serendipity['GET']['adminAction'] ?? '') == 'save') && ($serendipity['GET']['preview'] ?? true) == 'false') {
echo "\n".'<script>document.addEventListener("DOMContentLoaded", function() { window.parent.serendipity.eraseEntryEditorCache(); });</script>'."\n";
}
break;
}
}
/* This file defines the plugin API for serendipity.
* By extending these classes, you can add your own code
* to appear in the sidebar(s) of serendipity.
*
*
* The system defines a number of built-in plugins; these are
* identified by @class_name.
*
* Third-party plugins are identified by the name of the folder into
* which they were uploaded (so there is no @ sign at the start of
* their class name.
*
* The user creates instances of plugins; an instance is assigned
* an identifier like this:
* classname:uniqid()
*
* The user can configure instances of plugins.
*/
class serendipity_plugin_api
{
/**
* Register the default list of plugins for installation.
*
* @access public
* @return null
*/
static function register_default_plugins()
{
/* Register default sidebar plugins, order matters */
serendipity_plugin_api::create_plugin_instance('@serendipity_plugin_archives');
serendipity_plugin_api::create_plugin_instance('@serendipity_plugin_categories');
serendipity_plugin_api::create_plugin_instance('@serendipity_plugin_syndication');
serendipity_plugin_api::create_plugin_instance('@serendipity_plugin_superuser');
serendipity_plugin_api::create_plugin_instance('@serendipity_plugin_plug');
/* Register default event plugins */
serendipity_plugin_api::create_plugin_instance('serendipity_event_s9ymarkup', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_emoticate', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_nl2br', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_spamblock', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_spartacus', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_entryproperties', null, 'event');
serendipity_plugin_api::create_plugin_instance('serendipity_event_responsiveimages', null, 'event');
/* Register additional plugins? */
if (file_exists(S9Y_INCLUDE_PATH . 'plugins/preload.txt')) {
// Expects this format, one plugin per line:
// serendipity_event_xxx:event
// serendipity_plugin_xxx:left
$plugins = file(S9Y_INCLUDE_PATH . 'plugins/preload.txt');
foreach($plugins AS $plugin) {
$plugin = trim($plugin);
if (empty($plugin)) {
continue;
}
$plugin_info = explode(':', $plugin);
serendipity_plugin_api::create_plugin_instance($plugin_info[0], null, $plugin_info[1]);
}
}
}
/**
* Create an instance of a plugin.
*
* $plugin_class_id is of the form:
* @class_name for a built-in plugin
* or
* plugin_dir_name for a third-party plugin
* returns the instance identifier for the newly created plugin.
*
* TO BE IMPLEMENTED:
* If $copy_from_instance is not null, and identifies another plugin
* of the same class, then the persistent state will be copied.
* This allows the user to clone a plugin.
*
* @access public
* @param string classname of the plugin to insert (see description above for details)
* @param boolean (reserved) variable to indicate a copy of an existing instance
* @param string The type of the plugin to insert (event/left/right/hide/eventh)
* @param int The authorid of the plugin owner
* @param string The source path of the plugin file
* @return string ID of the new plugin
*/
static function create_plugin_instance($plugin_class_id, $copy_from_instance = null, $default_placement = 'right', $authorid = '0', $pluginPath = '')
{
global $serendipity;
$id = bin2hex(random_bytes(16));
$key = $plugin_class_id . ':' . $id;
$key = serendipity_db_escape_string($key);
// Secure Plugin path. No leading slashes, no backslashes, no "up" directories
$pluginPath = preg_replace('@^(/)@', '', $pluginPath);
$pluginPath = str_replace(array('..', "\\"), array('', '/'), serendipity_db_escape_string($pluginPath));
if ($pluginPath == 'online_repository') {
$pluginPath = $key;
}
$rs = serendipity_db_query("SELECT MAX(sort_order) as sort_order_max FROM {$serendipity['dbPrefix']}plugins WHERE placement = '$default_placement'", true, 'num');
if (is_array($rs) && isset($rs[0]) && !empty($rs[0])) {
$nextidx = intval($rs[0] + 1);
} else {
$nextidx = 0;
}
$serendipity['debug']['pluginload'][] = "Installing plugin: " . print_r(func_get_args(), true);
$iq = "INSERT INTO {$serendipity['dbPrefix']}plugins (name, sort_order, placement, authorid, path) values ('" . serendipity_db_escape_string(serendipity_specialchars($key)) . "', $nextidx, '$default_placement', '$authorid', '" . serendipity_specialchars($pluginPath) . "')";
$serendipity['debug']['pluginload'][] = $iq;
serendipity_db_query($iq);
serendipity_plugin_api::hook_event('backend_plugins_new_instance', $key, array('default_placement' => $default_placement));
/* Check for multiple dependencies */
$plugin =& serendipity_plugin_api::load_plugin($key, $authorid, $pluginPath);
if (is_object($plugin)) {
$bag = new serendipity_property_bag();
$plugin->introspect($bag);
serendipity_plugin_api::get_event_plugins(false, true); // Refresh static list of plugins to allow execution of added plugin
$plugin->register_dependencies(false, $authorid);
$plugin->install();
} else {
$serendipity['debug']['pluginload'][] = "Loading plugin failed painfully. File not found?";
echo '<span class="msg_error">' . ERROR . ': ' . serendipity_specialchars($key) . ' (' . serendipity_specialchars($pluginPath) . ')</span>';
}
return $key;
}
/**
* Removes a plugin by it's instance name
*
* @access public
* @param string The name of the plugin id ("serendipity_plugin_xxx:1232132fsdf")
* @return null
*/
static function remove_plugin_instance($plugin_instance_id)
{
global $serendipity;
$plugin_instance_id = serendipity_db_escape_string($plugin_instance_id);
$plugin =& serendipity_plugin_api::load_plugin($plugin_instance_id);
if (is_object($plugin)) {
$bag = new serendipity_property_bag();
$plugin->introspect($bag);
$plugin->uninstall($bag);
}
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}plugins where name='$plugin_instance_id'");
if (is_object($plugin)) {
$plugin->register_dependencies(true);
}
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}config where name LIKE '$plugin_instance_id/%'");
return;
}
/**
* Removes an empty plugin configuration value
*
* @access public
* @param string The name of the plugin id ("serendipity_plugin_xxx:1232132fsdf")
* @param array An array of configuration item names
* @return null
*/
static function remove_plugin_value($plugin_instance_id, $where)
{
global $serendipity;
$where_sql = array();
foreach($where AS $key) {
$where_sql[] = "(name LIKE '{$plugin_instance_id}/{$key}_%' AND value = '')";
}
$query = "DELETE FROM {$serendipity['dbPrefix']}config
WHERE " . implode(' OR ', $where_sql);
serendipity_db_query($query);
}
/**
* Retrieve a list of available plugin classes
*
* This function searches through all directories and loaded internal files and tries
* to detect the serendipity plugins.
*
* @access public
* @param boolean If true, only event plugins will be searched. If false, sidebar plugins will be searched.
* @return
*/
static function &enum_plugin_classes($event_only = false)
{
global $serendipity;
$classes = array();
/* built-in classes first */
$cls = get_declared_classes();
foreach ($cls AS $class_name) {
if (strncmp($class_name, 'serendipity_', 6)) {
continue;
}
$p = get_parent_class($class_name);
while ($p != 'serendipity_plugin' && $p != 'serendipity_event' && $p !== false) {
$p = get_parent_class($p);
}
if ($p == 'serendipity_plugin' && $class_name != 'serendipity_event' && (!$event_only || is_null($event_only))) {
$classes[$class_name] = array('name' => '@' . $class_name,
'type' => 'internal_event',
'true_name' => $class_name,
'pluginPath' => '');
} elseif ($p == 'serendipity_event' && $class_name != 'serendipity_event' && ($event_only || is_null($event_only))) {
$classes[$class_name] = array('name' => '@' . $class_name,
'type' => 'internal_plugin',
'true_name' => $class_name,
'pluginPath' => '');
}
}
/* GLOBAL third-party classes next */
$ppath = serendipity_getRealDir(__FILE__) . 'plugins';
serendipity_plugin_api::traverse_plugin_dir($ppath, $classes, $event_only);
/* LOCAL third-party classes next */
$local_ppath = $serendipity['serendipityPath'] . 'plugins';
if ($ppath != $local_ppath) {
serendipity_plugin_api::traverse_plugin_dir($local_ppath, $classes, $event_only);
}
return $classes;
}
/**
* Traverse a specific directory and search if a serendipity plugin exists there.
*
* @access public
* @param string The path to start from (usually '.')
* @param array A referenced array of currently found classes
* @param boolean If true, only event plugins will be searched. If false, only sidebar plugins will be searched.
* @param string The maindir where we started searching from [for recursive use]
* @return
*/
static function traverse_plugin_dir($ppath, &$classes, $event_only, $maindir = '')
{
$d = @opendir($ppath);
if ($d) {
while (($f = readdir($d)) !== false) {
if ($f[0] == '.' || $f == 'CVS' || !is_dir($ppath . '/' . $f) || !is_readable($ppath . '/' .$f)) {
continue;
}
$subd = opendir($ppath . '/' . $f);
if (!$subd) {
continue;
}
// Instead of only looking for directories, search for files within subdirectories
$final_loop = false;
while (($subf = readdir($subd)) !== false) {
if ($subf[0] == '.' || $subf == 'CVS') {
continue;
}
if (!$final_loop && is_dir($ppath . '/' . $f . '/' . $subf) && $maindir != $ppath . '/' . $f) {
// Search for another level of subdirectories
serendipity_plugin_api::traverse_plugin_dir($ppath . '/' . $f, $classes, $event_only, $f . '/');
// We can break after that operation because the current directory has been fully checked already.
$final_loop = true;
}
if (!preg_match('@^[^_]+_(event|plugin)_.+\.php$@i', $subf)) {
continue;
}
$class_name = str_replace('.php', '', $subf);
// If an external plugin/event already exists as internal, remove the internal reference because its redundant
if (isset($classes['@' . $class_name])) {
unset($classes['@' . $class_name]);
}
// A local plugin will be preferred over general plugins [used when calling this function the second time]
if (isset($classes[$class_name])) {
unset($classes[$class_name]);
}
if (!is_null($event_only) && $event_only && !serendipity_plugin_api::is_event_plugin($subf)) {
continue;
}
if (!is_null($event_only) && !$event_only && serendipity_plugin_api::is_event_plugin($subf)) {
continue;
}
$classes[$class_name] = array('name' => $class_name,
'true_name' => $class_name,
'type' => 'additional_plugin',
'pluginPath' => $maindir . $f);
}
closedir($subd);
}
closedir($d);
}
}
/**
* Returns a list of currently installed plugins
*
* @access public
* @param string The filter for plugins (left|right|hide|event|eventh)
* @return array The list of plugins
*/
static function get_installed_plugins($filter = '*')
{
$plugins = serendipity_plugin_api::enum_plugins($filter);
$res = array();
foreach ( (array)$plugins AS $plugin ) {
list($class_name) = explode(':', $plugin['name']);
$class_name = ltrim($class_name, '@');
$res[] = $class_name;
}
return $res;
}
/**
* Searches for installed plugins based on specific conditions
*
* @access public
* @param string The filter for plugins (left|right|hide|event|eventh)
* @param boolean If true, the filtering logic will be reversed and all plugins that are NOT part of the filter will be returned
* @param string Filter by a specific classname (like 'serendipity_plugin_archives'). Can take SQL wildcards.
* @param string Filter by a specific plugin instance id
* @return array Returns the associative array of found plugins in the database
*/
static function enum_plugins($filter = '*', $negate = false, $classname = null, $id = null)
{
global $serendipity;
$sql = "SELECT * from {$serendipity['dbPrefix']}plugins ";
$where = array();
if ($filter !== '*') {
if ($negate) {
$where[] = " placement != '" . serendipity_db_escape_string($filter) . "' ";
} else {
$where[] = " placement = '" . serendipity_db_escape_string($filter) . "' ";
}
}
if (!empty($classname)) {
$where[] = " (name LIKE '@" . serendipity_db_escape_string($classname) . "%' OR name LIKE '" . serendipity_db_escape_string($classname) . "%') ";
}
if (!empty($id)) {
$where[] = " name = '" . serendipity_db_escape_string($id) . "' ";
}
if (count($where) > 0) {
$sql .= ' WHERE ' . implode(' AND ', $where);
}
$sql .= ' ORDER BY placement, sort_order';
return serendipity_db_query($sql);
}
/**
* Count the number of plugins to which the filter criteria matches
*
* @access public
* @param string The filter for plugins (left|right|hide|event|eventh)
* @param boolean If true, the filtering logic will be reversed and all plugins that are NOT part of the filter will be evaluated
* @return int Number of plugins that were found.
*/
static function count_plugins($filter = '*', $negate = false)
{
global $serendipity;
// Can be shortcircuited via a $serendipity['prevent_sidebar_plugins_(left|right|event)'] variable!
if (!$negate && ($serendipity['prevent_sidebar_plugins_' . $filter] ?? false) == true) {
return 0;
}
$sql = "SELECT COUNT(placement) AS count from {$serendipity['dbPrefix']}plugins ";
if ($filter !== '*') {
if ($negate) {
$sql .= "WHERE placement != '$filter' ";
} else {
$sql .= "WHERE placement='$filter' ";
}
}
$count = serendipity_db_query($sql, true);
if (is_array($count) && isset($count[0])) {
return (int) $count[0];
}
return 0;
}
/**
* Detect the filename to use for a specific plugin
*
* @access public
* @param string The name of the plugin ('serendipity_event_archive')
* @param string The path to the plugin file (if empty, the current path structure will be used.)
* @param string If an instance ID is passed this means, the plugin to be loaded is internally available
* @return string Returns the filename to include for a specific plugin
*/
static function includePlugin($name, $pluginPath = '', $instance_id = '')
{
global $serendipity;
if (empty($pluginPath)) {
$pluginPath = $name;
}
$file = false;
// Security constraint
$pluginFile = 'plugins/' . $pluginPath . '/' . $name . '.php';
$pluginFile = preg_replace('@([\r\n\t\0\\\]+|\.\.+)@', '', $pluginFile);
// First try the local path, and then (if existing) a shared library repository ...
// Internal plugins ignored.
if (file_exists($serendipity['serendipityPath'] . $pluginFile)) {
$file = $serendipity['serendipityPath'] . $pluginFile;
} elseif (file_exists(S9Y_INCLUDE_PATH . $pluginFile)) {
$file = S9Y_INCLUDE_PATH . $pluginFile;
}
return $file;
}
/**
* Returns the plugin class name by a plugin instance ID
*
* @access public
* @param string The ID of a plugin
* @param boolean If true, the plugin is a internal plugin (prefixed with '@'). (Unused, keep for compat.)
* @return string The classname of the plugin
*/
static function getClassByInstanceID($instance_id, &$is_internal)
{
$instance = explode(':', $instance_id);
$class_name = ltrim($instance[0], '@');
return $class_name;
}
/**
* Auto-detect a plugin and see if the file information is given, and if not, detect it.
*
* @access public
* @param string The ID of a plugin to load
* @param string A reference variable that will hold the class name of the plugin (do not pass manually)
* @param string A reference variable that will hold the path to the plugin (do not pass manually)
* @return string Returns the filename of a plugin to load
*/
/* Probes for the plugin filename */
static function probePlugin($instance_id, &$class_name, &$pluginPath)
{
global $serendipity;
$filename = false;
$is_internal = false;
$class_name = serendipity_plugin_api::getClassByInstanceID($instance_id, $is_internal);
if (!$is_internal) {
/* plugin from the plugins/ dir */
// $serendipity['debug']['pluginload'][] = "Including plugin $class_name, $pluginPath";
$filename = serendipity_plugin_api::includePlugin($class_name, $pluginPath, $instance_id);
if (empty($filename) && !empty($instance_id)) {
// $serendipity['debug']['pluginload'][] = "No valid path/filename found.";
$sql = "SELECT path from {$serendipity['dbPrefix']}plugins WHERE name = '" . serendipity_db_escape_string($instance_id) . "'";
$plugdata = serendipity_db_query($sql, true, 'both', false, false, false, true);
if (is_array($plugdata) && isset($plugdata[0])) {
$pluginPath = $plugdata[0];
}
if (empty($pluginPath)) {
$pluginPath = $class_name;
}
// $serendipity['debug']['pluginload'][] = "Including plugin(2) $class_name, $pluginPath";
$filename = serendipity_plugin_api::includePlugin($class_name, $pluginPath);
}
if (empty($filename)) {
$serendipity['debug']['pluginload'][] = "No valid path/filename found. Aborting.";
$retval = false;
return $retval;
}
}
// $serendipity['debug']['pluginload'][] = "Found plugin file $filename";
return $filename;
}
/**
* Instantiates a plugin class
*
* @access public
* @param string The ID of the plugin to load
* @param int The owner of the plugin (can be autodetected)
* @param string The path to a plugin (can be autodetected)
* @param string The filename of a plugin (can be autodetected)
* @return
*/
static function &load_plugin($instance_id, $authorid = null, $pluginPath = '', $pluginFile = null)
{
global $serendipity;
if ($pluginFile === null) {
$class_name = '';
// $serendipity['debug']['pluginload'][] = "Init probe for plugin $instance_id, $class_name, $pluginPath";
$pluginFile = serendipity_plugin_api::probePlugin($instance_id, $class_name, $pluginPath);
} else {
$is_internal = false;
// $serendipity['debug']['pluginload'][] = "getClassByInstanceID $instance_id, $is_internal";
$class_name = serendipity_plugin_api::getClassByInstanceID($instance_id, $is_internal);
}
if (!class_exists($class_name) && !empty($pluginFile)) {
// $serendipity['debug']['pluginload'][] = "Classname does not exist. Including $pluginFile.";
include($pluginFile);
}
if (!class_exists($class_name)) {
$serendipity['debug']['pluginload'][] = "Classname $class_name still does not exist. Aborting.";
return false;
}
// $serendipity['debug']['pluginload'][] = "Returning new $class_name($instance_id)";
$p = new $class_name($instance_id);
if (!is_null($authorid)) {
$p->serendipity_owner = $authorid;
} else {
$sql = "SELECT authorid from {$serendipity['dbPrefix']}plugins WHERE name = '" . serendipity_db_escape_string($instance_id) . "'";
$owner = serendipity_db_query($sql, true);
if (is_array($owner) && isset($owner[0])) {
$p->serendipity_owner = $owner[0];
}
}
$p->pluginPath = $p->act_pluginPath = $pluginPath;
if (empty($p->act_pluginPath)) {
$p->act_pluginPath = $class_name;
}
$p->pluginFile = $pluginFile;
return $p;
}
/**
* Gets cached properties/information about a specific plugin, auto-loads a cache of all plugins
*
* @access public
* @param string The filename of the plugin to get information about
* @param array A referenced array that holds information about the plugin instance (self::load_plugin() response)
* @param type The type of the plugin (sidebar|event)
* @return array Information about the plugin
*/
static function &getPluginInfo(&$pluginFile, &$class_data, $type)
{
global $serendipity;
static $pluginlist = null;
if ($pluginlist === null) {
$data = serendipity_db_query("SELECT p.*,
pc.category
FROM {$serendipity['dbPrefix']}pluginlist AS p
LEFT OUTER JOIN {$serendipity['dbPrefix']}plugincategories AS pc
ON pc.class_name = p.class_name
WHERE p.pluginlocation = 'local' AND
p.plugintype = '" . serendipity_db_escape_string($type) . "'");
if (is_array($data)) {
foreach($data AS $p) {
if (isset($p['pluginpath'])) {
$p['pluginPath'] = $p['pluginpath'];
}
if (!isset($pluginlist[$p['plugin_file']])) {
$pluginlist[$p['plugin_file']] = $p;
}
$pluginlist[$p['plugin_file']]['groups'][] = $p['category'];
}
}
}
if (is_array($pluginlist[$pluginFile] ?? null) && !preg_match('@plugin_internal\.inc\.php@', $pluginFile)) {
$data = $pluginlist[$pluginFile];
if ((int) filemtime($pluginFile) == (int) $data['last_modified']) {
$data['stackable'] = serendipity_db_bool($data['stackable']);
$plugin = $data;
return $plugin;
}
}
$plugin =& serendipity_plugin_api::load_plugin($class_data['name'], null, $class_data['pluginPath'], $pluginFile);
return $plugin;
}
/**
* Set cache information about a plugin
*
* @access public
* @param mixed Either an plugin object or a plugin information array that holds the information about the plugin
* @param string The filename of the plugin
* @param object The property bag object bundled with the plugin
* @param array Previous/additional information about the plugin
* @param string The location/type of a plugin (local|spartacus)
* @return
*/
static function &setPluginInfo(&$plugin, &$pluginFile, &$bag, &$class_data, $pluginlocation = 'local')
{
global $serendipity;
static $dbfields = array(
'plugin_file',
'class_name',
'plugin_class',
'pluginPath',
'name',
'description',
'version',
'upgrade_version',
'plugintype',
'pluginlocation',
'stackable',
'author',
'requirements',
'website',
'last_modified'
);
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}pluginlist WHERE plugin_file = '" . serendipity_db_escape_string($pluginFile) . "' AND pluginlocation = '" . serendipity_db_escape_string($pluginlocation) . "'");
if (!empty($pluginFile) && file_exists($pluginFile)) {
$lastModified = filemtime($pluginFile);
} else {
$lastModified = 0;
}
if (is_object($plugin)) {
$data = array(
'class_name' => get_class($plugin),
'stackable' => $bag->get('stackable'),
'name' => $bag->get('name'),
'description' => $bag->get('description'),
'author' => $bag->get('author'),
'version' => $bag->get('version'),
'upgrade_version' => isset($class_data['upgrade_version']) ? $class_data['upgrade_version'] : $bag->get('version'),
'requirements' => serialize($bag->get('requirements')),
'website' => $bag->get('website'),
'plugin_class' => $class_data['name'],
'pluginPath' => $class_data['pluginPath'],
'plugin_file' => $pluginFile,
'pluginlocation' => $pluginlocation,
'plugintype' => $serendipity['GET']['type'],
'last_modified' => $lastModified
);
$groups = $bag->get('groups');
} elseif (is_array($plugin)) {
$data = $plugin;
$groups = $data['groups'];
unset($data['installable']);
unset($data['true_name']);
unset($data['customURI']);
unset($data['groups']);
if (isset($data['pluginpath'])) {
$data['pluginPath'] = $data['pluginpath'];
}
$data['requirements'] = serialize($data['requirements']);
}
if (!isset($data['stackable']) || empty($data['stackable'])) {
$data['stackable'] = '0';
}
if (!isset($data['last_modified'])) {
$data['last_modified'] = $lastModified;
}
// Only insert data keys that exist in the DB.
$insertdata = array();
foreach($dbfields AS $field) {
$insertdata[$field] = $data[$field] ?? null;
}
if ($data['upgradable'] ?? false) {
serendipity_db_query("UPDATE {$serendipity['dbPrefix']}pluginlist
SET upgrade_version = '" . serendipity_db_escape_string($data['upgrade_version']) . "'
WHERE plugin_class = '" . serendipity_db_escape_string($data['plugin_class']) . "'");
}
serendipity_db_insert('pluginlist', $insertdata);
serendipity_db_query("DELETE FROM {$serendipity['dbPrefix']}plugincategories WHERE class_name = '" . serendipity_db_escape_string($data['class_name']) . "'");
foreach((array)$groups AS $group) {
if (empty($group)) {
continue;
}
$cat = array(
'class_name' => $data['class_name'],
'category' => $group
);
serendipity_db_insert('plugincategories', $cat);
}
$data['groups'] = $groups;
return $data;
}
/**
* Moves a sidebar plugin to a different side or up/down
*
* @access public
* @param string The instance ID of a plugin
* @param string The new placement of a plugin (left|right|hide|event|eventh)
* @param string A new sort order for the plugin
* @return
*/
static function update_plugin_placement($name, $placement, $order = null)
{
global $serendipity;
$admin = '';
if (!serendipity_checkPermission('adminPlugins') && $placement == 'hide') {
// Only administrators can set plugins to 'hide' if they are not the owners.
$admin = " AND (authorid = 0 OR authorid = {$serendipity['authorid']})";
}
$sql = "UPDATE {$serendipity['dbPrefix']}plugins set placement='$placement' ";
if ($order !== null) {
$sql .= ", sort_order=$order ";
}
$sql .= "WHERE name='$name' $admin";
return serendipity_db_query($sql);
}
/**
* Updates the ownership information about a plugin
*
* @access public
* @param string The instance ID of the plugin
* @param int The ID of the new author owner of the plugin
* @return
*/
static function update_plugin_owner($name, $authorid)
{
global $serendipity;
if (empty($authorid) && $authorid != '0') {
return;
}
$admin = '';
if (!serendipity_checkPermission('adminPlugins')) {
$admin = " AND (authorid = 0 OR authorid = {$serendipity['authorid']})";
}
$sql = "UPDATE {$serendipity['dbPrefix']}plugins SET authorid='$authorid' WHERE name='$name' $admin";
return serendipity_db_query($sql);
}
/**
* Get a list of Sidebar plugins and pass them to Smarty
*
* @access public
* @param string The side of plugins to show (left/right/hide/event/eventh)
* @param string deprecated: Indicated which wrapping HTML element to use for plugins
* @param boolean Indicates whether only all plugins should be shown that are not in the $side list
* @param string Only show plugins of this plugin class
* @param string Only show a plugin with this instance ID
* @return string Smarty HTML output
*/
static function generate_plugins($side, $negate = false, $class = null, $id = null, $tpl = 'sidebar.tpl')
{
global $serendipity;
$plugins = serendipity_plugin_api::enum_plugins($side, $negate, $class, $id);
if (!is_array($plugins)) {
return;
}
if (!isset($serendipity['smarty'])) {
$serendipity['smarty_raw_mode'] = true;
serendipity_smarty_init();
}
$pluginData = array();
$addData = func_get_args();
serendipity_plugin_api::hook_event('frontend_generate_plugins', $plugins, $addData);
if (count($plugins) == 0) {
$serendipity['prevent_sidebar_plugins_' . $side] = true;
}
$loggedin = false;
if (serendipity_userLoggedIn() && serendipity_checkPermission('adminPlugins')) {
$loggedin = true;
}
foreach ($plugins AS $plugin_data) {
$plugin =& serendipity_plugin_api::load_plugin($plugin_data['name'], $plugin_data['authorid'], $plugin_data['path']);
if (is_object($plugin)) {
$class = get_class($plugin);
$title = '';
/* TODO: make generate_content NOT echo its output */
ob_start();
$show_plugin = $plugin->generate_content($title);
$content = ob_get_contents();
ob_end_clean();
if ($loggedin) {
$content .= '<div class="serendipity_edit_nugget"><a href="' . $serendipity['serendipityHTTPPath'] . 'serendipity_admin.php?serendipity[adminModule]=plugins&serendipity[plugin_to_conf]=' . serendipity_entities($plugin->instance) . '">' . EDIT . '</a></div>';
}
if ($show_plugin !== false) {
$pluginData[] = array('side' => $side,
'class' => $class,
'title' => $title,
'content' => $content,
'id' => $plugin->instance);
}
} else {
$pluginData[] = array('side' => $side,
'title' => ERROR,
'class' => $class,
'content' => sprintf(INCLUDE_ERROR, $plugin_data['name']));
}
}
serendipity_plugin_api::hook_event('frontend_sidebar_plugins', $pluginData, $addData);
$serendipity['smarty']->assignByRef('plugindata', $pluginData);
$serendipity['smarty']->assign('pluginside', ucfirst($side));
return serendipity_smarty_fetch('sidebar_'. $side, $tpl, true);
}