示例#1
0
 /**
  * @Security("has_role('ROLE_ADMIN')")
  */
 public function importAction()
 {
     /* Handle form. */
     $import = new WhitelistImport();
     $form = $this->createForm(new WhitelistImportType(), $import);
     $form->handleRequest($this->get('request'));
     /* Insert and redirect or show the form. */
     if ($form->isValid()) {
         $fileContent = file_get_contents($import->getFile()->getPathName());
         $rules = json_decode($fileContent, true);
         if ($rules) {
             $em = $this->getDoctrine()->getManager();
             foreach ($rules as $rule) {
                 $filterObj = $em->getRepository('SwdAnalyzerBundle:WhitelistFilter')->find($rule['filter']);
                 $ruleObj = new WhitelistRule();
                 $ruleObj->setProfile($import->getProfile());
                 $ruleObj->setPath($rule['path']);
                 $ruleObj->setCaller(str_replace('{BASE}', $import->getBase(), $rule['caller']));
                 $ruleObj->setMinLength($rule['min_length']);
                 $ruleObj->setMaxLength($rule['max_length']);
                 $ruleObj->setFilter($filterObj);
                 $ruleObj->setStatus(3);
                 $em->persist($ruleObj);
             }
             $em->flush();
             $this->get('session')->getFlashBag()->add('info', 'The rules were imported.');
         } else {
             $this->get('session')->getFlashBag()->add('alert', 'Invalid file.');
         }
         return $this->redirect($this->generateUrl('swd_analyzer_whitelist_rules'));
     } else {
         /* Render template. */
         return $this->render('SwdAnalyzerBundle:Whitelist:import.html.twig', array('form' => $form->createView()));
     }
 }
示例#2
0
 public function generateRules($settings)
 {
     if (!$this->statistics) {
         return;
     }
     foreach ($this->statistics as $caller => $caller_value) {
         foreach ($caller_value as $path => $stat) {
             /* Ignore slips. */
             if ($stat->getUniqueCounter() < $settings->getMinUniqueVisitors()) {
                 continue;
             }
             /* Create a new (pending) rule. */
             $rule = new WhitelistRule();
             $rule->setProfile($settings->getProfile());
             $rule->setPath($path);
             $rule->setCaller($caller);
             $rule->setStatus(3);
             $rule->setDate(new \DateTime());
             /**
              * If the variance is near zero we can use the average length as min and max length.
              * If the variance is a bit higher but no really high we can calculate a max length.
              * If the variance is too high it is not possible to set a min or max length.
              */
             if ($stat->getLengthVariance() < 1) {
                 $rule->setMinLength(ceil($stat->getAverageLength()));
                 $rule->setMaxLength(ceil($stat->getAverageLength()));
             } elseif ($stat->getLengthVariance() < 5) {
                 $rule->setMinLength($stat->getMinLength());
                 $rule->setMaxLength($stat->getMaxLength());
             } else {
                 $rule->setMinLength(-1);
                 $rule->setMaxLength(-1);
             }
             /**
              * Next we determine the filter. If almost every request used the same filter we can take that filter.
              * If this is not the case and there is no clear "winner" we have to take the "everything" filter.
              */
             $filter = $stat->getDominantFilter($settings->getMinFilterDominance());
             if ($filter === false) {
                 $everything = $this->em->getRepository('SwdAnalyzerBundle:WhitelistFilter')->findHighestImpact()->getSingleResult();
                 $rule->setFilter($everything);
             } else {
                 $rule->setFilter($filter);
             }
             /* Save rule in class attribute. */
             $this->rules[] = $rule;
         }
     }
 }