You are here

function domain_user_user in Domain Access 5

Same name and namespace in other branches
  1. 6.2 domain_user/domain_user.module \domain_user_user()

Implement hook_user()

File

domain_user/domain_user.module, line 191
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) {
        $items['domain'] = array(
          'title' => t(''),
          'value' => l($domain['path'], $domain['path']),
          'class' => '',
        );
        return array(
          t('Personal web site') => $items,
        );
      }
      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);
      }

      // 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')) {
          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_root = variable_get('domain_user_root', variable_get('domain_root', ''));
        $name = domain_user_strip_chars($account->name);
        $form_values['sitename'] = $account->name;
        $form_values['subdomain'] = $name . '.' . $user_root;
        $form_values['valid'] = $account->status;
        $form_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,
          );

          // Include the form file.
          include_once drupal_get_path('module', 'domain') . '/domain_admin.inc';

          // Set the scheme as needed.
          $form_values['domain_scheme'] = variable_get('domain_user_scheme', 'http');
          drupal_execute('domain_create_form', $form_values, $arguments);
          $domain = domain_lookup(NULL, $form_values['subdomain'], TRUE);
          if ($domain['domain_id']) {
            db_query("INSERT INTO {domain_user} 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']) {

          // 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 = 0 WHERE domain_id = %d", $domain['domain_id']);
          }
        }
      }
      if ($edit['domain_create_user'] && !user_access('create personal domain', $account)) {
        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' && $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;
  }
}