/
BitGroup.php
1748 lines (1537 loc) · 70.8 KB
/
BitGroup.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
/**
* @version $Header$
* Copyright (c) 2008 bitweaver Group
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details.
*
* Group class
* builds on core bitweaver functionality, such as the Liberty CMS engine
*
* @author wjames <will@tekimaki.com> spider <spider@viovio.com>
* @package groups
*/
/**
* Initialize
*/
/**
* This is used to uniquely identify the object
*/
define( 'BITGROUP_CONTENT_TYPE_GUID', 'bitgroup' );
/*
* Defines for basic roles you can't remove
*/
define( 'GROUPS_ROLE_ADMIN', 1);
define( 'GROUPS_ROLE_MANAGER', 2);
define( 'GROUPS_ROLE_MEMBER', 3);
/**
* Load up our moderation handlers
*/
require_once( GROUP_PKG_PATH.'moderation_inc.php' );
/**
* @package groups
*/
class BitGroup extends LibertyMime {
/**
* Primary key
* @public
*/
var $mGroupId;
/**
* During initialisation, be sure to call our base constructors
**/
function BitGroup( $pGroupId=NULL, $pContentId=NULL ) {
parent::__construct();
$this->mGroupId = (int)$pGroupId;
$this->mContentId = (int)$pContentId;
$this->mContentTypeGuid = BITGROUP_CONTENT_TYPE_GUID;
$this->registerContentType( BITGROUP_CONTENT_TYPE_GUID, array(
'content_type_guid' => BITGROUP_CONTENT_TYPE_GUID,
'content_name' => 'Group',
'handler_class' => 'BitGroup',
'handler_package' => 'group',
'handler_file' => 'BitGroup.php',
'maintainer_url' => 'http://www.bitweaver.org'
) );
// Permission setup
$this->mViewContentPerm = 'p_group_view';
$this->mCreateContentPerm = 'p_group_create';
$this->mUpdateContentPerm = 'p_group_update';
$this->mAdminContentPerm = 'p_group_admin';
// A reference to the group's affiliated board, see getBoard() below
$this->mBoardObj = NULL;
}
/**
* Load the data from the database
**/
function load() {
if( $this->verifyId( $this->mGroupId ) || $this->verifyId( $this->mContentId ) ) {
// LibertyContent::load()assumes you have joined already, and will not execute any sql!
// This is a significant performance optimization
$lookupColumn = $this->verifyId( $this->mGroupId ) ? 'group_id' : 'content_id';
$bindVars = array();
$selectSql = $joinSql = $whereSql = '';
array_push( $bindVars, $lookupId = @BitBase::verifyId( $this->mGroupId ) ? $this->mGroupId : $this->mContentId );
$this->getServicesSql( 'content_load_sql_function', $selectSql, $joinSql, $whereSql, $bindVars );
$query = "SELECT s.*, lc.*, lcds.`data` AS `summary`, lcda.`data` AS `after_registration`, ug.*, " .
"lfp.file_name AS `image_attachment_path`, " .
"uue.`login` AS modifier_user, uue.`real_name` AS modifier_real_name, " .
"uuc.`login` AS creator_user, uuc.`real_name` AS creator_real_name, " .
"brd.`board_id`" .
"$selectSql " .
"FROM `".BIT_DB_PREFIX."groups` s " .
"INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = s.`content_id` ) $joinSql" .
"INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON( ug.`group_id` = s.`group_id` ) " .
"LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcds ON (lc.`content_id` = lcds.`content_id` AND lcds.`data_type`='summary')" .
"LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcda ON (lc.`content_id` = lcda.`content_id` AND lcda.`data_type`='after_registration')" .
"LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` la ON( la.`content_id` = lc.`content_id` AND la.`is_primary` = 'y' )" .
"LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` lfp ON( lfp.`file_id` = la.`foreign_id` )" .
"LEFT OUTER JOIN `".BIT_DB_PREFIX."groups_content_cnxn_map` gccm ON ( lc.`content_id` = gccm.`group_content_id` )" .
"INNER JOIN `".BIT_DB_PREFIX."boards` brd ON (gccm.`to_content_id` = brd.`content_id`)" .
"LEFT JOIN `".BIT_DB_PREFIX."users_users` uue ON( uue.`user_id` = lc.`modifier_user_id` )" .
"LEFT JOIN `".BIT_DB_PREFIX."users_users` uuc ON( uuc.`user_id` = lc.`user_id` )" .
"WHERE s.`$lookupColumn`=? $whereSql";
$result = $this->mDb->query( $query, $bindVars );
if( $result && $result->numRows() ) {
$this->mInfo = $result->fields;
$this->mContentId = $result->fields['content_id'];
$this->mGroupId = $result->fields['group_id'];
$this->mInfo['creator'] =( isset( $result->fields['creator_real_name'] )? $result->fields['creator_real_name'] : $result->fields['creator_user'] );
$this->mInfo['editor'] =( isset( $result->fields['modifier_real_name'] )? $result->fields['modifier_real_name'] : $result->fields['modifier_user'] );
$this->mInfo['display_url'] = $this->getDisplayUrl();
$this->mInfo['display_urls'] = $this->getDisplayUrls();
$this->mInfo['parsed_data'] = $this->parseData();
$this->mInfo['num_members'] = $this->getMembersCount( $this->mGroupId );
$this->mInfo['thumbnail_url'] = liberty_fetch_thumbnails( array( "storage_path" => $this->mInfo['image_attachment_path'] ) );
$this->mContentTypePrefs = $this->getContentTypePrefs();
$this->mContentTypeData = $this->getContentTypeData();
// sets $this->mGroupMemberPermissions
$this->getMemberRolesAndPermsForGroup();
LibertyMime::load();
}
}
return( count( $this->mInfo ) );
}
/**
* Prepare data for preview
*/
function preparePreview( $pParamHash ) {
global $gBitSystem, $gBitUser;
if( empty( $this->mInfo['user_id'] ) ) {
$this->mInfo['user_id'] = $gBitUser->mUserId;
$this->mInfo['creator_user'] = $gBitUser->getField( 'login' );
$this->mInfo['creator_real_name'] = $gBitUser->getField( 'real_name' );
}
$this->mInfo['creator_user_id'] = $this->mInfo['user_id'];
if( empty( $this->mInfo['created'] ) ){
$this->mInfo['created'] = $gBitSystem->getUTCTime();
}
if( isset( $pParamHash["group"]["title"] ) ) {
$this->mInfo["title"] = $pParamHash["group"]["title"];
}
if( isset( $pParamHash["group"]["summary"] ) ) {
$this->mInfo["description"] = $pParamHash["group"]["summary"];
}
if( isset( $pParamHash["format_guid"] ) ) {
$this->mInfo['format_guid'] = $pParamHash["format_guid"];
}
if( isset( $pParamHash["group"]["edit"] ) ) {
$this->mInfo["data"] = $pParamHash["group"]["edit"];
$this->mInfo['parsed_data'] = $this->parseData();
}
}
/**
* @param array pParamHash hash of values that will be used to store the group
* be sure to pass by reference in case we need to make modifcations to the hash
*
* @return bool TRUE on success, FALSE if store could not occur. If FALSE, $this->mErrors will have reason why
*
* @access public
**/
function store( &$pParamHash ) {
global $gBitUser, $gBitSystem;
// Merge down groups prefix. This is a hack but is faster
// than rewriting verify to use things from the "group" prefix.
// @TODO: Rewire verify() to pull all data from 'group' prefix
if( !empty( $pParamHash['group'] ) ){
$pParamHash = array_merge($pParamHash, $pParamHash['group']);
}
$this->mDb->StartTrans();
// Verify and then store group and content.
if( $this->verify( $pParamHash ) && $gBitUser->storeGroup( $pParamHash ) && LibertyMime::store( $pParamHash ) ) {
$table = BIT_DB_PREFIX."groups";
if( $this->mGroupId ) {
// editing an existing group
$locId = array( "group_id" => $pParamHash['group_id'] );
$result = $this->mDb->associateUpdate( $table, $pParamHash['group_pkg_store'], $locId );
}else {
// new group
$pParamHash['group_pkg_store']['content_id'] = $pParamHash['content_id'];
$pParamHash['group_pkg_store']['group_id'] = $pParamHash['group_store']['group_id'];
$this->mGroupId = $pParamHash['group_store']['group_id'];
$result = $this->mDb->associateInsert( $table, $pParamHash['group_pkg_store'] );
// Make sure this user is in the group
$gBitUser->addUserToGroup( $gBitUser->mUserId, $this->mGroupId );
// Restore the group in users table to update the home link now that we have a group id
$pParamHash['home'] = GROUP_PKG_URL."index.php?group_id=".$this->mGroupId;
// Restore the home now
$gBitUser->storeGroup( $pParamHash );
// Autogenerate a board for this group
if ( $gBitSystem->isPackageActive( 'boards' ) ){
require_once( BOARDS_PKG_PATH.'BitBoard.php' );
$board = new BitBoard();
$boardHash = array(
"title" => $pParamHash['title']." ".tra('Forum'),
"data" => tra('Message board for the ').$pParamHash['title']." ".tra('Group'),
'boards_mailing_list' => preg_replace( '/[^a-z0-9]/', '', strtolower( $pParamHash['content_store']['title'] ) ),
'boards_mailing_list_password' => substr( md5( rand() ), 0, 8 ),
'group' => array( 'bypass_map_required' => TRUE ),
);
if ( $board->store( $boardHash ) ){
$this->linkContent( $board->mInfo );
$this->mBoardObj = &$board;
} else {
$this->mErrors['board'] = tra('Unknown error while creating the board.');
$this->mErrors = array_merge($this->mErrors, $board->mErrors);
}
}
}
// @TODO: This should be in boards
if ( $gBitSystem->isPackageActive( 'boards' ) ){
if ( empty( $board ) || !is_object( $board ) ){
$board = $this->getBoard();
}
// pass moderate messages selection on to our group board
$modComments = $pParamHash['group_pkg_store']['mod_msgs'] == 'y'?$pParamHash['group_pkg_store']['mod_msgs']:NULL;
$board->storePreference( 'moderate_comments', $modComments );
$list = $board->getPreference('boards_mailing_list');
if (!empty($list)) {
require_once(UTIL_PKG_PATH.'mailman_lib.php');
mailman_setmoderated($list, $modComments == 'y' ? 1 : 0);
}
}
if( count($this->mErrors) == 0) {
$this->mDb->CompleteTrans();
$this->load();
} else {
$this->mDb->RollbackTrans();
// Clear out the IDs
$this->mContentId = NULL;
$this->mGroupId = NULL;
}
}
return( count( $this->mErrors )== 0 );
}
/**
* Make sure the data is safe to store
* @param pParamHash be sure to pass by reference in case we need to make modifcations to the hash
* This function is responsible for data integrity and validation before any operations are performed with the $pParamHash
* NOTE: This is a PRIVATE METHOD!!!! do not call outside this class, under penalty of death!
*
* @param array pParams reference to hash of values that will be used to store the page, they will be modified where necessary
*
* @return bool TRUE on success, FALSE if verify failed. If FALSE, $this->mErrors will have reason why
*
* @access private
**/
function verify( &$pParamHash ) {
global $gBitUser, $gBitSystem;
// make sure we're all loaded up of we have a mGroupId
if( $this->verifyId( $this->mGroupId ) && empty( $this->mInfo ) ) {
$this->load();
}
if( @$this->verifyId( $this->mInfo['content_id'] ) ) {
$pParamHash['content_id'] = $this->mInfo['content_id'];
}
if( @$this->verifyId( $this->mInfo['group_id'] ) ) {
$pParamHash['group_id'] = $this->mInfo['group_id'];
}
if( @$this->verifyId( $this->mInfo['user_id'] ) ) {
$pParamHash['user_id'] = $this->mInfo['user_id'];
}
// It is possible a derived class set this to something different
if( @$this->verifyId( $pParamHash['content_type_guid'] ) ) {
$pParamHash['content_type_guid'] = $this->mContentTypeGuid;
}
if( @$this->verifyId( $pParamHash['content_id'] ) ) {
$pParamHash['group_pkg_store']['content_id'] = $pParamHash['content_id'];
}
if( @$this->verifyId( $pParamHash['group_id'] ) ) {
$pParamHash['group_store']['group_id'] = $pParamHash['group_id'];
$pParamHash['group_pkg_store']['group_id'] = $pParamHash['group_id'];
}
if( !empty( $pParamHash['data'] ) ) {
$pParamHash['edit'] = $pParamHash['data'];
}
// check for name issues, first truncate length if too long
if( !empty( $pParamHash['title'] ) ) {
if( empty( $this->mGroupId ) ) {
if( empty( $pParamHash['title'] ) ) {
$this->mErrors['title'] = tra('You must enter a name for this group.');
} else {
$bindVars = array( $pParamHash['title'], $this->mContentTypeGuid );
// check to see if this name is already taken
$query = "SELECT lc.title
FROM `".BIT_DB_PREFIX."liberty_content` lc
WHERE lc.`title` = ? AND lc.`content_type_guid` = ?";
if( $this->mDb->getOne( $query, $bindVars ) ){
$this->mErrors['title'] = tra("We're sorry, this group name is already taken, please create a unique group name");
}
$pParamHash['content_store']['title'] = substr( $pParamHash['title'], 0, 160 );
// Copy title to name for group verify
$pParamHash['name'] = $pParamHash['content_store']['title'];
}
} else {
$pParamHash['content_store']['title'] =( isset( $pParamHash['title'] ) )? substr( $pParamHash['title'], 0, 160 ): '';
// Copy title to name for group verify
$pParamHash['name'] = $pParamHash['content_store']['title'];
}
} else if( empty( $pParamHash['title'] ) ) {
// no name specified
$this->mErrors['title'] = tra('You must specify a name for this group.');
}
// Constrain summary to 250 to fit in groups desc table as well
if( isset($pParamHash['summary']) ) {
$pParamHash['summary'] = substr($pParamHash['summary'], 0, 250);
$pParamHash['desc'] = $pParamHash['summary'];
}
// Setup the group home URL
if( @$this->verifyId( $this->mGroupId ) ) {
$pParamHash['home'] = GROUP_PKG_URL."index.php?group_id=".$this->mGroupId;
}elseif( !empty($pParamHash['name']) ) {
$pParamHash['home'] = GROUP_PKG_URL.urlencode($pParamHash['name']);
}
// Do we have after_registration data?
if( !empty($pParamHash['after_registration']) && !empty($pParamHash['name']) ) {
$pParamHash['data_store']['after_registration'] = $pParamHash['after_registration'];
$pParamHash['after_registration_page'] = $pParamHash['home'] = GROUP_PKG_URL.'registered/'.$pParamHash['name'];
}
else if ( !empty($pParamHash['name']) ){
$pParamHash['data_store']['after_registration'] = NULL;
$pParamHash['after_registration_page'] = GROUP_PKG_URL.urlencode($pParamHash['name']);
}
$pParamHash['group_pkg_store']['view_content_public'] = !empty( $pParamHash['view_content_public'] )?$pParamHash['view_content_public']:'n';
$pParamHash['group_pkg_store']['mod_msgs'] = !empty( $pParamHash['mod_msgs'] )?$pParamHash['mod_msgs']:'n';
$pParamHash['group_pkg_store']['mod_content'] = !empty( $pParamHash['mod_content'] )?$pParamHash['mod_content']:'n';
$pParamHash['group_pkg_store']['admin_content_strict'] = !empty( $pParamHash['admin_content_strict'] )?$pParamHash['admin_content_strict']:'n';
// Make sure we don't set is_default or batch_set_default for security
if( isset($pParamHash['is_default']) || isset($pParamHash['batch_set_default']) ) {
$this->mErrors['default'] = tra('Attempt to set group as default group or batch set default. This is not allowed.');
}
// Verify the group information
$gBitUser->verifyGroup( $pParamHash );
// Merge errors from the group checks
$this->mErrors = array_merge($gBitUser->mErrors, $this->mErrors);
// if we have an error we get them all by checking parent classes for additional errors
if( count( $this->mErrors ) > 0 ){
parent::verify( $pParamHash );
}
return( count( $this->mErrors )== 0 );
}
/**
* This function removes a group entry
**/
function expunge() {
global $gBitSystem, $gBitUser;
if( $this->isValid() ) {
// before we clear out the group we need to know its board so we can clear it out too
$board = &$this->getBoard();
// before we clear out a a group we may need to nuke all its related content as well
/* Note: this is probably not perfect - we're assuming there are custom content perms if one of these is active
* which is the source of the issue. A more robust solution might allow content to be freed from its group
* something someone more ambitous and with that need can explore
*/
if( $gBitSystem->isFeatureActive( 'group_admin_content' ) || $gBitSystem->isFeatureActive('group_map_required') ){
// get all mapped content ids
$query = "SELECT `to_content_id` as `content_id` FROM `".BIT_DB_PREFIX."groups_content_cnxn_map` WHERE `group_content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ), 9999999 );
while( $res = $result->fetchRow() ) {
$groupContent[] = $res;
}
}
// delete the group and its related group pkg settings - order matters to respect constraints
$this->mDb->StartTrans();
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_invitations` WHERE `group_id` = ?";
$result = $this->mDb->query( $query, array( $this->mGroupId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_roles_perms_map` WHERE `group_content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_roles_users_map` WHERE `group_content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_content_cnxn_map` WHERE `group_content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_content_types` WHERE `group_content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
$query = "DELETE FROM `".BIT_DB_PREFIX."groups` WHERE `content_id` = ?";
$result = $this->mDb->query( $query, array( $this->mContentId ) );
// now we can expunge related content
if( !empty( $board ) ) {
// delete the associated board
$board->expunge();
}
// delete related content
if( !empty( $groupContent ) ){
foreach( $groupContent as $item ){
$obj = LibertyBase::getLibertyObject( $item['content_id'] );
// make sure the object loaded
if( is_object( $obj ) ){
$obj->expunge();
}
}
}
// expunge the parent objects
if( LibertyMime::expunge() ) {
if( $gBitUser->expungeGroup( $this->mGroupId ) ) {
$this->mDb->CompleteTrans();
}
else {
$this->mDb->RollbackTrans();
$this->mErrors['group_expunge'] = 'Users group expunge failed';
}
}
else {
$this->mDb->RollbackTrans();
$this->mErrors['group_expunge'] = 'Parent Liberty class expunge failed';
}
}
return( count( $this->mErrors ) == 0 );
}
/**
* Make sure group is loaded and valid
**/
function isValid() {
return( $this->verifyId( $this->mGroupId ) && $this->verifyId( $this->mContentId ) );
}
/**
* This function generates a list of records from the liberty_content database for use in a list page
**/
function getList( &$pParamHash ) {
global $gBitSystem, $gBitUser;
// this makes sure parameters used later on are set
LibertyContent::prepGetList( $pParamHash );
$selectSql = $joinSql = $whereSql = '';
$bindVars = array();
array_push( $bindVars, $this->mContentTypeGuid );
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, NULL, $pParamHash );
// this will set $find, $sort_mode, $max_records and $offset
extract( $pParamHash );
if( isset( $pParamHash['user_id'] )){
$joinSql .= " INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ugm ON (g.`group_id` = ugm.`group_id`)";
$whereSql .= " AND ugm.`user_id` = ?";
$bindVars[] = $pParamHash['user_id'];
}
if( isset( $pParamHash['mapped_content_id'] )){
$joinSql .= " INNER JOIN `".BIT_DB_PREFIX."groups_content_cnxn_map` gccm1 ON (g.`content_id` = gccm1.`group_content_id`)";
$whereSql .= " AND gccm1.`to_content_id` = ?";
$bindVars[] = $pParamHash['mapped_content_id'];
}
if( is_array( $find ) ) {
// you can use an array of groups
$whereSql .= " AND lc.`title` IN( ".implode( ',',array_fill( 0,count( $find ),'?' ) )." )";
$bindVars = array_merge ( $bindVars, $find );
} elseif( is_string( $find ) ) {
// or a string
$whereSql .= " AND UPPER( lc.`title` )like ? ";
$bindVars[] = '%' . strtoupper( $find ). '%';
}
$query = "SELECT g.*,
lc.`content_id`,
lc.`title`,
lc.`data`,
lcds.`data` AS `summary`,
lc.`content_type_guid`,
lct.`content_name`,
lct.`content_name_plural`,
lc.`last_modified`,
lc.`created`,
lfp.file_name AS `image_attachment_path`,
uue.`login` AS `modifier_user`,
uue.`real_name` AS `modifier_real_name`,
uue.`user_id` AS `modifier_user_id`,
uuc.`login` AS `creator_user`,
uuc.`real_name` AS `creator_real_name`,
uuc.`user_id` AS `creator_user_id`,
ug.*,
brd.`board_id`
$selectSql
FROM `".BIT_DB_PREFIX."groups` g
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = g.`content_id` )
INNER JOIN `".BIT_DB_PREFIX."liberty_content_types` lct ON (lc.`content_type_guid`=lct.`content_type_guid`)
INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON( ug.`group_id` = g.`group_id` )
INNER JOIN `".BIT_DB_PREFIX."users_users` uuc ON (lc.`user_id`=uuc.`user_id`)
INNER JOIN `".BIT_DB_PREFIX."users_users` uue ON (lc.`modifier_user_id`=uue.`user_id`)
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content_data` lcds ON (lc.`content_id` = lcds.`content_id` AND lcds.`data_type`='summary')
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` la ON( la.`content_id` = lc.`content_id` AND la.`is_primary` = 'y' )
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` lfp ON( lfp.`file_id` = la.`foreign_id` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."groups_content_cnxn_map` gccm ON ( lc.`content_id` = gccm.`group_content_id` )
INNER JOIN `".BIT_DB_PREFIX."boards` brd ON (gccm.`to_content_id` = brd.`content_id`)
$joinSql
WHERE lc.`content_type_guid` = ? $whereSql
ORDER BY ".$this->mDb->convertSortmode( $sort_mode );
$query_cant = "select count(*)
FROM `".BIT_DB_PREFIX."groups` g
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON( lc.`content_id` = g.`content_id` )
INNER JOIN `".BIT_DB_PREFIX."users_groups` ug ON( ug.`group_id` = g.`group_id` )
$joinSql
WHERE lc.`content_type_guid` = ? $whereSql";
$result = $this->mDb->query( $query, $bindVars, $max_records, $offset );
$ret = array();
while( $res = $result->fetchRow() ) {
$res['display_url'] = $this->getDisplayUrl( $res );
$res['num_members'] = $this->getMembersCount( $res['group_id'] );
$res['thumbnail_url'] = liberty_fetch_thumbnails( array( "storage_path" => $res['image_attachment_path'] ) );
$res['display_urls'] = $this->getDisplayUrls( $res );
$ret[] = $res;
}
$pParamHash["cant"] = $this->mDb->getOne( $query_cant, $bindVars );
// add all pagination info to pParamHash
LibertyContent::postGetList( $pParamHash );
return $ret;
}
/**
* Generates the URL to the group page
* @return the link to display the page.
*/
public static function getDisplayUrlFromHash( $pParamHash = NULL ) {
global $gBitSystem;
$ret = NULL;
if( !empty( $pParamHash['title'] ) ){
$groupName = $pParamHash['title'];
}
// @TODO even better would be to pass a name param in url instead of group_id
if( !empty( $pParamHash['group_id'] ) || !empty( $this->mGroupId ) ){
$groupId = $pParamHash['group_id'];
}
if( !empty( $groupName ) && $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
// let people add any old fucking char they want - not sure what kind of disaster this might be but we'll find out - 2/19/09 wjames5
$prettyGroupName = preg_replace( '/ /', '+', $groupName );
$ret = GROUP_PKG_URL.$prettyGroupName;
}elseif( !empty( $groupId ) ){
$ret = GROUP_PKG_URL."index.php?group_id=".$groupId;
}
// if all else fails try to get a value from a content id
if( empty( $ret ) ){
$contentId = !empty( $pContentId )?$pContentId:( !empty( $this->mContentId )?$this->mContentId:NULL );
$ret = LibertyContent::getDisplayUrlFromHash( $pParamHash );
}
return $ret;
}
/**
* Generates various urls for a group
* @return a hash of urls
*/
function getDisplayUrls( $pParamHash = NULL ) {
global $gBitSystem;
$ret = NULL;
if( !empty( $pParamHash['title'] ) || !empty( $this->mInfo['title'] ) ){
$groupName = !empty( $pParamHash['title'] )?$pParamHash['title']:$this->mInfo['title'];
}
// @TODO even better would be to pass a name param in url instead of group_id
if( !empty( $pParamHash['group_id'] ) || !empty( $this->mGroupId ) ){
$groupId = !empty( $pParamHash['group_id'] )?$pParamHash['group_id']:$this->mGroupId;
}
if( !empty( $groupName ) && $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
// let people add any old fucking char they want - not sure what kind of disaster this might be but we'll find out - 2/19/09 wjames5
$prettyGroupName = preg_replace( '/ /', '+', $groupName );
$ret['summary'] = GROUP_PKG_URL.$prettyGroupName;
$ret['about'] = GROUP_PKG_URL.$prettyGroupName."/about";
$ret['files'] = GROUP_PKG_URL.$prettyGroupName."/files";
$ret['manage'] = GROUP_PKG_URL.$prettyGroupName."/manage";
$ret['members'] = GROUP_PKG_URL.$prettyGroupName."/members";
$ret['settings'] = GROUP_PKG_URL.$prettyGroupName."/settings";
$ret['tasks'] = GROUP_PKG_URL.$prettyGroupName."/tasks";
$ret['theme'] = GROUP_PKG_URL.$prettyGroupName."/theme";
$ret['join'] = GROUP_PKG_URL.$prettyGroupName."/join";
}elseif( !empty( $groupId ) ){
$ret['summary'] = GROUP_PKG_URL."index.php?group_id=".$groupId;
$ret['about'] = GROUP_PKG_URL."about.php?group_id=".$groupId;
$ret['files'] = GROUP_PKG_URL."files.php?group_id=".$groupId;
$ret['manage'] = GROUP_PKG_URL."manage.php?group_id=".$groupId;
$ret['members'] = GROUP_PKG_URL."members.php?group_id=".$groupId;
$ret['settings'] = GROUP_PKG_URL."edit.php?group_id=".$groupId;
$ret['tasks'] = GROUP_PKG_URL."tasks.php?group_id=".$groupId;
$ret['theme'] = GROUP_PKG_URL."theme.php?group_id=".$groupId;
$ret['join'] = GROUP_PKG_URL."join.php?group_id=".$groupId;
}
return $ret;
}
public static function getTitleFromHash( $pParamHash = NULL, $pContentId = NULL ){
$ret = NULL;
if( @BitBase::verifyId( $pContentId ) ){
global $gBitDb;
$ret = $gBitDb->getOne( "SELECT lc.`title` FROM `".BIT_DB_PREFIX."liberty_content` lc WHERE lc.`content_id` = ? AND lc.`content_type_guid` = ?", array($pContentId, 'bitgroup' ) );
}else{
$ret = parent::getTitle ( $pParamHash );
}
return $ret;
}
// -------------------- Group Roles Funtions -------------------- //
function getRoles() {
$sql = "SELECT gr.* FROM `".BIT_DB_PREFIX."groups_roles` gr
ORDER BY gr.`role_name` ASC";
$ret = array();
if ( $roles = $this->mDb->query( $sql ) ){
while( $row = $roles->fetchRow() ) {
$roleId = $row['role_id'];
$ret[$roleId] = $row;
$ret[$roleId]['perms'] = array();
if ( @BitBase::verifyId( $this->mContentId ) ){
$ret[$roleId]['perms'] = $this->getRolesPerms( array( 'content_id' => $this->mContentId, 'role_id' => $roleId ));
}
}
}
return $ret;
}
/**
* @param array group_content_id, if unset, all role perm types are returned
**/
function getRolesPerms( $pParamHash = NULL ) {
$result = array();
$bindVars = array();
$whereSql = $selectSql = $fromSql = '';
if( @BitBase::verifyId( $pParamHash['content_id'] )) {
// $selectSql = ', rp.`perm_name` AS `hasPerm` ';
$fromSql = ' INNER JOIN `'.BIT_DB_PREFIX.'groups_roles_perms_map` rp ON ( rp.`perm_name` = gp.`perm_name` ) ';
$whereSql .= " WHERE rp.`group_content_id`=? AND rp.`role_id` = ?";
$bindVars[] = $pParamHash['content_id'];
$bindVars[] = $pParamHash['role_id'];
}
$sql = "SELECT gp.* $selectSql
FROM `".BIT_DB_PREFIX."groups_permissions` gp $fromSql $whereSql
ORDER BY gp.`perm_name` ASC";
$result = $this->mDb->getAssoc( $sql, $bindVars );
return $result;
}
function assignPermissionToRole( $perm, $pRoleId ) {
if( $this->isValid() ) {
$this->removePermissionFromRole( $perm, $pRoleId );
$query = "INSERT INTO `".BIT_DB_PREFIX."groups_roles_perms_map`( `perm_name`, `role_id`, `group_content_id`, `group_id` ) VALUES(?, ?, ?, ?)";
$result = $this->mDb->query($query, array($perm, $pRoleId, $this->mContentId, $this->mGroupId));
return TRUE;
}
return FALSE;
}
function removePermissionFromRole( $perm, $pRoleId ) {
if( $this->isValid() ) {
$query = "delete from `".BIT_DB_PREFIX."groups_roles_perms_map` where `perm_name` = ? and `role_id` = ? and `group_content_id` = ?";
$result = $this->mDb->query($query, array($perm, $pRoleId, $this->mContentId));
return TRUE;
}
return FALSE;
}
function assignUserRoleToGroup( $pRoleId, $pUserId ){
if( $this->isValid() ) {
$this->removeUserRoleFromGroup( $pRoleId, $pUserId );
$query = "INSERT INTO `".BIT_DB_PREFIX."groups_roles_users_map`( `role_id`, `user_id`, `group_content_id`, `group_id` ) VALUES(?, ?, ?, ?)";
$result = $this->mDb->query($query, array($pRoleId, $pUserId, $this->mContentId, $this->mGroupId));
return TRUE;
}
return FALSE;
}
function removeUserRoleFromGroup( $pRoleId, $pUserId ){
if( $this->isValid() ) {
$query = "delete from `".BIT_DB_PREFIX."groups_roles_users_map` where `role_id` = ? and `user_id` = ? and `group_content_id` = ?";
$result = $this->mDb->query($query, array($pRoleId, $pUserId, $this->mContentId));
return TRUE;
}
return FALSE;
}
/**
* getMemberRolesAndPermsForGroup
* this gets the permissions the user has for their roles in the group.
* perms here should not be confused with perms in users package,
* and the pemissions check routines in liberty, they are unrelated
**/
function getMemberRolesAndPermsForGroup(){
global $gBitUser;
if ( $this->verifyId( $this->mContentId ) ){
$this->mGroupMemberRoles = array();
$this->mGroupMemberPermissions = array();
// Load up the roles for this user
$roles = $this->mDb->getArray( "SELECT `role_id` from `".BIT_DB_PREFIX."groups_roles_users_map` WHERE `group_content_id` = ? AND user_id = ?", array($this->mContentId, $gBitUser->mUserId));
foreach ( $roles as $role ){
$this->mGroupMemberRoles[] = $role['role_id'];
}
// Are they a member as well?
if ( array_key_exists((int)$this->mGroupId, $gBitUser->mGroups ) ) {
$this->mGroupMemberRoles[] = GROUPS_ROLE_MEMBER;
}
// Now figure which set of permissions to load
if ( in_array(GROUPS_ROLE_ADMIN, $this->mGroupMemberRoles) ) {
// We might consider dropping this one and just check admin role.
$this->mGroupMemberPermissions = $this->mDb->getArray("SELECT perm_name FROM `".BIT_DB_PREFIX."groups_permissions`");
} elseif( !empty( $this->mGroupMemberRoles ) ){
$query = "SELECT DISTINCT(rp.`perm_name`)
FROM `".BIT_DB_PREFIX."groups_roles_perms_map` rp
WHERE rp.`group_content_id` = ? AND rp.`role_id` IN (".implode( ',',array_fill( 0,count( $this->mGroupMemberRoles ),'?' ) )." )";
$bindVars[] = $this->mContentId;
$bindVars = array_merge($bindVars, $this->mGroupMemberRoles);
$this->mGroupMemberPermissions = $this->mDb->getArray($query, $bindVars);
}
}
}
function getMembers(){
$ret = array();
if ( $this->verifyId( $this->mGroupId ) ){
$query = "SELECT uu.`user_id` AS hash_key, uu.`login`, uu.`real_name`, uu.`user_id`, uu.`email`
FROM `".BIT_DB_PREFIX."users_users` uu
INNER JOIN `".BIT_DB_PREFIX."users_groups_map` ug ON (uu.`user_id`=ug.`user_id`)
WHERE `group_id`=?";
$bindVars = array( $this->mGroupId );
if ( $users = $this->mDb->query( $query, $bindVars ) ){
while( $row = $users->fetchRow() ) {
$login = $row['login'];
$ret[$login] = $row;
$ret[$login]['roles'] = array();
if ( @BitBase::verifyId( $this->mContentId ) ){
$ret[$login]['roles'] = $this->getMemberRoles( $row['user_id'] );
}
}
}
}
return $ret;
}
/* getMembersCount
* gets the number of members in a group.
*/
function getMembersCount( $pGroupId ){
$result = NULL;
if( @$this->verifyId( $pGroupId ) ) {
$memberCantSql = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."users_groups_map` WHERE `group_id` = ?";
$result = $this->mDb->getOne($memberCantSql, array( $pGroupId ));
}
return $result;
}
function getMemberRoles( $pUserId ){
$ret = array();
if ( !empty($pUserId) && $this->verifyId( $this->mContentId ) ){
$query = "SELECT ru.`role_id` from `".BIT_DB_PREFIX."groups_roles_users_map` ru WHERE ru.`group_content_id` = ? AND ru.`user_id` = ? ORDER BY ru.`role_id` ASC";
$bindVars = array ( $this->mContentId , $pUserId );
$result = $this->mDb->getArray( $query, $bindVars );
foreach( $result as $role ){
$ret[] = $role['role_id'];
}
}
return $ret;
}
// -------------------- End Group Roles Funtions -------------------- //
// -------------------- Member Email Funtions -------------------- //
function storeUserEmailPref( $pPref, $pUser=NULL ){
global $gBitSystem, $gBitUser, $gSwitchboardSystem;
// if user is NULL get the active one
if ( !is_object( $pUser ) ){
$pUser = &$gBitUser;
}
// @TODO add errors if mailman or switchboard updates fail
$errors = array();
// if the content is this and its board has a mailing list then act on that
if ( ($board = $this->getBoard()) && $board->getPreference( 'boards_mailing_list' ) ){
require_once( UTIL_PKG_PATH.'mailman_lib.php' );
if( $pPref != 'none' ) {
mailman_addmember( $board->getPreference( 'boards_mailing_list' ), $pUser->getField( 'email' ), $pPref );
} elseif( $pPref == 'none' ) {
mailman_remove_member( $board->getPreference( 'boards_mailing_list' ), $pUser->getField( 'email' ) );
}
// @TODO - this is may be the future - but don't change it until ok from wjames5 or after april 1 2009
$gSwitchboardSystem->storeUserPref($pUser->mUserId, 'group', 'message', $this->mContentId, $pPref);
}
// no mailing list then store in switchboard
elseif ( $gBitSystem->isPackageActive('switchboard') ) {
$gSwitchboardSystem->storeUserPref($pUser->mUserId, 'group', 'message', $this->mContentId, $pPref);
}
return( count( $errors ) == 0 );
}
// dump the users email prefs
function deleteUserEmailPref( $pUser=NULL ){
global $gBitSystem, $gBitUser;
// if user is NULL get the active one
if ( !is_object( $pUser ) ){
$pUser = &$gBitUser;
}
// if this group has a board which has a mailing list then act on that
if ( ($board = $this->getBoard()) && $board->getPreference( 'boards_mailing_list' ) ){
require_once( UTIL_PKG_PATH.'mailman_lib.php' );
mailman_remove_member( $board->getPreference( 'boards_mailing_list' ), $pUser->getField( 'email' ) );
}
// if no mailing list delete from switchboard
elseif ( $gBitSystem->isPackageActive('switchboard') ) {
global $gSwitchboardSystem;
$gSwitchboardSystem->deleteUserPref($pUser->mUserId, 'group', 'message', $this->mContentId );
}
}
function getUserEmailPref( $pUser=NULL ){
global $gBitSystem, $gBitUser;
// if user is NULL get the active one
if ( !is_object( $pUser ) ){
$pUser = &$gBitUser;
}
// if this group has a board which has a mailing list then get prefs from it
if ( ($board = $this->getBoard()) && $board->getPreference( 'boards_mailing_list' ) ){
require_once( UTIL_PKG_PATH.'mailman_lib.php' );
if( $rslt = mailman_getsubscriptiontype($board->getPreference('boards_mailing_list'),$gBitUser->getField('email')) ){
$ret = $rslt[0];
}else{
$ret = 'none';
}
}
// no mailing list then get pref from switchboard
elseif ( $gBitSystem->isPackageActive('switchboard') ) {
global $gSwitchboardSystem;
if ( ($rslt = $gSwitchboardSystem->loadContentPrefs( $pUser->mUserId, $this->mContentId ) ) ){
$ret = $rslt[0]['delivery_style'];
}
else {
$ret = 'none';
}
}
return $ret;
}
// -------------------- END Member Email Funtions -------------------- //
// -------------------- Member Invitation Funtions -------------------- //
function verifyInvitation( &$pParamHash ){
$errors = array();
if( @$this->verifyId( $pParamHash['group_id'] ) ) {
$pParamHash['invite_store']['group_id'] = $pParamHash['group_id'];
}else{
$errors['group_id'] = "Invalid Group Id.";
}
if( !empty( $pParamHash['email'] ) ) {
$pParamHash['invite_store']['email'] = $pParamHash['email'];
}else{
$errors['email'] = "No email address was set.";
}
$pParamHash['invite_store']['invite_id'] = $this->genInviteId();
return( count( $errors ) == 0 );
}
function genInviteId(){
global $gBitUser;
$inviteId = $gBitUser->genPass(32);
// lets make sure its unique
if ( $this->mDb->getOne( "SELECT `invite_id` FROM `".BIT_DB_PREFIX."groups_invitations` WHERE `invite_id` = ?", array( $inviteId ) ) ){
// if its already in use lets try again
$inviteId = $this->genInviteId();
}
return $inviteId;
}
function storeInvitation( &$pParamHash ){
$result = FALSE;
if( $this->verifyInvitation( $pParamHash ) ) {
$this->mDb->StartTrans();
$this->mDb->associateInsert( BIT_DB_PREFIX."groups_invitations", $pParamHash['invite_store'] );
$this->mDb->CompleteTrans();
// re-query to confirm results
$result = $this->getInvitation( $pParamHash['invite_store']['invite_id'] );
}
return $result;
}
function getInvitation( &$pInviteId ){
$ret = FALSE;
if ( isset( $pInviteId ) ){
$bindVars = array( $pInviteId );
$query = "SELECT gi.*, uu.`user_id`, uu.`login`, uu.`real_name`
FROM `".BIT_DB_PREFIX."groups_invitations` gi
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`email` = gi.`email`)
WHERE `invite_id`=?";
$result = $this->mDb->query( $query, $bindVars );
if( $result && $result->numRows() ) {
$ret = $result->fields;
}
}
return $ret;
}
function getInvitationsList(){
$ret = array();
if( $this->isValid() ) {
$bindVars = array( 'group_id' => $this->mGroupId );
$query = "SELECT DISTINCT gi.`email`, gi.`invite_id`, uu.`user_id`, uu.`login`, uu.`real_name`
FROM `".BIT_DB_PREFIX."groups_invitations` gi
LEFT OUTER JOIN `".BIT_DB_PREFIX."users_users` uu ON (uu.`email` = gi.`email`)
WHERE gi.`group_id` = ? ORDER BY uu.`real_name`, uu.`login`, gi.`email`";
$result = $this->mDb->query( $query, $bindVars );
while ($res = $result->fetchrow()) {
$ret[] = $res;
};
}
return $ret;
}
function expungeInvitation( &$pInviteId ){
$return = FALSE;
if ( $this->isValid() && isset( $pInviteId ) ){
$bindVars = array( $pInviteId );
if ( $this->mDb->getOne("SELECT `invite_id` FROM `".BIT_DB_PREFIX."groups_invitations` WHERE `invite_id`=?", $bindVars ) ){
$query = "DELETE FROM `".BIT_DB_PREFIX."groups_invitations` WHERE `invite_id`=?";
$result = $this->mDb->query($query, $bindVars);
$return = TRUE;
}
}
return $return;
}
// -------------------- End Member Invitation Funtions -------------------- //
// -------------------- Content Mapping Funtions -------------------- //
/**
* linkContent
*
* @access public
* @return if errors
**/
function linkContent( $pParamHash ) {
if( $this->isValid() && isset( $pParamHash['content_id'] ) && $this->verifyId( $pParamHash['content_id'] ) ){