-
Notifications
You must be signed in to change notification settings - Fork 1
/
multisite.php
131 lines (96 loc) · 2.69 KB
/
multisite.php
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
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2017 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
* Developed by CaMer0n of e107.org
*/
class multisite
{
private static $debug = false;
public static function load($mySQLdefaultdb,$systemDir)
{
if(defined('e_DEBUG') && e_DEBUG===true && (basename($_SERVER["SCRIPT_NAME"]) !== 'thumb.php') && !defined('e_AJAX_REQUEST') && empty($_POST) && empty($_GET['e_ajax']))
{
self::$debug = true;
echo "<!-- DB: ".$mySQLdefaultdb." -->\n";
echo "<!-- Host: ".$_SERVER['HTTP_HOST']." -->\n";
echo "<!-- URI: ".$_SERVER['REQUEST_URI']." -->\n";
}
$path = '';
while (!file_exists("{$path}class2.php"))
{
$path .= "../";
}
$systemDir = $path.$systemDir;
if(empty($systemDir) || !file_exists($systemDir."multisite.json"))
{
echo (self::$debug === true) ? "<!-- SystemDir: ".$systemDir." -->" : null;
return array('database'=>$mySQLdefaultdb);
}
if($data = file_get_contents($systemDir."multisite.json"))
{
$config = json_decode($data,true);
if($mysqlData = self::detect($config))
{
define('e_MULTISITE_IN_USE',$mysqlData['database']);
return $mysqlData;
}
if(self::$debug === true)
{
echo "<!-- Multisite: No Match -->\n";
}
}
else
{
echo (self::$debug === true) ? "<!-- Multisite: Couldn't load json: ".$systemDir."multisite.json -->\n" : null;
}
return array('database'=>$mySQLdefaultdb);
}
private static function detect($config)
{
foreach($config as $site)
{
if(empty($site['active']) || empty($site['mysql']['database']) || empty($site['match']))
{
if(self::$debug === true)
{
echo "<!-- Skipping ".$site['name']." active: ".$site['active']." -->\n";
}
continue;
}
if($site['haystack'] === 'host' && ($_SERVER['HTTP_HOST'] === $site['match']))
{
return $site['mysql'];
}
elseif($site['haystack'] === 'url')
{
$regex = '/^\/'.$site['match'].'\//';
if(preg_match($regex, $_SERVER['REQUEST_URI'], $m))
{
define('e_HTTP',$m[0]);
define('e_SELF_OVERRIDE',true);
define('e_MULTISITE_MATCH', $site['match']);
// define('THEME','bootstrap3/');
return $site['mysql'];
}
if(self::$debug === true)
{
echo "<!-- Match Failed -->\n";
echo "<!-- Regex: ".$regex." -->\n";
echo "<!-- Hackstack ".$_SERVER['REQUEST_URI']." -->";
}
}
}
return false;
}
}
$multiMySQL = multisite::load($mySQLdefaultdb, $SYSTEM_DIRECTORY);
$mySQLdefaultdb = $multiMySQL['database'];
if(!empty($multiMySQL['prefix']))
{
$mySQLprefix = $multiMySQL['prefix'];
}
unset($multiMySQL);