/
extensions.php
1125 lines (1011 loc) · 30.4 KB
/
extensions.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
/**
* Plugin and Module Management API
*
* @package API - Extensions
* @copyright (c) Cotonti Team
* @license https://github.com/Cotonti/Cotonti/blob/master/License.txt
*/
defined('COT_CODE') or die('Wrong URL');
// Requirements
require_once cot_incfile('auth');
require_once cot_incfile('configuration');
require_once cot_langfile('admin', 'core');
/**
* A value returned by cot_extension_install() when updating and
* there is nothing to update
*/
define('COT_EXT_NOTHING_TO_UPDATE', 2);
/**
* Default plugin part execution priority
*/
define('COT_PLUGIN_DEFAULT_ORDER', 10);
/**
* Extension types
*/
define('COT_EXT_TYPE_MODULE', 'module');
define('COT_EXT_TYPE_PLUGIN', 'plug');
/**
* These parts ($name.$part.php) are reserved handlers with no hooks
*/
$cot_ext_ignore_parts = array('configure', 'install', 'setup', 'uninstall');
/**
* Applies custom SQL and PHP patches in a directory. Error and success
* messages are emitted via standard messaging API during execution.
*
* @param string $directory Directory path
* @param string $from_ver Current version, to patch starting from
* @param string $sql_pattern SQL patch file name pattern (PCRE)
* @param string $php_pattern PHP patch file name pattern (PCRE)
* @return string|bool The function returns TRUE if there are not patches to apply,
* FALSE if an error occurred while patching or a string containing version
* number of the latest applied patch if patching was successful.
*/
function cot_apply_patches(
$directory,
$from_ver,
$sql_pattern = 'patch_([\w\.\-\_]+)\.(sql)',
$php_pattern = 'patch_([\w\.\-\_]+)\.(inc)'
)
{
global $L, $Ls, $cfg, $R, $db;
// Find new patches
$dp = opendir($directory);
$delta = [];
while ($f = readdir($dp)) {
if (
preg_match('#^' . $sql_pattern . '$#', $f, $mt)
|| preg_match('#^' . $php_pattern . '$#', $f, $mt)
) {
$type = $mt[2] == 'sql' ? 'sql' : 'php';
$ver = $mt[1];
if (version_compare($ver, $from_ver) > 0 && !isset($delta[$ver][$type])) {
$delta[$ver][$type] = $directory . '/' . $f;
}
}
}
closedir($dp);
if (count($delta) == 0) {
return true;
}
// Apply patches in version order
uksort($delta, 'version_compare');
$max_ver = $from_ver;
static $executed = [];
foreach ($delta as $key => $val) {
if (isset($val['sql']) && !in_array($val['sql'], $executed)) {
$error = Cot::$db->runScript(file_get_contents($val['sql']));
$executed[] = $val['sql'];
if (empty($error)) {
cot_message(cot_rc('ext_patch_applied',
array('f' => $val['sql'], 'msg' => 'OK')));
} else {
cot_error(cot_rc('ext_patch_error', array('f' => $val['sql'], 'msg' => $error)));
return false;
}
}
if (isset($val['php']) && !in_array($val['php'], $executed)) {
$ret = include $val['php'];
$executed[] = $val['php'];
if ($ret !== false) {
$msg = $ret == 1 ? 'OK' : $ret;
cot_message(cot_rc('ext_patch_applied', array('f' => $val['php'], 'msg' => $msg)));
} else {
cot_error(cot_rc('ext_patch_error', array('f' => $val['php'], 'msg' => $L['Error'])));
return false;
}
}
$max_ver = $key;
}
return $max_ver;
}
/**
* Checks if all dependencies for selected extension are satisfied. It means
* that either all required modules and plugins are already installed or
* selected for installation.
*
* Unsatisfied requirements messages are emitted with error & messaging API.
*
* @param string $name Extension code
* @param bool $is_module TRUE for modules, FALSE for plugins
* @param array $selected_modules A list of modules currently in selection
* @param array $selected_plugins A list of plugins currently in selection
* @return bool TRUE if all dependencies are satisfied, or FALSE otherwise
*/
function cot_extension_dependencies_statisfied($name, $is_module = false,
$selected_modules = array(), $selected_plugins = array())
{
global $cfg, $L;
$path = $is_module ? $cfg['modules_dir'] . "/$name" : $cfg['plugins_dir'] . "/$name";
$ret = true;
// Get the dependency list
$info = cot_infoget("$path/$name.setup.php", 'COT_EXT');
$required_modules = empty($info['Requires_modules']) ? array()
: explode(',', $info['Requires_modules']);
$required_modules = array_map('trim', $required_modules);
$required_plugins = empty($info['Requires_plugins']) ? array()
: explode(',', $info['Requires_plugins']);
$required_plugins = array_map('trim', $required_plugins);
// Check each dependency
foreach ($required_modules as $req_ext)
{
if (!empty($req_ext) && !in_array($req_ext, $selected_modules)
&& !cot_extension_installed($req_ext))
{
cot_error(cot_rc('ext_dependency_error', array(
'name' => $name,
'type' => $is_module ? $L['Module'] : $L['Plugin'],
'dep_type' => $L['Module'],
'dep_name' => $req_ext
)));
$ret = false;
}
}
foreach ($required_plugins as $req_ext)
{
if (!empty($req_ext) && !in_array($req_ext, $selected_plugins)
&& !cot_extension_installed($req_ext))
{
cot_error(cot_rc('ext_dependency_error', array(
'name' => $name,
'type' => $is_module ? $L['Module'] : $L['Plugin'],
'dep_type' => $L['Plugin'],
'dep_name' => $req_ext
)));
$ret = false;
}
}
return $ret;
}
/**
* Installs or updates a Cotonti extension: module or plugin.
* Messages emitted during installation can be received through standard
* Cotonti messages interface.
* @param string $code Module (or plugin) code
* @param bool $isModule TRUE for modules, FALSE for plugins
* @param bool $update Perform update rather than new install
* @param bool $force_update Forces extension update even if version has not changed
* @return bool Operation status
* @global Cache $cache
*/
function cot_extension_install($code, $isModule = false, $update = false, $force_update = false)
{
global $cfg, $L, $R, $cache, $usr, $db_auth, $db_config, $db_users,
$db_core, $cot_groups, $cot_ext_ignore_parts, $db, $db_x, $env;
/** @deprecated in 0.9.24 for backward compatibility */
$is_module = $isModule;
/** @deprecated in 0.9.24 for backward compatibility */
$name = $code;
$path = $isModule ? Cot::$cfg['modules_dir'] . "/$code" : Cot::$cfg['plugins_dir'] . "/$code";
// Emit initial message
if ($update) {
cot_message(cot_rc('ext_updating', array(
'type' => $isModule ? Cot::$L['Module'] : Cot::$L['Plugin'],
'name' => $code
)));
} else {
cot_message(cot_rc('ext_installing', array(
'type' => $isModule ? $L['Module'] : $L['Plugin'],
'name' => $code
)));
}
// Check setup file and tags
$setup_file = $path . "/$code.setup.php";
if (!file_exists($setup_file)) {
cot_error(cot_rc('ext_setup_not_found', array('path' => $setup_file)));
return false;
}
$old_ext_format = false;
$info = cot_infoget($setup_file, 'COT_EXT');
if (!$info && cot_plugin_active('genoa')) {
// Try load old format info
$info = cot_infoget($setup_file, 'SED_EXTPLUGIN');
if ($info) {
$old_ext_format = true;
}
}
if ($info === false) {
cot_error('ext_invalid_format');
return false;
}
// Check versions
$res = Cot::$db->query('SELECT ct_version FROM ' . Cot::$db->core . ' WHERE ct_code = ?', $code);
if ($res->rowCount() == 1) {
$current_ver = $res->fetchColumn();
$res->closeCursor();
if ($update) {
if (version_compare($current_ver, $info['Version']) == 0 && !$force_update) {
// Nothing to update
cot_message(cot_rc('ext_up2date', array(
'type' => $isModule ? Cot::$L['Module'] :Cot::$L['Plugin'],
'name' => $code
)));
return COT_EXT_NOTHING_TO_UPDATE;
}
} else {
cot_clear_messages();
cot_error(cot_rc('ext_already_installed', array('name' => $code)));
return false;
}
}
$registeredParts = [];
if ($update) {
$query = $query = Cot::$db->query(
'SELECT * FROM ' . Cot::$db->plugins . ' WHERE pl_code = :code',
['code' => $code]
);
while ($row = $query->fetch()) {
if (!isset($registeredParts[$row['pl_part']]['hooks'])) {
$registeredParts[$row['pl_part']]['hooks'] = [];
}
$registeredParts[$row['pl_part']]['hooks'][$row['pl_hook']] = [
'hook' => $row['pl_hook'],
'order' => (int) $row['pl_order'],
];
unset($row['pl_hook'], $row['pl_order']);
$registeredParts[$row['pl_part']] = array_merge($registeredParts[$row['pl_part']], $row);
}
$query->closeCursor();
// Safely drop existing bindings
$bindings_cnt = cot_plugin_remove($code);
cot_message(cot_rc('ext_bindings_uninstalled', array('cnt' => $bindings_cnt)));
}
// Install hook parts and bindings
$hookBindings = [];
$dp = opendir($path);
while ($f = readdir($dp)) {
if (
preg_match("#^$code(\.([\w\.]+))?.php$#", $f, $mt)
&& (!isset($mt[2]) || !in_array($mt[2], $cot_ext_ignore_parts))
) {
$part_info = cot_infoget($path . "/$f", 'COT_EXT');
if (!$part_info && cot_plugin_active('genoa')) {
// Try to load old format info
$part_info = cot_infoget($path . "/$f", 'SED_EXTPLUGIN');
}
if ($part_info) {
if (empty($part_info['Hooks'])) {
$hooks = $isModule ? ['module'] : ['standalone'];
} else {
$hooks = explode(',', $part_info['Hooks']);
$hooks = is_array($hooks) ? array_map('trim', $hooks) : array();
}
if (empty($part_info['Order'])) {
$order = COT_PLUGIN_DEFAULT_ORDER;
} else {
$order = array_map('trim', explode(',', $part_info['Order']));
if (count($order) == 1 || count($order) < count($hooks)) {
$order = (int) $order[0];
}
}
$i = 0;
foreach ($hooks as $hook) {
$hookBinding = [
'part' => !isset($mt[2]) ? 'main' : $mt[2],
'file' => $f,
'hook' => $hook,
'order' => isset($order[$i]) ? (int) $order[$i] : $order,
];
if (isset($registeredParts[$hookBinding['part']])) {
$hookBinding['active'] = $registeredParts[$hookBinding['part']]['pl_active'];
}
$hookBindings[] = $hookBinding;
++$i;
}
}
}
}
closedir($dp);
$bindings_cnt = cot_plugin_add($hookBindings, $code, $info['Name'], $isModule);
cot_message(cot_rc('ext_bindings_installed', ['cnt' => $bindings_cnt]));
// Install config
$info_cfg = cot_infoget($setup_file, 'COT_EXT_CONFIG');
if (!$info_cfg && cot_plugin_active('genoa')) {
// Try to load old format config
$info_cfg = cot_infoget($setup_file, 'SED_EXTPLUGIN_CONFIG');
}
$options = cot_config_parse($info_cfg, $isModule);
if ($update) {
// Get differential config
if (cot_config_update($code, $options, $isModule) > 0) {
cot_message('ext_config_updated');
}
} elseif (count($options) > 0) {
if (cot_config_add($code, $options, $isModule)) {
cot_message('ext_config_installed');
} else {
cot_error('ext_config_error');
return false;
}
}
// Install structure config if present
$info_cfg = cot_infoget($setup_file, 'COT_EXT_CONFIG_STRUCTURE');
if ($info_cfg) {
$options = cot_config_parse($info_cfg, $isModule);
if ($update) {
if (cot_config_update($code, $options, $isModule, '__default') > 0) {
// Update all nested categories
$type = $isModule ? 'module' : 'plug';
$res = $db->query("SELECT DISTINCT config_subcat FROM $db_config
WHERE config_owner = '$type' AND config_cat = '$code'
AND config_subcat != '' AND config_subcat != '__default'");
$cat_list = $res->fetchAll(PDO::FETCH_COLUMN, 0);
foreach ($cat_list as $cat) {
cot_config_update($code, $options, $isModule, $cat);
}
cot_message('ext_config_struct_updated');
}
} elseif (count($options) > 0) {
if (cot_config_add($code, $options, $isModule, '__default')) {
cot_message('ext_config_struct_installed');
} else {
cot_error('ext_config_struct_error');
return false;
}
}
}
// Install / Update Auth
if (!isset($info['Auth_guests'])) {
$info['Auth_guests'] = '';
}
if (!isset($info['Lock_guests'])) {
$info['Lock_guests'] = '';
}
if (!isset($info['Auth_members'])) {
$info['Auth_members'] = '';
}
if (!isset($info['Lock_members'])) {
$info['Lock_members'] = '';
}
if ($update) {
// Only update auth locks
if ($isModule) {
$auth_code = $code;
$auth_option = 'a';
} else {
$auth_code = 'plug';
$auth_option = $code;
}
$lock_guests = cot_auth_getvalue($info['Lock_guests']);
$db->update($db_auth, array('auth_rights_lock' => $lock_guests), "
auth_code = '$auth_code' AND auth_option = '$auth_option'
AND (auth_groupid = " . COT_GROUP_GUESTS
. ' OR auth_groupid = ' . COT_GROUP_INACTIVE . ')');
$lock_members = cot_auth_getvalue($info['Lock_members']);
$ingore_groups = implode(',', array(
COT_GROUP_GUESTS,
COT_GROUP_INACTIVE,
COT_GROUP_BANNED,
COT_GROUP_SUPERADMINS
));
$db->update($db_auth, array('auth_rights_lock' => $lock_members),
"auth_code = '$auth_code' AND auth_option = '$auth_option' AND auth_groupid NOT IN ($ingore_groups)");
cot_message('ext_auth_locks_updated');
} else {
// Install auth
$insert_rows = array();
foreach ($cot_groups as $v) {
$v['skiprights'] = isset($v['skiprights']) ? $v['skiprights'] : false;
if (!$v['skiprights']) {
if ($v['id'] == COT_GROUP_GUESTS || $v['id'] == COT_GROUP_INACTIVE) {
$ins_auth = cot_auth_getvalue($info['Auth_guests']);
$ins_lock = cot_auth_getvalue($info['Lock_guests']);
if ($ins_auth > 128 || $ins_lock < 128) {
$ins_auth = ($ins_auth > 127) ? $ins_auth - 128 : $ins_auth;
$ins_lock = 128;
}
} elseif ($v['id'] == COT_GROUP_BANNED) {
$ins_auth = 0;
$ins_lock = 255;
} elseif ($v['id'] == COT_GROUP_SUPERADMINS) {
$ins_auth = 255;
$ins_lock = 255;
} else {
$ins_auth = cot_auth_getvalue($info['Auth_members']);
$ins_lock = cot_auth_getvalue($info['Lock_members']);
}
if ($isModule) {
$insert_rows[] = array(
'auth_groupid' => $v['id'],
'auth_code' => $code,
'auth_option' => 'a',
'auth_rights' => $ins_auth,
'auth_rights_lock' => $ins_lock,
'auth_setbyuserid' => $usr['id']
);
} else {
$insert_rows[] = array(
'auth_groupid' => $v['id'],
'auth_code' => 'plug',
'auth_option' => $code,
'auth_rights' => $ins_auth,
'auth_rights_lock' => $ins_lock,
'auth_setbyuserid' => $usr['id']
);
}
}
}
if ($db->insert($db_auth, $insert_rows)) {
$db->update($db_users, array('user_auth' => ''), "user_auth != ''");
cot_message('ext_auth_installed');
}
}
$new_ver = '';
if ($update) {
// Find and apply patches
if (file_exists("$path/setup")) {
$new_ver = cot_apply_patches("$path/setup", $current_ver);
}
if (version_compare($info['Version'], $new_ver) > 0 || $new_ver === true) {
$new_ver = $info['Version'];
}
} else {
if (file_exists($path . "/setup/$code.install.sql")) {
// Run SQL install script
$sql_err = $db->runScript(
file_get_contents("$path/setup/$code.install.sql"));
if (empty($sql_err)) {
cot_message(cot_rc('ext_executed_sql', array('ret' => 'OK')));
} else {
cot_error(cot_rc('ext_executed_sql', array('ret' => $sql_err)));
return false;
}
}
$install_handler = $old_ext_format ? $setup_file : $path . "/setup/$code.install.php";
if ($old_ext_format)
{
global $action;
$action = 'install';
}
if (file_exists($install_handler))
{
// Run PHP install handler
$envtmp = $env;
$env = array(
'ext' => $code,
'location' => $code,
'type' => ($isModule) ? 'module' : 'plug'
);
$ret = include $install_handler;
$env = $envtmp;
if ($ret !== false)
{
$msg = $ret == 1 ? 'OK' : $ret;
cot_message(cot_rc('ext_executed_php', array('ret' => $msg)));
}
else
{
cot_error(cot_rc('ext_executed_php',
array('ret' => $msg ? $msg : $L['Error'])));
return false;
}
}
}
// Register version information
if ($update) {
cot_extension_update($code, $new_ver, !$isModule);
cot_message(cot_rc('ext_updated', array(
'type' => $isModule ? $L['Module'] : $L['Plugin'],
'name' => $code,
'ver' => $new_ver
)));
/* === Hook === */
foreach (cot_getextplugins('extension.update.done') as $pl) {
include $pl;
}
/* ===== */
} else {
cot_extension_add($code, $info['Name'], $info['Version'], !$isModule);
/* === Hook === */
foreach (cot_getextplugins('extension.install.done') as $pl) {
include $pl;
}
/* ===== */
}
// Cleanup
cot_auth_reorder();
$cache && $cache->clear();
return true;
}
/**
* Uninstalls an extension and removes all its data
* @param string $code Extension code
* @param bool $isModule TRUE for modules, FALSE for plugins
* @global CotDB $db
* @global Cache $cache
*/
function cot_extension_uninstall($code, $isModule = false)
{
global $cfg, $db_auth, $db_config, $db_users, $db_updates, $cache, $db, $db_x, $db_plugins, $cot_plugins,
$cot_plugins_active, $cot_plugins_enabled, $cot_modules, $env, $structure, $db_structure, $L, $R;
$path = $isModule ? $cfg['modules_dir'] . "/$code" : $cfg['plugins_dir'] . "/$code";
// Emit initial message
cot_message(cot_rc('ext_uninstalling', array(
'type' => $isModule ? Cot::$L['Module'] : Cot::$L['Plugin'],
'name' => $code
)));
// Remove bindings
cot_plugin_remove($code);
// Drop auth and config
if ($isModule) {
Cot::$db->delete($db_config, "config_owner = 'module' AND config_cat = '$code'");
Cot::$db->delete($db_auth, "auth_code = '$code'");
} else {
Cot::$db->delete($db_config, "config_owner = 'plug' AND config_cat = '$code'");
Cot::$db->delete($db_auth, "auth_code = 'plug' AND auth_option = '$code'");
}
cot_message('ext_auth_uninstalled');
cot_message('ext_config_uninstalled');
// Remove extension structure
if ($isModule && isset($structure[$code])) {
$db->delete($db_structure, "structure_area = ?", $code);
unset($structure[$code]);
}
// Run SQL script if present
if (file_exists($path . "/setup/$code.uninstall.sql"))
{
$sql_err = $db->runScript(
file_get_contents("$path/setup/$code.uninstall.sql"));
if (empty($sql_err))
{
cot_message(cot_rc('ext_executed_sql', array('ret' => 'OK')));
}
else
{
cot_error(cot_rc('ext_executed_sql', array('ret' => $sql_err)));
}
}
// Run handler part
if (cot_plugin_active('genoa') && cot_infoget($path . "/$code.setup.php", 'SED_EXTPLUGIN'))
{
global $action;
$action = 'uninstall';
$uninstall_handler = $path . "/$code.setup.php";
}
else
{
$uninstall_handler = $path . "/setup/$code.uninstall.php";
}
if (file_exists($uninstall_handler)) {
$envtmp = $env;
$env = array(
'ext' => $code,
'location' => $code,
'type' => ($isModule) ? 'module' : 'plug'
);
$ret = include $uninstall_handler;
$env = $envtmp;
if ($ret !== false) {
cot_message(cot_rc('ext_executed_php', array('ret' => $ret)));
} else {
cot_error(cot_rc('ext_executed_php', array('ret' => $L['Error'])));
}
}
// Unregister from core table
cot_extension_remove($code, !$isModule);
$sql = $db->query("SELECT pl_code, pl_file, pl_hook, pl_module FROM $db_plugins
WHERE pl_active = 1 ORDER BY pl_hook ASC, pl_order ASC");
$cot_plugins = array();
if ($sql->rowCount() > 0) {
while ($row = $sql->fetch()) {
$cot_plugins[$row['pl_hook']][] = $row;
}
$sql->closeCursor();
}
$cot_plugins_active[$code] = false;
if (!$isModule) {
unset($cot_plugins_enabled[$code]);
} else {
unset($cot_modules[$code]);
}
/* === Hook === */
foreach (cot_getextplugins('extension.uninstall.done') as $pl) {
include $pl;
}
/* ===== */
// Clear cache
$db->update($db_users, array('user_auth' => ''), "user_auth != ''");
$cache && $cache->clear();
}
/**
* Parses PHPDoc file header into an array
*
* @param string $filename Path to a PHP file
* @return array Associative array containing PHPDoc contents. The array is
* empty if no PHPDoc was found
*/
function cot_file_phpdoc($filename)
{
$res = array();
$data = file_get_contents($filename);
if (preg_match('#^/\*\*(.*?)^\s\*/#ms', $data, $mt))
{
$phpdoc = preg_split('#\r?\n\s\*\s@#', $mt[1]);
$cnt = count($phpdoc);
if ($cnt > 0)
{
$res['description'] = trim(preg_replace('#\r?\n\s\*\s?#', '',
$phpdoc[0]));
for ($i = 1; $i < $cnt; $i++)
{
$delim = mb_strpos($phpdoc[$i], ' ');
$key = mb_substr($phpdoc[$i], 0, $delim);
$contents = trim(preg_replace('#\r?\n\s\*\s?#', '',
mb_substr($phpdoc[$i], $delim + 1)));
$res[$key] = $contents;
}
}
}
return $res;
}
/**
* Extract info from COT file headers
*
* @param string $file File path
* @param string $limiter Tag name
* @param int $maxsize Max header size
* @return array Array containing block data or FALSE on error
*/
function cot_infoget($file, $limiter = 'COT_EXT', $maxsize = 32768)
{
$result = array();
// Default data structure
if ($limiter == 'COT_EXT' || $limiter == 'SED_EXTPLUGIN') {
$result = array(
'Code' => '',
'Name' => '',
'Description' => '',
'Category' => '',
'Version' => '',
'Date' => '',
'Author' => '',
'Copyright' => '',
'Notes' => '',
'Auth_members' => '',
'Lock_members' => '',
'Auth_guests' => '',
'Lock_guests' => '',
);
if ($limiter == 'COT_EXT') {
$result['Requires_modules'] = '';
$result['Requires_plugins'] = '';
$result['Recommends_modules'] = '';
$result['Recommends_plugins'] = '';
}
}
$fp = @fopen($file, 'r');
if ($fp) {
$limiter_begin = '[BEGIN_' . $limiter . ']';
$limiter_end = '[END_' . $limiter . ']';
$data = fread($fp, $maxsize);
$begin = mb_strpos($data, $limiter_begin);
$end = mb_strpos($data, $limiter_end);
if ($end > $begin && $begin > 0) {
$lines = mb_substr($data, $begin + 8 + mb_strlen($limiter),
$end - $begin - mb_strlen($limiter) - 8);
$lines = explode("\n", $lines);
foreach ($lines as $line) {
$line = ltrim($line, " */");
$linex = preg_split('/\s*\=\s*/', trim($line), 2);
if ($linex[0]) {
$result[$linex[0]] = isset($linex[1]) ? $linex[1] : '';
}
}
} else {
$result = false;
}
} else {
$result = false;
}
@fclose($fp);
return $result;
}
/**
* Registers an extension in the core
*
* @param string $name Extension name (code)
* @param string $title Title name
* @param string $version Version number as A.B.C
* @param bool $is_plug Is a plugin
* @return bool TRUE on success, FALSE on error
* @global CotDB $db
*/
function cot_extension_add($name, $title, $version = '1.0.0', $is_plug = false)
{
global $db, $db_core;
$res = $db->insert($db_core, array('ct_code' => $name, 'ct_title' => $title,
'ct_version' => $version, 'ct_plug' => (int) $is_plug));
return $res > 0;
}
/**
* Compares 2 extension info entries by category code.
* post-install extensions are always last.
*
* @param array $ext1 Ext info 1
* @param array $ext2 Ext info 2
* @return int
*/
function cot_extension_catcmp($ext1, $ext2)
{
global $L;
if (isset($L['ext_cat_' . $ext1['Category']]))
{
$ext1['Category'] = $L['ext_cat_' . $ext1['Category']];
}
if (isset($L['ext_cat_' . $ext2['Category']]))
{
$ext2['Category'] = $L['ext_cat_' . $ext2['Category']];
}
if ($ext1['Category'] == $ext2['Category'])
{
// Compare by name
if ($ext1['Name'] == $ext2['Name'])
{
return 0;
}
else
{
return ($ext1['Name'] > $ext2['Name']) ? 1 : -1;
}
}
else
{
return ($ext1['Category'] > $ext2['Category'] || $ext1['Category'] == 'post-install') ? 1 : -1;
}
}
/**
* Checks if module is already installed
*
* @param string $name Module code
* @return bool
* @global CotDB $db
*/
function cot_extension_installed($name)
{
global $db_core;
$cnt = Cot::$db->query("SELECT COUNT(*) FROM $db_core WHERE ct_code = '$name'")->fetchColumn();
return $cnt > 0;
}
/**
* Returns installed extension type: 'module' if extension is a module,
* 'plug' if extension is a plugin or FALSE if extension is not installed.
*
* @param string $name Module code
* @return mixed
* @global CotDB $db
*/
function cot_extension_type($name)
{
global $db, $db_core;
$res = $db->query("SELECT ct_plug FROM $db_core WHERE ct_code = ?", $name);
if ($res->rowCount() == 0)
{
return false;
}
$is_plug = (int) $res->fetchColumn();
return $is_plug ? 'plug' : 'module';
}
/**
* Returns an array containing meta information for all extensions in a directory
*
* @param string $dir Directory to search for extensions in
* @return array Extension code => info array
*/
function cot_extension_list_info($dir)
{
$ext_list = array();
clearstatcache();
$dp = opendir($dir);
while ($f = readdir($dp))
{
$path = $dir . '/' . $f;
if ($f[0] != '.' && is_dir($path) && file_exists("$path/$f.setup.php"))
{
$info = cot_infoget("$path/$f.setup.php", 'COT_EXT');
if (!$info && cot_plugin_active('genoa')) {
// Try to load old format info
$info = cot_infoget("$path/$f.setup.php", 'SED_EXTPLUGIN');
}
if ($info == false) {
// Failed to load info block
// Lets use default data
$info = array(
'Code' => $f,
'Name' => $f,
'Description' => '',
'Category' => '',
'Version' => '',
'Date' => '',
'Author' => '',
'Copyright' => '',
'Notes' => '',
'Auth_members' => '',
'Lock_members' => '',
'Auth_guests' => '',
'Lock_guests' => '',
'Requires_modules' => '',
'Requires_plugins' => '',
'Recommends_modules' => '',
'Recommends_plugins' => ''
);
}
if (empty($info['Category'])) {
$info['Category'] = 'misc-ext';
}
$ext_list[$f] = $info;
}
}
closedir($dp);
return $ext_list;
}
/**
* Suspends (temporarily disables) a module
*
* @param string $name Module name
* @return bool
* @global CotDB $db
*/
function cot_extension_pause($name)
{
global $db, $db_core;
cot_plugin_pause($name);
return $db->update($db_core, array('ct_state' => 0), "ct_code = '$name'") == 1;
}
/**
* Unregisters a module from the core
*
* @param string $name Module name
* @return bool
* @global CotDB $db
*/
function cot_extension_remove($name)
{
global $db, $db_core;
return $db->delete($db_core, "ct_code = '$name'");
}
/**
* Resumes a paused module
*
* @param string $name Module name
* @return bool
* @global CotDB $db
*/
function cot_extension_resume($name)
{
global $db, $db_core;
cot_plugin_resume($name);
return $db->update($db_core, array('ct_state' => 1), "ct_code = '$name'") == 1;
}
/**
* Updates module version number in the registry
*
* @param string $name Module name
* @param string $version New version string
* @return bool
* @global CotDB $db
*/
function cot_extension_update($name, $version)
{
global $db, $db_core;
return $db->update($db_core, array('ct_version' => $version), "ct_code = '$name'");
}
/**
* Registers a plugin or module in hook registry
*
* Example:
* <code>
* $hook_bindings = array(
* array(