-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbootlib.php
318 lines (283 loc) · 11.5 KB
/
bootlib.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @category local
* @package local_vmoodle
* @author valery.fremaux ([email protected])
*/
function vmoodle_get_hostname() {
global $CFG;
// Special 3.5 and upper.
if ((strpos($CFG->wwwroot, 'https') === 0) ||
(!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') ||
!empty($CFG->overridetossl)) {
$protocol = 'https';
} else {
$protocol = 'http';
}
define('VMOODLE_BOOT', true);
/*
* This happens when a cli script needs to force one Vmoodle execution.
* This will force vmoodle switch using a hard defined constant.
*/
if (defined('CLI_VMOODLE_OVERRIDE')) {
$CFG->vmoodleroot = CLI_VMOODLE_OVERRIDE;
$CFG->vmoodlename = preg_replace('/https?:\/\//', '', CLI_VMOODLE_OVERRIDE);
$parts = explode('.', $CFG->vmoodlename);
$CFG->vhost = array_shift($parts);
return;
}
/*
* This is the standard case when each vmoodle runs on his own master single domain.
*/
$CFG->vmoodleroot = "{$protocol}://".@$_SERVER['HTTP_HOST'];
$CFG->vmoodlename = @$_SERVER['HTTP_HOST'];
if (empty($CFG->vmoodlename)) {
// Try again.
$CFG->vmoodleroot = "{$protocol}://".$_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != 80) {
$CFG->vmoodleroot .= ':'.$_SERVER['SERVER_PORT'];
}
$CFG->vmoodlename = $_SERVER['SERVER_NAME'];
}
/*
* When using a single domain with subpaths for instances, we need
* catch the first path element in host identity reference.
*/
if (!empty($CFG->vmoodleusesubpaths)) {
if (preg_match('#^/#', $CFG->dirroot)) {
// We are on a linux, (but not having full $CFG to know it).
$uri = preg_replace('#^/#', '', $_SERVER['REQUEST_URI']);
if (!preg_match('#/$#', $uri)) {
$path = dirname($uri);
} else {
$path = $uri;
}
$pathparts = explode('/', $path);
$firstpath = array_shift($pathparts);
if (($firstpath != '') && ($firstpath != '/') && ($firstpath != '.')) {
// If request uri goes into a subdir.
if (is_link($CFG->dirroot.'/'.$firstpath)) {
// Symbolic links in dirroot are characteristic to submoodledirs.
$CFG->vmoodleroot .= '/'.$firstpath;
$CFG->vmoodlename .= '/'.$firstpath;
}
}
} else {
echo "VMoodle sub paths are not supported on Windows systems. Change configuration. \n";
}
// echo "Baseroot : ".$CFG->vmoodleroot.'<br/>';
}
}
/**
* This is a boot lib that is required BEFORE we can have access
* to $CFG defines. Mysql type has been removed (php 5.5 ahead)
*/
function vmoodle_boot_configuration() {
global $CFG;
if (empty($CFG->dirroot)) {
die('VMoodle installs need $CFG->dirroot be defined explicitely in config.php.'."\n");
}
/*
* vconfig provides an bypassed configuration using vmoodle host definition
* from the vmoodle master instance
*/
$CFG->mainwwwroot = $CFG->wwwroot;
if ($CFG->vmoodleroot != $CFG->wwwroot) {
if (($CFG->vmasterdbtype == 'mysqli') || ($CFG->vmasterdbtype == 'mariadb')) {
$vmaster = new StdClass();
$vmaster->vdbtype = $CFG->vmasterdbtype;
$vmaster->vdbhost = $CFG->vmasterdbhost;
$vmaster->vdblogin = $CFG->vmasterdblogin;
$vmaster->vdbpass = $CFG->vmasterdbpass;
$vmaster->vdbname = $CFG->vmasterdbname;
if (!$sidecnx = vmoodle_make_connection($vmaster, true)) {
// If vmoodle cnx not valid.
die ('VMoodle master server ('.$vmaster->vdbtype.':'.$vmaster->vdbname.'@'.$vmaster->vdbhost.') unreachable from '.$CFG->wwwroot."\n");
}
$sql = "
SELECT
*
FROM
{$CFG->vmasterprefix}local_vmoodle
WHERE
vhostname = '$CFG->vmoodleroot'
";
$res = mysqli_query($sidecnx, $sql);
if ($res) {
if (mysqli_num_rows($res)) {
$vmoodle = mysqli_fetch_object($res);
vmoodle_feed_config($vmoodle);
} else {
die ("VMoodling (Mysql) : No configuration for this host : $CFG->vmoodleroot. Identified root is {$CFG->wwwroot} May be faked.\n");
}
} else {
die ("VMoodling (Mysql) : Could not fetch virtual moodle configuration\n");
}
} else if ($CFG->vmasterdbtype == 'postgres' || $CFG->vmasterdbtype == 'postgres7') {
$vmaster = new StdClass();
$vmaster->vdbtype = $CFG->vmasterdbtype;
$vmaster->vdbhost = $CFG->vmasterdbhost;
$vmaster->vdblogin = $CFG->vmasterdblogin;
$vmaster->vdbpass = $CFG->vmasterdbpass;
$sidecnx = vmoodle_make_connection($vmaster);
$sql = "
SELECT
*
FROM
{$CFG->vmasterprefix}local_vmoodle
WHERE
vhostname = '$CFG->vmoodleroot'
";
$res = pg_query($sidecnx, $sql);
if ($res) {
if (pg_num_rows($res)) {
$vmoodle = pg_fetch_object($res);
vmoodle_feed_config($vmoodle);
} else {
die ("VMoodling (Postgresql) : No configuration for this host. May be faked.\n");
}
pg_close($sidecnx);
} else {
die ("VMoodling (Postgresql) : Could not fetch virtual moodle configuration\n");
}
} else {
die("VMoodling : Unsupported Database for VMoodleMaster\n");
}
// Apply child default config if any.
/**
* Note that hard config cannot be anymore overriden by administration.
*
* Setup will additionnaly apply a local/defaults.php file if exists.
*/
if (!empty($CFG->vmoodlehardchildsdefaults)) {
$exclude = false;
if (!empty($CFG->vmoodlehardchildsdefaultsexclude)) {
if (preg_match('/'.$CFG->vmoodlehardchildsdefaultsexclude.'/', $CFG->vmoodleroot)) {
$exclude = true;
}
}
if (!$exclude) {
$default = $CFG->dirroot.'/local/defaults_'.$CFG->vmoodlehardchildsdefaults.'.php';
if (file_exists($default)) {
include($default);
} else {
if ($CFG->debug == E_ALL) {
throw new Exception("Trying to load an inexistant or unreacheable child defaults file\n");
}
}
}
}
} else if (empty($CFG->vmoodlenodefault)) {
// Apply master config hard defaults if any.
/**
* Note that hard config cannot be anymore overriden by administration.
*
* Setup will additionnaly apply a local/defaults.php file if exists.
*/
if (!empty($CFG->vmoodlehardmasterdefaults)) {
$default = $CFG->dirroot.'/local/defaults_'.$CFG->vmoodlehardmasterdefaults.'.php';
if (file_exists($default)) {
include($default);
} else {
if (@$CFG->debug == E_ALL) {
throw new Exception("Trying to load an inexistant or unreacheable master defaults file\n");
}
}
}
// Do nothing, just bypass.
assert(true);
} else {
die ("real moodle instance cannot be used in this VMoodle implementation\n");
}
}
/**
* provides a side connection to a vmoodle database
* mysql type has been removed.
* @param object $vmoodle a vmoodle database description
* @param boolean $binddb if true, the database is bound after connection.
* @return a connection
*/
function vmoodle_make_connection(&$vmoodle, $binddb = false, $interactive = false) {
global $CFG;
if (($vmoodle->vdbtype == 'mysqli') || ($vmoodle->vdbtype == 'mariadb')) {
// Important : force new link here.
$sidecnx = @mysqli_connect($vmoodle->vdbhost, $vmoodle->vdblogin, $vmoodle->vdbpass, '', 3306);
if (!$sidecnx) {
if (!empty($CFG->debug) && $CFG->debug == E_ALL) {
echo "VMoodle_make_connection : Server {$vmoodle->vdblogin}@{$vmoodle->vdbhost} unreachable";
if (!$interactive) die;
}
if (!$interactive) die ("VMoodle_make_connection : Server {$vmoodle->vdblogin}@{$vmoodle->vdbhost} unreachable");
return false;
}
if ($binddb) {
if (!mysqli_select_db($sidecnx, $vmoodle->vdbname)) {
if (!empty($CFG->debug) && $CFG->debug == E_ALL) {
echo ("VMoodle_make_connection : Database {$vmoodle->vdbname} not found");
if (!$interactive) die;
}
if (!$interactive) die ("VMoodle_make_connection : Database not found");
return false;
}
}
return $sidecnx;
} else if ($vmoodle->vdbtype == 'postgres') {
if (preg_match("/:/", $vmoodle->vdbhost)) {
list($host, $port) = explode(":", $vmoodle->vdbhost);
$port = "port=$port";
} else {
$host = $vmoodle->vdbhost;
$port = '';
}
$dbname = ($binddb) ? "dbname={$vmoodle->vdbname} " : '';
$cnxstr = "host={$host} {$port} user={$vmoodle->vdblogin} password={$vmoodle->vdbpass} {$dbname}";
$sidecnx = @pg_connect($cnxstr);
return $sidecnx;
} else {
echo "vmoodle_make_connection : Database not supported<br/>";
}
}
/**
* Get values from a virtual configuration and feed the apparent running config with them.
* @para object $vmoodle $vmoodle descriptor
*/
function vmoodle_feed_config($vmoodle) {
global $CFG;
$CFG->dbtype = $vmoodle->vdbtype;
$CFG->dbhost = $vmoodle->vdbhost;
$CFG->dbname = $vmoodle->vdbname;
// Security enhancement.
/*
* When using config forced logins and passwords for childs, the database
* register corresponding attributes can be cleared.
*/
if (empty($CFG->vchildsdblogin)) {
$CFG->dbuser = $vmoodle->vdblogin;
} else {
$CFG->dbuser = $CFG->vchildsdblogin;
}
if (empty($CFG->vchildsdbpass)) {
$CFG->dbpass = $vmoodle->vdbpass;
} else {
$CFG->dbpass = $CFG->vchildsdbpass;
}
$CFG->dboptions['dbpersist'] = $vmoodle->vdbpersist;
$CFG->prefix = $vmoodle->vdbprefix;
$CFG->wwwroot = $CFG->vmoodleroot;
$CFG->dataroot = $vmoodle->vdatapath;
}