'AddMembergroup',
'delete' => 'DeleteMembergroup',
'edit' => 'EditMembergroup',
'members' => 'MembergroupMembers'
);
if (isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]))
{
$sa = $subActions[$_REQUEST['sa']];
unset($subActions);
$sa();
}
else
MembergroupIndex();
}
function MembergroupIndex()
{
global $db_prefix, $txt, $scripturl, $context, $settings;
$context['page_title'] = $txt['membergroups_title'];
$query = db_query("
SELECT mg.ID_GROUP, mg.groupName, mg.minPosts, mg.onlineColor, mg.stars, COUNT(mem.ID_MEMBER) AS num_members
FROM {$db_prefix}membergroups AS mg
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_GROUP = mg.ID_GROUP OR FIND_IN_SET(mg.ID_GROUP, mem.additionalGroups) OR mg.ID_GROUP = mem.ID_POST_GROUP)
GROUP BY mg.ID_GROUP
ORDER BY mg.minPosts, IF(mg.ID_GROUP < 4, mg.ID_GROUP, 4), mg.groupName", __FILE__, __LINE__);
$context['groups'] = array(
'regular' => array(),
'post' => array()
);
while ($row = mysql_fetch_assoc($query))
{
$row['stars'] = explode('#', $row['stars']);
$context['groups'][$row['minPosts'] == -1 ? 'regular' : 'post'][$row['ID_GROUP']] = array(
'id' => $row['ID_GROUP'],
'name' => $row['groupName'],
'num_members' => $row['ID_GROUP'] != 3 ? $row['num_members'] : $txt['membergroups_guests_na'],
'allow_delete' => $row['ID_GROUP'] > 4,
'can_search' => $row['ID_GROUP'] != 3,
'href' => $scripturl . '?action=membergroups;sa=members;id=' . $row['ID_GROUP'],
'link' => '' . $row['num_members'] . '',
'is_post_group' => $row['minPosts'] != -1,
'min_posts' => $row['minPosts'] == -1 ? '-' : $row['minPosts'],
'color' => empty($row['onlineColor']) ? '' : $row['onlineColor'],
'stars' => !empty($row['stars'][0]) && !empty($row['stars'][1]) ? str_repeat('', $row['stars'][0]) : '',
'last_group' => false
);
}
mysql_free_result($query);
$request = db_query("
SELECT COUNT(ID_MEMBER)
FROM {$db_prefix}members
WHERE ID_GROUP = 0", __FILE__, __LINE__);
list ($num_members) = mysql_fetch_row($request);
mysql_free_result($request);
$context['groups'][count($context['groups']) - 1]['last_group'] = true;
}
function AddMembergroup()
{
global $db_prefix, $context, $txt, $sourcedir;
if (empty($_POST['group_name']))
{
$context['page_title'] = $txt['membergroups_new_group'];
$context['sub_template'] = 'new_group';
$context['postgroup'] = !empty($_POST['postgroup']);
$result = db_query("
SELECT ID_GROUP, groupName
FROM {$db_prefix}membergroups
WHERE ID_GROUP > 3 OR ID_GROUP = 2
ORDER BY minPosts, ID_GROUP != 2, groupName", __FILE__, __LINE__);
$context['groups'] = array();
while ($row = mysql_fetch_assoc($result))
$context['groups'][] = array(
'id' => $row['ID_GROUP'],
'name' => $row['groupName']
);
mysql_free_result($result);
$result = db_query("
SELECT ID_BOARD, name, childLevel
FROM {$db_prefix}boards
ORDER BY boardOrder", __FILE__, __LINE__);
$context['boards'] = array();
while ($row = mysql_fetch_assoc($result))
$context['boards'][] = array(
'id' => $row['ID_BOARD'],
'name' => $row['name'],
'child_level' => $row['childLevel'],
'selected' => false
);
mysql_free_result($result);
return;
}
checkSession();
$request = db_query("
SELECT groupName
FROM {$db_prefix}membergroups
WHERE groupName = '$_POST[group_name]'
LIMIT 1", __FILE__, __LINE__);
if (mysql_num_rows($request) != 0)
redirectexit('action=membergroups;');
mysql_free_result($request);
$request = db_query("
SELECT MAX(ID_GROUP)
FROM {$db_prefix}membergroups", __FILE__, __LINE__);
list ($ID_GROUP) = mysql_fetch_row($request);
mysql_free_result($request);
$ID_GROUP++;
db_query("
INSERT INTO {$db_prefix}membergroups
(ID_GROUP, groupName, minPosts, stars)
VALUES ($ID_GROUP, '$_POST[group_name]', " . (isset($_POST['min_posts']) ? (int) $_POST['min_posts'] : '-1') . ", '1#star.gif')", __FILE__, __LINE__);
// Update the post groups now, if this is a post group!
if (isset($_POST['min_posts']))
updateStats('postgroups');
if (!isset($_POST['copyperm']) || $_POST['copyperm'] == 1)
{
// Set default permission level.
require_once($sourcedir . '/ManagePermissions.php');
setPermissionLevel($_POST['level'], $ID_GROUP, 'null');
}
// Copy the permissions!
else
{
$_POST['copyperm'] = (int) $_POST['copyperm'];
$request = db_query("
SELECT permission, addDeny
FROM {$db_prefix}permissions
WHERE ID_GROUP = $_POST[copyperm]", __FILE__, __LINE__);
$setString = '';
while ($row = mysql_fetch_assoc($request))
$setString .= "
($ID_GROUP, '$row[permission]', $row[addDeny]),";
mysql_free_result($request);
if (!empty($setString))
db_query("
INSERT INTO {$db_prefix}permissions
(ID_GROUP, permission, addDeny)
VALUES" . substr($setString, 0, -1), __FILE__, __LINE__);
$request = db_query("
SELECT ID_BOARD, permission, addDeny
FROM {$db_prefix}board_permissions
WHERE ID_GROUP = $_POST[copyperm]", __FILE__, __LINE__);
$setString = '';
while ($row = mysql_fetch_assoc($request))
$setString .= "
($ID_GROUP, $row[ID_BOARD], '$row[permission]', $row[addDeny]),";
mysql_free_result($request);
if (!empty($setString))
db_query("
INSERT INTO {$db_prefix}board_permissions
(ID_GROUP, ID_BOARD, permission, addDeny)
VALUES" . substr($setString, 0, -1), __FILE__, __LINE__);
// Also get some membergroup information if we're not copying from guests...
if ($_POST['copyperm'] > 0)
{
$request = db_query("
SELECT onlineColor, maxMessages, stars
FROM {$db_prefix}membergroups
WHERE ID_GROUP = $_POST[copyperm]
LIMIT 1", __FILE__, __LINE__);
$group_info = mysql_fetch_assoc($request);
mysql_free_result($request);
// ...and update the new membergroup with it.
db_query("
UPDATE {$db_prefix}membergroups
SET
onlineColor = '$group_info[onlineColor]',
maxMessages = $group_info[maxMessages],
stars = '$group_info[stars]'
WHERE ID_GROUP = $ID_GROUP
LIMIT 1", __FILE__, __LINE__);
}
}
if (empty($_POST['boardaccess']))
$_POST['boardaccess'] = array();
$boards = array();
foreach ($_POST['boardaccess'] as $id => $dummy)
$boards[] = (int) $id;
// If they have no special access requirements then skip the rest of this.
if (count($boards) == 0)
redirectexit('action=membergroups;');
// Now it's the time to sort out which boards this new group has access to.
$result = db_query("
SELECT ID_BOARD, memberGroups
FROM {$db_prefix}boards
WHERE ID_BOARD IN (" . implode(', ', $boards) . ")
LIMIT " . count($boards), __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($result))
{
// They should have access... but there is a list of VIPs.
$memberGroups = explode(',', $row['memberGroups']);
$memberGroups[] = $ID_GROUP;
db_query("
UPDATE {$db_prefix}boards
SET memberGroups = '" . implode(',', $memberGroups) . "'
WHERE ID_BOARD = $row[ID_BOARD]
LIMIT 1", __FILE__, __LINE__);
}
mysql_free_result($result);
redirectexit('action=membergroups;');
}
function DeleteMembergroup()
{
global $db_prefix;
checkSession('request');
$_REQUEST['id'] = (int) $_REQUEST['id'];
if ($_REQUEST['id'] <= 4)
redirectexit('action=membergroups;');
db_query("
DELETE FROM {$db_prefix}membergroups
WHERE ID_GROUP = $_REQUEST[id]
LIMIT 1", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}permissions
WHERE ID_GROUP = $_REQUEST[id]", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}board_permissions
WHERE ID_GROUP = $_REQUEST[id]", __FILE__, __LINE__);
db_query("
UPDATE {$db_prefix}members
SET ID_GROUP = 0
WHERE ID_GROUP = $_REQUEST[id]", __FILE__, __LINE__);
$request = db_query("
SELECT ID_MEMBER, additionalGroups
FROM {$db_prefix}members
WHERE FIND_IN_SET($_REQUEST[id], additionalGroups)", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
$row['additionalGroups'] = array_flip(explode(',', $row['additionalGroups']));
unset($row['additionalGroups'][$_REQUEST['id']]);
$row['additionalGroups'] = implode(',', array_keys($row['additionalGroups']));
updateMemberData($row['ID_MEMBER'], array('additionalGroups' => '\'' . $row['additionalGroups'] . '\''));
}
mysql_free_result($request);
$request = db_query("
SELECT ID_BOARD, memberGroups
FROM {$db_prefix}boards
WHERE FIND_IN_SET($_REQUEST[id], memberGroups)", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
$row['memberGroups'] = array_flip(explode(',', $row['memberGroups']));
unset($row['memberGroups'][$_REQUEST['id']]);
$row['memberGroups'] = implode(',', array_keys($row['memberGroups']));
db_query("
UPDATE {$db_prefix}boards
SET memberGroups = '$row[memberGroups]'
WHERE ID_BOARD = $row[ID_BOARD]
LIMIT 1", __FILE__, __LINE__);
}
// Recalculate the post groups, as they likely changed.
updateStats('postgroups');
redirectexit('action=membergroups;');
}
function EditMembergroup()
{
global $db_prefix, $context, $txt;
$_GET['id'] = empty($_GET['id']) || $_GET['id'] < 0 ? 1 : (int) $_GET['id'];
if (isset($_POST['delete']))
DeleteMembergroup();
elseif (isset($_POST['submit']))
{
checkSession();
$_POST['max_messages'] = (int) $_POST['max_messages'];
$_POST['min_posts'] = isset($_POST['min_posts']) && $_POST['post_group'] == '1' && $_GET['id'] > 3 ? abs($_POST['min_posts']) : ($_GET['id'] == 4 ? 0 : -1);
$_POST['stars'] = (empty($_POST['star_count']) || $_POST['star_count'] < 0) ? '' : min((int) $_POST['star_count'], 99) . '#' . $_POST['star_image'];
db_query("
UPDATE {$db_prefix}membergroups
SET groupName = '$_POST[group_name]', onlineColor = '$_POST[online_color]',
maxMessages = $_POST[max_messages], minPosts = $_POST[min_posts], stars = '$_POST[stars]'
WHERE ID_GROUP = $_GET[id]
LIMIT 1", __FILE__, __LINE__);
// There might have been some post group changes.
updateStats('postgroups');
redirectexit('action=membergroups;');
}
$result = db_query("
SELECT groupName, minPosts, onlineColor, maxMessages, stars
FROM {$db_prefix}membergroups
WHERE ID_GROUP = $_GET[id]
LIMIT 1", __FILE__, __LINE__);
$row = mysql_fetch_assoc($result);
mysql_free_result($result);
$row['stars'] = explode('#', $row['stars']);
$context['group'] = array(
'id' => $_GET['id'],
'name' => $row['groupName'],
'editable_name' => htmlspecialchars($row['groupName']),
'color' => $row['onlineColor'],
'min_posts' => $row['minPosts'],
'max_messages' => $row['maxMessages'],
'star_count' => (int) $row['stars'][0],
'star_image' => isset($row['stars'][1]) ? $row['stars'][1] : '',
'is_post_group' => $row['minPosts'] != -1,
'allow_post_group' => $_GET['id'] > 4,
'allow_delete' => $_GET['id'] > 4
);
$context['sub_template'] = 'edit_group';
$context['page_title'] = $txt['membergroups_edit_group'];
}
// Display members of a group, and allow adding of members to a group. Silly function name though ;)
function MembergroupMembers()
{
global $txt, $scripturl, $db_prefix, $context, $modSettings;
$_REQUEST['id'] = (int) $_REQUEST['id'];
// Start!
$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;
// Load up the group details - and ensure this ISN'T a post group ;)
$request = db_query("
SELECT ID_GROUP AS id, groupName AS name, minPosts = -1 AS assignable
FROM {$db_prefix}membergroups
WHERE ID_GROUP = $_REQUEST[id]
LIMIT 1", __FILE__, __LINE__);
// Not really possible...
if (mysql_num_rows($request) == 0)
fatal_lang_error(1);
$context['group'] = mysql_fetch_assoc($request);
mysql_free_result($request);
if ($context['group']['id'] == 1 && !allowedTo('admin_forum'))
$context['group']['assignable'] = 0;
// Changing members in this group?
if (isset($_POST['sc']) && $context['group']['assignable'] && $_REQUEST['id'] != 3)
{
checkSession();
// Removing member from group?
if (isset($_POST['remove']) && isset($_REQUEST['rem']))
{
$members = array();
foreach ($_REQUEST['rem'] AS $remove => $dummy)
$members[] = (int) $remove;
// First, reset those who have this as their primary group - this is the easy one.
db_query("
UPDATE {$db_prefix}members
SET ID_GROUP = 0
WHERE ID_GROUP = $_REQUEST[id]
AND ID_MEMBER IN (" . implode(', ', $members) . ")
LIMIT " . count($members), __FILE__, __LINE__);
// Those who have it as part of their additional group must be updated the long way... sadly.
$request = db_query("
SELECT ID_MEMBER, additionalGroups
FROM {$db_prefix}members
WHERE FIND_IN_SET($_REQUEST[id], additionalGroups)
AND ID_MEMBER IN (" . implode(', ', $members) . ")
LIMIT " . count($members), __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
$tempGroup = array_flip(explode(',', $row['additionalGroups']));
unset($tempGroup[$_REQUEST['id']]);
$tempGroup = implode(',', array_flip($tempGroup));
// Do the update for this member - this may be slow for lots of people... but how many you really do at once?
db_query("
UPDATE {$db_prefix}members
SET additionalGroups = '$tempGroup'
WHERE ID_MEMBER = $row[ID_MEMBER]
LIMIT 1", __FILE__, __LINE__);
}
mysql_free_result($request);
}
// Must be adding...
elseif (isset($_REQUEST['add']) && !empty($_REQUEST['toAdd']))
{
// Get all the members to be added... taking into account names can be quoted ;)
$_REQUEST['toAdd'] = strtr(un_htmlspecialchars($_REQUEST['toAdd']), array('\\"' => '"'));
preg_match_all('~"([^"]+)"~', $_REQUEST['toAdd'], $matches);
$memberQuery = array_unique(array_merge($matches[1], explode(',', preg_replace('~"([^"]+)"~', '', $_REQUEST['toAdd']))));
foreach ($memberQuery as $index => $member)
{
if (strlen(trim($member)) > 0)
$memberQuery[$index] = strtolower(trim($member));
else
unset($memberQuery[$index]);
}
$request = db_query("
SELECT ID_MEMBER, ID_GROUP, additionalGroups
FROM {$db_prefix}members
WHERE memberName IN ('" . implode("', '", $memberQuery) . "')", __FILE__, __LINE__);
// Reset the query array and we'll use it to update the members.
$memberQuery = array(
'main_group' => array(),
'additional' => array()
);
while ($row = mysql_fetch_assoc($request))
{
// Verify that they are not already a member - and add them to our array.
if ($row['ID_GROUP'] != $_REQUEST['id'] && !in_array($_REQUEST['id'], explode(',', $row['additionalGroups'])))
$memberQuery[$row['ID_GROUP'] == 0 ? 'main_group' : 'additional'][] = $row['ID_MEMBER'];
}
mysql_free_result($request);
// Do the updates...
if (!empty($memberQuery['main_group']))
db_query("
UPDATE {$db_prefix}members
SET ID_GROUP = $_REQUEST[id]
WHERE ID_MEMBER IN (" . implode(', ', $memberQuery['main_group']) . ")
LIMIT " . count($memberQuery['main_group']), __FILE__, __LINE__);
// This one is more complicated!
if (!empty($memberQuery['additional']))
{
db_query("
UPDATE {$db_prefix}members
SET additionalGroups = IF(additionalGroups = '', '$_REQUEST[id]', CONCAT(additionalGroups, ',$_REQUEST[id]'))
WHERE ID_MEMBER IN (" . implode(', ', $memberQuery['additional']) . ")
LIMIT " . count($memberQuery['additional']), __FILE__, __LINE__);
}
}
}
// Sort out the sorting!
$sort_methods = array(
'name' => 'realName',
'email' => 'emailAddress',
'active' => 'lastLogin',
'registered' => 'dateRegistered',
'posts' => 'posts',
);
// They didn't pick one, default to by name..
if (!isset($_REQUEST['sort']) || !isset($sort_methods[$_REQUEST['sort']]))
{
$context['sort_by'] = 'name';
$querySort = 'realName';
}
// Otherwise default to ascending.
else
{
$context['sort_by'] = $_REQUEST['sort'];
$querySort = $sort_methods[$_REQUEST['sort']];
}
$context['sort_direction'] = isset($_REQUEST['desc']) ? 'down' : 'up';
// Count members of the group.
$request = db_query("
SELECT COUNT(ID_MEMBER)
FROM {$db_prefix}members
WHERE " . ($context['group']['assignable'] ? "ID_GROUP = $_REQUEST[id] OR FIND_IN_SET($_REQUEST[id], additionalGroups)" : "ID_POST_GROUP = $_REQUEST[id]"), __FILE__, __LINE__);
list ($context['total_members']) = mysql_fetch_row($request);
mysql_free_result($request);
// Create the page index.
$context['page_index'] = constructPageIndex($scripturl . '?action=membergroups;sa=members;id=' . $_REQUEST['id'] . ';sort=' . $context['sort_by'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $context['start'], $context['total_members'], $modSettings['defaultMaxMembers']);
// Load up all members of this group.
$request = db_query("
SELECT ID_MEMBER, realName, memberName, emailAddress, memberIP, dateRegistered, lastLogin, posts
FROM {$db_prefix}members
WHERE " . ($context['group']['assignable'] ? "ID_GROUP = $_REQUEST[id] OR FIND_IN_SET($_REQUEST[id], additionalGroups)" : "ID_POST_GROUP = $_REQUEST[id]") . "
ORDER BY $querySort " . ($context['sort_direction'] == 'down' ? 'DESC' : 'ASC') . "
LIMIT $context[start], $modSettings[defaultMaxMembers]", __FILE__, __LINE__);
$context['members'] = array();
while ($row = mysql_fetch_assoc($request))
$context['members'][] = array(
'id' => $row['ID_MEMBER'],
'name' => '' . $row['realName'] . '',
'email' => '' . $row['emailAddress'] . '',
'ip' => '' . $row['memberIP'] . '',
'registered' => timeformat($row['dateRegistered']),
'last_online' => empty($row['lastLogin']) ? $txt['never'] : timeformat($row['lastLogin']),
'posts' => $row['posts'],
);
mysql_free_result($request);
// Select the template.
$context['sub_template'] = 'group_members';
$context['page_title'] = $txt['membergroups_members_title'] . ': ' . $context['group']['name'];
}
// View all members.
function ViewMembers()
{
global $txt, $scripturl, $db_prefix, $context, $modSettings;
isAllowedTo('moderate_forum');
// Administration bar, I choose you!
adminIndex('view_members');
loadTemplate('ManageMembers');
loadLanguage('ManageMembers');
$allowed_sub_actions = array('all', 'search', 'query', 'delete');
// Set default sub action.
$context['sub_action'] = empty($_REQUEST['sa']) || !in_array($_REQUEST['sa'], $allowed_sub_actions) ? 'all' : $_REQUEST['sa'];
if ($context['sub_action'] == 'delete' && allowedTo('profile_remove_any'))
{
checkSession();
// Delete all the selected members.
deleteMembers(array_keys($_POST['delete']));
// Update the latest member...
updateStats('member');
// Switch to 'view all members'.
$context['sub_action'] = 'all';
}
// Retrieve the membergroups and postgroups.
if (in_array($context['sub_action'], array('search', 'query')))
{
$context['membergroups'] = array(
array(
'id' => 0,
'name' => $txt['membergroups_members'],
'can_be_additional' => false
)
);
$context['postgroups'] = array();
$request = db_query("
SELECT ID_GROUP, groupName, minPosts
FROM {$db_prefix}membergroups
WHERE ID_GROUP != 3
ORDER BY minPosts, IF(ID_GROUP < 4, ID_GROUP, 4), groupName", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
if ($row['minPosts'] == -1)
$context['membergroups'][] = array(
'id' => $row['ID_GROUP'],
'name' => $row['groupName'],
'can_be_additional' => true
);
else
$context['postgroups'][] = array(
'id' => $row['ID_GROUP'],
'name' => $row['groupName']
);
}
}
// Check input after a member search has been submitted.
if ($context['sub_action'] == 'query' && empty($_REQUEST['params']))
{
// Some data about the form fields and how they are linked to the database.
$params = array(
'mem_id' => array(
'db_fields' => array('ID_MEMBER'),
'type' => 'int',
'range' => true
),
'age' => array(
'db_fields' => array('birthdate'),
'type' => 'age',
'range' => true
),
'posts' => array(
'db_fields' => array('posts'),
'type' => 'int',
'range' => true
),
'reg_date' => array(
'db_fields' => array('dateRegistered'),
'type' => 'date',
'range' => true
),
'last_online' => array(
'db_fields' => array('lastLogin'),
'type' => 'date',
'range' => true
),
'gender' => array(
'db_fields' => array('gender'),
'type' => 'checkbox',
'values' => array('0', '1', '2'),
),
'activated' => array(
'db_fields' => array('is_activated'),
'type' => 'checkbox',
'values' => array('0', '1'),
),
'membername' => array(
'db_fields' => array('memberName', 'realName'),
'type' => 'string'
),
'email' => array(
'db_fields' => array('emailAddress'),
'type' => 'string'
),
'website' => array(
'db_fields' => array('websiteTitle', 'websiteUrl'),
'type' => 'string'
),
'location' => array(
'db_fields' => array('location'),
'type' => 'string'
),
'ip' => array(
'db_fields' => array('memberIP'),
'type' => 'string'
),
'messenger' => array(
'db_fields' => array('ICQ', 'AIM', 'YIM', 'MSN'),
'type' => 'string'
)
);
$range_trans = array(
'--' => '<',
'-' => '<=',
'=' => '=',
'+' => '>=',
'++' => '>'
);
// Loop through every field of the form.
$query_parts = array();
foreach ($params as $param_name => $param_info)
{
// Not filled in?
if (!isset($_POST[$param_name]) || $_POST[$param_name] == '')
continue;
// Make sure numeric values are really numeric.
if (in_array($param_info['type'], array('int', 'age')))
$_POST[$param_name] = (int) $_POST[$param_name];
// Date values have to match the specified format.
elseif ($param_info['type'] == 'date')
{
// Check if this date format is valid.
if (!preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $_POST[$param_name]))
continue;
// Add quotes for the database.
$_POST[$param_name] = strtotime($_POST[$param_name]);
}
// Those values that are in some kind of range (<, <=, =, >=, >).
if (!empty($param_info['range']))
{
// Default to '=', just in case...
if (empty($range_trans[$_POST['types'][$param_name]]))
$_POST['types'][$param_name] = '=';
// Handle special case 'age'.
if ($param_info['type'] == 'age')
{
// All people that were born between $lowerlimit and $upperlimit are currently the specified age.
$datearray = getdate(forum_time());
$upperlimit = str_pad($datearray['year'] - $_POST[$param_name], 4, '0') . '-' . str_pad($datearray['mon'], 2, '0', STR_PAD_LEFT) . '-' . str_pad($datearray['mday'], 2, '0', STR_PAD_LEFT);
$lowerlimit = str_pad($datearray['year'] - $_POST[$param_name] - 1, 4, '0') . '-' . str_pad($datearray['mon'], 2, '0', STR_PAD_LEFT) . '-' . str_pad($datearray['mday'], 2, '0', STR_PAD_LEFT);
if (in_array($_POST['types'][$param_name], array('-', '--', '=')))
$query_parts[] = "{$param_info['db_fields'][0]} > '" . ($_POST['types'][$param_name] == '--' ? $upperlimit : $lowerlimit) . "'";
if (in_array($_POST['types'][$param_name], array('+', '++', '=')))
{
$query_parts[] = "{$param_info['db_fields'][0]} <= '" . ($_POST['types'][$param_name] == '++' ? $lowerlimit : $upperlimit) . "'";
// Make sure that members that didn't set their birth year are not queried.
$query_parts[] = "{$param_info['db_fields'][0]} > '0000-12-31'";
}
}
else
$query_parts[] = $param_info['db_fields'][0] . ' ' . $range_trans[$_POST['types'][$param_name]] . ' ' . $_POST[$param_name];
}
// Checkboxes.
elseif ($param_info['type'] == 'checkbox')
{
// Each checkbox or no checkbox at all is checked -> ignore.
if (!is_array($_POST[$param_name]) || count($_POST[$param_name]) == 0 || count($_POST[$param_name]) == count($param_info['values']))
continue;
$query_parts[] = "{$param_info['db_fields'][0]} IN ('" . implode("', '", $_POST[$param_name]) . "')";
}
else
{
// Replace the wildcard characters ('*' and '?') into MySQL ones.
$_POST[$param_name] = strtolower(addslashes(strtr($_POST[$param_name], array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_'))));
$query_parts[] = '(' . implode(" LIKE '%{$_POST[$param_name]}%' OR ", $param_info['db_fields']) . " LIKE '%{$_POST[$param_name]}%')";
}
}
// Set up the membergroup query part.
$mg_query_parts = array();
// Primary membergroups, but only if at least was was not selected.
if (!empty($_POST['membergroups'][1]) && count($context['membergroups']) != count($_POST['membergroups'][1]))
$mg_query_parts[] = "ID_GROUP IN (" . implode(", ", $_POST['membergroups'][1]) . ")";
// Additional membergroups (these are only relevant if not all primary groups where selected!).
if (!empty($_POST['membergroups'][2]) && (empty($_POST['membergroups'][1]) || count($context['membergroups']) != count($_POST['membergroups'][1])))
foreach ($_POST['membergroups'][2] as $mg)
$mg_query_parts[] = "FIND_IN_SET(" . (int) $mg . ", additionalGroups)";
// Combine the one or two membergroup parts into one query part linked with an OR.
if (!empty($mg_query_parts))
$query_parts[] = '(' . implode(' OR ', $mg_query_parts) . ')';
// Get all selected post count related membergroups.
if (!empty($_POST['postgroups']) && count($_POST['postgroups']) != count($context['postgroups']))
$query_parts[] = "ID_POST_GROUP IN (" . implode(", ", $_POST['postgroups']) . ")";
// Construct the where part of the query.
$where = empty($query_parts) ? '1' : implode('
AND ', $query_parts);
}
// If the query information was already packed in the URL, decode it.
elseif ($context['sub_action'] == 'query')
$where = base64_decode($_REQUEST['params']);
// Construct the additional URL part with the query info in it.
$context['params_url'] = $context['sub_action'] == 'query' ? ';sa=query;params=' . base64_encode($where) : '';
// Get the title and sub template ready..
$context['page_title'] = $txt[9];
$context['sub_template'] = 'view_members';
// Determine whether to show the 'delete members' checkboxes.
$context['can_delete_members'] = allowedTo('profile_remove_any');
// All the columns they have to pick from...
$context['columns'] = array(
'ID_MEMBER' => array('label' => $txt['member_id']),
'memberName' => array('label' => $txt[35]),
'realName' => array('label' => $txt['display_name']),
'emailAddress' => array('label' => $txt['email_address']),
'memberIP' => array('label' => $txt['ip_address']),
'lastLogin' => array('label' => $txt['viewmembers_online']),
'posts' => array('label' => $txt[26])
);
// Default sort column to 'memberName' if the current one is unknown or not set.
if (!isset($_REQUEST['sort']) || !array_key_exists($_REQUEST['sort'], $context['columns']))
$_REQUEST['sort'] = 'memberName';
// Provide extra information about each column - the link, whether it's selected, etc.
foreach ($context['columns'] as $col => $dummy)
{
$context['columns'][$col]['href'] = $scripturl . '?action=viewmembers' . $context['params_url'] . ';sort=' . $col . ';start=0';
if (!isset($_REQUEST['desc']) && $col == $_REQUEST['sort'])
$context['columns'][$col]['href'] .= ';desc';
$context['columns'][$col]['link'] = '' . $context['columns'][$col]['label'] . '';
$context['columns'][$col]['selected'] = $_REQUEST['sort'] == $col;
}
$context['sort_by'] = $_REQUEST['sort'];
$context['sort_direction'] = !isset($_REQUEST['desc']) ? 'down' : 'up';
// Calculate the number of results.
if (empty($where) or $where == '1')
$num_members = $modSettings['memberCount'];
else
{
$request = db_query("
SELECT COUNT(ID_MEMBER)
FROM {$db_prefix}members
WHERE $where", __FILE__, __LINE__);
list ($num_members) = mysql_fetch_row($request);
}
// Construct the page links.
$context['page_index'] = constructPageIndex($scripturl . '?action=viewmembers' . $context['params_url'] . ';sort=' . $_REQUEST['sort'] . (isset($_REQUEST['desc']) ? ';desc' : ''), $_REQUEST['start'], $num_members, $modSettings['defaultMaxMembers']);
$context['start'] = $_REQUEST['start'];
$request = db_query("
SELECT
ID_MEMBER, memberName, realName, emailAddress, memberIP, IFNULL(lastLogin, 0) AS lastLogin, posts
FROM {$db_prefix}members" . ($context['sub_action'] == 'query' && !empty($where) ? "
WHERE $where" : '') . "
ORDER BY $_REQUEST[sort]" . (!isset($_REQUEST['desc']) ? '' : ' DESC') . "
LIMIT $_REQUEST[start], $modSettings[defaultMaxMembers]", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
// Calculate number of days since last online.
if (empty($row['lastLogin']))
$difference = $txt['never'];
else
{
// Today or some time ago?
$difference = jeffsdatediff($row['lastLogin']);
if (empty($difference))
$difference = $txt['viewmembers_today'];
elseif ($difference == 1)
$difference .= ' ' . $txt['viewmembers_day_ago'];
else
$difference .= ' ' . $txt['viewmembers_days_ago'];
}
$context['members'][] = array(
'id' => $row['ID_MEMBER'],
'username' => $row['memberName'],
'name' => $row['realName'],
'email' => $row['emailAddress'],
'ip' => $row['memberIP'],
'last_active' => $difference,
'posts' => $row['posts'],
'href' => $scripturl . '?action=profile;u=' . $row['ID_MEMBER'],
'link' => '' . $row['realName'] . ''
);
}
mysql_free_result($request);
}
function jeffsdatediff($old)
{
// Get the current time as the user would see it.
$forumTime = forum_time();
// Calculate the seconds that have passed since midnight.
$sinceMidnight = date('H', $forumTime) * 60 * 60 + date('i', $forumTime) * 60 + date('s', $forumTime);
// Take the difference between the two times.
$dis = time() - $old;
// Before midnight?
if ($dis < $sinceMidnight)
return 0;
else
$dis -= $sinceMidnight;
// Divide out the seconds in a day to get the number of days.
return ceil($dis / (24 * 60 * 60));
}
function submitSpammer($users)
{
global $db_prefix, $sourcedir, $modSettings, $ID_MEMBER;
// If it's not an array, make it so!
if (!is_array($users))
$users = array($users);
else
$users = array_unique($users);
if (empty($users)) {
return;
} elseif (count($users) == 1) {
list ($user) = $users;
$condition = '= ' . $user;
} else {
$condition = 'IN (' . implode(',', $users) . ')';
}
$res = db_query(
"SELECT memberName, emailAddress, memberIP
FROM {$db_prefix}members WHERE ID_MEMBER $condition",
__FILE__, __LINE__);
if ($res === FALSE) {
return;
}
while ($user = mysql_fetch_assoc($res)) {
if ($user === FALSE) continue;
actuallySubmitSpammer(
$user['memberName'],
$user['emailAddress'],
$user['memberIP']);
}
}
function actuallySubmitSpammer($_username, $_email, $_ipaddr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.stopforumspam.com/add");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'username' => $_username,
'email' => $_email,
'ip_addr' => $_ipaddr,
'api_key' => 'jk192x8p4GAmbY'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_exec($ch);
curl_close($ch);
}
// Delete a group of/single member.
function deleteMembers($users)
{
global $db_prefix, $sourcedir, $modSettings, $ID_MEMBER;
// If it's not an array, make it so!
if (!is_array($users))
$users = array($users);
else
$users = array_unique($users);
// How many are they deleting?
if (empty($users))
return;
elseif (count($users) == 1)
{
list ($user) = $users;
$condition = '= ' . $user;
if ($user == $ID_MEMBER)
isAllowedTo('profile_remove_own');
else
isAllowedTo('profile_remove_any');
}
else
{
$condition = 'IN (' . implode(',', $users) . ')';
// Deleting more than one? You can't have more than once account...
isAllowedTo('profile_remove_any');
// Log the action while we are here.
foreach ($users as $user)
logAction('delete_member', array('member' => $user));
}
// Make these peoples' posts guest posts.
db_query("
UPDATE {$db_prefix}messages
SET ID_MEMBER = 0" . (!empty($modSettings['allow_hideEmail']) ? ", posterEmail = ''" : '') . "
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
// Delete the member.
db_query("
DELETE FROM {$db_prefix}members
WHERE ID_MEMBER $condition
LIMIT " . count($users), __FILE__, __LINE__);
// Delete the logs...
db_query("
DELETE FROM {$db_prefix}log_topics
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}log_boards
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}log_mark_read
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}log_notify
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}log_online
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}collapsed_categories
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
db_query("
DELETE FROM {$db_prefix}themes
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
// Delete personal messages.
require_once($sourcedir . '/InstantMessage.php');
deleteMessages(null, null, $users);
db_query("
UPDATE {$db_prefix}instant_messages
SET ID_MEMBER_FROM = 0
WHERE ID_MEMBER_FROM $condition", __FILE__, __LINE__);
// Delete the moderator positions.
db_query("
DELETE FROM {$db_prefix}moderators
WHERE ID_MEMBER $condition", __FILE__, __LINE__);
// Make sure no member's birthday is still sticking in the calendar...
updateStats('calendar');
updateStats('member');
}
// Email your members...
function MailingList()
{
global $txt, $db_prefix, $sourcedir, $context;
global $scripturl, $modSettings, $user_info;
isAllowedTo('send_mail');
// Load the admin bar, select 'Email Your Members'..
adminIndex('email_members');
// Just came here....
if (!isset($_REQUEST['sa']))
{
loadTemplate('ManageMembers');
$context['page_title'] = $txt[6];
$context['sub_template'] = 'email_members';
$context['groups'] = array();
// Get all the extra groups as well as Administrator and Global Moderator.
$request = db_query("
SELECT mg.ID_GROUP, mg.groupName, COUNT(mem.ID_MEMBER) AS num_members
FROM {$db_prefix}membergroups AS mg
LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_GROUP = mg.ID_GROUP OR FIND_IN_SET(mg.ID_GROUP, mem.additionalGroups) OR mg.ID_GROUP = mem.ID_POST_GROUP)
GROUP BY mg.ID_GROUP
ORDER BY mg.minPosts, IF(mg.ID_GROUP < 4, mg.ID_GROUP, 4), mg.groupName", __FILE__, __LINE__);
while ($row = mysql_fetch_assoc($request))
{
$context['groups'][$row['ID_GROUP']] = array(
'id' => $row['ID_GROUP'],
'name' => $row['groupName'],
'member_count' => $row['num_members'],
);
}
mysql_free_result($request);
// Any moderators?
$request = db_query("
SELECT COUNT(DISTINCT ID_MEMBER) AS num_distinct_mods
FROM {$db_prefix}moderators
LIMIT 1", __FILE__, __LINE__);
list ($context['groups'][3]['member_count']) = mysql_fetch_row($request);
mysql_free_result($request);
$context['can_send_pm'] = allowedTo('pm_send');
return;
}
// Sending!
elseif ($_REQUEST['sa'] == 'send2')
{
checkSession();
require_once($sourcedir . '/Subs-Post.php');
// Get all the receivers.
$addressed = explode(';', $_POST['emails']);
$cleanlist = array();
foreach ($addressed as $curmem)
{
$curmem = trim($curmem);
if ($curmem != '')
$cleanlist[$curmem] = $curmem;
}
// Prepare the message for HTML.
if (isset($_POST['send_html']) && isset($_POST['parse_html']))
$_POST['message'] = str_replace(array("\n", ' '), array("
\n", ' '), stripslashes($_POST['message']));
elseif (!isset($_POST['send_html']))
$_POST['message'] = stripslashes($_POST['message']);
// Use the default time format.
$user_info['time_format'] = $modSettings['time_format'];
$variables = array(
'{$board_url}',
'{$current_time}',
'{$latest_member.link}',
'{$latest_member.id}',
'{$latest_member.name}'
);
// Replace in all the standard things.
$_POST['message'] = str_replace($variables,
array(
isset($_POST['send_html']) ? '' . $scripturl . '' : $scripturl,
timeformat(forum_time(), false),
isset($_POST['send_html']) ? '' . $modSettings['latestRealName'] . '' : $modSettings['latestRealName'],
$modSettings['latestMember'],
$modSettings['latestRealName']
), $_POST['message']);
$_POST['subject'] = str_replace($variables,
array(
$scripturl,
timeformat(forum_time(), false),
$modSettings['latestRealName'],
$modSettings['latestMember'],
$modSettings['latestRealName']
), stripslashes($_POST['subject']));
$from_member = array(
'{$member.email}',
'{$member.link}',
'{$member.id}',
'{$member.name}'
);
// This is here to prevent spam filters from tagging this as spam.
if (isset($_POST['send_html']) && preg_match('~\