Skip to content

Instantly share code, notes, and snippets.

@arjenblokzijl
Created May 21, 2013 19:05
Show Gist options
  • Save arjenblokzijl/5622347 to your computer and use it in GitHub Desktop.
Save arjenblokzijl/5622347 to your computer and use it in GitHub Desktop.
Fixed an unknown issue on a ProcessWire module. This gist is just temporary.
<?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'] = "/{$subdomain}/" . ltrim($_GET['it'], '/');
// 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment