| 1 | <?php |
||
| 6 | * Act as whitelist (or blacklist). |
||
| 7 | * |
||
| 8 | */ |
||
| 9 | class CWhitelist |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * Array to contain the whitelist options. |
||
| 13 | */ |
||
| 14 | private $whitelist = array(); |
||
| 15 | |||
| 16 | |||
| 17 | |||
| 18 | /** |
||
| 19 | * Set the whitelist from an array of strings, each item in the |
||
| 20 | * whitelist should be a regexp without the surrounding / or #. |
||
| 21 | * |
||
| 22 | * @param array $whitelist with all valid options, |
||
| 23 | * default is to clear the whitelist. |
||
| 24 | 6 | * |
|
| 25 | * @return $this |
||
| 26 | 6 | */ |
|
| 27 | 1 | public function set($whitelist = array()) |
|
| 36 | |||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * Check if item exists in the whitelist. |
||
| 41 | * |
||
| 42 | * @param string $item string to check. |
||
| 43 | * @param array $whitelist optional with all valid options, default is null. |
||
| 44 | 5 | * |
|
| 45 | * @return boolean true if item is in whitelist, else false. |
||
| 46 | 5 | */ |
|
| 47 | 2 | public function check($item, $whitelist = null) |
|
| 48 | 2 | { |
|
| 49 | if ($whitelist !== null) { |
||
| 50 | 5 | $this->set($whitelist); |
|
| 51 | 1 | } |
|
| 52 | |||
| 53 | if (empty($item) or empty($this->whitelist)) { |
||
| 54 | 4 | return false; |
|
| 55 | 4 | } |
|
| 56 | 2 | ||
| 57 | foreach ($this->whitelist as $regexp) { |
||
| 58 | 2 | if (preg_match("#$regexp#", $item)) { |
|
| 59 | return true; |
||
| 60 | 2 | } |
|
| 61 | } |
||
| 62 | |||
| 63 | return false; |
||
| 66 |