'subdomain_view_homepage', 'access callback' => TRUE, ); $items['admin/settings/subdomain'] = array( 'title' => t('Subdomain Settings'), 'description' => t('Configure subdomain settings.'), 'page callback' => 'subdomain_admin', 'page arguments' => array('subdomain_admin_settings'), 'access arguments' => array('administer site configuration'), 'file' => 'includes/subdomain.admin.inc', ); $items['admin/settings/subdomain/general'] = array( 'title' => t('General'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1, ); $items['admin/settings/subdomain/tools'] = array( 'title' => t('Tools'), 'type' => MENU_LOCAL_TASK, 'page callback' => 'subdomain_tools', 'file' => 'includes/subdomain.admin.inc', 'access arguments' => array('administer site configuration'), 'weight' => 0, ); $items['admin/settings/subdomain/filters'] = array( 'title' => t('Filters'), 'type' => MENU_LOCAL_TASK, 'page callback' => 'drupal_get_form', 'page arguments' => array('subdomain_filters_form'), 'file' => 'includes/subdomain.admin.inc', 'access arguments' => array('administer site configuration'), 'weight' => 0, ); $items['subdomain/validate'] = array( 'page callback' => 'subdomain_js_validate', 'access callback' => TRUE, ); $items['subdomain/test'] = array( 'page callback' => '_subdomain_test_request', 'access callback' => TRUE, 'file' => 'includes/subdomain.admin.inc' ); return $items; } /** * Implementation of hook_form_alter(). */ function subdomain_form_alter(&$form, $form_state, $form_id) { // Add subdomain field (if necessary) to appropriate entry form if (subdomain()->is_entry_form($form_id)) { subdomain()->entry_form_alter($form, $form_id); } } /** * Implementation of hook_user(). */ function subdomain_user($op, &$edit, &$account, $category = NULL) { if (subdomain()->mode == 'user') { if ($op == 'validate') { // If new user, check if subdomain exists if (!isset($edit['_account']) && subdomain()->exists($edit[subdomain()->form_field])) { form_set_error($field, t('Subdomain already taken. Please try a different one.')); } } else { // add uid to $edit, so save/delete handlers can get to it $edit['uid'] = $account->uid; subdomain()->data_handler($op, $edit); } } } /** * Implementation of hook_nodeapi(). */ function subdomain_nodeapi(&$node, $op, $teaser, $page) { if (subdomain()->mode == 'group') { subdomain()->data_handler($op, $node); } } /** * Implementation of hook_taxonomy(). */ function subdomain_taxonomy($op, $type, $array) { if (subdomain()->mode == 'term' && $type == 'term') { subdomain()->data_handler($op, $array); } } /** * Implementation of hook_node_type(). */ function subdomain_node_type($op, $info) { if (subdomain()->mode == 'contenttype') { subdomain()->data_handler($op, $info); } } /** * Inbound URL rewrite helper * If host includes subdomain, rewrite URI and internal path if necessary */ function subdomain_url_inbound_alter(&$result, $path, $path_language) { static $executed = FALSE; // Only investigate URL on 1st execution if (!$executed) { $executed = TRUE; // Fetch subdomain ID if ($subdomain = subdomain_get_current_subdomain()) { // Do nothing if subdomain is "www" or reserved if ($subdomain == 'www' && variable_get('subdomain_prepend_www', TRUE) || in_array($subdomain, subdomain_get_reserved_subdomains())) { return; } // Lookup subdomain ID $sid = subdomain_get_sid($subdomain); // If subdomain doesn't exist & non-node path, redirect to base domain // When deleting a subdomain object, this prevents user from ending up on // on page not found. if (empty($sid) && !_subdomain_id_from_path('nid', $path)) { $www = variable_get('subdomain_prepend_www', TRUE) ? 'www.' : ''; header("Location: ". _subdomain_get_protocol() . $www . subdomain_get_domain() . '/'. $path, TRUE, 301); exit; } // If subdomain doesn't exist, send user to page not found // Make exception for test subdomain elseif (empty($sid) && $subdomain != SUBDOMAIN_TEST_SUBDOMAIN) { $result = $_REQUEST['q'] = 'subdomain/notfound/'. $_SERVER['HTTP_HOST'] .'/'. $path; } } // If subdomain and no path, rewrite to subdomain homepage if (!empty($sid) && !isset($_REQUEST['q'])) { $result = $_REQUEST['q'] = subdomain()->get_homepage_path($sid); } elseif ($nid = _subdomain_id_from_path('nid', $result)) { // If the path is a node path for the current node-based subdomain (e.g. an og group edit path: node/%/edit), do nothing // TODO: Remember/figure-out why this is needed and document! if (subdomain()->type() == 'node' && isset($sid) && $nid == $sid) { // do nothing } // If we're viewing a node, ensure we're on the correct subdomain // Force PageNotFound if user attempting to view content on incorrect subdomain... elseif (subdomain()->get_content_subdomain($nid) != subdomain_get_current_subdomain()) { $result = $_REQUEST['q'] = 'subdomain/notfound/'. $_SERVER['HTTP_HOST'] .'/'. $path; } } } } /** * Outbound URL rewrite helper * Rewrite URLs: attach/remove subdmain as appropriate */ function subdomain_url_outbound_alter(&$path, &$options, $original_path, $clear_path_cache = FALSE) { // Cache outbound paths static $paths = array(); // Clear path cache (sometimes needed when new subdomains created - e.g. og) if ($clear_path_cache) { $paths = array(); } // Process path if not in cache if (!isset($paths[$original_path])) { $www = variable_get('subdomain_prepend_www', TRUE) ? 'www.' : ''; // If front page and we're on a subdomain, strip the subdomain if (($original_path == '' || $original_path == '') && subdomain_get_current_subdomain()) { $paths[$original_path]['path'] = $path; $paths[$original_path]['base_url'] = _subdomain_get_protocol() . $www . subdomain_get_domain(); } // Otherwise, process path and attach/remove subdomain if necessary else { // Determine subdomain if any from original_path, and alter path if necessary subdomain()->url_outbound_helper($subdomain, $path, $original_path, $is_system_path); // Cache $path in case it was altered $paths[$original_path]['path'] = $path; if ($is_system_path && subdomain_get_current_subdomain()) { $paths[$original_path]['base_url'] = _subdomain_get_protocol() . subdomain_get_current_subdomain() .'.'. subdomain_get_domain(); } elseif ($subdomain) { $paths[$original_path]['base_url'] = _subdomain_get_protocol() . $subdomain .'.'. subdomain_get_domain(); } else { $paths[$original_path]['base_url'] = _subdomain_get_protocol() . $www . subdomain_get_domain(); } } } // Set path and options $options['absolute'] = TRUE; $options['base_url'] = $paths[$original_path]['base_url']; $path = $paths[$original_path]['path']; } /** * Clear outbound URL cache */ function subdomain_url_outbound_cache_clear() { subdomain_url_outbound_alter($path, $options = array(), NULL, $clear_path_cache = TRUE); } function subdomain_view_homepage() { $output = ''; if (subdomain()->home == 'view') { // Build argument array for view $args = arg(); // Remove "subdomain/homepage" portions of path array_shift($args); array_shift($args); // Load and execute view $view = views_get_view(subdomain()->view); $output = $view->preview('default', $args); drupal_set_title($view->get_title()); } return $output; } function subdomain_get_reserved_subdomains() { $reserved = variable_get('subdomain_reserved', ''); $reserved = strtolower(ereg_replace(chr(13) . chr(10), "\n", $reserved)); $reserved = explode("\n", $reserved); // Add internal test subdomain to reserved list array_unshift($reserved, SUBDOMAIN_TEST_SUBDOMAIN); return $reserved; } function subdomain_get_domain() { global $cookie_domain; return trim($cookie_domain, '.'); } /** * Returns current subdomain if any */ function subdomain_get_current_subdomain() { static $subdomain; if (!isset($subdomain)) { $subdomain_length = strpos($_SERVER['HTTP_HOST'], '.'. subdomain_get_domain()); $subdomain = substr($_SERVER['HTTP_HOST'], 0, $subdomain_length); if (variable_get('subdomain_prepend_www', TRUE) && $subdomain == 'www') { $subdomain = ''; } } return $subdomain; } function subdomain_get_sid($subdomain) { static $sids = array(); if (!isset($sids[$subdomain])) { $sids[$subdomain] = db_result(db_query("SELECT sid FROM {subdomain} WHERE subdomain = '%s'", subdomain()->clean($subdomain))); } return $sids[$subdomain]; } function subdomain_get_subdomain($sid) { static $subdomains = array(); if (!isset($subdomains[$sid])) { $subdomains[$sid] = db_result(db_query("SELECT subdomain FROM {subdomain} WHERE sid = %d", $sid)); } return $subdomains[$sid]; } /** * JS form validation handler: checks whether subdomain already exists */ function subdomain_js_validate() { $reserved = subdomain_get_reserved_subdomains(); $is_reserved = in_array($_GET['subdomain'], $reserved); $sid = subdomain_get_sid($_GET['subdomain']); // Subdomain is valid if it doesn't exist or it's the one being edited drupal_json(array('valid' => !$is_reserved && (empty($sid) || $sid == $_GET['sid']))); exit; }