Initial work on #448: unified user-set interface
[platal.git] / classes / platalpage.php
index 68a001d..e2011dd 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2006 Polytechnique.org                              *
+ *  Copyright (C) 2003-2007 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -23,24 +23,23 @@ require_once 'smarty/libs/Smarty.class.php';
 
 class PlatalPage extends Smarty
 {
-    var $_page_type;
-    var $_tpl;
-    var $_errors;
-    var $_failure;
-
-    // defaults
-    var $caching          = false;
-    var $config_overwrite = false;
-    var $use_sub_dirs     = false;
+    private $_page_type;
+    private $_tpl;
+    private $_errors;
+    private $_failure;
+    private $_jsonVars;
 
     // {{{ function PlatalPage()
 
-    function PlatalPage($tpl, $type = SKINNED)
+    public function __construct($tpl, $type = SKINNED)
     {
-        global $globals;
+        parent::Smarty();
 
-        $this->Smarty();
+        global $globals;
 
+        $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/");
@@ -51,36 +50,60 @@ class PlatalPage extends Smarty
         $this->_page_type = $type;
         $this->_tpl       = $tpl;
         $this->_errors    = array();
+        $this->_jsonVars  = array();
         $this->_failure   = false;
 
         $this->register_prefilter('at_to_globals');
-        $this->register_prefilter('trimwhitespace');
         $this->addJsLink('xorg.js');
     }
 
     // }}}
     // {{{ function changeTpl()
 
-    function changeTpl($tpl, $type = SKINNED)
+    public function changeTpl($tpl, $type = SKINNED)
+    {
+       $this->_tpl       = $tpl;
+           $this->_page_type = $type;
+       $this->assign('xorg_tpl', $tpl);
+    }
+
+    // }}}
+    // {{{ function raw()
+
+    public function raw()
     {
-       $this->_tpl       = $tpl;
-       $this->_page_type = $type;
-       $this->assign('xorg_tpl', $tpl);
+        global $globals;
+        $this->assign('globals', $globals);
+        return $this->fetch($this->_tpl);
     }
 
     // }}}
     // {{{ function _run()
 
-    function _run($skin)
+    protected function _run($skin)
     {
         global $globals, $TIME_BEGIN;
 
         session_write_close();
 
+        $this->register_prefilter('trimwhitespace');
+        $this->register_prefilter('form_force_encodings');
         $this->assign('xorg_errors', $this->_errors);
         $this->assign('xorg_failure', $this->_failure);
         $this->assign('globals', $globals);
 
+        if (Env::has('json') && count($this->_jsonVars)) {
+            return $this->jsonDisplay();
+        }
+        
+        if (Env::v('display') == 'light') {
+            $this->_page_type = SIMPLE;
+        } elseif (Env::v('display') == 'raw') {
+            $this->_page_type = NO_SKIN;
+        } elseif (Env::v('display') == 'full') {
+            $this->_page_typ = SKINNED;
+        }
+
         switch ($this->_page_type) {
           case NO_SKIN:
             error_reporting(0);
@@ -96,6 +119,10 @@ class PlatalPage extends Smarty
         }
         $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");
+        }
 
         if (!$globals->debug) {
             error_reporting(0);
@@ -104,17 +131,18 @@ class PlatalPage extends Smarty
         }
 
         if ($globals->debug & 1) {
-            $this->assign('db_trace', XDB::trace_format($this, 'database-debug.tpl'));
+            PlBacktrace::clean();
+            $this->assign_by_ref('backtraces', PlBacktrace::$bt);
         }
 
         $this->assign('validate', true);
-        error_reporting(0);
+        #error_reporting(0);
         $result = $this->fetch($skin);
-        $ttime  = sprintf('Temps total: %.02fs<br />', microtime_float() - $TIME_BEGIN);
+        $ttime  = sprintf('Temps total: %.02fs - Mémoire totale : %dKo<br />', microtime(true) - $TIME_BEGIN
+                                                                                , memory_get_peak_usage(true) / 1024);
         $replc  = "<span class='erreur'>VALIDATION HTML INACTIVE</span><br />";
 
         if ($globals->debug & 2) {
-
             $fd = fopen($this->compile_dir."/valid.html","w");
             fwrite($fd, $result);
             fclose($fd);
@@ -139,7 +167,7 @@ class PlatalPage extends Smarty
     // }}}
     // {{{ function nb_errs()
 
-    function nb_errs()
+    public function nb_errs()
     {
         return count($this->_errors);
     }
@@ -147,7 +175,7 @@ class PlatalPage extends Smarty
     // }}}
     // {{{ function trig()
 
-    function trig($msg)
+    public function trig($msg)
     {
         $this->_errors[] = $msg;
     }
@@ -155,7 +183,7 @@ class PlatalPage extends Smarty
     // }}}
     // {{{ function kill()
 
-    function kill($msg)
+    public function kill($msg)
     {
         global $platal;
 
@@ -168,7 +196,7 @@ class PlatalPage extends Smarty
     // }}}
     // {{{ function addJsLink
 
-    function addJsLink($path)
+    public function addJsLink($path)
     {
         $this->append('xorg_js', $path);
     }
@@ -176,20 +204,66 @@ class PlatalPage extends Smarty
     // }}}
     // {{{ function addCssLink
 
-    function addCssLink($path)
+    public function addCssLink($path)
     {
         $this->append('xorg_css', $path);
     }
 
     // }}}
+    // {{{ function addCssInline
+
+    public function addCssInline($css)
+    {
+        if (!empty($css)) {
+            $this->append('xorg_inline_css', $css);
+        }    
+    }
+
+    // }}}
     // {{{ function setRssLink
 
-    function setRssLink($title, $path)
+    public function setRssLink($title, $path)
     {
         $this->assign('xorg_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");
+        $jsonbegin = Env::v('jsonBegin');
+        $jsonend = Env::v('jsonEnd');
+        if (Env::has('jsonVar')) {
+            $jsonbegin = Env::v('jsonVar').' = ';
+            $jsonend = ';';
+        } elseif (Env::has('jsonFunc')) {
+            $jsonbegin = Env::v('jsonFunc').'(';
+            $jsonend = ');';
+        }
+        echo $jsonbegin, json_encode($this->_jsonVars), $jsonend;
+        exit;
+    }
+    // }}}
+    // {{{ function jsonAssign
+    public function jsonAssign($var, $value)
+    {
+        $this->_jsonVars[$var] = $value;
+    }
+
+    // }}}
+}
+
+function escape_xorgDB(&$item, $key)
+{
+    if (is_a($item, 'XOrgDBIterator')) {
+        $expanded = array();
+        while ($a = $item->next()) {
+            $expanded[] = $a;
+        }
+        $item = $expanded;
+    }
 }
 
 // {{{ function escape_html ()
@@ -205,10 +279,10 @@ class PlatalPage extends Smarty
 function escape_html($string)
 {
     if (is_string($string)) {
-       $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
-       return preg_replace("/&(?![A-Za-z]{0,4}\w{2,3};|#[0-9]{2,4};)/", "&amp;" , strtr($string, $transtbl));
+       $transtbl = Array('<' => '&lt;', '>' => '&gt;', '"' => '&quot;', '\'' => '&#39;');
+           return strtr($string, $transtbl);
     } else {
-       return $string;
+           return $string;
     }
 }
 
@@ -243,65 +317,66 @@ function at_to_globals($tpl_source, &$smarty)
 
 function trimwhitespace($source, &$smarty)
 {
-    $tags = array('script', 'pre', 'textarea');
-
-    foreach ($tags as $tag) {
-        preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
-        $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
-    }
+    $tags = '(script|pre|textarea)';
+    preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
+    $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
 
     // remove all leading spaces, tabs and carriage returns NOT
     // preceeded by a php close tag.
     $source = preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source);
-
-    foreach ($tags as $tag) {
-        $source = preg_replace("!&&&{$tag}&&&!e",  'array_shift(${$tag}[0])', $source);
-    }
+    $source = preg_replace("!&&&tags&&&!e",  'array_shift($tagsmatches[0])', $source);
 
     return $source; 
 }
 
 // }}}
+// {{{
+
+function form_force_encodings($source, &$smarty)
+{
+    return preg_replace('/<form[^\w]/',
+                        '\0 accept-charset="utf-8" ',
+                        $source);
+}
+
+// }}}
 // {{{ function hide_emails
 
 function _hide_email($source)
 {
     $source = str_replace("\n", '', $source);
-    return '<script type="text/javascript">Nix.decode("' . addslashes(str_rot13($source)) . '");</script>'; 
+    return '<script type="text/javascript">//<![CDATA[' . "\n" .
+           'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
+           '//]]></script>'; 
 }
 
 function hide_emails($source, &$smarty)
 {
     //prevent email replacement in <script> and <textarea>
-    $tags = array('script', 'textarea', 'select');
-
-    foreach ($tags as $tag) {
-        preg_match_all("!<{$tag}[^>]+>.*?</{$tag}>!is", $source, ${$tag});
-        $source = preg_replace("!<{$tag}[^>]+>.*?</{$tag}>!is", "&&&{$tag}&&&", $source);
-    }
+    $tags = '(script|textarea|select)';
+    preg_match_all("!<$tags.*?>.*?</(\\1)>!ius", $source, $tagsmatches);
+    $source = preg_replace("!<$tags.*?>.*?</(\\1)>!ius", "&&&tags&&&", $source);
 
     //catch all emails in <a href="mailto:...">
-    preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", $source, $ahref);
-    $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9_.]+@[-a-z0-9_.]+[^\"']*[\"'][^>]*>.*?</a>!is", '&&&ahref&&&', $source);
+    preg_match_all("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", $source, $ahref);
+    $source = preg_replace("!<a[^>]+href=[\"'][^\"']*[-a-z0-9+_.]+@[-a-z0-9_.]+[^\"']*[\"'].*?>.*?</a>!ius", '&&&ahref&&&', $source);
 
     //prevant replacement in tag attributes
-    preg_match_all("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", $source, $misc);
-    $source = preg_replace("!<[^>]+[-a-z0-9_.]+@[-a-z0-9_.]+[^>]+>!is", '&&&misc&&&', $source);
+    preg_match_all("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", $source, $misc);
+    $source = preg_replace("!<[^>]+[-a-z0-9_+.]+@[-a-z0-9_.]+.+?>!ius", '&&&misc&&&', $source);
 
     //catch !
-    $source = preg_replace('!([-a-z0-9_.]+@[-a-z0-9_.]+)!ie', '_hide_email("\1")', $source); 
+    $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!iue', '_hide_email("\1")', $source); 
     $source = preg_replace('!&&&ahref&&&!e', '_hide_email(array_shift($ahref[0]))', $source);
 
     // restore data
     $source = preg_replace('!&&&misc&&&!e', 'array_shift($misc[0])', $source);
-    foreach ($tags as $tag) {
-        $source = preg_replace("!&&&{$tag}&&&!e",  'array_shift(${$tag}[0])', $source);
-    }
+    $source = preg_replace("!&&&tags&&&!e",  'array_shift($tagsmatches[0])', $source);
 
     return $source;
 }
 
 // }}}
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
 ?>