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 ($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('configure'),
'description' => t('Settings for the Domain Access module.')
);
$items[] = array(
'title' => t('Settings'),
'path' => 'admin/build/domain/settings',
'access' => $admin,
'type' => MENU_DEFAULT_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array('configure'),
'weight' => -10
);
$items[] = array(
'title' => t('Domain list'),
'path' => 'admin/build/domain/view',
'access' => $admin,
'type' => MENU_LOCAL_TASK,
'callback' => 'domain_admin',
'callback arguments' => array('view'),
'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' => $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)) {
$output .= theme('item_list', $this_node->subdomains, t('Subdomains'));
}
if (!empty($this_node->editors)) {
$output .= theme('item_list', $this_node->editors, t('Editors'));
}
if (isset($this_node->domain_source)) {
$this_domain = domain_lookup($this_node->domain_source);
$output .= theme('item_list', array($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('sitename', 'Drupal'));
while ($data = db_fetch_array($result)) {
$options[$data['domain_id']] = $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 '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>'. $domain['sitename'] .'</li>';
}
else {
$output .= '<li>'. variable_get('domain_sitename', variable_get('sitename', 'Drupal')) .'</li>';
}
}
}
$output .= '</ul>';
$items['domain'] = array('title' => t('Domain settings'),
'value' => $output,
);
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('sitename', '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;
$_url = parse_url($base_url);
if (!isset($_url['path'])) {
$_url['path'] = '/';
}
if (substr($_url['path'], -1) != '/') {
$_url['path'] .= '/';
}
$path = $domain['scheme'] .'://'. $domain['subdomain'] . $_url['path'];
return $path;
}
function domain_get_uri($domain) {
$path = $domain['scheme'] .'://'. $domain['subdomain'] . request_uri();
return $path;
}
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('sitename', '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('sitename', '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>'. $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>'. $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;
}
$behavior = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
if (domain_enabling() && $behavior == 1) {
$node->domain_site = TRUE;
}
$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) || domain_enabling()) {
$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);
node_access_rebuild();
domain_set_default_grant();
$realms = array('domain_site', 'domain_id', 'domain_editor');
foreach ($realms as $realm) {
$result = db_query("SELECT * FROM {node_access} WHERE realm = '%s'", $realm);
while ($data = db_fetch_array($result)) {
db_query("INSERT INTO {domain_access} VALUES (%d, %d, '%s')", $data['nid'], $data['gid'], $data['realm']);
}
}
}
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) {
&