1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* This file is part of Aura for PHP. |
5
|
|
|
* |
6
|
|
|
* @license http://opensource.org/licenses/bsd-license.php BSD |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
namespace Aura\Filter\Rule\Validate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* |
13
|
|
|
* Validates the value as a URL. |
14
|
|
|
* |
15
|
|
|
* @package Aura.Filter |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class Url |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* |
22
|
|
|
* Validates the value as a URL. |
23
|
|
|
* |
24
|
|
|
* The value must match a generic URL format; for example, |
25
|
|
|
* ``http://example.com``, ``mms://example.org``, and so on. |
26
|
|
|
* |
27
|
|
|
* @param object $subject The subject to be filtered. |
28
|
|
|
* |
29
|
|
|
* @param string $field The subject field name. |
30
|
|
|
* |
31
|
|
|
* @return bool True if valid, false if not. |
32
|
|
|
* |
33
|
|
|
*/ |
34
|
14 |
|
public function __invoke($subject, $field) |
35
|
|
|
{ |
36
|
14 |
|
$value = $subject->$field; |
37
|
14 |
|
if (! is_scalar($value)) { |
38
|
1 |
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// first, make sure there are no invalid chars, list from ext/filter |
42
|
|
|
$other = "$-_.+" // safe |
43
|
|
|
. "!*'()," // extra |
44
|
|
|
. "{}|\\^~[]`" // national |
45
|
|
|
. "<>#%\"" // punctuation |
46
|
13 |
|
. ";/?:@&="; // reserved |
47
|
|
|
|
48
|
13 |
|
$valid = 'a-zA-Z0-9' . preg_quote($other, '/'); |
49
|
13 |
|
$clean = preg_replace("/[^$valid]/", '', $value); |
50
|
13 |
|
if ($value != $clean) { |
51
|
4 |
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// now make sure it parses as a URL with scheme and host |
55
|
9 |
|
$result = @parse_url($value); |
56
|
9 |
|
if (empty($result['scheme']) || trim($result['scheme']) == '' || |
57
|
9 |
|
empty($result['host']) || trim($result['host']) == '') { |
58
|
|
|
// need a scheme and host |
59
|
3 |
|
return false; |
60
|
|
|
} else { |
61
|
|
|
// looks ok |
62
|
6 |
|
return true; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|