1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BurnBright\ExternalURLField; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\TextField; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* ExternalURLField |
9
|
|
|
* |
10
|
|
|
* Form field for entering, saving, validating external urls. |
11
|
|
|
*/ |
12
|
|
|
class ExternalURLField extends TextField |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Default configuration |
16
|
|
|
* |
17
|
|
|
* URL validation regular expression was sourced from |
18
|
|
|
* @see https://gist.github.com/dperini/729294 |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $default_config = array( |
22
|
|
|
'defaultparts' => array( |
23
|
|
|
'scheme' => 'http' |
24
|
|
|
), |
25
|
|
|
'removeparts' => array( |
26
|
|
|
'scheme' => false, |
27
|
|
|
'user' => true, |
28
|
|
|
'pass' => true, |
29
|
|
|
'host' => false, |
30
|
|
|
'port' => false, |
31
|
|
|
'path' => false, |
32
|
|
|
'query' => false, |
33
|
|
|
'fragment' => false |
34
|
|
|
), |
35
|
|
|
'html5validation' => true, |
36
|
|
|
'validregex' => '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $config; |
43
|
|
|
|
44
|
4 |
|
public function __construct($name, $title = null, $value = null) |
45
|
|
|
{ |
46
|
4 |
|
$this->config = $this->config()->default_config; |
47
|
|
|
|
48
|
4 |
|
parent::__construct($name, $title, $value); |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
public function Type() |
52
|
|
|
{ |
53
|
|
|
return 'url text'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $name |
58
|
|
|
* @param mixed $val |
59
|
|
|
*/ |
60
|
1 |
|
public function setConfig($name, $val = null) |
61
|
|
|
{ |
62
|
1 |
|
if (is_array($name) && $val == null) { |
63
|
1 |
|
foreach ($name as $n => $value) { |
64
|
1 |
|
$this->setConfig($n, $value); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
1 |
|
if (is_array($this->config[$name])) { |
70
|
1 |
|
if (!is_array($val)) { |
71
|
|
|
user_error("The value for $name must be an array"); |
72
|
|
|
} |
73
|
1 |
|
$this->config[$name] = array_merge($this->config[$name], $val); |
74
|
1 |
|
} elseif (isset($this->config[$name])) { |
75
|
1 |
|
$this->config[$name] = $val; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param String $name Optional, returns the whole configuration array if empty |
83
|
|
|
* @return mixed|array |
84
|
|
|
*/ |
85
|
1 |
|
public function getConfig($name = null) |
86
|
|
|
{ |
87
|
1 |
|
if ($name) { |
|
|
|
|
88
|
1 |
|
return isset($this->config[$name]) ? $this->config[$name] : null; |
89
|
|
|
} else { |
90
|
|
|
return $this->config; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set additional attributes |
96
|
|
|
* @return array Attributes |
97
|
|
|
*/ |
98
|
|
|
public function getAttributes() |
99
|
|
|
{ |
100
|
|
|
$attributes = array( |
101
|
|
|
'placeholder' => $this->config['defaultparts']['scheme'] . "://example.com" //example url |
102
|
|
|
); |
103
|
|
|
if ($this->config['html5validation']) { |
104
|
|
|
$attributes += array( |
105
|
|
|
'type' => 'url', //html5 field type |
106
|
|
|
'pattern' => 'https?://.+', //valid urls only |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return array_merge( |
111
|
|
|
parent::getAttributes(), |
112
|
|
|
$attributes |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Rebuild url on save |
118
|
|
|
* @param string $url |
119
|
|
|
*/ |
120
|
2 |
|
public function setValue($url) |
121
|
|
|
{ |
122
|
2 |
|
if ($url) { |
123
|
2 |
|
$url = $this->rebuildURL($url); |
124
|
|
|
} |
125
|
2 |
|
parent::setValue($url); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add config scheme, if missing. |
130
|
|
|
* Remove the parts of the url we don't want. |
131
|
|
|
* Set any defaults, if missing. |
132
|
|
|
* Remove any trailing slash, and rebuild. |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
2 |
|
protected function rebuildURL($url) |
136
|
|
|
{ |
137
|
2 |
|
$defaults = $this->config['defaultparts']; |
138
|
2 |
|
if (!preg_match('#^[a-zA-Z]+://#', $url)) { |
139
|
2 |
|
$url = $defaults['scheme'] . "://" . $url; |
140
|
|
|
} |
141
|
2 |
|
$parts = parse_url($url); |
142
|
2 |
|
if (!$parts) { |
143
|
|
|
//can't parse url, abort |
144
|
1 |
|
return ""; |
145
|
|
|
} |
146
|
2 |
|
foreach ($parts as $part => $value) { |
147
|
2 |
|
if ($this->config['removeparts'][$part] === true) { |
148
|
2 |
|
unset($parts[$part]); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
//set defaults, if missing |
152
|
2 |
|
foreach ($defaults as $part => $default) { |
153
|
2 |
|
if (!isset($parts[$part])) { |
154
|
2 |
|
$parts[$part] = $default; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
2 |
|
return rtrim(http_build_url($defaults, $parts), "/"); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Server side validation, using a regular expression. |
163
|
|
|
*/ |
164
|
1 |
|
public function validate($validator) |
165
|
|
|
{ |
166
|
1 |
|
$this->value = trim($this->value); |
167
|
1 |
|
$regex = $this->config['validregex']; |
168
|
1 |
|
if ($this->value && $regex && !preg_match($regex, $this->value)) { |
169
|
1 |
|
$validator->validationError( |
170
|
1 |
|
$this->name, |
171
|
1 |
|
_t('ExternalURLField.VALIDATION', "Please enter a valid URL"), |
172
|
1 |
|
"validation" |
173
|
|
|
); |
174
|
1 |
|
return false; |
175
|
|
|
} |
176
|
1 |
|
return true; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: