forked from ushahidi/platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultisiteManager.php
More file actions
140 lines (121 loc) · 3.55 KB
/
Copy pathMultisiteManager.php
File metadata and controls
140 lines (121 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php
/**
* Ushahidi Platform
*
* @author Ushahidi Team <team@ushahidi.com>
* @package Ushahidi\Platform
* @copyright 2014 Ushahidi
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3)
*/
namespace Ushahidi\App\Multisite;
use Illuminate\Contracts\Events\Dispatcher;
class MultisiteManager
{
protected $enabled;
protected $domain;
protected $currentSite;
/**
* @param array $config
*/
public function __construct($config, SiteRepository $repo, Dispatcher $events)
{
$this->enabled = $config['enabled'];
$this->domain = $config['domain'];
$this->repo = $repo;
$this->events = $events;
}
public function getDomain() : string
{
return $this->domain;
}
public function enabled() : bool
{
return (bool) $this->enabled;
}
/**
* @param string $host
* @return void
* @throws SiteNotFoundException
*/
public function setSiteFromHost(string $host)
{
// Validate the host
// This is very permissive filter. We're not using FILTER_FLAG_HOSTNAME
// because it would block IDNs
if (!filter_var($host, FILTER_VALIDATE_DOMAIN)) {
throw new \InvalidArgumentException();
}
// If $domain is set and we're at a subdomain of $domain...
if ($this->domain and substr($host, strlen($this->domain) * -1) == $this->domain) {
// ... grab just the subdomain
$subdomain = substr($host, 0, (strlen($this->domain) * -1) -1);
// ... and search for the subdomain/domain combination
$site = $this->repo->getByDomain($subdomain, $this->domain);
} else {
// ... otherwise search for the whole domain
$site = $this->repo->getByDomain('', $host);
}
// If we didn't find the site...
if (!$site) {
// ... throw SiteNotFound
throw new SiteNotFoundException();
}
$this->setSite($site);
}
/**
* @param int $siteId
* @return void
* @throws SiteNotFoundException
*/
public function setSiteById(int $siteId)
{
$site = $this->repo->getById($siteId);
// If we didn't find the site...
if (!$site) {
// ... throw SiteNotFound
throw new SiteNotFoundException();
}
$this->setSite($site);
}
/**
* Set site to fake default for single site mode
*/
public function setDefaultSite($domain = '')
{
$this->setSite(new Site(
[
'id' => 0,
'status' => 'deployed',
'domain' => $domain,
'db_host' => config('database.connections.mysql.host'),
'db_name' => config('database.connections.mysql.database'),
'db_username' => config('database.connections.mysql.username'),
'db_password' => config('database.connections.mysql.password'),
]
));
}
/**
* @param Site $site
* @return void
*/
public function setSite(Site $site)
{
$this->currentSite = $site;
// Trigger DB changes, etc
$this->events->dispatch('multisite.site.changed', ['site' => $site]);
}
/**
* @return Site
*/
public function getSite()
{
return $this->currentSite ?: false;
}
/**
* @return Site
*/
public function getSiteId()
{
return $this->currentSite ? $this->currentSite->getId() : false;
}
}