Simple Machines Community Forum

Customizing SMF => SMF Coding Discussion => Aiheen aloitti: ricoud - toukokuu 31, 2014, 02:55:34 AP

Otsikko: char encoding problem when using ssi
Kirjoitti: ricoud - toukokuu 31, 2014, 02:55:34 AP
Hello,
I installed smf 2.0.6 using utf8 encoding. My database is also utf8 encoded. When I send pm through the forum everything works well, accents are well displayed.
I have an application developed with CodeIgniter that is link to the smf forum through SSI. The application uses the same database than smf and I use netbeans 8.0 with utf8 file encoding.
So everything looks find but when I send a pm through the CI app the accents are not well encoded (ex: La tournée). Another problem, if a username content an accent I need to convert it to utf8, ex: utf8_encode($smf->get_context()['user']['username']), in order to have it well displayed on CI side.
It looks like there is a problem of encoding when passing between smf and CI but i can't figure out what's going wrong.
Thanks in advance for your help.
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: Arantor - toukokuu 31, 2014, 06:25:05 AP
Of course there is. SMF knows it's using UTF-8 but CI doesn't know or care, meaning that you will have to set up CI to use UTF-8 before passing anything to SMF functions.

Just because the files are encoded in UTF-8 does not somehow magically make everything else UTF-8 friendly. Your CI app should probably be indicating to users that it's UTF-8, <form> tags should generally have an accept-charset attribute indicating UTF-8 and all data needs to be handled as UTF-8, but it's not for the faint of heart.
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: ricoud - toukokuu 31, 2014, 07:57:25 AP
Thanks,
I fully understand that my CI app should handle utf8.
I already tell that to CI through the different config files :
config.php
/*
|--------------------------------------------------------------------------
| Default Character Set
|--------------------------------------------------------------------------
|
| This determines which character set is used by default in various methods
| that require a character set to be provided.
|
*/
$config['charset'] = 'UTF-8';


database.php
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';


The PM are sent following an action and used database stored information. Here is the used function
    function send_pm($to, $bcc, $subject, $message, $from = NULL) {
        global $smcFunc;
       
        require_once($this->sourcedir . '/Subs-Post.php');
        $CI = get_instance();
        $CI->db->select('ID, french_subject, french_message', FALSE)
            ->from('pm_table')
            ->where('message', $subject);
        $info = $CI->db->get()->result();
        if (count($info) !== 1)
            return FALSE;
        else
            $info = $info[0];
        $CI->db->select('base_table, table_key, key_id, string_order', FALSE)
            ->from('pm_variable_table')
            ->where('id_pm_table', $info->ID)
            ->order_by('string_order', 'ASC');
        $variables = $CI->db->get()->result();
        if (count($variables) === 0)
            return FALSE;
        $messageVar = [];
        $index = 0;
        foreach ($variables as $variable) {
            $CI->db->select($variable->table_key . ' AS var', FALSE)
                ->from($variable->base_table)
                ->where($variable->key_id, $message[$index]);
            $index++;
            $result = $CI->db->get()->result();
            if (count($result) !== 1)
                return FALSE;
            $messageVar[] = $result[0]->var;
        }
        $subject = $smcFunc['htmlspecialchars']($info->french_subject, ENT_QUOTES);
        $message = $smcFunc['htmlspecialchars'](vsprintf($info->french_message, $messageVar), ENT_QUOTES);
        $CI->db->select('id_member, member_name, real_name', FALSE)
                ->from('smf_members')
                ->where('id_member', $from);
        $from = [];
        $result = $CI->db->get()->result();
        if (count($result) !== 1)
            return FALSE;
        $from['id'] = $result[0]->id_member;
        $from['name'] = $result[0]->member_name;
        $from['username'] = $result[0]->real_name;
        return sendpm(array('to' => $to, 'bcc' => $bcc), $subject, $message, false, $from);
    }


Thanks in advance
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: Arantor - toukokuu 31, 2014, 04:01:30 IP
You might tell CI that but you also need to tell the browser... how are you doing that, assuming you are doing that in the first place? (A link to your CI site would help)
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: ricoud - kesäkuu 01, 2014, 05:28:33 AP
Hello,
The site isn't yet published, I'm still working on several things.

The problem should not come from browser as the pm are build with database information, sent through smf sendpm function and then displayed on the smf forum part of the site.

Yesterday I moved the code of my send_pm function inside smf Subs-Post.php function and ... got the same behavior  :-\
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: Arantor - kesäkuu 01, 2014, 11:40:28 AP
In which case it seems like CI isn't using UTF-8...
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: ricoud - kesäkuu 15, 2014, 11:01:25 AP
Hello,
I rewrite the send_pm function inside my CI code and everything works fine.
It looks to me that there is a global variable, telling smf to use UTF_8,  that isn't initialise when calling send_pm through ssi.php. Can someone tell me if there is such global variable so that I can declare it before calling send_pm.
Thanks in advance.
Otsikko: Re: char encoding problem when using ssi
Kirjoitti: Arantor - kesäkuu 15, 2014, 11:43:47 AP
There isn't. SSI.php fires up the guts of SMF including figuring out whether it's UTF-8 or not. I still reckon there's something else wrong but if it works for you, that works too.