'select', '#title' => t('Mode'), '#options' => $modes, '#default_value' => variable_get('subdomain_mode', 'user'), ); if (module_exists('taxonomy')) { $vocabs = taxonomy_get_vocabularies(); $vocab_options = array(); if (is_array($vocabs)) { foreach ($vocabs as $vid => $vocab) { if (!$vocab->tags) { $vocab_options[$vid] = $vocab->name; } } } if (count($vocab_options)) { $form['subdomain_vocab'] = array( '#type' => 'select', '#title' => t('Select a vocabulary'), '#description' => t('Terms in the selected vocabulary will have their own subdomain.'), '#options' => $vocab_options, '#default_value' => variable_get('subdomain_vocab', NULL), ); } else { $form['subdomain_vocab'] = array( '#type' => 'item', '#title' => t('Taxonomy vocabulary'), '#description' => t('No non-tag vocabularies found.
Create one before configuring taxonomy subdomains.
'), '#prefix' => '
', '#suffix' => '
', ); } } $form['subdomain_source'] = array( '#type' => 'select', '#title' => t('Source for subdomain text'), '#options' => array( 'auto' => t('Automatic: created from the user/group/term name'), 'custom' => t('Custom: User can specify a custom subdomain'), ), '#default_value' => variable_get('subdomain_source', 'default'), ); if (module_exists('views')) { $views = views_get_all_views(); $view_options = array(); foreach($views as $name => $view) { if (!$view->disabled) { $view_options[$name] = $name; } } $form['subdomain_home'] = array( '#type' => 'select', '#title' => t('Subdomain homepages'), '#options' => array( 'default' => t('Default'), 'view' => t('Custom view'), ), '#default_value' => variable_get('subdomain_home', 'default'), ); if (count($view_options)) { $form['subdomain_view'] = array( '#type' => 'select', '#title' => t('Select a view'), '#options' => $view_options, '#default_value' => variable_get('subdomain_view', ''), ); } else { $form['subdomain_view'] = array( '#type' => 'item', '#title' => t('Homepage view'), '#description' => t('No enabled views found.
Create a view before selecting a "custom view" for your subdomain homepages.
'), '#prefix' => '
', '#suffix' => '
', ); } } $form['#submit'][] = 'subdomain_admin_settings_submit'; // Ensure server is setup to properly handle subdomains _subdomain_validate_dns(); _subdomain_validate_cookie_domain(); _subdomain_validate_webserver_config(); return system_settings_form($form); } function subdomain_admin_settings_validate($form, &$form_state) { // Extract form values into simple variables foreach(array('mode', 'vocab', 'source', 'home', 'view') as $val) { ${$val} = $form_state['values']['subdomain_'. $val]; } if ($mode == 'term' && empty($vocab)) { form_set_error('subdomain_mode', t('You must create at least one non-tag vocabulary before configuring taxonomy subdomains.')); } if ($home == 'view' && empty($view)) { form_set_error('subdomain_home', t('You must create or enable at least one view before selecting a "Custom view" for your subdomain homepages.')); } elseif ($home == 'view') { // Check for presence of appropriate argument $view = views_get_view($view); $args = $view->display['default']->display_options['arguments']; $first_arg = empty($args) ? NULL : array_shift(array_keys($args)); if (empty($first_arg)) { form_set_error('subdomain_view', t('The selected view has no arguments. Add a uid/nid/tid argument to your view to filter your content for the active subdomain.')); } elseif ($mode == 'term' && $first_arg != 'tid' || $mode == 'group' && !in_array($first_arg, array('nid', 'group_nid')) || $mode == 'user' && $first_arg != 'uid') { drupal_set_message(t("The selected view's first argument was @arg. This may not work as expected.", array('@arg' => $first_arg)), 'warning'); } } } function subdomain_admin_settings_submit($form, &$form_state) { // If mode has changed, wipe subdomain tables // TODO: need user to confirm this... if ($form_state['values']['subdomain_mode'] != $form['subdomain_mode']['#default_value']) { db_query("DELETE FROM {subdomain};"); db_query("DELETE FROM {subdomain_content};"); } // _subdomain_clear_caches(); } // TODO: probably don't need function _subdomain_clear_caches() { // Flush all caches; no need to re-implement this. module_load_include('inc', 'system', 'system.admin'); $form = $form_state = array(); system_clear_cache_submit($form, $form_state); } function _subdomain_validate_dns($display_error = TRUE) { $domain = trim(subdomain_get_domain(), '.'); $subdomain = SUBDOMAIN_TEST_SUBDOMAIN .'.'. $domain; $domain_ip = gethostbyname($domain); $subdomain_ip = gethostbyname($subdomain); if ($domain_ip != $subdomain_ip && $display_error) { drupal_set_message(t("Subdomain error: @domain and @subdomain did not resolve to the same IP address. Your DNS may be improperly configured and subdomains will likely not work.", array('@domain' => $domain, '@subdomain' => $subdomain)), 'error'); } return $subdomain_ip == $domain_ip; } function _subdomain_validate_webserver_config() { if (_subdomain_validate_dns(FALSE)) { $domain = trim(subdomain_get_domain(), '.'); $subdomain = SUBDOMAIN_TEST_SUBDOMAIN .'.'. $domain; $url = "http://$subdomain/subdomain/test"; if (drupal_http_request($url)->data != SUBDOMAIN_TEST_RESPONSE) { drupal_set_message(t("Subdomain error: @subdomain was not correctly routed to this site. Ensure your webserver is setup to correctly route wildcard subdomains to this site", array('@domain' => $domain, '@subdomain' => $subdomain)), 'error'); } } } function _subdomain_validate_cookie_domain($display_error = TRUE) { $settings = file_get_contents('./'. conf_path() . '/settings.php'); $settings = str_replace(chr(13) . chr(10), "\n", $settings); $settings = explode("\n", $settings); foreach ($settings as $setting) { if (strpos($setting, '$cookie_domain') !== FALSE) { eval($setting); } } if (!isset($cookie_domain) && $display_error) { drupal_set_message(t('Subdomain error: The $cookie_domain variable in settings.php must be set or subdomains will not work.'), 'error'); } return isset($cookie_domain); } function _subdomain_test_request() { echo SUBDOMAIN_TEST_RESPONSE; exit; }