View source
<?php
define('DOMAIN_INSTALL_RULE', TRUE);
define('DOMAIN_SITE_GRANT', TRUE);
define('DOMAIN_ASSIGN_USERS', TRUE);
define('DOMAIN_LIST_SIZE', 25);
define('DOMAIN_EXPORT_STATUS_DATABASE', 0);
define('DOMAIN_EXPORT_STATUS_CODE', 1);
define('DOMAIN_ALL', 'DOMAIN_ALL');
define('DOMAIN_ACTIVE', 'DOMAIN_ACTIVE');
function domain_boot() {
include_once 'settings_custom_url.inc';
}
function domain_api_version() {
return 3;
}
function domain_init() {
global $_domain, $conf;
if (!is_array($_domain)) {
$_domain = array();
}
if (!isset($_domain['domain_id'])) {
$_domain = domain_default(TRUE);
$_domain['error'] = 'bootstrap include';
}
if (!isset($error) && isset($_domain['error'])) {
$error = 'Domain access failed to load during phase: ' . $_domain['error'] . '. Please check your settings.php file and site configuration.';
if (empty($_POST)) {
$hide = variable_get('domain_hide_errors', FALSE);
if (user_access('administer domains') && empty($hide)) {
drupal_set_message($error, 'error');
}
if (empty($hide)) {
watchdog('domain', $error, NULL, WATCHDOG_ERROR);
}
}
}
domain_node_save_redirect();
$www_replaced = FALSE;
if (variable_get('domain_www', 0) && strpos($_domain['subdomain'], 'www.') !== FALSE) {
$_domain['subdomain'] = str_replace('www.', '', $_domain['subdomain']);
$www_replaced = TRUE;
}
$domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
if ($domain != -1) {
$_domain = array_merge($domain, $_domain);
}
domain_initial_domain($_domain);
if ($www_replaced) {
drupal_goto(domain_get_uri($_domain));
}
if (empty($_domain['valid'])) {
domain_invalid_domain_requested();
}
if (variable_get('domain_sitename_override', 1)) {
$conf['site_name'] = $_domain['sitename'];
}
}
function domain_initial_domain($domain = array()) {
$initial =& drupal_static(__FUNCTION__);
if (!isset($initial) && !empty($domain)) {
$initial = $domain;
}
return $initial;
}
function domain_unserialize($object) {
if (empty($object)) {
return;
}
return unserialize($object);
}
function domain_menu() {
$items = array();
$admin = user_access('administer domains');
$items['admin/structure/domain'] = array(
'title' => 'Domains',
'access arguments' => array(
'administer domains',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_overview_form',
),
'file' => 'domain.admin.inc',
'description' => 'Manage and configure domains.',
);
$items['admin/structure/domain/view'] = array(
'title' => 'Domain list',
'access arguments' => array(
'administer domains',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_overview_form',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'file' => 'domain.admin.inc',
'description' => 'View domains for the site.',
'weight' => -50,
);
$items['admin/structure/domain/settings'] = array(
'title' => 'Settings',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_configure',
'file' => 'domain.admin.inc',
'description' => 'Configure Domain Access settings.',
'weight' => -20,
);
$items['admin/structure/domain/create'] = array(
'title' => 'Create domain',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_ACTION,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
),
'file' => 'domain.admin.inc',
'description' => 'Create new domain record.',
);
$batch = module_invoke_all('domain_batch');
if (!empty($batch)) {
$items['admin/structure/domain/batch'] = array(
'title' => 'Batch updating',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'domain_batch',
'file' => 'domain.admin.inc',
'description' => 'Batch update domain settings.',
'weight' => -15,
);
foreach ($batch as $key => $value) {
$items['admin/structure/domain/batch/' . $key] = array(
'title' => $value['#form']['#title'],
'access arguments' => isset($value['#permission']) ? array(
$value['#permission'],
) : array(
'administer domains',
),
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'page callback' => 'domain_batch',
'page arguments' => array(
$key,
),
'file' => 'domain.admin.inc',
'description' => isset($value['#description']) ? $value['#description'] : '',
'weight' => isset($value['#weight']) ? $value['#weight'] : 0,
);
}
}
$items['admin/structure/domain/nodes'] = array(
'title' => 'Content defaults',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_nodes_form',
),
'file' => 'domain.admin.inc',
'description' => 'Default domain settings for content.',
'weight' => -10,
);
$items['admin/structure/domain/roles'] = array(
'title' => 'User defaults',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_roles_form',
),
'file' => 'domain.admin.inc',
'description' => 'Default domain settings for users.',
'weight' => -5,
);
$items['admin/structure/domain/view/%domain'] = array(
'title' => 'View',
'title callback' => 'domain_title',
'title arguments' => array(
4,
),
'access arguments' => array(
'administer domains',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_form',
4,
),
'description' => 'Edit domain record.',
'file' => 'domain.admin.inc',
'weight' => -10,
);
$items['admin/structure/domain/view/%domain/edit'] = array(
'title' => 'Edit',
'access arguments' => array(
'administer domains',
),
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/domain/view/%domain/delete'] = array(
'title' => 'Delete',
'access arguments' => array(
'administer domains',
),
'type' => MENU_LOCAL_TASK,
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_delete_form',
4,
),
'description' => 'Delete domain record.',
'file' => 'domain.admin.inc',
'weight' => 50,
);
$items['admin/structure/domain/repair'] = array(
'title' => 'Domain update database',
'access arguments' => array(
'administer domains',
),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'domain_repair_form',
),
'type' => MENU_CALLBACK,
'file' => 'domain.admin.inc',
);
return $items;
}
function domain_title($domain) {
return $domain['subdomain'];
}
function domain_permission() {
$permissions = array(
'administer domains' => array(
'title' => t('Administer domain records and settings'),
'restrict access' => TRUE,
),
'access inactive domains' => array(
'title' => t('Access inactive domains'),
'restrict access' => TRUE,
),
'assign domain editors' => array(
'title' => t('Assign editors to domains'),
),
'set domain access' => array(
'title' => t('Set domain access status for all content'),
),
'publish to any assigned domain' => array(
'title' => t('Publish content to any assigned domain'),
),
'publish from assigned domain' => array(
'title' => t('Publish content only from assigned domain'),
),
'publish from default domain' => array(
'title' => t('Publish content only from the default domain'),
),
'edit domain content' => array(
'title' => t('Edit any content on assigned domains'),
),
'delete domain content' => array(
'title' => t('Delete any content on assigned domains'),
),
'view unpublished domain content' => array(
'title' => t('View unpublished content on assigned domains'),
),
);
foreach (node_permissions_get_configured_types() as $type) {
$permissions += domain_editor_list_permissions($type);
}
return $permissions;
}
function domain_editor_list_permissions($type) {
$info = node_type_get_type($type);
$type = check_plain($info->type);
$perms = array(
"create {$type} content on assigned domains" => array(
'title' => t('%type_name: Create new content on assigned domains', array(
'%type_name' => $info->name,
)),
),
"update {$type} content on assigned domains" => array(
'title' => t('%type_name: Edit any content on assigned domains', array(
'%type_name' => $info->name,
)),
),
"delete {$type} content on assigned domains" => array(
'title' => t('%type_name: Delete any content on assigned domains', array(
'%type_name' => $info->name,
)),
),
);
return $perms;
}
function domain_theme($existing, $type, $theme, $path) {
$themes = array(
'domain_batch_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
'domain_batch_title' => array(
'variables' => array(
'batch' => array(),
),
'file' => 'domain.admin.inc',
),
'domain_nodes_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
'domain_roles_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
'domain_overview_form' => array(
'render element' => 'form',
'file' => 'domain.admin.inc',
),
);
return $themes;
}
function domain_hook_info() {
$hooks['domain_load'] = array(
'group' => 'domain',
);
$hooks['domain_insert'] = array(
'group' => 'domain',
);
$hooks['domain_update'] = array(
'group' => 'domain',
);
$hooks['domain_delete'] = array(
'group' => 'domain',
);
$hooks['domain_reassign'] = array(
'group' => 'domain',
);
$hooks['domain_cron'] = array(
'group' => 'domain',
);
$hooks['domain_features_rebuild'] = array(
'group' => 'domain',
);
$hooks['domain_install'] = array(
'group' => 'domain',
);
$hooks['domain_ignore'] = array(
'group' => 'domain',
);
$hooks['domain_form'] = array(
'group' => 'domain',
);
$hooks['domain_warning'] = array(
'group' => 'domain',
);
$hooks['domain_source_alter'] = array(
'group' => 'domain',
);
$hooks['domain_source_path_alter'] = array(
'group' => 'domain',
);
$hooks['domain_batch'] = array(
'group' => 'domain',
);
$hooks['domain_batch_alter'] = array(
'group' => 'domain',
);
$hooks['domain_bootstrap_lookup'] = array(
'group' => 'domain',
);
$hooks['domain_bootstrap_full'] = array(
'group' => 'domain',
);
$hooks['domain_path'] = array(
'group' => 'domain',
);
$hooks['domain_warning_alter'] = array(
'group' => 'domain',
);
$hooks['domain_settings'] = array(
'group' => 'domain',
);
$hooks['domain_validate_alter'] = array(
'group' => 'domain',
);
return $hooks;
}
function domain_block_info() {
$blocks = array();
$blocks['information'] = array(
'info' => t('Domain access information'),
);
$blocks['server'] = array(
'info' => t('Domain access server information'),
'cache' => DRUPAL_NO_CACHE,
);
$blocks['switcher'] = array(
'info' => t('Domain switcher'),
);
return $blocks;
}
function domain_block_view($delta = '') {
if (empty($delta)) {
return;
}
module_load_include('inc', 'domain', 'domain.blocks');
$function = 'domain_block_view_' . $delta;
if (function_exists($function)) {
return $function();
}
}
function domain_user_load($users) {
foreach ($users as $uid => $account) {
$users[$uid]->domain_user = domain_get_user_domains($account);
}
}
function domain_user_set($account) {
if (!isset($account->domain_user)) {
$accounts = array(
$account->uid => $account,
);
domain_user_load($accounts);
}
}
function domain_user_insert(&$edit, $account, $category) {
domain_user_save($edit, $account, $category);
}
function domain_user_update(&$edit, $account, $category) {
domain_user_save($edit, $account, $category);
}
function domain_user_save(&$edit, $account, $category) {
if (!isset($edit['domain_user'])) {
return;
}
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
$values = array();
foreach ($edit['domain_user'] as $domain_id => $status) {
if ($status != 0) {
$values[] = array(
'uid' => $account->uid,
'domain_id' => $domain_id,
);
}
}
if (!empty($values)) {
$query = db_insert('domain_editor')
->fields(array(
'uid',
'domain_id',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
$edit['domain_user'] = NULL;
}
function domain_user_delete($account) {
db_delete('domain_editor')
->condition('uid', $account->uid)
->execute();
}
function domain_user_view($account, $view_mode) {
domain_user_set($account);
if ($view_mode != 'full') {
return;
}
if (!user_access('assign domain editors')) {
return;
}
$output = '';
$account->content['domain'] = array(
'#type' => 'user_profile_category',
'#weight' => 10,
'#title' => t('Domain status'),
);
if (empty($account->domain_user)) {
$output = t('This user is not assigned to a domain.');
}
else {
$items = array();
foreach (array_filter($account->domain_user) as $id) {
$domain = domain_lookup($id);
$items[] = check_plain($domain['sitename']);
}
$output = theme('item_list', array(
'items' => $items,
));
}
$account->content['domain']['domain_settings'] = array(
'#type' => 'user_profile_item',
'#title' => t('Assigned domains'),
'#markup' => $output,
);
}
function domain_form_user_profile_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
function domain_form_user_register_form_alter(&$form, &$form_state) {
domain_form_user_form_alter($form, $form_state);
}
function domain_form_user_form_alter(&$form, &$form_state) {
if (is_null($form['#user_category']) || !in_array($form['#user_category'], array(
'account',
'register',
))) {
return;
}
$_domain = domain_get_domain();
$add_roles = variable_get('domain_add_roles', 0);
if ($form['#user_category'] == 'register') {
$add_roles = TRUE;
}
$account = $form['#user'];
$account->domain_user = domain_get_user_domains($account, $add_roles);
if (empty($account->uid)) {
$default = array(
$_domain['domain_id'] => $_domain['domain_id'],
);
}
else {
$default = $account->domain_user;
}
if (user_access('assign domain editors')) {
$domains = domain_domains();
$options = array();
foreach ($domains as $domain) {
$options[$domain['domain_id']] = check_plain($domain['sitename']);
}
$format = domain_select_format();
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['domain']['domain_user'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#options' => $options,
'#title' => t('Domain access settings'),
'#description' => t('Select the affiliates that this user belongs to. Used to grant editing permissions for users with the "edit domain content" permission.'),
'#default_value' => $default,
);
if ($format) {
$form['domain']['domain_user']['#multiple'] = TRUE;
$form['domain']['domain_user']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
else {
$form['domain'] = array(
'domain_user' => array(
'#type' => 'value',
'#value' => $default,
),
);
}
}
function domain_user_operations() {
if (!user_access('assign domain editors')) {
return;
}
return array(
'domain' => array(
'label' => t('Assign users to domains'),
'callback' => 'domain_user_operation_assign',
),
);
}
function domain_form_user_admin_account_alter(&$form, $form_state) {
global $_domain;
if (!user_access('assign domain editors')) {
return;
}
$form['options']['#weight'] = -2;
$_domain = domain_get_domain();
$options = array();
$format = domain_select_format();
foreach (domain_domains() as $data) {
if ($data['valid'] || user_access('access inactive domains')) {
$options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate editor options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#prefix' => '<div class="description">' . t('If you select <em>Assign users to domains</em> above, you should confirm the <em>Affiliate editor options</em> settings below.') . '</div>',
'#weight' => -1,
);
$form['domain']['behavior'] = array(
'#type' => 'radios',
'#title' => t('Update behavior'),
'#options' => array(
0 => t('Replace old values with new settings'),
1 => t('Add new settings to existing values'),
2 => t('Remove selected domains from existing values'),
),
'#description' => t('Defines how new grants will be applied to the updated users.'),
'#default_value' => 0,
);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Assign to'),
'#options' => $options,
'#required' => FALSE,
'#description' => t('Select which affiliates these users should belong to. <em>Note: this will erase any current assignment for the selected users.</em>'),
'#default_value' => array(
$_domain['domain_id'],
),
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
$ops = array_pop($form['accounts']['#header']);
$form['accounts']['#header']['domain_user'] = array(
'data' => t('Assigned Domains'),
);
foreach (array_keys($form['accounts']['#options']) as $uid) {
$form['accounts']['#options'][$uid]['domain_user'] = theme('item_list', array(
'items' => _domain_user_list($uid),
));
}
$form['accounts']['#header']['operations'] = $ops;
$form['#submit'][] = 'domain_update_users';
}
function _domain_user_list($uid) {
$temp_account = new stdClass();
$temp_account->uid = $uid;
$list = domain_get_user_domains($temp_account, FALSE);
$domains = domain_domains();
$user_domains = array();
foreach ($list as $domain_id) {
$user_domains[] = check_plain($domains[$domain_id]['sitename']);
}
return $user_domains;
}
function domain_user_operation_assign($accounts) {
}
function domain_update_users($form, &$form_state) {
$values = $form_state['values'];
if ($values['operation'] != 'domain') {
return;
}
$add_roles = variable_get('domain_add_roles', 0);
$domains = array_filter($values['domains']);
foreach ($values['accounts'] as $uid) {
if (!empty($form_state['values']['behavior'])) {
$account = new stdClass();
$account->uid = $uid;
$current = domain_get_user_domains($account, $add_roles);
if ($form_state['values']['behavior'] == 1) {
$domains += $current;
}
else {
foreach ($domains as $domain_id) {
if (isset($current[$domain_id])) {
unset($current[$domain_id]);
}
}
$domains = $current;
}
}
db_delete('domain_editor')
->condition('uid', $uid)
->execute();
foreach ($domains as $domain_id) {
db_insert('domain_editor')
->fields(array(
'uid' => $uid,
'domain_id' => $domain_id,
))
->execute();
}
}
}
function domain_cron() {
$modules = module_implements('domain_cron');
if (!empty($modules)) {
$domains = domain_domains();
foreach ($domains as $domain) {
domain_set_domain($domain['domain_id'], TRUE);
foreach ($modules as $module) {
module_invoke($module, 'domain_cron', $domain);
}
}
domain_reset_domain(TRUE);
}
}
function domain_load($domain_id = NULL, $reset = FALSE) {
$domain = domain_lookup($domain_id, NULL, $reset);
if ($domain == -1) {
return FALSE;
}
return $domain;
}
function domain_save($values, $form_values = array()) {
$count = (bool) db_query("SELECT COUNT(domain_id) FROM {domain} WHERE is_default = 1")
->fetchField();
if (!$count) {
$values['is_default'] = 1;
}
if (!isset($values['machine_name'])) {
$values['machine_name'] = domain_machine_name($values['subdomain']);
}
if (!empty($values['domain_id'])) {
$action = 'domain_update';
$update_id = array(
'domain_id',
);
}
else {
$action = 'domain_insert';
$update_id = array();
}
if (!empty($values['is_default'])) {
db_update('domain')
->fields(array(
'is_default' => 0,
))
->condition('machine_name', $values['machine_name'], '<>')
->execute();
}
drupal_write_record('domain_export', $values, $update_id);
drupal_write_record('domain', $values, $update_id);
$domain = domain_lookup(NULL, $values['subdomain'], TRUE);
module_invoke_all($action, $domain, $form_values);
$domain = domain_lookup(NULL, $values['subdomain'], TRUE);
domain_static_reset();
variable_set('menu_rebuild_needed', TRUE);
return $domain;
}
function domain_static_reset() {
drupal_static_reset('domain_lookup_domains');
drupal_static_reset('domain_lookup_result');
drupal_static_reset('domain_id_list');
drupal_static_reset('domain_domains');
drupal_static_reset('domain_list_by_machine_name');
}
function domain_delete($domain, $values = array()) {
foreach (array(
'domain_access',
'domain_editor',
) as $table) {
if (isset($values[$table]) && $values[$table] != 'none') {
$new_domain = domain_lookup($values[$table]);
if ($new_domain != -1) {
domain_reassign($domain, $new_domain, $table);
}
}
}
module_invoke_all('domain_delete', $domain, $values);
db_delete('domain_access')
->condition('gid', $domain['domain_id'])
->condition('realm', 'domain_id')
->execute();
db_delete('domain_editor')
->condition('domain_id', $domain['domain_id'])
->execute();
db_delete('domain')
->condition('domain_id', $domain['domain_id'])
->execute();
db_delete('domain_export')
->condition('domain_id', $domain['domain_id'])
->execute();
variable_set('menu_rebuild_needed', TRUE);
node_access_needs_rebuild(TRUE);
}
function domain_lookup($domain_id = NULL, $subdomain = NULL, $reset = FALSE) {
$domains =& drupal_static(__FUNCTION__ . '_domains');
$result =& drupal_static(__FUNCTION__ . '_result');
$key = $domain_id . $subdomain;
if (!$reset && isset($domains[$key])) {
return $domains[$key];
}
if (!isset($result) || $reset) {
if (db_table_exists('domain_export')) {
$result = db_query("SELECT domain_id, subdomain, sitename, scheme, valid, weight, is_default, machine_name FROM {domain}", array(), array(
'fetch' => PDO::FETCH_ASSOC,
))
->fetchAllAssoc('domain_id');
}
else {
$result = db_query("SELECT domain_id, subdomain, sitename, scheme, valid, weight, is_default FROM {domain}", array(), array(
'fetch' => PDO::FETCH_ASSOC,
))
->fetchAllAssoc('domain_id');
}
}
if (is_null($domain_id) && is_null($subdomain) || $domain_id < 0) {
$domains[$key] = -1;
}
elseif (!isset($domains[$key]) || $reset) {
if ($subdomain) {
foreach ($result as $array) {
if ($array['subdomain'] == $subdomain) {
$domain = $array;
}
}
}
elseif (isset($result[$domain_id])) {
$domain = $result[$domain_id];
}
if (isset($domain['domain_id'])) {
$domains[$key] = domain_api($domain, $reset);
}
else {
$domains[$key] = -1;
}
}
return $domains[$key];
}
function domain_default($reset = FALSE, $alter = TRUE) {
$default =& drupal_static(__FUNCTION__);
$altered =& drupal_static(__FUNCTION__ . '_altered');
if (empty($default) || $reset) {
$altered = FALSE;
if (db_table_exists('domain_export')) {
$default = db_query("SELECT domain_id, subdomain, sitename, scheme, valid, weight, is_default, machine_name FROM {domain} WHERE is_default = 1")
->fetchAssoc();
}
else {
$default = db_query("SELECT domain_id, subdomain, sitename, scheme, valid, weight, is_default FROM {domain} WHERE is_default = 1")
->fetchAssoc();
}
if (empty($default)) {
$default = array(
'domain_id' => 0,
'subdomain' => isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '',
'sitename' => variable_get('site_name', 'Drupal'),
'scheme' => empty($_SERVER['HTTPS']) ? 'http' : 'https',
'valid' => 1,
'is_default' => 1,
'machine_name' => domain_machine_name($default['subdomain']),
);
}
}
if ($alter && !$altered) {
$default = domain_api($default, $reset);
}
return $default;
}
function domain_default_id() {
$id =& drupal_static(__FUNCTION__);
if (!isset($id)) {
$default = domain_default(FALSE, FALSE);
$id = $default['domain_id'];
}
return $id;
}
function domain_default_machine_name() {
$id =& drupal_static(__FUNCTION__);
if (!isset($id)) {
$default = domain_default(FALSE, FALSE);
$machine_name = $default['machine_name'];
}
return $machine_name;
}
function domain_set_primary_domain() {
$root = strtolower(rtrim($_SERVER['HTTP_HOST']));
if ($error = domain_valid_domain($root)) {
return;
}
$site = variable_get('site_name', 'Drupal');
$scheme = 'http';
if (!empty($_SERVER['HTTPS'])) {
$scheme = 'https';
}
$check = (bool) db_query("SELECT COUNT(domain_id) FROM {domain} WHERE is_default = 1")
->fetchField();
if (empty($check)) {
$domain = array(
'subdomain' => $root,
'sitename' => $site,
'scheme' => $scheme,
'weight' => -1,
'valid' => 1,
'is_default' => 1,
'machine_name' => domain_machine_name($root),
);
drupal_write_record('domain_export', $domain);
drupal_write_record('domain', $domain);
module_invoke_all('domain_insert', $domain, array());
}
}
function domain_domains($reset = FALSE) {
$domains =& drupal_static(__FUNCTION__);
if (empty($domains) || $reset) {
$domains = array();
$result = domain_id_list($reset);
foreach ($result as $domain_id) {
$domain = domain_lookup($domain_id, NULL, $reset);
$domains[$domain['domain_id']] = $domain;
}
}
return $domains;
}
function domain_id_list($reset = FALSE) {
$list =& drupal_static(__FUNCTION__);
if (isset($list) && empty($reset)) {
return $list;
}
$list = array();
$result = db_query("SELECT domain_id, machine_name FROM {domain} ORDER BY weight");
foreach ($result as $data) {
$list[$data->machine_name] = $data->domain_id;
}
return $list;
}
function domain_select_format() {
$domains = domain_domains();
$format = 0;
if (count($domains) > variable_get('domain_list_size', DOMAIN_LIST_SIZE)) {
$format = 1;
}
return variable_get('domain_select_format', $format);
}
function domain_validate($subdomain) {
$error_list = array();
$error = domain_valid_domain($subdomain);
if (!empty($error)) {
$error_list[] = $error;
}
if (!domain_unique_domain($subdomain)) {
$error_list[] = t('The domain value must be unique.');
}
return $error_list;
}
function domain_valid_domain($subdomain) {
$error_list = array();
$localhost_check = explode(':', $subdomain);
if (substr_count($subdomain, '.') == 0 && $localhost_check[0] != 'localhost') {
$error_list[] = t('At least one dot (.) is required, except when using <em>localhost</em>.');
}
if (substr_count($subdomain, ':') > 1) {
$error_list[] = t('Only one colon (:) is allowed.');
}
elseif (substr_count($subdomain, ':') == 1) {
$parts = explode(':', $subdomain);
$port = (int) $parts[1];
if (strcmp($port, $parts[1])) {
$error_list[] = t('The port protocol must be an integer.');
}
}
if (substr($subdomain, 0, 1) == '.') {
$error_list[] = t('The domain must not begin with a dot (.)');
}
if (substr($subdomain, -1) == '.') {
$error_list[] = t('The domain must not end with a dot (.)');
}
if (!variable_get('domain_allow_non_ascii', FALSE)) {
$pattern = '/^[a-z0-9\\.\\-:]*$/i';
if (!preg_match($pattern, $subdomain)) {
$error_list[] = t('Only alphanumeric characters, dashes, and a colon are allowed.');
}
}
if ($subdomain != drupal_strtolower($subdomain)) {
$error_list[] = t('Only lower-case characters are allowed.');
}
if (variable_get('domain_www', 0) && substr($subdomain, 0, strpos($subdomain, '.')) == 'www') {
$error_list[] = t('WWW prefix handling: Domains must be registered without the www. prefix.');
}
drupal_alter('domain_validate', $error_list, $subdomain);
if (!empty($error_list)) {
return t('The domain string is invalid for %subdomain:', array(
'%subdomain' => $subdomain,
)) . theme('item_list', array(
'items' => $error_list,
));
}
}
function domain_unique_domain($subdomain) {
$count = db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = :subdomain", array(
':subdomain' => $subdomain,
))
->fetchField();
return empty($count);
}
function domain_get_user_domains($account, $add_roles = TRUE) {
if (empty($account)) {
return array();
}
$uid = (int) $account->uid;
$user_domains = array();
$result = db_query("SELECT domain_id FROM {domain_editor} WHERE uid = :uid", array(
':uid' => $uid,
));
foreach ($result as $data) {
$user_domains[$data->domain_id] = $data->domain_id;
}
if ($add_roles) {
if (empty($account->roles)) {
$account->roles = array(
0 => 'new user',
);
}
$defaults = variable_get('domain_roles', array());
foreach ($account->roles as $rid => $role) {
$filter = array();
if (isset($defaults[$rid])) {
$filter = array_filter($defaults[$rid]);
}
if (!empty($filter)) {
if (!empty($filter[DOMAIN_ALL])) {
foreach (domain_id_list() as $domain_id) {
$user_domains[$domain_id] = $domain_id;
}
break;
}
else {
foreach ($filter as $machine_name => $status) {
if ($status && ($domain_id = domain_load_domain_id($machine_name))) {
$user_domains[$domain_id] = $domain_id;
}
}
}
}
}
}
return $user_domains;
}
function domain_api($domain, $reset = FALSE) {
$modules =& drupal_static(__FUNCTION__);
if (!isset($modules) || $reset) {
$modules = module_implements('domain_load');
if (function_exists('_domain_bootstrap_modules')) {
$bootstrap = _domain_bootstrap_modules();
$modules = array_unique(array_merge($modules, $bootstrap));
}
elseif (!variable_get('domain_hide_errors')) {
$severity = drupal_installation_attempted() ? 'warning' : 'error';
drupal_set_message(t('Domain module installation is incomplete. See INSTALL.txt and check your settings.php file.'), $severity, FALSE);
}
}
if (!empty($modules)) {
foreach ($modules as $module) {
if (module_exists($module)) {
$function = $module . '_domain_load';
module_load_include('inc', $module, $module . '.domain');
if (function_exists($function)) {
$function($domain);
}
}
}
}
return $domain;
}
function domain_set_domain($domain_id, $bootstrap = FALSE) {
global $_domain;
$_domain = domain_load($domain_id);
if ($bootstrap) {
_domain_bootstrap_invoke_all('full', $_domain);
}
}
function domain_reset_domain($bootstrap = FALSE) {
$domain = domain_initial_domain();
if (!empty($domain)) {
domain_set_domain($domain['domain_id'], $bootstrap);
}
}
function domain_get_domain() {
if (isset($GLOBALS['_domain'])) {
return $GLOBALS['_domain'];
}
}
function domain_check_primary($msg = 'default') {
$_domain = domain_get_domain();
$default = domain_default();
if ($_domain['domain_id'] != $default['domain_id']) {
if ($msg == 'default') {
drupal_set_message(t('You have been redirected: This page must be accessed from the primary domain.'));
}
elseif (!empty($msg)) {
drupal_set_message($msg);
}
domain_goto($default);
}
}
function domain_domain_load(&$domain) {
$domain['path'] = domain_get_path($domain);
$domain['site_grant'] = DOMAIN_SITE_GRANT;
}
function domain_get_path($domain) {
global $base_url;
if (empty($base_url)) {
return domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'];
}
$url = array();
if ($_url = @parse_url($base_url)) {
$url = $_url;
}
if (!isset($url['path'])) {
$url['path'] = '/';
}
if (substr($url['path'], -1) != '/') {
$url['path'] .= '/';
}
$path = domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'] . $url['path'];
return $path;
}
function domain_get_uri($domain) {
global $base_path;
$modules = _domain_path_modules();
if (!empty($modules) && !drupal_is_front_page()) {
$request_uri = request_path();
$options = array();
domain_path($domain['domain_id'], $request_uri, $options, $_GET['q']);
$request_uri = base_path() . $request_uri;
}
else {
$request_uri = request_uri();
}
$path = domain_check_scheme($domain['scheme']) . '://' . $domain['subdomain'] . $request_uri;
return $path;
}
function domain_check_scheme($scheme) {
if ($scheme != 'https') {
$scheme = 'http';
}
return $scheme;
}
function domain_goto($domain) {
$_domain = domain_get_domain();
if ($domain != -1 && $_domain['domain_id'] != $domain['domain_id']) {
$path = domain_get_uri($domain);
drupal_goto($path);
}
}
function domain_node_load($nodes, $types) {
$domains = domain_get_node_domains($nodes);
foreach ($nodes as $node) {
if (!isset($node->nid)) {
continue;
}
$defaults = domain_get_node_defaults($node->type);
$nodes[$node->nid]->domains = isset($domains[$node->nid]['domain_id']) ? $domains[$node->nid]['domain_id'] : array();
$nodes[$node->nid]->domain_site = isset($domains[$node->nid]['domain_site']) ? $domains[$node->nid]['domain_site'] : $defaults['domain_site'];
$nodes[$node->nid]->subdomains = array();
if (!empty($nodes[$node->nid]->domain_site)) {
$nodes[$node->nid]->subdomains[] = t('All affiliates');
}
if (!empty($nodes[$node->nid]->domains)) {
foreach ($nodes[$node->nid]->domains as $gid) {
$domain = domain_lookup($gid);
$nodes[$node->nid]->subdomains[] = $domain['sitename'];
}
}
else {
$nodes[$node->nid]->subdomains[] = t('This node is not assigned to a domain.');
}
}
}
function domain_node_view($node, $view_mode) {
if (empty($node->nid) || !in_array($view_mode, array(
'full',
'teaser',
))) {
return;
}
$output = '';
if (variable_get('domain_debug', 0) && user_access('set domain access')) {
if (!empty($node->subdomains)) {
$items = array();
foreach ($node->subdomains as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', array(
'items' => $items,
'title' => t('Assigned domains'),
));
}
if (!empty($node->editors)) {
$items = array();
foreach ($node->editors as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', array(
'items' => $items,
'title' => t('Editors'),
));
}
if (empty($output)) {
$output = t('This node is not assigned to a domain.');
}
$node->content['domain'] = array(
'#markup' => $output,
);
}
}
function domain_node_delete($node) {
db_delete('domain_access')
->condition('nid', $node->nid)
->execute();
}
function domain_node_presave($node) {
if (empty($node->devel_generate['domains'])) {
return;
}
$checked = array_filter($node->devel_generate['domains']);
if (empty($checked)) {
return;
}
shuffle($checked);
$domains = array_combine(array_values($checked), array_values($checked));
if (!empty($domains)) {
if (count($domains) > 1) {
$howmany = rand(0, count($domains) - 1);
for ($i = 0; $i < $howmany; $i++) {
array_pop($domains);
}
}
$node->domains = $domains;
$node->domain_source = current($domains);
$node->domain_site = $node->devel_generate['domain_site'] == 'all' ? 1 : ($node->devel_generate['domain_site'] == 'random' ? rand(0, 1) == 1 : 0);
$node->domains = array();
foreach ($domains as $id) {
$node->domains[$id] = $id;
}
}
}
function domain_node_insert($node) {
domain_node_access_records($node);
}
function domain_node_update($node) {
domain_node_access_records($node);
}
function domain_node_type_delete($info) {
variable_del('domain_node_' . $info->type);
}
function domain_get_node_match($nid) {
$domain =& drupal_static(__FUNCTION__, array());
if (isset($domain[$nid])) {
return $domain[$nid];
}
$id = db_query("SELECT gid FROM {domain_access} WHERE nid = :nid AND realm = :realm ORDER BY gid", array(
':nid' => $nid,
':realm' => 'domain_id',
))
->fetchField();
$source = NULL;
if ($id !== FALSE) {
$source = domain_lookup($id);
}
else {
$source = domain_get_domain();
}
drupal_alter('domain_source', $source, $nid);
$domain[$nid] = $source;
return $source;
}
function domain_get_content_urls($node) {
$urls = array();
if (empty($node->domains)) {
$options['absolute'] = TRUE;
$urls[] = url('node/' . $node->id, $options);
}
$weight = -100;
foreach ($node->domains as $domain_id) {
$domain = domain_load($domain_id);
if (!$domain['valid']) {
continue;
}
$path = drupal_get_path_alias('node/' . $node->nid, $node->language);
$options['language'] = $node->language;
domain_path($domain_id, $path, $options, $path);
$url = $domain['path'] . $path;
if ($domain['weight'] > $weight) {
$new[$domain_id] = $url;
$urls = $new + $urls;
}
else {
$urls[$domain_id] = $url;
}
$weight = $domain['weight'];
}
if (isset($node->domain_source) && isset($urls[$node->domain_source])) {
$canonical[$node->domain_source] = $urls[$node->domain_source];
unset($urls[$node->domain_source]);
$urls = $canonical + $urls;
}
return $urls;
}
function domain_get_path_match($path) {
$_domain = domain_get_domain();
if (!isset($_domain['path'])) {
$_domain = domain_lookup($_domain['domain_id'], NULL, TRUE);
}
$source = $_domain;
drupal_alter('domain_source_path', $source, $path);
return $source;
}
function domain_get_node_domains($nodes) {
$lookup =& drupal_static(__FUNCTION__, array());
if (!is_array($nodes)) {
$node_ids[$nodes] = $nodes;
$array = FALSE;
}
else {
$node_ids = $nodes;
$array = TRUE;
}
if (!$array && isset($lookup[$nodes])) {
return $lookup[$nodes];
}
$domains = array(
'domain_id' => array(),
'domain_site' => FALSE,
);
$records = array();
$result = db_query("SELECT nid, gid, realm FROM {domain_access} WHERE nid IN (:nid)", array(
':nid' => array_keys($node_ids),
));
foreach ($result as $data) {
if ($data->realm == 'domain_id') {
$records[$data->nid]['domain_id'][$data->gid] = $data->gid;
}
elseif ($data->realm == 'domain_site') {
$records[$data->nid]['domain_site'] = TRUE;
}
}
foreach ($node_ids as $nid => $value) {
$lookup[$nid] = $domains;
foreach (array(
'domain_id',
'domain_site',
) as $key) {
if (isset($records[$nid][$key])) {
$lookup[$nid][$key] = $records[$nid][$key];
}
}
}
if ($array) {
return $lookup;
}
if (isset($lookup[$nodes])) {
return $lookup[$nodes];
}
return $domains;
}
function domain_node_grants($account, $op) {
$_domain = domain_get_domain();
$grants = array();
domain_user_set($account);
if ($op == 'view') {
if (!empty($_domain['site_grant'])) {
$grants['domain_site'][] = 0;
}
if (isset($_domain['domain_id'])) {
$grants['domain_id'][] = $_domain['domain_id'];
}
if (domain_grant_all()) {
$grants = array(
'domain_all' => array(
0,
),
);
}
}
else {
$domains = $account->domain_user;
$perm = 'delete domain content';
if ($op == 'update') {
$perm = 'edit domain content';
}
if (user_access($perm, $account)) {
if (!empty($domains)) {
foreach ($domains as $id) {
if (abs($id) > 0) {
if ($id > 0) {
$grants['domain_id'][] = $id;
}
else {
$grants['domain_id'][] = 0;
}
}
}
}
}
}
if (!empty($grants['domain_id']) && user_access('view unpublished domain content', $account)) {
foreach ($grants['domain_id'] as $id) {
$domains = $account->domain_user;
if (!empty($domains[$id])) {
$grants['domain_unpublished'][] = $id;
}
}
}
return $grants;
}
function domain_node_access_records($node) {
$grants = array();
if (!isset($node->domain_site)) {
$defaults = domain_get_node_defaults($node->type, array(
domain_get_domain(),
));
$node->domain_site = $defaults['domain_site'];
$node->domains = $defaults['domain_id'];
}
if (!empty($node->domains_raw)) {
if (!isset($node->domains)) {
$node->domains = array();
}
foreach ($node->domains_raw as $value) {
if (!in_array($value, $node->domains)) {
$node->domains[$value] = $value;
}
}
}
if (!empty($node->domain_site)) {
$grants[] = array(
'realm' => 'domain_site',
'gid' => 0,
'grant_view' => $node->status,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
}
if (!empty($node->domains)) {
$domains = array_filter($node->domains);
}
$realm = 'domain_id';
if (!$node->status) {
$realm = 'domain_unpublished';
}
if (!empty($domains)) {
foreach (array_filter($node->domains) as $key => $value) {
$grants[] = array(
'realm' => $realm,
'gid' => $key,
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
}
else {
$default = domain_default();
$grants[] = array(
'realm' => $realm,
'gid' => $default['domain_id'],
'grant_view' => 1,
'grant_update' => 1,
'grant_delete' => 1,
'priority' => 0,
);
}
_domain_store_grants($node->nid, $grants);
return $grants;
}
function _domain_store_grants($nid, $grants = array()) {
if ($nid > 0 && !empty($grants)) {
db_delete('domain_access')
->condition('nid', $nid)
->execute();
$values = array();
foreach ($grants as $grant) {
if ($grant['realm'] == 'domain_unpublished') {
$grant['realm'] = 'domain_id';
}
$values[] = array(
'nid' => $nid,
'gid' => $grant['gid'],
'realm' => $grant['realm'],
);
}
$query = db_insert('domain_access')
->fields(array(
'nid',
'gid',
'realm',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
drupal_static_reset('domain_get_node_domains');
drupal_static_reset('domain_get_node_match');
domain_set_default_grant();
}
function domain_set_default_grant($reset = FALSE) {
$check =& drupal_static(__FUNCTION__, NULL);
if (is_null($check) || $reset) {
$check = (bool) db_query_range("SELECT 1 FROM {node_access} WHERE realm = :realm AND gid = :gid", 0, 1, array(
':realm' => 'domain_all',
':gid' => 0,
))
->fetchField();
if (empty($check)) {
db_insert('node_access')
->fields(array(
'nid' => 0,
'gid' => 0,
'realm' => 'domain_all',
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
))
->execute();
}
}
}
function domain_node_access($node, $op, $account) {
domain_user_set($account);
if (empty($account->domain_user)) {
return NODE_ACCESS_IGNORE;
}
$function = "domain_node_access_{$op}";
if (function_exists($function)) {
$type = is_string($node) ? $node : $node->type;
return $function($type, $node, $op, $account);
}
return NODE_ACCESS_IGNORE;
}
function domain_node_access_view($type, $node, $op, $account) {
domain_user_set($account);
if (!empty($node->status)) {
return NODE_ACCESS_IGNORE;
}
if (empty($node->domains) || empty($account->domain_user)) {
return NODE_ACCESS_IGNORE;
}
if (!user_access('view unpublished domain content', $account)) {
return NODE_ACCESS_IGNORE;
}
foreach ($node->domains as $key => $value) {
if (!empty($account->domain_user[$key])) {
return NODE_ACCESS_ALLOW;
}
}
return NODE_ACCESS_IGNORE;
}
function domain_node_access_create($type, $node, $op, $account) {
domain_user_set($account);
$_domain = domain_get_domain();
if (!isset($account->domain_user[$_domain['domain_id']])) {
return NODE_ACCESS_IGNORE;
}
$permission = "{$op} {$type} content on assigned domains";
if (user_access($permission, $account)) {
return NODE_ACCESS_ALLOW;
}
return NODE_ACCESS_IGNORE;
}
function domain_node_access_update($type, $node, $op, $account) {
domain_user_set($account);
if (empty($node->domains)) {
return NODE_ACCESS_IGNORE;
}
else {
$ignore = TRUE;
foreach ($node->domains as $domain_id) {
if (isset($account->domain_user[$domain_id])) {
$ignore = FALSE;
break;
}
}
if ($ignore) {
return NODE_ACCESS_IGNORE;
}
}
$permission = "{$op} {$type} content on assigned domains";
if (user_access($permission, $account)) {
return NODE_ACCESS_ALLOW;
}
return NODE_ACCESS_IGNORE;
}
function domain_node_access_delete($type, $node, $op, $account) {
return domain_node_access_update($type, $node, $op, $account);
}
function domain_enable() {
domain_set_default_grant(TRUE);
$default = domain_default();
$count = (bool) db_query_range("SELECT 1 FROM {domain_access}", 0, 1)
->fetchField();
if (empty($count)) {
$rule = DOMAIN_INSTALL_RULE;
$site = DOMAIN_SITE_GRANT;
$values = array();
$result = db_query("SELECT nid FROM {node}");
foreach ($result as $node) {
if (!empty($site)) {
$values[] = array(
'nid' => $node->nid,
'gid' => 0,
'realm' => 'domain_site',
);
}
if (!empty($rule)) {
$values[] = array(
'nid' => $node->nid,
'gid' => $default['domain_id'],
'realm' => 'domain_id',
);
}
}
$query = db_insert('domain_access')
->fields(array(
'nid',
'gid',
'realm',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
if (!DOMAIN_ASSIGN_USERS) {
return;
}
$result = db_query("SELECT uid FROM {users} WHERE uid > 0");
$values = array();
foreach ($result as $account) {
$check = (bool) db_query_range("SELECT COUNT(uid) FROM {domain_editor} WHERE uid = :uid", 0, 1, array(
':uid' => $account->uid,
))
->fetchField();
if (empty($check)) {
$values[] = array(
'domain_id' => $default['domain_id'],
'uid' => $account->uid,
);
}
}
$query = db_insert('domain_editor')
->fields(array(
'domain_id',
'uid',
));
foreach ($values as $record) {
$query
->values($record);
}
$query
->execute();
}
function domain_form_alter(&$form, &$form_state, $form_id) {
$forms = module_invoke_all('domain_ignore');
if (in_array($form_id, $forms)) {
return;
}
domain_warning_check($form_id);
$seo = variable_get('domain_seo', 0);
if ($seo && isset($form['#action'])) {
$domain = domain_initial_domain();
$action = parse_url($form['#action']);
if (isset($action['query'])) {
$action['path'] .= '?';
}
else {
$action['query'] = '';
}
if (empty($action['host'])) {
$form['#action'] = $domain['scheme'] . '://' . $domain['subdomain'] . $action['path'] . $action['query'];
}
}
if (empty($form['#node_edit_form'])) {
return;
}
global $user;
domain_user_set($user);
$_domain = domain_get_domain();
$defaults = domain_get_node_defaults($form['#node']->type);
$default_domain_site = (int) $defaults['domain_site'];
if (isset($form['#node']->nid) && !empty($form['#node']->domains)) {
$raw = $form['#node']->domains;
}
else {
$raw = $defaults['domain_id'];
}
$options = array();
$format = domain_select_format();
foreach (domain_domains() as $data) {
if ($data['valid'] || user_access('access inactive domains')) {
$options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
if (user_access('set domain access')) {
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access options'),
'#collapsible' => TRUE,
'#collapsed' => variable_get('domain_collapse_options', 0),
);
if (variable_get('domain_vertical_tab', 0)) {
$form['domain']['#group'] = 'additional_settings';
$form['domain']['#attributes'] = array(
'class' => array(
'domain-access-options-form',
),
);
$form['domain']['#attached'] = array(
'js' => array(
drupal_get_path('module', 'domain') . '/domain.node.js',
array(
'data' => array(
'domain' => array(
'fieldType' => $format,
),
),
'type' => 'setting',
),
),
);
}
$form['domain']['domain_site'] = array(
'#type' => 'checkbox',
'#title' => t('Send to all affiliates'),
'#required' => FALSE,
'#description' => t('Select if this content can be shown to all affiliates. This setting will override the options below, but you must still select a domain that "owns" this content.'),
'#default_value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : $default_domain_site,
);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $options,
'#required' => TRUE,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $defaults['domain_id'],
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
else {
$action = domain_form_permission_check();
if (!empty($action)) {
$user->domain_user = domain_get_user_domains($user);
$user_domains = array();
$default_options = array();
$user_options = array();
$raw_options = array();
if (!empty($user->domain_user)) {
foreach ($user->domain_user as $key => $value) {
if (abs($value) > 0) {
$user_domains[] = $value;
}
}
if (in_array($_domain['domain_id'], $user_domains)) {
$first_domain = $_domain['domain_id'];
}
else {
$first_domain = current($user_domains);
}
foreach ($options as $key => $value) {
if (in_array($key, $user_domains)) {
$user_options[$key] = $value;
}
}
}
foreach ($raw as $key => $value) {
if (in_array($value, $user_domains)) {
$default_options[] = $value;
}
else {
$raw_options[] = $value;
}
}
switch ($action) {
case 1:
$root = domain_default();
if ($root['domain_id'] != $_domain['domain_id']) {
domain_goto($root);
}
break;
case 2:
$domain = domain_lookup($first_domain);
if ($domain == -1 || empty($domain['valid']) && !user_access('access inactive domains')) {
domain_goto(domain_default());
}
elseif ($domain['domain_id'] != $_domain['domain_id']) {
domain_goto($domain);
}
break;
case 3:
if (empty($user_options)) {
drupal_access_denied();
drupal_exit();
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate publishing options'),
'#collapsible' => TRUE,
'#collapsed' => variable_get('domain_collapse_options', 0),
);
if (variable_get('domain_vertical_tab', 0)) {
$form['domain']['#group'] = 'additional_settings';
$form['domain']['#attributes'] = array(
'class' => array(
'domain-access-options-form',
),
);
$format = domain_select_format();
$form['domain']['#attached'] = array(
'js' => array(
drupal_get_path('module', 'domain') . '/domain.node.js',
array(
'data' => array(
'domain' => array(
'fieldType' => $format,
),
),
'type' => 'setting',
),
),
);
}
if (!empty($form['#node']->nid)) {
$raw = $raw_options;
}
else {
$raw = array();
}
empty($raw) ? $required = TRUE : ($required = FALSE);
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $user_options,
'#required' => $required,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $default_options,
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($user_options) > 10 ? 10 : count($user_options);
}
$list = array();
if (!empty($form['#node']->domain_site)) {
$list[]['data'] = t('All affiliates');
}
if (!empty($raw)) {
foreach ($raw as $did) {
$raw_domains = domain_lookup($did);
$list[]['data'] = check_plain($raw_domains['sitename']);
}
}
if (!empty($list)) {
$form['domain']['domains_notes'] = array(
'#type' => 'item',
'#title' => t('Publishing status'),
'#markup' => theme('item_list', array(
'items' => $list,
)),
'#description' => t('This content has also been published to these affiliates.'),
);
}
break;
}
}
$form['domain_site'] = array(
'#type' => 'value',
'#value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : $default_domain_site,
);
$form['domains_raw'] = array(
'#type' => 'value',
'#value' => $raw,
);
}
}
function domain_form_node_type_form_alter(&$form, &$form_state, $form_id) {
$options = array(
DOMAIN_ALL => t('All domains'),
DOMAIN_ACTIVE => t('Author\'s currently active domain'),
);
foreach (domain_domains() as $key => $value) {
$options[$value['machine_name']] = $value['sitename'];
}
$default_values = domain_default_node_access_settings($form['#node_type']->type);
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#access' => user_access('set domain access'),
);
$form['domain']['domain_node'] = array(
'#title' => t('Publish to'),
'#description' => t('You may set default domain publishing options when new content of this type will be created. When you publish to <em>All domains</em>, you can additionally define domain memberships.'),
'#type' => 'checkboxes',
'#tree' => TRUE,
'#options' => $options,
'#default_value' => $default_values,
);
}
function domain_form_system_site_information_settings_alter(&$form, &$form_state) {
if (variable_get('domain_sitename_override', 1)) {
$form['#submit'][] = 'domain_form_sitename_submit';
}
}
function domain_form_sitename_submit($form, &$form_state) {
if (isset($form['domain_settings'])) {
return;
}
db_update('domain')
->condition('domain_id', 0)
->fields(array(
'sitename' => $form_state['values']['site_name'],
))
->execute();
drupal_set_message(t('Primary domain settings updated.'));
}
function domain_form_permission_check() {
$perms = array(
'publish from default domain' => 1,
'publish from assigned domain' => 2,
'publish to any assigned domain' => 3,
);
$action = NULL;
foreach ($perms as $perm => $value) {
if (user_access($perm)) {
$action = $value;
}
}
return $action;
}
function domain_form_devel_generate_content_form_alter(&$form, &$form_state) {
$form['submit']['#weight'] = 10;
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain Access Options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 9,
);
$form['domain']['domain_site'] = array(
'#type' => 'select',
'#title' => t('Send to all affiliates'),
'#options' => array(
'none' => t('Never'),
'all' => t('Always'),
'random' => t('Randomly decide'),
),
'#description' => t('If you choose "always" or "randomly" you must select at least one domain below.'),
);
$format = domain_select_format();
foreach (domain_domains() as $data) {
if ($data['valid'] || user_access('access inactive domains')) {
$options[$data['domain_id']] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
}
}
$form['domain']['domains'] = array(
'#type' => empty($format) ? 'checkboxes' : 'select',
'#title' => t('Publish to'),
'#options' => $options,
'#description' => t('Generated content will be accessible on any or all of the domains checked above.'),
);
if ($format) {
$form['domain']['domains']['#multiple'] = TRUE;
$form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
}
}
function domain_grant_all($reset = FALSE) {
$grant =& drupal_static(__FUNCTION__);
$options = array();
if (!isset($grant) || $reset) {
$grant = FALSE;
if (variable_get('domain_search', 0) && arg(0) == 'search') {
$options['search'] = TRUE;
$grant = TRUE;
}
if (!$grant) {
$ref = explode('/', request_uri());
$script = array_pop($ref);
if (variable_get('domain_cron_rule', 1) && domain_cron_status()) {
$options['script'] = 'cron.php';
$grant = TRUE;
}
elseif (variable_get('domain_xmlrpc_rule', 0) && $script == 'xmlrpc.php') {
$options['script'] = $script;
$grant = TRUE;
}
}
if (!$grant) {
$pages = variable_get('domain_grant_all', "user/*/track");
$options['pages'] = $pages;
$regexp = '/^(' . preg_replace(array(
'/(\\r\\n?|\\n)/',
'/\\\\\\*/',
'/(^|\\|)\\\\<front\\\\>($|\\|)/',
), array(
'|',
'.*',
'\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
), preg_quote($pages, '/')) . ')$/';
$page_match = preg_match($regexp, $_GET['q']);
if (!$page_match && function_exists('drupal_get_path_alias')) {
$path = drupal_get_path_alias($_GET['q']);
if ($path != $_GET['q']) {
$page_match = preg_match($regexp, $path);
}
}
if ($page_match) {
$options['page_match'] = TRUE;
$grant = TRUE;
}
}
}
drupal_alter('domain_grant_all', $grant, $options);
return $grant;
}
function domain_cron_queue_info_alter(&$queues) {
domain_cron_status(TRUE);
}
function domain_cron_status($status = NULL) {
$return =& drupal_static(__FUNCTION__, FALSE);
if (is_null($status)) {
return $return;
}
$return = $status;
return $return;
}
function domain_modules_enabled() {
domain_bootstrap_register();
domain_check_for_update();
}
function domain_modules_disabled() {
domain_bootstrap_register();
}
function domain_bootstrap_register() {
$modules = array();
$lookup = module_implements('domain_bootstrap_lookup');
$full = module_implements('domain_bootstrap_full');
$modules = array_merge($lookup, $full);
variable_set('domain_bootstrap_modules', $modules);
}
function domain_resolve_host($name = '') {
if (empty($name)) {
$name = domain_request_name();
}
return domain_lookup_simple($name);
}
function domain_request_name() {
if (empty($_SERVER['HTTP_HOST'])) {
$domain = domain_default(FALSE, FALSE);
return $domain['subdomain'];
}
return strtolower(rtrim($_SERVER['HTTP_HOST']));
}
function domain_invalid_domain_requested() {
global $_domain, $user;
if (user_access('access inactive domains')) {
return;
}
$path = drupal_get_normal_path($_GET['q']);
$allow = array_filter(module_invoke_all('domain_invalid_request', $path, $_domain, $user));
if (!empty($allow)) {
return;
}
$node = menu_get_object();
if (empty($node->nid)) {
$item = menu_get_item();
$path = $item['href'];
if (drupal_is_front_page($item['href'])) {
$path = '';
}
$default = domain_default();
watchdog('domain', 'Invalid domain requested by %user on %domain; redirected to %default.', array(
'%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
'%domain' => $_domain['sitename'],
'%default' => $default['sitename'],
), WATCHDOG_WARNING);
drupal_goto($default['path'] . drupal_get_path_alias($path));
}
$path = "node/{$node->nid}";
$domain = domain_get_node_match($node->nid);
if ($domain['valid']) {
$redirect = $domain;
}
elseif (!empty($node->domains)) {
foreach ($node->domains as $domain_id) {
$domain = domain_lookup($domain_id);
if ($domain['valid']) {
$redirect = $domain;
break;
}
}
}
$extra = ' ' . t('node page.');
if (empty($redirect)) {
$redirect = domain_default();
$path = '';
$extra = '.';
}
watchdog('domain', 'Invalid domain requested by %user on %domain, redirected to %redirect', array(
'%user' => isset($user->name) ? $user->name : variable_get('anonymous', t('Anonymous')),
'%domain' => $_domain['sitename'],
'%redirect' => $redirect['sitename'] . $extra,
), WATCHDOG_WARNING);
drupal_goto($redirect['path'] . drupal_get_path_alias($path));
}
function domain_domain_invalid_request($path, $_domain, $account) {
if ($path == 'user/login') {
return TRUE;
}
return FALSE;
}
function domain_node_save_redirect() {
global $_domain;
if (!isset($_SESSION['domain_save_id'])) {
return;
}
$domain_id = $_SESSION['domain_save_id'];
unset($_SESSION['domain_save_id']);
$source = domain_lookup($domain_id);
if ($source['domain_id'] != -1 && $source['domain_id'] != $_domain['domain_id'] && ($source['valid'] || user_access('access inactive domains'))) {
domain_goto($source);
}
}
function domain_lookup_simple($name, $reset = FALSE) {
$cache =& drupal_static(__FUNCTION__, array());
if (empty($name)) {
return array();
}
if ($reset || !isset($cache[$name])) {
$domain = db_query("SELECT domain_id, subdomain, sitename FROM {domain} WHERE subdomain = :subdomain", array(
':subdomain' => $name,
))
->fetchAssoc();
if (!is_array($domain)) {
$domain = array();
}
if (!isset($domain['domain_id'])) {
$domain['domain_id'] = domain_default_id();
}
$domain['subdomain'] = $name;
$domain_new = _domain_bootstrap_invoke_all('lookup', $domain);
if (is_array($domain_new)) {
if (isset($domain_new['domain_id']) && is_array($domain_new['domain_id'])) {
foreach ($domain_new as $key => $value) {
if (is_array($value)) {
$domain_new[$key] = $value[0];
}
}
$modules = array();
foreach (_domain_bootstrap_modules() as $module) {
if (function_exists($module . '_domain_bootstrap_lookup')) {
$modules[] = $module;
}
}
$lookup = domain_lookup($domain_new['domain_id']);
$domain_new['error'] = t('domain lookup. More than one registered domain was returned. Defaulting to %domain. The likely cause is a conflict between %modules', array(
'%domain' => $lookup['sitename'],
'%modules' => implode(', ', $modules),
));
}
$domain = array_merge($domain, $domain_new);
}
$cache[$name] = $domain;
}
return $cache[$name];
}
function domain_domain_install() {
domain_bootstrap_register();
}
function domain_domain_batch() {
$batch = array();
$batch['subdomain'] = array(
'#form' => array(
'#title' => t('Domains'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 80,
'#description' => t('Enter the host value of the domain. No http:// or slashes.'),
'#required' => TRUE,
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain values.'),
'#data_type' => 'string',
'#update_all' => FALSE,
'#weight' => -10,
);
$batch['sitename'] = array(
'#form' => array(
'#title' => t('Names'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 80,
'#description' => t('The human-readable name for this domain.'),
'#required' => TRUE,
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain names.'),
'#data_type' => 'string',
'#update_all' => FALSE,
'#weight' => -10,
);
$batch['scheme'] = array(
'#form' => array(
'#title' => t('URL schemes'),
'#type' => 'radios',
'#options' => array(
'http' => 'http://',
'https' => 'https://',
),
'#description' => t('The URL scheme for accessing this domain.'),
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain URL schemes.'),
'#system_default' => variable_get('domain_scheme', 'http://'),
'#data_type' => 'string',
'#update_all' => TRUE,
'#weight' => -10,
);
$batch['valid'] = array(
'#form' => array(
'#title' => t('Valid domains'),
'#type' => 'radios',
'#options' => array(
1 => t('Active'),
0 => t('Inactive'),
),
'#description' => t('Allows users to access this domain.'),
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain status flags.'),
'#system_default' => 1,
'#data_type' => 'integer',
'#update_all' => TRUE,
'#weight' => -10,
);
foreach ($batch as $key => $value) {
$batch[$key]['#module'] = t('Domain Access');
}
return $batch;
}
function domain_warning_check($form_id) {
$_warning =& drupal_static(__FUNCTION__);
if (empty($_POST) && empty($_warning)) {
global $_domain;
$forms = array();
$forms = module_invoke_all('domain_warning');
drupal_alter('domain_warnings', $forms);
if ($form_id == 'domain_batch_form' || arg(2) != 'domain' && in_array($form_id, array_keys($forms))) {
$default = domain_default();
$link_text = '';
$link = isset($forms[$form_id]) ? $forms[$form_id] : NULL;
if (!empty($link)) {
$elements = array();
foreach ($_domain as $key => $value) {
if (!is_array($value)) {
$elements[$key] = $value;
}
}
$replace = explode('|', '%' . implode('|%', array_keys($elements)));
$values = explode('|', implode('|', $elements));
$link = str_replace($replace, $values, $link);
$link_text = t('You may submit changes to the current domain at <a href="!url">%link</a>.', array(
'!url' => url($link),
'%link' => $link,
));
}
$_path = domain_get_uri($default);
$message = '';
if ($_domain['domain_id'] != $default['domain_id']) {
$message = ' ' . t('and <a href="!url">may need to be entered from !domain</a>', array(
'!url' => $_path,
'!domain' => $default['subdomain'],
));
}
else {
$link_text = '';
}
drupal_set_message(t('This form submits changes to your default
configuration!message. !link', array(
'!message' => $message,
'!link' => $link_text,
)), 'warning', FALSE);
}
$_warning = TRUE;
}
}
function domain_path($domain_id, &$path, &$options, $original_path) {
$modules = _domain_path_modules();
if (!empty($modules)) {
foreach ($modules as $module) {
$function = $module . '_domain_path';
$function($domain_id, $path, $options, $original_path);
}
}
}
function _domain_path_modules() {
$modules =& drupal_static(__FUNCTION__);
if (!isset($modules)) {
$modules = module_implements('domain_path');
}
return $modules;
}
function domain_domain_ignore() {
return array(
'update_script_selection_form',
'authorize_filetransfer_form',
);
}
function domain_node_access_explain($row) {
$_domain = domain_get_domain();
$active = $_domain['subdomain'];
$domain = domain_lookup($row->gid);
$return = t('Domain Access') . ' -- ';
switch ($row->realm) {
case 'domain_all':
if (domain_grant_all() == TRUE) {
$return .= t('True: Allows content from all domains to be shown.');
}
else {
if (!empty($_domain['site_grant'])) {
$return .= t('False: Only allows content from the active domain (%domain) or from all affiliates.', array(
'%domain' => $active,
));
}
else {
$return .= t('False: Only allows content from the active domain (%domain).', array(
'%domain' => $active,
));
}
}
break;
case 'domain_site':
$return .= t('Viewable on all affiliate sites.');
break;
case 'domain_id':
$return .= t('Viewable on %domain<br />%domain privileged editors may edit and delete', array(
'%domain' => $domain['subdomain'],
));
break;
case 'domain_unpublished':
$return .= t('Unpublished node. Viewable on %domain<br />%domain privileged editors may edit and delete', array(
'%domain' => $domain['subdomain'],
));
break;
default:
$return = NULL;
break;
}
return $return;
}
function domain_node_access_acknowledge($grant) {
if ($grant['realm'] == 'domain_all') {
return TRUE;
}
}
function domain_field_extra_fields() {
$base = array(
'domain' => array(
'label' => t('Domain access'),
'description' => t('Domain Access settings.'),
'weight' => 1,
),
);
$base_display = $base;
$base_display['domain']['label'] .= ' ' . t('(debugging only)');
$extra = array();
foreach (node_type_get_names() as $name => $value) {
$extra['node'][$name]['form'] = $base;
$extra['node'][$name]['display'] = $base_display;
}
$extra['user']['user']['form'] = $base;
$extra['user']['user']['display'] = $base_display;
return $extra;
}
function domain_url_encode($string) {
$string = drupal_strtolower($string);
$string = str_replace('/', '', $string);
$pattern = '/[^a-zA-Z0-9\\/]+/ ';
$string = preg_replace($pattern, '', $string);
$string = preg_replace('/\\s+/', '', $string);
return $string;
}
function domain_simpletest() {
$module_name = 'domain';
$dir = drupal_get_path('module', $module_name) . '/tests';
$tests = file_scan_directory($dir, '/\\.test$/');
return array_keys($tests);
}
function domain_query_node_access_alter(QueryAlterableInterface $query) {
if (domain_admin_filter()) {
domain_alter_node_query($query, 'node');
}
}
function domain_admin_filter() {
static $return;
if (!isset($return)) {
$admin_force = variable_get('domain_force_admin', FALSE);
if (empty($admin_force) || !user_access('bypass node access') || domain_grant_all()) {
$return = FALSE;
}
else {
$return = TRUE;
}
}
return $return;
}
function domain_alter_node_query(QueryAlterableInterface $query, $type) {
global $user;
if (!($account = $query
->getMetaData('account'))) {
$account = $user;
}
if (!($op = $query
->getMetaData('op'))) {
$op = 'view';
}
if ($op != 'view') {
return;
}
$tables = $query
->getTables();
$base_table = $query
->getMetaData('base_table');
if (!$base_table) {
$fallback = '';
foreach ($tables as $alias => $table_info) {
if (!$table_info instanceof SelectQueryInterface) {
$table = $table_info['table'];
if ($table == 'node') {
$base_table = $table;
break;
}
if (!$base_table) {
$schema = drupal_get_schema($table);
if (isset($schema['fields']['nid'])) {
if (isset($schema['foreign keys'])) {
foreach ($schema['foreign keys'] as $relation) {
if ($relation['table'] === 'node' && $relation['columns'] === array(
'nid' => 'nid',
)) {
$base_table = $table;
}
}
}
else {
$fallback = $table;
}
}
}
}
}
if (!$base_table) {
if ($fallback) {
watchdog('security', 'Your node listing query is using @fallback as a base table in a query tagged for node access. This might not be secure and might not even work. Specify foreign keys in your schema to node.nid ', array(
'@fallback' => $fallback,
), WATCHDOG_WARNING);
$base_table = $fallback;
}
else {
throw new Exception(t('Query tagged for node access but there is no nid. Add foreign keys to node.nid in schema to fix.'));
}
}
}
$grants = node_access_grants($op, $account);
if ($type == 'entity') {
$node_conditions = db_and();
}
foreach ($tables as $nalias => $tableinfo) {
$table = $tableinfo['table'];
if (!$table instanceof SelectQueryInterface && $table == $base_table) {
$subquery = db_select('node_access', 'na')
->fields('na', array(
'nid',
));
$grant_conditions = db_or();
foreach ($grants as $realm => $gids) {
foreach ($gids as $gid) {
$grant_conditions
->condition(db_and()
->condition('na.gid', $gid)
->condition('na.realm', $realm));
}
}
if (count($grant_conditions
->conditions())) {
$subquery
->condition($grant_conditions);
}
$subquery
->condition('na.grant_' . $op, 1, '>=');
$field = 'nid';
if ($type == 'entity') {
$base_alias = $nalias;
$field = 'entity_id';
}
$subquery
->where("{$nalias}.{$field} = na.nid");
if ($type == 'entity') {
$node_conditions
->exists($subquery);
}
else {
$query
->exists($subquery);
}
}
}
if ($type == 'entity' && count($subquery
->conditions())) {
$node_conditions
->condition("{$base_alias}.entity_type", 'node');
$or = db_or();
$or
->condition($node_conditions);
$or
->condition("{$base_alias}.entity_type", 'node', '<>');
$query
->condition($or);
}
}
function domain_check_response($domain, $drush = FALSE) {
$t = empty($drush) ? 't' : 'dt';
if (variable_get('domain_skip_domain_check', 0)) {
drupal_set_message($t('Domain response checks have been disabled. Saving an incorrect domain may affect your site.'), 'warning', FALSE);
return FALSE;
}
$url = domain_get_path($domain) . drupal_get_path('module', 'domain') . '/tests/200.png';
$response = drupal_http_request($url, array(
'method' => 'HEAD',
'absolute' => TRUE,
));
if ($response->code != 200) {
return $t('!server is not responding as expected and may not be configured correctly at the server level. Server code !code was returned.', array(
'!server' => $url,
'!code' => $response->code,
));
}
return FALSE;
}
function domain_reassign($old_domain, $new_domain, $table) {
$old_id = $old_domain['domain_id'];
$new_id = $new_domain['domain_id'];
if ($table == 'domain_access') {
$nids = db_query("SELECT nid FROM {domain_access} WHERE realm = 'domain_id' AND gid = :domain_id", array(
':domain_id' => $new_id,
))
->fetchAllAssoc('nid');
$query = db_update('domain_access')
->condition('gid', $old_id)
->condition('realm', 'domain_id')
->fields(array(
'gid' => $new_id,
));
if (!empty($nids)) {
$query
->condition('nid', array_keys($nids), 'NOT IN');
}
$query
->execute();
}
elseif ($table == 'domain_editor') {
$uids = db_query("SELECT uid FROM {domain_editor} WHERE domain_id = :domain_id", array(
':domain_id' => $new_id,
))
->fetchAllAssoc('uid');
$query = db_update('domain_editor')
->condition('domain_id', $old_id)
->fields(array(
'domain_id' => $new_id,
));
if (!empty($uids)) {
$query
->condition('uid', array_keys($uids), 'NOT IN');
}
$query
->execute();
}
module_invoke_all('domain_reassign', $old_domain, $new_domain, $table);
menu_rebuild();
node_access_needs_rebuild(TRUE);
}
function domain_features_api() {
$components = array(
'domain' => array(
'name' => t('Domains'),
'default_hook' => 'domain_default_domains',
'default_file' => FEATURES_DEFAULTS_CUSTOM,
'default_filename' => 'domains',
'feature_source' => TRUE,
'file' => drupal_get_path('module', 'domain') . '/domain.features.inc',
),
);
return $components;
}
function domain_machine_name_load($machine_name, $full = FALSE) {
$domain = db_query('SELECT domain_id, subdomain, sitename, scheme, valid, weight, is_default, machine_name FROM {domain} WHERE machine_name = :machine_name', array(
':machine_name' => $machine_name,
))
->fetchAssoc();
if ($full && !empty($domain)) {
$domain = domain_api($domain, TRUE);
}
return $domain;
}
function domain_machine_name($subdomain) {
return preg_replace('/[^a-z0-9_]+/', '_', $subdomain);
}
function domain_check_machine_name($machine_name) {
return (bool) db_query("SELECT COUNT(1) FROM {domain} WHERE machine_name = :machine_name", array(
':machine_name' => $machine_name,
))
->fetchField();
}
function domain_load_domain_id($machine_name) {
$domains = domain_list_by_machine_name();
if (isset($domains[$machine_name])) {
return $domains[$machine_name]['domain_id'];
}
return FALSE;
}
function domain_list_by_machine_name() {
$domains =& drupal_static(__FUNCTION__);
if (!isset($domains)) {
$list = domain_domains();
foreach ($list as $record) {
$domains[$record['machine_name']] = $record;
}
}
return $domains;
}
function domain_machine_names() {
$list =& drupal_static(__FUNCTION__);
if (isset($list)) {
return $list;
}
$machine_names = db_query("SELECT machine_name FROM {domain_export}")
->fetchAll();
foreach ($machine_names as $machine_name) {
$list[] = $machine_name->machine_name;
}
return $list;
}
function domain_load_machine_name($domain_id) {
return db_query('SELECT machine_name FROM {domain_export} WHERE domain_id = :domain_id', array(
':domain_id' => $domain_id,
))
->fetchField();
}
function domain_features_load($module, $hook, $return = TRUE) {
module_load_include('inc', $module, $module . '.domains');
$function = $module . '_' . $hook;
if ($return && function_exists($function)) {
return $function();
}
}
function domain_features_get_options() {
$options = array(
'wipe-domain-tables' => t('Wipe tables on revert/rebuild'),
'all-domains' => t('Export all domains'),
);
foreach (domain_domains() as $domain) {
$options[$domain['machine_name']] = $domain['subdomain'];
}
return $options;
}
function domain_features_selection($data) {
$list = array();
if (!empty($data['all-domains'])) {
$data = array();
$domains = domain_domains(TRUE);
foreach ($domains as $domain) {
$data[] = $domain['machine_name'];
}
}
foreach ($data as $machine_name) {
$record = domain_machine_name_load($machine_name);
if (!empty($record)) {
$list[$record['machine_name']] = $record['machine_name'];
}
}
return $list;
}
function domain_features_wipe_tables(&$defaults) {
if (!empty($defaults['wipe-domain-tables'])) {
return TRUE;
}
return FALSE;
}
function domain_features_export_wipe_tables_code(&$data, &$code, $export, $type) {
if (!empty($export) && !empty($data['wipe-domain-tables']) || in_array('wipe-domain-tables', $data)) {
$code[] = " \${$type}['wipe-domain-tables'] = 'wipe-domain-tables';";
return TRUE;
}
return FALSE;
}
function domain_features_export_set_wipe_tables(&$export, $data, $type) {
if (in_array('wipe-domain-tables', $data)) {
$export['features'][$type]['wipe-domain-tables'] = 'wipe-domain-tables';
}
}
function domain_update_module_check() {
$list = array();
$modules = system_rebuild_module_data();
$dependencies = $modules['domain']->required_by;
$enabled_dependencies = array_intersect_key($dependencies, module_list());
if (empty($enabled_dependencies)) {
return $list;
}
foreach ($enabled_dependencies as $module => $data) {
module_load_install($module);
if ($schema = module_invoke($module, 'schema')) {
if ($tables = domain_test_schema($schema)) {
$list[$module]['name'] = $modules[$module]->info['name'];
$list[$module]['tables'] = $tables;
}
}
}
return $list;
}
function domain_test_schema($schema = array()) {
$tables = array();
foreach ($schema as $name => $table) {
if (db_table_exists($name) && isset($table['fields']['domain_id']['type']) && $table['fields']['domain_id']['type'] == 'int') {
$query = db_select($name)
->condition('domain_id', 0);
$check = $query
->countQuery()
->execute()
->fetchField();
if ($check) {
$tables[] = $name;
}
}
}
return $tables;
}
function domain_update_tables($list) {
$tables = array();
foreach ($list as $module) {
$tables = array_merge($tables, $module['tables']);
}
return $tables;
}
function domain_update_zero_records($tables) {
$default_id = domain_default_id();
$success = TRUE;
foreach ($tables as $table) {
$transaction = db_transaction();
try {
db_update($table)
->fields(array(
'domain_id' => $default_id,
))
->condition('domain_id', 0)
->execute();
} catch (Exception $e) {
$transaction
->rollback();
watchdog_exception('domain_repair', $e);
drupal_set_message(t('The update failed due to a duplicate record. You must manually correct the {@table} table in your database.', array(
'@table' => $table,
)), 'error');
$success = FALSE;
}
}
return $success;
}
function domain_update_messages(&$messages, $list) {
$t = get_t();
foreach ($list as $module => $data) {
$updated = $t('updated by an administrator');
if (user_access('administer domains')) {
$updated = l($t('updated for compatibility'), 'admin/structure/domain/repair');
}
$messages[] = $t('@module has a domain_id of 0 and needs to be !updated.', array(
'@module' => $data['name'],
'!updated' => $updated,
));
}
}
function domain_check_for_update() {
$list = domain_update_module_check();
if (!empty($list)) {
$messages = array();
domain_update_messages($messages, $list);
if (!drupal_is_cli()) {
drupal_set_message(theme('item_list', array(
'items' => $messages,
)), 'error', FALSE);
}
elseif (function_exists('drush_print')) {
foreach ($messages as $message) {
drush_print(" * {$message}");
}
drush_print(dt(" ** Run 'drush domain-repair' to update these tables."));
}
}
}
function domain_batch_actions() {
$batch =& drupal_static(__FUNCTION__);
if (!empty($batch)) {
return $batch;
}
$batch = module_invoke_all('domain_batch');
drupal_alter('domain_batch', $batch);
return $batch;
}
function domain_preprocess_html(&$variables) {
$classes = domain_page_classes();
foreach ($classes as $class) {
$variables['classes_array'][] = $class;
}
}
function domain_page_classes() {
$classes = array();
$settings = variable_get('domain_classes', 'domain-[current-domain:machine_name]');
if (!empty($settings)) {
$vars = check_plain(token_replace($settings));
$vars = preg_replace('/(\\r\\n|\\n)/', "\r\n", $vars);
$vars = explode("\r\n", token_replace($vars));
foreach ($vars as $var) {
$classes[] = drupal_clean_css_identifier(trim($var));
}
}
return $classes;
}
function domain_filter_info() {
$filters['domain_url'] = array(
'title' => t('Insert domain-sensitive URLs'),
'description' => t('Transforms relative URLs in the format <em>[canonical-url:path/to/item]</em> to canonical URLs.'),
'process callback' => 'domain_url_filter_process',
'tips callback' => 'domain_url_filter_tips',
'cache' => FALSE,
'weight' => -10,
);
return $filters;
}
function domain_url_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
$pattern = '/\\[canonical-url:(.+?)\\]/';
$text = preg_replace_callback($pattern, 'domain_url_replace', $text);
return $text;
}
function domain_url_replace($matches) {
return url($matches[1]);
}
function domain_url_filter_tips($filter, $format, $long) {
return t('Enter a relative URL in the format <em>[canonical-url:path/to/item]</em> to make the URL point to the canonical path.');
}
function domain_metatag_page_cache_cid_parts_alter(&$cid_parts) {
$_domain = domain_get_domain();
$cid_parts['domain_id'] = $_domain['domain_id'];
}
function domain_get_tokens($prefixes = array()) {
if (empty($prefixes)) {
$prefixes = array(
'current-domain',
'default-domain',
);
}
module_load_include('inc', 'domain', 'domain.tokens');
$info = domain_token_info();
foreach ($prefixes as $prefix) {
foreach ($info['tokens']['domain'] as $token => $data) {
$tokens["{$prefix}:{$token}"] = t('!token : !description', array(
'!token' => "[{$prefix}:{$token}]",
'!description' => $data['description'],
));
}
}
return $tokens;
}
function domain_migrate_api() {
$api = array(
'api' => 2,
'destination handlers' => array(
'DomainNodeHandler',
'DomainUserHandler',
),
);
return $api;
}
function domain_get_node_defaults($type, $fallback_domains = array()) {
$_domain = domain_get_domain();
$defaults = array(
'domain_id' => array(),
);
$settings = array_flip(domain_default_node_access_settings($type));
$defaults['domain_site'] = isset($settings['DOMAIN_ALL']) ? TRUE : FALSE;
if (isset($settings['DOMAIN_ACTIVE'])) {
$defaults['domain_id'][$_domain['domain_id']] = $_domain['domain_id'];
}
foreach ($settings as $key => $value) {
if ($key != 'DOMAIN_ACTIVE' && $key != 'DOMAIN_ALL') {
if ($domain = domain_machine_name_load($key)) {
$defaults['domain_id'][$domain['domain_id']] = $domain['domain_id'];
}
}
}
if (empty($defaults['domain_id']) && !empty($fallback_domains)) {
foreach ($fallback_domains as $domain) {
$defaults['domain_id'][$domain['domain_id']] = $domain['domain_id'];
}
}
return $defaults;
}
function domain_default_node_access_settings($type) {
$settings = variable_get('domain_node_' . $type, NULL);
if (is_null($settings)) {
$settings = array(
0 => 'DOMAIN_ACTIVE',
);
if (DOMAIN_INSTALL_RULE) {
$settings[] = 'DOMAIN_ALL';
}
}
elseif (isset($settings['DOMAIN_ALL'])) {
$old_settings = $settings;
$settings = array();
foreach ($old_settings as $key => $value) {
if ($value !== 0 || $key === 0 && $value === 0) {
$settings[] = $value;
}
}
}
return $settings;
}