1 |
85ad3d82
|
Assos Assos
|
<?php
|
2 |
|
|
|
3 |
|
|
/**
|
4 |
|
|
* @file
|
5 |
c304a780
|
Assos Assos
|
* Plugin to provide access control/visibility based on specified context string matching user-specified string.
|
6 |
85ad3d82
|
Assos Assos
|
*/
|
7 |
|
|
|
8 |
|
|
$plugin = array(
|
9 |
|
|
'title' => t("String: comparison"),
|
10 |
|
|
'description' => t('Control access by string match.'),
|
11 |
|
|
'callback' => 'ctools_string_equal_ctools_access_check',
|
12 |
|
|
'settings form' => 'ctools_string_equal_ctools_access_settings',
|
13 |
|
|
'summary' => 'ctools_string_equal_ctools_access_summary',
|
14 |
|
|
'required context' => new ctools_context_required(t('String'), 'string'),
|
15 |
|
|
'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE),
|
16 |
|
|
);
|
17 |
|
|
|
18 |
|
|
/**
|
19 |
c304a780
|
Assos Assos
|
* Settings form.
|
20 |
85ad3d82
|
Assos Assos
|
*/
|
21 |
|
|
function ctools_string_equal_ctools_access_settings($form, &$form_state, $conf) {
|
22 |
|
|
$form['settings']['operator'] = array(
|
23 |
|
|
'#type' => 'radios',
|
24 |
|
|
'#title' => t('Operator'),
|
25 |
|
|
'#options' => array(
|
26 |
|
|
'=' => t('Equal'),
|
27 |
|
|
'!=' => t('Not equal'),
|
28 |
|
|
'regex' => t('Regular expression'),
|
29 |
|
|
'!regex' => t('Not equal to regular expression'),
|
30 |
|
|
),
|
31 |
|
|
'#default_value' => $conf['operator'],
|
32 |
|
|
'#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.'),
|
33 |
|
|
);
|
34 |
|
|
|
35 |
|
|
$form['settings']['value'] = array(
|
36 |
|
|
'#type' => 'textfield',
|
37 |
|
|
'#title' => t('String'),
|
38 |
|
|
'#default_value' => $conf['value'],
|
39 |
|
|
);
|
40 |
|
|
|
41 |
|
|
$form['settings']['case'] = array(
|
42 |
|
|
'#type' => 'checkbox',
|
43 |
|
|
'#title' => t('Case sensitive'),
|
44 |
|
|
'#default_value' => $conf['case'],
|
45 |
|
|
);
|
46 |
|
|
return $form;
|
47 |
|
|
}
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
c304a780
|
Assos Assos
|
* Check for access.
|
51 |
85ad3d82
|
Assos Assos
|
*/
|
52 |
|
|
function ctools_string_equal_ctools_access_check($conf, $context) {
|
53 |
|
|
if (empty($context) || empty($context->data)) {
|
54 |
|
|
$string = '';
|
55 |
|
|
}
|
56 |
|
|
else {
|
57 |
|
|
$string = $context->data;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
$value = $conf['value'];
|
61 |
|
|
if (empty($conf['case'])) {
|
62 |
|
|
$string = drupal_strtolower($string);
|
63 |
|
|
$value = drupal_strtolower($value);
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
switch ($conf['operator']) {
|
67 |
|
|
case '=':
|
68 |
|
|
return $string === $value;
|
69 |
c304a780
|
Assos Assos
|
|
70 |
85ad3d82
|
Assos Assos
|
case '!=':
|
71 |
|
|
return $string !== $value;
|
72 |
c304a780
|
Assos Assos
|
|
73 |
85ad3d82
|
Assos Assos
|
case 'regex':
|
74 |
|
|
return preg_match($value, $string);
|
75 |
c304a780
|
Assos Assos
|
|
76 |
85ad3d82
|
Assos Assos
|
case '!regex':
|
77 |
|
|
return !preg_match($value, $string);
|
78 |
|
|
}
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
/**
|
82 |
c304a780
|
Assos Assos
|
* Provide a summary description based upon the specified context.
|
83 |
85ad3d82
|
Assos Assos
|
*/
|
84 |
|
|
function ctools_string_equal_ctools_access_summary($conf, $context) {
|
85 |
|
|
$values = array('@identifier' => $context->identifier, '@value' => $conf['value']);
|
86 |
|
|
switch ($conf['operator']) {
|
87 |
|
|
case '=':
|
88 |
|
|
return t('@identifier is "@value"', $values);
|
89 |
c304a780
|
Assos Assos
|
|
90 |
85ad3d82
|
Assos Assos
|
case '!=':
|
91 |
|
|
return t('@identifier is not "@value"', $values);
|
92 |
c304a780
|
Assos Assos
|
|
93 |
85ad3d82
|
Assos Assos
|
case 'regex':
|
94 |
|
|
return t('@identifier matches "@value"', $values);
|
95 |
c304a780
|
Assos Assos
|
|
96 |
85ad3d82
|
Assos Assos
|
case '!regex':
|
97 |
|
|
return t('@identifier does not match "@value"', $values);
|
98 |
|
|
}
|
99 |
|
|
} |