コード例 #1
0
 /**
  * Exports a value back into the request.
  *
  * Exports data into the request at the index given in the parameter
  * 'export'. If there is no such parameter, then the method returns
  * without exporting.
  *
  * Similar to getData() you should always use export() to submit data to
  * the request because it pays attention to paths and otherwise you could
  * overwrite stuff you don't want to.
  *
  * @param      mixed The value to be exported.
  * @param      string An optional name which should be used for exporting 
  *                    instead of the export parameter.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 protected function export($value, $name = null)
 {
     if ($name === null) {
         $name = $this->getParameter('export');
     }
     if (!is_string($name) || $name === '') {
         return;
     }
     $paramType = $this->getParameter('source');
     $array =& $this->validationParameters->getAll($paramType);
     $currentParts = $this->curBase->getParts();
     if (count($currentParts) > 0 && strpos($name, '%') !== false) {
         // this is a validator which actually has a base (<arguments base="xx">) set
         // and the export name contains sprintf syntax
         $name = vsprintf($name, $currentParts);
     }
     // CAUTION
     // we had a feature here during development that would allow [] at the end to append values to an array
     // that would, however, mean that we have to cast the value to an array, and, either way, a user would be able to manipulate the keys
     // example: we export to foo[], and the user supplies ?foo[28] in the URL. that means our export will be in foo[29]. foo[28] will be removed by the validation, but the keys are still potentially harmful
     // that's why we decided to remove this again
     $cp = new AgaviVirtualArrayPath($name);
     $cp->setValue($array, $value);
     if ($this->parentContainer !== null) {
         // make sure the parameter doesn't get removed by the validation manager
         if (is_array($value)) {
             // for arrays all child elements need to be marked as not processed
             foreach (AgaviArrayPathDefinition::getFlatKeyNames($value) as $keyName) {
                 $this->parentContainer->addArgumentResult(new AgaviValidationArgument($cp->pushRetNew($keyName)->__toString(), $this->getParameter('source')), AgaviValidator::SUCCESS, $this);
             }
         }
         $this->parentContainer->addArgumentResult(new AgaviValidationArgument($cp->__toString(), $this->getParameter('source')), AgaviValidator::SUCCESS, $this);
     }
 }
コード例 #2
0
 /**
  * Retrieve an array of flattened file names. This means when a file is an
  * array you wont get the name of the file in the result but instead all child
  * keys appended to the name (like foo[0],foo[1][0], ...).
  *
  * @return     array An indexed array of file names.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.0
  */
 public function getFlatFileNames()
 {
     return AgaviArrayPathDefinition::getFlatKeyNames($this->files);
 }
コード例 #3
0
 /**
  * Retrieve an array of flattened attribute names. This means if an attribute
  * is an array you wont get the name of the attribute in the result but 
  * instead all child keys appended to the name (like foo[0],foo[1][0], ...)
  *
  * @param      string An attribute namespace.
  *
  * @return     array An indexed array of attribute names, if the namespace
  *                   exists, otherwise null.
  *
  * @author     David Zülke <*****@*****.**>
  * @since      0.11.3
  */
 public function getFlatAttributeNames($ns = null)
 {
     if ($ns === null) {
         $ns = $this->defaultNamespace;
     }
     if (isset($this->attributes[$ns])) {
         return AgaviArrayPathDefinition::getFlatKeyNames($this->attributes[$ns]);
     }
 }
コード例 #4
0
 /**
  * Starts the validation process.
  *
  * @param      AgaviRequestDataHolder The data which should be validated.
  *
  * @return     bool true, if validation succeeded.
  *
  * @author     Uwe Mesecke <*****@*****.**>
  * @since      0.11.0
  */
 public function execute(AgaviRequestDataHolder $parameters)
 {
     $success = true;
     $this->report = new AgaviValidationReport();
     $result = AgaviValidator::SUCCESS;
     $req = $this->context->getRequest();
     $executedValidators = 0;
     foreach ($this->children as $validator) {
         ++$executedValidators;
         $validatorResult = $validator->execute($parameters);
         $result = max($result, $validatorResult);
         switch ($validatorResult) {
             case AgaviValidator::SUCCESS:
                 continue 2;
             case AgaviValidator::INFO:
                 continue 2;
             case AgaviValidator::SILENT:
                 continue 2;
             case AgaviValidator::NOTICE:
                 continue 2;
             case AgaviValidator::ERROR:
                 $success = false;
                 continue 2;
             case AgaviValidator::CRITICAL:
                 $success = false;
                 break 2;
         }
     }
     $this->report->setResult($result);
     $ma = $req->getParameter('module_accessor');
     $aa = $req->getParameter('action_accessor');
     $umap = $req->getParameter('use_module_action_parameters');
     $mode = $this->getParameter('mode');
     if ($executedValidators == 0 && $mode == self::MODE_STRICT) {
         // strict mode and no validators executed -> clear the parameters
         if ($umap) {
             $maParam = $parameters->getParameter($ma);
             $aaParam = $parameters->getParameter($aa);
         }
         $parameters->clearAll();
         if ($umap) {
             if ($maParam) {
                 $parameters->setParameter($ma, $maParam);
             }
             if ($aaParam) {
                 $parameters->setParameter($aa, $aaParam);
             }
         }
     }
     if ($mode == self::MODE_STRICT || $executedValidators > 0 && $mode == self::MODE_CONDITIONAL) {
         // first, we explicitly unset failed arguments
         // the primary purpose of this is to make sure that arrays that failed validation themselves (e.g. due to array length validation, or due to use of operator validators with an argument base) are removed
         // that's of course only necessary if validation failed
         $failedArguments = $this->report->getFailedArguments();
         foreach ($failedArguments as $argument) {
             $parameters->remove($argument->getSource(), $argument->getName());
         }
         // next, we remove all arguments from the request data that are not in the list of succeeded arguments
         // this will also remove any arguments that didn't have validation rules defined
         $succeededArguments = $this->report->getSucceededArguments();
         foreach ($parameters->getSourceNames() as $source) {
             $sourceItems = $parameters->getAll($source);
             foreach (AgaviArrayPathDefinition::getFlatKeyNames($sourceItems) as $name) {
                 if (!isset($succeededArguments[$source . '/' . $name]) && (!$umap || ($source != AgaviRequestDataHolder::SOURCE_PARAMETERS || $name != $ma && $name != $aa))) {
                     $parameters->remove($source, $name);
                 }
             }
         }
     }
     return $success;
 }
コード例 #5
0
 /**
  * Returns the flat key names of an array.
  *
  * This method calls itself recursively to flatten the keys.
  *
  * @param      array The array which keys should be returned.
  * @param      string The prefix for the name (only for internal use).
  *
  * @return     array The flattened keys.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 public static function getFlatKeyNames(array $array, $prefix = null)
 {
     $names = array();
     foreach ($array as $key => $value) {
         if ($prefix === null) {
             // create the top node when no prefix was given
             if (strlen($key) == 0) {
                 // when an empty key was used at top level, create a "relative" path, so the empty string doesn't get lost
                 $name = '[' . $key . ']';
             } else {
                 $name = $key;
             }
         } else {
             $name = $prefix . '[' . $key . ']';
         }
         if (is_array($value)) {
             $names = array_merge($names, AgaviArrayPathDefinition::getFlatKeyNames($value, $name));
         } else {
             $names[] = $name;
         }
     }
     return $names;
 }
コード例 #6
0
 /**
  * Retrieve an array of flattened parameter names. This means when a parameter
  * is an array you wont get the name of the parameter in the result but 
  * instead all child keys appended to the name (like foo[0],foo[1][0], ...)
  *
  * @return     array An indexed array of parameter names.
  *
  * @author     Dominik del Bondio <*****@*****.**>
  * @since      0.11.0
  */
 public function getFlatParameterNames()
 {
     return AgaviArrayPathDefinition::getFlatKeyNames($this->parameters);
 }