1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Subsites\Model; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\Controller; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\Forms\CheckboxField; |
8
|
|
|
use SilverStripe\Forms\FieldList; |
9
|
|
|
use SilverStripe\Forms\OptionsetField; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\Subsites\Forms\WildcardDomainField; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property string $Domain domain name of this subsite. Can include wildcards. Do not include the URL scheme here |
15
|
|
|
* @property string $Protocol Required protocol (http or https) if only one is supported. 'automatic' implies |
16
|
|
|
* that any links to this subsite should use the current protocol, and that both are supported. |
17
|
|
|
* @property string $SubstitutedDomain Domain name with all wildcards filled in |
18
|
|
|
* @property string $FullProtocol Full protocol including :// |
19
|
|
|
* @property bool $IsPrimary Is this the primary subdomain? |
20
|
|
|
*/ |
21
|
|
|
class SubsiteDomain extends DataObject |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
private static $table_name = 'SubsiteDomain'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private static $default_sort = '"IsPrimary" DESC'; |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** * |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private static $db = [ |
|
|
|
|
36
|
|
|
'Domain' => 'Varchar(255)', |
37
|
|
|
'Protocol' => "Enum('http,https,automatic','automatic')", |
38
|
|
|
'IsPrimary' => 'Boolean', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Specifies that this subsite is http only |
43
|
|
|
*/ |
44
|
|
|
const PROTOCOL_HTTP = 'http'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Specifies that this subsite is https only |
48
|
|
|
*/ |
49
|
|
|
const PROTOCOL_HTTPS = 'https'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Specifies that this subsite supports both http and https |
53
|
|
|
*/ |
54
|
|
|
const PROTOCOL_AUTOMATIC = 'automatic'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get the descriptive title for this domain |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getTitle() |
62
|
|
|
{ |
63
|
|
|
return $this->Domain; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private static $has_one = [ |
|
|
|
|
71
|
|
|
'Subsite' => Subsite::class, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @config |
76
|
|
|
* @var array |
77
|
|
|
*/ |
78
|
|
|
private static $summary_fields = [ |
|
|
|
|
79
|
|
|
'Domain', |
80
|
|
|
'IsPrimary', |
81
|
|
|
]; |
82
|
|
|
|
83
|
|
|
/*** @config |
84
|
|
|
* @var array |
85
|
|
|
*/ |
86
|
|
|
private static $casting = [ |
|
|
|
|
87
|
|
|
'SubstitutedDomain' => 'Varchar', |
88
|
|
|
'FullProtocol' => 'Varchar', |
89
|
|
|
'AbsoluteLink' => 'Varchar', |
90
|
|
|
]; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Whenever a Subsite Domain is written, rewrite the hostmap |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function onAfterWrite() |
98
|
|
|
{ |
99
|
|
|
Subsite::writeHostMap(); |
100
|
|
|
parent::onAfterWrite(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* |
105
|
|
|
* @return FieldList |
106
|
|
|
*/ |
107
|
|
|
public function getCMSFields() |
108
|
|
|
{ |
109
|
|
|
$protocols = [ |
110
|
|
|
self::PROTOCOL_HTTP => _t(__CLASS__ . '.PROTOCOL_HTTP', 'http://'), |
111
|
|
|
self::PROTOCOL_HTTPS => _t(__CLASS__ . '.PROTOCOL_HTTPS', 'https://'), |
112
|
|
|
self::PROTOCOL_AUTOMATIC => _t(__CLASS__ . '.PROTOCOL_AUTOMATIC', 'Automatic') |
113
|
|
|
]; |
114
|
|
|
$fields = FieldList::create( |
115
|
|
|
WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255) |
116
|
|
|
->setDescription(_t( |
117
|
|
|
__CLASS__ . '.DOMAIN_DESCRIPTION', |
118
|
|
|
'Hostname of this subsite (exclude protocol). Allows wildcards (*).' |
119
|
|
|
)), |
120
|
|
|
OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols) |
121
|
|
|
->setValue($this->Protocol ?: self::PROTOCOL_AUTOMATIC) |
122
|
|
|
->setDescription(_t( |
123
|
|
|
__CLASS__ . '.PROTOCOL_DESCRIPTION', |
124
|
|
|
'When generating links to this subsite, use the selected protocol. <br />' . |
125
|
|
|
'Selecting \'Automatic\' means subsite links will default to the current protocol.' |
126
|
|
|
)), |
127
|
|
|
CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary')) |
128
|
|
|
->setDescription(_t( |
129
|
|
|
__CLASS__ . '.ISPRIMARY_DESCRIPTION', |
130
|
|
|
'Mark this as the default domain for this subsite' |
131
|
|
|
)) |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->extend('updateCMSFields', $fields); |
135
|
|
|
return $fields; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* @param bool $includerelations |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function fieldLabels($includerelations = true) |
144
|
|
|
{ |
145
|
|
|
$labels = parent::fieldLabels($includerelations); |
146
|
|
|
$labels['Domain'] = _t(__CLASS__ . '.DOMAIN', 'Domain'); |
147
|
|
|
$labels['Protocol'] = _t(__CLASS__ . '.Protocol', 'Protocol'); |
148
|
|
|
$labels['IsPrimary'] = _t(__CLASS__ . '.IS_PRIMARY', 'Is Primary Domain?'); |
149
|
|
|
|
150
|
|
|
return $labels; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Get the link to this subsite |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function Link() |
159
|
|
|
{ |
160
|
|
|
return $this->getFullProtocol() . $this->Domain; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Gets the full protocol (including ://) for this domain |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function getFullProtocol() |
169
|
|
|
{ |
170
|
|
|
switch ($this->Protocol) { |
171
|
|
|
case self::PROTOCOL_HTTPS: |
172
|
|
|
return 'https://'; |
173
|
|
|
case self::PROTOCOL_HTTP: |
174
|
|
|
return 'http://'; |
175
|
|
|
default: |
176
|
|
|
return Director::protocol(); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Retrieves domain name with wildcards substituted with actual values |
182
|
|
|
* |
183
|
|
|
* @todo Refactor domains into separate wildcards / primary domains |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
public function getSubstitutedDomain() |
188
|
|
|
{ |
189
|
|
|
$currentHost = Director::host(); |
190
|
|
|
|
191
|
|
|
// If there are wildcards in the primary domain (not recommended), make some |
192
|
|
|
// educated guesses about what to replace them with: |
193
|
|
|
$domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain); |
194
|
|
|
|
195
|
|
|
// Default to "subsite." prefix for first wildcard |
196
|
|
|
// TODO Whats the significance of "subsite" in this context?! |
197
|
|
|
$domain = preg_replace('/^\*\./', "subsite.", $domain); |
198
|
|
|
|
199
|
|
|
// *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com' |
200
|
|
|
$domain = str_replace('.www.', '.', $domain); |
201
|
|
|
|
202
|
|
|
return $domain; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get absolute link for this domain |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function getAbsoluteLink() |
211
|
|
|
{ |
212
|
|
|
return $this->getFullProtocol() . $this->getSubstitutedDomain(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get absolute baseURL for this domain |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function absoluteBaseURL() |
221
|
|
|
{ |
222
|
|
|
return Controller::join_links( |
223
|
|
|
$this->getAbsoluteLink(), |
224
|
|
|
Director::baseURL() |
225
|
|
|
); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|