| 1 |
<?php
|
| 2 |
// $Id: flexifilter.components.inc,v 1.13 2008/02/03 22:16:12 cwgordon7 Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Implementation of hook_flexifilter_components()
|
| 6 |
*
|
| 7 |
* @return An array of components to be used by the Flexifilter module. The keys of this array
|
| 8 |
* are unique identifiers for the component (called the component class), and the values of the
|
| 9 |
* array are again arrays, with the following keys:
|
| 10 |
* - label : A human readable name for the component
|
| 11 |
* - description (optional) : A human readable description of what the component does (defaults to none)
|
| 12 |
* - is_container (optional) : TRUE if any of the #contains_ fields are TRUE (set automatically)\
|
| 13 |
* - is_advanced (optional) : TRUE if the component should only be shown in component dropdown lists in advanced editing mode (defaults to FALSE)
|
| 14 |
* - contains_condition (optional) : TRUE if the component has a condition associated with it (defaults to FALSE)
|
| 15 |
* - contains_components (optional) : TRUE if the component has children components (defaults to FALSE)
|
| 16 |
* - callback : A callback function which implements the component
|
| 17 |
* - callback_arguments (optional) : An array of arguments to pass to the callback function (defaults to none)
|
| 18 |
* - group (optional) : A human readable name of the group that the component belongs to (defaults to "Other")
|
| 19 |
*/
|
| 20 |
function flexifilter_flexifilter_components() {
|
| 21 |
$components = array();
|
| 22 |
// If, While, etc.
|
| 23 |
$components['flexifilter_control_if'] = array(
|
| 24 |
'label' => t('If'),
|
| 25 |
'description' => t('If and only if the condition is true, then the actions are performed.'),
|
| 26 |
'contains_condition' => TRUE,
|
| 27 |
'contains_components' => TRUE,
|
| 28 |
'callback' => 'flexifilter_component_if',
|
| 29 |
'group' => t('Control Structures'),
|
| 30 |
'step' => 'both',
|
| 31 |
);
|
| 32 |
$components['flexifilter_control_while'] = array(
|
| 33 |
'label' => t('While'),
|
| 34 |
'description' => t('While the condition is true, the actions are performed.'),
|
| 35 |
'contains_condition' => TRUE,
|
| 36 |
'contains_components' => TRUE,
|
| 37 |
'callback' => 'flexifilter_component_while',
|
| 38 |
'group' => t('Control Structures'),
|
| 39 |
'step' => 'both',
|
| 40 |
);
|
| 41 |
|
| 42 |
// Simple text operations
|
| 43 |
$components['flexifilter_text_replace'] = array(
|
| 44 |
'label' => t('Simple Text Replace'),
|
| 45 |
'description' => t('Does a primtive search & replace on the text.'),
|
| 46 |
'callback' => 'flexifilter_component_text_simple_replace',
|
| 47 |
'group' => t('Text: Simple'),
|
| 48 |
'step' => 'either',
|
| 49 |
);
|
| 50 |
$components['flexifilter_text_regex'] = array(
|
| 51 |
'label' => t('Regex Text Replace'),
|
| 52 |
'description' => t('Does a search & replace on the text using regular expressions.'),
|
| 53 |
'callback' => 'flexifilter_component_text_regex_replace',
|
| 54 |
'group' => t('Text: Intermediate'),
|
| 55 |
'step' => 'either',
|
| 56 |
);
|
| 57 |
$components['flexifilter_text_alternation'] = array(
|
| 58 |
'label' => t('Pattern-based Text Replacement'),
|
| 59 |
'description' => t('Cycles through several replacements on the text.'),
|
| 60 |
'callback' => 'flexifilter_component_text_pattern_replace',
|
| 61 |
'group' => t('Text: Intermediate'),
|
| 62 |
'step' => 'either',
|
| 63 |
);
|
| 64 |
$components['flexifilter_text_append'] = array(
|
| 65 |
'label' => t('Simple Text Append'),
|
| 66 |
'description' => t('Appends text to the end.'),
|
| 67 |
'callback' => 'flexifilter_component_text_append',
|
| 68 |
'group' => t('Text: Simple'),
|
| 69 |
'step' => 'either',
|
| 70 |
);
|
| 71 |
$components['flexifilter_text_prepend'] = array(
|
| 72 |
'label' => t('Simple Text Prepend'),
|
| 73 |
'description' => t('Prepends text to the beginning.'),
|
| 74 |
'callback' => 'flexifilter_component_text_prepend',
|
| 75 |
'group' => t('Text: Simple'),
|
| 76 |
'step' => 'either',
|
| 77 |
);
|
| 78 |
$components['flexifilter_text_substr'] = array(
|
| 79 |
'label' => t('Simple Text Slice'),
|
| 80 |
'description' => t('"Slices" a certain number of characters off either end of the text.'),
|
| 81 |
'callback' => 'flexifilter_component_text_substr',
|
| 82 |
'group' => t('Text: Simple'),
|
| 83 |
'step' => 'either',
|
| 84 |
);
|
| 85 |
$components['flexifilter_advanced_append'] = array(
|
| 86 |
'label' => t('Advanced Text Append'),
|
| 87 |
'description' => t('Appends text to the end.'),
|
| 88 |
'callback' => 'flexifilter_component_advanced_append',
|
| 89 |
'group' => t('Text: Intermediate'),
|
| 90 |
'contains_components' => TRUE,
|
| 91 |
'step' => 'either',
|
| 92 |
);
|
| 93 |
$components['flexifilter_advanced_prepend'] = array(
|
| 94 |
'label' => t('Advanced Text Prepend'),
|
| 95 |
'description' => t('Prepends text to the beginning.'),
|
| 96 |
'callback' => 'flexifilter_component_advanced_prepend',
|
| 97 |
'group' => t('Text: Intermediate'),
|
| 98 |
'contains_components' => TRUE,
|
| 99 |
'step' => 'either',
|
| 100 |
);
|
| 101 |
|
| 102 |
// Advanced.
|
| 103 |
$components['flexifilter_advanced_php'] = array(
|
| 104 |
'label' => t('PHP code'),
|
| 105 |
'description' => t('Uses php code to modify text.'),
|
| 106 |
'callback' => 'flexifilter_component_advanced_php',
|
| 107 |
'group' => t('Advanced'),
|
| 108 |
'is_advanced' => TRUE,
|
| 109 |
'step' => 'either',
|
| 110 |
);
|
| 111 |
|
| 112 |
// Chunks!
|
| 113 |
$components['flexifilter_chunk_grab'] = array(
|
| 114 |
'label' => t('Chunk grabber.'),
|
| 115 |
'description' => t('Grabs chunks of text for further filtering and then sticks the filtered version back into the text.'),
|
| 116 |
'callback' => 'flexifilter_component_chunk_grab',
|
| 117 |
'group' => t('Chunks'),
|
| 118 |
'contains_components' => TRUE,
|
| 119 |
'step' => 'either',
|
| 120 |
);
|
| 121 |
|
| 122 |
// Sequences
|
| 123 |
$components['flexifilter_basic_sequences'] = array(
|
| 124 |
'label' => t('Basic sequences'),
|
| 125 |
'description' => t('Choose from a variety of sequences to insert.'),
|
| 126 |
'callback' => 'flexifilter_component_sequences',
|
| 127 |
'group' => t('Sequences'),
|
| 128 |
'step' => 'either',
|
| 129 |
);
|
| 130 |
|
| 131 |
// Allow existing filters to be used as components
|
| 132 |
foreach (module_list() as $module) {
|
| 133 |
if ($module !== 'flexifilter') {
|
| 134 |
$list = module_invoke($module, 'filter', 'list');
|
| 135 |
if (isset($list) && is_array($list)) {
|
| 136 |
foreach ($list as $delta => $name) {
|
| 137 |
$components['flexifilter_existing__'. $module .'__filter__'. $delta] = array(
|
| 138 |
'label' => $name,
|
| 139 |
'description' => module_invoke($module, 'filter', 'description', $delta),
|
| 140 |
'callback' => 'flexifilter_existing_filter_as_component',
|
| 141 |
'callback_arguments' => array($module, $delta),
|
| 142 |
'group' => t('Existing Filters'),
|
| 143 |
'step' => 'both',
|
| 144 |
);
|
| 145 |
}
|
| 146 |
}
|
| 147 |
}
|
| 148 |
}
|
| 149 |
|
| 150 |
// Allow existing flexifilters to be used as components
|
| 151 |
foreach (flexifilter_get_filters(FALSE) as $fid => $filter) {
|
| 152 |
$components['flexifilter_existing_flexifilter__'. $fid] = array(
|
| 153 |
'label' => $filter['label'],
|
| 154 |
'description' => $filter['description'],
|
| 155 |
'callback' => 'flexifilter_existing_flexifilter_as_component',
|
| 156 |
'callback_arguments' => array($fid),
|
| 157 |
'group' => t('Existing Filters'),
|
| 158 |
'step' => 'both',
|
| 159 |
);
|
| 160 |
}
|
| 161 |
|
| 162 |
return $components;
|
| 163 |
}
|
| 164 |
|
| 165 |
/**
|
| 166 |
* Flexifilter component callback.
|
| 167 |
* Handles a simple text replacement through str_replace.
|
| 168 |
*/
|
| 169 |
function flexifilter_component_text_simple_replace($op, $settings, $text = '') {
|
| 170 |
switch ($op) {
|
| 171 |
case 'settings':
|
| 172 |
$form = array();
|
| 173 |
$form['find'] = array(
|
| 174 |
'#type' => 'textfield',
|
| 175 |
'#title' => t('Text to find'),
|
| 176 |
'#default_value' => isset($settings['find']) ? $settings['find'] : '',
|
| 177 |
'#required' => TRUE,
|
| 178 |
);
|
| 179 |
$form['replace'] = array(
|
| 180 |
'#type' => 'textfield',
|
| 181 |
'#title' => t('Replacement text'),
|
| 182 |
'#default_value' => isset($settings['replace']) ? $settings['replace'] : '',
|
| 183 |
'#required' => FALSE,
|
| 184 |
);
|
| 185 |
return $form;
|
| 186 |
|
| 187 |
case 'prepare':
|
| 188 |
case 'process':
|
| 189 |
return str_replace($settings['find'], isset($settings['replace']) ? $settings['replace'] : '', $text);
|
| 190 |
}
|
| 191 |
return $text;
|
| 192 |
}
|
| 193 |
|
| 194 |
/**
|
| 195 |
* Flexifilter component callback.
|
| 196 |
* Handles a more complex text replacement with regular expressions through preg_replace.
|
| 197 |
*/
|
| 198 |
function flexifilter_component_text_regex_replace($op, $settings, $text = '') {
|
| 199 |
switch ($op) {
|
| 200 |
case 'settings':
|
| 201 |
$form = array();
|
| 202 |
$form['find'] = array(
|
| 203 |
'#type' => 'textfield',
|
| 204 |
'#title' => t('Text to find'),
|
| 205 |
'#description' => t('Uses Perl compatible regular expressions'),
|
| 206 |
'#default_value' => isset($settings['find']) ? $settings['find'] : '',
|
| 207 |
'#required' => TRUE,
|
| 208 |
);
|
| 209 |
$form['replace'] = array(
|
| 210 |
'#type' => 'textfield',
|
| 211 |
'#title' => t('Replacement text'),
|
| 212 |
'#description' => t('May contain references of the form $n'),
|
| 213 |
'#default_value' => isset($settings['replace']) ? $settings['replace'] : '',
|
| 214 |
'#required' => FALSE,
|
| 215 |
);
|
| 216 |
return $form;
|
| 217 |
|
| 218 |
case 'prepare':
|
| 219 |
case 'process':
|
| 220 |
return preg_replace('~'. str_replace('~', '\~', $settings['find']) .'~', isset($settings['replace']) ? $settings['replace'] : '', $text);
|
| 221 |
}
|
| 222 |
return $text;
|
| 223 |
}
|
| 224 |
|
| 225 |
/**
|
| 226 |
* Flexifilter component callback.
|
| 227 |
* Handles a basic IF statement with a condition that determines whether or not its components are fired.
|
| 228 |
*/
|
| 229 |
function flexifilter_component_if($op, $settings, $text) {
|
| 230 |
switch ($op) {
|
| 231 |
case 'settings':
|
| 232 |
$form = array();
|
| 233 |
return $form;
|
| 234 |
|
| 235 |
case 'prepare':
|
| 236 |
case 'process':
|
| 237 |
if (flexifilter_invoke_condition($settings['condition'], $op, $text) == TRUE) {
|
| 238 |
$text = flexifilter_invoke_components($settings['components'], $op, $text);
|
| 239 |
}
|
| 240 |
default:
|
| 241 |
return $text;
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
/**
|
| 246 |
* Flexifilter component callback.
|
| 247 |
* Handles a basic WHILE statement with a condition that determines whether or not its components are fired.
|
| 248 |
* They will be fired until the condition is FALSE or the operation times out.
|
| 249 |
*/
|
| 250 |
function flexifilter_component_while($op, $settings, $text) {
|
| 251 |
switch ($op) {
|
| 252 |
case 'settings':
|
| 253 |
$form = array();
|
| 254 |
$form['limit'] = array(
|
| 255 |
'#type' => 'textfield',
|
| 256 |
'#title' => t('Number of iterations before abort'),
|
| 257 |
'#description' => t('In order to prevent infinite looping, the loop will stop after a given number of iterations, even if the condition is still true.'),
|
| 258 |
'#default_value' => isset($settings['limit']) ? $settings['limit'] : '100',
|
| 259 |
'#required' => TRUE,
|
| 260 |
);
|
| 261 |
if (!isset($settings['limit'])) {
|
| 262 |
$form['limit']['#value'] = $form['limit']['#default_value'];
|
| 263 |
}
|
| 264 |
return $form;
|
| 265 |
|
| 266 |
case 'prepare':
|
| 267 |
case 'process':
|
| 268 |
// Optimization case; the loop is skipped if none of the components do anything
|
| 269 |
// in this step. Also helps prevent naive infinite loops like this one:
|
| 270 |
// while (text contains "example") replace "example" with "e.g."
|
| 271 |
// As the replacement would only run in processing, the while loop would loop
|
| 272 |
// forever in preparation.
|
| 273 |
$limit = (integer)$settings['limit'];
|
| 274 |
$n = 0;
|
| 275 |
if (flexifilter_components_involve_step($settings['components'], $op)) {
|
| 276 |
while (flexifilter_invoke_condition($settings['condition'], $op, $text) == TRUE) {
|
| 277 |
$text = flexifilter_invoke_components($settings['components'], $op, $text);
|
| 278 |
$n++;
|
| 279 |
if ($n >= $limit) {
|
| 280 |
drupal_set_message(t('Flexifilter while loop aborted at @count iterations. Consider tweaking the limit or condition.', array('@count' => $n)));
|
| 281 |
break;
|
| 282 |
}
|
| 283 |
}
|
| 284 |
}
|
| 285 |
default:
|
| 286 |
return $text;
|
| 287 |
}
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Helper function for flexifilter_existing_filter_as_component.
|
| 292 |
* Modifies the global configuration and returns a backup of the old configuration.
|
| 293 |
*
|
| 294 |
* @param $settings
|
| 295 |
* The settings to set the global configuration to.
|
| 296 |
* @see _flexifilter_pop_conf
|
| 297 |
*/
|
| 298 |
function _flexifilter_push_conf($settings) {
|
| 299 |
global $conf;
|
| 300 |
$conf_backup = $conf;
|
| 301 |
foreach ($settings as $key => $value) {
|
| 302 |
$conf[$key] = $value;
|
| 303 |
}
|
| 304 |
return $conf_backup;
|
| 305 |
}
|
| 306 |
|
| 307 |
/**
|
| 308 |
* Helper function for flexifilter_existing_filter_as_component.
|
| 309 |
* Sets the global configuration back to the backup.
|
| 310 |
*
|
| 311 |
* @param $old
|
| 312 |
* The old settings to revert back to.
|
| 313 |
* @see _flexifilter_push_conf
|
| 314 |
*/
|
| 315 |
function _flexifilter_pop_conf($old) {
|
| 316 |
global $conf;
|
| 317 |
$conf = $old;
|
| 318 |
}
|
| 319 |
|
| 320 |
/**
|
| 321 |
* Flexifilter component callback.
|
| 322 |
* Handles the usage of another filter as a component.
|
| 323 |
*
|
| 324 |
* @param $module
|
| 325 |
* The module whose filter is being called.
|
| 326 |
* @param $delta
|
| 327 |
* The delta (identifier) of the filter in the module being called.
|
| 328 |
*/
|
| 329 |
function flexifilter_existing_filter_as_component($module, $delta, $op, $settings, $text) {
|
| 330 |
global $conf;
|
| 331 |
switch ($op) {
|
| 332 |
case 'settings':
|
| 333 |
$form = array();
|
| 334 |
$oldconf = _flexifilter_push_conf($settings);
|
| 335 |
$settings = module_invoke($module, 'filter', 'settings', $delta, 1, $text);
|
| 336 |
_flexifilter_pop_conf($oldconf);
|
| 337 |
if (is_array($settings) && count($settings) > 0) {
|
| 338 |
foreach (element_children($settings) as $key) {
|
| 339 |
$element = $settings[$key];
|
| 340 |
if ($element['#type'] == 'fieldset') {
|
| 341 |
foreach (element_children($element) as $key2) {
|
| 342 |
$form[$key2] = $element[$key2];
|
| 343 |
}
|
| 344 |
}
|
| 345 |
}
|
| 346 |
}
|
| 347 |
return $form;
|
| 348 |
|
| 349 |
case 'prepare':
|
| 350 |
case 'process':
|
| 351 |
$oldconf = _flexifilter_push_conf($settings);
|
| 352 |
$text = module_invoke($module, 'filter', $op, $delta, 1, $text);
|
| 353 |
_flexifilter_pop_conf($oldconf);
|
| 354 |
return $text;
|
| 355 |
}
|
| 356 |
}
|
| 357 |
|
| 358 |
/**
|
| 359 |
* Flexifilter component callback.
|
| 360 |
* Handles the usage of another flexifilter as a component.
|
| 361 |
*
|
| 362 |
* @param $fid
|
| 363 |
* The flexifilter id of the flexifilter being used as a component.
|
| 364 |
*/
|
| 365 |
function flexifilter_existing_flexifilter_as_component($fid, $op, $settings, $text) {
|
| 366 |
switch ($op) {
|
| 367 |
case 'settings':
|
| 368 |
return array();
|
| 369 |
|
| 370 |
case 'prepare':
|
| 371 |
case 'process':
|
| 372 |
$filters = flexifilter_get_filters();
|
| 373 |
$filter = $filters[$fid];
|
| 374 |
return flexifilter_invoke_components($filter['components'], $op, $text);
|
| 375 |
}
|
| 376 |
}
|
| 377 |
|
| 378 |
/**
|
| 379 |
* Flexifilter component callback.
|
| 380 |
* Replaces text based on a pattern.
|
| 381 |
*/
|
| 382 |
function flexifilter_component_text_pattern_replace($op, $settings, $text) {
|
| 383 |
switch ($op) {
|
| 384 |
case 'settings':
|
| 385 |
$form = array();
|
| 386 |
$max = isset($settings['number']) ? $settings['number'] : '2';
|
| 387 |
$form['find'] = array(
|
| 388 |
'#type' => 'textfield',
|
| 389 |
'#title' => t('Text to find'),
|
| 390 |
'#description' => t('Enter the text that will be replaced based on a pattern.'),
|
| 391 |
'#default_value' => isset($settings['find']) ? $settings['find'] : '',
|
| 392 |
'#required' => TRUE,
|
| 393 |
);
|
| 394 |
$form['replace'] = array(
|
| 395 |
'#type' => 'textarea',
|
| 396 |
'#title' => t('Text to replace'),
|
| 397 |
'#description' => t('Enter the text that will replace based on a pattern. Place separate replacements on new lines. The replacements will be cycled through. Use [[flexifilter_count]] to represent the numberth replacement this is, starting with 1.'),
|
| 398 |
'#default_value' => isset($settings['replace']) ? $settings['replace'] : '',
|
| 399 |
'#required' => TRUE,
|
| 400 |
);
|
| 401 |
return $form;
|
| 402 |
|
| 403 |
case 'prepare':
|
| 404 |
case 'process':
|
| 405 |
$find = $settings['find'];
|
| 406 |
$i = 0;
|
| 407 |
$count = 1;
|
| 408 |
$replace = explode("\n", $settings['replace']);
|
| 409 |
$max = count($replace);
|
| 410 |
foreach ($replace as $key => $value) {
|
| 411 |
$replace[$key] = trim($value);
|
| 412 |
}
|
| 413 |
while (($pos = strpos($text, $find)) !== FALSE) {
|
| 414 |
$text = flexifilter_component_text_pattern_replace_once($pos, $find, str_replace('[[flexifilter_count]]', $count, $replace[$i]), $text);
|
| 415 |
$i++;
|
| 416 |
$count++;
|
| 417 |
if ($i >= $max) {
|
| 418 |
// Reset $i.
|
| 419 |
$i = $i - $max;
|
| 420 |
}
|
| 421 |
}
|
| 422 |
return $text;
|
| 423 |
}
|
| 424 |
}
|
| 425 |
|
| 426 |
/**
|
| 427 |
* Helper function for flexifilter_component_text_pattern_replace. Replaces text only once.
|
| 428 |
* Cannot use str_replace with $count because Drupal supports PHP 4.
|
| 429 |
*/
|
| 430 |
function flexifilter_component_text_pattern_replace_once($pos, $search, $replace, $subject) {
|
| 431 |
return substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
|
| 432 |
}
|
| 433 |
|
| 434 |
/**
|
| 435 |
* Flexifilter component callback.
|
| 436 |
* Grabs chunks of text based on starting and ending tags.
|
| 437 |
*/
|
| 438 |
function flexifilter_component_chunk_grab($op, $settings, $text) {
|
| 439 |
switch ($op) {
|
| 440 |
case 'settings':
|
| 441 |
$form = array();
|
| 442 |
$form['starts'] = array(
|
| 443 |
'#type' => 'textfield',
|
| 444 |
'#title' => t('Chunk starts with'),
|
| 445 |
'#description' => t('Enter the text that the chunk will start with.'),
|
| 446 |
'#default_value' => isset($settings['starts']) ? $settings['starts'] : '[',
|
| 447 |
'#required' => TRUE,
|
| 448 |
);
|
| 449 |
$form['ends'] = array(
|
| 450 |
'#type' => 'textfield',
|
| 451 |
'#title' => t('Chunk ends with'),
|
| 452 |
'#description' => t('Enter the text that the chunk will end with.'),
|
| 453 |
'#default_value' => isset($settings['ends']) ? $settings['ends'] : ']',
|
| 454 |
'#required' => TRUE,
|
| 455 |
);
|
| 456 |
$form['pass_limits'] = array(
|
| 457 |
'#type' => 'checkbox',
|
| 458 |
'#title' => t('Pass delimiters'),
|
| 459 |
'#description' => t('Should the delimiters be passed into the chunk?'),
|
| 460 |
'#default_value' => isset($settings['pass_limits']) ? $settings['pass_limits'] : FALSE,
|
| 461 |
);
|
| 462 |
$form['case_sensitive'] = array(
|
| 463 |
'#type' => 'checkbox',
|
| 464 |
'#title' => t('Case sensitive'),
|
| 465 |
'#description' => t('This option will make the chunk selection case-sensitive if checked.'),
|
| 466 |
'#default_value' => isset($settings['case_sensitive']) ? $settings['case_sensitive'] : TRUE,
|
| 467 |
);
|
| 468 |
$form['include_rest'] = array(
|
| 469 |
'#type' => 'checkbox',
|
| 470 |
'#title' => t('Include rest'),
|
| 471 |
'#description' => t('This option will make everything not selected by the chunk grabber, although not passed into the chunk, passed out of the grabber.'),
|
| 472 |
'#default_value' => isset($settings['include_rest']) ? $settings['include_rest'] : TRUE,
|
| 473 |
);
|
| 474 |
return $form;
|
| 475 |
|
| 476 |
case 'prepare':
|
| 477 |
case 'process':
|
| 478 |
$start = $settings['starts'];
|
| 479 |
$end = $settings['ends'];
|
| 480 |
$pass_limits = $settings['pass_limits'];
|
| 481 |
$case_sensitive = $settings['case_sensitive'];
|
| 482 |
$include_rest = $settings['include_rest'];
|
| 483 |
$chunk = '';
|
| 484 |
$pos = 0;
|
| 485 |
$endpos = 0;
|
| 486 |
$tmp = '';
|
| 487 |
$tmp2 = '';
|
| 488 |
while ($pos !== FALSE && $endpos !== FALSE) {
|
| 489 |
$pos = $case_sensitive ? strpos($text, $start) : stripos($text, $start);
|
| 490 |
if ($include_rest) {
|
| 491 |
$tmp = substr($text, 0, $pos);
|
| 492 |
}
|
| 493 |
if ($pos !== FALSE) {
|
| 494 |
$endpos = $case_sensitive ? strpos($text, $end) : stripos($text, $end);
|
| 495 |
if ($endpos !== FALSE) {
|
| 496 |
if ($include_rest) {
|
| 497 |
$tmp2 = substr($text, $endpos + strlen($end));
|
| 498 |
}
|
| 499 |
if ($pass_limits) {
|
| 500 |
$endpos = $endpos + strlen($end);
|
| 501 |
}
|
| 502 |
else {
|
| 503 |
$pos = $pos + strlen($start);
|
| 504 |
}
|
| 505 |
$chunk = substr($text, $pos, $endpos - $pos);
|
| 506 |
$tmp .= flexifilter_invoke_components($settings['components'], $op, $chunk);
|
| 507 |
$tmp .= $tmp2;
|
| 508 |
$text = $tmp;
|
| 509 |
}
|
| 510 |
}
|
| 511 |
}
|
| 512 |
return $text;
|
| 513 |
}
|
| 514 |
}
|
| 515 |
|
| 516 |
/**
|
| 517 |
* Replacement for stripos() for PHP 4.x.
|
| 518 |
*/
|
| 519 |
if (!function_exists('stripos')) {
|
| 520 |
function stripos($haystack, $needle) {
|
| 521 |
return strpos(strtolower($haystack), strtolower($needle));
|
| 522 |
}
|
| 523 |
}
|
| 524 |
|
| 525 |
/**
|
| 526 |
* Flexifilter component callback.
|
| 527 |
* Simple text append.
|
| 528 |
*/
|
| 529 |
function flexifilter_component_text_append($op, $settings, $text) {
|
| 530 |
switch ($op) {
|
| 531 |
case 'settings':
|
| 532 |
$form = array();
|
| 533 |
$form['text'] = array(
|
| 534 |
'#type' => 'textfield',
|
| 535 |
'#title' => t('Text to append'),
|
| 536 |
'#description' => t('Enter the text to append.'),
|
| 537 |
'#default_value' => isset($settings['text']) ? $settings['text'] : '',
|
| 538 |
);
|
| 539 |
return $form;
|
| 540 |
|
| 541 |
case 'prepare':
|
| 542 |
case 'process':
|
| 543 |
$append = isset($settings['text']) ? $settings['text'] : '';
|
| 544 |
return $text . $append;
|
| 545 |
}
|
| 546 |
}
|
| 547 |
|
| 548 |
/**
|
| 549 |
* Flexifilter component callback.
|
| 550 |
* Simple text prepend.
|
| 551 |
*/
|
| 552 |
function flexifilter_component_text_prepend($op, $settings, $text) {
|
| 553 |
switch ($op) {
|
| 554 |
case 'settings':
|
| 555 |
$form = array();
|
| 556 |
$form['text'] = array(
|
| 557 |
'#type' => 'textfield',
|
| 558 |
'#title' => t('Text to prepend'),
|
| 559 |
'#description' => t('Enter the text to prepend.'),
|
| 560 |
'#default_value' => isset($settings['text']) ? $settings['text'] : '',
|
| 561 |
);
|
| 562 |
return $form;
|
| 563 |
|
| 564 |
case 'prepare':
|
| 565 |
case 'process':
|
| 566 |
$prepend = isset($settings['text']) ? $settings['text'] : '';
|
| 567 |
return $prepend . $text;
|
| 568 |
}
|
| 569 |
}
|
| 570 |
|
| 571 |
/**
|
| 572 |
* Flexifilter component callback.
|
| 573 |
* "Slices" some text off.
|
| 574 |
*/
|
| 575 |
function flexifilter_component_text_substr($op, $settings, $text) {
|
| 576 |
switch ($op) {
|
| 577 |
case 'settings':
|
| 578 |
$form = array();
|
| 579 |
$form['substr_start'] = array(
|
| 580 |
'#type' => 'textfield',
|
| 581 |
'#title' => t('The number of characters in at which to slice'),
|
| 582 |
'#description' => t('The number of characters in at which to slice. Negative numbers will slice from the end. 0 doesn\'t do anything, 1 slices off the first character, and -1 slices off everything except the last character.'),
|
| 583 |
'#default_value' => isset($settings['substr_start']) ? $settings['substr_start'] : '0',
|
| 584 |
);
|
| 585 |
$form['substr_length'] = array(
|
| 586 |
'#type' => 'textfield',
|
| 587 |
'#title' => t('The maximum number of characters to allow in this slice.'),
|
| 588 |
'#description' => t('The number of characters in at which to slice. Negative numbers will slice from the end. Leave this empty for it not to do anything, 1 allows only one character, and -1 takes off one character from the end.'),
|
| 589 |
'#default_value' => isset($settings['substr_length']) ? $settings['substr_length'] : '',
|
| 590 |
);
|
| 591 |
return $form;
|
| 592 |
|
| 593 |
case 'prepare':
|
| 594 |
case 'process':
|
| 595 |
$substr_start = isset($settings['substr_start']) ? $settings['substr_start'] : '0';
|
| 596 |
$substr_length = isset($settings['substr_length']) ? $settings['substr_length'] : '';
|
| 597 |
if ($substr_length === '') {
|
| 598 |
return substr($text, $substr_start);
|
| 599 |
}
|
| 600 |
return substr($text, $substr_start, $substr_length);
|
| 601 |
}
|
| 602 |
}
|
| 603 |
|
| 604 |
/**
|
| 605 |
* Flexifilter component callback.
|
| 606 |
* Allows user to enter php code to be evaluated during filtering process. (So somewhat safe as in it won't be evaluated on every page load).
|
| 607 |
*/
|
| 608 |
function flexifilter_component_advanced_php($op, $settings, $text) {
|
| 609 |
switch ($op) {
|
| 610 |
case 'settings':
|
| 611 |
$form = array();
|
| 612 |
$form['code'] = array(
|
| 613 |
'#type' => 'textarea',
|
| 614 |
'#title' => t('PHP code to be evaluated.'),
|
| 615 |
'#description' => t('Enter the code to evaluate. Does not need <?php ?> tags. Available variables are <em>$text</em>, which contains the text, and <em>$op</em>, which contains the operation, which will either be "prepare" or "process". This should return the text. WARNING: Evaluating incorrect PHP code can break your site.'),
|
| 616 |
'#default_value' => isset($settings['code']) ? $settings['code'] : 'return $text;',
|
| 617 |
);
|
| 618 |
return $form;
|
| 619 |
|
| 620 |
case 'prepare':
|
| 621 |
case 'process':
|
| 622 |
$code = isset($settings['code']) ? $settings['code'] : 'return $text;';
|
| 623 |
return eval($code);
|
| 624 |
}
|
| 625 |
}
|
| 626 |
|
| 627 |
/**
|
| 628 |
* Flexifilter component callback.
|
| 629 |
* Appends text based on the text passed back from the text passed in to the components which represents the entire text passed into this component.
|
| 630 |
*/
|
| 631 |
function flexifilter_component_advanced_append($op, $settings, $text) {
|
| 632 |
switch ($op) {
|
| 633 |
case 'settings':
|
| 634 |
$form = array();
|
| 635 |
return $form;
|
| 636 |
|
| 637 |
case 'prepare':
|
| 638 |
case 'process':
|
| 639 |
$append = flexifilter_invoke_components($settings['components'], $op, $text);
|
| 640 |
return $text . $append;
|
| 641 |
}
|
| 642 |
}
|
| 643 |
|
| 644 |
/**
|
| 645 |
* Flexifilter component callback.
|
| 646 |
* Prepends text based on the text passed back from the text passed in to the components which represents the entire text passed into this component.
|
| 647 |
*/
|
| 648 |
function flexifilter_component_advanced_prepend($op, $settings, $text) {
|
| 649 |
switch ($op) {
|
| 650 |
case 'settings':
|
| 651 |
$form = array();
|
| 652 |
return $form;
|
| 653 |
|
| 654 |
case 'prepare':
|
| 655 |
case 'process':
|
| 656 |
$prepend = flexifilter_invoke_components($settings['components'], $op, $text);
|
| 657 |
return $prepend . $text;
|
| 658 |
}
|
| 659 |
}
|
| 660 |
|
| 661 |
/**
|
| 662 |
* Basic sequence callback.
|
| 663 |
*/
|
| 664 |
function flexifilter_component_sequences($op, $settings, $text) {
|
| 665 |
switch ($op) {
|
| 666 |
case 'settings':
|
| 667 |
$form = array();
|
| 668 |
$form['text'] = array(
|
| 669 |
'#type' => 'textfield',
|
| 670 |
'#title' => t('Text to replace'),
|
| 671 |
'#description' => t('Enter the text to replace. Leave blank for simple insertion.'),
|
| 672 |
'#default_value' => isset($settings['text']) ? $settings['text'] : '',
|
| 673 |
);
|
| 674 |
$form['sequence'] = array(
|
| 675 |
'#type' => 'radios',
|
| 676 |
'#title' => t('Sequence type'),
|
| 677 |
'#options' => module_invoke_all('flexifilter_sequences'),
|
| 678 |
'#default_value' => 'flexifilter_sequence_numbers',
|
| 679 |
);
|
| 680 |
return $form;
|
| 681 |
|
| 682 |
case 'prepare':
|
| 683 |
case 'process':
|
| 684 |
static $_flexifilter_sequence_count = 0;
|
| 685 |
if (!empty($settings['text'])) {
|
| 686 |
while (flexifilter_sequences_replace_once($settings['text'], call_user_func($settings['sequence'], $_flexifilter_sequence_count), $text)) {
|
| 687 |
$_flexifilter_sequence_count++;
|
| 688 |
}
|
| 689 |
return $text;
|
| 690 |
}
|
| 691 |
return call_user_func($settings['sequence'], $_flexifilter_sequence_count);
|
| 692 |
}
|
| 693 |
}
|
| 694 |
|
| 695 |
|
| 696 |
function flexifilter_sequences_replace_once($search, $replace, &$subject) {
|
| 697 |
if (($pos = strpos($subject, $search)) !== FALSE) {
|
| 698 |
$subject = substr($subject, 0, $pos) . $replace . substr($subject, $pos + strlen($search));
|
| 699 |
return TRUE;
|
| 700 |
}
|
| 701 |
return FALSE;
|
| 702 |
}
|
| 703 |
|
| 704 |
/**
|
| 705 |
* Implementation of hook_flexifilter_sequences.
|
| 706 |
*/
|
| 707 |
function flexifilter_flexifilter_sequences() {
|
| 708 |
return array(
|
| 709 |
'flexifilter_sequence_numbers' => t('Simple numbers'),
|
| 710 |
'flexifilter_sequence_letters_uc' => t('Letters, upper case'),
|
| 711 |
'flexifilter_sequence_letters_lc' => t('Letters, lower case'),
|
| 712 |
);
|
| 713 |
}
|
| 714 |
|
| 715 |
/**
|
| 716 |
* Sequence callback: simple numbers.
|
| 717 |
*/
|
| 718 |
function flexifilter_sequence_numbers($num) {
|
| 719 |
return $num + 1;
|
| 720 |
}
|
| 721 |
|
| 722 |
function flexifilter_sequence_letters_uc($num) {
|
| 723 |
return strtoupper(flexifilter_sequence_letters_lc($num));
|
| 724 |
}
|
| 725 |
|
| 726 |
function flexifilter_sequence_letters_lc($num) {
|
| 727 |
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
|
| 728 |
return $letters[$num % 26];
|
| 729 |
}
|