array( 'name' => t('PHP code'), 'description' => t('Validates if the following PHP code returns TRUE (PHP-mode, experts only).'), ), 'condition_requirements_pages' => array( 'name' => t('Pages'), 'description' => t('Validates (not) on a selection of pages.'), ), 'condition_requirements_sites' => array( 'name' => t('Sites'), 'description' => t('Validates (not) on a selection of site configurations.'), ), ); } /*********************************************************************************** * Implementation of a Condition Requirement. * PHP Code. */ function condition_requirements_php_form($context) { $form['php_code'] = array( '#type' => 'textarea', '#title' => t('PHP Code'), '#description' => t('Enter PHP code between <?php ?>. Note that executing incorrect PHP-code can break your Drupal site.'), '#default_value' => $context['code'], ); return $form; } /** * Submit: Implementation of a Condition Requirement. * PHP Code. */ function condition_requirements_php_form_submit($form, &$form_state) { if ($form_state['values']['php_code']) { return array('code' => $form_state['values']['php_code']); } } /** * Execute: Implementation of a Condition Requirement. * PHP Code. */ function condition_requirements_php(&$condition, $context = array()) { return (!$context['code'] || drupal_eval($context['code'])); } /*********************************************************************************** * Implementation of a Condition Requirement. * Pages. */ function condition_requirements_pages_form($context) { $form['pages_operator'] = array( '#type' => 'radios', '#title' => t('Validate'), '#options' => array( '' => t("Always (skip requirement)"), CONDITION_NONE => t('Except on the listed pages.'), CONDITION_ANY => t('Only on the listed pages.'), ), '#default_value' => $context['operator'], ); $form['pages_pages'] = array( '#type' => 'textarea', '#title' => t('Pages'), '#description' => t('Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are blog for the blog page and blog/* for every personal blog. <front> is the front page.'), '#default_value' => $context['pages'], ); return $form; } /** * Submit: Implementation of a Condition Requirement. * Pages. */ function condition_requirements_pages_form_submit($form, &$form_state) { if ($form_state['values']['pages_operator'] || $form_state['values']['pages_pages']) { return array( 'operator' => $form_state['values']['pages_operator'], 'pages' => $form_state['values']['pages_pages'], ); } } /** * Execute: Implementation of a Condition Requirement. * Pages. */ function condition_requirements_pages(&$condition, $context = array()) { if (!$context['operator'] || ($context['operator'] == CONDITION_NONE && !$context['pages'])) { return TRUE; } if (!$context['pages'] && $context['operator'] == CONDITION_ANY) { return FALSE; } $alias = drupal_get_path_alias($_GET['q']); return ($context['operator'] == CONDITION_NONE xor (drupal_match_path($alias, $context['pages']) || ($_GET['q'] != $alias && drupal_match_path($_GET['q'], $context['pages'])))); } /*********************************************************************************** * Implementation of a Condition Requirement. * Sites. */ function condition_requirements_sites_form($context) { $options = array(); $handle = opendir('./sites'); while ($dir = readdir($handle)) { if (is_dir('./sites/'.$dir) && file_exists('./sites/'.$dir.'/settings.php')) { $options[$dir] = (substr(conf_path(), 6) == $dir) ? ''.$dir.'' : $dir; } } $form['sites_operator'] = array( '#type' => 'radios', '#title' => t('Validate'), '#options' => array( '' => t("Always (skip requirement)"), CONDITION_NONE => t('Except on the listed sites.'), CONDITION_ANY => t('Only on the listed sites.'), ), '#default_value' => $context['operator'], ); $form['sites_sites'] = array( '#type' => 'checkboxes', '#title' => t('Sites'), '#options' => $options, '#description' => t('Listed are all Drupal !multisite directories. The currently active is bold.', array('!multisite' => l(t('multi-site'), 'http://drupal.org/getting-started/5/install/multi-site', array('attributes' => array('target' => '_blank'))))), '#default_value' => is_array($context['sites']) ? $context['sites'] : array(), ); return $form; } /** * Submit: Implementation of a Condition Requirement. * Sites. */ function condition_requirements_sites_form_submit($form, &$form_state) { if ($form_state['values']['sites_operator'] || $form_state['values']['sites_sites']) { return array( 'operator' => $form_state['values']['sites_operator'], 'sites' => $form_state['values']['sites_sites'], ); } } /** * Execute: Implementation of a Condition Requirement. * Sites. */ function condition_requirements_sites(&$condition, $context = array()) { if (!$context['operator'] || ($context['operator'] == CONDITION_NONE && !$context['sites'])) { return TRUE; } if (!$context['sites'] && $context['operator'] == CONDITION_ANY) { return FALSE; } $site = substr(conf_path(), 6); $regexp = '/^('.preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/'), array('|', '.*'), preg_quote($context['sites'], '/')).')$/'; return ($context['operator'] == CONDITION_NONE xor preg_match($regexp, $site)); } ?>