Ooops.
[platal.git] / classes / plpage.php
index 30a29b3..c2cd19e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2008 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -19,7 +19,9 @@
  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                *
  ***************************************************************************/
 
-require_once 'smarty/libs/Smarty.class.php';
+if (!@include_once 'smarty/libs/Smarty.class.php') {
+    require_once 'smarty/Smarty.class.php';
+}
 
 abstract class PlPage extends Smarty
 {
@@ -40,24 +42,25 @@ abstract class PlPage extends Smarty
         $this->caching       = false;
         $this->config_overwrite = false;
         $this->use_sub_dirs  = false;
-        $this->template_dir  = $globals->spoolroot."/templates/";
-        $this->compile_dir   = $globals->spoolroot."/spool/templates_c/";
-        array_unshift($this->plugins_dir, $globals->spoolroot."/plugins/");
-        $this->config_dir    = $globals->spoolroot."/configs/";
+        $this->template_dir  = $globals->spoolroot . '/templates/';
+        $this->compile_dir   = $globals->spoolroot . '/spool/templates_c/';
+        array_unshift($this->plugins_dir,
+                      $globals->spoolroot . '/core/plugins/',
+                      $globals->spoolroot . '/plugins/');
+        $this->config_dir    = $globals->spoolroot . '/configs/';
 
-        $this->compile_check = !empty($globals->debug);
+        $this->compile_check = !empty($globals->debug) || $globals->smarty_autocompile;
 
         $this->_errors    = array('errors' => array());
         $this->_jsonVars  = array();
         $this->_failure   = false;
 
         if ($globals->mode != 'rw') {
-            $this->trigError("En raison d'une maintenance, une partie des fonctionnalités du site sont"
-                      . " actuellement désactivée, en particuliers aucune donnée ne sera sauvegardée");
+            $this->trigError("En raison d'une maintenance, une partie des fonctionnalités du site est"
+                             . " actuellement désactivée, en particulier aucune donnée ne sera sauvegardée");
         }
         $this->register_prefilter('at_to_globals');
-        $this->addJsLink('xorg.js');
-        $this->addJsLink('jquery.js');
+        $this->register_prefilter('get_class_constants');
     }
 
     // }}}
@@ -65,9 +68,29 @@ abstract class PlPage extends Smarty
 
     public function changeTpl($tpl, $type = SKINNED)
     {
-      $this->_tpl       = $tpl;
-      $this->_page_type = $type;
-      $this->assign('xorg_tpl', $tpl);
+        $this->_tpl       = $tpl;
+        $this->_page_type = $type;
+        $this->assign('pl_tpl', $tpl);
+    }
+
+    // }}}
+    // {{{ function getCoreTpl()
+
+    public static function getCoreTpl($tpl)
+    {
+        global $globals;
+        return $globals->spoolroot . '/core/templates/' . $tpl;
+    }
+
+    // }}}
+    // {{{ function coreTpl()
+
+    /** Use a template from the core.
+     */
+    public function coreTpl($tpl, $type = SKINNED)
+    {
+        global $globals;
+        $this->changeTpl(self::getCoreTpl($tpl), $type);
     }
 
     // }}}
@@ -85,29 +108,38 @@ abstract class PlPage extends Smarty
 
     protected function _run($skin)
     {
-        global $globals, $TIME_BEGIN;
+        global $globals, $platal, $TIME_BEGIN;
 
-        session_write_close();
+        Platal::session()->close();
 
         $this->register_prefilter('trimwhitespace');
         $this->register_prefilter('form_force_encodings');
         $this->register_prefilter('wiki_include');
-        $this->register_prefilter('if_has_perms');
-        $this->assign('xorg_triggers', $this->_errors);
-        $this->assign('xorg_errors', $this->nb_errs());
-        $this->assign('xorg_failure', $this->_failure);
-        $this->assign('globals', $globals);
+        $this->register_prefilter('core_include');
+        $this->register_prefilter('if_rewrites');
+        $this->assign('pl_triggers', $this->_errors);
+        $this->assign('pl_errors', $this->nb_errs());
+        $this->assign('pl_failure', $this->_failure);
+        $this->assign_by_ref('platal', $platal);
+        $this->assign_by_ref('globals', $globals);
 
         if (Env::has('json') && count($this->_jsonVars)) {
             return $this->jsonDisplay();
         }
 
-        if (Env::v('display') == 'light') {
+        $display = Env::s('display');
+        if ($display == 'light' && $this->_page_type == SKINNED) {
             $this->_page_type = SIMPLE;
-        } elseif (Env::v('display') == 'raw') {
+        } elseif ($display == 'raw') {
             $this->_page_type = NO_SKIN;
-        } elseif (Env::v('display') == 'full') {
-            $this->_page_typ = SKINNED;
+        } elseif ($display == 'full') {
+            $this->_page_type = SKINNED;
+        }
+
+        if ($this->_page_type == SIMPLE) {
+            $this->assign('simple', true);
+        } else {
+            $this->assign('simple', false);
         }
 
         switch ($this->_page_type) {
@@ -119,8 +151,6 @@ abstract class PlPage extends Smarty
             exit;
 
           case SIMPLE:
-            $this->assign('simple', true);
-
           case SKINNED:
             $this->register_modifier('escape_html', 'escape_html');
             $this->default_modifiers = Array('@escape_html');
@@ -128,15 +158,15 @@ abstract class PlPage extends Smarty
         if (S::i('auth') <= AUTH_PUBLIC) {
             $this->register_outputfilter('hide_emails');
         }
-        $this->addJsLink('wiki.js');
         header("Accept-Charset: utf-8");
         if (Env::v('forceXml')) {
-            header("Content-Type: text/xml; charset=utf-8");
+            pl_content_headers("text/xml");
         }
 
         if (!$globals->debug) {
             error_reporting(0);
             $this->display($skin);
+            pl_print_errors(true);
             exit;
         }
 
@@ -152,28 +182,23 @@ abstract class PlPage extends Smarty
         if ($globals->debug & DEBUG_BT) {
             PlBacktrace::clean();
             $this->assign_by_ref('backtraces', PlBacktrace::$bt);
-            $result = str_replace('@@BACKTRACE@@', $this->fetch('skin/common.backtrace.tpl'), $result);
+            $result = str_replace('@@BACKTRACE@@',
+                                  $this->fetch(self::getCoreTpl('backtrace.tpl')),
+                                  $result);
         } else {
             $result = str_replace('@@BACKTRACE@@', '', $result);
         }
 
         $replc  = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
         if ($globals->debug & DEBUG_VALID) {
+            global $platal;
             $fd = fopen($this->compile_dir."/valid.html","w");
             fwrite($fd, $result);
             fclose($fd);
 
-            exec($globals->spoolroot."/bin/devel/xhtml.validate.pl ".$this->compile_dir."/valid.html", $val);
-            foreach ($val as $h) {
-                if (preg_match("/^X-W3C-Validator-Errors: (\d+)$/", $h, $m)) {
-                    $replc = '<span style="color: #080;">HTML OK</span><br />';
-                    if ($m[1]) {
-                        $replc = "<span class='erreur'><a href='http://validator.w3.org/check?uri={$globals->baseurl}"
-                            ."/valid.html&amp;ss=1#result'>{$m[1]} ERREUR(S) !!!</a></span><br />";
-                    }
-                    break;
-                }
-            }
+            $replc = '<span id="html_valid"><span style="color: #860">VALIDATION HTML EN COURS</span></span>'
+                   . '<script type="text/javascript">$("#html_valid").updateHtml("validator");</script>'
+                   . '<br />';
         }
 
         echo str_replace("@HOOK@", $ttime.$replc, $result);
@@ -217,24 +242,92 @@ abstract class PlPage extends Smarty
     }
 
     // }}}
+    // {{{ function trigRedirect
+
+    // Acts as trig(), but replaces the template with a simple one displaying
+    // the error messages and a "continue" link.
+    private function trigRedirect($msg, $continue, $type = 'errors')
+    {
+        $this->trig($msg, $type);
+        $this->coreTpl('msgredirect.tpl');
+        $this->assign('continue', $continue);
+        $this->run();
+    }
+
+    public function trigErrorRedirect($msg, $continue)
+    {
+        $this->trigRedirect($msg, $continue, 'errors');
+    }
+
+    public function trigWarningRedirect($msg, $continue)
+    {
+        $this->trigRedirect($msg, $continue, 'warnings');
+    }
+
+    public function trigSuccessRedirect($msg, $continue)
+    {
+        $this->trigRedirect($msg, $continue, 'success');
+    }
+
+    // }}}
     // {{{ function kill()
 
-    public function kill($msg)
+    public function kill($msg, $type = 'errors')
     {
+        // PHP is used on command line... do not run the whole page stuff.
+        if (php_sapi_name() == 'cli') {
+            echo $msg . "\n";
+            exit(-1);
+        }
+
         global $platal;
 
-        $this->assign('platal', $platal);
-        $this->trigError($msg);
+        $this->trig($msg, $type);
         $this->_failure = true;
         $this->run();
     }
 
+    public function killError($msg)
+    {
+        $this->kill($msg, 'errors');
+    }
+
+    public function killWarning($msg)
+    {
+        $this->kill($msg, 'warnings');
+    }
+
+    public function killSuccess($msg)
+    {
+        $this->kill($msg, 'success');
+    }
+
+    // }}}
+    // {{{ function setTitle
+
+    public function setTitle($title)
+    {
+        global $globals;
+        if (isset($globals->core->sitename)) {
+            $title = $globals->core->sitename . ' :: ' . $title;
+        }
+        $this->assign('pl_title', $title);
+    }
+
     // }}}
     // {{{ function addJsLink
 
-    public function addJsLink($path)
+    public function addJsLink($filename, $internal = true, $static_content = true)
     {
-        $this->append('xorg_js', $path);
+        if ($internal) {
+            if ($static_content) {
+                $this->append('pl_js', pl_static_content_path("javascript/", $filename));
+            } else {
+                $this->append('pl_js', "javascript/$filename");
+            }
+        } else {
+            $this->append('pl_js', $filename);
+        }
     }
 
     // }}}
@@ -242,16 +335,25 @@ abstract class PlPage extends Smarty
 
     public function addCssLink($path)
     {
-        $this->append('xorg_css', $path);
+        $this->append('pl_css', $path);
     }
 
     // }}}
+    // {{{ function addLink
+
+    public function addLink($rel, $path)
+    {
+        $this->append('pl_link', array('rel' => $rel, 'href' => $path));
+    }
+
+
+    // }}}
     // {{{ function addCssInline
 
     public function addCssInline($css)
     {
         if (!empty($css)) {
-            $this->append('xorg_inline_css', $css);
+            $this->append('pl_inline_css', $css);
         }
     }
 
@@ -260,15 +362,18 @@ abstract class PlPage extends Smarty
 
     public function setRssLink($title, $path)
     {
-        $this->assign('xorg_rss', array('title' => $title, 'href' => $path));
+        $this->assign('pl_rss', array('title' => $title, 'href' => $path));
     }
 
     // }}}
     // {{{ function jsonDisplay
     protected function jsonDisplay()
     {
-        header("Content-type: text/javascript; charset=utf-8");
-        array_walk_recursive($this->_jsonVars, "escape_xorgDB");
+        pl_content_headers("text/javascript");
+        if (!empty(PlBacktrace::$bt)) {
+            $this->jsonAssign('pl_backtraces', PlBacktrace::$bt);
+        }
+        array_walk_recursive($this->_jsonVars, "escape_XDB");
         $jsonbegin = Env::v('jsonBegin');
         $jsonend = Env::v('jsonEnd');
         if (Env::has('jsonVar')) {
@@ -282,6 +387,17 @@ abstract class PlPage extends Smarty
         exit;
     }
     // }}}
+
+    public function runJSon()
+    {
+        pl_content_headers("text/javascript");
+        if (!empty(PlBacktrace::$bt)) {
+            $this->jsonAssign('pl_backtraces', PlBacktrace::$bt);
+        }
+        echo json_encode($this->_jsonVars);
+        exit;
+    }
+
     // {{{ function jsonAssign
     public function jsonAssign($var, $value)
     {
@@ -291,9 +407,9 @@ abstract class PlPage extends Smarty
     // }}}
 }
 
-function escape_xorgDB(&$item, $key)
+function escape_XDB(&$item, $key)
 {
-    if (is_a($item, 'XOrgDBIterator')) {
+    if ($item instanceof XDBIterator) {
         $expanded = array();
         while ($a = $item->next()) {
             $expanded[] = $a;
@@ -342,15 +458,36 @@ function _to_globals($s) {
  * compilation plugin used to import $globals confing through #globals.foo.bar# directives
  */
 
-function at_to_globals($tpl_source, &$smarty)
+function at_to_globals($tpl_source, $smarty)
 {
     return preg_replace('/#globals\.([a-zA-Z0-9_.]+?)#/e', '_to_globals(\'\\1\')', $tpl_source);
 }
 
 // }}}
+// {{{ function get_class_constants()
+
+/**
+ * helper
+ */
+
+function _get_class_const($class, $const)
+{
+    return var_export(constant($class . '::' . $const), true);
+}
+
+/**
+ * Compilation plugin used to import class constants through calls to #Class::CONSTANT#
+ */
+
+function get_class_constants($tpl_source, $smarty)
+{
+    return preg_replace('/#([a-zA-Z0-9_]+)::([A-Z0-9_]+)#/e', '_get_class_const(\'\\1\',\'\\2\')', $tpl_source);
+}
+
+// }}}
 // {{{  function trimwhitespace
 
-function trimwhitespace($source, &$smarty)
+function trimwhitespace($source, $smarty)
 {
     $tags = '(script|pre|textarea)';
     preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
@@ -367,30 +504,39 @@ function trimwhitespace($source, &$smarty)
 // }}}
 // {{{ function wiki_include
 
-function wiki_include($source, &$smarty)
+function wiki_include($source, $smarty)
 {
+    global $globals;
     return preg_replace('/\{include( [^}]*)? wiki=([^} ]+)(.*?)\}/ui',
-                        '{include\1 file="../spool/wiki.d/cache_\2.tpl"\3 included=1}',
+                        '{include\1 file="' . $globals->spoolroot . '/spool/wiki.d/cache_\2.tpl"\3 included=1}',
+                        $source);
+}
+
+function core_include($source, $smarty)
+{
+    global $globals;
+    return preg_replace('/\{include( [^}]*)? core=([^} ]+)(.*?)\}/ui',
+                        '{include\1 file="' . $globals->spoolroot . '/core/templates/\2"\3}',
                         $source);
 }
 
 // }}}
 //{{{ function hasPerm
 
-function if_has_perms($source, &$smarty)
+function if_rewrites($source, $smarty)
 {
-    $source = preg_replace('/\{if([^}]*) (\!?)hasPerms\(([^)]+)\)([^}]*)\}/',
-                           '{if\1 \2$smarty.session.perms->hasFlagCombination(\3)\4}',
-                           $source);
-    return preg_replace('/\{if([^}]*) (\!?)hasPerm\(([^)]+)\)([^}]*)\}/',
-                        '{if\1 \2($smarty.session.perms && $smarty.session.perms->hasFlag(\3))\4}',
+    $perms = 'isset($smarty.session.user|smarty:nodefaults) && $smarty.session.user';
+    return preg_replace(array('/\{(else)?if([^}]*) (\!?)hasPerms?\(([^)]+)\)([^}]*)\}/',
+                              '/\{(else)?if([^}]*) (\!?)t\(([^)]+)\)([^}]*)\}/'),
+                        array('{\1if\2 \3(' . $perms . '->checkPerms(\4))\5}',
+                              '{\1if\2 \3(isset(\4|smarty:nodefaults) && (\4|smarty:nodefaults))\5}'),
                         $source);
 }
 
 // }}}
 // {{{
 
-function form_force_encodings($source, &$smarty)
+function form_force_encodings($source, $smarty)
 {
     return preg_replace('/<form[^\w]/',
                         '\0 accept-charset="utf-8" ',
@@ -404,11 +550,11 @@ function _hide_email($source)
 {
     $source = str_replace("\n", '', $source);
     return '<script type="text/javascript">//<![CDATA[' . "\n" .
-           'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
-           '//]]></script>';
+        'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
+        '//]]></script>';
 }
 
-function hide_emails($source, &$smarty)
+function hide_emails($source, $smarty)
 {
     if (!strpos($source, '@')) {
         return $source;