-
Notifications
You must be signed in to change notification settings - Fork 18
/
Multisite.module
92 lines (71 loc) · 2.75 KB
/
Multisite.module
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
<?php
/**
*
* Multisite module for Processwire
* (C) 2012 Antti Peisa, Avoine Oy
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class Multisite extends WireData implements Module, ConfigurableModule {
public static function getModuleInfo() {
return array(
'title' => 'Multisite',
'version' => 001,
'summary' => 'Allows multiple sites with different domains run from single PW-site and database.',
'singular' => true,
'autoload' => true,
);
}
public function init() {
$this->subdomainsArray = explode("\n", strtolower($this->subdomains));
$this->it = (isset($_GET['it'])) ? ltrim($_GET['it'], '/') : '';
foreach($this->subdomainsArray as $subdomain) {
$subdomain = trim($subdomain);
$config = wire('config');
$httpHost = strtolower(wire('config')->httpHost);
if(strpos($httpHost, $subdomain) !== false) {
$this->subdomain = $subdomain;
// change path like /about/contact/ to /campaign.com/about/contact/
$_GET['it'] = (isset($_GET['it'])) ? "/{$subdomain}/" . ltrim($_GET['it'], '/') : "/{$subdomain}/";
// hook for path method to make path() and url() methods context aware
$this->addHookAfter('Page::path', $this, 'modifyPath');
break;
}
}
}
public function ready() {
// if $this->subdomain was valid in init(), then no need to do more here.
if($this->subdomain) return;
$this->subdomain = $this->page->rootParent->name;
// if rootParent->name isn't a subdomain, then no need to continue
if(!in_array($this->subdomain, $this->subdomainsArray)) return;
// if subdomain is visible on url, we might wanna redirect
if(strpos($this->it, $this->subdomain) === 0 && !$this->page->editable()) {
$http = ($this->config->https) ? "https://" : "http://";
$url = $http . $this->subdomain;
$url .= str_replace("/{$this->subdomain}/", '/', $this->page->url);
$this->session->redirect($url);
}
}
public function modifyPath($event) {
$event->return = str_replace("{$this->subdomain}/", '', $event->return);
}
static public function getModuleConfigInputfields(array $data) {
$fields = new InputfieldWrapper();
$modules = wire('modules');
$field = $modules->get("InputfieldTextarea");
$field->name = "subdomains";
$field->label = "Other sites running from same install";
$field->description = "Add each domain for their own line. Don't add http or https, just simple domain like: www.example.com tai campaign.example.com";
$field->notes = "IMPORTANT: create homepage for each of these domains right on the root. Name should be exactly same as the domain you use here.";
$field->attr('value', $data['subdomains']);
$fields->add($field);
return $fields;
}
}