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 | */ |
10 | class object {}; |
11 | |
251387d0 |
12 | /** |
13 | * Base Moodle Exception class |
14 | */ |
15 | class 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 | /** |
cce1b0b9 |
48 | * Exception indicating programming error, must be fixed by a programer. For example |
49 | * a core API might throw this type of exception if a plugin calls it incorrectly. |
655bbf51 |
50 | */ |
51 | class coding_exception extends moodle_exception { |
655bbf51 |
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 | |
cce1b0b9 |
62 | /** |
63 | * An exception that indicates something really weird happended. For example, |
64 | * if you do switch ($context->contextlevel), and have one case for each |
65 | * CONTEXT_... constant. You might throw an invalid_state_exception in the |
f630a546 |
66 | * default case, to just in case something really weird is going on, and |
8dc0ae8f |
67 | * $context->contextlevel is invalid - rather than ignoring this possibility. |
cce1b0b9 |
68 | */ |
69 | class invalid_state_exception extends moodle_exception { |
70 | /** |
71 | * Constructor |
72 | * @param string $hint short description of problem |
73 | * @param string $debuginfo optional more detailed information |
74 | */ |
75 | function __construct($hint, $debuginfo=null) { |
76 | parent::__construct('invalidstatedetected', 'debug', '', $hint, $debuginfo); |
77 | } |
78 | } |
79 | |
251387d0 |
80 | /** |
81 | * Default exception handler, uncought exceptions are equivalent to using print_error() |
82 | */ |
83 | function default_exception_handler($ex) { |
1fe1d104 |
84 | global $CFG; |
85 | |
251387d0 |
86 | $backtrace = $ex->getTrace(); |
87 | $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex)); |
88 | array_unshift($backtrace, $place); |
89 | |
0ae8f5fc |
90 | $earlyerror = !isset($CFG->theme) || !isset($CFG->stylesheets); |
91 | foreach ($backtrace as $stackframe) { |
92 | if (isset($stackframe['function']) && $stackframe['function'] == 'print_header') { |
93 | $earlyerror = true; |
94 | break; |
1fe1d104 |
95 | } |
0ae8f5fc |
96 | } |
97 | |
98 | if ($ex instanceof moodle_exception) { |
99 | $errorcode = $ex->errorcode; |
100 | $module = $ex->module; |
101 | $a = $ex->a; |
102 | $link = $ex->link; |
103 | $debuginfo = $ex->debuginfo; |
251387d0 |
104 | } else { |
0ae8f5fc |
105 | $errorcode = 'generalexceptionmessage'; |
106 | $module = 'error'; |
107 | $a = $ex->getMessage(); |
108 | $link = ''; |
109 | $debuginfo = null; |
110 | } |
111 | |
112 | if ($earlyerror) { |
113 | _print_early_error($errorcode, $module, $a, $backtrace, $debuginfo); |
114 | } else { |
115 | _print_normal_error($errorcode, $module, $a, $link, $backtrace, $debuginfo); |
251387d0 |
116 | } |
117 | } |
6a525ce2 |
118 | |
fbf2c91e |
119 | /** |
120 | * This function verifies the sanity of PHP configuration |
121 | * and stops execution if anything critical found. |
122 | */ |
123 | function setup_validate_php_configuration() { |
124 | // this must be very fast - no slow checks here!!! |
125 | |
126 | if (ini_get_bool('register_globals')) { |
127 | print_error('globalswarning', 'admin'); |
128 | } |
129 | if (ini_get_bool('session.auto_start')) { |
130 | print_error('sessionautostartwarning', 'admin'); |
131 | } |
132 | if (ini_get_bool('magic_quotes_runtime')) { |
133 | print_error('fatalmagicquotesruntime', 'admin'); |
134 | } |
135 | } |
136 | |
11e7b506 |
137 | /** |
75781f87 |
138 | * Initialises $FULLME and friends. Private function. Should only be called from |
139 | * setup.php. |
11e7b506 |
140 | */ |
141 | function initialise_fullme() { |
142 | global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT; |
143 | |
75781f87 |
144 | // Detect common config error. |
84b88cfd |
145 | if (substr($CFG->wwwroot, -1) == '/') { |
146 | print_error('wwwrootslash', 'error'); |
147 | } |
148 | |
75781f87 |
149 | if (CLI_SCRIPT) { |
150 | initialise_fullme_cli(); |
151 | return; |
37ccf1fe |
152 | } |
11e7b506 |
153 | |
75781f87 |
154 | $wwwroot = parse_url($CFG->wwwroot); |
155 | if (!isset($wwwroot['path'])) { |
156 | $wwwroot['path'] = ''; |
157 | } |
158 | $wwwroot['path'] .= '/'; |
159 | |
160 | $rurl = setup_get_remote_url(); |
11e7b506 |
161 | |
75781f87 |
162 | // Check that URL is under $CFG->wwwroot. |
163 | if (strpos($rurl['path'], $wwwroot['path']) === 0) { |
164 | $SCRIPT = substr($rurl['path'], strlen($wwwroot['path'])-1); |
165 | } else { |
166 | // Probably some weird external script |
167 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; |
11e7b506 |
168 | return; |
169 | } |
170 | |
75781f87 |
171 | // $CFG->sslproxy specifies if external SSL appliance is used |
172 | // (That is, the Moodle server uses http, with an external box translating everything to https). |
173 | if (empty($CFG->sslproxy)) { |
174 | if ($rurl['scheme'] == 'http' and $wwwroot['scheme'] == 'https') { |
175 | print_error('sslonlyaccess', 'error'); |
176 | } |
177 | } |
178 | |
179 | // $CFG->reverseproxy specifies if reverse proxy server used. |
180 | // Used in load balancing scenarios. |
181 | // Do not abuse this to try to solve lan/wan access problems!!!!! |
182 | if (empty($CFG->reverseproxy)) { |
183 | if (($rurl['host'] != $wwwroot['host']) or |
184 | (!empty($wwwroot['port']) and $rurl['port'] != $wwwroot['port'])) { |
185 | print_error('wwwrootmismatch', 'error', '', $CFG->wwwroot); |
186 | } |
187 | } |
188 | |
189 | // hopefully this will stop all those "clever" admins trying to set up moodle |
190 | // with two different addresses in intranet and Internet |
191 | if (!empty($CFG->reverseproxy) && $rurl['host'] == $wwwroot['host']) { |
192 | print_error('reverseproxyabused', 'error'); |
193 | } |
194 | |
195 | $hostandport = $rurl['scheme'] . '://' . $wwwroot['host']; |
196 | if (!empty($wwwroot['port'])) { |
197 | $hostandport .= ':'.$wwwroot['port']; |
198 | } |
199 | |
200 | $FULLSCRIPT = $hostandport . $rurl['path']; |
201 | $FULLME = $hostandport . $rurl['fullpath']; |
202 | $ME = $rurl['fullpath']; |
203 | $rurl['path'] = $rurl['fullpath']; |
204 | } |
205 | |
206 | /** |
207 | * Initialises $FULLME and friends for command line scripts. |
208 | * This is a private method for use by initialise_fullme. |
209 | */ |
210 | function initialise_fullme_cli() { |
80e30f25 |
211 | global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT; |
212 | |
75781f87 |
213 | // Urls do not make much sense in CLI scripts |
214 | $backtrace = debug_backtrace(); |
215 | $topfile = array_pop($backtrace); |
216 | $topfile = realpath($topfile['file']); |
217 | $dirroot = realpath($CFG->dirroot); |
218 | |
219 | if (strpos($topfile, $dirroot) !== 0) { |
220 | // Probably some weird external script |
221 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; |
222 | } else { |
223 | $relativefile = substr($topfile, strlen($dirroot)); |
224 | $relativefile = str_replace('\\', '/', $relativefile); // Win fix |
225 | $SCRIPT = $FULLSCRIPT = $relativefile; |
226 | $FULLME = $ME = null; |
227 | } |
228 | } |
229 | |
230 | /** |
231 | * Get the URL that PHP/the web server thinks it is serving. Private function |
232 | * used by initialise_fullme. In your code, use $PAGE->url, $SCRIPT, etc. |
233 | * @return array in the same format that parse_url returns, with the addition of |
234 | * a 'fullpath' element, which includes any slasharguments path. |
235 | */ |
236 | function setup_get_remote_url() { |
11e7b506 |
237 | $rurl = array(); |
75781f87 |
238 | list($rurl['host']) = explode(':', $_SERVER['HTTP_HOST']); |
11e7b506 |
239 | $rurl['port'] = $_SERVER['SERVER_PORT']; |
75781f87 |
240 | $rurl['path'] = $_SERVER['SCRIPT_NAME']; // Script path without slash arguments |
11e7b506 |
241 | |
242 | if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) { |
243 | //Apache server |
244 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
245 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded |
246 | |
247 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) { |
248 | //lighttpd |
249 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
250 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded |
251 | |
252 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) { |
253 | //IIS |
254 | $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https'; |
255 | $rurl['fullpath'] = $_SERVER['SCRIPT_NAME']; |
256 | |
257 | // NOTE: ignore PATH_INFO because it is incorrectly encoded using 8bit filesystem legacy encoding in IIS |
258 | // since 2.0 we rely on iis rewrite extenssion like Helicon ISAPI_rewrite |
21517960 |
259 | // example rule: RewriteRule ^([^\?]+?\.php)(\/.+)$ $1\?file=$2 [QSA] |
11e7b506 |
260 | |
261 | if ($_SERVER['QUERY_STRING'] != '') { |
991ec2ee |
262 | $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING']; |
11e7b506 |
263 | } |
264 | $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility |
265 | |
266 | } else { |
75781f87 |
267 | throw new moodle_exception('unsupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']); |
11e7b506 |
268 | } |
75781f87 |
269 | return $rurl; |
11e7b506 |
270 | } |
271 | |
d3f9f1f8 |
272 | /** |
273 | * Initializes our performance info early. |
274 | * |
275 | * Pairs up with get_performance_info() which is actually |
251387d0 |
276 | * in moodlelib.php. This function is here so that we can |
277 | * call it before all the libs are pulled in. |
d3f9f1f8 |
278 | * |
279 | * @uses $PERF |
280 | */ |
281 | function init_performance_info() { |
282 | |
6fc4ad72 |
283 | global $PERF, $CFG, $USER; |
251387d0 |
284 | |
ab130a0b |
285 | $PERF = new object(); |
d3f9f1f8 |
286 | $PERF->logwrites = 0; |
287 | if (function_exists('microtime')) { |
288 | $PERF->starttime = microtime(); |
289 | } |
290 | if (function_exists('memory_get_usage')) { |
291 | $PERF->startmemory = memory_get_usage(); |
292 | } |
293 | if (function_exists('posix_times')) { |
251387d0 |
294 | $PERF->startposixtimes = posix_times(); |
d3f9f1f8 |
295 | } |
b65567f4 |
296 | if (function_exists('apd_set_pprof_trace')) { |
297 | // APD profiling |
6fc4ad72 |
298 | if ($USER->id > 0 && $CFG->perfdebug >= 15) { |
251387d0 |
299 | $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id; |
6fc4ad72 |
300 | mkdir($tempdir); |
301 | apd_set_pprof_trace($tempdir); |
302 | $PERF->profiling = true; |
303 | } |
b65567f4 |
304 | } |
d3f9f1f8 |
305 | } |
306 | |
76f3815b |
307 | /** |
308 | * Function to raise the memory limit to a new value. |
309 | * Will respect the memory limit if it is higher, thus allowing |
310 | * settings in php.ini, apache conf or command line switches |
311 | * to override it |
312 | * |
313 | * The memory limit should be expressed with a string (eg:'64M') |
314 | * |
315 | * @param string $newlimit the new memory limit |
316 | * @return bool |
317 | */ |
11e7b506 |
318 | function raise_memory_limit($newlimit) { |
76f3815b |
319 | |
320 | if (empty($newlimit)) { |
321 | return false; |
322 | } |
323 | |
324 | $cur = @ini_get('memory_limit'); |
325 | if (empty($cur)) { |
326 | // if php is compiled without --enable-memory-limits |
327 | // apparently memory_limit is set to '' |
328 | $cur=0; |
329 | } else { |
330 | if ($cur == -1){ |
331 | return true; // unlimited mem! |
332 | } |
333 | $cur = get_real_size($cur); |
334 | } |
335 | |
336 | $new = get_real_size($newlimit); |
337 | if ($new > $cur) { |
338 | ini_set('memory_limit', $newlimit); |
7022dd39 |
339 | return true; |
340 | } |
341 | return false; |
342 | } |
343 | |
344 | /** |
345 | * Function to reduce the memory limit to a new value. |
346 | * Will respect the memory limit if it is lower, thus allowing |
347 | * settings in php.ini, apache conf or command line switches |
348 | * to override it |
349 | * |
350 | * The memory limit should be expressed with a string (eg:'64M') |
351 | * |
352 | * @param string $newlimit the new memory limit |
353 | * @return bool |
354 | */ |
f630a546 |
355 | function reduce_memory_limit($newlimit) { |
7022dd39 |
356 | if (empty($newlimit)) { |
357 | return false; |
358 | } |
359 | $cur = @ini_get('memory_limit'); |
360 | if (empty($cur)) { |
361 | // if php is compiled without --enable-memory-limits |
362 | // apparently memory_limit is set to '' |
363 | $cur=0; |
364 | } else { |
365 | if ($cur == -1){ |
366 | return true; // unlimited mem! |
367 | } |
368 | $cur = get_real_size($cur); |
369 | } |
370 | |
371 | $new = get_real_size($newlimit); |
372 | // -1 is smaller, but it means unlimited |
373 | if ($new < $cur && $new != -1) { |
374 | ini_set('memory_limit', $newlimit); |
76f3815b |
375 | return true; |
376 | } |
377 | return false; |
378 | } |
379 | |
380 | /** |
381 | * Converts numbers like 10M into bytes. |
382 | * |
383 | * @param mixed $size The size to be converted |
384 | * @return mixed |
385 | */ |
386 | function get_real_size($size=0) { |
387 | if (!$size) { |
388 | return 0; |
389 | } |
11e7b506 |
390 | $scan = array(); |
76f3815b |
391 | $scan['MB'] = 1048576; |
392 | $scan['Mb'] = 1048576; |
393 | $scan['M'] = 1048576; |
394 | $scan['m'] = 1048576; |
395 | $scan['KB'] = 1024; |
396 | $scan['Kb'] = 1024; |
397 | $scan['K'] = 1024; |
398 | $scan['k'] = 1024; |
399 | |
400 | while (list($key) = each($scan)) { |
401 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { |
402 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; |
403 | break; |
404 | } |
405 | } |
406 | return $size; |
407 | } |
408 | |
d3f9f1f8 |
409 | /** |
410 | * Create a directory. |
411 | * |
412 | * @uses $CFG |
413 | * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 |
414 | * param bool $shownotices If true then notification messages will be printed out on error. |
415 | * @return string|false Returns full path to directory if successful, false if not |
416 | */ |
417 | function make_upload_directory($directory, $shownotices=true) { |
418 | |
419 | global $CFG; |
420 | |
421 | $currdir = $CFG->dataroot; |
422 | |
423 | umask(0000); |
424 | |
425 | if (!file_exists($currdir)) { |
b10a14a3 |
426 | if (!mkdir($currdir, $CFG->directorypermissions) or !is_writable($currdir)) { |
d3f9f1f8 |
427 | if ($shownotices) { |
251387d0 |
428 | echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '. |
d3f9f1f8 |
429 | $currdir .' with web server write access</div>'."<br />\n"; |
430 | } |
431 | return false; |
432 | } |
d100b8a0 |
433 | } |
434 | |
435 | // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open |
436 | if (!file_exists($currdir.'/.htaccess')) { |
d3f9f1f8 |
437 | if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety |
edaf04d4 |
438 | @fwrite($handle, "deny from all\r\nAllowOverride None\r\nNote: this file is broken intentionally, we do not want anybody to undo it in subdirectory!\r\n"); |
d3f9f1f8 |
439 | @fclose($handle); |
440 | } |
441 | } |
442 | |
443 | $dirarray = explode('/', $directory); |
444 | |
445 | foreach ($dirarray as $dir) { |
446 | $currdir = $currdir .'/'. $dir; |
447 | if (! file_exists($currdir)) { |
448 | if (! mkdir($currdir, $CFG->directorypermissions)) { |
449 | if ($shownotices) { |
251387d0 |
450 | echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('. |
d3f9f1f8 |
451 | $currdir .')</div>'."<br />\n"; |
452 | } |
453 | return false; |
454 | } |
455 | //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it |
456 | } |
457 | } |
458 | |
459 | return $currdir; |
460 | } |
461 | |
419e1d93 |
462 | function init_memcached() { |
463 | global $CFG, $MCACHE; |
464 | |
f917d0ea |
465 | include_once($CFG->libdir . '/memcached.class.php'); |
466 | $MCACHE = new memcached; |
467 | if ($MCACHE->status()) { |
468 | return true; |
251387d0 |
469 | } |
f917d0ea |
470 | unset($MCACHE); |
251387d0 |
471 | return false; |
419e1d93 |
472 | } |
473 | |
2142d492 |
474 | function init_eaccelerator() { |
475 | global $CFG, $MCACHE; |
476 | |
477 | include_once($CFG->libdir . '/eaccelerator.class.php'); |
478 | $MCACHE = new eaccelerator; |
f917d0ea |
479 | if ($MCACHE->status()) { |
2142d492 |
480 | return true; |
251387d0 |
481 | } |
2142d492 |
482 | unset($MCACHE); |
483 | return false; |
484 | } |
485 | |
486 | |
487 | |
d3f9f1f8 |
488 | ?> |