| 1 |
<?php
|
| 2 |
// $Id: multidomain.module,v 1.3 2008/11/04 04:47:27 betz Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Don't rewrite node SQL to filter by taxonomy.
|
| 6 |
*/
|
| 7 |
define('MULTIDOMAIN_TAXONOMY_REWRITE_NONE', 0);
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Rewrite node SQL to filter by taxonomy for non-default domains.
|
| 11 |
*/
|
| 12 |
define('MULTIDOMAIN_TAXONOMY_REWRITE_NON_DEFAULT', 1);
|
| 13 |
|
| 14 |
/**
|
| 15 |
* Rewrite node SQL to filter by taxonomy for all domains.
|
| 16 |
*/
|
| 17 |
define('MULTIDOMAIN_TAXONOMY_REWRITE_ALL', 2);
|
| 18 |
|
| 19 |
/**
|
| 20 |
* Implementation of hook_help().
|
| 21 |
*/
|
| 22 |
function multidomain_help($section) {
|
| 23 |
switch ($section) {
|
| 24 |
case 'admin/settings/multidomain':
|
| 25 |
return t('The multi-domain module allows you to configure multiple domains for your content to be hosted on. You can configure several different domains, and the rules for matching them.');
|
| 26 |
break;
|
| 27 |
}
|
| 28 |
}
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_perm().
|
| 32 |
*/
|
| 33 |
function multidomain_perm() {
|
| 34 |
return array('administer multidomain');
|
| 35 |
}
|
| 36 |
|
| 37 |
/**
|
| 38 |
* Implementation of hook_menu().
|
| 39 |
*/
|
| 40 |
function multidomain_menu($may_cache = true) {
|
| 41 |
global $user;
|
| 42 |
$items = array();
|
| 43 |
if ($may_cache) {
|
| 44 |
} else {
|
| 45 |
$domains = variable_get('multidomain_sites', array());
|
| 46 |
|
| 47 |
$items[] = array(
|
| 48 |
'path' => 'admin/settings/multidomain', 'title' => t('Multiple domains'),
|
| 49 |
'callback' => 'drupal_get_form',
|
| 50 |
'callback arguments' => array('multidomain_settings'),
|
| 51 |
'access' => user_access('administer multidomain'),
|
| 52 |
'description' => t('Configure which domains your site will be hosted on.'),
|
| 53 |
);
|
| 54 |
|
| 55 |
$items[] = array(
|
| 56 |
'path' => 'admin/settings/multidomain/list',
|
| 57 |
'title' => t('List'),
|
| 58 |
'callback' => 'drupal_get_form',
|
| 59 |
'callback arguments' => array('multidomain_settings'),
|
| 60 |
'access' => user_access('administer multidomain'),
|
| 61 |
'description' => t('Configure which domains your site will be hosted on.'),
|
| 62 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 63 |
'weight' => -10
|
| 64 |
);
|
| 65 |
$items[] = array(
|
| 66 |
'path' => 'admin/settings/multidomain/settings',
|
| 67 |
'title' => t('Configure'),
|
| 68 |
'description' => t('The description of the menu item. It is used as title attribute and on the administration overview page.'),
|
| 69 |
'callback' => 'drupal_get_form',
|
| 70 |
'callback arguments' => array('multidomain_default_domain_settings'),
|
| 71 |
'access' => user_access('administer multidomain'),
|
| 72 |
'type' => MENU_LOCAL_TASK,
|
| 73 |
);
|
| 74 |
$items[] = array(
|
| 75 |
'path' => 'admin/settings/multidomain/settings/default',
|
| 76 |
'title' => t('Default domain'),
|
| 77 |
'callback' => 'drupal_get_form',
|
| 78 |
'callback arguments' => array('multidomain_default_domain_settings'),
|
| 79 |
'access' => user_access('administer multidomain'),
|
| 80 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 81 |
);
|
| 82 |
|
| 83 |
foreach ($domains as $url => $data) {
|
| 84 |
$items[] = array(
|
| 85 |
'path' => 'admin/settings/multidomain/settings/'. str_replace('://', '_', $url),
|
| 86 |
'title' => t('@domain', array('@domain' => $url)),
|
| 87 |
'callback' => 'drupal_get_form',
|
| 88 |
'callback arguments' => array('multidomain_domain_settings', $url),
|
| 89 |
'access' => user_access('administer multidomain'),
|
| 90 |
'type' => MENU_LOCAL_TASK,
|
| 91 |
);
|
| 92 |
$items[] = array(
|
| 93 |
'path' => 'admin/settings/multidomain/delete/'. str_replace('://', '_', $url),
|
| 94 |
'title' => t('@domain', array('@domain' => $url)),
|
| 95 |
'callback' => 'drupal_get_form',
|
| 96 |
'callback arguments' => array('multidomain_domain_delete', $url),
|
| 97 |
'access' => user_access('administer multidomain'),
|
| 98 |
'type' => MENU_CALLBACK,
|
| 99 |
);
|
| 100 |
}
|
| 101 |
}
|
| 102 |
return $items;
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Implementation of hook_term_path().
|
| 107 |
*
|
| 108 |
* Link to the home page of the relevant domain.
|
| 109 |
* Because of the hook_nodeapi() implementation, these links shouldn't
|
| 110 |
* normally show up in any case in regular node views.
|
| 111 |
*/
|
| 112 |
function multidomain_term_path($term) {
|
| 113 |
return $term->name;
|
| 114 |
}
|
| 115 |
|
| 116 |
/**
|
| 117 |
* Implementation of hook_nodeapi().
|
| 118 |
*
|
| 119 |
* Remove terms in the multiple domain vocabulary from the node's
|
| 120 |
* array before viewing.
|
| 121 |
*/
|
| 122 |
function multidomain_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
|
| 123 |
switch($op) {
|
| 124 |
case 'view':
|
| 125 |
if (isset($node->taxonomy)) {
|
| 126 |
$vid = variable_get('multidomain_vocabulary', '');
|
| 127 |
foreach ($node->taxonomy as $index => $term) {
|
| 128 |
if ($term->vid == $vid) {
|
| 129 |
unset($node->taxonomy[$index]);
|
| 130 |
}
|
| 131 |
}
|
| 132 |
}
|
| 133 |
break;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
|
| 137 |
function multidomain_default_domain_settings() {
|
| 138 |
$form = array();
|
| 139 |
$form['singlesignon_master_url'] = array(
|
| 140 |
'#type' => 'textfield',
|
| 141 |
'#title' => t('Primary domain name'),
|
| 142 |
'#description' => t('The domain name for content to be hosted on, provided no other settings are found. This is the same value as the single signon primary domain.'),
|
| 143 |
'#default_value' => variable_get('singlesignon_master_url', 'http://'),
|
| 144 |
'#validate' => array('multidomain_valid_domain' => array()),
|
| 145 |
'#weight' => -10,
|
| 146 |
'#size' => 40,
|
| 147 |
'#maxlength' => 255,
|
| 148 |
);
|
| 149 |
|
| 150 |
$form['multidomain_force_default_domain'] = array(
|
| 151 |
'#type' => 'checkbox',
|
| 152 |
'#title' => t('Force default domain'),
|
| 153 |
'#default_value' => variable_get('multidomain_force_default_domain', true),
|
| 154 |
'#description' => t("Always use this domain if no matching domains were found? This option is recommended, but it might be necessary to disable this for certain configurations of shared tables.")
|
| 155 |
);
|
| 156 |
|
| 157 |
$form['taxonomy'] = array(
|
| 158 |
'#type' => 'fieldset',
|
| 159 |
'#title' => t('Taxonomy settings'),
|
| 160 |
);
|
| 161 |
|
| 162 |
$vid = variable_get('multidomain_vocabulary', '');
|
| 163 |
$vocabulary = taxonomy_get_vocabulary($vid);
|
| 164 |
|
| 165 |
$form['taxonomy']['multidomain_taxonomy_rewrite_sql'] = array(
|
| 166 |
'#type' => 'radios',
|
| 167 |
'#title' => t('Content filtering by domain'),
|
| 168 |
'#default_value' => variable_get('multidomain_taxonomy_rewrite_sql', MULTIDOMAIN_TAXONOMY_REWRITE_NONE),
|
| 169 |
'#options' => array(MULTIDOMAIN_TAXONOMY_REWRITE_NONE => t('None: all domains see all content'), MULTIDOMAIN_TAXONOMY_REWRITE_NON_DEFAULT => t('All except default: the default domain sees all content while other domains see only their own content'), MULTIDOMAIN_TAXONOMY_REWRITE_ALL => t('All: all domains, including the default, see only their own content')),
|
| 170 |
'#description' => t('Content may be assigned to a domain by category. Select whether you want each domain to see only content assigned to itself.'),
|
| 171 |
);
|
| 172 |
|
| 173 |
$form['taxonomy']['multidomain_nodes'] = array(
|
| 174 |
'#type' => 'checkboxes',
|
| 175 |
'#title' => t('Domain vocabulary node types'),
|
| 176 |
'#default_value' => $vocabulary->nodes,
|
| 177 |
'#options' => node_get_types('names'),
|
| 178 |
'#description' => t('A list of node types you want to be associated with domains.'),
|
| 179 |
'#required' => TRUE,
|
| 180 |
);
|
| 181 |
|
| 182 |
$form = system_settings_form($form);
|
| 183 |
// Don't use the default form handling.
|
| 184 |
unset($form['#base']);
|
| 185 |
return $form;
|
| 186 |
}
|
| 187 |
|
| 188 |
/**
|
| 189 |
* Form submit callback. Save additional data not saved by default.
|
| 190 |
*/
|
| 191 |
function multidomain_default_domain_settings_submit($form_id, $form_values) {
|
| 192 |
$op = isset($form_values['op']) ? $form_values['op'] : '';
|
| 193 |
if ($op != t('Reset to defaults')) {
|
| 194 |
$vid = variable_get('multidomain_vocabulary', '');
|
| 195 |
$vocabulary = (array) taxonomy_get_vocabulary($vid);
|
| 196 |
$vocabulary['nodes'] = array_filter($form_values['multidomain_nodes']);
|
| 197 |
taxonomy_save_vocabulary($vocabulary);
|
| 198 |
// Create or update a term associated with this domain.
|
| 199 |
if ($form_values['singlesignon_master_url'] != variable_get('singlesignon_master_url', 'http://')) {
|
| 200 |
if ($tid = variable_get('multidomain_master_domain_tid', 0)) {
|
| 201 |
$term = (array) taxonomy_get_term($tid);
|
| 202 |
}
|
| 203 |
else {
|
| 204 |
$term = array(
|
| 205 |
'description' => '',
|
| 206 |
'vid' => $vid,
|
| 207 |
// Set a low weigth as this is the primary domain.
|
| 208 |
'weight' => -10,
|
| 209 |
);
|
| 210 |
}
|
| 211 |
// In either case, set the new name.
|
| 212 |
$term['name'] = $form_values['singlesignon_master_url'];
|
| 213 |
taxonomy_save_term($term);
|
| 214 |
variable_set('multidomain_master_domain_tid', $term['tid']);
|
| 215 |
}
|
| 216 |
}
|
| 217 |
// We don't want this saved as a variable.
|
| 218 |
unset($form_values['multidomain_nodes']);
|
| 219 |
// Save the remaining settings.
|
| 220 |
system_settings_form_submit($form_id, $form_values);
|
| 221 |
}
|
| 222 |
|
| 223 |
function multidomain_domain_delete($url) {
|
| 224 |
$form = array();
|
| 225 |
$form['url'] = array('#type' => 'hidden', '#value' => $url);
|
| 226 |
return confirm_form($form, t('Do you want to delete the @domain domain?', array('@domain' => $url)), 'admin/settings/multidomain');
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Menu callback. Delete a domain.
|
| 231 |
*
|
| 232 |
* %todo Delete the taxonomy term associated with this domain?
|
| 233 |
*/
|
| 234 |
function multidomain_domain_delete_submit($form_id, $values) {
|
| 235 |
$domains = variable_get('multidomain_sites', array());
|
| 236 |
unset($domains[$values['url']]);
|
| 237 |
variable_set('multidomain_sites', $domains);
|
| 238 |
}
|
| 239 |
|
| 240 |
function multidomain_valid_domain($element) {
|
| 241 |
if (!(preg_match('/^https:\/\//i', $element['#value']) || preg_match('/^http:\/\//i', $element['#value']))) {
|
| 242 |
form_error($element, t('Domains must be preceded by http:// or https://.'));
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
function multidomain_domain_settings($url = 'default') {
|
| 247 |
$domains = variable_get('multidomain_sites', array());
|
| 248 |
$edit = $domains[$url];
|
| 249 |
|
| 250 |
$form = array();
|
| 251 |
$form['tid'] = array('#type' => 'value', '#value' => isset($edit['tid']) ? $edit['tid'] : '');
|
| 252 |
$form['url'] = array('#type' => 'hidden', '#value' => $url);
|
| 253 |
$form['domain'] = array(
|
| 254 |
'#type' => 'textfield',
|
| 255 |
'#title' => t('Domain name'),
|
| 256 |
'#description' => t('The domain name of this url. The http:// is required.'),
|
| 257 |
'#default_value' => $url,
|
| 258 |
'#size' => 40,
|
| 259 |
'#maxlength' => 255,
|
| 260 |
'#validate' => array('multidomain_valid_domain' => array()),
|
| 261 |
);
|
| 262 |
|
| 263 |
$form['weight'] = array(
|
| 264 |
'#type' => 'weight',
|
| 265 |
'#title' => t('Weight'),
|
| 266 |
'#description' => t('The weight defines which domain will get used. Domains with lower weights get matched first.'),
|
| 267 |
'#delta' => 10,
|
| 268 |
'#default_value' => $edit['weight'],
|
| 269 |
);
|
| 270 |
|
| 271 |
// Add variables that can be set for this domain.
|
| 272 |
$form['variables'] = array(
|
| 273 |
'#type' => 'fieldset',
|
| 274 |
'#title' => t('Site configuration options'),
|
| 275 |
'#collapsible' => TRUE,
|
| 276 |
'#tree' => TRUE,
|
| 277 |
);
|
| 278 |
|
| 279 |
$forms = array(
|
| 280 |
// Key is form_id.
|
| 281 |
'system_site_information_settings' => array(
|
| 282 |
// Values are keys of form elements.
|
| 283 |
array('site_name'),
|
| 284 |
array('site_mail'),
|
| 285 |
array('site_slogan'),
|
| 286 |
array('site_mission'),
|
| 287 |
array('site_footer'),
|
| 288 |
array('site_frontpage'),
|
| 289 |
),
|
| 290 |
'menu_configure' => array(
|
| 291 |
array('settings_links', 'menu_primary_menu'),
|
| 292 |
array('settings_links', 'menu_secondary_menu'),
|
| 293 |
),
|
| 294 |
);
|
| 295 |
|
| 296 |
$form['variables'] += multidomain_get_form_bits($forms);
|
| 297 |
|
| 298 |
// Add default theme select.
|
| 299 |
$options = array();
|
| 300 |
foreach (list_themes() as $theme) {
|
| 301 |
if ($theme->status) {
|
| 302 |
$options[$theme->name] = $theme->name;
|
| 303 |
}
|
| 304 |
}
|
| 305 |
$form['variables']['theme_default'] = array(
|
| 306 |
'#type' => 'select',
|
| 307 |
'#title' => t('Default theme'),
|
| 308 |
'#options' => $options,
|
| 309 |
'#default_value' => variable_get('theme_default', 'garland'),
|
| 310 |
);
|
| 311 |
|
| 312 |
$types = node_get_types();
|
| 313 |
$form['page_vis_settings']['types'] = array(
|
| 314 |
'#type' => 'checkboxes',
|
| 315 |
'#title' => t('Use this domain for the following types of posts'),
|
| 316 |
'#options' => drupal_map_assoc(array_keys($types)),
|
| 317 |
'#default_value' => $edit['types'],
|
| 318 |
);
|
| 319 |
|
| 320 |
$access = user_access('use PHP for block visibility');
|
| 321 |
if ($edit['visibility'] == 2 && !$access) {
|
| 322 |
$form['page_vis_settings'] = array();
|
| 323 |
$form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
|
| 324 |
$form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
|
| 325 |
}
|
| 326 |
else {
|
| 327 |
$options = array(t('Match every page except the listed pages.'), t('Match only the listed pages.'));
|
| 328 |
$description = t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
|
| 329 |
|
| 330 |
if ($access) {
|
| 331 |
$options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
|
| 332 |
$description .= ' '. t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
|
| 333 |
}
|
| 334 |
$form['page_vis_settings']['visibility'] = array(
|
| 335 |
'#type' => 'radios',
|
| 336 |
'#title' => t('Use this domain for specific pages'),
|
| 337 |
'#options' => $options,
|
| 338 |
'#default_value' => $edit['visibility'],
|
| 339 |
);
|
| 340 |
$form['page_vis_settings']['pages'] = array(
|
| 341 |
'#type' => 'textarea',
|
| 342 |
'#title' => t('Pages'),
|
| 343 |
'#default_value' => $edit['pages'],
|
| 344 |
'#description' => $description,
|
| 345 |
);
|
| 346 |
}
|
| 347 |
|
| 348 |
$form['init'] = array(
|
| 349 |
'#type' => 'textarea',
|
| 350 |
'#title' => t('Initialization code'),
|
| 351 |
'#description' => t('Code to run during initialization of pages on this domain. Remember the <em><?php</em> and <em>?></em> tags.'),
|
| 352 |
'#default_value' => $edit['init'],
|
| 353 |
);
|
| 354 |
|
| 355 |
$form['submit'] = array(
|
| 356 |
'#type' => 'submit',
|
| 357 |
'#value' => t('Submit'),
|
| 358 |
);
|
| 359 |
|
| 360 |
return $form;
|
| 361 |
}
|
| 362 |
|
| 363 |
function multidomain_domain_settings_validate($form_id, $form_values) {
|
| 364 |
$domains = variable_get('multidomain_sites', array());
|
| 365 |
if (isset($domains[$form_values['domain']]) && ($form_values['url'] != $form_values['domain'])) {
|
| 366 |
form_set_error('domain', t('This domain has already been created.'));
|
| 367 |
}
|
| 368 |
}
|
| 369 |
|
| 370 |
function multidomain_domain_settings_submit($form_id, $form_values) {
|
| 371 |
$domains = variable_get('multidomain_sites', array());
|
| 372 |
$vid = variable_get('multidomain_vocabulary', '');
|
| 373 |
// Load an existing term by id.
|
| 374 |
if (isset($form_values['tid']) && $form_values['tid']) {
|
| 375 |
$term = (array) taxonomy_get_term($form_values['tid']);
|
| 376 |
}
|
| 377 |
// Or by name.
|
| 378 |
else {
|
| 379 |
$result = db_query("SELECT * FROM {term_data} WHERE name = '%s' AND vid = %d", $form_values['url'], $vid);
|
| 380 |
if (db_num_rows($result)) {
|
| 381 |
$term = db_fetch_array($result);
|
| 382 |
$form_values['tid'] = $term['tid'];
|
| 383 |
}
|
| 384 |
}
|
| 385 |
// If no existing term, create one.
|
| 386 |
if (!isset($term)) {
|
| 387 |
$term = array(
|
| 388 |
'name' => $form_values['domain'],
|
| 389 |
'description' => '',
|
| 390 |
'vid' => $vid,
|
| 391 |
'weight' => $form_values['weight'],
|
| 392 |
);
|
| 393 |
taxonomy_save_term($term);
|
| 394 |
$form_values['tid'] = $term['tid'];
|
| 395 |
drupal_set_message(t('Domain term created.'));
|
| 396 |
}
|
| 397 |
// If domain or weight has changed, update the term tied to this domain.
|
| 398 |
elseif (($form_values['domain'] != $form_values['url']) || ($form_values['weight'] != $domains[$form_values['url']]['weight'])) {
|
| 399 |
$term['name'] = $form_values['domain'];
|
| 400 |
$term['weight'] = $form_values['weight'];
|
| 401 |
taxonomy_save_term($term);
|
| 402 |
drupal_set_message(t('Domain term updated.'));
|
| 403 |
}
|
| 404 |
// If the domain has been changed, unset the previous data.
|
| 405 |
if ($form_values['domain'] != $form_values['url']) {
|
| 406 |
unset($domains[$form_values['url']]);
|
| 407 |
}
|
| 408 |
|
| 409 |
// If the value is the same as the main domain's default, don't save it.
|
| 410 |
// This ensures that later changes to the main domain's settings will
|
| 411 |
// apply also to other domains, unless that other domain has already
|
| 412 |
// had a custom value set.
|
| 413 |
foreach ($form_values['variables'] as $name => $value) {
|
| 414 |
// Determine if there is an existing value.
|
| 415 |
if ($existing_value = db_result(db_query("SELECT value FROM {variable} WHERE name = '%s'", $name))) {
|
| 416 |
// If so, unserialize it and compare it to the value submitted.
|
| 417 |
if ($value == unserialize($existing_value)) {
|
| 418 |
unset($form_values['variables'][$name]);
|
| 419 |
}
|
| 420 |
}
|
| 421 |
}
|
| 422 |
|
| 423 |
$domains[$form_values['domain']] = $form_values;
|
| 424 |
uasort($domains, '_multidomain_domain_sort');
|
| 425 |
variable_set('multidomain_sites', $domains);
|
| 426 |
return 'admin/settings/multidomain';
|
| 427 |
}
|
| 428 |
|
| 429 |
/**
|
| 430 |
* Function used by uasort to sort domains by weight.
|
| 431 |
*/
|
| 432 |
function _multidomain_domain_sort($a, $b) {
|
| 433 |
$a_weight = (is_array($a) && isset($a['weight'])) ? $a['weight'] : 0;
|
| 434 |
$b_weight = (is_array($b) && isset($b['weight'])) ? $b['weight'] : 0;
|
| 435 |
if ($a_weight == $b_weight) {
|
| 436 |
return 0;
|
| 437 |
}
|
| 438 |
return ($a_weight < $b_weight) ? -1 : 1;
|
| 439 |
}
|
| 440 |
|
| 441 |
function multidomain_settings() {
|
| 442 |
$sites = variable_get('multidomain_sites', array());
|
| 443 |
$form['sites']['default']['name'] = array(
|
| 444 |
'#value' => t('@default <strong>(default)</strong>', array( '@default' => variable_get('singlesignon_master_url', t('Not defined')))),
|
| 445 |
);
|
| 446 |
$form['sites']['default']['settings'] = array(
|
| 447 |
'#value' => l(t('Configure'), 'admin/settings/multidomain/settings'),
|
| 448 |
);
|
| 449 |
foreach ($sites as $url => $site) {
|
| 450 |
$form['sites'][$url]['name'] = array(
|
| 451 |
'#value' => $url,
|
| 452 |
);
|
| 453 |
$links = array(
|
| 454 |
l('Configure', 'admin/settings/multidomain/settings/'. str_replace('://', '_', $url)),
|
| 455 |
l('Delete', 'admin/settings/multidomain/delete/'. str_replace('://', '_', $url)),
|
| 456 |
);
|
| 457 |
$form['sites'][$url]['settings'] = array(
|
| 458 |
'#value' => implode(' | ', $links),
|
| 459 |
);
|
| 460 |
}
|
| 461 |
$form['new']['new_domain'] = array(
|
| 462 |
'#type' => 'textfield',
|
| 463 |
'#size' => 40,
|
| 464 |
'#maxlength' => 255,
|
| 465 |
'#validate' => array('multidomain_valid_domain' => array()),
|
| 466 |
);
|
| 467 |
$form['new']['submit'] = array(
|
| 468 |
'#type' => 'submit',
|
| 469 |
'#value' => t('Add domain'),
|
| 470 |
);
|
| 471 |
return $form;
|
| 472 |
}
|
| 473 |
|
| 474 |
function multidomain_settings_validate($form_id, $form_values) {
|
| 475 |
$domains = variable_get('multidomain_sites', array());
|
| 476 |
if (isset($domains[$form_values['new_domain']])) {
|
| 477 |
form_set_error('new][new_domain', t('This domain has already been created.'));
|
| 478 |
}
|
| 479 |
}
|
| 480 |
|
| 481 |
function multidomain_settings_submit($form_id, $form_values) {
|
| 482 |
$domains = variable_get('multidomain_sites', array());
|
| 483 |
// Create a term for the new domain.
|
| 484 |
$term = array(
|
| 485 |
'name' => $form_values['new_domain'],
|
| 486 |
'description' => '',
|
| 487 |
'vid' => variable_get('multidomain_vocabulary', ''),
|
| 488 |
'weight' => 0,
|
| 489 |
);
|
| 490 |
taxonomy_save_term($term);
|
| 491 |
$domains[$form_values['new_domain']] = array('tid' => $term['tid']);
|
| 492 |
variable_set('multidomain_sites', $domains);
|
| 493 |
}
|
| 494 |
|
| 495 |
function theme_multidomain_settings($form) {
|
| 496 |
$rows = array();
|
| 497 |
|
| 498 |
if (sizeof(element_children($form['sites']))) {
|
| 499 |
foreach ($form['sites'] as $url => $elements) {
|
| 500 |
if (!element_child($url)) {
|
| 501 |
continue;
|
| 502 |
}
|
| 503 |
|
| 504 |
$row = array();
|
| 505 |
$row[] = drupal_render($form['sites'][$url]['name']);
|
| 506 |
$row[] = drupal_render($form['sites'][$url]['settings']);
|
| 507 |
$rows[] = $row;
|
| 508 |
}
|
| 509 |
}
|
| 510 |
|
| 511 |
$rows[] = array(
|
| 512 |
drupal_render($form['new']['new_domain']),
|
| 513 |
drupal_render($form['new']['submit']),
|
| 514 |
);
|
| 515 |
$form['table'] = array(
|
| 516 |
'#value' => theme('table', array(t('Domain'),t('Operations')), $rows),
|
| 517 |
);
|
| 518 |
|
| 519 |
return drupal_render($form);
|
| 520 |
}
|
| 521 |
|
| 522 |
/**
|
| 523 |
* Implementation of hook_init().
|
| 524 |
*/
|
| 525 |
function multidomain_init() {
|
| 526 |
global $conf;
|
| 527 |
|
| 528 |
$domains = variable_get('multidomain_sites', array());
|
| 529 |
$protocol = ( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ;
|
| 530 |
if ($domain = $domains[$protocol . $_SERVER['HTTP_HOST']]) {
|
| 531 |
// Determine if we are currently on the home page.
|
| 532 |
$front = drupal_is_front_page();
|
| 533 |
|
| 534 |
// Set any variables specific to this domain.
|
| 535 |
if(isset($domain['variables']) && is_array($domain['variables'])) {
|
| 536 |
foreach($domain['variables'] as $name => $value) {
|
| 537 |
$conf[$name] = $value;
|
| 538 |
}
|
| 539 |
}
|
| 540 |
if ($domain['init']) {
|
| 541 |
drupal_eval($domain['init']);
|
| 542 |
}
|
| 543 |
|
| 544 |
// If we are on the home page, reinitiate the $_GET['q'] value.
|
| 545 |
if ($front) {
|
| 546 |
unset($_GET['q']);
|
| 547 |
drupal_init_path();
|
| 548 |
}
|
| 549 |
}
|
| 550 |
}
|
| 551 |
|
| 552 |
/**
|
| 553 |
* Implementation of hook_db_rewrite_sql().
|
| 554 |
*
|
| 555 |
* Limit nodes returned by domain term.
|
| 556 |
*/
|
| 557 |
function multidomain_db_rewrite_sql($query, $primary_table, $primary_field) {
|
| 558 |
static $domains;
|
| 559 |
static $rewrite;
|
| 560 |
static $default;
|
| 561 |
$return = array();
|
| 562 |
if ($primary_field == 'nid') {
|
| 563 |
if ($domains === NULL) {
|
| 564 |
$domains = variable_get('multidomain_sites', array());
|
| 565 |
$rewrite = variable_get('multidomain_taxonomy_rewrite_sql', MULTIDOMAIN_TAXONOMY_REWRITE_NONE);
|
| 566 |
$default = variable_get('singlesignon_master_url', 'http://');
|
| 567 |
// The domains array doesn't include the default domain. Add it if needed.
|
| 568 |
if ($rewrite == MULTIDOMAIN_TAXONOMY_REWRITE_ALL) {
|
| 569 |
$domains[$default] = array(
|
| 570 |
'tid' => variable_get('multidomain_master_domain_tid', 0),
|
| 571 |
);
|
| 572 |
}
|
| 573 |
}
|
| 574 |
$current = ($_SERVER['HTTPS'] ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'];
|
| 575 |
switch ($rewrite) {
|
| 576 |
// If we are not rewriting, simply return.
|
| 577 |
case MULTIDOMAIN_TAXONOMY_REWRITE_NONE:
|
| 578 |
return $return;
|
| 579 |
// If we are are rewriting all but the default, return if this is the default.
|
| 580 |
case MULTIDOMAIN_TAXONOMY_REWRITE_NON_DEFAULT:
|
| 581 |
if ($current == $default) {
|
| 582 |
return $return;
|
| 583 |
}
|
| 584 |
// Fall through.
|
| 585 |
case MULTIDOMAIN_TAXONOMY_REWRITE_ALL:
|
| 586 |
// Determine if we are on a registered domain.
|
| 587 |
if ($domain = $domains[$current]) {
|
| 588 |
if (isset($domain['tid'])) {
|
| 589 |
$return['join'] = 'INNER JOIN {term_node} multidomain_tn ON multidomain_tn.nid = '. $primary_table .'.nid';
|
| 590 |
$return['where'] = 'multidomain_tn.tid = '. $domain['tid'];
|
| 591 |
}
|
| 592 |
}
|
| 593 |
}
|
| 594 |
}
|
| 595 |
return $return;
|
| 596 |
}
|
| 597 |
|
| 598 |
/**
|
| 599 |
* Return specified elements from an array of forms.
|
| 600 |
*/
|
| 601 |
function multidomain_get_form_bits($forms) {
|
| 602 |
$form = array();
|
| 603 |
foreach ($forms as $form_id => $elements) {
|
| 604 |
if (function_exists($form_id)) {
|
| 605 |
$source_form = $form_id();
|
| 606 |
foreach ($elements as $element) {
|
| 607 |
$return = $source_form;
|
| 608 |
foreach ($element as $key) {
|
| 609 |
if (isset($return[$key])) {
|
| 610 |
$return = $return[$key];
|
| 611 |
}
|
| 612 |
else {
|
| 613 |
break;
|
| 614 |
}
|
| 615 |
}
|
| 616 |
if ($return !== $source_form) {
|
| 617 |
$form[$key] = $return;
|
| 618 |
}
|
| 619 |
}
|
| 620 |
}
|
| 621 |
}
|
| 622 |
return $form;
|
| 623 |
}
|
| 624 |
|
| 625 |
if (!function_exists('custom_url_rewrite')) {
|
| 626 |
function custom_url_rewrite($type, $alias, $real_path) {
|
| 627 |
static $default = null;
|
| 628 |
static $force_default = null;
|
| 629 |
static $domains = null;
|
| 630 |
static $protocol = null;
|
| 631 |
static $clean_url = null;
|
| 632 |
|
| 633 |
$path = ( $alias ) ? $alias : $real_path;
|
| 634 |
// Don't rewrite fully qualified URLs or singlesignon redirects.
|
| 635 |
if (strpos($path, '://') !== FALSE || strpos($path, 'singlesignon/') === 0) {
|
| 636 |
return $path;
|
| 637 |
}
|
| 638 |
|
| 639 |
if (is_null($default)) {
|
| 640 |
// initialise defaults
|
| 641 |
$default = variable_get('singlesignon_master_url','');
|
| 642 |
$force_default = variable_get('multidomain_force_default_domain', false);
|
| 643 |
$domains = variable_get('multidomain_sites', array());
|
| 644 |
$clean_url = (bool)variable_get('clean_url', '0');
|
| 645 |
$protocol = $_SERVER['HTTPS'] ? 'https://' : 'http://' ;
|
| 646 |
}
|
| 647 |
$base = $force_default ? $default : NULL;
|
| 648 |
foreach ($domains as $domain => $info) {
|
| 649 |
$source = $type == 'alias' ? $real_path : drupal_lookup_path($path);
|
| 650 |
$arg = explode('/', $source);
|
| 651 |
// Determine if this is a node.
|
| 652 |
$is_node = ($arg[0] == 'node' && is_numeric($arg[1]));
|
| 653 |
$page_match = FALSE;
|
| 654 |
|
| 655 |
// If this is a link to the configuration of this domain, match it.
|
| 656 |
// This is needed because we need the variables to be set to this domain's
|
| 657 |
// versions, so that the form elements load with the correct defaults.
|
| 658 |
if ($path == 'admin/settings/multidomain/settings/'. str_replace('://', '_', $domain)) {
|
| 659 |
$page_match = TRUE;
|
| 660 |
}
|
| 661 |
// Match path by taxonomy.
|
| 662 |
if (!$page_match && $is_node) {
|
| 663 |
$page_match = db_num_rows(db_query('SELECT tn.* FROM {term_node} tn WHERE tn.nid = %d AND tn.tid = %d', $arg[1], $info['tid'])) ? TRUE : FALSE;
|
| 664 |
}
|
| 665 |
// Match path if necessary.
|
| 666 |
if (!$page_match && $info['pages']) {
|
| 667 |
if ($info['visibility'] < 2) {
|
| 668 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($info['pages'], '/')) .')$/';
|
| 669 |
// Compare with the internal and path alias (if any).
|
| 670 |
$page_match = preg_match($regexp, $alias);
|
| 671 |
if ($path != $real_path) {
|
| 672 |
$page_match = $page_match || preg_match($regexp, $real_path);
|
| 673 |
}
|
| 674 |
// When $block->visibility has a value of 0, the block is displayed on
|
| 675 |
// all pages except those listed in $block->pages. When set to 1, it
|
| 676 |
// is displayed only on those pages listed in $block->pages.
|
| 677 |
$page_match = !($info['visibility'] xor $page_match);
|
| 678 |
}
|
| 679 |
else {
|
| 680 |
$page_match = drupal_eval($info['pages']);
|
| 681 |
}
|
| 682 |
}
|
| 683 |
|
| 684 |
if (!$page_match && !empty($info['types']) && $is_node) {
|
| 685 |
$node = node_load($arg[1]);
|
| 686 |
$page_match = array_key_exists($node->type, $info['types']) && $info['types'][$node->type];
|
| 687 |
}
|
| 688 |
if ($page_match) {
|
| 689 |
$base = $domain;
|
| 690 |
break;
|
| 691 |
}
|
| 692 |
}
|
| 693 |
if ($type == 'alias') {
|
| 694 |
if (($protocol . $_SERVER['HTTP_HOST'] != $base) && !is_null($base) && (strpos($path, '://') === FALSE)) {
|
| 695 |
$path = $base .'/'. (!$clean_url ? '?q=' : '') . $path;
|
| 696 |
}
|
| 697 |
}
|
| 698 |
else {
|
| 699 |
// Permanent redirect on old pages.
|
| 700 |
#if ($protocol . $_SERVER['HTTP_HOST'] != $base) {
|
| 701 |
# header("HTTP/1.1 301 Moved Permanently");
|
| 702 |
# header("Location: " . $base .'/'. (!$clean_url ? '?q=' : '') . $path;);
|
| 703 |
# exit();
|
| 704 |
#}
|
| 705 |
}
|
| 706 |
return $path;
|
| 707 |
}
|
| 708 |
}
|
| 709 |
else {
|
| 710 |
drupal_set_message(t('The multi domain module has found a custom_url_rewrite function that is already defined. This conflicts with the module\'s operations, and as such the multi domain module will not work.'), 'error');
|
| 711 |
}
|