/usr/share/php/kohana3.1/system/classes/kohana/validation.php is in libkohana3.1-core-php 3.1.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 | <?php defined('SYSPATH') or die('No direct script access.');
/**
* Array and variable validation.
*
* @package Kohana
* @category Security
* @author Kohana Team
* @copyright (c) 2008-2011 Kohana Team
* @license http://kohanaframework.org/license
*/
class Kohana_Validation extends ArrayObject {
/**
* Creates a new Validation instance.
*
* @param array array to use for validation
* @return Validation
*/
public static function factory(array $array)
{
return new Validation($array);
}
// Bound values
protected $_bound = array();
// Field rules
protected $_rules = array();
// Field labels
protected $_labels = array();
// Rules that are executed even when the value is empty
protected $_empty_rules = array('not_empty', 'matches');
// Error list, field => rule
protected $_errors = array();
/**
* Sets the unique "any field" key and creates an ArrayObject from the
* passed array.
*
* @param array array to validate
* @return void
*/
public function __construct(array $array)
{
parent::__construct($array, ArrayObject::STD_PROP_LIST);
}
/**
* Copies the current rule to a new array.
*
* $copy = $array->copy($new_data);
*
* @param array new data set
* @return Validation
* @since 3.0.5
*/
public function copy(array $array)
{
// Create a copy of the current validation set
$copy = clone $this;
// Replace the data set
$copy->exchangeArray($array);
return $copy;
}
/**
* Returns the array representation of the current object.
*
* @return array
*/
public function as_array()
{
return $this->getArrayCopy();
}
/**
* Sets or overwrites the label name for a field.
*
* @param string field name
* @param string label
* @return $this
*/
public function label($field, $label)
{
// Set the label for this field
$this->_labels[$field] = $label;
return $this;
}
/**
* Sets labels using an array.
*
* @param array list of field => label names
* @return $this
*/
public function labels(array $labels)
{
$this->_labels = $labels + $this->_labels;
return $this;
}
/**
* Overwrites or appends rules to a field. Each rule will be executed once.
* All rules must be string names of functions method names. Parameters must
* match the parameters of the callback function exactly
*
* Aliases you can use in callback parameters:
* - :validation - the validation object
* - :field - the field name
* - :value - the value of the field
*
* // The "username" must not be empty and have a minimum length of 4
* $validation->rule('username', 'not_empty')
* ->rule('username', 'min_length', array('username', 4));
*
* // The "password" field must match the "password_repeat" field
* $validation->rule('password', 'matches', array(':validation', 'password', 'password_repeat'));
*
* @param string field name
* @param callback valid PHP callback
* @param array extra parameters for the rule
* @return $this
*/
public function rule($field, $rule, array $params = NULL)
{
if ($params === NULL)
{
// Default to array(':value')
$params = array(':value');
}
if ($field !== TRUE AND ! isset($this->_labels[$field]))
{
// Set the field label to the field name
$this->_labels[$field] = preg_replace('/[^\pL]+/u', ' ', $field);
}
// Store the rule and params for this rule
$this->_rules[$field][] = array($rule, $params);
return $this;
}
/**
* Add rules using an array.
*
* @param string field name
* @param array list of callbacks
* @return $this
*/
public function rules($field, array $rules)
{
foreach ($rules as $rule)
{
$this->rule($field, $rule[0], Arr::get($rule, 1));
}
return $this;
}
/**
* Bind a value to a parameter definition.
*
* // This allows you to use :model in the parameter definition of rules
* $validation->bind(':model', $model)
* ->rule('status', 'valid_status', array(':model'));
*
* @param string variable name or an array of variables
* @param mixed value
* @return $this
*/
public function bind($key, $value = NULL)
{
if (is_array($key))
{
foreach ($key as $name => $value)
{
$this->_bound[$name] = $value;
}
}
else
{
$this->_bound[$key] = $value;
}
return $this;
}
/**
* Executes all validation rules. This should
* typically be called within an if/else block.
*
* if ($validation->check())
* {
* // The data is valid, do something here
* }
*
* @param boolean allow empty array?
* @return boolean
*/
public function check()
{
if (Kohana::$profiling === TRUE)
{
// Start a new benchmark
$benchmark = Profiler::start('Validation', __FUNCTION__);
}
// New data set
$data = $this->_errors = array();
// Store the original data because this class should not modify it post-validation
$original = $this->getArrayCopy();
// Get a list of the expected fields
$expected = Arr::merge(array_keys($original), array_keys($this->_labels));
// Import the rules locally
$rules = $this->_rules;
foreach ($expected as $field)
{
// Use the submitted value or NULL if no data exists
$data[$field] = Arr::get($this, $field);
if (isset($rules[TRUE]))
{
if ( ! isset($rules[$field]))
{
// Initialize the rules for this field
$rules[$field] = array();
}
// Append the rules
$rules[$field] = array_merge($rules[$field], $rules[TRUE]);
}
}
// Overload the current array with the new one
$this->exchangeArray($data);
// Remove the rules that apply to every field
unset($rules[TRUE]);
// Bind the validation object to :validation
$this->bind(':validation', $this);
// Execute the rules
foreach ($rules as $field => $set)
{
// Get the field value
$value = $this[$field];
// Bind the field name and value to :field and :value respectively
$this->bind(array
(
':field' => $field,
':value' => $value,
));
foreach ($set as $array)
{
// Rules are defined as array($rule, $params)
list($rule, $params) = $array;
foreach ($params as $key => $param)
{
if (is_string($param) AND array_key_exists($param, $this->_bound))
{
// Replace with bound value
$params[$key] = $this->_bound[$param];
}
}
// Default the error name to be the rule (except array and lambda rules)
$error_name = $rule;
if (is_array($rule))
{
// This is an array callback, the method name is the error name
$error_name = $rule[1];
$passed = call_user_func_array($rule, $params);
}
elseif ( ! is_string($rule))
{
// This is a lambda function, there is no error name (errors must be added manually)
$error_name = FALSE;
$passed = call_user_func_array($rule, $params);
}
elseif (method_exists('Valid', $rule))
{
// Use a method in this object
$method = new ReflectionMethod('Valid', $rule);
// Call static::$rule($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
elseif (strpos($rule, '::') === FALSE)
{
// Use a function call
$function = new ReflectionFunction($rule);
// Call $function($this[$field], $param, ...) with Reflection
$passed = $function->invokeArgs($params);
}
else
{
// Split the class and method of the rule
list($class, $method) = explode('::', $rule, 2);
// Use a static method call
$method = new ReflectionMethod($class, $method);
// Call $Class::$method($this[$field], $param, ...) with Reflection
$passed = $method->invokeArgs(NULL, $params);
}
// Ignore return values from rules when the field is empty
if ( ! in_array($rule, $this->_empty_rules) AND ! Valid::not_empty($value))
continue;
if ($passed === FALSE AND $error_name !== FALSE)
{
// Add the rule to the errors
$this->error($field, $error_name, $params);
// This field has an error, stop executing rules
break;
}
}
}
// Restore the data to its original form
$this->exchangeArray($original);
if (isset($benchmark))
{
// Stop benchmarking
Profiler::stop($benchmark);
}
return empty($this->_errors);
}
/**
* Add an error to a field.
*
* @param string field name
* @param string error message
* @return $this
*/
public function error($field, $error, array $params = NULL)
{
$this->_errors[$field] = array($error, $params);
return $this;
}
/**
* Returns the error messages. If no file is specified, the error message
* will be the name of the rule that failed. When a file is specified, the
* message will be loaded from "field/rule", or if no rule-specific message
* exists, "field/default" will be used. If neither is set, the returned
* message will be "file/field/rule".
*
* By default all messages are translated using the default language.
* A string can be used as the second parameter to specified the language
* that the message was written in.
*
* // Get errors from messages/forms/login.php
* $errors = $Validation->errors('forms/login');
*
* @uses Kohana::message
* @param string file to load error messages from
* @param mixed translate the message
* @return array
*/
public function errors($file = NULL, $translate = TRUE)
{
if ($file === NULL)
{
// Return the error list
return $this->_errors;
}
// Create a new message list
$messages = array();
foreach ($this->_errors as $field => $set)
{
list($error, $params) = $set;
// Get the label for this field
$label = $this->_labels[$field];
if ($translate)
{
if (is_string($translate))
{
// Translate the label using the specified language
$label = __($label, NULL, $translate);
}
else
{
// Translate the label
$label = __($label);
}
}
// Start the translation values list
$values = array(
':field' => $label,
':value' => Arr::get($this, $field),
);
if (is_array($values[':value']))
{
// All values must be strings
$values[':value'] = implode(', ', Arr::flatten($values[':value']));
}
if ($params)
{
foreach ($params as $key => $value)
{
if (is_array($value))
{
// All values must be strings
$value = implode(', ', Arr::flatten($value));
}
elseif (is_object($value))
{
// Objects cannot be used in message files
continue;
}
// Check if a label for this parameter exists
if (isset($this->_labels[$value]))
{
// Use the label as the value, eg: related field name for "matches"
$value = $this->_labels[$value];
if ($translate)
{
if (is_string($translate))
{
// Translate the value using the specified language
$value = __($value, NULL, $translate);
}
else
{
// Translate the value
$value = __($value);
}
}
}
// Add each parameter as a numbered value, starting from 1
$values[':param'.($key + 1)] = $value;
}
}
if ($message = Kohana::message($file, "{$field}.{$error}"))
{
// Found a message for this field and error
}
elseif ($message = Kohana::message($file, "{$field}.default"))
{
// Found a default message for this field
}
elseif ($message = Kohana::message($file, $error))
{
// Found a default message for this error
}
elseif ($message = Kohana::message('validation', $error))
{
// Found a default message for this error
}
else
{
// No message exists, display the path expected
$message = "{$file}.{$field}.{$error}";
}
if ($translate)
{
if (is_string($translate))
{
// Translate the message using specified language
$message = __($message, $values, $translate);
}
else
{
// Translate the message using the default language
$message = __($message, $values);
}
}
else
{
// Do not translate, just replace the values
$message = strtr($message, $values);
}
// Set the message for this field
$messages[$field] = $message;
}
return $messages;
}
} // End Validation
|