/** * Initialize this storage instance. * * Available options: * * table_name: string * * key_col: string column of the keys * * data_col: string column of the data * * additional_indexes: array(col=>value) list of additional indexes to use * Warning: NEVER use column names supplied by user. This class have some * safety measures against this type of injection, but it is not bulletproof. * * @param array $options An associative array of options * * @returns bool true, if initialization completes successfully */ public function initialize($options = array()) { parent::initialize($options); // $options are now available as $this->options $needed_options = array('table_name', 'key_col', 'data_col'); foreach ($needed_options as $needed) { if (!isset($this->options[$needed])) { throw new sfInitializationException('You must provide a "' . $needed . '" option to DatabaseStorage.'); } } // check required options Preconditions::checkIsString($this->options['table_name']); Preconditions::checkMatchesPattern(self::SAFE_TABLE_NAME, $this->options['table_name']); Preconditions::checkIsString($this->options['key_col']); Preconditions::checkMatchesPattern(self::SAFE_COLUMN_NAME, $this->options['key_col']); Preconditions::checkIsString($this->options['data_col']); Preconditions::checkMatchesPattern(self::SAFE_COLUMN_NAME, $this->options['data_col']); // check optional options if (isset($options['additional_indexes'])) { foreach ($options['additional_indexes'] as $name => $value) { Preconditions::checkIsString($name); Preconditions::checkMatchesPattern(self::SAFE_COLUMN_NAME, $name); } } }
/** * Invoke an action given its name * * This function checks if public non-abstract non-static runAction method * exists in this object and calls it in such a case with request and response * parameters * * @param Trace $trace trace object * @param string $action action name * @param Request $request incoming request */ public function invokeAction(Trace $trace, $action, Request $request) { Preconditions::checkIsString($action); Preconditions::checkMatchesPattern('@^[A-Z][a-zA-Z]*$@', $action, '$action must start with capital letter and ' . 'contain only letters.'); $methodName = 'run' . $action; if (!method_exists($this, $methodName)) { throw new Exception('Action method ' . $methodName . ' does not exist'); } $method = new ReflectionMethod($this, $methodName); if (!$method->isPublic()) { throw new Exception('Action method ' . $methodName . ' is not public'); } if ($method->isAbstract()) { throw new Exception('Action method ' . $methodName . ' is abstract'); } if ($method->isStatic()) { throw new Exception('Action method ' . $methodName . ' is static'); } if ($method->isConstructor()) { throw new Exception('Action method ' . $methodName . ' is constructor'); } if ($method->isDestructor()) { throw new Exception('Action method ' . $methodName . ' is destructor'); } return $method->invoke($this, $trace, $request); }
/** * Construct a CosignServiceCookie if all arguments are valid * * @param string $name Cookie name * @param string $value Cookie value * @param string $domain Cookie domain */ public function __construct($name, $value, $domain) { Preconditions::checkMatchesPattern(self::NAME_PATTERN, $name, '$name did not match allowed pattern.'); Preconditions::checkMatchesPattern(self::VALUE_PATTERN, $value, '$value did not match allowed pattern.'); Preconditions::checkMatchesPattern(self::DOMAIN_PATTERN, $domain, '$domain did not match allowed pattern.'); $this->name = $name; $this->value = $value; $this->domain = $domain; }
/** * Reads data from this storage. * Reading non-existent key will return null. * * @param string $key key to the data * * @returns mixed Data associated with the key * * @throws sfStorageException on failure */ public function read($key) { Preconditions::checkIsString($key); // basic sanity checking so there will be no binary-data tricks Preconditions::checkMatchesPattern('@^[a-zA-Z0-9._/]*$@', $key, 'Key contains invalid characters'); $path = $this->options['root_path'] . '/' . $key . '.dat'; if (!file_exists($path)) { return null; // empty read } $content = file_get_contents($path); if ($content === false) { throw new sfStorageException("Failed to read data for key '{$key}'"); } $value = @eval($content); if ($value === false) { $err = error_get_last(); throw new sfStorageException("Parse error while reading data for key '{$key}': " . htmlspecialchars('at line ' . $err['line'] . ': ' . $err['message'], null, 'UTF-8')); } if ($value === null) { throw new sfStorageException("No returned value while reading data for key '{$key}'"); } return $value; }
public function testIsStringAndMatchesFailOnNonMatch() { $this->setExpectedException("InvalidArgumentException"); $x = 'def'; Preconditions::checkMatchesPattern("/^abc\$/", $x, "not matching"); }