Development

/branches/1.0/lib/validator/sfUrlValidator.class.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/validator/sfUrlValidator.class.php

Revision 8720, 1.9 kB (checked in by FabianLange, 7 years ago)

fixed url validation bug by applied carls patch (1.1 backport). fixes #1373

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 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  * sfUrlValidator verifies a parameter contains a value that qualifies as a valid URL.
13  *
14  * @package    symfony
15  * @subpackage validator
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfUrlValidator extends sfValidator
20 {
21   /**
22    * Executes this validator.
23    *
24    * @param mixed A file or parameter value/array
25    * @param error An error message reference
26    *
27    * @return bool true, if this validator executes successfully, otherwise false
28    */
29   public function execute(&$value, &$error)
30   {
31     $re = '~^
32       (https?|ftp)://                         # http or https or ftp
33       (
34         ([a-z0-9-]+\.)+[a-z]{2,6}             # a domain name
35           |                                   #  or
36         \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}    # a IP address
37       )
38       (:[0-9]+)?                              # a port (optional)
39       (/?|/\S+)                               # a /, nothing or a / with something
40     $~ix';
41     if (!preg_match($re, $value))
42     {
43       $error = $this->getParameterHolder()->get('url_error');
44       return false;
45     }
46
47     return true;
48   }
49
50   /**
51    * Initializes this validator.
52    *
53    * @param sfContext The current application context
54    * @param array   An associative array of initialization parameters
55    *
56    * @return bool true, if initialization completes successfully, otherwise false
57    */
58   public function initialize($context, $parameters = null)
59   {
60     // initialize parent
61     parent::initialize($context);
62
63     // set defaults
64     $this->getParameterHolder()->set('url_error', 'Invalid input');
65
66     $this->getParameterHolder()->add($parameters);
67
68     return true;
69   }
70 }
71
Note: See TracBrowser for help on using the browser.