comparison forum/Sources/ManageMembergroups.php @ 76:e3e11437ecea website

Add forum code
author Chris Cannam
date Sun, 07 Jul 2013 11:25:48 +0200
parents
children
comparison
equal deleted inserted replaced
75:72f59aa7e503 76:e3e11437ecea
1 <?php
2
3 /**
4 * Simple Machines Forum (SMF)
5 *
6 * @package SMF
7 * @author Simple Machines http://www.simplemachines.org
8 * @copyright 2011 Simple Machines
9 * @license http://www.simplemachines.org/about/smf/license.php BSD
10 *
11 * @version 2.0
12 */
13
14 if (!defined('SMF'))
15 die('Hacking attempt...');
16
17 /* This file is concerned with anything in the Manage Membergroups screen.
18
19 void ModifyMembergroups()
20 - entrance point of the 'Manage Membergroups' center.
21 - called by ?action=admin;area=membergroups.
22 - loads the ManageMembergroups template.
23 - loads the MangeMembers language file.
24 - requires the manage_membergroups or the admin_forum permission.
25 - calls a function based on the given subaction.
26 - defaults to sub action 'index' or without manage_membergroup
27 permissions to 'settings'.
28
29 void MembergroupIndex()
30 - shows an overview of the current membergroups.
31 - called by ?action=admin;area=membergroups.
32 - requires the manage_membergroups permission.
33 - uses the main ManageMembergroups template.
34 - splits the membergroups in regular ones and post count based groups.
35 - also counts the number of members part of each membergroup.
36
37 void AddMembergroup()
38 - allows to add a membergroup and set some initial properties.
39 - called by ?action=admin;area=membergroups;sa=add.
40 - requires the manage_membergroups permission.
41 - uses the new_group sub template of ManageMembergroups.
42 - allows to use a predefined permission profile or copy one from
43 another group.
44 - redirects to action=admin;area=membergroups;sa=edit;group=x.
45
46 void DeleteMembergroup()
47 - deletes a membergroup by URL.
48 - called by ?action=admin;area=membergroups;sa=delete;group=x;session_var=y.
49 - requires the manage_membergroups permission.
50 - redirects to ?action=admin;area=membergroups.
51
52 void EditMembergroup()
53 - screen to edit a specific membergroup.
54 - called by ?action=admin;area=membergroups;sa=edit;group=x.
55 - requires the manage_membergroups permission.
56 - uses the edit_group sub template of ManageMembergroups.
57 - also handles the delete button of the edit form.
58 - redirects to ?action=admin;area=membergroups.
59
60 void ModifyMembergroupsettings()
61 - set some general membergroup settings and permissions.
62 - called by ?action=admin;area=membergroups;sa=settings
63 - requires the admin_forum permission (and manage_permissions for
64 changing permissions)
65 - uses membergroup_settings sub template of ManageMembergroups.
66 - redirects to itself.
67 */
68
69 // The entrance point for all 'Manage Membergroup' actions.
70 function ModifyMembergroups()
71 {
72 global $context, $txt, $scripturl, $sourcedir;
73
74 $subActions = array(
75 'add' => array('AddMembergroup', 'manage_membergroups'),
76 'delete' => array('DeleteMembergroup', 'manage_membergroups'),
77 'edit' => array('EditMembergroup', 'manage_membergroups'),
78 'index' => array('MembergroupIndex', 'manage_membergroups'),
79 'members' => array('MembergroupMembers', 'manage_membergroups', 'Groups.php'),
80 'settings' => array('ModifyMembergroupsettings', 'admin_forum'),
81 );
82
83 // Default to sub action 'index' or 'settings' depending on permissions.
84 $_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : (allowedTo('manage_membergroups') ? 'index' : 'settings');
85
86 // Is it elsewhere?
87 if (isset($subActions[$_REQUEST['sa']][2]))
88 require_once($sourcedir . '/' . $subActions[$_REQUEST['sa']][2]);
89
90 // Do the permission check, you might not be allowed her.
91 isAllowedTo($subActions[$_REQUEST['sa']][1]);
92
93 // Language and template stuff, the usual.
94 loadLanguage('ManageMembers');
95 loadTemplate('ManageMembergroups');
96
97 // Setup the admin tabs.
98 $context[$context['admin_menu_name']]['tab_data'] = array(
99 'title' => $txt['membergroups_title'],
100 'help' => 'membergroups',
101 'description' => $txt['membergroups_description'],
102 );
103
104 // Call the right function.
105 $subActions[$_REQUEST['sa']][0]();
106 }
107
108 // An overview of the current membergroups.
109 function MembergroupIndex()
110 {
111 global $txt, $scripturl, $context, $settings, $smcFunc, $sourcedir;
112
113 $context['page_title'] = $txt['membergroups_title'];
114
115 // The first list shows the regular membergroups.
116 $listOptions = array(
117 'id' => 'regular_membergroups_list',
118 'title' => $txt['membergroups_regular'],
119 'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort2']) ? ';sort2=' . urlencode($_REQUEST['sort2']) : ''),
120 'default_sort_col' => 'name',
121 'get_items' => array(
122 'file' => $sourcedir . '/Subs-Membergroups.php',
123 'function' => 'list_getMembergroups',
124 'params' => array(
125 'regular',
126 ),
127 ),
128 'columns' => array(
129 'name' => array(
130 'header' => array(
131 'value' => $txt['membergroups_name'],
132 ),
133 'data' => array(
134 'function' => create_function('$rowData', '
135 global $scripturl;
136
137 // Since the moderator group has no explicit members, no link is needed.
138 if ($rowData[\'id_group\'] == 3)
139 $group_name = $rowData[\'group_name\'];
140 else
141 {
142 $color_style = empty($rowData[\'online_color\']) ? \'\' : sprintf(\' style="color: %1$s;"\', $rowData[\'online_color\']);
143 $group_name = sprintf(\'<a href="%1$s?action=admin;area=membergroups;sa=members;group=%2$d"%3$s>%4$s</a>\', $scripturl, $rowData[\'id_group\'], $color_style, $rowData[\'group_name\']);
144 }
145
146 // Add a help option for moderator and administrator.
147 if ($rowData[\'id_group\'] == 1)
148 $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_administrator" onclick="return reqWin(this.href);">?</a>)\', $scripturl);
149 elseif ($rowData[\'id_group\'] == 3)
150 $group_name .= sprintf(\' (<a href="%1$s?action=helpadmin;help=membergroup_moderator" onclick="return reqWin(this.href);">?</a>)\', $scripturl);
151
152 return $group_name;
153 '),
154 ),
155 'sort' => array(
156 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, group_name',
157 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, group_name DESC',
158 ),
159 ),
160 'stars' => array(
161 'header' => array(
162 'value' => $txt['membergroups_stars'],
163 ),
164 'data' => array(
165 'function' => create_function('$rowData', '
166 global $settings;
167
168 $stars = explode(\'#\', $rowData[\'stars\']);
169
170 // In case no stars are setup, return with nothing
171 if (empty($stars[0]) || empty($stars[1]))
172 return \'\';
173
174 // Otherwise repeat the image a given number of times.
175 else
176 {
177 $image = sprintf(\'<img src="%1$s/%2$s" alt="*" />\', $settings[\'images_url\'], $stars[1]);
178 return str_repeat($image, $stars[0]);
179 }
180 '),
181
182 ),
183 'sort' => array(
184 'default' => 'stars',
185 'reverse' => 'stars DESC',
186 )
187 ),
188 'members' => array(
189 'header' => array(
190 'value' => $txt['membergroups_members_top'],
191 ),
192 'data' => array(
193 'function' => create_function('$rowData', '
194 global $txt;
195
196 // No explicit members for the moderator group.
197 return $rowData[\'id_group\'] == 3 ? $txt[\'membergroups_guests_na\'] : $rowData[\'num_members\'];
198 '),
199 'style' => 'text-align: center',
200 ),
201 'sort' => array(
202 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, 1',
203 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, 1 DESC',
204 ),
205 ),
206 'modify' => array(
207 'header' => array(
208 'value' => $txt['modify'],
209 ),
210 'data' => array(
211 'sprintf' => array(
212 'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
213 'params' => array(
214 'id_group' => false,
215 ),
216 ),
217 'style' => 'text-align: center',
218 ),
219 ),
220 ),
221 'additional_rows' => array(
222 array(
223 'position' => 'below_table_data',
224 'value' => '[<a href="' . $scripturl . '?action=admin;area=membergroups;sa=add;generalgroup">' . $txt['membergroups_add_group'] . '</a>]',
225 ),
226 ),
227 );
228
229 require_once($sourcedir . '/Subs-List.php');
230 createList($listOptions);
231
232 // The second list shows the post count based groups.
233 $listOptions = array(
234 'id' => 'post_count_membergroups_list',
235 'title' => $txt['membergroups_post'],
236 'base_href' => $scripturl . '?action=admin;area=membergroups' . (isset($_REQUEST['sort']) ? ';sort=' . urlencode($_REQUEST['sort']) : ''),
237 'default_sort_col' => 'required_posts',
238 'request_vars' => array(
239 'sort' => 'sort2',
240 'desc' => 'desc2',
241 ),
242 'get_items' => array(
243 'file' => $sourcedir . '/Subs-Membergroups.php',
244 'function' => 'list_getMembergroups',
245 'params' => array(
246 'post_count',
247 ),
248 ),
249 'columns' => array(
250 'name' => array(
251 'header' => array(
252 'value' => $txt['membergroups_name'],
253 ),
254 'data' => array(
255 'function' => create_function('$rowData', '
256 global $scripturl;
257
258 $colorStyle = empty($rowData[\'online_color\']) ? \'\' : sprintf(\' style="color: %1$s;"\', $rowData[\'online_color\']);
259 return sprintf(\'<a href="%1$s?action=moderate;area=viewgroups;sa=members;group=%2$d"%3$s>%4$s</a>\', $scripturl, $rowData[\'id_group\'], $colorStyle, $rowData[\'group_name\']);
260 '),
261 ),
262 'sort' => array(
263 'default' => 'group_name',
264 'reverse' => 'group_name DESC',
265 ),
266 ),
267 'stars' => array(
268 'header' => array(
269 'value' => $txt['membergroups_stars'],
270 ),
271 'data' => array(
272 'function' => create_function('$rowData', '
273 global $settings;
274
275 $stars = explode(\'#\', $rowData[\'stars\']);
276
277 if (empty($stars[0]) || empty($stars[1]))
278 return \'\';
279 else
280 {
281 $star_image = sprintf(\'<img src="%1$s/%2$s" alt="*" />\', $settings[\'images_url\'], $stars[1]);
282 return str_repeat($star_image, $stars[0]);
283 }
284 '),
285 ),
286 'sort' => array(
287 'default' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, stars',
288 'reverse' => 'CASE WHEN id_group < 4 THEN id_group ELSE 4 END, stars DESC',
289 )
290 ),
291 'members' => array(
292 'header' => array(
293 'value' => $txt['membergroups_members_top'],
294 ),
295 'data' => array(
296 'db' => 'num_members',
297 'style' => 'text-align: center',
298 ),
299 'sort' => array(
300 'default' => '1 DESC',
301 'reverse' => '1',
302 ),
303 ),
304 'required_posts' => array(
305 'header' => array(
306 'value' => $txt['membergroups_min_posts'],
307 ),
308 'data' => array(
309 'db' => 'min_posts',
310 'style' => 'text-align: center',
311 ),
312 'sort' => array(
313 'default' => 'min_posts',
314 'reverse' => 'min_posts DESC',
315 ),
316 ),
317 'modify' => array(
318 'header' => array(
319 'value' => $txt['modify'],
320 ),
321 'data' => array(
322 'sprintf' => array(
323 'format' => '<a href="' . $scripturl . '?action=admin;area=membergroups;sa=edit;group=%1$d">' . $txt['membergroups_modify'] . '</a>',
324 'params' => array(
325 'id_group' => false,
326 ),
327 ),
328 'style' => 'text-align: center',
329 ),
330 ),
331 ),
332 'additional_rows' => array(
333 array(
334 'position' => 'below_table_data',
335 'value' => '[<a href="' . $scripturl . '?action=admin;area=membergroups;sa=add;postgroup">' . $txt['membergroups_add_group'] . '</a>]',
336 ),
337 ),
338 );
339
340 createList($listOptions);
341 }
342
343 // Add a membergroup.
344 function AddMembergroup()
345 {
346 global $context, $txt, $sourcedir, $modSettings, $smcFunc;
347
348 // A form was submitted, we can start adding.
349 if (!empty($_POST['group_name']))
350 {
351 checkSession();
352
353 $postCountBasedGroup = isset($_POST['min_posts']) && (!isset($_POST['postgroup_based']) || !empty($_POST['postgroup_based']));
354 $_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
355
356 // !!! Check for members with same name too?
357
358 $request = $smcFunc['db_query']('', '
359 SELECT MAX(id_group)
360 FROM {db_prefix}membergroups',
361 array(
362 )
363 );
364 list ($id_group) = $smcFunc['db_fetch_row']($request);
365 $smcFunc['db_free_result']($request);
366 $id_group++;
367
368 $smcFunc['db_insert']('',
369 '{db_prefix}membergroups',
370 array(
371 'id_group' => 'int', 'description' => 'string', 'group_name' => 'string-80', 'min_posts' => 'int',
372 'stars' => 'string', 'online_color' => 'string', 'group_type' => 'int',
373 ),
374 array(
375 $id_group, '', $_POST['group_name'], ($postCountBasedGroup ? (int) $_POST['min_posts'] : '-1'),
376 '1#star.gif', '', $_POST['group_type'],
377 ),
378 array('id_group')
379 );
380
381 // Update the post groups now, if this is a post group!
382 if (isset($_POST['min_posts']))
383 updateStats('postgroups');
384
385 // You cannot set permissions for post groups if they are disabled.
386 if ($postCountBasedGroup && empty($modSettings['permission_enable_postgroups']))
387 $_POST['perm_type'] = '';
388
389 if ($_POST['perm_type'] == 'predefined')
390 {
391 // Set default permission level.
392 require_once($sourcedir . '/ManagePermissions.php');
393 setPermissionLevel($_POST['level'], $id_group, 'null');
394 }
395 // Copy or inherit the permissions!
396 elseif ($_POST['perm_type'] == 'copy' || $_POST['perm_type'] == 'inherit')
397 {
398 $copy_id = $_POST['perm_type'] == 'copy' ? (int) $_POST['copyperm'] : (int) $_POST['inheritperm'];
399
400 // Are you a powerful admin?
401 if (!allowedTo('admin_forum'))
402 {
403 $request = $smcFunc['db_query']('', '
404 SELECT group_type
405 FROM {db_prefix}membergroups
406 WHERE id_group = {int:copy_from}
407 LIMIT {int:limit}',
408 array(
409 'copy_from' => $copy_id,
410 'limit' => 1,
411 )
412 );
413 list ($copy_type) = $smcFunc['db_fetch_row']($request);
414 $smcFunc['db_free_result']($request);
415
416 // Protected groups are... well, protected!
417 if ($copy_type == 1)
418 fatal_lang_error('membergroup_does_not_exist');
419 }
420
421 // Don't allow copying of a real priviledged person!
422 require_once($sourcedir . '/ManagePermissions.php');
423 loadIllegalPermissions();
424
425 $request = $smcFunc['db_query']('', '
426 SELECT permission, add_deny
427 FROM {db_prefix}permissions
428 WHERE id_group = {int:copy_from}',
429 array(
430 'copy_from' => $copy_id,
431 )
432 );
433 $inserts = array();
434 while ($row = $smcFunc['db_fetch_assoc']($request))
435 {
436 if (empty($context['illegal_permissions']) || !in_array($row['permission'], $context['illegal_permissions']))
437 $inserts[] = array($id_group, $row['permission'], $row['add_deny']);
438 }
439 $smcFunc['db_free_result']($request);
440
441 if (!empty($inserts))
442 $smcFunc['db_insert']('insert',
443 '{db_prefix}permissions',
444 array('id_group' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
445 $inserts,
446 array('id_group', 'permission')
447 );
448
449 $request = $smcFunc['db_query']('', '
450 SELECT id_profile, permission, add_deny
451 FROM {db_prefix}board_permissions
452 WHERE id_group = {int:copy_from}',
453 array(
454 'copy_from' => $copy_id,
455 )
456 );
457 $inserts = array();
458 while ($row = $smcFunc['db_fetch_assoc']($request))
459 $inserts[] = array($id_group, $row['id_profile'], $row['permission'], $row['add_deny']);
460 $smcFunc['db_free_result']($request);
461
462 if (!empty($inserts))
463 $smcFunc['db_insert']('insert',
464 '{db_prefix}board_permissions',
465 array('id_group' => 'int', 'id_profile' => 'int', 'permission' => 'string', 'add_deny' => 'int'),
466 $inserts,
467 array('id_group', 'id_profile', 'permission')
468 );
469
470 // Also get some membergroup information if we're copying and not copying from guests...
471 if ($copy_id > 0 && $_POST['perm_type'] == 'copy')
472 {
473 $request = $smcFunc['db_query']('', '
474 SELECT online_color, max_messages, stars
475 FROM {db_prefix}membergroups
476 WHERE id_group = {int:copy_from}
477 LIMIT 1',
478 array(
479 'copy_from' => $copy_id,
480 )
481 );
482 $group_info = $smcFunc['db_fetch_assoc']($request);
483 $smcFunc['db_free_result']($request);
484
485 // ...and update the new membergroup with it.
486 $smcFunc['db_query']('', '
487 UPDATE {db_prefix}membergroups
488 SET
489 online_color = {string:online_color},
490 max_messages = {int:max_messages},
491 stars = {string:stars}
492 WHERE id_group = {int:current_group}',
493 array(
494 'max_messages' => $group_info['max_messages'],
495 'current_group' => $id_group,
496 'online_color' => $group_info['online_color'],
497 'stars' => $group_info['stars'],
498 )
499 );
500 }
501 // If inheriting say so...
502 elseif ($_POST['perm_type'] == 'inherit')
503 {
504 $smcFunc['db_query']('', '
505 UPDATE {db_prefix}membergroups
506 SET id_parent = {int:copy_from}
507 WHERE id_group = {int:current_group}',
508 array(
509 'copy_from' => $copy_id,
510 'current_group' => $id_group,
511 )
512 );
513 }
514 }
515
516 // Make sure all boards selected are stored in a proper array.
517 $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
518 foreach ($_POST['boardaccess'] as $key => $value)
519 $_POST['boardaccess'][$key] = (int) $value;
520
521 // Only do this if they have special access requirements.
522 if (!empty($_POST['boardaccess']))
523 $smcFunc['db_query']('', '
524 UPDATE {db_prefix}boards
525 SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
526 WHERE id_board IN ({array_int:board_list})',
527 array(
528 'board_list' => $_POST['boardaccess'],
529 'blank_string' => '',
530 'group_id_string' => (string) $id_group,
531 'comma_group' => ',' . $id_group,
532 )
533 );
534
535 // If this is joinable then set it to show group membership in people's profiles.
536 if (empty($modSettings['show_group_membership']) && $_POST['group_type'] > 1)
537 updateSettings(array('show_group_membership' => 1));
538
539 // Rebuild the group cache.
540 updateSettings(array(
541 'settings_updated' => time(),
542 ));
543
544 // We did it.
545 logAction('add_group', array('group' => $_POST['group_name']), 'admin');
546
547 // Go change some more settings.
548 redirectexit('action=admin;area=membergroups;sa=edit;group=' . $id_group);
549 }
550
551 // Just show the 'add membergroup' screen.
552 $context['page_title'] = $txt['membergroups_new_group'];
553 $context['sub_template'] = 'new_group';
554 $context['post_group'] = isset($_REQUEST['postgroup']);
555 $context['undefined_group'] = !isset($_REQUEST['postgroup']) && !isset($_REQUEST['generalgroup']);
556 $context['allow_protected'] = allowedTo('admin_forum');
557
558 $result = $smcFunc['db_query']('', '
559 SELECT id_group, group_name
560 FROM {db_prefix}membergroups
561 WHERE (id_group > {int:moderator_group} OR id_group = {int:global_mod_group})' . (empty($modSettings['permission_enable_postgroups']) ? '
562 AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
563 AND group_type != {int:is_protected}') . '
564 ORDER BY min_posts, id_group != {int:global_mod_group}, group_name',
565 array(
566 'moderator_group' => 3,
567 'global_mod_group' => 2,
568 'min_posts' => -1,
569 'is_protected' => 1,
570 )
571 );
572 $context['groups'] = array();
573 while ($row = $smcFunc['db_fetch_assoc']($result))
574 $context['groups'][] = array(
575 'id' => $row['id_group'],
576 'name' => $row['group_name']
577 );
578 $smcFunc['db_free_result']($result);
579
580 $result = $smcFunc['db_query']('', '
581 SELECT id_board, name, child_level
582 FROM {db_prefix}boards
583 ORDER BY board_order',
584 array(
585 )
586 );
587 $context['boards'] = array();
588 while ($row = $smcFunc['db_fetch_assoc']($result))
589 $context['boards'][] = array(
590 'id' => $row['id_board'],
591 'name' => $row['name'],
592 'child_level' => $row['child_level'],
593 'selected' => false
594 );
595 $smcFunc['db_free_result']($result);
596 }
597
598 // Deleting a membergroup by URL (not implemented).
599 function DeleteMembergroup()
600 {
601 global $sourcedir;
602
603 checkSession('get');
604
605 require_once($sourcedir . '/Subs-Membergroups.php');
606 deleteMembergroups((int) $_REQUEST['group']);
607
608 // Go back to the membergroup index.
609 redirectexit('action=admin;area=membergroups;');
610 }
611
612 // Editing a membergroup.
613 function EditMembergroup()
614 {
615 global $context, $txt, $sourcedir, $modSettings, $smcFunc;
616
617 $_REQUEST['group'] = isset($_REQUEST['group']) && $_REQUEST['group'] > 0 ? (int) $_REQUEST['group'] : 0;
618
619 // Make sure this group is editable.
620 if (!empty($_REQUEST['group']))
621 {
622 $request = $smcFunc['db_query']('', '
623 SELECT id_group
624 FROM {db_prefix}membergroups
625 WHERE id_group = {int:current_group}' . (allowedTo('admin_forum') ? '' : '
626 AND group_type != {int:is_protected}') . '
627 LIMIT {int:limit}',
628 array(
629 'current_group' => $_REQUEST['group'],
630 'is_protected' => 1,
631 'limit' => 1,
632 )
633 );
634 list ($_REQUEST['group']) = $smcFunc['db_fetch_row']($request);
635 $smcFunc['db_free_result']($request);
636 }
637
638 // Now, do we have a valid id?
639 if (empty($_REQUEST['group']))
640 fatal_lang_error('membergroup_does_not_exist', false);
641
642 // The delete this membergroup button was pressed.
643 if (isset($_POST['delete']))
644 {
645 checkSession();
646
647 require_once($sourcedir . '/Subs-Membergroups.php');
648 deleteMembergroups($_REQUEST['group']);
649
650 redirectexit('action=admin;area=membergroups;');
651 }
652 // A form was submitted with the new membergroup settings.
653 elseif (isset($_POST['submit']))
654 {
655 // Validate the session.
656 checkSession();
657
658 // Can they really inherit from this group?
659 if ($_POST['group_inherit'] != -2 && !allowedTo('admin_forum'))
660 {
661 $request = $smcFunc['db_query']('', '
662 SELECT group_type
663 FROM {db_prefix}membergroups
664 WHERE id_group = {int:inherit_from}
665 LIMIT {int:limit}',
666 array(
667 'inherit_from' => $_POST['group_inherit'],
668 'limit' => 1,
669 )
670 );
671 list ($inherit_type) = $smcFunc['db_fetch_row']($request);
672 $smcFunc['db_free_result']($request);
673 }
674
675 // Set variables to their proper value.
676 $_POST['max_messages'] = isset($_POST['max_messages']) ? (int) $_POST['max_messages'] : 0;
677 $_POST['min_posts'] = isset($_POST['min_posts']) && isset($_POST['group_type']) && $_POST['group_type'] == -1 && $_REQUEST['group'] > 3 ? abs($_POST['min_posts']) : ($_REQUEST['group'] == 4 ? 0 : -1);
678 $_POST['stars'] = (empty($_POST['star_count']) || $_POST['star_count'] < 0) ? '' : min((int) $_POST['star_count'], 99) . '#' . $_POST['star_image'];
679 $_POST['group_desc'] = isset($_POST['group_desc']) && ($_REQUEST['group'] == 1 || (isset($_POST['group_type']) && $_POST['group_type'] != -1)) ? trim($_POST['group_desc']) : '';
680 $_POST['group_type'] = !isset($_POST['group_type']) || $_POST['group_type'] < 0 || $_POST['group_type'] > 3 || ($_POST['group_type'] == 1 && !allowedTo('admin_forum')) ? 0 : (int) $_POST['group_type'];
681 $_POST['group_hidden'] = empty($_POST['group_hidden']) || $_POST['min_posts'] != -1 || $_REQUEST['group'] == 3 ? 0 : (int) $_POST['group_hidden'];
682 $_POST['group_inherit'] = $_REQUEST['group'] > 1 && $_REQUEST['group'] != 3 && (empty($inherit_type) || $inherit_type != 1) ? (int) $_POST['group_inherit'] : -2;
683
684 // !!! Don't set online_color for the Moderators group?
685
686 // Do the update of the membergroup settings.
687 $smcFunc['db_query']('', '
688 UPDATE {db_prefix}membergroups
689 SET group_name = {string:group_name}, online_color = {string:online_color},
690 max_messages = {int:max_messages}, min_posts = {int:min_posts}, stars = {string:stars},
691 description = {string:group_desc}, group_type = {int:group_type}, hidden = {int:group_hidden},
692 id_parent = {int:group_inherit}
693 WHERE id_group = {int:current_group}',
694 array(
695 'max_messages' => $_POST['max_messages'],
696 'min_posts' => $_POST['min_posts'],
697 'group_type' => $_POST['group_type'],
698 'group_hidden' => $_POST['group_hidden'],
699 'group_inherit' => $_POST['group_inherit'],
700 'current_group' => (int) $_REQUEST['group'],
701 'group_name' => $_POST['group_name'],
702 'online_color' => $_POST['online_color'],
703 'stars' => $_POST['stars'],
704 'group_desc' => $_POST['group_desc'],
705 )
706 );
707
708 // Time to update the boards this membergroup has access to.
709 if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
710 {
711 $_POST['boardaccess'] = empty($_POST['boardaccess']) || !is_array($_POST['boardaccess']) ? array() : $_POST['boardaccess'];
712 foreach ($_POST['boardaccess'] as $key => $value)
713 $_POST['boardaccess'][$key] = (int) $value;
714
715 // Find all board this group is in, but shouldn't be in.
716 $request = $smcFunc['db_query']('', '
717 SELECT id_board, member_groups
718 FROM {db_prefix}boards
719 WHERE FIND_IN_SET({string:current_group}, member_groups) != 0' . (empty($_POST['boardaccess']) ? '' : '
720 AND id_board NOT IN ({array_int:board_access_list})'),
721 array(
722 'current_group' => (int) $_REQUEST['group'],
723 'board_access_list' => $_POST['boardaccess'],
724 )
725 );
726 while ($row = $smcFunc['db_fetch_assoc']($request))
727 $smcFunc['db_query']('', '
728 UPDATE {db_prefix}boards
729 SET member_groups = {string:member_group_access}
730 WHERE id_board = {int:current_board}',
731 array(
732 'current_board' => $row['id_board'],
733 'member_group_access' => implode(',', array_diff(explode(',', $row['member_groups']), array($_REQUEST['group']))),
734 )
735 );
736 $smcFunc['db_free_result']($request);
737
738 // Add the membergroup to all boards that hadn't been set yet.
739 if (!empty($_POST['boardaccess']))
740 $smcFunc['db_query']('', '
741 UPDATE {db_prefix}boards
742 SET member_groups = CASE WHEN member_groups = {string:blank_string} THEN {string:group_id_string} ELSE CONCAT(member_groups, {string:comma_group}) END
743 WHERE id_board IN ({array_int:board_list})
744 AND FIND_IN_SET({int:current_group}, member_groups) = 0',
745 array(
746 'board_list' => $_POST['boardaccess'],
747 'blank_string' => '',
748 'current_group' => (int) $_REQUEST['group'],
749 'group_id_string' => (string) (int) $_REQUEST['group'],
750 'comma_group' => ',' . $_REQUEST['group'],
751 )
752 );
753 }
754
755 // Remove everyone from this group!
756 if ($_POST['min_posts'] != -1)
757 {
758 $smcFunc['db_query']('', '
759 UPDATE {db_prefix}members
760 SET id_group = {int:regular_member}
761 WHERE id_group = {int:current_group}',
762 array(
763 'regular_member' => 0,
764 'current_group' => (int) $_REQUEST['group'],
765 )
766 );
767
768 $request = $smcFunc['db_query']('', '
769 SELECT id_member, additional_groups
770 FROM {db_prefix}members
771 WHERE FIND_IN_SET({string:current_group}, additional_groups) != 0',
772 array(
773 'current_group' => (int) $_REQUEST['group'],
774 )
775 );
776 $updates = array();
777 while ($row = $smcFunc['db_fetch_assoc']($request))
778 $updates[$row['additional_groups']][] = $row['id_member'];
779 $smcFunc['db_free_result']($request);
780
781 foreach ($updates as $additional_groups => $memberArray)
782 updateMemberData($memberArray, array('additional_groups' => implode(',', array_diff(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
783 }
784 elseif ($_REQUEST['group'] != 3)
785 {
786 // Making it a hidden group? If so remove everyone with it as primary group (Actually, just make them additional).
787 if ($_POST['group_hidden'] == 2)
788 {
789 $request = $smcFunc['db_query']('', '
790 SELECT id_member, additional_groups
791 FROM {db_prefix}members
792 WHERE id_group = {int:current_group}
793 AND FIND_IN_SET({int:current_group}, additional_groups) = 0',
794 array(
795 'current_group' => (int) $_REQUEST['group'],
796 )
797 );
798 $updates = array();
799 while ($row = $smcFunc['db_fetch_assoc']($request))
800 $updates[$row['additional_groups']][] = $row['id_member'];
801 $smcFunc['db_free_result']($request);
802
803 foreach ($updates as $additional_groups => $memberArray)
804 updateMemberData($memberArray, array('additional_groups' => implode(',', array_merge(explode(',', $additional_groups), array((int) $_REQUEST['group'])))));
805
806 $smcFunc['db_query']('', '
807 UPDATE {db_prefix}members
808 SET id_group = {int:regular_member}
809 WHERE id_group = {int:current_group}',
810 array(
811 'regular_member' => 0,
812 'current_group' => $_REQUEST['group'],
813 )
814 );
815 }
816
817 // Either way, let's check our "show group membership" setting is correct.
818 $request = $smcFunc['db_query']('', '
819 SELECT COUNT(*)
820 FROM {db_prefix}membergroups
821 WHERE group_type > {int:non_joinable}',
822 array(
823 'non_joinable' => 1,
824 )
825 );
826 list ($have_joinable) = $smcFunc['db_fetch_row']($request);
827 $smcFunc['db_free_result']($request);
828
829 // Do we need to update the setting?
830 if ((empty($modSettings['show_group_membership']) && $have_joinable) || (!empty($modSettings['show_group_membership']) && !$have_joinable))
831 updateSettings(array('show_group_membership' => $have_joinable ? 1 : 0));
832 }
833
834 // Do we need to set inherited permissions?
835 if ($_POST['group_inherit'] != -2 && $_POST['group_inherit'] != $_POST['old_inherit'])
836 {
837 require_once($sourcedir . '/ManagePermissions.php');
838 updateChildPermissions($_POST['group_inherit']);
839 }
840
841 // Finally, moderators!
842 $moderator_string = isset($_POST['group_moderators']) ? trim($_POST['group_moderators']) : '';
843 $smcFunc['db_query']('', '
844 DELETE FROM {db_prefix}group_moderators
845 WHERE id_group = {int:current_group}',
846 array(
847 'current_group' => $_REQUEST['group'],
848 )
849 );
850 if ((!empty($moderator_string) || !empty($_POST['moderator_list'])) && $_POST['min_posts'] == -1 && $_REQUEST['group'] != 3)
851 {
852 // Get all the usernames from the string
853 if (!empty($moderator_string))
854 {
855 $moderator_string = strtr(preg_replace('~&amp;#(\d{4,5}|[2-9]\d{2,4}|1[2-9]\d);~', '&#$1;', htmlspecialchars($moderator_string), ENT_QUOTES), array('&quot;' => '"'));
856 preg_match_all('~"([^"]+)"~', $moderator_string, $matches);
857 $moderators = array_merge($matches[1], explode(',', preg_replace('~"[^"]+"~', '', $moderator_string)));
858 for ($k = 0, $n = count($moderators); $k < $n; $k++)
859 {
860 $moderators[$k] = trim($moderators[$k]);
861
862 if (strlen($moderators[$k]) == 0)
863 unset($moderators[$k]);
864 }
865
866 // Find all the id_member's for the member_name's in the list.
867 $group_moderators = array();
868 if (!empty($moderators))
869 {
870 $request = $smcFunc['db_query']('', '
871 SELECT id_member
872 FROM {db_prefix}members
873 WHERE member_name IN ({array_string:moderators}) OR real_name IN ({array_string:moderators})
874 LIMIT ' . count($moderators),
875 array(
876 'moderators' => $moderators,
877 )
878 );
879 while ($row = $smcFunc['db_fetch_assoc']($request))
880 $group_moderators[] = $row['id_member'];
881 $smcFunc['db_free_result']($request);
882 }
883 }
884 else
885 {
886 $moderators = array();
887 foreach ($_POST['moderator_list'] as $moderator)
888 $moderators[] = (int) $moderator;
889
890 $group_moderators = array();
891 if (!empty($moderators))
892 {
893 $request = $smcFunc['db_query']('', '
894 SELECT id_member
895 FROM {db_prefix}members
896 WHERE id_member IN ({array_int:moderators})
897 LIMIT {int:num_moderators}',
898 array(
899 'moderators' => $moderators,
900 'num_moderators' => count($moderators),
901 )
902 );
903 while ($row = $smcFunc['db_fetch_assoc']($request))
904 $group_moderators[] = $row['id_member'];
905 $smcFunc['db_free_result']($request);
906 }
907 }
908
909 // Found some?
910 if (!empty($group_moderators))
911 {
912 $mod_insert = array();
913 foreach ($group_moderators as $moderator)
914 $mod_insert[] = array($_REQUEST['group'], $moderator);
915
916 $smcFunc['db_insert']('insert',
917 '{db_prefix}group_moderators',
918 array('id_group' => 'int', 'id_member' => 'int'),
919 $mod_insert,
920 array('id_group', 'id_member')
921 );
922 }
923 }
924
925 // There might have been some post group changes.
926 updateStats('postgroups');
927 // We've definetely changed some group stuff.
928 updateSettings(array(
929 'settings_updated' => time(),
930 ));
931
932 // Log the edit.
933 logAction('edited_group', array('group' => $_POST['group_name']), 'admin');
934
935 redirectexit('action=admin;area=membergroups');
936 }
937
938 // Fetch the current group information.
939 $request = $smcFunc['db_query']('', '
940 SELECT group_name, description, min_posts, online_color, max_messages, stars, group_type, hidden, id_parent
941 FROM {db_prefix}membergroups
942 WHERE id_group = {int:current_group}
943 LIMIT 1',
944 array(
945 'current_group' => (int) $_REQUEST['group'],
946 )
947 );
948 if ($smcFunc['db_num_rows']($request) == 0)
949 fatal_lang_error('membergroup_does_not_exist', false);
950 $row = $smcFunc['db_fetch_assoc']($request);
951 $smcFunc['db_free_result']($request);
952
953 $row['stars'] = explode('#', $row['stars']);
954
955 $context['group'] = array(
956 'id' => $_REQUEST['group'],
957 'name' => $row['group_name'],
958 'description' => htmlspecialchars($row['description']),
959 'editable_name' => htmlspecialchars($row['group_name']),
960 'color' => $row['online_color'],
961 'min_posts' => $row['min_posts'],
962 'max_messages' => $row['max_messages'],
963 'star_count' => (int) $row['stars'][0],
964 'star_image' => isset($row['stars'][1]) ? $row['stars'][1] : '',
965 'is_post_group' => $row['min_posts'] != -1,
966 'type' => $row['min_posts'] != -1 ? 0 : $row['group_type'],
967 'hidden' => $row['min_posts'] == -1 ? $row['hidden'] : 0,
968 'inherited_from' => $row['id_parent'],
969 'allow_post_group' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
970 'allow_delete' => $_REQUEST['group'] == 2 || $_REQUEST['group'] > 4,
971 'allow_protected' => allowedTo('admin_forum'),
972 );
973
974 // Get any moderators for this group
975 $request = $smcFunc['db_query']('', '
976 SELECT mem.id_member, mem.real_name
977 FROM {db_prefix}group_moderators AS mods
978 INNER JOIN {db_prefix}members AS mem ON (mem.id_member = mods.id_member)
979 WHERE mods.id_group = {int:current_group}',
980 array(
981 'current_group' => $_REQUEST['group'],
982 )
983 );
984 $context['group']['moderators'] = array();
985 while ($row = $smcFunc['db_fetch_assoc']($request))
986 $context['group']['moderators'][$row['id_member']] = $row['real_name'];
987 $smcFunc['db_free_result']($request);
988
989 $context['group']['moderator_list'] = empty($context['group']['moderators']) ? '' : '&quot;' . implode('&quot;, &quot;', $context['group']['moderators']) . '&quot;';
990
991 if (!empty($context['group']['moderators']))
992 list ($context['group']['last_moderator_id']) = array_slice(array_keys($context['group']['moderators']), -1);
993
994 // Get a list of boards this membergroup is allowed to see.
995 $context['boards'] = array();
996 if ($_REQUEST['group'] == 2 || $_REQUEST['group'] > 3)
997 {
998 $result = $smcFunc['db_query']('', '
999 SELECT id_board, name, child_level, FIND_IN_SET({string:current_group}, member_groups) != 0 AS can_access
1000 FROM {db_prefix}boards
1001 ORDER BY board_order',
1002 array(
1003 'current_group' => (int) $_REQUEST['group'],
1004 )
1005 );
1006 while ($row = $smcFunc['db_fetch_assoc']($result))
1007 $context['boards'][] = array(
1008 'id' => $row['id_board'],
1009 'name' => $row['name'],
1010 'child_level' => $row['child_level'],
1011 'selected' => !(empty($row['can_access']) || $row['can_access'] == 'f'),
1012 );
1013 $smcFunc['db_free_result']($result);
1014 }
1015
1016 // Finally, get all the groups this could be inherited off.
1017 $request = $smcFunc['db_query']('', '
1018 SELECT id_group, group_name
1019 FROM {db_prefix}membergroups
1020 WHERE id_group != {int:current_group}' .
1021 (empty($modSettings['permission_enable_postgroups']) ? '
1022 AND min_posts = {int:min_posts}' : '') . (allowedTo('admin_forum') ? '' : '
1023 AND group_type != {int:is_protected}') . '
1024 AND id_group NOT IN (1, 3)
1025 AND id_parent = {int:not_inherited}',
1026 array(
1027 'current_group' => (int) $_REQUEST['group'],
1028 'min_posts' => -1,
1029 'not_inherited' => -2,
1030 'is_protected' => 1,
1031 )
1032 );
1033 $context['inheritable_groups'] = array();
1034 while ($row = $smcFunc['db_fetch_assoc']($request))
1035 $context['inheritable_groups'][$row['id_group']] = $row['group_name'];
1036 $smcFunc['db_free_result']($request);
1037
1038 $context['sub_template'] = 'edit_group';
1039 $context['page_title'] = $txt['membergroups_edit_group'];
1040 }
1041
1042 // Set general membergroup settings.
1043 function ModifyMembergroupsettings()
1044 {
1045 global $context, $sourcedir, $scripturl, $modSettings, $txt;
1046
1047 $context['sub_template'] = 'show_settings';
1048 $context['page_title'] = $txt['membergroups_settings'];
1049
1050 // Needed for the settings functions.
1051 require_once($sourcedir . '/ManageServer.php');
1052
1053 // Don't allow assignment of guests.
1054 $context['permissions_excluded'] = array(-1);
1055
1056 // Only one thing here!
1057 $config_vars = array(
1058 array('permissions', 'manage_membergroups'),
1059 );
1060
1061 if (isset($_REQUEST['save']))
1062 {
1063 checkSession();
1064
1065 // Yeppers, saving this...
1066 saveDBSettings($config_vars);
1067 redirectexit('action=admin;area=membergroups;sa=settings');
1068 }
1069
1070 // Some simple context.
1071 $context['post_url'] = $scripturl . '?action=admin;area=membergroups;save;sa=settings';
1072 $context['settings_title'] = $txt['membergroups_settings'];
1073
1074 prepareDBSettingContext($config_vars);
1075 }
1076
1077 ?>