Add Platal::assert() function
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Feb 2010 20:47:42 +0000 (21:47 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 22 Feb 2010 20:47:42 +0000 (21:47 +0100)
Used to put assertion in the code.
If an assertion fails, a message is written to spool/tmp/assert_errors,
   and another one is shown to the user

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/platal.php
modules/core.php
templates/assert_errors.tpl [new file with mode: 0644]

index 866d7c2..6d32374 100644 (file)
@@ -356,6 +356,20 @@ abstract class Platal
         }
     }
 
+       public static function assert($cond, $error, $userfriendly)
+       {
+               global $globals;
+               if ($cond === false) {
+                       header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error');
+                       $file = fopen($globals->spoolroot . '/spool/tmp/assert_erros', 'a');
+                       fwrite($file, '<pre>' . pl_entities($error) . '</pre>\n');
+                       fclose($file);
+
+                       Platal::page()->kill($userfriendly);
+               }
+       }
+
+
     static public function &page()
     {
         global $platal;
index 63d7d9e..adbcf38 100644 (file)
@@ -31,6 +31,7 @@ class CoreModule extends PLModule
             'purge_cache'   => $this->make_hook('purge_cache',   AUTH_COOKIE, 'admin'),
             'kill_sessions' => $this->make_hook('kill_sessions', AUTH_COOKIE, 'admin'),
             'sql_errors'    => $this->make_hook('sqlerror',      AUTH_COOKIE, 'admin'),
+            'assert_errors' => $this->make_hook('asserterror',      AUTH_COOKIE, 'admin'),
 
             'wiki_help'     => $this->make_hook('wiki_help',     AUTH_PUBLIC),
             'wiki_preview'  => $this->make_hook('wiki_preview',  AUTH_COOKIE, 'user', NO_AUTH),
@@ -188,6 +189,19 @@ class CoreModule extends PLModule
             $page->trigSuccess("Erreurs MySQL effacées.");
         }
     }
+
+    function handler_asserterror(&$page) {
+        global $globals;
+        $page->coreTpl('assert_errors.tpl');
+        $file = @file_get_contents($globals->spoolroot . '/spool/tmp/assert_errors');
+        if ($file !== false) {
+            $page->assign('errors', utf8_encode($file));
+        }
+        if (Post::has('clear')) {
+            @unlink($globals->spoolroot . '/spool/tmp/assert_errors');
+            $page->trigSuccess("Erreurs d'assertion effacées.");
+        }
+    }
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
diff --git a/templates/assert_errors.tpl b/templates/assert_errors.tpl
new file mode 100644 (file)
index 0000000..058a5cf
--- /dev/null
@@ -0,0 +1,38 @@
+{**************************************************************************}
+{*                                                                        *}
+{*  Copyright (C) 2003-2010 Polytechnique.org                             *}
+{*  http://opensource.polytechnique.org/                                  *}
+{*                                                                        *}
+{*  This program is free software; you can redistribute it and/or modify  *}
+{*  it under the terms of the GNU General Public License as published by  *}
+{*  the Free Software Foundation; either version 2 of the License, or     *}
+{*  (at your option) any later version.                                   *}
+{*                                                                        *}
+{*  This program is distributed in the hope that it will be useful,       *}
+{*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *}
+{*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *}
+{*  GNU General Public License for more details.                          *}
+{*                                                                        *}
+{*  You should have received a copy of the GNU General Public License     *}
+{*  along with this program; if not, write to the Free Software           *}
+{*  Foundation, Inc.,                                                     *}
+{*  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA               *}
+{*                                                                        *}
+{**************************************************************************}
+
+<h1>Erreurs d'assertions</h1>
+<p>
+  {if $errors}
+    {$errors|smarty:nodefaults}
+  {else}
+    Il n'y a pas d'erreurs actuellement recensées.
+  {/if}
+</p>
+<form action="assert_errors" method="post">
+  <div>
+    <input type="submit" name="clear" value="Effacer les erreurs" />
+  </div>
+</form>
+
+
+{* vim:set et sws=2 sts=2 sw=2 enc=utf-8: *}