Skip to content

Instantly share code, notes, and snippets.

@neclimdul
Created June 22, 2011 21:54
Show Gist options
  • Save neclimdul/1041323 to your computer and use it in GitHub Desktop.
Save neclimdul/1041323 to your computer and use it in GitHub Desktop.
GET access argument
<?php
/**
* @file
* Plugin to provide access control/visibility based on specified get argument matching user-specified string
*/
$plugin = array(
'title' => t("GET: comparison"),
'description' => t('Control access by comparing a GET argument.'),
'callback' => 'ctools_get_ctools_access_check',
'settings form' => 'ctools_get_ctools_access_settings',
'summary' => 'ctools_get_ctools_access_summary',
//'required context' => new ctools_context_required(t('String'), 'string'),
'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
);
/**
* Settings form
*/
function ctools_get_ctools_access_settings(&$form, &$form_state, $conf) {
$form['settings']['operator'] = array(
'#type' => 'select',
'#title' => t('Operator'),
'#options' => array(
'isset' => t('Is set'),
'=' => t('Equal'),
'!=' => t('Not equal'),
'regex' => t('Regular expression'),
'!regex' => t('Not equal to regular expression'),
),
'#default_value' => $conf['operator'],
'#id' => 'ctools-select-operator',
'#description' => t('If using a regular expression, you should enclose the pattern in slashes like so: <em>/foo/</em>. If you need to compare against slashes you can use another character to enclose the pattern, such as @. See <a href="http://www.php.net/manual/en/reference.pcre.pattern.syntax.php">PHP regex documentation</a> for more.'),
);
$form['settings']['argument'] = array(
'#type' => 'textfield',
'#title' => t('Argument'),
'#default_value' => $conf['argument'],
);
ctools_include('dependent');
$form['settings']['value'] = array(
'#type' => 'textfield',
'#title' => t('Value'),
'#default_value' => $conf['value'],
'#process' => array('ctools_dependent_process'),
'#dependency' => array('ctools-select-operator' => array('=', '!=', 'regex', '!regex')),
);
$form['settings']['case'] = array(
'#type' => 'checkbox',
'#title' => t('Case sensitive'),
'#default_value' => $conf['case'],
);
}
/**
* Check for access
*/
function ctools_get_ctools_access_check($conf, $context) {
$argument = isset($_GET[$conf['argument']]) ? $_GET[$conf['argument']] : '';
$value = $conf['value'];
if (empty($conf['case'])) {
$argument = drupal_strtolower($argument);
$value = drupal_strtolower($value);
}
switch ($conf['operator']) {
case 'isset':
return isset($_GET[$conf['argument']]);
case '=':
return $argument === $value;
case '!=':
return $argument !== $value;
case 'regex':
return preg_match($value, $argument);
case '!regex':
return !preg_match($value, $argument);
}
}
/**
* Provide a summary description based upon the specified context
*/
function ctools_get_ctools_access_summary($conf, $context) {
$values = array('@identifier' => $conf['argument'], '@value' => $conf['value']);
switch ($conf['operator']) {
case 'isset':
return t('GET argument @identifier is set', $values);
case '=':
return t('GET argument @identifier is "@value"', $values);
case '!=':
return t('GET argument @identifier is not "@value"', $values);
case 'regex':
return t('GET argument @identifier matches "@value"', $values);
case '!regex':
return t('GET argument @identifier does not match "@value"', $values);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment