View source
<?php
define('DOMAIN_INSTALL_RULE', TRUE);
define('DOMAIN_EDITOR_RULE', FALSE);
define('DOMAIN_SITE_GRANT', TRUE);
function domain_init() {
global $_domain, $conf;
$_domain = array();
$_subdomain = strtolower(rtrim($_SERVER['HTTP_HOST']));
$raw_domain = $_subdomain;
if (variable_get('domain_www', 0)) {
$_subdomain = str_replace('www.', '', $_subdomain);
}
$data = db_fetch_array(db_query("SELECT domain_id FROM {domain} WHERE subdomain = '%s'", $_subdomain));
$_domain = domain_lookup($data['domain_id']);
if ($_domain == -1) {
$_domain = domain_default();
if (variable_get('domain_redirect_wildcards', TRUE) && !empty($_domain['subdomain']) && $_subdomain != $_domain['subdomain']) {
$request = domain_get_uri($_domain);
if (variable_get('domain_redirect_alert', TRUE)) {
drupal_set_message(t('You have followed an incorrect link to this website. Please update your links and bookmarks to <a href="!url">!url</a>.', array(
'!url' => $request,
)));
}
drupal_goto($request);
}
}
if ($raw_domain != $_subdomain) {
drupal_goto(domain_get_uri($_domain));
}
if (!$_domain['valid'] && !user_access('administer domains')) {
$_domain = domain_default();
drupal_goto($_domain['path']);
}
$conf['site_name'] = $_domain['sitename'];
}
function domain_menu($may_cache) {
$items = array();
$admin = user_access('administer domains');
if ($may_cache) {
$items[] = array(
'title' => t('Domains'),
'path' => 'admin/build/domain',
'access' => $admin,
'callback' => 'domain_admin',
'callback arguments' => array(
'view',
),
'description' => t('Settings for the Domain Access module.'),
);
$items[] = array(
'title' => t('Domain list'),
'path' => 'admin/build/domain/view',
'access' => $admin,
'type' => MENU_DEFAULT_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array(
'view',
),
'weight' => -10,
);
$items[] = array(
'title' => t('Settings'),
'path' => 'admin/build/domain/settings',
'access' => $admin,
'type' => MENU_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array(
'configure',
),
'weight' => -8,
);
$items[] = array(
'title' => t('Create domain record'),
'path' => 'admin/build/domain/create',
'access' => $admin,
'type' => MENU_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array(
'create',
),
'weight' => -4,
);
$items[] = array(
'title' => t('Node settings'),
'path' => 'admin/build/domain/advanced',
'access' => $admin,
'type' => MENU_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array(
'advanced',
),
'weight' => -2,
);
$batch = module_invoke_all('domainbatch');
if (!empty($batch)) {
$items[] = array(
'title' => t('Batch updating'),
'path' => 'admin/build/domain/batch',
'access' => $admin,
'type' => MENU_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array(
'batch',
),
'weight' => 0,
);
foreach ($batch as $key => $value) {
$items[] = array(
'title' => $value['#form']['#title'],
'path' => 'admin/build/domain/batch/' . $key,
'access' => isset($value['#permission']) ? user_access($value['#permission']) : $admin,
'type' => MENU_CALLBACK,
'callback' => 'domain_admin',
'callback arguments' => array(
'batch',
$key,
),
'weight' => $value['#weight'],
);
}
}
}
else {
$items[] = array(
'title' => t('Edit domain record'),
'path' => 'admin/build/domain/edit',
'access' => $admin,
'type' => MENU_CALLBACK,
'callback' => 'domain_admin',
'callback arguments' => array(
'edit',
arg(4),
),
);
$items[] = array(
'title' => t('Delete domain record'),
'path' => 'admin/build/domain/delete',
'access' => $admin,
'type' => MENU_CALLBACK,
'callback' => 'domain_admin',
'callback arguments' => array(
'delete',
arg(4),
),
);
if (arg(0) == 'admin') {
domain_set_default_grant();
}
}
return $items;
}
function domain_perm() {
$perms = array(
'administer domains',
'assign domain editors',
'edit domain nodes',
'set domain access',
'view domain publishing',
);
return $perms;
}
function domain_block($op = 'list', $delta = 0, $edit = array()) {
global $_domain, $base_url;
$blocks = array();
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Domain switcher'),
);
$blocks[1] = array(
'info' => t('Domain access information'),
);
return $blocks;
break;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = t('Domain switcher');
$items = array();
$domains = domain_domains();
$msg = FALSE;
foreach ($domains as $domain) {
if ($domain['valid']) {
$title = $domain['sitename'];
$allow = TRUE;
}
else {
$title = $domain['sitename'] . ' *';
$allow = FALSE;
if (user_access('administer domains')) {
$msg = TRUE;
$allow = TRUE;
}
}
if ($allow) {
$items[] = l($title, domain_get_uri($domain));
}
}
$block['content'] = theme('item_list', $items);
if ($msg) {
$block['content'] .= t('<em>* Inactive domain.</em>');
}
break;
case 1:
$block['content'] = '';
if (arg(0) == 'node' && is_numeric(arg(1))) {
$block['subject'] = t('Domain access information');
$this_node = node_load(arg(1));
$output = '';
if (!empty($this_node->subdomains)) {
$items = array();
foreach ($this_node->subdomains as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', $items, t('Subdomains'));
}
if (!empty($this_node->editors)) {
$items = array();
foreach ($this_node->editors as $name) {
$items[] = check_plain($name);
}
$output .= theme('item_list', $items, t('Editors'));
}
if (isset($this_node->domain_source)) {
$this_domain = domain_lookup($this_node->domain_source);
$output .= theme('item_list', array(
check_plain($this_domain['sitename']),
), t('Source domain'));
}
if (empty($output)) {
$output = t('This node is not assigned to a domain.');
}
$block['content'] = '<p>' . t('%node is published with the following Domain Access rules:', array(
'%node' => $this_node->title,
)) . '</p>' . $output;
}
return $block;
break;
}
return $block;
break;
}
}
function domain_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'form':
case 'register':
if (is_null($category) || $category == 'account') {
global $_domain;
$result = db_query("SELECT domain_id, subdomain, sitename, scheme FROM {domain}");
$options = array();
if (empty($account->domain_user)) {
$_domain['domain_id'] == 0 ? $default = array(
-1,
) : ($default = array(
$_domain['domain_id'],
));
}
else {
$default = $account->domain_user;
}
$options[-1] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
while ($data = db_fetch_array($result)) {
$options[$data['domain_id']] = check_plain($data['sitename']);
}
if (user_access('assign domain editors')) {
$form['domain_user'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 1,
);
$form['domain_user']['domain_user'] = array(
'#type' => 'checkboxes',
'#options' => $options,
'#title' => t('Domain access setttings'),
'#description' => t('Select the affiliates that this user belongs to. Used to grant editing permissions for users with the "edit domain nodes" permission.'),
'#default_value' => $default,
);
}
else {
$form['domain_user'] = array(
'#type' => 'value',
'#value' => $default,
);
}
return $form;
}
break;
case 'validate':
return array(
'domain_user' => $edit['domain_user'],
);
break;
case 'insert':
case 'update':
if (!isset($edit['domain_user'])) {
return;
}
break;
case 'view':
if (user_access('assign domain editors') && !empty($account->domain_user)) {
$output = '<ul>';
foreach ($account->domain_user as $id) {
if (abs($id) > 0) {
if ($id > 0) {
$domain = domain_lookup($id);
$output .= '<li>' . check_plain($domain['sitename']) . '</li>';
}
else {
$output .= '<li>' . check_plain(variable_get('domain_sitename', variable_get('site_name', 'Drupal'))) . '</li>';
}
}
}
$output .= '</ul>';
$items['domain'] = array(
'title' => t('Domain settings'),
'value' => $output,
'class' => 'status',
);
return array(
t('Domain status') => $items,
);
}
break;
}
}
function domain_cron() {
global $_domain;
$modules = module_implements('domaincron');
if (!empty($modules)) {
$_temp = $_domain;
$domains = domain_domains();
foreach ($domains as $domain) {
if (function_exists('_domain_conf_load')) {
_domain_conf_load($domain);
}
if (function_exists('_domain_prefix_load')) {
_domain_prefix_load($domain);
}
$_domain = $domain;
foreach ($modules as $module) {
module_invoke($module, 'domaincron', $domain);
}
}
$_domain = $_temp;
}
}
function domain_admin($action, $id = NULL) {
include_once 'domain_admin.inc';
$func = 'domain_' . $action;
return $func($id);
}
function domain_lookup($domain_id = NULL, $subdomain = NULL, $reset = FALSE) {
static $domains;
if (is_null($domain_id) && is_null($subdomain)) {
return -1;
}
$key = $domain_id . $subdomain;
if (!isset($domains[$key]) || $reset) {
if ($subdomain) {
$domain = db_fetch_array(db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain} WHERE subdomain = '%s'", $subdomain));
}
else {
if ($domain_id == 0) {
$domain = domain_default();
}
else {
$domain = db_fetch_array(db_query("SELECT domain_id, subdomain, sitename, scheme, valid FROM {domain} WHERE domain_id = %d", $domain_id));
}
}
if (isset($domain['domain_id'])) {
$domains[$key] = domain_api($domain);
}
else {
$domains[$key] = -1;
}
}
return $domains[$key];
}
function domain_default($reset = FALSE) {
static $default;
if (empty($default) || $reset) {
$default['domain_id'] = 0;
$default['sitename'] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
$default['subdomain'] = variable_get('domain_root', '');
$default['scheme'] = variable_get('domain_scheme', 'http');
$default['valid'] = TRUE;
$default = domain_api($default);
}
return $default;
}
function domain_domains($reset = FALSE) {
static $domains;
if (empty($domains) || $reset) {
$domains = array();
$domains[] = domain_default($reset);
$result = db_query("SELECT domain_id FROM {domain}");
while ($data = db_fetch_array($result)) {
$domain = domain_lookup($data['domain_id'], NULL, TRUE);
$domains[$domain['domain_id']] = $domain;
}
}
$sort = variable_get('domain_sort', 'id');
uasort($domains, '_domain_' . $sort . '_sort');
return $domains;
}
function _domain_id_sort($a, $b) {
return $a['domain_id'] < $b['domain_id'] ? -1 : 1;
}
function _domain_name_sort($a, $b) {
return strcmp($a['sitename'], $b['sitename']);
}
function _domain_url_sort($a, $b) {
return strcmp($a['subdomain'], $b['subdomain']);
}
function _domain_rid_sort($a, $b) {
return $a['domain_id'] > $b['domain_id'] ? -1 : 1;
}
function _domain_rname_sort($a, $b) {
return strcmp($b['sitename'], $a['sitename']);
}
function _domain_rurl_sort($a, $b) {
return strcmp($a['subdomain'], $b['subdomain']);
}
function domain_api($domain) {
static $_modules;
if (!isset($_modules)) {
$_modules = module_implements('domainload');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domainload';
$function($domain);
}
}
return $domain;
}
function domain_domainload(&$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 = parse_url($base_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) {
$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) {
global $_domain;
if ($domain != -1 && $_domain['domain_id'] != $domain['domain_id']) {
$path = domain_get_uri($domain);
drupal_goto($path);
}
}
function domain_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'prepare':
case 'load':
$node->domains = array();
$node->editors = array();
$node->domain_site = FALSE;
$result = db_query("SELECT gid, realm FROM {domain_access} WHERE nid = %d AND (realm = '%s' OR realm = '%s' OR realm = '%s')", $node->nid, 'domain_id', 'domain_site', 'domain_editor');
while ($data = db_fetch_object($result)) {
$data->gid == 0 ? $gid = -1 : ($gid = $data->gid);
if ($data->realm == 'domain_id') {
$node->domains[$gid] = $gid;
if ($gid > 0) {
$domain = domain_lookup($gid);
$node->subdomains[] = $domain['sitename'];
}
else {
$node->subdomains[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
}
}
else {
if ($data->realm == 'domain_site') {
$node->domain_site = TRUE;
$node->subdomains[] = t('All affiliates');
}
else {
if ($data->realm == 'domain_editor') {
$node->domain_editor = TRUE;
if ($gid > 0) {
$domain = domain_lookup($gid);
$node->editors[] = $domain['sitename'];
}
else {
$node->editors[] = variable_get('domain_sitename', variable_get('site_name', 'Drupal'));
}
}
}
}
}
break;
case 'view':
if ($a3 !== FALSE || $a4 !== FALSE) {
$output = '';
$debug = variable_get('domain_debug', 0);
if ($debug && user_access('set domain access')) {
if (!empty($node->subdomains)) {
$output .= '<p><b>Subdomains</b></p><ul>';
foreach ($node->subdomains as $name) {
$output .= '<li>' . check_plain($name) . '</li>';
}
$output .= '</ul>';
$node->content['subdomains'] = array(
'#value' => $output,
'#weight' => 20,
);
}
if (!empty($node->editors)) {
$output = '<p><b>Editors</b></p><ul>';
foreach ($node->editors as $name) {
$output .= '<li>' . check_plain($name) . '</li>';
}
$output .= '</ul>';
$node->content['editors'] = array(
'#value' => $output,
'#weight' => 21,
);
}
if (empty($output)) {
$node->content['domain'] = array(
'#value' => t('This node is not assigned to a domain.'),
'#weight' => 22,
);
}
}
}
break;
case 'insert':
case 'update':
$grants = domain_node_access_records($node);
_domain_write_records($node->nid, $grants);
break;
case 'delete':
db_query("DELETE FROM {domain_access} WHERE nid = %d", $node->nid);
break;
}
}
function domain_node_grants($account, $op) {
global $_domain;
$rules = variable_get('domain_access_rules', FALSE);
if ($op == 'view') {
if ($_domain['site_grant']) {
$grants['domain_site'][] = 0;
if ($rules) {
$grants['domain_site']['group'] = 'domain';
}
}
$grants['domain_id'][] = $_domain['domain_id'];
if ($rules) {
$grants['domain_id']['group'] = 'domain';
}
if ($op == 'view' && domain_grant_all()) {
if ($rules) {
return array();
}
else {
return array(
'domain_all' => array(
0,
),
);
}
}
}
else {
$editors = variable_get('domain_editors', DOMAIN_EDITOR_RULE);
if ($editors && user_access('edit domain nodes', $account)) {
if (!empty($account->domain_user)) {
foreach ($account->domain_user as $id) {
if (abs($id) > 0) {
if ($id > 0) {
$grants['domain_editor'][] = $id;
}
else {
$grants['domain_editor'][] = 0;
}
if ($rules) {
$grants['domain_editor']['check'] = TRUE;
}
}
}
}
}
}
static $_modules;
if (!isset($_modules)) {
$_modules = module_implements('domaingrants');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domaingrants';
$function($grants, $account, $op);
}
}
return $grants;
}
function domain_node_access_records($node) {
if (domain_disabling()) {
return;
}
$grants = array();
if (is_array($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 ($node->domain_site) {
$grants[] = array(
'realm' => 'domain_site',
'gid' => 0,
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'priority' => 0,
);
}
$editors = variable_get('domain_editors', DOMAIN_EDITOR_RULE);
if (!empty($node->domains)) {
foreach ($node->domains as $key => $value) {
if (abs($value) > 0) {
$key == -1 ? $key = 0 : ($key = $key);
$grants[] = array(
'realm' => 'domain_id',
'gid' => $key,
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'priority' => 0,
);
if ($editors) {
$grants[] = array(
'realm' => 'domain_editor',
'gid' => $key,
'grant_view' => FALSE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
}
}
}
if (empty($grants)) {
$grants[] = array(
'realm' => 'domain_id',
'gid' => 0,
'grant_view' => TRUE,
'grant_update' => FALSE,
'grant_delete' => FALSE,
'priority' => 0,
);
if ($editors) {
$grants[] = array(
'realm' => 'domain_editor',
'gid' => 0,
'grant_view' => FALSE,
'grant_update' => TRUE,
'grant_delete' => TRUE,
'priority' => 0,
);
}
}
static $_modules;
if (!isset($_modules)) {
$_modules = module_implements('domainrecords');
}
if (!empty($_modules)) {
foreach ($_modules as $module) {
$function = $module . '_domainrecords';
$function($grants, $node);
}
}
return $grants;
}
function _domain_write_records($nid, $grants = array()) {
if ($nid > 0 && !empty($grants)) {
db_query("DELETE FROM {domain_access} WHERE nid = %d", $nid);
foreach ($grants as $grant) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid, $grant['gid'], $grant['realm']);
}
}
}
function domain_enable() {
domain_enabling(TRUE);
$count = db_result(db_query("SELECT COUNT(*) FROM {domain_access}"));
if ($count == 0) {
$rule = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
$edit = variable_get('domain_editors', DOMAIN_EDITOR_RULE);
$site = DOMAIN_SITE_GRANT;
$nids = db_query("SELECT nid FROM {node}");
while ($nid = db_fetch_object($nids)) {
if (!empty($site)) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid->nid, 0, 'domain_site');
}
if (!empty($rule)) {
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid->nid, 0, 'domain_id');
if (!empty($edit)) {
variable_set('domain_editors', TRUE);
db_query("INSERT INTO {domain_access} (nid, gid, realm) VALUES (%d, %d, '%s')", $nid->nid, 0, 'domain_editor');
}
}
}
}
node_access_rebuild();
domain_set_default_grant();
}
function domain_set_default_grant() {
$check = db_result(db_query("SELECT COUNT(nid) FROM {node_access} WHERE realm = 'domain_all' AND gid = 0"));
if (!$check) {
db_query("INSERT INTO {node_access} VALUES (0, 0, 'domain_all', 1, 0, 0)");
}
}
function domain_enabling($set = NULL) {
static $enabling = FALSE;
if ($set !== NULL) {
$enabling = $set;
}
return $enabling;
}
function domain_disable() {
domain_disabling(TRUE);
node_access_rebuild();
$result = db_result(db_query("SELECT COUNT(realm) FROM {node_access} WHERE realm <> 'all'"));
if ($result == 0) {
db_query("DELETE FROM {node_access}");
db_query("INSERT INTO {node_access} VALUES (0, 0, 'all', 1, 0, 0)");
}
}
function domain_disabling($set = NULL) {
static $disabling = FALSE;
if ($set !== NULL) {
$disabling = $set;
}
return $disabling;
}
function domain_form_alter($form_id, &$form) {
$forms = module_invoke_all('domainignore');
if (in_array($form_id, $forms)) {
return;
}
domain_warning_check($form_id);
$seo = variable_get('domain_seo', 0);
if ($seo && isset($form['#action'])) {
global $_domain;
$action = parse_url($form['#action']);
if ($action['query']) {
$action['path'] .= '?';
}
if (empty($action['host'])) {
$form['#action'] = check_url($_domain['scheme'] . '://' . $_domain['subdomain'] . $action['path'] . $action['query']);
}
}
if ($form_id == 'system_site_information_settings' && arg(2) != 'domain') {
$form['#submit']['domain_form_sitename_submit'] = array();
}
if ($form['#id'] == 'node-form' && !$form['#node']->cck_dummy_node_form) {
global $_domain, $user;
$default = array(
$_domain['domain_id'],
);
$behavior = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
if ($_domain['domain_id'] == 0) {
$default[] = -1;
}
if ($form['#node']->nid) {
$raw = $form['#node']->domains;
}
else {
$raw = $default;
}
$options = array();
foreach (domain_domains() as $data) {
$data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);
if ($data['valid'] || user_access('administer domains')) {
$options[$key] = check_plain($data['sitename']);
}
}
if (user_access('set domain access')) {
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Domain access options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['domain']['domain_site'] = array(
'#type' => 'checkbox',
'#prefix' => t('<p><b>Publishing options:</b>'),
'#suffix' => '</p>',
'#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.'),
'#default_value' => isset($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $behavior),
);
$form['domain']['domains'] = array(
'#type' => 'checkboxes',
'#title' => t('Publish to'),
'#options' => $options,
'#required' => TRUE,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->nid) ? $form['#node']->domains : $default,
);
}
else {
if (user_access('view domain publishing')) {
$action = variable_get('domain_options', 0);
$user_domains = array();
$default_options = array();
if (!isset($user->domain_user)) {
$user->domain_user = array();
}
foreach ($user->domain_user as $key => $value) {
if (abs($value) > 0) {
$user_domains[] = $value;
}
}
$first_domain = current($user_domains);
$user_options = array();
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 || $domain['valid'] == 0) {
domain_goto(domain_default());
}
else {
if ($domain['domain_id'] != $_domain['domain_id']) {
domain_goto($domain);
}
}
break;
case 3:
if (empty($user_options)) {
$form = array();
return drupal_access_denied();
}
$form['domain'] = array(
'#type' => 'fieldset',
'#title' => t('Affiliate publishing options'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
if ($form['#node']->nid) {
$raw = $raw_options;
}
else {
$raw = array();
}
empty($raw) ? $required = TRUE : ($required = FALSE);
$form['domain']['domains'] = array(
'#type' => 'checkboxes',
'#title' => t('Publish to'),
'#options' => $user_options,
'#required' => $required,
'#description' => t('Select which affiliates can access this content.'),
'#default_value' => isset($form['#node']->nid) ? $form['#node']->domains : $default_options,
);
$list = array();
if ($form['#node']->domain_site) {
$list[]['data'] = t('All affiliates');
}
if (!empty($raw)) {
foreach ($raw as $did) {
$did == -1 ? $id = 0 : ($id = $did);
$raw_domains = domain_lookup($id);
$list[]['data'] = check_plain($raw_domains['sitename']);
}
}
if (!empty($list)) {
$form['domain']['domains_notes'] = array(
'#value' => '<label><b>' . t('Publishing status:') . '</b></label>' . theme('item_list', $list) . '<div class="description">' . t('This content has also been published to these affiliates.') . '</div>',
);
}
break;
}
}
$form['domain_site'] = array(
'#type' => 'value',
'#value' => isset($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $behavior),
);
$form['domains_raw'] = array(
'#type' => 'value',
'#value' => $raw,
);
}
if (variable_get('domain_editors', DOMAIN_EDITOR_RULE) == 1 && user_access('edit domain nodes')) {
$access = variable_get('domain_form_elements', array(
'options',
'delete',
'comment_settings',
'path',
));
foreach ($access as $item) {
$form[$item]['#access'] = TRUE;
}
}
}
}
function domain_form_sitename_submit($form_id, $form_values) {
variable_set('domain_sitename', $form_values['site_name']);
drupal_set_message(t('Primary domain settings updated.'));
}
function domain_grant_all($reset = FALSE) {
static $grant;
if (!isset($grant) || $reset) {
$grant = FALSE;
if (variable_get('domain_search', 0) && arg(0) == 'search') {
$grant = TRUE;
}
if (!$grant && variable_get('domain_cron_rule', 1)) {
$ref = explode('/', request_uri());
$script = array_pop($ref);
if ($script == 'cron.php') {
$grant = TRUE;
}
}
if (!$grant) {
$pages = variable_get('domain_grant_all', "user/*/track");
$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) {
$path = drupal_get_path_alias($_GET['q']);
if ($path != $_GET['q']) {
$page_match = preg_match($regexp, $path);
}
}
if ($page_match) {
$grant = TRUE;
}
}
}
return $grant;
}
function domain_domaininstall() {
if (url('domain_access_test_path') != url('domain_access_path_test')) {
drupal_set_message(t('The <em>custom_url_rewrite_outbound()</em> patch is not installed. Some features are not available. See the Patches to Drupal Core section of <a href="!url">INSTALL.txt</a>', array(
'!url' => base_path() . drupal_get_path('module', 'domain') . '/INSTALL.txt',
)));
}
}
function domain_domainupdate($op, $domain = array(), $edit = array()) {
switch ($op) {
case 'delete':
if ($domain != -1) {
db_query("DELETE FROM {node_access} WHERE realm = 'domain_id' AND gid = %d", $domain['domain_id']);
db_query("DELETE FROM {node_access} WHERE realm = 'domain_editor' AND gid = %d", $domain['domain_id']);
db_query("DELETE FROM {domain_access} WHERE realm = 'domain_id' AND gid = %d", $domain['domain_id']);
db_query("DELETE FROM {domain_access} WHERE realm = 'domain_editor' AND gid = %d", $domain['domain_id']);
}
break;
}
menu_rebuild();
}
function domain_domainbatch() {
$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.'),
'#variable' => 'domain_root',
'#validate' => 'domain_batch_validate',
'#data_type' => 'string',
'#weight' => -10,
);
$batch['sitename'] = array(
'#form' => array(
'#title' => t('Site names'),
'#type' => 'textfield',
'#size' => 40,
'#maxlength' => 80,
'#description' => t('The site name to display for this domain.'),
'#required' => TRUE,
),
'#domain_action' => 'domain',
'#meta_description' => t('Edit all domain site names.'),
'#variable' => 'domain_sitename',
'#validate' => 'domain_batch_validate',
'#data_type' => 'string',
'#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://'),
'#variable' => 'domain_scheme',
'#data_type' => 'string',
'#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',
'#weight' => -10,
);
return $batch;
}
function domain_batch_validate($form_values) {
$case = $form_values['variable'];
$batch = $form_values['domain_batch'];
switch ($case) {
case 'domain_root':
foreach ($batch as $key => $value) {
$subdomain = strtolower(urlencode($value));
$check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s' AND domain_id <> %d", $value, $key));
if ($check > 0 || $key > 0 && $value == variable_get('domain_root', '')) {
form_set_error('domain_batch', t('Each domain value must be unique.'));
}
}
break;
case 'domain_sitename':
foreach ($batch as $key => $value) {
$check = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE sitename = '%s' AND domain_id <> %d", $value, $key));
if ($check > 0 || $key > 0 && $value == variable_get('domain_sitename', 'Drupal')) {
form_set_error('domain_batch', t('Each site name value must be unique.'));
}
}
break;
}
}
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_warning_check($form_id) {
static $_warning;
if (!$_POST && empty($_warning)) {
global $_domain;
$forms = array();
$forms = module_invoke_all('domainwarnings');
if (arg(2) != 'domain' && in_array($form_id, $forms)) {
$default = domain_default();
if ($_domain['domain_id'] != $default['domain_id']) {
$_path = domain_get_uri($default);
drupal_set_message(t('You are viewing #this. This form may need to be entered from <a href="!url">!domain</a>', array(
'#this' => $_domain['subdomain'],
'!url' => $_path,
'!domain' => $default['subdomain'],
)));
}
}
$_warning = TRUE;
}
}
function domain_db_rewrite_sql($query, $primary_table, $primary_field, $args) {
global $_domain;
$admin_force = variable_get('domain_force_admin', FALSE);
if (!$admin_force || $primary_field != 'nid' || !user_access('administer nodes') || domain_grant_all()) {
return;
}
$domain_id = (int) $_domain['domain_id'];
$return = array(
'join' => "INNER JOIN {domain_access} da_admin ON {$primary_table}.nid = da_admin.nid",
'where' => "(da_admin.gid = 0 AND da_admin.realm = 'domain_site') OR (da_admin.gid = {$domain_id} AND da_admin.realm = 'domain_id')",
);
return $return;
}
function domain_node_access_explain($row) {
global $_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 {
$return .= t('False: Only allows content from the active domain (%domain) or from all affiliates.', array(
'%domain' => $active,
));
}
break;
case 'domain_site':
$return .= t('Viewable on all affiliate sites.');
break;
case 'domain_id':
$return .= t('Viewable on %domain.', array(
'%domain' => $domain['subdomain'],
));
break;
case 'domain_editor':
$return .= t('Editable by %domain editors.', array(
'%domain' => $domain['subdomain'],
));
break;
default:
$return = NULL;
break;
}
return $return;
}
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();
if (substr_count($subdomain, '.') == 0) {
$error_list[] = t('At least one dot (.) is required.');
}
if (substr_count($subdomain, ':') > 1) {
$error_list[] = t('Only one colon (:) is allowed.');
}
else {
if (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 (.)');
}
$pattern = '/^[a-z0-9\\.\\-:]*$/i';
if (!preg_match($pattern, $subdomain)) {
$error_list[] = t('Only alphanumeric characters, dashes, and a colon are allowed.');
}
if (!empty($error_list)) {
return t('The domain string is invalid:') . theme('item_list', $error_list);
}
}
function domain_unique_domain($subdomain) {
$count = db_result(db_query("SELECT COUNT(domain_id) FROM {domain} WHERE subdomain = '%s'", $subdomain));
return !(bool) $count;
}