Fix baseurl for images and javascript
[banana.git] / banana / page.inc.php
index acd2e65..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
 {
@@ -189,14 +191,17 @@ class BananaPage extends Smarty
         $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');
+        }
 
         if (!Banana::$debug_smarty) {
             $error_level = error_reporting(0);
         }
         $text = $this->fetch($tpl);
-        if ($ent) {
-            $text = banana_utf8entities($text);
-        }
         if (!Banana::$debug_smarty) {
             error_reporting($error_level);
         }
@@ -225,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) {
@@ -271,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 . '"';
@@ -279,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>';
     }
@@ -317,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)