| 1 |
<?php
|
| 2 |
|
| 3 |
define(REFERER_THEME, 'referer_theme');
|
| 4 |
define(REFERER_THEME_TIMESTAMP, 'referer_theme_timestamp');
|
| 5 |
define(REFERER_THEME_THEME, 'referer_theme_theme');
|
| 6 |
define(REFERER_THEME_INTERVAL, 'referer_theme_interval');
|
| 7 |
define(REFERER_THEME_MAX_SITES, 3);
|
| 8 |
// Number of seconds to keep the site in the assigned theme
|
| 9 |
// for the current visitor. Default is 3 hours
|
| 10 |
define(REFERER_THEME_PERIOD, 60*60*3);
|
| 11 |
|
| 12 |
function referer_theme_help($section) {
|
| 13 |
switch ($section) {
|
| 14 |
case 'admin/modules#description':
|
| 15 |
case 'admin/settings/referer_theme':
|
| 16 |
case 'admin/help#referer_theme':
|
| 17 |
return t('This module changes the site to a different theme for each visitor based on referer.');
|
| 18 |
}
|
| 19 |
}
|
| 20 |
|
| 21 |
function referer_theme_admin_settings() {
|
| 22 |
$theme_list = array();
|
| 23 |
foreach(list_themes() as $key => $theme) {
|
| 24 |
if ($theme->status == 1) {
|
| 25 |
$theme_list[$key] = $theme->name;
|
| 26 |
}
|
| 27 |
}
|
| 28 |
|
| 29 |
for($i=1; $i<REFERER_THEME_MAX_SITES+1; $i++) {
|
| 30 |
$set = 'host' . $i;
|
| 31 |
$form[$set] = array(
|
| 32 |
'#type' => 'fieldset',
|
| 33 |
'#title' => t('Host ' . $i),
|
| 34 |
'#description' => t(''),
|
| 35 |
);
|
| 36 |
|
| 37 |
$param = REFERER_THEME . '_site_' . $i;
|
| 38 |
$form[$set][$param] = array(
|
| 39 |
'#type' => 'textfield',
|
| 40 |
'#title' => t('Site host name'),
|
| 41 |
'#default_value' => variable_get($param, ''),
|
| 42 |
'#size' => 25,
|
| 43 |
'#maxlength' => 25,
|
| 44 |
'#description' => t('The host name of the site that is allowed to referer_theme. This is the part after http:// and before the first /.'),
|
| 45 |
);
|
| 46 |
|
| 47 |
$param = REFERER_THEME . '_theme_' . $i;
|
| 48 |
$form[$set][$param] = array(
|
| 49 |
'#type' => 'select',
|
| 50 |
'#title' => t('Site theme'),
|
| 51 |
'#default_value' => variable_get($param, ''),
|
| 52 |
'#options' => $theme_list,
|
| 53 |
);
|
| 54 |
}
|
| 55 |
|
| 56 |
return system_settings_form($form);
|
| 57 |
}
|
| 58 |
|
| 59 |
|
| 60 |
function referer_theme_menu($may_cache) {
|
| 61 |
$items = array();
|
| 62 |
if ($may_cache) {
|
| 63 |
$items[] = array(
|
| 64 |
'path' => 'admin/settings/referer_theme',
|
| 65 |
'title' => t('Referer theme'),
|
| 66 |
'description' => t('Referer theme settings'),
|
| 67 |
'callback' => 'drupal_get_form',
|
| 68 |
'callback arguments' => array('referer_theme_admin_settings'),
|
| 69 |
'access' => user_access('administer site configuration'),
|
| 70 |
'type' => MENU_NORMAL_ITEM, // optional
|
| 71 |
);
|
| 72 |
|
| 73 |
|
| 74 |
// Check if we should force a reset
|
| 75 |
if ($_GET[REFERER_THEME] == 'false') {
|
| 76 |
_referer_theme_session_reset();
|
| 77 |
return $items;
|
| 78 |
}
|
| 79 |
|
| 80 |
// Check if the time interval expired
|
| 81 |
_referer_theme_session_check_reset();
|
| 82 |
|
| 83 |
// Check if we already have an referer_theme theme
|
| 84 |
if (_referer_theme_session_get()) {
|
| 85 |
return $items;
|
| 86 |
}
|
| 87 |
|
| 88 |
// Check the referer and set the referer_theme if applicable
|
| 89 |
_referer_theme_site_match();
|
| 90 |
return $items;
|
| 91 |
}
|
| 92 |
|
| 93 |
return $items;
|
| 94 |
}
|
| 95 |
|
| 96 |
function _referer_theme_site_match() {
|
| 97 |
$page_match = false;
|
| 98 |
|
| 99 |
for($i=1; $i<REFERER_THEME_MAX_SITES+1; $i++) {
|
| 100 |
$site = variable_get(REFERER_THEME . '_site_' . $i, '');
|
| 101 |
if ($site) {
|
| 102 |
if (_referer_theme_check_referer($site, $i)) {
|
| 103 |
$page_match = true;
|
| 104 |
break;
|
| 105 |
}
|
| 106 |
}
|
| 107 |
}
|
| 108 |
|
| 109 |
return $page_match;
|
| 110 |
}
|
| 111 |
|
| 112 |
function _referer_theme_check_referer($site, $index) {
|
| 113 |
$rc = false;
|
| 114 |
$protocol = ($_SERVER['HTTPS'] == 'on') ? 'https' : 'http';
|
| 115 |
$pattern = preg_quote("$protocol://$site", '/');
|
| 116 |
$pattern = '/^' . $pattern . '/';
|
| 117 |
$referer = $_SERVER['HTTP_REFERER'];
|
| 118 |
if (preg_match($pattern, $referer)) {
|
| 119 |
_referer_theme_session_set($index);
|
| 120 |
$rc = true;
|
| 121 |
}
|
| 122 |
return $rc;
|
| 123 |
}
|
| 124 |
|
| 125 |
function _referer_theme_session_get() {
|
| 126 |
global $custom_theme;
|
| 127 |
|
| 128 |
if ($_SESSION[REFERER_THEME_TIMESTAMP]) {
|
| 129 |
$custom_theme = $_SESSION[REFERER_THEME_THEME];
|
| 130 |
return true;
|
| 131 |
}
|
| 132 |
|
| 133 |
return false;
|
| 134 |
}
|
| 135 |
|
| 136 |
function _referer_theme_session_set($site_index) {
|
| 137 |
global $custom_theme;
|
| 138 |
|
| 139 |
$theme = variable_get(REFERER_THEME . '_theme_' . $site_index, '');
|
| 140 |
$_SESSION[REFERER_THEME_TIMESTAMP] = time();
|
| 141 |
$_SESSION[REFERER_THEME_THEME] = $theme;
|
| 142 |
|
| 143 |
$custom_theme = $theme;
|
| 144 |
}
|
| 145 |
|
| 146 |
function _referer_theme_session_reset() {
|
| 147 |
unset($_SESSION[REFERER_THEME_TIMESTAMP]);
|
| 148 |
unset($_SESSION[REFERER_THEME_THEME]);
|
| 149 |
}
|
| 150 |
|
| 151 |
function _referer_theme_session_check_reset() {
|
| 152 |
$timestamp = $_SESSION[REFERER_THEME_TIMESTAMP] + variable_get(REFERER_THEME_INTERVAL, REFERER_THEME_PERIOD);
|
| 153 |
$now = time();
|
| 154 |
if ($timestamp > 0 && $timestamp <= $now) {
|
| 155 |
_referer_theme_session_reset();
|
| 156 |
return true;
|
| 157 |
}
|
| 158 |
|
| 159 |
return false;
|
| 160 |
}
|
| 161 |
|