PDA

View Full Version : help me to simplify my Quick Style Chooser mod


JonZ
04-12-2009, 01:32 AM
vB Version: 3.7.3
Here's a summary of what I am trying to achieve:

I am trying to make the Quick Style Chooser (QSC) to actually record the theme into the users profile. What is the Quick Style Chooser? it the drop down list that appear at the bottom of vB pages, which allow you to change your theme design on the fly. You can actually see an example of it at http://www.vbulletin.com/forum

http://img13.imageshack.us/img13/2184/vbulletin.th.jpg (http://img13.imageshack.us/my.php?image=vbulletin.jpg)

What this chooser don't do however is it doesn't record the theme into the users profile and just query the actual webpage to adjust the choosen theme based on cookies, which is fine for anonymous users.

__________________________________________________ ____________________

What I did:

So I took the form in users Control Panel as a template, and take portions of code that would submit a selection the information based their values. On my HTML file, I put this html segment:


<form action="/board/myprofile.php" method="post" >
<input type="hidden" name="s" value="index.php" /> <!-- same path location where this form is executed -->
<input type="hidden" name="do" value="updateoptions" />
<input type="hidden" name="securitytoken" value="<?=$vbulletin->userinfo['securitytoken']?>" />
<select class="forumselector" name="newstyleset" id="sel_newstyleset" onchange="this.form.submit()";>
<?=$quickchooserbits?>
</select>
</form>


notice that I replaced the original onchange="switch_id( this, 'style')" by "this.form.submit()". So when a selection is made, the script automatically do a submit action and bring the user to myprofile.php.


__________________________________________________ __________________

My problems:

I don't have a great understanding in PHP, but I can solve many issues by improvisations.
Here what I did is I torn appart the original profile.php where the information of a submition was treated in the user CP.


<?php
// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT', 'myprofile');
define('CSRF_PROTECTION', true);

// ######################### REQUIRE BACK-END ############################
require_once('./global.php');

// ############################### start update options ###############################
if ($_POST['do'] == 'updateoptions')
{
$web = $_POST['s'];
if ($show['member']){

$vbulletin->input->clean_array_gpc('p', array('newstyleset' => TYPE_INT));

$userdata =& datamanager_init('User', $vbulletin, ERRTYPE_STANDARD);
$userdata->set_existing($vbulletin->userinfo);

// style set
if ($vbulletin->options['allowchangestyles'] AND $vbulletin->userinfo['realstyleid'] != $vbulletin->GPC['newstyleset'])
{
$userdata->set('styleid', $vbulletin->GPC['newstyleset']);
}

($hook = vBulletinHook::fetch_hook('profile_updateoptions') ) ? eval($hook) : false;
$userdata->save();
}
else {
exec_header_redirect($web.'styleid');
}
}

// ################################################## ###########################

exec_header_redirect('$web');

?>


The good news is, it working, almost. If I execute my html, choose an item from the dropdown list, it load myprofile.php, my theme change and the information is also changed in the user's profile (UCP). The bad news is, I haven't find a way to just change the theme based on cookies like the original QSC do for anonymous users, and I'm kinda stuck on this problem.

What the original QSC is doing to change the cookie's mode theme, is to get the value of a selected item, and paste it to the current address (ie: myweb.com/board/?styleid=45). You can try it on every board that support styles, take the web address and paste ?styleid=(styleid number here), you will browse temporarely on another theme. Where that query is being treated in the php files, I don't know.

The other problem, is the simplicity. I would like to not have an independent file (myprofile.php) to treat the transaction but in the html itself, just like the original QSC do. This way I could re-use the code on each new module without querying myprofile.php everytime, maybe in a form of a custom function. What I liked on the original QSC is I could just reuse the code in 3 lines, and that was it. This new one take 5-6 html lines, an independent file, and support codes for every page of my sites.

Also the problem with this code, is I have to specify a path with exec_header_redirect() function, when I just want to reload the same page I was before. Not very practical when I want to re-use this function on other pages.


____________________________


This is where I am stuck. I really hope you will help me to resolve this.
Please do many suggestions you can.


Thanks advance.

PS/ The QSC on this very board is working exactly the way I want. It change on the fly, AND it is saved in the user's profile. You can verify it yourself. Choose a color, log out, then log back in, the style you have choosen still apply.

Dismounted
04-12-2009, 04:13 AM
The bad news is, I haven't find a way to just change the theme based on cookies like the original QSC do for anonymous users, and I'm kinda stuck on this problem.
Use a template conditional, switching to the JS version if the viewer is a guest.
The other problem, is the simplicity. I would like to not have an independent file (myprofile.php) to treat the transaction but in the html itself, just like the original QSC do.
You must write to the database to change the Style ID for the user, and this cannot be done in JS. (The data manager call writes to the database.) You may be able to piggyback on profile.php's action, but I haven't looked into that myself.

JonZ
04-12-2009, 12:12 PM
You must write to the database to change the Style ID for the user, and this cannot be done in JS. (The data manager call writes to the database.) You may be able to piggyback on profile.php's action, but I haven't looked into that myself.

When you look on vb.org QSF, it transacting with misc.php, save the setting in the users profile and get you back to the page you were on. Is that what you mean by piggyback?

My php knowledge is pretty limited, I'm was not able so far to create a condition to switch user's theme if they are guests. I don't know what is the processing behind it (?styleid=x?).

And I don't know how to make the query to land back the user to the same page he was browsing before.

any more suggestions?

thanks

dstruct2k
04-12-2009, 07:04 PM
Try something similar to: <if condition="$bbuserinfo[usergroup] == '1'">
<!-- Original code for people not signed in -->
<else />
<!-- Your new code for people logged in -->
</if>

That conditional says "If the user isn't logged in, use old code, else use new code" and should give the desired effect.

JonZ
04-12-2009, 07:26 PM
..

I'm not working in plugin mode, I'm working straight in html/php. Besides, it basically my condition on my code above (maybe just not the proper $variable used), I just don't know how to make a code that will switch anonymous user's style.

dstruct2k
04-13-2009, 03:30 AM
Plugin? That code should be placed inside the template. <if> conditionals are evaluated by the template engine. Just replace the standard quick chooser wherever it appears in the footer template with the more elaborate version I posted.

JonZ
04-13-2009, 03:59 AM
The reason why I am doing it outside of plugin mode or template for vB is I am doing this for outside vB, like portals. If I have to do your if code, I will have to do a pluggin to support the new chooser outside vB.

Dismounted
04-13-2009, 04:46 AM
When you look on vb.org QSF, it transacting with misc.php, save the setting in the users profile and get you back to the page you were on. Is that what you mean by piggyback?
The one used here uses a custom plugin attached to misc.php to run the code to change a user's style (similar to the one you posted).
And I don't know how to make the query to land back the user to the same page he was browsing before.
Use the referrer.
The reason why I am doing it outside of plugin mode or template for vB is I am doing this for outside vB, like portals. If I have to do your if code, I will have to do a pluggin to support the new chooser outside vB.
If you're creating a portal, aren't you include global.php or the like (to get user info and security tokens)?

JonZ
04-13-2009, 05:01 AM
The one used here uses a custom plugin attached to misc.php to run the code to change a user's style (similar to the one you posted).

So you mean that you took like the portion of the code from profile.php and integrate it in misc.php? How do you bounce back the users where they were browsing?


Use the referrer.
can you be more specific? what does that mean.

If you're creating a portal, aren't you include global.php or the like (to get user info and security tokens)?
Yes, I did. Still, I'm not going to edit my external files in ACP. It a personal preference and it more friendly for the others webmasters who is going to maintain the external files only without accessing the ACP (keeping it safe).

Dismounted
04-13-2009, 06:40 AM
can you be more specific? what does that mean.
http://en.wikipedia.org/wiki/HTTP_referrer

JonZ
04-13-2009, 07:04 AM
http://en.wikipedia.org/wiki/HTTP_referrer

....

How about a link on how to do it in PHP?

Dismounted
04-13-2009, 07:31 AM
I'll take it you didn't bother to search for it yourself and expect it handed on a silver platter? (You would have found the answer very quickly.)

http://www.php.net/manual/en/reserved.variables.server.php

JonZ
04-13-2009, 08:58 AM
Sorry but don't you think to be more explicit when you talk in terminology or just leave some random links? I'm not asking to write the code for me here. Otherwise I just keep bumping this thread asking what you mean, I don't think it is productive for neither of us.

I did not understand first what the HTTP referrer was about in your wiki, it only explain what the terminology mean, keeping me more confused on what you were refering to until i saw your HTTP_referer array which is where I started to understand and do more research on this.

Let me develop on this, and I'll post back my findings. *EDIT* The server I am working on doesn't allow HTTP_referer. Is there another way of getting it?

in the meanwhile, I look for the DB formula to change the style for the non-members who are switching themes in my IF condition. Any ideas how? (is it something like this? $vbulletin->userinfo['styleid'])

Mark.B
04-13-2009, 09:32 AM
I've been having a play around with this to see if I can make it do what you want, but at the moment my stumbling block is that I cannot get your original code to actually change the style...until that works, there isn't much point me tweaking the rest to make it do as you describe.

It seems to call the file ok as it redirects to ../index.php afterwards, but it's not actually changing the style.

JonZ
04-13-2009, 02:00 PM
I've been having a play around with this to see if I can make it do what you want, but at the moment my stumbling block is that I cannot get your original code to actually change the style...until that works, there isn't much point me tweaking the rest to make it do as you describe.

It seems to call the file ok as it redirects to ../index.php afterwards, but it's not actually changing the style.



I have this behavior too when I'm not logged to the forum, were you logged on when you tried it? I haven't found the formula for guests to update the style yet.

Mark.B
04-13-2009, 02:14 PM
Yes, I was logged on. It just didn't change anything. Not sure what's wrong though.

JonZ
04-13-2009, 02:44 PM
Hmm, that's mean that the condition of the if is not TRUE and exit it, or the condition inside just don't work. One way to know it is to comment out the exec_header_redirect(); and write echo ('this condition is true'); under $userdata->save(); If the blank page is white, the first if ($show['member'])) is not TRUE.

If it echoing, try the other if above, move the echo under $userdata->set('styleid', $vbulletin->GPC['newstyleset']);. If it still not echoing then try it on else{}.

Also, which version of vB please you have?

Mark.B
04-13-2009, 05:28 PM
I got it...I had got it sat next to the orginal method but that sits a form inside another form...think I have it working now. Just going to play about with conditionals....

JonZ
04-13-2009, 05:39 PM
Are you sure myprofile.php is on the same location of your html portion? It not supposed to load anywhere else than myprofile.php.

Oh and make sure your html portion reside inside of a .php file (ie index.php), otherwise your browser would not interpret the php commands.

replace the securitytoken value by this: <?=$vbulletin->userinfo['securitytoken']?> ( my original token value was specified elsewhere).

if you commented out or removed the exec_header_redirect(); (there's both 2 of them) the webpage would stop loading and leave a blank page.

EDIT: oh, I got your edit message. Glad you got it working.

Mark.B
04-13-2009, 06:08 PM
Yes, working now. However the bit I'm struggling with is the same bit you were struggling with, but for different reasons.

I'm having trouble making the conditional work due to having to mess about with the location of the <form> tags...I shall keep playing with this and come back to it at some point.

--------------- Added 1239651438 at 1239651438 ---------------

What would also be useful is if there is a URL equivalent to that form...eg:
myprofile.php?do=updateoptions&newstyleset=xxx
That would allow clickable links to styles (which currently just set a cookie like the normal stylechooser) to alter the user's settings too.

JonZ
04-13-2009, 07:02 PM
that ?styleid query is captured somewhere in the requested files, I have no idea which one is treating the '?styleid' since there's so many things happening in global.php. It would be logical to think it somewhere in global.php since it requested in all files, but by my experiments, it may not be in this file. If I can identify where the query is treated I could extract the formula where the theme change and refresh in the browser.



Meanwhile, I found an exchange solution for my redirect URL problem. In my form I replaced the "s" value:

<input type="hidden" name="s" value="index.php" />


This way I have control of the user redirection.

in myprofile.php, I replaced the exec_header_redirect(); with this:

exec_header_redirect($_POST['s']);


This way, myprofile.php is recyclable in any forms I put with the "s" id.


I'm going to re-edit my original code on post #1 on this effect.

Mark.B
04-13-2009, 07:13 PM
AH yes...I did the redirect by changing the redirect line to this:
exec_header_redirect($_SERVER['HTTP_REFERER']);
That sends you back to the page you were already on, thus replicating almost exactly the behaviour of the default style chooser.

If anybody knows how to pull the URL out of this please let me know. :D

JonZ
04-13-2009, 07:22 PM
AH yes...I did the redirect by changing the redirect line to this:
exec_header_redirect($_SERVER['HTTP_REFERER']);
That sends you back to the page you were already on, thus replicating almost exactly the behaviour of the default style chooser.

If anybody knows how to pull the URL out of this please let me know. :D

You can just echo it echo($_SERVER['HTTP_REFERER']);


Anyway I tried that already by the suggestion of Dismounted. Unfortunately, my server block this HTTP_REFERER requests and it not reliable on some browsers (IE from what I've read). Hence the workaround I've found on post #21.

Dismounted
04-14-2009, 04:01 AM
Unfortunately, my server block this HTTP_REFERER requests and it not reliable on some browsers (IE from what I've read).
Why does your server do this - there is no need to. It is not "reliable" in IE when users decide to ramp up the "security" level in IE options, which also breaks many other things (not only in vBulletin), so you may as well use it. By the way, vBulletin already defines an easy variable to fetch the referrer, $vbulletin->url.
Hence the workaround I've found on post #21.
Whatever you do, don't use s. It is used by vBulletin to determine a user's session hash (if cookies are disabled).

JonZ
04-14-2009, 04:15 AM
Why does your server do this.

Firewalls blocks it.

Mark.B
10-31-2010, 02:03 AM
An old thread...but...does anyone know what changes would have to be made to myprofile.php to get this to work in vB4? Got the template side done but the form just reloads index.php.