Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 32... Línea 32...
32
define('DEBUG_NONE', 0);
32
define('DEBUG_NONE', 0);
33
/** Fatal errors only */
33
/** Fatal errors only */
34
define('DEBUG_MINIMAL', E_ERROR | E_PARSE);
34
define('DEBUG_MINIMAL', E_ERROR | E_PARSE);
35
/** Errors, warnings and notices */
35
/** Errors, warnings and notices */
36
define('DEBUG_NORMAL', E_ERROR | E_PARSE | E_WARNING | E_NOTICE);
36
define('DEBUG_NORMAL', E_ERROR | E_PARSE | E_WARNING | E_NOTICE);
37
/** All problems except strict PHP warnings */
37
/** All problems. Formerly, all problems, except the erstwhile strict PHP warnings before E_STRICT got deprecated. */
38
define('DEBUG_ALL', E_ALL & ~E_STRICT);
38
define('DEBUG_ALL', E_ALL);
39
/** DEBUG_ALL with all debug messages and strict warnings */
39
/** Same as DEBUG_ALL since E_STRICT was deprecated. */
40
define('DEBUG_DEVELOPER', E_ALL | E_STRICT);
40
define('DEBUG_DEVELOPER', E_ALL);
Línea 41... Línea 41...
41
 
41
 
42
/** Remove any memory limits */
42
/** Remove any memory limits */
43
define('MEMORY_UNLIMITED', -1);
43
define('MEMORY_UNLIMITED', -1);
44
/** Standard memory limit for given platform */
44
/** Standard memory limit for given platform */
Línea 50... Línea 50...
50
define('MEMORY_EXTRA', -3);
50
define('MEMORY_EXTRA', -3);
51
/** Extremely large memory limit - not recommended for standard scripts */
51
/** Extremely large memory limit - not recommended for standard scripts */
52
define('MEMORY_HUGE', -4);
52
define('MEMORY_HUGE', -4);
Línea 53... Línea 53...
53
 
53
 
54
/**
-
 
55
 * Base Moodle Exception class
-
 
56
 *
-
 
57
 * Although this class is defined here, you cannot throw a moodle_exception until
-
 
58
 * after moodlelib.php has been included (which will happen very soon).
-
 
59
 *
-
 
60
 * @package    core
-
 
61
 * @subpackage lib
-
 
62
 * @copyright  2008 Petr Skoda  {@link http://skodak.org}
-
 
63
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
64
 */
-
 
65
class moodle_exception extends Exception {
-
 
66
 
-
 
67
    /**
-
 
68
     * @var string The name of the string from error.php to print
-
 
69
     */
-
 
70
    public $errorcode;
-
 
71
 
-
 
72
    /**
-
 
73
     * @var string The name of module
-
 
74
     */
-
 
75
    public $module;
-
 
76
 
-
 
77
    /**
-
 
78
     * @var mixed Extra words and phrases that might be required in the error string
-
 
79
     */
-
 
80
    public $a;
-
 
81
 
-
 
82
    /**
-
 
83
     * @var string 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.
-
 
84
     */
-
 
85
    public $link;
-
 
86
 
-
 
87
    /**
-
 
88
     * @var string Optional information to aid the debugging process
-
 
89
     */
-
 
90
    public $debuginfo;
-
 
91
 
-
 
92
    /**
-
 
93
     * Constructor
-
 
94
     * @param string $errorcode The name of the string from error.php to print
-
 
95
     * @param string $module name of module
-
 
96
     * @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.
-
 
97
     * @param mixed $a Extra words and phrases that might be required in the error string
-
 
98
     * @param string $debuginfo optional debugging information
-
 
99
     */
-
 
100
    function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) {
-
 
101
        global $CFG;
-
 
102
 
-
 
103
        if (empty($module) || $module == 'moodle' || $module == 'core') {
-
 
104
            $module = 'error';
-
 
105
        }
-
 
106
 
-
 
107
        $this->errorcode = $errorcode;
-
 
108
        $this->module    = $module;
-
 
109
        $this->link      = $link;
-
 
110
        $this->a         = $a;
-
 
111
        $this->debuginfo = is_null($debuginfo) ? null : (string)$debuginfo;
-
 
112
 
-
 
113
        if (get_string_manager()->string_exists($errorcode, $module)) {
-
 
114
            $message = get_string($errorcode, $module, $a);
-
 
115
            $haserrorstring = true;
-
 
116
        } else {
-
 
117
            $message = $module . '/' . $errorcode;
-
 
118
            $haserrorstring = false;
-
 
119
        }
-
 
120
 
-
 
121
        $isinphpunittest = (defined('PHPUNIT_TEST') && PHPUNIT_TEST);
-
 
122
        $hasdebugdeveloper = (
-
 
123
            isset($CFG->debugdisplay) &&
-
 
124
            isset($CFG->debug) &&
-
 
125
            $CFG->debugdisplay &&
-
 
126
            $CFG->debug === DEBUG_DEVELOPER
-
 
127
        );
-
 
128
 
-
 
129
        if ($debuginfo) {
-
 
130
            if ($isinphpunittest || $hasdebugdeveloper) {
-
 
131
                $message = "$message ($debuginfo)";
-
 
132
            }
-
 
133
        }
-
 
134
 
-
 
135
        if (!$haserrorstring and $isinphpunittest) {
-
 
136
            // Append the contents of $a to $debuginfo so helpful information isn't lost.
-
 
137
            // This emulates what {@link get_exception_info()} does. Unfortunately that
-
 
138
            // function is not used by phpunit.
-
 
139
            $message .= PHP_EOL.'$a contents: '.print_r($a, true);
-
 
140
        }
-
 
141
 
-
 
142
        parent::__construct($message, 0);
-
 
143
    }
-
 
144
}
-
 
145
 
-
 
146
/**
-
 
147
 * Course/activity access exception.
-
 
148
 *
-
 
149
 * This exception is thrown from require_login()
-
 
150
 *
-
 
151
 * @package    core_access
-
 
152
 * @copyright  2010 Petr Skoda  {@link http://skodak.org}
-
 
153
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
154
 */
-
 
155
class require_login_exception extends moodle_exception {
-
 
156
    /**
-
 
157
     * Constructor
-
 
158
     * @param string $debuginfo Information to aid the debugging process
-
 
159
     */
-
 
160
    function __construct($debuginfo) {
-
 
161
        parent::__construct('requireloginerror', 'error', '', NULL, $debuginfo);
-
 
162
    }
-
 
163
}
-
 
164
 
-
 
165
/**
-
 
166
 * Session timeout exception.
-
 
167
 *
-
 
168
 * This exception is thrown from require_login()
-
 
169
 *
-
 
170
 * @package    core_access
-
 
171
 * @copyright  2015 Andrew Nicols <andrew@nicols.co.uk>
-
 
172
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
173
 */
-
 
174
class require_login_session_timeout_exception extends require_login_exception {
-
 
175
    /**
-
 
176
     * Constructor
-
 
177
     */
-
 
178
    public function __construct() {
-
 
179
        moodle_exception::__construct('sessionerroruser', 'error');
-
 
180
    }
-
 
181
}
-
 
182
 
-
 
183
/**
-
 
184
 * Web service parameter exception class
-
 
185
 * @deprecated since Moodle 2.2 - use moodle exception instead
-
 
186
 * This exception must be thrown to the web service client when a web service parameter is invalid
-
 
187
 * The error string is gotten from webservice.php
-
 
188
 */
-
 
189
class webservice_parameter_exception extends moodle_exception {
-
 
190
    /**
-
 
191
     * Constructor
-
 
192
     * @param string $errorcode The name of the string from webservice.php to print
-
 
193
     * @param string $a The name of the parameter
-
 
194
     * @param string $debuginfo Optional information to aid debugging
-
 
195
     */
-
 
196
    function __construct($errorcode=null, $a = '', $debuginfo = null) {
-
 
197
        parent::__construct($errorcode, 'webservice', '', $a, $debuginfo);
-
 
198
    }
-
 
199
}
-
 
200
 
-
 
201
/**
-
 
202
 * Exceptions indicating user does not have permissions to do something
-
 
203
 * and the execution can not continue.
-
 
204
 *
-
 
205
 * @package    core_access
-
 
206
 * @copyright  2009 Petr Skoda  {@link http://skodak.org}
-
 
207
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
208
 */
-
 
209
class required_capability_exception extends moodle_exception {
-
 
210
    /**
-
 
211
     * Constructor
-
 
212
     * @param context $context The context used for the capability check
-
 
213
     * @param string $capability The required capability
-
 
214
     * @param string $errormessage The error message to show the user
-
 
215
     * @param string $stringfile
-
 
216
     */
-
 
217
    function __construct($context, $capability, $errormessage, $stringfile) {
-
 
218
        $capabilityname = get_capability_string($capability);
-
 
219
        if ($context->contextlevel == CONTEXT_MODULE and preg_match('/:view$/', $capability)) {
-
 
220
            // we can not go to mod/xx/view.php because we most probably do not have cap to view it, let's go to course instead
-
 
221
            $parentcontext = $context->get_parent_context();
-
 
222
            $link = $parentcontext->get_url();
-
 
223
        } else {
-
 
224
            $link = $context->get_url();
-
 
225
        }
-
 
226
        parent::__construct($errormessage, $stringfile, $link, $capabilityname);
-
 
227
    }
-
 
228
}
-
 
229
 
-
 
230
/**
-
 
231
 * Exception indicating programming error, must be fixed by a programer. For example
-
 
232
 * a core API might throw this type of exception if a plugin calls it incorrectly.
-
 
233
 *
-
 
234
 * @package    core
-
 
235
 * @subpackage lib
-
 
236
 * @copyright  2008 Petr Skoda  {@link http://skodak.org}
-
 
237
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
238
 */
-
 
239
class coding_exception extends moodle_exception {
-
 
240
    /**
-
 
241
     * Constructor
-
 
242
     * @param string $hint short description of problem
-
 
243
     * @param string $debuginfo detailed information how to fix problem
-
 
244
     */
-
 
245
    function __construct($hint, $debuginfo=null) {
-
 
246
        parent::__construct('codingerror', 'debug', '', $hint, $debuginfo);
-
 
247
    }
-
 
248
}
-
 
249
 
-
 
250
/**
-
 
251
 * Exception indicating malformed parameter problem.
-
 
252
 * This exception is not supposed to be thrown when processing
-
 
253
 * user submitted data in forms. It is more suitable
-
 
254
 * for WS and other low level stuff.
-
 
255
 *
-
 
256
 * @package    core
-
 
257
 * @subpackage lib
-
 
258
 * @copyright  2009 Petr Skoda  {@link http://skodak.org}
-
 
259
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
260
 */
-
 
261
class invalid_parameter_exception extends moodle_exception {
-
 
262
    /**
-
 
263
     * Constructor
-
 
264
     * @param string $debuginfo some detailed information
-
 
265
     */
-
 
266
    function __construct($debuginfo=null) {
-
 
267
        parent::__construct('invalidparameter', 'debug', '', null, $debuginfo);
-
 
268
    }
-
 
269
}
-
 
270
 
-
 
271
/**
-
 
272
 * Exception indicating malformed response problem.
-
 
273
 * This exception is not supposed to be thrown when processing
-
 
274
 * user submitted data in forms. It is more suitable
-
 
275
 * for WS and other low level stuff.
-
 
276
 */
-
 
277
class invalid_response_exception extends moodle_exception {
-
 
278
    /**
-
 
279
     * Constructor
-
 
280
     * @param string $debuginfo some detailed information
-
 
281
     */
-
 
282
    function __construct($debuginfo=null) {
-
 
283
        parent::__construct('invalidresponse', 'debug', '', null, $debuginfo);
-
 
284
    }
-
 
285
}
-
 
286
 
-
 
287
/**
-
 
288
 * An exception that indicates something really weird happened. For example,
-
 
289
 * if you do switch ($context->contextlevel), and have one case for each
-
 
290
 * CONTEXT_... constant. You might throw an invalid_state_exception in the
-
 
291
 * default case, to just in case something really weird is going on, and
-
 
292
 * $context->contextlevel is invalid - rather than ignoring this possibility.
-
 
293
 *
-
 
294
 * @package    core
-
 
295
 * @subpackage lib
-
 
296
 * @copyright  2009 onwards Martin Dougiamas  {@link http://moodle.com}
-
 
297
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
298
 */
-
 
299
class invalid_state_exception extends moodle_exception {
-
 
300
    /**
-
 
301
     * Constructor
-
 
302
     * @param string $hint short description of problem
-
 
303
     * @param string $debuginfo optional more detailed information
-
 
304
     */
-
 
305
    function __construct($hint, $debuginfo=null) {
-
 
306
        parent::__construct('invalidstatedetected', 'debug', '', $hint, $debuginfo);
-
 
307
    }
-
 
308
}
-
 
309
 
-
 
310
/**
-
 
311
 * An exception that indicates incorrect permissions in $CFG->dataroot
-
 
312
 *
-
 
313
 * @package    core
-
 
314
 * @subpackage lib
-
 
315
 * @copyright  2010 Petr Skoda {@link http://skodak.org}
-
 
316
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
317
 */
-
 
318
class invalid_dataroot_permissions extends moodle_exception {
-
 
319
    /**
-
 
320
     * Constructor
-
 
321
     * @param string $debuginfo optional more detailed information
-
 
322
     */
-
 
323
    function __construct($debuginfo = NULL) {
-
 
324
        parent::__construct('invaliddatarootpermissions', 'error', '', NULL, $debuginfo);
-
 
325
    }
-
 
326
}
-
 
327
 
-
 
328
/**
-
 
329
 * An exception that indicates that file can not be served
-
 
330
 *
-
 
331
 * @package    core
-
 
332
 * @subpackage lib
-
 
333
 * @copyright  2010 Petr Skoda {@link http://skodak.org}
-
 
334
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
335
 */
-
 
336
class file_serving_exception extends moodle_exception {
-
 
337
    /**
-
 
338
     * Constructor
-
 
339
     * @param string $debuginfo optional more detailed information
-
 
340
     */
-
 
341
    function __construct($debuginfo = NULL) {
-
 
342
        parent::__construct('cannotservefile', 'error', '', NULL, $debuginfo);
-
 
343
    }
-
 
344
}
-
 
345
 
-
 
346
/**
54
/**
347
 * Get the Whoops! handler.
55
 * Get the Whoops! handler.
348
 *
56
 *
349
 * @return \Whoops\Run|null
57
 * @return \Whoops\Run|null
350
 */
58
 */
Línea 365... Línea 73...
365
 
73
 
366
    if (empty($CFG->debugdisplay)) {
74
    if (empty($CFG->debugdisplay)) {
367
        return null;
75
        return null;
Línea 368... Línea 76...
368
    }
76
    }
369
 
77
 
370
    if (!$CFG->debug_developer_use_pretty_exceptions) {
78
    if (empty($CFG->debug_developer_use_pretty_exceptions)) {
Línea 371... Línea 79...
371
        return null;
79
        return null;
372
    }
80
    }
Línea 420... Línea 128...
420
}
128
}
Línea 421... Línea 129...
421
 
129
 
422
/**
130
/**
423
 * Default exception handler.
131
 * Default exception handler.
424
 *
132
 *
425
 * @param Exception $ex
133
 * @param Throwable $ex
426
 * @return void -does not return. Terminates execution!
134
 * @return void -does not return. Terminates execution!
427
 */
135
 */
428
function default_exception_handler($ex) {
136
function default_exception_handler(Throwable $ex): void {
Línea 429... Línea 137...
429
    global $CFG, $DB, $OUTPUT, $USER, $FULLME, $SESSION, $PAGE;
137
    global $CFG, $DB, $OUTPUT, $USER, $FULLME, $SESSION, $PAGE;
430
 
138
 
Línea 680... Línea 388...
680
 
388
 
681
    return $info;
389
    return $info;
Línea 682... Línea 390...
682
}
390
}
683
 
-
 
684
/**
-
 
685
 * @deprecated since Moodle 3.8 MDL-61038 - please do not use this function any more.
-
 
686
 * @see \core\uuid::generate()
-
 
687
 */
-
 
688
function generate_uuid() {
-
 
689
    throw new coding_exception('generate_uuid() cannot be used anymore. Please use ' .
-
 
690
        '\core\uuid::generate() instead.');
-
 
691
}
-
 
692
 
391
 
693
/**
392
/**
694
 * Returns the Moodle Docs URL in the users language for a given 'More help' link.
393
 * Returns the Moodle Docs URL in the users language for a given 'More help' link.
695
 *
394
 *
696
 * There are three cases:
395
 * There are three cases:
Línea 1135... Línea 834...
1135
        if (isset($_SERVER['QUERY_STRING']) and $_SERVER['QUERY_STRING'] !== '') {
834
        if (isset($_SERVER['QUERY_STRING']) and $_SERVER['QUERY_STRING'] !== '') {
1136
            $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING'];
835
            $rurl['fullpath'] .= '?'.$_SERVER['QUERY_STRING'];
1137
        }
836
        }
1138
        $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility
837
        $_SERVER['REQUEST_URI'] = $rurl['fullpath']; // extra IIS compatibility
Línea 1139... Línea -...
1139
 
-
 
1140
/* NOTE: following servers are not fully tested! */
-
 
1141
 
-
 
1142
    } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
-
 
1143
        //lighttpd - not officially supported
-
 
1144
        $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1145
 
838
 
1146
    } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false) {
-
 
1147
        //nginx - not officially supported
839
    } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'nginx') !== false) {
1148
        if (!isset($_SERVER['SCRIPT_NAME'])) {
840
        if (!isset($_SERVER['SCRIPT_NAME'])) {
1149
            die('Invalid server configuration detected, please try to add "fastcgi_param SCRIPT_NAME $fastcgi_script_name;" to the nginx server configuration.');
841
            die('Invalid server configuration detected, please try to add "fastcgi_param SCRIPT_NAME $fastcgi_script_name;" to the nginx server configuration.');
1150
        }
-
 
1151
        $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1152
 
-
 
1153
     } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'cherokee') !== false) {
-
 
1154
         //cherokee - not officially supported
-
 
1155
         $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1156
 
-
 
1157
     } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'zeus') !== false) {
-
 
1158
         //zeus - not officially supported
-
 
1159
         $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1160
 
-
 
1161
    } else if (stripos($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false) {
-
 
1162
        //LiteSpeed - not officially supported
-
 
1163
        $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1164
 
-
 
1165
    } else if ($_SERVER['SERVER_SOFTWARE'] === 'HTTPD') {
-
 
1166
        //obscure name found on some servers - this is definitely not supported
-
 
1167
        $rurl['fullpath'] = $_SERVER['REQUEST_URI']; // TODO: verify this is always properly encoded
-
 
1168
 
-
 
1169
    } else if (strpos($_SERVER['SERVER_SOFTWARE'], 'PHP') === 0) {
-
 
1170
        // built-in PHP Development Server
842
        }
1171
        $rurl['fullpath'] = $_SERVER['REQUEST_URI'];
-
 
1172
 
843
        $rurl['fullpath'] = $_SERVER['REQUEST_URI'];
-
 
844
    } else {
1173
    } else {
845
        // Any other servers we can assume will pass the request_uri normally.
1174
        throw new moodle_exception('unsupportedwebserver', 'error', '', $_SERVER['SERVER_SOFTWARE']);
846
        $rurl['fullpath'] = $_SERVER['REQUEST_URI'];
Línea 1175... Línea 847...
1175
    }
847
    }
1176
 
848
 
1177
    // sanitize the url a bit more, the encoding style may be different in vars above
849
    // sanitize the url a bit more, the encoding style may be different in vars above
Línea 1476... Línea 1148...
1476
 
1148
 
1477
    return (int) $size;
1149
    return (int) $size;
Línea 1478... Línea 1150...
1478
}
1150
}
1479
 
-
 
1480
/**
-
 
1481
 * Try to disable all output buffering and purge
-
 
1482
 * all headers.
-
 
1483
 *
-
 
1484
 * @access private to be called only from lib/setup.php !
-
 
1485
 * @return void
-
 
1486
 */
-
 
1487
function disable_output_buffering() {
-
 
1488
    $olddebug = error_reporting(0);
-
 
1489
 
-
 
1490
    // disable compression, it would prevent closing of buffers
-
 
1491
    if (ini_get_bool('zlib.output_compression')) {
-
 
1492
        ini_set('zlib.output_compression', 'Off');
-
 
1493
    }
-
 
1494
 
-
 
1495
    // try to flush everything all the time
-
 
1496
    ob_implicit_flush(true);
-
 
1497
 
-
 
1498
    // close all buffers if possible and discard any existing output
-
 
1499
    // this can actually work around some whitespace problems in config.php
-
 
1500
    while(ob_get_level()) {
-
 
1501
        if (!ob_end_clean()) {
-
 
1502
            // prevent infinite loop when buffer can not be closed
-
 
1503
            break;
-
 
1504
        }
-
 
1505
    }
-
 
1506
 
-
 
1507
    // disable any other output handlers
-
 
1508
    ini_set('output_handler', '');
-
 
1509
 
-
 
1510
    error_reporting($olddebug);
-
 
1511
 
-
 
1512
    // Disable buffering in nginx.
-
 
1513
    header('X-Accel-Buffering: no');
-
 
1514
 
-
 
1515
}
-
 
1516
 
1151
 
1517
/**
1152
/**
1518
 * Check whether a major upgrade is needed.
1153
 * Check whether a major upgrade is needed.
1519
 *
1154
 *
1520
 * That is defined as an upgrade that changes something really fundamental
1155
 * That is defined as an upgrade that changes something really fundamental
Línea 1999... Línea 1634...
1999
            }
1634
            }
2000
        }
1635
        }
2001
    }
1636
    }
2002
}
1637
}
Línea 2003... Línea -...
2003
 
-
 
2004
/**
-
 
2005
 * This class solves the problem of how to initialise $OUTPUT.
-
 
2006
 *
-
 
2007
 * The problem is caused be two factors
-
 
2008
 * <ol>
-
 
2009
 * <li>On the one hand, we cannot be sure when output will start. In particular,
-
 
2010
 * an error, which needs to be displayed, could be thrown at any time.</li>
-
 
2011
 * <li>On the other hand, we cannot be sure when we will have all the information
-
 
2012
 * necessary to correctly initialise $OUTPUT. $OUTPUT depends on the theme, which
-
 
2013
 * (potentially) depends on the current course, course categories, and logged in user.
-
 
2014
 * It also depends on whether the current page requires HTTPS.</li>
-
 
2015
 * </ol>
-
 
2016
 *
-
 
2017
 * So, it is hard to find a single natural place during Moodle script execution,
-
 
2018
 * which we can guarantee is the right time to initialise $OUTPUT. Instead we
-
 
2019
 * adopt the following strategy
-
 
2020
 * <ol>
-
 
2021
 * <li>We will initialise $OUTPUT the first time it is used.</li>
-
 
2022
 * <li>If, after $OUTPUT has been initialised, the script tries to change something
-
 
2023
 * that $OUTPUT depends on, we throw an exception making it clear that the script
-
 
2024
 * did something wrong.
-
 
2025
 * </ol>
-
 
2026
 *
-
 
2027
 * The only problem with that is, how do we initialise $OUTPUT on first use if,
-
 
2028
 * it is going to be used like $OUTPUT->somthing(...)? Well that is where this
-
 
2029
 * class comes in. Initially, we set up $OUTPUT = new bootstrap_renderer(). Then,
-
 
2030
 * when any method is called on that object, we initialise $OUTPUT, and pass the call on.
-
 
2031
 *
-
 
2032
 * Note that this class is used before lib/outputlib.php has been loaded, so we
-
 
2033
 * must be careful referring to classes/functions from there, they may not be
-
 
2034
 * defined yet, and we must avoid fatal errors.
-
 
2035
 *
-
 
2036
 * @copyright 2009 Tim Hunt
-
 
2037
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
-
 
2038
 * @since     Moodle 2.0
-
 
2039
 */
-
 
2040
class bootstrap_renderer {
-
 
2041
    /**
-
 
2042
     * Handles re-entrancy. Without this, errors or debugging output that occur
-
 
2043
     * during the initialisation of $OUTPUT, cause infinite recursion.
-
 
2044
     * @var boolean
-
 
2045
     */
-
 
2046
    protected $initialising = false;
-
 
2047
 
-
 
2048
    /**
-
 
2049
     * Have we started output yet?
-
 
2050
     * @return boolean true if the header has been printed.
-
 
2051
     */
-
 
2052
    public function has_started() {
-
 
2053
        return false;
-
 
2054
    }
-
 
2055
 
-
 
2056
    /**
-
 
2057
     * Constructor - to be used by core code only.
-
 
2058
     * @param string $method The method to call
-
 
2059
     * @param array $arguments Arguments to pass to the method being called
-
 
2060
     * @return string
-
 
2061
     */
-
 
2062
    public function __call($method, $arguments) {
-
 
2063
        global $OUTPUT, $PAGE;
-
 
2064
 
-
 
2065
        $recursing = false;
-
 
2066
        if ($method == 'notification') {
-
 
2067
            // Catch infinite recursion caused by debugging output during print_header.
-
 
2068
            $backtrace = debug_backtrace();
-
 
2069
            array_shift($backtrace);
-
 
2070
            array_shift($backtrace);
-
 
2071
            $recursing = is_early_init($backtrace);
-
 
2072
        }
-
 
2073
 
-
 
2074
        $earlymethods = array(
-
 
2075
            'fatal_error' => 'early_error',
-
 
2076
            'notification' => 'early_notification',
-
 
2077
        );
-
 
2078
 
-
 
2079
        // If lib/outputlib.php has been loaded, call it.
-
 
2080
        if (!empty($PAGE) && !$recursing) {
-
 
2081
            if (array_key_exists($method, $earlymethods)) {
-
 
2082
                //prevent PAGE->context warnings - exceptions might appear before we set any context
-
 
2083
                $PAGE->set_context(null);
-
 
2084
            }
-
 
2085
            $PAGE->initialise_theme_and_output();
-
 
2086
            return call_user_func_array(array($OUTPUT, $method), $arguments);
-
 
2087
        }
-
 
2088
 
-
 
2089
        $this->initialising = true;
-
 
2090
 
-
 
2091
        // Too soon to initialise $OUTPUT, provide a couple of key methods.
-
 
2092
        if (array_key_exists($method, $earlymethods)) {
-
 
2093
            return call_user_func_array(array('bootstrap_renderer', $earlymethods[$method]), $arguments);
-
 
2094
        }
-
 
2095
 
-
 
2096
        throw new coding_exception('Attempt to start output before enough information is known to initialise the theme.');
-
 
2097
    }
-
 
2098
 
-
 
2099
    /**
-
 
2100
     * Returns nicely formatted error message in a div box.
-
 
2101
     * @static
-
 
2102
     * @param string $message error message
-
 
2103
     * @param ?string $moreinfourl (ignored in early errors)
-
 
2104
     * @param ?string $link (ignored in early errors)
-
 
2105
     * @param ?array $backtrace
-
 
2106
     * @param ?string $debuginfo
-
 
2107
     * @return string
-
 
2108
     */
-
 
2109
    public static function early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo = null) {
-
 
2110
        global $CFG;
-
 
2111
 
-
 
2112
        $content = "<div class='alert-danger'>$message</div>";
-
 
2113
        // Check whether debug is set.
-
 
2114
        $debug = (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER);
-
 
2115
        // Also check we have it set in the config file. This occurs if the method to read the config table from the
-
 
2116
        // database fails, reading from the config table is the first database interaction we have.
-
 
2117
        $debug = $debug || (!empty($CFG->config_php_settings['debug'])  && $CFG->config_php_settings['debug'] >= DEBUG_DEVELOPER );
-
 
2118
        if ($debug) {
-
 
2119
            if (!empty($debuginfo)) {
-
 
2120
                // Remove all nasty JS.
-
 
2121
                if (function_exists('s')) { // Function may be not available for some early errors.
-
 
2122
                    $debuginfo = s($debuginfo);
-
 
2123
                } else {
-
 
2124
                    // Because weblib is not available for these early errors, we
-
 
2125
                    // just duplicate s() code here to be safe.
-
 
2126
                    $debuginfo = preg_replace('/&amp;#(\d+|x[0-9a-f]+);/i', '&#$1;',
-
 
2127
                    htmlspecialchars($debuginfo, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE));
-
 
2128
                }
-
 
2129
                $debuginfo = str_replace("\n", '<br />', $debuginfo); // keep newlines
-
 
2130
                $content .= '<div class="notifytiny">Debug info: ' . $debuginfo . '</div>';
-
 
2131
            }
-
 
2132
            if (!empty($backtrace)) {
-
 
2133
                $content .= '<div class="notifytiny">Stack trace: ' . format_backtrace($backtrace, false) . '</div>';
-
 
2134
            }
-
 
2135
        }
-
 
2136
 
-
 
2137
        return $content;
-
 
2138
    }
-
 
2139
 
-
 
2140
    /**
-
 
2141
     * This function should only be called by this class, or from exception handlers
-
 
2142
     * @static
-
 
2143
     * @param string $message error message
-
 
2144
     * @param string $moreinfourl (ignored in early errors)
-
 
2145
     * @param string $link (ignored in early errors)
-
 
2146
     * @param array $backtrace
-
 
2147
     * @param string $debuginfo extra information for developers
-
 
2148
     * @return ?string
-
 
2149
     */
-
 
2150
    public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = null) {
-
 
2151
        global $CFG;
-
 
2152
 
-
 
2153
        if (CLI_SCRIPT) {
-
 
2154
            echo "!!! $message !!!\n";
-
 
2155
            if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) {
-
 
2156
                if (!empty($debuginfo)) {
-
 
2157
                    echo "\nDebug info: $debuginfo";
-
 
2158
                }
-
 
2159
                if (!empty($backtrace)) {
-
 
2160
                    echo "\nStack trace: " . format_backtrace($backtrace, true);
-
 
2161
                }
-
 
2162
            }
-
 
2163
            return;
-
 
2164
 
-
 
2165
        } else if (AJAX_SCRIPT) {
-
 
2166
            $e = new stdClass();
-
 
2167
            $e->error      = $message;
-
 
2168
            $e->stacktrace = NULL;
-
 
2169
            $e->debuginfo  = NULL;
-
 
2170
            if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) {
-
 
2171
                if (!empty($debuginfo)) {
-
 
2172
                    $e->debuginfo = $debuginfo;
-
 
2173
                }
-
 
2174
                if (!empty($backtrace)) {
-
 
2175
                    $e->stacktrace = format_backtrace($backtrace, true);
-
 
2176
                }
-
 
2177
            }
-
 
2178
            $e->errorcode  = $errorcode;
-
 
2179
            @header('Content-Type: application/json; charset=utf-8');
-
 
2180
            echo json_encode($e);
-
 
2181
            return;
-
 
2182
        }
-
 
2183
 
-
 
2184
        // In the name of protocol correctness, monitoring and performance
-
 
2185
        // profiling, set the appropriate error headers for machine consumption.
-
 
2186
        $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
-
 
2187
        @header($protocol . ' 500 Internal Server Error');
-
 
2188
 
-
 
2189
        // better disable any caching
-
 
2190
        @header('Content-Type: text/html; charset=utf-8');
-
 
2191
        @header('X-UA-Compatible: IE=edge');
-
 
2192
        @header('Cache-Control: no-store, no-cache, must-revalidate');
-
 
2193
        @header('Cache-Control: post-check=0, pre-check=0', false);
-
 
2194
        @header('Pragma: no-cache');
-
 
2195
        @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
-
 
2196
        @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
-
 
2197
 
-
 
2198
        if (function_exists('get_string')) {
-
 
2199
            $strerror = get_string('error');
-
 
2200
        } else {
-
 
2201
            $strerror = 'Error';
-
 
2202
        }
-
 
2203
 
-
 
2204
        $content = self::early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo);
-
 
2205
 
-
 
2206
        return self::plain_page($strerror, $content);
-
 
2207
    }
-
 
2208
 
-
 
2209
    /**
-
 
2210
     * Early notification message
-
 
2211
     * @static
-
 
2212
     * @param string $message
-
 
2213
     * @param string $classes usually notifyproblem or notifysuccess
-
 
2214
     * @return string
-
 
2215
     */
-
 
2216
    public static function early_notification($message, $classes = 'notifyproblem') {
-
 
2217
        return '<div class="' . $classes . '">' . $message . '</div>';
-
 
2218
    }
-
 
2219
 
-
 
2220
    /**
-
 
2221
     * Page should redirect message.
-
 
2222
     * @static
-
 
2223
     * @param string $encodedurl redirect url
-
 
2224
     * @return string
-
 
2225
     */
-
 
2226
    public static function plain_redirect_message($encodedurl) {
-
 
2227
        $message = '<div style="margin-top: 3em; margin-left:auto; margin-right:auto; text-align:center;">' . get_string('pageshouldredirect') . '<br /><a href="'.
-
 
2228
                $encodedurl .'">'. get_string('continue') .'</a></div>';
-
 
2229
        return self::plain_page(get_string('redirect'), $message);
-
 
2230
    }
-
 
2231
 
-
 
2232
    /**
-
 
2233
     * Early redirection page, used before full init of $PAGE global
-
 
2234
     * @static
-
 
2235
     * @param string $encodedurl redirect url
-
 
2236
     * @param string $message redirect message
-
 
2237
     * @param int $delay time in seconds
-
 
2238
     * @return string redirect page
-
 
2239
     */
-
 
2240
    public static function early_redirect_message($encodedurl, $message, $delay) {
-
 
2241
        $meta = '<meta http-equiv="refresh" content="'. $delay .'; url='. $encodedurl .'" />';
-
 
2242
        $content = self::early_error_content($message, null, null, null);
-
 
2243
        $content .= self::plain_redirect_message($encodedurl);
-
 
2244
 
-
 
2245
        return self::plain_page(get_string('redirect'), $content, $meta);
-
 
2246
    }
-
 
2247
 
-
 
2248
    /**
-
 
2249
     * Output basic html page.
-
 
2250
     * @static
-
 
2251
     * @param string $title page title
-
 
2252
     * @param string $content page content
-
 
2253
     * @param string $meta meta tag
-
 
2254
     * @return string html page
-
 
2255
     */
-
 
2256
    public static function plain_page($title, $content, $meta = '') {
-
 
2257
        global $CFG;
-
 
2258
 
-
 
2259
        if (function_exists('get_string') && function_exists('get_html_lang')) {
-
 
2260
            $htmllang = get_html_lang();
-
 
2261
        } else {
-
 
2262
            $htmllang = '';
-
 
2263
        }
-
 
2264
 
-
 
2265
        $footer = '';
-
 
2266
        if (function_exists('get_performance_info')) { // Function may be not available for some early errors.
-
 
2267
            if (MDL_PERF_TEST) {
-
 
2268
                $perfinfo = get_performance_info();
-
 
2269
                $footer = '<footer>' . $perfinfo['html'] . '</footer>';
-
 
2270
            }
-
 
2271
        }
-
 
2272
 
-
 
2273
        ob_start();
-
 
2274
        include($CFG->dirroot . '/error/plainpage.php');
-
 
2275
        $html = ob_get_contents();
-
 
2276
        ob_end_clean();
-
 
2277
 
-
 
2278
        return $html;
-
 
2279
    }
-
 
Línea 2280... Línea 1638...
2280
}
1638
 
2281
 
1639
 
2282
/**
1640
/**
2283
 * Add http stream instrumentation
1641
 * Add http stream instrumentation