function form($object)
 {
     $i18n = exponent_lang_loadFile('datatypes/formbuilder_form.php');
     global $db;
     if (!defined('SYS_FORMS')) {
         require_once BASE . 'subsystems/forms.php';
     }
     if (!defined('SYS_USERS')) {
         require_once BASE . 'subsystems/users.php';
     }
     //global $user;
     exponent_forms_initialize();
     $form = new form();
     if (!isset($object->id)) {
         $object->name = '';
         $object->description = '';
         $object->is_email = 0;
         $object->is_saved = 1;
         $object->response = $i18n['default_response'];
         $object->resetbtn = $i18n['default_resetbtn'];
         $object->submitbtn = $i18n['default_submitbtn'];
         $object->subject = $i18n['default_subject'];
     } else {
         $form->meta('id', $object->id);
     }
     $form->register('name', $i18n['name'], new textcontrol($object->name));
     $form->register('description', $i18n['description'], new texteditorcontrol($object->description));
     $form->register('response', $i18n['response'], new htmleditorcontrol($object->response));
     $form->register(null, '', new htmlcontrol('<br><br><b>' . $i18n['button_header'] . '</b><br><hr><br>'));
     $form->register('submitbtn', $i18n['submitbtn'], new textcontrol($object->submitbtn));
     $form->register('resetbtn', $i18n['resetbtn'], new textcontrol($object->resetbtn));
     $form->register(null, '', new htmlcontrol('<br><br><b>' . $i18n['email_header'] . '</b><br><hr><br>'));
     $form->register('is_email', $i18n['is_email'], new checkboxcontrol($object->is_email, false));
     $userlist = array();
     $users = exponent_users_getAllUsers();
     foreach ($users as $locuser) {
         $userlist[$locuser->id] = $locuser->username;
     }
     $defaults = array();
     foreach ($db->selectObjects('formbuilder_address', 'form_id=' . $object->id . ' and user_id != 0') as $address) {
         $locuser = exponent_users_getUserById($address->user_id);
         $defaults[$locuser->id] = $locuser->username;
     }
     $form->register('users', $i18n['users'], new listbuildercontrol($defaults, $userlist));
     $groups = exponent_users_getAllGroups();
     $grouplist = array();
     $defaults = array();
     foreach ($groups as $group) {
         $grouplist[$group->id] = $group->name;
     }
     if ($grouplist != null) {
         foreach ($db->selectObjects('formbuilder_address', 'form_id=' . $object->id . ' and group_id != 0') as $address) {
             $group = exponent_users_getGroupById($address->group_id);
             $defaults[$group->id] = $group->name;
         }
         $form->register('groups', $i18n['groups'], new listbuildercontrol($defaults, $grouplist));
     }
     $defaults = array();
     foreach ($db->selectObjects('formbuilder_address', 'form_id=' . $object->id . " and email != ''") as $address) {
         $defaults[$address->email] = $address->email;
     }
     $form->register('addresses', $i18n['addresses'], new listbuildercontrol($defaults, null));
     $form->register('subject', $i18n['subject'], new textcontrol($object->subject));
     $form->register(null, '', new htmlcontrol('<br /><br /><b>' . $i18n['database_header'] . '</b><br /><hr size="1" /><br />'));
     $form->register('is_saved', $i18n['is_saved'], new checkboxcontrol($object->is_saved, false));
     $form->register(null, '', new htmlcontrol('<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' . $i18n['warning_data_loss'] . '<br />'));
     if ($object->is_saved == 1) {
         $form->controls['is_saved']->disabled = true;
         $form->meta('is_saved', '1');
     }
     $form->register(null, '', new htmlcontrol('<br /><br /><br />'));
     $form->register('submit', '', new buttongroupcontrol($i18n['save'], '', $i18n['cancel']));
     return $form;
 }
# License, or (at your option) any later version.
#
# GPL: http://www.gnu.org/licenses/gpl.txt
#
##################################################
// Part of the User Management category
if (!defined('EXPONENT')) {
    exit('');
}
if (isset($_GET['id']) && exponent_permissions_check('user_management', exponent_core_makeLocation('administrationmodule'))) {
    if (!defined('SYS_USERS')) {
        require_once BASE . 'subsystems/users.php';
    }
    $u = exponent_users_getUserById(intval($_GET['id']));
    if ($u) {
        $groups = exponent_users_getAllGroups();
        $admin = array();
        $membership = array();
        foreach ($db->selectObjects('groupmembership', 'member_id=' . $u->id) as $m) {
            $membership[] = $m->group_id;
            if ($m->is_admin == 1) {
                $admin[] = $m->group_id;
            }
        }
        for ($i = 0; $i < count($groups); $i++) {
            if (in_array($groups[$i]->id, $membership)) {
                $groups[$i]->is_member = 1;
                if (in_array($groups[$i]->id, $admin)) {
                    $groups[$i]->is_admin = 1;
                } else {
                    $groups[$i]->is_admin = 0;
 /*
 $g = null;
 $g->id = 0;
 $g->name = $i18n['anonymous_group_name'];
 $g->description = $i18n['anonymous_group_description'];
 $g->inclusive = 1;
 //$g->code = "";
 foreach ($perms as $perm=>$name) {
 	$var = "perms_$perm";
 	if (exponent_permissions_checkGroup($g,$perm,$loc,true)) $g->$var = 1;
 	else if (exponent_permissions_checkGroup($g,$perm,$loc)) $g->$var = 2;
 	else $g->$var = 0;
 }
 $users[] = $g;
 */
 foreach (exponent_users_getAllGroups() as $g) {
     foreach ($perms as $perm => $name) {
         $var = 'perms_' . $perm;
         if (exponent_permissions_checkGroup($g, $perm, $loc, true)) {
             $g->{$var} = 1;
         } else {
             if (exponent_permissions_checkGroup($g, $perm, $loc)) {
                 $g->{$var} = 2;
             } else {
                 $g->{$var} = 0;
             }
         }
     }
     $users[] = $g;
 }
 $template->assign('have_users', count($users) > 0);
function exponent_users_getGroupsForUser($u, $allow_exclusive = 1, $allow_inclusive = 1)
{
    global $db;
    if ($u == null || !isset($u->id)) {
        // Don't have enough information to consult the membership tables.
        // Return an empty array.
        return array();
    }
    // Holding array for the groups.
    $groups = array();
    if ($u->is_admin == 1) {
        // For administrators, we synthesize group memberships - they effectively
        // belong to all groups.  So, we call exponent_users_getAllGroups, and pass the
        // filtration criteria arguments (2 and 3) to it.
        return exponent_users_getAllGroups($allow_exclusive, $allow_inclusive);
    }
    foreach ($db->selectObjects('groupmembership', 'member_id=' . $u->id) as $m) {
        // Loop over the membership records for this user, and select the full
        // group object for each group.
        $o = $db->selectObject('group', 'id=' . $m->group_id);
        if ($o->inclusive == 1 && $allow_inclusive) {
            // The group is inclusive and the caller has asked us to allow inclusive groups.
            // Append the group object to the end of the holding array.
            $groups[] = $o;
        }
        if ($o->inclusive == 0 && $allow_exclusive) {
            // The group is exclusive and the caller has asked us to allow exclusive groups.
            // Append the group object to the end of the holding array.
            $groups[] = $o;
        }
    }
    // Return the list of group objects to the caller.
    return $groups;
}
 function form($object)
 {
     $i18n = exponent_lang_loadFile('datatypes/privatemessage.php');
     if (!defined('SYS_FORMS')) {
         require_once BASE . 'subsystems/forms.php';
     }
     exponent_forms_initialize();
     $form = new form();
     $users = array();
     $groups = array();
     global $db, $user;
     if (!defined('SYS_USERS')) {
         require_once BASE . 'subsystems/users.php';
     }
     if (exponent_permissions_check('contact_all', exponent_core_makeLocation('InboxModule'))) {
         foreach (exponent_users_getAllUsers() as $u) {
             $users[$u->id] = $u->firstname . ' ' . $u->lastname . ' (' . $u->username . ')';
         }
     } else {
         foreach (exponent_users_getGroupsForUser($user, 1, 0) as $g) {
             foreach (exponent_users_getUsersInGroup($g) as $u) {
                 $users[$u->id] = $u->firstname . ' ' . $u->lastname . ' (' . $u->username . ')';
             }
         }
     }
     // Process other uses who the current user has blocked, and remove them from the list
     // Process other users who have blocked the current user, and remove them from the list.
     foreach ($db->selectObjects('inbox_contactbanned', 'owner=' . $user->id . ' OR user_id=' . $user->id) as $blocked) {
         if ($blocked->user_id == $user->id) {
             // Blocked by someone else.  Remove the owner (user who blocked us)
             unset($users[$blocked->owner]);
         } else {
             if ($blocked->owner == $user->id) {
                 // We blocked the user, remove the blocked user_id
                 unset($users[$blocked->user_id]);
             }
         }
     }
     uasort($users, 'strnatcmp');
     $groups = array();
     foreach ($db->selectObjects('inbox_contactlist', 'owner=' . $user->id) as $g) {
         $groups['list_' . $g->id] = $g->name . ' ' . $i18n['personal_list'];
     }
     if (exponent_permissions_check('contact_all', exponent_core_makeLocation('InboxModule'))) {
         foreach (exponent_users_getAllGroups(1, 0) as $g) {
             $groups['group_' . $g->id] = $g->name . ' ' . $i18n['system_group'];
         }
     } else {
         foreach (exponent_users_getGroupsForUser($user, 1, 0) as $g) {
             $groups['group_' . $g->id] = $g->name . ' ' . $i18n['system_group'];
         }
     }
     uasort($groups, 'strnatcmp');
     $recipient_caption = $i18n['recipient'];
     $group_recipient_caption = $i18n['group_recipient'];
     $btn = new buttongroupcontrol($i18n['save'], '', $i18n['cancel']);
     $object->group_recipient = array();
     if ($object == null || !isset($object->recipient)) {
         $object->subject = '';
         $object->body = '';
         $object->recipient = array();
         if (!count($users) && !count($groups)) {
             $btn->disabled = true;
         }
     } else {
         if (!defined('SYS_USERS')) {
             require_once BASE . 'subsystems/users.php';
         }
         $u = exponent_users_getUserById($object->recipient);
         $form->register(null, '', new htmlcontrol(sprintf($i18n['replyto'], $u->firstname . ' ' . $u->lastname . ' (' . $u->username . ')')));
         $form->meta('replyto', $object->recipient);
         $object->recipient = array();
         unset($users[$u->id]);
         $recipient_caption = $i18n['copyto'];
         $group_recipient_caption = $i18n['group_copyto'];
     }
     if (count($users)) {
         $form->register('recipients', $recipient_caption, new listbuildercontrol($object->recipient, $users));
     }
     if (count($groups)) {
         $form->register('group_recipients', $group_recipient_caption, new listbuildercontrol($object->group_recipient, $groups));
     }
     if (!count($groups) && !count($users)) {
         $form->register(null, '', new htmlcontrol('<div class="error">' . $i18n['nocontacts'] . '</div>'));
     }
     $form->register('subject', $i18n['subject'], new textcontrol($object->subject));
     $form->register('body', $i18n['body'], new htmleditorcontrol($object->body));
     $form->register('submit', '', $btn);
     return $form;
 }