summaryrefslogtreecommitdiffstats
path: root/includes/eqdkp_plugins.php
blob: c9fd29d818afefc9e7869987997871660440905b (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
<?php
/******************************
 * EQdkp
 * Copyright 2002-2003
 * Licensed under the GNU GPL.  See COPYING for full terms.
 * ------------------
 * eqdkp_plugins.php
 * Began: Sun Mar 15 2003
 * 
 * $Id: eqdkp_plugins.php 46 2007-06-19 07:29:11Z tsigo $
 * 
 ******************************/

if ( !defined('EQDKP_INC') )
{
     die('Do not access this file directly.');
}
 
// Plugin states
define('PLUGIN_INITIALIZED', 1);
define('PLUGIN_REGISTERED',  2);
define('PLUGIN_INSTALLED',   4);
define('PLUGIN_UNINSTALLED', 8);
define('PLUGIN_ALL', PLUGIN_INITIALIZED | PLUGIN_REGISTERED | PLUGIN_INSTALLED | PLUGIN_UNINSTALLED);

// SQL types
define('SQL_INSTALL',   1);
define('SQL_UNINSTALL', 2);

/**
* EQdkp Plugin Manager class
* Framework for all plugins
* Available to every page as $pm
*/

class EQdkp_Plugin_Manager
{
    var $registered  = array();             // Registered plugins       @var registered
    var $installed   = array();             // Installed plugin         @var installed
    var $uninstalled = array();             // Uninstalled plugins      @var uninstalled
    
    var $plugins     = array();             // Plugin objects           @var plugins
    
    var $errors      = array();             // Error messages           @var errors
    var $error_die   = true;                // Die on errors?           @var error_die
    var $debug       = false;               // Debug?                   @var debug
    
    var $valid_data_types = array('id', 'code', 'name', 'installed', 'contact', 'path', 'template_path', 'version');
    
    // ---------------------------------------------------------
    // Core methods
    // ---------------------------------------------------------
    
    /**
    * Constructor
    * Populate the registered, installed and uninstalled arrays, initializing
    *   any installed plugins
    * 
    * @param    bool    $error_die Die on error messages?
    * @param    bool    $debug Output debug messages?
    */
    function eqdkp_plugin_manager($error_die = false, $debug = false)
    {
        global $db;
        
        $this->error_die = $error_die;
        $this->debug = ( $debug ) ? true : false;
        
        // Populate arrays of registered/installed/uninstalled plugins
        $sql = 'SELECT plugin_code, plugin_path, plugin_installed
                FROM ' . PLUGINS_TABLE . '
                ORDER BY plugin_name';
        $result = $db->query($sql);
        
        while ( $row = $db->fetch_record($result) )
        {
            $plugin_code = $row['plugin_code'];
            $plugin_path = $row['plugin_path'];
            
            // Add this plugin's code to our registered list
            $this->registered[$plugin_code] = $plugin_code;
            
            // Add the plugin code to either the installed or uninstalled list
            if ( $row['plugin_installed'] == 1 )
            {
                $this->installed[$plugin_code] = $plugin_code;
                
                // Attempt to initialize the installed plugin
                if ( !$this->initialize($plugin_code, $plugin_path) )
                {
                    $this->error_append('Plugin Manager Instantiation', $plugin_code .' is installed but could not be initialized.', true);
                }
            }
            else
            {
                $this->uninstalled[$plugin_code] = $plugin_code;
            }
        }
        $db->free_result($result);
        
        // Output any errors that may have occurred
        $this->error_check(true);
    }
    
    /**
    * Perform a check on $plugin_code
    * 
    * @param    int     $check PLUGIN_INITIALIZED | PLUGIN_REGISTERED | PLUGIN_INSTALLED | PLUGIN_UNINSTALLED
    * @param    string  $plugin_code
    * @return bool
    */
    function check($check, $plugin_code)
    {
        switch ( $check )
        {
            case PLUGIN_INITIALIZED:
                $retval = ( is_null($plugin_code) ) ? false : isset($this->plugins[$plugin_code]);
                break;
                
            case PLUGIN_REGISTERED:
                $retval = ( is_null($plugin_code) ) ? false : isset($this->registered[$plugin_code]);
                break;
                
            case PLUGIN_INSTALLED:
                $retval = ( is_null($plugin_code) ) ? false : isset($this->installed[$plugin_code]);
                break;
                
            case PLUGIN_UNINSTALLED:
                $retval = ( is_null($plugin_code) ) ? false : isset($this->uninstalled[$plugin_code]);
                break;
                
            default:
                $retval = false;
        }

        return $retval;
    }
    
    // ---------------------------------------------------------
    // Initialize / Register / Install / Uninstall methods
    // ---------------------------------------------------------
    
    /**
    * Attempt to initialize a plugin
    * 
    * @param    string  $plugin_code Plugin to initialize
    * @param    string  $plugin_path Optional, directory where the plugin resides
    * @return   bool
    */
    function initialize($plugin_code, $plugin_path = '')
    {
        global $db, $eqdkp_root_path;
        
        // Prevent re-initializing the same plugin
        if ( $this->check(PLUGIN_INITIALIZED, $plugin_code) )
        {
            return false;
        }
        
        $plugin_path = ( !empty($plugin_path) ) ? $plugin_path : $plugin_code;
        $plugin_dir  = $eqdkp_root_path . 'plugins/' . $plugin_path . '/';
        
        if ( !is_dir($plugin_dir) )
        {
            // Couldn't find this plugin's directory
            //$this->error_append('Initialization', 'Directory "' . $plugin_dir . '" does not exist.', true);
            
            // Directory doesn't exist, we'll never get this plugin inititalized
            // Remove its entry from the database
            $sql = 'DELETE FROM ' . PLUGINS_TABLE . "
                    WHERE plugin_code = '" . $plugin_code . "'";
            $db->query($sql);
        }
        else
        {
            // Search for a file named <code>_plugin_class.php containing a class named <code>_Plugin_Class
            $plugin_class      = $plugin_code . '_Plugin_Class';
            $plugin_class_file = $plugin_dir . strtolower($plugin_class) . '.php';
            
            if ( !is_file($plugin_class_file) )
            {
                // Plugin's class file doesn't exist
                //$this->error_append('Initialization', 'File "' . $plugin_class_file . '" does not exist.', true);
                
                // Plugin class file doesn't exist, remove its entry from the database
                $sql = 'DELETE FROM ' . PLUGINS_TABLE . "
                        WHERE plugin_code = '" . $plugin_code . "'";
                $db->query($sql);
            }
            else
            {
                include_once($plugin_class_file);
                
                if ( !class_exists($plugin_class) )
                {
                    // Plugin's class definition does not exist
                    $this->error_append('Initialization', 'Class "' . $plugin_class . '" does not exist.', true);
                }
                else
                {
                    $plugin_object = new $plugin_class($this);
                    
                    if ( !is_object($plugin_object) )
                    {
                        // Plugin's class failed to instantiate
                        $this->error_append('Initialization', 'Class "' . $plugin_class . '" failed to instantiate.', true);
                    }
                    else
                    {
                        $this->plugins[$plugin_code] = $plugin_object;
                    }
                } // class_exists
            } // is_file
        } // is_dir
        unset($plugin_object, $plugin_class, $plugin_class_file, $plugin_path, $plugin_dir);
        
        if ( !$this->check(PLUGIN_INITIALIZED, $plugin_code) )
        {
            // Initialization failed
            $this->error_append('Initialization', 'Plugin "' . $plugin_code . '" could not be initialized.', true);
        }
        $this->error_check(true);
        
        // If we get to here, there were no errors (since error_check didn't kill our output)
        return true;
    }
    
    /**
    * Register a specific plugin, or all available plugins
    * 
    * @param $s_plugin_code
    * @return bool
    */
    function register($s_plugin_code = '')
    {
        global $eqdkp_root_path;
        
        // s_plugin_code - as in static, this plugin_code doesn't change
        // d_plugin_code - as in dynamic, this will change with each iteration of readdir()
        
        // Prevent re-registering the same plugin
        if ( $this->check(PLUGIN_REGISTERED, $s_plugin_code) )
        {
            return true;
        }
        
        // Search for plugins and make sure they are registered
        if ( $dir = @opendir($eqdkp_root_path . 'plugins/') )
        {
            while ( $d_plugin_code = @readdir($dir) )
            {
                $cwd = $eqdkp_root_path . 'plugins/' . $d_plugin_code;
                if ( 	(!is_file($cwd)) && 
		 	(!is_link($cwd)) && 
			($d_plugin_code != '.') && 
			($d_plugin_code != '..') && 
			($d_plugin_code != 'CVS') && 
			($d_plugin_code != '.svn') &&
			(substr($d_plugin_code, 0, 1) != '_') )
                {
                    // If $d_plugin_code is in our array of registered codes,
                    // continue with the next iteration of the while loop
                    if ( in_array($d_plugin_code, $this->registered) )
                    {
                        continue;
                    }
                    else
                    {
                        // If $s_plugin_code is defined, only register that plugin
                        // Otherwise, register all unregistered plugins
                        if ( (!empty($s_plugin_code)) && ($s_plugin_code != $d_plugin_code) )
                        {
                            // Break out of the loop if $s_plugin_code is defined
                            // and this directory doesn't belong to $s_plugin_code
                            continue;
                        }
                        else
                        {
                            if ( !$this->initialize($d_plugin_code) )
                            {
                                // Couldn't initialize
                                $this->error_append('Registeration', 'Plugin "' . $d_plugin_code . '" could not be initialized.', true);
                            }
                            else
                            {
                                // Get the plugin's object
                                $plugin_object = $this->get_plugin($d_plugin_code);
                                
                                if ( !is_object($plugin_object) )
                                {
                                    // Object couldn't be instantiated
                                    $this->error_append('Registration', 'Plugin class for "' . $d_plugin_code . '" could not be initialized.', true);
                                }
                                else
                                {
                                    // Make the plugin register itself
                                    $plugin_object->register($d_plugin_code);
                                    
                                    // Update the plugin manager arrays
                                    $this->registered[$d_plugin_code] = $d_plugin_code;
                                    $this->uninstalled[$d_plugin_code] = $d_plugin_code;
                                }
                            }
                        }
                    }
                } // is directory
                unset($plugin_object, $d_plugin_code, $cwd);
            } // readdir
        } // opendir
	else {
	print "fopen didn't work.<br>";
	}

        unset($dir);
        
        // If a specific plugin was given, check to make sure it was registered
        if ( !empty($s_plugin_code) )
        {
            if ( !$this->check(PLUGIN_REGISTERED, $plugin_code) )
            {
                // Initialization failed
                $this->error_append('Registration', 'Plugin "' . $plugin_code . '" could not be registered.', true);
            }
        }
        $this->error_check(true);
        
        return true;
    }
    
    /**
    * Install a plugin
    * 
    * @param $plugin_code
    * @return bool
    */
    function install($plugin_code)
    {
        // Prevent re-installing this plugin
        if ( $this->check(PLUGIN_INSTALLED, $plugin_code) )
        {
            return true;
        }
        
        // Initialize the plugin - initialize() prevents this if it needs to
        $this->initialize($plugin_code);
        
        // Get the plugin object
        $plugin_object = $this->get_plugin($plugin_code);
        if ( !is_object($plugin_object) )
        {
            $this->error_append('Instantiation', 'Plugin "' . $plugin_code . '" failed to create a valid object.', true);
        }
        else
        {
            // Run the install
            if ( !$plugin_object->run_sql(SQL_INSTALL) )
            {
                $this->error_append('Installation', 'Plugin "' . $plugin_code . '" installation SQL failed.', true);
            }
            else
            {
                // Remove this item from our uninstalled array
                unset($this->uninstalled[$plugin_code]);
                
                // Add it to our installed array
                $this->installed[$plugin_code] = $plugin_code;
            }
        }
        
        if ( !$this->check(PLUGIN_INSTALLED, $plugin_code) )
        {
            $this->error_append('Installation', 'Plugin "' . $plugin_code . '" failed to install.', true);
        }
        
        $this->error_check(true);
        
        return true;
    }
    
    /**
    * Uninstall a plugin
    * 
    * @param $plugin_code
    * @return bool
    */
    function uninstall($plugin_code)
    {
        if ( $this->check(PLUGIN_UNINSTALLED, $plugin_code) )
        {
            return true;
        }
        
        $this->initialize($plugin_code);
        
        // Get the plugin object
        $plugin_object = $this->get_plugin($plugin_code);
        if ( !is_object($plugin_object) )
        {
            $this->error_append('Instantiation', 'Plugin "' . $plugin_code . '" failed to create a valid object.', true);
        }
        else
        {
            // Run the uninstall
            if ( !$plugin_object->run_sql(SQL_UNINSTALL) )
            {
                $this->error_append('Uninstallation', 'Plugin "' . $plugin_code . '" uninstallation SQL failed.', true);
            }
            else
            {
                // Remove this item from our installed array
                unset($this->installed[$plugin_code]);
                
                // Add it to our uninstalled array
                $this->uninstalled[$plugin_code] = $plugin_code;
            }
        }
        
        if ( !$this->check(PLUGIN_UNINSTALLED, $plugin_code) )
        {
            $this->error_append('Uninstallation', 'Plugin "' . $plugin_code . '" failed to uninstall.', true);
        }
        
        $this->error_check(true);
        
        return true;
    }
    
    // ---------------------------------------------------------
    // Plugin methods
    // ---------------------------------------------------------
    
    /**
    * Return $plugin_code's associated object
    * 
    * @param $plugin_code
    * @return mixed Object / false
    */
    function get_plugin($plugin_code)
    {
        return ( @is_object($this->plugins[$plugin_code]) ) ? $this->plugins[$plugin_code] : false;
    }
    
    /**
    * Get multiple plugins
    * 
    * @param $filter Type of plugins to get (can be |'d together using the PLUGIN_x constants)
    * @return array Associative array of plugin objects as code => object 
    */
    function get_plugins($filter = PLUGIN_INSTALLED)
    {
        $retval = array();
        $unset_array = array();
        
        // Get initialized plugins
        if ( $filter & PLUGIN_INITIALIZED )
        {
            $retval = array_merge($retval, $this->plugins);
        }
        
        // Get installed plugins
        if ( $filter & PLUGIN_INSTALLED )
        {
            foreach ( $this->installed as $plugin_code )
            {
                // Initialize the plugin if it's not already
                if ( !$this->check(PLUGIN_INITIALIZED, $plugin_code) )
                {
                    $this->initialize($plugin_code);
                }
                
                $retval[$plugin_code] = $this->get_plugin($plugin_code);
            }
        }
        
        // Get uninstalled plugins
        if ( $filter & PLUGIN_UNINSTALLED )
        {
            foreach ( $this->uninstalled as $plugin_code )
            {
                if ( !$this->check(PLUGIN_INITIALIZED, $plugin_code) )
                {
                    $this->initialize($plugin_code);
                    $unset_array[] = $plugin_code;
                }
                
                $retval[$plugin_code] = $this->get_plugin($plugin_code);
            }
        }
        
        // Unset any plugins that may have been initialized just for this purpose
        foreach ( $unset_array as $plugin_code )
        {
            unset($this->plugins[$plugin_code]);
        }
        
        return $retval;
    }
    
    /**
    * Get a plugin's language file
    * 
    * @param $plugin_code Either a specific plugin, or 'all'
    * @return bool
    */
    function get_language_pack($plugin_code = 'all')
    {
        global $eqdkp_root_path, $user;
        
        // Recursive if we're getting the language packs for every plugin
        if ( $plugin_code == 'all' )
        {
            foreach ( $this->installed as $plugin_code )
            {
                $this->get_language_pack($plugin_code);
            }
        }
        else
        {
            $lang_file = $eqdkp_root_path . 'plugins/' . $plugin_code . '/language/' . $user->lang_name . '/lang_main.php';
            
            if ( file_exists($lang_file) )
            {
                include_once($lang_file);
                
                $user->lang = ( @is_array($lang) ) ? array_merge($user->lang, $lang) : $user->lang;
            }
        }
       
        return true;
    }
    
    // ---------------------------------------------------------
    // Hook methods
    // ---------------------------------------------------------
    
    /**
    * Add plugins' permission box arrays to an existing permissions array
    * Modifies the array by reference
    * 
    * @param $cbox_array Array we're modifying
    * @return bool
    */
    function generate_permission_boxes(&$cbox_array)
    {
        global $user;
        
        foreach ( $this->get_plugins(PLUGIN_INSTALLED) as $plugin_code => $plugin_object )
        {
            if ( $plugin_object->is_permissions() )
            {
                $cbox_array = array_merge($cbox_array, $plugin_object->permission_boxes());
            }
        }
        
        return true;
    }
    
    /**
    * Call hooks for a specific page
    * 
    * @param $page
    * @return mixed Result of <plugin_object>::do_hook()
    */
    function do_hooks($page)
    {
        global $eqdkp_root_path;
        
        $retval = array();
        
        $request = ( isset($_SERVER['REQUEST_URI']) ) ? $_SERVER['REQUEST_URI'] : $_SERVER['SCRIPT_NAME'] . (( isset($_SERVER['QUERY_STRING']) ) ? '?' . $_SERVER['QUERY_STRING'] : '');
        
        // If we've been handed a raw URL string, shorten it appropriately
        if ( $request == $page )
        {
            $slash_count = substr_count($eqdkp_root_path, '/');
            $url = split('/', $request);
            $url = array_slice($url, -$count);
            $page = join('/', $url);
        }
        
        foreach ( $this->get_plugins() as $plugin_code => $plugin_object )
        {
            $retval[$plugin_code] = $plugin_object->do_hook($page);
        }
        
        return $retval;
    }
    
    /**
    * Return log actions
    * 
    * @return array
    */
    function get_log_actions()
    {
        $retval = array();
        
        foreach ( $this->get_plugins() as $plugin_code => $plugin_object )
        {
            foreach ( $plugin_object->get_log_actions() as $action_type => $lang_string )
            {
                $retval[$action_type] = $lang_string;
            }
        }
        
        return $retval;
    }
    
    /**
    * Generate a menu
    * 
    * @param $menu_name
    * @deprec Array format removes the need for this
    * @return string Menu
    */
    function generate_menu($menu_name)
    {
        $menu_array = array();
        
        foreach ( $this->get_plugins(PLUGIN_INSTALLED) as $plugin_code => $plugin_object )
        {
            $menu_array = array_merge($menu_array, $plugin_object->get_menu($menu_name));
        }
        
        $menu_string = '';
        
        foreach ( $menu_array as $plugin_code => $menu )
        {
            if ( ($menu != '') && ($this->check(PLUGIN_INSTALLED, $plugin_code)) )
            {
                $menu_string .= ' | ' . $menu;
            }
        }
        
        return $menu_string;
    }
    
    /**
    * Get a menu array
    * 
    * @param $menu_name
    * @return array
    */
    function get_menus($menu_name = 'admin_menu')
    {
        $menu_array = array();
        
        foreach ( $this->get_plugins(PLUGIN_INSTALLED) as $plugin_code => $plugin_object )
        {
            $plugin_array = $plugin_object->get_menu($menu_name);
            $menu_array = (is_array($plugin_array[$plugin_code])) ? array_merge($menu_array, $plugin_array[$plugin_code]) : $menu_array;
        }
        
        return $menu_array;
    }
    
    // ---------------------------------------------------------
    // Error methods
    // ---------------------------------------------------------
    
    /**
    * Add an error message to the error buffer
    * 
    * @param $location Part of the class the error occurred in
    * @param $error Error message
    * @param $force Add this message regardless of $this->debug?
    * @return bool
    */
    function error_append($location, $error, $force = false)
    {
        if ( ($this->debug) || ($force) )
        {
            $this->errors[] = '<b>' . $location . ':</b> ' . $error;
            
            return true;
        }
        
        return false;
    }
    
    /**
    * Check to see if errors exist in the queue
    * 
    * @return bool
    */
    function is_error()
    {
        return ( sizeof($this->errors) > 0 ) ? true : false;
    }
    
    /**
    * Output error messages if we need to
    * 
    * @param $die Can override $this->error_die
    * @return bool
    */
    function error_check($die = false)
    {
        if ( ($this->is_error()) && (($this->error_die) || ($die)) )
        {
            $message = '<b>The following error(s) occurred:</b><br />';
            foreach ( $this->errors as $error )
            {
                $message .= $error . '<br />';
            }
            die("$message");
        }
        else
        {
            // Wipe the error buffer
            $this->errors = array();
            
            return false;
        }
    }
    
    // ---------------------------------------------------------
    // Data methods
    // ---------------------------------------------------------
    
    /**
    * Add plugin data to $plugin_code
    * 
    * @param $plugin_code
    * @param $type Data type (may be a relational array of type => data)
    * @param $data Data value
    * @return bool
    */
    function add_data($plugin_code, $type, $data = '')
    {
        if ( $this->check(PLUGIN_INITIALIZED, $plugin_code) )
        {
            if ( is_array($type) )
            {
                foreach ( $type as $i_type => $data )
                {
                    $this->add_data($i_type, $data);
                }
            }
            else
            {
                if ( in_array($type, $this->valid_data_types) )
                {
                    $plugin_object = $this->plugins[$plugin_code];
                    
                    if ( is_object($plugin_object) )
                    {
                        $plugin_object->add_data($type, $data);
                    }
                    
                    return true;
                }
            }
        }
        $this->error_append('Add Data', 'Invalid data type ("' . $type . '").');
        
        return false;
    }
    
    /**
    * Get plugin data for $plugin_code
    * @param $plugin_code
    * @param $type Data type
    */
    function get_data($plugin_code, $type)
    {
        if ( $this->check(PLUGIN_INITIALIZED, $plugin_code) )
        {
            if ( in_array($type, $this->valid_data_types) )
            {
                $plugin_object = $this->plugins[$plugin_code];
                
                if ( is_object($plugin_object) )
                {
                    return $plugin_object->get_data($type);
                }
            }
        }
        $this->error_append('Get Data', 'Invalid data type ("' . $type . '").');
        
        return false;
    }
}

/**
* EQdkp Plugin class
* Framework for individual classes
* Is used only as an extension to a plugin's actual class
*/

class EQdkp_Plugin
{
    var $pm;                                // Plugin manager       @var pm
    
    var $data          = array();           // Plugin data          @var data
    var $permissions   = array();           // Permission data      @var permissions
    
    var $install_sql   = array();           // Install queries      @var install_sql
    var $uninstall_sql = array();           // Uninstall queries    @var uninstall_sql
    
    var $hooks         = array();           // Hook calls           @var hooks
    var $menus         = array();           // Menu definitions     @var menus
    var $log_actions   = array();           // Log actions          @var log_actions
    
    // ---------------------------------------------------------
    // Core methods
    // ---------------------------------------------------------
    
    /**
    * Constructor
    * Stores a reference to the EQdkp Plugin Manager object
    * 
    * @param $pm Plugin Manager object
    */
    function eqdkp_plugin($pm = '')
    {
        if ( !empty($pm) )
        {
            $this->pm = &$pm;
        }
    }
    
    // ---------------------------------------------------------
    // Install / Uninstall methods
    // ---------------------------------------------------------
    
    /**
    * Add an SQL query to the appropriate array
    * 
    * @param $type SQL_INSTALL | SQL_UNINSTALL
    * @param $sql Query string
    * @param $order Step
    */
    function add_sql($type, $sql, $order = '')
    {
        switch ( $type )
        {
            case SQL_INSTALL:
                $order = ( $order == '' ) ? (count($this->install_sql) + 2) : $order;
                $this->install_sql[$order] = $sql;
                break;
                
            case SQL_UNINSTALL:
                $order = ( $order == '' ) ? (count($this->uninstall_sql) + 3) : $order;
                $this->uninstall_sql[$order] = $sql;
                break;
        }
    }
    
    /**
    * Run an SQL query from the appropriate array
    * 
    * @param $type SQL_INSTALL | SQL_UNINSTALL
    * @return bool
    */
    function run_sql($type)
    {
        global $db;
        
        $permissions = $this->get_permissions();
        ksort($permissions);
        
        switch ( $type )
        {
            case SQL_INSTALL:
                // Check if we need to add permissions
                if ( count($permissions) > 0 )
                {
                    $sql = 'INSERT INTO ' . AUTH_OPTIONS_TABLE . '
                            (auth_id, auth_value, auth_default)
                            VALUES ';
                            
                    foreach ( $permissions as $auth_id => $permission )
                    {
                        $sql .= "('" . $auth_id . "','" . $permission['auth_value'] . "','" . $permission['auth_default'] . "'), ";
                    }
                    $sql = preg_replace('/, $/', '', $sql);
                    $this->add_sql(SQL_INSTALL, $sql, 0);
                }
                
                // Set the plugin as 'installed'
                $sql = 'UPDATE ' . PLUGINS_TABLE . " 
                        SET plugin_installed='1' 
                        WHERE plugin_code='" . $this->get_data('code') . "'";
                $this->add_sql(SQL_INSTALL, $sql, 1);
                
                ksort($this->install_sql);
                foreach ( $this->install_sql as $sql )
                {
                    if ( !$db->query($sql) )
                    {
                        // SQL_DB has its own error handling
                        return false;
                    }
                }
                break;
                
            case SQL_UNINSTALL:
                // Check if we need to remove permissions
                if ( count($permissions) > 0 )
                {
                    $in_clause = '';
                    foreach ( $permissions as $auth_id => $permission )
                    {
                        $in_clause .= $auth_id . ',';
                    }
                    $in_clause = preg_replace('/,$/', '', $in_clause);
                    
                    // Auth Options
                    $sql = 'DELETE FROM ' . AUTH_OPTIONS_TABLE . '
                            WHERE auth_id IN(' . $in_clause . ')';
                    $this->add_sql(SQL_UNINSTALL, $sql, 0);
                    
                    // Auth Users
                    $sql = 'DELETE FROM ' . AUTH_USERS_TABLE . '
                            WHERE auth_id IN(' . $in_clause . ')';
                    $this->add_sql(SQL_UNINSTALL, $sql, 1);
                }
                
                // Set the plugin as 'uninstalled'
                $sql = 'UPDATE ' . PLUGINS_TABLE . "
                        SET plugin_installed='0'
                        WHERE plugin_code='" . $this->get_data('code') . "'";
                $this->add_sql(SQL_UNINSTALL, $sql, 2);
                
                ksort($this->uninstall_sql);
                foreach ( $this->uninstall_sql as $sql )
                {
                    if ( !$db->query($sql) )
                    {
                        return false;
                    }
                }
                break;
                
            default:
                $this->pm->error_append('Run SQL', 'EQdkp_Plugin::run_sql() called without a valid type specified.');
        }
        
        $this->pm->error_check(true);
        
        return true;
    }
    
    /**
    * Provide feedback to the plugins.php script
    * 
    * @param $type SQL_INSTALL | SQL_UNINSTALL
    * @param $show_error Show error message(s)
    */
    function message($type, $show_error = true)
    {
        global $db, $tpl, $eqdkp, $user;
        global $gen_simple_header, $start_time, $eqdkp_root_path;
        
        //Generic install statement
        $plugin_name = $this->get_data('name');

        switch ( $type )
        {
            case SQL_INSTALL:
                $install_uninstall = 'install';
                $installed_uninstalled = 'installed';
                break;
            
            case SQL_UNINSTALL:
                $install_uninstall = 'uninstall';
                $installed_uninstalled = 'uninstalled';
                break;
            
            // Fail-safe, this shouldn't ever happen    
            default:
                $install_uninstall = 'message';
                $installed_uninstalled = 'called message()';
                break;
        }
        
        if ( ($this->pm->is_error()) && ($show_error) )
        {
            $title = 'Error';
            $text  = 'Errors were detected during the ' . $install_uninstall . ' process: ' . $this->pm->get_errors();
            $text .= $plugin_name . ' may not have ' . $installed_uninstalled . ' correctly.';
        }
        else
        {
            $title = 'Success';
            $text  = $plugin_name . ' ' . $installed_uninstalled . ' successfully.';
        }
        
        // Use regular die if $tpl is invalid or message_die is undefined
        if ( (!is_object($tpl)) || (!function_exists('message_die')) )
        {
            die($text);
        }
        else
        {
            message_die($text, $title);
        }
    }
    
    // ---------------------------------------------------------
    // Registration methods
    // ---------------------------------------------------------
    
    /**
    * Register a plugin in the database
    * 
    * @param $plugin_code
    * @return bool
    */
    function register($plugin_code)
    {
        global $db;
        
        $sql = 'SELECT plugin_installed
                FROM ' . PLUGINS_TABLE . "
                WHERE plugin_code='" . $plugin_code . "'";
        $registered = $db->query_first($sql);
        unset($sql);
        
        if ( ($registered != '0') && ($registered != '1') )
        {
            $plugin_path    = $this->get_data('path');
            $plugin_name    = $this->get_data('name');
            $plugin_contact = $this->get_data('contact');
            $plugin_version = $this->get_data('version');
                
            $query = $db->build_query('INSERT', array(
                'plugin_name'      => $plugin_name,
                'plugin_code'      => $plugin_code,
                'plugin_installed' => '0',
                'plugin_path'      => $plugin_path,
                'plugin_contact'   => $plugin_contact,
                'plugin_version'   => $plugin_version)
            );
            
            if ( !$db->query('INSERT INTO ' . PLUGINS_TABLE . $query) )
            {
                return false;
            }
            unset($query, $plugin_path, $plugin_name, $plugin_contact, $plugin_version);
        }
        
        return true;
    }
    
    // ---------------------------------------------------------
    // Permission methods
    // ---------------------------------------------------------
    
    /**
    * Add permission options for this plugin
    * 
    * @param $auth_id
    * @param $auth_value
    * @param $auth_default
    * @param $text Text describing this permission (checkbox)
    */
    function add_permission($auth_id, $auth_value, $auth_default, $text)
    {
        $this->permissions[$auth_id] = array(
            'auth_id'      => $auth_id,
            'auth_value'   => $auth_value,
            'auth_default' => $auth_default,
            'text'         => $text);
    }
    
    /**
    * Check if any permissions are set
    * 
    * @return bool
    */
    function is_permissions()
    {
        return ( count($this->permissions) ) ? true : false;
    }
    
    /**
    * Return permission array for this object
    * 
    * @return array
    */
    function get_permissions()
    {
        return $this->permissions;
    }
    
    /**
    * Add this object's permission options in the form of an
    * array to an existing permission boxes array
    * 
    * @return array
    */
    function permission_boxes()
    {
        global $user;
        
        $cbox_array = array();
        
        foreach ( $this->permissions as $auth_id => $permissions )
        {
            // All plugin permissions are italic, a_* should be bold/italic
            $text = '<i>' . (( preg_match('/^a_/', $permissions['auth_value']) ) ? '<b>' . $permissions['text'] . '</b>' : $permissions['text']) . '</i>';
             
            // Look for $user->lang['<code>_plugin'] - otherwise just use $user->lang['<code>']
            $code = $this->get_data('code');
            $cbox_group = ( isset($user->lang[$code . '_plugin']) ) ? $user->lang[$code . '_plugin'] : $user->lang[$code];
            $cbox_array[$cbox_group][] = array(
                'CBNAME'    => $permissions['auth_value'], 
                'CBCHECKED' => $permissions['auth_id'], 
                'TEXT'      => $text);
        }
        
        return $cbox_array;
    }
    
    // ---------------------------------------------------------
    // Log methods
    // ---------------------------------------------------------
    
    /**
    * Add a log action
    * 
    * @var $action_type
    * @var $action_text
    */
    function add_log_action($action_type, $action_text)
    {
        $this->log_actions[$action_type] = $action_text;
    }
    
    /**
    * Get log actions
    * 
    * @return array
    */
    function get_log_actions()
    {
        return $this->log_actions;
    }
    
    // ---------------------------------------------------------
    // Menu methods
    // ---------------------------------------------------------
    
    /**
    * Add a link to a menu
    * 
    * @param $menu_name
    * @param $menu_item
    */
    function add_menu($menu_name, $menu_item)
    {
        $this->menus[$menu_name][ $this->get_data('code') ] = $menu_item;
    }
    
    /**
    * Return a menu definition
    * 
    * @param $menu_name
    */
    function get_menu($menu_name)
    {
        return ( isset($this->menus[$menu_name]) ) ? $this->menus[$menu_name] : false;
    }
    
    // ---------------------------------------------------------
    // Hook methods
    // ---------------------------------------------------------    

    /**
    * Add a hook definition
    * 
    * @param $page
    * @param $hook_name
    */
    function add_hook($page, $hook_name)
    {
        $this->hooks[$page][] = $hook_name;
    }
    
    /**
    * Perform a hook call
    * 
    * @param $s_page Page with the hook
    * @return array
    */
    function do_hook($s_page)
    {
        $retval = array();
        
        foreach ( $this->hooks as $d_page => $page_hooks )
        {
            if ( $s_page == $d_page )
            {
                foreach ( $page_hooks as $hook )
                {
                    if ( method_exists($this, $hook) )
                    {
                        $retval[$hook] = $this->$hook();
                    }
                }
            }
        }
        
        return $retval;
    }
    
    // ---------------------------------------------------------
    // Data methods
    // ---------------------------------------------------------
    
    /**
    * Add plugin data to this object
    * 
    * @param $type Data type (may be a relational array of type => data)
    * @param $data Data value
    * @return bool
    */
    function add_data($type, $data = '')
    {
        if ( is_array($type) )
        {
            foreach ( $type as $i_type => $data )
            {
                $this->add_data($i_type, $data);
            }
        }
        else
        {
            if ( in_array($type, $this->pm->valid_data_types) )
            {
                $this->data[$type] = $data;
                
                return true;
            }
            else
            {
                message_die('Invalid data type ("' . $type . '").', 'Data error');
                
                return false;
            }
        }
    }
    
    /**
    * Get plugin data for this object
    * 
    * @param $type Data type
    */
    function get_data($type)
    {
        if ( in_array($type, $this->pm->valid_data_types) )
        {
            return ( isset($this->data[$type]) ) ? $this->data[$type] : false;
        }
        else
        {
            message_die('Invalid data type ("' . $type . '").', 'Data error');
            
            return false;
        }
    }
}
?>