PHP 8.2.30
Preview: FileValidator.php Size: 6.11 KB
/opt/cloudlinux/alt-php55/root/usr/share/pear/Symfony/Component/Validator/Constraints/FileValidator.php

<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Symfony\Component\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\HttpFoundation\File\File as FileObject;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * @author Bernhard Schussek <bschussek@gmail.com>
 *
 * @api
 */
class FileValidator extends ConstraintValidator
{
    /**
     * {@inheritDoc}
     */
    public function validate($value, Constraint $constraint)
    {
        if (null === $value || '' === $value) {
            return;
        }

        if ($value instanceof UploadedFile && !$value->isValid()) {
            switch ($value->getError()) {
                case UPLOAD_ERR_INI_SIZE:
                    if ($constraint->maxSize) {
                        if (ctype_digit((string) $constraint->maxSize)) {
                            $maxSize = (int) $constraint->maxSize;
                        } elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
                            $maxSize = $constraint->maxSize * 1024;
                        } elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
                            $maxSize = $constraint->maxSize * 1048576;
                        } else {
                            throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
                        }
                        $maxSize = min(UploadedFile::getMaxFilesize(), $maxSize);
                    } else {
                        $maxSize = UploadedFile::getMaxFilesize();
                    }

                    $this->context->addViolation($constraint->uploadIniSizeErrorMessage, array(
                        '{{ limit }}' => $maxSize,
                        '{{ suffix }}' => 'bytes',
                    ));

                    return;
                case UPLOAD_ERR_FORM_SIZE:
                    $this->context->addViolation($constraint->uploadFormSizeErrorMessage);

                    return;
                case UPLOAD_ERR_PARTIAL:
                    $this->context->addViolation($constraint->uploadPartialErrorMessage);

                    return;
                case UPLOAD_ERR_NO_FILE:
                    $this->context->addViolation($constraint->uploadNoFileErrorMessage);

                    return;
                case UPLOAD_ERR_NO_TMP_DIR:
                    $this->context->addViolation($constraint->uploadNoTmpDirErrorMessage);

                    return;
                case UPLOAD_ERR_CANT_WRITE:
                    $this->context->addViolation($constraint->uploadCantWriteErrorMessage);

                    return;
                case UPLOAD_ERR_EXTENSION:
                    $this->context->addViolation($constraint->uploadExtensionErrorMessage);

                    return;
                default:
                    $this->context->addViolation($constraint->uploadErrorMessage);

                    return;
            }
        }

        if (!is_scalar($value) && !$value instanceof FileObject && !(is_object($value) && method_exists($value, '__toString'))) {
            throw new UnexpectedTypeException($value, 'string');
        }

        $path = $value instanceof FileObject ? $value->getPathname() : (string) $value;

        if (!is_file($path)) {
            $this->context->addViolation($constraint->notFoundMessage, array('{{ file }}' => $path));

            return;
        }

        if (!is_readable($path)) {
            $this->context->addViolation($constraint->notReadableMessage, array('{{ file }}' => $path));

            return;
        }

        if ($constraint->maxSize) {
            if (ctype_digit((string) $constraint->maxSize)) {
                $size = filesize($path);
                $limit = (int) $constraint->maxSize;
                $suffix = 'bytes';
            } elseif (preg_match('/^\d++k$/', $constraint->maxSize)) {
                $size = round(filesize($path) / 1000, 2);
                $limit = (int) $constraint->maxSize;
                $suffix = 'kB';
            } elseif (preg_match('/^\d++M$/', $constraint->maxSize)) {
                $size = round(filesize($path) / 1000000, 2);
                $limit = (int) $constraint->maxSize;
                $suffix = 'MB';
            } else {
                throw new ConstraintDefinitionException(sprintf('"%s" is not a valid maximum size', $constraint->maxSize));
            }

            if ($size > $limit) {
                $this->context->addViolation($constraint->maxSizeMessage, array(
                    '{{ size }}'    => $size,
                    '{{ limit }}'   => $limit,
                    '{{ suffix }}'  => $suffix,
                    '{{ file }}'    => $path,
                ));

                return;
            }
        }

        if ($constraint->mimeTypes) {
            if (!$value instanceof FileObject) {
                $value = new FileObject($value);
            }

            $mimeTypes = (array) $constraint->mimeTypes;
            $mime = $value->getMimeType();
            $valid = false;

            foreach ($mimeTypes as $mimeType) {
                if ($mimeType === $mime) {
                    $valid = true;
                    break;
                }

                if ($discrete = strstr($mimeType, '/*', true)) {
                    if (strstr($mime, '/', true) === $discrete) {
                        $valid = true;
                        break;
                    }
                }
            }

            if (false === $valid) {
                $this->context->addViolation($constraint->mimeTypesMessage, array(
                    '{{ type }}'    => '"'.$mime.'"',
                    '{{ types }}'   => '"'.implode('", "', $mimeTypes) .'"',
                    '{{ file }}'    => $path,
                ));
            }
        }
    }
}

Directory Contents

Dirs: 1 × Files: 90

Name Size Perms Modified Actions
- drwxr-xr-x 2024-03-03 22:54:39
Edit Download
1.09 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.22 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.52 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.14 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
511 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
775 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.61 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.42 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
699 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
3.97 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.08 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.87 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.73 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.14 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.48 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
520 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.25 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.82 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
522 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.25 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
514 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
522 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
510 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.25 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
584 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.98 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
489 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
588 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
559 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.07 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.45 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
511 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
797 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.53 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
6.11 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
497 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
516 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
631 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
610 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
630 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
399 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
479 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.92 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
523 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
598 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.91 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
5.64 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.99 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.64 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.29 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.86 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
578 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.80 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
522 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.25 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.54 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.21 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
491 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
510 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
625 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
604 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
518 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.24 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
476 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.90 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
518 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
766 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
496 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
597 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
530 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
604 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
516 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
728 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
509 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
929 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
404 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.06 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.38 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
2.39 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.24 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
404 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
514 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.21 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
509 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
821 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
774 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
1.39 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
560 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download
3.59 KB lrw-r--r-- 2019-12-18 11:24:08
Edit Download
907 B lrw-r--r-- 2019-12-18 11:24:08
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).