Commit | Line | Data |
---|---|---|
b37eac91 | 1 | <?php |
d3f9f1f8 | 2 | |
b37eac91 | 3 | // This file is part of Moodle - http://moodle.org/ |
4 | // | |
5 | // Moodle is free software: you can redistribute it and/or modify | |
6 | // it under the terms of the GNU General Public License as published by | |
7 | // the Free Software Foundation, either version 3 of the License, or | |
8 | // (at your option) any later version. | |
9 | // | |
10 | // Moodle is distributed in the hope that it will be useful, | |
11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | // GNU General Public License for more details. | |
14 | // | |
15 | // You should have received a copy of the GNU General Public License | |
16 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | ||
19 | /** | |
20 | * These functions are required very early in the Moodle | |
21 | * setup process, before any of the main libraries are | |
22 | * loaded. | |
30fa50d0 | 23 | * |
78bfb562 PS |
24 | * @package core |
25 | * @subpackage lib | |
26 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
27 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
b37eac91 | 28 | */ |
d3f9f1f8 | 29 | |
78bfb562 PS |
30 | defined('MOODLE_INTERNAL') || die(); |
31 | ||
c84a2dbe | 32 | /// Debug levels /// |
33 | /** no warnings at all */ | |
34 | define ('DEBUG_NONE', 0); | |
35 | /** E_ERROR | E_PARSE */ | |
36 | define ('DEBUG_MINIMAL', 5); | |
37 | /** E_ERROR | E_PARSE | E_WARNING | E_NOTICE */ | |
38 | define ('DEBUG_NORMAL', 15); | |
39 | /** E_ALL without E_STRICT for now, do show recoverable fatal errors */ | |
40 | define ('DEBUG_ALL', 6143); | |
41 | /** DEBUG_ALL with extra Moodle debug messages - (DEBUG_ALL | 32768) */ | |
42 | define ('DEBUG_DEVELOPER', 38911); | |
43 | ||
6a525ce2 | 44 | /** |
b08b3569 | 45 | * Simple class. It is usually used instead of stdClass because it looks |
1387fcdd | 46 | * more familiar to Java developers ;-) Do not use for type checking of |
b08b3569 | 47 | * function parameters. |
b37eac91 | 48 | * |
49 | * @package moodlecore | |
50 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
51 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
6a525ce2 | 52 | */ |
b08b3569 | 53 | class object extends stdClass {}; |
6a525ce2 | 54 | |
251387d0 | 55 | /** |
56 | * Base Moodle Exception class | |
b37eac91 | 57 | * |
c84a2dbe | 58 | * Although this class is defined here, you cannot throw a moodle_exception until |
59 | * after moodlelib.php has been included (which will happen very soon). | |
60 | * | |
b37eac91 | 61 | * @package moodlecore |
62 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
63 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
251387d0 | 64 | */ |
65 | class moodle_exception extends Exception { | |
66 | public $errorcode; | |
67 | public $module; | |
68 | public $a; | |
69 | public $link; | |
eee5d9bb | 70 | public $debuginfo; |
251387d0 | 71 | |
72 | /** | |
73 | * Constructor | |
74 | * @param string $errorcode The name of the string from error.php to print | |
75 | * @param string $module name of module | |
76 | * @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. | |
77 | * @param object $a Extra words and phrases that might be required in the error string | |
eee5d9bb | 78 | * @param string $debuginfo optional debugging information |
251387d0 | 79 | */ |
5ca18631 | 80 | function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) { |
81 | if (empty($module) || $module == 'moodle' || $module == 'core') { | |
251387d0 | 82 | $module = 'error'; |
83 | } | |
84 | ||
5ca18631 | 85 | $this->errorcode = $errorcode; |
86 | $this->module = $module; | |
87 | $this->link = $link; | |
88 | $this->a = $a; | |
89 | $this->debuginfo = $debuginfo; | |
251387d0 | 90 | |
4f6be42c DM |
91 | if (get_string_manager()->string_exists($errorcode, $module)) { |
92 | $message = get_string($errorcode, $module, $a); | |
93 | } else { | |
94 | $message = $module . '/' . $errorcode; | |
95 | } | |
251387d0 | 96 | |
97 | parent::__construct($message, 0); | |
98 | } | |
99 | } | |
100 | ||
df997f84 PS |
101 | /** |
102 | * Course/activity access exception. | |
103 | * | |
104 | * This exception is thrown from require_login() | |
105 | */ | |
106 | class require_login_exception extends moodle_exception { | |
107 | function __construct($debuginfo) { | |
108 | parent::__construct('requireloginerror', 'error', '', NULL, $debuginfo); | |
109 | } | |
110 | } | |
111 | ||
559a5dbd | 112 | /** |
113 | * Web service parameter exception class | |
114 | * | |
115 | * This exception must be thrown to the web service client when a web service parameter is invalid | |
116 | * The error string is gotten from webservice.php | |
117 | */ | |
118 | class webservice_parameter_exception extends moodle_exception { | |
119 | /** | |
120 | * Constructor | |
121 | * @param string $errorcode The name of the string from webservice.php to print | |
122 | * @param string $a The name of the parameter | |
123 | */ | |
124 | function __construct($errorcode=null, $a = '') { | |
125 | parent::__construct($errorcode, 'webservice', '', $a, null); | |
126 | } | |
127 | } | |
128 | ||
9a0df45a | 129 | /** |
130 | * Exceptions indicating user does not have permissions to do something | |
131 | * and the execution can not continue. | |
132 | */ | |
133 | class required_capability_exception extends moodle_exception { | |
134 | function __construct($context, $capability, $errormessage, $stringfile) { | |
135 | $capabilityname = get_capability_string($capability); | |
136 | parent::__construct($errormessage, $stringfile, get_context_url($context), $capabilityname); | |
137 | } | |
138 | } | |
139 | ||
655bbf51 | 140 | /** |
cce1b0b9 | 141 | * Exception indicating programming error, must be fixed by a programer. For example |
142 | * a core API might throw this type of exception if a plugin calls it incorrectly. | |
b37eac91 | 143 | * |
144 | * @package moodlecore | |
145 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
146 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
655bbf51 | 147 | */ |
148 | class coding_exception extends moodle_exception { | |
655bbf51 | 149 | /** |
150 | * Constructor | |
151 | * @param string $hint short description of problem | |
152 | * @param string $debuginfo detailed information how to fix problem | |
153 | */ | |
154 | function __construct($hint, $debuginfo=null) { | |
155 | parent::__construct('codingerror', 'debug', '', $hint, $debuginfo); | |
a3f7cbf6 | 156 | } |
157 | } | |
158 | ||
159 | /** | |
160 | * Exception indicating malformed parameter problem. | |
161 | * This exception is not supposed to be thrown when processing | |
162 | * user submitted data in forms. It is more suitable | |
163 | * for WS and other low level stuff. | |
164 | */ | |
165 | class invalid_parameter_exception extends moodle_exception { | |
166 | /** | |
167 | * Constructor | |
168 | * @param string $debuginfo some detailed information | |
169 | */ | |
170 | function __construct($debuginfo=null) { | |
171 | parent::__construct('invalidparameter', 'debug', '', null, $debuginfo); | |
655bbf51 | 172 | } |
173 | } | |
174 | ||
d07ff72d | 175 | /** |
176 | * Exception indicating malformed response problem. | |
177 | * This exception is not supposed to be thrown when processing | |
178 | * user submitted data in forms. It is more suitable | |
179 | * for WS and other low level stuff. | |
180 | */ | |
181 | class invalid_response_exception extends moodle_exception { | |
182 | /** | |
183 | * Constructor | |
184 | * @param string $debuginfo some detailed information | |
185 | */ | |
186 | function __construct($debuginfo=null) { | |
187 | parent::__construct('invalidresponse', 'debug', '', null, $debuginfo); | |
188 | } | |
189 | } | |
190 | ||
cce1b0b9 | 191 | /** |
1387fcdd | 192 | * An exception that indicates something really weird happened. For example, |
cce1b0b9 | 193 | * if you do switch ($context->contextlevel), and have one case for each |
194 | * CONTEXT_... constant. You might throw an invalid_state_exception in the | |
f630a546 | 195 | * default case, to just in case something really weird is going on, and |
8dc0ae8f | 196 | * $context->contextlevel is invalid - rather than ignoring this possibility. |
b37eac91 | 197 | * |
198 | * @package moodlecore | |
199 | * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com} | |
200 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
cce1b0b9 | 201 | */ |
202 | class invalid_state_exception extends moodle_exception { | |
203 | /** | |
204 | * Constructor | |
205 | * @param string $hint short description of problem | |
206 | * @param string $debuginfo optional more detailed information | |
207 | */ | |
208 | function __construct($hint, $debuginfo=null) { | |
209 | parent::__construct('invalidstatedetected', 'debug', '', $hint, $debuginfo); | |
210 | } | |
211 | } | |
212 | ||
251387d0 | 213 | /** |
1387fcdd | 214 | * Default exception handler, uncaught exceptions are equivalent to error() in 1.9 and earlier |
fd1a792e | 215 | * |
30fa50d0 | 216 | * @param Exception $ex |
c19bc39c | 217 | * @return void -does not return. Terminates execution! |
251387d0 | 218 | */ |
c19bc39c | 219 | function default_exception_handler($ex) { |
695c5ec4 | 220 | global $DB, $OUTPUT; |
1fbdf76d | 221 | |
222 | // detect active db transactions, rollback and log as error | |
695c5ec4 | 223 | abort_all_db_transactions(); |
1fe1d104 | 224 | |
c19bc39c | 225 | $info = get_exception_info($ex); |
34a2777c | 226 | |
c19bc39c | 227 | if (debugging('', DEBUG_MINIMAL)) { |
2e9b772f | 228 | $logerrmsg = "Default exception handler: ".$info->message.' Debug: '.$info->debuginfo."\n".format_backtrace($info->backtrace, true); |
695c5ec4 | 229 | error_log($logerrmsg); |
fd1a792e | 230 | } |
231 | ||
c19bc39c PS |
232 | if (is_early_init($info->backtrace)) { |
233 | echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); | |
b7009474 | 234 | } else { |
3c1ea58b | 235 | try { |
a56c457e PS |
236 | if ($DB) { |
237 | // If you enable db debugging and exception is thrown, the print footer prints a lot of rubbish | |
238 | $DB->set_debug(0); | |
239 | } | |
3c1ea58b PS |
240 | echo $OUTPUT->fatal_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); |
241 | } catch (Exception $out_ex) { | |
242 | // default exception handler MUST not throw any exceptions!! | |
2e9b772f | 243 | // the problem here is we do not know if page already started or not, we only know that somebody messed up in outputlib or theme |
3c1ea58b | 244 | // so we just print at least something instead of "Exception thrown without a stack frame in Unknown on line 0":-( |
1adaa404 PS |
245 | if (CLI_SCRIPT or AJAX_SCRIPT) { |
246 | // just ignore the error and send something back using the safest method | |
247 | echo bootstrap_renderer::early_error($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); | |
248 | } else { | |
249 | echo bootstrap_renderer::early_error_content($info->message, $info->moreinfourl, $info->link, $info->backtrace, $info->debuginfo); | |
250 | $outinfo = get_exception_info($out_ex); | |
251 | echo bootstrap_renderer::early_error_content($outinfo->message, $outinfo->moreinfourl, $outinfo->link, $outinfo->backtrace, $outinfo->debuginfo); | |
252 | } | |
3c1ea58b | 253 | } |
7544d13c | 254 | } |
255 | ||
34a2777c | 256 | exit(1); // General error code |
257 | } | |
258 | ||
85ba1e78 PS |
259 | /** |
260 | * Default error handler, prevents some white screens. | |
261 | * @param int $errno | |
262 | * @param string $errstr | |
263 | * @param string $errfile | |
264 | * @param int $errline | |
265 | * @param array $errcontext | |
266 | * @return bool false means use default error handler | |
267 | */ | |
268 | function default_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { | |
269 | if ($errno == 4096) { | |
270 | //fatal catchable error | |
271 | throw new coding_exception('PHP catchable fatal error', $errstr); | |
272 | } | |
273 | return false; | |
274 | } | |
275 | ||
695c5ec4 PS |
276 | /** |
277 | * Unconditionally abort all database transactions, this function | |
278 | * should be called from exception handlers only. | |
279 | * @return void | |
280 | */ | |
281 | function abort_all_db_transactions() { | |
282 | global $CFG, $DB, $SCRIPT; | |
283 | ||
d5a8d9aa | 284 | // default exception handler MUST not throw any exceptions!! |
03221650 | 285 | |
695c5ec4 PS |
286 | if ($DB && $DB->is_transaction_started()) { |
287 | error_log('Database transaction aborted automatically in ' . $CFG->dirroot . $SCRIPT); | |
d5a8d9aa PS |
288 | // note: transaction blocks should never change current $_SESSION |
289 | $DB->force_transaction_rollback(); | |
695c5ec4 PS |
290 | } |
291 | } | |
292 | ||
b7009474 | 293 | /** |
50764d37 PS |
294 | * This function encapsulates the tests for whether an exception was thrown in |
295 | * early init -- either during setup.php or during init of $OUTPUT. | |
b7009474 | 296 | * |
297 | * If another exception is thrown then, and if we do not take special measures, | |
298 | * we would just get a very cryptic message "Exception thrown without a stack | |
299 | * frame in Unknown on line 0". That makes debugging very hard, so we do take | |
300 | * special measures in default_exception_handler, with the help of this function. | |
301 | * | |
302 | * @param array $backtrace the stack trace to analyse. | |
303 | * @return boolean whether the stack trace is somewhere in output initialisation. | |
304 | */ | |
50764d37 | 305 | function is_early_init($backtrace) { |
b7009474 | 306 | $dangerouscode = array( |
307 | array('function' => 'header', 'type' => '->'), | |
308 | array('class' => 'bootstrap_renderer'), | |
50764d37 | 309 | array('file' => dirname(__FILE__).'/setup.php'), |
b7009474 | 310 | ); |
311 | foreach ($backtrace as $stackframe) { | |
312 | foreach ($dangerouscode as $pattern) { | |
313 | $matches = true; | |
314 | foreach ($pattern as $property => $value) { | |
315 | if (!isset($stackframe[$property]) || $stackframe[$property] != $value) { | |
316 | $matches = false; | |
317 | } | |
318 | } | |
319 | if ($matches) { | |
320 | return true; | |
321 | } | |
322 | } | |
323 | } | |
324 | return false; | |
325 | } | |
326 | ||
34a2777c | 327 | /** |
cbf05caa PS |
328 | * Abort execution by throwing of a general exception, |
329 | * default exception handler displays the error message in most cases. | |
34a2777c | 330 | * |
331 | * @param string $errorcode The name of the language string containing the error message. | |
332 | * Normally this should be in the error.php lang file. | |
333 | * @param string $module The language file to get the error message from. | |
334 | * @param string $link The url where the user will be prompted to continue. | |
335 | * If no url is provided the user will be directed to the site index page. | |
336 | * @param object $a Extra words and phrases that might be required in the error string | |
cbf05caa PS |
337 | * @param string $debuginfo optional debugging information |
338 | * @return void, always throws exception! | |
34a2777c | 339 | */ |
cbf05caa PS |
340 | function print_error($errorcode, $module = 'error', $link = '', $a = null, $debuginfo = null) { |
341 | throw new moodle_exception($errorcode, $module, $link, $a, $debuginfo); | |
34a2777c | 342 | } |
343 | ||
344 | /** | |
c19bc39c PS |
345 | * Returns detailed information about specified exception. |
346 | * @param exception $ex | |
347 | * @return object | |
34a2777c | 348 | */ |
c19bc39c | 349 | function get_exception_info($ex) { |
34a2777c | 350 | global $CFG, $DB, $SESSION; |
351 | ||
c19bc39c PS |
352 | if ($ex instanceof moodle_exception) { |
353 | $errorcode = $ex->errorcode; | |
354 | $module = $ex->module; | |
355 | $a = $ex->a; | |
356 | $link = $ex->link; | |
357 | $debuginfo = $ex->debuginfo; | |
358 | } else { | |
359 | $errorcode = 'generalexceptionmessage'; | |
360 | $module = 'error'; | |
361 | $a = $ex->getMessage(); | |
362 | $link = ''; | |
363 | $debuginfo = null; | |
34a2777c | 364 | } |
365 | ||
c19bc39c PS |
366 | $backtrace = $ex->getTrace(); |
367 | $place = array('file'=>$ex->getFile(), 'line'=>$ex->getLine(), 'exception'=>get_class($ex)); | |
368 | array_unshift($backtrace, $place); | |
369 | ||
c84a2dbe | 370 | // Be careful, no guarantee moodlelib.php is loaded. |
34a2777c | 371 | if (empty($module) || $module == 'moodle' || $module == 'core') { |
372 | $module = 'error'; | |
373 | } | |
4f6be42c DM |
374 | if (function_exists('get_string_manager')) { |
375 | if (get_string_manager()->string_exists($errorcode, $module)) { | |
376 | $message = get_string($errorcode, $module, $a); | |
377 | } elseif ($module == 'error' && get_string_manager()->string_exists($errorcode, 'moodle')) { | |
c84a2dbe | 378 | // Search in moodle file if error specified - needed for backwards compatibility |
379 | $message = get_string($errorcode, 'moodle', $a); | |
4f6be42c DM |
380 | } else { |
381 | $message = $module . '/' . $errorcode; | |
c84a2dbe | 382 | } |
383 | } else { | |
384 | $message = $module . '/' . $errorcode; | |
385 | } | |
386 | ||
387 | // Be careful, no guarantee weblib.php is loaded. | |
388 | if (function_exists('clean_text')) { | |
389 | $message = clean_text($message); | |
390 | } else { | |
391 | $message = htmlspecialchars($message); | |
34a2777c | 392 | } |
34a2777c | 393 | |
394 | if (!empty($CFG->errordocroot)) { | |
395 | $errordocroot = $CFG->errordocroot; | |
396 | } else if (!empty($CFG->docroot)) { | |
397 | $errordocroot = $CFG->docroot; | |
398 | } else { | |
399 | $errordocroot = 'http://docs.moodle.org'; | |
400 | } | |
401 | if ($module === 'error') { | |
402 | $modulelink = 'moodle'; | |
0ae8f5fc | 403 | } else { |
34a2777c | 404 | $modulelink = $module; |
251387d0 | 405 | } |
34a2777c | 406 | $moreinfourl = $errordocroot . '/en/error/' . $modulelink . '/' . $errorcode; |
407 | ||
d4a03c00 | 408 | if (empty($link)) { |
34a2777c | 409 | if (!empty($SESSION->fromurl)) { |
410 | $link = $SESSION->fromurl; | |
411 | unset($SESSION->fromurl); | |
412 | } else { | |
413 | $link = $CFG->wwwroot .'/'; | |
414 | } | |
415 | } | |
416 | ||
c19bc39c PS |
417 | $info = new object(); |
418 | $info->message = $message; | |
419 | $info->errorcode = $errorcode; | |
420 | $info->backtrace = $backtrace; | |
421 | $info->link = $link; | |
422 | $info->moreinfourl = $moreinfourl; | |
423 | $info->a = $a; | |
424 | $info->debuginfo = $debuginfo; | |
30fa50d0 | 425 | |
c19bc39c | 426 | return $info; |
34a2777c | 427 | } |
428 | ||
34a2777c | 429 | /** |
430 | * Formats a backtrace ready for output. | |
431 | * | |
432 | * @param array $callers backtrace array, as returned by debug_backtrace(). | |
433 | * @param boolean $plaintext if false, generates HTML, if true generates plain text. | |
434 | * @return string formatted backtrace, ready for output. | |
435 | */ | |
436 | function format_backtrace($callers, $plaintext = false) { | |
a0394be4 | 437 | // do not use $CFG->dirroot because it might not be available in destructors |
34a2777c | 438 | $dirroot = dirname(dirname(__FILE__)); |
30fa50d0 | 439 | |
34a2777c | 440 | if (empty($callers)) { |
441 | return ''; | |
442 | } | |
443 | ||
444 | $from = $plaintext ? '' : '<ul style="text-align: left">'; | |
445 | foreach ($callers as $caller) { | |
446 | if (!isset($caller['line'])) { | |
447 | $caller['line'] = '?'; // probably call_user_func() | |
448 | } | |
449 | if (!isset($caller['file'])) { | |
450 | $caller['file'] = 'unknownfile'; // probably call_user_func() | |
451 | } | |
452 | $from .= $plaintext ? '* ' : '<li>'; | |
453 | $from .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']); | |
454 | if (isset($caller['function'])) { | |
455 | $from .= ': call to '; | |
456 | if (isset($caller['class'])) { | |
457 | $from .= $caller['class'] . $caller['type']; | |
458 | } | |
459 | $from .= $caller['function'] . '()'; | |
460 | } else if (isset($caller['exception'])) { | |
461 | $from .= ': '.$caller['exception'].' thrown'; | |
462 | } | |
463 | $from .= $plaintext ? "\n" : '</li>'; | |
464 | } | |
465 | $from .= $plaintext ? '' : '</ul>'; | |
466 | ||
467 | return $from; | |
251387d0 | 468 | } |
6a525ce2 | 469 | |
fbf2c91e | 470 | /** |
471 | * This function verifies the sanity of PHP configuration | |
472 | * and stops execution if anything critical found. | |
473 | */ | |
474 | function setup_validate_php_configuration() { | |
475 | // this must be very fast - no slow checks here!!! | |
476 | ||
477 | if (ini_get_bool('register_globals')) { | |
478 | print_error('globalswarning', 'admin'); | |
479 | } | |
480 | if (ini_get_bool('session.auto_start')) { | |
481 | print_error('sessionautostartwarning', 'admin'); | |
482 | } | |
483 | if (ini_get_bool('magic_quotes_runtime')) { | |
484 | print_error('fatalmagicquotesruntime', 'admin'); | |
485 | } | |
486 | } | |
487 | ||
12bb0c3e PS |
488 | /** |
489 | * Initialise global $CFG variable | |
490 | * @return void | |
491 | */ | |
492 | function initialise_cfg() { | |
493 | global $CFG, $DB; | |
8b8aa060 | 494 | |
12bb0c3e PS |
495 | try { |
496 | if ($DB) { | |
497 | $localcfg = $DB->get_records_menu('config', array(), '', 'name,value'); | |
498 | foreach ($localcfg as $name=>$value) { | |
499 | if (property_exists($CFG, $name)) { | |
500 | // config.php settings always take precedence | |
501 | continue; | |
502 | } | |
503 | $CFG->{$name} = $value; | |
504 | } | |
505 | } | |
506 | } catch (dml_read_exception $e) { | |
507 | // most probably empty db, going to install soon | |
508 | } | |
509 | } | |
510 | ||
11e7b506 | 511 | /** |
75781f87 | 512 | * Initialises $FULLME and friends. Private function. Should only be called from |
513 | * setup.php. | |
11e7b506 | 514 | */ |
515 | function initialise_fullme() { | |
516 | global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT; | |
517 | ||
75781f87 | 518 | // Detect common config error. |
84b88cfd | 519 | if (substr($CFG->wwwroot, -1) == '/') { |
520 | print_error('wwwrootslash', 'error'); | |
521 | } | |
522 | ||
75781f87 | 523 | if (CLI_SCRIPT) { |
524 | initialise_fullme_cli(); | |
525 | return; | |
37ccf1fe | 526 | } |
11e7b506 | 527 | |
75781f87 | 528 | $wwwroot = parse_url($CFG->wwwroot); |
529 | if (!isset($wwwroot['path'])) { | |
530 | $wwwroot['path'] = ''; | |
531 | } | |
532 | $wwwroot['path'] .= '/'; | |
533 | ||
534 | $rurl = setup_get_remote_url(); | |
11e7b506 | 535 | |
75781f87 | 536 | // Check that URL is under $CFG->wwwroot. |
537 | if (strpos($rurl['path'], $wwwroot['path']) === 0) { | |
538 | $SCRIPT = substr($rurl['path'], strlen($wwwroot['path'])-1); | |
539 | } else { | |
540 | // Probably some weird external script | |
541 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; | |
11e7b506 | 542 | return; |
543 | } | |
544 | ||
75781f87 | 545 | // $CFG->sslproxy specifies if external SSL appliance is used |
546 | // (That is, the Moodle server uses http, with an external box translating everything to https). | |
547 | if (empty($CFG->sslproxy)) { | |
548 | if ($rurl['scheme'] == 'http' and $wwwroot['scheme'] == 'https') { | |
549 | print_error('sslonlyaccess', 'error'); | |
550 | } | |
551 | } | |
552 | ||
553 | // $CFG->reverseproxy specifies if reverse proxy server used. | |
554 | // Used in load balancing scenarios. | |
555 | // Do not abuse this to try to solve lan/wan access problems!!!!! | |
556 | if (empty($CFG->reverseproxy)) { | |
557 | if (($rurl['host'] != $wwwroot['host']) or | |
558 | (!empty($wwwroot['port']) and $rurl['port'] != $wwwroot['port'])) { | |
68a45361 | 559 | // Explain the problem and redirect them to the right URL |
df92ba9a PS |
560 | if (!defined('NO_MOODLE_COOKIES')) { |
561 | define('NO_MOODLE_COOKIES', true); | |
562 | } | |
68a45361 | 563 | redirect($CFG->wwwroot, get_string('wwwrootmismatch', 'error', $CFG->wwwroot), 3); |
75781f87 | 564 | } |
565 | } | |
566 | ||
567 | // hopefully this will stop all those "clever" admins trying to set up moodle | |
568 | // with two different addresses in intranet and Internet | |
569 | if (!empty($CFG->reverseproxy) && $rurl['host'] == $wwwroot['host']) { | |
570 | print_error('reverseproxyabused', 'error'); | |
571 | } | |
572 | ||
573 | $hostandport = $rurl['scheme'] . '://' . $wwwroot['host']; | |
574 | if (!empty($wwwroot['port'])) { | |
575 | $hostandport .= ':'.$wwwroot['port']; | |
576 | } | |
577 | ||
578 | $FULLSCRIPT = $hostandport . $rurl['path']; | |
579 | $FULLME = $hostandport . $rurl['fullpath']; | |
580 | $ME = $rurl['fullpath']; | |
581 | $rurl['path'] = $rurl['fullpath']; | |
582 | } | |
583 | ||
584 | /** | |
585 | * Initialises $FULLME and friends for command line scripts. | |
586 | * This is a private method for use by initialise_fullme. | |
587 | */ | |
588 | function initialise_fullme_cli() { | |
80e30f25 | 589 | global $CFG, $FULLME, $ME, $SCRIPT, $FULLSCRIPT; |
590 | ||
75781f87 | 591 | // Urls do not make much sense in CLI scripts |
592 | $backtrace = debug_backtrace(); | |
593 | $topfile = array_pop($backtrace); | |
594 | $topfile = realpath($topfile['file']); | |
595 | $dirroot = realpath($CFG->dirroot); | |
596 | ||
597 | if (strpos($topfile, $dirroot) !== 0) { | |
598 | // Probably some weird external script | |
599 | $SCRIPT = $FULLSCRIPT = $FULLME = $ME = null; | |
600 | } else { | |
601 | $relativefile = substr($topfile, strlen($dirroot)); | |
602 | $relativefile = str_replace('\\', '/', $relativefile); // Win fix | |
603 | $SCRIPT = $FULLSCRIPT = $relativefile; | |
604 | $FULLME = $ME = null; | |
605 | } | |
606 | } | |
607 | ||
608 | /** | |
609 | * Get the URL that PHP/the web server thinks it is serving. Private function | |
610 | * used by initialise_fullme. In your code, use $PAGE->url, $SCRIPT, etc. | |
611 | * @return array in the same format that parse_url returns, with the addition of | |
612 | * a 'fullpath' element, which includes any slasharguments path. | |
613 | */ | |
614 | function setup_get_remote_url() { | |
11e7b506 | 615 | $rurl = array(); |
75781f87 | 616 | list($rurl['host']) = explode(':', $_SERVER['HTTP_HOST']); |
11e7b506 | 617 | $rurl['port'] = $_SERVER['SERVER_PORT']; |
75781f87 | 618 | $rurl['path'] = $_SERVER['SCRIPT_NAME']; // Script path without slash arguments |
11e7b506 | 619 | |
620 | if (stripos($_SERVER['SERVER_SOFTWARE'], 'apache') !== false) { | |
621 | //Apache server | |
622 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; | |
623 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded | |
624 | ||
625 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) { | |
199c1981 | 626 | //lighttpd - not officially supported |
11e7b506 | 627 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
628 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded | |
629 | ||
7e13a265 | 630 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false) { |
199c1981 | 631 | //nginx - not officially supported |
7e13a265 | 632 | $rurl['scheme'] = empty($_SERVER['HTTPS']) ? 'http' : 'https'; |
633 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded | |
634 | ||
11e7b506 | 635 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'iis') !== false) { |
199c1981 | 636 | //IIS - needs a lot of tweaking to make it work |
11e7b506 | 637 | $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https'; |
638 | $rurl['fullpath'] = $_SERVER['SCRIPT_NAME']; | |
639 | ||
640 | // NOTE: ignore PATH_INFO because it is incorrectly encoded using 8bit filesystem legacy encoding in IIS | |
641 | // since 2.0 we rely on iis rewrite extenssion like Helicon ISAPI_rewrite | |
21517960 | 642 | // example rule: RewriteRule ^([^\?]+?\.php)(\/.+)$ $1\?file=$2 [QSA] |
11e7b506 | 643 | |
644 | if ($_SERVER['QUERY_STRING'] != '') { | |
991ec2ee | 645 | $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING']; |
11e7b506 | 646 | } |
647 | $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility | |
648 | ||
199c1981 PS |
649 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'cherokee') !== false) { |
650 | //cherokee - not officially supported | |
651 | $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https'; | |
652 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded | |
653 | ||
8b8aa060 PS |
654 | } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'zeus') !== false) { |
655 | //zeus - not officially supported | |
656 | $rurl['scheme'] = ($_SERVER['HTTPS'] == 'off') ? 'http' : 'https'; | |
657 | $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded | |
658 | ||
659 | } else { | |
75781f87 | 660 | throw new moodle_exception('unsupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']); |
11e7b506 | 661 | } |
75781f87 | 662 | return $rurl; |
11e7b506 | 663 | } |
664 | ||
d3f9f1f8 | 665 | /** |
666 | * Initializes our performance info early. | |
667 | * | |
668 | * Pairs up with get_performance_info() which is actually | |
251387d0 | 669 | * in moodlelib.php. This function is here so that we can |
670 | * call it before all the libs are pulled in. | |
d3f9f1f8 | 671 | * |
672 | * @uses $PERF | |
673 | */ | |
674 | function init_performance_info() { | |
675 | ||
6fc4ad72 | 676 | global $PERF, $CFG, $USER; |
251387d0 | 677 | |
ab130a0b | 678 | $PERF = new object(); |
d3f9f1f8 | 679 | $PERF->logwrites = 0; |
680 | if (function_exists('microtime')) { | |
681 | $PERF->starttime = microtime(); | |
c84a2dbe | 682 | } |
d3f9f1f8 | 683 | if (function_exists('memory_get_usage')) { |
684 | $PERF->startmemory = memory_get_usage(); | |
685 | } | |
686 | if (function_exists('posix_times')) { | |
251387d0 | 687 | $PERF->startposixtimes = posix_times(); |
d3f9f1f8 | 688 | } |
b65567f4 | 689 | if (function_exists('apd_set_pprof_trace')) { |
690 | // APD profiling | |
6fc4ad72 | 691 | if ($USER->id > 0 && $CFG->perfdebug >= 15) { |
251387d0 | 692 | $tempdir = $CFG->dataroot . '/temp/profile/' . $USER->id; |
6fc4ad72 | 693 | mkdir($tempdir); |
694 | apd_set_pprof_trace($tempdir); | |
695 | $PERF->profiling = true; | |
696 | } | |
b65567f4 | 697 | } |
d3f9f1f8 | 698 | } |
699 | ||
31a99877 | 700 | /** |
701 | * Indicates whether we are in the middle of the initial Moodle install. | |
702 | * | |
703 | * Very occasionally it is necessary avoid running certain bits of code before the | |
704 | * Moodle installation has completed. The installed flag is set in admin/index.php | |
705 | * after Moodle core and all the plugins have been installed, but just before | |
706 | * the person doing the initial install is asked to choose the admin password. | |
707 | * | |
708 | * @return boolean true if the initial install is not complete. | |
709 | */ | |
710 | function during_initial_install() { | |
711 | global $CFG; | |
712 | return empty($CFG->rolesactive); | |
713 | } | |
714 | ||
76f3815b | 715 | /** |
716 | * Function to raise the memory limit to a new value. | |
717 | * Will respect the memory limit if it is higher, thus allowing | |
718 | * settings in php.ini, apache conf or command line switches | |
719 | * to override it | |
720 | * | |
721 | * The memory limit should be expressed with a string (eg:'64M') | |
722 | * | |
723 | * @param string $newlimit the new memory limit | |
724 | * @return bool | |
725 | */ | |
11e7b506 | 726 | function raise_memory_limit($newlimit) { |
76f3815b | 727 | |
728 | if (empty($newlimit)) { | |
729 | return false; | |
730 | } | |
731 | ||
732 | $cur = @ini_get('memory_limit'); | |
733 | if (empty($cur)) { | |
734 | // if php is compiled without --enable-memory-limits | |
735 | // apparently memory_limit is set to '' | |
736 | $cur=0; | |
737 | } else { | |
738 | if ($cur == -1){ | |
739 | return true; // unlimited mem! | |
740 | } | |
741 | $cur = get_real_size($cur); | |
742 | } | |
743 | ||
744 | $new = get_real_size($newlimit); | |
745 | if ($new > $cur) { | |
746 | ini_set('memory_limit', $newlimit); | |
7022dd39 | 747 | return true; |
748 | } | |
749 | return false; | |
750 | } | |
751 | ||
752 | /** | |
753 | * Function to reduce the memory limit to a new value. | |
754 | * Will respect the memory limit if it is lower, thus allowing | |
755 | * settings in php.ini, apache conf or command line switches | |
756 | * to override it | |
757 | * | |
758 | * The memory limit should be expressed with a string (eg:'64M') | |
759 | * | |
760 | * @param string $newlimit the new memory limit | |
761 | * @return bool | |
762 | */ | |
f630a546 | 763 | function reduce_memory_limit($newlimit) { |
7022dd39 | 764 | if (empty($newlimit)) { |
765 | return false; | |
766 | } | |
767 | $cur = @ini_get('memory_limit'); | |
768 | if (empty($cur)) { | |
769 | // if php is compiled without --enable-memory-limits | |
770 | // apparently memory_limit is set to '' | |
771 | $cur=0; | |
772 | } else { | |
773 | if ($cur == -1){ | |
774 | return true; // unlimited mem! | |
775 | } | |
776 | $cur = get_real_size($cur); | |
777 | } | |
778 | ||
779 | $new = get_real_size($newlimit); | |
780 | // -1 is smaller, but it means unlimited | |
781 | if ($new < $cur && $new != -1) { | |
782 | ini_set('memory_limit', $newlimit); | |
76f3815b | 783 | return true; |
784 | } | |
785 | return false; | |
786 | } | |
787 | ||
788 | /** | |
789 | * Converts numbers like 10M into bytes. | |
790 | * | |
791 | * @param mixed $size The size to be converted | |
792 | * @return mixed | |
793 | */ | |
794 | function get_real_size($size=0) { | |
795 | if (!$size) { | |
796 | return 0; | |
797 | } | |
11e7b506 | 798 | $scan = array(); |
76f3815b | 799 | $scan['MB'] = 1048576; |
800 | $scan['Mb'] = 1048576; | |
801 | $scan['M'] = 1048576; | |
802 | $scan['m'] = 1048576; | |
803 | $scan['KB'] = 1024; | |
804 | $scan['Kb'] = 1024; | |
805 | $scan['K'] = 1024; | |
806 | $scan['k'] = 1024; | |
807 | ||
808 | while (list($key) = each($scan)) { | |
809 | if ((strlen($size)>strlen($key))&&(substr($size, strlen($size) - strlen($key))==$key)) { | |
810 | $size = substr($size, 0, strlen($size) - strlen($key)) * $scan[$key]; | |
811 | break; | |
812 | } | |
813 | } | |
814 | return $size; | |
815 | } | |
816 | ||
5e39d7aa | 817 | /** |
818 | * Check whether a major upgrade is needed. That is defined as an upgrade that | |
819 | * changes something really fundamental in the database, so nothing can possibly | |
820 | * work until the database has been updated, and that is defined by the hard-coded | |
821 | * version number in this function. | |
822 | */ | |
823 | function redirect_if_major_upgrade_required() { | |
824 | global $CFG; | |
64f93798 | 825 | $lastmajordbchanges = 2010070300; |
5e39d7aa | 826 | if (empty($CFG->version) or (int)$CFG->version < $lastmajordbchanges or |
827 | during_initial_install() or !empty($CFG->adminsetuppending)) { | |
828 | try { | |
829 | @session_get_instance()->terminate_current(); | |
830 | } catch (Exception $e) { | |
831 | // Ignore any errors, redirect to upgrade anyway. | |
832 | } | |
a38c7a10 | 833 | $url = $CFG->wwwroot . '/' . $CFG->admin . '/index.php'; |
5e39d7aa | 834 | @header($_SERVER['SERVER_PROTOCOL'] . ' 303 See Other'); |
a38c7a10 PS |
835 | @header('Location: ' . $url); |
836 | echo bootstrap_renderer::plain_redirect_message(htmlspecialchars($url)); | |
5e39d7aa | 837 | exit; |
838 | } | |
839 | } | |
840 | ||
d3f9f1f8 | 841 | /** |
842 | * Create a directory. | |
843 | * | |
844 | * @uses $CFG | |
845 | * @param string $directory a string of directory names under $CFG->dataroot eg stuff/assignment/1 | |
846 | * param bool $shownotices If true then notification messages will be printed out on error. | |
847 | * @return string|false Returns full path to directory if successful, false if not | |
848 | */ | |
849 | function make_upload_directory($directory, $shownotices=true) { | |
850 | ||
851 | global $CFG; | |
852 | ||
853 | $currdir = $CFG->dataroot; | |
854 | ||
855 | umask(0000); | |
856 | ||
857 | if (!file_exists($currdir)) { | |
59ba2bb0 | 858 | if (!mkdir($currdir, $CFG->directorypermissions, true) or !is_writable($currdir)) { |
d3f9f1f8 | 859 | if ($shownotices) { |
251387d0 | 860 | echo '<div class="notifyproblem" align="center">ERROR: You need to create the directory '. |
d3f9f1f8 | 861 | $currdir .' with web server write access</div>'."<br />\n"; |
862 | } | |
863 | return false; | |
864 | } | |
d100b8a0 | 865 | } |
866 | ||
867 | // Make sure a .htaccess file is here, JUST IN CASE the files area is in the open | |
868 | if (!file_exists($currdir.'/.htaccess')) { | |
d3f9f1f8 | 869 | if ($handle = fopen($currdir.'/.htaccess', 'w')) { // For safety |
edaf04d4 | 870 | @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 | 871 | @fclose($handle); |
872 | } | |
873 | } | |
874 | ||
875 | $dirarray = explode('/', $directory); | |
876 | ||
877 | foreach ($dirarray as $dir) { | |
878 | $currdir = $currdir .'/'. $dir; | |
879 | if (! file_exists($currdir)) { | |
880 | if (! mkdir($currdir, $CFG->directorypermissions)) { | |
881 | if ($shownotices) { | |
251387d0 | 882 | echo '<div class="notifyproblem" align="center">ERROR: Could not find or create a directory ('. |
d3f9f1f8 | 883 | $currdir .')</div>'."<br />\n"; |
884 | } | |
885 | return false; | |
886 | } | |
887 | //@chmod($currdir, $CFG->directorypermissions); // Just in case mkdir didn't do it | |
888 | } | |
889 | } | |
890 | ||
891 | return $currdir; | |
892 | } | |
893 | ||
419e1d93 | 894 | function init_memcached() { |
895 | global $CFG, $MCACHE; | |
896 | ||
f917d0ea | 897 | include_once($CFG->libdir . '/memcached.class.php'); |
898 | $MCACHE = new memcached; | |
899 | if ($MCACHE->status()) { | |
900 | return true; | |
251387d0 | 901 | } |
f917d0ea | 902 | unset($MCACHE); |
251387d0 | 903 | return false; |
419e1d93 | 904 | } |
905 | ||
2142d492 | 906 | function init_eaccelerator() { |
907 | global $CFG, $MCACHE; | |
908 | ||
909 | include_once($CFG->libdir . '/eaccelerator.class.php'); | |
910 | $MCACHE = new eaccelerator; | |
f917d0ea | 911 | if ($MCACHE->status()) { |
2142d492 | 912 | return true; |
251387d0 | 913 | } |
2142d492 | 914 | unset($MCACHE); |
915 | return false; | |
916 | } | |
917 | ||
918 | ||
c84a2dbe | 919 | /** |
920 | * This class solves the problem of how to initialise $OUTPUT. | |
921 | * | |
922 | * The problem is caused be two factors | |
923 | * <ol> | |
924 | * <li>On the one hand, we cannot be sure when output will start. In particular, | |
30fa50d0 | 925 | * an error, which needs to be displayed, could be thrown at any time.</li> |
c84a2dbe | 926 | * <li>On the other hand, we cannot be sure when we will have all the information |
927 | * necessary to correctly initialise $OUTPUT. $OUTPUT depends on the theme, which | |
928 | * (potentially) depends on the current course, course categories, and logged in user. | |
929 | * It also depends on whether the current page requires HTTPS.</li> | |
930 | * </ol> | |
931 | * | |
932 | * So, it is hard to find a single natural place during Moodle script execution, | |
933 | * which we can guarantee is the right time to initialise $OUTPUT. Instead we | |
934 | * adopt the following strategy | |
935 | * <ol> | |
936 | * <li>We will initialise $OUTPUT the first time it is used.</li> | |
937 | * <li>If, after $OUTPUT has been initialised, the script tries to change something | |
7e0d6675 | 938 | * that $OUTPUT depends on, we throw an exception making it clear that the script |
c84a2dbe | 939 | * did something wrong. |
940 | * </ol> | |
941 | * | |
942 | * The only problem with that is, how do we initialise $OUTPUT on first use if, | |
943 | * it is going to be used like $OUTPUT->somthing(...)? Well that is where this | |
944 | * class comes in. Initially, we set up $OUTPUT = new bootstrap_renderer(). Then, | |
945 | * when any method is called on that object, we initialise $OUTPUT, and pass the call on. | |
946 | * | |
947 | * Note that this class is used before lib/outputlib.php has been loaded, so we | |
1387fcdd | 948 | * must be careful referring to classes/functions from there, they may not be |
c84a2dbe | 949 | * defined yet, and we must avoid fatal errors. |
950 | * | |
951 | * @copyright 2009 Tim Hunt | |
952 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
953 | * @since Moodle 2.0 | |
954 | */ | |
955 | class bootstrap_renderer { | |
956 | /** | |
957 | * Handles re-entrancy. Without this, errors or debugging output that occur | |
958 | * during the initialisation of $OUTPUT, cause infinite recursion. | |
959 | * @var boolean | |
960 | */ | |
961 | protected $initialising = false; | |
962 | ||
b7009474 | 963 | /** |
964 | * Have we started output yet? | |
965 | * @return boolean true if the header has been printed. | |
966 | */ | |
967 | public function has_started() { | |
968 | return false; | |
969 | } | |
970 | ||
c84a2dbe | 971 | public function __call($method, $arguments) { |
b7009474 | 972 | global $OUTPUT, $PAGE; |
c84a2dbe | 973 | |
87b6851c | 974 | $recursing = false; |
975 | if ($method == 'notification') { | |
1387fcdd | 976 | // Catch infinite recursion caused by debugging output during print_header. |
87b6851c | 977 | $backtrace = debug_backtrace(); |
978 | array_shift($backtrace); | |
979 | array_shift($backtrace); | |
50764d37 | 980 | $recursing = is_early_init($backtrace); |
87b6851c | 981 | } |
982 | ||
eb5bdb35 PS |
983 | $earlymethods = array( |
984 | 'fatal_error' => 'early_error', | |
985 | 'notification' => 'early_notification', | |
986 | ); | |
987 | ||
c84a2dbe | 988 | // If lib/outputlib.php has been loaded, call it. |
87b6851c | 989 | if (!empty($PAGE) && !$recursing) { |
eb5bdb35 PS |
990 | if (array_key_exists($method, $earlymethods)) { |
991 | //prevent PAGE->context warnings - exceptions might appear before we set any context | |
992 | $PAGE->set_context(null); | |
993 | } | |
b7009474 | 994 | $PAGE->initialise_theme_and_output(); |
995 | return call_user_func_array(array($OUTPUT, $method), $arguments); | |
c84a2dbe | 996 | } |
2142d492 | 997 | |
c84a2dbe | 998 | $this->initialising = true; |
eb5bdb35 | 999 | |
c84a2dbe | 1000 | // Too soon to initialise $OUTPUT, provide a couple of key methods. |
c84a2dbe | 1001 | if (array_key_exists($method, $earlymethods)) { |
1002 | return call_user_func_array(array('bootstrap_renderer', $earlymethods[$method]), $arguments); | |
1003 | } | |
1004 | ||
1005 | throw new coding_exception('Attempt to start output before enough information is known to initialise the theme.'); | |
1006 | } | |
1007 | ||
1008 | /** | |
1387fcdd | 1009 | * Returns nicely formatted error message in a div box. |
30fa50d0 | 1010 | * @return string |
c84a2dbe | 1011 | */ |
3c1ea58b | 1012 | public static function early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo = null) { |
b7009474 | 1013 | global $CFG; |
1014 | ||
3c1ea58b PS |
1015 | $content = '<div style="margin-top: 6em; margin-left:auto; margin-right:auto; color:#990000; text-align:center; font-size:large; border-width:1px; |
1016 | border-color:black; background-color:#ffffee; border-style:solid; border-radius: 20px; border-collapse: collapse; | |
1017 | width: 80%; -moz-border-radius: 20px; padding: 15px"> | |
1018 | ' . $message . ' | |
1019 | </div>'; | |
1020 | if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) { | |
1021 | if (!empty($debuginfo)) { | |
c5d18164 PS |
1022 | $debuginfo = s($debuginfo); // removes all nasty JS |
1023 | $debuginfo = str_replace("\n", '<br />', $debuginfo); // keep newlines | |
1024 | $content .= '<div class="notifytiny">Debug info: ' . $debuginfo . '</div>'; | |
3c1ea58b PS |
1025 | } |
1026 | if (!empty($backtrace)) { | |
1027 | $content .= '<div class="notifytiny">Stack trace: ' . format_backtrace($backtrace, false) . '</div>'; | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | return $content; | |
1032 | } | |
1033 | ||
1034 | /** | |
1035 | * This function should only be called by this class, or from exception handlers | |
1036 | * @return string | |
1037 | */ | |
1038 | public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null) { | |
1adaa404 PS |
1039 | global $CFG; |
1040 | ||
1041 | if (CLI_SCRIPT) { | |
1042 | echo "!!! $message !!!\n"; | |
1043 | if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) { | |
1044 | if (!empty($debuginfo)) { | |
1045 | echo "\nDebug info: $debuginfo"; | |
1046 | } | |
1047 | if (!empty($backtrace)) { | |
1048 | echo "\nStack trace: " . format_backtrace($backtrace, true); | |
1049 | } | |
1050 | } | |
1051 | return; | |
1052 | ||
1053 | } else if (AJAX_SCRIPT) { | |
1054 | $e = new stdClass(); | |
1055 | $e->error = $message; | |
1056 | $e->stacktrace = NULL; | |
1057 | $e->debuginfo = NULL; | |
1058 | if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) { | |
1059 | if (!empty($debuginfo)) { | |
1060 | $e->debuginfo = $debuginfo; | |
1061 | } | |
1062 | if (!empty($backtrace)) { | |
1063 | $e->stacktrace = format_backtrace($backtrace, true); | |
1064 | } | |
1065 | } | |
6db3eee0 | 1066 | @header('Content-Type: application/json'); |
1adaa404 PS |
1067 | echo json_encode($e); |
1068 | return; | |
1069 | } | |
1070 | ||
c84a2dbe | 1071 | // In the name of protocol correctness, monitoring and performance |
1387fcdd | 1072 | // profiling, set the appropriate error headers for machine consumption |
c84a2dbe | 1073 | if (isset($_SERVER['SERVER_PROTOCOL'])) { |
1074 | // Avoid it with cron.php. Note that we assume it's HTTP/1.x | |
1387fcdd | 1075 | // The 503 ode here means our Moodle does not work at all, the error happened too early |
c84a2dbe | 1076 | @header($_SERVER['SERVER_PROTOCOL'] . ' 503 Service Unavailable'); |
1077 | } | |
1078 | ||
1079 | // better disable any caching | |
1080 | @header('Content-Type: text/html; charset=utf-8'); | |
1081 | @header('Cache-Control: no-store, no-cache, must-revalidate'); | |
1082 | @header('Cache-Control: post-check=0, pre-check=0', false); | |
1083 | @header('Pragma: no-cache'); | |
1084 | @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT'); | |
1085 | @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); | |
1086 | ||
5e39d7aa | 1087 | if (function_exists('get_string')) { |
c84a2dbe | 1088 | $strerror = get_string('error'); |
1089 | } else { | |
c84a2dbe | 1090 | $strerror = 'Error'; |
1091 | } | |
1092 | ||
3c1ea58b | 1093 | $content = self::early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo); |
5e39d7aa | 1094 | |
1095 | return self::plain_page($strerror, $content); | |
c84a2dbe | 1096 | } |
1097 | ||
1098 | public static function early_notification($message, $classes = 'notifyproblem') { | |
1099 | return '<div class="' . $classes . '">' . $message . '</div>'; | |
1100 | } | |
5e39d7aa | 1101 | |
1102 | public static function plain_redirect_message($encodedurl) { | |
1103 | $message = '<p>' . get_string('pageshouldredirect') . '</p><p><a href="'. | |
1104 | $encodedurl .'">'. get_string('continue') .'</a></p>'; | |
a0a268d5 | 1105 | return self::plain_page(get_string('redirect'), $message); |
5e39d7aa | 1106 | } |
1107 | ||
1108 | protected static function plain_page($title, $content) { | |
1109 | if (function_exists('get_string') && function_exists('get_html_lang')) { | |
1110 | $htmllang = get_html_lang(); | |
1111 | } else { | |
1112 | $htmllang = ''; | |
1113 | } | |
1114 | ||
1115 | return '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
1116 | <html xmlns="http://www.w3.org/1999/xhtml" ' . $htmllang . '> | |
1117 | <head> | |
1118 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
1119 | <title>' . $title . '</title> | |
1120 | </head><body>' . $content . '</body></html>'; | |
1121 | } | |
c84a2dbe | 1122 | } |