2012-03-25 18:35:01 +02:00
|
|
|
<?php
|
|
|
|
|
2017-05-24 12:32:05 +02:00
|
|
|
namespace SilverStripe\Subsites\Model;
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\Control\Director;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\FieldList;
|
2017-05-29 13:42:42 +02:00
|
|
|
use SilverStripe\Forms\OptionsetField;
|
2016-09-22 16:38:29 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
2017-04-23 22:23:34 +02:00
|
|
|
use SilverStripe\Subsites\Forms\WildcardDomainField;
|
2016-09-22 16:38:29 +02:00
|
|
|
|
2014-07-14 07:17:44 +02:00
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* @property string $Domain domain name of this subsite. Can include wildcards. Do not include the URL scheme here
|
|
|
|
* @property string $Protocol Required protocol (http or https) if only one is supported. 'automatic' implies
|
|
|
|
* that any links to this subsite should use the current protocol, and that both are supported.
|
|
|
|
* @property string $SubstitutedDomain Domain name with all wildcards filled in
|
|
|
|
* @property string $FullProtocol Full protocol including ://
|
|
|
|
* @property bool $IsPrimary Is this the primary subdomain?
|
2014-07-14 07:17:44 +02:00
|
|
|
*/
|
2017-05-24 15:26:28 +02:00
|
|
|
class SubsiteDomain extends DataObject
|
|
|
|
{
|
2013-05-06 12:21:09 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
private static $table_name = 'SubsiteDomain';
|
2017-05-24 14:31:56 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-24 15:26:28 +02:00
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2017-06-01 14:49:55 +02:00
|
|
|
private static $default_sort = '"IsPrimary" DESC';
|
2017-05-24 15:26:28 +02:00
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
/** *
|
2017-04-23 22:23:34 +02:00
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $db = [
|
2017-06-01 14:49:55 +02:00
|
|
|
'Domain' => 'Varchar(255)',
|
|
|
|
'Protocol' => "Enum('http,https,automatic','automatic')",
|
|
|
|
'IsPrimary' => 'Boolean',
|
2017-04-23 22:23:34 +02:00
|
|
|
];
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* Specifies that this subsite is http only
|
2017-05-29 13:42:42 +02:00
|
|
|
*/
|
|
|
|
const PROTOCOL_HTTP = 'http';
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Specifies that this subsite is https only
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
const PROTOCOL_HTTPS = 'https';
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Specifies that this subsite supports both http and https
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
const PROTOCOL_AUTOMATIC = 'automatic';
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Get the descriptive title for this domain
|
2017-05-24 15:26:28 +02:00
|
|
|
*
|
2017-05-29 13:42:42 +02:00
|
|
|
* @return string
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-05-29 13:42:42 +02:00
|
|
|
public function getTitle()
|
2017-05-24 15:26:28 +02:00
|
|
|
{
|
2017-05-29 13:42:42 +02:00
|
|
|
return $this->Domain;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-04-23 22:23:34 +02:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $has_one = [
|
2017-06-01 14:49:55 +02:00
|
|
|
'Subsite' => Subsite::class,
|
2017-04-23 22:23:34 +02:00
|
|
|
];
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
|
|
|
* @config
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $summary_fields = [
|
|
|
|
'Domain',
|
|
|
|
'IsPrimary',
|
|
|
|
];
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/*** @config
|
2017-05-29 13:42:42 +02:00
|
|
|
* @var array
|
2017-05-24 15:26:28 +02:00
|
|
|
*/
|
2017-04-23 22:23:34 +02:00
|
|
|
private static $casting = [
|
2017-05-29 13:42:42 +02:00
|
|
|
'SubstitutedDomain' => 'Varchar',
|
|
|
|
'FullProtocol' => 'Varchar',
|
|
|
|
'AbsoluteLink' => 'Varchar',
|
2017-04-23 22:23:34 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whenever a Subsite Domain is written, rewrite the hostmap
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function onAfterWrite()
|
|
|
|
{
|
|
|
|
Subsite::writeHostMap();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
2017-08-28 11:52:32 +02:00
|
|
|
* @return FieldList
|
2017-04-23 22:23:34 +02:00
|
|
|
*/
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$protocols = [
|
2017-05-29 13:42:42 +02:00
|
|
|
self::PROTOCOL_HTTP => _t('SubsiteDomain.PROTOCOL_HTTP', 'http://'),
|
|
|
|
self::PROTOCOL_HTTPS => _t('SubsiteDomain.PROTOCOL_HTTPS', 'https://'),
|
|
|
|
self::PROTOCOL_AUTOMATIC => _t('SubsiteDomain.PROTOCOL_AUTOMATIC', 'Automatic')
|
2017-04-23 22:23:34 +02:00
|
|
|
];
|
|
|
|
$fields = new FieldList(
|
|
|
|
WildcardDomainField::create('Domain', $this->fieldLabel('Domain'), null, 255)
|
|
|
|
->setDescription(_t(
|
2017-05-29 13:42:42 +02:00
|
|
|
'SubsiteDomain.DOMAIN_DESCRIPTION',
|
|
|
|
'Hostname of this subsite (exclude protocol). Allows wildcards (*).'
|
|
|
|
)),
|
|
|
|
OptionsetField::create('Protocol', $this->fieldLabel('Protocol'), $protocols)
|
|
|
|
->setDescription(_t(
|
|
|
|
'SubsiteDomain.PROTOCOL_DESCRIPTION',
|
|
|
|
'When generating links to this subsite, use the selected protocol. <br />' .
|
|
|
|
'Selecting \'Automatic\' means subsite links will default to the current protocol.'
|
2015-11-24 05:15:08 +01:00
|
|
|
)),
|
|
|
|
CheckboxField::create('IsPrimary', $this->fieldLabel('IsPrimary'))
|
|
|
|
->setDescription(_t(
|
|
|
|
'SubsiteDomain.PROTOCOL_DESCRIPTION',
|
|
|
|
'Mark this as the default domain for this subsite'
|
|
|
|
))
|
2015-11-23 04:53:45 +01:00
|
|
|
);
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
$this->extend('updateCMSFields', $fields);
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param bool $includerelations
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fieldLabels($includerelations = true)
|
|
|
|
{
|
|
|
|
$labels = parent::fieldLabels($includerelations);
|
|
|
|
$labels['Domain'] = _t('SubsiteDomain.DOMAIN', 'Domain');
|
2017-05-29 13:42:42 +02:00
|
|
|
$labels['Protocol'] = _t('SubsiteDomain.Protocol', 'Protocol');
|
|
|
|
$labels['IsPrimary'] = _t('SubsiteDomain.IS_PRIMARY', 'Is Primary Domain?');
|
2017-05-24 15:26:28 +02:00
|
|
|
|
|
|
|
return $labels;
|
|
|
|
}
|
|
|
|
|
2017-05-29 13:42:42 +02:00
|
|
|
/**
|
|
|
|
* Get the link to this subsite
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link()
|
|
|
|
{
|
|
|
|
return $this->getFullProtocol() . $this->Domain;
|
2017-05-24 15:26:28 +02:00
|
|
|
}
|
2017-05-29 13:42:42 +02:00
|
|
|
|
2017-04-23 22:23:34 +02:00
|
|
|
/**
|
2017-05-29 13:42:42 +02:00
|
|
|
* Gets the full protocol (including ://) for this domain
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getFullProtocol()
|
|
|
|
{
|
|
|
|
switch ($this->Protocol) {
|
2017-04-23 22:23:34 +02:00
|
|
|
case self::PROTOCOL_HTTPS: {
|
2017-05-29 13:42:42 +02:00
|
|
|
return 'https://';
|
|
|
|
}
|
2017-04-23 22:23:34 +02:00
|
|
|
case self::PROTOCOL_HTTP: {
|
2017-05-29 13:42:42 +02:00
|
|
|
return 'http://';
|
|
|
|
}
|
2017-04-23 22:23:34 +02:00
|
|
|
default: {
|
2017-05-29 13:42:42 +02:00
|
|
|
return Director::protocol();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves domain name with wildcards substituted with actual values
|
|
|
|
*
|
|
|
|
* @todo Refactor domains into separate wildcards / primary domains
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSubstitutedDomain()
|
|
|
|
{
|
|
|
|
$currentHost = $_SERVER['HTTP_HOST'];
|
|
|
|
|
|
|
|
// If there are wildcards in the primary domain (not recommended), make some
|
|
|
|
// educated guesses about what to replace them with:
|
2017-04-23 22:23:34 +02:00
|
|
|
$domain = preg_replace('/\.\*$/', ".{$currentHost}", $this->Domain);
|
2017-05-29 13:42:42 +02:00
|
|
|
|
|
|
|
// Default to "subsite." prefix for first wildcard
|
2015-11-24 05:15:08 +01:00
|
|
|
// TODO Whats the significance of "subsite" in this context?!
|
|
|
|
$domain = preg_replace('/^\*\./', "subsite.", $domain);
|
2017-05-29 13:42:42 +02:00
|
|
|
|
|
|
|
// *Only* removes "intermediate" subdomains, so 'subdomain.www.domain.com' becomes 'subdomain.domain.com'
|
|
|
|
$domain = str_replace('.www.', '.', $domain);
|
|
|
|
|
|
|
|
return $domain;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get absolute link for this domain
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAbsoluteLink()
|
|
|
|
{
|
|
|
|
return $this->getFullProtocol() . $this->getSubstitutedDomain();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get absolute baseURL for this domain
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function absoluteBaseURL()
|
|
|
|
{
|
|
|
|
return Controller::join_links(
|
|
|
|
$this->getAbsoluteLink(),
|
2015-11-24 05:15:08 +01:00
|
|
|
Director::baseURL()
|
|
|
|
);
|
2017-04-23 22:23:34 +02:00
|
|
|
}
|
2013-04-30 07:53:09 +02:00
|
|
|
}
|