| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Conditions are sets of requirements that make the condition met or not.
|
| 7 |
* Other modules can use this to provide conditional actions.
|
| 8 |
*/
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of a Condition Requirement.
|
| 12 |
*/
|
| 13 |
function condition_requirements_requirement_info() {
|
| 14 |
return array(
|
| 15 |
'condition_requirements_php' => array(
|
| 16 |
'name' => t('PHP code'),
|
| 17 |
'description' => t('Validates if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).'),
|
| 18 |
),
|
| 19 |
'condition_requirements_pages' => array(
|
| 20 |
'name' => t('Pages'),
|
| 21 |
'description' => t('Validates (not) on a selection of pages.'),
|
| 22 |
),
|
| 23 |
'condition_requirements_sites' => array(
|
| 24 |
'name' => t('Sites'),
|
| 25 |
'description' => t('Validates (not) on a selection of site configurations.'),
|
| 26 |
),
|
| 27 |
'condition_requirements_hostname' => array(
|
| 28 |
'name' => t('Hostname'),
|
| 29 |
'description' => t('Validates (not) on a selection of site hostnames.'),
|
| 30 |
),
|
| 31 |
);
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Implementation of a Condition Requirement.
|
| 36 |
* PHP Code.
|
| 37 |
*/
|
| 38 |
function condition_requirements_php_form($context) {
|
| 39 |
$form['php_code'] = array(
|
| 40 |
'#type' => 'textarea',
|
| 41 |
'#title' => t('PHP Code'),
|
| 42 |
'#description' => t('Enter PHP code between <em><?php ?></em>. Note that executing incorrect PHP-code can break your Drupal site.'),
|
| 43 |
'#default_value' => $context['code'],
|
| 44 |
);
|
| 45 |
|
| 46 |
return $form;
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Submit: Implementation of a Condition Requirement.
|
| 51 |
* PHP Code.
|
| 52 |
*/
|
| 53 |
function condition_requirements_php_form_submit($form, &$form_state) {
|
| 54 |
if ($form_state['values']['php_code']) {
|
| 55 |
return array('code' => $form_state['values']['php_code']);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
/**
|
| 60 |
* Execute: Implementation of a Condition Requirement.
|
| 61 |
* PHP Code.
|
| 62 |
*/
|
| 63 |
function condition_requirements_php(&$condition, $context = array()) {
|
| 64 |
return (!$context['code'] || drupal_eval($context['code']));
|
| 65 |
}
|
| 66 |
|
| 67 |
/**
|
| 68 |
* Implementation of a Condition Requirement.
|
| 69 |
* Pages.
|
| 70 |
*/
|
| 71 |
function condition_requirements_pages_form($context) {
|
| 72 |
$form['pages_operator'] = array(
|
| 73 |
'#type' => 'radios',
|
| 74 |
'#title' => t('Validate'),
|
| 75 |
'#options' => array(
|
| 76 |
'' => t("Always (skip requirement)"),
|
| 77 |
CONDITION_NONE => t('Except on the listed pages.'),
|
| 78 |
CONDITION_ANY => t('Only on the listed pages.'),
|
| 79 |
),
|
| 80 |
'#default_value' => $context['operator'],
|
| 81 |
);
|
| 82 |
$form['pages_pages'] = array(
|
| 83 |
'#type' => 'textarea',
|
| 84 |
'#title' => t('Pages'),
|
| 85 |
'#description' => t('Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are <em>blog</em> for the blog page and <em>blog/*</em> for every personal blog. <em><front></em> is the front page.'),
|
| 86 |
'#default_value' => $context['pages'],
|
| 87 |
);
|
| 88 |
|
| 89 |
return $form;
|
| 90 |
}
|
| 91 |
|
| 92 |
/**
|
| 93 |
* Submit: Implementation of a Condition Requirement.
|
| 94 |
* Pages.
|
| 95 |
*/
|
| 96 |
function condition_requirements_pages_form_submit($form, &$form_state) {
|
| 97 |
if ($form_state['values']['pages_operator'] && $form_state['values']['pages_pages']) {
|
| 98 |
return array(
|
| 99 |
'operator' => $form_state['values']['pages_operator'],
|
| 100 |
'pages' => $form_state['values']['pages_pages'],
|
| 101 |
);
|
| 102 |
}
|
| 103 |
}
|
| 104 |
|
| 105 |
/**
|
| 106 |
* Execute: Implementation of a Condition Requirement.
|
| 107 |
* Pages.
|
| 108 |
*/
|
| 109 |
function condition_requirements_pages(&$condition, $context = array()) {
|
| 110 |
$alias = drupal_get_path_alias($_GET['q']);
|
| 111 |
|
| 112 |
return ($context['operator'] == CONDITION_NONE xor (drupal_match_path($alias, $context['pages']) || ($_GET['q'] != $alias && drupal_match_path($_GET['q'], $context['pages']))));
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Implementation of a Condition Requirement.
|
| 117 |
* Sites.
|
| 118 |
*/
|
| 119 |
function condition_requirements_sites_form($context) {
|
| 120 |
$options = array();
|
| 121 |
$handle = opendir('./sites');
|
| 122 |
|
| 123 |
while ($dir = readdir($handle)) {
|
| 124 |
if (is_dir('./sites/'. $dir) && file_exists('./sites/'. $dir .'/settings.php')) {
|
| 125 |
$options[$dir] = (drupal_substr(conf_path(), 6) == $dir) ? '<strong>'. $dir .'</strong>' : $dir;
|
| 126 |
}
|
| 127 |
}
|
| 128 |
|
| 129 |
$form['sites_operator'] = array(
|
| 130 |
'#type' => 'radios',
|
| 131 |
'#title' => t('Validate'),
|
| 132 |
'#options' => array(
|
| 133 |
'' => t("Always (skip requirement)"),
|
| 134 |
CONDITION_NONE => t('Except on the listed sites.'),
|
| 135 |
CONDITION_ANY => t('Only on the listed sites.'),
|
| 136 |
),
|
| 137 |
'#default_value' => $context['operator'],
|
| 138 |
);
|
| 139 |
$form['sites_sites'] = array(
|
| 140 |
'#type' => 'checkboxes',
|
| 141 |
'#title' => t('Sites'),
|
| 142 |
'#options' => $options,
|
| 143 |
'#description' => t('Listed are all Drupal !multisite directories. The currently active is <strong>bold</strong>.', array('!multisite' => l(t('multi-site'), 'http://drupal.org/getting-started/5/install/multi-site', array('attributes' => array('target' => '_blank'))))),
|
| 144 |
'#default_value' => is_array($context['sites']) ? $context['sites'] : array(),
|
| 145 |
);
|
| 146 |
|
| 147 |
return $form;
|
| 148 |
}
|
| 149 |
|
| 150 |
/**
|
| 151 |
* Submit: Implementation of a Condition Requirement.
|
| 152 |
* Sites.
|
| 153 |
*/
|
| 154 |
function condition_requirements_sites_form_submit($form, &$form_state) {
|
| 155 |
if ($form_state['values']['sites_operator'] && $form_state['values']['sites_sites']) {
|
| 156 |
return array(
|
| 157 |
'operator' => $form_state['values']['sites_operator'],
|
| 158 |
'sites' => array_unique(array_values($form_state['values']['sites_sites'])),
|
| 159 |
);
|
| 160 |
}
|
| 161 |
}
|
| 162 |
|
| 163 |
/**
|
| 164 |
* Execute: Implementation of a Condition Requirement.
|
| 165 |
* Sites.
|
| 166 |
*/
|
| 167 |
function condition_requirements_sites(&$condition, $context = array()) {
|
| 168 |
return ($context['operator'] == CONDITION_NONE xor in_array(drupal_substr(conf_path(), 6), $context['sites']));
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Implementation of a Condition Requirement.
|
| 173 |
* Hostname.
|
| 174 |
*/
|
| 175 |
function condition_requirements_hostname_form($context) {
|
| 176 |
$form['hostname_operator'] = array(
|
| 177 |
'#type' => 'radios',
|
| 178 |
'#title' => t('Validate'),
|
| 179 |
'#options' => array(
|
| 180 |
'' => t("Always (skip requirement)"),
|
| 181 |
CONDITION_NONE => t('Except on the listed hostnames.'),
|
| 182 |
CONDITION_ANY => t('Only on the listed hostnames.'),
|
| 183 |
),
|
| 184 |
'#default_value' => $context['operator'],
|
| 185 |
);
|
| 186 |
$form['hostname_hostnames'] = array(
|
| 187 |
'#type' => 'textarea',
|
| 188 |
'#title' => t('Hostnames'),
|
| 189 |
'#default_value' => $context['hostnames'],
|
| 190 |
'#description' => t('Enter one or more (partial) hostnames (one on each line), e.g. <code>example.com</code>'),
|
| 191 |
);
|
| 192 |
|
| 193 |
return $form;
|
| 194 |
}
|
| 195 |
|
| 196 |
/**
|
| 197 |
* Submit: Implementation of a Condition Requirement.
|
| 198 |
* Hostname.
|
| 199 |
*/
|
| 200 |
function condition_requirements_hostname_form_submit($form, &$form_state) {
|
| 201 |
if ($form_state['values']['hostname_operator'] && $form_state['values']['hostname_hostnames']) {
|
| 202 |
return array(
|
| 203 |
'operator' => $form_state['values']['hostname_operator'],
|
| 204 |
'hostnames' => $form_state['values']['hostname_hostnames'],
|
| 205 |
);
|
| 206 |
}
|
| 207 |
}
|
| 208 |
|
| 209 |
/**
|
| 210 |
* Execute: Implementation of a Condition Requirement.
|
| 211 |
* Hostname.
|
| 212 |
*/
|
| 213 |
function condition_requirements_hostname(&$condition, $context = array()) {
|
| 214 |
$hostnames = preg_split('/[\r\n]+/', $context['hostnames']);
|
| 215 |
$found = FALSE;
|
| 216 |
|
| 217 |
foreach ($hostnames as $hostname) {
|
| 218 |
if (stristr($_SERVER['HTTP_HOST'], $hostname)) {
|
| 219 |
$found = TRUE;
|
| 220 |
}
|
| 221 |
}
|
| 222 |
|
| 223 |
return ($context['operator'] == CONDITION_NONE xor $found);
|
| 224 |
}
|