Better email-hidder
[platal.git] / classes / platalpage.php
index b0f5d6a..fad9a66 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   *
@@ -55,6 +55,7 @@ class PlatalPage extends Smarty
 
         $this->register_prefilter('at_to_globals');
         $this->register_prefilter('trimwhitespace');
+        $this->register_prefilter('form_force_encodings');
         $this->addJsLink('xorg.js');
     }
 
@@ -63,9 +64,9 @@ class PlatalPage extends Smarty
 
     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('xorg_tpl', $tpl);
     }
 
     // }}}
@@ -81,6 +82,14 @@ class PlatalPage extends Smarty
         $this->assign('xorg_failure', $this->_failure);
         $this->assign('globals', $globals);
 
+        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);
@@ -89,10 +98,14 @@ class PlatalPage extends Smarty
 
           case SIMPLE:
             $this->assign('simple', true);
+
           case SKINNED:
-           $this->register_modifier('escape_html', 'escape_html');
-           $this->default_modifiers = Array('@escape_html');
+           $this->register_modifier('escape_html', 'escape_html');
+               $this->default_modifiers = Array('@escape_html');
         }
+        $this->register_outputfilter('hide_emails');
+        $this->addJsLink('wiki.js');
+        header("Accept-Charset: iso-8859-15, latin9, us-ascii, ascii");
 
         if (!$globals->debug) {
             error_reporting(0);
@@ -101,7 +114,7 @@ class PlatalPage extends Smarty
         }
 
         if ($globals->debug & 1) {
-            $this->assign('db_trace', XDB::trace_format($this, 'database-debug.tpl'));
+            $this->assign('db_trace', XDB::trace_format($this, 'skin/common.database-debug.tpl'));
         }
 
         $this->assign('validate', true);
@@ -179,6 +192,16 @@ class PlatalPage extends Smarty
     }
 
     // }}}
+    // {{{ function addCssInline
+
+    function addCssInline($css)
+    {
+        if (!empty($css)) {
+            $this->append('xorg_inline_css', $css);
+        }    
+    }
+
+    // }}}
     // {{{ function setRssLink
 
     function setRssLink($title, $path)
@@ -259,6 +282,60 @@ function trimwhitespace($source, &$smarty)
 }
 
 // }}}
+// {{{
+
+function form_force_encodings($source, &$smarty)
+{
+    return preg_replace('/<form[^\w]/',
+                        '\0 accept-charset="iso-8859-15 latin9 us-ascii ascii" ',
+                        $source);
+}
+
+// }}}
+// {{{ function hide_emails
+
+function _hide_email($source)
+{
+    $source = str_replace("\n", '', $source);
+    return '<script type="text/javascript">//<![CDATA[' . "\n" .
+           'Nix.decode("' . addslashes(str_rot13($source)) . '");' . "\n" .
+           '//]]></script>'; 
+}
+
+function hide_emails($source, &$smarty)
+{
+    fix_encoding($source);
+
+    //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);
+    }
+
+    //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);
+
+    //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);
+
+    //catch !
+    $source = preg_replace('!([-a-z0-9_+.]+@[-a-z0-9_.]+)!ie', '_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);
+    }
+
+    return $source;
+}
+
+// }}}
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
 ?>