first commit

This commit is contained in:
CHIEFSOFT\ameye
2025-11-22 09:54:51 -05:00
commit 07a07ab49f
722 changed files with 125787 additions and 0 deletions
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Validation\CreditCardRules as NonStrictCreditCardRules;
/**
* Class CreditCardRules
*
* Provides validation methods for common credit-card inputs.
*
* @see http://en.wikipedia.org/wiki/Credit_card_number
* @see \CodeIgniter\Validation\StrictRules\CreditCardRulesTest
*/
class CreditCardRules
{
private readonly NonStrictCreditCardRules $nonStrictCreditCardRules;
public function __construct()
{
$this->nonStrictCreditCardRules = new NonStrictCreditCardRules();
}
/**
* Verifies that a credit card number is valid and matches the known
* formats for a wide number of credit card types. This does not verify
* that the card is a valid card, only that the number is formatted correctly.
*
* Example:
* $rules = [
* 'cc_num' => 'valid_cc_number[visa]'
* ];
*
* @param array|bool|float|int|object|string|null $ccNumber
*/
public function valid_cc_number($ccNumber, string $type): bool
{
if (! is_string($ccNumber)) {
return false;
}
return $this->nonStrictCreditCardRules->valid_cc_number($ccNumber, $type);
}
}
+317
View File
@@ -0,0 +1,317 @@
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Exceptions\InvalidArgumentException;
use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use Config\Mimes;
/**
* File validation rules
*
* @see \CodeIgniter\Validation\StrictRules\FileRulesTest
*/
class FileRules
{
/**
* Request instance. So we can get access to the files.
*
* @var IncomingRequest
*/
protected $request;
/**
* Constructor.
*/
public function __construct(?RequestInterface $request = null)
{
if (! $request instanceof RequestInterface) {
$request = service('request');
}
assert($request instanceof IncomingRequest || $request instanceof CLIRequest);
$this->request = $request;
}
/**
* Verifies that $name is the name of a valid uploaded file.
*/
public function uploaded(?string $blank, string $name): bool
{
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if (ENVIRONMENT === 'testing') {
if ($file->getError() !== 0) {
return false;
}
} else {
// Note: cannot unit test this; no way to over-ride ENVIRONMENT?
// @codeCoverageIgnoreStart
if (! $file->isValid()) {
return false;
}
// @codeCoverageIgnoreEnd
}
}
return true;
}
/**
* Verifies if the file's size in Kilobytes is no larger than the parameter.
*/
public function max_size(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$paramArray = explode(',', $params);
if (count($paramArray) !== 2) {
throw new InvalidArgumentException('Invalid max_size parameter: "' . $params . '"');
}
$name = array_shift($paramArray);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
return false;
}
if ($file->getSize() / 1024 > $paramArray[0]) {
return false;
}
}
return true;
}
/**
* Uses the mime config file to determine if a file is considered an "image",
* which for our purposes basically means that it's a raster image or svg.
*/
public function is_image(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
// We know that our mimes list always has the first mime
// start with `image` even when then are multiple accepted types.
$type = Mimes::guessTypeFromExtension($file->getExtension()) ?? '';
if (mb_strpos($type, 'image') !== 0) {
return false;
}
}
return true;
}
/**
* Checks to see if an uploaded file's mime type matches one in the parameter.
*/
public function mime_in(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
if (! in_array($file->getMimeType(), $params, true)) {
return false;
}
}
return true;
}
/**
* Checks to see if an uploaded file's extension matches one in the parameter.
*/
public function ext_in(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
if (! in_array($file->guessExtension(), $params, true)) {
return false;
}
}
return true;
}
/**
* Checks an uploaded file to verify that the dimensions are within
* a specified allowable dimension.
*/
public function max_dims(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
// Get Parameter sizes
$allowedWidth = $params[0] ?? 0;
$allowedHeight = $params[1] ?? 0;
// Get uploaded image size
$info = getimagesize($file->getTempName());
if ($info === false) {
// Cannot get the image size.
return false;
}
$fileWidth = $info[0];
$fileHeight = $info[1];
if ($fileWidth > $allowedWidth || $fileHeight > $allowedHeight) {
return false;
}
}
return true;
}
/**
* Checks an uploaded file to verify that the dimensions are greater than
* a specified dimension.
*/
public function min_dims(?string $blank, string $params): bool
{
// Grab the file name off the top of the $params
// after we split it.
$params = explode(',', $params);
$name = array_shift($params);
$files = $this->request->getFileMultiple($name);
if ($files === null) {
$files = [$this->request->getFile($name)];
}
foreach ($files as $file) {
if ($file === null) {
return false;
}
if ($file->getError() === UPLOAD_ERR_NO_FILE) {
return true;
}
// Get Parameter sizes
$minimumWidth = $params[0] ?? 0;
$minimumHeight = $params[1] ?? 0;
// Get uploaded image size
$info = getimagesize($file->getTempName());
if ($info === false) {
// Cannot get the image size.
return false;
}
$fileWidth = $info[0];
$fileHeight = $info[1];
if ($fileWidth < $minimumWidth || $fileHeight < $minimumHeight) {
return false;
}
}
return true;
}
}
@@ -0,0 +1,411 @@
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Validation\FormatRules as NonStrictFormatRules;
/**
* Format validation Rules.
*
* @see \CodeIgniter\Validation\StrictRules\FormatRulesTest
*/
class FormatRules
{
private readonly NonStrictFormatRules $nonStrictFormatRules;
public function __construct()
{
$this->nonStrictFormatRules = new NonStrictFormatRules();
}
/**
* Alpha
*
* @param array|bool|float|int|object|string|null $str
*/
public function alpha($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->alpha($str);
}
/**
* Alpha with spaces.
*
* @param array|bool|float|int|object|string|null $value Value.
*
* @return bool True if alpha with spaces, else false.
*/
public function alpha_space($value = null): bool
{
if (! is_string($value)) {
return false;
}
return $this->nonStrictFormatRules->alpha_space($value);
}
/**
* Alphanumeric with underscores and dashes
*
* @param array|bool|float|int|object|string|null $str
*/
public function alpha_dash($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->alpha_dash($str);
}
/**
* Alphanumeric, spaces, and a limited set of punctuation characters.
* Accepted punctuation characters are: ~ tilde, ! exclamation,
* # number, $ dollar, % percent, & ampersand, * asterisk, - dash,
* _ underscore, + plus, = equals, | vertical bar, : colon, . period
* ~ ! # $ % & * - _ + = | : .
*
* @param array|bool|float|int|object|string|null $str
*
* @return bool
*/
public function alpha_numeric_punct($str)
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->alpha_numeric_punct($str);
}
/**
* Alphanumeric
*
* @param array|bool|float|int|object|string|null $str
*/
public function alpha_numeric($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->alpha_numeric($str);
}
/**
* Alphanumeric w/ spaces
*
* @param array|bool|float|int|object|string|null $str
*/
public function alpha_numeric_space($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->alpha_numeric_space($str);
}
/**
* Any type of string
*
* @param array|bool|float|int|object|string|null $str
*/
public function string($str = null): bool
{
return $this->nonStrictFormatRules->string($str);
}
/**
* Decimal number
*
* @param array|bool|float|int|object|string|null $str
*/
public function decimal($str = null): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->decimal($str);
}
/**
* String of hexidecimal characters
*
* @param array|bool|float|int|object|string|null $str
*/
public function hex($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->hex($str);
}
/**
* Integer
*
* @param array|bool|float|int|object|string|null $str
*/
public function integer($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->integer($str);
}
/**
* Is a Natural number (0,1,2,3, etc.)
*
* @param array|bool|float|int|object|string|null $str
*/
public function is_natural($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->is_natural($str);
}
/**
* Is a Natural number, but not a zero (1,2,3, etc.)
*
* @param array|bool|float|int|object|string|null $str
*/
public function is_natural_no_zero($str = null): bool
{
if (is_int($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->is_natural_no_zero($str);
}
/**
* Numeric
*
* @param array|bool|float|int|object|string|null $str
*/
public function numeric($str = null): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->numeric($str);
}
/**
* Compares value against a regular expression pattern.
*
* @param array|bool|float|int|object|string|null $str
*/
public function regex_match($str, string $pattern): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->regex_match($str, $pattern);
}
/**
* Validates that the string is a valid timezone as per the
* timezone_identifiers_list function.
*
* @see http://php.net/manual/en/datetimezone.listidentifiers.php
*
* @param array|bool|float|int|object|string|null $str
*/
public function timezone($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->timezone($str);
}
/**
* Valid Base64
*
* Tests a string for characters outside of the Base64 alphabet
* as defined by RFC 2045 http://www.faqs.org/rfcs/rfc2045
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_base64($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_base64($str);
}
/**
* Valid JSON
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_json($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_json($str);
}
/**
* Checks for a correctly formatted email address
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_email($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_email($str);
}
/**
* Validate a comma-separated list of email addresses.
*
* Example:
* valid_emails[one@example.com,two@example.com]
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_emails($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_emails($str);
}
/**
* Validate an IP address (human readable format or binary string - inet_pton)
*
* @param array|bool|float|int|object|string|null $ip
* @param string|null $which IP protocol: 'ipv4' or 'ipv6'
*/
public function valid_ip($ip = null, ?string $which = null): bool
{
if (! is_string($ip)) {
return false;
}
return $this->nonStrictFormatRules->valid_ip($ip, $which);
}
/**
* Checks a string to ensure it is (loosely) a URL.
*
* Warning: this rule will pass basic strings like
* "banana"; use valid_url_strict for a stricter rule.
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_url($str = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_url($str);
}
/**
* Checks a URL to ensure it's formed correctly.
*
* @param array|bool|float|int|object|string|null $str
* @param string|null $validSchemes comma separated list of allowed schemes
*/
public function valid_url_strict($str = null, ?string $validSchemes = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_url_strict($str, $validSchemes);
}
/**
* Checks for a valid date and matches a given date format
*
* @param array|bool|float|int|object|string|null $str
*/
public function valid_date($str = null, ?string $format = null): bool
{
if (! is_string($str)) {
return false;
}
return $this->nonStrictFormatRules->valid_date($str, $format);
}
}
+394
View File
@@ -0,0 +1,394 @@
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Validation\StrictRules;
use CodeIgniter\Helpers\Array\ArrayHelper;
use CodeIgniter\Validation\Rules as NonStrictRules;
/**
* Validation Rules.
*
* @see \CodeIgniter\Validation\StrictRules\RulesTest
*/
class Rules
{
private readonly NonStrictRules $nonStrictRules;
public function __construct()
{
$this->nonStrictRules = new NonStrictRules();
}
/**
* The value does not match another field in $data.
*
* @param array|bool|float|int|object|string|null $str
* @param array $data Other field/value pairs
*/
public function differs(
$str,
string $otherField,
array $data,
?string $error = null,
?string $field = null,
): bool {
if (str_contains($otherField, '.')) {
return $str !== dot_array_search($otherField, $data);
}
if (! array_key_exists($otherField, $data)) {
return false;
}
if (str_contains($field, '.')) {
if (! ArrayHelper::dotKeyExists($field, $data)) {
return false;
}
} elseif (! array_key_exists($field, $data)) {
return false;
}
return $str !== ($data[$otherField] ?? null);
}
/**
* Equals the static value provided.
*
* @param array|bool|float|int|object|string|null $str
*/
public function equals($str, string $val): bool
{
return $this->nonStrictRules->equals($str, $val);
}
/**
* Returns true if $str is $val characters long.
* $val = "5" (one) | "5,8,12" (multiple values)
*
* @param array|bool|float|int|object|string|null $str
*/
public function exact_length($str, string $val): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->exact_length($str, $val);
}
/**
* Greater than
*
* @param array|bool|float|int|object|string|null $str expects int|string
*/
public function greater_than($str, string $min): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->greater_than($str, $min);
}
/**
* Equal to or Greater than
*
* @param array|bool|float|int|object|string|null $str expects int|string
*/
public function greater_than_equal_to($str, string $min): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->greater_than_equal_to($str, $min);
}
/**
* Checks the database to see if the given value exist.
* Can ignore records by field/value to filter (currently
* accept only one filter).
*
* Example:
* is_not_unique[dbGroup.table.field,where_field,where_value]
* is_not_unique[table.field,where_field,where_value]
* is_not_unique[menu.id,active,1]
*
* @param array|bool|float|int|object|string|null $str
*/
public function is_not_unique($str, string $field, array $data): bool
{
if (is_object($str) || is_array($str)) {
return false;
}
return $this->nonStrictRules->is_not_unique($str, $field, $data);
}
/**
* Value should be within an array of values
*
* @param array|bool|float|int|object|string|null $value
*/
public function in_list($value, string $list): bool
{
if (is_int($value) || is_float($value)) {
$value = (string) $value;
}
if (! is_string($value)) {
return false;
}
return $this->nonStrictRules->in_list($value, $list);
}
/**
* Checks the database to see if the given value is unique. Can
* ignore a single record by field/value to make it useful during
* record updates.
*
* Example:
* is_unique[dbGroup.table.field,ignore_field,ignore_value]
* is_unique[table.field,ignore_field,ignore_value]
* is_unique[users.email,id,5]
*
* @param array|bool|float|int|object|string|null $str
*/
public function is_unique($str, string $field, array $data): bool
{
if (is_object($str) || is_array($str)) {
return false;
}
return $this->nonStrictRules->is_unique($str, $field, $data);
}
/**
* Less than
*
* @param array|bool|float|int|object|string|null $str expects int|string
*/
public function less_than($str, string $max): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->less_than($str, $max);
}
/**
* Equal to or Less than
*
* @param array|bool|float|int|object|string|null $str expects int|string
*/
public function less_than_equal_to($str, string $max): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->less_than_equal_to($str, $max);
}
/**
* Matches the value of another field in $data.
*
* @param array|bool|float|int|object|string|null $str
* @param array $data Other field/value pairs
*/
public function matches(
$str,
string $otherField,
array $data,
?string $error = null,
?string $field = null,
): bool {
if (str_contains($otherField, '.')) {
return $str === dot_array_search($otherField, $data);
}
if (! array_key_exists($otherField, $data)) {
return false;
}
if (str_contains($field, '.')) {
if (! ArrayHelper::dotKeyExists($field, $data)) {
return false;
}
} elseif (! array_key_exists($field, $data)) {
return false;
}
return $str === ($data[$otherField] ?? null);
}
/**
* Returns true if $str is $val or fewer characters in length.
*
* @param array|bool|float|int|object|string|null $str
*/
public function max_length($str, string $val): bool
{
if (is_int($str) || is_float($str) || null === $str) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->max_length($str, $val);
}
/**
* Returns true if $str is at least $val length.
*
* @param array|bool|float|int|object|string|null $str
*/
public function min_length($str, string $val): bool
{
if (is_int($str) || is_float($str)) {
$str = (string) $str;
}
if (! is_string($str)) {
return false;
}
return $this->nonStrictRules->min_length($str, $val);
}
/**
* Does not equal the static value provided.
*
* @param array|bool|float|int|object|string|null $str
*/
public function not_equals($str, string $val): bool
{
return $this->nonStrictRules->not_equals($str, $val);
}
/**
* Value should not be within an array of values.
*
* @param array|bool|float|int|object|string|null $value
*/
public function not_in_list($value, string $list): bool
{
if (null === $value) {
return true;
}
if (is_int($value) || is_float($value)) {
$value = (string) $value;
}
if (! is_string($value)) {
return false;
}
return $this->nonStrictRules->not_in_list($value, $list);
}
/**
* @param array|bool|float|int|object|string|null $str
*/
public function required($str = null): bool
{
return $this->nonStrictRules->required($str);
}
/**
* The field is required when any of the other required fields are present
* in the data.
*
* Example (field is required when the password field is present):
*
* required_with[password]
*
* @param array|bool|float|int|object|string|null $str
* @param string|null $fields List of fields that we should check if present
* @param array $data Complete list of fields from the form
*/
public function required_with($str = null, ?string $fields = null, array $data = []): bool
{
return $this->nonStrictRules->required_with($str, $fields, $data);
}
/**
* The field is required when all the other fields are present
* in the data but not required.
*
* Example (field is required when the id or email field is missing):
*
* required_without[id,email]
*
* @param array|bool|float|int|object|string|null $str
* @param string|null $otherFields The param fields of required_without[].
* @param string|null $field This rule param fields aren't present, this field is required.
*/
public function required_without(
$str = null,
?string $otherFields = null,
array $data = [],
?string $error = null,
?string $field = null,
): bool {
return $this->nonStrictRules->required_without($str, $otherFields, $data, $error, $field);
}
/**
* The field exists in $data.
*
* @param array|bool|float|int|object|string|null $value The field value.
* @param string|null $param The rule's parameter.
* @param array $data The data to be validated.
* @param string|null $field The field name.
*/
public function field_exists(
$value = null,
?string $param = null,
array $data = [],
?string $error = null,
?string $field = null,
): bool {
if (str_contains($field, '.')) {
return ArrayHelper::dotKeyExists($field, $data);
}
return array_key_exists($field, $data);
}
}