type)) { switch ($op) { case 'validate': $node->title = trim($node->title); // Allow only letters and spaces if (!preg_match('!^[a-zA-Z ]+$!', $node->title)) { form_set_error('title', 'The Group Name can contain only letters and spaces'); } // Disallow reserved subdomain elseif (subdomain_reserved($node->title)) { form_set_error('title', "Sorry. '$node->title' is not available for use. Please choose a different name for your group."); } // Ensure group name is unique elseif (db_result(db_query("SELECT 1 FROM {node} WHERE nid <> %d AND type = '%s' AND title = '%s'", $node->nid, $node->type, $node->title))) { form_set_error('title', 'There is already a Group with that name. Please choose a different name.'); } // Disallow changes to group name (to avoid SEO issues and path alias getting out of sync with group content aliases) if ($node->nid) { $currentgroupname = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $node->nid)); if (strtolower($currentgroupname) != strtolower($node->title)) { form_set_error('title', 'Sorry. group names are permanent and may not be changed. You may only change capitalization'); } } break; case 'load': $node->subdomain = subdomain_raw_to_subdomain($node->title); break; } } } /** * Implementation of hook_user */ function subdomain_user($op, &$edit, &$user_edit, $category = NULL) { if ($op == 'validate' && variable_get('subdomain_type', SUBDOMAIN_GROUP) == SUBDOMAIN_USER) { // for SEO, disallow username changes if subdomains are set to Node Authors (TODO : make configurable) if (is_numeric($user_edit->uid) && $user_edit->name != $edit['name']) { form_set_error('name', 'Sorry. Usernames are permanent and cannot be changed'); } // Allow only letters and spaces in usernames elseif (!preg_match('!^[a-zA-Z ]+$!', $edit['name'])) { form_set_error('name', 'User Names can contain only letters and spaces'); } } } /** * Returns True if $subdomain is reserved */ function subdomain_reserved($name = NULL) { $subdomain = subdomain_raw_to_subdomain($name); $reserved_subdomain = ereg_replace(chr(13) . chr(10), "\n", variable_get('subdomain_reserved', '')); if (in_array($subdomain, explode("\n", $reserved_subdomain))) { return TRUE; } } /** * Returns base domain minus subdomain (e.g. example.com) */ function subdomain_get_domain() { $host = explode('.', $_SERVER['HTTP_HOST']); $tld = array_pop($host); $domain = array_pop($host); return $domain .'.'. $tld; } /** * Looks for subdomain token in Drupal generated URLs; if present, prepends to domain * NOTE: Requires a 1-line patch to includes/common.inc (See install.txt) */ function subdomain_url_rewrite(&$path, &$base) { if (substr($path, 0, 1) == SUBDOMAIN_TOKEN_PREFIX) { $path_parts = explode('/', substr($path, 1)); $base = 'http://'. array_shift($path_parts) .'.'. subdomain_get_domain() .'/'; $path = implode('/', $path_parts); } else { $base = 'http://'. subdomain_get_domain() . '/'; } } /** * Implementation of hook_token_list() */ function subdomain_token_list($type = 'all') { if ($type == 'node') { $tokens['node']['subdomain'] = t('Subdomain token (place at the start of the path)'); } if ($type == 'taxonomy') { $tokens['taxonomy']['subdomain'] = t('Subdomain token (place at the start of the path)'); } elseif ($type == 'user') { $tokens['user']['subdomain'] = t('Subdomain token (place at the start of the path)'); } return $tokens; } /** * Implementation of hook_token_values() */ function subdomain_token_values($type, $object = NULL) { $values['subdomain'] = NULL; switch ($type) { case 'node': // Are we rewriting OG groups? or Node authors? $type = variable_get('subdomain_type', SUBDOMAIN_GROUP); if ($type == SUBDOMAIN_GROUP) { if (og_is_group_type($object->type)) { $values['subdomain'] = subdomain_build_token($object->title, $object->nid); } elseif (isset($object->og_groups)) { $groupname = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $object->og_groups[0])); // 1st Group in array is used for subdomain $values['subdomain'] = subdomain_build_token($groupname, $object->og_groups[0]); } else { $values['subdomain'] = ''; } } elseif ($type == SUBDOMAIN_USER) { if (!empty($object->name)) { $values['subdomain'] = subdomain_build_token($object->name, $object->uid); } } break; case 'taxonomy': if (module_exists("og_forum")) { if ($object->group_id) { // NOTE : depends on custom code in the "insert" section of og_forum_nodeapi setting the group_id attribute $group_name = db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $object->group_id)); $value = subdomain_build_token($group_name, $object->group_id); } elseif ($result = db_fetch_object(db_query("SELECT title, n.nid FROM {node} n INNER JOIN og ON n.nid = og.nid INNER JOIN og_term ogt ON og.nid = ogt.nid WHERE ogt.tid = %d", $object->tid))) { $value = subdomain_build_token($result->title, $result->nid); } $values['subdomain'] = $value; } break; case 'user': if (variable_get('subdomain_type', SUBDOMAIN_GROUP) == SUBDOMAIN_USER) { $values['subdomain'] = subdomain_build_token($object->name, $object->uid); } } return $values; } function subdomain_menu($may_cache) { if ($may_cache) { $items[] = array( 'path' => 'admin/settings/subdomain', 'title' => t('Subdomain Settings'), 'description' => t('Configure subdomain settings.'), 'callback' => 'drupal_get_form', 'callback arguments' => array('subdomain_admin_settings'), 'access' => user_access('administer site configuration') ); } return $items; } function subdomain_admin_settings() { $default = implode("\n", array('www', 'static')); if (module_exists('og')) { $options = array(SUBDOMAIN_GROUP => 'Organic Groups', SUBDOMAIN_USER => 'Node Authors'); } else { $options = array(SUBDOMAIN_GROUP => 'Node Authors'); } drupal_add_js(drupal_get_path('module', 'subdomain') . '/subdomain.js'); $form['subdomain_settings']['subdomain_type'] = array( '#type' => 'select', '#title' => t('Create subdomains for'), '#options' => $options, '#default_value' => variable_get('subdomain_type', SUBDOMAIN_GROUP) ); $form['subdomain_settings']['subdomain_style'] = array( '#type' => 'select', '#title' => t('Using'), '#options' => array(SUBDOMAIN_STYLE_NAME => 'Group or Node Author name', SUBDOMAIN_STYLE_CUSTOM => 'Custom...'), '#default_value' => variable_get('subdomain_style', SUBDOMAIN_STYLE_NAME) ); $form['subdomain_settings']['subdomain_custom'] = array( '#type' => 'textfield', '#title' => t('Custom subdomain'), '#description' => 'You may use the tokens %name% and %id% for group-name/user-name and group-nid/user-uid respectively', '#default_value' => variable_get('subdomain_custom', NULL), '#prefix' => '