Index: trunk/php/lib/sanitize_lib.php
===================================================================
--- trunk/php/lib/sanitize_lib.php (revision 1098)
+++ trunk/php/lib/sanitize_lib.php (revision 3082)
@@ -38,5 +38,5 @@
                 $inside = preg_replace('!/?>$!', '', $inside);
                 $attrs = '';
-                if (preg_match_all('/\s*(\w+)\s*=(?:([\'"])(.*?)\2|([^\s]+))\s*/', $inside, $matches, PREG_SET_ORDER)) {
+                if (preg_match_all('/\s*(\w+)\s*=(?:([\'"])(.*?)\2|([^\s]+))\s*/s', $inside, $matches, PREG_SET_ORDER)) {
                     foreach ($matches as $match) {
                         $attr = strtolower($match[1]);
@@ -55,5 +55,5 @@
                                 $dec_val = preg_replace('/&#0*58(?:=;|[^0-9])/', ':', $dec_val);
                                 $dec_val = preg_replace('/&#x0*3[Aa](?:=;|[^a-fA-F0-9])/', ':', $dec_val);
-                                if (preg_match('/^(.+?):/', $dec_val, $proto_match)) {
+                                if (preg_match('/^([\s\S]+?):/', $dec_val, $proto_match)) {
                                     $proto = $proto_match[1];
                                     if (preg_match('/[\r\n\t]/', $proto)) {
Index: trunk/php/lib/function.mtvar.php
===================================================================
--- trunk/php/lib/function.mtvar.php (revision 2873)
+++ trunk/php/lib/function.mtvar.php (revision 3082)
@@ -111,4 +111,13 @@
                 }
             }
+            else {
+                if (array_key_exists('to_json', $args) && $args['to_json']) {
+                    if (function_exists('json_encode')) {
+                        $return_val = json_encode($value);
+                    } else {
+                        $return_val = '';
+                    }
+                }
+            }
         }
         elseif (is_array($value)) {
@@ -140,9 +149,10 @@
             }
             else {
-                if (!array_key_exists('to_json', $args) && $args['to_json']) {
-                    $glue = $args['glue'];
-                    if (!isset($glue))
-                        $glue = '';
-                    $return_val = implode($glue, $value);
+                if (array_key_exists('to_json', $args) && $args['to_json']) {
+                    if (function_exists('json_encode')) {
+                        $return_val = json_encode($value);
+                    } else {
+                        $return_val = '';
+                    }
                 }
             }
Index: trunk/php/lib/thumbnail_lib.php
===================================================================
--- trunk/php/lib/thumbnail_lib.php (revision 2691)
+++ trunk/php/lib/thumbnail_lib.php (revision 3082)
@@ -56,5 +56,5 @@
     }
 
-    function _make_dest_name ($w, $h, $format, $dest_type) {
+    function _make_dest_name ($w, $h, $format, $dest_type, $id) {
         $output = $this->src_type;
         if ($dest_type != 'auto') {
@@ -82,9 +82,11 @@
         $patterns[1] = '/%h/';
         $patterns[2] = '/%f/';
-        $patterns[3] = '/%x/';
+        $patterns[3] = '/%i/';
+        $patterns[4] = '/%x/';
         $replacement[0] = $w;
         $replacement[1] = $h;
         $replacement[2] = $basename;
-        $replacement[3] = $ext;
+        $replacement[3] = $id;
+        $replacement[4] = $ext;
 
         return preg_replace($patterns, $replacement, $format);
@@ -92,5 +94,5 @@
 
     # Load or generate a thumbnail.
-    function get_thumbnail (&$dest, &$width, &$height, $scale = 0, $format = '%f-thumb-%wx%h%x', $dest_type = 'auto') {
+    function get_thumbnail (&$dest, &$width, &$height, $id, $scale = 0, $format = '%f-thumb-%wx%h-%i%x', $dest_type = 'auto') {
         if (empty($this->src_file)) return false;
         if (!file_exists($this->src_file)) return false;
@@ -130,5 +132,5 @@
         # Decide a destination file name
         if (empty($dest)) {
-            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type);
+            $dest = $this->_make_dest_name($thumb_w_name, $thumb_h_name, $format, $dest_type, $id);
         }
 
Index: trunk/php/lib/modifier.setvar.php
===================================================================
--- trunk/php/lib/modifier.setvar.php (revision 1174)
+++ trunk/php/lib/modifier.setvar.php (revision 3082)
@@ -9,7 +9,10 @@
     global $mt;
     $ctx =& $mt->context();
-    $vars =& $ctx->__stash['vars'];
+    if (array_key_exists('__inside_set_hashvar', $ctx->__stash)) {
+        $vars =& $ctx->__stash['__inside_set_hashvar'];
+    } else {
+        $vars =& $ctx->__stash['vars'];
+    }
     $vars[$name] = $text;
     return '';
 }
-?>
Index: trunk/php/lib/MTUtil.php
===================================================================
--- trunk/php/lib/MTUtil.php (revision 3034)
+++ trunk/php/lib/MTUtil.php (revision 3082)
@@ -1301,5 +1301,5 @@
     $dest;
     $thumb_name = $static_file_path.DIRECTORY_SEPARATOR.$image_path.DIRECTORY_SEPARATOR.$format;
-    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name, 'png')) {
+    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $asset['asset_id'], $scale, $thumb_name, 'png')) {
         return '';
     }
@@ -1314,5 +1314,5 @@
 
 # for compatibility...
-function make_thumbnail_file($src, $dest, $width, $height, $scale = 0, $dest_type = 'auto') {
+function make_thumbnail_file($src, $dest, $width, $height, $scale = 0, $dest_type = 'auto', $id = 0) {
     require_once('thumbnail_lib.php');
     $thumb = new Thumbnail($src);
@@ -1320,10 +1320,10 @@
     $thumb_w = $width;
     $thumb_h = $height;
-    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, null, $dest_type);
+    $thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $id, $scale, null, $dest_type);
 
     return array($thumb_w, $thumb_h);
 }
 
-function get_thumbnail_file($asset, $blog, $width = 0, $height = 0, $scale = 0, $format = '%f-thumb-%wx%h%x') {
+function get_thumbnail_file($asset, $blog, $width = 0, $height = 0, $scale = 0, $format = '%f-thumb-%wx%h-%i%x') {
     # Get parameter
     $site_path = $blog['blog_site_path'];
@@ -1338,7 +1338,13 @@
     $ts = preg_replace('![^0-9]!', '', $asset['asset_created_on']);
     $date_stamp = format_ts('%Y/%m', $ts, $blog);
-    $cache_dir = $site_path . DIRECTORY_SEPARATOR . $cache_path . DIRECTORY_SEPARATOR . $date_stamp . DIRECTORY_SEPARATOR;
+    $base_path = $site_path;
+    if (preg_match('/^%a/', $asset['asset_file_path']) && !empty($blog['blog_archive_path'])) {
+        $base_path = $blog['blog_archive_path'];
+        $base_path = preg_replace('/\/$/', '', $base_path);
+    }
+
+    $cache_dir = $base_path . DIRECTORY_SEPARATOR . $cache_path . DIRECTORY_SEPARATOR . $date_stamp . DIRECTORY_SEPARATOR;
     $thumb_name = $cache_dir . $format;
- 
+
     # generate thumbnail
     require_once('thumbnail_lib.php');
@@ -1347,5 +1353,5 @@
     $thumb_h = $height;
     $dest;
-    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $scale, $thumb_name)) {
+    if (!$thumb->get_thumbnail($dest, $thumb_w, $thumb_h, $asset['asset_id'], $scale, $thumb_name)) {
         return '';
     }
@@ -1353,9 +1359,11 @@
     # make url
     $basename = basename($dest);
-    $site_url = $blog['blog_site_url'];
-    if (!preg_match('!/$!', $site_url))
-        $site_url .= '/';
-
-    $thumb_url = $site_url . $cache_path . '/' . $date_stamp . '/' . $basename;
+    $base_url = $blog['blog_site_url'];
+    if (preg_match('/^%a/', $asset['asset_file_path']) && !empty($blog['blog_archive_url']))
+        $base_url = $blog['blog_archive_url'];
+    if (!preg_match('!/$!', $base_url))
+        $base_url .= '/';
+
+    $thumb_url = $base_url . $cache_path . '/' . $date_stamp . '/' . $basename;
 
     return array($thumb_url, $thumb_w, $thumb_h, $thumb_name);
