function domain_user_user in Domain Access 6.2
Same name and namespace in other branches
- 5 domain_user/domain_user.module \domain_user_user()
Implement hook_user()
File
- domain_user/
domain_user.module, line 128 - Creates unique subdomains for registered users.
Code
function domain_user_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'view':
$domain = domain_user_lookup($account->uid);
if ($domain != -1) {
$account->content['summary']['domain_user'] = array(
'#type' => 'user_profile_item',
'#title' => t('Personal web site'),
'#weight' => 6,
'#value' => l($domain['path'], $domain['path']),
);
}
break;
case 'register':
case 'form':
// This function will return -1 if no domain exists. If no user exists yet, assume -1.
$domain = -1;
if (isset($account->uid)) {
$domain = domain_user_lookup($account->uid);
}
else {
$account->uid = 0;
$account->roles = array(
0 => t('anonymous user'),
);
}
// New users throw E_ALL errors.
$name = t('username');
if (isset($account->name)) {
// Sanitize the username according to the host RFC.
$name = domain_user_strip_chars($account->name);
}
$default = domain_default();
$root = variable_get('domain_user_root', $default['subdomain']);
// If the user name is on the ban list, we do not create a domain.
// TODO: Maybe we should set a message here.
if ($domain == -1 && domain_lookup(NULL, $name . '.' . $root) == -1) {
$create_domain = variable_get('domain_user', 0);
if (user_access('create personal domain', $account) || user_access('create user domains')) {
// For the add user page, we force the checkbox.
if (empty($account->uid) && arg(0) == 'admin' && $create_domain == 1) {
$create_domain = 2;
}
if ($create_domain == 1 && !empty($root)) {
$form['domain_user_domain']['domain_create_user'] = array(
'#type' => 'value',
'#value' => 1,
);
}
else {
if ($create_domain == 2 && !empty($root)) {
$form['domain_user_domain'] = array(
'#type' => 'fieldset',
'#title' => t('Personal web site'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 1,
);
$form['domain_user_domain']['domain_create_user'] = array(
'#type' => 'checkbox',
'#return_value' => 1,
'#title' => t('Yes, I want to create my own site at <b>!user.!site</b>', array(
'!user' => $name,
'!site' => $root,
)),
);
}
}
return $form;
}
}
break;
case 'insert':
case 'update':
// If we did not come from our expected form, do nothing.
if (!isset($edit['domain_create_user'])) {
return;
}
if (!empty($edit['domain_create_user']) && (user_access('create personal domain', $account) || !user_access('create user domains'))) {
$user_root = variable_get('domain_user_root', variable_get('domain_root', ''));
$name = domain_user_strip_chars($account->name);
$form_state['values']['sitename'] = $account->name;
$form_state['values']['subdomain'] = $name . '.' . $user_root;
$form_state['values']['valid'] = $account->status;
$form_state['values']['user_submitted'] = TRUE;
// This function will return -1 if no domain exists.
$domain = domain_user_lookup($account->uid);
if ($domain == -1) {
// Set arguments to be passed to the form
$arguments = array(
'user_submitted' => TRUE,
'ignore_domain_status_check' => TRUE,
);
// Include the form file.
include_once drupal_get_path('module', 'domain') . '/domain.admin.inc';
// Set the scheme as needed.
$form_state['values']['domain_scheme'] = variable_get('domain_user_scheme', 'http');
drupal_execute('domain_form', $form_state, array(), $arguments);
$domain = domain_lookup(NULL, $form_state['values']['subdomain'], TRUE);
if ($domain['domain_id']) {
db_query("INSERT INTO {domain_user} (domain_id, uid) VALUES (%d, %d)", $domain['domain_id'], $account->uid);
// If this user has never registered before, or settings require it, remove other domain editing permissions.
if (empty($account->login) || !variable_get('domain_user_assign', 0)) {
db_query("DELETE FROM {domain_editor} WHERE uid = %d", $account->uid);
}
db_query("INSERT INTO {domain_editor} (domain_id, uid) VALUES (%d, %d)", $domain['domain_id'], $account->uid);
$edit['domains'][] = $domain['domain_id'];
drupal_set_message(t('Your personal URL is <a href="!url">!url</a>.', array(
'!url' => url($domain['path']),
)));
}
else {
drupal_set_message(t('Your personal URL could not be created.'));
}
}
// Set the user's default domain to their subdomain.
if ($domain['domain_id'] > 0) {
// If the user cannot assign domain editors, only allow their unique domain.
if (!user_access('assign domain editors')) {
$edit['domain_user'] = array();
}
$edit['domain_user'][$domain['domain_id']] = $domain['domain_id'];
// If the user account is blocked, set the domain to invalid.
if ($account->status == 0) {
db_query("UPDATE {domain} SET valid = %d WHERE domain_id = %d", 0, $domain['domain_id']);
}
}
}
if (isset($edit['domain_create_user']) && !(user_access('create personal domain', $account) || user_access('create user domains'))) {
drupal_set_message(t('Your personal URL could not be created.'));
}
// Throw away what we do not need.
$edit['domain_create_user'] = NULL;
$edit['domains'] = NULL;
// Special case if the username has changed.
if ($op == 'update' && isset($edit['name']) && $edit['name'] != $account->name) {
$domain = domain_user_lookup($account->uid, TRUE);
if ($domain != -1) {
$user_root = variable_get('domain_user_root', variable_get('domain_root', ''));
$name = domain_user_strip_chars($edit['name']);
$string = $name . '.' . $user_root;
db_query("UPDATE {domain} SET subdomain = '%s', sitename = '%s' WHERE domain_id = %d", $string, $edit['name'], $domain['domain_id']);
}
}
break;
case 'login':
// If the user has a personal domain, take them there.
$domain = domain_user_lookup($account->uid);
if (variable_get('domain_user_login', 1) && $domain != -1) {
// We cannot do a redirect on login, which forces us to use a $_SESSION variable.
// Only store the uid here, no need to store extra data where it can get hijacked.
$_SESSION['domain_user'] = $account->uid;
}
break;
case 'delete':
// Delete the record
// Run the lookup before we delete the row!
$domain = domain_user_lookup($account->uid);
if ($domain != -1) {
db_query("DELETE FROM {domain} WHERE domain_id = %d", $domain['domain_id']);
// Let other modules act.
module_invoke_all('domainupdate', 'delete', $domain);
}
break;
}
}