From 6a51b7f0cd8900cf5bd848088f136600c4a98d4e Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 16 Nov 2012 13:08:12 -0600
Subject: [PATCH] Tweak JSON attachment parsing for API

    * Don't require a content-type, so data:,this is content
      is perfectly allowable. Such content is assumed text/plain
    * Support the charset hint, so
      data:text/plain;charset=iso-8859-1,content here
      will be translated to UTF-8 if the php iconv() function exists.
      Otherwise, content is left intact and assumed by the database to by
      UTF-8 already
---
 include/class.api.php | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/include/class.api.php b/include/class.api.php
index 1bd254634..8ad7ceb2d 100644
--- a/include/class.api.php
+++ b/include/class.api.php
@@ -255,7 +255,7 @@ class ApiJsonDataParser extends JsonDataParser {
                     # PHP5: fopen("data://$data[5:]");
                     if (substr($data, 0, 5) != "data:") {
                         $info = array(
-                            "data" => $data, 
+                            "data" => $data,
                             "type" => "text/plain",
                             "name" => key($info));
                     } else {
@@ -264,11 +264,17 @@ class ApiJsonDataParser extends JsonDataParser {
                         list($type, $extra) = explode(";", $meta);
                         $info = array(
                             "data" => $contents,
-                            "type" => $type,
+                            "type" => ($type) ? $type : "text/plain",
                             "name" => key($info));
                         if (substr($extra, -6) == "base64")
                             $info["encoding"] = "base64";
-                        # TODO: Handle 'charset' hint in $extra
+                        # Handle 'charset' hint in $extra, such as
+                        # data:text/plain;charset=iso-8859-1,Blah
+                        # Convert to utf-8 since it's the encoding scheme
+                        # for the database. Otherwise, assume utf-8
+                        list($param,$charset) = explode('=', $extra);
+                        if ($param == 'charset' && function_exists('iconv'))
+                            $contents = iconv($charset, "UTF-8", $contents);
                     }
                 }
                 unset($value);
-- 
GitLab