admin interface: MDL-11061 Incorrect check check on wwwroot causing invalid
[moodle.git] / lib / setuplib.php
CommitLineData
251387d0 1<?php // $Id$
2 // These functions are required very early in the Moodle
3 // setup process, before any of the main libraries are
d3f9f1f8 4 // loaded.
5
6
6a525ce2 7/**
8 * Simple class
9 */
10class object {};
11
251387d0 12/**
13 * Base Moodle Exception class
14 */
15class moodle_exception extends Exception {
16 public $errorcode;
17 public $module;
18 public $a;
19 public $link;
eee5d9bb 20 public $debuginfo;
251387d0 21
22 /**
23 * Constructor
24 * @param string $errorcode The name of the string from error.php to print
25 * @param string $module name of module
26 * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page.
27 * @param object $a Extra words and phrases that might be required in the error string
eee5d9bb 28 * @param string $debuginfo optional debugging information
251387d0 29 */
5ca18631 30 function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) {
31 if (empty($module) || $module == 'moodle' || $module == 'core') {
251387d0 32 $module = 'error';
33 }
34
5ca18631 35 $this->errorcode = $errorcode;
36 $this->module = $module;
37 $this->link = $link;
38 $this->a = $a;
39 $this->debuginfo = $debuginfo;
251387d0 40
5ca18631 41 $message = get_string($errorcode, $module, $a);
251387d0 42
43 parent::__construct($message, 0);
44 }
45}
46
655bbf51 47/**
9214025e 48 * Exception indicating programming error, must be fixed by a programer.
655bbf51 49 */
50class coding_exception extends moodle_exception {
51
52 /**
53 * Constructor
54 * @param string $hint short description of problem
55 * @param string $debuginfo detailed information how to fix problem
56 */
57 function __construct($hint, $debuginfo=null) {
58 parent::__construct('codingerror', 'debug', '', $hint, $debuginfo);
59 }
60}
61
251387d0 62/**
63 * Default exception handler, uncought exceptions are equivalent to using print_error()
64 */
65function default_exception_handler($ex) {
1fe1d104 66 global $CFG;
67
251387d0 68 $backtrace = $ex->getTrace();
69 $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex));
70 array_unshift($backtrace, $place);
71
72 if ($ex instanceof moodle_exception) {
1fe1d104 73 if (!isset($CFG->theme) or !isset($CFG->stylesheets)) {
ce152606 74 _print_early_error($ex->errorcode, $ex->module, $ex->a, $backtrace, $ex->debuginfo);
1fe1d104 75 } else {
76 _print_normal_error($ex->errorcode, $ex->module, $ex->a, $ex->link, $backtrace, $ex->debuginfo);
77 }
251387d0 78 } else {
1fe1d104 79 if (!isset($CFG->theme) or !isset($CFG->stylesheets)) {
80 _print_early_error('generalexceptionmessage', 'error', $ex->getMessage());
81 } else {
82 _print_normal_error('generalexceptionmessage', 'error', $ex->getMessage(), '', $backtrace);
83 }
251387d0 84 }
85}
6a525ce2 86
11e7b506 87/**
88 * Initialises $FULLME and friends.
89 * @return void
90 */
91function initialise_fullme() {
92 global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT;
93
94 $url = parse_url($CFG->wwwroot);
37ccf1fe 95 if (!isset($url['path'])) {
96 $url['path'] = '';
97 }
98 $url['path'] .= '/';
11e7b506 99
100 if (CLI_SCRIPT) {
101 // urls do not make much sense in CLI scripts
102 $backtrace = debug_backtrace();
103 $topfile = array_pop($backtrace);
104 $topfile = realpath($topfile['file']);
105 $dirroot = realpath($CFG->dirroot);
106
107 if (strpos($topfile, $dirroot) !== 0) {
108 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null;
109 } else {
110 $relme = substr($topfile, strlen($dirroot));
111 $relme = str_replace('\\', '/', $relme); // Win fix
112 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = $relme;
113 }
114
115 return;
116 }
117
118 $rurl = array();
119 $hostport = explode(':', $_SERVER['HTTP_HOST']);
120 $rurl['host'] = reset($hostport);
121 $rurl['port'] = $_SERVER['SERVER_PORT'];
122 $rurl['path'] = $_SERVER['SCRIPT_NAME']; // script path without slash arguments
123
124 if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) {
125 //Apache server
126 $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https';
127 $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
128
129 } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
130 //lighttpd
131 $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https';
132 $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
133
134 } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) {
135 //IIS
136 $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https';
137 $rurl['fullpath'] = $_SERVER['SCRIPT_NAME'];
138
139 // NOTE: ignore PATH_INFO because it is incorrectly encoded using 8bit filesystem legacy encoding in IIS
140 // since 2.0 we rely on iis rewrite extenssion like Helicon ISAPI_rewrite
21517960 141 // example rule: RewriteRule ^([^\?]+?\.php)(\/.+)$ $1\?file=$2 [QSA]
11e7b506 142
143 if ($_SERVER['QUERY_STRING'] != '') {
991ec2ee 144 $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING'];
11e7b506 145 }
146 $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility
147
148 } else {
149 print_error('usupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']);
150 }
151
152 if (strpos($rurl['path'], $url['path']) === 0) {
37ccf1fe 153 $SCRIPT = substr($rurl['path'], strlen($url['path'])-1);
11e7b506 154 } else {
155 // probably some weird external script
156 $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null;
157 return;
158 }
159
160 // $CFG->sslproxy specifies if external SSL apliance is used (server using http, ext box translating everything to https)
161 if (empty($CFG->sslproxy)) {
162 if ($rurl['scheme'] == 'http' and $url['scheme'] == 'https') {
163 print_error('sslonlyaccess', 'error');
164 }
165 }
166
167 // $CFG->reverseproxy specifies if reverse proxy server used - used in advanced load balancing setups only!
168 // this is not supposed to solve lan/wan access problems!!!!!
169 if (empty($CFG->reverseproxy)) {
170 if (($rurl['host'] != $url['host']) or (!empty($url['port']) and $rurl['port'] != $url['port'])) {
171 print_error('wwwrootmismatch', 'error', '', $CFG->wwwroot);
172 }
f1e715ff 173 } else {
174 if ($rurl['host'] == $url['host']) {
175 // hopefully this will stop all those "clever" admins trying to set up moodle with two different addresses in intranet and Internet
176 print_error('reverseproxyabused', 'error');
177 }
11e7b506 178 }
179
180 $FULLME = $rurl['scheme'].'://'.$url['host'];
181 if (!empty($url['port'])) {
182 $FULLME .= ':'.$url['port'];
183 }
184 $FULLSCRIPT = $FULLME.$rurl['path'];
185 $FULLME = $FULLME.$rurl['fullpath'];
186 $ME = $rurl['fullpath'];
187
188}
189
d3f9f1f8 190/**
191 * Initializes our performance info early.
192 *
193 * Pairs up with get_performance_info() which is actually
251387d0 194 * in moodlelib.php. This function is here so that we can
195 * call it before all the libs are pulled in.
d3f9f1f8 196 *
197 * @uses $PERF
198 */
199function init_performance_info() {
200
6fc4ad72 201 global $PERF, $CFG, $USER;
251387d0 202
ab130a0b 203 $PERF = new object();
d3f9f1f8 204 $PERF->logwrites = 0;
205 if (function_exists('microtime')) {
206 $PERF->starttime = microtime();
207 }
208 if (function_exists('memory_get_usage')) {
209 $PERF->startmemory = memory_get_usage();
210 }
211 if (function_exists('posix_times')) {
251387d0 212 $PERF->startposixtimes = posix_times();
d3f9f1f8 213 }
b65567f4 214 if (function_exists('apd_set_pprof_trace')) {
215 // APD profiling
6fc4ad72 216 if ($USER->id > 0 && $CFG->perfdebug >= 15) {
251387d0 217 $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id;
6fc4ad72 218 mkdir($tempdir);
219 apd_set_pprof_trace($tempdir);
220 $PERF->profiling = true;
221 }
b65567f4 222 }
d3f9f1f8 223}
224
76f3815b 225/**
226 * Function to raise the memory limit to a new value.
227 * Will respect the memory limit if it is higher, thus allowing
228 * settings in php.ini, apache conf or command line switches
229 * to override it
230 *
231 * The memory limit should be expressed with a string (eg:'64M')
232 *
233 * @param string $newlimit the new memory limit
234 * @return bool
235 */
11e7b506 236function raise_memory_limit($newlimit) {
76f3815b 237
238 if (empty($newlimit)) {
239 return false;
240 }
241
242 $cur = @ini_get('memory_limit');
243 if (empty($cur)) {
244 // if php is compiled without --enable-memory-limits
245 // apparently memory_limit is set to ''
246 $cur=0;
247 } else {
248 if ($cur == -1){
249 return true; // unlimited mem!
250 }
251 $cur = get_real_size($cur);
252 }
253
254 $new = get_real_size($newlimit);
255 if ($new > $cur) {
256 ini_set('memory_limit', $newlimit);
257 return true;
258 }
259 return false;
260}
261
262/**
263 * Converts numbers like 10M into bytes.
264 *
265 * @param mixed $size The size to be converted
266 * @return mixed
267 */
268function get_real_size($size=0) {
269 if (!$size) {
270 return 0;
271 }
11e7b506 272 $scan = array();
76f3815b 273 $scan['MB'] = 1048576;
274 $scan['Mb'] = 1048576;
275 $scan['M'] = 1048576;
276 $scan['m'] = 1048576;
277 $scan['KB'] = 1024;
278 $scan['Kb'] = 1024;
279 $scan['K'] = 1024;
280 $scan['k'] = 1024;
281
282 while (list($key) = each($scan)) {
283 if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) {
284 $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key];
285 break;
286 }
287 }
288 return $size;
289}
290
d3f9f1f8 291/**
292 * Create a directory.
293 *
294 * @uses $CFG
295 * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1
296 * param bool $shownotices If true then notification messages will be printed out on error.
297 * @return string|false Returns full path to directory if successful, false if not
298 */
299function make_upload_directory($directory, $shownotices=true) {
300
301 global $CFG;
302
303 $currdir = $CFG->dataroot;
304
305 umask(0000);
306
307 if (!file_exists($currdir)) {
308 if (! mkdir($currdir, $CFG->directorypermissions)) {
309 if ($shownotices) {
251387d0 310 echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '.
d3f9f1f8 311 $currdir .' with web server write access</div>'."<br />\n";
312 }
313 return false;
314 }
d100b8a0 315 }
316
317 // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open
318 if (!file_exists($currdir.'/.htaccess')) {
d3f9f1f8 319 if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety
d389b128 320 @fwrite($handle, "deny from all\r\nAllowOverride None\r\n");
d3f9f1f8 321 @fclose($handle);
322 }
323 }
324
325 $dirarray = explode('/', $directory);
326
327 foreach ($dirarray as $dir) {
328 $currdir = $currdir .'/'. $dir;
329 if (! file_exists($currdir)) {
330 if (! mkdir($currdir, $CFG->directorypermissions)) {
331 if ($shownotices) {
251387d0 332 echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('.
d3f9f1f8 333 $currdir .')</div>'."<br />\n";
334 }
335 return false;
336 }
337 //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it
338 }
339 }
340
341 return $currdir;
342}
343
419e1d93 344function init_memcached() {
345 global $CFG, $MCACHE;
346
f917d0ea 347 include_once($CFG->libdir . '/memcached.class.php');
348 $MCACHE = new memcached;
349 if ($MCACHE->status()) {
350 return true;
251387d0 351 }
f917d0ea 352 unset($MCACHE);
251387d0 353 return false;
419e1d93 354}
355
2142d492 356function init_eaccelerator() {
357 global $CFG, $MCACHE;
358
359 include_once($CFG->libdir . '/eaccelerator.class.php');
360 $MCACHE = new eaccelerator;
f917d0ea 361 if ($MCACHE->status()) {
2142d492 362 return true;
251387d0 363 }
2142d492 364 unset($MCACHE);
365 return false;
366}
367
368
369
d3f9f1f8 370?>