$this->Flash->success(__('The Regexp has been saved.'));
$this->redirect(array('action'=>'index'));
}else{
if(!($this->Session->check('Message.flash'))){
$this->Flash->error(__('The Regexp could not be saved. Please, try again.'));
}
}
}else{
$success=false;
foreach($typesas$key=>$type){
if($this->request->data['Regexp'][$key]==1){
$this->Regexp->create();
$this->request->data['Regexp']['type']=$type;
$this->Regexp->save($this->request->data);
$success=true;
}
}
if($success){
$this->Flash->success(__('The Regular expressions have been saved.'));
$this->redirect(array('action'=>'index'));
}else{
$this->Flash->error(__('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.'));
// 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.
// 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)){
$this->Regexp->find_similar($id,true);
$this->Flash->success('The Regexp has been saved');
$this->redirect(array('action'=>'index'));
}else{
if(!($this->Session->check('Message.flash'))){
$this->Flash->error('The Regexp could not be saved. Please, try again.');
}
}
}else{
// Keep track of which types could not be entered
$failcount=0;
$oldArray=$this->Regexp->find_similar($id);
$success=false;
foreach($typesas$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;
$success=true;
// Add to the failcount if the save fails. Ideally this should be 0
if(!$this->Regexp->save($this->request->data)){
$failcount++;
}
}
}
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($oldArrayas$old){
$this->Regexp->delete($old[0]);
}
$this->Flash->success(__('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->Flash->error('There were issues saving all of the regexp entries, therefore the old entries were not deleted.');
}
}else{
$this->Flash->error(__('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.'));
// 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
// 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++;
}
}
}
}
if(count($deletable)){
foreach($deletableas$item){
$this->Attribute->delete($item);
}
}
$this->Flash->info(__('All done! Number of changed attributes: '.$modifications.' Number of deletions: '.count($deletable)));