Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace core\output;
18
 
19
use coding_exception;
20
 
21
/**
22
 * This class solves the problem of how to initialise $OUTPUT.
23
 *
24
 * The problem is caused be two factors
25
 * <ol>
26
 * <li>On the one hand, we cannot be sure when output will start. In particular,
27
 * an error, which needs to be displayed, could be thrown at any time.</li>
28
 * <li>On the other hand, we cannot be sure when we will have all the information
29
 * necessary to correctly initialise $OUTPUT. $OUTPUT depends on the theme, which
30
 * (potentially) depends on the current course, course categories, and logged in user.
31
 * It also depends on whether the current page requires HTTPS.</li>
32
 * </ol>
33
 *
34
 * So, it is hard to find a single natural place during Moodle script execution,
35
 * which we can guarantee is the right time to initialise $OUTPUT. Instead we
36
 * adopt the following strategy
37
 * <ol>
38
 * <li>We will initialise $OUTPUT the first time it is used.</li>
39
 * <li>If, after $OUTPUT has been initialised, the script tries to change something
40
 * that $OUTPUT depends on, we throw an exception making it clear that the script
41
 * did something wrong.
42
 * </ol>
43
 *
44
 * The only problem with that is, how do we initialise $OUTPUT on first use if,
45
 * it is going to be used like $OUTPUT->somthing(...)? Well that is where this
46
 * class comes in. Initially, we set up $OUTPUT = new bootstrap_renderer(). Then,
47
 * when any method is called on that object, we initialise $OUTPUT, and pass the call on.
48
 *
49
 * Note that this class is used before lib/outputlib.php has been loaded, so we
50
 * must be careful referring to classes/functions from there, they may not be
51
 * defined yet, and we must avoid fatal errors.
52
 *
53
 * @package    core
54
 * @copyright 2009 Tim Hunt
55
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
56
 * @since     Moodle 2.0
57
 */
58
class bootstrap_renderer {
59
    /**
60
     * Handles re-entrancy. Without this, errors or debugging output that occur
61
     * during the initialisation of $OUTPUT, cause infinite recursion.
62
     *
63
     * @var bool
64
     */
65
    protected $initialising = false;
66
 
67
    /**
68
     * Whether output has started yet.
69
     *
70
     * @return bool true if the header has been printed.
71
     */
72
    public function has_started() {
73
        return false;
74
    }
75
 
76
    /**
77
     * Constructor - to be used by core code only.
78
     *
79
     * @param string $method The method to call
80
     * @param array $arguments Arguments to pass to the method being called
81
     * @return string
82
     */
83
    public function __call($method, $arguments) {
84
        // phpcs:disable moodle.PHP.ForbiddenGlobalUse.BadGlobal
85
        global $OUTPUT, $PAGE;
86
 
87
        $recursing = false;
88
        if ($method == 'notification') {
89
            // Catch infinite recursion caused by debugging output during print_header.
90
            // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
91
            $backtrace = debug_backtrace();
92
            array_shift($backtrace);
93
            array_shift($backtrace);
94
            $recursing = is_early_init($backtrace);
95
        }
96
 
97
        $earlymethods = [
98
            'fatal_error' => 'early_error',
99
            'notification' => 'early_notification',
100
        ];
101
 
102
        // If lib/outputlib.php has been loaded, call it.
103
        if (!empty($PAGE) && !$recursing) {
104
            if (array_key_exists($method, $earlymethods)) {
105
                // Prevent PAGE->context warnings - exceptions might appear before we set any context.
106
                $PAGE->set_context(null);
107
            }
108
            $PAGE->initialise_theme_and_output();
109
            return call_user_func_array([$OUTPUT, $method], $arguments);
110
        }
111
 
112
        $this->initialising = true;
113
 
114
        // Too soon to initialise $OUTPUT, provide a couple of key methods.
115
        if (array_key_exists($method, $earlymethods)) {
116
            return call_user_func_array([self::class, $earlymethods[$method]], $arguments);
117
        }
118
 
119
        throw new coding_exception('Attempt to start output before enough information is known to initialise the theme.');
120
    }
121
 
122
    /**
123
     * Returns nicely formatted error message in a div box.
124
     *
125
     * @param string $message error message
126
     * @param ?string $moreinfourl (ignored in early errors)
127
     * @param ?string $link (ignored in early errors)
128
     * @param ?array $backtrace
129
     * @param ?string $debuginfo
130
     * @return string
131
     */
132
    public static function early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo = null) {
133
        global $CFG;
134
 
135
        $content = "<div class='alert-danger'>$message</div>";
136
        // Check whether debug is set.
137
        $debug = (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER);
138
        // Also check we have it set in the config file. This occurs if the method to read the config table from the
139
        // database fails, reading from the config table is the first database interaction we have.
140
        $debug = $debug || (!empty($CFG->config_php_settings['debug'])  && $CFG->config_php_settings['debug'] >= DEBUG_DEVELOPER );
141
        if ($debug) {
142
            if (!empty($debuginfo)) {
143
                // Remove all nasty JS.
144
                if (function_exists('s')) { // Function may be not available for some early errors.
145
                    $debuginfo = s($debuginfo);
146
                } else {
147
                    // Because weblib is not available for these early errors, we
148
                    // just duplicate s() code here to be safe.
149
                    $debuginfo = preg_replace(
150
                        '/&amp;#(\d+|x[0-9a-f]+);/i',
151
                        '&#$1;',
152
                        htmlspecialchars($debuginfo, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE)
153
                    );
154
                }
155
                $debuginfo = str_replace("\n", '<br />', $debuginfo); // Keep newlines.
156
                $content .= '<div class="notifytiny">Debug info: ' . $debuginfo . '</div>';
157
            }
158
            if (!empty($backtrace)) {
159
                $content .= '<div class="notifytiny">Stack trace: ' . format_backtrace($backtrace, false) . '</div>';
160
            }
161
        }
162
 
163
        return $content;
164
    }
165
 
166
    /**
167
     * This function should only be called by this class, or from exception handlers
168
     *
169
     * @param string $message error message
170
     * @param string $moreinfourl (ignored in early errors)
171
     * @param string $link (ignored in early errors)
172
     * @param array $backtrace
173
     * @param string $debuginfo extra information for developers
174
     * @return ?string
175
     */
176
    public static function early_error($message, $moreinfourl, $link, $backtrace, $debuginfo = null, $errorcode = null) {
177
        global $CFG;
178
 
179
        if (CLI_SCRIPT) {
180
            echo "!!! $message !!!\n";
181
            if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) {
182
                if (!empty($debuginfo)) {
183
                    echo "\nDebug info: $debuginfo";
184
                }
185
                if (!empty($backtrace)) {
186
                    echo "\nStack trace: " . format_backtrace($backtrace, true);
187
                }
188
            }
189
            return;
190
        } else if (AJAX_SCRIPT) {
191
            $error = (object) [
192
                'error' => $message,
193
                'stacktrace' => null,
194
                'debuginfo' => null,
195
                'errorcode' => $errorcode,
196
            ];
197
            if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) {
198
                if (!empty($debuginfo)) {
199
                    $error->debuginfo = $debuginfo;
200
                }
201
                if (!empty($backtrace)) {
202
                    $error->stacktrace = format_backtrace($backtrace, true);
203
                }
204
            }
205
            @header('Content-Type: application/json; charset=utf-8');
206
            echo json_encode($error);
207
            return;
208
        }
209
 
210
        // In the name of protocol correctness, monitoring and performance
211
        // profiling, set the appropriate error headers for machine consumption.
212
        $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0');
213
        @header($protocol . ' 500 Internal Server Error');
214
 
215
        // Better disable any caching.
216
        @header('Content-Type: text/html; charset=utf-8');
217
        @header('X-UA-Compatible: IE=edge');
218
        @header('Cache-Control: no-store, no-cache, must-revalidate');
219
        @header('Cache-Control: post-check=0, pre-check=0', false);
220
        @header('Pragma: no-cache');
221
        @header('Expires: Mon, 20 Aug 1969 09:23:00 GMT');
222
        @header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
223
 
224
        if (function_exists('get_string')) {
225
            $strerror = get_string('error');
226
        } else {
227
            $strerror = 'Error';
228
        }
229
 
230
        $content = self::early_error_content($message, $moreinfourl, $link, $backtrace, $debuginfo);
231
 
232
        return self::plain_page($strerror, $content);
233
    }
234
 
235
    /**
236
     * Early notification message
237
     *
238
     * @param string $message
239
     * @param string $classes usually notifyproblem or notifysuccess
240
     * @return string
241
     */
242
    public static function early_notification($message, $classes = 'notifyproblem') {
243
        return '<div class="' . $classes . '">' . $message . '</div>';
244
    }
245
 
246
    /**
247
     * Page should redirect message.
248
     *
249
     * @param string $encodedurl redirect url
250
     * @return string
251
     */
252
    public static function plain_redirect_message($encodedurl) {
253
        $message = '<div style="margin-top: 3em; margin-left:auto; margin-right:auto; text-align:center;">';
254
        $message .= get_string('pageshouldredirect') . '<br /><a href="';
255
        $message .= $encodedurl . '">' . get_string('continue') . '</a></div>';
256
        return self::plain_page(get_string('redirect'), $message);
257
    }
258
 
259
    /**
260
     * Early redirection page, used before full init of $PAGE global.
261
     *
262
     * @param string $encodedurl redirect url
263
     * @param string $message redirect message
264
     * @param int $delay time in seconds
265
     * @return string redirect page
266
     */
267
    public static function early_redirect_message($encodedurl, $message, $delay) {
268
        $meta = '<meta http-equiv="refresh" content="' . $delay . '; url=' . $encodedurl . '" />';
269
        $content = self::early_error_content($message, null, null, null);
270
        $content .= self::plain_redirect_message($encodedurl);
271
 
272
        return self::plain_page(get_string('redirect'), $content, $meta);
273
    }
274
 
275
    /**
276
     * Output basic html page.
277
     *
278
     * @param string $title page title
279
     * @param string $content page content
280
     * @param string $meta meta tag
281
     * @return string html page
282
     */
283
    public static function plain_page($title, $content, $meta = '') {
284
        global $CFG;
285
 
286
        if (function_exists('get_string') && function_exists('get_html_lang')) {
287
            $htmllang = get_html_lang();
288
        } else {
289
            $htmllang = '';
290
        }
291
 
292
        $footer = '';
293
        if (function_exists('get_performance_info')) { // Function may be not available for some early errors.
294
            if (MDL_PERF_TEST) {
295
                $perfinfo = get_performance_info();
296
                $footer = '<footer>' . $perfinfo['html'] . '</footer>';
297
            }
298
        }
299
 
300
        ob_start();
301
        include($CFG->dirroot . '/error/plainpage.php');
302
        $html = ob_get_contents();
303
        ob_end_clean();
304
 
305
        return $html;
306
    }
307
}
308
 
309
// Alias this class to the old name.
310
// This file will be autoloaded by the legacyclasses autoload system.
311
// In future all uses of this class will be corrected and the legacy references will be removed.
312
class_alias(bootstrap_renderer::class, \bootstrap_renderer::class);