| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: settings_custom_url.inc,v 1.24 2009/10/24 16:18:52 agentken Exp $
|
| 4 |
|
| 5 |
/**
|
| 6 |
* @file
|
| 7 |
* Include file for settings.php to provide advanced
|
| 8 |
* functionality for rewriting links as needed.
|
| 9 |
*
|
| 10 |
* @ingroup domain
|
| 11 |
*/
|
| 12 |
|
| 13 |
/**
|
| 14 |
* Implement custom_url_rewrite_outbound() if the url_alter.module is not enabled.
|
| 15 |
*/
|
| 16 |
if (!function_exists('custom_url_rewrite_outbound')) {
|
| 17 |
function custom_url_rewrite_outbound(&$path, &$options, $original_path) {
|
| 18 |
domain_url_outbound_alter($path, $options, $original_path);
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Legacy wrapper for users of 6.x.2, rc8.
|
| 24 |
*/
|
| 25 |
function domain_url_rewrite_outbound(&$path, &$options, $original_path) {
|
| 26 |
domain_url_outbound_alter($path, $options, $original_path);
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
* Implement hook_url_outbound_alter().
|
| 31 |
*
|
| 32 |
* Forces absolute paths for domains when needed.
|
| 33 |
*/
|
| 34 |
function domain_url_outbound_alter(&$path, &$options, $original_path) {
|
| 35 |
global $_domain;
|
| 36 |
|
| 37 |
// If the domain_id is not set, then the Domain module is not active, and we cannot run this function.
|
| 38 |
if (!isset($_domain['domain_id'])) {
|
| 39 |
return;
|
| 40 |
}
|
| 41 |
|
| 42 |
// Set static variables for the node lookups, to remove redundant queries.
|
| 43 |
static $domain_site, $domain, $nodepaths, $path_rewrite, $use_source, $root, $check_nodes, $path_lookup, $path_alter;
|
| 44 |
|
| 45 |
// This routine only needs to be run from certain urls or if we want to
|
| 46 |
// force all links to go to a single domain for SEO.
|
| 47 |
// See http://drupal.org/node/195366 for the background.
|
| 48 |
|
| 49 |
// These two functions are statically cached elsewhere.
|
| 50 |
// Are we viewing nodes from all sites?
|
| 51 |
$check = domain_grant_all();
|
| 52 |
// Are we forcing path rewrites?
|
| 53 |
$seo = variable_get('domain_seo', 0);
|
| 54 |
// Check for source rewrites.
|
| 55 |
if (!isset($use_source)) {
|
| 56 |
$use_source = (bool) count(module_implements('domain_source_alter'));
|
| 57 |
}
|
| 58 |
// Querying for nodepaths is expensive, so only do it if needed.
|
| 59 |
if (!isset($check_nodes)) {
|
| 60 |
$check_nodes = $check || $seo || $use_source;
|
| 61 |
}
|
| 62 |
// Check for path rewrites.
|
| 63 |
if (!isset($path_alter)) {
|
| 64 |
$path_alter = (bool) count(module_implements('domain_source_path_alter'));
|
| 65 |
}
|
| 66 |
// Get the root url.
|
| 67 |
if (!isset($root)) {
|
| 68 |
$root = domain_lookup(variable_get('domain_default_source', 0));
|
| 69 |
}
|
| 70 |
|
| 71 |
// Now run the path rewrite sequence, if necessary.
|
| 72 |
if ($check_nodes || $path_alter) {
|
| 73 |
$nid = FALSE;
|
| 74 |
$target_domain_id = $_domain['domain_id'];
|
| 75 |
if ($check_nodes) {
|
| 76 |
// Check to see if this is a node or comment link and set $nid accordingly.
|
| 77 |
// We static the $nid results to make this more efficient.
|
| 78 |
$pattern = explode('/', $original_path);
|
| 79 |
|
| 80 |
// Advanced pattern matching, we find the node id based on token %n in the path string.
|
| 81 |
if (!isset($nodepaths)) {
|
| 82 |
$pathdata = variable_get('domain_paths', "node/%n\r\nnode/%n/edit\r\ncomment/reply/%n\r\nnode/add/book/parent/%n\r\nbook/export/html/%n\r\nnode/%n/outline");
|
| 83 |
$path_match = preg_replace('/(\r\n?|\n)/', '|', $pathdata);
|
| 84 |
$nodepaths = explode("|", $path_match);
|
| 85 |
}
|
| 86 |
foreach ($nodepaths as $match) {
|
| 87 |
$match_array = explode('/', $match);
|
| 88 |
$placeholder = array_search('%n', $match_array);
|
| 89 |
if (isset($pattern[$placeholder])) {
|
| 90 |
$match_array[$placeholder] = $pattern[$placeholder];
|
| 91 |
if (is_numeric($pattern[$placeholder]) && $match_array == $pattern) {
|
| 92 |
$nid = (int) $pattern[$placeholder];
|
| 93 |
break;
|
| 94 |
}
|
| 95 |
}
|
| 96 |
}
|
| 97 |
}
|
| 98 |
// This path has matched a node id, so it may need to be rewritten.
|
| 99 |
if (!empty($nid)) {
|
| 100 |
// Remove redundancy from the domain_site check.
|
| 101 |
if (!isset($domain_site[$nid])) {
|
| 102 |
// If this check works, we don't need to rewrite the path unless SEO rules demand it.
|
| 103 |
$domain_site[$nid] = db_result(db_query("SELECT COUNT(nid) FROM {domain_access} WHERE nid = %d AND gid = 0 AND realm = '%s'", $nid, 'domain_site'));
|
| 104 |
}
|
| 105 |
if (!$domain_site[$nid] || $use_source) {
|
| 106 |
// Remove rendundancy from the domain_id check.
|
| 107 |
if (!isset($domain[$nid])) {
|
| 108 |
$domain[$nid] = domain_get_node_match($nid);
|
| 109 |
}
|
| 110 |
// Can we and do we need to rewrite this path?
|
| 111 |
if ($domain[$nid] != -1 && $domain[$nid]['domain_id'] != $_domain['domain_id']) {
|
| 112 |
$options['absolute'] = TRUE;
|
| 113 |
// In this case, the $base_url cannot have a trailing slash
|
| 114 |
$options['base_url'] = rtrim($domain[$nid]['path'], '/');
|
| 115 |
// Domain Source trumps the seo rules below.
|
| 116 |
if ($use_source) {
|
| 117 |
$seo = FALSE;
|
| 118 |
}
|
| 119 |
$target_domain_id = $domain[$nid]['domain_id'];
|
| 120 |
}
|
| 121 |
}
|
| 122 |
// If strict SEO rules are enabled, we set "all affiliate" links to the root domain.
|
| 123 |
// Only needed if we are not on the default source domain.
|
| 124 |
else if ($root != -1 && $seo && $_domain['domain_id'] != $root['domain_id']) {
|
| 125 |
$options['absolute'] = TRUE;
|
| 126 |
// In this case, the $base_url cannot have a trailing slash
|
| 127 |
$options['base_url'] = rtrim($root['path'], '/');
|
| 128 |
$target_domain_id = $root['domain_id'];
|
| 129 |
}
|
| 130 |
}
|
| 131 |
// Hook for non-node paths.
|
| 132 |
else if (!empty($path_alter)) {
|
| 133 |
// Must use md5 here, to prevent array keys from breaking.
|
| 134 |
$name = md5($path);
|
| 135 |
if (!isset($path_lookup[$name])) {
|
| 136 |
$path_lookup[$name] = domain_get_path_match($path);
|
| 137 |
}
|
| 138 |
if ($path_lookup[$name] != $_domain) {
|
| 139 |
$options['absolute'] = TRUE;
|
| 140 |
// In this case, the $base_url cannot have a trailing slash
|
| 141 |
$options['base_url'] = rtrim($path_lookup[$name]['path'], '/');
|
| 142 |
$target_domain_id = $path_lookup[$name]['domain_id'];
|
| 143 |
// TODO: merge this code with the above for cleanup.
|
| 144 |
}
|
| 145 |
}
|
| 146 |
// We may have to implement hook_domainpath().
|
| 147 |
if (!isset($path_rewrite)) {
|
| 148 |
$path_rewrite = count(_domain_path_modules());
|
| 149 |
}
|
| 150 |
// Allow path changes, if needed.
|
| 151 |
if ($path_rewrite > 0 && $path != '') {
|
| 152 |
$path = domain_path($target_domain_id, $original_path, isset($options['language']) ? $options['language']->language : '');
|
| 153 |
}
|
| 154 |
}
|
| 155 |
}
|