Overview
Comment:Utilisation de KD2\ErrorManager comme gestionnaire d'erreur au lieu du gestionnaire maison
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | dev
Files: files | file ages | folders
SHA1: d56397f05850eb40ae64ec39d18dd372256b9c61
User & Date: bohwaz on 2017-01-19 03:58:49
Other Links: branch diff | manifest | tags
Context
2017-01-19
04:22
Correction de certains bugs dans les templates avec la transition à Smartyer check-in: 2a49b9aeb6 user: bohwaz tags: dev
03:58
Utilisation de KD2\ErrorManager comme gestionnaire d'erreur au lieu du gestionnaire maison check-in: d56397f058 user: bohwaz tags: dev
2017-01-18
23:43
Templates : Suppression des "escape" devenus inutiles check-in: e633a1ec56 user: bohwaz tags: dev
Changes

Modified src/include/init.php from [fa76653d43] to [0a9a2da865].

1
2
3
4


5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11
12
13




+
+







<?php

namespace Garradin;

use KD2\ErrorManager;

error_reporting(-1);

/*
 * Version de Garradin
 */

function garradin_version()
109
110
111
112
113
114
115
116

117
118
119
120
121
122
123
111
112
113
114
115
116
117

118
119
120
121
122
123
124
125







-
+







{
    define('Garradin\PLUGINS_SYSTEM', '');
}

// Affichage des erreurs par défaut
if (!defined('Garradin\SHOW_ERRORS'))
{
    define('Garradin\SHOW_ERRORS', true);
    define('Garradin\SHOW_ERRORS', false);
}

if (!defined('Garradin\MAIL_ERRORS'))
{
    define('Garradin\MAIL_ERRORS', false);
}

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179

180
181
182

183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

289
290
291
292
293
294
295
296



297
298
299
300
301







302
303
304
305
306
307
308
309
310
311

312
313
314
315
316


317
318
319
320
321
322
323
324
325

326
327
328
329
330
331
332



333
334



























































335
336

337
338
339
340
341
342
343
151
152
153
154
155
156
157
























158



159





































































































160
161
162
163

164
165
166
167
168
169



170
171
172
173
174
175
176

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197


198
199
200




201
202
203

204
205
206
207
208
209
210
211
212
213
214


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

275
276
277
278
279
280
281
282







-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-




-
+





-
-
-
+
+
+




-
+
+
+
+
+
+
+










+



-
-
+
+

-
-
-
-



-
+







+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

-
+







    }
    else
    {
        ini_set('date.timezone', 'Europe/Paris');
    }
}

ini_set('error_log', DATA_ROOT . '/error.log');
ini_set('log_errors', true);

if (SHOW_ERRORS)
{
    // Gestion par défaut des erreurs
    ini_set('display_errors', true);
    ini_set('html_errors', false);

    if (PHP_SAPI != 'cli')
    {
        ini_set('error_prepend_string', '<!DOCTYPE html><meta charset="utf-8" /><style type="text/css">body { font-family: sans-serif; } h3 { color: darkred; } 
            pre { text-shadow: 2px 2px 5px black; color: darkgreen; font-size: 2em; float: left; margin: 0 1em 0 0; padding: 1em; background: #cfc; border-radius: 50px; }</style>
            <pre> \__/<br /> (xx)<br />//||\\\\</pre>
            <h1>Erreur fatale</h1>
            <p>Une erreur fatale s\'est produite à l\'exécution de Garradin. Pour rapporter ce bug
            merci d\'inclure le message ci-dessous :</p>
            <h3>');
        ini_set('error_append_string', '</h3><hr />
            <p><a href="http://dev.kd2.org/garradin/Rapporter%20un%20bug">Comment rapporter un bug</a></p>');
    }
}

/*
/**
 * Gestion des erreurs et exceptions
 */

 * Auto-chargement des dépendances
class UserException extends \LogicException
{
}

function exception_error_handler($errno, $errstr, $errfile, $errline )
{
    // For @ ignored errors
    if (error_reporting() === 0) return;
    throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
}

function exception_handler($e)
{
    if ($e instanceOf UserException || $e instanceOf \KD2\MiniSkelMarkupException)
    {
        try {
            if (PHP_SAPI == 'cli')
            {
                echo $e->getMessage();
            }
            else
            {
                $tpl = Template::getInstance();

                $tpl->assign('error', $e->getMessage());
                $tpl->display('error.tpl');
            }

            exit;
        }
        catch (Exception $e)
        {
        }
    }

    $file = str_replace(ROOT, '', $e->getFile());

    $error = "Exception of type ".get_class($e)." happened !\n\n".
        $e->getCode()." - ".$e->getMessage()."\n\nIn: ".
        $file . ":" . $e->getLine()."\n\n";

    if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI']))
        $error .= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n\n";

    $error .= $e->getTraceAsString();
    $error .= "\n-------------\n";
    $error .= 'Garradin version: ' . garradin_version() . "\n";
    $error .= 'Garradin manifest: ' . garradin_manifest() . "\n";
    $error .= 'PHP version: ' . phpversion() . "\n";
    $error .= 'Garradin data root: ' . \Garradin\DATA_ROOT . "\n";

    foreach ($_SERVER as $key=>$value)
    {
        if (is_array($value))
            $value = json_encode($value);

        $error .= $key . ': ' . $value . "\n";
    }
    
    $error = str_replace("\r", '', $error);
    error_log($error);
    
    if (MAIL_ERRORS)
    {
        mail(MAIL_ERRORS, '[Garradin] Erreur d\'exécution', $error, 'From: "' . WWW_URL . '" <noreply@no.reply>');
    }

    if (PHP_SAPI == 'cli')
    {
        echo $error;
    }
    else
    {
        echo '<!DOCTYPE html><meta charset="utf-8" /><style type="text/css">body { font-family: sans-serif; } h3 { color: darkred; }
        pre { text-shadow: 2px 2px 5px black; color: darkgreen; font-size: 2em; float: left; margin: 0 1em 0 0; padding: 1em; background: #cfc; border-radius: 50px; }</style>
        <pre> \__/<br /> (xx)<br />//||\\\\</pre>
        <h1>Erreur d\'exécution</h1>';

        if (SHOW_ERRORS)
        {
            echo '<p>Une erreur s\'est produite à l\'exécution de Garradin. Pour rapporter ce bug
            merci d\'inclure le message suivant :</p>
            <textarea cols="70" rows="'.substr_count($error, "\n").'">'.htmlspecialchars($error, ENT_QUOTES, 'UTF-8').'</textarea>
            <hr />
            <p><a href="http://dev.kd2.org/garradin/Rapporter%20un%20bug">Comment rapporter un bug</a></p>';
        }
        else
        {
            echo '<p>Une erreur s\'est produite à l\'exécution de Garradin.</p>
            <p>Le webmaster a été prévenu.</p>';
        }
    }

    exit;
}

set_error_handler('Garradin\exception_error_handler');
set_exception_handler('Garradin\exception_handler');

/**
 * Auto-load classes and libs
 */
class Loader
{
    /**
     * Already loaded filenames
     * Liste des classes déjà chargées
     * @var array
     */
    static protected $loaded = [];

    /**
     * Loads a class from the $name
     * @param  stringg $classname
     * @return bool true
     * Inclure un fichier de classe depuis le nom de la classe
     * @param  string $classname
     * @return void
     */
    static public function load($classname)
    {
        $classname = ltrim($classname, '\\');
        

        if (array_key_exists($classname, self::$loaded))
        {
            return true;
        }
        
        // Plugins
        if (substr($classname, 0, 16) == 'Garradin\\Plugin\\')
        {
            $classname = substr($classname, 16);
            $plugin_name = substr($classname, 0, strpos($classname, '\\'));
            $filename = str_replace('\\', '/', substr($classname, strpos($classname, '\\')+1));
            
            $path = 'phar://' . PLUGINS_ROOT . '/' . strtolower($plugin_name) . '.tar.gz/lib/' . $filename . '.php';
        }
        else
        {
            // PSR-0 autoload
            $filename = str_replace('\\', '/', $classname);
            $path = ROOT . '/include/lib/' . $filename . '.php';
        }

        if (array_key_exists($path, self::$loaded))
        
        if (!file_exists($path))
        {
            return true;
        }

        if (!file_exists($path)) {
            throw new \Exception('File '.$path.' doesn\'t exists');
        }

        self::$loaded[$path] = true;
        self::$loaded[$classname] = true;

        require $path;
    }
}

\spl_autoload_register(['Garradin\Loader', 'load'], true);

/*
 * Gestion des erreurs et exceptions
 */
$n = new Membres;


class UserException extends \LogicException
{
}

// activer le gestionnaire d'erreurs/exceptions
ErrorManager::enable(SHOW_ERRORS ? ErrorManager::DEVELOPMENT : ErrorManager::PRODUCTION);
ErrorManager::setLogFile(DATA_ROOT . '/error.log');

// activer l'envoi de mails si besoin est
if (MAIL_ERRORS)
{
    ErrorManager::setEmail(MAIL_ERRORS);
}

ErrorManager::setExtraDebugEnv([
    'Garradin version' => garradin_version(),
    'Garradin data root' => DATA_ROOT,
    ]);

ErrorManager::setProductionErrorTemplate('<!DOCTYPE html><html><head><title>Erreur interne</title>
    <style type="text/css">
    body {font-family: sans-serif; }
    code, p, h1 { max-width: 400px; margin: 1em auto; display: block; }
    code { text-align: right; color: #666; }
    a { color: blue; }
    </style></head><body><h1>Erreur interne</h1><p>Désolé mais le serveur a rencontré une erreur interne
    et ne peut répondre à votre requête. Merci de ré-essayer plus tard.</p>
    <p>Si vous suspectez un bug dans Garradin, vous pouvez suivre 
    <a href="http://dev.kd2.org/garradin/Rapporter+un+bug">ces instructions</a>
    pour le rapporter.</p>
    <if(email)><p>Un-e responsable a été notifié-e et cette erreur sera corrigée dès que possible.</p></if>
    <if(log)><code>Référence : <b>{$ref}</b></code></if>
    <p><a href="' . WWW_URL . '">&larr; Retour à la page d\'accueil</a></p>
    </body></html>');

ErrorManager::setHtmlFooter('<hr /><section><article>Cette erreur est peut-être un bug dans Garradin&nbsp;? En ce cas vous pouvez le rapporter en suivant <a href="http://dev.kd2.org/garradin/Rapporter+un+bug">ces instructions</a>.</section></article>');

function user_error($e)
{
    if (PHP_SAPI == 'cli')
    {
        echo $e->getMessage();
    }
    else
    {
        $tpl = Template::getInstance();

        $tpl->assign('error', $e->getMessage());
        $tpl->display('error.tpl');
    }

    exit;
}

// Message d'erreur simple pour les erreurs de l'utilisateur
ErrorManager::setCustomExceptionHandler('\Garradin\UserException', '\Garradin\user_error');
ErrorManager::setCustomExceptionHandler('\KD2\MiniSkelMarkupException', '\Garradin\user_error');

/*
 * Inclusion des fichiers de base
 * Vérifications pour enclencher le processus d'installation ou de mise à jour
 */

if (!defined('Garradin\INSTALL_PROCESS') && !defined('Garradin\UPGRADE_PROCESS'))
{
    if (!file_exists(DB_FILE))
    {
        Utils::redirect('/admin/install.php');