|
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
|
|
|
public function __construct($name, $title = null, $value = null) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->config = $this->config()->default_config; |
|
47
|
|
|
|
|
48
|
|
|
parent::__construct($name, $title, $value); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function Type() |
|
52
|
|
|
{ |
|
53
|
|
|
return 'url text'; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $name |
|
58
|
|
|
* @param mixed $val |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setConfig($name, $val = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (is_array($name) && $val == null) { |
|
63
|
|
|
foreach ($name as $n => $value) { |
|
64
|
|
|
$this->setConfig($n, $value); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
if (is_array($this->config[$name])) { |
|
70
|
|
|
if (!is_array($val)) { |
|
71
|
|
|
user_error("The value for $name must be an array"); |
|
72
|
|
|
} |
|
73
|
|
|
$this->config[$name] = array_merge($this->config[$name], $val); |
|
74
|
|
|
} elseif (isset($this->config[$name])) { |
|
75
|
|
|
$this->config[$name] = $val; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $this; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param String $name Optional, returns the whole configuration array if empty |
|
83
|
|
|
* @return mixed|array |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getConfig($name = null) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($name) { |
|
|
|
|
|
|
88
|
|
|
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
|
|
|
public function setValue($url) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($url) { |
|
123
|
|
|
$url = $this->rebuildURL($url); |
|
124
|
|
|
} |
|
125
|
|
|
parent::setValue($url); |
|
126
|
|
|
} |
|
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
|
|
|
protected function rebuildURL($url) |
|
136
|
|
|
{ |
|
137
|
|
|
$defaults = $this->config['defaultparts']; |
|
138
|
|
|
if (!preg_match('#^[a-zA-Z]+://#', $url)) { |
|
139
|
|
|
$url = $defaults['scheme'] . "://" . $url; |
|
140
|
|
|
} |
|
141
|
|
|
$parts = parse_url($url); |
|
142
|
|
|
if (!$parts) { |
|
143
|
|
|
//can't parse url, abort |
|
144
|
|
|
return ""; |
|
145
|
|
|
} |
|
146
|
|
|
foreach ($parts as $part => $value) { |
|
147
|
|
|
if ($this->config['removeparts'][$part] === true) { |
|
148
|
|
|
unset($parts[$part]); |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
//set defaults, if missing |
|
152
|
|
|
foreach ($defaults as $part => $default) { |
|
153
|
|
|
if (!isset($parts[$part])) { |
|
154
|
|
|
$parts[$part] = $default; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return rtrim(http_build_url($defaults, $parts), "/"); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Server side validation, using a regular expression. |
|
163
|
|
|
*/ |
|
164
|
|
|
public function validate($validator) |
|
165
|
|
|
{ |
|
166
|
|
|
$this->value = trim($this->value); |
|
167
|
|
|
$regex = $this->config['validregex']; |
|
168
|
|
|
if ($this->value && $regex && !preg_match($regex, $this->value)) { |
|
169
|
|
|
$validator->validationError( |
|
170
|
|
|
$this->name, |
|
171
|
|
|
_t('ExternalURLField.VALIDATION', "Please enter a valid URL"), |
|
172
|
|
|
"validation" |
|
173
|
|
|
); |
|
174
|
|
|
return false; |
|
175
|
|
|
} |
|
176
|
|
|
return true; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: