| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* sfValidatorUrl validates Urls.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage validator
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfValidatorUrl.class.php 22149 2009-09-18 14:09:53Z Kris.Wallsmith $
|
|
|
18 |
*/
|
|
|
19 |
class sfValidatorUrl extends sfValidatorRegex
|
|
|
20 |
{
|
|
|
21 |
const REGEX_URL_FORMAT = '~^
|
|
|
22 |
(%s):// # protocol
|
|
|
23 |
(
|
|
|
24 |
([a-z0-9-]+\.)+[a-z]{2,6} # a domain name
|
|
|
25 |
| # or
|
|
|
26 |
\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} # a IP address
|
|
|
27 |
)
|
|
|
28 |
(:[0-9]+)? # a port (optional)
|
|
|
29 |
(/?|/\S+) # a /, nothing or a / with something
|
|
|
30 |
$~ix';
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* Available options:
|
|
|
34 |
*
|
|
|
35 |
* * protocols: An array of acceptable URL protocols (http, https, ftp and ftps by default)
|
|
|
36 |
*
|
|
|
37 |
* @param array $options An array of options
|
|
|
38 |
* @param array $messages An array of error messages
|
|
|
39 |
*
|
|
|
40 |
* @see sfValidatorRegex
|
|
|
41 |
*/
|
|
|
42 |
protected function configure($options = array(), $messages = array())
|
|
|
43 |
{
|
|
|
44 |
parent::configure($options, $messages);
|
|
|
45 |
|
|
|
46 |
$this->addOption('protocols', array('http', 'https', 'ftp', 'ftps'));
|
|
|
47 |
$this->setOption('pattern', new sfCallable(array($this, 'generateRegex')));
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
/**
|
|
|
51 |
* Generates the current validator's regular expression.
|
|
|
52 |
*
|
|
|
53 |
* @return string
|
|
|
54 |
*/
|
|
|
55 |
public function generateRegex()
|
|
|
56 |
{
|
|
|
57 |
return sprintf(self::REGEX_URL_FORMAT, implode('|', $this->getOption('protocols')));
|
|
|
58 |
}
|
|
|
59 |
}
|