2013-05-31 17:50:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
App::uses('Regexp', 'Model');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Behavior to regexp all string fields in a model
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class RegexpBehavior extends ModelBehavior {
|
2015-09-07 10:06:34 +02:00
|
|
|
|
|
|
|
private $__allRegexp = array();
|
|
|
|
|
|
|
|
public function setup(Model $model, $config = null) {
|
|
|
|
$regexp = new Regexp();
|
|
|
|
$this->__allRegexp = $regexp->find('all');
|
|
|
|
}
|
2013-05-31 17:50:00 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-08 17:15:04 +02:00
|
|
|
* replace the current value according to the regexp rules, or block blacklisted regular expressions
|
2013-05-31 17:50:00 +02:00
|
|
|
*
|
|
|
|
* @param Model $Model
|
|
|
|
* @param unknown_type $array
|
|
|
|
*/
|
2013-07-08 17:15:04 +02:00
|
|
|
public function runRegexp(Model $Model, $type, $value) {
|
2015-09-07 10:06:34 +02:00
|
|
|
foreach ($this->__allRegexp as $regexp) {
|
2013-07-08 17:15:04 +02:00
|
|
|
if (!empty($regexp['Regexp']['replacement']) && !empty($regexp['Regexp']['regexp']) && ($regexp['Regexp']['type'] === 'ALL' || $regexp['Regexp']['type'] === $type)) {
|
|
|
|
$value = preg_replace($regexp['Regexp']['regexp'], $regexp['Regexp']['replacement'], $value);
|
2013-05-31 17:50:00 +02:00
|
|
|
}
|
2013-07-08 17:15:04 +02:00
|
|
|
if (empty($regexp['Regexp']['replacement']) && preg_match($regexp['Regexp']['regexp'], $value) && ($regexp['Regexp']['type'] === 'ALL' || $regexp['Regexp']['type'] === $type)) {
|
2013-05-31 17:50:00 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-07-08 17:15:04 +02:00
|
|
|
return $value;
|
2013-05-31 17:50:00 +02:00
|
|
|
}
|
|
|
|
}
|