2013-03-07 15:19:55 +01:00
< ? php
App :: uses ( 'AppModel' , 'Model' );
/**
* Regexp Model
*
*/
2018-07-19 11:48:22 +02:00
class Regexp extends AppModel
{
public $actsAs = array (
2019-02-10 13:08:12 +01:00
'SysLogLogable.SysLogLogable' => array ( // TODO Audit, logable
2018-07-19 11:48:22 +02:00
'roleModel' => 'Role' ,
'roleKey' => 'role_id' ,
'change' => 'full'
),
);
2013-03-07 15:19:55 +01:00
2018-07-19 11:48:22 +02:00
public $validate = array (
'regexp' => array (
'rule' => 'checkRegexp' ,
'message' => 'This is not a valid regular expression. Don\'t forget the delimiters!'
)
);
2013-03-08 13:16:02 +01:00
2018-07-19 11:48:22 +02:00
public $useTable = 'regexp' ;
2013-07-11 13:43:36 +02:00
2018-07-19 11:48:22 +02:00
public function beforeValidate ( $options = array ())
{
$this -> sanitizeModifiers ( $this -> data [ 'Regexp' ][ 'regexp' ]);
}
2013-03-07 15:19:55 +01:00
2018-07-19 11:48:22 +02:00
public function sanitizeModifiers ( & $regex )
{
preg_match ( '/[a-zA-Z]*$/i' , $regex , $modifiers );
if ( ! empty ( $modifiers [ 0 ])) {
$modifier_length = strlen ( $modifiers [ 0 ]);
$regex = substr ( $regex , 0 , - $modifier_length );
$modifiers [ 0 ] = str_ireplace ( 'e' , '' , $modifiers [ 0 ]);
$regex .= $modifiers [ 0 ];
}
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
public function checkRegexp ()
{
if ( @ preg_replace ( $this -> data [ 'Regexp' ][ 'regexp' ], 'success' , $this -> data [ 'Regexp' ][ 'regexp' ]) != null ) {
return true ;
}
return false ;
}
2016-06-04 01:08:16 +02:00
2018-07-19 11:48:22 +02:00
// find all the similar Regular expressions and return them. If $delete is true, delete them instead of returning them.
public function find_similar ( $id , $delete = false )
{
$allRegexp = $this -> find ( 'all' );
$original = null ;
$finalArray = array ();
// Let's find and read the original so we know what to look for:
foreach ( $allRegexp as $k => $v ) {
if ( $v [ 'Regexp' ][ 'id' ] == $id ) {
$original = $v ;
}
}
// if we found the original, let's try to find all of the regexp values that match the original in the regexp and replacement fields.
// We should get a list of all the IDs (and their respective types) of regular expression entries that are duplicates created for various types.
2020-09-01 12:56:35 +02:00
// ip-src /127.0.0.1/ -> '' and ip-dst /127.0.0.1/ -> '' (entries that blocklists the ip-source and ip-destination addresses 127.0.0.1) will be returned when editing
2018-07-19 11:48:22 +02:00
// ip-src /127.0.0.1/ -> '', but other /127.0.0.1/ -> 'localhost' will not
if ( $original != null ) {
foreach ( $allRegexp as $k => $v ) {
if ( $original [ 'Regexp' ][ 'regexp' ] === $v [ 'Regexp' ][ 'regexp' ] && $original [ 'Regexp' ][ 'replacement' ] === $v [ 'Regexp' ][ 'replacement' ]) {
if ( $delete ) {
// if the delete parameter is set to true, delete the regular expression. This is used for edits
$this -> delete ( $v [ 'Regexp' ][ 'id' ]);
} else {
$finalArray [] = array ( $v [ 'Regexp' ][ 'id' ], $v [ 'Regexp' ][ 'type' ]);
}
}
}
}
return $finalArray ;
}
2013-07-11 13:43:36 +02:00
2018-07-19 11:48:22 +02:00
public function replaceSpecific ( $string , $allRegexp = null , $type )
{
$orig = $string ;
foreach ( $allRegexp as $regexp ) {
if ( strlen ( $regexp [ 'Regexp' ][ 'replacement' ]) && strlen ( $regexp [ 'Regexp' ][ 'regexp' ]) && ( $regexp [ 'Regexp' ][ 'type' ] === 'ALL' || $regexp [ 'Regexp' ][ 'type' ] === $type )) {
$string = preg_replace ( $regexp [ 'Regexp' ][ 'regexp' ], $regexp [ 'Regexp' ][ 'replacement' ], $string );
}
if ( ! strlen ( $regexp [ 'Regexp' ][ 'replacement' ]) && preg_match ( $regexp [ 'Regexp' ][ 'regexp' ], $string ) && ( $regexp [ 'Regexp' ][ 'type' ] === 'ALL' || $regexp [ 'Regexp' ][ 'type' ] === $type )) {
return 0 ;
}
}
if ( $orig === $string ) {
return 2 ;
}
return 1 ;
}
2016-06-06 10:09:55 +02:00
}