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'; |
||||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
25 | |||||||
26 | /** |
||||||
27 | * |
||||||
28 | * @var string |
||||||
29 | */ |
||||||
30 | private static $default_sort = '"IsPrimary" DESC'; |
||||||
0 ignored issues
–
show
|
|||||||
31 | |||||||
32 | /** * |
||||||
33 | * @var array |
||||||
34 | */ |
||||||
35 | private static $db = [ |
||||||
0 ignored issues
–
show
|
|||||||
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 = [ |
||||||
0 ignored issues
–
show
|
|||||||
71 | 'Subsite' => Subsite::class, |
||||||
72 | ]; |
||||||
73 | |||||||
74 | /** |
||||||
75 | * @config |
||||||
76 | * @var array |
||||||
77 | */ |
||||||
78 | private static $summary_fields = [ |
||||||
0 ignored issues
–
show
|
|||||||
79 | 'Domain', |
||||||
80 | 'IsPrimary', |
||||||
81 | ]; |
||||||
82 | |||||||
83 | /*** @config |
||||||
84 | * @var array |
||||||
85 | */ |
||||||
86 | private static $casting = [ |
||||||
0 ignored issues
–
show
|
|||||||
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 | } |
||||||
101 | |||||||
102 | /** |
||||||
103 | * |
||||||
104 | * @return FieldList |
||||||
105 | */ |
||||||
106 | public function getCMSFields() |
||||||
107 | { |
||||||
108 | $protocols = [ |
||||||
109 | self::PROTOCOL_HTTP => _t(__CLASS__ . '.PROTOCOL_HTTP', 'http://'), |
||||||
110 | self::PROTOCOL_HTTPS => _t(__CLASS__ . '.PROTOCOL_HTTPS', 'https://'), |
||||||
111 | self::PROTOCOL_AUTOMATIC => _t(__CLASS__ . '.PROTOCOL_AUTOMATIC', 'Automatic') |
||||||
112 | ]; |
||||||
113 | $fields = new FieldList( |
||||||
114 | WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255) |
||||||
0 ignored issues
–
show
255 of type integer is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() 'Domain' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
115 | ->setDescription(_t( |
||||||
116 | __CLASS__ . '.DOMAIN_DESCRIPTION', |
||||||
117 | 'Hostname of this subsite (exclude protocol). Allows wildcards (*).' |
||||||
118 | )), |
||||||
119 | OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols) |
||||||
120 | ->setDescription(_t( |
||||||
121 | __CLASS__ . '.PROTOCOL_DESCRIPTION', |
||||||
122 | 'When generating links to this subsite, use the selected protocol. <br />' . |
||||||
123 | 'Selecting \'Automatic\' means subsite links will default to the current protocol.' |
||||||
124 | )), |
||||||
125 | CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary')) |
||||||
126 | ->setDescription(_t( |
||||||
127 | __CLASS__ . '.PROTOCOL_DESCRIPTION', |
||||||
128 | 'Mark this as the default domain for this subsite' |
||||||
129 | )) |
||||||
130 | ); |
||||||
131 | |||||||
132 | $this->extend('updateCMSFields', $fields); |
||||||
133 | return $fields; |
||||||
134 | } |
||||||
135 | |||||||
136 | /** |
||||||
137 | * |
||||||
138 | * @param bool $includerelations |
||||||
139 | * @return array |
||||||
140 | */ |
||||||
141 | public function fieldLabels($includerelations = true) |
||||||
142 | { |
||||||
143 | $labels = parent::fieldLabels($includerelations); |
||||||
144 | $labels['Domain'] = _t(__CLASS__ . '.DOMAIN', 'Domain'); |
||||||
145 | $labels['Protocol'] = _t(__CLASS__ . '.Protocol', 'Protocol'); |
||||||
146 | $labels['IsPrimary'] = _t(__CLASS__ . '.IS_PRIMARY', 'Is Primary Domain?'); |
||||||
147 | |||||||
148 | return $labels; |
||||||
0 ignored issues
–
show
|
|||||||
149 | } |
||||||
150 | |||||||
151 | /** |
||||||
152 | * Get the link to this subsite |
||||||
153 | * |
||||||
154 | * @return string |
||||||
155 | */ |
||||||
156 | public function Link() |
||||||
157 | { |
||||||
158 | return $this->getFullProtocol() . $this->Domain; |
||||||
159 | } |
||||||
160 | |||||||
161 | /** |
||||||
162 | * Gets the full protocol (including ://) for this domain |
||||||
163 | * |
||||||
164 | * @return string |
||||||
165 | */ |
||||||
166 | public function getFullProtocol() |
||||||
167 | { |
||||||
168 | switch ($this->Protocol) { |
||||||
169 | case self::PROTOCOL_HTTPS: |
||||||
170 | return 'https://'; |
||||||
171 | case self::PROTOCOL_HTTP: |
||||||
172 | return 'http://'; |
||||||
173 | default: |
||||||
174 | return Director::protocol(); |
||||||
175 | } |
||||||
176 | } |
||||||
177 | |||||||
178 | /** |
||||||
179 | * Retrieves domain name with wildcards substituted with actual values |
||||||
180 | * |
||||||
181 | * @todo Refactor domains into separate wildcards / primary domains |
||||||
182 | * |
||||||
183 | * @return string |
||||||
184 | */ |
||||||
185 | public function getSubstitutedDomain() |
||||||
186 | { |
||||||
187 | $currentHost = $_SERVER['HTTP_HOST']; |
||||||
188 | |||||||
189 | // If there are wildcards in the primary domain (not recommended), make some |
||||||
190 | // educated guesses about what to replace them with: |
||||||
191 | $domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain); |
||||||
192 | |||||||
193 | // Default to "subsite." prefix for first wildcard |
||||||
194 | // TODO Whats the significance of "subsite" in this context?! |
||||||
195 | $domain = preg_replace('/^\*\./', "subsite.", $domain); |
||||||
196 | |||||||
197 | // *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com' |
||||||
198 | $domain = str_replace('.www.', '.', $domain); |
||||||
199 | |||||||
200 | return $domain; |
||||||
201 | } |
||||||
202 | |||||||
203 | /** |
||||||
204 | * Get absolute link for this domain |
||||||
205 | * |
||||||
206 | * @return string |
||||||
207 | */ |
||||||
208 | public function getAbsoluteLink() |
||||||
209 | { |
||||||
210 | return $this->getFullProtocol() . $this->getSubstitutedDomain(); |
||||||
211 | } |
||||||
212 | |||||||
213 | /** |
||||||
214 | * Get absolute baseURL for this domain |
||||||
215 | * |
||||||
216 | * @return string |
||||||
217 | */ |
||||||
218 | public function absoluteBaseURL() |
||||||
219 | { |
||||||
220 | return Controller::join_links( |
||||||
221 | $this->getAbsoluteLink(), |
||||||
222 | Director::baseURL() |
||||||
223 | ); |
||||||
224 | } |
||||||
225 | } |
||||||
226 |