View source
<?php
namespace Drupal\customfilter\Plugin\Filter;
use Drupal\customfilter\Entity\CustomFilter;
use Drupal\filter\Plugin\FilterBase;
use Drupal\filter\FilterProcessResult;
class CustomFilterBaseFilter extends FilterBase {
public function process($text, $langcode) {
if (empty($text)) {
return '';
}
$entity = CustomFilter::load($this->settings['id']);
$globals =& static::getGlobals('push');
$globals->text = $text;
$rules = $entity
->getRulesTree();
if (is_array($rules) && count($rules)) {
static::getCodeVars(TRUE);
$globals->stack = array();
foreach ($rules as $rule) {
if ($rule['enabled']) {
$globals->stack[] = $rule;
$globals->text = preg_replace_callback($rule['pattern'], [
CustomFilterBaseFilter::class,
'applyRules',
], $globals->text);
array_pop($globals->stack);
}
}
}
$text = $globals->text;
static::getGlobals('pop');
$result = new FilterProcessResult($text);
$cache_tags = array(
'customfilter:' . $this->settings['id'],
);
$result
->addCacheTags($cache_tags);
return $result;
}
public function tips($long = FALSE) {
$entity = CustomFilter::load($this->settings['id']);
if ($long) {
return $entity
->getLongtip();
}
else {
return $entity
->getShorttip();
}
}
public static function applyRules(array $matches) {
$globals =& static::getGlobals();
$result = $matches[0];
$rule = end($globals->stack);
$code = $rule['code'];
$pattern = $rule['pattern'];
$replacement = $rule['replacement'];
if (is_array($sub = $rule['sub']) && count($sub)) {
foreach ($sub as $subrule) {
if ($subrule['enabled']) {
$globals->stack[] = $subrule;
$substr =& $matches[$subrule['matches']];
$substr = preg_replace_callback($subrule['pattern'], [
CustomFilterBaseFilter::class,
'applyRules',
], $substr);
array_pop($globals->stack);
}
}
if ($code) {
CustomFilterBaseFilter::replaceCallback($replacement, TRUE);
$result = CustomFilterBaseFilter::replaceCallback($matches);
}
else {
$result = $replacement;
$rmatches = array();
$reps = array();
preg_match_all('/([^\\\\]|^)(\\$([0-9]{1,2}|\\{([0-9]{1,2})\\}))/', $replacement, $rmatches, PREG_OFFSET_CAPTURE);
foreach ($rmatches[4] as $key => $val) {
if ($val == '') {
$index = $rmatches[3][$key][0];
}
else {
$index = $rmatches[4][$key][0];
}
$offset = $rmatches[2][$key][1];
$length = strlen($rmatches[2][$key][0]);
$reps[] = array(
'index' => $index,
'offset' => $offset,
'length' => $length,
);
}
krsort($reps);
foreach ($reps as $rep) {
$result = substr_replace($result, $matches[$rep['index']], $rep['offset'], $rep['length']);
}
}
}
elseif ($code) {
CustomFilterBaseFilter::replaceCallback($replacement, TRUE);
$result = preg_replace_callback($pattern, [
CustomFilterBaseFilter::class,
'replaceCallback',
], $result);
}
else {
$result = preg_replace($pattern, $replacement, $result);
}
return $result;
}
public static function replaceCallback($matches, $init = FALSE) {
static $code;
if ($init) {
$code = $matches;
return;
}
$vars =& static::getCodeVars();
@eval($code);
return isset($result) ? $result : '';
}
public static function &getCodeVars($reset = FALSE) {
static $vars;
if (!isset($vars) || $reset) {
$vars = new \stdClass();
}
return $vars;
}
public static function &getGlobals($op = '') {
static $globals = array(), $index = 0;
if ($op == 'push') {
$globals[++$index] = new \stdClass();
}
elseif ($op == 'pop' && $index) {
unset($globals[$index--]);
}
return $globals[$index];
}
}