| 1 |
<?php
|
| 2 |
// $Id: flexifilter.conditions.inc,v 1.5 2008/01/21 16:42:35 corsix Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_flexifilter_conditions()
|
| 6 |
*/
|
| 7 |
function flexifilter_flexifilter_conditions() {
|
| 8 |
$conditions = array();
|
| 9 |
$conditions['flexifilter_group_or'] = array(
|
| 10 |
'label' => t('OR Group'),
|
| 11 |
'description' => t('True if any of the contained conditions are true'),
|
| 12 |
'contains_conditions' => TRUE,
|
| 13 |
'callback' => 'flexifilter_condition_group_or',
|
| 14 |
'group' => t('Logic'),
|
| 15 |
);
|
| 16 |
$conditions['flexifilter_group_and'] = array(
|
| 17 |
'label' => t('AND Group'),
|
| 18 |
'description' => t('True if all of the contained conditions are true'),
|
| 19 |
'contains_conditions' => TRUE,
|
| 20 |
'callback' => 'flexifilter_condition_group_and',
|
| 21 |
'group' => t('Logic'),
|
| 22 |
);
|
| 23 |
$conditions['flexifilter_group_not'] = array(
|
| 24 |
'label' => t('NOT Group'),
|
| 25 |
'description' => t('True if any of the contained conditions are false'),
|
| 26 |
'contains_conditions' => TRUE,
|
| 27 |
'callback' => 'flexifilter_condition_group_not',
|
| 28 |
'group' => t('Logic'),
|
| 29 |
);
|
| 30 |
$conditions['flexifilter_true'] = array(
|
| 31 |
'label' => t('Always true'),
|
| 32 |
'callback' => 'flexifilter_condition_constant',
|
| 33 |
'callback_arguments' => array(TRUE),
|
| 34 |
'group' => t('Logic'),
|
| 35 |
);
|
| 36 |
$conditions['flexifilter_false'] = array(
|
| 37 |
'label' => t('Always false'),
|
| 38 |
'callback' => 'flexifilter_condition_constant',
|
| 39 |
'callback_arguments' => array(FALSE),
|
| 40 |
'group' => t('Logic'),
|
| 41 |
);
|
| 42 |
$conditions['flexifilter_text_search'] = array(
|
| 43 |
'label' => t('Text contains phrase'),
|
| 44 |
'description' => t('True if the text contains the specified phrase. Can optionally use regular expressions.'),
|
| 45 |
'callback' => 'flexifilter_condition_text_search',
|
| 46 |
'group' => t('Text'),
|
| 47 |
);
|
| 48 |
$conditions['flexifilter_text_length'] = array(
|
| 49 |
'label' => t('Text length is...'),
|
| 50 |
'description' => t('True if the text length meets the condition.'),
|
| 51 |
'callback' => 'flexifilter_condition_text_length',
|
| 52 |
'group' => t('Text'),
|
| 53 |
);
|
| 54 |
$conditions['flexifilter_step_prepare'] = array(
|
| 55 |
'label' => t('Is preparation step'),
|
| 56 |
'description' => t('True only during the preparation step of filtering.'),
|
| 57 |
'callback' => 'flexifilter_condition_step',
|
| 58 |
'callback_arguments' => array('prepare'),
|
| 59 |
'group' => t('Filtering Steps'),
|
| 60 |
'is_advanced' => TRUE,
|
| 61 |
);
|
| 62 |
$conditions['flexifilter_step_process'] = array(
|
| 63 |
'label' => t('Is processing step'),
|
| 64 |
'description' => t('True only during the processing step of filtering.'),
|
| 65 |
'callback' => 'flexifilter_condition_step',
|
| 66 |
'callback_arguments' => array('process'),
|
| 67 |
'group' => t('Filtering Steps'),
|
| 68 |
'is_advanced' => TRUE,
|
| 69 |
);
|
| 70 |
// Advanced.
|
| 71 |
$conditions['flexifilter_advanced_php'] = array(
|
| 72 |
'label' => t('PHP code'),
|
| 73 |
'description' => t('Uses php code to evaluate a condition.'),
|
| 74 |
'callback' => 'flexifilter_condition_advanced_php',
|
| 75 |
'group' => t('Advanced'),
|
| 76 |
'is_advanced' => TRUE,
|
| 77 |
'step' => 'either',
|
| 78 |
);
|
| 79 |
return $conditions;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Flexifilter condition callback.
|
| 84 |
* Returns TRUE if the phrase/regex is found in the text.
|
| 85 |
*/
|
| 86 |
function flexifilter_condition_text_search($op, $settings, $text) {
|
| 87 |
switch ($op) {
|
| 88 |
case 'settings':
|
| 89 |
$form = array();
|
| 90 |
$form['find'] = array(
|
| 91 |
'#type' => 'textfield',
|
| 92 |
'#title' => t('Text to find'),
|
| 93 |
'#default_value' => isset($settings['find']) ? $settings['find'] : '',
|
| 94 |
'#required' => TRUE,
|
| 95 |
);
|
| 96 |
$form['is_regex'] = array(
|
| 97 |
'#type' => 'checkbox',
|
| 98 |
'#title' => t('Use regular expressions'),
|
| 99 |
'#description' => t('If ticked, then the above text to find is interpreted as a regular expression.'),
|
| 100 |
'#default_value' => isset($settings['is_regex']) ? $settings['is_regex'] : 0,
|
| 101 |
);
|
| 102 |
return $form;
|
| 103 |
|
| 104 |
case 'prepare':
|
| 105 |
case 'process':
|
| 106 |
if (isset($settings['is_regex']) && $settings['is_regex'] == 1) {
|
| 107 |
return preg_match('~'. str_replace('~', '\~', $settings['find']) .'~', $text) == 1;
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
return strpos($text, $settings['find']) !== FALSE;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
}
|
| 114 |
|
| 115 |
/**
|
| 116 |
* Flexifilter condition callback.
|
| 117 |
* Returns TRUE if the text length in characters meets the user-specified condition.
|
| 118 |
*/
|
| 119 |
function flexifilter_condition_text_length($op, $settings, $text) {
|
| 120 |
switch ($op) {
|
| 121 |
case 'settings':
|
| 122 |
$form = array();
|
| 123 |
$form['operator'] = array(
|
| 124 |
'#type' => 'radios',
|
| 125 |
'#title' => t('Operator'),
|
| 126 |
'#options' => array(
|
| 127 |
0 => 'Is less than',
|
| 128 |
1 => 'Is less than or equal to',
|
| 129 |
2 => 'Is equal to',
|
| 130 |
3 => 'Is more than or equal to',
|
| 131 |
4 => 'Is more than',
|
| 132 |
),
|
| 133 |
'#default_value' => isset($settings['operator']) ? $settings['operator'] : 0,
|
| 134 |
'#required' => TRUE,
|
| 135 |
);
|
| 136 |
$form['num'] = array(
|
| 137 |
'#type' => 'textfield',
|
| 138 |
'#title' => t('Length in character count'),
|
| 139 |
'#desrciption' => t('Leave blank for teaser length.'),
|
| 140 |
'#default_value' => isset($settings['num']) ? $settings['num'] : '',
|
| 141 |
'#required' => TRUE,
|
| 142 |
);
|
| 143 |
return $form;
|
| 144 |
|
| 145 |
case 'prepare':
|
| 146 |
case 'process':
|
| 147 |
$length = strlen($text);
|
| 148 |
$operator = isset($settings['operator']) ? $settings['operator'] : 0;
|
| 149 |
$num = isset($settings['num']) ? $settings['num'] : variable_get('teaser_length', 600);;
|
| 150 |
switch ($operator) {
|
| 151 |
case 0:
|
| 152 |
return ($length < $num);
|
| 153 |
|
| 154 |
case 1:
|
| 155 |
return ($length <= $num);
|
| 156 |
|
| 157 |
case 2:
|
| 158 |
return ($length == $num);
|
| 159 |
|
| 160 |
case 3:
|
| 161 |
return ($length >= $num);
|
| 162 |
|
| 163 |
case 4:
|
| 164 |
return ($length > $num);
|
| 165 |
}
|
| 166 |
}
|
| 167 |
}
|
| 168 |
|
| 169 |
/**
|
| 170 |
* Flexifilter condition callback.
|
| 171 |
* Returns TRUE if all of the conditions it contains are TRUE.
|
| 172 |
*/
|
| 173 |
function flexifilter_condition_group_and($op, $settings, $text) {
|
| 174 |
switch ($op) {
|
| 175 |
case 'settings':
|
| 176 |
$form = array();
|
| 177 |
return $form;
|
| 178 |
|
| 179 |
case 'prepare':
|
| 180 |
case 'process':
|
| 181 |
foreach ($settings['conditions'] as $condition) {
|
| 182 |
if (flexifilter_invoke_condition($condition, $op, $text) == FALSE) {
|
| 183 |
return FALSE;
|
| 184 |
}
|
| 185 |
}
|
| 186 |
return TRUE;
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
/**
|
| 191 |
* Flexifilter condition callback.
|
| 192 |
* Returns TRUE if any of the conditions it contains are TRUE.
|
| 193 |
*/
|
| 194 |
function flexifilter_condition_group_or($op, $settings, $text) {
|
| 195 |
switch ($op) {
|
| 196 |
case 'settings':
|
| 197 |
$form = array();
|
| 198 |
return $form;
|
| 199 |
|
| 200 |
case 'prepare':
|
| 201 |
case 'process':
|
| 202 |
foreach ($settings['conditions'] as $condition) {
|
| 203 |
if (flexifilter_invoke_condition($condition, $op, $text) == TRUE) {
|
| 204 |
return TRUE;
|
| 205 |
}
|
| 206 |
}
|
| 207 |
return FALSE;
|
| 208 |
}
|
| 209 |
}
|
| 210 |
|
| 211 |
/**
|
| 212 |
* Flexifilter condition callback.
|
| 213 |
* Returns TRUE if any of the conditions it contains are FALSE.
|
| 214 |
*/
|
| 215 |
function flexifilter_condition_group_not($op, $settings, $text) {
|
| 216 |
switch ($op) {
|
| 217 |
case 'settings':
|
| 218 |
$form = array();
|
| 219 |
return $form;
|
| 220 |
|
| 221 |
case 'prepare':
|
| 222 |
case 'process':
|
| 223 |
foreach ($settings['conditions'] as $condition) {
|
| 224 |
if (flexifilter_invoke_condition($condition, $op, $text) == FALSE) {
|
| 225 |
return TRUE;
|
| 226 |
}
|
| 227 |
}
|
| 228 |
return FALSE;
|
| 229 |
}
|
| 230 |
}
|
| 231 |
|
| 232 |
/**
|
| 233 |
* Flexifilter condition callback.
|
| 234 |
* Returns $constant.
|
| 235 |
*
|
| 236 |
* @param $constant
|
| 237 |
* The constant to return, either TRUE or FALSE.
|
| 238 |
*/
|
| 239 |
function flexifilter_condition_constant($constant, $op, $settings, $text) {
|
| 240 |
switch ($op) {
|
| 241 |
case 'settings':
|
| 242 |
return array();
|
| 243 |
|
| 244 |
case 'prepare':
|
| 245 |
case 'process':
|
| 246 |
return $constant;
|
| 247 |
}
|
| 248 |
}
|
| 249 |
|
| 250 |
/**
|
| 251 |
* Flexifilter condition callback.
|
| 252 |
* Returns TRUE if the current step matches $step.
|
| 253 |
*
|
| 254 |
* @param $step
|
| 255 |
* The step to match, either 'prepare' or 'process'.
|
| 256 |
*/
|
| 257 |
function flexifilter_condition_step($step, $op, $settings, $text) {
|
| 258 |
switch ($op) {
|
| 259 |
case 'settings':
|
| 260 |
return array();
|
| 261 |
|
| 262 |
case 'prepare':
|
| 263 |
case 'process':
|
| 264 |
return $step == $op;
|
| 265 |
}
|
| 266 |
}
|
| 267 |
|
| 268 |
/**
|
| 269 |
* Flexifilter condition callback.
|
| 270 |
* Returns whatever the custom php code written by the user returns.
|
| 271 |
* Is somewhat safe (as in it won't break your site) in that it only runs during the filtering process, thus not on every page load.
|
| 272 |
*/
|
| 273 |
function flexifilter_condition_advanced_php($op, $settings, $text) {
|
| 274 |
switch ($op) {
|
| 275 |
case 'settings':
|
| 276 |
$form = array();
|
| 277 |
$form['code'] = array(
|
| 278 |
'#type' => 'textarea',
|
| 279 |
'#title' => t('PHP code to be evaluated.'),
|
| 280 |
'#description' => t('Enter the code to evaluate. Does not need <?php ?> tags. Available variables are <em>$text</em>, which contains the text, and $op, which contains the operation, which will either be "prepare" or "process". This should return either TRUE if the condition is true or FALSE if the condition is false. WARNING: Evaluating incorrect PHP code can break your site.'),
|
| 281 |
'#default_value' => isset($settings['code']) ? $settings['code'] : 'return TRUE;',
|
| 282 |
);
|
| 283 |
return $form;
|
| 284 |
|
| 285 |
case 'prepare':
|
| 286 |
case 'process':
|
| 287 |
$code = isset($settings['code']) ? $settings['code'] : 'return TRUE;';
|
| 288 |
return eval($code);
|
| 289 |
}
|
| 290 |
}
|
| 291 |
|