]> git.mjollnir.org Git - s9y.git/commitdiff
Fix wrong usage of serendipity_smarty_init() when being used multiple times
authorgarvinhicking <garvinhicking>
Fri, 11 Aug 2006 12:24:32 +0000 (12:24 +0000)
committergarvinhicking <garvinhicking>
Fri, 11 Aug 2006 12:24:32 +0000 (12:24 +0000)
docs/NEWS
include/functions_smarty.inc.php

index 8089099d75e7d4848c31130ee49d5db773037739..4a78bb4adccb972589a5025460b5aa45cbe2d73d 100644 (file)
--- a/docs/NEWS
+++ b/docs/NEWS
@@ -3,6 +3,10 @@
 Version 1.1-alpha7()
 ------------------------------------------------------------------------
 
+    * Fix multiple loading of $serendipity['smarty'] theme options when
+      calling serendipity_smarty_init() more than once. Many thanks to
+      Sven Ediger for reporting this. (garvinhicking)
+
     * Properly sort media directories (garvinhicking)
 
     * Better use of "return by references" in some vital areas.
index a327f3c11bd5cd9ca1c6539e48740036d27fefbd..433453d0a6341a40e1472068e25ed27be4af88d7 100644 (file)
@@ -2,6 +2,11 @@
 # Copyright (c) 2003-2005, Jannis Hermanns (on behalf the Serendipity Developer Team)
 # All rights reserved.  See LICENSE file for licensing details
 
+if (defined('S9Y_FRAMEWORK_SMARTY')) {
+    return;
+}
+@define('S9Y_FRAMEWORK_SMARTY', true);
+
 /**
  * Fetch a list of trackbacks for an entry
  *
@@ -11,7 +16,7 @@
  * @param   boolean If true, also non-approved trackbacks will be shown
  * @return
  */
-function serendipity_fetchTrackbacks($id, $limit = null, $showAll = false) {
+function &serendipity_fetchTrackbacks($id, $limit = null, $showAll = false) {
     global $serendipity;
 
     if (!$showAll) {
@@ -41,10 +46,10 @@ function serendipity_fetchTrackbacks($id, $limit = null, $showAll = false) {
  * @param   array       The superarray of trackbacks to display
  * @return
  */
-function serendipity_printTrackbacks($trackbacks) {
+function &serendipity_printTrackbacks(&$trackbacks) {
     global $serendipity;
 
-    $serendipity['smarty']->assign('trackbacks', $trackbacks);
+    $serendipity['smarty']->assign_by_ref('trackbacks', $trackbacks);
 
     return serendipity_smarty_fetch('TRACKBACKS', 'trackbacks.tpl');
 }
@@ -61,8 +66,8 @@ function serendipity_printTrackbacks($trackbacks) {
 function &serendipity_smarty_fetch($block, $file, $echo = false) {
     global $serendipity;
 
-    $output = $serendipity['smarty']->fetch('file:'. serendipity_getTemplateFile($file, 'serendipityPath'), null, null, ($echo === true && $serendipity['smarty_raw_mode']));
-    $serendipity['smarty']->assign($block, $output);
+    $output =& $serendipity['smarty']->fetch('file:'. serendipity_getTemplateFile($file, 'serendipityPath'), null, null, ($echo === true && $serendipity['smarty_raw_mode']));
+    $serendipity['smarty']->assign_by_ref($block, $output);
 
     return $output;
 }
@@ -79,6 +84,31 @@ function serendipity_emptyPrefix($string, $prefix = ': ') {
     return (!empty($string) ? $prefix . htmlspecialchars($string) : '');
 }
 
+/**
+ * Smarty Modifier: Return a remembered variable
+ *
+ * @access public
+ * @param   string  The variable name
+ * @param   string  The wanted value
+ * @param   boolean Force default?
+ * @param   string  Returned attribute
+ * @return  string  The return string
+ */
+function serendipity_ifRemember($name, $value, $isDefault = false, $att = 'checked') {
+    global $serendipity;
+
+    if (!is_array($serendipity['COOKIE']) && !$isDefault) {
+        return false;
+    }
+
+    if ((!is_array($serendipity['COOKIE']) && $isDefault) ||
+        (!isset($serendipity['COOKIE']['serendipity_' . $name]) && $isDefault) ||
+        (isset($serendipity['COOKIE']['serendipity_' . $name]) && $serendipity['COOKIE']['serendipity_' . $name] == $value)) {
+
+        return " $att=\"$att\" ";
+    }
+}
+
 /**
  * Smarty Function: Fetch and print a single or multiple entries
  *
@@ -433,6 +463,33 @@ function serendipity_smarty_getFile($params, &$smarty) {
     return serendipity_getTemplateFile($params['file']);
 }
 
+
+/**
+ * Smarty Function: Picks a specified key from an array and returns it
+ *
+ * @access public
+ * @param   array       Smarty parameter input array:
+ *                          array: The array you want to check
+ *                          key: The keyname
+ *                          default: What (string) to return when array does not contain the key.
+ * @param   object       Smarty object
+ * @return  string      The requested filename with full path
+ */
+function serendipity_smarty_pickKey($params, &$smarty) {
+    if ( !isset($params['array']) ) {
+        $smarty->trigger_error(__FUNCTION__ .": missing 'array' parameter");
+        return;
+    }
+
+    if ( !isset($params['key']) ) {
+        $smarty->trigger_error(__FUNCTION__ .": missing 'key' parameter");
+        return;
+    }
+
+    return serendipity_pickKey($params['array'], $params['key'], $params['default']);
+}
+
+
 /**
  * Smarty Function: Get a permalink for an entry
  *
@@ -463,9 +520,14 @@ function serendipity_smarty_rss_getguid($params, &$smarty) {
  * @param   int     The timestamp to format (unix seconds)
  * @param   string  The strftime() format options on how to format this string
  * @param   boolean Shall timezone conversions be applied?
+ * @param   boolean Try to detect a valid timestamp?
  * @return
  */
-function serendipity_smarty_formatTime($timestamp, $format, $useOffset = true) {
+function serendipity_smarty_formatTime($timestamp, $format, $useOffset = true, $detectTimestamp = false) {
+    if ($detectTimestamp !== false && stristr($detectTimestamp, 'date') === false) {
+        return $timestamp;
+    }
+
     if (defined($format)) {
         return serendipity_formatTime(constant($format), $timestamp, $useOffset);
     } else {
@@ -495,7 +557,11 @@ function &serendipity_smarty_printComments($params, &$smarty) {
         $params['mode'] = VIEWMODE_THREADED;
     }
 
-    $comments = serendipity_fetchComments($params['entry']);
+    if (isset($params['order']) && $params['order'] != 'DESC') {
+        $params['order'] = 'ASC';
+    }
+
+    $comments = serendipity_fetchComments($params['entry'], (int)$params['limit'], $params['order']);
 
     if (!empty($serendipity['POST']['preview'])) {
         $comments[] =
@@ -522,13 +588,14 @@ function &serendipity_smarty_printComments($params, &$smarty) {
  * @param   object  Smarty object
  * @return
  */
-function serendipity_smarty_printTrackbacks($params, &$smarty) {
+function &serendipity_smarty_printTrackbacks($params, &$smarty) {
     if ( !isset($params['entry']) ) {
         $smarty->trigger_error(__FUNCTION__ .": missing 'entry' parameter");
         return;
     }
 
-    return serendipity_printTrackbacks(serendipity_fetchTrackbacks($params['entry']));
+    $tb =& serendipity_fetchTrackbacks($params['entry']);
+    return serendipity_printTrackbacks($tb);
 }
 
 /**
@@ -551,125 +618,144 @@ function &serendipity_replaceSmartyVars(&$tpl_source, &$smarty) {
  * @return null
  */
 function serendipity_smarty_init($vars = array()) {
-    global $serendipity;
+    global $serendipity, $template_config;
 
     if (!isset($serendipity['smarty'])) {
-        @define('SMARTY_DIR', S9Y_PEAR_PATH . 'Smarty/libs/');
-        require_once SMARTY_DIR . 'Smarty.class.php';
-        $serendipity['smarty'] = new Smarty;
-        if ($serendipity['production'] === 'debug') {
-            $serendipity['smarty']->force_compile   = true;
-            $serendipity['smarty']->debugging       = true;
+        $template_dir = $serendipity['serendipityPath'] . $serendipity['templatePath'] . $serendipity['template'];
+        if (!defined('IN_serendipity_admin') && file_exists($template_dir . '/template.inc.php')) {
+            // If this file exists, a custom template engine will be loaded.
+            // Beware: Smarty is used in the Admin backend, despite of this.
+            include $template_dir . '/template.inc.php';
+        } else {
+            // Default Smarty Engine will be used
+
+            @define('SMARTY_DIR', S9Y_PEAR_PATH . 'Smarty/libs/');
+            if (!class_exists('Smarty')) {
+                require SMARTY_DIR . 'Smarty.class.php';
+            }
+            $serendipity['smarty'] = new Smarty;
+            if ($serendipity['production'] === 'debug') {
+                $serendipity['smarty']->force_compile   = true;
+                $serendipity['smarty']->debugging       = true;
+            }
+
+            $serendipity['smarty']->template_dir  = array(
+               $template_dir,
+               $serendipity['serendipityPath'] . $serendipity['templatePath'] . 'default'
+            );
+            $serendipity['smarty']->compile_dir   = $serendipity['serendipityPath'] . PATH_SMARTY_COMPILE;
+
+            if (!is_dir($serendipity['smarty']->compile_dir) || !is_writable($serendipity['smarty']->compile_dir)) {
+                serendipity_die(sprintf(DIRECTORY_WRITE_ERROR, $serendipity['smarty']->compile_dir));
+            }
+
+            $serendipity['smarty']->config_dir    = $template_dir;
+            $serendipity['smarty']->secure_dir    = array($serendipity['serendipityPath'] . $serendipity['templatePath']);
+            $serendipity['smarty']->security_settings['MODIFIER_FUNCS']  = array('sprintf', 'sizeof', 'count', 'rand', 'print_r', 'str_repeat');
+            $serendipity['smarty']->security_settings['ALLOW_CONSTANTS'] = true;
+            $serendipity['smarty']->security      = true;
+            $serendipity['smarty']->use_sub_dirs  = false;
+            $serendipity['smarty']->compile_check = true;
+            $serendipity['smarty']->compile_id    = &$serendipity['template'];
+
+            $serendipity['smarty']->register_modifier('makeFilename', 'serendipity_makeFilename');
+            $serendipity['smarty']->register_modifier('xhtml_target', 'serendipity_xhtml_target');
+            $serendipity['smarty']->register_modifier('emptyPrefix', 'serendipity_emptyPrefix');
+            $serendipity['smarty']->register_modifier('formatTime', 'serendipity_smarty_formatTime');
+            $serendipity['smarty']->register_modifier('serendipity_utf8_encode', 'serendipity_utf8_encode');
+            $serendipity['smarty']->register_modifier('ifRemember', 'serendipity_ifRemember');
+
+            $serendipity['smarty']->register_function('serendipity_printSidebar', 'serendipity_smarty_printSidebar');
+            $serendipity['smarty']->register_function('serendipity_hookPlugin', 'serendipity_smarty_hookPlugin');
+            $serendipity['smarty']->register_function('serendipity_showPlugin', 'serendipity_smarty_showPlugin');
+            $serendipity['smarty']->register_function('serendipity_getFile', 'serendipity_smarty_getFile');
+            $serendipity['smarty']->register_function('serendipity_printComments', 'serendipity_smarty_printComments');
+            $serendipity['smarty']->register_function('serendipity_printTrackbacks', 'serendipity_smarty_printTrackbacks');
+            $serendipity['smarty']->register_function('serendipity_rss_getguid', 'serendipity_smarty_rss_getguid');
+            $serendipity['smarty']->register_function('serendipity_fetchPrintEntries', 'serendipity_smarty_fetchPrintEntries');
+            $serendipity['smarty']->register_function('serendipity_getTotalCount', 'serendipity_smarty_getTotalCount');
+            $serendipity['smarty']->register_function('pickKey', 'serendipity_smarty_pickKey');
+
+            $serendipity['smarty']->register_prefilter('serendipity_replaceSmartyVars');
         }
-        $serendipity['smarty']->template_dir  = array(
-               $serendipity['serendipityPath'] . $serendipity['templatePath'] . $serendipity['template'],
-               $serendipity['serendipityPath'] . $serendipity['templatePath'] . 'default'
-        );
-        $serendipity['smarty']->compile_dir   = $serendipity['serendipityPath'] . PATH_SMARTY_COMPILE;
 
-        if (!is_dir($serendipity['smarty']->compile_dir) || !is_writable($serendipity['smarty']->compile_dir)) {
-            serendipity_die(sprintf(DIRECTORY_WRITE_ERROR, $serendipity['smarty']->compile_dir));
+        if (!isset($serendipity['smarty_raw_mode'])) {
+            if (file_exists($serendipity['smarty']->config_dir . '/layout.php') && $serendipity['template'] != 'default') {
+                $serendipity['smarty_raw_mode'] = true;
+            } else {
+                $serendipity['smarty_raw_mode'] = false;
+            }
         }
-
-        $serendipity['smarty']->config_dir    = &$serendipity['smarty']->template_dir[0];
-        $serendipity['smarty']->secure_dir    = array($serendipity['serendipityPath'] . $serendipity['templatePath']);
-        $serendipity['smarty']->security_settings['MODIFIER_FUNCS']  = array('sprintf', 'sizeof', 'count', 'rand');
-        $serendipity['smarty']->security_settings['ALLOW_CONSTANTS'] = true;
-        $serendipity['smarty']->security      = true;
-        $serendipity['smarty']->use_sub_dirs  = false;
-        $serendipity['smarty']->compile_check = true;
-        $serendipity['smarty']->compile_id    = &$serendipity['template'];
-
-        $serendipity['smarty']->register_modifier('makeFilename', 'serendipity_makeFilename');
-        $serendipity['smarty']->register_modifier('xhtml_target', 'serendipity_xhtml_target');
-        $serendipity['smarty']->register_modifier('emptyPrefix', 'serendipity_emptyPrefix');
-        $serendipity['smarty']->register_modifier('formatTime', 'serendipity_smarty_formatTime');
-        $serendipity['smarty']->register_modifier('serendipity_utf8_encode', 'serendipity_utf8_encode');
-
-        $serendipity['smarty']->register_function('serendipity_printSidebar', 'serendipity_smarty_printSidebar');
-        $serendipity['smarty']->register_function('serendipity_hookPlugin', 'serendipity_smarty_hookPlugin');
-        $serendipity['smarty']->register_function('serendipity_showPlugin', 'serendipity_smarty_showPlugin');
-        $serendipity['smarty']->register_function('serendipity_getFile', 'serendipity_smarty_getFile');
-        $serendipity['smarty']->register_function('serendipity_printComments', 'serendipity_smarty_printComments');
-        $serendipity['smarty']->register_function('serendipity_printTrackbacks', 'serendipity_smarty_printTrackbacks');
-        $serendipity['smarty']->register_function('serendipity_rss_getguid', 'serendipity_smarty_rss_getguid');
-        $serendipity['smarty']->register_function('serendipity_fetchPrintEntries', 'serendipity_smarty_fetchPrintEntries');
-        $serendipity['smarty']->register_function('serendipity_getTotalCount', 'serendipity_smarty_getTotalCount');
-
-        $serendipity['smarty']->register_prefilter('serendipity_replaceSmartyVars');
-    }
-
-    if (!isset($serendipity['smarty_raw_mode'])) {
-        if (file_exists($serendipity['smarty']->config_dir . '/layout.php') && $serendipity['template'] != 'default') {
-            $serendipity['smarty_raw_mode'] = true;
-        } else {
-            $serendipity['smarty_raw_mode'] = false;
+    
+        if (!isset($serendipity['smarty_file'])) {
+            $serendipity['smarty_file'] = 'index.tpl';
         }
-    }
-
-    if (!isset($serendipity['smarty_file'])) {
-        $serendipity['smarty_file'] = 'index.tpl';
-    }
-
-    $category      = false;
-    $category_info = array();
-    if (isset($serendipity['GET']['category'])) {
-        $category = (int)$serendipity['GET']['category'];
-        if (isset($GLOBALS['cInfo'])) {
-            $category_info = $GLOBALS['cInfo'];
-        } else {
-            $category_info = serendipity_fetchCategoryInfo($category);
+    
+        $category      = false;
+        $category_info = array();
+        if (isset($serendipity['GET']['category'])) {
+            $category = (int)$serendipity['GET']['category'];
+            if (isset($GLOBALS['cInfo'])) {
+                $category_info = $GLOBALS['cInfo'];
+            } else {
+                $category_info = serendipity_fetchCategoryInfo($category);
+            }
+        }
+    
+        if (!isset($serendipity['smarty_vars']['head_link_stylesheet'])) {
+            $serendipity['smarty_vars']['head_link_stylesheet'] = serendipity_rewriteURL('serendipity.css');
+        }
+    
+        $serendipity['smarty']->assign(
+            array(
+                'head_charset'              => LANG_CHARSET,
+                'head_version'              => $serendipity['version'],
+                'head_title'                => $serendipity['head_title'],
+                'head_subtitle'             => $serendipity['head_subtitle'],
+                'head_link_stylesheet'      => $serendipity['smarty_vars']['head_link_stylesheet'],
+    
+                'is_xhtml'                  => true,
+                'use_popups'                => $serendipity['enablePopup'],
+                'is_embedded'               => (!$serendipity['embed'] || $serendipity['embed'] === 'false' || $serendipity['embed'] === false) ? false : true,
+                'is_raw_mode'               => $serendipity['smarty_raw_mode'],
+                'is_logged_in'              => serendipity_userLoggedIn(),
+    
+                'entry_id'                  => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])) ? $serendipity['GET']['id'] : false,
+                'is_single_entry'           => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])),
+    
+                'blogTitle'                 => htmlspecialchars($serendipity['blogTitle']),
+                'blogSubTitle'              => (!empty($serendipity['blogSubTitle']) ? htmlspecialchars($serendipity['blogSubTitle']) : ''),
+                'blogDescription'           => htmlspecialchars($serendipity['blogDescription']),
+    
+                'serendipityHTTPPath'       => $serendipity['serendipityHTTPPath'],
+                'serendipityBaseURL'        => $serendipity['baseURL'],
+                'serendipityRewritePrefix'  => $serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '',
+                'serendipityIndexFile'      => $serendipity['indexFile'],
+                'serendipityVersion'        => $serendipity['version'],
+    
+                'lang'                      => $serendipity['lang'],
+                'category'                  => $category,
+                'category_info'             => $category_info,
+                'template'                  => $serendipity['template'],
+    
+                'dateRange'                 => (!empty($serendipity['range']) ? $serendipity['range'] : array())
+            )
+        );
+    
+        if (count($vars) > 0) {
+            $serendipity['smarty']->assign($vars);
+        }
+    
+        // For advanced usage, we allow template authors to create a file 'config.inc.php' where they can
+        // setup custom smarty variables, modifiers etc. to use in their templates.
+        @include_once $serendipity['smarty']->config_dir . '/config.inc.php';
+    
+        if (is_array($template_config)) {
+            $template_vars =& serendipity_loadThemeOptions($template_config);
+            $serendipity['smarty']->assign_by_ref('template_option', $template_vars);
         }
     }
 
-    if (!isset($serendipity['smarty_vars']['head_link_stylesheet'])) {
-        $serendipity['smarty_vars']['head_link_stylesheet'] = serendipity_rewriteURL('serendipity.css');
-    }
-
-    $serendipity['smarty']->assign(
-        array(
-            'head_charset'              => LANG_CHARSET,
-            'head_version'              => $serendipity['version'],
-            'head_title'                => $serendipity['head_title'],
-            'head_subtitle'             => $serendipity['head_subtitle'],
-            'head_link_stylesheet'      => $serendipity['smarty_vars']['head_link_stylesheet'],
-
-            'is_xhtml'                  => $serendipity['XHTML11'],
-            'use_popups'                => $serendipity['enablePopup'],
-            'is_embedded'               => (!$serendipity['embed'] || $serendipity['embed'] === 'false' || $serendipity['embed'] === false) ? false : true,
-            'is_raw_mode'               => $serendipity['smarty_raw_mode'],
-            'is_logged_in'              => serendipity_userLoggedIn(),
-
-            'entry_id'                  => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])) ? $serendipity['GET']['id'] : false,
-            'is_single_entry'           => (isset($serendipity['GET']['id']) && is_numeric($serendipity['GET']['id'])),
-
-            'blogTitle'                 => htmlspecialchars($serendipity['blogTitle']),
-            'blogSubTitle'              => (!empty($serendipity['blogSubTitle']) ? htmlspecialchars($serendipity['blogSubTitle']) : ''),
-            'blogDescription'           => htmlspecialchars($serendipity['blogDescription']),
-
-            'serendipityHTTPPath'       => $serendipity['serendipityHTTPPath'],
-            'serendipityBaseURL'        => $serendipity['baseURL'],
-            'serendipityRewritePrefix'  => $serendipity['rewrite'] == 'none' ? $serendipity['indexFile'] . '?/' : '',
-            'serendipityIndexFile'      => $serendipity['indexFile'],
-            'serendipityVersion'        => $serendipity['version'],
-
-            'lang'                      => $serendipity['lang'],
-            'category'                  => $category,
-            'category_info'             => $category_info,
-            'template'                  => $serendipity['template'],
-
-            'dateRange'                 => (!empty($serendipity['range']) ? $serendipity['range'] : array())
-        )
-    );
-
-    if (count($vars) > 0) {
-        $serendipity['smarty']->assign($vars);
-    }
-
-    // For advanced usage, we allow template authors to create a file 'config.inc.php' where they can
-    // setup custom smarty variables, modifiers etc. to use in their templates.
-    @include_once $serendipity['smarty']->config_dir . '/config.inc.php';
-
     return true;
 }
 
@@ -714,12 +800,12 @@ global $serendipity;
     chdir($serendipity_directory);
     $raw_data = ob_get_contents();
     ob_end_clean();
-    $serendipity['smarty']->assign('content_message', $raw_data);
+    $serendipity['smarty']->assign_by_ref('content_message', $raw_data);
 
     serendipity_smarty_fetch('CONTENT', 'content.tpl');
+    $serendipity['smarty']->assign('ENTRIES', '');
     if (empty($serendipity['smarty_file'])) {
         $serendipity['smarty_file'] = '404.tpl';
     }
     $serendipity['smarty']->display(serendipity_getTemplateFile($serendipity['smarty_file'], 'serendipityPath'));
 }
-