Exemplo n.º 1
0
 /**
  * @param array $config
  * @param null $name
  * @param string $storageRole
  * @return Context
  * @throws \Exception
  */
 public function buildFromArray(array $config, $name = null, $storageRole = Context::STORAGE_ROLE_CHILD)
 {
     $context = new Context($name, null, Context::TYPE_STANDARD, $storageRole);
     if (is_array($config['fields'])) {
         $fields = $config['fields'];
         // Iterate all fields and create a rule set
         foreach ($fields as $fieldName => $fieldData) {
             $vulnerabilities = $this->buildVulnerabilitySetFromArray($fieldData);
             $vulnElement = new VulnerableElement($vulnerabilities);
             // Add rule to the rule set
             $field = new Field($fieldName, $vulnElement, FieldDescriptor::SOURCE_ANY);
             $context->addField($field);
         }
     }
     foreach (['actions', 'contexts'] as $subContextType) {
         if (is_array($config[$subContextType])) {
             foreach ($config[$subContextType] as $contextName => $contextData) {
                 $child = $this->buildFromArray($contextData, $contextName);
                 $type = $subContextType == 'actions' ? Context::TYPE_ACTION : Context::TYPE_STANDARD;
                 $child->setType($type);
                 $context->addChild($child);
             }
         }
     }
     if (is_array($config['vulnerabilities'])) {
         $vulnerabilities = $this->buildVulnerabilitySetFromArray($config['vulnerabilities']);
         $vulnElement = new VulnerableElement($vulnerabilities);
         $context->setVulnTree($vulnElement);
     }
     return $context;
 }
Exemplo n.º 2
0
 /**
  * Dive into child context.
  *
  * @param $childName
  * @param bool $createIfNotExists
  * @return $this
  * @throws \Exception
  */
 public function goDown($childName, $createIfNotExists = true)
 {
     $context = $this->currentContext ?: $this->rootContext;
     $target = $context->getChildByName($childName);
     if ($target) {
         $this->currentContext = $target;
         $this->level++;
     } else {
         if ($createIfNotExists) {
             $target = new Context($childName);
             $this->currentContext->addChild($target);
             $target->setRequest($this->currentContext->getRequest());
             $this->currentContext = $target;
         } else {
             throw new \RuntimeException("No such context: \"{$childName}\"");
         }
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * @param array $config
  * @param null $name
  * @param string $storageRole
  * @return Context
  * @throws \Exception
  */
 public function buildContextFromArray(array $config, $name = null, $storageRole = Context::STORAGE_ROLE_CHILD)
 {
     $type = in_array($config['type'], Context::getTypes()) ? $config['type'] : Context::TYPE_STANDARD;
     $technology = in_array($config['technology'], Context::getTechnologies()) ? $config['technology'] : Context::TECH_GENERIC;
     $context = new Context($name, null, $type, $storageRole);
     $context->setTechnology($technology);
     $context->setMappedTo($config['mapped_to'] ?: '');
     if (is_array($config['fields'])) {
         $fields = $config['fields'];
         // Iterate all fields and create a rule set
         foreach ($fields as $fieldData) {
             $vulnElement = $this->buildVulnerabilityElementFromArray($fieldData['vulnerabilities']);
             $source = $fieldData['source'] ?: FieldDescriptor::SOURCE_ANY;
             if (!in_array($source, FieldDescriptor::getSources())) {
                 throw new \InvalidArgumentException("Invalid source for field '{$fieldData['name']}': " . $source);
             }
             $field = new Field($fieldData['name'], $vulnElement, $source);
             $context->addField($field);
         }
     }
     if (is_array($config['children'])) {
         foreach ($config['children'] as $contextName => $contextData) {
             $child = $this->buildContextFromArray($contextData, $contextName);
             $context->addChild($child);
         }
     }
     if (is_array($config['vulnerabilities'])) {
         $vulnElement = $this->buildVulnerabilityElementFromArray($config['vulnerabilities']);
         $context->setVulnTree($vulnElement);
     }
     return $context;
 }