Encoding in AJAX

I install a Zenphoto on my server. And I saw that in themes don't have a encoding declaration:
``
(my server send all pages in `ISO-8859-2`). Polish chars are broken.

And I saw that Ajax script to add a titles and description save a polish chars (`?, ?, ?, ?, ?, ó, ?, ?...`) bad.
Example:
?a?ó?? ?ó?t? ja??

changing to:
Za%u017Có%u0142%u0107 %u017Có%u0142t%u0105 ja%u017A%u0144

Comments

  • i've sent patches to fix this to tristan some time ago but still no reply..
  • pronvit: write it in comment. :)
  • add to beginning of template-functions.php
    `

    /**

    * Function converts an Javascript escaped string back into a string with specified charset (default is UTF-8).

    * Modified function from http://pure-essence.net/stuff/code/utf8RawUrlDecode.phps

    *

    * @param string $source escaped with Javascript's escape() function

    * @param string $iconv_to destination character set will be used as second paramether in the iconv function. Default is UTF-8.

    * @return string

    */

    function unescape($source, $iconv_to = 'UTF-8') {

    $decodedStr = '';

    $pos = 0;

    $len = strlen ($source);

    while ($pos < $len) {

    $charAt = substr ($source, $pos, 1);

    if ($charAt == '%') {

    $pos++;

    $charAt = substr ($source, $pos, 1);

    if ($charAt == 'u') {

    // we got a unicode character

    $pos++;

    $unicodeHexVal = substr ($source, $pos, 4);

    $unicode = hexdec ($unicodeHexVal);

    $decodedStr .= code2utf($unicode);

    $pos += 4;

    }

    else {

    // we have an escaped ascii character

    $hexVal = substr ($source, $pos, 2);

    $decodedStr .= chr (hexdec ($hexVal));

    $pos += 2;

    }

    }

    else {

    $decodedStr .= $charAt;

    $pos++;

    }

    }

    if ($iconv_to != "UTF-8") {

    $decodedStr = iconv("UTF-8", $iconv_to, $decodedStr);

    }

    return $decodedStr;

    }

    /**

    * Function coverts number of utf char into that character.

    * Function taken from: http://sk2.php.net/manual/en/function.utf8-encode.php#49336

    *

    * @param int $num

    * @return utf8char

    */

    function code2utf($num){

    if($num<128)return chr($num);<br />
    if($num<2048)return chr(($num>>6)+192).chr(($num&63)+128);

    if($num<65536)return chr(($num>>12)+224).chr((($num>>6)&63)+128).chr(($num&63)+128);

    if($num<2097152)return chr(($num>>18)+240).chr((($num>>12)&63)+128).chr((($num>>6)&63)+128) .chr(($num&63)+128);

    return '';

    }

    `

    then change stripslashes($newtitle) to stripslashes(unescape($newtitle))
    and stripslashes($newdesc) to stripslashes(unescape($newdesc)).

    and don't forget to add meta content-type = utf-8

  • Thanks. But one polish letter (ó) doesn't work now fine.
  • trisweb Administrator
    I'm sure there are better ways to do this. I have pronvit's mods, but I'll look into better encoding options.

    Thanks for the report.
  • why this way isn't good enough?
  • trisweb Administrator
    It might be, I just always look for the best way... is there something wrong with that? ;-)

    It probably is fine. You're welcome to hack ZP to use it if need be.
  • nothing wrong.
    but I think that these encoding problems are quite critical for non-english users. and why to search for something more if solution is available
  • encoding problems are quite critical for non-english users
    It's a very big problem.
  • Easy solution for non-english users.

    add
    header("Content-type: text/html; charset=utf-8");
    into zp/index.php in the first line to override the default charset.
    in Sajax.php, replace "escape" with "encodeURIComponent".
    It works very well for Chinese characters.
    http://blog.dukechina.org/gallery/
  • WeiChen: thanks :)
  • I think, that this line:
    header("Content-type: text/html; charset=utf-8");
    is good thing to adding into admin.php
  • Shouldn't a simple htmlentities() PHP function change any special character to a valid html entity? I mean it converts all applicable characters to HTML entities. http://fi.php.net/htmlentities . I just don't know where to put this so that it would convert the text to html entities BEFORE it's saved to the database. I need Finnish/Scandinavian characters and they're coming out as just blocks or question marks. Help, please. Where could I put this?
  • hdort Member
    WeiChen: seems to work ok for German, too! Many thanks!

    Perhaps this change should be added in the next version?
  • Oh, but this doesn't work for Russian language... shit =)
  • Mania Member
    Spacemonkey: I used WeiChen method but with a charset ISO-8859-15 to make Finnish letters work.

    However, if the image has Finnish letters, it won't display properly. :(
  • adas Member
    In latest version this bug is not resolved...
  • trisweb Administrator
    It is now! Version to be released today with support for any desired character-set, including UTF-8 by default.
  • It still shows all scandinavian letters (ä,Ã¥,ö) as question marks in File Info and Tags. Does anyone know what to do for this?
  • acrylian Administrator, Developer
    Generally zenphoto should display them correct, but if your filenames included ÖÄÜ then your host's server must support that (I am german and all the hosts I know here don't!). I recommend uploading images without any special characters and rename the images in the admin if those characters are needed. That makes the live easier..:-)
    And of course your MySQL-Database must have the right encoding too.
  • If I have scandinavian letters in my filename, everything works out great. But if I have scandinavian letters in file-info (description) or keywords (tags), they are shown as question marks.

    It's not because of my server provider, and it's propably not because of mysql either, though I don't understand how I could change mysql to utf-8 (or to anything).

    The idea of changing all question marks in admin is not an option, because in Finnish language ä:s and ö:s are almoust as common as i or e in English, and there is usually a lot of text in fileinfos.

    Anyway, thanks for one of the greatest software ever!
  • acrylian Administrator, Developer
    Ok, then I got you wrong, of course changing that manually is not an option. I supposed you have the 1.1.2 release running? Have you maybe altered a theme and maybe by accident saved the files with the wrong encoding? I did this mistake myself several times...If that is so all utf-8 encoding within zp, your server etc doesn't help. Please check this. If that is not the case, we might have really a problem...
  • I have version 1.1.2. I checked zp-core and default-theme directory. There was some files which wasnt utf-8, but after I had saved them as utf-8 file-infos and keywords were still question marks. So there is still a problem to solve.
  • I had a lot of utf-8 problems too. Here is what i do to avoid it:

    1. set utf-8 to false.
    2. refresh the zenphoto-database & metadata
    3. set utf-8 to true again.

    This works for me.
Sign In or Register to comment.