Fixed usernames with apostrophes
authorAlexander Ebert <ebert@woltlab.com>
Mon, 15 Apr 2013 12:15:20 +0000 (14:15 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 15 Apr 2013 12:15:20 +0000 (14:15 +0200)
wcfsetup/install/files/lib/system/template/plugin/EncodeJSONModifierTemplatePlugin.class.php [new file with mode: 0644]
wcfsetup/install/files/lib/util/StringUtil.class.php

diff --git a/wcfsetup/install/files/lib/system/template/plugin/EncodeJSONModifierTemplatePlugin.class.php b/wcfsetup/install/files/lib/system/template/plugin/EncodeJSONModifierTemplatePlugin.class.php
new file mode 100644 (file)
index 0000000..1ceaee0
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+namespace wcf\system\template\plugin;
+use wcf\system\template\TemplateEngine;
+use wcf\util\StringUtil;
+
+/**
+ * Template modifier plugin which formats a JSON string for usage in a single quoted
+ * javascript string by escapes single quotes and new lines.
+ * 
+ * Usage:
+ *     {$string|encodeJSON}
+ *     {"bl''ah"|encodeJSON}
+ * 
+ * @author     Alexander Ebert
+ * @copyright  2001-2013 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system.template.plugin
+ * @category   Community Framework
+ */
+class EncodeJSONModifierTemplatePlugin implements IModifierTemplatePlugin {
+       /**
+        * @see wcf\system\template\IModifierTemplatePlugin::execute()
+        */
+       public function execute($tagArgs, TemplateEngine $tplObj) {
+               return StringUtil::encodeJSON($tagArgs[0]);
+       }
+}
index 90f7b4a546cd186567b9d155ce5f7f8db13fb1f3..6e4198861ad117986ddcb263b4f74d777681faaf 100644 (file)
@@ -106,6 +106,21 @@ final class StringUtil {
                return $string;
        }
        
+       /**
+        * Encodes JSON strings. This is not the same as PHP's json_encode()!
+        * 
+        * @param       string          $string
+        * @return      string
+        */
+       public static function encodeJSON($string) {
+               $string = self::encodeJS($string);
+               
+               // single quotes must be encoded as HTML entity
+               $string = self::replace("\'", "&#39;", $string);
+               
+               return $string;
+       }
+       
        /**
         * Decodes html entities.
         *