HTTP / PHP problem.

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

HTTP / PHP problem.

Post by hgm »

I am posting this here in the hope the lack of response to my attempts for repairing the poll function of the forum are just because most programmers do not read the General forum section.

The problem is the phpBB script viewforum.php, which is responsible for showing a page of postings from a specified discussion in a specified section. Such a page can contain a poll, which is a HTML <form> containing radio button <input> controls for selecting one or more votes, and a 'Submit vote' button to upload that form. Hitting that button triggers a POST request at a URL identical to that for the displayed page, that is, it also invokes the viewtopic.php script on the server. The info on the setting of the radio buttons is in the 'form data' of this POST request.

Now the viewtopic.php script appears to distinguish requests for displaying the page from requests for submitting the vote by means of the (Boolean) value of a variable $update. This is set from the parameters to the request (line 49, $update = $request->variable('update', false);), which defaults to 'false'. So only if there is a name=value pair update=something_not_false the request will be interpreted as a vote submission, and be processed by code starting at line 942.

The problem now is that, in the TalkChess forum, voting attempts do not include such a parameter, neither in the URL nor in the form data. So $upgrade remains at the 'false' default, and sure enough, the voting does not work. The HTML generated for the voting form is also such that you would not expect such a parameter to be passed to the POST request. Now the strange thing is that in a freshly setup phpBB forum the voting does work, despite that everything is the same, and in particular an 'update' parameter is also not passed with the POST request. (Something I checked in the developer panel of the Chrome browser, which does show both the header and the form data.)

I am really at a loss for understanding how this can be possible. Is there anyone who could give me some pointers?
Norabor
Posts: 5
Joined: Wed Jun 24, 2020 1:23 pm
Full name: Meikel Weber

Re: HTTP / PHP problem.

Post by Norabor »

The submit button is also an input control with name and value. Name is "update" and value is something else. It only gets set in the POST if the button is clicked. (At least the template at github looks that way)
Norabor
Posts: 5
Joined: Wed Jun 24, 2020 1:23 pm
Full name: Meikel Weber

Re: HTTP / PHP problem.

Post by Norabor »

In reality it seems a lot more funny. There is a lot of javascript (from core.js) going on for forms that have the ajax-option (and voting has it).
The submit button will recieve a data-clicked='true' value during the js-load phase. And the event handler for submitting a form will serialize the form and explicitly add any submit-button with data-clicked set to the POST data. So the posted data should contain some update=true value. It is harder to debug as it is not easy to generate log-output in the result html for ajax calls through javascript. The script seems to replace the form after vote submission by the current vote results. No full pageload is done.

You can turn-off this ajax-stuff by removing the data-ajax attribute from the form tag. Then it should work the old-fashioned way and do a full page round-trip and maybe allow you to better see the values.
Norabor
Posts: 5
Joined: Wed Jun 24, 2020 1:23 pm
Full name: Meikel Weber

Re: HTTP / PHP problem.

Post by Norabor »

ok. found the problem. The "name.js" calls a function "KludgeInit". This function completely replaces the poll html code and thereby removes any event handlers already attached (especially the event handler attached to the "submit" button). So the "update" value is not properly tagged and thus not any more submitted.

I'm not sure what the "KludgeInit()" is supposed to do. It looks like pretty straight forward JS and the KludgeInit is called by a 1 second timer.

Solutions:
* Rewrite name.js to not totally replace the HTML
* Run KludgeInit immediately before the stuff from ajax.js (and core.js) runs.
* Re-Attach the click event-handler to the submit button (core.js line 437)
* Simply add an <input type="hidden" name="update" value="true"> to the form in the templates
* remove the data-ajax tag from the vote-form (without AJAX it should work)

I don't think that "POST" detection is a good indicator for a poll submit. Who knows how many POST calls go through PHPBB codebase that are not vote submissions.
pedrojdm2021
Posts: 157
Joined: Fri Apr 30, 2021 7:19 am
Full name: Pedro Duran

Re: HTTP / PHP problem.

Post by pedrojdm2021 »

What version of phpBB are you using? i think you sould try the new version,
these new updates usually fixes stuff like this.

you should never lost any information in the database or anything like that (you can always test the changes in a localhost to be sure that everything gets updated correctly and everything is still running fine)

you can see the update guide here:
https://www.phpbb.com/downloads/3.3/patch
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: HTTP / PHP problem.

Post by hgm »

Norabor wrote: Fri Sep 17, 2021 12:55 am ok. found the problem. The "name.js" calls a function "KludgeInit". This function completely replaces the poll html code and thereby removes any event handlers already attached (especially the event handler attached to the "submit" button). So the "update" value is not properly tagged and thus not any more submitted.

I'm not sure what the "KludgeInit()" is supposed to do. It looks like pretty straight forward JS and the KludgeInit is called by a 1 second timer.

Solutions:
* Rewrite name.js to not totally replace the HTML
* Run KludgeInit immediately before the stuff from ajax.js (and core.js) runs.
* Re-Attach the click event-handler to the submit button (core.js line 437)
* Simply add an <input type="hidden" name="update" value="true"> to the form in the templates
* remove the data-ajax tag from the vote-form (without AJAX it should work)

I don't think that "POST" detection is a good indicator for a poll submit. Who knows how many POST calls go through PHPBB codebase that are not vote submissions.
You are a genius! I would have never thought of that. And you are spot on; when I remove the JavaScript the voting works again.

This means it was I who broke the polling, as name.js was written by me. :oops: Its purpose was to substitute diagrams for [ d] and [ pgn] tags in postings from before the upgrade to phpBB 3. So I guess I will do that in a more careful way. It was only supposed to process posting contents, (class = "content"), but apparently the poll form is also in a <div> of that class. I will fix it by only actually making the substitution if the processed innerHTML gets changed. (Which should never be the case in the poll form.)
Norabor
Posts: 5
Joined: Wed Jun 24, 2020 1:23 pm
Full name: Meikel Weber

Re: HTTP / PHP problem.

Post by Norabor »

It should be perfectly ok to run the skript just before core.js (without the timer). This way all html is already loaded and can be modified by the script. And once all modifications for diagrams and pgn is done, phpbb can do its stuff without further interference. No rewrite needed for that.
User avatar
hgm
Posts: 27788
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: HTTP / PHP problem.

Post by hgm »

It never worked for me to run these kind of scripts without delay. They appeared to run before the complete page was loaded, so that the getElementsByClassName method did not see all elements of that class, and the later occurrences were not processed. The problem is that I cannot place JavaScript just anywhere I want on the page. I tried to place it in a footer, but even with HTML switched on phpBB only accepts some HTML tags, and <script> was not one of those. I could use unrestricted HTML in the replacement text for custom BBcodes, though. But such codes could not be used in the footer. But the 'forum rules' box appears on every page, when it is non-empty, and BBcodes could be used there. So this was the solution I arrived at. But the forum-rules box is at the top of the page, not at the bottom. I Experimented with 'onload' to prevent that, but then they did not seem to run at all.

I am surprised the voting form needs a custom handler for submission. I would have thought that simply adding an extra &update=true to the 'action' URL of the form would be a more natural solution. As you mentioned, testing for the method being POST is a bit risky, as other submissions might use that method too. Although I am not sure there are any other submissions that would use viewtopic.php to be processed. I can imagine that the radio buttons need non-standard handling if multiple options can be voted, though. So it is better not to interfere with the way things were designed to work.

Anyway, the fix to the name.js to avoid touching the voting box was totaly trivial. I just kept a copy of the enitial content by "var oldt = t;", and made the actual substitution dependent on the content being changed ("if(t != oldt) list[ i].innerHTML = t;"). That did the trick; the polls are now working again. Thanks to you! 8-)
Norabor
Posts: 5
Joined: Wed Jun 24, 2020 1:23 pm
Full name: Meikel Weber

Re: HTTP / PHP problem.

Post by Norabor »

You‘re welcome. I‘m happy that after years of only reading, I can actually contribute something useful :D