root/trunk/apps/main/lib/sfBlacklistValidator.class.php

Revision 2, 1.3 kB (checked in by nperriault, 2 years ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 class sfBlacklistValidator extends sfValidator
3 {
4
5   public function initialize($context, $parameters = null)
6   {
7     // initialize parent
8     parent::initialize($context);
9
10     // set defaults
11     $this->getParameterHolder()->set('blacklist_error', 'Value is not an allowed one');
12     $this->getParameterHolder()->set('case_sensitive', false);
13     $this->getParameterHolder()->set('trim', true);
14     $this->getParameterHolder()->add($parameters);
15     return true;
16   }
17
18   public function execute(&$value, &$error)
19   {
20     // Forbidden names
21     $blacklist = $this->getParameter('blacklist');
22     $casesensitive = $this->getParameter('case_sensitive');
23     $trim = $this->getParameter('trim');
24     
25     if ($trim)
26     {
27       $value = trim($value);
28     }
29     
30     if (is_array($blacklist) && count($blacklist) > 0)
31     {
32       if ($casesensitive)
33       {
34         $match = in_array($value, $blacklist);
35       }
36       else
37       {
38         $match = false;
39         foreach ($blacklist as $item)
40         {
41           if (is_string($item) && strtolower($item) == strtolower($value))
42           {
43             $match = true;
44             break;
45           }
46         }
47       }
48       if ($match)
49       {
50         $error = $this->getParameterHolder()->get('blacklist_error');
51         return false;
52       }
53     }
54     
55     return true;
56   }
57  
58 }
59
Note: See TracBrowser for help on using the browser.