Fix baseurl for images and javascript
[banana.git] / banana / page.inc.php
index 92d4509..7d313ef 100644 (file)
@@ -7,7 +7,9 @@
 * Copyright: See COPYING files that comes with this distribution
 ********************************************************************************/
 
-require_once 'smarty/libs/Smarty.class.php';
+if (!include_once('Smarty.class.php')) {
+    require_once 'smarty/libs/Smarty.class.php';
+}
 
 class BananaPage extends Smarty
 {
@@ -15,15 +17,18 @@ class BananaPage extends Smarty
     private $page  = null;
 
     private $pages   = array();
+    private $killed  = array();
     private $actions = array();
 
+    public $css = '';
+
     public function __construct()
     {
         $this->Smarty();
 
         $this->compile_check = Banana::$debug_smarty;
         $this->template_dir  = dirname(__FILE__) . '/templates/';
-        $this->compile_dir   = dirname(dirname(__FILE__)) . '/spool/templates_c/';
+        $this->compile_dir   = Banana::$spool_root . '/templates_c/';
         $this->register_prefilter('banana_trimwhitespace');
     
     }
@@ -53,6 +58,7 @@ class BananaPage extends Smarty
     public function setPage($page)
     {
         $this->page = $page;
+        return true;
     }
 
     /** Register an action to show on banana page
@@ -77,10 +83,27 @@ class BananaPage extends Smarty
         $this->pages[$name] = array('text' => $text, 'template' => $template);
         return true;
     }
-    
-    /** Generate XHTML code
+
+    /** Remove a page
+     * @param page STRING page name to kill
      */
-    public function run()
+    public function killPage($page)
+    {
+        $this->killed[] = $page;
+    }
+
+    /** Add Inline CSS to put in the page headers
+     * @param css CSS code
+     */
+    public function addCssInline($css)
+    {
+        $this->css .= $css;
+    }
+
+    /** Preparte the page generation
+     * @return template to use
+     */
+    protected function prepare()
     {
         $this->registerPage('subscribe', _b_('Abonnements'), null);
         $this->registerPage('forums', _b_('Les forums'), null);
@@ -91,17 +114,59 @@ class BananaPage extends Smarty
                 if ($this->page == 'cancel') {
                     $this->registerPage('cancel', _b_('Annulation'), null);
                 } elseif ($this->page == 'new') {
-                    $this->registerPage('new', _b_('Répondre'), null);
+                    $this->registerPage('new', _b_('Répondre'), null);
                 }
             } elseif ($this->page == 'new') {
                 $this->registerPage('new', _b_('Nouveau'), null);
             }
         }
+        foreach ($this->killed as $page) {
+            unset($this->pages[$page]);
+        }
         foreach ($this->actions as $key=>&$action) {
             if (!is_null($action['pages']) && !in_array($this->page, $action['pages'])) {
                 unset($this->actions[$key]);
             }
         }
+
+        return 'banana-base.tpl';
+    }
+    
+    /** Generate XHTML code
+     */
+    public function run()
+    {
+        $tpl = $this->prepare();
+        if (!isset($this->pages[$this->page])) {
+            $this->trig(_b_('La page demandée n\'existe pas'));
+            $this->actions = array();
+            $this->page = null;
+        }
+
+        return $this->_run($tpl);
+    }
+
+    /** Generate feed XML code
+     */
+    public function feed()
+    {
+        @list($lg) = explode('_', Banana::$profile['locale']);
+        $tpl = 'banana-feed-' . Banana::$feed_format . '.tpl';
+        $this->assign('copyright', Banana::$feed_copyright);
+        $this->assign('generator', Banana::$feed_generator);
+        $this->assign('email',     Banana::$feed_email);
+        $this->assign('title_prefix', Banana::$feed_namePrefix);
+        $this->assign('language', $lg);
+        $this->register_function('rss_date', 'rss_date');
+        header('Content-Type: application/rss+xml; charset=utf-8');
+        echo $this->_run($tpl, false);
+        exit;
+    }
+
+    /** Code generation
+     */
+    private function _run($tpl, $ent = true)
+    {
         $this->assign('group',     Banana::$group);
         $this->assign('artid',     Banana::$artid);
         $this->assign('part',      Banana::$part);
@@ -110,21 +175,33 @@ class BananaPage extends Smarty
         $this->assign('profile',   Banana::$profile);
         $this->assign('spool',     Banana::$spool);
         $this->assign('protocole', Banana::$protocole);
+        $this->assign('showboxlist', Banana::$spool_boxlist);
+        $this->assign('showthread',  Banana::$msgshow_withthread);
+        $this->assign('withtabs'   , Banana::$withtabs);
+        $this->assign('feed_format', Banana::$feed_format);
+        $this->assign('feed_active', Banana::$feed_active);
+
+        $this->register_function('url',     array($this, 'makeUrl'));
+        $this->register_function('link',    array($this, 'makeLink'));
+        $this->register_function('imglink', array($this, 'makeImgLink'));
+        $this->register_function('img',     array($this, 'makeImg'));
+        $this->register_modifier('b',       '_b_');
 
         $this->assign('errors',    $this->error);
         $this->assign('page',      $this->page);
         $this->assign('pages',     $this->pages);
         $this->assign('actions',   $this->actions);
+        $this->register_modifier('banana_utf8entities', 'banana_utf8entities');
+        $this->register_modifier('banana_entities', 'banana_entities');
+
+        if ($ent) {
+            $this->default_modifiers = Array('@banana_entities');
+        }
 
-        $this->register_function('url',     array($this, 'makeUrl'));
-        $this->register_function('link',    array($this, 'makeLink'));
-        $this->register_function('imglink', array($this, 'makeImgLink'));
-        $this->register_function('img',     array($this, 'makeImg'));
         if (!Banana::$debug_smarty) {
             $error_level = error_reporting(0);
         }
-        $text = $this->fetch('banana-base.tpl');
-        $text = banana_utf8entities($text);
+        $text = $this->fetch($tpl);
         if (!Banana::$debug_smarty) {
             error_reporting($error_level);
         }
@@ -153,7 +230,7 @@ class BananaPage extends Smarty
             return $res;
         }   
         $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
-        $host  = $_SERVER['HTTP_HOST'];
+        $host  = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
         $file  = $_SERVER['PHP_SELF'];
     
         if (count($params) != 0) {
@@ -199,7 +276,7 @@ class BananaPage extends Smarty
             $popup .= ' (raccourci : ' . $accesskey . ')';
         }
         if (!is_null($popup)) {
-            $popup = ' title="' . banana_htmlentities($popup) . '"';
+            $popup = ' title="' . banana_entities($popup) . '"';
         }
         if (!is_null($class)) {
             $class = ' class="' . $class . '"';
@@ -207,7 +284,7 @@ class BananaPage extends Smarty
         if (!is_null($accesskey)) {
             $accesskey = ' accesskey="' . $accesskey . '"';
         }
-        return '<a href="' . banana_htmlentities($link) . '"'
+        return '<a href="' . banana_entities($link) . '"'
               . $popup . $class . $accesskey
               . '>' . $text . '</a>';
     }
@@ -245,13 +322,35 @@ class BananaPage extends Smarty
         }
 
         $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
-        $host  = $_SERVER['HTTP_HOST'];
+        $host  = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
         $file  = dirname($_SERVER['PHP_SELF']) . '/img/' . $img;
         $url   = $proto . $host . $file;
 
         return '<img src="' . $url . '"' . $height . $width . ' alt="' . _b_($alt) . '" />';
     }
     
+    /** Build a link to one of the banana built-in javascript
+     * @param src STRING javascript name
+     * @return Javascript tag
+     */
+    public function makeJs($src)
+    {
+        if (!Banana::$msgshow_javascript) {
+            return '';
+        }
+        if (function_exists('hook_makeJs')
+                && $res = hook_makeJs($src)) {
+            return $res;
+        }
+
+        $proto = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
+        $host  = Banana::$baseurl ? Banana::$baseurl : $_SERVER['SERVER_NAME'];
+        $file  = dirname($_SERVER['PHP_SELF']) . '/javascript/' . $src . '.js';
+        $url   = $proto . $host . $file;
+
+        return '<script type="text/javascript" src="' . $url . '"/></script>';
+    }
+    
     /** Build a link with an image as text
      * @param params ARRAY image and location data
      * @param smarty OBJECT Smarty instance associated (null if none)
@@ -264,15 +363,18 @@ class BananaPage extends Smarty
      */
     public function makeImgLink(array $params, &$smarty = null)
     {
-        $params['alt'] = _b_($params['alt']);
         if (!isset($params['popup'])) {
-            $params['popup'] = $params['alt'];
+            $params['popup'] = @$params['alt'];
         }    
         $img = $this->makeImg($params, $smarty);
         if (isset($params['text'])) {
             $img .= ' ' . $params['text'];
         }
         $params['text'] = $img;
+        unset($params['alt']);
+        unset($params['img']);
+        unset($params['width']);
+        unset($params['height']);
         return $this->makeLink($params, $smarty);
     }
 
@@ -308,7 +410,14 @@ function banana_trimwhitespace($source, &$smarty)
 }
 
 // }}}
+// {{{ function rss_date
 
+function rss_date($t)
+{
+    return date('r', $t);
+}
+
+// }}}
 
-// vim:set et sw=4 sts=4 ts=4:
+// vim:set et sw=4 sts=4 ts=4 enc=utf-8:
 ?>