forked from tsugiproject/tsugi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.php
209 lines (174 loc) · 6.61 KB
/
setup.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
<?php
// This is where we change the overall database version to trigger
// upgrade checking - don't change this unless you want to trigger
// database upgrade messages it should be the max of all versions in
// all database.php files.
$CFG->dbversion = 201501061040;
// Just turn this off to avoid security holes due to XML parsing
if ( function_exists ( 'libxml_disable_entity_loader' ) ) libxml_disable_entity_loader();
function die_with_error_log($msg, $extra=false, $prefix="DIE:") {
error_log($prefix.' '.$msg.' '.$extra);
print_stack_trace();
die($msg); // with error_log
}
function echo_log($msg) {
echo($msg);
error_log(str_replace("\n"," ",$msg));
}
function session_safe_id() {
$retval = session_id();
if ( strlen($retval) > 10 ) return '**********'.substr($retval,5);
}
function print_stack_trace() {
ob_start();
debug_print_backtrace();
$data = ob_get_clean();
error_log($data);
}
if ( isset($CFG->upgrading) && $CFG->upgrading === true ) require_once("upgrading.php");
// TODO: Before removing this, make sure to find code below that is dependent on lms_lib
// is covered properly - or perhaps decide this belongs here forever...
require_once $CFG->dirroot."/lib/lms_lib.php"; // During transition
// Check if we have been asked to do cookie or cookieless sessions
if ( defined('COOKIE_SESSION') ) {
// Do nothing - let the session be in a cookie
} else {
ini_set('session.use_cookies', '0');
ini_set('session.use_only_cookies',0);
ini_set('session.use_trans_sid',1);
}
if ( ! isset($CFG) ) die_with_error_log("Please configure this product using config.php");
if ( ! isset($CFG->staticroot) ) die_with_error_log('$CFG->staticroot not defined in config.php');
if ( ! isset($CFG->timezone) ) die_with_error_log('$CFG->timezone not defined in config.php');
if ( strpos($CFG->dbprefix, ' ') !== false ) die_with_error_log('$CFG->dbprefix cannot have spaces in it');
if ( !isset($CFG->ownername) ) $CFG->ownername = false;
if ( !isset($CFG->owneremail) ) $CFG->owneremail = false;
if ( !isset($CFG->providekeys) ) $CFG->providekeys = false;
// Set this to the temporary folder if not set - dev only
if ( ! isset($CFG->dataroot) ) {
$tmp = sys_get_temp_dir();
if (strlen($tmp) > 1 && substr($tmp, -1) == '/') $tmp = substr($tmp,0,-1);
$CFG->dataroot = $tmp;
}
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ALL );
ini_set('display_errors', 1);
if ( isset($CFG->sessionlifetime) ) {
ini_set('session.gc_maxlifetime', $CFG->sessionlifetime);
} else {
$CFG->sessionlifetime = ini_get('session.gc_maxlifetime');
}
date_default_timezone_set($CFG->timezone);
function htmlpre_utf8($string) {
return str_replace("<","<",$string);
}
function htmlspec_utf8($string) {
return htmlspecialchars($string,ENT_QUOTES,$encoding = 'UTF-8');
}
function htmlent_utf8($string) {
return htmlentities($string,ENT_QUOTES,$encoding = 'UTF-8');
}
// Makes sure a string is safe as an href
function safe_href($string) {
return str_replace(array('"', '<'),
array('"',''), $string);
}
// Convienence method to wrap sha256
function lti_sha256($val) {
return hash('sha256', $val);
}
// Convienence method to get the local path if we are doing
function route_get_local_path($dir) {
$uri = $_SERVER['REQUEST_URI']; // /tsugi/lti/some/cool/stuff
$root = $_SERVER['DOCUMENT_ROOT']; // /Applications/MAMP/htdocs
$cwd = $dir; // /Applications/MAMP/htdocs/tsugi/lti
if ( strlen($cwd) < strlen($root) + 1 ) return false;
$lwd = substr($cwd,strlen($root)); // /tsugi/lti
if ( strlen($root) < strlen($lwd) + 2 ) return false;
$local = substr($uri,strlen($lwd)+1); // some/cool/stuff
return $local;
}
function addSession($url) {
if ( ini_get('session.use_cookies') != '0' ) return $url;
if ( stripos($url, '&'.session_name().'=') > 0 ||
stripos($url, '?'.session_name().'=') > 0 ) return $url;
$parameter = session_name().'='.session_id();
if ( strpos($url, $parameter) !== false ) return $url;
$url = $url . (strpos($url,'?') > 0 ? "&" : "?");
$url = $url . $parameter;
return $url;
}
function reconstruct_query($baseurl, $newparms=false) {
foreach ( $_GET as $k => $v ) {
if ( $k == session_name() ) continue;
if ( is_array($newparms) && array_key_exists($k, $newparms) ) continue;
$baseurl = add_url_parm($baseurl, $k, $v);
}
if ( is_array($newparms) ) foreach ( $newparms as $k => $v ) {
$baseurl = add_url_parm($baseurl, $k, $v);
}
return $baseurl;
}
function add_url_parm($url, $key, $val) {
$url .= strpos($url,'?') === false ? '?' : '&';
$url .= urlencode($key) . '=' . urlencode($val);
return $url;
}
// Request headers for earlier version of PHP and nginx
// http://www.php.net/manual/en/function.getallheaders.php
if (!function_exists('apache_request_headers')) {
function apache_request_headers() {
foreach($_SERVER as $key=>$value) {
if (substr($key,0,5)=="HTTP_") {
$key=str_replace(" ","-",ucwords(strtolower(str_replace("_"," ",substr($key,5)))));
$out[$key]=$value;
} else {
$out[$key]=$value;
}
}
return $out;
}
}
// Convience method, pattern borrowed from WordPress
function __($message, $textdomain=false) {
if ( ! function_exists('gettext')) return $message;
if ( $textdomain === false ) {
return gettext($message);
} else {
return dgettext($textdomain, $message);
}
}
function _e($message, $textdomain=false) {
echo(__($message, $textdomain));
}
function _m($message, $textdomain=false) {
return __($message, "master");
}
function _me($message, $textdomain=false) {
echo(_m($message, $textdomain));
}
if (function_exists('bindtextdomain')) {
bindtextdomain("master", $CFG->dirroot."/locale");
}
// Set up the user's locale
if ( function_exists('bindtextdomain') && function_exists('textdomain') && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ) {
if ( class_exists('Locale') ) {
$locale = Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE']);
} else { // Crude fallback if it is missing
$pieces = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
$locale = $pieces[0];
}
putenv('LC_ALL='.$locale);
setlocale(LC_ALL, $locale);
$domain = getScriptFolder();
bindtextdomain($domain, getScriptPathFull()."/locale");
textdomain($domain);
}
// TODO: Create this as well related to OUTPUT. See Moodle.
// global $PAGE;
// Define these globals later.
global $OUTPUT, $USER, $CONTEXT, $LINK;
$USER = false;
$CONTEXT = false;
$LINK = false;
// No trailer