UrlValidator.php 1.44 KB
Newer Older
1
<?php
2 3
namespace T3o\TerFe2\Validation\Validator;

4 5
/*
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16

17 18 19 20 21
/**
 * Validator for url
 *
 * @version $Id$
 */
22
class UrlValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
23
{
24

25 26 27 28 29 30
    /**
     * Returns TRUE, if the given property ($propertyValue) is a valid url and has a HTTP(S) scheme.
     *
     * If at least one error occurred, the result is FALSE.
     *
     * @param mixed $value The value that should be validated
31
     * @return bool TRUE if the value is valid, FALSE if an error occured
32 33 34 35 36 37
     */
    public function isValid($value)
    {
        $value = (string)$value;
        if ($value !== '' && ($value !== filter_var($value, FILTER_SANITIZE_URL) || !$this->hasValidScheme($value))) {
            $this->addError('The given subject was not a valid url.', 1364118054);
Markus Sommer's avatar
Markus Sommer committed
38
            return false;
39
        }
Markus Sommer's avatar
Markus Sommer committed
40
        return true;
41
    }
42

43 44
    /**
     * @param string $url
45
     * @return bool
46 47 48
     */
    private function hasValidScheme($url)
    {
49
        return in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true);
50 51
    }
}