2012-11-30 13:52:09 +01:00
< ? php
2013-01-04 15:49:52 +01:00
2012-11-30 13:52:09 +01:00
App :: uses ( 'AppController' , 'Controller' );
2013-01-04 15:49:52 +01:00
2012-11-30 13:52:09 +01:00
/**
2013-01-04 15:49:52 +01:00
* Regexps Controller
2012-11-30 13:52:09 +01:00
*
2013-01-04 15:49:52 +01:00
* @ property Regexp $Regexp
2012-11-30 13:52:09 +01:00
*/
2012-12-20 19:47:38 +01:00
class RegexpController extends AppController {
2012-11-30 13:52:09 +01:00
2013-01-04 15:49:52 +01:00
public $components = array ( 'Security' , 'RequestHandler' , 'AdminCrud' );
2012-11-30 13:52:09 +01:00
2012-12-19 02:48:53 +01:00
public $paginate = array (
'limit' => 60 ,
'order' => array (
2012-12-20 19:47:38 +01:00
'Regexp.id' => 'ASC'
2012-12-19 02:48:53 +01:00
)
);
2012-12-19 11:30:10 +01:00
2012-12-19 02:48:53 +01:00
public function beforeFilter () {
parent :: beforeFilter ();
}
2012-11-30 13:52:09 +01:00
/**
2013-01-04 15:49:52 +01:00
* admin_add method
2012-11-30 13:52:09 +01:00
*
* @ return void
*/
2013-01-04 15:49:52 +01:00
public function admin_add () {
2013-07-08 17:15:04 +02:00
$this -> loadModel ( 'Attribute' );
$types = array_keys ( $this -> Attribute -> typeDefinitions );
2013-10-03 11:45:27 +02:00
if ( ! $this -> userRole [ 'perm_regexp_access' ]) $this -> redirect ( array ( 'controller' => 'regexp' , 'action' => 'index' , 'admin' => false ));
2013-07-08 17:15:04 +02:00
if ( $this -> request -> is ( 'post' )) {
if ( $this -> request -> data [ 'Regexp' ][ 'all' ] == 1 ) {
$this -> Regexp -> create ();
if ( $this -> Regexp -> save ( $this -> request -> data )) {
$this -> Session -> setFlash ( __ ( 'The Regexp has been saved.' ));
$this -> redirect ( array ( 'action' => 'index' ));
} else {
if ( ! ( $this -> Session -> check ( 'Message.flash' ))) {
$this -> Session -> setFlash ( __ ( 'The Regexp could not be saved. Please, try again.' ));
}
}
} else {
2015-07-27 08:13:20 +02:00
$success = false ;
2013-07-08 17:15:04 +02:00
foreach ( $types as $key => $type ) {
if ( $this -> request -> data [ 'Regexp' ][ $key ] == 1 ) {
$this -> Regexp -> create ();
$this -> request -> data [ 'Regexp' ][ 'type' ] = $type ;
2013-07-11 13:43:36 +02:00
$this -> Regexp -> save ( $this -> request -> data );
2015-07-27 08:13:20 +02:00
$success = true ;
2013-07-08 17:15:04 +02:00
}
}
2015-07-27 08:13:20 +02:00
if ( $success ) {
$this -> Session -> setFlash ( __ ( 'The Regular expressions have been saved.' ));
$this -> redirect ( array ( 'action' => 'index' ));
} else {
$this -> Session -> setFlash ( __ ( 'Could not create the Regex entry as no types were selected. Either check "All" or check the types that you wish the Regex to affect.' ));
}
2013-07-08 17:15:04 +02:00
}
}
$this -> set ( 'types' , $types );
2012-11-30 13:52:09 +01:00
}
/**
2013-01-04 15:49:52 +01:00
* admin_index method
2012-11-30 13:52:09 +01:00
*
* @ return void
*/
2013-01-04 15:49:52 +01:00
public function admin_index () {
2013-10-03 11:45:27 +02:00
if ( ! $this -> userRole [ 'perm_regexp_access' ]) $this -> redirect ( array ( 'controller' => 'regexp' , 'action' => 'index' , 'admin' => false ));
2013-01-04 15:49:52 +01:00
$this -> AdminCrud -> adminIndex ();
2012-11-30 13:52:09 +01:00
}
/**
2013-01-04 15:49:52 +01:00
* admin_edit method
2012-11-30 13:52:09 +01:00
*
* @ param string $id
* @ return void
* @ throws NotFoundException
*/
public function admin_edit ( $id = null ) {
2013-07-08 17:15:04 +02:00
// unlike other edits, the new regexp edit will actually create copies of an entry and delete the old ones. The reason for this is that each regular expression can now
// have several entries for different types. For example, /127.0.0.1/ -> '' can be an entry for ip-src, ip-dst, but not url, meaning that the string 127.0.0.1 would be blocked
// for ip-src and ip-dst attribute entry, but not for url.
$this -> loadModel ( 'Attribute' );
$types = array_keys ( $this -> Attribute -> typeDefinitions );
// send the user away if he/she's no admin
2013-10-03 11:45:27 +02:00
if ( ! $this -> userRole [ 'perm_regexp_access' ]) $this -> redirect ( array ( 'controller' => 'regexp' , 'action' => 'index' , 'admin' => false ));
2013-07-08 17:15:04 +02:00
$this -> Regexp -> id = $id ;
if ( ! $this -> Regexp -> exists ()) {
throw new NotFoundException ( 'Invalid Regexp' );
}
if ( $this -> request -> is ( 'post' ) || $this -> request -> is ( 'put' )) {
unset ( $this -> request -> data [ 'Regexp' ][ 'id' ]);
// If 'all' is set, it overrides all other type settings. Create an attribute with the "all" setting and save it. Also, delete the original(s)
if ( $this -> request -> data [ 'Regexp' ][ 'all' ] == 1 ) {
$this -> Regexp -> create ();
$this -> request -> data [ 'Regexp' ][ 'type' ] = 'ALL' ;
if ( $this -> Regexp -> save ( $this -> request -> data )) {
2013-07-11 13:43:36 +02:00
$this -> Regexp -> find_similar ( $id , true );
2013-07-08 17:15:04 +02:00
$this -> Session -> setFlash ( 'The Regexp has been saved' );
$this -> redirect ( array ( 'action' => 'index' ));
} else {
if ( ! ( $this -> Session -> check ( 'Message.flash' ))) {
$this -> Session -> setFlash ( 'The Regexp could not be saved. Please, try again.' );
}
}
} else {
// Keep track of which types could not be entered
$failcount = 0 ;
2013-07-11 13:43:36 +02:00
$oldArray = $this -> Regexp -> find_similar ( $id );
2015-07-27 08:26:01 +02:00
$success = false ;
2013-07-08 17:15:04 +02:00
foreach ( $types as $key => $type ) {
// If the checkbox for this type was ticked, create an entry for it
if ( $this -> request -> data [ 'Regexp' ][ $key ] == 1 ) {
$this -> Regexp -> create ();
$this -> request -> data [ 'Regexp' ][ 'type' ] = $type ;
2015-07-27 08:26:01 +02:00
$success = true ;
2013-07-08 17:15:04 +02:00
// Add to the failcount if the save fails. Ideally this should be 0
if ( ! $this -> Regexp -> save ( $this -> request -> data )) {
$failcount ++ ;
}
}
}
2015-07-27 08:26:01 +02:00
if ( $success ) {
if ( $failcount == 0 ) {
// we have managed to successfully save all of the new attributes, time to run through the array containing all of the old entries and delete them.
foreach ( $oldArray as $old ) {
$this -> Regexp -> delete ( $old [ 0 ]);
}
$this -> Session -> setFlash ( __ ( 'The Regular expressions have been saved.' ));
$this -> redirect ( array ( 'action' => 'index' ));
} else {
// Since some insertions failed, don't delete the old entries. It's an edit that failed after all
$this -> Session -> setFlash ( 'There were issues saving all of the regexp entries, therefore the old entries were not deleted.' );
2013-07-08 17:15:04 +02:00
}
} else {
2015-07-27 08:26:01 +02:00
$this -> Session -> setFlash ( __ ( 'Could not create the Regex entry as no types were selected. Either check "All" or check the types that you wish the Regex to affect.' ));
foreach ( $this -> request -> data [ 'Regexp' ] as $k => $t ) {
if ( is_numeric ( $k )) $values [ $k ] = ( $t == '1' ) ? true : false ;
}
$this -> set ( 'all' , $this -> request -> data [ 'Regexp' ][ 'all' ] == '1' ? true : false );
$this -> set ( 'value' , $values );
2013-07-08 17:15:04 +02:00
}
}
} else {
// Show the user the regular expression entry that he/she is trying to edit, but also find all of the similar entries and mark the checkboxes appropriately
// Similar meaning entries with the same 'regexp' and 'replacement' fields but different types
$this -> request -> data [ 'Regexp' ][ 'id' ] = $id ;
$this -> request -> data = $this -> Regexp -> read ( null , $id );
2013-07-11 13:43:36 +02:00
$similarArray = $this -> Regexp -> find_similar ( $id );
2013-07-08 17:15:04 +02:00
$values = array ();
// all is set separately from the other check-boxes
if ( $this -> request -> data [ 'Regexp' ][ 'type' ] === 'ALL' ) $this -> set ( 'all' , true );
else $this -> set ( 'all' , false );
// set the checkboxes for each type
foreach ( $types as $key => $type ) {
$values [ $key ] = false ;
foreach ( $similarArray as $similar ) {
if ( $type === $similar [ 1 ]) $values [ $key ] = true ;
}
}
$this -> set ( 'value' , $values );
}
2015-07-27 08:26:01 +02:00
$this -> set ( 'types' , $types );
2013-07-08 17:15:04 +02:00
}
2012-11-30 13:52:09 +01:00
/**
2013-01-04 15:49:52 +01:00
* admin_delete method
2012-11-30 13:52:09 +01:00
*
* @ param string $id
* @ return void
* @ throws MethodNotAllowedException
* @ throws NotFoundException
*/
public function admin_delete ( $id = null ) {
2013-10-03 11:45:27 +02:00
if ( ! $this -> userRole [ 'perm_regexp_access' ]) $this -> redirect ( array ( 'controller' => 'regexp' , 'action' => 'index' , 'admin' => false ));
2013-01-04 15:49:52 +01:00
$this -> AdminCrud -> adminDelete ( $id );
2012-11-30 13:52:09 +01:00
}
2012-12-04 08:51:27 +01:00
2013-01-28 11:44:09 +01:00
/**
* index method
*
* @ return void
*/
public function index () {
$this -> recursive = 0 ;
2013-04-24 15:33:22 +02:00
$this -> set ( 'list' , $this -> paginate ());
2013-01-28 11:44:09 +01:00
}
2012-12-04 08:51:27 +01:00
/**
*
*/
public function admin_clean () {
2015-11-13 23:57:03 +01:00
if ( ! $this -> _isSiteAdmin () || ! $this -> request -> is ( 'post' )) throw new MethodNotAllowedException ( 'This action is only accessible via a POST request.' );
2013-07-04 15:45:11 +02:00
$allRegexp = $this -> Regexp -> find ( 'all' );
2013-01-04 15:49:52 +01:00
$deletable = array ();
2013-07-08 17:15:04 +02:00
$modifications = 0 ;
$this -> loadModel ( 'Attribute' );
$all = $this -> Attribute -> find ( 'all' , array ( 'recursive' => - 1 ));
2013-01-04 15:49:52 +01:00
foreach ( $all as $item ) {
2013-07-11 13:43:36 +02:00
$result = $this -> Regexp -> replaceSpecific ( $item [ 'Attribute' ][ 'value' ], $allRegexp , $item [ 'Attribute' ][ 'type' ]);
2013-07-08 17:15:04 +02:00
// 0 = delete it, it is a blocked regexp; 1 = ran regexp check, made changes, resave this attribute with the new value; 2 = ran regexp check, no changes made, go on
if ( $result == 0 ) $deletable [] = $item [ 'Attribute' ][ 'id' ];
else {
// Until now this wasn't checked and all attributes were resaved, no matter if they were changed...
if ( $result == 1 ) {
$this -> Attribute -> save ( $item );
$modifications ++ ;
}
2012-12-04 08:51:27 +01:00
}
}
2013-01-04 15:49:52 +01:00
if ( count ( $deletable )) {
foreach ( $deletable as $item ) {
2013-07-08 17:15:04 +02:00
$this -> Attribute -> delete ( $item );
2012-12-04 08:51:27 +01:00
}
}
2013-07-08 17:15:04 +02:00
$this -> Session -> setFlash ( __ ( 'All done! Number of changed attributes: ' . $modifications . ' Number of deletions: ' . count ( $deletable )));
$this -> redirect ( array ( 'action' => 'index' ));
2012-12-04 08:51:27 +01:00
}
2015-11-16 19:47:31 +01:00
public function cleanRegexModifiers () {
if ( ! $this -> _isSiteAdmin () || ! $this -> request -> is ( 'post' )) throw new MethodNotAllowedException ();
$entries = $this -> Regexp -> find ( 'all' , array ());
$changes = 0 ;
foreach ( $entries as $entry ) {
$length = strlen ( $entry [ 'Regexp' ][ 'regexp' ]);
$this -> Regexp -> sanitizeModifiers ( $entry [ 'Regexp' ][ 'regexp' ]);
if ( strlen ( $entry [ 'Regexp' ][ 'regexp' ]) < $length ) {
$this -> Regexp -> save ( $entry );
$changes ++ ;
}
}
$this -> Session -> setFlash ( __ ( 'All done! Found and cleaned ' . $changes . ' potentially malcious regexes.' ));
$this -> redirect ( '/pages/display/administration' );
}
2012-12-04 08:51:27 +01:00
}