News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

Javascript / cookie

Started by HunterP, January 27, 2015, 05:31:06 PM

Previous topic - Next topic

HunterP


Ok, I've been away for quite a while and trying to find some things out (again).

echo '
<script type="text/javascript"><!-- // --><![CDATA[
var oMainHeaderToggle = new smc_Toggle({
bToggleEnabled: true,
bCurrentlyCollapsed: ', empty($options['collapse_header']) ? 'false' : 'true', ',
aSwappableContainers: [
\'upper_section\'
],
aSwapImages: [
{
sId: \'upshrink\',
srcExpanded: smf_images_url + \'/upshrink.png\',
altExpanded: ', JavaScriptEscape($txt['upshrink_description']), ',
srcCollapsed: smf_images_url + \'/upshrink2.png\',
altCollapsed: ', JavaScriptEscape($txt['upshrink_description']), '
}
],
oThemeOptions: {
bUseThemeSettings: ', $context['user']['is_guest'] ? 'false' : 'true', ',
sOptionName: \'collapse_header\',
sSessionVar: ', JavaScriptEscape($context['session_var']), ',
sSessionId: ', JavaScriptEscape($context['session_id']), '
},
oCookieOptions: {
bUseCookie: ', $context['user']['is_guest'] ? 'true' : 'false', ',
sCookieName: \'upshrink\'
}
});
// ]]></script>';


A while ago I used this script to set a cookie somewhere else. I think I found out that this value was stored in the DB somewhere, but I can't figure out where. One of the last lines says : bUseCookie: ', $context['user']['is_guest'] ? 'true' : 'false'.

Anyway, I'm trying to set a variable which is not a boolean, so it's not a toggle, but it's a (short) text string.

Basically, I'm trying to extend the Nchat modification into more than just one room. This mod works with AJAX and want the user to be able to select a room via a dropdown box and this setting needs to be saved somewhere. Preferably serverside, but maybe I'm thinking to difficult. Either the server or the client needs to know which room is selected to read/write to the correct corresponding text files on the server.

Arantor

Well, are you saving it for guests or not? If you're not saving for guests, you don't use cookies.

There are myriad ways to accomplish this but the first question really is 'for guests too?'.

HunterP


I was expecting you to reply :) In basic, the mod allows guests, but on my forum the chatbox is only available for members. So if that makes it a bit easier; no guests :)

Arantor

That makes it a ton easier.

I don't actually know how Nchat works but presumably it has a table where the messages are stored. Presumably you would store a channel id in that table as well, and then store that channel id in the members table. Easiest way to do that is to just add the column and then use updateMemberData server side to store it.

As for the dropdown part, you'll have to have some kind of form handler on the server to receive it, check it isn't a guest, check it's a valid channel, update the members table and presumably reload the new chat.

It's a seriously complex thing to build :(

HunterP


Well, the mod doesn't use the DB at all. It stores the messages in a text file :(

Arantor

That will make things *very* complicated :( Quickest route: multiple text files. But that's still a lot of faff :(

HunterP

Quote from: Arantor on January 27, 2015, 06:19:23 PM
That will make things *very* complicated :( Quickest route: multiple text files. But that's still a lot of faff :(

For the different rooms; yes. That would be the best solution. But the problem at this point is how to switch rooms and how to save the setting. The Nchat is running in javascript on the client with no connection to the DB. However, I managed to copy/edit the JS which I started this topic with to collapse/expand the chat, for users who are not interested. this works and as I said, I'm not quite sure where this is saved. I'm checking for empty($options['collapse_nchatbox']) and that works. So this script seems to be saving something, somewhere. That's where I like to know if I can save the room as a short text string (like "General") the same way...?

Arantor

No, trust me, a much larger problem is creating the room in the first place.

I'm not sure you want to put it in $options, the code you have will do precisely nothing for you about putting anything into $options either...

HunterP


Ok, so what does this code do? The setting seems to get stored somewhere...

Arantor

That passes it to smc_Toggle to do it, which invokes smf_setThemeOption. Which is what $options is built out of, but is different per theme and comes with absolutely no sanitisation on your part so you can't verify that the channel is legitimate.

HunterP


Ok, found it. It seems to be stored in smf_themes. How does this interaction between the script and the DB work? Since the script runs on the client and the DB on the server :) If I'm able to store text strings the same way, I could store the room's name for each user. If not present, the user has never switched before and the default room will be used.

Arantor

smf_setThemeOption calls magic on the server to change the DB. Without sanitisation. Meaning rooms users wouldn't have access to could be granted access with a single line of JavaScript that anyone can run with barely 5 seconds' effort in their browser.

HunterP


Ok thanks, so that isn't the best approach :(

HunterP

Quote from: HunterP on January 27, 2015, 06:47:18 PM
Ok thanks, so that isn't the best approach :(

On the other hand, I was planning to have some general rooms for all users and just one for the moderators. This last one will only be available in the moderators dropdown and to prevent hacking, I could run an additional test before actually accessing the room (is this user actually a moderator?).

Would this be a convenient solution?

HunterP


Think I've got everything working now, the JS seems to be calling an URL which eventually stores the setting. I'm using this line to set the value from JS :

smf_setThemeOption(\'nchat_room\', document.getElementById(\'nchat_room\').value, null, ', JavaScriptEscape($context['session_id']), ', ', JavaScriptEscape($context['session_var']), ', null);

This works and the creating/switching between the rooms wasn't difficult at all, they are just separate text files.

Advertisement: