-
-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathuserclass_class.php
2175 lines (1834 loc) · 65.7 KB
/
userclass_class.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
/*
* e107 website system
*
* Copyright (C) 2008-2017 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
* User class functions
*
*/
/**
*
* @package e107
* @subpackage e107_handlers
*
* This class handles all user-related user class functions. Admin functions inherit from it.
*/
if (!defined('e107_INIT')) { exit; }
e107::includeLan(e_LANGUAGEDIR.e_LANGUAGE.'/lan_userclass.php');
/*
Fixed classes occupy a numeric block from e_UC_SPECIAL_BASE to e_UC_SPECIAL_END, plus zero = e_UC_PUBLIC
(Note that in 0.7/1.0, class numbers stopped at 255. Now they can be up to 65535).
For info:
define("e_UC_PUBLIC", 0);
define("e_UC_MAINADMIN", 250);
define("e_UC_READONLY", 251);
define("e_UC_GUEST", 252);
define("e_UC_MEMBER", 253);
define("e_UC_ADMIN", 254);
define("e_UC_NOBODY", 255);
*/
define('e_UC_ADMINMOD' ,249); // Admins (includes main admins)
define('e_UC_MODS' ,248); // Moderators (who aren't admins)
define('e_UC_NEWUSER' ,247); // Users in 'probationary' period
define('e_UC_BOTS' ,246); // Reserved to identify search bots
// 243..245 reserved for future predefined user classes
define('e_UC_SPECIAL_BASE' ,243); // Assign class IDs 243 and above for fixed/special purposes
define('e_UC_SPECIAL_END' ,255); // Highest 'special' class
define('UC_ICON_DIR', e_IMAGE_ABS.'generic/'); // Directory for the icons used in the admin tree displays
define('e_UC_BLANK' ,'-32767'); // Code for internal use - needs to be large to avoid confusion with 'not a member of...'
define('UC_TYPE_STD' , '0'); // User class is 'normal'
define('UC_TYPE_GROUP' , '1'); // User class is a group or list of subsidiary classes
define('UC_CACHE_TAG', 'nomd5_classtree');
/**
*
*/
class user_class
{
public $class_tree; // Simple array, filled with current tree. Additional field class_children is an array of child user classes (by ID)
protected $class_parents; // Array of class IDs of 'parent' (i.e. top level) classes
public $fixed_classes = array(); // The 'predefined' core classes (constants beginning 'e_UC_') (would be nice to have this R/O outside)
public $text_class_link = array(); // List of 'core' user classes and the related constants
protected $sql_r; // We'll use our own DB to avoid interactions
protected $isAdmin; // Set true if we're an instance of user_class_admin
// Constructor
public function __construct()
{
$this->sql_r = e107::getDb('sql_r');
$this->isAdmin = FALSE;
$this->fixed_classes = array(
e_UC_PUBLIC => UC_LAN_0,
e_UC_GUEST => UC_LAN_1,
e_UC_NOBODY => UC_LAN_2,
e_UC_MEMBER => UC_LAN_3,
e_UC_ADMIN => UC_LAN_5,
e_UC_MAINADMIN => UC_LAN_6,
e_UC_READONLY => UC_LAN_4,
e_UC_NEWUSER => UC_LAN_9,
e_UC_BOTS => UC_LAN_10,
// e_UC_MODS => UC_LAN_7 // specific to Forum plugin
);
$this->text_class_link = array('public' => e_UC_PUBLIC, 'guest' => e_UC_GUEST, 'nobody' => e_UC_NOBODY, 'member' => e_UC_MEMBER,
'admin' => e_UC_ADMIN, 'main' => e_UC_MAINADMIN, 'new' => e_UC_NEWUSER,/* 'mods' => e_UC_MODS,*/
'bots' => e_UC_BOTS, 'readonly' => e_UC_READONLY);
$this->readTree(TRUE); // Initialise the classes on entry
}
/**
* @param $id
* @return false|mixed
*/
public function getFixedClassDescription($id)
{
if(isset($this->fixed_classes[$id]))
{
return $this->fixed_classes[$id];
}
return false;
}
/**
* Take a key value such as 'member' and return it's numerical value.
* @param $text
* @return bool
*/
public function getClassFromKey($text)
{
if(isset($this->text_class_link[$text]))
{
return $this->text_class_link[$text];
}
return false;
}
/**
* Return value of isAdmin
*/
public function isAdmin()
{
return $this->isAdmin;
}
/**
* Ensure the tree of userclass data is stored in our object ($this->class_tree).
* Only read if its either not present, or the $force flag is set.
* Data is cached if enabled
*
* @param boolean $force - set to TRUE to force a re-read of the info regardless.
* @return void
*/
public function readTree($force = FALSE)
{
if (isset($this->class_tree) && count($this->class_tree) && !$force) return;
$e107 = e107::getInstance();
$this->class_tree = array();
$this->class_parents = array();
if ($temp = $e107->ecache->retrieve_sys(UC_CACHE_TAG))
{
$this->class_tree = e107::unserialize($temp);
unset($temp);
}
else
{
if($this->sql_r->field('userclass_classes','userclass_parent') && $this->sql_r->select('userclass_classes', '*', 'ORDER BY userclass_parent,userclass_name', 'nowhere')) // The order statement should give a consistent return
{
while ($row = $this->sql_r->fetch())
{
$this->class_tree[$row['userclass_id']] = $row;
$this->class_tree[$row['userclass_id']]['class_children'] = array(); // Create the child array in case needed
}
}
// Add in any fixed classes that aren't already defined (they historically didn't have a DB entry, although now its facilitated (and necessary for tree structure)
foreach ($this->fixed_classes as $c => $d)
{
if (!isset($this->class_tree[$c]))
{
switch ($c)
{
case e_UC_ADMIN :
case e_UC_MAINADMIN :
$this->class_tree[$c]['userclass_parent'] = e_UC_NOBODY;
break;
case e_UC_NEWUSER :
$this->class_tree[$c]['userclass_parent'] = e_UC_MEMBER;
break;
default :
$this->class_tree[$c]['userclass_parent'] = e_UC_PUBLIC;
}
$this->class_tree[$c]['userclass_id'] = $c;
$this->class_tree[$c]['userclass_name'] = $d;
$this->class_tree[$c]['userclass_description'] = 'Fixed class';
$this->class_tree[$c]['userclass_visibility'] = e_UC_PUBLIC;
$this->class_tree[$c]['userclass_editclass'] = e_UC_MAINADMIN;
$this->class_tree[$c]['userclass_accum'] = $c;
$this->class_tree[$c]['userclass_type'] = UC_TYPE_STD;
}
}
$userCache = e107::serialize($this->class_tree, FALSE);
$e107->ecache->set_sys(UC_CACHE_TAG,$userCache);
unset($userCache);
}
// Now build the tree.
// There are just two top-level classes - 'Everybody' and 'Nobody'
$this->class_parents[e_UC_PUBLIC] = e_UC_PUBLIC;
$this->class_parents[e_UC_NOBODY] = e_UC_NOBODY;
foreach ($this->class_tree as $uc)
{
if (($uc['userclass_id'] != e_UC_PUBLIC) && ($uc['userclass_id'] != e_UC_NOBODY))
{
if (!isset($this->class_tree[$uc['userclass_parent']]))
{
echo "Orphaned class record: ID=".$uc['userclass_id']." Name=".$uc['userclass_name']." Parent=".$uc['userclass_parent']."<br />";
}
else
{ // Add to array
$this->class_tree[$uc['userclass_parent']]['class_children'][] = $uc['userclass_id'];
}
}
}
}
/**
* Given the list of 'base' classes a user belongs to, returns a comma separated list including ancestors. Duplicates stripped
*
* @param string $startList - comma-separated list of classes user belongs to
* @param boolean $asArray - if TRUE, result returned as array; otherwise result returned as string
* @return string|array of user classes; format determined by $asArray
*/
public function get_all_user_classes($startList, $asArray = FALSE)
{
$is = array();
$start_array = explode(',', $startList);
foreach ($start_array as $sa)
{ // Merge in latest values - should eliminate duplicates as it goes
$is[] = $sa; // add parent to the flat list first
if (isset($this->class_tree[$sa]))
{
if($this->class_tree[$sa]['userclass_accum'])
{
$is = array_merge($is,explode(',',$this->class_tree[$sa]['userclass_accum']));
}
}
}
if ($asArray)
{
return array_unique($is);
}
return implode(',',array_unique($is));
}
/**
* Returns a list of user classes which can be edited by the specified classlist
*
* @param string $classList - comma-separated list of classes to consider - default current user's class list
* @param boolean $asArray - if TRUE, result returned as array; otherwise result returned as string
* @return string|array of user classes; format determined by $asArray
*/
public function get_editable_classes($classList = USERCLASS_LIST, $asArray = FALSE)
{
$ret = array();
$blockers = array(e_UC_PUBLIC => 1, e_UC_READONLY => 1, e_UC_MEMBER => 1, e_UC_NOBODY => 1, e_UC_GUEST => 1, e_UC_NEWUSER => 1, e_UC_BOTS => 1);
$possibles = array_flip(explode(',',$classList));
unset($possibles[e_UC_READONLY]);
foreach ($this->class_tree as $uc => $uv)
{
if (!isset($blockers[$uc]))
{
$ec = $uv['userclass_editclass'];
// $ec = $uv['userclass_visibility'];
if (isset($possibles[$ec]))
{
$ret[] = $uc;
}
}
}
if ($asArray) { return $ret; }
return implode(',',$ret);
}
/**
* Combines the selected editable classes into the main class list for a user.
*
* @param array|string $combined - the complete list of current class memberships
* @param array|string $possible - the classes which are being edited
* @param array|string $actual - the actual membership of the editable classes
* @param boolean $asArray - if TRUE, result returned as array; otherwise result returned as string
* @return string|array of user classes; format determined by $asArray
*/
public function mergeClassLists($combined, $possible, $actual, $asArray = FALSE)
{
if (!is_array($combined)) { $combined = explode(',',$combined); }
if (!is_array($possible)) { $possible = explode(',',$possible); }
if (!is_array($actual)) { $actual = explode(',',$actual); }
$combined = array_flip($combined);
foreach ($possible as $p)
{
if (in_array($p,$actual))
{ // Class must be in final array
$combined[$p] = 1;
}
else
{
unset($combined[$p]);
}
}
$combined = array_keys($combined);
if ($asArray) { return $combined; }
return implode(',', $combined);
}
/**
* Remove the fixed classes from a class list
* Removes all classes in the reserved block, as well as e_UC_PUBLIC
* @param array|string $inClasses - the complete list of current class memberships
* @return string|array of user classes; format is the same as $inClasses
*/
public function stripFixedClasses($inClasses)
{
$asArray = TRUE;
if (!is_array($inClasses))
{
$asArray = FALSE;
$inClasses = explode(',',$inClasses);
}
/*
$inClasses = array_flip($inClasses);
foreach ($this->fixed_classes as $k => $v)
{
if (isset($inClasses[$k])) { unset($inClasses[$k]); }
}
$inClasses = array_keys($inClasses);
*/
foreach ($inClasses as $k => $uc)
{
if ((($uc >= e_UC_SPECIAL_BASE) && ($uc <= e_UC_SPECIAL_END)) || ($uc == e_UC_PUBLIC))
{
unset($inClasses[$k]);
}
}
if ($asArray) { return ($inClasses); }
return implode(',',$inClasses);
}
/**
* Given a comma separated list, returns the minimum number of class memberships required to achieve this (i.e. strips classes 'above' another in the tree)
* Requires the class tree to have been initialised
*
* @param array|string $classList - the complete list of current class memberships
*
* @return string|array of user classes; format is the same as $classList
*/
public function normalise_classes($classList)
{
if (is_array($classList))
{
$asArray = TRUE;
$oldClasses = $classList;
}
else
{
$asArray = FALSE;
$oldClasses = explode(',',$classList);
}
$dropClasses = array();
foreach ($oldClasses as $c)
{ // Look at our parents (which are in 'userclass_accum') - if any of them are contained in oldClasses, we can drop them.
$tc = array_flip(explode(',',$this->class_tree[$c]['userclass_accum']));
unset($tc[$c]); // Current class should be in $tc anyway
foreach ($tc as $tc_c => $v)
{
if (in_array($tc_c,$oldClasses))
{
$dropClasses[] = $tc_c;
}
}
}
$newClasses = array_diff($oldClasses,$dropClasses);
if ($asArray) { return $newClasses; }
return implode(',',$newClasses);
}
/**
* @param string $optlist - comma-separated list of classes/class types to be included in the list
It allows selection of the classes to be shown in the dropdown. All or none can be included, separated by comma. Valid options are:
public
guest
nobody
member
readonly
admin
main - main admin
new - new users
bots - search bot class
classes - shows all classes
matchclass - if 'classes' is set, this option will only show the classes that the user is a member of
* @return array
*/
public function getClassList($optlist)
{
return $this->uc_required_class_list($optlist);
}
/**
* Generate a dropdown list of user classes from which to select - virtually as the deprecated r_userclass() function did
* [ $mode parameter of r_userclass() removed - $optlist is more flexible) ]
*
* @param string $fieldname - name of select list
* @param mixed $curval - current selected value (empty string if no current value)
* @param string $optlist - comma-separated list of classes/class types to be included in the list
It allows selection of the classes to be shown in the dropdown. All or none can be included, separated by comma. Valid options are:
public
guest
nobody
member
readonly
admin
main - main admin
new - new users
bots - search bot class
classes - shows all classes
matchclass - if 'classes' is set, this option will only show the classes that the user is a member of
blank - puts an empty option at the top of select dropdowns
filter - only show those classes where member is in a class permitted to view them - e.g. as the new 'visible to' field - added for 2.0
force - show all classes (subject to the other options, including matchclass) - added for 2.0
all - alias for 'force'
no-excludes - if present, doesn't show the 'not member of' list
is-checkbox - if present, suppresses the <optgroup...> construct round the 'not member of' list
editable - can only appear on its own - returns list of those classes the user can edit (manage)
* @param string $extra_js - can add JS handlers (e.g. 'onclick', 'onchange') if required
*/
public function uc_dropdown($fieldname, $curval = 0, $optlist = '', $extra_js = null)
{
$optlist = (string) $optlist;
$show_classes = self::uc_required_class_list($optlist); // Get list of classes which meet criteria
$text = '';
foreach ($show_classes as $k => $v)
{
if ($k == e_UC_BLANK)
{
$text .= "<option value=''> </option>\n";
}
else
{
$s = ($curval == $k && $curval !== '') ? "selected='selected'" : '';
$text .= "<option class='uc-select' value='".$k."' ".$s.">".$v."</option>\n";
}
}
if(is_array($extra_js))
{
$options = $extra_js;
$extra_js = '';
}
$id = $fieldname;
$class = "tbox form-control";
if(!empty($options['class']))
{
$class .= " ".$options['class'];
}
if(!empty($options['id']))
{
$id = $options['id'];
}
// Inverted Classes
if(strpos($optlist, 'no-excludes') === FALSE)
{
if (strpos($optlist, 'is-checkbox') !== FALSE)
{
$text .= "\n".UC_LAN_INVERTLABEL."<br />\n";
}
else
{
$text .= "\n";
$text .= '<optgroup label=\''.UC_LAN_INVERTLABEL.'\'>';
$text .= "\n";
}
foreach ($show_classes as $k => $v)
{
if($k != e_UC_PUBLIC && $k != e_UC_NOBODY && $k != e_UC_READONLY) // remove everyone, nobody and readonly from list.
{
$s = ($curval == ('-'.$k) && $curval !== '') ? "selected='selected'" : '';
$text .= "<option class='uc-select-inverted' value='-".$k."' ".$s.">".str_replace("[x]", $v, UC_LAN_INVERT)."</option>\n";
}
}
$text .= "</optgroup>\n";
}
// Only return the select box if we've ended up with some options
if ($text) $text = "\n<select class='".$class."' name='$fieldname' id='$id' {$extra_js}>\n".$text."</select>\n";
return $text;
}
/**
* Generate an ordered array classid=>classname - used for dropdown and check box lists
*
* @param string $optlist - comma-separated list of classes/class types to include (see uc_dropdown for details)
* @param boolean $just_ids - if TRUE, each returned array value is '1'; otherwise it is the class name
* @return array of user classes; ky is numeric class id, value is '1' or class name according to $just_ids
*/
public function uc_required_class_list($optlist = '', $just_ids = FALSE)
{
$ret = array();
$opt_arr = array();
if ($optlist)
{
$opt_arr = array_map('trim', explode(',',$optlist));
}
$opt_arr = array_flip($opt_arr); // This also eliminates duplicates which could arise from applying the other options, although shouldn't matter
if (isset($opt_arr['no-excludes'])) unset($opt_arr['no-excludes']);
if (isset($opt_arr['is-checkbox'])) unset($opt_arr['is-checkbox']);
if (count($opt_arr) == 0)
{
$opt_arr = array('public' => 1, 'guest' => 1, 'nobody' => 1, 'member' => 1, 'classes' => 1);
}
if (isset($opt_arr['all']))
{
unset($opt_arr['all']);
$opt_arr['force'] = 1;
}
if (isset($opt_arr['editable']))
{
$temp = array_flip(explode(',',$this->get_editable_classes()));
if ($just_ids) return $temp;
foreach ($temp as $c => $t)
{
$temp[$c] = $this->class_tree[$c]['userclass_name'];
}
return $temp;
}
if (isset($opt_arr['force'])) unset($opt_arr['filter']);
if (isset($opt_arr['blank']))
{
$ret[e_UC_BLANK] = 1;
}
// Do the 'fixed' classes next
foreach ($this->text_class_link as $k => $v)
{
// if (isset($opt_arr[$k]) || isset($opt_arr['force']))
if (isset($opt_arr[$k]))
{
$ret[$v] = $just_ids ? '1' : $this->fixed_classes[$v];
}
}
// Now do the user-defined classes
if (isset($opt_arr['classes']) || isset($opt_arr['force']))
{ // Display those classes the user is allowed to:
// Main admin always sees the lot
// a) Mask the 'fixed' user classes which have already been processed
// b) Apply the visibility option field ('userclass_visibility')
// c) Apply the matchclass option if appropriate
foreach($this->class_tree as $uc_id => $row)
{
if (!array_key_exists($uc_id,$this->fixed_classes)
&& ( getperms('0')
|| (
(!isset($opt_arr['matchclass']) || check_class($uc_id))
&&
(!isset($opt_arr['filter']) || check_class($row['userclass_visibility']))
)
)
)
{
$ret[$uc_id] = $just_ids ? '1' : $this->class_tree[$uc_id]['userclass_name'];
}
}
}
/* Above loop slightly changes the display order of earlier code versions.
If readonly must be last, delete it from the $text_class_link array, and uncomment the following code
if (isset($opt_arr['readonly']))
{
$ret[e_UC_READONLY] = $this->class_tree[e_UC_READONLY]['userclass_description'];
}
*/
return $ret;
}
/**
* Very similar to self::uc_dropdown, but returns a list of check boxes. Doesn't encapsulate it.
*
* @param string $fieldname is the name for the array of checkboxes
* @param string $curval is a comma separated list of class IDs for boxes which are checked.
* @param string $optlist as for uc_dropdown
* @param boolean $showdescription - if TRUE, appends the class description in brackets
* @param boolean $asArray - if TRUE, result returned as array; otherwise result returned as string
*
* return string|array according to $asArray
* @return array|string
*/
public function uc_checkboxes($fieldname, $curval='', $optlist = '', $showdescription = FALSE, $asArray = FALSE)
{
$show_classes = $this->uc_required_class_list($optlist);
$frm = e107::getForm();
$curArray = explode(',', $curval); // Array of current values
$ret = array();
foreach ($show_classes as $k => $v)
{
if ($k != e_UC_BLANK)
{
// $c = (in_array($k,$curArray)) ? " checked='checked'" : '';
$c = (in_array($k,$curArray)) ? true : false;
if ($showdescription) $v .= ' ('.$this->getDescription($k).')';
//$ret[] = "<div class='field-spacer'><input type='checkbox' class='checkbox' name='{$fieldname}[{$k}]' id='{$fieldname}-{$k}' value='{$k}'{$c} /><label for='{$fieldname}-{$k}'>".$v."</label></div>\n";
$name = $fieldname.'['.$k.']';
$ret[] = $frm->checkbox($name,$k,$c,$v);
//$ret[] = "<div class='field-spacer'><input type='checkbox' class='checkbox' name='{$fieldname}[{$k}]' id='{$fieldname}-{$k}' value='{$k}'{$c} /><label for='{$fieldname}-{$k}'>".$v."</label></div>\n";
}
}
if ($asArray) return $ret;
return implode('', $ret);
}
/**
* Used by @see{vetted_tree()} to generate lower levels of tree
*
* @param string $listnum - class number of the parent. Is negative if the class is 'Everyone except...' (Must be a string because 0 == -0)
* @param integer $nest_level - indicates our level in the tree - 0 is the top level; increases as we descend the tree. Positive value.
* @param string $current_value - comma-separated list of integers indicating classes selected. (Spaces not permitted)
* @param array $perms - list of classes we are allowed to display
* @param string $opt_options - passed to callback function; not otherwise used
*/
protected function vetted_sub_tree($treename, $callback, $listnum, $nest_level, $current_value, $perms, $opt_options)
{
$ret = '';
$nest_level++;
$listIndex = abs($listnum);
$classSign = (strpos($listnum, '-') === 0) ? '-' : '+';
//echo "Subtree: {$listnum}, {$nest_level}, {$current_value}, {$classSign}:{$listIndex}<br />";
if(isset($this->class_tree[$listIndex]['class_children']))
{
foreach ($this->class_tree[$listIndex]['class_children'] as $p)
{
$classValue = $classSign.$p;
// Looks like we don't need to differentiate between function and class calls
if (isset($perms[$p]))
{
$ret .= call_user_func($callback, $treename, $classValue, $current_value, $nest_level, $opt_options);
}
$ret .= $this->vetted_sub_tree($treename, $callback, $classValue, $nest_level, $current_value, $perms, $opt_options);
}
}
return $ret;
}
/**
* create an indented tree - for example within a select box or a list of check boxes.
* For each displayed element, the callback routine is called
* @param string $treename is the name given to the elements where required
* @param function|object $callback is a routine used to generate each element; there are three implemented within this class:
* select (the default) - generates the option list. Text requires to be encapsulated in a <select......./select> tag set
* - can also be used with multi-select boxes
* checkbox - generates a set of checkboxes
* checkbox_desc - generates a set of checkboxes with the class description in brackets
* Alternative callbacks can be used to achieve different layouts/styles
* @param integer|string $current_value - single class number for single-select dropdown; comma separated array of class numbers for checkbox list or multi-select
* @param string $optlist works the same as for @see uc_dropdown()
* @param string $opt_options - passed to callback function; not otherwise used
* @return string - formatted HTML for tree
*/
public function vetted_tree($treename, $callback='', $current_value='', $optlist = '', $opt_options = '')
{
$ret = '';
if (!$callback) $callback=array($this,'select');
$current_value = str_replace(' ','',$current_value); // Simplifies parameter passing for the tidy-minded
$notCheckbox = (strpos($optlist, 'is-checkbox') === FALSE);
$perms = $this->uc_required_class_list($optlist,TRUE); // List of classes which we can display
if (isset($perms[e_UC_BLANK]))
{
$ret .= call_user_func($callback, $treename, e_UC_BLANK, $current_value, 0, $opt_options);
}
foreach ($this->class_parents as $p)
{
if (isset($perms[$p]))
{
$ret .= call_user_func($callback, $treename, $p, $current_value, 0, $opt_options);
}
$ret .= $this->vetted_sub_tree($treename, $callback, $p, 0, $current_value, $perms, $opt_options);
}
// Inverted classes. (negative values for exclusion).
if(strpos($optlist, 'no-excludes') === FALSE)
{
if ($notCheckbox)
{
$ret .= "\n";
$ret .= '<optgroup label=\''.UC_LAN_INVERTLABEL.'\'>';
$ret .= "\n";
}
else
{
$ret .= "\n".UC_LAN_INVERTLABEL."<br />\n";
}
foreach ($this->class_parents as $k => $p) // Currently key and data are the same
{
//echo "Class parent: {$k}:{$p}<br />";
if($k != e_UC_PUBLIC && $k != e_UC_NOBODY && $k != e_UC_READONLY) // remove everyone, nobody and readonly from list.
{
if (isset($perms[$p]))
{
$ret .= call_user_func($callback, $treename, '-'.$p, $current_value, 0, $opt_options);
}
}
$ret .= $this->vetted_sub_tree($treename, $callback, '-'.$p, 0, $current_value, $perms, $opt_options);
}
if ($notCheckbox)
{
$ret .= "</optgroup>\n";
}
}
return $ret;
}
/**
* Callback for vetted_tree - Creates the option list for a selection box
* It is the caller's responsibility to enclose this list in a <select...../select> structure
* Can be used as a basis for similar functions
*
* @param string $treename - name of tree elements (not used with select; used with checkboxes, for example)
* @param string $classnum - user class being displayed. This may be negative to indicate 'everyone but...'
* - special numeric part e_UC_BLANK adds a blank option in the list.
* @param integer|string $current_value - single class number for single-select dropdown; comma separated array of class numbers for checkbox list or multi-select
* @param integer $nest_level - 'depth' of this item in the tree. Zero is base level. May be used to indent or highlight dependent on level
* @param string $opt_options - passed to callback function; not otherwise used
*
* @return string - option list
*/
public function select($treename, $classnum, $current_value, $nest_level, $opt_options = '')
{
$classIndex = abs($classnum); // Handle negative class values
$classSign = (strpos($classnum, '-') === 0) ? '-' : '';
if ($classIndex == e_UC_BLANK) return "<option value=''> </option>\n";
$tmp = explode(',',$current_value);
$sel = in_array($classnum, $tmp) ? " selected='selected'" : '';
if ($nest_level == 0)
{
$prefix = '';
$style = " style='font-weight:bold; font-style: italic;'";
}
elseif ($nest_level == 1)
{
$prefix = ' ';
$style = " style='font-weight:bold'";
}
else
{
$prefix = ' '.str_repeat('--',$nest_level-1).'>';
$style = '';
}
$ucString = $this->class_tree[$classIndex]['userclass_name'];
if ($classSign == '-')
{
$ucString = str_replace('[x]', $ucString, UC_LAN_INVERT);
}
return "<option value='{$classSign}{$classIndex}'{$sel}{$style}>".$prefix.$ucString."</option>\n";
}
/**
* Callback for vetted_tree - displays indented checkboxes with class name only
* See @link select for parameter details
*/
public function checkbox($treename, $classnum, $current_value, $nest_level, $opt_options = '')
{
$frm = e107::getForm();
$classIndex = abs($classnum); // Handle negative class values
$classSign = (strpos($classnum, '-') === 0) ? '-' : '';
if ($classIndex == e_UC_BLANK) return '';
$tmp = explode(',',$current_value);
$chk = in_array($classnum, $tmp) ? " checked='checked'" : '';
$style = "";
if ($nest_level == 0)
{
$style = " style='font-weight:bold'";
}
elseif($nest_level > 1)
{
$style = " style='text-indent:".(1.2 * $nest_level)."em'";
}
$ucString = $this->class_tree[$classIndex]['userclass_name'];
if ($classSign == '-')
{
$ucString = str_replace('[x]', $ucString, UC_LAN_INVERT);
}
$checked = in_array($classnum, $tmp) ? true : false;
$pre = "<div {$style}>";
$post = "</div>\n";
if(defset('THEME_VERSION') === 2.3)
{
$pre = '';
$post = '';
}
return $pre.$frm->checkbox($treename.'[]',$classSign.$classIndex, $checked, array('label'=> $ucString)).$post;
// return "<div {$style}><input type='checkbox' class='checkbox' name='{$treename}[]' id='{$treename}_{$classSign}{$classIndex}' value='{$classSign}{$classIndex}'{$chk} />".$ucString."</div>\n";
}
/**
* Callback for vetted_tree - displays indented checkboxes with class name, and description in brackets
* See @link select for parameter details
*/
public function checkbox_desc($treename, $classnum, $current_value, $nest_level, $opt_options = '')
{
$classIndex = abs($classnum); // Handle negative class values
$classSign = (strpos($classnum, '-') === 0) ? '-' : '';
if ($classIndex == e_UC_BLANK) return '';
$tmp = explode(',',$current_value);
$chk = in_array($classnum, $tmp) ? " checked='checked'" : '';
if ($nest_level == 0)
{
$style = " style='font-weight:bold'";
}
else
{
$style = " style='text-indent:".(0.3 * $nest_level)."em'";
}
$id = "{$treename}_{$classnum}";
$ucString = $this->class_tree[$classIndex]['userclass_name'];
if ($classSign == '-')
{
$ucString = str_replace('[x]', $ucString, UC_LAN_INVERT);
}
$description = $ucString.' ('.$this->class_tree[$classIndex]['userclass_description'].")";
$id ="{$treename}_{$classSign}{$classnum}";
$pre = "<div class='checkbox' {$style}>";
$post = "</div>\n";
if(defset('THEME_VERSION') === 2.3)
{
$pre = '';
$post = '';
}
return $pre.e107::getForm()->checkbox($treename.'[]', $classnum , $chk, array("id"=>$id,'label'=>$description)).$post;
// return "<div {$style}><input type='checkbox' class='checkbox' name='{$treename}[]' id='{$treename}_{$classSign}{$classnum}' value='{$classSign}{$classnum}'{$chk} />".$this->class_tree[$classIndex]['userclass_name'].' ('.$this->class_tree[$classIndex]['userclass_description'].")</div>\n";
}
/**
* Return array of all classes, limited according to membership of the userclass_visibility field if $filter is set.
* @param string|integer $filter - user class or class list in format acceptable to check_class()
* @return array of class elements, each itself an array:
* Index field - userclass_id
* Data fields - userclass_name, userclass_description, userclass_editclass
*/
public function uc_get_classlist($filter = FALSE)
{
$ret = array();
$this->readTree(FALSE); // Make sure we have data
foreach ($this->class_tree as $k => $v)
{
if (!$filter || check_class($filter))
{
$ret[$k] = array('userclass_name' => $v, 'userclass_description' => $v['userclass_description'], 'userclass_editclass' => $v['userclass_editclass']);
}
}
return $ret;
}
/**
* Return class name for given class ID
* Handles 'not a member of...' construct by replacing '--CLASS--' in UC_LAN_INVERT with the class name
* @param integer $id - class number. A negative number indicates 'not a member of...'
* @return string class name
*/
public function getName($id)
{
$id = (int) $id;
$cn = abs($id);
$ucString = 'Class:'.$id; // Debugging aid - this should be overridden
if (isset($this->class_tree[$cn]))
{
$ucString = $this->class_tree[$cn]['userclass_name'];
}
elseif (isset($this->fixed_classes[$cn]))
{
$ucString = $this->fixed_classes[$cn];
}
if($id < 0)
{
//$val = abs($id);
//$name = isset($this->class_tree[$val]['userclass_name']) ? $this->class_tree[$val]['userclass_name'] : $this->fixed_classes[$val];
$ucString = str_replace('[x]', $ucString, UC_LAN_INVERT);
}
return $ucString;
}
/**
* Return a key-name identifier for given class ID
* @param integer $id - class number. A negative number indicates 'not a member of...'
* @return string class name ke
*/
public function getIdentifier($id)
{
$cn = abs($id);
$ucString = '';
$fixedClasses = array_flip($this->text_class_link);
if(isset($fixedClasses[$cn]))
{
return $fixedClasses[$cn];
}
if(isset($this->class_tree[$cn]))
{
return e107::getForm()->name2id($this->class_tree[$cn]['userclass_name']);