weblog-content /wp-content/plugins/simple-forum/admin/panel-usergroups/support/sfa-usergroups-save.php

Language PHP Lines 235
MD5 Hash ca6549f44f98ace7b065ecccef479c97 Estimated Cost $4,048 (why?)
Repository https://bitbucket.org/openfarmtech/weblog-content View Raw File View Project SPDX
  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
<?php
/*
Simple:Press
Admin User Groups Support Functions
$LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
$Rev: 3818 $
*/

if (preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF']))
{
	die('Access Denied');
}

# function to create a new user group
function sfa_save_usergroups_new_usergroup()
{
    check_admin_referer('forum-adminform_usergroupnew', 'forum-adminform_usergroupnew');

    # if no usergroup name supplied use a default name
    if (empty($_POST['usergroup_name']))
    {
        $usergroupname = __("New User Group", "sforum");
    } else {
        $usergroupname = sf_filter_title_save(trim($_POST['usergroup_name']));
    }

    $usergroupdesc = sf_filter_title_save(trim($_POST['usergroup_desc']));
    if (isset($_POST['usergroup_is_moderator']))
	{
		$usergroupismod = 1;
	} else {
		$usergroupismod = 0;
	}

    # create the usergroup
    $success = sfa_create_usergroup_row($usergroupname, $usergroupdesc, $usergroupismod, true);

    if ($success == false)
    {
        $mess = __("New User Group Creation Failed!", "sforum");
    } else {
        $mess = __("New User Group Created", "sforum");
    }

    return $mess;
}

# function to update an existing user group
function sfa_save_usergroups_edit_usergroup()
{
	global $wpdb;

    check_admin_referer('forum-adminform_usergroupedit', 'forum-adminform_usergroupedit');

    $usergroupdata = array();
    $usergroup_id = sf_esc_int($_POST['usergroup_id']);
    $usergroupdata['usergroup_name'] = sf_filter_title_save(trim($_POST['usergroup_name']));
    $usergroupdata['usergroup_desc'] = sf_filter_title_save(trim($_POST['usergroup_desc']));
    if (isset($_POST['usergroup_is_moderator'])) { $usergroupdata['usergroup_is_moderator'] = 1; } else { $usergroupdata['usergroup_is_moderator'] = 0; }

    # ensure that something has actually changed
    if ($usergroupdata['usergroup_name'] == $_POST['ugroup_name'] && $usergroupdata['usergroup_desc'] ==
        $_POST['ugroup_desc'] && $usergroupdata['usergroup_is_moderator'] == $_POST['ugroup_ismod'])
    {
        $mess = __("No Data Changed", "sforum");
        return $mess;
    }

    # update the user group info
	$sql = "UPDATE ".SFUSERGROUPS." SET ";
	$sql.= 'usergroup_name="'.$usergroupdata['usergroup_name'].'", ';
	$sql.= 'usergroup_desc="'.$usergroupdata['usergroup_desc'].'", ';
	$sql.= 'usergroup_is_moderator="'.$usergroupdata['usergroup_is_moderator'].'" ';
	$sql.= 'WHERE usergroup_id="'.$usergroup_id.'";';
    $success = $wpdb->query($sql);
    if ($success == false)
    {
        $mess = __("User Group Update Failed!", "sforum");
    } else {
        $mess = __("User Group Record Updated", "sforum");
    }

    return $mess;
}

function sfa_save_usergroups_delete_usergroup()
{
	global $wpdb;

    check_admin_referer('forum-adminform_usergroupdelete', 'forum-adminform_usergroupdelete');

    $usergroup_id = sf_esc_int($_POST['usergroup_id']);

    # dont allow updates to the default user groups
    $usergroup = sfa_get_usergroups_row($usergroup_id);
    if ($usergroup->usergroup_locked)
    {
        $mess = __("Sorry, the default User Groups cannot be deleted.", "sforum");
        return $mess;
    }

    # remove all memberships for this user group
    $wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE usergroup_id=".$usergroup_id);

	# remove any permission sets using this user group
	$permissions = $wpdb->get_results("SELECT permission_id FROM ".SFPERMISSIONS." WHERE usergroup_id=".$usergroup_id);
	if ($permissions)
	{
		foreach ($permissions as $permission)
		{
			sfa_remove_permission_data($permission->permission_id);
		}
	}

	# remove any group default permissions using this user group
	$wpdb->query("DELETE FROM ".SFDEFPERMISSIONS." WHERE usergroup_id=".$usergroup_id);

    # remove the user group
   	$wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE usergroup_id=".$usergroup_id);
    $success = $wpdb->query("DELETE FROM ".SFUSERGROUPS." WHERE usergroup_id=".$usergroup_id);
    if ($success == false)
    {
        $mess = __("User Group Delete Failed!", "sforum");
    } else {
        $mess = __("User Group Deleted", "sforum");

        #get affected members and update pm flag
		$members = $wpdb->get_results("SELECT user_id FROM ".SFMEMBERSHIPS.' WHERE usergroup_id='.$usergroup_id);
		sfc_rebuild_members_pm($members);
    }

    return $mess;
}

function sfa_save_usergroups_add_members()
{
    check_admin_referer('forum-adminform_membernew', 'forum-adminform_membernew');

    # add the users to the user group membership
    $usergroup_id = sf_esc_int($_POST['usergroup_id']);
    if (isset($_POST['member_id']))
    {
    	$user_id_list = array_unique($_POST['member_id']);
	}

    if (!isset($user_id_list)){
	    $mess = __("No Data Changed!", "sforum");
		return $mess;
	}

	$members = array();
	for( $x=0; $x<count($user_id_list); $x++)
	{
		$user_id = sf_esc_int($user_id_list[$x]);
		$check = sf_check_membership($usergroup_id, $user_id);
		if (empty($check))
		{
			$success = sfc_add_membership($usergroup_id, $user_id);
		}
 	   	if ($success == false)
		{
	    	$mess = __("Member Add Failed!", "sforum");
	    	return $mess;
		}

		# build members list to update
		$members[$x]->user_id = $user_id;

		# update mod flag
 	   	sfc_update_member_moderator_flag($user_id);
	}

	sfc_rebuild_members_pm($members);

    $mess = __("Member(s) Added to User Group", "sforum");

    return $mess;
}

function sfa_save_usergroups_delete_members()
{
	global $wpdb;

    check_admin_referer('forum-adminform_memberdel', 'forum-adminform_memberdel');

    $usergroup_id = sf_esc_int($_POST['usergroupid']);
    $new_usergroup_id = $_POST['usergroup_id'];
    if (isset($_POST['dmember_id']))
    {
	    $user_id_list = array_unique($_POST['dmember_id']);
	}

	# make sure not moving to same user group
	if (!isset($user_id_list) || $usergroup_id == $new_usergroup_id)
	{
	    $mess = __("No Data Changed!", "sforum");
		return $mess;
	}

	$members = array();
	for( $x=0; $x<count($user_id_list); $x++)
	{
		$user_id = sf_esc_int($user_id_list[$x]);
		$success = $wpdb->query("DELETE FROM ".SFMEMBERSHIPS." WHERE user_id=".$user_id." AND usergroup_id=".$usergroup_id);

		# build members list to update
		$members[$x]->user_id = $user_id;

	    if ($new_usergroup_id != -1)
	    {
			$check = sf_check_membership($new_usergroup_id, $user_id);
			if (empty($check))
			{
				$success = sfc_add_membership($new_usergroup_id, $user_id);
			}
	    }

		# update mod flag
	    sfc_update_member_moderator_flag($user_id);
	}

	# update pm flag for affected members
	sfc_rebuild_members_pm($members);

    if ($new_usergroup_id != -1)
    {
	    $mess = __("Member(s) Moved", "sforum");
	} else {
	    $mess = __("Member(s) Deleted From User Group", "sforum");
	}

    return $mess;
}

?>
Back to Top