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